• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <ctime>
17 
18 #include "system_event_wrapper.h"
19 
20 #include "bundle_constants.h"
21 #include "common_event_manager.h"
22 #include "common_event_subscriber.h"
23 #include "common_event_support.h"
24 
25 #include "asset_log.h"
26 
27 namespace {
28 using namespace OHOS::AppExecFwk::Constants;
29 using namespace OHOS::EventFwk;
30 
31 const char * const APP_ID = "appId";
32 const char * const APP_INDEX = "appIndex";
33 const char * const COMMON_EVENT_RESTORE_START = "usual.event.RESTORE_START";
34 const char * const COMMON_EVENT_USER_PIN_CREATED = "USER_PIN_CREATED_EVENT";
35 const char * const BUNDLE_NAME = "bundleName";
36 const char * const PERMISSION_MANAGE_USER_IDM = "ohos.permission.MANAGE_USER_IDM";
37 const char * const DEVELOPER_ID = "developerId";
38 const char * const GROUP_IDS = "assetAccessGroups";
39 const char * const OWNER_INFO_SEPARATOR = "_";
40 const char * const GROUP_SEPARATOR = ",";
41 
ParseGroupIds(const std::string & groupIds,std::vector<std::string> & groupIdStrs,std::vector<ConstAssetBlob> & groupIdBlobs,ConstAssetBlobArray & groupIdBlobArray)42 void ParseGroupIds(const std::string &groupIds, std::vector<std::string> &groupIdStrs,
43     std::vector<ConstAssetBlob> &groupIdBlobs, ConstAssetBlobArray &groupIdBlobArray)
44 {
45     if (!groupIds.empty()) {
46         size_t start = 0;
47         size_t end;
48         while ((end = groupIds.find(GROUP_SEPARATOR, start)) != std::string::npos) {
49             groupIdStrs.push_back(groupIds.substr(start, end - start));
50             start = ++end;
51         }
52         groupIdStrs.push_back(groupIds.substr(start, end));
53         for (const std::string &groupIdStr : groupIdStrs) {
54             groupIdBlobs.push_back({ .size = groupIdStr.size(),
55                 .data = reinterpret_cast<const uint8_t *>(groupIdStr.c_str()) });
56         }
57         groupIdBlobArray = { .size = groupIdBlobs.size(),
58             .blob = reinterpret_cast<const ConstAssetBlob *>(&groupIdBlobs[0]) };
59     } else {
60         groupIdBlobArray = { .size = 0, .blob = nullptr };
61     }
62 }
63 
HandlePackageRemoved(const OHOS::AAFwk::Want & want,bool isSandBoxApp,OnPackageRemoved onPackageRemoved)64 void HandlePackageRemoved(const OHOS::AAFwk::Want &want, bool isSandBoxApp, OnPackageRemoved onPackageRemoved)
65 {
66     int userId = want.GetIntParam(USER_ID, INVALID_USERID);
67     std::string appId = want.GetStringParam(APP_ID);
68     int appIndex = isSandBoxApp ? want.GetIntParam(SANDBOX_APP_INDEX, -1) : want.GetIntParam(APP_INDEX, -1);
69     if (appId.empty() || userId == INVALID_USERID || appIndex == -1) {
70         LOGE("[FATAL]Get removed owner info failed, userId=%{public}d, appId=%{public}s, appIndex=%{public}d",
71             userId, appId.c_str(), appIndex);
72         return;
73     }
74     std::string owner = appId + OWNER_INFO_SEPARATOR + std::to_string(appIndex);
75     ConstAssetBlob ownerBlob = { .size = owner.size(), .data = reinterpret_cast<const uint8_t *>(owner.c_str()) };
76 
77     std::string bundleName = want.GetBundle();
78     ConstAssetBlob bundleNameBlob = { .size = bundleName.size(),
79         .data = reinterpret_cast<const uint8_t *>(bundleName.c_str()) };
80 
81     std::string developerId = want.GetStringParam(DEVELOPER_ID);
82     ConstAssetBlob developerIdBlob = {
83         .size = developerId.size(), .data = reinterpret_cast<const uint8_t *>(developerId.c_str())
84     };
85     std::string groupIds = want.GetStringParam(GROUP_IDS);
86     std::vector<ConstAssetBlob> groupIdBlobs;
87     std::vector<std::string> groupIdStrs;
88     ConstAssetBlobArray groupIdBlobArray;
89     ParseGroupIds(groupIds, groupIdStrs, groupIdBlobs, groupIdBlobArray);
90 
91     if (onPackageRemoved != nullptr) {
92         onPackageRemoved({ userId, appIndex, ownerBlob, developerIdBlob, groupIdBlobArray, bundleNameBlob });
93     }
94     LOGI("[INFO]Receive event: PACKAGE_REMOVED, userId=%{public}d, appId=%{public}s, appIndex=%{public}d", userId,
95         appId.c_str(), appIndex);
96 }
97 
HandleAppRestore(const OHOS::AAFwk::Want & want,OnAppRestore onAppRestore)98 void HandleAppRestore(const OHOS::AAFwk::Want &want, OnAppRestore onAppRestore)
99 {
100     if (onAppRestore != nullptr) {
101         int userId = want.GetIntParam(USER_ID, INVALID_USERID);
102         std::string bundleName = want.GetStringParam(BUNDLE_NAME);
103 
104         int appIndex = want.GetIntParam(SANDBOX_APP_INDEX, -1);
105         if (appIndex == -1) {
106             LOGI("[INFO]Get app restore info failed, default as index 0.");
107             appIndex = 0;
108         }
109 
110         onAppRestore(userId, reinterpret_cast<const uint8_t *>(bundleName.c_str()), appIndex);
111         LOGI("[INFO]Receive event: RESTORE_START.");
112     }
113 }
114 
115 class SystemEventHandler : public CommonEventSubscriber {
116 public:
SystemEventHandler(const CommonEventSubscribeInfo & subscribeInfo,const EventCallBack eventCallBack)117     explicit SystemEventHandler(const CommonEventSubscribeInfo &subscribeInfo, const EventCallBack eventCallBack)
118         : CommonEventSubscriber(subscribeInfo), eventCallBack(eventCallBack) {}
119     ~SystemEventHandler() = default;
OnReceiveEvent(const CommonEventData & data)120     void OnReceiveEvent(const CommonEventData &data) override
121     {
122         long startTime = std::clock();
123         auto want = data.GetWant();
124         std::string action = want.GetAction();
125         if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
126             HandlePackageRemoved(want, false, this->eventCallBack.onPackageRemoved);
127         } else if (action == CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) {
128             HandlePackageRemoved(want, true, this->eventCallBack.onPackageRemoved);
129         } else if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
130             int userId = data.GetCode();
131             if (this->eventCallBack.onUserRemoved != nullptr) {
132                 this->eventCallBack.onUserRemoved(userId);
133             }
134             LOGI("[INFO] Receive event: USER_REMOVED, userId=%{public}d", userId);
135         } else if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
136             if (this->eventCallBack.onScreenOff != nullptr) {
137                 this->eventCallBack.onScreenOff();
138             }
139             LOGI("[INFO]Receive event: SCREEN_OFF, start_time: %{public}ld", startTime);
140         } else if (action == CommonEventSupport::COMMON_EVENT_CHARGING) {
141             if (this->eventCallBack.onCharging != nullptr) {
142                 this->eventCallBack.onCharging();
143             }
144             LOGI("[INFO]Receive event: CHARGING, start_time: %{public}ld", startTime);
145         } else if (action == COMMON_EVENT_RESTORE_START) {
146             HandleAppRestore(want, this->eventCallBack.onAppRestore);
147         } else if (action == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
148             if (this->eventCallBack.onUserUnlocked != nullptr) {
149                 int userId = data.GetCode();
150                 this->eventCallBack.onUserUnlocked(userId);
151             }
152             LOGI("[INFO]Receive event: USER_UNLOCKED, start_time: %{public}ld", startTime);
153         } else if (action == COMMON_EVENT_USER_PIN_CREATED) {
154             if (this->eventCallBack.onUserUnlocked != nullptr) {
155                 int userId = data.GetCode();
156                 this->eventCallBack.onUserUnlocked(userId);
157             }
158             LOGI("[INFO]Receive event: USER_PIN_CREATED_EVENT, start_time: %{public}ld", startTime);
159         } else {
160             LOGW("[WARNING]Receive unknown event: %{public}s", action.c_str());
161         }
162     }
163 private:
164     const EventCallBack eventCallBack;
165 };
166 
167 std::shared_ptr<SystemEventHandler> g_eventHandler = nullptr;
168 std::shared_ptr<SystemEventHandler> g_pinEventHandler = nullptr;
SubscribePinEvent(const EventCallBack eventCallBack)169 bool SubscribePinEvent(const EventCallBack eventCallBack)
170 {
171     MatchingSkills matchingSkills;
172     matchingSkills.AddEvent(COMMON_EVENT_USER_PIN_CREATED);
173     CommonEventSubscribeInfo info(matchingSkills);
174     info.SetPermission(PERMISSION_MANAGE_USER_IDM);
175     if (g_pinEventHandler == nullptr) {
176         g_pinEventHandler = std::shared_ptr<SystemEventHandler>(
177             new (std::nothrow) SystemEventHandler(info, eventCallBack));
178         if (g_pinEventHandler == nullptr) {
179             LOGE("[FATAL]Asset pin event handler is nullptr.");
180             return false;
181         }
182     }
183 
184     return CommonEventManager::SubscribeCommonEvent(g_pinEventHandler);
185 }
186 
UnSubscribePinEvent(void)187 bool UnSubscribePinEvent(void)
188 {
189     if (g_pinEventHandler == nullptr) {
190         LOGW("Asset pin event handler is nullptr, no need to unsubscribe.");
191         return false;
192     }
193 
194     bool res = CommonEventManager::UnSubscribeCommonEvent(g_pinEventHandler);
195     g_pinEventHandler = nullptr;
196     return res;
197 }
198 
199 }
200 
SubscribeSystemEvent(const EventCallBack eventCallBack)201 bool SubscribeSystemEvent(const EventCallBack eventCallBack)
202 {
203     bool ret = SubscribePinEvent(eventCallBack);
204     LOGI("Subscribe pin event result: %d", ret);
205 
206     MatchingSkills matchingSkills;
207     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
208     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
209     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
210     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
211     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CHARGING);
212     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
213     matchingSkills.AddEvent(COMMON_EVENT_RESTORE_START);
214     CommonEventSubscribeInfo info(matchingSkills);
215     if (g_eventHandler == nullptr) {
216         g_eventHandler = std::shared_ptr<SystemEventHandler>(
217             new (std::nothrow) SystemEventHandler(info, eventCallBack));
218         if (g_eventHandler == nullptr) {
219             LOGE("[FATAL]Asset system event handler is nullptr.");
220             return false;
221         }
222     }
223 
224     return CommonEventManager::SubscribeCommonEvent(g_eventHandler);
225 }
226 
UnSubscribeSystemEvent(void)227 bool UnSubscribeSystemEvent(void)
228 {
229     bool ret = UnSubscribePinEvent();
230     LOGI("UnSubscribe pin event result: %d", ret);
231 
232     if (g_eventHandler == nullptr) {
233         LOGW("Asset system event handler is nullptr, no need to unsubscribe.");
234         return false;
235     }
236 
237     bool res = CommonEventManager::UnSubscribeCommonEvent(g_eventHandler);
238     g_eventHandler = nullptr;
239     return res;
240 }
241