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 "event_report.h"
17
18 #include <set>
19 #include "app_log_wrapper.h"
20 #include "bundle_util.h"
21 #include "bundle_file_util.h"
22 #ifdef HISYSEVENT_ENABLE
23 #include "inner_event_report.h"
24 #endif
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 const BundleEventType BUNDLE_EXCEPTION_SYS_EVENT_MAP_KEY[] = {
30 BundleEventType::INSTALL, BundleEventType::UNINSTALL,
31 BundleEventType::UPDATE, BundleEventType::RECOVER
32 };
33 const BMSEventType BUNDLE_EXCEPTION_SYS_EVENT_MAP_VALUE[] = {
34 BMSEventType::BUNDLE_INSTALL_EXCEPTION, BMSEventType::BUNDLE_UNINSTALL_EXCEPTION,
35 BMSEventType::BUNDLE_UPDATE_EXCEPTION, BMSEventType::PRE_BUNDLE_RECOVER_EXCEPTION
36 };
37
38 const BundleEventType BUNDLE_SYS_EVENT_MAP_KEY[] = {
39 BundleEventType::INSTALL, BundleEventType::UNINSTALL,
40 BundleEventType::UPDATE, BundleEventType::RECOVER,
41 BundleEventType::QUICK_FIX,
42 };
43 const BMSEventType BUNDLE_SYS_EVENT_MAP_VALUE[] = {
44 BMSEventType::BUNDLE_INSTALL, BMSEventType::BUNDLE_UNINSTALL,
45 BMSEventType::BUNDLE_UPDATE, BMSEventType::PRE_BUNDLE_RECOVER,
46 BMSEventType::APPLY_QUICK_FIX,
47 };
48 const std::set<int32_t> INTERCEPTED_ERROR_CODE_SET = {
49 ERR_APPEXECFWK_INSTALL_SELF_UPDATE_NOT_MDM,
50 ERR_APP_DISTRIBUTION_TYPE_NOT_ALLOW_INSTALL,
51 ERR_APPEXECFWK_INSTALL_ENTERPRISE_BUNDLE_NOT_ALLOWED,
52 ERR_BUNDLE_MANAGER_CODE_SIGNATURE_DELIVERY_FILE_FAILED,
53 ERR_APPEXECFWK_INSTALL_PERMISSION_DENIED,
54 ERR_BUNDLE_MANAGER_APP_CONTROL_DISALLOWED_UNINSTALL,
55 ERR_APPEXECFWK_INSTALL_EXISTED_ENTERPRISE_BUNDLE_NOT_ALLOWED
56 };
57 constexpr const char* PARTITION_NAME = "/data";
58 }
59
SendBundleSystemEvent(BundleEventType bundleEventType,const EventInfo & eventInfo)60 void EventReport::SendBundleSystemEvent(BundleEventType bundleEventType, const EventInfo& eventInfo)
61 {
62 BMSEventType bmsEventType = BMSEventType::UNKNOW;
63 if (eventInfo.errCode != ERR_OK) {
64 size_t len = sizeof(BUNDLE_EXCEPTION_SYS_EVENT_MAP_KEY) / sizeof(BundleEventType);
65 for (size_t i = 0; i < len; i++) {
66 if (bundleEventType == BUNDLE_EXCEPTION_SYS_EVENT_MAP_KEY[i]) {
67 bmsEventType = BUNDLE_EXCEPTION_SYS_EVENT_MAP_VALUE[i];
68 break;
69 }
70 }
71 EventInfo info = ProcessIsIntercepted(eventInfo);
72 SendSystemEvent(bmsEventType, info);
73 return;
74 }
75
76 size_t len = sizeof(BUNDLE_SYS_EVENT_MAP_KEY) / sizeof(BundleEventType);
77 for (size_t i = 0; i < len; i++) {
78 if (bundleEventType == BUNDLE_SYS_EVENT_MAP_KEY[i]) {
79 bmsEventType = BUNDLE_SYS_EVENT_MAP_VALUE[i];
80 break;
81 }
82 }
83
84 SendSystemEvent(bmsEventType, eventInfo);
85 }
86
ProcessIsIntercepted(const EventInfo & eventInfo)87 EventInfo EventReport::ProcessIsIntercepted(const EventInfo &eventInfo)
88 {
89 if (INTERCEPTED_ERROR_CODE_SET.find(eventInfo.errCode) != INTERCEPTED_ERROR_CODE_SET.end()) {
90 EventInfo info = eventInfo;
91 info.isIntercepted = true;
92 return info;
93 }
94 return eventInfo;
95 }
96
SendScanSysEvent(BMSEventType bMSEventType)97 void EventReport::SendScanSysEvent(BMSEventType bMSEventType)
98 {
99 EventInfo eventInfo;
100 eventInfo.timeStamp = BundleUtil::GetCurrentTimeMs();
101 EventReport::SendSystemEvent(bMSEventType, eventInfo);
102 }
103
SendUserSysEvent(UserEventType userEventType,int32_t userId)104 void EventReport::SendUserSysEvent(UserEventType userEventType, int32_t userId)
105 {
106 EventInfo eventInfo;
107 eventInfo.timeStamp = BundleUtil::GetCurrentTimeMs();
108 eventInfo.userId = userId;
109 eventInfo.userEventType = userEventType;
110 EventReport::SendSystemEvent(BMSEventType::BMS_USER_EVENT, eventInfo);
111 }
112
SendComponentStateSysEventForException(const std::string & bundleName,const std::string & abilityName,int32_t userId,bool isEnable,int32_t appIndex,const std::string & caller)113 void EventReport::SendComponentStateSysEventForException(const std::string &bundleName, const std::string &abilityName,
114 int32_t userId, bool isEnable, int32_t appIndex, const std::string &caller)
115 {
116 EventInfo eventInfo;
117 eventInfo.bundleName = bundleName;
118 eventInfo.abilityName = abilityName;
119 eventInfo.userId = userId;
120 eventInfo.isEnable = isEnable;
121 eventInfo.appIndex = appIndex;
122 eventInfo.callingBundleName = caller;
123 BMSEventType bmsEventType = BMSEventType::BUNDLE_STATE_CHANGE_EXCEPTION;
124
125 EventReport::SendSystemEvent(bmsEventType, eventInfo);
126 }
127
SendComponentStateSysEvent(const std::string & bundleName,const std::string & abilityName,int32_t userId,bool isEnable,int32_t appIndex,const std::string & caller)128 void EventReport::SendComponentStateSysEvent(const std::string &bundleName, const std::string &abilityName,
129 int32_t userId, bool isEnable, int32_t appIndex, const std::string &caller)
130 {
131 EventInfo eventInfo;
132 eventInfo.bundleName = bundleName;
133 eventInfo.abilityName = abilityName;
134 eventInfo.userId = userId;
135 eventInfo.isEnable = isEnable;
136 eventInfo.appIndex = appIndex;
137 eventInfo.callingBundleName = caller;
138 BMSEventType bmsEventType = BMSEventType::BUNDLE_STATE_CHANGE;
139
140 EventReport::SendSystemEvent(bmsEventType, eventInfo);
141 }
142
SendCleanCacheSysEvent(const std::string & bundleName,int32_t userId,bool isCleanCache,bool exception,int32_t callingUid,const std::string & callingBundleName)143 void EventReport::SendCleanCacheSysEvent(
144 const std::string &bundleName, int32_t userId, bool isCleanCache, bool exception,
145 int32_t callingUid, const std::string &callingBundleName)
146 {
147 EventInfo eventInfo;
148 eventInfo.bundleName = bundleName;
149 eventInfo.userId = userId;
150 eventInfo.isCleanCache = isCleanCache;
151 eventInfo.callingUid = callingUid;
152 eventInfo.callingBundleName = callingBundleName;
153 BMSEventType bmsEventType;
154 if (exception) {
155 bmsEventType = BMSEventType::BUNDLE_CLEAN_CACHE_EXCEPTION;
156 } else {
157 bmsEventType = BMSEventType::BUNDLE_CLEAN_CACHE;
158 }
159
160 EventReport::SendSystemEvent(bmsEventType, eventInfo);
161 }
162
SendCleanCacheSysEventWithIndex(const std::string & bundleName,int32_t userId,int32_t appIndex,bool isCleanCache,bool exception,int32_t callingUid,const std::string & callingBundleName)163 void EventReport::SendCleanCacheSysEventWithIndex(
164 const std::string &bundleName, int32_t userId, int32_t appIndex, bool isCleanCache, bool exception,
165 int32_t callingUid, const std::string &callingBundleName)
166 {
167 EventInfo eventInfo;
168 eventInfo.bundleName = bundleName;
169 eventInfo.userId = userId;
170 eventInfo.appIndex = appIndex;
171 eventInfo.isCleanCache = isCleanCache;
172 eventInfo.callingUid = callingUid;
173 eventInfo.callingBundleName = callingBundleName;
174 BMSEventType bmsEventType;
175 if (exception) {
176 bmsEventType = BMSEventType::BUNDLE_CLEAN_CACHE_EXCEPTION;
177 } else {
178 bmsEventType = BMSEventType::BUNDLE_CLEAN_CACHE;
179 }
180
181 EventReport::SendSystemEvent(bmsEventType, eventInfo);
182 }
183
SendQueryAbilityInfoByContinueTypeSysEvent(const std::string & bundleName,const std::string & abilityName,ErrCode errCode,int32_t userId,const std::string & continueType)184 void EventReport::SendQueryAbilityInfoByContinueTypeSysEvent(const std::string &bundleName,
185 const std::string &abilityName, ErrCode errCode, int32_t userId, const std::string &continueType)
186 {
187 EventInfo eventInfo;
188 eventInfo.bundleName = bundleName;
189 eventInfo.abilityName = abilityName;
190 eventInfo.errCode = errCode;
191 eventInfo.continueType = continueType;
192 eventInfo.userId = userId,
193 EventReport::SendSystemEvent(BMSEventType::QUERY_OF_CONTINUE_TYPE, eventInfo);
194 }
195
SendCpuSceneEvent(const std::string & processName,const int32_t sceneId)196 void EventReport::SendCpuSceneEvent(const std::string &processName, const int32_t sceneId)
197 {
198 EventInfo eventInfo;
199 eventInfo.sceneId = sceneId;
200 eventInfo.processName = processName;
201 eventInfo.timeStamp = BundleUtil::GetCurrentTimeMs();
202 EventReport::SendSystemEvent(BMSEventType::CPU_SCENE_ENTRY, eventInfo);
203 }
204
SendFreeInstallEvent(const std::string & bundleName,const std::string & abilityName,const std::string & moduleName,bool isFreeInstall,int64_t timeStamp)205 void EventReport::SendFreeInstallEvent(const std::string &bundleName, const std::string &abilityName,
206 const std::string &moduleName, bool isFreeInstall, int64_t timeStamp)
207 {
208 EventInfo eventInfo;
209 eventInfo.bundleName = bundleName;
210 eventInfo.abilityName = abilityName;
211 eventInfo.moduleName = moduleName;
212 eventInfo.isFreeInstall = isFreeInstall;
213 eventInfo.timeStamp = timeStamp;
214 EventReport::SendSystemEvent(BMSEventType::FREE_INSTALL_EVENT, eventInfo);
215 }
216
SendDiskSpaceEvent(const std::string & fileName,int64_t freeSize,int32_t operationType)217 void EventReport::SendDiskSpaceEvent(const std::string &fileName,
218 int64_t freeSize, int32_t operationType)
219 {
220 EventInfo eventInfo;
221 eventInfo.fileName = fileName;
222 eventInfo.freeSize = freeSize;
223 eventInfo.operationType = operationType;
224 EventReport::SendSystemEvent(BMSEventType::BMS_DISK_SPACE, eventInfo);
225 }
226
SendAppControlRuleEvent(const EventInfo & eventInfo)227 void EventReport::SendAppControlRuleEvent(const EventInfo& eventInfo)
228 {
229 EventReport::SendSystemEvent(BMSEventType::APP_CONTROL_RULE, eventInfo);
230 }
231
SendDbErrorEvent(const std::string & dbName,int32_t operationType,int32_t errorCode)232 void EventReport::SendDbErrorEvent(const std::string &dbName, int32_t operationType, int32_t errorCode)
233 {
234 APP_LOGI("SendDbErrorEvent dbname:%{public}s operation:%{public}d", dbName.c_str(), operationType);
235 EventInfo eventInfo;
236 eventInfo.dbName = dbName;
237 eventInfo.operationType = operationType;
238 eventInfo.errorCode = errorCode;
239 EventReport::SendSystemEvent(BMSEventType::DB_ERROR, eventInfo);
240 }
241
ReportDataPartitionUsageEvent()242 void EventReport::ReportDataPartitionUsageEvent()
243 {
244 if (!BundleFileUtil::IsReportDataPartitionUsageEvent(PARTITION_NAME)) {
245 APP_LOGD("data partitioning threshold has not been reached.");
246 return;
247 }
248 EventInfo eventInfo;
249 EventReport::SendSystemEvent(BMSEventType::DATA_PARTITION_USAGE_EVENT, eventInfo);
250 }
251
SendDefaultAppEvent(DefaultAppActionType actionType,int32_t userId,const std::string & callingName,const std::string & want,const std::string & utd)252 void EventReport::SendDefaultAppEvent(DefaultAppActionType actionType, int32_t userId, const std::string& callingName,
253 const std::string& want, const std::string& utd)
254 {
255 EventInfo eventInfo;
256 eventInfo.actionType = static_cast<int32_t>(actionType);
257 eventInfo.userId = userId;
258 eventInfo.callingName = callingName;
259 eventInfo.want = want;
260 eventInfo.utd = utd;
261 EventReport::SendSystemEvent(BMSEventType::DEFAULT_APP, eventInfo);
262 }
263
SendSystemEvent(BMSEventType bmsEventType,const EventInfo & eventInfo)264 void EventReport::SendSystemEvent(BMSEventType bmsEventType, const EventInfo& eventInfo)
265 {
266 #ifdef HISYSEVENT_ENABLE
267 InnerEventReport::SendSystemEvent(bmsEventType, eventInfo);
268 #else
269 APP_LOGD("Hisysevent is disabled");
270 #endif
271 }
272 } // namespace AppExecFwk
273 } // namespace OHOS