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 efficiencyUids_.remove(resourceInfo->GetUid());
78 }
79
OnAppEfficiencyResourcesApply(const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> & resourceInfo)80 void BackgroundTaskObserver::OnAppEfficiencyResourcesApply(
81 const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> &resourceInfo)
82 {
83 OnProcEfficiencyResourcesApply(resourceInfo);
84 }
85
OnAppEfficiencyResourcesReset(const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> & resourceInfo)86 void BackgroundTaskObserver::OnAppEfficiencyResourcesReset(
87 const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> &resourceInfo)
88 {
89 OnProcEfficiencyResourcesReset(resourceInfo);
90 }
91
GetContinuousTaskApps()92 void BackgroundTaskObserver::GetContinuousTaskApps()
93 {
94 std::vector<std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>> continuousTasks;
95 ErrCode result = BackgroundTaskMgr::BackgroundTaskMgrHelper::GetContinuousTaskApps(continuousTasks);
96 if (result != ERR_OK) {
97 TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to GetContinuousTaskApps, ErrCode: %{public}d", result);
98 return;
99 }
100 std::lock_guard<std::mutex> lock(bgTaskMutex_);
101 bgTaskUids_.clear();
102 for (size_t index = 0; index < continuousTasks.size(); index++) {
103 bgTaskUids_.push_front(continuousTasks[index]->GetCreatorUid());
104 }
105 }
106
GetEfficiencyResourcesTaskApps()107 void BackgroundTaskObserver::GetEfficiencyResourcesTaskApps()
108 {
109 std::vector<std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo>> appList;
110 std::vector<std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo>> procList;
111 ErrCode result = BackgroundTaskMgr::BackgroundTaskMgrHelper::GetEfficiencyResourcesInfos(appList, procList);
112 if (result != ERR_OK) {
113 TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to GetEfficiencyResourcesInfos, err: %{public}d", result);
114 return;
115 }
116 std::lock_guard<std::mutex> lock(efficiencyMutex_);
117 efficiencyUids_.clear();
118 for (auto& info : appList) {
119 if (info == nullptr) {
120 continue;
121 }
122 if ((info->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) != 0) {
123 efficiencyUids_.push_back(info->GetUid());
124 }
125 }
126 for (auto& info : procList) {
127 if (info == nullptr) {
128 continue;
129 }
130 if ((info->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) != 0) {
131 efficiencyUids_.push_back(info->GetUid());
132 }
133 }
134 }
135
IsBackgroundTaskUid(const int uid)136 bool BackgroundTaskObserver::IsBackgroundTaskUid(const int uid)
137 {
138 std::lock_guard<std::mutex> lock(bgTaskMutex_);
139 auto iter = find(bgTaskUids_.begin(), bgTaskUids_.end(), uid);
140 if (iter != bgTaskUids_.end()) {
141 return true;
142 }
143 return false;
144 }
145
IsEfficiencyResourcesTaskUid(const int uid)146 bool BackgroundTaskObserver::IsEfficiencyResourcesTaskUid(const int uid)
147 {
148 std::lock_guard<std::mutex> lock(efficiencyMutex_);
149 auto iter = std::find(efficiencyUids_.begin(), efficiencyUids_.end(), uid);
150 if (iter != efficiencyUids_.end()) {
151 return true;
152 }
153 return false;
154 }
155
GetAppManager()156 sptr<AppExecFwk::IAppMgr> BackgroundTaskObserver::GetAppManager()
157 {
158 if (appManager_ == nullptr) {
159 auto appObj =
160 OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->GetSystemAbility(APP_MGR_SERVICE_ID);
161 if (appObj == nullptr) {
162 TAG_LOGE(AAFwkTag::ABILITYMGR, "Failed to get app manager service.");
163 return nullptr;
164 }
165 appManager_ = iface_cast<AppExecFwk::IAppMgr>(appObj);
166 }
167 return appManager_;
168 }
169 } // namespace AAFwk
170 } // namespace OHOS
171 #endif
172