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 };
42 }
43
SendBundleSystemEvent(BundleEventType bundleEventType,const EventInfo & eventInfo)44 void EventReport::SendBundleSystemEvent(BundleEventType bundleEventType, const EventInfo& eventInfo)
45 {
46 BMSEventType bmsEventType = BMSEventType::UNKNOW;
47 std::unordered_map<BundleEventType, BMSEventType>::const_iterator iter;
48 if (eventInfo.errCode != ERR_OK) {
49 iter = BUNDLE_EXCEPTION_SYS_EVENT_MAP.find(bundleEventType);
50 if (iter != BUNDLE_EXCEPTION_SYS_EVENT_MAP.end()) {
51 bmsEventType = iter->second;
52 }
53
54 SendSystemEvent(bmsEventType, eventInfo);
55 return;
56 }
57
58 iter = BUNDLE_SYS_EVENT_MAP.find(bundleEventType);
59 if (iter != BUNDLE_SYS_EVENT_MAP.end()) {
60 bmsEventType = iter->second;
61 }
62
63 SendSystemEvent(bmsEventType, eventInfo);
64 }
65
SendScanSysEvent(BMSEventType bMSEventType)66 void EventReport::SendScanSysEvent(BMSEventType bMSEventType)
67 {
68 EventInfo eventInfo;
69 eventInfo.timeStamp = BundleUtil::GetCurrentTime();
70 EventReport::SendSystemEvent(bMSEventType, eventInfo);
71 }
72
SendComponentStateSysEvent(const std::string & bundleName,const std::string & abilityName,int32_t userId,bool isEnable,bool exception)73 void EventReport::SendComponentStateSysEvent(const std::string &bundleName,
74 const std::string &abilityName, int32_t userId, bool isEnable, bool exception)
75 {
76 EventInfo eventInfo;
77 eventInfo.bundleName = bundleName;
78 eventInfo.abilityName = abilityName;
79 eventInfo.userId = userId;
80 eventInfo.isEnable = isEnable;
81 BMSEventType bmsEventType;
82 if (exception) {
83 bmsEventType = BMSEventType::BUNDLE_STATE_CHANGE_EXCEPTION;
84 } else {
85 bmsEventType = BMSEventType::BUNDLE_STATE_CHANGE;
86 }
87
88 EventReport::SendSystemEvent(bmsEventType, eventInfo);
89 }
90
SendCleanCacheSysEvent(const std::string & bundleName,int32_t userId,bool isCleanCache,bool exception)91 void EventReport::SendCleanCacheSysEvent(
92 const std::string &bundleName, int32_t userId, bool isCleanCache, bool exception)
93 {
94 EventInfo eventInfo;
95 eventInfo.bundleName = bundleName;
96 eventInfo.userId = userId;
97 eventInfo.isCleanCache = isCleanCache;
98 BMSEventType bmsEventType;
99 if (exception) {
100 bmsEventType = BMSEventType::BUNDLE_CLEAN_CACHE_EXCEPTION;
101 } else {
102 bmsEventType = BMSEventType::BUNDLE_CLEAN_CACHE;
103 }
104
105 EventReport::SendSystemEvent(bmsEventType, eventInfo);
106 }
107
SendSystemEvent(BMSEventType bmsEventType,const EventInfo & eventInfo)108 void EventReport::SendSystemEvent(BMSEventType bmsEventType, const EventInfo& eventInfo)
109 {
110 #ifdef HISYSEVENT_ENABLE
111 InnerEventReport::SendSystemEvent(bmsEventType, eventInfo);
112 #else
113 APP_LOGD("Hisysevent is disabled");
114 #endif
115 }
116 } // namespace AppExecFwk
117 } // namespace OHOS