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 "system_event_observer.h"
17 
18 #include "bundle_constants.h"
19 #include "common_event_support.h"
20 
21 #include "bg_continuous_task_mgr.h"
22 #include "bgtaskmgr_inner_errors.h"
23 #include "continuous_task_log.h"
24 #include "bg_efficiency_resources_mgr.h"
25 
26 namespace OHOS {
27 namespace BackgroundTaskMgr {
28 namespace {
29 const std::string TASK_ON_BUNDLEINFO_CHANGED = "OnBundleInfoChanged";
30 const std::string TASK_ON_OS_ACCOUNT_CHANGED = "OnOsAccountChanged";
31 }
32 
SystemEventObserver(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)33 SystemEventObserver::SystemEventObserver(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
34     : EventFwk::CommonEventSubscriber(subscribeInfo) {}
35 
Subscribe()36 __attribute__((no_sanitize("cfi"))) bool SystemEventObserver::Subscribe()
37 {
38     if (!EventFwk::CommonEventManager::SubscribeCommonEvent(shared_from_this())) {
39         BGTASK_LOGI("SubscribeCommonEvent occur exception.");
40         return false;
41     }
42     return true;
43 }
44 
Unsubscribe()45 __attribute__((no_sanitize("cfi"))) bool SystemEventObserver::Unsubscribe()
46 {
47     if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(shared_from_this())) {
48         BGTASK_LOGI("UnsubscribeCommonEvent occur exception.");
49         return false;
50     }
51     return true;
52 }
53 
SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> & handler)54 void SystemEventObserver::SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
55 {
56     handler_ = handler;
57 }
58 
SetBgContinuousTaskMgr(const std::shared_ptr<BgContinuousTaskMgr> & bgContinuousTaskMgr)59 void SystemEventObserver::SetBgContinuousTaskMgr(const std::shared_ptr<BgContinuousTaskMgr> &bgContinuousTaskMgr)
60 {
61     bgContinuousTaskMgr_ = bgContinuousTaskMgr;
62 }
63 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)64 void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
65 {
66     OnReceiveEventContinuousTask(eventData);
67     OnReceiveEventEfficiencyRes(eventData);
68 }
69 
OnReceiveEventContinuousTask(const EventFwk::CommonEventData & eventData)70 void SystemEventObserver::OnReceiveEventContinuousTask(const EventFwk::CommonEventData &eventData)
71 {
72     auto handler = handler_.lock();
73     if (!handler) {
74         BGTASK_LOGE("handler is null");
75         return;
76     }
77     auto bgContinuousTaskMgr = bgContinuousTaskMgr_.lock();
78     if (!bgContinuousTaskMgr) {
79         BGTASK_LOGE("bgContinuousTaskMgr is null");
80         return;
81     }
82     AAFwk::Want want = eventData.GetWant();
83     std::string action = want.GetAction();
84     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED
85         || action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED
86         || action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
87         int32_t userId = eventData.GetCode();
88         BGTASK_LOGI("user of id :%{public}d state is changed, action is : %{public}s", userId, action.c_str());
89         auto task = [bgContinuousTaskMgr, userId]() { bgContinuousTaskMgr->OnAccountsStateChanged(userId); };
90         handler->PostTask(task, TASK_ON_OS_ACCOUNT_CHANGED);
91         return;
92     }
93     std::string bundleName = want.GetElement().GetBundleName();
94     int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
95     BGTASK_LOGI("get common event action = %{public}s, bundleName = %{public}s, uid = %{public}d",
96         action.c_str(), bundleName.c_str(), uid);
97     auto task = [bgContinuousTaskMgr, action, bundleName, uid]() {
98         bgContinuousTaskMgr->OnBundleInfoChanged(action, bundleName, uid);
99     };
100     handler->PostTask(task, TASK_ON_BUNDLEINFO_CHANGED);
101 }
102 
OnReceiveEventEfficiencyRes(const EventFwk::CommonEventData & eventData)103 void SystemEventObserver::OnReceiveEventEfficiencyRes(const EventFwk::CommonEventData &eventData)
104 {
105     AAFwk::Want want = eventData.GetWant();
106     std::string action = want.GetAction();
107     std::string bundleName = want.GetElement().GetBundleName();
108     int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
109     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
110         DelayedSingleton<BgEfficiencyResourcesMgr>::GetInstance()->RemoveAppRecord(uid, bundleName, true);
111     }
112 }
113 }  // namespace BackgroundTaskMgr
114 }  // namespace OHOS