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