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_subscriber_proxy.h"
17 #include "ibackground_task_subscriber_ipc_interface_code.h"
18
19 #include <message_parcel.h>
20
21 #include "transient_task_log.h"
22
23 namespace OHOS {
24 namespace BackgroundTaskMgr {
BackgroundTaskSubscriberProxy(const sptr<IRemoteObject> & impl)25 BackgroundTaskSubscriberProxy::BackgroundTaskSubscriberProxy(const sptr<IRemoteObject>& impl)
26 : IRemoteProxy<IBackgroundTaskSubscriber>(impl) {}
~BackgroundTaskSubscriberProxy()27 BackgroundTaskSubscriberProxy::~BackgroundTaskSubscriberProxy() {}
28
OnConnected()29 void BackgroundTaskSubscriberProxy::OnConnected()
30 {
31 sptr<IRemoteObject> remote = Remote();
32 if (remote == nullptr) {
33 BGTASK_LOGE("OnConnected remote is dead.");
34 return;
35 }
36 MessageParcel data;
37 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
38 BGTASK_LOGE("OnConnected write interface token failed.");
39 return;
40 }
41
42 MessageParcel reply;
43 MessageOption option = {MessageOption::TF_ASYNC};
44 int32_t ret = remote->SendRequest(
45 static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_CONNECTED), data, reply, option);
46 if (ret!= ERR_OK) {
47 BGTASK_LOGE("OnConnected SendRequest failed, error code: %d", ret);
48 }
49 }
50
OnDisconnected()51 void BackgroundTaskSubscriberProxy::OnDisconnected()
52 {
53 sptr<IRemoteObject> remote = Remote();
54 if (remote == nullptr) {
55 BGTASK_LOGE("OnDisconnected remote is dead.");
56 return;
57 }
58 MessageParcel data;
59 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
60 BGTASK_LOGE("OnDisconnected write interface token failed.");
61 return;
62 }
63
64 MessageParcel reply;
65 MessageOption option = {MessageOption::TF_ASYNC};
66 int32_t ret = remote->SendRequest(
67 static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_DISCONNECTED), data, reply, option);
68 if (ret != ERR_OK) {
69 BGTASK_LOGE("OnDisconnected SendRequest failed, error code: %d", ret);
70 }
71 }
72
OnTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo> & info)73 void BackgroundTaskSubscriberProxy::OnTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo>& info)
74 {
75 sptr<IRemoteObject> remote = Remote();
76 if (remote == nullptr) {
77 BGTASK_LOGE("OnTransientTaskStart remote is dead.");
78 return;
79 }
80 if (info == nullptr) {
81 BGTASK_LOGE("OnTransientTaskStart TransientTaskAppInfo is null.");
82 return;
83 }
84
85 MessageParcel data;
86 bool res = data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor());
87 if (!res) {
88 BGTASK_LOGE("OnTransientTaskStart write descriptor failed.");
89 return;
90 }
91 if (!info->Marshalling(data)) {
92 BGTASK_LOGE("OnTransientTaskStart write parcel failed.");
93 return;
94 }
95 MessageParcel reply;
96 MessageOption option = {MessageOption::TF_ASYNC};
97 int32_t ret = remote->SendRequest(
98 static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_TRANSIENT_TASK_START), data, reply, option);
99 if (ret != ERR_NONE) {
100 BGTASK_LOGE("OnTransientTaskStart SendRequest failed, error code: %{public}d", ret);
101 }
102 }
103
OnTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo> & info)104 void BackgroundTaskSubscriberProxy::OnTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo>& info)
105 {
106 sptr<IRemoteObject> remote = Remote();
107 if (remote == nullptr) {
108 BGTASK_LOGE("remote is dead.");
109 return;
110 }
111 if (info == nullptr) {
112 BGTASK_LOGE("OnTransientTaskEnd TransientTaskAppInfo is null.");
113 return;
114 }
115
116 MessageParcel data;
117 bool res = data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor());
118 if (!res) {
119 BGTASK_LOGE("OnTransientTaskEnd write descriptor failed.");
120 return;
121 }
122 if (!info->Marshalling(data)) {
123 BGTASK_LOGE("OnTransientTaskEnd write parcel failed.");
124 return;
125 }
126 MessageParcel reply;
127 MessageOption option = {MessageOption::TF_ASYNC};
128 int32_t ret = remote->SendRequest(
129 static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_TRANSIENT_TASK_END), data, reply, option);
130 if (ret != ERR_NONE) {
131 BGTASK_LOGE("OnTransientTaskEnd SendRequest failed, error code: %{public}d", ret);
132 }
133 }
134
OnTransientTaskErr(const std::shared_ptr<TransientTaskAppInfo> & info)135 void BackgroundTaskSubscriberProxy::OnTransientTaskErr(const std::shared_ptr<TransientTaskAppInfo>& info)
136 {
137 sptr<IRemoteObject> remote = Remote();
138 if (remote == nullptr) {
139 BGTASK_LOGE("remote is dead.");
140 return;
141 }
142 if (info == nullptr) {
143 BGTASK_LOGE("OnTransientTaskErr TransientTaskAppInfo is null.");
144 return;
145 }
146
147 MessageParcel data;
148 bool res = data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor());
149 if (!res) {
150 BGTASK_LOGE("OnTransientTaskErr write descriptor failed.");
151 return;
152 }
153 if (!info->Marshalling(data)) {
154 BGTASK_LOGE("OnTransientTaskErr write parcel failed.");
155 return;
156 }
157 MessageParcel reply;
158 MessageOption option = {MessageOption::TF_ASYNC};
159 int32_t ret = remote->SendRequest(
160 static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_TRANSIENT_TASK_ERR), data, reply, option);
161 if (ret != ERR_NONE) {
162 BGTASK_LOGE("OnTransientTaskErr SendRequest failed, error code: %{public}d", ret);
163 }
164 }
165
OnAppTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo> & info)166 void BackgroundTaskSubscriberProxy::OnAppTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo>& info)
167 {
168 sptr<IRemoteObject> remote = Remote();
169 if (remote == nullptr) {
170 BGTASK_LOGE("OnAppTransientTaskStart remote is dead.");
171 return;
172 }
173 if (info == nullptr) {
174 BGTASK_LOGE("OnAppTransientTaskStart TransientTaskAppInfo is null.");
175 return;
176 }
177
178 MessageParcel data;
179 bool res = data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor());
180 if (!res) {
181 BGTASK_LOGE("OnAppTransientTaskStart write descriptor failed.");
182 return;
183 }
184 if (!info->Marshalling(data)) {
185 BGTASK_LOGE("OnAppTransientTaskStart write parcel failed.");
186 return;
187 }
188 MessageParcel reply;
189 MessageOption option = {MessageOption::TF_ASYNC};
190 int32_t ret = remote->SendRequest(static_cast<uint32_t>(
191 IBackgroundTaskSubscriberInterfaceCode::ON_APP_TRANSIENT_TASK_START), data, reply, option);
192 if (ret != ERR_NONE) {
193 BGTASK_LOGE("OnAppTransientTaskStart SendRequest failed, error code: %{public}d", ret);
194 }
195 }
196
OnAppTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo> & info)197 void BackgroundTaskSubscriberProxy::OnAppTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo>& info)
198 {
199 sptr<IRemoteObject> remote = Remote();
200 if (remote == nullptr) {
201 BGTASK_LOGE("OnAppTransientTaskEnd remote is dead.");
202 return;
203 }
204 if (info == nullptr) {
205 BGTASK_LOGE("OnAppTransientTaskEnd TransientTaskAppInfo is null.");
206 return;
207 }
208
209 MessageParcel data;
210 bool res = data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor());
211 if (!res) {
212 BGTASK_LOGE("OnAppTransientTaskEnd write descriptor failed.");
213 return;
214 }
215 if (!info->Marshalling(data)) {
216 BGTASK_LOGE("OnAppTransientTaskEnd write parcel failed.");
217 return;
218 }
219 MessageParcel reply;
220 MessageOption option = {MessageOption::TF_ASYNC};
221 int32_t ret = remote->SendRequest(
222 static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_APP_TRANSIENT_TASK_END), data, reply, option);
223 if (ret != ERR_NONE) {
224 BGTASK_LOGE("OnAppTransientTaskEndSendRequest failed, error code: %{public}d", ret);
225 }
226 }
227
OnContinuousTaskStart(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)228 void BackgroundTaskSubscriberProxy::OnContinuousTaskStart(
229 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo)
230 {
231 sptr<IRemoteObject> remote = Remote();
232 if (remote == nullptr) {
233 BGTASK_LOGE("OnContinuousTaskStart remote is dead.");
234 return;
235 }
236 if (continuousTaskCallbackInfo == nullptr) {
237 BGTASK_LOGE("OnContinuousTaskStart continuousTaskCallbackInfo is nullptr.");
238 return;
239 }
240
241 MessageParcel data;
242 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
243 BGTASK_LOGE("OnContinuousTaskStart write interface token failed.");
244 return;
245 }
246
247 if (!data.WriteParcelable(continuousTaskCallbackInfo.get())) {
248 BGTASK_LOGE("OnContinuousTaskStart write continuousTaskCallbackInfo failed.");
249 return;
250 }
251
252 MessageParcel reply;
253 MessageOption option = {MessageOption::TF_ASYNC};
254 int32_t result = remote->SendRequest(
255 static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_CONTINUOUS_TASK_START), data, reply, option);
256 if (result != ERR_OK) {
257 BGTASK_LOGE("OnContinuousTaskStart SendRequest error");
258 }
259 }
260
261
OnContinuousTaskUpdate(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)262 void BackgroundTaskSubscriberProxy::OnContinuousTaskUpdate(
263 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo)
264 {
265 sptr<IRemoteObject> remote = Remote();
266 if (remote == nullptr) {
267 BGTASK_LOGE("OnContinuousTaskUpdate remote is dead.");
268 return;
269 }
270 if (continuousTaskCallbackInfo == nullptr) {
271 BGTASK_LOGE("OnContinuousTaskUpdate continuousTaskCallbackInfo is nullptr.");
272 return;
273 }
274
275 MessageParcel data;
276 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
277 BGTASK_LOGE("OnContinuousTaskUpdate write interface token failed.");
278 return;
279 }
280
281 if (!data.WriteParcelable(continuousTaskCallbackInfo.get())) {
282 BGTASK_LOGE("OnContinuousTaskUpdate write continuousTaskCallbackInfo failed.");
283 return;
284 }
285
286 MessageParcel reply;
287 MessageOption option = {MessageOption::TF_ASYNC};
288 int32_t result = remote->SendRequest(
289 static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_CONTINUOUS_TASK_UPDATE), data, reply, option);
290 if (result != ERR_OK) {
291 BGTASK_LOGE("OnContinuousTaskUpdate SendRequest error");
292 }
293 }
294
OnContinuousTaskStop(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)295 void BackgroundTaskSubscriberProxy::OnContinuousTaskStop(
296 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo)
297 {
298 sptr<IRemoteObject> remote = Remote();
299 if (remote == nullptr) {
300 BGTASK_LOGE("OnContinuousTaskStop remote is dead.");
301 return;
302 }
303 if (continuousTaskCallbackInfo == nullptr) {
304 BGTASK_LOGE("OnContinuousTaskStop continuousTaskCallbackInfo is nullptr.");
305 return;
306 }
307
308 MessageParcel data;
309 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
310 BGTASK_LOGE("OnContinuousTaskStop write interface token failed.");
311 return;
312 }
313
314 if (!data.WriteParcelable(continuousTaskCallbackInfo.get())) {
315 BGTASK_LOGE("OnContinuousTaskStop write notification failed.");
316 return;
317 }
318
319 MessageParcel reply;
320 MessageOption option = {MessageOption::TF_ASYNC};
321 int32_t result = remote->SendRequest(
322 static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_CONTINUOUS_TASK_STOP), data, reply, option);
323 if (result != ERR_OK) {
324 BGTASK_LOGE("OnContinuousTaskStop SendRequest error");
325 }
326 }
327
OnAppContinuousTaskStop(int32_t uid)328 void BackgroundTaskSubscriberProxy::OnAppContinuousTaskStop(int32_t uid)
329 {
330 sptr<IRemoteObject> remote = Remote();
331 if (remote == nullptr) {
332 BGTASK_LOGE("OnAppContinuousTaskStop remote is dead.");
333 return;
334 }
335
336 MessageParcel data;
337 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
338 BGTASK_LOGE("OnAppContinuousTaskStop write interface token failed.");
339 return;
340 }
341
342 if (!data.WriteInt32(uid)) {
343 BGTASK_LOGE("OnAppContinuousTaskStop write uid failed.");
344 return;
345 }
346
347 MessageParcel reply;
348 MessageOption option = {MessageOption::TF_ASYNC};
349 int32_t result = remote->SendRequest(static_cast<uint32_t>(
350 IBackgroundTaskSubscriberInterfaceCode::ON_APP_CONTINUOUS_TASK_STOP), data, reply, option);
351 if (result != ERR_OK) {
352 BGTASK_LOGE("OnAppContinuousTaskStop SendRequest error");
353 }
354 }
355
356
OnAppEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)357 void BackgroundTaskSubscriberProxy::OnAppEfficiencyResourcesApply(
358 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo)
359 {
360 sptr<IRemoteObject> remote = Remote();
361 if (remote == nullptr) {
362 BGTASK_LOGE("OnAppEfficiencyResourcesApply remote is dead.");
363 return;
364 }
365 if (resourceInfo == nullptr) {
366 BGTASK_LOGE("OnAppEfficiencyResourcesApply resourceInfo is nullptr.");
367 return;
368 }
369
370 MessageParcel data;
371 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
372 BGTASK_LOGE("OnAppEfficiencyResourcesApply write interface token failed.");
373 return;
374 }
375
376 if (!data.WriteParcelable(resourceInfo.get())) {
377 BGTASK_LOGE("OnAppEfficiencyResourcesApply write notification failed.");
378 return;
379 }
380
381 MessageParcel reply;
382 MessageOption option;
383 int32_t result = remote->SendRequest(static_cast<uint32_t>(
384 IBackgroundTaskSubscriberInterfaceCode::ON_APP_EFFICIENCY_RESOURCES_APPLY), data, reply, option);
385 if (result != ERR_OK) {
386 BGTASK_LOGE("OnAppEfficiencyResourcesApply SendRequest error");
387 }
388 }
389
OnAppEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)390 void BackgroundTaskSubscriberProxy::OnAppEfficiencyResourcesReset(
391 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo)
392 {
393 sptr<IRemoteObject> remote = Remote();
394 if (remote == nullptr) {
395 BGTASK_LOGE("OnAppEfficiencyResourcesReset remote is dead.");
396 return;
397 }
398
399 if (resourceInfo == nullptr) {
400 BGTASK_LOGE("OnAppEfficiencyResourcesReset resourceInfo is nullptr.");
401 return;
402 }
403
404 MessageParcel data;
405 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
406 BGTASK_LOGE("OnAppEfficiencyResourcesReset write interface token failed.");
407 return;
408 }
409
410 if (!data.WriteParcelable(resourceInfo.get())) {
411 BGTASK_LOGE("OnAppEfficiencyResourcesReset write notification failed.");
412 return;
413 }
414
415 MessageParcel reply;
416 MessageOption option;
417 int32_t result = remote->SendRequest(static_cast<uint32_t>(
418 IBackgroundTaskSubscriberInterfaceCode::ON_APP_EFFICIENCY_RESOURCES_RESET), data, reply, option);
419 if (result != ERR_OK) {
420 BGTASK_LOGE("OnAppEfficiencyResourcesReset SendRequest error");
421 }
422 }
423
OnProcEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)424 void BackgroundTaskSubscriberProxy::OnProcEfficiencyResourcesApply(
425 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo)
426 {
427 sptr<IRemoteObject> remote = Remote();
428 if (remote == nullptr) {
429 BGTASK_LOGE("OnProcEfficiencyResourcesApply remote is dead.");
430 return;
431 }
432 if (resourceInfo == nullptr) {
433 BGTASK_LOGE("OnProcEfficiencyResourcesApply resourceInfo is nullptr.");
434 return;
435 }
436
437 MessageParcel data;
438 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
439 BGTASK_LOGE("OnProcEfficiencyResourcesApply write interface token failed.");
440 return;
441 }
442
443 if (!data.WriteParcelable(resourceInfo.get())) {
444 BGTASK_LOGE("OnProcEfficiencyResourcesApply write notification failed.");
445 return;
446 }
447
448 MessageParcel reply;
449 MessageOption option;
450 int32_t result = remote->SendRequest(static_cast<uint32_t>(
451 IBackgroundTaskSubscriberInterfaceCode::ON_PROC_EFFICIENCY_RESOURCES_APPLY), data, reply, option);
452 if (result != ERR_OK) {
453 BGTASK_LOGE("OnProcEfficiencyResourcesApply SendRequest error");
454 }
455 }
456
OnProcEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)457 void BackgroundTaskSubscriberProxy::OnProcEfficiencyResourcesReset(
458 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo)
459 {
460 sptr<IRemoteObject> remote = Remote();
461 if (remote == nullptr) {
462 BGTASK_LOGE("OnProcEfficiencyResourcesReset remote is dead.");
463 return;
464 }
465
466 if (resourceInfo == nullptr) {
467 BGTASK_LOGE("OnProcEfficiencyResourcesReset resourceInfo is nullptr.");
468 return;
469 }
470
471 MessageParcel data;
472 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
473 BGTASK_LOGE("OnProcEfficiencyResourcesReset write interface token failed.");
474 return;
475 }
476
477 if (!data.WriteParcelable(resourceInfo.get())) {
478 BGTASK_LOGE("OnProcEfficiencyResourcesReset write notification failed.");
479 return;
480 }
481
482 MessageParcel reply;
483 MessageOption option;
484 int32_t result = remote->SendRequest(static_cast<uint32_t>(
485 IBackgroundTaskSubscriberInterfaceCode::ON_PROC_EFFICIENCY_RESOURCES_RESET), data, reply, option);
486 if (result != ERR_OK) {
487 BGTASK_LOGE("OnProcEfficiencyResourcesReset SendRequest error");
488 }
489 }
490 } // namespace BackgroundTaskMgr
491 } // namespace OHOS