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
23 using namespace std;
24
25 namespace OHOS {
26 namespace BackgroundTaskMgr {
BackgroundTaskMgrProxy(const sptr<IRemoteObject> & impl)27 BackgroundTaskMgrProxy::BackgroundTaskMgrProxy(const sptr<IRemoteObject>& impl)
28 :IRemoteProxy<IBackgroundTaskMgr>(impl) {}
~BackgroundTaskMgrProxy()29 BackgroundTaskMgrProxy::~BackgroundTaskMgrProxy() {}
30
RequestSuspendDelay(const std::u16string & reason,const sptr<IExpiredCallback> & callback,std::shared_ptr<DelaySuspendInfo> & delayInfo)31 ErrCode BackgroundTaskMgrProxy::RequestSuspendDelay(const std::u16string& reason,
32 const sptr<IExpiredCallback>& callback, std::shared_ptr<DelaySuspendInfo> &delayInfo)
33 {
34 if (callback == nullptr) {
35 BGTASK_LOGE("RequestSuspendDelay callback is null");
36 return ERR_CALLBACK_NULL_OR_TYPE_ERR;
37 }
38
39 MessageParcel data;
40 MessageParcel reply;
41 MessageOption option = {MessageOption::TF_SYNC};
42 if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
43 BGTASK_LOGE("RequestSuspendDelay write descriptor failed");
44 return ERR_BGTASK_PARCELABLE_FAILED;
45 }
46 if (!data.WriteString16(reason)) {
47 BGTASK_LOGE("RequestSuspendDelay write reason failed");
48 return ERR_BGTASK_PARCELABLE_FAILED;
49 }
50 if (!data.WriteRemoteObject(callback->AsObject())) {
51 BGTASK_LOGE("RequestSuspendDelay write callback failed");
52 return ERR_BGTASK_PARCELABLE_FAILED;
53 }
54 ErrCode result = InnerTransact(REQUEST_SUSPEND_DELAY, option, data, reply);
55 if (result != ERR_OK) {
56 BGTASK_LOGE("RequestSuspendDelay transact ErrCode=%{public}d", result);
57 return ERR_BGTASK_TRANSACT_FAILED;
58 }
59 if (!reply.ReadInt32(result)) {
60 BGTASK_LOGE("RequestSuspendDelay fail: read result failed.");
61 return ERR_BGTASK_PARCELABLE_FAILED;
62 }
63 delayInfo = DelaySuspendInfo::Unmarshalling(reply);
64 if (delayInfo == nullptr) {
65 BGTASK_LOGE("RequestSuspendDelay read result failed");
66 return ERR_BGTASK_PARCELABLE_FAILED;
67 }
68 return result;
69 }
70
CancelSuspendDelay(int32_t requestId)71 ErrCode BackgroundTaskMgrProxy::CancelSuspendDelay(int32_t requestId)
72 {
73 MessageParcel data;
74 MessageParcel reply;
75 MessageOption option = {MessageOption::TF_SYNC};
76 if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
77 BGTASK_LOGE("RequestSuspendDelay write descriptor failed");
78 return ERR_BGTASK_PARCELABLE_FAILED;
79 }
80 if (!data.WriteInt32(requestId)) {
81 BGTASK_LOGE("RequestSuspendDelay write requestId failed");
82 return ERR_BGTASK_PARCELABLE_FAILED;
83 }
84
85 ErrCode result = InnerTransact(CANCEL_SUSPEND_DELAY, option, data, reply);
86 if (result != ERR_OK) {
87 BGTASK_LOGE("RequestSuspendDelay fail: transact ErrCode=%{public}d", result);
88 return ERR_BGTASK_TRANSACT_FAILED;
89 }
90 if (!reply.ReadInt32(result)) {
91 BGTASK_LOGE("RequestSuspendDelay fail: read result failed.");
92 return ERR_BGTASK_PARCELABLE_FAILED;
93 }
94 return result;
95 }
96
GetRemainingDelayTime(int32_t requestId,int32_t & delayTime)97 ErrCode BackgroundTaskMgrProxy::GetRemainingDelayTime(int32_t requestId, int32_t &delayTime)
98 {
99 MessageParcel data;
100 MessageParcel reply;
101 MessageOption option = {MessageOption::TF_SYNC};
102 if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
103 BGTASK_LOGE(" BackgroundTaskMgrProxy::%{public}s, write descriptor failed", __func__);
104 return ERR_BGTASK_PARCELABLE_FAILED;
105 }
106 if (!data.WriteInt32(requestId)) {
107 BGTASK_LOGE(" BackgroundTaskMgrProxy::%{public}s, write requestId failed", __func__);
108 return ERR_BGTASK_PARCELABLE_FAILED;
109 }
110
111 ErrCode result = InnerTransact(GET_REMAINING_DELAY_TIME, option, data, reply);
112 if (result != ERR_OK) {
113 BGTASK_LOGE("GetRemainingDelayTime fail: transact ErrCode=%{public}d", result);
114 return ERR_BGTASK_TRANSACT_FAILED;
115 }
116 if (!reply.ReadInt32(result)) {
117 BGTASK_LOGE("GetRemainingDelayTime fail: read result failed.");
118 return ERR_BGTASK_PARCELABLE_FAILED;
119 }
120 if (!reply.ReadInt32(delayTime)) {
121 BGTASK_LOGE("GetRemainingDelayTime fail: read result failed.");
122 return ERR_BGTASK_PARCELABLE_FAILED;
123 }
124 return result;
125 }
126
StartBackgroundRunning(const sptr<ContinuousTaskParam> & taskParam)127 ErrCode BackgroundTaskMgrProxy::StartBackgroundRunning(const sptr<ContinuousTaskParam> &taskParam)
128 {
129 if (taskParam == nullptr) {
130 return ERR_BGTASK_INVALID_PARAM;
131 }
132
133 MessageParcel data;
134 if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
135 return ERR_BGTASK_PARCELABLE_FAILED;
136 }
137
138 if (!data.WriteParcelable(taskParam)) {
139 return ERR_BGTASK_PARCELABLE_FAILED;
140 }
141
142 MessageParcel reply;
143 MessageOption option = {MessageOption::TF_SYNC};
144
145 ErrCode result = InnerTransact(START_BACKGROUND_RUNNING, option, data, reply);
146 if (result != ERR_OK) {
147 BGTASK_LOGE("StartBackgroundRunning fail: transact ErrCode=%{public}d", result);
148 return ERR_BGTASK_TRANSACT_FAILED;
149 }
150 if (!reply.ReadInt32(result)) {
151 BGTASK_LOGE("StartBackgroundRunning fail: read result failed.");
152 return ERR_BGTASK_PARCELABLE_FAILED;
153 }
154 return result;
155 }
156
StopBackgroundRunning(const std::string & abilityName,const sptr<IRemoteObject> & abilityToken)157 ErrCode BackgroundTaskMgrProxy::StopBackgroundRunning(const std::string &abilityName,
158 const sptr<IRemoteObject> &abilityToken)
159 {
160 MessageParcel data;
161 if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
162 return ERR_BGTASK_PARCELABLE_FAILED;
163 }
164
165 std::u16string u16AbilityName = Str8ToStr16(abilityName);
166 if (!data.WriteString16(u16AbilityName)) {
167 BGTASK_LOGE("StopBackgroundRunning parcel ability Name failed");
168 return ERR_BGTASK_PARCELABLE_FAILED;
169 }
170
171 if (!data.WriteRemoteObject(abilityToken)) {
172 BGTASK_LOGE("StopBackgroundRunning parcel ability token failed");
173 return ERR_BGTASK_PARCELABLE_FAILED;
174 }
175
176 MessageParcel reply;
177 MessageOption option = {MessageOption::TF_SYNC};
178
179 ErrCode result = InnerTransact(STOP_BACKGROUND_RUNNING, option, data, reply);
180 if (result != ERR_OK) {
181 BGTASK_LOGE("StopBackgroundRunning transact ErrCode=%{public}d", result);
182 return ERR_BGTASK_TRANSACT_FAILED;
183 }
184 if (!reply.ReadInt32(result)) {
185 BGTASK_LOGE("StopBackgroundRunning read result failed.");
186 return ERR_BGTASK_PARCELABLE_FAILED;
187 }
188 return result;
189 }
190
SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber> & subscriber)191 ErrCode BackgroundTaskMgrProxy::SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber)
192 {
193 if (subscriber == nullptr) {
194 BGTASK_LOGE("SubscribeBackgroundTask subscriber is null");
195 return ERR_BGTASK_PARCELABLE_FAILED;
196 }
197
198 MessageParcel data;
199 MessageParcel reply;
200 MessageOption option = {MessageOption::TF_SYNC};
201 if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
202 BGTASK_LOGE("SubscribeBackgroundTask write descriptor failed");
203 return ERR_BGTASK_PARCELABLE_FAILED;
204 }
205 if (!data.WriteRemoteObject(subscriber->AsObject())) {
206 BGTASK_LOGE("SubscribeBackgroundTask write subscriber failed");
207 return ERR_BGTASK_PARCELABLE_FAILED;
208 }
209
210 ErrCode result = InnerTransact(SUBSCRIBE_BACKGROUND_TASK, option, data, reply);
211 if (result != ERR_OK) {
212 BGTASK_LOGE("SubscribeBackgroundTask fail: transact ErrCode=%{public}d", result);
213 return ERR_BGTASK_TRANSACT_FAILED;
214 }
215 if (!reply.ReadInt32(result)) {
216 BGTASK_LOGE("SubscribeBackgroundTask fail: read result failed.");
217 return ERR_BGTASK_PARCELABLE_FAILED;
218 }
219 return result;
220 }
221
UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber> & subscriber)222 ErrCode BackgroundTaskMgrProxy::UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber)
223 {
224 if (subscriber == nullptr) {
225 BGTASK_LOGE("UnsubscribeBackgroundTask subscriber is null");
226 return ERR_BGTASK_PARCELABLE_FAILED;
227 }
228
229 MessageParcel data;
230 MessageParcel reply;
231 MessageOption option = {MessageOption::TF_SYNC};
232 if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
233 BGTASK_LOGE("UnsubscribeBackgroundTask write descriptor failed");
234 return ERR_BGTASK_PARCELABLE_FAILED;
235 }
236 if (!data.WriteRemoteObject(subscriber->AsObject())) {
237 BGTASK_LOGE("UnsubscribeBackgroundTask write subscriber failed");
238 return ERR_BGTASK_PARCELABLE_FAILED;
239 }
240
241 ErrCode result = InnerTransact(UNSUBSCRIBE_BACKGROUND_TASK, option, data, reply);
242 if (result != ERR_OK) {
243 BGTASK_LOGE("UnsubscribeBackgroundTask fail: transact ErrCode=%{public}d", result);
244 return ERR_BGTASK_TRANSACT_FAILED;
245 }
246 if (!reply.ReadInt32(result)) {
247 BGTASK_LOGE("UnsubscribeBackgroundTask fail: read result failed.");
248 return ERR_BGTASK_PARCELABLE_FAILED;
249 }
250 return result;
251 }
252
GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> & list)253 ErrCode BackgroundTaskMgrProxy::GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list)
254 {
255 MessageParcel data;
256 MessageParcel reply;
257 MessageOption option = {MessageOption::TF_SYNC};
258 if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
259 BGTASK_LOGE("GetTransientTaskApps write descriptor failed");
260 return ERR_BGTASK_PARCELABLE_FAILED;
261 }
262
263 ErrCode result = InnerTransact(GET_TRANSIENT_TASK_APPS, option, data, reply);
264 if (result != ERR_OK) {
265 BGTASK_LOGE("GetTransientTaskApps fail: transact ErrCode=%{public}d", result);
266 return ERR_BGTASK_TRANSACT_FAILED;
267 }
268 if (!reply.ReadInt32(result)) {
269 BGTASK_LOGE("GetTransientTaskApps fail: read result failed.");
270 return ERR_BGTASK_PARCELABLE_FAILED;
271 }
272
273 int32_t infoSize = reply.ReadInt32();
274 for (int32_t i = 0; i < infoSize; i++) {
275 auto info = TransientTaskAppInfo::Unmarshalling(reply);
276 if (!info) {
277 BGTASK_LOGE("GetTransientTaskApps Read Parcelable infos failed.");
278 return ERR_BGTASK_PARCELABLE_FAILED;
279 }
280 list.emplace_back(info);
281 }
282
283 return result;
284 }
285
GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> & list)286 ErrCode BackgroundTaskMgrProxy::GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list)
287 {
288 MessageParcel data;
289 MessageParcel reply;
290 MessageOption option = {MessageOption::TF_SYNC};
291 if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
292 BGTASK_LOGE("GetContinuousTaskApps write descriptor failed");
293 return ERR_BGTASK_PARCELABLE_FAILED;
294 }
295
296 ErrCode result = InnerTransact(GET_CONTINUOUS_TASK_APPS, option, data, reply);
297 if (result != ERR_OK) {
298 BGTASK_LOGE("GetContinuousTaskApps fail: transact ErrCode=%{public}d", result);
299 return ERR_BGTASK_TRANSACT_FAILED;
300 }
301 if (!reply.ReadInt32(result)) {
302 BGTASK_LOGE("GetContinuousTaskApps fail: read result failed.");
303 return ERR_BGTASK_PARCELABLE_FAILED;
304 }
305
306 if (result != ERR_OK) {
307 BGTASK_LOGE("GetContinuousTaskApps failed.");
308 return result;
309 }
310
311 int32_t infoSize = reply.ReadInt32();
312 for (int32_t i = 0; i < infoSize; i++) {
313 auto info = ContinuousTaskCallbackInfo::Unmarshalling(reply);
314 if (!info) {
315 BGTASK_LOGE("GetContinuousTaskApps Read Parcelable infos failed.");
316 return ERR_BGTASK_PARCELABLE_FAILED;
317 }
318 list.emplace_back(info);
319 }
320
321 return result;
322 }
323
StopContinuousTask(int32_t uid,int32_t pid,uint32_t taskType)324 ErrCode BackgroundTaskMgrProxy::StopContinuousTask(int32_t uid, int32_t pid, uint32_t taskType)
325 {
326 MessageParcel data;
327 if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
328 return ERR_BGTASK_PARCELABLE_FAILED;
329 }
330
331 if (!data.WriteInt32(uid)) {
332 BGTASK_LOGE("StopContinuousTask parcel uid failed");
333 return ERR_BGTASK_PARCELABLE_FAILED;
334 }
335
336 if (!data.WriteInt32(pid)) {
337 BGTASK_LOGE("StopContinuousTask parcel pid failed");
338 return ERR_BGTASK_PARCELABLE_FAILED;
339 }
340
341 if (!data.WriteUint32(taskType)) {
342 BGTASK_LOGE("StopContinuousTask parcel taskType failed");
343 return ERR_BGTASK_PARCELABLE_FAILED;
344 }
345
346 MessageParcel reply;
347 MessageOption option = {MessageOption::TF_SYNC};
348
349 ErrCode result = InnerTransact(STOP_CONTINUOUS_TASK, option, data, reply);
350 if (result != ERR_OK) {
351 BGTASK_LOGE("StopContinuousTask transact ErrCode=%{public}d", result);
352 return ERR_BGTASK_TRANSACT_FAILED;
353 }
354 if (!reply.ReadInt32(result)) {
355 BGTASK_LOGE("StopContinuousTask read result failed.");
356 return ERR_BGTASK_PARCELABLE_FAILED;
357 }
358 return result;
359 }
360
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)361 ErrCode BackgroundTaskMgrProxy::InnerTransact(uint32_t code, MessageOption &flags,
362 MessageParcel &data, MessageParcel &reply)
363 {
364 auto remote = Remote();
365 if (remote == nullptr) {
366 BGTASK_LOGE("InnerTransact get Remote fail code %{public}d", code);
367 return ERR_DEAD_OBJECT;
368 }
369 int32_t err = remote->SendRequest(code, data, reply, flags);
370 switch (err) {
371 case NO_ERROR: {
372 return ERR_OK;
373 }
374 case DEAD_OBJECT: {
375 BGTASK_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}u", err, code);
376 return ERR_DEAD_OBJECT;
377 }
378 default: {
379 BGTASK_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}u", err, code);
380 return ERR_BGTASK_TRANSACT_FAILED;
381 }
382 }
383 }
384
ApplyEfficiencyResources(const sptr<EfficiencyResourceInfo> & resourceInfo)385 ErrCode BackgroundTaskMgrProxy::ApplyEfficiencyResources(const sptr<EfficiencyResourceInfo> &resourceInfo)
386 {
387 if (resourceInfo == nullptr) {
388 return ERR_BGTASK_INVALID_PARAM;
389 }
390
391 MessageParcel data;
392 MessageParcel reply;
393 MessageOption option = {MessageOption::TF_SYNC};
394 if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
395 BGTASK_LOGE("ApplyEfficiencyResources write descriptor failed");
396 return ERR_BGTASK_PARCELABLE_FAILED;
397 }
398
399 if (!data.WriteParcelable(resourceInfo)) {
400 return ERR_BGTASK_PARCELABLE_FAILED;
401 }
402 BGTASK_LOGD("start send data in apply res function from bgtask proxy");
403 ErrCode result = InnerTransact(APPLY_EFFICIENCY_RESOURCES, option, data, reply);
404 if (result != ERR_OK) {
405 BGTASK_LOGE("ApplyEfficiencyResources fail: transact ErrCode=%{public}d", result);
406 return ERR_BGTASK_TRANSACT_FAILED;
407 }
408 if (!reply.ReadInt32(result)) {
409 BGTASK_LOGE("ApplyEfficiencyResources fail: read result failed.");
410 return ERR_BGTASK_PARCELABLE_FAILED;
411 }
412 return result;
413 }
414
ResetAllEfficiencyResources()415 ErrCode BackgroundTaskMgrProxy::ResetAllEfficiencyResources()
416 {
417 MessageParcel data;
418 MessageParcel reply;
419 MessageOption option = {MessageOption::TF_SYNC};
420 if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
421 BGTASK_LOGE("ResetAllEfficiencyResources write descriptor failed");
422 return ERR_BGTASK_PARCELABLE_FAILED;
423 }
424 BGTASK_LOGD("start send data in reset all res function from bgtask proxy");
425 ErrCode result = InnerTransact(RESET_ALL_EFFICIENCY_RESOURCES, option, data, reply);
426 if (result != ERR_OK) {
427 BGTASK_LOGE("ResetAllEfficiencyResources fail: transact ErrCode=%{public}d", result);
428 return ERR_BGTASK_TRANSACT_FAILED;
429 }
430 if (!reply.ReadInt32(result)) {
431 BGTASK_LOGE("ResetAllEfficiencyResources fail: read result failed.");
432 return ERR_BGTASK_PARCELABLE_FAILED;
433 }
434 return result;
435 }
436
GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> & appList,std::vector<std::shared_ptr<ResourceCallbackInfo>> & procList)437 ErrCode BackgroundTaskMgrProxy::GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<
438 ResourceCallbackInfo>> &appList, std::vector<std::shared_ptr<ResourceCallbackInfo>> &procList)
439 {
440 MessageParcel data;
441 MessageParcel reply;
442 MessageOption option = {MessageOption::TF_SYNC};
443 if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
444 BGTASK_LOGE("GetEfficiencyResourcesInfos write descriptor failed");
445 return ERR_BGTASK_PARCELABLE_FAILED;
446 }
447
448 ErrCode result = InnerTransact(GET_EFFICIENCY_RESOURCES_INFOS, option, data, reply);
449 if (result != ERR_OK) {
450 BGTASK_LOGE("GetEfficiencyResourcesInfos fail: transact ErrCode=%{public}d", result);
451 return ERR_BGTASK_TRANSACT_FAILED;
452 }
453 if (!reply.ReadInt32(result)) {
454 BGTASK_LOGE("GetEfficiencyResourcesInfos fail: read result failed.");
455 return ERR_BGTASK_PARCELABLE_FAILED;
456 }
457
458 if (result != ERR_OK) {
459 return result;
460 }
461
462 int32_t infoSize = reply.ReadInt32();
463 for (int32_t i = 0; i < infoSize; i++) {
464 auto info = ResourceCallbackInfo::Unmarshalling(reply);
465 if (!info) {
466 BGTASK_LOGE("GetContinuousTaskApps Read Parcelable infos failed.");
467 return ERR_BGTASK_PARCELABLE_FAILED;
468 }
469 appList.emplace_back(info);
470 }
471
472 infoSize = reply.ReadInt32();
473 for (int32_t i = 0; i < infoSize; i++) {
474 auto info = ResourceCallbackInfo::Unmarshalling(reply);
475 if (!info) {
476 BGTASK_LOGE("GetContinuousTaskApps Read Parcelable infos failed.");
477 return ERR_BGTASK_PARCELABLE_FAILED;
478 }
479 procList.emplace_back(info);
480 }
481
482 return result;
483 }
484 } // namespace BackgroundTaskMgr
485 } // namespace OHOS