• 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_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     if (!reply.WriteInt32(taskParam->continuousTaskId_)) {
244         BGTASK_LOGE("HandleStartBackgroundRunning write continuousTaskId failed");
245         return ERR_BGTASK_PARCELABLE_FAILED;
246     }
247     BGTASK_LOGI("BackgroundTaskMgrStub write notificationId %{public}d, continuousTaskId %{public}d",
248         taskParam->notificationId_, taskParam->continuousTaskId_);
249     return ERR_OK;
250 }
251 
HandleUpdateBackgroundRunning(MessageParcel & data,MessageParcel & reply)252 ErrCode BackgroundTaskMgrStub::HandleUpdateBackgroundRunning(MessageParcel &data, MessageParcel &reply)
253 {
254     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
255         "BackgroundTaskManager::ContinuousTask::Stub::HandleUpdateBackgroundRunning");
256 
257     sptr<ContinuousTaskParam> taskParam = data.ReadParcelable<ContinuousTaskParam>();
258     if (taskParam == nullptr) {
259         BGTASK_LOGE("ContinuousTaskParam ReadParcelable failed");
260         return ERR_BGTASK_PARCELABLE_FAILED;
261     }
262     BGTASK_LOGI("HandleUpdateBackgroundRunning start");
263     ErrCode result = UpdateBackgroundRunning(taskParam);
264     if (!reply.WriteInt32(result)) {
265         BGTASK_LOGE("HandleUpdateBackgroundRunning write result failed, ErrCode=%{public}d", result);
266         return ERR_BGTASK_PARCELABLE_FAILED;
267     }
268     if (!reply.WriteInt32(taskParam->notificationId_)) {
269         BGTASK_LOGE("HandleUpdateBackgroundRunning write notificatinId failed");
270         return ERR_BGTASK_PARCELABLE_FAILED;
271     }
272     if (!reply.WriteInt32(taskParam->continuousTaskId_)) {
273         BGTASK_LOGE("HandleUpdateBackgroundRunning write continuousTaskId failed");
274         return ERR_BGTASK_PARCELABLE_FAILED;
275     }
276     BGTASK_LOGI("write notificationId %{public}d, continuousTaskId: %{public}d", taskParam->notificationId_,
277         taskParam->continuousTaskId_);
278     return ERR_OK;
279 }
280 
HandleStopBackgroundRunning(MessageParcel & data,MessageParcel & reply)281 ErrCode BackgroundTaskMgrStub::HandleStopBackgroundRunning(MessageParcel &data, MessageParcel &reply)
282 {
283     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
284         "BackgroundTaskManager::ContinuousTask::Stub::HandleStopBackgroundRunning");
285 
286     std::u16string u16AbilityName;
287     if (!data.ReadString16(u16AbilityName)) {
288         return ERR_BGTASK_PARCELABLE_FAILED;
289     }
290     std::string abilityName = Str16ToStr8(u16AbilityName);
291     sptr<IRemoteObject> abilityToken = data.ReadRemoteObject();
292     int32_t abilityId = data.ReadInt32();
293     ErrCode result = StopBackgroundRunning(abilityName, abilityToken, abilityId);
294     if (!reply.WriteInt32(result)) {
295         BGTASK_LOGE("HandleStopBackgroundRunning write result failed, ErrCode=%{public}d", result);
296         return ERR_BGTASK_PARCELABLE_FAILED;
297     }
298     return ERR_OK;
299 }
300 
HandleSubscribeBackgroundTask(MessageParcel & data,MessageParcel & reply)301 ErrCode BackgroundTaskMgrStub::HandleSubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)
302 {
303     sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
304     if (subscriber == nullptr) {
305         BGTASK_LOGE("HandleSubscribeBackgroundTask Read callback fail.");
306         return ERR_BGTASK_PARCELABLE_FAILED;
307     }
308 
309     ErrCode result = SubscribeBackgroundTask(iface_cast<IBackgroundTaskSubscriber>(subscriber));
310     if (!reply.WriteInt32(result)) {
311         BGTASK_LOGE("HandleSubscribeBackgroundTask Write result failed, ErrCode=%{public}d", result);
312         return ERR_BGTASK_PARCELABLE_FAILED;
313     }
314     return ERR_OK;
315 }
316 
HandleUnsubscribeBackgroundTask(MessageParcel & data,MessageParcel & reply)317 ErrCode BackgroundTaskMgrStub::HandleUnsubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)
318 {
319     sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
320     if (subscriber == nullptr) {
321         BGTASK_LOGE("HandleUnsubscribeBackgroundTask Read callback fail.");
322         return ERR_BGTASK_PARCELABLE_FAILED;
323     }
324 
325     ErrCode result = UnsubscribeBackgroundTask(iface_cast<IBackgroundTaskSubscriber>(subscriber));
326     if (!reply.WriteInt32(result)) {
327         BGTASK_LOGE("HandleUnsubscribeBackgroundTask Write result failed, ErrCode=%{public}d", result);
328         return ERR_BGTASK_PARCELABLE_FAILED;
329     }
330     return ERR_OK;
331 }
332 
HandleGetTransientTaskApps(MessageParcel & data,MessageParcel & reply)333 ErrCode BackgroundTaskMgrStub::HandleGetTransientTaskApps(MessageParcel& data, MessageParcel& reply)
334 {
335     std::vector<std::shared_ptr<TransientTaskAppInfo>> appinfos;
336     ErrCode result = GetTransientTaskApps(appinfos);
337     if (!reply.WriteInt32(result)) {
338         return ERR_BGTASK_PARCELABLE_FAILED;
339     }
340     reply.WriteInt32(appinfos.size());
341     for (auto &info : appinfos) {
342         if (info == nullptr) {
343             return ERR_BGTASK_INVALID_PARAM;
344         }
345         if (!info->Marshalling(reply)) {
346             return ERR_BGTASK_PARCELABLE_FAILED;
347         }
348     }
349     return ERR_OK;
350 }
351 
HandlePauseTransientTaskTimeForInner(MessageParcel & data,MessageParcel & reply)352 ErrCode BackgroundTaskMgrStub::HandlePauseTransientTaskTimeForInner(MessageParcel& data, MessageParcel& reply)
353 {
354     int32_t uid = data.ReadInt32();
355     ErrCode result = PauseTransientTaskTimeForInner(uid);
356     if (!reply.WriteInt32(result)) {
357         BGTASK_LOGE("HandlePauseTransientTaskTimeForInner write result failed, ErrCode=%{public}d", result);
358         return ERR_BGTASK_PARCELABLE_FAILED;
359     }
360     return ERR_OK;
361 }
362 
HandleStartTransientTaskTimeForInner(MessageParcel & data,MessageParcel & reply)363 ErrCode BackgroundTaskMgrStub::HandleStartTransientTaskTimeForInner(MessageParcel& data, MessageParcel& reply)
364 {
365     int32_t uid = data.ReadInt32();
366     ErrCode result = StartTransientTaskTimeForInner(uid);
367     if (!reply.WriteInt32(result)) {
368         BGTASK_LOGE("HandleStartTransientTaskTimeForInner write result failed, ErrCode=%{public}d", result);
369         return ERR_BGTASK_PARCELABLE_FAILED;
370     }
371     return ERR_OK;
372 }
373 
HandleGetContinuousTaskApps(MessageParcel & data,MessageParcel & reply)374 ErrCode BackgroundTaskMgrStub::HandleGetContinuousTaskApps(MessageParcel& data, MessageParcel& reply)
375 {
376     std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> appInfos;
377     ErrCode result = GetContinuousTaskApps(appInfos);
378     if (!reply.WriteInt32(result)) {
379         return ERR_BGTASK_PARCELABLE_FAILED;
380     }
381     reply.WriteInt32(appInfos.size());
382     for (auto &info : appInfos) {
383         if (info == nullptr) {
384             return ERR_BGTASK_INVALID_PARAM;
385         }
386         if (!info->Marshalling(reply)) {
387             return ERR_BGTASK_PARCELABLE_FAILED;
388         }
389     }
390     return ERR_OK;
391 }
392 
HandleApplyEfficiencyResources(MessageParcel & data,MessageParcel & reply)393 ErrCode BackgroundTaskMgrStub::HandleApplyEfficiencyResources(MessageParcel& data, MessageParcel& reply)
394 {
395     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
396         "BackgroundTaskManager::EfficiencyResource::Stub::HandleApplyEfficiencyResources");
397 
398     BGTASK_LOGD("start receive data in apply res function from bgtask proxy");
399     sptr<EfficiencyResourceInfo> resourceInfoPtr = data.ReadParcelable<EfficiencyResourceInfo>();
400     if (resourceInfoPtr == nullptr) {
401         BGTASK_LOGE("EfficiencyResourceInfo ReadParcelable failed");
402         return ERR_BGTASK_PARCELABLE_FAILED;
403     }
404     ErrCode result = ApplyEfficiencyResources(resourceInfoPtr);
405     if (!reply.WriteInt32(result)) {
406         BGTASK_LOGE("HandleApplyEfficiencyResources write result failed, ErrCode=%{public}d", result);
407         return ERR_BGTASK_PARCELABLE_FAILED;
408     }
409     return ERR_OK;
410 }
411 
HandleResetAllEfficiencyResources(MessageParcel & data,MessageParcel & reply)412 ErrCode BackgroundTaskMgrStub::HandleResetAllEfficiencyResources(MessageParcel& data, MessageParcel& reply)
413 {
414     HitraceScoped traceScoped(HITRACE_TAG_OHOS,
415         "BackgroundTaskManager::EfficiencyResource::Stub::HandleResetAllEfficiencyResources");
416 
417     BGTASK_LOGD("start receive data in reset all res function from bgtask proxy");
418     ErrCode result = ResetAllEfficiencyResources();
419     if (!reply.WriteInt32(result)) {
420         BGTASK_LOGE("HandleResetAllEfficiencyResources Write result failed, ErrCode=%{public}d", result);
421         return ERR_BGTASK_PARCELABLE_FAILED;
422     }
423     return ERR_OK;
424 }
425 
HandleGetEfficiencyResourcesInfos(MessageParcel & data,MessageParcel & reply)426 ErrCode BackgroundTaskMgrStub::HandleGetEfficiencyResourcesInfos(MessageParcel& data, MessageParcel& reply)
427 {
428     std::vector<std::shared_ptr<ResourceCallbackInfo>> appInfos;
429     std::vector<std::shared_ptr<ResourceCallbackInfo>> procInfos;
430     ErrCode result = GetEfficiencyResourcesInfos(appInfos, procInfos);
431     if (!reply.WriteInt32(result)) {
432         BGTASK_LOGE("HandleGetEfficiencyResourcesInfos write result failed, ErrCode=%{public}d", result);
433         return ERR_BGTASK_PARCELABLE_FAILED;
434     }
435     if (WriteInfoToParcel(appInfos, reply) != ERR_OK
436         || WriteInfoToParcel(procInfos, reply) != ERR_OK) {
437         BGTASK_LOGE("HandleGetEfficiencyResourcesInfos write result failed");
438         return ERR_BGTASK_PARCELABLE_FAILED;
439     }
440     return ERR_OK;
441 }
442 
WriteInfoToParcel(const std::vector<std::shared_ptr<ResourceCallbackInfo>> & infoMap,MessageParcel & reply)443 ErrCode BackgroundTaskMgrStub::WriteInfoToParcel(
444     const std::vector<std::shared_ptr<ResourceCallbackInfo>>& infoMap, MessageParcel& reply)
445 {
446     reply.WriteInt32(infoMap.size());
447     for (auto &info : infoMap) {
448         if (info == nullptr) {
449             return ERR_BGTASK_INVALID_PARAM;
450         }
451         if (!info->Marshalling(reply)) {
452             return ERR_BGTASK_PARCELABLE_FAILED;
453         }
454     }
455     return ERR_OK;
456 }
457 
HandleStopContinuousTask(MessageParcel & data,MessageParcel & reply)458 ErrCode BackgroundTaskMgrStub::HandleStopContinuousTask(MessageParcel& data, MessageParcel& reply)
459 {
460     int32_t uid = data.ReadInt32();
461     int32_t pid = data.ReadInt32();
462     uint32_t taskType = data.ReadUint32();
463     std::string key = data.ReadString();
464     ErrCode result = StopContinuousTask(uid, pid, taskType, key);
465     if (!reply.WriteInt32(result)) {
466         BGTASK_LOGE("HandleStopContinuousTask write result failed, ErrCode=%{public}d", result);
467         return ERR_BGTASK_PARCELABLE_FAILED;
468     }
469     return ERR_OK;
470 }
471 
HandleSetBgTaskConfig(MessageParcel & data,MessageParcel & reply)472 ErrCode BackgroundTaskMgrStub::HandleSetBgTaskConfig(MessageParcel& data, MessageParcel& reply)
473 {
474     std::string configData;
475     if (!data.ReadString(configData)) {
476         BGTASK_LOGE("read parce configData error");
477         return ERR_BGTASK_PARCELABLE_FAILED;
478     }
479     int32_t sourceType;
480     if (!data.ReadInt32(sourceType)) {
481         BGTASK_LOGE("read parce sourceType error");
482         return ERR_BGTASK_PARCELABLE_FAILED;
483     }
484     ErrCode result = SetBgTaskConfig(configData, sourceType);
485     if (!reply.WriteInt32(result)) {
486         BGTASK_LOGE("HandleSetBgTaskConfig write result failed, ErrCode=%{public}d", result);
487         return ERR_BGTASK_PARCELABLE_FAILED;
488     }
489     return ERR_OK;
490 }
491 }  // namespace BackgroundTaskMgr
492 }  // namespace OHOS