1dfe32fa1Soh_ci/*
2dfe32fa1Soh_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3dfe32fa1Soh_ci * Licensed under the Apache License, Version 2.0 (the "License");
4dfe32fa1Soh_ci * you may not use this file except in compliance with the License.
5dfe32fa1Soh_ci * You may obtain a copy of the License at
6dfe32fa1Soh_ci *
7dfe32fa1Soh_ci *     http://www.apache.org/licenses/LICENSE-2.0
8dfe32fa1Soh_ci *
9dfe32fa1Soh_ci * Unless required by applicable law or agreed to in writing, software
10dfe32fa1Soh_ci * distributed under the License is distributed on an "AS IS" BASIS,
11dfe32fa1Soh_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12dfe32fa1Soh_ci * See the License for the specific language governing permissions and
13dfe32fa1Soh_ci * limitations under the License.
14dfe32fa1Soh_ci */
15dfe32fa1Soh_ci
16dfe32fa1Soh_ci#include <ctime>
17dfe32fa1Soh_ci
18dfe32fa1Soh_ci#include "system_event_wrapper.h"
19dfe32fa1Soh_ci
20dfe32fa1Soh_ci#include "bundle_constants.h"
21dfe32fa1Soh_ci#include "common_event_manager.h"
22dfe32fa1Soh_ci#include "common_event_subscriber.h"
23dfe32fa1Soh_ci#include "common_event_support.h"
24dfe32fa1Soh_ci
25dfe32fa1Soh_ci#include "asset_log.h"
26dfe32fa1Soh_ci
27dfe32fa1Soh_cinamespace {
28dfe32fa1Soh_ciusing namespace OHOS::AppExecFwk::Constants;
29dfe32fa1Soh_ciusing namespace OHOS::EventFwk;
30dfe32fa1Soh_ci
31dfe32fa1Soh_ciconst char * const APP_ID = "appId";
32dfe32fa1Soh_ciconst char * const COMMON_EVENT_RESTORE_START = "usual.event.RESTORE_START";
33dfe32fa1Soh_ciconst char * const COMMON_EVENT_USER_PIN_CREATED = "USER_PIN_CREATED_EVENT";
34dfe32fa1Soh_ciconst char * const BUNDLE_NAME = "bundleName";
35dfe32fa1Soh_ciconst char * const PERMISSION_MANAGE_USER_IDM = "ohos.permission.MANAGE_USER_IDM";
36dfe32fa1Soh_ci
37dfe32fa1Soh_civoid HandlePackageRemoved(const OHOS::AAFwk::Want &want, bool isSandBoxApp, OnPackageRemoved onPackageRemoved)
38dfe32fa1Soh_ci{
39dfe32fa1Soh_ci    int userId = want.GetIntParam(USER_ID, INVALID_USERID);
40dfe32fa1Soh_ci    std::string appId = want.GetStringParam(APP_ID);
41dfe32fa1Soh_ci    int appIndex = isSandBoxApp ? want.GetIntParam(SANDBOX_APP_INDEX, -1) : 0;
42dfe32fa1Soh_ci    if (appId.empty() || userId == INVALID_USERID || appIndex == -1) {
43dfe32fa1Soh_ci        LOGE("[FATAL]Get removed owner info failed, userId=%{public}d, appId=%{public}s, appIndex=%{public}d",
44dfe32fa1Soh_ci            userId, appId.c_str(), appIndex);
45dfe32fa1Soh_ci        return;
46dfe32fa1Soh_ci    }
47dfe32fa1Soh_ci
48dfe32fa1Soh_ci    std::string owner = appId + '_' + std::to_string(appIndex);
49dfe32fa1Soh_ci    std::string bundleName = want.GetBundle();
50dfe32fa1Soh_ci    if (onPackageRemoved != nullptr) {
51dfe32fa1Soh_ci        onPackageRemoved(userId, reinterpret_cast<const uint8_t *>(owner.c_str()), owner.size(),
52dfe32fa1Soh_ci            reinterpret_cast<const uint8_t *>(bundleName.c_str()), appIndex);
53dfe32fa1Soh_ci    }
54dfe32fa1Soh_ci    LOGI("[INFO]Receive event: PACKAGE_REMOVED, userId=%{public}d, appId=%{public}s, appIndex=%{public}d, ",
55dfe32fa1Soh_ci        userId, appId.c_str(), appIndex);
56dfe32fa1Soh_ci}
57dfe32fa1Soh_ci
58dfe32fa1Soh_civoid HandleAppRestore(const OHOS::AAFwk::Want &want, OnAppRestore onAppRestore)
59dfe32fa1Soh_ci{
60dfe32fa1Soh_ci    if (onAppRestore != nullptr) {
61dfe32fa1Soh_ci        int userId = want.GetIntParam(USER_ID, INVALID_USERID);
62dfe32fa1Soh_ci        std::string bundleName = want.GetStringParam(BUNDLE_NAME);
63dfe32fa1Soh_ci
64dfe32fa1Soh_ci        int appIndex = want.GetIntParam(SANDBOX_APP_INDEX, -1);
65dfe32fa1Soh_ci        if (appIndex == -1) {
66dfe32fa1Soh_ci            LOGI("[INFO]Get app restore info failed, default as index 0.");
67dfe32fa1Soh_ci            appIndex = 0;
68dfe32fa1Soh_ci        }
69dfe32fa1Soh_ci
70dfe32fa1Soh_ci        onAppRestore(userId, reinterpret_cast<const uint8_t *>(bundleName.c_str()), appIndex);
71dfe32fa1Soh_ci        LOGI("[INFO]Receive event: RESTORE_START.");
72dfe32fa1Soh_ci    }
73dfe32fa1Soh_ci}
74dfe32fa1Soh_ci
75dfe32fa1Soh_ciclass SystemEventHandler : public CommonEventSubscriber {
76dfe32fa1Soh_cipublic:
77dfe32fa1Soh_ci    explicit SystemEventHandler(const CommonEventSubscribeInfo &subscribeInfo, const EventCallBack eventCallBack)
78dfe32fa1Soh_ci        : CommonEventSubscriber(subscribeInfo), eventCallBack(eventCallBack) {}
79dfe32fa1Soh_ci    ~SystemEventHandler() = default;
80dfe32fa1Soh_ci    void OnReceiveEvent(const CommonEventData &data) override
81dfe32fa1Soh_ci    {
82dfe32fa1Soh_ci        long startTime = std::clock();
83dfe32fa1Soh_ci        auto want = data.GetWant();
84dfe32fa1Soh_ci        std::string action = want.GetAction();
85dfe32fa1Soh_ci        if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
86dfe32fa1Soh_ci            HandlePackageRemoved(want, false, this->eventCallBack.onPackageRemoved);
87dfe32fa1Soh_ci        } else if (action == CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) {
88dfe32fa1Soh_ci            HandlePackageRemoved(want, true, this->eventCallBack.onPackageRemoved);
89dfe32fa1Soh_ci        } else if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
90dfe32fa1Soh_ci            int userId = data.GetCode();
91dfe32fa1Soh_ci            if (this->eventCallBack.onUserRemoved != nullptr) {
92dfe32fa1Soh_ci                this->eventCallBack.onUserRemoved(userId);
93dfe32fa1Soh_ci            }
94dfe32fa1Soh_ci            LOGI("[INFO] Receive event: USER_REMOVED, userId=%{public}d", userId);
95dfe32fa1Soh_ci        } else if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
96dfe32fa1Soh_ci            if (this->eventCallBack.onScreenOff != nullptr) {
97dfe32fa1Soh_ci                this->eventCallBack.onScreenOff();
98dfe32fa1Soh_ci            }
99dfe32fa1Soh_ci            LOGI("[INFO]Receive event: SCREEN_OFF, start_time: %{public}ld", startTime);
100dfe32fa1Soh_ci        } else if (action == CommonEventSupport::COMMON_EVENT_CHARGING) {
101dfe32fa1Soh_ci            if (this->eventCallBack.onCharging != nullptr) {
102dfe32fa1Soh_ci                this->eventCallBack.onCharging();
103dfe32fa1Soh_ci            }
104dfe32fa1Soh_ci            LOGI("[INFO]Receive event: CHARGING, start_time: %{public}ld", startTime);
105dfe32fa1Soh_ci        } else if (action == COMMON_EVENT_RESTORE_START) {
106dfe32fa1Soh_ci            HandleAppRestore(want, this->eventCallBack.onAppRestore);
107dfe32fa1Soh_ci        } else if (action == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
108dfe32fa1Soh_ci            if (this->eventCallBack.onUserUnlocked != nullptr) {
109dfe32fa1Soh_ci                int userId = data.GetCode();
110dfe32fa1Soh_ci
111dfe32fa1Soh_ci                this->eventCallBack.onUserUnlocked(userId);
112dfe32fa1Soh_ci            }
113dfe32fa1Soh_ci            LOGI("[INFO]Receive event: USER_UNLOCKED, start_time: %{public}ld", startTime);
114dfe32fa1Soh_ci        } else if (action == COMMON_EVENT_USER_PIN_CREATED) {
115dfe32fa1Soh_ci            if (this->eventCallBack.onUserUnlocked != nullptr) {
116dfe32fa1Soh_ci                int userId = data.GetCode();
117dfe32fa1Soh_ci                this->eventCallBack.onUserUnlocked(userId);
118dfe32fa1Soh_ci            }
119dfe32fa1Soh_ci            LOGI("[INFO]Receive event: USER_PIN_CREATED_EVENT, start_time: %{public}ld", startTime);
120dfe32fa1Soh_ci        } else {
121dfe32fa1Soh_ci            LOGW("[WARNING]Receive unknown event: %{public}s", action.c_str());
122dfe32fa1Soh_ci        }
123dfe32fa1Soh_ci    }
124dfe32fa1Soh_ciprivate:
125dfe32fa1Soh_ci    const EventCallBack eventCallBack;
126dfe32fa1Soh_ci};
127dfe32fa1Soh_ci
128dfe32fa1Soh_cistd::shared_ptr<SystemEventHandler> g_eventHandler = nullptr;
129dfe32fa1Soh_cistd::shared_ptr<SystemEventHandler> g_pinEventHandler = nullptr;
130dfe32fa1Soh_cibool SubscribePinEvent(const EventCallBack eventCallBack)
131dfe32fa1Soh_ci{
132dfe32fa1Soh_ci    MatchingSkills matchingSkills;
133dfe32fa1Soh_ci    matchingSkills.AddEvent(COMMON_EVENT_USER_PIN_CREATED);
134dfe32fa1Soh_ci    CommonEventSubscribeInfo info(matchingSkills);
135dfe32fa1Soh_ci    info.SetPermission(PERMISSION_MANAGE_USER_IDM);
136dfe32fa1Soh_ci    if (g_pinEventHandler == nullptr) {
137dfe32fa1Soh_ci        g_pinEventHandler = std::shared_ptr<SystemEventHandler>(
138dfe32fa1Soh_ci            new (std::nothrow) SystemEventHandler(info, eventCallBack));
139dfe32fa1Soh_ci        if (g_pinEventHandler == nullptr) {
140dfe32fa1Soh_ci            LOGE("[FATAL]Asset pin event handler is nullptr.");
141dfe32fa1Soh_ci            return false;
142dfe32fa1Soh_ci        }
143dfe32fa1Soh_ci    }
144dfe32fa1Soh_ci
145dfe32fa1Soh_ci    return CommonEventManager::SubscribeCommonEvent(g_pinEventHandler);
146dfe32fa1Soh_ci}
147dfe32fa1Soh_ci
148dfe32fa1Soh_cibool UnSubscribePinEvent(void)
149dfe32fa1Soh_ci{
150dfe32fa1Soh_ci    if (g_pinEventHandler == nullptr) {
151dfe32fa1Soh_ci        LOGW("Asset pin event handler is nullptr, no need to unsubscribe.");
152dfe32fa1Soh_ci        return false;
153dfe32fa1Soh_ci    }
154dfe32fa1Soh_ci
155dfe32fa1Soh_ci    bool res = CommonEventManager::UnSubscribeCommonEvent(g_pinEventHandler);
156dfe32fa1Soh_ci    g_pinEventHandler = nullptr;
157dfe32fa1Soh_ci    return res;
158dfe32fa1Soh_ci}
159dfe32fa1Soh_ci
160dfe32fa1Soh_ci}
161dfe32fa1Soh_ci
162dfe32fa1Soh_cibool SubscribeSystemEvent(const EventCallBack eventCallBack)
163dfe32fa1Soh_ci{
164dfe32fa1Soh_ci    bool ret = SubscribePinEvent(eventCallBack);
165dfe32fa1Soh_ci    LOGI("Subscribe pin event result: %d", ret);
166dfe32fa1Soh_ci
167dfe32fa1Soh_ci    MatchingSkills matchingSkills;
168dfe32fa1Soh_ci    matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
169dfe32fa1Soh_ci    matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
170dfe32fa1Soh_ci    matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
171dfe32fa1Soh_ci    matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
172dfe32fa1Soh_ci    matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CHARGING);
173dfe32fa1Soh_ci    matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
174dfe32fa1Soh_ci    matchingSkills.AddEvent(COMMON_EVENT_RESTORE_START);
175dfe32fa1Soh_ci    CommonEventSubscribeInfo info(matchingSkills);
176dfe32fa1Soh_ci    if (g_eventHandler == nullptr) {
177dfe32fa1Soh_ci        g_eventHandler = std::shared_ptr<SystemEventHandler>(
178dfe32fa1Soh_ci            new (std::nothrow) SystemEventHandler(info, eventCallBack));
179dfe32fa1Soh_ci        if (g_eventHandler == nullptr) {
180dfe32fa1Soh_ci            LOGE("[FATAL]Asset system event handler is nullptr.");
181dfe32fa1Soh_ci            return false;
182dfe32fa1Soh_ci        }
183dfe32fa1Soh_ci    }
184dfe32fa1Soh_ci
185dfe32fa1Soh_ci    return CommonEventManager::SubscribeCommonEvent(g_eventHandler);
186dfe32fa1Soh_ci}
187dfe32fa1Soh_ci
188dfe32fa1Soh_cibool UnSubscribeSystemEvent(void)
189dfe32fa1Soh_ci{
190dfe32fa1Soh_ci    bool ret = UnSubscribePinEvent();
191dfe32fa1Soh_ci    LOGI("UnSubscribe pin event result: %d", ret);
192dfe32fa1Soh_ci
193dfe32fa1Soh_ci    if (g_eventHandler == nullptr) {
194dfe32fa1Soh_ci        LOGW("Asset system event handler is nullptr, no need to unsubscribe.");
195dfe32fa1Soh_ci        return false;
196dfe32fa1Soh_ci    }
197dfe32fa1Soh_ci
198dfe32fa1Soh_ci    bool res = CommonEventManager::UnSubscribeCommonEvent(g_eventHandler);
199dfe32fa1Soh_ci    g_eventHandler = nullptr;
200dfe32fa1Soh_ci    return res;
201dfe32fa1Soh_ci}
202