• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
16 #include "background_task_observer.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "sa_mgr_client.h"
20 #include "system_ability_definition.h"
21 #include "resource_type.h"
22 
23 namespace OHOS {
24 namespace AAFwk {
BackgroundTaskObserver()25 BackgroundTaskObserver::BackgroundTaskObserver()
26 {}
27 
~BackgroundTaskObserver()28 BackgroundTaskObserver::~BackgroundTaskObserver()
29 {}
30 
OnContinuousTaskStart(const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)31 void BackgroundTaskObserver::OnContinuousTaskStart(const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>
32     &continuousTaskCallbackInfo)
33 {
34     TAG_LOGD(
35         AAFwkTag::ABILITYMGR, "OnContinuousTaskStart, uid:%{public}d", continuousTaskCallbackInfo->GetCreatorUid());
36     std::lock_guard<std::mutex> lock(bgTaskMutex_);
37     bgTaskUids_.push_front(continuousTaskCallbackInfo->GetCreatorUid());
38     if (appManager_ == nullptr) {
39         GetAppManager();
40     }
41     if (appManager_ != nullptr) {
42         appManager_->SetContinuousTaskProcess(continuousTaskCallbackInfo->GetCreatorPid(), true);
43     }
44 }
45 
OnContinuousTaskStop(const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)46 void BackgroundTaskObserver::OnContinuousTaskStop(const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>
47     &continuousTaskCallbackInfo)
48 {
49     TAG_LOGD(AAFwkTag::ABILITYMGR, "OnContinuousTaskStop, uid:%{public}d", continuousTaskCallbackInfo->GetCreatorUid());
50     std::lock_guard<std::mutex> lock(bgTaskMutex_);
51     bgTaskUids_.remove(continuousTaskCallbackInfo->GetCreatorUid());
52     if (appManager_ == nullptr) {
53         GetAppManager();
54     }
55     if (appManager_ != nullptr) {
56         appManager_->SetContinuousTaskProcess(continuousTaskCallbackInfo->GetCreatorPid(), false);
57     }
58 }
59 
OnProcEfficiencyResourcesApply(const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> & resourceInfo)60 void BackgroundTaskObserver::OnProcEfficiencyResourcesApply(
61     const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> &resourceInfo)
62 {
63     if (!resourceInfo || (resourceInfo->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) == 0) {
64         return;
65     }
66     std::lock_guard<std::mutex> lock(efficiencyMutex_);
67     efficiencyUids_.push_back(resourceInfo->GetUid());
68 }
69 
OnProcEfficiencyResourcesReset(const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> & resourceInfo)70 void BackgroundTaskObserver::OnProcEfficiencyResourcesReset(
71     const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> &resourceInfo)
72 {
73     if (!resourceInfo || (resourceInfo->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) == 0) {
74         return;
75     }
76     std::lock_guard<std::mutex> lock(efficiencyMutex_);
77     int32_t uid = resourceInfo->GetUid();
78     auto iter = std::find(efficiencyUids_.begin(), efficiencyUids_.end(), uid);
79     if (iter != efficiencyUids_.end()) {
80         efficiencyUids_.erase(iter);
81     }
82 }
83 
OnAppEfficiencyResourcesApply(const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> & resourceInfo)84 void BackgroundTaskObserver::OnAppEfficiencyResourcesApply(
85     const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> &resourceInfo)
86 {
87     OnProcEfficiencyResourcesApply(resourceInfo);
88 }
89 
OnAppEfficiencyResourcesReset(const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> & resourceInfo)90 void BackgroundTaskObserver::OnAppEfficiencyResourcesReset(
91     const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> &resourceInfo)
92 {
93     OnProcEfficiencyResourcesReset(resourceInfo);
94 }
95 
GetContinuousTaskApps()96 void BackgroundTaskObserver::GetContinuousTaskApps()
97 {
98     std::vector<std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>> continuousTasks;
99     ErrCode result = BackgroundTaskMgr::BackgroundTaskMgrHelper::GetContinuousTaskApps(continuousTasks);
100     if (result != ERR_OK) {
101         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to GetContinuousTaskApps, err: %{public}d", result);
102         return;
103     }
104     std::lock_guard<std::mutex> lock(bgTaskMutex_);
105     bgTaskUids_.clear();
106     for (size_t index = 0; index < continuousTasks.size(); index++) {
107         bgTaskUids_.push_front(continuousTasks[index]->GetCreatorUid());
108     }
109 }
110 
GetEfficiencyResourcesTaskApps()111 void BackgroundTaskObserver::GetEfficiencyResourcesTaskApps()
112 {
113     std::vector<std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo>> appList;
114     std::vector<std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo>> procList;
115     ErrCode result = BackgroundTaskMgr::BackgroundTaskMgrHelper::GetEfficiencyResourcesInfos(appList, procList);
116     if (result != ERR_OK) {
117         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to GetEfficiencyResourcesInfos, err: %{public}d", result);
118         return;
119     }
120     std::lock_guard<std::mutex> lock(efficiencyMutex_);
121     efficiencyUids_.clear();
122     for (auto& info : appList) {
123         if (info == nullptr) {
124             continue;
125         }
126         if ((info->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) != 0) {
127             efficiencyUids_.push_back(info->GetUid());
128         }
129     }
130     for (auto& info : procList) {
131         if (info == nullptr) {
132             continue;
133         }
134         if ((info->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) != 0) {
135             efficiencyUids_.push_back(info->GetUid());
136         }
137     }
138 }
139 
IsBackgroundTaskUid(const int uid)140 bool BackgroundTaskObserver::IsBackgroundTaskUid(const int uid)
141 {
142     std::lock_guard<std::mutex> lock(bgTaskMutex_);
143     auto iter = find(bgTaskUids_.begin(), bgTaskUids_.end(), uid);
144     if (iter != bgTaskUids_.end()) {
145         return true;
146     }
147     return false;
148 }
149 
IsEfficiencyResourcesTaskUid(const int uid)150 bool BackgroundTaskObserver::IsEfficiencyResourcesTaskUid(const int uid)
151 {
152     std::lock_guard<std::mutex> lock(efficiencyMutex_);
153     auto iter = std::find(efficiencyUids_.begin(), efficiencyUids_.end(), uid);
154     if (iter != efficiencyUids_.end()) {
155         return true;
156     }
157     return false;
158 }
159 
GetAppManager()160 sptr<AppExecFwk::IAppMgr> BackgroundTaskObserver::GetAppManager()
161 {
162     if (appManager_ == nullptr) {
163         auto appObj =
164             OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->GetSystemAbility(APP_MGR_SERVICE_ID);
165         if (appObj == nullptr) {
166             TAG_LOGE(AAFwkTag::ABILITYMGR, "null appObj");
167             return nullptr;
168         }
169         appManager_ = iface_cast<AppExecFwk::IAppMgr>(appObj);
170     }
171     return appManager_;
172 }
173 }  // namespace AAFwk
174 }  // namespace OHOS
175 #endif
176