1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_EVENT_REPORT_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_EVENT_REPORT_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "appexecfwk_errors.h"
23 #include "bundle_constants.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 enum class BMSEventType {
28     UNKNOW = 0,
29     /***********FAULT EVENT**************/
30     BUNDLE_INSTALL_EXCEPTION,
31     BUNDLE_UNINSTALL_EXCEPTION,
32     BUNDLE_UPDATE_EXCEPTION,
33     PRE_BUNDLE_RECOVER_EXCEPTION,
34     BUNDLE_STATE_CHANGE_EXCEPTION,
35     BUNDLE_CLEAN_CACHE_EXCEPTION,
36     /***********BEHAVIOR EVENT***********/
37     BOOT_SCAN_START,
38     BOOT_SCAN_END,
39     BUNDLE_INSTALL,
40     BUNDLE_UNINSTALL,
41     BUNDLE_UPDATE,
42     PRE_BUNDLE_RECOVER,
43     BUNDLE_STATE_CHANGE,
44     BUNDLE_CLEAN_CACHE,
45     BMS_USER_EVENT,
46     APPLY_QUICK_FIX,
47     CPU_SCENE_ENTRY,
48     AOT_COMPILE_SUMMARY,
49     AOT_COMPILE_RECORD,
50     QUERY_OF_CONTINUE_TYPE,
51     FREE_INSTALL_EVENT,
52     BMS_DISK_SPACE
53 };
54 
55 enum class BundleEventType {
56     UNKNOW = 0,
57     INSTALL,
58     UNINSTALL,
59     UPDATE,
60     RECOVER,
61     QUICK_FIX
62 };
63 
64 enum class InstallScene {
65     NORMAL = 0,
66     BOOT,
67     REBOOT,
68     CREATE_USER,
69     REMOVE_USER,
70 };
71 
72 enum HiSysEventType {
73     FAULT     = 1,    // system fault event
74     STATISTIC = 2,    // system statistic event
75     SECURITY  = 3,    // system security event
76     BEHAVIOR  = 4     // system behavior event
77 };
78 
79 enum class UserEventType {
80     UNKNOW = 0,
81     CREATE_START,
82     CREATE_END,
83     REMOVE_START,
84     REMOVE_END,
85 };
86 
87 struct EventInfo {
88     int32_t userId = Constants::INVALID_USERID;
89     std::string bundleName;
90     std::string moduleName;
91     std::string abilityName;
92     std::string packageName;
93     std::string applicationVersion;
94     int64_t timeStamp = 0;
95     uint32_t versionCode = 0;
96 
97     // for install and uninstall
98     int32_t callingUid = 0;
99     std::string callingAppId;
100     std::string callingBundleName;
101     std::vector<std::string> filePath;
102     std::vector<std::string> hashValue;
103     // only for install
104     std::string fingerprint;
105     bool hideDesktopIcon = false;
106     std::string appDistributionType;
107 
108     // only used for preBundle
109     bool isPreInstallApp = false;
110     InstallScene preBundleScene = InstallScene::NORMAL;
111 
112     // only used for clean cache
113     bool isCleanCache = true;
114 
115     // only used for component disable or enable
116     bool isEnable = false;
117 
118     // only used for free install
119     bool isFreeInstallMode = false;
120 
121     // only used in fault event
122     ErrCode errCode = ERR_OK;
123 
124     // only used in user event
125     UserEventType userEventType = UserEventType::UNKNOW;
126 
127     // for quick fix
128     int32_t applyQuickFixFrequency = 0;
129     // AOT
130     std::vector<std::string> totalBundleNames;
131     uint32_t successCnt = 0;
132     std::string compileMode;
133     bool compileResult = false;
134     std::string failureReason;
135     int64_t costTimeSeconds = 0;
136     int32_t sceneId = 0;
137     std::string processName;
138     int32_t appIndex = 0;
139 
140     //for query of continue type
141     std::string continueType;
142     //for free install event
143     bool isFreeInstall = false;
144     std::string fileName;
145     int64_t freeSize = 0;
146     int32_t operationType = 0;
147 
ResetEventInfo148     void Reset()
149     {
150         userId = Constants::INVALID_USERID;
151         bundleName.clear();
152         moduleName.clear();
153         abilityName.clear();
154         packageName.clear();
155         applicationVersion.clear();
156         versionCode = 0;
157         timeStamp = 0;
158         preBundleScene = InstallScene::NORMAL;
159         isCleanCache = false;
160         isPreInstallApp = false;
161         isFreeInstallMode = false;
162         isEnable = false;
163         errCode = ERR_OK;
164         userEventType = UserEventType::UNKNOW;
165         callingUid = 0;
166         callingAppId.clear();
167         callingBundleName.clear();
168         filePath.clear();
169         hashValue.clear();
170         fingerprint.clear();
171         hideDesktopIcon = false;
172         appDistributionType.clear();
173         applyQuickFixFrequency = 0;
174         totalBundleNames.clear();
175         successCnt = 0;
176         compileMode.clear();
177         compileResult = false;
178         failureReason.clear();
179         costTimeSeconds = 0;
180         continueType.clear();
181         sceneId = 0;
182         processName.clear();
183         appIndex = 0;
184         isFreeInstall = false;
185         fileName.clear();
186         freeSize = 0;
187         operationType = 0;
188     }
189 };
190 
191 class EventReport {
192 public:
193     /**
194      * @brief Send bundle system events.
195      * @param bundleEventType Indicates the bundle eventType.
196      * @param eventInfo Indicates the eventInfo.
197      */
198     static void SendBundleSystemEvent(BundleEventType bundleEventType, const EventInfo& eventInfo);
199     /**
200      * @brief Send scan system events.
201      * @param bMSEventType Indicates the bMSEventType.
202      */
203     static void SendScanSysEvent(BMSEventType bMSEventType);
204     /**
205      * @brief Send component diable or enable system events.
206      * @param bundleName Indicates the bundleName.
207      * @param abilityName Indicates the abilityName.
208      * @param userId Indicates the userId.
209      * @param isEnable Indicates the isEnable.
210      * @param appIndex Indicates the app index for clone app.
211      */
212     static void SendComponentStateSysEventForException(const std::string &bundleName, const std::string &abilityName,
213         int32_t userId, bool isEnable, int32_t appIndex, const std::string &callingName);
214     /**
215      * @brief Send component diable or enable system events.
216      * @param bundleName Indicates the bundleName.
217      * @param abilityName Indicates the abilityName.
218      * @param userId Indicates the userId.
219      * @param isEnable Indicates the isEnable.
220      * @param appIndex Indicates the app index for clone app.
221      */
222     static void SendComponentStateSysEvent(const std::string &bundleName, const std::string &abilityName,
223         int32_t userId, bool isEnable, int32_t appIndex, const std::string &callingName);
224     /**
225      * @brief Send clean cache system events.
226      * @param bundleName Indicates the bundleName.
227      * @param userId Indicates the userId.
228      * @param isCleanCache Indicates the isCleanCache.
229      * @param exception Indicates the exception.
230      */
231     static void SendCleanCacheSysEvent(
232         const std::string &bundleName, int32_t userId, bool isCleanCache, bool exception);
233     /**
234      * @brief Send clean cache system events.
235      * @param bundleName Indicates the bundleName.
236      * @param userId Indicates the userId.
237      * @param appIndex Indicates the appIndex.
238      * @param isCleanCache Indicates the isCleanCache.
239      * @param exception Indicates the exception.
240      */
241     static void SendCleanCacheSysEventWithIndex(
242         const std::string &bundleName, int32_t userId, int32_t appIndex, bool isCleanCache, bool exception);
243     /**
244      * @brief Send system events.
245      * @param eventType Indicates the bms eventInfo.
246      * @param eventInfo Indicates the eventInfo.
247      */
248     static void SendSystemEvent(BMSEventType eventType, const EventInfo& eventInfo);
249     /**
250      * @brief Send user system events.
251      * @param userEventType Indicates the userEventType.
252      * @param userId Indicates the userId.
253      */
254     static void SendUserSysEvent(UserEventType userEventType, int32_t userId);
255 
256     /**
257      * @brief Send query abilityInfos by continueType system events.
258      * @param bundleName Indicates the bundleName.
259      * @param abilityName Indicates the abilityName.
260      * @param errCode code of result.
261      * @param continueType Indicates the continueType.
262      */
263     static void SendQueryAbilityInfoByContinueTypeSysEvent(const std::string &bundleName,
264         const std::string &abilityName, ErrCode errCode, int32_t userId, const std::string &continueType);
265 
266     static void SendCpuSceneEvent(const std::string &processName, const int32_t sceneId);
267     /**
268      *@brief send free install event
269      *@param bundleName Indicates the bundleName.
270      *@param abilityName Indicates the abilityName.
271      *@param moduleName Indicates the moduleName.
272      *@param isFreeInstall Indicates the isFreeInstall.
273      *@param timeStamp Indicates the timeStamp.
274      */
275     static void SendFreeInstallEvent(const std::string &bundleName, const std::string &abilityName,
276         const std::string &moduleName, bool isFreeInstall, int64_t timeStamp);
277 
278     /**
279      * @brief Send system events the disk space in insufficient when an applicaiton is begin installed ir uninstall .
280      * @param fileName file name.
281      * @param freeSize free size.
282      * @param operationType operation type.
283      */
284     static void SendDiskSpaceEvent(const std::string &fileName,
285         int64_t freeSize, int32_t operationType);
286 };
287 }  // namespace AppExecFwk
288 }  // namespace OHOS
289 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_EVENT_REPORT_H
290