• 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 
CancelSuspendDelay(int32_t requestId)32 ErrCode BackgroundTaskManager::CancelSuspendDelay(int32_t requestId)
33 {
34     std::lock_guard<std::mutex> lock(mutex_);
35     if (!GetBackgroundTaskManagerProxy()) {
36         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
37         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
38     }
39     return backgroundTaskMgrProxy_->CancelSuspendDelay(requestId);
40 }
41 
RequestSuspendDelay(const std::u16string & reason,const ExpiredCallback & callback,std::shared_ptr<DelaySuspendInfo> & delayInfo)42 ErrCode BackgroundTaskManager::RequestSuspendDelay(const std::u16string &reason,
43     const ExpiredCallback &callback, std::shared_ptr<DelaySuspendInfo> &delayInfo)
44 {
45     std::lock_guard<std::mutex> lock(mutex_);
46     if (!GetBackgroundTaskManagerProxy()) {
47         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
48         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
49     }
50 
51     sptr<ExpiredCallback::ExpiredCallbackImpl> callbackSptr = callback.GetImpl();
52     if (callbackSptr == nullptr) {
53         BGTASK_LOGE("callbackSptr is nullptr");
54         return ERR_CALLBACK_NULL_OR_TYPE_ERR;
55     }
56     return backgroundTaskMgrProxy_->RequestSuspendDelay(reason, callbackSptr, delayInfo);
57 }
58 
GetRemainingDelayTime(int32_t requestId,int32_t & delayTime)59 ErrCode BackgroundTaskManager::GetRemainingDelayTime(int32_t requestId, int32_t &delayTime)
60 {
61     std::lock_guard<std::mutex> lock(mutex_);
62     if (!GetBackgroundTaskManagerProxy()) {
63         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
64         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
65     }
66     return backgroundTaskMgrProxy_->GetRemainingDelayTime(requestId, delayTime);
67 }
68 
RequestStartBackgroundRunning(const ContinuousTaskParam & taskParam)69 ErrCode BackgroundTaskManager::RequestStartBackgroundRunning(const ContinuousTaskParam &taskParam)
70 {
71     StartTrace(HITRACE_TAG_OHOS, "BackgroundTaskManager::RequestStartBackgroundRunning");
72     std::lock_guard<std::mutex> lock(mutex_);
73     if (!GetBackgroundTaskManagerProxy()) {
74         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
75         FinishTrace(HITRACE_TAG_OHOS);
76         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
77     }
78 
79     sptr<ContinuousTaskParam> taskParamPtr = new (std::nothrow) ContinuousTaskParam(taskParam);
80     if (taskParamPtr == nullptr) {
81         BGTASK_LOGE("Failed to create continuous task param");
82         return ERR_BGTASK_NO_MEMORY;
83     }
84     ErrCode ret = backgroundTaskMgrProxy_->StartBackgroundRunning(taskParamPtr);
85     FinishTrace(HITRACE_TAG_OHOS);
86     return ret;
87 }
88 
RequestBackgroundRunningForInner(const ContinuousTaskParamForInner & taskParam)89 ErrCode BackgroundTaskManager::RequestBackgroundRunningForInner(const ContinuousTaskParamForInner &taskParam)
90 {
91     std::lock_guard<std::mutex> lock(mutex_);
92     if (!GetBackgroundTaskManagerProxy()) {
93         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
94         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
95     }
96 
97     sptr<ContinuousTaskParamForInner> taskParamPtr = new (std::nothrow) ContinuousTaskParamForInner(taskParam);
98     if (taskParamPtr == nullptr) {
99         BGTASK_LOGE("Failed to create continuous task param");
100         return ERR_BGTASK_NO_MEMORY;
101     }
102 
103     return backgroundTaskMgrProxy_->RequestBackgroundRunningForInner(taskParamPtr);
104 }
105 
RequestStopBackgroundRunning(const std::string & abilityName,const sptr<IRemoteObject> & abilityToken)106 ErrCode BackgroundTaskManager::RequestStopBackgroundRunning(const std::string &abilityName,
107     const sptr<IRemoteObject> &abilityToken)
108 {
109     StartTrace(HITRACE_TAG_OHOS, "BackgroundTaskManager::RequestStopBackgroundRunning");
110     std::lock_guard<std::mutex> lock(mutex_);
111     if (!GetBackgroundTaskManagerProxy()) {
112         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
113         FinishTrace(HITRACE_TAG_OHOS);
114         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
115     }
116 
117     ErrCode ret = backgroundTaskMgrProxy_->StopBackgroundRunning(abilityName, abilityToken);
118     FinishTrace(HITRACE_TAG_OHOS);
119     return ret;
120 }
121 
SubscribeBackgroundTask(const BackgroundTaskSubscriber & subscriber)122 __attribute__((no_sanitize("cfi"))) ErrCode BackgroundTaskManager::SubscribeBackgroundTask(
123     const BackgroundTaskSubscriber &subscriber)
124 {
125     std::lock_guard<std::mutex> lock(mutex_);
126     if (!GetBackgroundTaskManagerProxy()) {
127         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
128         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
129     }
130     sptr<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl> subscriberSptr = subscriber.GetImpl();
131     if (subscriberSptr == nullptr) {
132         BGTASK_LOGE("subscriberSptr is nullptr");
133         return ERR_BGTASK_INVALID_PARAM;
134     }
135     return backgroundTaskMgrProxy_->SubscribeBackgroundTask(subscriberSptr);
136 }
137 
UnsubscribeBackgroundTask(const BackgroundTaskSubscriber & subscriber)138 ErrCode BackgroundTaskManager::UnsubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber)
139 {
140     std::lock_guard<std::mutex> lock(mutex_);
141     if (!GetBackgroundTaskManagerProxy()) {
142         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
143         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
144     }
145     sptr<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl> subscriberSptr = subscriber.GetImpl();
146     if (subscriberSptr == nullptr) {
147         BGTASK_LOGE("subscriberSptr is nullptr");
148         return ERR_BGTASK_INVALID_PARAM;
149     }
150     return backgroundTaskMgrProxy_->UnsubscribeBackgroundTask(subscriberSptr);
151 }
152 
GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> & list)153 ErrCode BackgroundTaskManager::GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list)
154 {
155     std::lock_guard<std::mutex> lock(mutex_);
156     if (!GetBackgroundTaskManagerProxy()) {
157         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
158         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
159     }
160     return backgroundTaskMgrProxy_->GetTransientTaskApps(list);
161 }
162 
ApplyEfficiencyResources(const EfficiencyResourceInfo & resourceInfo)163 ErrCode BackgroundTaskManager::ApplyEfficiencyResources(const EfficiencyResourceInfo &resourceInfo)
164 {
165     std::lock_guard<std::mutex> lock(mutex_);
166     if (!GetBackgroundTaskManagerProxy()) {
167         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
168         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
169     }
170     sptr<EfficiencyResourceInfo> resourceInfoPtr = new (std::nothrow) EfficiencyResourceInfo(resourceInfo);
171     if (resourceInfoPtr == nullptr) {
172         BGTASK_LOGE("Failed to create efficiency resource info");
173         return ERR_BGTASK_NO_MEMORY;
174     }
175     ErrCode ret = backgroundTaskMgrProxy_->ApplyEfficiencyResources(resourceInfoPtr);
176     return ret;
177 }
178 
ResetAllEfficiencyResources()179 ErrCode BackgroundTaskManager::ResetAllEfficiencyResources()
180 {
181     std::lock_guard<std::mutex> lock(mutex_);
182     if (!GetBackgroundTaskManagerProxy()) {
183         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
184         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
185     }
186 
187     ErrCode ret = backgroundTaskMgrProxy_->ResetAllEfficiencyResources();
188     return ret;
189 }
190 
GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> & appList,std::vector<std::shared_ptr<ResourceCallbackInfo>> & procList)191 ErrCode BackgroundTaskManager::GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> &appList,
192     std::vector<std::shared_ptr<ResourceCallbackInfo>> &procList)
193 {
194     std::lock_guard<std::mutex> lock(mutex_);
195     if (!GetBackgroundTaskManagerProxy()) {
196         BGTASK_LOGE("GetEfficiencyResourcesInfos failed.");
197         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
198     }
199 
200     return backgroundTaskMgrProxy_->GetEfficiencyResourcesInfos(appList, procList);
201 }
202 
GetBackgroundTaskManagerProxy()203 bool BackgroundTaskManager::GetBackgroundTaskManagerProxy()
204 {
205     if (backgroundTaskMgrProxy_ != nullptr) {
206         return true;
207     }
208     sptr<ISystemAbilityManager> systemAbilityManager =
209         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
210     if (systemAbilityManager == nullptr) {
211         BGTASK_LOGE("GetBackgroundTaskManagerProxy GetSystemAbilityManager failed.");
212         return false;
213     }
214 
215     sptr<IRemoteObject> remoteObject =
216         systemAbilityManager->GetSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID);
217     if (remoteObject == nullptr) {
218         BGTASK_LOGE("GetBackgroundTaskManagerProxy GetSystemAbility failed.");
219         return false;
220     }
221 
222     backgroundTaskMgrProxy_ = iface_cast<BackgroundTaskMgr::IBackgroundTaskMgr>(remoteObject);
223     if ((backgroundTaskMgrProxy_ == nullptr) || (backgroundTaskMgrProxy_->AsObject() == nullptr)) {
224         BGTASK_LOGE("GetBackgroundTaskManagerProxy iface_cast remoteObject failed.");
225         return false;
226     }
227 
228     recipient_ = new (std::nothrow) BgTaskMgrDeathRecipient(*this);
229     if (recipient_ == nullptr) {
230         return false;
231     }
232     backgroundTaskMgrProxy_->AsObject()->AddDeathRecipient(recipient_);
233     return true;
234 }
235 
GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> & list)236 ErrCode BackgroundTaskManager::GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list)
237 {
238     std::lock_guard<std::mutex> lock(mutex_);
239     if (!GetBackgroundTaskManagerProxy()) {
240         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
241         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
242     }
243     return backgroundTaskMgrProxy_->GetContinuousTaskApps(list);
244 }
245 
StopContinuousTask(int32_t uid,int32_t pid,uint32_t taskType)246 ErrCode BackgroundTaskManager::StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType)
247 {
248     std::lock_guard<std::mutex> lock(mutex_);
249     if (!GetBackgroundTaskManagerProxy()) {
250         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
251         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
252     }
253     return backgroundTaskMgrProxy_->StopContinuousTask(uid, pid, taskType);
254 }
255 
ResetBackgroundTaskManagerProxy()256 void BackgroundTaskManager::ResetBackgroundTaskManagerProxy()
257 {
258     std::lock_guard<std::mutex> lock(mutex_);
259     if ((backgroundTaskMgrProxy_ != nullptr) && (backgroundTaskMgrProxy_->AsObject() != nullptr)) {
260         backgroundTaskMgrProxy_->AsObject()->RemoveDeathRecipient(recipient_);
261     }
262     backgroundTaskMgrProxy_ = nullptr;
263 }
264 
BgTaskMgrDeathRecipient(BackgroundTaskManager & backgroundTaskManager)265 BackgroundTaskManager::BgTaskMgrDeathRecipient::BgTaskMgrDeathRecipient(BackgroundTaskManager &backgroundTaskManager)
266     : backgroundTaskManager_(backgroundTaskManager) {}
267 
~BgTaskMgrDeathRecipient()268 BackgroundTaskManager::BgTaskMgrDeathRecipient::~BgTaskMgrDeathRecipient() {}
269 
OnRemoteDied(const wptr<IRemoteObject> & remote)270 void BackgroundTaskManager::BgTaskMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
271 {
272     backgroundTaskManager_.ResetBackgroundTaskManagerProxy();
273 }
274 }  // namespace BackgroundTaskMgr
275 }  // namespace OHOS