• 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 "iservice_registry.h"
19 #include "system_ability_definition.h"
20 
21 #include "bgtaskmgr_inner_errors.h"
22 #include "bgtaskmgr_log_wrapper.h"
23 #include "delay_suspend_info.h"
24 
25 namespace OHOS {
26 namespace BackgroundTaskMgr {
BackgroundTaskManager()27 BackgroundTaskManager::BackgroundTaskManager() {}
28 
~BackgroundTaskManager()29 BackgroundTaskManager::~BackgroundTaskManager() {}
30 
CancelSuspendDelay(int32_t requestId)31 ErrCode BackgroundTaskManager::CancelSuspendDelay(int32_t requestId)
32 {
33     if (!GetBackgroundTaskManagerProxy()) {
34         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
35         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
36     }
37     return backgroundTaskMgrProxy_->CancelSuspendDelay(requestId);
38 }
39 
RequestSuspendDelay(const std::u16string & reason,const ExpiredCallback & callback,std::shared_ptr<DelaySuspendInfo> & delayInfo)40 ErrCode BackgroundTaskManager::RequestSuspendDelay(const std::u16string &reason,
41     const ExpiredCallback &callback, std::shared_ptr<DelaySuspendInfo> &delayInfo)
42 {
43     if (!GetBackgroundTaskManagerProxy()) {
44         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
45         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
46     }
47 
48     sptr<ExpiredCallback::ExpiredCallbackImpl> callbackSptr = callback.GetImpl();
49     if (callbackSptr == nullptr) {
50         BGTASK_LOGE("callbackSptr == nullptr");
51         return ERR_BGTASK_INVALID_PARAM;
52     }
53     return backgroundTaskMgrProxy_->RequestSuspendDelay(reason, callbackSptr, delayInfo);
54 }
55 
GetRemainingDelayTime(int32_t requestId,int32_t & delayTime)56 ErrCode BackgroundTaskManager::GetRemainingDelayTime(int32_t requestId, int32_t &delayTime)
57 {
58     if (!GetBackgroundTaskManagerProxy()) {
59         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
60         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
61     }
62     return backgroundTaskMgrProxy_->GetRemainingDelayTime(requestId, delayTime);
63 }
64 
RequestStartBackgroundRunning(const ContinuousTaskParam & taskParam)65 ErrCode BackgroundTaskManager::RequestStartBackgroundRunning(const ContinuousTaskParam &taskParam)
66 {
67     BGTASK_LOGI("begin");
68     if (!GetBackgroundTaskManagerProxy()) {
69         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
70         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
71     }
72 
73     sptr<ContinuousTaskParam> taskParamPtr = new (std::nothrow) ContinuousTaskParam(taskParam);
74     if (taskParamPtr == nullptr) {
75         BGTASK_LOGE("Failed to create continuous task param");
76         return ERR_BGTASK_NO_MEMORY;
77     }
78     BGTASK_LOGI("end");
79     return backgroundTaskMgrProxy_->StartBackgroundRunning(taskParamPtr);
80 }
81 
RequestStopBackgroundRunning(const std::string & abilityName,const sptr<IRemoteObject> & abilityToken)82 ErrCode BackgroundTaskManager::RequestStopBackgroundRunning(const std::string &abilityName,
83     const sptr<IRemoteObject> &abilityToken)
84 {
85     BGTASK_LOGI("begin");
86     if (!GetBackgroundTaskManagerProxy()) {
87         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
88         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
89     }
90     BGTASK_LOGI("end");
91     return backgroundTaskMgrProxy_->StopBackgroundRunning(abilityName, abilityToken);
92 }
93 
SubscribeBackgroundTask(const BackgroundTaskSubscriber & subscriber)94 ErrCode BackgroundTaskManager::SubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber)
95 {
96     if (!GetBackgroundTaskManagerProxy()) {
97         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
98         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
99     }
100     sptr<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl> subscriberSptr = subscriber.GetImpl();
101     if (subscriberSptr == nullptr) {
102         BGTASK_LOGE("subscriberSptr == nullptr");
103         return ERR_BGTASK_INVALID_PARAM;
104     }
105     return backgroundTaskMgrProxy_->SubscribeBackgroundTask(subscriberSptr);
106 }
107 
UnsubscribeBackgroundTask(const BackgroundTaskSubscriber & subscriber)108 ErrCode BackgroundTaskManager::UnsubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber)
109 {
110     if (!GetBackgroundTaskManagerProxy()) {
111         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
112         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
113     }
114     sptr<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl> subscriberSptr = subscriber.GetImpl();
115     if (subscriberSptr == nullptr) {
116         BGTASK_LOGE("subscriberSptr == nullptr");
117         return ERR_BGTASK_INVALID_PARAM;
118     }
119     return backgroundTaskMgrProxy_->UnsubscribeBackgroundTask(subscriberSptr);
120 }
121 
GetBackgroundTaskManagerProxy()122 bool BackgroundTaskManager::GetBackgroundTaskManagerProxy()
123 {
124     if (backgroundTaskMgrProxy_ != nullptr) {
125         return true;
126     }
127     std::lock_guard<std::mutex> lock(mutex_);
128     sptr<ISystemAbilityManager> systemAbilityManager =
129         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
130     if (systemAbilityManager == nullptr) {
131         BGTASK_LOGE("GetSystemAbilityManager failed.");
132         return false;
133     }
134 
135     sptr<IRemoteObject> remoteObject =
136         systemAbilityManager->GetSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID);
137     if (remoteObject == nullptr) {
138         BGTASK_LOGE("GetSystemAbility failed.");
139         return false;
140     }
141 
142     backgroundTaskMgrProxy_ = iface_cast<BackgroundTaskMgr::IBackgroundTaskMgr>(remoteObject);
143     if ((backgroundTaskMgrProxy_ == nullptr) || (backgroundTaskMgrProxy_->AsObject() == nullptr)) {
144         BGTASK_LOGE("iface_cast remoteObject failed.");
145         return false;
146     }
147 
148     recipient_ = new (std::nothrow) BgTaskMgrDeathRecipient(*this);
149     if (recipient_ == nullptr) {
150         return false;
151     }
152     backgroundTaskMgrProxy_->AsObject()->AddDeathRecipient(recipient_);
153     return true;
154 }
155 
ResetBackgroundTaskManagerProxy()156 void BackgroundTaskManager::ResetBackgroundTaskManagerProxy()
157 {
158     std::lock_guard<std::mutex> lock(mutex_);
159     if ((backgroundTaskMgrProxy_ != nullptr) && (backgroundTaskMgrProxy_->AsObject() != nullptr)) {
160         backgroundTaskMgrProxy_->AsObject()->RemoveDeathRecipient(recipient_);
161     }
162     backgroundTaskMgrProxy_ = nullptr;
163 }
164 
ShellDump(const std::vector<std::string> & dumpOption,std::vector<std::string> & dumpInfo)165 ErrCode BackgroundTaskManager::ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo)
166 {
167     if (!GetBackgroundTaskManagerProxy()) {
168         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
169         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
170     }
171     return backgroundTaskMgrProxy_->ShellDump(dumpOption, dumpInfo);
172 }
173 
BgTaskMgrDeathRecipient(BackgroundTaskManager & backgroundTaskManager)174 BackgroundTaskManager::BgTaskMgrDeathRecipient::BgTaskMgrDeathRecipient(BackgroundTaskManager &backgroundTaskManager)
175     : backgroundTaskManager_(backgroundTaskManager) {};
176 
~BgTaskMgrDeathRecipient()177 BackgroundTaskManager::BgTaskMgrDeathRecipient::~BgTaskMgrDeathRecipient() {};
178 
OnRemoteDied(const wptr<IRemoteObject> & remote)179 void BackgroundTaskManager::BgTaskMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
180 {
181     backgroundTaskManager_.ResetBackgroundTaskManagerProxy();
182 }
183 }  // namespace BackgroundTaskMgr
184 }  // namespace OHOS