1 /*
2 * Copyright (c) 2021-2024 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 "app_scheduler_proxy.h"
17
18 #include "freeze_util.h"
19 #include "hilog_tag_wrapper.h"
20 #include "hitrace_meter.h"
21 #include "ipc_types.h"
22 #include "iremote_object.h"
23 #include "app_scheduler_const.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
AppSchedulerProxy(const sptr<IRemoteObject> & impl)27 AppSchedulerProxy::AppSchedulerProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IAppScheduler>(impl)
28 {}
29
WriteInterfaceToken(MessageParcel & data)30 bool AppSchedulerProxy::WriteInterfaceToken(MessageParcel &data)
31 {
32 if (!data.WriteInterfaceToken(AppSchedulerProxy::GetDescriptor())) {
33 TAG_LOGE(AAFwkTag::APPMGR, "write interface token failed");
34 return false;
35 }
36 return true;
37 }
38
ScheduleForegroundApplication()39 bool AppSchedulerProxy::ScheduleForegroundApplication()
40 {
41 TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy::ScheduleForegroundApplication start");
42 MessageParcel data;
43 MessageParcel reply;
44 MessageOption option(MessageOption::TF_ASYNC);
45 if (!WriteInterfaceToken(data)) {
46 return false;
47 }
48 int32_t ret =
49 SendTransactCmd(static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_FOREGROUND_APPLICATION_TRANSACTION),
50 data,
51 reply,
52 option);
53 if (ret != NO_ERROR) {
54 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
55 return false;
56 }
57 return true;
58 }
59
ScheduleBackgroundApplication()60 void AppSchedulerProxy::ScheduleBackgroundApplication()
61 {
62 MessageParcel data;
63 MessageParcel reply;
64 MessageOption option(MessageOption::TF_ASYNC);
65 if (!WriteInterfaceToken(data)) {
66 return;
67 }
68 int32_t ret =
69 SendTransactCmd(static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_BACKGROUND_APPLICATION_TRANSACTION),
70 data,
71 reply,
72 option);
73 if (ret != NO_ERROR) {
74 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is wrong, error code: %{public}d", ret);
75 }
76 }
77
ScheduleTerminateApplication(bool isLastProcess)78 void AppSchedulerProxy::ScheduleTerminateApplication(bool isLastProcess)
79 {
80 MessageParcel data;
81 MessageParcel reply;
82 MessageOption option(MessageOption::TF_ASYNC);
83 if (!WriteInterfaceToken(data)) {
84 return;
85 }
86 if (!data.WriteBool(isLastProcess)) {
87 TAG_LOGE(AAFwkTag::APPMGR, "Write bool failed.");
88 return;
89 }
90 int32_t ret = SendTransactCmd(
91 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_TERMINATE_APPLICATION_TRANSACTION), data, reply, option);
92 if (ret != NO_ERROR) {
93 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is unsuccessful, error code: %{public}d", ret);
94 }
95 }
96
ScheduleLowMemory()97 void AppSchedulerProxy::ScheduleLowMemory()
98 {
99 TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy::ScheduleLowMemory begin");
100 MessageParcel data;
101 MessageParcel reply;
102 MessageOption option(MessageOption::TF_ASYNC);
103 if (!WriteInterfaceToken(data)) {
104 return;
105 }
106 int32_t ret = SendTransactCmd(
107 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_LOWMEMORY_APPLICATION_TRANSACTION), data, reply, option);
108 if (ret != NO_ERROR) {
109 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
110 }
111 }
112
ScheduleMemoryLevel(int32_t level)113 void AppSchedulerProxy::ScheduleMemoryLevel(int32_t level)
114 {
115 uint32_t operation = static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_MEMORYLEVEL_APPLICATION_TRANSACTION);
116 ScheduleMemoryCommon(level, operation);
117 }
118
ScheduleHeapMemory(const int32_t pid,OHOS::AppExecFwk::MallocInfo & mallocInfo)119 void AppSchedulerProxy::ScheduleHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo)
120 {
121 TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy::ScheduleHeapMemory start");
122 uint32_t operation = static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_HEAPMEMORY_APPLICATION_TRANSACTION);
123 MessageParcel data;
124 MessageParcel reply;
125 MessageOption option(MessageOption::TF_SYNC);
126 if (!WriteInterfaceToken(data)) {
127 TAG_LOGE(AAFwkTag::APPMGR, "AppSchedulerProxy !WriteInterfaceToken.");
128 return;
129 }
130 data.WriteInt32(pid);
131 int32_t ret = SendTransactCmd(operation, data, reply, option);
132 if (ret != NO_ERROR) {
133 TAG_LOGE(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
134 return;
135 }
136
137 std::unique_ptr<MallocInfo> info(reply.ReadParcelable<MallocInfo>());
138 if (info == nullptr) {
139 TAG_LOGE(AAFwkTag::APPMGR, "MallocInfo ReadParcelable nullptr");
140 return;
141 }
142 mallocInfo = *info;
143 }
144
ScheduleJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo & info)145 void AppSchedulerProxy::ScheduleJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info)
146 {
147 TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy::ScheduleJsHeapMemory start");
148 uint32_t operation = static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_JSHEAP_MEMORY_APPLICATION_TRANSACTION);
149 MessageParcel data;
150 MessageParcel reply;
151 MessageOption option(MessageOption::TF_ASYNC);
152 if (!WriteInterfaceToken(data)) {
153 TAG_LOGE(AAFwkTag::APPMGR, "AppSchedulerProxy !WriteInterfaceToken.");
154 return;
155 }
156 data.WriteParcelable(&info);
157 int32_t ret = SendTransactCmd(operation, data, reply, option);
158 if (ret != NO_ERROR) {
159 TAG_LOGE(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
160 return;
161 }
162 }
163
ScheduleShrinkMemory(const int32_t level)164 void AppSchedulerProxy::ScheduleShrinkMemory(const int32_t level)
165 {
166 uint32_t operation = static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_SHRINK_MEMORY_APPLICATION_TRANSACTION);
167 ScheduleMemoryCommon(level, operation);
168 }
169
ScheduleMemoryCommon(const int32_t level,const uint32_t operation)170 void AppSchedulerProxy::ScheduleMemoryCommon(const int32_t level, const uint32_t operation)
171 {
172 MessageParcel data;
173 MessageParcel reply;
174 MessageOption option(MessageOption::TF_ASYNC);
175 if (!WriteInterfaceToken(data)) {
176 return;
177 }
178 data.WriteInt32(level);
179 int32_t ret = SendTransactCmd(operation, data, reply, option);
180 if (ret != NO_ERROR) {
181 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
182 }
183 }
184
ScheduleLaunchAbility(const AbilityInfo & info,const sptr<IRemoteObject> & token,const std::shared_ptr<AAFwk::Want> & want,int32_t abilityRecordId)185 void AppSchedulerProxy::ScheduleLaunchAbility(const AbilityInfo &info, const sptr<IRemoteObject> &token,
186 const std::shared_ptr<AAFwk::Want> &want, int32_t abilityRecordId)
187 {
188 MessageParcel data;
189 MessageParcel reply;
190 MessageOption option(MessageOption::TF_ASYNC);
191 if (!WriteInterfaceToken(data)) {
192 return;
193 }
194 data.WriteParcelable(&info);
195 AbilityRuntime::FreezeUtil::LifecycleFlow flow = {token, AbilityRuntime::FreezeUtil::TimeoutState::LOAD};
196 if (token) {
197 if (!data.WriteBool(true) || !data.WriteRemoteObject(token.GetRefPtr())) {
198 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write flag and token");
199 return;
200 }
201 } else {
202 if (!data.WriteBool(false)) {
203 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write flag");
204 return;
205 }
206 }
207
208 if (!data.WriteParcelable(want.get())) {
209 TAG_LOGE(AAFwkTag::APPMGR, "write want fail.");
210 AbilityRuntime::FreezeUtil::GetInstance().AppendLifecycleEvent(flow,
211 "ERROR AppLifeCycleDeal::LaunchAbility; write want fail");
212 return;
213 }
214 if (!data.WriteInt32(abilityRecordId)) {
215 TAG_LOGE(AAFwkTag::APPMGR, "write ability record id fail.");
216 return;
217 }
218 int32_t ret = SendTransactCmd(
219 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_LAUNCH_ABILITY_TRANSACTION), data, reply, option);
220 if (ret != NO_ERROR) {
221 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
222 AbilityRuntime::FreezeUtil::GetInstance().AppendLifecycleEvent(flow,
223 std::string("ERROR AppLifeCycleDeal::LaunchAbility; ipc error ") + std::to_string(ret));
224 }
225 }
226
ScheduleCleanAbility(const sptr<IRemoteObject> & token,bool isCacheProcess)227 void AppSchedulerProxy::ScheduleCleanAbility(const sptr<IRemoteObject> &token, bool isCacheProcess)
228 {
229 MessageParcel data;
230 MessageParcel reply;
231 MessageOption option(MessageOption::TF_ASYNC);
232 if (!WriteInterfaceToken(data)) {
233 return;
234 }
235 if (!data.WriteRemoteObject(token.GetRefPtr())) {
236 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
237 return;
238 }
239 if (!data.WriteBool(isCacheProcess)) {
240 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write bool");
241 return;
242 }
243 int32_t ret = SendTransactCmd(
244 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_CLEAN_ABILITY_TRANSACTION), data, reply, option);
245 if (ret != NO_ERROR) {
246 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
247 }
248 }
249
ScheduleLaunchApplication(const AppLaunchData & launchData,const Configuration & config)250 void AppSchedulerProxy::ScheduleLaunchApplication(const AppLaunchData &launchData, const Configuration &config)
251 {
252 TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy ScheduleLaunchApplication start");
253 MessageParcel data;
254 MessageParcel reply;
255 MessageOption option(MessageOption::TF_ASYNC);
256 if (!WriteInterfaceToken(data)) {
257 return;
258 }
259
260 if (!data.WriteParcelable(&launchData)) {
261 TAG_LOGE(AAFwkTag::APPMGR, "WriteParcelable launchData failed");
262 return;
263 }
264
265 if (!data.WriteParcelable(&config)) {
266 TAG_LOGE(AAFwkTag::APPMGR, "WriteParcelable config failed");
267 return ;
268 }
269
270 int32_t ret = SendTransactCmd(
271 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_LAUNCH_APPLICATION_TRANSACTION), data, reply, option);
272 if (ret != NO_ERROR) {
273 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
274 }
275 }
276
ScheduleUpdateApplicationInfoInstalled(const ApplicationInfo & appInfo)277 void AppSchedulerProxy::ScheduleUpdateApplicationInfoInstalled(const ApplicationInfo &appInfo)
278 {
279 TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy ScheduleUpdateApplicationInfoInstalled begin");
280 MessageParcel data;
281 if (!WriteInterfaceToken(data)) {
282 return;
283 }
284 if (!data.WriteParcelable(&appInfo)) {
285 return ;
286 }
287 MessageParcel reply;
288 MessageOption option(MessageOption::TF_ASYNC);
289 int32_t ret = SendTransactCmd(
290 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_UPDATE_APPLICATION_INFO_INSTALLED), data, reply, option);
291 if (ret != NO_ERROR) {
292 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
293 }
294 TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy ScheduleUpdateApplicationInfoInstalled end");
295 }
296
ScheduleAbilityStage(const HapModuleInfo & abilityStage)297 void AppSchedulerProxy::ScheduleAbilityStage(const HapModuleInfo &abilityStage)
298 {
299 TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy ScheduleAbilityStage start");
300 MessageParcel data;
301 constexpr int32_t max = 10000;
302 constexpr int32_t large = 60;
303 constexpr int32_t mid = 20;
304 auto abilityInfoSize = static_cast<int32_t>(abilityStage.abilityInfos.size());
305 auto extensionInfoSize = static_cast<int32_t>(abilityStage.extensionInfos.size());
306 if (abilityInfoSize > max || extensionInfoSize > max) {
307 TAG_LOGE(AAFwkTag::APPMGR, "size exceeds max");
308 return;
309 }
310 auto componentSize = abilityInfoSize + extensionInfoSize;
311 if (componentSize > large) {
312 constexpr int32_t size = 2 * 1024 * 1024; // 1.6 M
313 data.SetDataCapacity(size);
314 } else if (componentSize > mid) {
315 constexpr int32_t size = 800 * 1024; // 800 kb
316 data.SetDataCapacity(size);
317 }
318 if (!WriteInterfaceToken(data)) {
319 return;
320 }
321
322 if (!data.WriteParcelable(&abilityStage)) {
323 return ;
324 }
325
326 MessageParcel reply;
327 MessageOption option(MessageOption::TF_ASYNC);
328 int32_t ret = SendTransactCmd(
329 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_ABILITY_STAGE_INFO), data, reply, option);
330 if (ret != NO_ERROR) {
331 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
332 }
333 TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy ScheduleAbilityStage end");
334 }
335
ScheduleProfileChanged(const Profile & profile)336 void AppSchedulerProxy::ScheduleProfileChanged(const Profile &profile)
337 {
338 MessageParcel data;
339 MessageParcel reply;
340 MessageOption option(MessageOption::TF_ASYNC);
341 if (!WriteInterfaceToken(data)) {
342 return;
343 }
344 data.WriteParcelable(&profile);
345 int32_t ret = SendTransactCmd(
346 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_PROFILE_CHANGED_TRANSACTION), data, reply, option);
347 if (ret != NO_ERROR) {
348 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
349 }
350 }
351
ScheduleConfigurationUpdated(const Configuration & config)352 void AppSchedulerProxy::ScheduleConfigurationUpdated(const Configuration &config)
353 {
354 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
355 MessageParcel data;
356 MessageParcel reply;
357 MessageOption option(MessageOption::TF_ASYNC);
358 if (!WriteInterfaceToken(data)) {
359 return;
360 }
361 data.WriteParcelable(&config);
362 int32_t ret = SendTransactCmd(
363 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_CONFIGURATION_UPDATED), data, reply, option);
364 if (ret != NO_ERROR) {
365 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
366 }
367 }
368
ScheduleProcessSecurityExit()369 void AppSchedulerProxy::ScheduleProcessSecurityExit()
370 {
371 MessageParcel data;
372 MessageParcel reply;
373 MessageOption option(MessageOption::TF_ASYNC);
374 if (!WriteInterfaceToken(data)) {
375 return;
376 }
377 int32_t ret = SendTransactCmd(
378 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_PROCESS_SECURITY_EXIT_TRANSACTION), data, reply, option);
379 if (ret != NO_ERROR) {
380 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
381 }
382 }
383
ScheduleAcceptWant(const AAFwk::Want & want,const std::string & moduleName)384 void AppSchedulerProxy::ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName)
385 {
386 MessageParcel data;
387 MessageParcel reply;
388 MessageOption option(MessageOption::TF_ASYNC);
389 if (!WriteInterfaceToken(data)) {
390 return;
391 }
392
393 if (!data.WriteParcelable(&want) || !data.WriteString(moduleName)) {
394 return;
395 }
396 int32_t ret = SendTransactCmd(
397 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_ACCEPT_WANT), data, reply, option);
398 if (ret != NO_ERROR) {
399 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
400 }
401 }
402
ScheduleNewProcessRequest(const AAFwk::Want & want,const std::string & moduleName)403 void AppSchedulerProxy::ScheduleNewProcessRequest(const AAFwk::Want &want, const std::string &moduleName)
404 {
405 MessageParcel data;
406 MessageParcel reply;
407 MessageOption option(MessageOption::TF_ASYNC);
408 if (!WriteInterfaceToken(data)) {
409 return;
410 }
411
412 if (!data.WriteParcelable(&want) || !data.WriteString(moduleName)) {
413 return;
414 }
415 sptr<IRemoteObject> remote = Remote();
416 if (remote == nullptr) {
417 TAG_LOGE(AAFwkTag::APPMGR, "Remote() is NULL");
418 return;
419 }
420 int32_t ret = remote->SendRequest(
421 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_NEW_PROCESS_REQUEST), data, reply, option);
422 if (ret != NO_ERROR) {
423 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
424 }
425 }
426
ScheduleNotifyLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback,const int32_t recordId)427 int32_t AppSchedulerProxy::ScheduleNotifyLoadRepairPatch(const std::string &bundleName,
428 const sptr<IQuickFixCallback> &callback, const int32_t recordId)
429 {
430 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
431 MessageParcel data;
432 if (!WriteInterfaceToken(data)) {
433 TAG_LOGE(AAFwkTag::APPMGR, "ScheduleNotifyLoadRepairPatch, Write interface token failed.");
434 return ERR_INVALID_DATA;
435 }
436
437 if (!data.WriteString(bundleName)) {
438 TAG_LOGE(AAFwkTag::APPMGR, "ScheduleNotifyLoadRepairPatch, Write bundle name failed.");
439 return ERR_INVALID_DATA;
440 }
441
442 if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
443 TAG_LOGE(AAFwkTag::APPMGR, "Write callback failed.");
444 return ERR_INVALID_DATA;
445 }
446
447 if (!data.WriteInt32(recordId)) {
448 TAG_LOGE(AAFwkTag::APPMGR, "Write record id failed.");
449 return ERR_INVALID_DATA;
450 }
451
452 MessageParcel reply;
453 MessageOption option;
454 int32_t ret = SendTransactCmd(static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_NOTIFY_LOAD_REPAIR_PATCH),
455 data, reply, option);
456 if (ret != 0) {
457 TAG_LOGE(AAFwkTag::APPMGR, "ScheduleNotifyLoadRepairPatch, Send request failed with errno %{public}d.", ret);
458 return ret;
459 }
460
461 return reply.ReadInt32();
462 }
463
ScheduleNotifyHotReloadPage(const sptr<IQuickFixCallback> & callback,const int32_t recordId)464 int32_t AppSchedulerProxy::ScheduleNotifyHotReloadPage(const sptr<IQuickFixCallback> &callback, const int32_t recordId)
465 {
466 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
467 MessageParcel data;
468 if (!WriteInterfaceToken(data)) {
469 TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed!");
470 return ERR_INVALID_DATA;
471 }
472
473 if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
474 TAG_LOGE(AAFwkTag::APPMGR, "Write callback failed.");
475 return ERR_INVALID_DATA;
476 }
477
478 if (!data.WriteInt32(recordId)) {
479 TAG_LOGE(AAFwkTag::APPMGR, "Write record id failed.");
480 return ERR_INVALID_DATA;
481 }
482
483 MessageParcel reply;
484 MessageOption option;
485 auto ret = SendTransactCmd(static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_NOTIFY_HOT_RELOAD_PAGE),
486 data, reply, option);
487 if (ret != 0) {
488 TAG_LOGE(AAFwkTag::APPMGR, "Send request failed with errno %{public}d.", ret);
489 return ret;
490 }
491
492 return reply.ReadInt32();
493 }
494
ScheduleNotifyUnLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback,const int32_t recordId)495 int32_t AppSchedulerProxy::ScheduleNotifyUnLoadRepairPatch(const std::string &bundleName,
496 const sptr<IQuickFixCallback> &callback, const int32_t recordId)
497 {
498 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
499 MessageParcel data;
500 if (!WriteInterfaceToken(data)) {
501 TAG_LOGE(AAFwkTag::APPMGR, "Schedule notify unload patch, Write interface token failed.");
502 return ERR_INVALID_DATA;
503 }
504
505 if (!data.WriteString(bundleName)) {
506 TAG_LOGE(AAFwkTag::APPMGR, "Schedule notify unload patch, Write bundle name failed.");
507 return ERR_INVALID_DATA;
508 }
509
510 if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
511 TAG_LOGE(AAFwkTag::APPMGR, "Write callback failed.");
512 return ERR_INVALID_DATA;
513 }
514
515 if (!data.WriteInt32(recordId)) {
516 TAG_LOGE(AAFwkTag::APPMGR, "Write record id failed.");
517 return ERR_INVALID_DATA;
518 }
519
520 MessageParcel reply;
521 MessageOption option;
522 int32_t ret = SendTransactCmd(
523 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_NOTIFY_UNLOAD_REPAIR_PATCH), data, reply, option);
524 if (ret != 0) {
525 TAG_LOGE(AAFwkTag::APPMGR, "Schedule notify unload patch, Send request failed with errno %{public}d.", ret);
526 return ret;
527 }
528
529 return reply.ReadInt32();
530 }
531
ScheduleNotifyAppFault(const FaultData & faultData)532 int32_t AppSchedulerProxy::ScheduleNotifyAppFault(const FaultData &faultData)
533 {
534 MessageParcel data;
535
536 if (!WriteInterfaceToken(data)) {
537 TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
538 return ERR_INVALID_DATA;
539 }
540
541 if (!data.WriteParcelable(&faultData)) {
542 TAG_LOGE(AAFwkTag::APPMGR, "Write FaultData error.");
543 return ERR_FLATTEN_OBJECT;
544 }
545
546 MessageParcel reply;
547 MessageOption option(MessageOption::TF_ASYNC);
548 auto ret = SendTransactCmd(static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_NOTIFY_FAULT),
549 data, reply, option);
550 if (ret != NO_ERROR) {
551 TAG_LOGE(AAFwkTag::APPMGR, "Send request failed with error code %{public}d.", ret);
552 return ret;
553 }
554
555 return reply.ReadInt32();
556 }
557
ScheduleChangeAppGcState(int32_t state)558 int32_t AppSchedulerProxy::ScheduleChangeAppGcState(int32_t state)
559 {
560 MessageParcel data;
561
562 if (!WriteInterfaceToken(data)) {
563 TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed");
564 return ERR_INVALID_DATA;
565 }
566
567 data.WriteInt32(state);
568 MessageParcel reply;
569 MessageOption option(MessageOption::TF_ASYNC);
570 auto ret = SendTransactCmd(static_cast<uint32_t>(IAppScheduler::Message::APP_GC_STATE_CHANGE),
571 data, reply, option);
572 if (ret != NO_ERROR) {
573 TAG_LOGE(AAFwkTag::APPMGR, "Send request failed with error code %{public}d.", ret);
574 return ret;
575 }
576
577 return NO_ERROR;
578 }
579
AttachAppDebug()580 void AppSchedulerProxy::AttachAppDebug()
581 {
582 TAG_LOGD(AAFwkTag::APPMGR, "called");
583 MessageParcel data;
584 if (!WriteInterfaceToken(data)) {
585 TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
586 return;
587 }
588
589 MessageParcel reply;
590 MessageOption option(MessageOption::TF_ASYNC);
591 auto ret = SendTransactCmd(
592 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_ATTACH_APP_DEBUG), data, reply, option);
593 if (ret != NO_ERROR) {
594 TAG_LOGE(AAFwkTag::APPMGR, "Failed to send request with error code: %{public}d", ret);
595 }
596 }
597
DetachAppDebug()598 void AppSchedulerProxy::DetachAppDebug()
599 {
600 TAG_LOGD(AAFwkTag::APPMGR, "called");
601 MessageParcel data;
602 if (!WriteInterfaceToken(data)) {
603 TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
604 return;
605 }
606
607 MessageParcel reply;
608 MessageOption option(MessageOption::TF_ASYNC);
609 auto ret = SendTransactCmd(
610 static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_DETACH_APP_DEBUG), data, reply, option);
611 if (ret != NO_ERROR) {
612 TAG_LOGE(AAFwkTag::APPMGR, "Send request failed with error code: %{public}d", ret);
613 }
614 }
615
ScheduleDumpIpcStart(std::string & result)616 int32_t AppSchedulerProxy::ScheduleDumpIpcStart(std::string& result)
617 {
618 TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy::ScheduleDumpIpcStart start");
619 uint32_t operation = static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_DUMP_IPC_START);
620 MessageParcel data;
621 MessageParcel reply;
622 MessageOption option(MessageOption::TF_SYNC);
623 if (!WriteInterfaceToken(data)) {
624 result.append(MSG_DUMP_IPC_START_STAT, strlen(MSG_DUMP_IPC_START_STAT))
625 .append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
626 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
627 TAG_LOGE(AAFwkTag::APPMGR, "AppSchedulerProxy !WriteInterfaceToken.");
628 return DumpErrorCode::ERR_INTERNAL_ERROR;
629 }
630 int32_t ret = SendTransactCmd(operation, data, reply, option);
631 if (ret != NO_ERROR) {
632 result.append(MSG_DUMP_IPC_START_STAT, strlen(MSG_DUMP_IPC_START_STAT))
633 .append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
634 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
635 TAG_LOGE(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
636 return DumpErrorCode::ERR_INTERNAL_ERROR;
637 }
638 if (!reply.ReadString(result)) {
639 result.append(MSG_DUMP_IPC_START_STAT, strlen(MSG_DUMP_IPC_START_STAT))
640 .append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
641 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
642 TAG_LOGE(AAFwkTag::APPMGR, "Fail to read string of ScheduleDumpIpcStart result");
643 return DumpErrorCode::ERR_INTERNAL_ERROR;
644 }
645 return DumpErrorCode::ERR_OK;
646 }
647
ScheduleDumpIpcStop(std::string & result)648 int32_t AppSchedulerProxy::ScheduleDumpIpcStop(std::string& result)
649 {
650 TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy::ScheduleDumpIpcStop start");
651 uint32_t operation = static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_DUMP_IPC_STOP);
652 MessageParcel data;
653 MessageParcel reply;
654 MessageOption option(MessageOption::TF_SYNC);
655 if (!WriteInterfaceToken(data)) {
656 result.append(MSG_DUMP_IPC_STOP_STAT, strlen(MSG_DUMP_IPC_STOP_STAT))
657 .append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
658 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
659 TAG_LOGE(AAFwkTag::APPMGR, "AppSchedulerProxy !WriteInterfaceToken.");
660 return DumpErrorCode::ERR_INTERNAL_ERROR;
661 }
662 int32_t ret = SendTransactCmd(operation, data, reply, option);
663 if (ret != NO_ERROR) {
664 result.append(MSG_DUMP_IPC_STOP_STAT, strlen(MSG_DUMP_IPC_STOP_STAT))
665 .append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
666 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
667 TAG_LOGE(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
668 return DumpErrorCode::ERR_INTERNAL_ERROR;
669 }
670 if (!reply.ReadString(result)) {
671 result.append(MSG_DUMP_IPC_STOP_STAT, strlen(MSG_DUMP_IPC_STOP_STAT))
672 .append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
673 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
674 TAG_LOGE(AAFwkTag::APPMGR, "Fail to read string of ScheduleDumpIpcStop result");
675 return DumpErrorCode::ERR_INTERNAL_ERROR;
676 }
677 return DumpErrorCode::ERR_OK;
678 }
679
ScheduleDumpIpcStat(std::string & result)680 int32_t AppSchedulerProxy::ScheduleDumpIpcStat(std::string& result)
681 {
682 TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy::ScheduleDumpIpcStat start");
683 uint32_t operation = static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_DUMP_IPC_STAT);
684 MessageParcel data;
685 MessageParcel reply;
686 MessageOption option(MessageOption::TF_SYNC);
687 if (!WriteInterfaceToken(data)) {
688 result.append(MSG_DUMP_IPC_STAT, strlen(MSG_DUMP_IPC_STAT))
689 .append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
690 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
691 TAG_LOGE(AAFwkTag::APPMGR, "AppSchedulerProxy !WriteInterfaceToken.");
692 return DumpErrorCode::ERR_INTERNAL_ERROR;
693 }
694 int32_t ret = SendTransactCmd(operation, data, reply, option);
695 if (ret != NO_ERROR) {
696 result.append(MSG_DUMP_IPC_STAT, strlen(MSG_DUMP_IPC_STAT))
697 .append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
698 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
699 TAG_LOGE(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
700 return DumpErrorCode::ERR_INTERNAL_ERROR;
701 }
702 if (!reply.ReadString(result)) {
703 result.append(MSG_DUMP_IPC_STAT, strlen(MSG_DUMP_IPC_STAT))
704 .append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
705 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
706 TAG_LOGE(AAFwkTag::APPMGR, "Fail to read string of ScheduleDumpIpcStat result");
707 return DumpErrorCode::ERR_INTERNAL_ERROR;
708 }
709 return DumpErrorCode::ERR_OK;
710 }
711
ScheduleDumpFfrt(std::string & result)712 int32_t AppSchedulerProxy::ScheduleDumpFfrt(std::string& result)
713 {
714 TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy::ScheduleDumpFfrt start");
715 uint32_t operation = static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_DUMP_FFRT);
716 MessageParcel data;
717 MessageParcel reply;
718 reply.SetMaxCapacity(MAX_CAPACITY);
719 MessageOption option(MessageOption::TF_SYNC);
720 if (!WriteInterfaceToken(data)) {
721 result.append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
722 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
723 TAG_LOGE(AAFwkTag::APPMGR, "AppSchedulerProxy !WriteInterfaceToken.");
724 return DumpErrorCode::ERR_INTERNAL_ERROR;
725 }
726 int32_t ret = SendTransactCmd(operation, data, reply, option);
727 if (ret != NO_ERROR) {
728 result.append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
729 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
730 TAG_LOGE(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
731 return DumpErrorCode::ERR_INTERNAL_ERROR;
732 }
733 if (!reply.ReadString(result)) {
734 result.append(MSG_DUMP_FAIL, strlen(MSG_DUMP_FAIL))
735 .append(MSG_DUMP_FAIL_REASON_INTERNAL, strlen(MSG_DUMP_FAIL_REASON_INTERNAL));
736 TAG_LOGE(AAFwkTag::APPMGR, "Fail to read string of ScheduleDumpFfrt result");
737 return DumpErrorCode::ERR_INTERNAL_ERROR;
738 }
739 return DumpErrorCode::ERR_OK;
740 }
741
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)742 int32_t AppSchedulerProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
743 MessageParcel &reply, MessageOption &option)
744 {
745 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
746 sptr<IRemoteObject> remote = Remote();
747 if (remote == nullptr) {
748 TAG_LOGE(AAFwkTag::APPMGR, "Remote is nullptr.");
749 return ERR_NULL_OBJECT;
750 }
751
752 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "remote->SendRequest");
753 auto ret = remote->SendRequest(code, data, reply, option);
754 if (ret != NO_ERROR) {
755 TAG_LOGE(AAFwkTag::APPMGR, "Send request failed with error code: %{public}d", ret);
756 return ret;
757 }
758 return ret;
759 }
760
ScheduleCacheProcess()761 void AppSchedulerProxy::ScheduleCacheProcess()
762 {
763 uint32_t operation = static_cast<uint32_t>(IAppScheduler::Message::SCHEDULE_CACHE_PROCESS);
764 MessageParcel data;
765 MessageParcel reply;
766 MessageOption option(MessageOption::TF_ASYNC);
767 if (!WriteInterfaceToken(data)) {
768 return;
769 }
770 int32_t ret = SendTransactCmd(operation, data, reply, option);
771 if (ret != NO_ERROR) {
772 TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
773 }
774 }
775 } // namespace AppExecFwk
776 } // namespace OHOS
777