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