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 
HandlePackageRemoved(const OHOS::AAFwk::Want & want,bool isSandBoxApp,OnPackageRemoved onPackageRemoved)38 void HandlePackageRemoved(const OHOS::AAFwk::Want &want, bool isSandBoxApp, OnPackageRemoved onPackageRemoved)
39 {
40     int userId = want.GetIntParam(USER_ID, INVALID_USERID);
41     std::string appId = want.GetStringParam(APP_ID);
42     int appIndex = isSandBoxApp ? want.GetIntParam(SANDBOX_APP_INDEX, -1) : want.GetIntParam(APP_INDEX, -1);
43     if (appId.empty() || userId == INVALID_USERID || appIndex == -1) {
44         LOGE("[FATAL]Get removed owner info failed, userId=%{public}d, appId=%{public}s, appIndex=%{public}d",
45             userId, appId.c_str(), appIndex);
46         return;
47     }
48 
49     std::string owner = appId + '_' + std::to_string(appIndex);
50     std::string bundleName = want.GetBundle();
51     if (onPackageRemoved != nullptr) {
52         onPackageRemoved(userId, reinterpret_cast<const uint8_t *>(owner.c_str()), owner.size(),
53             reinterpret_cast<const uint8_t *>(bundleName.c_str()), appIndex);
54     }
55     LOGI("[INFO]Receive event: PACKAGE_REMOVED, userId=%{public}d, appId=%{public}s, appIndex=%{public}d, ",
56         userId, appId.c_str(), appIndex);
57 }
58 
HandleAppRestore(const OHOS::AAFwk::Want & want,OnAppRestore onAppRestore)59 void HandleAppRestore(const OHOS::AAFwk::Want &want, OnAppRestore onAppRestore)
60 {
61     if (onAppRestore != nullptr) {
62         int userId = want.GetIntParam(USER_ID, INVALID_USERID);
63         std::string bundleName = want.GetStringParam(BUNDLE_NAME);
64 
65         int appIndex = want.GetIntParam(SANDBOX_APP_INDEX, -1);
66         if (appIndex == -1) {
67             LOGI("[INFO]Get app restore info failed, default as index 0.");
68             appIndex = 0;
69         }
70 
71         onAppRestore(userId, reinterpret_cast<const uint8_t *>(bundleName.c_str()), appIndex);
72         LOGI("[INFO]Receive event: RESTORE_START.");
73     }
74 }
75 
76 class SystemEventHandler : public CommonEventSubscriber {
77 public:
SystemEventHandler(const CommonEventSubscribeInfo & subscribeInfo,const EventCallBack eventCallBack)78     explicit SystemEventHandler(const CommonEventSubscribeInfo &subscribeInfo, const EventCallBack eventCallBack)
79         : CommonEventSubscriber(subscribeInfo), eventCallBack(eventCallBack) {}
80     ~SystemEventHandler() = default;
OnReceiveEvent(const CommonEventData & data)81     void OnReceiveEvent(const CommonEventData &data) override
82     {
83         long startTime = std::clock();
84         auto want = data.GetWant();
85         std::string action = want.GetAction();
86         if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
87             HandlePackageRemoved(want, false, this->eventCallBack.onPackageRemoved);
88         } else if (action == CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) {
89             HandlePackageRemoved(want, true, this->eventCallBack.onPackageRemoved);
90         } else if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
91             int userId = data.GetCode();
92             if (this->eventCallBack.onUserRemoved != nullptr) {
93                 this->eventCallBack.onUserRemoved(userId);
94             }
95             LOGI("[INFO] Receive event: USER_REMOVED, userId=%{public}d", userId);
96         } else if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
97             if (this->eventCallBack.onScreenOff != nullptr) {
98                 this->eventCallBack.onScreenOff();
99             }
100             LOGI("[INFO]Receive event: SCREEN_OFF, start_time: %{public}ld", startTime);
101         } else if (action == CommonEventSupport::COMMON_EVENT_CHARGING) {
102             if (this->eventCallBack.onCharging != nullptr) {
103                 this->eventCallBack.onCharging();
104             }
105             LOGI("[INFO]Receive event: CHARGING, start_time: %{public}ld", startTime);
106         } else if (action == COMMON_EVENT_RESTORE_START) {
107             HandleAppRestore(want, this->eventCallBack.onAppRestore);
108         } else if (action == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
109             if (this->eventCallBack.onUserUnlocked != nullptr) {
110                 int userId = data.GetCode();
111 
112                 this->eventCallBack.onUserUnlocked(userId);
113             }
114             LOGI("[INFO]Receive event: USER_UNLOCKED, start_time: %{public}ld", startTime);
115         } else if (action == COMMON_EVENT_USER_PIN_CREATED) {
116             if (this->eventCallBack.onUserUnlocked != nullptr) {
117                 int userId = data.GetCode();
118                 this->eventCallBack.onUserUnlocked(userId);
119             }
120             LOGI("[INFO]Receive event: USER_PIN_CREATED_EVENT, start_time: %{public}ld", startTime);
121         } else {
122             LOGW("[WARNING]Receive unknown event: %{public}s", action.c_str());
123         }
124     }
125 private:
126     const EventCallBack eventCallBack;
127 };
128 
129 std::shared_ptr<SystemEventHandler> g_eventHandler = nullptr;
130 std::shared_ptr<SystemEventHandler> g_pinEventHandler = nullptr;
SubscribePinEvent(const EventCallBack eventCallBack)131 bool SubscribePinEvent(const EventCallBack eventCallBack)
132 {
133     MatchingSkills matchingSkills;
134     matchingSkills.AddEvent(COMMON_EVENT_USER_PIN_CREATED);
135     CommonEventSubscribeInfo info(matchingSkills);
136     info.SetPermission(PERMISSION_MANAGE_USER_IDM);
137     if (g_pinEventHandler == nullptr) {
138         g_pinEventHandler = std::shared_ptr<SystemEventHandler>(
139             new (std::nothrow) SystemEventHandler(info, eventCallBack));
140         if (g_pinEventHandler == nullptr) {
141             LOGE("[FATAL]Asset pin event handler is nullptr.");
142             return false;
143         }
144     }
145 
146     return CommonEventManager::SubscribeCommonEvent(g_pinEventHandler);
147 }
148 
UnSubscribePinEvent(void)149 bool UnSubscribePinEvent(void)
150 {
151     if (g_pinEventHandler == nullptr) {
152         LOGW("Asset pin event handler is nullptr, no need to unsubscribe.");
153         return false;
154     }
155 
156     bool res = CommonEventManager::UnSubscribeCommonEvent(g_pinEventHandler);
157     g_pinEventHandler = nullptr;
158     return res;
159 }
160 
161 }
162 
SubscribeSystemEvent(const EventCallBack eventCallBack)163 bool SubscribeSystemEvent(const EventCallBack eventCallBack)
164 {
165     bool ret = SubscribePinEvent(eventCallBack);
166     LOGI("Subscribe pin event result: %d", ret);
167 
168     MatchingSkills matchingSkills;
169     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
170     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
171     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
172     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
173     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CHARGING);
174     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
175     matchingSkills.AddEvent(COMMON_EVENT_RESTORE_START);
176     CommonEventSubscribeInfo info(matchingSkills);
177     if (g_eventHandler == nullptr) {
178         g_eventHandler = std::shared_ptr<SystemEventHandler>(
179             new (std::nothrow) SystemEventHandler(info, eventCallBack));
180         if (g_eventHandler == nullptr) {
181             LOGE("[FATAL]Asset system event handler is nullptr.");
182             return false;
183         }
184     }
185 
186     return CommonEventManager::SubscribeCommonEvent(g_eventHandler);
187 }
188 
UnSubscribeSystemEvent(void)189 bool UnSubscribeSystemEvent(void)
190 {
191     bool ret = UnSubscribePinEvent();
192     LOGI("UnSubscribe pin event result: %d", ret);
193 
194     if (g_eventHandler == nullptr) {
195         LOGW("Asset system event handler is nullptr, no need to unsubscribe.");
196         return false;
197     }
198 
199     bool res = CommonEventManager::UnSubscribeCommonEvent(g_eventHandler);
200     g_eventHandler = nullptr;
201     return res;
202 }
203