• 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 "background_task_manager.h"
17 
18 #include "hitrace_meter.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 
22 #include "bgtaskmgr_inner_errors.h"
23 #include "bgtaskmgr_log_wrapper.h"
24 #include "delay_suspend_info.h"
25 
26 namespace OHOS {
27 namespace BackgroundTaskMgr {
BackgroundTaskManager()28 BackgroundTaskManager::BackgroundTaskManager() {}
29 
~BackgroundTaskManager()30 BackgroundTaskManager::~BackgroundTaskManager() {}
31 
32 #define GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN              \
33     if (!GetBackgroundTaskManagerProxy()) {                    \
34         BGTASK_LOGE("GetBackgroundTaskManager Proxy failed."); \
35         return ERR_BGTASK_SERVICE_NOT_CONNECTED;               \
36     }
37 
CancelSuspendDelay(int32_t requestId)38 ErrCode BackgroundTaskManager::CancelSuspendDelay(int32_t requestId)
39 {
40     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
41         "BackgroundTaskManager::TransientTask::Mgr::CancelSuspendDelay");
42 
43     std::lock_guard<std::mutex> lock(mutex_);
44     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
45 
46     return proxy_->CancelSuspendDelay(requestId);
47 }
48 
RequestSuspendDelay(const std::u16string & reasonU16,const ExpiredCallback & callback,std::shared_ptr<DelaySuspendInfo> & delayInfo)49 ErrCode BackgroundTaskManager::RequestSuspendDelay(const std::u16string &reasonU16,
50     const ExpiredCallback &callback, std::shared_ptr<DelaySuspendInfo> &delayInfo)
51 {
52     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
53         "BackgroundTaskManager::TransientTask::Mgr::RequestSuspendDelay");
54 
55     std::lock_guard<std::mutex> lock(mutex_);
56     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
57 
58     sptr<ExpiredCallback::ExpiredCallbackImpl> callbackSptr = callback.GetImpl();
59     if (callbackSptr == nullptr) {
60         BGTASK_LOGE("callbackSptr is nullptr");
61         return ERR_CALLBACK_NULL_OR_TYPE_ERR;
62     }
63     delayInfo = std::make_shared<DelaySuspendInfo>();
64     if (!delayInfo) {
65         BGTASK_LOGE("delayInfo is nullptr");
66         return ERR_CALLBACK_NULL_OR_TYPE_ERR;
67     }
68     std::string reason = Str16ToStr8(reasonU16);
69     return proxy_->RequestSuspendDelay(reason, callbackSptr, *delayInfo.get());
70 }
71 
GetRemainingDelayTime(int32_t requestId,int32_t & delayTime)72 ErrCode BackgroundTaskManager::GetRemainingDelayTime(int32_t requestId, int32_t &delayTime)
73 {
74     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
75         "BackgroundTaskManager::TransientTask::Mgr::GetRemainingDelayTime");
76 
77     std::lock_guard<std::mutex> lock(mutex_);
78     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
79 
80     return proxy_->GetRemainingDelayTime(requestId, delayTime);
81 }
82 
RequestStartBackgroundRunning(ContinuousTaskParam & taskParam)83 ErrCode BackgroundTaskManager::RequestStartBackgroundRunning(ContinuousTaskParam &taskParam)
84 {
85     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
86         "BackgroundTaskManager::ContinuousTask::Mgr::RequestStartBackgroundRunning");
87 
88     std::lock_guard<std::mutex> lock(mutex_);
89     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
90 
91     sptr<ContinuousTaskParam> taskParamPtr = new (std::nothrow) ContinuousTaskParam(taskParam);
92     if (taskParamPtr == nullptr || taskParamPtr.GetRefPtr() == nullptr) {
93         BGTASK_LOGE("Failed to create continuous task param");
94         return ERR_BGTASK_NO_MEMORY;
95     }
96     BGTASK_LOGD("%{public}u = %{public}u, %{public}d = %{public}d, abilityId = %{public}d",
97         static_cast<uint32_t>(taskParamPtr->bgModeIds_.size()),
98         static_cast<uint32_t>(taskParam.bgModeIds_.size()),
99         taskParamPtr->isBatchApi_, taskParam.isBatchApi_,
100         taskParamPtr->abilityId_);
101     int32_t notificationId = taskParamPtr->notificationId_;
102     int32_t continuousTaskId = taskParamPtr->continuousTaskId_;
103     ErrCode res = proxy_->StartBackgroundRunning(*taskParamPtr.GetRefPtr(), notificationId, continuousTaskId);
104     taskParam.notificationId_ = notificationId;
105     taskParam.continuousTaskId_ = continuousTaskId;
106     return res;
107 }
108 
RequestUpdateBackgroundRunning(ContinuousTaskParam & taskParam)109 ErrCode BackgroundTaskManager::RequestUpdateBackgroundRunning(ContinuousTaskParam &taskParam)
110 {
111     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
112         "BackgroundTaskManager::ContinuousTask::Mgr::RequestUpdateBackgroundRunning");
113 
114     std::lock_guard<std::mutex> lock(mutex_);
115     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
116 
117     sptr<ContinuousTaskParam> taskParamPtr = new (std::nothrow) ContinuousTaskParam(taskParam);
118     if (taskParamPtr == nullptr || taskParamPtr.GetRefPtr() == nullptr) {
119         BGTASK_LOGE("Failed to create continuous task param");
120         return ERR_BGTASK_NO_MEMORY;
121     }
122 
123     BGTASK_LOGD(" %{public}u = %{public}u, %{public}d = %{public}d, abilityId = %{public}d",
124         static_cast<uint32_t>(taskParamPtr->bgModeIds_.size()),
125         static_cast<uint32_t>(taskParam.bgModeIds_.size()), taskParamPtr->isBatchApi_, taskParam.isBatchApi_,
126         taskParamPtr->abilityId_);
127     int32_t notificationId = taskParamPtr->notificationId_;
128     int32_t continuousTaskId = taskParamPtr->continuousTaskId_;
129     ErrCode ret = proxy_->UpdateBackgroundRunning(*taskParamPtr.GetRefPtr(), notificationId, continuousTaskId);
130     taskParam.notificationId_ = notificationId;
131     taskParam.continuousTaskId_ = continuousTaskId;
132     return ret;
133 }
134 
RequestBackgroundRunningForInner(const ContinuousTaskParamForInner & taskParam)135 ErrCode BackgroundTaskManager::RequestBackgroundRunningForInner(const ContinuousTaskParamForInner &taskParam)
136 {
137     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
138         "BackgroundTaskManager::ContinuousTask::Mgr::RequestBackgroundRunningForInner");
139 
140     std::lock_guard<std::mutex> lock(mutex_);
141     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
142 
143     sptr<ContinuousTaskParamForInner> taskParamPtr = new (std::nothrow) ContinuousTaskParamForInner(taskParam);
144     if (taskParamPtr == nullptr || taskParamPtr.GetRefPtr() == nullptr) {
145         BGTASK_LOGE("Failed to create continuous task param");
146         return ERR_BGTASK_NO_MEMORY;
147     }
148 
149     return proxy_->RequestBackgroundRunningForInner(*taskParamPtr.GetRefPtr());
150 }
151 
RequestStopBackgroundRunning(const std::string & abilityName,const sptr<IRemoteObject> & abilityToken,int32_t abilityId)152 ErrCode BackgroundTaskManager::RequestStopBackgroundRunning(const std::string &abilityName,
153     const sptr<IRemoteObject> &abilityToken, int32_t abilityId)
154 {
155     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
156         "BackgroundTaskManager::ContinuousTask::Mgr::RequestStopBackgroundRunning");
157 
158     std::lock_guard<std::mutex> lock(mutex_);
159     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
160 
161     return proxy_->StopBackgroundRunning(abilityName, abilityToken, abilityId);
162 }
163 
SubscribeBackgroundTask(const BackgroundTaskSubscriber & subscriber)164 __attribute__((no_sanitize("cfi"))) ErrCode BackgroundTaskManager::SubscribeBackgroundTask(
165     const BackgroundTaskSubscriber &subscriber)
166 {
167     std::lock_guard<std::mutex> lock(mutex_);
168     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
169 
170     sptr<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl> subscriberSptr = subscriber.GetImpl();
171     if (subscriberSptr == nullptr) {
172         BGTASK_LOGE("subscriberSptr is nullptr");
173         return ERR_BGTASK_INVALID_PARAM;
174     }
175     return proxy_->SubscribeBackgroundTask(subscriberSptr);
176 }
177 
UnsubscribeBackgroundTask(const BackgroundTaskSubscriber & subscriber)178 ErrCode BackgroundTaskManager::UnsubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber)
179 {
180     std::lock_guard<std::mutex> lock(mutex_);
181     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
182 
183     sptr<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl> subscriberSptr = subscriber.GetImpl();
184     if (subscriberSptr == nullptr) {
185         BGTASK_LOGE("subscriberSptr is nullptr");
186         return ERR_BGTASK_INVALID_PARAM;
187     }
188     return proxy_->UnsubscribeBackgroundTask(subscriberSptr);
189 }
190 
GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> & list)191 ErrCode BackgroundTaskManager::GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list)
192 {
193     std::lock_guard<std::mutex> lock(mutex_);
194     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
195 
196     std::vector<TransientTaskAppInfo> TaskAppsList;
197     ErrCode result = proxy_->GetTransientTaskApps(TaskAppsList);
198     if (result == ERR_OK) {
199         list.clear();
200         for (const auto& item : TaskAppsList) {
201             list.push_back(std::make_shared<TransientTaskAppInfo>(item));
202         }
203     }
204 
205     return result;
206 }
207 
PauseTransientTaskTimeForInner(int32_t uid)208 ErrCode BackgroundTaskManager::PauseTransientTaskTimeForInner(int32_t uid)
209 {
210     std::lock_guard<std::mutex> lock(mutex_);
211     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
212 
213     return proxy_->PauseTransientTaskTimeForInner(uid);
214 }
215 
StartTransientTaskTimeForInner(int32_t uid)216 ErrCode BackgroundTaskManager::StartTransientTaskTimeForInner(int32_t uid)
217 {
218     std::lock_guard<std::mutex> lock(mutex_);
219     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
220 
221     return proxy_->StartTransientTaskTimeForInner(uid);
222 }
223 
ApplyEfficiencyResources(const EfficiencyResourceInfo & resourceInfo)224 ErrCode BackgroundTaskManager::ApplyEfficiencyResources(const EfficiencyResourceInfo &resourceInfo)
225 {
226     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
227         "BackgroundTaskManager::EfficiencyResource::Mgr::ApplyEfficiencyResources");
228 
229     std::lock_guard<std::mutex> lock(mutex_);
230     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
231 
232     sptr<EfficiencyResourceInfo> resourceInfoPtr = new (std::nothrow) EfficiencyResourceInfo(resourceInfo);
233     if (resourceInfoPtr == nullptr || resourceInfoPtr.GetRefPtr() == nullptr) {
234         BGTASK_LOGE("Failed to create efficiency resource info");
235         return ERR_BGTASK_NO_MEMORY;
236     }
237     return proxy_->ApplyEfficiencyResources(*resourceInfoPtr.GetRefPtr());
238 }
239 
ResetAllEfficiencyResources()240 ErrCode BackgroundTaskManager::ResetAllEfficiencyResources()
241 {
242     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
243         "BackgroundTaskManager::EfficiencyResource::Mgr::ResetAllEfficiencyResources");
244 
245     std::lock_guard<std::mutex> lock(mutex_);
246     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
247 
248     return proxy_->ResetAllEfficiencyResources();
249 }
250 
GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> & appList,std::vector<std::shared_ptr<ResourceCallbackInfo>> & procList)251 ErrCode BackgroundTaskManager::GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> &appList,
252     std::vector<std::shared_ptr<ResourceCallbackInfo>> &procList)
253 {
254     std::lock_guard<std::mutex> lock(mutex_);
255     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
256 
257     std::vector<ResourceCallbackInfo> resourceAppList;
258     std::vector<ResourceCallbackInfo> resourceProcList;
259     ErrCode result = proxy_->GetEfficiencyResourcesInfos(resourceAppList, resourceProcList);
260     if (result == ERR_OK) {
261         appList.clear();
262         procList.clear();
263         for (const auto& item : resourceAppList) {
264             appList.push_back(std::make_shared<ResourceCallbackInfo>(item));
265         }
266 
267         for (const auto& item : resourceProcList) {
268             procList.push_back(std::make_shared<ResourceCallbackInfo>(item));
269         }
270     }
271     return result;
272 }
273 
GetBackgroundTaskManagerProxy()274 bool BackgroundTaskManager::GetBackgroundTaskManagerProxy()
275 {
276     if (proxy_ != nullptr) {
277         return true;
278     }
279     sptr<ISystemAbilityManager> systemAbilityManager =
280         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
281     if (systemAbilityManager == nullptr) {
282         BGTASK_LOGE("GetBackgroundTaskManagerProxy GetSystemAbilityManager failed.");
283         return false;
284     }
285 
286     sptr<IRemoteObject> remoteObject =
287         systemAbilityManager->GetSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID);
288     if (remoteObject == nullptr) {
289         BGTASK_LOGE("GetBackgroundTaskManagerProxy GetSystemAbility failed.");
290         return false;
291     }
292 
293     proxy_ = iface_cast<BackgroundTaskMgr::IBackgroundTaskMgr>(remoteObject);
294     if ((proxy_ == nullptr) || (proxy_->AsObject() == nullptr)) {
295         BGTASK_LOGE("GetBackgroundTaskManagerProxy iface_cast remoteObject failed.");
296         return false;
297     }
298 
299     recipient_ = new (std::nothrow) BgTaskMgrDeathRecipient(*this);
300     if (recipient_ == nullptr) {
301         return false;
302     }
303     proxy_->AsObject()->AddDeathRecipient(recipient_);
304     return true;
305 }
306 
GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> & list)307 ErrCode BackgroundTaskManager::GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list)
308 {
309     std::lock_guard<std::mutex> lock(mutex_);
310     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
311 
312     std::vector<ContinuousTaskCallbackInfo> TaskAppsList;
313     ErrCode result = proxy_->GetContinuousTaskApps(TaskAppsList);
314     if (result == ERR_OK) {
315         list.clear();
316         for (const auto& item : TaskAppsList) {
317             list.push_back(std::make_shared<ContinuousTaskCallbackInfo>(item));
318         }
319     }
320 
321     return result;
322 }
323 
StopContinuousTask(int32_t uid,int32_t pid,uint32_t taskType,const std::string & key)324 ErrCode BackgroundTaskManager::StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key)
325 {
326     std::lock_guard<std::mutex> lock(mutex_);
327     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
328 
329     return proxy_->StopContinuousTask(uid, pid, taskType, key);
330 }
331 
ResetBackgroundTaskManagerProxy()332 void BackgroundTaskManager::ResetBackgroundTaskManagerProxy()
333 {
334     std::lock_guard<std::mutex> lock(mutex_);
335     if ((proxy_ != nullptr) && (proxy_->AsObject() != nullptr)) {
336         proxy_->AsObject()->RemoveDeathRecipient(recipient_);
337     }
338     proxy_ = nullptr;
339 }
340 
SetBgTaskConfig(const std::string & configData,int32_t sourceType)341 ErrCode BackgroundTaskManager::SetBgTaskConfig(const std::string &configData, int32_t sourceType)
342 {
343     std::lock_guard<std::mutex> lock(mutex_);
344     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
345 
346     return proxy_->SetBgTaskConfig(configData, sourceType);
347 }
348 
BgTaskMgrDeathRecipient(BackgroundTaskManager & backgroundTaskManager)349 BackgroundTaskManager::BgTaskMgrDeathRecipient::BgTaskMgrDeathRecipient(BackgroundTaskManager &backgroundTaskManager)
350     : backgroundTaskManager_(backgroundTaskManager) {}
351 
~BgTaskMgrDeathRecipient()352 BackgroundTaskManager::BgTaskMgrDeathRecipient::~BgTaskMgrDeathRecipient() {}
353 
OnRemoteDied(const wptr<IRemoteObject> & remote)354 void BackgroundTaskManager::BgTaskMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
355 {
356     backgroundTaskManager_.ResetBackgroundTaskManagerProxy();
357 }
358 }  // namespace BackgroundTaskMgr
359 }  // namespace OHOS