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