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 "app_log_wrapper.h"
19 #include "bundle_util.h"
20 #ifdef HISYSEVENT_ENABLE
21 #include "inner_event_report.h"
22 #endif
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const std::unordered_map<BundleEventType, BMSEventType> BUNDLE_EXCEPTION_SYS_EVENT_MAP = {
28     { BundleEventType::INSTALL, BMSEventType::BUNDLE_INSTALL_EXCEPTION },
29     { BundleEventType::UNINSTALL, BMSEventType::BUNDLE_UNINSTALL_EXCEPTION },
30     { BundleEventType::UPDATE, BMSEventType::BUNDLE_UPDATE_EXCEPTION },
31     { BundleEventType::RECOVER, BMSEventType::PRE_BUNDLE_RECOVER_EXCEPTION },
32 };
33 
34 const std::unordered_map<BundleEventType, BMSEventType> BUNDLE_SYS_EVENT_MAP = {
35     { BundleEventType::INSTALL, BMSEventType::BUNDLE_INSTALL },
36     { BundleEventType::UNINSTALL, BMSEventType::BUNDLE_UNINSTALL },
37     { BundleEventType::UPDATE, BMSEventType::BUNDLE_UPDATE },
38     { BundleEventType::RECOVER, BMSEventType::PRE_BUNDLE_RECOVER },
39     { BundleEventType::QUICK_FIX, BMSEventType::APPLY_QUICK_FIX }
40 };
41 }
42 
SendBundleSystemEvent(BundleEventType bundleEventType,const EventInfo & eventInfo)43 void EventReport::SendBundleSystemEvent(BundleEventType bundleEventType, const EventInfo& eventInfo)
44 {
45     BMSEventType bmsEventType = BMSEventType::UNKNOW;
46     std::unordered_map<BundleEventType, BMSEventType>::const_iterator iter;
47     if (eventInfo.errCode != ERR_OK) {
48         iter = BUNDLE_EXCEPTION_SYS_EVENT_MAP.find(bundleEventType);
49         if (iter != BUNDLE_EXCEPTION_SYS_EVENT_MAP.end()) {
50             bmsEventType = iter->second;
51         }
52 
53         SendSystemEvent(bmsEventType, eventInfo);
54         return;
55     }
56 
57     iter = BUNDLE_SYS_EVENT_MAP.find(bundleEventType);
58     if (iter != BUNDLE_SYS_EVENT_MAP.end()) {
59         bmsEventType = iter->second;
60     }
61 
62     SendSystemEvent(bmsEventType, eventInfo);
63 }
64 
SendScanSysEvent(BMSEventType bMSEventType)65 void EventReport::SendScanSysEvent(BMSEventType bMSEventType)
66 {
67     EventInfo eventInfo;
68     eventInfo.timeStamp = BundleUtil::GetCurrentTimeMs();
69     EventReport::SendSystemEvent(bMSEventType, eventInfo);
70 }
71 
SendUserSysEvent(UserEventType userEventType,int32_t userId)72 void EventReport::SendUserSysEvent(UserEventType userEventType, int32_t userId)
73 {
74     EventInfo eventInfo;
75     eventInfo.timeStamp = BundleUtil::GetCurrentTimeMs();
76     eventInfo.userId = userId;
77     eventInfo.userEventType = userEventType;
78     EventReport::SendSystemEvent(BMSEventType::BMS_USER_EVENT, eventInfo);
79 }
80 
SendComponentStateSysEventForException(const std::string & bundleName,const std::string & abilityName,int32_t userId,bool isEnable,int32_t appIndex,const std::string & caller)81 void EventReport::SendComponentStateSysEventForException(const std::string &bundleName, const std::string &abilityName,
82     int32_t userId, bool isEnable, int32_t appIndex, const std::string &caller)
83 {
84     EventInfo eventInfo;
85     eventInfo.bundleName = bundleName;
86     eventInfo.abilityName = abilityName;
87     eventInfo.userId = userId;
88     eventInfo.isEnable = isEnable;
89     eventInfo.appIndex = appIndex;
90     eventInfo.callingBundleName = caller;
91     BMSEventType bmsEventType = BMSEventType::BUNDLE_STATE_CHANGE_EXCEPTION;
92 
93     EventReport::SendSystemEvent(bmsEventType, eventInfo);
94 }
95 
SendComponentStateSysEvent(const std::string & bundleName,const std::string & abilityName,int32_t userId,bool isEnable,int32_t appIndex,const std::string & caller)96 void EventReport::SendComponentStateSysEvent(const std::string &bundleName, const std::string &abilityName,
97     int32_t userId, bool isEnable, int32_t appIndex, const std::string &caller)
98 {
99     EventInfo eventInfo;
100     eventInfo.bundleName = bundleName;
101     eventInfo.abilityName = abilityName;
102     eventInfo.userId = userId;
103     eventInfo.isEnable = isEnable;
104     eventInfo.appIndex = appIndex;
105     eventInfo.callingBundleName = caller;
106     BMSEventType bmsEventType = BMSEventType::BUNDLE_STATE_CHANGE;
107 
108     EventReport::SendSystemEvent(bmsEventType, eventInfo);
109 }
110 
SendCleanCacheSysEvent(const std::string & bundleName,int32_t userId,bool isCleanCache,bool exception)111 void EventReport::SendCleanCacheSysEvent(
112     const std::string &bundleName, int32_t userId, bool isCleanCache, bool exception)
113 {
114     EventInfo eventInfo;
115     eventInfo.bundleName = bundleName;
116     eventInfo.userId = userId;
117     eventInfo.isCleanCache = isCleanCache;
118     BMSEventType bmsEventType;
119     if (exception) {
120         bmsEventType = BMSEventType::BUNDLE_CLEAN_CACHE_EXCEPTION;
121     } else {
122         bmsEventType = BMSEventType::BUNDLE_CLEAN_CACHE;
123     }
124 
125     EventReport::SendSystemEvent(bmsEventType, eventInfo);
126 }
127 
SendCleanCacheSysEventWithIndex(const std::string & bundleName,int32_t userId,int32_t appIndex,bool isCleanCache,bool exception)128 void EventReport::SendCleanCacheSysEventWithIndex(
129     const std::string &bundleName, int32_t userId, int32_t appIndex, bool isCleanCache, bool exception)
130 {
131     EventInfo eventInfo;
132     eventInfo.bundleName = bundleName;
133     eventInfo.userId = userId;
134     eventInfo.appIndex = appIndex;
135     eventInfo.isCleanCache = isCleanCache;
136     BMSEventType bmsEventType;
137     if (exception) {
138         bmsEventType = BMSEventType::BUNDLE_CLEAN_CACHE_EXCEPTION;
139     } else {
140         bmsEventType = BMSEventType::BUNDLE_CLEAN_CACHE;
141     }
142 
143     EventReport::SendSystemEvent(bmsEventType, eventInfo);
144 }
145 
SendQueryAbilityInfoByContinueTypeSysEvent(const std::string & bundleName,const std::string & abilityName,ErrCode errCode,int32_t userId,const std::string & continueType)146 void EventReport::SendQueryAbilityInfoByContinueTypeSysEvent(const std::string &bundleName,
147     const std::string &abilityName, ErrCode errCode, int32_t userId, const std::string &continueType)
148 {
149     EventInfo eventInfo;
150     eventInfo.bundleName = bundleName;
151     eventInfo.abilityName = abilityName;
152     eventInfo.errCode = errCode;
153     eventInfo.continueType = continueType;
154     eventInfo.userId = userId,
155     EventReport::SendSystemEvent(BMSEventType::QUERY_OF_CONTINUE_TYPE, eventInfo);
156 }
157 
SendCpuSceneEvent(const std::string & processName,const int32_t sceneId)158 void EventReport::SendCpuSceneEvent(const std::string &processName, const int32_t sceneId)
159 {
160     EventInfo eventInfo;
161     eventInfo.sceneId = sceneId;
162     eventInfo.processName = processName;
163     eventInfo.timeStamp = BundleUtil::GetCurrentTimeMs();
164     EventReport::SendSystemEvent(BMSEventType::CPU_SCENE_ENTRY, eventInfo);
165 }
166 
SendFreeInstallEvent(const std::string & bundleName,const std::string & abilityName,const std::string & moduleName,bool isFreeInstall,int64_t timeStamp)167 void EventReport::SendFreeInstallEvent(const std::string &bundleName, const std::string &abilityName,
168     const std::string &moduleName, bool isFreeInstall, int64_t timeStamp)
169 {
170     EventInfo eventInfo;
171     eventInfo.bundleName = bundleName;
172     eventInfo.abilityName = abilityName;
173     eventInfo.moduleName = moduleName;
174     eventInfo.isFreeInstall = isFreeInstall;
175     eventInfo.timeStamp = timeStamp;
176     EventReport::SendSystemEvent(BMSEventType::FREE_INSTALL_EVENT, eventInfo);
177 }
178 
SendDiskSpaceEvent(const std::string & fileName,int64_t freeSize,int32_t operationType)179 void EventReport::SendDiskSpaceEvent(const std::string &fileName,
180     int64_t freeSize, int32_t operationType)
181 {
182     EventInfo eventInfo;
183     eventInfo.fileName = fileName;
184     eventInfo.freeSize = freeSize;
185     eventInfo.operationType = operationType;
186     EventReport::SendSystemEvent(BMSEventType::BMS_DISK_SPACE, eventInfo);
187 }
188 
SendSystemEvent(BMSEventType bmsEventType,const EventInfo & eventInfo)189 void EventReport::SendSystemEvent(BMSEventType bmsEventType, const EventInfo& eventInfo)
190 {
191 #ifdef HISYSEVENT_ENABLE
192     InnerEventReport::SendSystemEvent(bmsEventType, eventInfo);
193 #else
194     APP_LOGD("Hisysevent is disabled");
195 #endif
196 }
197 }  // namespace AppExecFwk
198 }  // namespace OHOS