• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_mgr_stub.h"
17 
18 #include "ability_info.h"
19 #include "app_malloc_info.h"
20 #include "app_mgr_proxy.h"
21 #include "app_scheduler_interface.h"
22 #include "appexecfwk_errors.h"
23 #include "bundle_info.h"
24 #include "hilog_wrapper.h"
25 #include "hitrace_meter.h"
26 #include "iapp_state_callback.h"
27 #include "ipc_skeleton.h"
28 #include "ipc_types.h"
29 #include "iremote_object.h"
30 #include "want.h"
31 
32 namespace OHOS {
33 namespace AppExecFwk {
34 constexpr int32_t CYCLE_LIMIT = 1000;
AppMgrStub()35 AppMgrStub::AppMgrStub()
36 {
37     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_ATTACH_APPLICATION)] =
38         &AppMgrStub::HandleAttachApplication;
39     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_APPLICATION_FOREGROUNDED)] =
40         &AppMgrStub::HandleApplicationForegrounded;
41     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_APPLICATION_BACKGROUNDED)] =
42         &AppMgrStub::HandleApplicationBackgrounded;
43     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_APPLICATION_TERMINATED)] =
44         &AppMgrStub::HandleApplicationTerminated;
45     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_CHECK_PERMISSION)] =
46         &AppMgrStub::HandleCheckPermission;
47     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_ABILITY_CLEANED)] =
48         &AppMgrStub::HandleAbilityCleaned;
49     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_GET_MGR_INSTANCE)] = &AppMgrStub::HandleGetAmsMgr;
50     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_CLEAR_UP_APPLICATION_DATA)] =
51         &AppMgrStub::HandleClearUpApplicationData;
52     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_GET_ALL_RUNNING_PROCESSES)] =
53         &AppMgrStub::HandleGetAllRunningProcesses;
54     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_NOTIFY_MEMORY_LEVEL)] =
55         &AppMgrStub::HandleNotifyMemoryLevel;
56     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_GET_RUNNING_PROCESSES_BY_USER_ID)] =
57         &AppMgrStub::HandleGetProcessRunningInfosByUserId;
58     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_ADD_ABILITY_STAGE_INFO_DONE)] =
59         &AppMgrStub::HandleAddAbilityStageDone;
60     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::STARTUP_RESIDENT_PROCESS)] =
61         &AppMgrStub::HandleStartupResidentProcess;
62     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::REGISTER_APPLICATION_STATE_OBSERVER)] =
63         &AppMgrStub::HandleRegisterApplicationStateObserver;
64     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::UNREGISTER_APPLICATION_STATE_OBSERVER)] =
65         &AppMgrStub::HandleUnregisterApplicationStateObserver;
66     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::GET_FOREGROUND_APPLICATIONS)] =
67         &AppMgrStub::HandleGetForegroundApplications;
68     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::START_USER_TEST_PROCESS)] =
69         &AppMgrStub::HandleStartUserTestProcess;
70     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::FINISH_USER_TEST)] =
71         &AppMgrStub::HandleFinishUserTest;
72     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::SCHEDULE_ACCEPT_WANT_DONE)] =
73         &AppMgrStub::HandleScheduleAcceptWantDone;
74     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::SCHEDULE_NEW_PROCESS_REQUEST_DONE)] =
75         &AppMgrStub::HandleScheduleNewProcessRequestDone;
76     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_GET_ABILITY_RECORDS_BY_PROCESS_ID)] =
77         &AppMgrStub::HandleGetAbilityRecordsByProcessID;
78     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::PRE_START_NWEBSPAWN_PROCESS)] =
79         &AppMgrStub::HandlePreStartNWebSpawnProcess;
80     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::START_RENDER_PROCESS)] =
81         &AppMgrStub::HandleStartRenderProcess;
82     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::ATTACH_RENDER_PROCESS)] =
83         &AppMgrStub::HandleAttachRenderProcess;
84     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::GET_RENDER_PROCESS_TERMINATION_STATUS)] =
85         &AppMgrStub::HandleGetRenderProcessTerminationStatus;
86     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::GET_CONFIGURATION)] =
87         &AppMgrStub::HandleGetConfiguration;
88     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::UPDATE_CONFIGURATION)] =
89         &AppMgrStub::HandleUpdateConfiguration;
90     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::REGISTER_CONFIGURATION_OBSERVER)] =
91         &AppMgrStub::HandleRegisterConfigurationObserver;
92     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::UNREGISTER_CONFIGURATION_OBSERVER)] =
93         &AppMgrStub::HandleUnregisterConfigurationObserver;
94     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_GET_PROCESS_RUNNING_INFORMATION)] =
95         &AppMgrStub::HandleGetProcessRunningInformation;
96     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::DUMP_HEAP_MEMORY_PROCESS)] =
97         &AppMgrStub::HandleDumpHeapMemory;
98 #ifdef ABILITY_COMMAND_FOR_TEST
99     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::BLOCK_APP_SERVICE)] =
100         &AppMgrStub::HandleBlockAppServiceDone;
101 #endif
102     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::GET_APP_RUNNING_STATE)] =
103         &AppMgrStub::HandleGetAppRunningStateByBundleName;
104     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_LOAD_REPAIR_PATCH)] =
105         &AppMgrStub::HandleNotifyLoadRepairPatch;
106     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_HOT_RELOAD_PAGE)] =
107         &AppMgrStub::HandleNotifyHotReloadPage;
108 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
109     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::SET_CONTINUOUSTASK_PROCESS)] =
110         &AppMgrStub::HandleSetContinuousTaskProcess;
111 #endif
112     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_UNLOAD_REPAIR_PATCH)] =
113         &AppMgrStub::HandleNotifyUnLoadRepairPatch;
114     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::IS_SHARED_BUNDLE_RUNNING)] =
115         &AppMgrStub::HandleIsSharedBundleRunning;
116     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::START_NATIVE_PROCESS_FOR_DEBUGGER)] =
117         &AppMgrStub::HandleStartNativeProcessForDebugger;
118     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_APP_FAULT)] =
119         &AppMgrStub::HandleNotifyFault;
120     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_APP_FAULT_BY_SA)] =
121         &AppMgrStub::HandleNotifyFaultBySA;
122     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::JUDGE_SANDBOX_BY_PID)] =
123         &AppMgrStub::HandleJudgeSandboxByPid;
124     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::GET_BUNDLE_NAME_BY_PID)] =
125         &AppMgrStub::HandleGetBundleNameByPid;
126     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_GET_ALL_RENDER_PROCESSES)] =
127         &AppMgrStub::HandleGetAllRenderProcesses;
128     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::GET_PROCESS_MEMORY_BY_PID)] =
129         &AppMgrStub::HandleGetProcessMemoryByPid;
130     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::GET_PIDS_BY_BUNDLENAME)] =
131         &AppMgrStub::HandleGetRunningProcessInformation;
132     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::CHANGE_APP_GC_STATE)] =
133             &AppMgrStub::HandleChangeAppGcState;
134     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_PAGE_SHOW)] =
135         &AppMgrStub::HandleNotifyPageShow;
136     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_PAGE_HIDE)] =
137         &AppMgrStub::HandleNotifyPageHide;
138     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::REGISTER_APP_RUNNING_STATUS_LISTENER)] =
139         &AppMgrStub::HandleRegisterAppRunningStatusListener;
140     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::UNREGISTER_APP_RUNNING_STATUS_LISTENER)] =
141         &AppMgrStub::HandleUnregisterAppRunningStatusListener;
142     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::REGISTER_APP_FOREGROUND_STATE_OBSERVER)] =
143         &AppMgrStub::HandleRegisterAppForegroundStateObserver;
144     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::UNREGISTER_APP_FOREGROUND_STATE_OBSERVER)] =
145         &AppMgrStub::HandleUnregisterAppForegroundStateObserver;
146     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::REGISTER_ABILITY_FOREGROUND_STATE_OBSERVER)] =
147         &AppMgrStub::HandleRegisterAbilityForegroundStateObserver;
148     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::UNREGISTER_ABILITY_FOREGROUND_STATE_OBSERVER)] =
149         &AppMgrStub::HandleUnregisterAbilityForegroundStateObserver;
150     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::IS_APPLICATION_RUNNING)] =
151         &AppMgrStub::HandleIsApplicationRunning;
152     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::START_CHILD_PROCESS)] =
153         &AppMgrStub::HandleStartChildProcess;
154     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::GET_CHILD_PROCCESS_INFO_FOR_SELF)] =
155         &AppMgrStub::HandleGetChildProcessInfoForSelf;
156     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::ATTACH_CHILD_PROCESS)] =
157         &AppMgrStub::HandleAttachChildProcess;
158     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::EXIT_CHILD_PROCESS_SAFELY)] =
159         &AppMgrStub::HandleExitChildProcessSafely;
160     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::IS_FINAL_APP_PROCESS)] =
161         &AppMgrStub::HandleIsFinalAppProcess;
162     memberFuncMap_[static_cast<uint32_t>(AppMgrInterfaceCode::APP_CLEAR_UP_APPLICATION_DATA_BY_SELF)] =
163         &AppMgrStub::HandleClearUpApplicationDataBySelf;
164 }
165 
~AppMgrStub()166 AppMgrStub::~AppMgrStub()
167 {
168     memberFuncMap_.clear();
169 }
170 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)171 int AppMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
172 {
173     HILOG_DEBUG("AppMgrStub::OnReceived, code = %{public}u, flags= %{public}d.", code, option.GetFlags());
174     std::u16string descriptor = AppMgrStub::GetDescriptor();
175     std::u16string remoteDescriptor = data.ReadInterfaceToken();
176     if (descriptor != remoteDescriptor) {
177         HILOG_ERROR("local descriptor is not equal to remote");
178         return ERR_INVALID_STATE;
179     }
180 
181     auto itFunc = memberFuncMap_.find(code);
182     if (itFunc != memberFuncMap_.end()) {
183         auto memberFunc = itFunc->second;
184         if (memberFunc != nullptr) {
185             return (this->*memberFunc)(data, reply);
186         }
187     }
188     HILOG_DEBUG("AppMgrStub::OnRemoteRequest end");
189     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
190 }
191 
HandleAttachApplication(MessageParcel & data,MessageParcel & reply)192 int32_t AppMgrStub::HandleAttachApplication(MessageParcel &data, MessageParcel &reply)
193 {
194     HITRACE_METER(HITRACE_TAG_APP);
195     sptr<IRemoteObject> client = data.ReadRemoteObject();
196     AttachApplication(client);
197     return NO_ERROR;
198 }
199 
HandleApplicationForegrounded(MessageParcel & data,MessageParcel & reply)200 int32_t AppMgrStub::HandleApplicationForegrounded(MessageParcel &data, MessageParcel &reply)
201 {
202     HITRACE_METER(HITRACE_TAG_APP);
203     ApplicationForegrounded(data.ReadInt32());
204     return NO_ERROR;
205 }
206 
HandleApplicationBackgrounded(MessageParcel & data,MessageParcel & reply)207 int32_t AppMgrStub::HandleApplicationBackgrounded(MessageParcel &data, MessageParcel &reply)
208 {
209     HITRACE_METER(HITRACE_TAG_APP);
210     ApplicationBackgrounded(data.ReadInt32());
211     return NO_ERROR;
212 }
213 
HandleApplicationTerminated(MessageParcel & data,MessageParcel & reply)214 int32_t AppMgrStub::HandleApplicationTerminated(MessageParcel &data, MessageParcel &reply)
215 {
216     HITRACE_METER(HITRACE_TAG_APP);
217     ApplicationTerminated(data.ReadInt32());
218     return NO_ERROR;
219 }
220 
HandleCheckPermission(MessageParcel & data,MessageParcel & reply)221 int32_t AppMgrStub::HandleCheckPermission(MessageParcel &data, MessageParcel &reply)
222 {
223     HITRACE_METER(HITRACE_TAG_APP);
224     int32_t recordId = data.ReadInt32();
225     std::string permission = data.ReadString();
226     int32_t result = CheckPermission(recordId, permission);
227     reply.WriteInt32(result);
228     return NO_ERROR;
229 }
230 
HandleAbilityCleaned(MessageParcel & data,MessageParcel & reply)231 int32_t AppMgrStub::HandleAbilityCleaned(MessageParcel &data, MessageParcel &reply)
232 {
233     HITRACE_METER(HITRACE_TAG_APP);
234     sptr<IRemoteObject> token = data.ReadRemoteObject();
235     AbilityCleaned(token);
236     return NO_ERROR;
237 }
238 
HandleGetAmsMgr(MessageParcel & data,MessageParcel & reply)239 int32_t AppMgrStub::HandleGetAmsMgr(MessageParcel &data, MessageParcel &reply)
240 {
241     HITRACE_METER(HITRACE_TAG_APP);
242     int32_t result = NO_ERROR;
243     sptr<IAmsMgr> amsMgr = GetAmsMgr();
244     if (!amsMgr) {
245         HILOG_ERROR("abilitymgr instance is nullptr");
246         result = ERR_NO_INIT;
247     } else {
248         if (!reply.WriteRemoteObject(amsMgr->AsObject())) {
249             HILOG_ERROR("failed to reply abilitymgr instance to client, for write parcel error");
250             result = ERR_APPEXECFWK_PARCEL_ERROR;
251         }
252     }
253     reply.WriteInt32(result);
254     return NO_ERROR;
255 }
256 
HandleClearUpApplicationData(MessageParcel & data,MessageParcel & reply)257 int32_t AppMgrStub::HandleClearUpApplicationData(MessageParcel &data, MessageParcel &reply)
258 {
259     HITRACE_METER(HITRACE_TAG_APP);
260     std::string bundleName = data.ReadString();
261     int32_t userId = data.ReadInt32();
262     int32_t result = ClearUpApplicationData(bundleName, userId);
263     reply.WriteInt32(result);
264     return NO_ERROR;
265 }
266 
HandleClearUpApplicationDataBySelf(MessageParcel & data,MessageParcel & reply)267 int32_t AppMgrStub::HandleClearUpApplicationDataBySelf(MessageParcel &data, MessageParcel &reply)
268 {
269     HITRACE_METER(HITRACE_TAG_APP);
270     int32_t userId = data.ReadInt32();
271     int32_t result = ClearUpApplicationDataBySelf(userId);
272     reply.WriteInt32(result);
273     return NO_ERROR;
274 }
275 
HandleGetAllRunningProcesses(MessageParcel & data,MessageParcel & reply)276 int32_t AppMgrStub::HandleGetAllRunningProcesses(MessageParcel &data, MessageParcel &reply)
277 {
278     HITRACE_METER(HITRACE_TAG_APP);
279     std::vector<RunningProcessInfo> info;
280     auto result = GetAllRunningProcesses(info);
281     reply.WriteInt32(info.size());
282     for (auto &it : info) {
283         if (!reply.WriteParcelable(&it)) {
284             return ERR_INVALID_VALUE;
285         }
286     }
287     if (!reply.WriteInt32(result)) {
288         return ERR_INVALID_VALUE;
289     }
290     return NO_ERROR;
291 }
292 
HandleGetProcessRunningInfosByUserId(MessageParcel & data,MessageParcel & reply)293 int32_t AppMgrStub::HandleGetProcessRunningInfosByUserId(MessageParcel &data, MessageParcel &reply)
294 {
295     HITRACE_METER(HITRACE_TAG_APP);
296     int32_t userId = data.ReadInt32();
297     std::vector<RunningProcessInfo> info;
298     auto result = GetProcessRunningInfosByUserId(info, userId);
299     reply.WriteInt32(info.size());
300     for (auto &it : info) {
301         if (!reply.WriteParcelable(&it)) {
302             return ERR_INVALID_VALUE;
303         }
304     }
305     if (!reply.WriteInt32(result)) {
306         return ERR_INVALID_VALUE;
307     }
308     HILOG_DEBUG("AppMgrStub::HandleGetAllRunningProcesses end");
309     return NO_ERROR;
310 }
311 
HandleGetAllRenderProcesses(MessageParcel & data,MessageParcel & reply)312 int32_t AppMgrStub::HandleGetAllRenderProcesses(MessageParcel &data, MessageParcel &reply)
313 {
314     HITRACE_METER(HITRACE_TAG_APP);
315     std::vector<RenderProcessInfo> info;
316     auto result = GetAllRenderProcesses(info);
317     reply.WriteInt32(info.size());
318     for (auto &it : info) {
319         if (!reply.WriteParcelable(&it)) {
320             return ERR_INVALID_VALUE;
321         }
322     }
323     if (!reply.WriteInt32(result)) {
324         return ERR_INVALID_VALUE;
325     }
326     return NO_ERROR;
327 }
328 
HandleJudgeSandboxByPid(MessageParcel & data,MessageParcel & reply)329 int32_t AppMgrStub::HandleJudgeSandboxByPid(MessageParcel &data, MessageParcel &reply)
330 {
331     HITRACE_METER(HITRACE_TAG_APP);
332     int32_t pid = data.ReadInt32();
333     bool isSandbox = false;
334     auto result = JudgeSandboxByPid(pid, isSandbox);
335     if (!reply.WriteBool(isSandbox)) {
336         return ERR_INVALID_VALUE;
337     }
338     if (!reply.WriteInt32(result)) {
339         return ERR_INVALID_VALUE;
340     }
341     return NO_ERROR;
342 }
343 
HandleGetProcessRunningInformation(MessageParcel & data,MessageParcel & reply)344 int32_t AppMgrStub::HandleGetProcessRunningInformation(MessageParcel &data, MessageParcel &reply)
345 {
346     HITRACE_METER(HITRACE_TAG_APP);
347     RunningProcessInfo info;
348     auto result = GetProcessRunningInformation(info);
349     if (!reply.WriteParcelable(&info)) {
350         return ERR_INVALID_VALUE;
351     }
352     if (!reply.WriteInt32(result)) {
353         return ERR_INVALID_VALUE;
354     }
355     return NO_ERROR;
356 }
357 
HandleAddAbilityStageDone(MessageParcel & data,MessageParcel & reply)358 int32_t AppMgrStub::HandleAddAbilityStageDone(MessageParcel &data, MessageParcel &reply)
359 {
360     int32_t recordId = data.ReadInt32();
361     AddAbilityStageDone(recordId);
362     return NO_ERROR;
363 }
364 
HandleNotifyMemoryLevel(MessageParcel & data,MessageParcel & reply)365 int32_t AppMgrStub::HandleNotifyMemoryLevel(MessageParcel &data, MessageParcel &reply)
366 {
367     HITRACE_METER(HITRACE_TAG_APP);
368     int32_t level = data.ReadInt32();
369     auto result = NotifyMemoryLevel(level);
370     if (!reply.WriteInt32(result)) {
371         return ERR_INVALID_VALUE;
372     }
373     return NO_ERROR;
374 }
375 
HandleDumpHeapMemory(MessageParcel & data,MessageParcel & reply)376 int32_t AppMgrStub::HandleDumpHeapMemory(MessageParcel &data, MessageParcel &reply)
377 {
378     HILOG_DEBUG("AppMgrStub::HandleDumpHeapMemory.");
379     HITRACE_METER(HITRACE_TAG_APP);
380     int32_t pid = data.ReadInt32();
381     struct OHOS::AppExecFwk::MallocInfo mallocInfo;
382     auto result = DumpHeapMemory(pid, mallocInfo);
383     if (result != NO_ERROR) {
384         return result;
385     }
386     reply.WriteParcelable(&mallocInfo);
387     return NO_ERROR;
388 }
389 
HandleStartupResidentProcess(MessageParcel & data,MessageParcel & reply)390 int32_t AppMgrStub::HandleStartupResidentProcess(MessageParcel &data, MessageParcel &reply)
391 {
392     HITRACE_METER(HITRACE_TAG_APP);
393     std::vector<AppExecFwk::BundleInfo> bundleInfos;
394     int32_t infoSize = data.ReadInt32();
395     if (infoSize > CYCLE_LIMIT) {
396         HILOG_ERROR("infoSize is too large");
397         return ERR_INVALID_VALUE;
398     }
399     for (int32_t i = 0; i < infoSize; i++) {
400         std::unique_ptr<AppExecFwk::BundleInfo> bundleInfo(data.ReadParcelable<AppExecFwk::BundleInfo>());
401         if (!bundleInfo) {
402             HILOG_ERROR("Read Parcelable infos failed.");
403             return ERR_INVALID_VALUE;
404         }
405         bundleInfos.emplace_back(*bundleInfo);
406     }
407     StartupResidentProcess(bundleInfos);
408     return NO_ERROR;
409 }
410 
HandleRegisterApplicationStateObserver(MessageParcel & data,MessageParcel & reply)411 int32_t AppMgrStub::HandleRegisterApplicationStateObserver(MessageParcel &data, MessageParcel &reply)
412 {
413     std::vector<std::string> bundleNameList;
414     auto callback = iface_cast<AppExecFwk::IApplicationStateObserver>(data.ReadRemoteObject());
415     data.ReadStringVector(&bundleNameList);
416     int32_t result = RegisterApplicationStateObserver(callback, bundleNameList);
417     reply.WriteInt32(result);
418     return NO_ERROR;
419 }
420 
HandleUnregisterApplicationStateObserver(MessageParcel & data,MessageParcel & reply)421 int32_t AppMgrStub::HandleUnregisterApplicationStateObserver(MessageParcel &data, MessageParcel &reply)
422 {
423     auto callback = iface_cast<AppExecFwk::IApplicationStateObserver>(data.ReadRemoteObject());
424     int32_t result = UnregisterApplicationStateObserver(callback);
425     reply.WriteInt32(result);
426     return NO_ERROR;
427 }
428 
HandleRegisterAbilityForegroundStateObserver(MessageParcel & data,MessageParcel & reply)429 int32_t AppMgrStub::HandleRegisterAbilityForegroundStateObserver(MessageParcel &data, MessageParcel &reply)
430 {
431     auto callback = iface_cast<AppExecFwk::IAbilityForegroundStateObserver>(data.ReadRemoteObject());
432     int32_t result = RegisterAbilityForegroundStateObserver(callback);
433     if (!reply.WriteInt32(result)) {
434         HILOG_ERROR("Fail to write result.");
435         return ERR_INVALID_VALUE;
436     }
437     return NO_ERROR;
438 }
439 
HandleUnregisterAbilityForegroundStateObserver(MessageParcel & data,MessageParcel & reply)440 int32_t AppMgrStub::HandleUnregisterAbilityForegroundStateObserver(MessageParcel &data, MessageParcel &reply)
441 {
442     auto callback = iface_cast<AppExecFwk::IAbilityForegroundStateObserver>(data.ReadRemoteObject());
443     int32_t result = UnregisterAbilityForegroundStateObserver(callback);
444     if (!reply.WriteInt32(result)) {
445         HILOG_ERROR("Fail to write result.");
446         return ERR_INVALID_VALUE;
447     }
448     return NO_ERROR;
449 }
450 
HandleGetForegroundApplications(MessageParcel & data,MessageParcel & reply)451 int32_t AppMgrStub::HandleGetForegroundApplications(MessageParcel &data, MessageParcel &reply)
452 {
453     std::vector<AppStateData> appStateDatas;
454     int32_t result = GetForegroundApplications(appStateDatas);
455     reply.WriteInt32(appStateDatas.size());
456     for (auto &it : appStateDatas) {
457         if (!reply.WriteParcelable(&it)) {
458             return ERR_INVALID_VALUE;
459         }
460     }
461     if (!reply.WriteInt32(result)) {
462         return ERR_INVALID_VALUE;
463     }
464     return result;
465 }
466 
HandleStartUserTestProcess(MessageParcel & data,MessageParcel & reply)467 int32_t AppMgrStub::HandleStartUserTestProcess(MessageParcel &data, MessageParcel &reply)
468 {
469     std::unique_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
470     if (want == nullptr) {
471         HILOG_ERROR("want is nullptr");
472         return ERR_INVALID_VALUE;
473     }
474     std::unique_ptr<BundleInfo> bundleInfo(data.ReadParcelable<BundleInfo>());
475     if (bundleInfo == nullptr) {
476         HILOG_ERROR("want is nullptr");
477         return ERR_INVALID_VALUE;
478     }
479     auto observer = data.ReadRemoteObject();
480     int32_t userId = data.ReadInt32();
481     int32_t result = StartUserTestProcess(*want, observer, *bundleInfo, userId);
482     reply.WriteInt32(result);
483     return result;
484 }
485 
HandleFinishUserTest(MessageParcel & data,MessageParcel & reply)486 int32_t AppMgrStub::HandleFinishUserTest(MessageParcel &data, MessageParcel &reply)
487 {
488     std::string msg = data.ReadString();
489     int64_t resultCode = data.ReadInt64();
490     std::string bundleName = data.ReadString();
491     int32_t result = FinishUserTest(msg, resultCode, bundleName);
492     reply.WriteInt32(result);
493     return result;
494 }
495 
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer,const std::vector<std::string> & bundleNameList)496 int32_t AppMgrStub::RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer,
497     const std::vector<std::string> &bundleNameList)
498 {
499     return NO_ERROR;
500 }
501 
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)502 int32_t AppMgrStub::UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
503 {
504     return NO_ERROR;
505 }
506 
GetForegroundApplications(std::vector<AppStateData> & list)507 int32_t AppMgrStub::GetForegroundApplications(std::vector<AppStateData> &list)
508 {
509     return NO_ERROR;
510 }
511 
HandleScheduleAcceptWantDone(MessageParcel & data,MessageParcel & reply)512 int32_t AppMgrStub::HandleScheduleAcceptWantDone(MessageParcel &data, MessageParcel &reply)
513 {
514     auto recordId = data.ReadInt32();
515     AAFwk::Want *want = data.ReadParcelable<AAFwk::Want>();
516     if (want == nullptr) {
517         HILOG_ERROR("want is nullptr");
518         return ERR_INVALID_VALUE;
519     }
520     auto flag = data.ReadString();
521 
522     ScheduleAcceptWantDone(recordId, *want, flag);
523     delete want;
524     return NO_ERROR;
525 }
526 
HandleScheduleNewProcessRequestDone(MessageParcel & data,MessageParcel & reply)527 int32_t AppMgrStub::HandleScheduleNewProcessRequestDone(MessageParcel &data, MessageParcel &reply)
528 {
529     auto recordId = data.ReadInt32();
530     AAFwk::Want *want = data.ReadParcelable<AAFwk::Want>();
531     if (want == nullptr) {
532         HILOG_ERROR("want is nullptr");
533         return ERR_INVALID_VALUE;
534     }
535     auto flag = data.ReadString();
536 
537     ScheduleNewProcessRequestDone(recordId, *want, flag);
538     delete want;
539     return NO_ERROR;
540 }
541 
HandleGetAbilityRecordsByProcessID(MessageParcel & data,MessageParcel & reply)542 int32_t AppMgrStub::HandleGetAbilityRecordsByProcessID(MessageParcel &data, MessageParcel &reply)
543 {
544     HITRACE_METER(HITRACE_TAG_APP);
545     int32_t pid = data.ReadInt32();
546     std::vector<sptr<IRemoteObject>> tokens;
547     auto result = GetAbilityRecordsByProcessID(pid, tokens);
548     reply.WriteInt32(tokens.size());
549     for (auto &it : tokens) {
550         if (!reply.WriteRemoteObject(it)) {
551             HILOG_ERROR("failed to write query result.");
552             return ERR_FLATTEN_OBJECT;
553         }
554     }
555     if (!reply.WriteInt32(result)) {
556         return ERR_INVALID_VALUE;
557     }
558     return NO_ERROR;
559 }
560 
HandlePreStartNWebSpawnProcess(MessageParcel & data,MessageParcel & reply)561 int32_t AppMgrStub::HandlePreStartNWebSpawnProcess(MessageParcel &data, MessageParcel &reply)
562 {
563     HILOG_INFO("HandlePreNWebSpawnProcess");
564     int32_t result = PreStartNWebSpawnProcess();
565     if (!reply.WriteInt32(result)) {
566         HILOG_ERROR("write result error.");
567         return ERR_INVALID_VALUE;
568     }
569     return result;
570 }
571 
HandleStartRenderProcess(MessageParcel & data,MessageParcel & reply)572 int32_t AppMgrStub::HandleStartRenderProcess(MessageParcel &data, MessageParcel &reply)
573 {
574     std::string renderParam = data.ReadString();
575     int32_t ipcFd = data.ReadFileDescriptor();
576     int32_t sharedFd = data.ReadFileDescriptor();
577     int32_t crashFd = data.ReadFileDescriptor();
578     int32_t renderPid = 0;
579     int32_t result =
580         StartRenderProcess(renderParam, ipcFd, sharedFd, crashFd, renderPid);
581     if (!reply.WriteInt32(result)) {
582         HILOG_ERROR("write result error.");
583         return ERR_INVALID_VALUE;
584     }
585     if (!reply.WriteInt32(renderPid)) {
586         HILOG_ERROR("write renderPid error.");
587         return ERR_INVALID_VALUE;
588     }
589     return result;
590 }
591 
HandleAttachRenderProcess(MessageParcel & data,MessageParcel & reply)592 int32_t AppMgrStub::HandleAttachRenderProcess(MessageParcel &data, MessageParcel &reply)
593 {
594     sptr<IRemoteObject> scheduler = data.ReadRemoteObject();
595     AttachRenderProcess(scheduler);
596     return NO_ERROR;
597 }
598 
HandleGetRenderProcessTerminationStatus(MessageParcel & data,MessageParcel & reply)599 int32_t AppMgrStub::HandleGetRenderProcessTerminationStatus(MessageParcel &data, MessageParcel &reply)
600 {
601     int32_t renderPid = data.ReadInt32();
602     if (renderPid <= 0) {
603         HILOG_ERROR("invalid renderPid value");
604         return ERR_INVALID_VALUE;
605     }
606     int status = 0;
607     int32_t result = GetRenderProcessTerminationStatus(renderPid, status);
608     if (!reply.WriteInt32(result)) {
609         HILOG_ERROR("write result error.");
610         return ERR_INVALID_VALUE;
611     }
612     if (!reply.WriteInt32(status)) {
613         HILOG_ERROR("write status error.");
614         return ERR_INVALID_VALUE;
615     }
616     return result;
617 }
618 
HandleGetConfiguration(MessageParcel & data,MessageParcel & reply)619 int32_t AppMgrStub::HandleGetConfiguration(MessageParcel &data, MessageParcel &reply)
620 {
621     Configuration config;
622     int ret = GetConfiguration(config);
623     if (ret != ERR_OK) {
624         HILOG_ERROR("AppMgrStub GetConfiguration error");
625         return ERR_INVALID_VALUE;
626     }
627     if (!reply.WriteParcelable(&config)) {
628         HILOG_ERROR("AppMgrStub GetConfiguration error");
629         return ERR_INVALID_VALUE;
630     }
631     if (!reply.WriteInt32(ret)) {
632         return ERR_INVALID_VALUE;
633     }
634     return NO_ERROR;
635 }
636 
HandleUpdateConfiguration(MessageParcel & data,MessageParcel & reply)637 int32_t AppMgrStub::HandleUpdateConfiguration(MessageParcel &data, MessageParcel &reply)
638 {
639     std::unique_ptr<Configuration> config(data.ReadParcelable<Configuration>());
640     if (!config) {
641         HILOG_ERROR("AppMgrStub read configuration error");
642         return ERR_INVALID_VALUE;
643     }
644     int32_t ret = UpdateConfiguration(*config);
645     if (!reply.WriteInt32(ret)) {
646         return ERR_INVALID_VALUE;
647     }
648     return NO_ERROR;
649 }
650 
HandleRegisterConfigurationObserver(MessageParcel & data,MessageParcel & reply)651 int32_t AppMgrStub::HandleRegisterConfigurationObserver(MessageParcel &data, MessageParcel &reply)
652 {
653     auto observer = iface_cast<AppExecFwk::IConfigurationObserver>(data.ReadRemoteObject());
654     int32_t result = RegisterConfigurationObserver(observer);
655     reply.WriteInt32(result);
656     return NO_ERROR;
657 }
658 
HandleUnregisterConfigurationObserver(MessageParcel & data,MessageParcel & reply)659 int32_t AppMgrStub::HandleUnregisterConfigurationObserver(MessageParcel &data, MessageParcel &reply)
660 {
661     auto observer = iface_cast<AppExecFwk::IConfigurationObserver>(data.ReadRemoteObject());
662     int32_t result = UnregisterConfigurationObserver(observer);
663     reply.WriteInt32(result);
664     return NO_ERROR;
665 }
666 
667 #ifdef ABILITY_COMMAND_FOR_TEST
HandleBlockAppServiceDone(MessageParcel & data,MessageParcel & reply)668 int32_t AppMgrStub::HandleBlockAppServiceDone(MessageParcel &data, MessageParcel &reply)
669 {
670     HILOG_INFO("%{public}s", __func__);
671     int32_t result = BlockAppService();
672     reply.WriteInt32(result);
673     return result;
674 }
675 #endif
676 
HandleGetAppRunningStateByBundleName(MessageParcel & data,MessageParcel & reply)677 int32_t AppMgrStub::HandleGetAppRunningStateByBundleName(MessageParcel &data, MessageParcel &reply)
678 {
679     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
680     HILOG_DEBUG("function called.");
681     std::string bundleName = data.ReadString();
682     auto ret = GetAppRunningStateByBundleName(bundleName);
683     if (!reply.WriteBool(ret)) {
684         return ERR_INVALID_VALUE;
685     }
686     return NO_ERROR;
687 }
688 
HandleNotifyLoadRepairPatch(MessageParcel & data,MessageParcel & reply)689 int32_t AppMgrStub::HandleNotifyLoadRepairPatch(MessageParcel &data, MessageParcel &reply)
690 {
691     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
692     HILOG_DEBUG("function called.");
693     std::string bundleName = data.ReadString();
694     auto callback = iface_cast<IQuickFixCallback>(data.ReadRemoteObject());
695     auto ret = NotifyLoadRepairPatch(bundleName, callback);
696     if (!reply.WriteInt32(ret)) {
697         return ERR_INVALID_VALUE;
698     }
699     return NO_ERROR;
700 }
701 
HandleNotifyHotReloadPage(MessageParcel & data,MessageParcel & reply)702 int32_t AppMgrStub::HandleNotifyHotReloadPage(MessageParcel &data, MessageParcel &reply)
703 {
704     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
705     HILOG_DEBUG("function called.");
706     std::string bundleName = data.ReadString();
707     auto callback = iface_cast<IQuickFixCallback>(data.ReadRemoteObject());
708     auto ret = NotifyHotReloadPage(bundleName, callback);
709     if (!reply.WriteInt32(ret)) {
710         return ERR_INVALID_VALUE;
711     }
712     return NO_ERROR;
713 }
714 
715 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
HandleSetContinuousTaskProcess(MessageParcel & data,MessageParcel & reply)716 int32_t AppMgrStub::HandleSetContinuousTaskProcess(MessageParcel &data, MessageParcel &reply)
717 {
718     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
719     HILOG_DEBUG("function called.");
720     int32_t pid = data.ReadInt32();
721     bool isContinuousTask = data.ReadBool();
722     auto ret = SetContinuousTaskProcess(pid, isContinuousTask);
723     if (!reply.WriteInt32(ret)) {
724         return ERR_INVALID_VALUE;
725     }
726     return NO_ERROR;
727 }
728 #endif
729 
HandleNotifyUnLoadRepairPatch(MessageParcel & data,MessageParcel & reply)730 int32_t AppMgrStub::HandleNotifyUnLoadRepairPatch(MessageParcel &data, MessageParcel &reply)
731 {
732     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
733     HILOG_DEBUG("function called.");
734     std::string bundleName = data.ReadString();
735     auto callback = iface_cast<IQuickFixCallback>(data.ReadRemoteObject());
736     auto ret = NotifyUnLoadRepairPatch(bundleName, callback);
737     if (!reply.WriteInt32(ret)) {
738         return ERR_INVALID_VALUE;
739     }
740     return NO_ERROR;
741 }
742 
HandleIsSharedBundleRunning(MessageParcel & data,MessageParcel & reply)743 int32_t AppMgrStub::HandleIsSharedBundleRunning(MessageParcel &data, MessageParcel &reply)
744 {
745     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
746     HILOG_DEBUG("function called.");
747     std::string bundleName = data.ReadString();
748     uint32_t versionCode = data.ReadUint32();
749     bool result = IsSharedBundleRunning(bundleName, versionCode);
750     if (!reply.WriteBool(result)) {
751         return ERR_INVALID_VALUE;
752     }
753     return NO_ERROR;
754 }
755 
HandleStartNativeProcessForDebugger(MessageParcel & data,MessageParcel & reply)756 int32_t AppMgrStub::HandleStartNativeProcessForDebugger(MessageParcel &data, MessageParcel &reply)
757 {
758     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
759     HILOG_DEBUG("function called.");
760     std::unique_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
761     if (want == nullptr) {
762         HILOG_ERROR("want is nullptr");
763         return ERR_INVALID_VALUE;
764     }
765     auto result = StartNativeProcessForDebugger(*want);
766     if (!reply.WriteInt32(result)) {
767         HILOG_ERROR("fail to write result.");
768         return ERR_INVALID_VALUE;
769     }
770     return NO_ERROR;
771 }
772 
HandleGetBundleNameByPid(MessageParcel & data,MessageParcel & reply)773 int32_t AppMgrStub::HandleGetBundleNameByPid(MessageParcel &data, MessageParcel &reply)
774 {
775     int32_t pid = data.ReadInt32();
776     std::string bundleName;
777     int32_t uid;
778     auto result = GetBundleNameByPid(pid, bundleName, uid);
779     if (result != ERR_OK) {
780         return result;
781     }
782 
783     if (!reply.WriteString(bundleName)) {
784         return ERR_INVALID_VALUE;
785     }
786 
787     if (!reply.WriteInt32(uid)) {
788         return ERR_INVALID_VALUE;
789     }
790     return NO_ERROR;
791 }
792 
HandleNotifyFault(MessageParcel & data,MessageParcel & reply)793 int32_t AppMgrStub::HandleNotifyFault(MessageParcel &data, MessageParcel &reply)
794 {
795     std::unique_ptr<FaultData> faultData(data.ReadParcelable<FaultData>());
796     if (faultData == nullptr) {
797         HILOG_ERROR("ReadParcelable<FaultData> failed");
798         return ERR_INVALID_VALUE;
799     }
800 
801     int32_t result = NotifyAppFault(*faultData);
802     if (!reply.WriteInt32(result)) {
803         HILOG_ERROR("reply write failed.");
804         return ERR_INVALID_VALUE;
805     }
806     return NO_ERROR;
807 }
808 
HandleNotifyFaultBySA(MessageParcel & data,MessageParcel & reply)809 int32_t AppMgrStub::HandleNotifyFaultBySA(MessageParcel &data, MessageParcel &reply)
810 {
811     std::unique_ptr<AppFaultDataBySA> faultData(data.ReadParcelable<AppFaultDataBySA>());
812     if (faultData == nullptr) {
813         HILOG_ERROR("ReadParcelable<AppFaultDataBySA> failed");
814         return ERR_INVALID_VALUE;
815     }
816 
817     int32_t result = NotifyAppFaultBySA(*faultData);
818     if (!reply.WriteInt32(result)) {
819         HILOG_ERROR("reply write failed.");
820         return ERR_INVALID_VALUE;
821     }
822     return NO_ERROR;
823 }
824 
HandleGetProcessMemoryByPid(MessageParcel & data,MessageParcel & reply)825 int32_t AppMgrStub::HandleGetProcessMemoryByPid(MessageParcel &data, MessageParcel &reply)
826 {
827     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
828     int32_t pid = data.ReadInt32();
829     int32_t memorySize = 0;
830     auto result = GetProcessMemoryByPid(pid, memorySize);
831     if (!reply.WriteInt32(memorySize)) {
832         HILOG_ERROR("Memory size write failed.");
833         return ERR_INVALID_VALUE;
834     }
835     if (!reply.WriteInt32(result)) {
836         HILOG_ERROR("fail to write result.");
837         return ERR_INVALID_VALUE;
838     }
839     return NO_ERROR;
840 }
841 
HandleGetRunningProcessInformation(MessageParcel & data,MessageParcel & reply)842 int32_t AppMgrStub::HandleGetRunningProcessInformation(MessageParcel &data, MessageParcel &reply)
843 {
844     HITRACE_METER(HITRACE_TAG_APP);
845     std::string bundleName = data.ReadString();
846     int32_t userId = data.ReadInt32();
847     std::vector<RunningProcessInfo> info;
848     auto result = GetRunningProcessInformation(bundleName, userId, info);
849     reply.WriteInt32(info.size());
850     for (auto &it : info) {
851         if (!reply.WriteParcelable(&it)) {
852             return ERR_INVALID_VALUE;
853         }
854     }
855     if (!reply.WriteInt32(result)) {
856         HILOG_ERROR("fail to write result.");
857         return ERR_INVALID_VALUE;
858     }
859     return NO_ERROR;
860 }
861 
HandleChangeAppGcState(MessageParcel & data,MessageParcel & reply)862 int32_t AppMgrStub::HandleChangeAppGcState(MessageParcel &data, MessageParcel &reply)
863 {
864     HITRACE_METER(HITRACE_TAG_APP);
865     int32_t pid = data.ReadInt32();
866     int32_t state = data.ReadInt32();
867     int32_t ret = ChangeAppGcState(pid, state);
868     reply.WriteInt32(ret);
869     return NO_ERROR;
870 }
871 
HandleNotifyPageShow(MessageParcel & data,MessageParcel & reply)872 int32_t AppMgrStub::HandleNotifyPageShow(MessageParcel &data, MessageParcel &reply)
873 {
874     sptr<IRemoteObject> token = data.ReadRemoteObject();
875     std::unique_ptr<PageStateData> pageStateData(data.ReadParcelable<PageStateData>());
876     if (token == nullptr || pageStateData == nullptr) {
877         HILOG_ERROR("read data failed");
878         return ERR_INVALID_VALUE;
879     }
880 
881     auto result = NotifyPageShow(token, *pageStateData);
882     if (!reply.WriteInt32(result)) {
883         HILOG_ERROR("fail to write result.");
884         return ERR_INVALID_VALUE;
885     }
886     return NO_ERROR;
887 }
888 
HandleNotifyPageHide(MessageParcel & data,MessageParcel & reply)889 int32_t AppMgrStub::HandleNotifyPageHide(MessageParcel &data, MessageParcel &reply)
890 {
891     sptr<IRemoteObject> token = data.ReadRemoteObject();
892     std::unique_ptr<PageStateData> pageStateData(data.ReadParcelable<PageStateData>());
893     if (token == nullptr || pageStateData == nullptr) {
894         HILOG_ERROR("read data failed");
895         return ERR_INVALID_VALUE;
896     }
897 
898     auto result = NotifyPageHide(token, *pageStateData);
899     if (!reply.WriteInt32(result)) {
900         HILOG_ERROR("fail to write result.");
901         return ERR_INVALID_VALUE;
902     }
903     return NO_ERROR;
904 }
905 
HandleRegisterAppRunningStatusListener(MessageParcel & data,MessageParcel & reply)906 int32_t AppMgrStub::HandleRegisterAppRunningStatusListener(MessageParcel &data, MessageParcel &reply)
907 {
908     auto listener = data.ReadRemoteObject();
909     if (listener == nullptr) {
910         HILOG_ERROR("Read data failed.");
911         return ERR_INVALID_VALUE;
912     }
913 
914     auto result = RegisterAppRunningStatusListener(listener);
915     if (!reply.WriteInt32(result)) {
916         HILOG_ERROR("Fail to write result.");
917         return ERR_INVALID_VALUE;
918     }
919     return NO_ERROR;
920 }
921 
HandleUnregisterAppRunningStatusListener(MessageParcel & data,MessageParcel & reply)922 int32_t AppMgrStub::HandleUnregisterAppRunningStatusListener(MessageParcel &data, MessageParcel &reply)
923 {
924     auto listener = data.ReadRemoteObject();
925     if (listener == nullptr) {
926         HILOG_ERROR("Read data failed.");
927         return ERR_INVALID_VALUE;
928     }
929 
930     auto result = UnregisterAppRunningStatusListener(listener);
931     if (!reply.WriteInt32(result)) {
932         HILOG_ERROR("Fail to write result.");
933         return ERR_INVALID_VALUE;
934     }
935     return NO_ERROR;
936 }
937 
HandleRegisterAppForegroundStateObserver(MessageParcel & data,MessageParcel & reply)938 int32_t AppMgrStub::HandleRegisterAppForegroundStateObserver(MessageParcel &data, MessageParcel &reply)
939 {
940     auto callback = iface_cast<AppExecFwk::IAppForegroundStateObserver>(data.ReadRemoteObject());
941     if (callback == nullptr) {
942         HILOG_ERROR("Callback is null.");
943         return ERR_INVALID_VALUE;
944     }
945     int32_t result = RegisterAppForegroundStateObserver(callback);
946     if (!reply.WriteInt32(result)) {
947         HILOG_ERROR("Fail to write result.");
948         return ERR_INVALID_VALUE;
949     }
950     return NO_ERROR;
951 }
952 
HandleUnregisterAppForegroundStateObserver(MessageParcel & data,MessageParcel & reply)953 int32_t AppMgrStub::HandleUnregisterAppForegroundStateObserver(MessageParcel &data, MessageParcel &reply)
954 {
955     auto callback = iface_cast<AppExecFwk::IAppForegroundStateObserver>(data.ReadRemoteObject());
956     if (callback == nullptr) {
957         HILOG_ERROR("Callback is null.");
958         return ERR_INVALID_VALUE;
959     }
960     int32_t result = UnregisterAppForegroundStateObserver(callback);
961     if (!reply.WriteInt32(result)) {
962         HILOG_ERROR("Fail to write result.");
963         return ERR_INVALID_VALUE;
964     }
965     return NO_ERROR;
966 }
967 
HandleIsApplicationRunning(MessageParcel & data,MessageParcel & reply)968 int32_t AppMgrStub::HandleIsApplicationRunning(MessageParcel &data, MessageParcel &reply)
969 {
970     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
971     HILOG_DEBUG("Called.");
972     std::string bundleName = data.ReadString();
973     bool isRunning = false;
974     int32_t result = IsApplicationRunning(bundleName, isRunning);
975     if (!reply.WriteBool(isRunning)) {
976         return ERR_INVALID_VALUE;
977     }
978     if (!reply.WriteInt32(result)) {
979         return ERR_INVALID_VALUE;
980     }
981     return NO_ERROR;
982 }
983 
HandleStartChildProcess(MessageParcel & data,MessageParcel & reply)984 int32_t AppMgrStub::HandleStartChildProcess(MessageParcel &data, MessageParcel &reply)
985 {
986     HILOG_DEBUG("called.");
987     std::string srcEntry = data.ReadString();
988     int32_t childPid = 0;
989     int32_t result = StartChildProcess(srcEntry, childPid);
990     if (!reply.WriteInt32(result)) {
991         HILOG_ERROR("Write result error.");
992         return ERR_INVALID_VALUE;
993     }
994     if (result == ERR_OK && !reply.WriteInt32(childPid)) {
995         HILOG_ERROR("Write childPid error.");
996         return ERR_INVALID_VALUE;
997     }
998     return NO_ERROR;
999 }
1000 
HandleGetChildProcessInfoForSelf(MessageParcel & data,MessageParcel & reply)1001 int32_t AppMgrStub::HandleGetChildProcessInfoForSelf(MessageParcel &data, MessageParcel &reply)
1002 {
1003     HILOG_DEBUG("called.");
1004     ChildProcessInfo info;
1005     auto result = GetChildProcessInfoForSelf(info);
1006     if (!reply.WriteInt32(result)) {
1007         HILOG_ERROR("Write result error.");
1008         return ERR_INVALID_VALUE;
1009     }
1010     if (result == ERR_OK && !reply.WriteParcelable(&info)) {
1011         return ERR_INVALID_VALUE;
1012     }
1013     return NO_ERROR;
1014 }
1015 
HandleAttachChildProcess(MessageParcel & data,MessageParcel & reply)1016 int32_t AppMgrStub::HandleAttachChildProcess(MessageParcel &data, MessageParcel &reply)
1017 {
1018     HILOG_DEBUG("called.");
1019     sptr<IRemoteObject> scheduler = data.ReadRemoteObject();
1020     AttachChildProcess(scheduler);
1021     return NO_ERROR;
1022 }
1023 
HandleExitChildProcessSafely(MessageParcel & data,MessageParcel & reply)1024 int32_t AppMgrStub::HandleExitChildProcessSafely(MessageParcel &data, MessageParcel &reply)
1025 {
1026     HILOG_DEBUG("called.");
1027     ExitChildProcessSafely();
1028     return NO_ERROR;
1029 }
1030 
HandleIsFinalAppProcess(MessageParcel & data,MessageParcel & reply)1031 int32_t AppMgrStub::HandleIsFinalAppProcess(MessageParcel &data, MessageParcel &reply)
1032 {
1033     HILOG_DEBUG("Called.");
1034     if (!reply.WriteBool(IsFinalAppProcess())) {
1035         HILOG_ERROR("Fail to write bool result.");
1036         return ERR_INVALID_VALUE;
1037     }
1038     return NO_ERROR;
1039 }
1040 }  // namespace AppExecFwk
1041 }  // namespace OHOS
1042