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
OnAppTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo> & info)135 void BackgroundTaskSubscriberProxy::OnAppTransientTaskStart(const std::shared_ptr<TransientTaskAppInfo>& info)
136 {
137 sptr<IRemoteObject> remote = Remote();
138 if (remote == nullptr) {
139 BGTASK_LOGE("OnAppTransientTaskStart remote is dead.");
140 return;
141 }
142 if (info == nullptr) {
143 BGTASK_LOGE("OnAppTransientTaskStart TransientTaskAppInfo is null.");
144 return;
145 }
146
147 MessageParcel data;
148 bool res = data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor());
149 if (!res) {
150 BGTASK_LOGE("OnAppTransientTaskStart write descriptor failed.");
151 return;
152 }
153 if (!info->Marshalling(data)) {
154 BGTASK_LOGE("OnAppTransientTaskStart write parcel failed.");
155 return;
156 }
157 MessageParcel reply;
158 MessageOption option = {MessageOption::TF_ASYNC};
159 int32_t ret = remote->SendRequest(static_cast<uint32_t>(
160 IBackgroundTaskSubscriberInterfaceCode::ON_APP_TRANSIENT_TASK_START), data, reply, option);
161 if (ret != ERR_NONE) {
162 BGTASK_LOGE("OnAppTransientTaskStart SendRequest failed, error code: %{public}d", ret);
163 }
164 }
165
OnAppTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo> & info)166 void BackgroundTaskSubscriberProxy::OnAppTransientTaskEnd(const std::shared_ptr<TransientTaskAppInfo>& info)
167 {
168 sptr<IRemoteObject> remote = Remote();
169 if (remote == nullptr) {
170 BGTASK_LOGE("OnAppTransientTaskEnd remote is dead.");
171 return;
172 }
173 if (info == nullptr) {
174 BGTASK_LOGE("OnAppTransientTaskEnd TransientTaskAppInfo is null.");
175 return;
176 }
177
178 MessageParcel data;
179 bool res = data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor());
180 if (!res) {
181 BGTASK_LOGE("OnAppTransientTaskEnd write descriptor failed.");
182 return;
183 }
184 if (!info->Marshalling(data)) {
185 BGTASK_LOGE("OnAppTransientTaskEnd write parcel failed.");
186 return;
187 }
188 MessageParcel reply;
189 MessageOption option = {MessageOption::TF_ASYNC};
190 int32_t ret = remote->SendRequest(
191 static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_APP_TRANSIENT_TASK_END), data, reply, option);
192 if (ret != ERR_NONE) {
193 BGTASK_LOGE("OnAppTransientTaskEndSendRequest failed, error code: %{public}d", ret);
194 }
195 }
196
OnContinuousTaskStart(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)197 void BackgroundTaskSubscriberProxy::OnContinuousTaskStart(
198 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo)
199 {
200 sptr<IRemoteObject> remote = Remote();
201 if (remote == nullptr) {
202 BGTASK_LOGE("OnContinuousTaskStart remote is dead.");
203 return;
204 }
205 if (continuousTaskCallbackInfo == nullptr) {
206 BGTASK_LOGE("OnContinuousTaskStart continuousTaskCallbackInfo is nullptr.");
207 return;
208 }
209
210 MessageParcel data;
211 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
212 BGTASK_LOGE("OnContinuousTaskStart write interface token failed.");
213 return;
214 }
215
216 if (!data.WriteParcelable(continuousTaskCallbackInfo.get())) {
217 BGTASK_LOGE("OnContinuousTaskStart write continuousTaskCallbackInfo failed.");
218 return;
219 }
220
221 MessageParcel reply;
222 MessageOption option = {MessageOption::TF_ASYNC};
223 int32_t result = remote->SendRequest(
224 static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_CONTINUOUS_TASK_START), data, reply, option);
225 if (result != ERR_OK) {
226 BGTASK_LOGE("OnContinuousTaskStart SendRequest error");
227 }
228 }
229
OnContinuousTaskStop(const std::shared_ptr<ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)230 void BackgroundTaskSubscriberProxy::OnContinuousTaskStop(
231 const std::shared_ptr<ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo)
232 {
233 sptr<IRemoteObject> remote = Remote();
234 if (remote == nullptr) {
235 BGTASK_LOGE("OnContinuousTaskStop remote is dead.");
236 return;
237 }
238 if (continuousTaskCallbackInfo == nullptr) {
239 BGTASK_LOGE("OnContinuousTaskStop continuousTaskCallbackInfo is nullptr.");
240 return;
241 }
242
243 MessageParcel data;
244 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
245 BGTASK_LOGE("OnContinuousTaskStop write interface token failed.");
246 return;
247 }
248
249 if (!data.WriteParcelable(continuousTaskCallbackInfo.get())) {
250 BGTASK_LOGE("OnContinuousTaskStop write notification failed.");
251 return;
252 }
253
254 MessageParcel reply;
255 MessageOption option = {MessageOption::TF_ASYNC};
256 int32_t result = remote->SendRequest(
257 static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_CONTINUOUS_TASK_STOP), data, reply, option);
258 if (result != ERR_OK) {
259 BGTASK_LOGE("OnContinuousTaskStop SendRequest error");
260 }
261 }
262
OnAppContinuousTaskStop(int32_t uid)263 void BackgroundTaskSubscriberProxy::OnAppContinuousTaskStop(int32_t uid)
264 {
265 sptr<IRemoteObject> remote = Remote();
266 if (remote == nullptr) {
267 BGTASK_LOGE("OnAppContinuousTaskStop remote is dead.");
268 return;
269 }
270
271 MessageParcel data;
272 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
273 BGTASK_LOGE("OnAppContinuousTaskStop write interface token failed.");
274 return;
275 }
276
277 if (!data.WriteInt32(uid)) {
278 BGTASK_LOGE("OnAppContinuousTaskStop write uid failed.");
279 return;
280 }
281
282 MessageParcel reply;
283 MessageOption option = {MessageOption::TF_ASYNC};
284 int32_t result = remote->SendRequest(static_cast<uint32_t>(
285 IBackgroundTaskSubscriberInterfaceCode::ON_APP_CONTINUOUS_TASK_STOP), data, reply, option);
286 if (result != ERR_OK) {
287 BGTASK_LOGE("OnAppContinuousTaskStop SendRequest error");
288 }
289 }
290
291
OnAppEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)292 void BackgroundTaskSubscriberProxy::OnAppEfficiencyResourcesApply(
293 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo)
294 {
295 sptr<IRemoteObject> remote = Remote();
296 if (remote == nullptr) {
297 BGTASK_LOGE("OnAppEfficiencyResourcesApply remote is dead.");
298 return;
299 }
300 if (resourceInfo == nullptr) {
301 BGTASK_LOGE("OnAppEfficiencyResourcesApply resourceInfo is nullptr.");
302 return;
303 }
304
305 MessageParcel data;
306 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
307 BGTASK_LOGE("OnAppEfficiencyResourcesApply write interface token failed.");
308 return;
309 }
310
311 if (!data.WriteParcelable(resourceInfo.get())) {
312 BGTASK_LOGE("OnAppEfficiencyResourcesApply write notification failed.");
313 return;
314 }
315
316 MessageParcel reply;
317 MessageOption option;
318 int32_t result = remote->SendRequest(static_cast<uint32_t>(
319 IBackgroundTaskSubscriberInterfaceCode::ON_APP_EFFICIENCY_RESOURCES_APPLY), data, reply, option);
320 if (result != ERR_OK) {
321 BGTASK_LOGE("OnAppEfficiencyResourcesApply SendRequest error");
322 }
323 }
324
OnAppEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)325 void BackgroundTaskSubscriberProxy::OnAppEfficiencyResourcesReset(
326 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo)
327 {
328 sptr<IRemoteObject> remote = Remote();
329 if (remote == nullptr) {
330 BGTASK_LOGE("OnAppEfficiencyResourcesReset remote is dead.");
331 return;
332 }
333
334 if (resourceInfo == nullptr) {
335 BGTASK_LOGE("OnAppEfficiencyResourcesReset resourceInfo is nullptr.");
336 return;
337 }
338
339 MessageParcel data;
340 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
341 BGTASK_LOGE("OnAppEfficiencyResourcesReset write interface token failed.");
342 return;
343 }
344
345 if (!data.WriteParcelable(resourceInfo.get())) {
346 BGTASK_LOGE("OnAppEfficiencyResourcesReset write notification failed.");
347 return;
348 }
349
350 MessageParcel reply;
351 MessageOption option;
352 int32_t result = remote->SendRequest(static_cast<uint32_t>(
353 IBackgroundTaskSubscriberInterfaceCode::ON_APP_EFFICIENCY_RESOURCES_RESET), data, reply, option);
354 if (result != ERR_OK) {
355 BGTASK_LOGE("OnAppEfficiencyResourcesReset SendRequest error");
356 }
357 }
358
OnProcEfficiencyResourcesApply(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)359 void BackgroundTaskSubscriberProxy::OnProcEfficiencyResourcesApply(
360 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo)
361 {
362 sptr<IRemoteObject> remote = Remote();
363 if (remote == nullptr) {
364 BGTASK_LOGE("OnProcEfficiencyResourcesApply remote is dead.");
365 return;
366 }
367 if (resourceInfo == nullptr) {
368 BGTASK_LOGE("OnProcEfficiencyResourcesApply resourceInfo is nullptr.");
369 return;
370 }
371
372 MessageParcel data;
373 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
374 BGTASK_LOGE("OnProcEfficiencyResourcesApply write interface token failed.");
375 return;
376 }
377
378 if (!data.WriteParcelable(resourceInfo.get())) {
379 BGTASK_LOGE("OnProcEfficiencyResourcesApply write notification failed.");
380 return;
381 }
382
383 MessageParcel reply;
384 MessageOption option;
385 int32_t result = remote->SendRequest(static_cast<uint32_t>(
386 IBackgroundTaskSubscriberInterfaceCode::ON_PROC_EFFICIENCY_RESOURCES_APPLY), data, reply, option);
387 if (result != ERR_OK) {
388 BGTASK_LOGE("OnProcEfficiencyResourcesApply SendRequest error");
389 }
390 }
391
OnProcEfficiencyResourcesReset(const std::shared_ptr<ResourceCallbackInfo> & resourceInfo)392 void BackgroundTaskSubscriberProxy::OnProcEfficiencyResourcesReset(
393 const std::shared_ptr<ResourceCallbackInfo> &resourceInfo)
394 {
395 sptr<IRemoteObject> remote = Remote();
396 if (remote == nullptr) {
397 BGTASK_LOGE("OnProcEfficiencyResourcesReset remote is dead.");
398 return;
399 }
400
401 if (resourceInfo == nullptr) {
402 BGTASK_LOGE("OnProcEfficiencyResourcesReset resourceInfo is nullptr.");
403 return;
404 }
405
406 MessageParcel data;
407 if (!data.WriteInterfaceToken(BackgroundTaskSubscriberProxy::GetDescriptor())) {
408 BGTASK_LOGE("OnProcEfficiencyResourcesReset write interface token failed.");
409 return;
410 }
411
412 if (!data.WriteParcelable(resourceInfo.get())) {
413 BGTASK_LOGE("OnProcEfficiencyResourcesReset write notification failed.");
414 return;
415 }
416
417 MessageParcel reply;
418 MessageOption option;
419 int32_t result = remote->SendRequest(static_cast<uint32_t>(
420 IBackgroundTaskSubscriberInterfaceCode::ON_PROC_EFFICIENCY_RESOURCES_RESET), data, reply, option);
421 if (result != ERR_OK) {
422 BGTASK_LOGE("OnProcEfficiencyResourcesReset SendRequest error");
423 }
424 }
425 } // namespace BackgroundTaskMgr
426 } // namespace OHOS