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