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_mgr_stub.h"
17
18 #include <ipc_skeleton.h>
19 #include <string_ex.h>
20
21 #include "hitrace_meter.h"
22 #include "bgtaskmgr_inner_errors.h"
23 #include "bgtaskmgr_log_wrapper.h"
24 #include "delay_suspend_info.h"
25 #include "ibackground_task_mgr_ipc_interface_code.h"
26
27 using namespace std;
28
29 namespace OHOS {
30 namespace BackgroundTaskMgr {
31
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 ErrCode BackgroundTaskMgrStub::OnRemoteRequest(uint32_t code,
33 MessageParcel& data, MessageParcel& reply, MessageOption& option)
34 {
35 std::u16string descriptor = BackgroundTaskMgrStub::GetDescriptor();
36 std::u16string remoteDescriptor = data.ReadInterfaceToken();
37 if (descriptor != remoteDescriptor) {
38 BGTASK_LOGE("BackgroundTaskMgrStub: Local descriptor not match remote.");
39 return ERR_TRANSACTION_FAILED;
40 }
41
42 return HandleOnRemoteResquestFunc(code, data, reply, option);
43 }
44
HandleOnRemoteResquestFunc(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)45 ErrCode BackgroundTaskMgrStub::HandleOnRemoteResquestFunc(uint32_t code,
46 MessageParcel& data, MessageParcel& reply, MessageOption& option)
47 {
48 switch (code) {
49 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::REQUEST_SUSPEND_DELAY):
50 HandleRequestSuspendDelay(data, reply);
51 break;
52 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::CANCEL_SUSPEND_DELAY):
53 HandleCancelSuspendDelay(data, reply);
54 break;
55 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::GET_REMAINING_DELAY_TIME):
56 HandleGetRemainingDelayTime(data, reply);
57 break;
58 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::START_BACKGROUND_RUNNING):
59 HandleStartBackgroundRunning(data, reply);
60 break;
61 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::STOP_BACKGROUND_RUNNING):
62 HandleStopBackgroundRunning(data, reply);
63 break;
64 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::SUBSCRIBE_BACKGROUND_TASK):
65 HandleSubscribeBackgroundTask(data, reply);
66 break;
67 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::UNSUBSCRIBE_BACKGROUND_TASK):
68 HandleUnsubscribeBackgroundTask(data, reply);
69 break;
70 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::GET_TRANSIENT_TASK_APPS):
71 HandleGetTransientTaskApps(data, reply);
72 break;
73 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::GET_CONTINUOUS_TASK_APPS):
74 HandleGetContinuousTaskApps(data, reply);
75 break;
76 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::APPLY_EFFICIENCY_RESOURCES):
77 HandleApplyEfficiencyResources(data, reply);
78 break;
79 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::RESET_ALL_EFFICIENCY_RESOURCES):
80 HandleResetAllEfficiencyResources(data, reply);
81 break;
82 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::GET_EFFICIENCY_RESOURCES_INFOS):
83 HandleGetEfficiencyResourcesInfos(data, reply);
84 break;
85 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::STOP_CONTINUOUS_TASK):
86 HandleStopContinuousTask(data, reply);
87 break;
88 case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::REQUEST_BACKGROUND_RUNNING_FOR_INNER):
89 HandleBackgroundRunningForInner(data, reply);
90 break;
91 default:
92 return IRemoteStub::OnRemoteRequest(code, data, reply, option);
93 }
94 return ERR_OK;
95 }
96
HandleRequestSuspendDelay(MessageParcel & data,MessageParcel & reply)97 ErrCode BackgroundTaskMgrStub::HandleRequestSuspendDelay(MessageParcel& data, MessageParcel& reply)
98 {
99 std::u16string reason = data.ReadString16();
100 sptr<IRemoteObject> callback = data.ReadRemoteObject();
101 if (callback == nullptr) {
102 BGTASK_LOGE("HandleRequestSuspendDelay Read callback fail.");
103 return ERR_BGTASK_PARCELABLE_FAILED;
104 }
105
106 std::shared_ptr<DelaySuspendInfo> info;
107 ErrCode result = RequestSuspendDelay(reason, iface_cast<IExpiredCallback>(callback), info);
108 if (!reply.WriteInt32(result)) {
109 BGTASK_LOGE("HandleRequestSuspendDelay Write result failed, ErrCode=%{public}d", result);
110 return ERR_BGTASK_PARCELABLE_FAILED;
111 }
112
113 if (info == nullptr || !info->Marshalling(reply)) {
114 BGTASK_LOGE("HandleRequestSuspendDelay Write result fail.");
115 return ERR_BGTASK_PARCELABLE_FAILED;
116 }
117 return ERR_OK;
118 }
119
HandleCancelSuspendDelay(MessageParcel & data,MessageParcel & reply)120 ErrCode BackgroundTaskMgrStub::HandleCancelSuspendDelay(MessageParcel& data, MessageParcel& reply)
121 {
122 int32_t id = data.ReadInt32();
123 ErrCode result = CancelSuspendDelay(id);
124 if (!reply.WriteInt32(result)) {
125 BGTASK_LOGE("HandleCancelSuspendDelay Write result failed, ErrCode=%{public}d", result);
126 return ERR_BGTASK_PARCELABLE_FAILED;
127 }
128 return ERR_OK;
129 }
130
HandleGetRemainingDelayTime(MessageParcel & data,MessageParcel & reply)131 ErrCode BackgroundTaskMgrStub::HandleGetRemainingDelayTime(MessageParcel& data, MessageParcel& reply)
132 {
133 int32_t id = data.ReadInt32();
134 int32_t time = 0;
135 ErrCode result = GetRemainingDelayTime(id, time);
136 if (!reply.WriteInt32(result)) {
137 BGTASK_LOGE("HandleGetRemainingDelayTime Write result failed, ErrCode=%{public}d", result);
138 return ERR_BGTASK_PARCELABLE_FAILED;
139 }
140 if (!reply.WriteInt32(time)) {
141 BGTASK_LOGE("HandleGetRemainingDelayTime Write result fail.");
142 return ERR_BGTASK_PARCELABLE_FAILED;
143 }
144 return ERR_OK;
145 }
146
HandleBackgroundRunningForInner(MessageParcel & data,MessageParcel & reply)147 ErrCode BackgroundTaskMgrStub::HandleBackgroundRunningForInner(MessageParcel &data, MessageParcel &reply)
148 {
149 StartTrace(HITRACE_TAG_OHOS, "BackgroundTaskMgrStub::HandleBackgroundRunningForInner");
150 sptr<ContinuousTaskParamForInner> taskParam = data.ReadParcelable<ContinuousTaskParamForInner>();
151 if (taskParam == nullptr) {
152 BGTASK_LOGE("ContinuousTaskParamForInner ReadParcelable failed");
153 FinishTrace(HITRACE_TAG_OHOS);
154 return ERR_BGTASK_PARCELABLE_FAILED;
155 }
156
157 ErrCode result = RequestBackgroundRunningForInner(taskParam);
158 if (!reply.WriteInt32(result)) {
159 BGTASK_LOGE("HandleBackgroundRunningForInner write result failed, ErrCode=%{public}d", result);
160 FinishTrace(HITRACE_TAG_OHOS);
161 return ERR_BGTASK_PARCELABLE_FAILED;
162 }
163
164 FinishTrace(HITRACE_TAG_OHOS);
165 return ERR_OK;
166 }
167
HandleStartBackgroundRunning(MessageParcel & data,MessageParcel & reply)168 ErrCode BackgroundTaskMgrStub::HandleStartBackgroundRunning(MessageParcel &data, MessageParcel &reply)
169 {
170 StartTrace(HITRACE_TAG_OHOS, "BackgroundTaskMgrStub::HandleStartBackgroundRunning");
171 sptr<ContinuousTaskParam> taskParam = data.ReadParcelable<ContinuousTaskParam>();
172 if (taskParam == nullptr) {
173 BGTASK_LOGE("ContinuousTaskParam ReadParcelable failed");
174 FinishTrace(HITRACE_TAG_OHOS);
175 return ERR_BGTASK_PARCELABLE_FAILED;
176 }
177 ErrCode result = StartBackgroundRunning(taskParam);
178 if (!reply.WriteInt32(result)) {
179 BGTASK_LOGE("HandleStartBackgroundRunning write result failed, ErrCode=%{public}d", result);
180 FinishTrace(HITRACE_TAG_OHOS);
181 return ERR_BGTASK_PARCELABLE_FAILED;
182 }
183 FinishTrace(HITRACE_TAG_OHOS);
184 return ERR_OK;
185 }
186
HandleStopBackgroundRunning(MessageParcel & data,MessageParcel & reply)187 ErrCode BackgroundTaskMgrStub::HandleStopBackgroundRunning(MessageParcel &data, MessageParcel &reply)
188 {
189 StartTrace(HITRACE_TAG_OHOS, "BackgroundTaskMgrStub::HandleStopBackgroundRunning");
190 std::u16string u16AbilityName;
191 if (!data.ReadString16(u16AbilityName)) {
192 FinishTrace(HITRACE_TAG_OHOS);
193 return ERR_BGTASK_PARCELABLE_FAILED;
194 }
195 std::string abilityName = Str16ToStr8(u16AbilityName);
196 sptr<IRemoteObject> abilityToken = data.ReadRemoteObject();
197 ErrCode result = StopBackgroundRunning(abilityName, abilityToken);
198 if (!reply.WriteInt32(result)) {
199 BGTASK_LOGE("HandleStopBackgroundRunning write result failed, ErrCode=%{public}d", result);
200 FinishTrace(HITRACE_TAG_OHOS);
201 return ERR_BGTASK_PARCELABLE_FAILED;
202 }
203 FinishTrace(HITRACE_TAG_OHOS);
204 return ERR_OK;
205 }
206
HandleSubscribeBackgroundTask(MessageParcel & data,MessageParcel & reply)207 ErrCode BackgroundTaskMgrStub::HandleSubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)
208 {
209 sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
210 if (subscriber == nullptr) {
211 BGTASK_LOGE("HandleSubscribeBackgroundTask Read callback fail.");
212 return ERR_BGTASK_PARCELABLE_FAILED;
213 }
214
215 ErrCode result = SubscribeBackgroundTask(iface_cast<IBackgroundTaskSubscriber>(subscriber));
216 if (!reply.WriteInt32(result)) {
217 BGTASK_LOGE("HandleSubscribeBackgroundTask Write result failed, ErrCode=%{public}d", result);
218 return ERR_BGTASK_PARCELABLE_FAILED;
219 }
220 return ERR_OK;
221 }
222
HandleUnsubscribeBackgroundTask(MessageParcel & data,MessageParcel & reply)223 ErrCode BackgroundTaskMgrStub::HandleUnsubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)
224 {
225 sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
226 if (subscriber == nullptr) {
227 BGTASK_LOGE("HandleUnsubscribeBackgroundTask Read callback fail.");
228 return ERR_BGTASK_PARCELABLE_FAILED;
229 }
230
231 ErrCode result = UnsubscribeBackgroundTask(iface_cast<IBackgroundTaskSubscriber>(subscriber));
232 if (!reply.WriteInt32(result)) {
233 BGTASK_LOGE("HandleUnsubscribeBackgroundTask Write result failed, ErrCode=%{public}d", result);
234 return ERR_BGTASK_PARCELABLE_FAILED;
235 }
236 return ERR_OK;
237 }
238
HandleGetTransientTaskApps(MessageParcel & data,MessageParcel & reply)239 ErrCode BackgroundTaskMgrStub::HandleGetTransientTaskApps(MessageParcel& data, MessageParcel& reply)
240 {
241 std::vector<std::shared_ptr<TransientTaskAppInfo>> appinfos;
242 ErrCode result = GetTransientTaskApps(appinfos);
243 if (!reply.WriteInt32(result)) {
244 return ERR_BGTASK_PARCELABLE_FAILED;
245 }
246 reply.WriteInt32(appinfos.size());
247 for (auto &info : appinfos) {
248 if (info == nullptr) {
249 return ERR_BGTASK_INVALID_PARAM;
250 }
251 if (!info->Marshalling(reply)) {
252 return ERR_BGTASK_PARCELABLE_FAILED;
253 }
254 }
255 return ERR_OK;
256 }
257
HandleGetContinuousTaskApps(MessageParcel & data,MessageParcel & reply)258 ErrCode BackgroundTaskMgrStub::HandleGetContinuousTaskApps(MessageParcel& data, MessageParcel& reply)
259 {
260 std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> appInfos;
261 ErrCode result = GetContinuousTaskApps(appInfos);
262 if (!reply.WriteInt32(result)) {
263 return ERR_BGTASK_PARCELABLE_FAILED;
264 }
265 reply.WriteInt32(appInfos.size());
266 for (auto &info : appInfos) {
267 if (info == nullptr) {
268 return ERR_BGTASK_INVALID_PARAM;
269 }
270 if (!info->Marshalling(reply)) {
271 return ERR_BGTASK_PARCELABLE_FAILED;
272 }
273 }
274 return ERR_OK;
275 }
276
HandleApplyEfficiencyResources(MessageParcel & data,MessageParcel & reply)277 ErrCode BackgroundTaskMgrStub::HandleApplyEfficiencyResources(MessageParcel& data, MessageParcel& reply)
278 {
279 BGTASK_LOGD("start receive data in apply res function from bgtask proxy");
280 sptr<EfficiencyResourceInfo> resourceInfoPtr = data.ReadParcelable<EfficiencyResourceInfo>();
281 if (resourceInfoPtr == nullptr) {
282 BGTASK_LOGE("EfficiencyResourceInfo ReadParcelable failed");
283 return ERR_BGTASK_PARCELABLE_FAILED;
284 }
285 ErrCode result = ApplyEfficiencyResources(resourceInfoPtr);
286 if (!reply.WriteInt32(result)) {
287 BGTASK_LOGE("HandleApplyEfficiencyResources write result failed, ErrCode=%{public}d", result);
288 return ERR_BGTASK_PARCELABLE_FAILED;
289 }
290 return ERR_OK;
291 }
292
HandleResetAllEfficiencyResources(MessageParcel & data,MessageParcel & reply)293 ErrCode BackgroundTaskMgrStub::HandleResetAllEfficiencyResources(MessageParcel& data, MessageParcel& reply)
294 {
295 BGTASK_LOGD("start receive data in reset all res function from bgtask proxy");
296 ErrCode result = ResetAllEfficiencyResources();
297 if (!reply.WriteInt32(result)) {
298 BGTASK_LOGE("HandleResetAllEfficiencyResources Write result failed, ErrCode=%{public}d", result);
299 return ERR_BGTASK_PARCELABLE_FAILED;
300 }
301 return ERR_OK;
302 }
303
HandleGetEfficiencyResourcesInfos(MessageParcel & data,MessageParcel & reply)304 ErrCode BackgroundTaskMgrStub::HandleGetEfficiencyResourcesInfos(MessageParcel& data, MessageParcel& reply)
305 {
306 std::vector<std::shared_ptr<ResourceCallbackInfo>> appInfos;
307 std::vector<std::shared_ptr<ResourceCallbackInfo>> procInfos;
308 ErrCode result = GetEfficiencyResourcesInfos(appInfos, procInfos);
309 if (!reply.WriteInt32(result)) {
310 BGTASK_LOGE("HandleGetEfficiencyResourcesInfos write result failed, ErrCode=%{public}d", result);
311 return ERR_BGTASK_PARCELABLE_FAILED;
312 }
313 if (WriteInfoToParcel(appInfos, reply) != ERR_OK
314 || WriteInfoToParcel(procInfos, reply) != ERR_OK) {
315 BGTASK_LOGE("HandleGetEfficiencyResourcesInfos write result failed");
316 return ERR_BGTASK_PARCELABLE_FAILED;
317 }
318 return ERR_OK;
319 }
320
WriteInfoToParcel(const std::vector<std::shared_ptr<ResourceCallbackInfo>> & infoMap,MessageParcel & reply)321 ErrCode BackgroundTaskMgrStub::WriteInfoToParcel(
322 const std::vector<std::shared_ptr<ResourceCallbackInfo>>& infoMap, MessageParcel& reply)
323 {
324 reply.WriteInt32(infoMap.size());
325 for (auto &info : infoMap) {
326 if (info == nullptr) {
327 return ERR_BGTASK_INVALID_PARAM;
328 }
329 if (!info->Marshalling(reply)) {
330 return ERR_BGTASK_PARCELABLE_FAILED;
331 }
332 }
333 return ERR_OK;
334 }
335
HandleStopContinuousTask(MessageParcel & data,MessageParcel & reply)336 ErrCode BackgroundTaskMgrStub::HandleStopContinuousTask(MessageParcel& data, MessageParcel& reply)
337 {
338 int32_t uid = data.ReadInt32();
339 int32_t pid = data.ReadInt32();
340 uint32_t taskType = data.ReadUint32();
341 ErrCode result = StopContinuousTask(uid, pid, taskType);
342 if (!reply.WriteInt32(result)) {
343 BGTASK_LOGE("HandleStopContinuousTask write result failed, ErrCode=%{public}d", result);
344 return ERR_BGTASK_PARCELABLE_FAILED;
345 }
346 return ERR_OK;
347 }
348 } // namespace BackgroundTaskMgr
349 } // namespace OHOS