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 <unordered_map>
19
20 #include "app_log_wrapper.h"
21 #include "bundle_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 std::unordered_map<BundleEventType, BMSEventType> BUNDLE_EXCEPTION_SYS_EVENT_MAP = {
30 { BundleEventType::INSTALL, BMSEventType::BUNDLE_INSTALL_EXCEPTION },
31 { BundleEventType::UNINSTALL, BMSEventType::BUNDLE_UNINSTALL_EXCEPTION },
32 { BundleEventType::UPDATE, BMSEventType::BUNDLE_UPDATE_EXCEPTION },
33 { BundleEventType::RECOVER, BMSEventType::PRE_BUNDLE_RECOVER_EXCEPTION },
34 };
35
36 const std::unordered_map<BundleEventType, BMSEventType> BUNDLE_SYS_EVENT_MAP = {
37 { BundleEventType::INSTALL, BMSEventType::BUNDLE_INSTALL },
38 { BundleEventType::UNINSTALL, BMSEventType::BUNDLE_UNINSTALL },
39 { BundleEventType::UPDATE, BMSEventType::BUNDLE_UPDATE },
40 { BundleEventType::RECOVER, BMSEventType::PRE_BUNDLE_RECOVER },
41 { BundleEventType::QUICK_FIX, BMSEventType::APPLY_QUICK_FIX }
42 };
43 }
44
SendBundleSystemEvent(BundleEventType bundleEventType,const EventInfo & eventInfo)45 void EventReport::SendBundleSystemEvent(BundleEventType bundleEventType, const EventInfo& eventInfo)
46 {
47 BMSEventType bmsEventType = BMSEventType::UNKNOW;
48 std::unordered_map<BundleEventType, BMSEventType>::const_iterator iter;
49 if (eventInfo.errCode != ERR_OK) {
50 iter = BUNDLE_EXCEPTION_SYS_EVENT_MAP.find(bundleEventType);
51 if (iter != BUNDLE_EXCEPTION_SYS_EVENT_MAP.end()) {
52 bmsEventType = iter->second;
53 }
54
55 SendSystemEvent(bmsEventType, eventInfo);
56 return;
57 }
58
59 iter = BUNDLE_SYS_EVENT_MAP.find(bundleEventType);
60 if (iter != BUNDLE_SYS_EVENT_MAP.end()) {
61 bmsEventType = iter->second;
62 }
63
64 SendSystemEvent(bmsEventType, eventInfo);
65 }
66
SendScanSysEvent(BMSEventType bMSEventType)67 void EventReport::SendScanSysEvent(BMSEventType bMSEventType)
68 {
69 EventInfo eventInfo;
70 eventInfo.timeStamp = BundleUtil::GetCurrentTimeMs();
71 EventReport::SendSystemEvent(bMSEventType, eventInfo);
72 }
73
SendUserSysEvent(UserEventType userEventType,int32_t userId)74 void EventReport::SendUserSysEvent(UserEventType userEventType, int32_t userId)
75 {
76 EventInfo eventInfo;
77 eventInfo.timeStamp = BundleUtil::GetCurrentTimeMs();
78 eventInfo.userId = userId;
79 eventInfo.userEventType = userEventType;
80 EventReport::SendSystemEvent(BMSEventType::BMS_USER_EVENT, eventInfo);
81 }
82
SendComponentStateSysEvent(const std::string & bundleName,const std::string & abilityName,int32_t userId,bool isEnable,bool exception)83 void EventReport::SendComponentStateSysEvent(const std::string &bundleName,
84 const std::string &abilityName, int32_t userId, bool isEnable, bool exception)
85 {
86 EventInfo eventInfo;
87 eventInfo.bundleName = bundleName;
88 eventInfo.abilityName = abilityName;
89 eventInfo.userId = userId;
90 eventInfo.isEnable = isEnable;
91 BMSEventType bmsEventType;
92 if (exception) {
93 bmsEventType = BMSEventType::BUNDLE_STATE_CHANGE_EXCEPTION;
94 } else {
95 bmsEventType = BMSEventType::BUNDLE_STATE_CHANGE;
96 }
97
98 EventReport::SendSystemEvent(bmsEventType, eventInfo);
99 }
100
SendCleanCacheSysEvent(const std::string & bundleName,int32_t userId,bool isCleanCache,bool exception)101 void EventReport::SendCleanCacheSysEvent(
102 const std::string &bundleName, int32_t userId, bool isCleanCache, bool exception)
103 {
104 EventInfo eventInfo;
105 eventInfo.bundleName = bundleName;
106 eventInfo.userId = userId;
107 eventInfo.isCleanCache = isCleanCache;
108 BMSEventType bmsEventType;
109 if (exception) {
110 bmsEventType = BMSEventType::BUNDLE_CLEAN_CACHE_EXCEPTION;
111 } else {
112 bmsEventType = BMSEventType::BUNDLE_CLEAN_CACHE;
113 }
114
115 EventReport::SendSystemEvent(bmsEventType, eventInfo);
116 }
117
SendSystemEvent(BMSEventType bmsEventType,const EventInfo & eventInfo)118 void EventReport::SendSystemEvent(BMSEventType bmsEventType, const EventInfo& eventInfo)
119 {
120 #ifdef HISYSEVENT_ENABLE
121 InnerEventReport::SendSystemEvent(bmsEventType, eventInfo);
122 #else
123 APP_LOGD("Hisysevent is disabled");
124 #endif
125 }
126 } // namespace AppExecFwk
127 } // namespace OHOS