• 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 "bgtask_hitrace_chain.h"
24 #include "bg_transient_task_mgr.h"
25 #include "continuous_task_log.h"
26 #include "bg_efficiency_resources_mgr.h"
27 
28 namespace OHOS {
29 namespace BackgroundTaskMgr {
30 namespace {
31 const std::string TASK_ON_PROCESS_DIED = "OnProcessDiedTask";
32 const std::string TASK_ON_ABILITY_STATE_CHANGED = "OnAbilityStateChangedTask";
33 const std::string TASK_ON_APP_CACHE_STATE_CHANGED = "OnAppCacheStateChangedTask";
34 const std::string TASK_ON_APP_DIED = "OnAppDiedTask";
35 }
36 
OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)37 void AppStateObserver::OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData)
38 {
39     BgTaskHiTraceChain traceChain(__func__);
40     if (abilityStateData.abilityState != static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED)) {
41         return;
42     }
43     BGTASK_LOGD("ability state changed, uid: %{public}d abilityName: %{public}s, abilityState: %{public}d, "
44         "abilityId: %{public}d",
45         abilityStateData.uid, abilityStateData.abilityName.c_str(), abilityStateData.abilityState,
46         abilityStateData.abilityRecordId);
47     int32_t uid = abilityStateData.uid;
48     int32_t abilityId = abilityStateData.abilityRecordId;
49     std::string abilityName = abilityStateData.abilityName;
50     auto task = [uid, abilityName, abilityId]() {
51         DelayedSingleton<BgContinuousTaskMgr>::GetInstance()->OnAbilityStateChanged(uid, abilityName, abilityId);
52     };
53     if (!handler_) {
54         BGTASK_LOGE("handler_ null");
55         return;
56     }
57     handler_->PostTask(task, TASK_ON_ABILITY_STATE_CHANGED);
58 }
59 
OnAppCacheStateChanged(const AppExecFwk::AppStateData & appStateData)60 void AppStateObserver::OnAppCacheStateChanged(const AppExecFwk::AppStateData &appStateData)
61 {
62     BgTaskHiTraceChain traceChain(__func__);
63     if (appStateData.state != static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_CACHED)) {
64         return;
65     }
66     if (!handler_) {
67         BGTASK_LOGE("handler_ null");
68         return;
69     }
70     BGTASK_LOGD("app cache, name : %{public}s,  uid : %{public}d, pid : %{public}d, state : %{public}d,",
71         appStateData.bundleName.c_str(), appStateData.uid, appStateData.pid, appStateData.state);
72     int32_t uid = appStateData.uid;
73     int32_t pid = appStateData.pid;
74     std::string bundleName = appStateData.bundleName;
75     auto task = [uid, pid, bundleName]() {
76         DelayedSingleton<BgTransientTaskMgr>::GetInstance()->OnAppCacheStateChanged(uid, pid, bundleName);
77     };
78     handler_->PostTask(task, TASK_ON_APP_CACHE_STATE_CHANGED);
79 }
80 
OnProcessDied(const AppExecFwk::ProcessData & processData)81 void AppStateObserver::OnProcessDied(const AppExecFwk::ProcessData &processData)
82 {
83     BgTaskHiTraceChain traceChain(__func__);
84     BGTASK_LOGD("process died, uid : %{public}d, pid : %{public}d", processData.uid, processData.pid);
85     OnProcessDiedEfficiencyRes(processData);
86 }
87 
OnProcessDiedEfficiencyRes(const AppExecFwk::ProcessData & processData)88 void AppStateObserver::OnProcessDiedEfficiencyRes(const AppExecFwk::ProcessData &processData)
89 {
90     BgTaskHiTraceChain traceChain(__func__);
91     DelayedSingleton<BgEfficiencyResourcesMgr>::GetInstance()->
92         RemoveProcessRecord(processData.uid, processData.pid, processData.bundleName);
93 }
94 
OnAppStopped(const AppExecFwk::AppStateData & appStateData)95 void AppStateObserver::OnAppStopped(const AppExecFwk::AppStateData &appStateData)
96 {
97     BgTaskHiTraceChain traceChain(__func__);
98     BGTASK_LOGD("app stopped, uid : %{public}d", appStateData.uid);
99     if (!ValidateAppStateData(appStateData)) {
100         BGTASK_LOGE("%{public}s : validate app state data failed!", __func__);
101         return;
102     }
103     auto uid = appStateData.uid;
104     auto bundleName = appStateData.bundleName;
105     auto task = [uid]() {
106         DelayedSingleton<BgContinuousTaskMgr>::GetInstance()->OnAppStopped(uid);
107     };
108     if (!handler_) {
109         BGTASK_LOGE("handler_ null.");
110     } else {
111         handler_->PostTask(task, TASK_ON_APP_DIED);
112     }
113     DelayedSingleton<BgEfficiencyResourcesMgr>::GetInstance()->RemoveAppRecord(uid, bundleName, false);
114 }
115 
ValidateAppStateData(const AppExecFwk::AppStateData & appStateData)116 bool AppStateObserver::ValidateAppStateData(const AppExecFwk::AppStateData &appStateData)
117 {
118     return appStateData.uid > 0 && appStateData.bundleName.size() > 0;
119 }
120 
SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> & handler)121 void AppStateObserver::SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
122 {
123     handler_ = handler;
124 }
125 }  // namespace BackgroundTaskMgr
126 }  // namespace OHOS