• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "app_state_observer.h"
17 
18 #include "app_mgr_constants.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 
22 #include "bg_continuous_task_mgr.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_PROCESS_DIED = "OnProcessDiedTask";
30 const std::string TASK_ON_ABILITY_STATE_CHANGED = "OnAbilityStateChangedTask";
31 }
32 
33 #ifdef BGTASK_MGR_UNIT_TEST
34 #define WEAK_FUNC __attribute__((weak))
35 #else
36 #define WEAK_FUNC
37 #endif
38 
AppStateObserver()39 AppStateObserver::AppStateObserver() {}
40 
~AppStateObserver()41 AppStateObserver::~AppStateObserver() {}
42 
CheckParamValid()43 bool AppStateObserver::CheckParamValid()
44 {
45     if (handler_.expired()) {
46         BGTASK_LOGE("AppStateObserver handler is null");
47         return false;
48     }
49     if (bgContinuousTaskMgr_.expired()) {
50         BGTASK_LOGE("AppStateObserver bgContinuousTaskMgr is null");
51         return false;
52     }
53     return true;
54 }
55 
OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)56 void AppStateObserver::OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData)
57 {
58     if (abilityStateData.abilityState != static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED)) {
59         return;
60     }
61     BGTASK_LOGD("ability state changed, uid: %{public}d abilityName: %{public}s, abilityState: %{public}d",
62         abilityStateData.uid, abilityStateData.abilityName.c_str(), abilityStateData.abilityState);
63     if (!CheckParamValid()) {
64         return;
65     }
66     int32_t uid = abilityStateData.uid;
67     std::string abilityName = abilityStateData.abilityName;
68     auto task = [this, uid, abilityName]() {
69         this->bgContinuousTaskMgr_.lock()->OnAbilityStateChanged(uid, abilityName);
70     };
71 
72     auto handler = handler_.lock();
73     if (handler == nullptr) {
74         return;
75     }
76     handler->PostTask(task, TASK_ON_ABILITY_STATE_CHANGED);
77 }
78 
OnProcessDied(const AppExecFwk::ProcessData & processData)79 void AppStateObserver::OnProcessDied(const AppExecFwk::ProcessData &processData)
80 {
81     BGTASK_LOGD("process died, uid : %{public}d, pid : %{public}d", processData.uid, processData.pid);
82     OnProcessDiedContinuousTask(processData);
83     OnProcessDiedEfficiencyRes(processData);
84 }
85 
OnProcessDiedContinuousTask(const AppExecFwk::ProcessData & processData)86 void AppStateObserver::OnProcessDiedContinuousTask(const AppExecFwk::ProcessData &processData)
87 {
88     if (!CheckParamValid()) {
89         return;
90     }
91     auto task = [this, processData]() {
92         this->bgContinuousTaskMgr_.lock()->OnProcessDied(processData.uid, processData.pid);
93     };
94     handler_.lock()->PostTask(task, TASK_ON_PROCESS_DIED);
95 }
96 
OnProcessDiedEfficiencyRes(const AppExecFwk::ProcessData & processData)97 void AppStateObserver::OnProcessDiedEfficiencyRes(const AppExecFwk::ProcessData &processData)
98 {
99     auto bgEfficiencyResourcesMgr = bgEfficiencyResourcesMgr_.lock();
100     if (!bgEfficiencyResourcesMgr) {
101         BGTASK_LOGE("bgEfficiencyResourcesMgr is null");
102         return;
103     }
104     bgEfficiencyResourcesMgr->RemoveProcessRecord(processData.uid, processData.pid, processData.bundleName);
105 }
106 
OnAppStopped(const AppExecFwk::AppStateData & appStateData)107 void AppStateObserver::OnAppStopped(const AppExecFwk::AppStateData &appStateData)
108 {
109     if (!ValidateAppStateData(appStateData)) {
110         BGTASK_LOGE("%{public}s : validate app state data failed!", __func__);
111         return;
112     }
113     auto uid = appStateData.uid;
114     auto bundleName = appStateData.bundleName;
115     auto bgEfficiencyResourcesMgr = bgEfficiencyResourcesMgr_.lock();
116     if (!bgEfficiencyResourcesMgr) {
117         BGTASK_LOGE("bgEfficiencyResourcesMgr is null");
118         return;
119     }
120     bgEfficiencyResourcesMgr->RemoveAppRecord(uid, bundleName, false);
121 }
122 
ValidateAppStateData(const AppExecFwk::AppStateData & appStateData)123 inline bool AppStateObserver::ValidateAppStateData(const AppExecFwk::AppStateData &appStateData)
124 {
125     return appStateData.uid > 0 && appStateData.bundleName.size() > 0;
126 }
127 
SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> & handler)128 void AppStateObserver::SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
129 {
130     handler_ = handler;
131 }
132 
SetBgContinuousTaskMgr(const std::shared_ptr<BgContinuousTaskMgr> & bgContinuousTaskMgr)133 void AppStateObserver::SetBgContinuousTaskMgr(const std::shared_ptr<BgContinuousTaskMgr> &bgContinuousTaskMgr)
134 {
135     bgContinuousTaskMgr_ = bgContinuousTaskMgr;
136 }
137 
SetBgEfficiencyResourcesMgr(const std::shared_ptr<BgEfficiencyResourcesMgr> & resourceMgr)138 void WEAK_FUNC AppStateObserver::SetBgEfficiencyResourcesMgr(
139     const std::shared_ptr<BgEfficiencyResourcesMgr> &resourceMgr)
140 {
141     bgEfficiencyResourcesMgr_ = resourceMgr;
142 }
143 
Subscribe()144 bool WEAK_FUNC AppStateObserver::Subscribe()
145 {
146     std::lock_guard<std::mutex> lock(mutex_);
147 
148     if (!Connect()) {
149         return false;
150     }
151     appMgrProxy_->RegisterApplicationStateObserver(iface_cast<AppExecFwk::IApplicationStateObserver>(this));
152     return true;
153 }
154 
Unsubscribe()155 bool WEAK_FUNC AppStateObserver::Unsubscribe()
156 {
157     std::lock_guard<std::mutex> lock(mutex_);
158     if (!Connect()) {
159         return false;
160     }
161     appMgrProxy_->UnregisterApplicationStateObserver(iface_cast<AppExecFwk::IApplicationStateObserver>(this));
162     return true;
163 }
164 
Connect()165 bool AppStateObserver::Connect()
166 {
167     if (appMgrProxy_ != nullptr) {
168         return true;
169     }
170 
171     sptr<ISystemAbilityManager> systemAbilityManager =
172         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
173     if (systemAbilityManager == nullptr) {
174         BGTASK_LOGE("get SystemAbilityManager failed");
175         return false;
176     }
177 
178     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID);
179     if (remoteObject == nullptr) {
180         BGTASK_LOGE("get App Manager Service failed");
181         return false;
182     }
183 
184     appMgrProxy_ = iface_cast<AppExecFwk::IAppMgr>(remoteObject);
185     if (!appMgrProxy_ || !appMgrProxy_->AsObject()) {
186         BGTASK_LOGE("get app mgr proxy failed!");
187         return false;
188     }
189     return true;
190 }
191 }  // namespace BackgroundTaskMgr
192 }  // namespace OHOS