1 /*
2 * Copyright (c) 2022 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 #include "inner_event_report.h"
17
18 #include "app_log_wrapper.h"
19 #include "hisysevent.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 // event type
25 const std::string BUNDLE_INSTALL_EXCEPTION = "BUNDLE_INSTALL_EXCEPTION";
26 const std::string BUNDLE_UNINSTALL_EXCEPTION = "BUNDLE_UNINSTALL_EXCEPTION";
27 const std::string BUNDLE_UPDATE_EXCEPTION = "BUNDLE_UPDATE_EXCEPTION";
28 const std::string PRE_BUNDLE_RECOVER_EXCEPTION = "PRE_BUNDLE_RECOVER_EXCEPTION";
29 const std::string BUNDLE_STATE_CHANGE_EXCEPTION = "BUNDLE_STATE_CHANGE_EXCEPTION";
30 const std::string BUNDLE_CLEAN_CACHE_EXCEPTION = "BUNDLE_CLEAN_CACHE_EXCEPTION";
31
32 const std::string BOOT_SCAN_START = "BOOT_SCAN_START";
33 const std::string BOOT_SCAN_END = "BOOT_SCAN_END";
34 const std::string BUNDLE_INSTALL = "BUNDLE_INSTALL";
35 const std::string BUNDLE_UNINSTALL = "BUNDLE_UNINSTALL";
36 const std::string BUNDLE_UPDATE = "BUNDLE_UPDATE";
37 const std::string PRE_BUNDLE_RECOVER = "PRE_BUNDLE_RECOVER";
38 const std::string BUNDLE_STATE_CHANGE = "BUNDLE_STATE_CHANGE";
39 const std::string BUNDLE_CLEAN_CACHE = "BUNDLE_CLEAN_CACHE";
40
41 // event params
42 const std::string EVENT_PARAM_USERID = "USERID";
43 const std::string EVENT_PARAM_UID = "UID";
44 const std::string EVENT_PARAM_BUNDLE_NAME = "BUNDLE_NAME";
45 const std::string EVENT_PARAM_ERROR_CODE = "ERROR_CODE";
46 const std::string EVENT_PARAM_ABILITY_NAME = "ABILITY_NAME";
47 const std::string EVENT_PARAM_TIME = "TIME";
48 const std::string EVENT_PARAM_VERSION = "VERSION";
49 const std::string EVENT_PARAM_SCENE = "SCENE";
50 const std::string EVENT_PARAM_CLEAN_TYPE = "CLEAN_TYPE";
51 const std::string EVENT_PARAM_INSTALL_TYPE = "INSTALL_TYPE";
52 const std::string EVENT_PARAM_STATE = "STATE";
53
54 const std::string FREE_INSTALL_TYPE = "FreeInstall";
55 const std::string PRE_BUNDLE_INSTALL_TYPE = "PreBundleInstall";
56 const std::string NORMAL_INSTALL_TYPE = "normalInstall";
57 const std::string NORMAL_SCENE = "Normal";
58 const std::string BOOT_SCENE = "Boot";
59 const std::string REBOOT_SCENE = "Reboot";
60 const std::string CREATE_USER_SCENE = "CreateUser";
61 const std::string REMOVE_USER_SCENE = "CreateUser";
62 const std::string CLEAN_CACHE = "cleanCache";
63 const std::string CLEAN_DATA = "cleanData";
64 const std::string ENABLE = "enable";
65 const std::string DISABLE = "disable";
66 const std::string APPLICATION = "application";
67 const std::string ABILITY = "ability";
68 const std::string TYPE = "TYPE";
69
70 const std::unordered_map<InstallScene, std::string> INSTALL_SCENE_STR_MAP = {
71 { InstallScene::NORMAL, NORMAL_SCENE },
72 { InstallScene::BOOT, BOOT_SCENE },
73 { InstallScene::REBOOT, REBOOT_SCENE },
74 { InstallScene::CREATE_USER, CREATE_USER_SCENE },
75 { InstallScene::REMOVE_USER, REMOVE_USER_SCENE },
76 };
77
GetInstallType(const EventInfo & eventInfo)78 std::string GetInstallType(const EventInfo& eventInfo)
79 {
80 std::string installType = NORMAL_INSTALL_TYPE;
81 if (eventInfo.isFreeInstallMode) {
82 installType = FREE_INSTALL_TYPE;
83 } else if (eventInfo.isPreInstallApp) {
84 installType = PRE_BUNDLE_INSTALL_TYPE;
85 }
86
87 return installType;
88 }
89
GetInstallScene(const EventInfo & eventInfo)90 std::string GetInstallScene(const EventInfo& eventInfo)
91 {
92 std::string installScene = NORMAL_SCENE;
93 auto iter = INSTALL_SCENE_STR_MAP.find(eventInfo.preBundleScene);
94 if (iter != INSTALL_SCENE_STR_MAP.end()) {
95 installScene = iter->second;
96 }
97
98 return installScene;
99 }
100 }
101
102 std::unordered_map<BMSEventType, void (*)(const EventInfo& eventInfo)>
103 InnerEventReport::bmsSysEventMap_ = {
104 { BMSEventType::BUNDLE_INSTALL_EXCEPTION,
__anone24855cb0202() 105 [](const EventInfo& eventInfo) {
106 InnerSendBundleInstallExceptionEvent(eventInfo);
107 } },
108 { BMSEventType::BUNDLE_UNINSTALL_EXCEPTION,
__anone24855cb0302() 109 [](const EventInfo& eventInfo) {
110 InnerSendBundleUninstallExceptionEvent(eventInfo);
111 } },
112 { BMSEventType::BUNDLE_UPDATE_EXCEPTION,
__anone24855cb0402() 113 [](const EventInfo& eventInfo) {
114 InnerSendBundleUpdateExceptionEvent(eventInfo);
115 } },
116 { BMSEventType::PRE_BUNDLE_RECOVER_EXCEPTION,
__anone24855cb0502() 117 [](const EventInfo& eventInfo) {
118 InnerSendPreBundleRecoverExceptionEvent(eventInfo);
119 } },
120 { BMSEventType::BUNDLE_STATE_CHANGE_EXCEPTION,
__anone24855cb0602() 121 [](const EventInfo& eventInfo) {
122 InnerSendBundleStateChangeExceptionEvent(eventInfo);
123 } },
124 { BMSEventType::BUNDLE_CLEAN_CACHE_EXCEPTION,
__anone24855cb0702() 125 [](const EventInfo& eventInfo) {
126 InnerSendBundleCleanCacheExceptionEvent(eventInfo);
127 } },
128 { BMSEventType::BOOT_SCAN_START,
__anone24855cb0802() 129 [](const EventInfo& eventInfo) {
130 InnerSendBootScanStartEvent(eventInfo);
131 } },
132 { BMSEventType::BOOT_SCAN_END,
__anone24855cb0902() 133 [](const EventInfo& eventInfo) {
134 InnerSendBootScanEndEvent(eventInfo);
135 } },
136 { BMSEventType::BUNDLE_INSTALL,
__anone24855cb0a02() 137 [](const EventInfo& eventInfo) {
138 InnerSendBundleInstallEvent(eventInfo);
139 } },
140 { BMSEventType::BUNDLE_UNINSTALL,
__anone24855cb0b02() 141 [](const EventInfo& eventInfo) {
142 InnerSendBundleUninstallEvent(eventInfo);
143 } },
144 { BMSEventType::BUNDLE_UPDATE,
__anone24855cb0c02() 145 [](const EventInfo& eventInfo) {
146 InnerSendBundleUpdateEvent(eventInfo);
147 } },
148 { BMSEventType::PRE_BUNDLE_RECOVER,
__anone24855cb0d02() 149 [](const EventInfo& eventInfo) {
150 InnerSendPreBundleRecoverEvent(eventInfo);
151 } },
152 { BMSEventType::BUNDLE_STATE_CHANGE,
__anone24855cb0e02() 153 [](const EventInfo& eventInfo) {
154 InnerSendBundleStateChangeEvent(eventInfo);
155 } },
156 { BMSEventType::BUNDLE_CLEAN_CACHE,
__anone24855cb0f02() 157 [](const EventInfo& eventInfo) {
158 InnerSendBundleCleanCacheEvent(eventInfo);
159 } },
160 };
161
SendSystemEvent(BMSEventType bmsEventType,const EventInfo & eventInfo)162 void InnerEventReport::SendSystemEvent(BMSEventType bmsEventType, const EventInfo& eventInfo)
163 {
164 auto iter = bmsSysEventMap_.find(bmsEventType);
165 if (iter == bmsSysEventMap_.end()) {
166 return;
167 }
168
169 iter->second(eventInfo);
170 }
171
InnerSendBundleInstallExceptionEvent(const EventInfo & eventInfo)172 void InnerEventReport::InnerSendBundleInstallExceptionEvent(const EventInfo& eventInfo)
173 {
174 InnerEventWrite(
175 BUNDLE_INSTALL_EXCEPTION,
176 HiSysEventType::FAULT,
177 EVENT_PARAM_USERID, eventInfo.userId,
178 EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
179 EVENT_PARAM_VERSION, eventInfo.versionCode,
180 EVENT_PARAM_INSTALL_TYPE, GetInstallType(eventInfo),
181 EVENT_PARAM_SCENE, GetInstallScene(eventInfo),
182 EVENT_PARAM_ERROR_CODE, eventInfo.errCode);
183 }
184
InnerSendBundleUninstallExceptionEvent(const EventInfo & eventInfo)185 void InnerEventReport::InnerSendBundleUninstallExceptionEvent(const EventInfo& eventInfo)
186 {
187 InnerEventWrite(
188 BUNDLE_UNINSTALL_EXCEPTION,
189 HiSysEventType::FAULT,
190 EVENT_PARAM_USERID, eventInfo.userId,
191 EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
192 EVENT_PARAM_VERSION, eventInfo.versionCode,
193 EVENT_PARAM_INSTALL_TYPE, GetInstallType(eventInfo),
194 EVENT_PARAM_ERROR_CODE, eventInfo.errCode);
195 }
196
InnerSendBundleUpdateExceptionEvent(const EventInfo & eventInfo)197 void InnerEventReport::InnerSendBundleUpdateExceptionEvent(const EventInfo& eventInfo)
198 {
199 InnerEventWrite(
200 BUNDLE_UPDATE_EXCEPTION,
201 HiSysEventType::FAULT,
202 EVENT_PARAM_USERID, eventInfo.userId,
203 EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
204 EVENT_PARAM_VERSION, eventInfo.versionCode,
205 EVENT_PARAM_INSTALL_TYPE, GetInstallType(eventInfo),
206 EVENT_PARAM_ERROR_CODE, eventInfo.errCode);
207 }
208
InnerSendPreBundleRecoverExceptionEvent(const EventInfo & eventInfo)209 void InnerEventReport::InnerSendPreBundleRecoverExceptionEvent(const EventInfo& eventInfo)
210 {
211 InnerEventWrite(
212 PRE_BUNDLE_RECOVER_EXCEPTION,
213 HiSysEventType::FAULT,
214 EVENT_PARAM_USERID, eventInfo.userId,
215 EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
216 EVENT_PARAM_VERSION, eventInfo.versionCode,
217 EVENT_PARAM_INSTALL_TYPE, PRE_BUNDLE_INSTALL_TYPE,
218 EVENT_PARAM_ERROR_CODE, eventInfo.errCode);
219 }
220
InnerSendBundleStateChangeExceptionEvent(const EventInfo & eventInfo)221 void InnerEventReport::InnerSendBundleStateChangeExceptionEvent(const EventInfo& eventInfo)
222 {
223 std::string type = eventInfo.abilityName.empty() ? APPLICATION : ABILITY;
224 InnerEventWrite(
225 BUNDLE_STATE_CHANGE_EXCEPTION,
226 HiSysEventType::FAULT,
227 EVENT_PARAM_USERID, eventInfo.userId,
228 EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
229 EVENT_PARAM_ABILITY_NAME, eventInfo.abilityName,
230 TYPE, type);
231 }
232
InnerSendBundleCleanCacheExceptionEvent(const EventInfo & eventInfo)233 void InnerEventReport::InnerSendBundleCleanCacheExceptionEvent(const EventInfo& eventInfo)
234 {
235 std::string cleanType = eventInfo.isCleanCache ? CLEAN_CACHE : CLEAN_DATA;
236 InnerEventWrite(
237 BUNDLE_CLEAN_CACHE_EXCEPTION,
238 HiSysEventType::FAULT,
239 EVENT_PARAM_USERID, eventInfo.userId,
240 EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
241 EVENT_PARAM_CLEAN_TYPE, cleanType);
242 }
243
InnerSendBootScanStartEvent(const EventInfo & eventInfo)244 void InnerEventReport::InnerSendBootScanStartEvent(const EventInfo& eventInfo)
245 {
246 InnerEventWrite(
247 BOOT_SCAN_START,
248 HiSysEventType::BEHAVIOR,
249 EVENT_PARAM_TIME, eventInfo.timeStamp);
250 }
251
InnerSendBootScanEndEvent(const EventInfo & eventInfo)252 void InnerEventReport::InnerSendBootScanEndEvent(const EventInfo& eventInfo)
253 {
254 InnerEventWrite(
255 BOOT_SCAN_END,
256 HiSysEventType::BEHAVIOR,
257 EVENT_PARAM_TIME, eventInfo.timeStamp);
258 }
259
InnerSendBundleInstallEvent(const EventInfo & eventInfo)260 void InnerEventReport::InnerSendBundleInstallEvent(const EventInfo& eventInfo)
261 {
262 InnerEventWrite(
263 BUNDLE_INSTALL,
264 HiSysEventType::BEHAVIOR,
265 EVENT_PARAM_USERID, eventInfo.userId,
266 EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
267 EVENT_PARAM_VERSION, eventInfo.versionCode,
268 EVENT_PARAM_INSTALL_TYPE, GetInstallType(eventInfo),
269 EVENT_PARAM_SCENE, GetInstallScene(eventInfo));
270 }
271
InnerSendBundleUninstallEvent(const EventInfo & eventInfo)272 void InnerEventReport::InnerSendBundleUninstallEvent(const EventInfo& eventInfo)
273 {
274 InnerEventWrite(
275 BUNDLE_UNINSTALL,
276 HiSysEventType::BEHAVIOR,
277 EVENT_PARAM_USERID, eventInfo.userId,
278 EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
279 EVENT_PARAM_VERSION, eventInfo.versionCode,
280 EVENT_PARAM_INSTALL_TYPE, GetInstallType(eventInfo));
281 }
282
InnerSendBundleUpdateEvent(const EventInfo & eventInfo)283 void InnerEventReport::InnerSendBundleUpdateEvent(const EventInfo& eventInfo)
284 {
285 InnerEventWrite(
286 BUNDLE_UPDATE,
287 HiSysEventType::BEHAVIOR,
288 EVENT_PARAM_USERID, eventInfo.userId,
289 EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
290 EVENT_PARAM_VERSION, eventInfo.versionCode,
291 EVENT_PARAM_INSTALL_TYPE, GetInstallType(eventInfo));
292 }
293
InnerSendPreBundleRecoverEvent(const EventInfo & eventInfo)294 void InnerEventReport::InnerSendPreBundleRecoverEvent(const EventInfo& eventInfo)
295 {
296 InnerEventWrite(
297 PRE_BUNDLE_RECOVER,
298 HiSysEventType::BEHAVIOR,
299 EVENT_PARAM_USERID, eventInfo.userId,
300 EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
301 EVENT_PARAM_VERSION, eventInfo.versionCode,
302 EVENT_PARAM_INSTALL_TYPE, PRE_BUNDLE_INSTALL_TYPE);
303 }
304
InnerSendBundleStateChangeEvent(const EventInfo & eventInfo)305 void InnerEventReport::InnerSendBundleStateChangeEvent(const EventInfo& eventInfo)
306 {
307 std::string type = eventInfo.abilityName.empty() ? APPLICATION : ABILITY;
308 std::string state = eventInfo.isEnable ? ENABLE : DISABLE;
309 InnerEventWrite(
310 BUNDLE_STATE_CHANGE,
311 HiSysEventType::BEHAVIOR,
312 EVENT_PARAM_USERID, eventInfo.userId,
313 EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
314 EVENT_PARAM_ABILITY_NAME, eventInfo.abilityName,
315 TYPE, type,
316 EVENT_PARAM_STATE, state);
317 }
318
InnerSendBundleCleanCacheEvent(const EventInfo & eventInfo)319 void InnerEventReport::InnerSendBundleCleanCacheEvent(const EventInfo& eventInfo)
320 {
321 std::string cleanType = eventInfo.isCleanCache ? CLEAN_CACHE : CLEAN_DATA;
322 InnerEventWrite(
323 BUNDLE_CLEAN_CACHE,
324 HiSysEventType::BEHAVIOR,
325 EVENT_PARAM_USERID, eventInfo.userId,
326 EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
327 EVENT_PARAM_CLEAN_TYPE, cleanType);
328 }
329
330 template<typename... Types>
InnerEventWrite(const std::string & eventName,HiSysEventType type,Types...keyValues)331 void InnerEventReport::InnerEventWrite(
332 const std::string &eventName,
333 HiSysEventType type,
334 Types... keyValues)
335 {
336 OHOS::HiviewDFX::HiSysEvent::Write(
337 OHOS::HiviewDFX::HiSysEvent::Domain::BUNDLE_MANAGER,
338 eventName,
339 static_cast<OHOS::HiviewDFX::HiSysEvent::EventType>(type),
340 keyValues...);
341 }
342 } // namespace AppExecFwk
343 } // namespace OHOS