• 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 ErrCode BackgroundTaskManager::SubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber)
123 {
124     std::lock_guard<std::mutex> lock(mutex_);
125     if (!GetBackgroundTaskManagerProxy()) {
126         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
127         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
128     }
129     sptr<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl> subscriberSptr = subscriber.GetImpl();
130     if (subscriberSptr == nullptr) {
131         BGTASK_LOGE("subscriberSptr is nullptr");
132         return ERR_BGTASK_INVALID_PARAM;
133     }
134     return backgroundTaskMgrProxy_->SubscribeBackgroundTask(subscriberSptr);
135 }
136 
UnsubscribeBackgroundTask(const BackgroundTaskSubscriber & subscriber)137 ErrCode BackgroundTaskManager::UnsubscribeBackgroundTask(const BackgroundTaskSubscriber &subscriber)
138 {
139     std::lock_guard<std::mutex> lock(mutex_);
140     if (!GetBackgroundTaskManagerProxy()) {
141         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
142         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
143     }
144     sptr<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl> subscriberSptr = subscriber.GetImpl();
145     if (subscriberSptr == nullptr) {
146         BGTASK_LOGE("subscriberSptr is nullptr");
147         return ERR_BGTASK_INVALID_PARAM;
148     }
149     return backgroundTaskMgrProxy_->UnsubscribeBackgroundTask(subscriberSptr);
150 }
151 
GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> & list)152 ErrCode BackgroundTaskManager::GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list)
153 {
154     std::lock_guard<std::mutex> lock(mutex_);
155     if (!GetBackgroundTaskManagerProxy()) {
156         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
157         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
158     }
159     return backgroundTaskMgrProxy_->GetTransientTaskApps(list);
160 }
161 
ApplyEfficiencyResources(const EfficiencyResourceInfo & resourceInfo)162 ErrCode BackgroundTaskManager::ApplyEfficiencyResources(const EfficiencyResourceInfo &resourceInfo)
163 {
164     std::lock_guard<std::mutex> lock(mutex_);
165     if (!GetBackgroundTaskManagerProxy()) {
166         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
167         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
168     }
169     sptr<EfficiencyResourceInfo> resourceInfoPtr = new (std::nothrow) EfficiencyResourceInfo(resourceInfo);
170     if (resourceInfoPtr == nullptr) {
171         BGTASK_LOGE("Failed to create efficiency resource info");
172         return ERR_BGTASK_NO_MEMORY;
173     }
174     ErrCode ret = backgroundTaskMgrProxy_->ApplyEfficiencyResources(resourceInfoPtr);
175     return ret;
176 }
177 
ResetAllEfficiencyResources()178 ErrCode BackgroundTaskManager::ResetAllEfficiencyResources()
179 {
180     std::lock_guard<std::mutex> lock(mutex_);
181     if (!GetBackgroundTaskManagerProxy()) {
182         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
183         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
184     }
185 
186     ErrCode ret = backgroundTaskMgrProxy_->ResetAllEfficiencyResources();
187     return ret;
188 }
189 
GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> & appList,std::vector<std::shared_ptr<ResourceCallbackInfo>> & procList)190 ErrCode BackgroundTaskManager::GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> &appList,
191     std::vector<std::shared_ptr<ResourceCallbackInfo>> &procList)
192 {
193     std::lock_guard<std::mutex> lock(mutex_);
194     if (!GetBackgroundTaskManagerProxy()) {
195         BGTASK_LOGE("GetEfficiencyResourcesInfos failed.");
196         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
197     }
198 
199     return backgroundTaskMgrProxy_->GetEfficiencyResourcesInfos(appList, procList);
200 }
201 
GetBackgroundTaskManagerProxy()202 bool BackgroundTaskManager::GetBackgroundTaskManagerProxy()
203 {
204     if (backgroundTaskMgrProxy_ != nullptr) {
205         return true;
206     }
207     sptr<ISystemAbilityManager> systemAbilityManager =
208         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
209     if (systemAbilityManager == nullptr) {
210         BGTASK_LOGE("GetBackgroundTaskManagerProxy GetSystemAbilityManager failed.");
211         return false;
212     }
213 
214     sptr<IRemoteObject> remoteObject =
215         systemAbilityManager->GetSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID);
216     if (remoteObject == nullptr) {
217         BGTASK_LOGE("GetBackgroundTaskManagerProxy GetSystemAbility failed.");
218         return false;
219     }
220 
221     backgroundTaskMgrProxy_ = iface_cast<BackgroundTaskMgr::IBackgroundTaskMgr>(remoteObject);
222     if ((backgroundTaskMgrProxy_ == nullptr) || (backgroundTaskMgrProxy_->AsObject() == nullptr)) {
223         BGTASK_LOGE("GetBackgroundTaskManagerProxy iface_cast remoteObject failed.");
224         return false;
225     }
226 
227     recipient_ = new (std::nothrow) BgTaskMgrDeathRecipient(*this);
228     if (recipient_ == nullptr) {
229         return false;
230     }
231     backgroundTaskMgrProxy_->AsObject()->AddDeathRecipient(recipient_);
232     return true;
233 }
234 
GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> & list)235 ErrCode BackgroundTaskManager::GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list)
236 {
237     std::lock_guard<std::mutex> lock(mutex_);
238     if (!GetBackgroundTaskManagerProxy()) {
239         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
240         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
241     }
242     return backgroundTaskMgrProxy_->GetContinuousTaskApps(list);
243 }
244 
StopContinuousTask(int32_t uid,int32_t pid,uint32_t taskType)245 ErrCode BackgroundTaskManager::StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType)
246 {
247     std::lock_guard<std::mutex> lock(mutex_);
248     if (!GetBackgroundTaskManagerProxy()) {
249         BGTASK_LOGE("GetBackgroundTaskManagerProxy failed.");
250         return ERR_BGTASK_SERVICE_NOT_CONNECTED;
251     }
252     return backgroundTaskMgrProxy_->StopContinuousTask(uid, pid, taskType);
253 }
254 
ResetBackgroundTaskManagerProxy()255 void BackgroundTaskManager::ResetBackgroundTaskManagerProxy()
256 {
257     std::lock_guard<std::mutex> lock(mutex_);
258     if ((backgroundTaskMgrProxy_ != nullptr) && (backgroundTaskMgrProxy_->AsObject() != nullptr)) {
259         backgroundTaskMgrProxy_->AsObject()->RemoveDeathRecipient(recipient_);
260     }
261     backgroundTaskMgrProxy_ = nullptr;
262 }
263 
BgTaskMgrDeathRecipient(BackgroundTaskManager & backgroundTaskManager)264 BackgroundTaskManager::BgTaskMgrDeathRecipient::BgTaskMgrDeathRecipient(BackgroundTaskManager &backgroundTaskManager)
265     : backgroundTaskManager_(backgroundTaskManager) {}
266 
~BgTaskMgrDeathRecipient()267 BackgroundTaskManager::BgTaskMgrDeathRecipient::~BgTaskMgrDeathRecipient() {}
268 
OnRemoteDied(const wptr<IRemoteObject> & remote)269 void BackgroundTaskManager::BgTaskMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
270 {
271     backgroundTaskManager_.ResetBackgroundTaskManagerProxy();
272 }
273 }  // namespace BackgroundTaskMgr
274 }  // namespace OHOS