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     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
147         "BackgroundTaskManager::TransientTask::Stub::HandleRequestSuspendDelay");
148 
149     std::u16string reason = data.ReadString16();
150     sptr<IRemoteObject> callback = data.ReadRemoteObject();
151     if (callback == nullptr) {
152         BGTASK_LOGE("HandleRequestSuspendDelay Read callback fail.");
153         return ERR_BGTASK_PARCELABLE_FAILED;
154     }
155 
156     std::shared_ptr<DelaySuspendInfo> info;
157     ErrCode result = RequestSuspendDelay(reason, iface_cast<IExpiredCallback>(callback), info);
158     if (!reply.WriteInt32(result)) {
159         BGTASK_LOGE("HandleRequestSuspendDelay Write result failed, ErrCode=%{public}d", result);
160         return ERR_BGTASK_PARCELABLE_FAILED;
161     }
162 
163     if (info == nullptr || !info->Marshalling(reply)) {
164         BGTASK_LOGE("HandleRequestSuspendDelay Write result fail.");
165         return ERR_BGTASK_PARCELABLE_FAILED;
166     }
167     return ERR_OK;
168 }
169 
HandleCancelSuspendDelay(MessageParcel & data,MessageParcel & reply)170 ErrCode BackgroundTaskMgrStub::HandleCancelSuspendDelay(MessageParcel& data, MessageParcel& reply)
171 {
172     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
173         "BackgroundTaskManager::TransientTask::Stub::HandleCancelSuspendDelay");
174 
175     int32_t id = data.ReadInt32();
176     ErrCode result = CancelSuspendDelay(id);
177     if (!reply.WriteInt32(result)) {
178         BGTASK_LOGE("HandleCancelSuspendDelay Write result failed, ErrCode=%{public}d", result);
179         return ERR_BGTASK_PARCELABLE_FAILED;
180     }
181     return ERR_OK;
182 }
183 
HandleGetRemainingDelayTime(MessageParcel & data,MessageParcel & reply)184 ErrCode BackgroundTaskMgrStub::HandleGetRemainingDelayTime(MessageParcel& data, MessageParcel& reply)
185 {
186     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
187         "BackgroundTaskManager::TransientTask::Stub::HandleGetRemainingDelayTime");
188 
189     int32_t id = data.ReadInt32();
190     int32_t time = 0;
191     ErrCode result =  GetRemainingDelayTime(id, time);
192     if (!reply.WriteInt32(result)) {
193         BGTASK_LOGE("HandleGetRemainingDelayTime Write result failed, ErrCode=%{public}d", result);
194         return ERR_BGTASK_PARCELABLE_FAILED;
195     }
196     if (!reply.WriteInt32(time)) {
197         BGTASK_LOGE("HandleGetRemainingDelayTime Write result fail.");
198         return ERR_BGTASK_PARCELABLE_FAILED;
199     }
200     return ERR_OK;
201 }
202 
HandleBackgroundRunningForInner(MessageParcel & data,MessageParcel & reply)203 ErrCode BackgroundTaskMgrStub::HandleBackgroundRunningForInner(MessageParcel &data, MessageParcel &reply)
204 {
205     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
206         "BackgroundTaskManager::ContinuousTask::Stub::HandleBackgroundRunningForInner");
207 
208     sptr<ContinuousTaskParamForInner> taskParam = data.ReadParcelable<ContinuousTaskParamForInner>();
209     if (taskParam == nullptr) {
210         BGTASK_LOGE("ContinuousTaskParamForInner ReadParcelable failed");
211         return ERR_BGTASK_PARCELABLE_FAILED;
212     }
213 
214     ErrCode result = RequestBackgroundRunningForInner(taskParam);
215     if (!reply.WriteInt32(result)) {
216         BGTASK_LOGE("HandleBackgroundRunningForInner write result failed, ErrCode=%{public}d", result);
217         return ERR_BGTASK_PARCELABLE_FAILED;
218     }
219 
220     return ERR_OK;
221 }
222 
HandleStartBackgroundRunning(MessageParcel & data,MessageParcel & reply)223 ErrCode BackgroundTaskMgrStub::HandleStartBackgroundRunning(MessageParcel &data, MessageParcel &reply)
224 {
225     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
226         "BackgroundTaskManager::ContinuousTask::Stub::HandleStartBackgroundRunning");
227 
228     sptr<ContinuousTaskParam> taskParam = data.ReadParcelable<ContinuousTaskParam>();
229     if (taskParam == nullptr) {
230         BGTASK_LOGE("ContinuousTaskParam ReadParcelable failed");
231         return ERR_BGTASK_PARCELABLE_FAILED;
232     }
233     BGTASK_LOGI("HandleStartBackgroundRunning start");
234     ErrCode result = StartBackgroundRunning(taskParam);
235     if (!reply.WriteInt32(result)) {
236         BGTASK_LOGE("HandleStartBackgroundRunning write result failed, ErrCode=%{public}d", result);
237         return ERR_BGTASK_PARCELABLE_FAILED;
238     }
239     if (!reply.WriteInt32(taskParam->notificationId_)) {
240         BGTASK_LOGE("HandleStartBackgroundRunning write notificatinId failed");
241         return ERR_BGTASK_PARCELABLE_FAILED;
242     }
243     BGTASK_LOGI("write notificationId %{public}d", taskParam->notificationId_);
244     return ERR_OK;
245 }
246 
HandleUpdateBackgroundRunning(MessageParcel & data,MessageParcel & reply)247 ErrCode BackgroundTaskMgrStub::HandleUpdateBackgroundRunning(MessageParcel &data, MessageParcel &reply)
248 {
249     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
250         "BackgroundTaskManager::ContinuousTask::Stub::HandleUpdateBackgroundRunning");
251 
252     sptr<ContinuousTaskParam> taskParam = data.ReadParcelable<ContinuousTaskParam>();
253     if (taskParam == nullptr) {
254         BGTASK_LOGE("ContinuousTaskParam ReadParcelable failed");
255         return ERR_BGTASK_PARCELABLE_FAILED;
256     }
257     BGTASK_LOGI("HandleUpdateBackgroundRunning start");
258     ErrCode result = UpdateBackgroundRunning(taskParam);
259     if (!reply.WriteInt32(result)) {
260         BGTASK_LOGE("HandleUpdateBackgroundRunning write result failed, ErrCode=%{public}d", result);
261         return ERR_BGTASK_PARCELABLE_FAILED;
262     }
263     if (!reply.WriteInt32(taskParam->notificationId_)) {
264         BGTASK_LOGE("HandleUpdateBackgroundRunning write notificatinId failed");
265         return ERR_BGTASK_PARCELABLE_FAILED;
266     }
267     BGTASK_LOGI("write notificationId %{public}d", taskParam->notificationId_);
268     return ERR_OK;
269 }
270 
HandleStopBackgroundRunning(MessageParcel & data,MessageParcel & reply)271 ErrCode BackgroundTaskMgrStub::HandleStopBackgroundRunning(MessageParcel &data, MessageParcel &reply)
272 {
273     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
274         "BackgroundTaskManager::ContinuousTask::Stub::HandleStopBackgroundRunning");
275 
276     std::u16string u16AbilityName;
277     if (!data.ReadString16(u16AbilityName)) {
278         return ERR_BGTASK_PARCELABLE_FAILED;
279     }
280     std::string abilityName = Str16ToStr8(u16AbilityName);
281     sptr<IRemoteObject> abilityToken = data.ReadRemoteObject();
282     int32_t abilityId = data.ReadInt32();
283     ErrCode result = StopBackgroundRunning(abilityName, abilityToken, abilityId);
284     if (!reply.WriteInt32(result)) {
285         BGTASK_LOGE("HandleStopBackgroundRunning write result failed, ErrCode=%{public}d", result);
286         return ERR_BGTASK_PARCELABLE_FAILED;
287     }
288     return ERR_OK;
289 }
290 
HandleSubscribeBackgroundTask(MessageParcel & data,MessageParcel & reply)291 ErrCode BackgroundTaskMgrStub::HandleSubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)
292 {
293     sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
294     if (subscriber == nullptr) {
295         BGTASK_LOGE("HandleSubscribeBackgroundTask Read callback fail.");
296         return ERR_BGTASK_PARCELABLE_FAILED;
297     }
298 
299     ErrCode result = SubscribeBackgroundTask(iface_cast<IBackgroundTaskSubscriber>(subscriber));
300     if (!reply.WriteInt32(result)) {
301         BGTASK_LOGE("HandleSubscribeBackgroundTask Write result failed, ErrCode=%{public}d", result);
302         return ERR_BGTASK_PARCELABLE_FAILED;
303     }
304     return ERR_OK;
305 }
306 
HandleUnsubscribeBackgroundTask(MessageParcel & data,MessageParcel & reply)307 ErrCode BackgroundTaskMgrStub::HandleUnsubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)
308 {
309     sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
310     if (subscriber == nullptr) {
311         BGTASK_LOGE("HandleUnsubscribeBackgroundTask Read callback fail.");
312         return ERR_BGTASK_PARCELABLE_FAILED;
313     }
314 
315     ErrCode result = UnsubscribeBackgroundTask(iface_cast<IBackgroundTaskSubscriber>(subscriber));
316     if (!reply.WriteInt32(result)) {
317         BGTASK_LOGE("HandleUnsubscribeBackgroundTask Write result failed, ErrCode=%{public}d", result);
318         return ERR_BGTASK_PARCELABLE_FAILED;
319     }
320     return ERR_OK;
321 }
322 
HandleGetTransientTaskApps(MessageParcel & data,MessageParcel & reply)323 ErrCode BackgroundTaskMgrStub::HandleGetTransientTaskApps(MessageParcel& data, MessageParcel& reply)
324 {
325     std::vector<std::shared_ptr<TransientTaskAppInfo>> appinfos;
326     ErrCode result = GetTransientTaskApps(appinfos);
327     if (!reply.WriteInt32(result)) {
328         return ERR_BGTASK_PARCELABLE_FAILED;
329     }
330     reply.WriteInt32(appinfos.size());
331     for (auto &info : appinfos) {
332         if (info == nullptr) {
333             return ERR_BGTASK_INVALID_PARAM;
334         }
335         if (!info->Marshalling(reply)) {
336             return ERR_BGTASK_PARCELABLE_FAILED;
337         }
338     }
339     return ERR_OK;
340 }
341 
HandlePauseTransientTaskTimeForInner(MessageParcel & data,MessageParcel & reply)342 ErrCode BackgroundTaskMgrStub::HandlePauseTransientTaskTimeForInner(MessageParcel& data, MessageParcel& reply)
343 {
344     int32_t uid = data.ReadInt32();
345     ErrCode result = PauseTransientTaskTimeForInner(uid);
346     if (!reply.WriteInt32(result)) {
347         BGTASK_LOGE("HandlePauseTransientTaskTimeForInner write result failed, ErrCode=%{public}d", result);
348         return ERR_BGTASK_PARCELABLE_FAILED;
349     }
350     return ERR_OK;
351 }
352 
HandleStartTransientTaskTimeForInner(MessageParcel & data,MessageParcel & reply)353 ErrCode BackgroundTaskMgrStub::HandleStartTransientTaskTimeForInner(MessageParcel& data, MessageParcel& reply)
354 {
355     int32_t uid = data.ReadInt32();
356     ErrCode result = StartTransientTaskTimeForInner(uid);
357     if (!reply.WriteInt32(result)) {
358         BGTASK_LOGE("HandleStartTransientTaskTimeForInner write result failed, ErrCode=%{public}d", result);
359         return ERR_BGTASK_PARCELABLE_FAILED;
360     }
361     return ERR_OK;
362 }
363 
HandleGetContinuousTaskApps(MessageParcel & data,MessageParcel & reply)364 ErrCode BackgroundTaskMgrStub::HandleGetContinuousTaskApps(MessageParcel& data, MessageParcel& reply)
365 {
366     std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> appInfos;
367     ErrCode result = GetContinuousTaskApps(appInfos);
368     if (!reply.WriteInt32(result)) {
369         return ERR_BGTASK_PARCELABLE_FAILED;
370     }
371     reply.WriteInt32(appInfos.size());
372     for (auto &info : appInfos) {
373         if (info == nullptr) {
374             return ERR_BGTASK_INVALID_PARAM;
375         }
376         if (!info->Marshalling(reply)) {
377             return ERR_BGTASK_PARCELABLE_FAILED;
378         }
379     }
380     return ERR_OK;
381 }
382 
HandleApplyEfficiencyResources(MessageParcel & data,MessageParcel & reply)383 ErrCode BackgroundTaskMgrStub::HandleApplyEfficiencyResources(MessageParcel& data, MessageParcel& reply)
384 {
385     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
386         "BackgroundTaskManager::EfficiencyResource::Stub::HandleApplyEfficiencyResources");
387 
388     BGTASK_LOGD("start receive data in apply res function from bgtask proxy");
389     sptr<EfficiencyResourceInfo> resourceInfoPtr = data.ReadParcelable<EfficiencyResourceInfo>();
390     if (resourceInfoPtr == nullptr) {
391         BGTASK_LOGE("EfficiencyResourceInfo ReadParcelable failed");
392         return ERR_BGTASK_PARCELABLE_FAILED;
393     }
394     ErrCode result = ApplyEfficiencyResources(resourceInfoPtr);
395     if (!reply.WriteInt32(result)) {
396         BGTASK_LOGE("HandleApplyEfficiencyResources write result failed, ErrCode=%{public}d", result);
397         return ERR_BGTASK_PARCELABLE_FAILED;
398     }
399     return ERR_OK;
400 }
401 
HandleResetAllEfficiencyResources(MessageParcel & data,MessageParcel & reply)402 ErrCode BackgroundTaskMgrStub::HandleResetAllEfficiencyResources(MessageParcel& data, MessageParcel& reply)
403 {
404     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
405         "BackgroundTaskManager::EfficiencyResource::Stub::HandleResetAllEfficiencyResources");
406 
407     BGTASK_LOGD("start receive data in reset all res function from bgtask proxy");
408     ErrCode result = ResetAllEfficiencyResources();
409     if (!reply.WriteInt32(result)) {
410         BGTASK_LOGE("HandleResetAllEfficiencyResources Write result failed, ErrCode=%{public}d", result);
411         return ERR_BGTASK_PARCELABLE_FAILED;
412     }
413     return ERR_OK;
414 }
415 
HandleGetEfficiencyResourcesInfos(MessageParcel & data,MessageParcel & reply)416 ErrCode BackgroundTaskMgrStub::HandleGetEfficiencyResourcesInfos(MessageParcel& data, MessageParcel& reply)
417 {
418     std::vector<std::shared_ptr<ResourceCallbackInfo>> appInfos;
419     std::vector<std::shared_ptr<ResourceCallbackInfo>> procInfos;
420     ErrCode result = GetEfficiencyResourcesInfos(appInfos, procInfos);
421     if (!reply.WriteInt32(result)) {
422         BGTASK_LOGE("HandleGetEfficiencyResourcesInfos write result failed, ErrCode=%{public}d", result);
423         return ERR_BGTASK_PARCELABLE_FAILED;
424     }
425     if (WriteInfoToParcel(appInfos, reply) != ERR_OK
426         || WriteInfoToParcel(procInfos, reply) != ERR_OK) {
427         BGTASK_LOGE("HandleGetEfficiencyResourcesInfos write result failed");
428         return ERR_BGTASK_PARCELABLE_FAILED;
429     }
430     return ERR_OK;
431 }
432 
WriteInfoToParcel(const std::vector<std::shared_ptr<ResourceCallbackInfo>> & infoMap,MessageParcel & reply)433 ErrCode BackgroundTaskMgrStub::WriteInfoToParcel(
434     const std::vector<std::shared_ptr<ResourceCallbackInfo>>& infoMap, MessageParcel& reply)
435 {
436     reply.WriteInt32(infoMap.size());
437     for (auto &info : infoMap) {
438         if (info == nullptr) {
439             return ERR_BGTASK_INVALID_PARAM;
440         }
441         if (!info->Marshalling(reply)) {
442             return ERR_BGTASK_PARCELABLE_FAILED;
443         }
444     }
445     return ERR_OK;
446 }
447 
HandleStopContinuousTask(MessageParcel & data,MessageParcel & reply)448 ErrCode BackgroundTaskMgrStub::HandleStopContinuousTask(MessageParcel& data, MessageParcel& reply)
449 {
450     int32_t uid = data.ReadInt32();
451     int32_t pid = data.ReadInt32();
452     uint32_t taskType = data.ReadUint32();
453     std::string key = data.ReadString();
454     ErrCode result = StopContinuousTask(uid, pid, taskType, key);
455     if (!reply.WriteInt32(result)) {
456         BGTASK_LOGE("HandleStopContinuousTask write result failed, ErrCode=%{public}d", result);
457         return ERR_BGTASK_PARCELABLE_FAILED;
458     }
459     return ERR_OK;
460 }
461 
HandleSetBgTaskConfig(MessageParcel & data,MessageParcel & reply)462 ErrCode BackgroundTaskMgrStub::HandleSetBgTaskConfig(MessageParcel& data, MessageParcel& reply)
463 {
464     std::string configData;
465     if (!data.ReadString(configData)) {
466         BGTASK_LOGE("read parce configData error");
467         return ERR_BGTASK_PARCELABLE_FAILED;
468     }
469     int32_t sourceType;
470     if (!data.ReadInt32(sourceType)) {
471         BGTASK_LOGE("read parce sourceType error");
472         return ERR_BGTASK_PARCELABLE_FAILED;
473     }
474     ErrCode result = SetBgTaskConfig(configData, sourceType);
475     if (!reply.WriteInt32(result)) {
476         BGTASK_LOGE("HandleSetBgTaskConfig write result failed, ErrCode=%{public}d", result);
477         return ERR_BGTASK_PARCELABLE_FAILED;
478     }
479     return ERR_OK;
480 }
481 }  // namespace BackgroundTaskMgr
482 }  // namespace OHOS