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 const std::string TASK_ON_APP_DIED = "OnAppDiedTask";
32 }
33
OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)34 void AppStateObserver::OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData)
35 {
36 if (abilityStateData.abilityState != static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED)) {
37 return;
38 }
39 BGTASK_LOGI("ability state changed, uid: %{public}d abilityName: %{public}s, abilityState: %{public}d, "
40 "abilityId: %{public}d",
41 abilityStateData.uid, abilityStateData.abilityName.c_str(), abilityStateData.abilityState,
42 abilityStateData.abilityRecordId);
43 int32_t uid = abilityStateData.uid;
44 int32_t abilityId = abilityStateData.abilityRecordId;
45 std::string abilityName = abilityStateData.abilityName;
46 auto task = [uid, abilityName, abilityId]() {
47 DelayedSingleton<BgContinuousTaskMgr>::GetInstance()->OnAbilityStateChanged(uid, abilityName, abilityId);
48 };
49 if (!handler_) {
50 BGTASK_LOGE("handler_ null");
51 return;
52 }
53 handler_->PostTask(task, TASK_ON_ABILITY_STATE_CHANGED);
54 }
55
OnProcessDied(const AppExecFwk::ProcessData & processData)56 void AppStateObserver::OnProcessDied(const AppExecFwk::ProcessData &processData)
57 {
58 BGTASK_LOGD("process died, uid : %{public}d, pid : %{public}d", processData.uid, processData.pid);
59 OnProcessDiedEfficiencyRes(processData);
60 }
61
OnProcessDiedEfficiencyRes(const AppExecFwk::ProcessData & processData)62 void AppStateObserver::OnProcessDiedEfficiencyRes(const AppExecFwk::ProcessData &processData)
63 {
64 DelayedSingleton<BgEfficiencyResourcesMgr>::GetInstance()->
65 RemoveProcessRecord(processData.uid, processData.pid, processData.bundleName);
66 }
67
OnAppStopped(const AppExecFwk::AppStateData & appStateData)68 void AppStateObserver::OnAppStopped(const AppExecFwk::AppStateData &appStateData)
69 {
70 BGTASK_LOGD("app stopped, uid : %{public}d", appStateData.uid);
71 if (!ValidateAppStateData(appStateData)) {
72 BGTASK_LOGE("%{public}s : validate app state data failed!", __func__);
73 return;
74 }
75 auto uid = appStateData.uid;
76 auto bundleName = appStateData.bundleName;
77 auto task = [uid]() {
78 DelayedSingleton<BgContinuousTaskMgr>::GetInstance()->OnAppStopped(uid);
79 };
80 if (!handler_) {
81 BGTASK_LOGE("handler_ null.");
82 } else {
83 handler_->PostTask(task, TASK_ON_APP_DIED);
84 }
85 DelayedSingleton<BgEfficiencyResourcesMgr>::GetInstance()->RemoveAppRecord(uid, bundleName, false);
86 }
87
ValidateAppStateData(const AppExecFwk::AppStateData & appStateData)88 inline bool AppStateObserver::ValidateAppStateData(const AppExecFwk::AppStateData &appStateData)
89 {
90 return appStateData.uid > 0 && appStateData.bundleName.size() > 0;
91 }
92
SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> & handler)93 void AppStateObserver::SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
94 {
95 handler_ = handler;
96 }
97 } // namespace BackgroundTaskMgr
98 } // namespace OHOS