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 
HandleContinuousTask(uint32_t code, MessageParcel& data, MessageParcel& reply)45 void BackgroundTaskMgrStub::HandleContinuousTask(uint32_t code, MessageParcel& data, MessageParcel& reply)
46 {
47     switch (code) {
48         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::START_BACKGROUND_RUNNING):
49             HandleStartBackgroundRunning(data, reply);
50             break;
51         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::UPDATE_BACKGROUND_RUNNING):
52             HandleUpdateBackgroundRunning(data, reply);
53             break;
54         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::STOP_BACKGROUND_RUNNING):
55             HandleStopBackgroundRunning(data, reply);
56             break;
57         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::GET_CONTINUOUS_TASK_APPS):
58             HandleGetContinuousTaskApps(data, reply);
59             break;
60         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::STOP_CONTINUOUS_TASK):
61             HandleStopContinuousTask(data, reply);
62             break;
63         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::REQUEST_BACKGROUND_RUNNING_FOR_INNER):
64             HandleBackgroundRunningForInner(data, reply);
65             break;
66         default:
67             BGTASK_LOGE("code is error");
68     }
69 }
70 
HandleTransientTask(uint32_t code, MessageParcel& data, MessageParcel& reply)71 void BackgroundTaskMgrStub::HandleTransientTask(uint32_t code, MessageParcel& data, MessageParcel& reply)
72 {
73     switch (code) {
74         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::REQUEST_SUSPEND_DELAY):
75             HandleRequestSuspendDelay(data, reply);
76             break;
77         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::CANCEL_SUSPEND_DELAY):
78             HandleCancelSuspendDelay(data, reply);
79             break;
80         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::GET_REMAINING_DELAY_TIME):
81             HandleGetRemainingDelayTime(data, reply);
82             break;
83         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::GET_TRANSIENT_TASK_APPS):
84             HandleGetTransientTaskApps(data, reply);
85             break;
86         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::PAUSE_TRANSIENT_TASK_TIME_FOR_INNER):
87             HandlePauseTransientTaskTimeForInner(data, reply);
88             break;
89         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::START_TRANSIENT_TASK_TIME_FOR_INNER):
90             HandleStartTransientTaskTimeForInner(data, reply);
91             break;
92         default:
93             BGTASK_LOGE("code is error");
94     }
95 }
96 
HandleOnRemoteResquestFunc(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)97 ErrCode BackgroundTaskMgrStub::HandleOnRemoteResquestFunc(uint32_t code,
98     MessageParcel& data, MessageParcel& reply, MessageOption& option)
99 {
100     switch (code) {
101         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::REQUEST_SUSPEND_DELAY):
102         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::CANCEL_SUSPEND_DELAY):
103         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::GET_REMAINING_DELAY_TIME):
104         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::GET_TRANSIENT_TASK_APPS):
105         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::PAUSE_TRANSIENT_TASK_TIME_FOR_INNER):
106             [[fallthrough]];
107         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::START_TRANSIENT_TASK_TIME_FOR_INNER):
108             HandleTransientTask(code, data, reply);
109             break;
110         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::START_BACKGROUND_RUNNING):
111         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::UPDATE_BACKGROUND_RUNNING):
112         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::STOP_BACKGROUND_RUNNING):
113         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::GET_CONTINUOUS_TASK_APPS):
114         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::STOP_CONTINUOUS_TASK):
115             [[fallthrough]];
116         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::REQUEST_BACKGROUND_RUNNING_FOR_INNER):
117             HandleContinuousTask(code, data, reply);
118             break;
119         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::SUBSCRIBE_BACKGROUND_TASK):
120             HandleSubscribeBackgroundTask(data, reply);
121             break;
122         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::UNSUBSCRIBE_BACKGROUND_TASK):
123             HandleUnsubscribeBackgroundTask(data, reply);
124             break;
125         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::APPLY_EFFICIENCY_RESOURCES):
126             HandleApplyEfficiencyResources(data, reply);
127             break;
128         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::RESET_ALL_EFFICIENCY_RESOURCES):
129             HandleResetAllEfficiencyResources(data, reply);
130             break;
131         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::GET_EFFICIENCY_RESOURCES_INFOS):
132             HandleGetEfficiencyResourcesInfos(data, reply);
133             break;
134         case static_cast<uint32_t>(BackgroundTaskMgrStubInterfaceCode::SET_BGTASK_CONFIG):
135             HandleSetBgTaskConfig(data, reply);
136             break;
137         default:
138             BGTASK_LOGE("BackgroundTaskMgrStub: code is not match");
139             return IRemoteStub::OnRemoteRequest(code, data, reply, option);
140     }
141     return ERR_OK;
142 }
143 
HandleRequestSuspendDelay(MessageParcel& data, MessageParcel& reply)144 ErrCode BackgroundTaskMgrStub::HandleRequestSuspendDelay(MessageParcel& data, MessageParcel& reply)
145 {
146     std::u16string reason = data.ReadString16();
147     sptr<IRemoteObject> callback = data.ReadRemoteObject();
148     if (callback == nullptr) {
149         BGTASK_LOGE("HandleRequestSuspendDelay Read callback fail.");
150         return ERR_BGTASK_PARCELABLE_FAILED;
151     }
152 
153     std::shared_ptr<DelaySuspendInfo> info;
154     ErrCode result = RequestSuspendDelay(reason, iface_cast<IExpiredCallback>(callback), info);
155     if (!reply.WriteInt32(result)) {
156         BGTASK_LOGE("HandleRequestSuspendDelay Write result failed, ErrCode=%{public}d", result);
157         return ERR_BGTASK_PARCELABLE_FAILED;
158     }
159 
160     if (info == nullptr || !info->Marshalling(reply)) {
161         BGTASK_LOGE("HandleRequestSuspendDelay Write result fail.");
162         return ERR_BGTASK_PARCELABLE_FAILED;
163     }
164     return ERR_OK;
165 }
166 
HandleCancelSuspendDelay(MessageParcel& data, MessageParcel& reply)167 ErrCode BackgroundTaskMgrStub::HandleCancelSuspendDelay(MessageParcel& data, MessageParcel& reply)
168 {
169     int32_t id = data.ReadInt32();
170     ErrCode result = CancelSuspendDelay(id);
171     if (!reply.WriteInt32(result)) {
172         BGTASK_LOGE("HandleCancelSuspendDelay Write result failed, ErrCode=%{public}d", result);
173         return ERR_BGTASK_PARCELABLE_FAILED;
174     }
175     return ERR_OK;
176 }
177 
HandleGetRemainingDelayTime(MessageParcel& data, MessageParcel& reply)178 ErrCode BackgroundTaskMgrStub::HandleGetRemainingDelayTime(MessageParcel& data, MessageParcel& reply)
179 {
180     int32_t id = data.ReadInt32();
181     int32_t time = 0;
182     ErrCode result =  GetRemainingDelayTime(id, time);
183     if (!reply.WriteInt32(result)) {
184         BGTASK_LOGE("HandleGetRemainingDelayTime Write result failed, ErrCode=%{public}d", result);
185         return ERR_BGTASK_PARCELABLE_FAILED;
186     }
187     if (!reply.WriteInt32(time)) {
188         BGTASK_LOGE("HandleGetRemainingDelayTime Write result fail.");
189         return ERR_BGTASK_PARCELABLE_FAILED;
190     }
191     return ERR_OK;
192 }
193 
HandleBackgroundRunningForInner(MessageParcel &data, MessageParcel &reply)194 ErrCode BackgroundTaskMgrStub::HandleBackgroundRunningForInner(MessageParcel &data, MessageParcel &reply)
195 {
196     StartTrace(HITRACE_TAG_OHOS, "BackgroundTaskMgrStub::HandleBackgroundRunningForInner");
197     sptr<ContinuousTaskParamForInner> taskParam = data.ReadParcelable<ContinuousTaskParamForInner>();
198     if (taskParam == nullptr) {
199         BGTASK_LOGE("ContinuousTaskParamForInner ReadParcelable failed");
200         FinishTrace(HITRACE_TAG_OHOS);
201         return ERR_BGTASK_PARCELABLE_FAILED;
202     }
203 
204     ErrCode result = RequestBackgroundRunningForInner(taskParam);
205     if (!reply.WriteInt32(result)) {
206         BGTASK_LOGE("HandleBackgroundRunningForInner write result failed, ErrCode=%{public}d", result);
207         FinishTrace(HITRACE_TAG_OHOS);
208         return ERR_BGTASK_PARCELABLE_FAILED;
209     }
210 
211     FinishTrace(HITRACE_TAG_OHOS);
212     return ERR_OK;
213 }
214 
HandleStartBackgroundRunning(MessageParcel &data, MessageParcel &reply)215 ErrCode BackgroundTaskMgrStub::HandleStartBackgroundRunning(MessageParcel &data, MessageParcel &reply)
216 {
217     StartTrace(HITRACE_TAG_OHOS, "BackgroundTaskMgrStub::HandleStartBackgroundRunning");
218     sptr<ContinuousTaskParam> taskParam = data.ReadParcelable<ContinuousTaskParam>();
219     if (taskParam == nullptr) {
220         BGTASK_LOGE("ContinuousTaskParam ReadParcelable failed");
221         FinishTrace(HITRACE_TAG_OHOS);
222         return ERR_BGTASK_PARCELABLE_FAILED;
223     }
224     BGTASK_LOGI("HandleStartBackgroundRunning start");
225     ErrCode result = StartBackgroundRunning(taskParam);
226     if (!reply.WriteInt32(result)) {
227         BGTASK_LOGE("HandleStartBackgroundRunning write result failed, ErrCode=%{public}d", result);
228         FinishTrace(HITRACE_TAG_OHOS);
229         return ERR_BGTASK_PARCELABLE_FAILED;
230     }
231     if (!reply.WriteInt32(taskParam->notificationId_)) {
232         BGTASK_LOGE("HandleStartBackgroundRunning write notificatinId failed");
233         FinishTrace(HITRACE_TAG_OHOS);
234         return ERR_BGTASK_PARCELABLE_FAILED;
235     }
236     BGTASK_LOGI("write notificationId %{public}d", taskParam->notificationId_);
237     FinishTrace(HITRACE_TAG_OHOS);
238     return ERR_OK;
239 }
240 
HandleUpdateBackgroundRunning(MessageParcel &data, MessageParcel &reply)241 ErrCode BackgroundTaskMgrStub::HandleUpdateBackgroundRunning(MessageParcel &data, MessageParcel &reply)
242 {
243     StartTrace(HITRACE_TAG_OHOS, "BackgroundTaskMgrStub::HandleUpdateBackgroundRunning");
244     sptr<ContinuousTaskParam> taskParam = data.ReadParcelable<ContinuousTaskParam>();
245     if (taskParam == nullptr) {
246         BGTASK_LOGE("ContinuousTaskParam ReadParcelable failed");
247         FinishTrace(HITRACE_TAG_OHOS);
248         return ERR_BGTASK_PARCELABLE_FAILED;
249     }
250     BGTASK_LOGI("HandleUpdateBackgroundRunning start");
251     ErrCode result = UpdateBackgroundRunning(taskParam);
252     if (!reply.WriteInt32(result)) {
253         BGTASK_LOGE("HandleUpdateBackgroundRunning write result failed, ErrCode=%{public}d", result);
254         FinishTrace(HITRACE_TAG_OHOS);
255         return ERR_BGTASK_PARCELABLE_FAILED;
256     }
257     if (!reply.WriteInt32(taskParam->notificationId_)) {
258         BGTASK_LOGE("HandleUpdateBackgroundRunning write notificatinId failed");
259         FinishTrace(HITRACE_TAG_OHOS);
260         return ERR_BGTASK_PARCELABLE_FAILED;
261     }
262     BGTASK_LOGI("write notificationId %{public}d", taskParam->notificationId_);
263     FinishTrace(HITRACE_TAG_OHOS);
264     return ERR_OK;
265 }
266 
HandleStopBackgroundRunning(MessageParcel &data, MessageParcel &reply)267 ErrCode BackgroundTaskMgrStub::HandleStopBackgroundRunning(MessageParcel &data, MessageParcel &reply)
268 {
269     StartTrace(HITRACE_TAG_OHOS, "BackgroundTaskMgrStub::HandleStopBackgroundRunning");
270     std::u16string u16AbilityName;
271     if (!data.ReadString16(u16AbilityName)) {
272         FinishTrace(HITRACE_TAG_OHOS);
273         return ERR_BGTASK_PARCELABLE_FAILED;
274     }
275     std::string abilityName = Str16ToStr8(u16AbilityName);
276     sptr<IRemoteObject> abilityToken = data.ReadRemoteObject();
277     int32_t abilityId = data.ReadInt32();
278     ErrCode result = StopBackgroundRunning(abilityName, abilityToken, abilityId);
279     if (!reply.WriteInt32(result)) {
280         BGTASK_LOGE("HandleStopBackgroundRunning write result failed, ErrCode=%{public}d", result);
281         FinishTrace(HITRACE_TAG_OHOS);
282         return ERR_BGTASK_PARCELABLE_FAILED;
283     }
284     FinishTrace(HITRACE_TAG_OHOS);
285     return ERR_OK;
286 }
287 
HandleSubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)288 ErrCode BackgroundTaskMgrStub::HandleSubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)
289 {
290     sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
291     if (subscriber == nullptr) {
292         BGTASK_LOGE("HandleSubscribeBackgroundTask Read callback fail.");
293         return ERR_BGTASK_PARCELABLE_FAILED;
294     }
295 
296     ErrCode result = SubscribeBackgroundTask(iface_cast<IBackgroundTaskSubscriber>(subscriber));
297     if (!reply.WriteInt32(result)) {
298         BGTASK_LOGE("HandleSubscribeBackgroundTask Write result failed, ErrCode=%{public}d", result);
299         return ERR_BGTASK_PARCELABLE_FAILED;
300     }
301     return ERR_OK;
302 }
303 
HandleUnsubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)304 ErrCode BackgroundTaskMgrStub::HandleUnsubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)
305 {
306     sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
307     if (subscriber == nullptr) {
308         BGTASK_LOGE("HandleUnsubscribeBackgroundTask Read callback fail.");
309         return ERR_BGTASK_PARCELABLE_FAILED;
310     }
311 
312     ErrCode result = UnsubscribeBackgroundTask(iface_cast<IBackgroundTaskSubscriber>(subscriber));
313     if (!reply.WriteInt32(result)) {
314         BGTASK_LOGE("HandleUnsubscribeBackgroundTask Write result failed, ErrCode=%{public}d", result);
315         return ERR_BGTASK_PARCELABLE_FAILED;
316     }
317     return ERR_OK;
318 }
319 
HandleGetTransientTaskApps(MessageParcel& data, MessageParcel& reply)320 ErrCode BackgroundTaskMgrStub::HandleGetTransientTaskApps(MessageParcel& data, MessageParcel& reply)
321 {
322     std::vector<std::shared_ptr<TransientTaskAppInfo>> appinfos;
323     ErrCode result = GetTransientTaskApps(appinfos);
324     if (!reply.WriteInt32(result)) {
325         return ERR_BGTASK_PARCELABLE_FAILED;
326     }
327     reply.WriteInt32(appinfos.size());
328     for (auto &info : appinfos) {
329         if (info == nullptr) {
330             return ERR_BGTASK_INVALID_PARAM;
331         }
332         if (!info->Marshalling(reply)) {
333             return ERR_BGTASK_PARCELABLE_FAILED;
334         }
335     }
336     return ERR_OK;
337 }
338 
HandlePauseTransientTaskTimeForInner(MessageParcel& data, MessageParcel& reply)339 ErrCode BackgroundTaskMgrStub::HandlePauseTransientTaskTimeForInner(MessageParcel& data, MessageParcel& reply)
340 {
341     int32_t uid = data.ReadInt32();
342     ErrCode result = PauseTransientTaskTimeForInner(uid);
343     if (!reply.WriteInt32(result)) {
344         BGTASK_LOGE("HandlePauseTransientTaskTimeForInner write result failed, ErrCode=%{public}d", result);
345         return ERR_BGTASK_PARCELABLE_FAILED;
346     }
347     return ERR_OK;
348 }
349 
HandleStartTransientTaskTimeForInner(MessageParcel& data, MessageParcel& reply)350 ErrCode BackgroundTaskMgrStub::HandleStartTransientTaskTimeForInner(MessageParcel& data, MessageParcel& reply)
351 {
352     int32_t uid = data.ReadInt32();
353     ErrCode result = StartTransientTaskTimeForInner(uid);
354     if (!reply.WriteInt32(result)) {
355         BGTASK_LOGE("HandleStartTransientTaskTimeForInner write result failed, ErrCode=%{public}d", result);
356         return ERR_BGTASK_PARCELABLE_FAILED;
357     }
358     return ERR_OK;
359 }
360 
HandleGetContinuousTaskApps(MessageParcel& data, MessageParcel& reply)361 ErrCode BackgroundTaskMgrStub::HandleGetContinuousTaskApps(MessageParcel& data, MessageParcel& reply)
362 {
363     std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> appInfos;
364     ErrCode result = GetContinuousTaskApps(appInfos);
365     if (!reply.WriteInt32(result)) {
366         return ERR_BGTASK_PARCELABLE_FAILED;
367     }
368     reply.WriteInt32(appInfos.size());
369     for (auto &info : appInfos) {
370         if (info == nullptr) {
371             return ERR_BGTASK_INVALID_PARAM;
372         }
373         if (!info->Marshalling(reply)) {
374             return ERR_BGTASK_PARCELABLE_FAILED;
375         }
376     }
377     return ERR_OK;
378 }
379 
HandleApplyEfficiencyResources(MessageParcel& data, MessageParcel& reply)380 ErrCode BackgroundTaskMgrStub::HandleApplyEfficiencyResources(MessageParcel& data, MessageParcel& reply)
381 {
382     BGTASK_LOGD("start receive data in apply res function from bgtask proxy");
383     sptr<EfficiencyResourceInfo> resourceInfoPtr = data.ReadParcelable<EfficiencyResourceInfo>();
384     if (resourceInfoPtr == nullptr) {
385         BGTASK_LOGE("EfficiencyResourceInfo ReadParcelable failed");
386         return ERR_BGTASK_PARCELABLE_FAILED;
387     }
388     ErrCode result = ApplyEfficiencyResources(resourceInfoPtr);
389     if (!reply.WriteInt32(result)) {
390         BGTASK_LOGE("HandleApplyEfficiencyResources write result failed, ErrCode=%{public}d", result);
391         return ERR_BGTASK_PARCELABLE_FAILED;
392     }
393     return ERR_OK;
394 }
395 
HandleResetAllEfficiencyResources(MessageParcel& data, MessageParcel& reply)396 ErrCode BackgroundTaskMgrStub::HandleResetAllEfficiencyResources(MessageParcel& data, MessageParcel& reply)
397 {
398     BGTASK_LOGD("start receive data in reset all res function from bgtask proxy");
399     ErrCode result = ResetAllEfficiencyResources();
400     if (!reply.WriteInt32(result)) {
401         BGTASK_LOGE("HandleResetAllEfficiencyResources Write result failed, ErrCode=%{public}d", result);
402         return ERR_BGTASK_PARCELABLE_FAILED;
403     }
404     return ERR_OK;
405 }
406 
HandleGetEfficiencyResourcesInfos(MessageParcel& data, MessageParcel& reply)407 ErrCode BackgroundTaskMgrStub::HandleGetEfficiencyResourcesInfos(MessageParcel& data, MessageParcel& reply)
408 {
409     std::vector<std::shared_ptr<ResourceCallbackInfo>> appInfos;
410     std::vector<std::shared_ptr<ResourceCallbackInfo>> procInfos;
411     ErrCode result = GetEfficiencyResourcesInfos(appInfos, procInfos);
412     if (!reply.WriteInt32(result)) {
413         BGTASK_LOGE("HandleGetEfficiencyResourcesInfos write result failed, ErrCode=%{public}d", result);
414         return ERR_BGTASK_PARCELABLE_FAILED;
415     }
416     if (WriteInfoToParcel(appInfos, reply) != ERR_OK
417         || WriteInfoToParcel(procInfos, reply) != ERR_OK) {
418         BGTASK_LOGE("HandleGetEfficiencyResourcesInfos write result failed");
419         return ERR_BGTASK_PARCELABLE_FAILED;
420     }
421     return ERR_OK;
422 }
423 
WriteInfoToParcel( const std::vector<std::shared_ptr<ResourceCallbackInfo>>& infoMap, MessageParcel& reply)424 ErrCode BackgroundTaskMgrStub::WriteInfoToParcel(
425     const std::vector<std::shared_ptr<ResourceCallbackInfo>>& infoMap, MessageParcel& reply)
426 {
427     reply.WriteInt32(infoMap.size());
428     for (auto &info : infoMap) {
429         if (info == nullptr) {
430             return ERR_BGTASK_INVALID_PARAM;
431         }
432         if (!info->Marshalling(reply)) {
433             return ERR_BGTASK_PARCELABLE_FAILED;
434         }
435     }
436     return ERR_OK;
437 }
438 
HandleStopContinuousTask(MessageParcel& data, MessageParcel& reply)439 ErrCode BackgroundTaskMgrStub::HandleStopContinuousTask(MessageParcel& data, MessageParcel& reply)
440 {
441     int32_t uid = data.ReadInt32();
442     int32_t pid = data.ReadInt32();
443     uint32_t taskType = data.ReadUint32();
444     std::string key = data.ReadString();
445     ErrCode result = StopContinuousTask(uid, pid, taskType, key);
446     if (!reply.WriteInt32(result)) {
447         BGTASK_LOGE("HandleStopContinuousTask write result failed, ErrCode=%{public}d", result);
448         return ERR_BGTASK_PARCELABLE_FAILED;
449     }
450     return ERR_OK;
451 }
452 
HandleSetBgTaskConfig(MessageParcel& data, MessageParcel& reply)453 ErrCode BackgroundTaskMgrStub::HandleSetBgTaskConfig(MessageParcel& data, MessageParcel& reply)
454 {
455     std::string configData;
456     if (!data.ReadString(configData)) {
457         BGTASK_LOGE("read parce configData error");
458         return ERR_BGTASK_PARCELABLE_FAILED;
459     }
460     int32_t sourceType;
461     if (!data.ReadInt32(sourceType)) {
462         BGTASK_LOGE("read parce sourceType error");
463         return ERR_BGTASK_PARCELABLE_FAILED;
464     }
465     ErrCode result = SetBgTaskConfig(configData, sourceType);
466     if (!reply.WriteInt32(result)) {
467         BGTASK_LOGE("HandleSetBgTaskConfig write result failed, ErrCode=%{public}d", result);
468         return ERR_BGTASK_PARCELABLE_FAILED;
469     }
470     return ERR_OK;
471 }
472 }  // namespace BackgroundTaskMgr
473 }  // namespace OHOS