• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 
GetAllTransientTasks(int32_t & remainingQuota,std::vector<std::shared_ptr<DelaySuspendInfo>> & list)83 ErrCode BackgroundTaskManager::GetAllTransientTasks(int32_t &remainingQuota,
84     std::vector<std::shared_ptr<DelaySuspendInfo>> &list)
85 {
86     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
87         "BackgroundTaskManager::ContinuousTask::Mgr::GetAllTransientTasks");
88 
89     std::lock_guard<std::mutex> lock(mutex_);
90     if (!GetBackgroundTaskManagerProxy()) {
91         BGTASK_LOGE("GetBackgroundTaskManager Proxy failed.");
92         return ERR_BGTASK_TRANSIENT_SERVICE_NOT_CONNECTED;
93     }
94 
95     return proxy_->GetAllTransientTasks(remainingQuota, list);
96 }
97 
RequestStartBackgroundRunning(ContinuousTaskParam & taskParam)98 ErrCode BackgroundTaskManager::RequestStartBackgroundRunning(ContinuousTaskParam &taskParam)
99 {
100     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
101         "BackgroundTaskManager::ContinuousTask::Mgr::RequestStartBackgroundRunning");
102 
103     std::lock_guard<std::mutex> lock(mutex_);
104     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
105 
106     sptr<ContinuousTaskParam> taskParamPtr = new (std::nothrow) ContinuousTaskParam(taskParam);
107     if (taskParamPtr == nullptr || taskParamPtr.GetRefPtr() == nullptr) {
108         BGTASK_LOGE("Failed to create continuous task param");
109         return ERR_BGTASK_NO_MEMORY;
110     }
111     BGTASK_LOGD("%{public}u = %{public}u, %{public}d = %{public}d, abilityId = %{public}d",
112         static_cast<uint32_t>(taskParamPtr->bgModeIds_.size()),
113         static_cast<uint32_t>(taskParam.bgModeIds_.size()),
114         taskParamPtr->isBatchApi_, taskParam.isBatchApi_,
115         taskParamPtr->abilityId_);
116     int32_t notificationId = taskParamPtr->notificationId_;
117     int32_t continuousTaskId = taskParamPtr->continuousTaskId_;
118     ErrCode res = proxy_->StartBackgroundRunning(*taskParamPtr.GetRefPtr(), notificationId, continuousTaskId);
119     taskParam.notificationId_ = notificationId;
120     taskParam.continuousTaskId_ = continuousTaskId;
121     return res;
122 }
123 
RequestUpdateBackgroundRunning(ContinuousTaskParam & taskParam)124 ErrCode BackgroundTaskManager::RequestUpdateBackgroundRunning(ContinuousTaskParam &taskParam)
125 {
126     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
127         "BackgroundTaskManager::ContinuousTask::Mgr::RequestUpdateBackgroundRunning");
128 
129     std::lock_guard<std::mutex> lock(mutex_);
130     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
131 
132     sptr<ContinuousTaskParam> taskParamPtr = new (std::nothrow) ContinuousTaskParam(taskParam);
133     if (taskParamPtr == nullptr || taskParamPtr.GetRefPtr() == nullptr) {
134         BGTASK_LOGE("Failed to create continuous task param");
135         return ERR_BGTASK_NO_MEMORY;
136     }
137 
138     BGTASK_LOGD(" %{public}u = %{public}u, %{public}d = %{public}d, abilityId = %{public}d",
139         static_cast<uint32_t>(taskParamPtr->bgModeIds_.size()),
140         static_cast<uint32_t>(taskParam.bgModeIds_.size()), taskParamPtr->isBatchApi_, taskParam.isBatchApi_,
141         taskParamPtr->abilityId_);
142     int32_t notificationId = taskParamPtr->notificationId_;
143     int32_t continuousTaskId = taskParamPtr->continuousTaskId_;
144     ErrCode ret = proxy_->UpdateBackgroundRunning(*taskParamPtr.GetRefPtr(), notificationId, continuousTaskId);
145     taskParam.notificationId_ = notificationId;
146     taskParam.continuousTaskId_ = continuousTaskId;
147     return ret;
148 }
149 
RequestBackgroundRunningForInner(const ContinuousTaskParamForInner & taskParam)150 ErrCode BackgroundTaskManager::RequestBackgroundRunningForInner(const ContinuousTaskParamForInner &taskParam)
151 {
152     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
153         "BackgroundTaskManager::ContinuousTask::Mgr::RequestBackgroundRunningForInner");
154 
155     std::lock_guard<std::mutex> lock(mutex_);
156     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
157 
158     sptr<ContinuousTaskParamForInner> taskParamPtr = new (std::nothrow) ContinuousTaskParamForInner(taskParam);
159     if (taskParamPtr == nullptr || taskParamPtr.GetRefPtr() == nullptr) {
160         BGTASK_LOGE("Failed to create continuous task param");
161         return ERR_BGTASK_NO_MEMORY;
162     }
163 
164     return proxy_->RequestBackgroundRunningForInner(*taskParamPtr.GetRefPtr());
165 }
166 
RequestGetContinuousTasksByUidForInner(int32_t uid,std::vector<std::shared_ptr<ContinuousTaskInfo>> & list)167 ErrCode BackgroundTaskManager::RequestGetContinuousTasksByUidForInner(int32_t uid,
168     std::vector<std::shared_ptr<ContinuousTaskInfo>> &list)
169 {
170     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
171         "BackgroundTaskManager::ContinuousTask::Mgr::RequestGetContinuousTasksByUidForInner");
172 
173     std::lock_guard<std::mutex> lock(mutex_);
174     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
175 
176     if (uid < 0) {
177         BGTASK_LOGE("param uid: %{public}d is invaild.", uid);
178         return ERR_BGTASK_INVALID_UID;
179     }
180 
181     std::vector<ContinuousTaskInfo> tasksList;
182     ErrCode result = proxy_->RequestGetContinuousTasksByUidForInner(uid, tasksList);
183     if (result == ERR_OK) {
184         list.clear();
185         for (const auto& item : tasksList) {
186             list.push_back(std::make_shared<ContinuousTaskInfo>(item));
187         }
188     }
189     return result;
190 }
191 
RequestStopBackgroundRunning(const std::string & abilityName,const sptr<IRemoteObject> & abilityToken,int32_t abilityId)192 ErrCode BackgroundTaskManager::RequestStopBackgroundRunning(const std::string &abilityName,
193     const sptr<IRemoteObject> &abilityToken, int32_t abilityId)
194 {
195     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
196         "BackgroundTaskManager::ContinuousTask::Mgr::RequestStopBackgroundRunning");
197 
198     std::lock_guard<std::mutex> lock(mutex_);
199     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
200 
201     return proxy_->StopBackgroundRunning(abilityName, abilityToken, abilityId);
202 }
203 
RequestGetAllContinuousTasks(std::vector<std::shared_ptr<ContinuousTaskInfo>> & list)204 ErrCode BackgroundTaskManager::RequestGetAllContinuousTasks(std::vector<std::shared_ptr<ContinuousTaskInfo>> &list)
205 {
206     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
207         "BackgroundTaskManager::ContinuousTask::Mgr::RequestGetAllContinuousTasks");
208 
209     std::lock_guard<std::mutex> lock(mutex_);
210     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
211 
212     std::vector<ContinuousTaskInfo> tasksList;
213     ErrCode result = proxy_->GetAllContinuousTasks(tasksList);
214     if (result == ERR_OK) {
215         list.clear();
216         for (const auto& item : tasksList) {
217             list.push_back(std::make_shared<ContinuousTaskInfo>(item));
218         }
219     }
220     return result;
221 }
222 
RequestGetAllContinuousTasks(std::vector<std::shared_ptr<ContinuousTaskInfo>> & list,bool includeSuspended)223 ErrCode BackgroundTaskManager::RequestGetAllContinuousTasks(
224     std::vector<std::shared_ptr<ContinuousTaskInfo>> &list, bool includeSuspended)
225 {
226     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
227         "BackgroundTaskManager::ContinuousTask::Mgr::GetAllContinuousTasksIncludeSuspended");
228 
229     std::lock_guard<std::mutex> lock(mutex_);
230     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
231 
232     return proxy_->GetAllContinuousTasks(list, includeSuspended);
233 }
234 
SubscribeBackgroundTask(const BackgroundTaskSubscriber & subscriber)235 __attribute__((no_sanitize("cfi"))) ErrCode BackgroundTaskManager::SubscribeBackgroundTask(
236     const BackgroundTaskSubscriber &subscriber)
237 {
238     std::lock_guard<std::mutex> lock(mutex_);
239     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
240 
241     sptr<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl> subscriberSptr = subscriber.GetImpl();
242     if (subscriberSptr == nullptr) {
243         BGTASK_LOGE("subscriberSptr is nullptr");
244         return ERR_BGTASK_INVALID_PARAM;
245     }
246     return proxy_->SubscribeBackgroundTask(subscriberSptr, subscriber.flag_);
247 }
248 
UnsubscribeBackgroundTask(const BackgroundTaskSubscriber & subscriber)249 ErrCode BackgroundTaskManager::UnsubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber)
250 {
251     std::lock_guard<std::mutex> lock(mutex_);
252     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
253 
254     sptr<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl> subscriberSptr = subscriber.GetImpl();
255     if (subscriberSptr == nullptr) {
256         BGTASK_LOGE("subscriberSptr is nullptr");
257         return ERR_BGTASK_INVALID_PARAM;
258     }
259     return proxy_->UnsubscribeBackgroundTask(subscriberSptr);
260 }
261 
GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> & list)262 ErrCode BackgroundTaskManager::GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list)
263 {
264     std::lock_guard<std::mutex> lock(mutex_);
265     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
266 
267     std::vector<TransientTaskAppInfo> TaskAppsList;
268     ErrCode result = proxy_->GetTransientTaskApps(TaskAppsList);
269     if (result == ERR_OK) {
270         list.clear();
271         for (const auto& item : TaskAppsList) {
272             list.push_back(std::make_shared<TransientTaskAppInfo>(item));
273         }
274     }
275 
276     return result;
277 }
278 
PauseTransientTaskTimeForInner(int32_t uid)279 ErrCode BackgroundTaskManager::PauseTransientTaskTimeForInner(int32_t uid)
280 {
281     std::lock_guard<std::mutex> lock(mutex_);
282     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
283 
284     return proxy_->PauseTransientTaskTimeForInner(uid);
285 }
286 
StartTransientTaskTimeForInner(int32_t uid)287 ErrCode BackgroundTaskManager::StartTransientTaskTimeForInner(int32_t uid)
288 {
289     std::lock_guard<std::mutex> lock(mutex_);
290     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
291 
292     return proxy_->StartTransientTaskTimeForInner(uid);
293 }
294 
ApplyEfficiencyResources(const EfficiencyResourceInfo & resourceInfo)295 ErrCode BackgroundTaskManager::ApplyEfficiencyResources(const EfficiencyResourceInfo &resourceInfo)
296 {
297     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
298         "BackgroundTaskManager::EfficiencyResource::Mgr::ApplyEfficiencyResources");
299 
300     std::lock_guard<std::mutex> lock(mutex_);
301     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
302 
303     sptr<EfficiencyResourceInfo> resourceInfoPtr = new (std::nothrow) EfficiencyResourceInfo(resourceInfo);
304     if (resourceInfoPtr == nullptr || resourceInfoPtr.GetRefPtr() == nullptr) {
305         BGTASK_LOGE("Failed to create efficiency resource info");
306         return ERR_BGTASK_NO_MEMORY;
307     }
308     return proxy_->ApplyEfficiencyResources(*resourceInfoPtr.GetRefPtr());
309 }
310 
ResetAllEfficiencyResources()311 ErrCode BackgroundTaskManager::ResetAllEfficiencyResources()
312 {
313     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
314         "BackgroundTaskManager::EfficiencyResource::Mgr::ResetAllEfficiencyResources");
315 
316     std::lock_guard<std::mutex> lock(mutex_);
317     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
318 
319     return proxy_->ResetAllEfficiencyResources();
320 }
321 
GetAllEfficiencyResources(std::vector<std::shared_ptr<EfficiencyResourceInfo>> & resourceInfoList)322 ErrCode BackgroundTaskManager::GetAllEfficiencyResources(
323     std::vector<std::shared_ptr<EfficiencyResourceInfo>> &resourceInfoList)
324 {
325     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
326         "BackgroundTaskManager::EfficiencyResource::Mgr::GetAllEfficiencyResources");
327 
328     std::lock_guard<std::mutex> lock(mutex_);
329     if (!GetBackgroundTaskManagerProxy()) {
330         BGTASK_LOGE("GetBackgroundTaskManager Proxy failed.");
331         return ERR_BGTASK_RESOURCES_SERVICE_NOT_CONNECTED;
332     }
333 
334     std::vector<EfficiencyResourceInfo> list;
335     ErrCode result = proxy_->GetAllEfficiencyResources(list);
336     if (result == ERR_OK) {
337         resourceInfoList.clear();
338         for (const auto& item : list) {
339             resourceInfoList.push_back(std::make_shared<EfficiencyResourceInfo>(item));
340         }
341     }
342     return result;
343 }
344 
GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> & appList,std::vector<std::shared_ptr<ResourceCallbackInfo>> & procList)345 ErrCode BackgroundTaskManager::GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> &appList,
346     std::vector<std::shared_ptr<ResourceCallbackInfo>> &procList)
347 {
348     std::lock_guard<std::mutex> lock(mutex_);
349     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
350 
351     std::vector<ResourceCallbackInfo> resourceAppList;
352     std::vector<ResourceCallbackInfo> resourceProcList;
353     ErrCode result = proxy_->GetEfficiencyResourcesInfos(resourceAppList, resourceProcList);
354     if (result == ERR_OK) {
355         appList.clear();
356         procList.clear();
357         for (const auto& item : resourceAppList) {
358             appList.push_back(std::make_shared<ResourceCallbackInfo>(item));
359         }
360 
361         for (const auto& item : resourceProcList) {
362             procList.push_back(std::make_shared<ResourceCallbackInfo>(item));
363         }
364     }
365     return result;
366 }
367 
GetBackgroundTaskManagerProxy()368 bool BackgroundTaskManager::GetBackgroundTaskManagerProxy()
369 {
370     if (proxy_ != nullptr) {
371         return true;
372     }
373     sptr<ISystemAbilityManager> systemAbilityManager =
374         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
375     if (systemAbilityManager == nullptr) {
376         BGTASK_LOGE("GetBackgroundTaskManagerProxy GetSystemAbilityManager failed.");
377         return false;
378     }
379 
380     sptr<IRemoteObject> remoteObject =
381         systemAbilityManager->GetSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID);
382     if (remoteObject == nullptr) {
383         BGTASK_LOGE("GetBackgroundTaskManagerProxy GetSystemAbility failed.");
384         return false;
385     }
386 
387     proxy_ = iface_cast<BackgroundTaskMgr::IBackgroundTaskMgr>(remoteObject);
388     if ((proxy_ == nullptr) || (proxy_->AsObject() == nullptr)) {
389         BGTASK_LOGE("GetBackgroundTaskManagerProxy iface_cast remoteObject failed.");
390         return false;
391     }
392 
393     recipient_ = new (std::nothrow) BgTaskMgrDeathRecipient(*this);
394     if (recipient_ == nullptr) {
395         return false;
396     }
397     proxy_->AsObject()->AddDeathRecipient(recipient_);
398     return true;
399 }
400 
GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> & list)401 ErrCode BackgroundTaskManager::GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list)
402 {
403     std::lock_guard<std::mutex> lock(mutex_);
404     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
405 
406     std::vector<ContinuousTaskCallbackInfo> TaskAppsList;
407     ErrCode result = proxy_->GetContinuousTaskApps(TaskAppsList);
408     if (result == ERR_OK) {
409         list.clear();
410         for (const auto& item : TaskAppsList) {
411             list.push_back(std::make_shared<ContinuousTaskCallbackInfo>(item));
412         }
413     }
414 
415     return result;
416 }
417 
StopContinuousTask(int32_t uid,int32_t pid,uint32_t taskType,const std::string & key)418 ErrCode BackgroundTaskManager::StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key)
419 {
420     std::lock_guard<std::mutex> lock(mutex_);
421     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
422 
423     return proxy_->StopContinuousTask(uid, pid, taskType, key);
424 }
425 
SuspendContinuousTask(int32_t uid,int32_t pid,int32_t reason,const std::string & key)426 ErrCode BackgroundTaskManager::SuspendContinuousTask(int32_t uid, int32_t pid, int32_t reason, const std::string &key)
427 {
428     std::lock_guard<std::mutex> lock(mutex_);
429     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
430 
431     return proxy_->SuspendContinuousTask(uid, pid, reason, key);
432 }
433 
ActiveContinuousTask(int32_t uid,int32_t pid,const std::string & key)434 ErrCode BackgroundTaskManager::ActiveContinuousTask(int32_t uid, int32_t pid, const std::string &key)
435 {
436     std::lock_guard<std::mutex> lock(mutex_);
437     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
438 
439     return proxy_->ActiveContinuousTask(uid, pid, key);
440 }
441 
AVSessionNotifyUpdateNotification(int32_t uid,int32_t pid,bool isPublish)442 ErrCode BackgroundTaskManager::AVSessionNotifyUpdateNotification(int32_t uid, int32_t pid, bool isPublish)
443 {
444     std::lock_guard<std::mutex> lock(mutex_);
445     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
446 
447     return proxy_->AVSessionNotifyUpdateNotification(uid, pid, isPublish);
448 }
449 
ResetBackgroundTaskManagerProxy()450 void BackgroundTaskManager::ResetBackgroundTaskManagerProxy()
451 {
452     std::lock_guard<std::mutex> lock(mutex_);
453     if ((proxy_ != nullptr) && (proxy_->AsObject() != nullptr)) {
454         proxy_->AsObject()->RemoveDeathRecipient(recipient_);
455     }
456     proxy_ = nullptr;
457 }
458 
SetBgTaskConfig(const std::string & configData,int32_t sourceType)459 ErrCode BackgroundTaskManager::SetBgTaskConfig(const std::string &configData, int32_t sourceType)
460 {
461     std::lock_guard<std::mutex> lock(mutex_);
462     GET_BACK_GROUND_TASK_MANAGER_PROXY_RETURN
463 
464     return proxy_->SetBgTaskConfig(configData, sourceType);
465 }
466 
BgTaskMgrDeathRecipient(BackgroundTaskManager & backgroundTaskManager)467 BackgroundTaskManager::BgTaskMgrDeathRecipient::BgTaskMgrDeathRecipient(BackgroundTaskManager &backgroundTaskManager)
468     : backgroundTaskManager_(backgroundTaskManager) {}
469 
~BgTaskMgrDeathRecipient()470 BackgroundTaskManager::BgTaskMgrDeathRecipient::~BgTaskMgrDeathRecipient() {}
471 
OnRemoteDied(const wptr<IRemoteObject> & remote)472 void BackgroundTaskManager::BgTaskMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
473 {
474     backgroundTaskManager_.ResetBackgroundTaskManagerProxy();
475 }
476 }  // namespace BackgroundTaskMgr
477 }  // namespace OHOS