xref: /aosp_15_r20/system/apex/apexd/apexd_metrics.h (revision 33f3758387333dbd2962d7edbd98681940d895da)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <map>
20 #include <memory>
21 #include <span>
22 #include <string>
23 #include <vector>
24 
25 #include "apex_constants.h"
26 
27 namespace android::apex {
28 
29 class ApexFile;
30 class ApexSession;
31 
32 enum class InstallType {
33   Staged,
34   NonStaged,
35 };
36 
37 enum class InstallResult {
38   Success,
39   Failure,
40 };
41 
42 class Metrics {
43  public:
44   struct ApexFileInfo {
45     std::string name;
46     int64_t version;
47     bool shared_libs;
48     int64_t file_size;
49     std::string file_hash;
50     ApexPartition partition;
51     std::vector<std::string> hals;
52   };
53 
54   virtual ~Metrics() = default;
55   virtual void SendInstallationRequested(InstallType install_type,
56                                          bool is_rollback,
57                                          const ApexFileInfo& info) = 0;
58   virtual void SendInstallationEnded(const std::string& file_hash,
59                                      InstallResult result) = 0;
60 };
61 
62 std::unique_ptr<Metrics> InitMetrics(std::unique_ptr<Metrics> metrics);
63 
64 void SendSessionApexInstallationEndedAtom(const ApexSession& session,
65                                           InstallResult install_result);
66 
67 // Helper class to send "installation_requested" event. Events are
68 // sent in its destructor using Metrics::Send* methods.
69 class InstallRequestedEvent {
70  public:
InstallRequestedEvent(InstallType install_type,bool is_rollback)71   InstallRequestedEvent(InstallType install_type, bool is_rollback)
72       : install_type_(install_type), is_rollback_(is_rollback) {}
73   // Sends the "requested" event.
74   // Sends the "end" event if it's non-staged or failed.
75   ~InstallRequestedEvent();
76 
77   void AddFiles(std::span<const ApexFile> files);
78 
79   // Adds HAL Information for each APEX.
80   // Since the event can contain multiple APEX files, HAL information is
81   // passed as a map of APEX name to a list of HAL names.
82   void AddHals(const std::map<std::string, std::vector<std::string>>& hals);
83 
84   // Marks the current installation request has succeeded.
85   void MarkSucceeded();
86 
87   // Returns file hashes for APEX files added by AddFile()
88   std::vector<std::string> GetFileHashes() const;
89 
90  private:
91   InstallType install_type_;
92   bool is_rollback_;
93   std::vector<Metrics::ApexFileInfo> files_;
94   bool succeeded_ = false;
95 };
96 
97 }  // namespace android::apex
98