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_proxy.h"
17 
18 #include <message_parcel.h>
19 #include <string_ex.h>
20 
21 #include "bgtaskmgr_log_wrapper.h"
22 #include "ibackground_task_mgr_ipc_interface_code.h"
23 
24 using namespace std;
25 
26 namespace OHOS {
27 namespace BackgroundTaskMgr {
BackgroundTaskMgrProxy(const sptr<IRemoteObject> & impl)28 BackgroundTaskMgrProxy::BackgroundTaskMgrProxy(const sptr<IRemoteObject>& impl)
29     :IRemoteProxy<IBackgroundTaskMgr>(impl) {}
~BackgroundTaskMgrProxy()30 BackgroundTaskMgrProxy::~BackgroundTaskMgrProxy() {}
31 
RequestSuspendDelay(const std::u16string & reason,const sptr<IExpiredCallback> & callback,std::shared_ptr<DelaySuspendInfo> & delayInfo)32 ErrCode BackgroundTaskMgrProxy::RequestSuspendDelay(const std::u16string& reason,
33     const sptr<IExpiredCallback>& callback, std::shared_ptr<DelaySuspendInfo> &delayInfo)
34 {
35     if (callback == nullptr) {
36         BGTASK_LOGE("RequestSuspendDelay callback is null");
37         return ERR_CALLBACK_NULL_OR_TYPE_ERR;
38     }
39 
40     MessageParcel data;
41     MessageParcel reply;
42     MessageOption option = {MessageOption::TF_SYNC};
43     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
44         BGTASK_LOGE("RequestSuspendDelay write descriptor failed");
45         return ERR_BGTASK_PARCELABLE_FAILED;
46     }
47     if (!data.WriteString16(reason)) {
48         BGTASK_LOGE("RequestSuspendDelay write reason failed");
49         return ERR_BGTASK_PARCELABLE_FAILED;
50     }
51     if (!data.WriteRemoteObject(callback->AsObject())) {
52         BGTASK_LOGE("RequestSuspendDelay write callback failed");
53         return ERR_BGTASK_PARCELABLE_FAILED;
54     }
55     ErrCode result = InnerTransact(static_cast<uint32_t>(
56         BackgroundTaskMgrStubInterfaceCode::REQUEST_SUSPEND_DELAY), option, data, reply);
57     if (result != ERR_OK) {
58         BGTASK_LOGE("RequestSuspendDelay transact ErrCode=%{public}d", result);
59         return ERR_BGTASK_TRANSACT_FAILED;
60     }
61     if (!reply.ReadInt32(result)) {
62         BGTASK_LOGE("RequestSuspendDelay fail: read result failed.");
63         return ERR_BGTASK_PARCELABLE_FAILED;
64     }
65     delayInfo = DelaySuspendInfo::Unmarshalling(reply);
66     if (delayInfo == nullptr) {
67         BGTASK_LOGE("RequestSuspendDelay read result failed");
68         return ERR_BGTASK_PARCELABLE_FAILED;
69     }
70     return result;
71 }
72 
CancelSuspendDelay(int32_t requestId)73 ErrCode BackgroundTaskMgrProxy::CancelSuspendDelay(int32_t requestId)
74 {
75     MessageParcel data;
76     MessageParcel reply;
77     MessageOption option = {MessageOption::TF_SYNC};
78     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
79         BGTASK_LOGE("RequestSuspendDelay write descriptor failed");
80         return ERR_BGTASK_PARCELABLE_FAILED;
81     }
82     if (!data.WriteInt32(requestId)) {
83         BGTASK_LOGE("RequestSuspendDelay write requestId failed");
84         return ERR_BGTASK_PARCELABLE_FAILED;
85     }
86 
87     ErrCode result = InnerTransact(static_cast<uint32_t>(
88         BackgroundTaskMgrStubInterfaceCode::CANCEL_SUSPEND_DELAY), option, data, reply);
89     if (result != ERR_OK) {
90         BGTASK_LOGE("RequestSuspendDelay fail: transact ErrCode=%{public}d", result);
91         return ERR_BGTASK_TRANSACT_FAILED;
92     }
93     if (!reply.ReadInt32(result)) {
94         BGTASK_LOGE("RequestSuspendDelay fail: read result failed.");
95         return ERR_BGTASK_PARCELABLE_FAILED;
96     }
97     return result;
98 }
99 
GetRemainingDelayTime(int32_t requestId,int32_t & delayTime)100 ErrCode BackgroundTaskMgrProxy::GetRemainingDelayTime(int32_t requestId, int32_t &delayTime)
101 {
102     MessageParcel data;
103     MessageParcel reply;
104     MessageOption option = {MessageOption::TF_SYNC};
105     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
106         BGTASK_LOGE(" BackgroundTaskMgrProxy::%{public}s, write descriptor failed", __func__);
107         return ERR_BGTASK_PARCELABLE_FAILED;
108     }
109     if (!data.WriteInt32(requestId)) {
110         BGTASK_LOGE(" BackgroundTaskMgrProxy::%{public}s, write requestId failed", __func__);
111         return ERR_BGTASK_PARCELABLE_FAILED;
112     }
113 
114     ErrCode result = InnerTransact(static_cast<uint32_t>(
115         BackgroundTaskMgrStubInterfaceCode::GET_REMAINING_DELAY_TIME), option, data, reply);
116     if (result != ERR_OK) {
117         BGTASK_LOGE("GetRemainingDelayTime fail: transact ErrCode=%{public}d", result);
118         return ERR_BGTASK_TRANSACT_FAILED;
119     }
120     if (!reply.ReadInt32(result)) {
121         BGTASK_LOGE("GetRemainingDelayTime fail: read result failed.");
122         return ERR_BGTASK_PARCELABLE_FAILED;
123     }
124     if (!reply.ReadInt32(delayTime)) {
125         BGTASK_LOGE("GetRemainingDelayTime fail: read result failed.");
126         return ERR_BGTASK_PARCELABLE_FAILED;
127     }
128     return result;
129 }
130 
RequestBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> & taskParam)131 ErrCode BackgroundTaskMgrProxy::RequestBackgroundRunningForInner(const sptr<ContinuousTaskParamForInner> &taskParam)
132 {
133     if (taskParam == nullptr) {
134         return ERR_BGTASK_INVALID_PARAM;
135     }
136 
137     MessageParcel dataInfo;
138     if (!dataInfo.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
139         return ERR_BGTASK_PARCELABLE_FAILED;
140     }
141 
142     if (!dataInfo.WriteParcelable(taskParam)) {
143         return ERR_BGTASK_PARCELABLE_FAILED;
144     }
145 
146     MessageParcel reply;
147     MessageOption option = {MessageOption::TF_SYNC};
148 
149     ErrCode result = InnerTransact(static_cast<uint32_t>(
150         BackgroundTaskMgrStubInterfaceCode::REQUEST_BACKGROUND_RUNNING_FOR_INNER), option, dataInfo, reply);
151     if (result != ERR_OK) {
152         BGTASK_LOGE("RequestBackgroundRunningForInner fail: transact ErrCode=%{public}d", result);
153         return ERR_BGTASK_TRANSACT_FAILED;
154     }
155     if (!reply.ReadInt32(result)) {
156         BGTASK_LOGE("RequestBackgroundRunningForInner fail: read result failed.");
157         return ERR_BGTASK_PARCELABLE_FAILED;
158     }
159     return result;
160 }
161 
StartBackgroundRunning(const sptr<ContinuousTaskParam> & taskParam)162 ErrCode BackgroundTaskMgrProxy::StartBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam)
163 {
164     if (taskParam == nullptr) {
165         return ERR_BGTASK_INVALID_PARAM;
166     }
167 
168     MessageParcel data;
169     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
170         return ERR_BGTASK_PARCELABLE_FAILED;
171     }
172 
173     if (!data.WriteParcelable(taskParam)) {
174         return ERR_BGTASK_PARCELABLE_FAILED;
175     }
176 
177     MessageParcel reply;
178     MessageOption option = {MessageOption::TF_SYNC};
179 
180     ErrCode result = InnerTransact(static_cast<uint32_t>(
181         BackgroundTaskMgrStubInterfaceCode::START_BACKGROUND_RUNNING), option, data, reply);
182     if (result != ERR_OK) {
183         BGTASK_LOGE("StartBackgroundRunning fail: transact ErrCode=%{public}d", result);
184         return ERR_BGTASK_TRANSACT_FAILED;
185     }
186     if (!reply.ReadInt32(result)) {
187         BGTASK_LOGE("StartBackgroundRunning fail: read result failed.");
188         return ERR_BGTASK_PARCELABLE_FAILED;
189     }
190     int32_t notificationId = -1;
191     if (!reply.ReadInt32(notificationId)) {
192         BGTASK_LOGE("StartBackgroundRunning fail: read notificationId failed.");
193         return ERR_BGTASK_PARCELABLE_FAILED;
194     }
195     BGTASK_LOGI("read notificationId %{public}d", notificationId);
196     taskParam->notificationId_ = notificationId;
197     return result;
198 }
199 
UpdateBackgroundRunning(const sptr<ContinuousTaskParam> & taskParam)200 ErrCode BackgroundTaskMgrProxy::UpdateBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam)
201 {
202     if (taskParam == nullptr) {
203         return ERR_BGTASK_INVALID_PARAM;
204     }
205 
206     MessageParcel data;
207     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
208         return ERR_BGTASK_PARCELABLE_FAILED;
209     }
210 
211     if (!data.WriteParcelable(taskParam)) {
212         return ERR_BGTASK_PARCELABLE_FAILED;
213     }
214 
215     MessageParcel reply;
216     MessageOption option = {MessageOption::TF_SYNC};
217 
218     ErrCode result = InnerTransact(static_cast<uint32_t>(
219         BackgroundTaskMgrStubInterfaceCode::UPDATE_BACKGROUND_RUNNING), option, data, reply);
220     if (result != ERR_OK) {
221         BGTASK_LOGE("UpdateBackgroundRunning fail: transact ErrCode=%{public}d", result);
222         return ERR_BGTASK_TRANSACT_FAILED;
223     }
224     if (!reply.ReadInt32(result)) {
225         BGTASK_LOGE("UpdateBackgroundRunning fail: read result failed.");
226         return ERR_BGTASK_PARCELABLE_FAILED;
227     }
228     int32_t notificationId = -1;
229     if (!reply.ReadInt32(notificationId)) {
230         BGTASK_LOGE("UpdateBackgroundRunning fail: read notificationId failed.");
231         return ERR_BGTASK_PARCELABLE_FAILED;
232     }
233     BGTASK_LOGI("read notificationId %{public}d", notificationId);
234     taskParam->notificationId_ = notificationId;
235     return result;
236 }
237 
StopBackgroundRunning(const std::string & abilityName,const sptr<IRemoteObject> & abilityToken,int32_t abilityId)238 ErrCode BackgroundTaskMgrProxy::StopBackgroundRunning(const std::string &abilityName,
239     const sptr<IRemoteObject> &abilityToken, int32_t abilityId)
240 {
241     MessageParcel data;
242     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
243         return ERR_BGTASK_PARCELABLE_FAILED;
244     }
245 
246     std::u16string u16AbilityName = Str8ToStr16(abilityName);
247     if (!data.WriteString16(u16AbilityName)) {
248         BGTASK_LOGE("StopBackgroundRunning parcel ability Name failed");
249         return ERR_BGTASK_PARCELABLE_FAILED;
250     }
251 
252     if (!data.WriteRemoteObject(abilityToken)) {
253         BGTASK_LOGE("StopBackgroundRunning parcel ability token failed");
254         return ERR_BGTASK_PARCELABLE_FAILED;
255     }
256 
257     if (!data.WriteInt32(abilityId)) {
258         BGTASK_LOGE("StopBackgroundRunning parcel abilityId failed");
259         return ERR_BGTASK_PARCELABLE_FAILED;
260     }
261 
262     MessageParcel reply;
263     MessageOption option = {MessageOption::TF_SYNC};
264 
265     ErrCode result = InnerTransact(static_cast<uint32_t>(
266         BackgroundTaskMgrStubInterfaceCode::STOP_BACKGROUND_RUNNING), option, data, reply);
267     if (result != ERR_OK) {
268         BGTASK_LOGE("StopBackgroundRunning transact ErrCode=%{public}d", result);
269         return ERR_BGTASK_TRANSACT_FAILED;
270     }
271     if (!reply.ReadInt32(result)) {
272         BGTASK_LOGE("StopBackgroundRunning read result failed.");
273         return ERR_BGTASK_PARCELABLE_FAILED;
274     }
275     return result;
276 }
277 
SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber> & subscriber)278 ErrCode BackgroundTaskMgrProxy::SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber)
279 {
280     if (subscriber == nullptr) {
281         BGTASK_LOGE("SubscribeBackgroundTask subscriber is null");
282         return ERR_BGTASK_PARCELABLE_FAILED;
283     }
284 
285     MessageParcel data;
286     MessageParcel reply;
287     MessageOption option = {MessageOption::TF_SYNC};
288     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
289         BGTASK_LOGE("SubscribeBackgroundTask write descriptor failed");
290         return ERR_BGTASK_PARCELABLE_FAILED;
291     }
292     if (!data.WriteRemoteObject(subscriber->AsObject())) {
293         BGTASK_LOGE("SubscribeBackgroundTask write subscriber failed");
294         return ERR_BGTASK_PARCELABLE_FAILED;
295     }
296 
297     ErrCode result = InnerTransact(static_cast<uint32_t>(
298         BackgroundTaskMgrStubInterfaceCode::SUBSCRIBE_BACKGROUND_TASK), option, data, reply);
299     if (result != ERR_OK) {
300         BGTASK_LOGE("SubscribeBackgroundTask fail: transact ErrCode=%{public}d", result);
301         return ERR_BGTASK_TRANSACT_FAILED;
302     }
303     if (!reply.ReadInt32(result)) {
304         BGTASK_LOGE("SubscribeBackgroundTask fail: read result failed.");
305         return ERR_BGTASK_PARCELABLE_FAILED;
306     }
307     return result;
308 }
309 
UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber> & subscriber)310 ErrCode BackgroundTaskMgrProxy::UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber)
311 {
312     if (subscriber == nullptr) {
313         BGTASK_LOGE("UnsubscribeBackgroundTask subscriber is null");
314         return ERR_BGTASK_PARCELABLE_FAILED;
315     }
316 
317     MessageParcel data;
318     MessageParcel reply;
319     MessageOption option = {MessageOption::TF_SYNC};
320     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
321         BGTASK_LOGE("UnsubscribeBackgroundTask write descriptor failed");
322         return ERR_BGTASK_PARCELABLE_FAILED;
323     }
324     if (!data.WriteRemoteObject(subscriber->AsObject())) {
325         BGTASK_LOGE("UnsubscribeBackgroundTask write subscriber failed");
326         return ERR_BGTASK_PARCELABLE_FAILED;
327     }
328 
329     ErrCode result = InnerTransact(static_cast<uint32_t>(
330         BackgroundTaskMgrStubInterfaceCode::UNSUBSCRIBE_BACKGROUND_TASK), option, data, reply);
331     if (result != ERR_OK) {
332         BGTASK_LOGE("UnsubscribeBackgroundTask fail: transact ErrCode=%{public}d", result);
333         return ERR_BGTASK_TRANSACT_FAILED;
334     }
335     if (!reply.ReadInt32(result)) {
336         BGTASK_LOGE("UnsubscribeBackgroundTask fail: read result failed.");
337         return ERR_BGTASK_PARCELABLE_FAILED;
338     }
339     return result;
340 }
341 
GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> & list)342 ErrCode BackgroundTaskMgrProxy::GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list)
343 {
344     MessageParcel data;
345     MessageParcel reply;
346     MessageOption option = {MessageOption::TF_SYNC};
347     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
348         BGTASK_LOGE("GetTransientTaskApps write descriptor failed");
349         return ERR_BGTASK_PARCELABLE_FAILED;
350     }
351 
352     ErrCode result = InnerTransact(static_cast<uint32_t>(
353         BackgroundTaskMgrStubInterfaceCode::GET_TRANSIENT_TASK_APPS), option, data, reply);
354     if (result != ERR_OK) {
355         BGTASK_LOGE("GetTransientTaskApps fail: transact ErrCode=%{public}d", result);
356         return ERR_BGTASK_TRANSACT_FAILED;
357     }
358     if (!reply.ReadInt32(result)) {
359         BGTASK_LOGE("GetTransientTaskApps fail: read result failed.");
360         return ERR_BGTASK_PARCELABLE_FAILED;
361     }
362 
363     int32_t infoSize = reply.ReadInt32();
364     for (int32_t i = 0; i < infoSize; i++) {
365         auto info = TransientTaskAppInfo::Unmarshalling(reply);
366         if (!info) {
367             BGTASK_LOGE("GetTransientTaskApps Read Parcelable infos failed.");
368             return ERR_BGTASK_PARCELABLE_FAILED;
369         }
370         list.emplace_back(info);
371     }
372 
373     return result;
374 }
375 
PauseTransientTaskTimeForInner(int32_t uid)376 ErrCode BackgroundTaskMgrProxy::PauseTransientTaskTimeForInner(int32_t uid)
377 {
378     MessageParcel data;
379     MessageParcel reply;
380     MessageOption option = {MessageOption::TF_SYNC};
381     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
382         BGTASK_LOGE("PauseTransientTaskTimeForInner write descriptor failed");
383         return ERR_BGTASK_PARCELABLE_FAILED;
384     }
385     if (!data.WriteInt32(uid)) {
386         BGTASK_LOGE("PauseTransientTaskTimeForInner parcel uid failed");
387         return ERR_BGTASK_PARCELABLE_FAILED;
388     }
389     ErrCode result = InnerTransact(static_cast<uint32_t>(
390         BackgroundTaskMgrStubInterfaceCode::PAUSE_TRANSIENT_TASK_TIME_FOR_INNER), option, data, reply);
391     if (result != ERR_OK) {
392         BGTASK_LOGE("PauseTransientTaskTimeForInner fail: transact ErrCode=%{public}d", result);
393         return ERR_BGTASK_TRANSACT_FAILED;
394     }
395     if (!reply.ReadInt32(result)) {
396         BGTASK_LOGE("PauseTransientTaskTimeForInner fail: read result failed.");
397         return ERR_BGTASK_PARCELABLE_FAILED;
398     }
399     return result;
400 }
401 
StartTransientTaskTimeForInner(int32_t uid)402 ErrCode BackgroundTaskMgrProxy::StartTransientTaskTimeForInner(int32_t uid)
403 {
404     MessageParcel data;
405     MessageParcel reply;
406     MessageOption option = {MessageOption::TF_SYNC};
407     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
408         BGTASK_LOGE("StartTransientTaskTimeForInner write descriptor failed");
409         return ERR_BGTASK_PARCELABLE_FAILED;
410     }
411     if (!data.WriteInt32(uid)) {
412         BGTASK_LOGE("StartTransientTaskTimeForInner parcel uid failed");
413         return ERR_BGTASK_PARCELABLE_FAILED;
414     }
415     ErrCode result = InnerTransact(static_cast<uint32_t>(
416         BackgroundTaskMgrStubInterfaceCode::START_TRANSIENT_TASK_TIME_FOR_INNER), option, data, reply);
417     if (result != ERR_OK) {
418         BGTASK_LOGE("StartTransientTaskTimeForInner fail: transact ErrCode=%{public}d", result);
419         return ERR_BGTASK_TRANSACT_FAILED;
420     }
421     if (!reply.ReadInt32(result)) {
422         BGTASK_LOGE("StartTransientTaskTimeForInner fail: read result failed.");
423         return ERR_BGTASK_PARCELABLE_FAILED;
424     }
425     return result;
426 }
427 
GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> & list)428 ErrCode BackgroundTaskMgrProxy::GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list)
429 {
430     MessageParcel data;
431     MessageParcel reply;
432     MessageOption option = {MessageOption::TF_SYNC};
433     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
434         BGTASK_LOGE("GetContinuousTaskApps write descriptor failed");
435         return ERR_BGTASK_PARCELABLE_FAILED;
436     }
437 
438     ErrCode result = InnerTransact(static_cast<uint32_t>(
439         BackgroundTaskMgrStubInterfaceCode::GET_CONTINUOUS_TASK_APPS), option, data, reply);
440     if (result != ERR_OK) {
441         BGTASK_LOGE("GetContinuousTaskApps fail: transact ErrCode=%{public}d", result);
442         return ERR_BGTASK_TRANSACT_FAILED;
443     }
444     if (!reply.ReadInt32(result)) {
445         BGTASK_LOGE("GetContinuousTaskApps fail: read result failed.");
446         return ERR_BGTASK_PARCELABLE_FAILED;
447     }
448 
449     if (result != ERR_OK) {
450         BGTASK_LOGE("GetContinuousTaskApps failed.");
451         return result;
452     }
453 
454     int32_t infoSize = reply.ReadInt32();
455     for (int32_t i = 0; i < infoSize; i++) {
456         auto info = ContinuousTaskCallbackInfo::Unmarshalling(reply);
457         if (!info) {
458             BGTASK_LOGE("GetContinuousTaskApps Read Parcelable infos failed.");
459             return ERR_BGTASK_PARCELABLE_FAILED;
460         }
461         list.emplace_back(info);
462     }
463 
464     return result;
465 }
466 
StopContinuousTask(int32_t uid,int32_t pid,uint32_t taskType,const std::string & key)467 ErrCode BackgroundTaskMgrProxy::StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType, const std::string &key)
468 {
469     MessageParcel data;
470     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
471         return ERR_BGTASK_PARCELABLE_FAILED;
472     }
473 
474     if (!data.WriteInt32(uid)) {
475         BGTASK_LOGE("StopContinuousTask parcel uid failed");
476         return ERR_BGTASK_PARCELABLE_FAILED;
477     }
478 
479     if (!data.WriteInt32(pid)) {
480         BGTASK_LOGE("StopContinuousTask parcel pid failed");
481         return ERR_BGTASK_PARCELABLE_FAILED;
482     }
483 
484     if (!data.WriteUint32(taskType)) {
485         BGTASK_LOGE("StopContinuousTask parcel taskType failed");
486         return ERR_BGTASK_PARCELABLE_FAILED;
487     }
488 
489     if (!data.WriteString(key)) {
490         BGTASK_LOGE("StopContinuousTask parcel key failed");
491         return ERR_BGTASK_PARCELABLE_FAILED;
492     }
493 
494     MessageParcel reply;
495     MessageOption option = {MessageOption::TF_SYNC};
496 
497     ErrCode result = InnerTransact(static_cast<uint32_t>(
498         BackgroundTaskMgrStubInterfaceCode::STOP_CONTINUOUS_TASK), option, data, reply);
499     if (result != ERR_OK) {
500         BGTASK_LOGE("StopContinuousTask transact ErrCode=%{public}d", result);
501         return ERR_BGTASK_TRANSACT_FAILED;
502     }
503     if (!reply.ReadInt32(result)) {
504         BGTASK_LOGE("StopContinuousTask read result failed.");
505         return ERR_BGTASK_PARCELABLE_FAILED;
506     }
507     return result;
508 }
509 
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)510 ErrCode BackgroundTaskMgrProxy::InnerTransact(uint32_t code, MessageOption &flags,
511     MessageParcel &data, MessageParcel &reply)
512 {
513     auto remote = Remote();
514     if (remote == nullptr) {
515         BGTASK_LOGE("InnerTransact get Remote fail code %{public}d", code);
516         return ERR_DEAD_OBJECT;
517     }
518     int32_t err = remote->SendRequest(code, data, reply, flags);
519     switch (err) {
520         case NO_ERROR: {
521             return ERR_OK;
522         }
523         case DEAD_OBJECT: {
524             BGTASK_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}u", err, code);
525             return ERR_DEAD_OBJECT;
526         }
527         default: {
528             BGTASK_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}u", err, code);
529             return ERR_BGTASK_TRANSACT_FAILED;
530         }
531     }
532 }
533 
ApplyEfficiencyResources(const sptr<EfficiencyResourceInfo> & resourceInfo)534 ErrCode BackgroundTaskMgrProxy::ApplyEfficiencyResources(const sptr<EfficiencyResourceInfo> &resourceInfo)
535 {
536     if (resourceInfo == nullptr) {
537         return ERR_BGTASK_INVALID_PARAM;
538     }
539 
540     MessageParcel data;
541     MessageParcel reply;
542     MessageOption option = {MessageOption::TF_SYNC};
543     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
544         BGTASK_LOGE("ApplyEfficiencyResources write descriptor failed");
545         return ERR_BGTASK_PARCELABLE_FAILED;
546     }
547 
548     if (!data.WriteParcelable(resourceInfo)) {
549         return ERR_BGTASK_PARCELABLE_FAILED;
550     }
551     BGTASK_LOGD("start send data in apply res function from bgtask proxy");
552     ErrCode result = InnerTransact(static_cast<uint32_t>(
553         BackgroundTaskMgrStubInterfaceCode::APPLY_EFFICIENCY_RESOURCES), option, data, reply);
554     if (result != ERR_OK) {
555         BGTASK_LOGE("ApplyEfficiencyResources fail: transact ErrCode=%{public}d", result);
556         return ERR_BGTASK_TRANSACT_FAILED;
557     }
558     if (!reply.ReadInt32(result)) {
559         BGTASK_LOGE("ApplyEfficiencyResources fail: read result failed.");
560         return ERR_BGTASK_PARCELABLE_FAILED;
561     }
562     return result;
563 }
564 
ResetAllEfficiencyResources()565 ErrCode BackgroundTaskMgrProxy::ResetAllEfficiencyResources()
566 {
567     MessageParcel data;
568     MessageParcel reply;
569     MessageOption option = {MessageOption::TF_SYNC};
570     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
571         BGTASK_LOGE("ResetAllEfficiencyResources write descriptor failed");
572         return ERR_BGTASK_PARCELABLE_FAILED;
573     }
574     BGTASK_LOGD("start send data in reset all res function from bgtask proxy");
575     ErrCode result = InnerTransact(static_cast<uint32_t>(
576         BackgroundTaskMgrStubInterfaceCode::RESET_ALL_EFFICIENCY_RESOURCES), option, data, reply);
577     if (result != ERR_OK) {
578         BGTASK_LOGE("ResetAllEfficiencyResources fail: transact ErrCode=%{public}d", result);
579         return ERR_BGTASK_TRANSACT_FAILED;
580     }
581     if (!reply.ReadInt32(result)) {
582         BGTASK_LOGE("ResetAllEfficiencyResources fail: read result failed.");
583         return ERR_BGTASK_PARCELABLE_FAILED;
584     }
585     return result;
586 }
587 
GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> & appList,std::vector<std::shared_ptr<ResourceCallbackInfo>> & procList)588 ErrCode BackgroundTaskMgrProxy::GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<
589     ResourceCallbackInfo>> &appList, std::vector<std::shared_ptr<ResourceCallbackInfo>> &procList)
590 {
591     MessageParcel data;
592     MessageParcel reply;
593     MessageOption option = {MessageOption::TF_SYNC};
594     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
595         BGTASK_LOGE("GetEfficiencyResourcesInfos write descriptor failed");
596         return ERR_BGTASK_PARCELABLE_FAILED;
597     }
598 
599     ErrCode result = InnerTransact(static_cast<uint32_t>(
600         BackgroundTaskMgrStubInterfaceCode::GET_EFFICIENCY_RESOURCES_INFOS), option, data, reply);
601     if (result != ERR_OK) {
602         BGTASK_LOGE("GetEfficiencyResourcesInfos fail: transact ErrCode=%{public}d", result);
603         return ERR_BGTASK_TRANSACT_FAILED;
604     }
605     if (!reply.ReadInt32(result)) {
606         BGTASK_LOGE("GetEfficiencyResourcesInfos fail: read result failed.");
607         return ERR_BGTASK_PARCELABLE_FAILED;
608     }
609 
610     if (result != ERR_OK) {
611         return result;
612     }
613 
614     int32_t infoSize = reply.ReadInt32();
615     for (int32_t i = 0; i < infoSize; i++) {
616         auto info = ResourceCallbackInfo::Unmarshalling(reply);
617         if (!info) {
618             BGTASK_LOGE("GetContinuousTaskApps Read Parcelable infos failed.");
619             return ERR_BGTASK_PARCELABLE_FAILED;
620         }
621         appList.emplace_back(info);
622     }
623 
624     infoSize = reply.ReadInt32();
625     for (int32_t i = 0; i < infoSize; i++) {
626         auto info = ResourceCallbackInfo::Unmarshalling(reply);
627         if (!info) {
628             BGTASK_LOGE("GetContinuousTaskApps Read Parcelable infos failed.");
629             return ERR_BGTASK_PARCELABLE_FAILED;
630         }
631         procList.emplace_back(info);
632     }
633 
634     return result;
635 }
636 
SetBgTaskConfig(const std::string & configData,int32_t sourceType)637 ErrCode BackgroundTaskMgrProxy::SetBgTaskConfig(const std::string &configData, int32_t sourceType)
638 {
639     MessageParcel data;
640     MessageParcel reply;
641     MessageOption option = {MessageOption::TF_SYNC};
642     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
643         BGTASK_LOGE("SetBgTaskConfig write descriptor failed");
644         return ERR_BGTASK_PARCELABLE_FAILED;
645     }
646     if (!data.WriteString(configData)) {
647         BGTASK_LOGE("SetBgTaskConfig write configData failed");
648         return ERR_BGTASK_PARCELABLE_FAILED;
649     }
650     if (!data.WriteInt32(sourceType)) {
651         BGTASK_LOGE("SetBgTaskConfig write sourceType failed");
652         return ERR_BGTASK_PARCELABLE_FAILED;
653     }
654     ErrCode result = InnerTransact(static_cast<uint32_t>(
655         BackgroundTaskMgrStubInterfaceCode::SET_BGTASK_CONFIG), option, data, reply);
656     if (result != ERR_OK) {
657         BGTASK_LOGE("SetBgTaskConfig fail: transact ErrCode=%{public}d", result);
658         return ERR_BGTASK_TRANSACT_FAILED;
659     }
660     if (!reply.ReadInt32(result)) {
661         BGTASK_LOGE("SetBgTaskConfig fail: read result failed.");
662         return ERR_BGTASK_PARCELABLE_FAILED;
663     }
664     return result;
665 }
666 }  // namespace BackgroundTaskMgr
667 }  // namespace OHOS