• 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_mgr_proxy.h"
17 
18 #include "ability_manager_errors.h"
19 #include "appexecfwk_errors.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 "parcel_util.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 constexpr int32_t CYCLE_LIMIT = 1000;
30 constexpr int32_t MAX_PROCESS_STATE_COUNT = 1000;
AppMgrProxy(const sptr<IRemoteObject> & impl)31 AppMgrProxy::AppMgrProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IAppMgr>(impl)
32 {}
33 
WriteInterfaceToken(MessageParcel & data)34 bool AppMgrProxy::WriteInterfaceToken(MessageParcel &data)
35 {
36     if (!data.WriteInterfaceToken(AppMgrProxy::GetDescriptor())) {
37         TAG_LOGE(AAFwkTag::APPMGR, "write interface token failed");
38         return false;
39     }
40     return true;
41 }
42 
AttachApplication(const sptr<IRemoteObject> & obj)43 void AppMgrProxy::AttachApplication(const sptr<IRemoteObject> &obj)
44 {
45     MessageParcel data;
46     MessageParcel reply;
47     MessageOption option(MessageOption::TF_SYNC);
48     if (!WriteInterfaceToken(data)) {
49         return;
50     }
51     if (obj == nullptr || obj.GetRefPtr() == nullptr) {
52         TAG_LOGE(AAFwkTag::APPMGR, "app scheduler null");
53     }
54     PARCEL_UTIL_WRITE_NORET(data, RemoteObject, obj.GetRefPtr());
55 
56     PARCEL_UTIL_SENDREQ_NORET(AppMgrInterfaceCode::APP_ATTACH_APPLICATION, data, reply, option);
57 }
58 
PreloadApplication(const std::string & bundleName,int32_t userId,AppExecFwk::PreloadMode preloadMode,int32_t appIndex)59 int32_t AppMgrProxy::PreloadApplication(const std::string &bundleName, int32_t userId,
60     AppExecFwk::PreloadMode preloadMode, int32_t appIndex)
61 {
62     TAG_LOGD(AAFwkTag::APPMGR, "called");
63     MessageParcel data;
64     MessageParcel reply;
65     MessageOption option(MessageOption::TF_SYNC);
66     if (!WriteInterfaceToken(data)) {
67         TAG_LOGE(AAFwkTag::APPMGR, "PreloadApplication Write interface token failed.");
68         return IPC_PROXY_ERR;
69     }
70     PARCEL_UTIL_WRITE_RET_INT(data, String16, Str8ToStr16(bundleName));
71     PARCEL_UTIL_WRITE_RET_INT(data, Int32, userId);
72     PARCEL_UTIL_WRITE_RET_INT(data, Int32, static_cast<int32_t>(preloadMode));
73     PARCEL_UTIL_WRITE_RET_INT(data, Int32, appIndex);
74 
75     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::PRELOAD_APPLICATION, data, reply, option);
76     return reply.ReadInt32();
77 }
78 
ApplicationForegrounded(const int32_t recordId)79 void AppMgrProxy::ApplicationForegrounded(const int32_t recordId)
80 {
81     MessageParcel data;
82     MessageParcel reply;
83     MessageOption option(MessageOption::TF_SYNC);
84     if (!WriteInterfaceToken(data)) {
85         return;
86     }
87     PARCEL_UTIL_WRITE_NORET(data, Int32, recordId);
88 
89     PARCEL_UTIL_SENDREQ_NORET(AppMgrInterfaceCode::APP_APPLICATION_FOREGROUNDED, data, reply, option);
90 }
91 
ApplicationBackgrounded(const int32_t recordId)92 void AppMgrProxy::ApplicationBackgrounded(const int32_t recordId)
93 {
94     MessageParcel data;
95     MessageParcel reply;
96     MessageOption option(MessageOption::TF_SYNC);
97     if (!WriteInterfaceToken(data)) {
98         return;
99     }
100     PARCEL_UTIL_WRITE_NORET(data, Int32, recordId);
101 
102     PARCEL_UTIL_SENDREQ_NORET(AppMgrInterfaceCode::APP_APPLICATION_BACKGROUNDED, data, reply, option);
103 }
104 
ApplicationTerminated(const int32_t recordId)105 void AppMgrProxy::ApplicationTerminated(const int32_t recordId)
106 {
107     MessageParcel data;
108     MessageParcel reply;
109     MessageOption option(MessageOption::TF_SYNC);
110     if (!WriteInterfaceToken(data)) {
111         return;
112     }
113     PARCEL_UTIL_WRITE_NORET(data, Int32, recordId);
114 
115     PARCEL_UTIL_SENDREQ_NORET(AppMgrInterfaceCode::APP_APPLICATION_TERMINATED, data, reply, option);
116 }
117 
AbilityCleaned(const sptr<IRemoteObject> & token)118 void AppMgrProxy::AbilityCleaned(const sptr<IRemoteObject> &token)
119 {
120     MessageParcel data;
121     MessageParcel reply;
122     MessageOption option(MessageOption::TF_SYNC);
123     if (!WriteInterfaceToken(data)) {
124         return;
125     }
126     PARCEL_UTIL_WRITE_NORET(data, RemoteObject, token.GetRefPtr());
127 
128     PARCEL_UTIL_SENDREQ_NORET(AppMgrInterfaceCode::APP_ABILITY_CLEANED, data, reply, option);
129 }
130 
GetAmsMgr()131 sptr<IAmsMgr> AppMgrProxy::GetAmsMgr()
132 {
133     MessageParcel data;
134     MessageParcel reply;
135     if (!WriteInterfaceToken(data)) {
136         return nullptr;
137     }
138     if (!SendTransactCmd(AppMgrInterfaceCode::APP_GET_MGR_INSTANCE, data, reply)) {
139         return nullptr;
140     }
141     sptr<IRemoteObject> object = reply.ReadRemoteObject();
142     sptr<IAmsMgr> amsMgr = iface_cast<IAmsMgr>(object);
143     if (!amsMgr) {
144         TAG_LOGE(AAFwkTag::APPMGR, "Ability manager service instance is nullptr. ");
145         return nullptr;
146     }
147     return amsMgr;
148 }
149 
ClearUpApplicationData(const std::string & bundleName,int32_t appCloneIndex,const int32_t userId)150 int32_t AppMgrProxy::ClearUpApplicationData(const std::string &bundleName, int32_t appCloneIndex, const int32_t userId)
151 {
152     TAG_LOGI(AAFwkTag::APPMGR, "Called.");
153     MessageParcel data;
154     MessageParcel reply;
155     MessageOption option(MessageOption::TF_SYNC);
156     if (!WriteInterfaceToken(data)) {
157         return ERR_FLATTEN_OBJECT;
158     }
159     PARCEL_UTIL_WRITE_RET_INT(data, String, bundleName);
160     PARCEL_UTIL_WRITE_RET_INT(data, Int32, appCloneIndex);
161     PARCEL_UTIL_WRITE_RET_INT(data, Int32, userId);
162 
163     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::APP_CLEAR_UP_APPLICATION_DATA, data, reply, option);
164     return reply.ReadInt32();
165 }
166 
ClearUpApplicationDataBySelf(int32_t userId)167 int32_t AppMgrProxy::ClearUpApplicationDataBySelf(int32_t userId)
168 {
169     TAG_LOGI(AAFwkTag::APPMGR, "called");
170     MessageParcel data;
171     MessageParcel reply;
172     MessageOption option(MessageOption::TF_SYNC);
173     if (!WriteInterfaceToken(data)) {
174         return ERR_FLATTEN_OBJECT;
175     }
176     PARCEL_UTIL_WRITE_RET_INT(data, Int32, userId);
177 
178     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::APP_CLEAR_UP_APPLICATION_DATA_BY_SELF, data, reply, option);
179     return reply.ReadInt32();
180 }
181 
GetAllRunningProcesses(std::vector<RunningProcessInfo> & info)182 int32_t AppMgrProxy::GetAllRunningProcesses(std::vector<RunningProcessInfo> &info)
183 {
184     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
185     MessageParcel data;
186     MessageParcel reply;
187     MessageOption option(MessageOption::TF_SYNC);
188     if (!WriteInterfaceToken(data)) {
189         return ERR_FLATTEN_OBJECT;
190     }
191     if (!SendTransactCmd(AppMgrInterfaceCode::APP_GET_ALL_RUNNING_PROCESSES, data, reply)) {
192         return ERR_NULL_OBJECT;
193     }
194     auto error = GetParcelableInfos<RunningProcessInfo>(reply, info);
195     if (error != NO_ERROR) {
196         TAG_LOGE(AAFwkTag::APPMGR, "GetParcelableInfos fail, error: %{public}d", error);
197         return error;
198     }
199     int result = reply.ReadInt32();
200     return result;
201 }
202 
GetRunningMultiAppInfoByBundleName(const std::string & bundleName,RunningMultiAppInfo & info)203 int32_t AppMgrProxy::GetRunningMultiAppInfoByBundleName(const std::string &bundleName,
204     RunningMultiAppInfo &info)
205 {
206     MessageParcel data;
207     MessageParcel reply;
208     MessageOption option(MessageOption::TF_SYNC);
209     if (!WriteInterfaceToken(data)) {
210         return ERR_FLATTEN_OBJECT;
211     }
212     PARCEL_UTIL_WRITE_RET_INT(data, String, bundleName);
213 
214     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_RUNNING_MULTIAPP_INFO_BY_BUNDLENAME, data, reply, option);
215     std::unique_ptr<RunningMultiAppInfo> infoReply(reply.ReadParcelable<RunningMultiAppInfo>());
216     if (infoReply == nullptr) {
217         TAG_LOGW(AAFwkTag::APPMGR, "reply ReadParcelable is nullptr");
218         return ERR_NULL_OBJECT;
219     }
220     info = *infoReply;
221     int result = reply.ReadInt32();
222     return result;
223 }
224 
GetAllRunningInstanceKeysBySelf(std::vector<std::string> & instanceKeys)225 int32_t AppMgrProxy::GetAllRunningInstanceKeysBySelf(std::vector<std::string> &instanceKeys)
226 {
227     MessageParcel data;
228     MessageParcel reply;
229     MessageOption option(MessageOption::TF_SYNC);
230     if (!WriteInterfaceToken(data)) {
231         return ERR_FLATTEN_OBJECT;
232     }
233 
234     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_All_RUNNING_INSTANCE_KEYS_BY_SELF, data, reply, option);
235     if (!reply.ReadStringVector(&instanceKeys)) {
236         return ERR_INVALID_DATA;
237     }
238     int32_t result = reply.ReadInt32();
239     return result;
240 }
241 
GetAllRunningInstanceKeysByBundleName(const std::string & bundleName,std::vector<std::string> & instanceKeys,int32_t userId)242 int32_t AppMgrProxy::GetAllRunningInstanceKeysByBundleName(const std::string &bundleName,
243     std::vector<std::string> &instanceKeys, int32_t userId)
244 {
245     MessageParcel data;
246     MessageParcel reply;
247     MessageOption option(MessageOption::TF_SYNC);
248     if (!WriteInterfaceToken(data)) {
249         return ERR_FLATTEN_OBJECT;
250     }
251     PARCEL_UTIL_WRITE_RET_INT(data, String, bundleName);
252     PARCEL_UTIL_WRITE_RET_INT(data, Int32, userId);
253 
254     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_All_RUNNING_INSTANCE_KEYS_BY_BUNDLENAME, data, reply, option);
255     if (!reply.ReadStringVector(&instanceKeys)) {
256         return ERR_INVALID_DATA;
257     }
258     int32_t result = reply.ReadInt32();
259     return result;
260 }
261 
GetRunningProcessesByBundleType(const BundleType bundleType,std::vector<RunningProcessInfo> & info)262 int32_t AppMgrProxy::GetRunningProcessesByBundleType(const BundleType bundleType,
263     std::vector<RunningProcessInfo> &info)
264 {
265     MessageParcel data;
266     MessageParcel reply;
267     MessageOption option(MessageOption::TF_SYNC);
268     if (!WriteInterfaceToken(data)) {
269         return ERR_FLATTEN_OBJECT;
270     }
271     PARCEL_UTIL_WRITE_RET_INT(data, Int32, static_cast<int32_t>(bundleType));
272 
273     if (!SendTransactCmd(AppMgrInterfaceCode::APP_GET_RUNNING_PROCESSES_BY_BUNDLE_TYPE, data, reply)) {
274         return ERR_NULL_OBJECT;
275     }
276     auto error = GetParcelableInfos<RunningProcessInfo>(reply, info);
277     if (error != NO_ERROR) {
278         TAG_LOGE(AAFwkTag::APPMGR, "GetParcelableInfos fail, error: %{public}d", error);
279         return error;
280     }
281     int result = reply.ReadInt32();
282     return result;
283 }
284 
GetAllRenderProcesses(std::vector<RenderProcessInfo> & info)285 int32_t AppMgrProxy::GetAllRenderProcesses(std::vector<RenderProcessInfo> &info)
286 {
287     MessageParcel data;
288     MessageParcel reply;
289     MessageOption option(MessageOption::TF_SYNC);
290     if (!WriteInterfaceToken(data)) {
291         return ERR_FLATTEN_OBJECT;
292     }
293     if (!SendTransactCmd(AppMgrInterfaceCode::APP_GET_ALL_RENDER_PROCESSES, data, reply)) {
294         return ERR_NULL_OBJECT;
295     }
296     auto error = GetParcelableInfos<RenderProcessInfo>(reply, info);
297     if (error != NO_ERROR) {
298         TAG_LOGE(AAFwkTag::APPMGR, "GetParcelableInfos fail, error: %{public}d", error);
299         return error;
300     }
301     int result = reply.ReadInt32();
302     return result;
303 }
304 
GetAllChildrenProcesses(std::vector<ChildProcessInfo> & info)305 int AppMgrProxy::GetAllChildrenProcesses(std::vector<ChildProcessInfo> &info)
306 {
307     MessageParcel data;
308     MessageParcel reply;
309 #ifdef SUPPORT_CHILD_PROCESS
310     MessageOption option(MessageOption::TF_SYNC);
311     if (!WriteInterfaceToken(data)) {
312         return ERR_FLATTEN_OBJECT;
313     }
314     if (!SendTransactCmd(AppMgrInterfaceCode::GET_ALL_CHILDREN_PROCESSES, data, reply)) {
315         return ERR_NULL_OBJECT;
316     }
317     auto error = GetParcelableInfos<ChildProcessInfo>(reply, info);
318     if (error != NO_ERROR) {
319         TAG_LOGE(AAFwkTag::APPMGR, "GetParcelableInfos fail, error: %{public}d", error);
320         return error;
321     }
322 #endif // SUPPORT_CHILD_PROCESS
323     int result = reply.ReadInt32();
324     return result;
325 }
326 
JudgeSandboxByPid(pid_t pid,bool & isSandbox)327 int32_t AppMgrProxy::JudgeSandboxByPid(pid_t pid, bool &isSandbox)
328 {
329     MessageParcel data;
330     MessageParcel reply;
331     MessageOption option;
332     if (!WriteInterfaceToken(data)) {
333         return ERR_FLATTEN_OBJECT;
334     }
335     PARCEL_UTIL_WRITE_RET_INT(data, Int32, pid);
336 
337     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::JUDGE_SANDBOX_BY_PID, data, reply, option);
338     isSandbox = reply.ReadBool();
339     return reply.ReadInt32();
340 }
341 
IsTerminatingByPid(pid_t pid,bool & isTerminating)342 int32_t AppMgrProxy::IsTerminatingByPid(pid_t pid, bool &isTerminating)
343 {
344     MessageParcel data;
345     MessageParcel reply;
346     MessageOption option;
347     if (!WriteInterfaceToken(data)) {
348         return ERR_FLATTEN_OBJECT;
349     }
350     PARCEL_UTIL_WRITE_RET_INT(data, Int32, pid);
351 
352     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::IS_TERMINATING_BY_PID, data, reply, option);
353     isTerminating = reply.ReadBool();
354     return reply.ReadInt32();
355 }
356 
GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> & info,int32_t userId)357 int32_t AppMgrProxy::GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId)
358 {
359     MessageParcel data;
360     MessageParcel reply;
361     MessageOption option(MessageOption::TF_SYNC);
362 
363     if (!WriteInterfaceToken(data)) {
364         return ERR_FLATTEN_OBJECT;
365     }
366     PARCEL_UTIL_WRITE_RET_INT(data, Int32, userId);
367 
368     if (!SendTransactCmd(AppMgrInterfaceCode::APP_GET_RUNNING_PROCESSES_BY_USER_ID, data, reply)) {
369         return ERR_NULL_OBJECT;
370     }
371     auto error = GetParcelableInfos<RunningProcessInfo>(reply, info);
372     if (error != NO_ERROR) {
373         TAG_LOGE(AAFwkTag::APPMGR, "GetParcelableInfos fail, error: %{public}d", error);
374         return error;
375     }
376     int result = reply.ReadInt32();
377     return result;
378 }
379 
GetProcessRunningInformation(RunningProcessInfo & info)380 int32_t AppMgrProxy::GetProcessRunningInformation(RunningProcessInfo &info)
381 {
382     MessageParcel data;
383     MessageParcel reply;
384 
385     if (!WriteInterfaceToken(data)) {
386         return ERR_FLATTEN_OBJECT;
387     }
388     if (!SendTransactCmd(AppMgrInterfaceCode::APP_GET_PROCESS_RUNNING_INFORMATION, data, reply)) {
389         return ERR_NULL_OBJECT;
390     }
391     std::unique_ptr<RunningProcessInfo> infoReply(reply.ReadParcelable<RunningProcessInfo>());
392     if (infoReply == nullptr) {
393         TAG_LOGW(AAFwkTag::APPMGR, "reply ReadParcelable is nullptr");
394         return ERR_NULL_OBJECT;
395     }
396     info = *infoReply;
397     return reply.ReadInt32();
398 }
399 
NotifyMemoryLevel(int32_t level)400 int32_t AppMgrProxy::NotifyMemoryLevel(int32_t level)
401 {
402     MessageParcel data;
403     MessageParcel reply;
404     MessageOption option(MessageOption::TF_SYNC);
405 
406     if (!WriteInterfaceToken(data)) {
407         return ERR_FLATTEN_OBJECT;
408     }
409     PARCEL_UTIL_WRITE_RET_INT(data, Int32, level);
410 
411     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::APP_NOTIFY_MEMORY_LEVEL, data, reply, option);
412     return reply.ReadInt32();
413 }
414 
NotifyProcMemoryLevel(const std::map<pid_t,MemoryLevel> & procLevelMap)415 int32_t AppMgrProxy::NotifyProcMemoryLevel(const std::map<pid_t, MemoryLevel> &procLevelMap)
416 {
417     MessageParcel data;
418     MessageParcel reply;
419     MessageOption option(MessageOption::TF_SYNC);
420 
421     if (!WriteInterfaceToken(data)) {
422         return ERR_FLATTEN_OBJECT;
423     }
424     MemoryLevelInfo memoryLevelInfo(procLevelMap);
425     PARCEL_UTIL_WRITE_RET_INT(data, Parcelable, &memoryLevelInfo);
426 
427     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::APP_NOTIFY_PROC_MEMORY_LEVEL, data, reply, option);
428     return reply.ReadInt32();
429 }
430 
DumpHeapMemory(const int32_t pid,OHOS::AppExecFwk::MallocInfo & mallocInfo)431 int32_t AppMgrProxy::DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo)
432 {
433     TAG_LOGD(AAFwkTag::APPMGR, "AppMgrProxy::DumpHeapMemory.");
434     MessageParcel data;
435     MessageParcel reply;
436     if (!WriteInterfaceToken(data)) {
437         return ERR_FLATTEN_OBJECT;
438     }
439     PARCEL_UTIL_WRITE_RET_INT(data, Int32, pid);
440 
441     MessageOption option(MessageOption::TF_SYNC);
442     int32_t ret = SendRequest(AppMgrInterfaceCode::DUMP_HEAP_MEMORY_PROCESS, data, reply, option);
443     if (ret != NO_ERROR) {
444         TAG_LOGE(AAFwkTag::APPMGR, "AppMgrProxy SendRequest is failed, error code: %{public}d", ret);
445         return ret;
446     }
447 
448     std::unique_ptr<MallocInfo> info(reply.ReadParcelable<MallocInfo>());
449     if (info == nullptr) {
450         TAG_LOGE(AAFwkTag::APPMGR, "MallocInfo ReadParcelable nullptr");
451         return ERR_NULL_OBJECT;
452     }
453     mallocInfo = *info;
454     return ret;
455 }
456 
DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo & info)457 int32_t AppMgrProxy::DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info)
458 {
459     TAG_LOGD(AAFwkTag::APPMGR, "AppMgrProxy::DumpJsHeapMemory.");
460     MessageParcel data;
461     MessageParcel reply;
462     MessageOption option(MessageOption::TF_SYNC);
463     if (!WriteInterfaceToken(data)) {
464         return ERR_FLATTEN_OBJECT;
465     }
466     PARCEL_UTIL_WRITE_RET_INT(data, Parcelable, &info);
467 
468     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::DUMP_JSHEAP_MEMORY_PROCESS, data, reply, option);
469     return reply.ReadInt32();
470 }
471 
SendTransactCmd(AppMgrInterfaceCode code,MessageParcel & data,MessageParcel & reply)472 bool AppMgrProxy::SendTransactCmd(AppMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply)
473 {
474     MessageOption option(MessageOption::TF_SYNC);
475     int32_t result = SendRequest(code, data, reply, option);
476     if (result != NO_ERROR) {
477         TAG_LOGE(AAFwkTag::APPMGR, "receive error transact code %{public}d in transact cmd %{public}d", result, code);
478         return false;
479     }
480     return true;
481 }
482 
AddAbilityStageDone(const int32_t recordId)483 void AppMgrProxy::AddAbilityStageDone(const int32_t recordId)
484 {
485     MessageParcel data;
486     MessageParcel reply;
487     if (!WriteInterfaceToken(data)) {
488         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
489         return;
490     }
491 
492     if (!data.WriteInt32(recordId)) {
493         TAG_LOGE(AAFwkTag::APPMGR, "want write failed.");
494         return;
495     }
496 
497     if (!SendTransactCmd(AppMgrInterfaceCode::APP_ADD_ABILITY_STAGE_INFO_DONE, data, reply)) {
498         TAG_LOGE(AAFwkTag::APPMGR, "SendTransactCmd failed");
499         return;
500     }
501     return;
502 }
503 
StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> & bundleInfos)504 void AppMgrProxy::StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos)
505 {
506     MessageParcel data;
507     MessageParcel reply;
508     if (!WriteInterfaceToken(data)) {
509         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
510         return;
511     }
512 
513     if (!data.WriteInt32(bundleInfos.size())) {
514         TAG_LOGE(AAFwkTag::APPMGR, "write bundle info size failed.");
515         return;
516     }
517 
518     for (auto &bundleInfo : bundleInfos) {
519         if (!data.WriteParcelable(&bundleInfo)) {
520             TAG_LOGE(AAFwkTag::APPMGR, "write bundle info failed");
521             return;
522         }
523     }
524 
525     if (!SendTransactCmd(AppMgrInterfaceCode::STARTUP_RESIDENT_PROCESS, data, reply)) {
526         TAG_LOGE(AAFwkTag::APPMGR, "SendTransactCmd failed");
527         return;
528     }
529     return;
530 }
531 
532 template<typename T>
GetParcelableInfos(MessageParcel & reply,std::vector<T> & parcelableInfos)533 int AppMgrProxy::GetParcelableInfos(MessageParcel &reply, std::vector<T> &parcelableInfos)
534 {
535     int32_t infoSize = reply.ReadInt32();
536     if (infoSize > CYCLE_LIMIT) {
537         TAG_LOGE(AAFwkTag::APPMGR, "infoSize is too large");
538         return ERR_INVALID_VALUE;
539     }
540     for (int32_t i = 0; i < infoSize; i++) {
541         std::unique_ptr<T> info(reply.ReadParcelable<T>());
542         if (!info) {
543             TAG_LOGE(AAFwkTag::APPMGR, "Read Parcelable infos failed");
544             return ERR_INVALID_VALUE;
545         }
546         parcelableInfos.emplace_back(*info);
547     }
548     TAG_LOGD(AAFwkTag::APPMGR, "get parcelable infos success");
549     return NO_ERROR;
550 }
551 
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer,const std::vector<std::string> & bundleNameList)552 int AppMgrProxy::RegisterApplicationStateObserver(
553     const sptr<IApplicationStateObserver> &observer, const std::vector<std::string> &bundleNameList)
554 {
555     if (!observer) {
556         TAG_LOGE(AAFwkTag::APPMGR, "observer null");
557         return ERR_INVALID_VALUE;
558     }
559     TAG_LOGD(AAFwkTag::APPMGR, "RegisterApplicationStateObserver start");
560     MessageParcel data;
561     MessageParcel reply;
562     MessageOption option;
563     if (!WriteInterfaceToken(data)) {
564         return ERR_FLATTEN_OBJECT;
565     }
566     if (!data.WriteRemoteObject(observer->AsObject())) {
567         TAG_LOGE(AAFwkTag::APPMGR, "observer write failed.");
568         return ERR_FLATTEN_OBJECT;
569     }
570     if (!data.WriteStringVector(bundleNameList)) {
571         TAG_LOGE(AAFwkTag::APPMGR, "bundleNameList write failed.");
572         return ERR_FLATTEN_OBJECT;
573     }
574 
575     auto error = SendRequest(AppMgrInterfaceCode::REGISTER_APPLICATION_STATE_OBSERVER,
576         data, reply, option);
577     if (error != NO_ERROR) {
578         TAG_LOGE(AAFwkTag::APPMGR, "Send request error: %{public}d", error);
579         return error;
580     }
581     return reply.ReadInt32();
582 }
583 
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)584 int AppMgrProxy::UnregisterApplicationStateObserver(
585     const sptr<IApplicationStateObserver> &observer)
586 {
587     if (!observer) {
588         TAG_LOGE(AAFwkTag::APPMGR, "observer null");
589         return ERR_INVALID_VALUE;
590     }
591     TAG_LOGD(AAFwkTag::APPMGR, "UnregisterApplicationStateObserver start");
592     MessageParcel data;
593     MessageParcel reply;
594     MessageOption option;
595     if (!WriteInterfaceToken(data)) {
596         return ERR_FLATTEN_OBJECT;
597     }
598     PARCEL_UTIL_WRITE_RET_INT(data, RemoteObject, observer->AsObject());
599 
600     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::UNREGISTER_APPLICATION_STATE_OBSERVER, data, reply, option);
601     return reply.ReadInt32();
602 }
603 
RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> & observer)604 int32_t AppMgrProxy::RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer)
605 {
606     TAG_LOGD(AAFwkTag::APPMGR, "called");
607     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
608     if (observer == nullptr) {
609         TAG_LOGE(AAFwkTag::APPMGR, "Observer is null.");
610         return ERR_INVALID_VALUE;
611     }
612 
613     MessageParcel data;
614     if (!WriteInterfaceToken(data)) {
615         return ERR_FLATTEN_OBJECT;
616     }
617     PARCEL_UTIL_WRITE_RET_INT(data, RemoteObject, observer->AsObject());
618     MessageParcel reply;
619     MessageOption option;
620 
621     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::REGISTER_ABILITY_FOREGROUND_STATE_OBSERVER, data, reply, option);
622     return reply.ReadInt32();
623 }
624 
UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> & observer)625 int32_t AppMgrProxy::UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer)
626 {
627     TAG_LOGD(AAFwkTag::APPMGR, "called");
628     if (observer == nullptr) {
629         TAG_LOGE(AAFwkTag::APPMGR, "Observer is null.");
630         return ERR_INVALID_VALUE;
631     }
632 
633     MessageParcel data;
634     if (!WriteInterfaceToken(data)) {
635         return ERR_FLATTEN_OBJECT;
636     }
637     PARCEL_UTIL_WRITE_RET_INT(data, RemoteObject, observer->AsObject());
638     MessageParcel reply;
639     MessageOption option;
640     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::UNREGISTER_ABILITY_FOREGROUND_STATE_OBSERVER, data, reply, option);
641     return reply.ReadInt32();
642 }
643 
GetForegroundApplications(std::vector<AppStateData> & list)644 int AppMgrProxy::GetForegroundApplications(std::vector<AppStateData> &list)
645 {
646     MessageParcel data;
647     MessageParcel reply;
648     MessageOption option;
649     if (!WriteInterfaceToken(data)) {
650         return ERR_FLATTEN_OBJECT;
651     }
652     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_FOREGROUND_APPLICATIONS, data, reply, option);
653     int32_t infoSize = reply.ReadInt32();
654     if (infoSize > CYCLE_LIMIT) {
655         TAG_LOGE(AAFwkTag::APPMGR, "infoSize is too large");
656         return ERR_INVALID_VALUE;
657     }
658     for (int32_t i = 0; i < infoSize; i++) {
659         std::unique_ptr<AppStateData> info(reply.ReadParcelable<AppStateData>());
660         if (!info) {
661             TAG_LOGE(AAFwkTag::APPMGR, "Read Parcelable infos failed.");
662             return ERR_INVALID_VALUE;
663         }
664         list.emplace_back(*info);
665     }
666     return reply.ReadInt32();
667 }
668 
StartUserTestProcess(const AAFwk::Want & want,const sptr<IRemoteObject> & observer,const BundleInfo & bundleInfo,int32_t userId)669 int AppMgrProxy::StartUserTestProcess(
670     const AAFwk::Want &want, const sptr<IRemoteObject> &observer, const BundleInfo &bundleInfo, int32_t userId)
671 {
672     MessageParcel data;
673     MessageParcel reply;
674     MessageOption option;
675 
676     if (!WriteInterfaceToken(data)) {
677         return ERR_FLATTEN_OBJECT;
678     }
679     PARCEL_UTIL_WRITE_RET_INT(data, Parcelable, &want);
680     PARCEL_UTIL_WRITE_RET_INT(data, RemoteObject, observer);
681     PARCEL_UTIL_WRITE_RET_INT(data, Parcelable, &bundleInfo);
682     PARCEL_UTIL_WRITE_RET_INT(data, Int32, userId);
683 
684     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::START_USER_TEST_PROCESS, data, reply, option);
685     return reply.ReadInt32();
686 }
687 
FinishUserTest(const std::string & msg,const int64_t & resultCode,const std::string & bundleName)688 int AppMgrProxy::FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName)
689 {
690     MessageParcel data;
691     MessageParcel reply;
692     MessageOption option;
693 
694     if (!WriteInterfaceToken(data)) {
695         return ERR_FLATTEN_OBJECT;
696     }
697 
698     PARCEL_UTIL_WRITE_RET_INT(data, String, msg);
699     PARCEL_UTIL_WRITE_RET_INT(data, Int64, resultCode);
700     PARCEL_UTIL_WRITE_RET_INT(data, String, bundleName);
701 
702     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::FINISH_USER_TEST, data, reply, option);
703     return reply.ReadInt32();
704 }
705 
ScheduleAcceptWantDone(const int32_t recordId,const AAFwk::Want & want,const std::string & flag)706 void AppMgrProxy::ScheduleAcceptWantDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag)
707 {
708     MessageParcel data;
709     MessageParcel reply;
710     AAFwk::ExtendMaxIpcCapacityForInnerWant(data);
711     if (!WriteInterfaceToken(data)) {
712         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
713         return;
714     }
715 
716     if (!data.WriteInt32(recordId) || !data.WriteParcelable(&want) || !data.WriteString(flag)) {
717         TAG_LOGE(AAFwkTag::APPMGR, "want write failed.");
718         return;
719     }
720 
721     if (!SendTransactCmd(AppMgrInterfaceCode::SCHEDULE_ACCEPT_WANT_DONE, data, reply)) {
722         TAG_LOGE(AAFwkTag::APPMGR, "SendTransactCmd failed");
723         return;
724     }
725 }
726 
ScheduleNewProcessRequestDone(const int32_t recordId,const AAFwk::Want & want,const std::string & flag)727 void AppMgrProxy::ScheduleNewProcessRequestDone(const int32_t recordId, const AAFwk::Want &want,
728     const std::string &flag)
729 {
730     MessageParcel data;
731     MessageParcel reply;
732     AAFwk::ExtendMaxIpcCapacityForInnerWant(data);
733     if (!WriteInterfaceToken(data)) {
734         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
735         return;
736     }
737 
738     if (!data.WriteInt32(recordId) || !data.WriteParcelable(&want) || !data.WriteString(flag)) {
739         TAG_LOGE(AAFwkTag::APPMGR, "want write failed.");
740         return;
741     }
742 
743     if (!SendTransactCmd(AppMgrInterfaceCode::SCHEDULE_NEW_PROCESS_REQUEST_DONE, data, reply)) {
744         TAG_LOGE(AAFwkTag::APPMGR, "SendTransactCmd failed");
745         return;
746     }
747 }
748 
GetAbilityRecordsByProcessID(const int pid,std::vector<sptr<IRemoteObject>> & tokens)749 int AppMgrProxy::GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens)
750 {
751     MessageParcel data;
752     MessageParcel reply;
753     MessageOption option(MessageOption::TF_SYNC);
754 
755     if (!WriteInterfaceToken(data)) {
756         return ERR_FLATTEN_OBJECT;
757     }
758     if (!data.WriteInt32(pid)) {
759         TAG_LOGE(AAFwkTag::APPMGR, "write pid failed");
760         return ERR_FLATTEN_OBJECT;
761     }
762     if (!SendTransactCmd(AppMgrInterfaceCode::APP_GET_ABILITY_RECORDS_BY_PROCESS_ID, data, reply)) {
763         return ERR_NULL_OBJECT;
764     }
765     int32_t infoSize = reply.ReadInt32();
766     if (infoSize > CYCLE_LIMIT) {
767         TAG_LOGE(AAFwkTag::APPMGR, "infoSize is too large");
768         return ERR_INVALID_VALUE;
769     }
770     for (int32_t i = 0; i < infoSize; i++) {
771         auto iRemote = reply.ReadRemoteObject();
772         tokens.emplace_back(iRemote);
773     }
774     return reply.ReadInt32();
775 }
776 
PreStartNWebSpawnProcess()777 int AppMgrProxy::PreStartNWebSpawnProcess()
778 {
779     TAG_LOGI(AAFwkTag::APPMGR, "PreStartNWebSpawnProcess");
780     MessageParcel data;
781     MessageParcel reply;
782     MessageOption option;
783     if (!WriteInterfaceToken(data)) {
784         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
785         return ERR_FLATTEN_OBJECT;
786     }
787 
788     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::PRE_START_NWEBSPAWN_PROCESS, data, reply, option);
789     auto result = reply.ReadInt32();
790     if (result != 0) {
791         TAG_LOGW(AAFwkTag::APPMGR, "PreStartNWebSpawnProcess failed, result: %{public}d", result);
792     }
793     return result;
794 }
795 
StartRenderProcess(const std::string & renderParam,int32_t ipcFd,int32_t sharedFd,int32_t crashFd,pid_t & renderPid,bool isGPU)796 int AppMgrProxy::StartRenderProcess(const std::string &renderParam,
797                                     int32_t ipcFd, int32_t sharedFd,
798                                     int32_t crashFd, pid_t &renderPid, bool isGPU)
799 {
800     if (renderParam.empty() || ipcFd <= 0 || sharedFd <= 0 || crashFd <= 0) {
801         TAG_LOGE(AAFwkTag::APPMGR, "Invalid params, renderParam:%{private}s, ipcFd:%{public}d, "
802             "sharedFd:%{public}d, crashFd:%{public}d", renderParam.c_str(), ipcFd, sharedFd, crashFd);
803         return -1;
804     }
805 
806     MessageParcel data;
807     MessageParcel reply;
808     MessageOption option;
809     if (!WriteInterfaceToken(data)) {
810         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
811         return ERR_FLATTEN_OBJECT;
812     }
813 
814     if (!data.WriteString(renderParam)) {
815         TAG_LOGE(AAFwkTag::APPMGR, "want paramSize failed.");
816         return -1;
817     }
818 
819     if (!data.WriteFileDescriptor(ipcFd) || !data.WriteFileDescriptor(sharedFd) ||
820         !data.WriteFileDescriptor(crashFd)) {
821         TAG_LOGE(AAFwkTag::APPMGR, "want fd failed, ipcFd:%{public}d, sharedFd:%{public}d, "
822             "crashFd:%{public}d", ipcFd, sharedFd, crashFd);
823         return -1;
824     }
825 
826     if (!data.WriteBool(isGPU)) {
827         TAG_LOGE(AAFwkTag::APPMGR, "want processType failed.");
828         return -1;
829     }
830 
831     int32_t ret = SendRequest(AppMgrInterfaceCode::START_RENDER_PROCESS, data,
832         reply, option);
833     if (ret != NO_ERROR) {
834         TAG_LOGW(AAFwkTag::APPMGR, "StartRenderProcess SendRequest is failed, error code: %{public}d", ret);
835         return ret;
836     }
837 
838     auto result = reply.ReadInt32();
839     renderPid = reply.ReadInt32();
840     if (result != 0) {
841         TAG_LOGW(AAFwkTag::APPMGR, "StartRenderProcess failed, result: %{public}d", result);
842     }
843     return result;
844 }
845 
AttachRenderProcess(const sptr<IRemoteObject> & renderScheduler)846 void AppMgrProxy::AttachRenderProcess(const sptr<IRemoteObject> &renderScheduler)
847 {
848     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
849     if (!renderScheduler) {
850         TAG_LOGE(AAFwkTag::APPMGR, "renderScheduler is null");
851         return;
852     }
853 
854     TAG_LOGD(AAFwkTag::APPMGR, "AttachRenderProcess start");
855     MessageParcel data;
856     MessageParcel reply;
857     MessageOption option;
858     if (!WriteInterfaceToken(data)) {
859         return;
860     }
861     if (!data.WriteRemoteObject(renderScheduler)) {
862         TAG_LOGE(AAFwkTag::APPMGR, "renderScheduler write failed.");
863         return;
864     }
865 
866     if (!SendTransactCmd(AppMgrInterfaceCode::ATTACH_RENDER_PROCESS, data, reply)) {
867         TAG_LOGE(AAFwkTag::APPMGR, "SendTransactCmd ATTACH_RENDER_PROCESS failed");
868         return;
869     }
870 }
871 
SaveBrowserChannel(sptr<IRemoteObject> browser)872 void AppMgrProxy::SaveBrowserChannel(sptr<IRemoteObject> browser)
873 {
874     if (!browser) {
875         TAG_LOGE(AAFwkTag::APPMGR, "browser is null");
876         return;
877     }
878     MessageParcel data;
879     MessageParcel reply;
880     MessageOption option;
881     if (!WriteInterfaceToken(data)) {
882         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
883         return;
884     }
885 
886     if (!data.WriteRemoteObject(browser)) {
887         TAG_LOGE(AAFwkTag::APPMGR, "browser write failed.");
888         return;
889     }
890 
891     if (!SendTransactCmd(AppMgrInterfaceCode::SAVE_BROWSER_CHANNEL, data, reply)) {
892         TAG_LOGE(AAFwkTag::APPMGR, "SendTransactCmd SAVE_BROWSER_CHANNEL failed");
893         return;
894     }
895 }
896 
GetRenderProcessTerminationStatus(pid_t renderPid,int & status)897 int AppMgrProxy::GetRenderProcessTerminationStatus(pid_t renderPid, int &status)
898 {
899     MessageParcel data;
900     MessageParcel reply;
901     MessageOption option;
902     if (!WriteInterfaceToken(data)) {
903         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
904         return ERR_FLATTEN_OBJECT;
905     }
906 
907     if (!data.WriteInt32(renderPid)) {
908         TAG_LOGE(AAFwkTag::APPMGR, "write renderPid failed.");
909         return -1;
910     }
911 
912     int32_t ret = SendRequest(AppMgrInterfaceCode::GET_RENDER_PROCESS_TERMINATION_STATUS, data, reply, option);
913     if (ret != NO_ERROR) {
914         TAG_LOGW(AAFwkTag::APPMGR, "GetRenderProcessTerminationStatus SendRequest is failed, error code: %{public}d",
915             ret);
916         return ret;
917     }
918 
919     auto result = reply.ReadInt32();
920     if (result != 0) {
921         TAG_LOGW(AAFwkTag::APPMGR, "GetRenderProcessTerminationStatus failed, result: %{public}d", result);
922         return result;
923     }
924     status = reply.ReadInt32();
925     return 0;
926 }
927 
UpdateConfiguration(const Configuration & config,const int32_t userId)928 int32_t AppMgrProxy::UpdateConfiguration(const Configuration &config, const int32_t userId)
929 {
930     TAG_LOGI(AAFwkTag::APPMGR, "AppMgrProxy UpdateConfiguration");
931     MessageParcel data;
932     MessageParcel reply;
933     MessageOption option(MessageOption::TF_SYNC);
934     if (!WriteInterfaceToken(data)) {
935         return ERR_INVALID_DATA;
936     }
937     if (!data.WriteParcelable(&config)) {
938         TAG_LOGE(AAFwkTag::APPMGR, "parcel config failed");
939         return ERR_INVALID_DATA;
940     }
941     if (!data.WriteInt32(userId)) {
942         TAG_LOGE(AAFwkTag::APPMGR, "parcel userId failed");
943         return ERR_INVALID_DATA;
944     }
945     int32_t ret = SendRequest(AppMgrInterfaceCode::UPDATE_CONFIGURATION, data, reply, option);
946     if (ret != NO_ERROR) {
947         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
948         return ret;
949     }
950     return reply.ReadInt32();
951 }
952 
UpdateConfigurationByBundleName(const Configuration & config,const std::string & name,int32_t appIndex)953 int32_t AppMgrProxy::UpdateConfigurationByBundleName(const Configuration &config, const std::string &name,
954     int32_t appIndex)
955 {
956     TAG_LOGI(AAFwkTag::APPMGR, "AppMgrProxy UpdateConfigurationByBundleName");
957     MessageParcel data;
958     if (!WriteInterfaceToken(data)) {
959         return ERR_INVALID_DATA;
960     }
961     if (!data.WriteParcelable(&config)) {
962         TAG_LOGE(AAFwkTag::APPMGR, "parcel config failed");
963         return ERR_INVALID_DATA;
964     }
965     if (!data.WriteString(name)) {
966         TAG_LOGE(AAFwkTag::APPMGR, "parcel name failed");
967         return ERR_INVALID_DATA;
968     }
969     if (!data.WriteInt32(appIndex)) {
970         TAG_LOGE(AAFwkTag::APPMGR, "parcel appIndex failed");
971         return ERR_INVALID_DATA;
972     }
973     MessageParcel reply;
974     MessageOption option(MessageOption::TF_SYNC);
975     int32_t ret = SendRequest(AppMgrInterfaceCode::UPDATE_CONFIGURATION_BY_BUNDLE_NAME, data, reply, option);
976     if (ret != NO_ERROR) {
977         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
978         return ret;
979     }
980     return reply.ReadInt32();
981 }
982 
GetConfiguration(Configuration & config)983 int32_t AppMgrProxy::GetConfiguration(Configuration& config)
984 {
985     MessageParcel data;
986     MessageParcel reply;
987     MessageOption option(MessageOption::TF_SYNC);
988     if (!WriteInterfaceToken(data)) {
989         TAG_LOGE(AAFwkTag::APPMGR, "parcel data failed");
990         return ERR_INVALID_DATA;
991     }
992     int32_t ret = SendRequest(AppMgrInterfaceCode::GET_CONFIGURATION, data, reply, option);
993     if (ret != NO_ERROR) {
994         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
995         return ret;
996     }
997 
998     std::unique_ptr<Configuration> info(reply.ReadParcelable<Configuration>());
999     if (!info) {
1000         TAG_LOGE(AAFwkTag::APPMGR, "read configuration failed.");
1001         return ERR_UNKNOWN_OBJECT;
1002     }
1003     config = *info;
1004     return reply.ReadInt32();
1005 }
1006 
RegisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)1007 int32_t AppMgrProxy::RegisterConfigurationObserver(const sptr<IConfigurationObserver>& observer)
1008 {
1009     if (!observer) {
1010         TAG_LOGE(AAFwkTag::APPMGR, "observer null");
1011         return ERR_INVALID_VALUE;
1012     }
1013     TAG_LOGD(AAFwkTag::APPMGR, "RegisterConfigurationObserver start");
1014     MessageParcel data;
1015     MessageParcel reply;
1016     MessageOption option;
1017     if (!WriteInterfaceToken(data)) {
1018         return ERR_FLATTEN_OBJECT;
1019     }
1020 
1021     if (!data.WriteRemoteObject(observer->AsObject())) {
1022         TAG_LOGE(AAFwkTag::APPMGR, "observer write failed.");
1023         return ERR_FLATTEN_OBJECT;
1024     }
1025 
1026     auto error = SendRequest(AppMgrInterfaceCode::REGISTER_CONFIGURATION_OBSERVER, data, reply, option);
1027     if (error != NO_ERROR) {
1028         TAG_LOGE(AAFwkTag::APPMGR, "Send request error: %{public}d", error);
1029         return error;
1030     }
1031     return reply.ReadInt32();
1032 }
1033 
UnregisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)1034 int32_t AppMgrProxy::UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
1035 {
1036     TAG_LOGD(AAFwkTag::APPMGR, "UnregisterConfigurationObserver start");
1037     MessageParcel data;
1038     MessageParcel reply;
1039     MessageOption option;
1040     if (!WriteInterfaceToken(data)) {
1041         return ERR_FLATTEN_OBJECT;
1042     }
1043 
1044     if (!data.WriteRemoteObject(observer->AsObject())) {
1045         TAG_LOGE(AAFwkTag::APPMGR, "observer write failed.");
1046         return ERR_FLATTEN_OBJECT;
1047     }
1048 
1049     auto error = SendRequest(AppMgrInterfaceCode::UNREGISTER_CONFIGURATION_OBSERVER,
1050         data, reply, option);
1051     if (error != NO_ERROR) {
1052         TAG_LOGE(AAFwkTag::APPMGR, "Send request error: %{public}d", error);
1053         return error;
1054     }
1055     return reply.ReadInt32();
1056 }
1057 
GetAppRunningStateByBundleName(const std::string & bundleName)1058 bool AppMgrProxy::GetAppRunningStateByBundleName(const std::string &bundleName)
1059 {
1060     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1061     TAG_LOGD(AAFwkTag::APPMGR, "called");
1062     MessageParcel data;
1063     if (!WriteInterfaceToken(data)) {
1064         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1065         return false;
1066     }
1067 
1068     if (!data.WriteString(bundleName)) {
1069         TAG_LOGE(AAFwkTag::APPMGR, "Write bundle name failed.");
1070         return false;
1071     }
1072 
1073     MessageParcel reply;
1074     MessageOption option;
1075     auto ret = SendRequest(AppMgrInterfaceCode::GET_APP_RUNNING_STATE,
1076         data, reply, option);
1077     if (ret != 0) {
1078         TAG_LOGW(AAFwkTag::APPMGR, "Send request failed with error code %{public}d.", ret);
1079         return false;
1080     }
1081 
1082     return reply.ReadBool();
1083 }
1084 
NotifyLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)1085 int32_t AppMgrProxy::NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
1086 {
1087     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1088     TAG_LOGD(AAFwkTag::APPMGR, "called");
1089     MessageParcel data;
1090     if (!WriteInterfaceToken(data)) {
1091         TAG_LOGE(AAFwkTag::APPMGR, "NotifyLoadRepairPatch, Write interface token failed.");
1092         return ERR_INVALID_DATA;
1093     }
1094 
1095     if (!data.WriteString(bundleName)) {
1096         TAG_LOGE(AAFwkTag::APPMGR, "NotifyLoadRepairPatch, Write bundle name failed.");
1097         return ERR_INVALID_DATA;
1098     }
1099 
1100     if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
1101         TAG_LOGE(AAFwkTag::APPMGR, "Write callback failed.");
1102         return ERR_INVALID_DATA;
1103     }
1104 
1105     MessageParcel reply;
1106     MessageOption option;
1107     auto ret = SendRequest(AppMgrInterfaceCode::NOTIFY_LOAD_REPAIR_PATCH,
1108         data, reply, option);
1109     if (ret != 0) {
1110         TAG_LOGW(AAFwkTag::APPMGR, "NotifyLoadRepairPatch, Send request failed with error code %{public}d.", ret);
1111         return ret;
1112     }
1113 
1114     return reply.ReadInt32();
1115 }
1116 
NotifyHotReloadPage(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)1117 int32_t AppMgrProxy::NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
1118 {
1119     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1120     TAG_LOGD(AAFwkTag::APPMGR, "called");
1121     MessageParcel data;
1122     if (!WriteInterfaceToken(data)) {
1123         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1124         return ERR_INVALID_DATA;
1125     }
1126 
1127     if (!data.WriteString(bundleName)) {
1128         TAG_LOGE(AAFwkTag::APPMGR, "Write bundle name failed.");
1129         return ERR_INVALID_DATA;
1130     }
1131 
1132     if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
1133         TAG_LOGE(AAFwkTag::APPMGR, "Write callback failed.");
1134         return ERR_INVALID_DATA;
1135     }
1136 
1137     MessageParcel reply;
1138     MessageOption option;
1139     auto ret = SendRequest(AppMgrInterfaceCode::NOTIFY_HOT_RELOAD_PAGE,
1140         data, reply, option);
1141     if (ret != 0) {
1142         TAG_LOGW(AAFwkTag::APPMGR, "Send request failed with error code %{public}d.", ret);
1143         return ret;
1144     }
1145 
1146     return reply.ReadInt32();
1147 }
1148 
1149 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
SetContinuousTaskProcess(int32_t pid,bool isContinuousTask)1150 int32_t AppMgrProxy::SetContinuousTaskProcess(int32_t pid, bool isContinuousTask)
1151 {
1152     TAG_LOGD(AAFwkTag::APPMGR, "SetContinuousTaskProcess start.");
1153     MessageParcel data;
1154     MessageParcel reply;
1155     MessageOption option;
1156 
1157     if (!WriteInterfaceToken(data)) {
1158         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1159         return ERR_INVALID_DATA;
1160     }
1161 
1162     if (!data.WriteInt32(pid)) {
1163         TAG_LOGE(AAFwkTag::APPMGR, "uid write failed.");
1164         return ERR_INVALID_DATA;
1165     }
1166 
1167     if (!data.WriteBool(isContinuousTask)) {
1168         TAG_LOGE(AAFwkTag::APPMGR, "isContinuousTask write failed.");
1169         return ERR_INVALID_DATA;
1170     }
1171 
1172     auto ret = SendRequest(AppMgrInterfaceCode::SET_CONTINUOUSTASK_PROCESS,
1173         data, reply, option);
1174     if (ret != NO_ERROR) {
1175         TAG_LOGW(AAFwkTag::APPMGR, "Send request failed with error code %{public}d.", ret);
1176         return ret;
1177     }
1178 
1179     return reply.ReadInt32();
1180 }
1181 #endif
1182 
NotifyUnLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)1183 int32_t AppMgrProxy::NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
1184 {
1185     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1186     TAG_LOGD(AAFwkTag::APPMGR, "called");
1187     MessageParcel data;
1188     if (!WriteInterfaceToken(data)) {
1189         TAG_LOGE(AAFwkTag::APPMGR, "Notify unload patch, Write interface token failed.");
1190         return ERR_INVALID_DATA;
1191     }
1192 
1193     if (!data.WriteString(bundleName)) {
1194         TAG_LOGE(AAFwkTag::APPMGR, "Notify unload patch, Write bundle name failed.");
1195         return ERR_INVALID_DATA;
1196     }
1197 
1198     if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
1199         TAG_LOGE(AAFwkTag::APPMGR, "Write callback failed.");
1200         return ERR_INVALID_DATA;
1201     }
1202 
1203     MessageParcel reply;
1204     MessageOption option;
1205 
1206     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::NOTIFY_UNLOAD_REPAIR_PATCH, data, reply, option);
1207     return reply.ReadInt32();
1208 }
1209 
IsSharedBundleRunning(const std::string & bundleName,uint32_t versionCode)1210 bool AppMgrProxy::IsSharedBundleRunning(const std::string &bundleName, uint32_t versionCode)
1211 {
1212     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1213     TAG_LOGD(AAFwkTag::APPMGR, "called");
1214     MessageParcel data;
1215     if (!WriteInterfaceToken(data)) {
1216         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1217         return false;
1218     }
1219     if (!data.WriteString(bundleName) || !data.WriteUint32(versionCode)) {
1220         TAG_LOGE(AAFwkTag::APPMGR, "Write bundle name or version code failed.");
1221         return false;
1222     }
1223 
1224     MessageParcel reply;
1225     MessageOption option;
1226     auto ret = SendRequest(AppMgrInterfaceCode::IS_SHARED_BUNDLE_RUNNING,
1227         data, reply, option);
1228     if (ret != NO_ERROR) {
1229         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
1230         return false;
1231     }
1232 
1233     return reply.ReadBool();
1234 }
1235 
StartNativeProcessForDebugger(const AAFwk::Want & want)1236 int32_t AppMgrProxy::StartNativeProcessForDebugger(const AAFwk::Want &want)
1237 {
1238     MessageParcel data;
1239     if (!WriteInterfaceToken(data)) {
1240         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1241         return ERR_FLATTEN_OBJECT;
1242     }
1243     if (!data.WriteParcelable(&want)) {
1244         TAG_LOGE(AAFwkTag::APPMGR, "want write failed.");
1245         return ERR_FLATTEN_OBJECT;
1246     }
1247 
1248     MessageParcel reply;
1249     MessageOption option;
1250 
1251     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::START_NATIVE_PROCESS_FOR_DEBUGGER, data, reply, option);
1252     return reply.ReadInt32();
1253 }
1254 
GetBundleNameByPid(const int pid,std::string & bundleName,int32_t & uid)1255 int32_t AppMgrProxy::GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid)
1256 {
1257     MessageParcel data;
1258     MessageParcel reply;
1259     MessageOption option;
1260 
1261     if (!WriteInterfaceToken(data)) {
1262         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1263         return ERR_INVALID_DATA;
1264     }
1265 
1266     if (!data.WriteInt32(pid)) {
1267         TAG_LOGE(AAFwkTag::APPMGR, "pid write failed.");
1268         return ERR_INVALID_DATA;
1269     }
1270 
1271     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_BUNDLE_NAME_BY_PID, data, reply, option);
1272     bundleName = reply.ReadString();
1273     uid = reply.ReadInt32();
1274     return ERR_NONE;
1275 }
1276 
GetRunningProcessInfoByPid(const pid_t pid,OHOS::AppExecFwk::RunningProcessInfo & info)1277 int32_t AppMgrProxy::GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info)
1278 {
1279     TAG_LOGD(AAFwkTag::APPMGR, "start");
1280     MessageParcel data;
1281     MessageParcel reply;
1282     MessageOption option;
1283     if (!WriteInterfaceToken(data)) {
1284         return ERR_INVALID_DATA;
1285     }
1286 
1287     if (!data.WriteInt32(static_cast<int32_t>(pid))) {
1288         TAG_LOGE(AAFwkTag::APPMGR, "parcel WriteInt32 failed.");
1289         return ERR_INVALID_DATA;
1290     }
1291 
1292     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_RUNNING_PROCESS_INFO_BY_PID, data, reply, option);
1293 
1294     std::unique_ptr<AppExecFwk::RunningProcessInfo> processInfo(reply.ReadParcelable<AppExecFwk::RunningProcessInfo>());
1295     if (processInfo == nullptr) {
1296         TAG_LOGE(AAFwkTag::APPMGR, "recv process info failded");
1297         return ERR_INVALID_DATA;
1298     }
1299     info = *processInfo;
1300     return reply.ReadInt32();
1301 }
1302 
GetRunningProcessInfoByChildProcessPid(const pid_t childPid,OHOS::AppExecFwk::RunningProcessInfo & info)1303 int32_t AppMgrProxy::GetRunningProcessInfoByChildProcessPid(const pid_t childPid,
1304     OHOS::AppExecFwk::RunningProcessInfo &info)
1305 {
1306     TAG_LOGD(AAFwkTag::APPMGR, "start");
1307     MessageParcel data;
1308     MessageParcel reply;
1309     MessageOption option;
1310     if (!WriteInterfaceToken(data)) {
1311         return ERR_INVALID_DATA;
1312     }
1313 
1314     if (!data.WriteInt32(static_cast<int32_t>(childPid))) {
1315         TAG_LOGE(AAFwkTag::APPMGR, "parcel childPid failed.");
1316         return ERR_INVALID_DATA;
1317     }
1318 
1319     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_RUNNING_PROCESS_INFO_BY_CHILD_PROCESS_PID,
1320         data, reply, option);
1321 
1322     std::unique_ptr<AppExecFwk::RunningProcessInfo> processInfo(reply.ReadParcelable<AppExecFwk::RunningProcessInfo>());
1323     if (processInfo == nullptr) {
1324         TAG_LOGE(AAFwkTag::APPMGR, "recv process info failded");
1325         return ERR_INVALID_DATA;
1326     }
1327     info = *processInfo;
1328     return reply.ReadInt32();
1329 }
1330 
NotifyAppFault(const FaultData & faultData)1331 int32_t AppMgrProxy::NotifyAppFault(const FaultData &faultData)
1332 {
1333     TAG_LOGI(AAFwkTag::APPMGR, "called");
1334     MessageParcel data;
1335 
1336     if (!WriteInterfaceToken(data)) {
1337         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1338         return ERR_FLATTEN_OBJECT;
1339     }
1340 
1341     if (!data.WriteParcelable(&faultData)) {
1342         TAG_LOGE(AAFwkTag::APPMGR, "Write FaultData error.");
1343         return ERR_FLATTEN_OBJECT;
1344     }
1345 
1346     MessageParcel reply;
1347     MessageOption option;
1348 
1349     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::NOTIFY_APP_FAULT, data, reply, option);
1350     return reply.ReadInt32();
1351 }
1352 
NotifyAppFaultBySA(const AppFaultDataBySA & faultData)1353 int32_t AppMgrProxy::NotifyAppFaultBySA(const AppFaultDataBySA &faultData)
1354 {
1355     TAG_LOGI(AAFwkTag::APPMGR, "called");
1356     MessageParcel data;
1357 
1358     if (!WriteInterfaceToken(data)) {
1359         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1360         return ERR_FLATTEN_OBJECT;
1361     }
1362 
1363     if (!data.WriteParcelable(&faultData)) {
1364         TAG_LOGE(AAFwkTag::APPMGR, "Write FaultDataBySA error.");
1365         return ERR_FLATTEN_OBJECT;
1366     }
1367 
1368     MessageParcel reply;
1369     MessageOption option;
1370 
1371     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::NOTIFY_APP_FAULT_BY_SA, data, reply, option);
1372     return reply.ReadInt32();
1373 }
1374 
SetAppFreezeFilter(int32_t pid)1375 bool AppMgrProxy::SetAppFreezeFilter(int32_t pid)
1376 {
1377     TAG_LOGD(AAFwkTag::APPMGR, "called");
1378     MessageParcel data;
1379     MessageParcel reply;
1380     MessageOption option;
1381     if (!WriteInterfaceToken(data)) {
1382         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1383         return false;
1384     }
1385     if (!data.WriteInt32(pid)) {
1386         TAG_LOGE(AAFwkTag::APPMGR, "write pid failed.");
1387         return false;
1388     }
1389     auto ret = SendRequest(AppMgrInterfaceCode::SET_APPFREEZE_FILTER,
1390         data, reply, option);
1391     if (ret != NO_ERROR) {
1392         TAG_LOGE(AAFwkTag::APPMGR, "Send request failed with error code %{public}d.", ret);
1393         return false;
1394     }
1395     return reply.ReadBool();
1396 }
1397 
GetProcessMemoryByPid(const int32_t pid,int32_t & memorySize)1398 int32_t AppMgrProxy::GetProcessMemoryByPid(const int32_t pid, int32_t &memorySize)
1399 {
1400     TAG_LOGD(AAFwkTag::APPMGR, "GetProcessMemoryByPid start");
1401     MessageParcel data;
1402     MessageParcel reply;
1403     MessageOption option;
1404     if (!WriteInterfaceToken(data)) {
1405         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1406         return ERR_FLATTEN_OBJECT;
1407     }
1408 
1409     if (!data.WriteInt32(pid)) {
1410         TAG_LOGE(AAFwkTag::APPMGR, "write pid failed.");
1411         return ERR_INVALID_DATA;
1412     }
1413 
1414     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_PROCESS_MEMORY_BY_PID, data, reply, option);
1415     memorySize = reply.ReadInt32();
1416     auto result = reply.ReadInt32();
1417     return result;
1418 }
1419 
GetRunningProcessInformation(const std::string & bundleName,int32_t userId,std::vector<RunningProcessInfo> & info)1420 int32_t AppMgrProxy::GetRunningProcessInformation(
1421     const std::string &bundleName, int32_t userId, std::vector<RunningProcessInfo> &info)
1422 {
1423     TAG_LOGD(AAFwkTag::APPMGR, "GetRunningProcessInformation start");
1424     MessageParcel data;
1425     MessageParcel reply;
1426     if (!WriteInterfaceToken(data)) {
1427         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1428         return ERR_FLATTEN_OBJECT;
1429     }
1430 
1431     if (!data.WriteString(bundleName)) {
1432         TAG_LOGE(AAFwkTag::APPMGR, "write bundleName failed.");
1433         return ERR_INVALID_DATA;
1434     }
1435 
1436     if (!data.WriteInt32(userId)) {
1437         TAG_LOGE(AAFwkTag::APPMGR, "write userId failed.");
1438         return ERR_INVALID_DATA;
1439     }
1440 
1441     MessageOption option(MessageOption::TF_SYNC);
1442     auto ret = SendRequest(AppMgrInterfaceCode::GET_PIDS_BY_BUNDLENAME,
1443         data, reply, option);
1444     if (ret != NO_ERROR) {
1445         TAG_LOGE(AAFwkTag::APPMGR, "Send request failed with error code %{public}d.", ret);
1446         return ret;
1447     }
1448 
1449     auto error = GetParcelableInfos<RunningProcessInfo>(reply, info);
1450     if (error != NO_ERROR) {
1451         TAG_LOGE(AAFwkTag::APPMGR, "GetParcelableInfos fail, error: %{public}d", error);
1452         return error;
1453     }
1454     return reply.ReadInt32();
1455 }
1456 
ChangeAppGcState(pid_t pid,int32_t state)1457 int32_t AppMgrProxy::ChangeAppGcState(pid_t pid, int32_t state)
1458 {
1459     TAG_LOGD(AAFwkTag::APPMGR, "called");
1460     MessageParcel data;
1461     MessageParcel reply;
1462     if (!WriteInterfaceToken(data)) {
1463         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1464         return ERR_FLATTEN_OBJECT;
1465     }
1466     MessageOption option(MessageOption::TF_ASYNC);
1467     if (!data.WriteInt32(pid)) {
1468         TAG_LOGE(AAFwkTag::APPMGR, "Pid write failed.");
1469         return ERR_FLATTEN_OBJECT;
1470     }
1471     if (!data.WriteInt32(state)) {
1472         TAG_LOGE(AAFwkTag::APPMGR, "State write failed.");
1473         return ERR_FLATTEN_OBJECT;
1474     }
1475 
1476     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::CHANGE_APP_GC_STATE, data, reply, option);
1477     return NO_ERROR;
1478 }
1479 
NotifyPageShow(const sptr<IRemoteObject> & token,const PageStateData & pageStateData)1480 int32_t AppMgrProxy::NotifyPageShow(const sptr<IRemoteObject> &token, const PageStateData &pageStateData)
1481 {
1482     TAG_LOGD(AAFwkTag::APPMGR, "call");
1483     MessageParcel data;
1484     MessageParcel reply;
1485     MessageOption option(MessageOption::TF_ASYNC);
1486 
1487     if (!WriteInterfaceToken(data)) {
1488         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1489         return ERR_FLATTEN_OBJECT;
1490     }
1491     if (!data.WriteRemoteObject(token)) {
1492         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
1493         return ERR_INVALID_DATA;
1494     }
1495     if (!data.WriteParcelable(&pageStateData)) {
1496         TAG_LOGE(AAFwkTag::APPMGR, "Write PageStateData error.");
1497         return ERR_FLATTEN_OBJECT;
1498     }
1499 
1500     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::NOTIFY_PAGE_SHOW, data, reply, option);
1501     return NO_ERROR;
1502 }
1503 
NotifyPageHide(const sptr<IRemoteObject> & token,const PageStateData & pageStateData)1504 int32_t AppMgrProxy::NotifyPageHide(const sptr<IRemoteObject> &token, const PageStateData &pageStateData)
1505 {
1506     TAG_LOGD(AAFwkTag::APPMGR, "called");
1507     MessageParcel data;
1508     MessageParcel reply;
1509     MessageOption option(MessageOption::TF_ASYNC);
1510 
1511     if (!WriteInterfaceToken(data)) {
1512         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1513         return ERR_FLATTEN_OBJECT;
1514     }
1515     if (!data.WriteRemoteObject(token)) {
1516         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
1517         return ERR_INVALID_DATA;
1518     }
1519     if (!data.WriteParcelable(&pageStateData)) {
1520         TAG_LOGE(AAFwkTag::APPMGR, "Write PageStateData error.");
1521         return ERR_FLATTEN_OBJECT;
1522     }
1523 
1524     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::NOTIFY_PAGE_HIDE, data, reply, option);
1525     return NO_ERROR;
1526 }
1527 
SendRequest(AppMgrInterfaceCode code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1528 int32_t AppMgrProxy::SendRequest(AppMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply,
1529     MessageOption& option)
1530 {
1531     sptr<IRemoteObject> remote = Remote();
1532     if (remote == nullptr) {
1533         TAG_LOGE(AAFwkTag::APPMGR, "Remote() is NULL");
1534         return ERR_NULL_OBJECT;
1535     }
1536 
1537     return remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
1538 }
1539 
RegisterAppRunningStatusListener(const sptr<IRemoteObject> & listener)1540 int32_t AppMgrProxy::RegisterAppRunningStatusListener(const sptr<IRemoteObject> &listener)
1541 {
1542     MessageParcel data;
1543     if (!WriteInterfaceToken(data)) {
1544         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1545         return ERR_FLATTEN_OBJECT;
1546     }
1547     if (listener == nullptr || !data.WriteRemoteObject(listener)) {
1548         TAG_LOGE(AAFwkTag::APPMGR, "Write listener failed.");
1549         return ERR_FLATTEN_OBJECT;
1550     }
1551 
1552     MessageParcel reply;
1553     MessageOption option;
1554 
1555     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::REGISTER_APP_RUNNING_STATUS_LISTENER, data, reply, option);
1556     return reply.ReadInt32();
1557 }
1558 
UnregisterAppRunningStatusListener(const sptr<IRemoteObject> & listener)1559 int32_t AppMgrProxy::UnregisterAppRunningStatusListener(const sptr<IRemoteObject> &listener)
1560 {
1561     MessageParcel data;
1562     if (!WriteInterfaceToken(data)) {
1563         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1564         return ERR_FLATTEN_OBJECT;
1565     }
1566     if (listener == nullptr || !data.WriteRemoteObject(listener)) {
1567         TAG_LOGE(AAFwkTag::APPMGR, "Write listener failed.");
1568         return ERR_FLATTEN_OBJECT;
1569     }
1570 
1571     MessageParcel reply;
1572     MessageOption option;
1573 
1574     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::UNREGISTER_APP_RUNNING_STATUS_LISTENER, data, reply, option);
1575     return reply.ReadInt32();
1576 }
1577 
RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> & observer)1578 int32_t AppMgrProxy::RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer)
1579 {
1580     MessageParcel data;
1581     if (!WriteInterfaceToken(data)) {
1582         return ERR_FLATTEN_OBJECT;
1583     }
1584     if (observer == nullptr || !data.WriteRemoteObject(observer->AsObject())) {
1585         TAG_LOGE(AAFwkTag::APPMGR, "Observer is null or Write Remote failed.");
1586         return ERR_FLATTEN_OBJECT;
1587     }
1588     MessageParcel reply;
1589     MessageOption option;
1590 
1591     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::REGISTER_APP_FOREGROUND_STATE_OBSERVER, data, reply, option);
1592     return reply.ReadInt32();
1593 }
1594 
UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> & observer)1595 int32_t AppMgrProxy::UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer)
1596 {
1597     MessageParcel data;
1598     if (!WriteInterfaceToken(data)) {
1599         return ERR_FLATTEN_OBJECT;
1600     }
1601     if (observer == nullptr || !data.WriteRemoteObject(observer->AsObject())) {
1602         TAG_LOGE(AAFwkTag::APPMGR, "Observer is null or Write Remote failed.");
1603         return ERR_FLATTEN_OBJECT;
1604     }
1605 
1606     MessageParcel reply;
1607     MessageOption option;
1608 
1609     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::UNREGISTER_APP_FOREGROUND_STATE_OBSERVER, data, reply, option);
1610     return reply.ReadInt32();
1611 }
1612 
IsApplicationRunning(const std::string & bundleName,bool & isRunning)1613 int32_t AppMgrProxy::IsApplicationRunning(const std::string &bundleName, bool &isRunning)
1614 {
1615     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1616     TAG_LOGD(AAFwkTag::APPMGR, "called");
1617     isRunning = false;
1618     MessageParcel data;
1619     MessageParcel reply;
1620     MessageOption option;
1621     if (!WriteInterfaceToken(data)) {
1622         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1623         return ERR_INVALID_DATA;
1624     }
1625     PARCEL_UTIL_WRITE_RET_INT(data, String, bundleName);
1626 
1627     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::IS_APPLICATION_RUNNING, data, reply, option);
1628     isRunning = reply.ReadBool();
1629     return reply.ReadInt32();
1630 }
1631 
IsAppRunning(const std::string & bundleName,int32_t appCloneIndex,bool & isRunning)1632 int32_t AppMgrProxy::IsAppRunning(const std::string &bundleName, int32_t appCloneIndex, bool &isRunning)
1633 {
1634     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1635     TAG_LOGD(AAFwkTag::APPMGR, "called");
1636     MessageParcel data;
1637     if (!WriteInterfaceToken(data)) {
1638         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1639         return ERR_INVALID_DATA;
1640     }
1641     PARCEL_UTIL_WRITE_RET_INT(data, String, bundleName);
1642     PARCEL_UTIL_WRITE_RET_INT(data, Int32, appCloneIndex);
1643 
1644     MessageParcel reply;
1645     MessageOption option;
1646 
1647     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::IS_APP_RUNNING, data, reply, option);
1648     isRunning = reply.ReadBool();
1649     return reply.ReadInt32();
1650 }
1651 
IsAppRunningByBundleNameAndUserId(const std::string & bundleName,int32_t userId,bool & isRunning)1652 int32_t AppMgrProxy::IsAppRunningByBundleNameAndUserId(const std::string &bundleName, int32_t userId, bool &isRunning)
1653 {
1654     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1655     TAG_LOGD(AAFwkTag::APPMGR, "called");
1656     MessageParcel data;
1657     if (!WriteInterfaceToken(data)) {
1658         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1659         return ERR_INVALID_DATA;
1660     }
1661     PARCEL_UTIL_WRITE_RET_INT(data, String, bundleName);
1662     PARCEL_UTIL_WRITE_RET_INT(data, Int32, userId);
1663 
1664     MessageParcel reply;
1665     MessageOption option;
1666 
1667     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::IS_APP_RUNNING_BY_BUNDLE_NAME_AND_USER_ID, data, reply, option);
1668     isRunning = reply.ReadBool();
1669     return reply.ReadInt32();
1670 }
1671 
1672 #ifdef SUPPORT_CHILD_PROCESS
StartChildProcess(pid_t & childPid,const ChildProcessRequest & request)1673 int32_t AppMgrProxy::StartChildProcess(pid_t &childPid, const ChildProcessRequest &request)
1674 {
1675     TAG_LOGD(AAFwkTag::APPMGR, "called");
1676     if (request.srcEntry.empty()) {
1677         TAG_LOGE(AAFwkTag::APPMGR, "Invalid params, srcEntry:%{private}s", request.srcEntry.c_str());
1678         return ERR_INVALID_VALUE;
1679     }
1680     MessageParcel data;
1681     if (!WriteInterfaceToken(data)) {
1682         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
1683         return IPC_PROXY_ERR;
1684     }
1685     if (!data.WriteParcelable(&request)) {
1686         TAG_LOGE(AAFwkTag::APPMGR, "Write param request failed.");
1687         return IPC_PROXY_ERR;
1688     }
1689 
1690     MessageParcel reply;
1691     MessageOption option;
1692 
1693     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::START_CHILD_PROCESS, data, reply, option);
1694     auto result = reply.ReadInt32();
1695     if (result == ERR_OK) {
1696         childPid = reply.ReadInt32();
1697     }
1698     return result;
1699 }
1700 
GetChildProcessInfoForSelf(ChildProcessInfo & info)1701 int32_t AppMgrProxy::GetChildProcessInfoForSelf(ChildProcessInfo &info)
1702 {
1703     TAG_LOGD(AAFwkTag::APPMGR, "called");
1704     MessageParcel data;
1705     MessageParcel reply;
1706     MessageOption option;
1707     if (!WriteInterfaceToken(data)) {
1708         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
1709         return ERR_FLATTEN_OBJECT;
1710     }
1711 
1712     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_CHILD_PROCCESS_INFO_FOR_SELF, data, reply, option);
1713     auto result = reply.ReadInt32();
1714     if (result == ERR_OK) {
1715         std::unique_ptr<ChildProcessInfo> infoReply(reply.ReadParcelable<ChildProcessInfo>());
1716         info = *infoReply;
1717     }
1718     return result;
1719 }
1720 
AttachChildProcess(const sptr<IRemoteObject> & childScheduler)1721 void AppMgrProxy::AttachChildProcess(const sptr<IRemoteObject> &childScheduler)
1722 {
1723     TAG_LOGD(AAFwkTag::APPMGR, "called");
1724     if (!childScheduler) {
1725         TAG_LOGE(AAFwkTag::APPMGR, "childScheduler is null");
1726         return;
1727     }
1728     MessageParcel data;
1729     MessageParcel reply;
1730     MessageOption option;
1731     if (!WriteInterfaceToken(data)) {
1732         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
1733         return;
1734     }
1735     PARCEL_UTIL_WRITE_NORET(data, RemoteObject, childScheduler.GetRefPtr());
1736 
1737     PARCEL_UTIL_SENDREQ_NORET(AppMgrInterfaceCode::ATTACH_CHILD_PROCESS, data, reply, option);
1738 }
1739 
ExitChildProcessSafely()1740 void AppMgrProxy::ExitChildProcessSafely()
1741 {
1742     TAG_LOGD(AAFwkTag::APPMGR, "called");
1743     MessageParcel data;
1744     MessageParcel reply;
1745     MessageOption option;
1746     if (!WriteInterfaceToken(data)) {
1747         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
1748         return;
1749     }
1750 
1751     PARCEL_UTIL_SENDREQ_NORET(AppMgrInterfaceCode::EXIT_CHILD_PROCESS_SAFELY, data, reply, option);
1752 }
1753 #endif // SUPPORT_CHILD_PROCESS
1754 
IsFinalAppProcess()1755 bool AppMgrProxy::IsFinalAppProcess()
1756 {
1757     TAG_LOGD(AAFwkTag::APPMGR, "called");
1758     MessageParcel data;
1759     if (!WriteInterfaceToken(data)) {
1760         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1761         return false;
1762     }
1763 
1764     MessageParcel reply;
1765     MessageOption option;
1766     auto ret = SendRequest(AppMgrInterfaceCode::IS_FINAL_APP_PROCESS,
1767         data, reply, option);
1768     if (ret != NO_ERROR) {
1769         TAG_LOGE(AAFwkTag::APPMGR, "Send request is failed, error code: %{public}d", ret);
1770         return false;
1771     }
1772 
1773     return reply.ReadBool();
1774 }
1775 
RegisterRenderStateObserver(const sptr<IRenderStateObserver> & observer)1776 int32_t AppMgrProxy::RegisterRenderStateObserver(const sptr<IRenderStateObserver> &observer)
1777 {
1778     TAG_LOGD(AAFwkTag::APPMGR, "called");
1779     MessageParcel data;
1780     if (!WriteInterfaceToken(data)) {
1781         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1782         return ERR_INVALID_DATA;
1783     }
1784     if (observer == nullptr || !data.WriteRemoteObject(observer->AsObject())) {
1785         TAG_LOGE(AAFwkTag::APPMGR, "Observer is null or Write Remote failed.");
1786         return ERR_FLATTEN_OBJECT;
1787     }
1788 
1789     MessageParcel reply;
1790     MessageOption option;
1791 
1792     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::REGISTER_RENDER_STATUS_OBSERVER, data, reply, option);
1793     return reply.ReadInt32();
1794 }
1795 
UnregisterRenderStateObserver(const sptr<IRenderStateObserver> & observer)1796 int32_t AppMgrProxy::UnregisterRenderStateObserver(const sptr<IRenderStateObserver> &observer)
1797 {
1798     TAG_LOGD(AAFwkTag::APPMGR, "called");
1799     MessageParcel data;
1800     if (!WriteInterfaceToken(data)) {
1801         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1802         return ERR_INVALID_DATA;
1803     }
1804     if (observer == nullptr || !data.WriteRemoteObject(observer->AsObject())) {
1805         TAG_LOGE(AAFwkTag::APPMGR, "Observer is null or Write Remote failed.");
1806         return ERR_FLATTEN_OBJECT;
1807     }
1808 
1809     MessageParcel reply;
1810     MessageOption option;
1811 
1812     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::UNREGISTER_RENDER_STATUS_OBSERVER, data, reply, option);
1813     return reply.ReadInt32();
1814 }
1815 
UpdateRenderState(pid_t renderPid,int32_t state)1816 int32_t AppMgrProxy::UpdateRenderState(pid_t renderPid, int32_t state)
1817 {
1818     TAG_LOGD(AAFwkTag::APPMGR, "called");
1819     MessageParcel data;
1820     if (!WriteInterfaceToken(data)) {
1821         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1822         return ERR_INVALID_DATA;
1823     }
1824     PARCEL_UTIL_WRITE_RET_INT(data, Int32, renderPid);
1825     PARCEL_UTIL_WRITE_RET_INT(data, Int32, state);
1826 
1827     MessageParcel reply;
1828     MessageOption option;
1829 
1830     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::UPDATE_RENDER_STATUS, data, reply, option);
1831     return reply.ReadInt32();
1832 }
1833 
SignRestartAppFlag(int32_t uid,const std::string & instanceKey)1834 int32_t AppMgrProxy::SignRestartAppFlag(int32_t uid, const std::string &instanceKey)
1835 {
1836     TAG_LOGD(AAFwkTag::APPMGR, "called");
1837     MessageParcel data;
1838     MessageParcel reply;
1839     MessageOption option;
1840     if (!WriteInterfaceToken(data)) {
1841         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1842         return IPC_PROXY_ERR;
1843     }
1844     PARCEL_UTIL_WRITE_RET_INT(data, Int32, uid);
1845     PARCEL_UTIL_WRITE_RET_INT(data, String, instanceKey);
1846 
1847     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::SIGN_RESTART_APP_FLAG, data, reply, option);
1848     return reply.ReadInt32();
1849 }
1850 
GetAppRunningUniqueIdByPid(pid_t pid,std::string & appRunningUniqueId)1851 int32_t AppMgrProxy::GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId)
1852 {
1853     TAG_LOGD(AAFwkTag::APPMGR, "called");
1854     MessageParcel data;
1855     MessageParcel reply;
1856     MessageOption option;
1857     if (!WriteInterfaceToken(data)) {
1858         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1859         return IPC_PROXY_ERR;
1860     }
1861     PARCEL_UTIL_WRITE_RET_INT(data, Int32, pid);
1862 
1863     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_APP_RUNNING_UNIQUE_ID_BY_PID, data, reply, option);
1864     auto result = reply.ReadInt32();
1865     if (result == ERR_OK) {
1866         appRunningUniqueId = reply.ReadString();
1867         TAG_LOGD(AAFwkTag::APPMGR, "appRunningUniqueId = %{public}s", appRunningUniqueId.c_str());
1868     }
1869     return result;
1870 }
1871 
GetAllUIExtensionRootHostPid(pid_t pid,std::vector<pid_t> & hostPids)1872 int32_t AppMgrProxy::GetAllUIExtensionRootHostPid(pid_t pid, std::vector<pid_t> &hostPids)
1873 {
1874     MessageParcel data;
1875     if (!WriteInterfaceToken(data)) {
1876         TAG_LOGE(AAFwkTag::APPMGR, "Write remote object failed.");
1877         return ERR_INVALID_DATA;
1878     }
1879 
1880     PARCEL_UTIL_WRITE_RET_INT(data, Int32, pid);
1881 
1882     MessageParcel reply;
1883     MessageOption option;
1884 
1885     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_ALL_UI_EXTENSION_ROOT_HOST_PID, data, reply, option);
1886 
1887     int32_t size = reply.ReadInt32();
1888     if (size > CYCLE_LIMIT) {
1889         TAG_LOGE(AAFwkTag::APPMGR, "Vector is too large.");
1890         return ERR_INVALID_VALUE;
1891     }
1892 
1893     for (int32_t i = 0; i < size; i++) {
1894         pid_t temp = reply.ReadInt32();
1895         hostPids.emplace_back(temp);
1896     }
1897 
1898     return reply.ReadInt32();
1899 }
1900 
GetAllUIExtensionProviderPid(pid_t hostPid,std::vector<pid_t> & providerPids)1901 int32_t AppMgrProxy::GetAllUIExtensionProviderPid(pid_t hostPid, std::vector<pid_t> &providerPids)
1902 {
1903     MessageParcel data;
1904     if (!WriteInterfaceToken(data)) {
1905         TAG_LOGE(AAFwkTag::APPMGR, "Write remote object failed.");
1906         return ERR_INVALID_DATA;
1907     }
1908     PARCEL_UTIL_WRITE_RET_INT(data, Int32, hostPid);
1909 
1910     MessageParcel reply;
1911     MessageOption option;
1912 
1913     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_ALL_UI_EXTENSION_PROVIDER_PID, data, reply, option);
1914 
1915     int32_t size = reply.ReadInt32();
1916     if (size > CYCLE_LIMIT) {
1917         TAG_LOGE(AAFwkTag::APPMGR, "Vector is too large.");
1918         return ERR_INVALID_VALUE;
1919     }
1920 
1921     for (int32_t i = 0; i < size; i++) {
1922         pid_t temp = reply.ReadInt32();
1923         providerPids.emplace_back(temp);
1924     }
1925 
1926     return reply.ReadInt32();
1927 }
1928 
NotifyMemorySizeStateChanged(bool isMemorySizeSufficient)1929 int32_t AppMgrProxy::NotifyMemorySizeStateChanged(bool isMemorySizeSufficient)
1930 {
1931     MessageParcel data;
1932     if (!WriteInterfaceToken(data)) {
1933         return ERR_INVALID_DATA;
1934     }
1935     PARCEL_UTIL_WRITE_RET_INT(data, Bool, isMemorySizeSufficient);
1936 
1937     MessageParcel reply;
1938     MessageOption option;
1939 
1940     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::NOTIFY_MEMORY_SIZE_STATE_CHANGED, data, reply, option);
1941     return reply.ReadInt32();
1942 }
1943 
SetSupportedProcessCacheSelf(bool isSupport)1944 int32_t AppMgrProxy::SetSupportedProcessCacheSelf(bool isSupport)
1945 {
1946     TAG_LOGD(AAFwkTag::APPMGR, "called");
1947     MessageParcel data;
1948     if (!WriteInterfaceToken(data)) {
1949         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1950         return ERR_INVALID_DATA;
1951     }
1952     PARCEL_UTIL_WRITE_RET_INT(data, Bool, isSupport);
1953 
1954     MessageParcel reply;
1955     MessageOption option;
1956 
1957     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::SET_SUPPORTED_PROCESS_CACHE_SELF, data, reply, option);
1958     return reply.ReadInt32();
1959 }
1960 
SetSupportedProcessCache(int32_t pid,bool isSupport)1961 int32_t AppMgrProxy::SetSupportedProcessCache(int32_t pid, bool isSupport)
1962 {
1963     TAG_LOGD(AAFwkTag::APPMGR, "called");
1964     MessageParcel data;
1965     if (!WriteInterfaceToken(data)) {
1966         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1967         return ERR_INVALID_DATA;
1968     }
1969     PARCEL_UTIL_WRITE_RET_INT(data, Bool, isSupport);
1970     PARCEL_UTIL_WRITE_RET_INT(data, Int32, pid);
1971 
1972     MessageParcel reply;
1973     MessageOption option;
1974 
1975     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::SET_SUPPORTED_PROCESS_CACHE, data, reply, option);
1976     return reply.ReadInt32();
1977 }
1978 
SetAppAssertionPauseState(bool flag)1979 void AppMgrProxy::SetAppAssertionPauseState(bool flag)
1980 {
1981     TAG_LOGD(AAFwkTag::APPMGR, "called");
1982     MessageParcel data;
1983     MessageParcel reply;
1984     MessageOption option;
1985     if (!WriteInterfaceToken(data)) {
1986         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1987         return;
1988     }
1989     PARCEL_UTIL_WRITE_NORET(data, Bool, flag);
1990 
1991     PARCEL_UTIL_SENDREQ_NORET(AppMgrInterfaceCode::SET_APP_ASSERT_PAUSE_STATE_SELF, data, reply, option);
1992 }
1993 
1994 #ifdef SUPPORT_CHILD_PROCESS
StartNativeChildProcess(const std::string & libName,int32_t childProcessCount,const sptr<IRemoteObject> & callback)1995 int32_t AppMgrProxy::StartNativeChildProcess(const std::string &libName, int32_t childProcessCount,
1996     const sptr<IRemoteObject> &callback)
1997 {
1998     TAG_LOGD(AAFwkTag::APPMGR, "called");
1999     if (libName.empty() || !callback) {
2000         TAG_LOGE(AAFwkTag::APPMGR, "Invalid params, libName:%{private}s", libName.c_str());
2001         return ERR_INVALID_VALUE;
2002     }
2003 
2004     MessageParcel data;
2005     MessageParcel reply;
2006     MessageOption option;
2007     if (!WriteInterfaceToken(data)) {
2008         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
2009         return IPC_PROXY_ERR;
2010     }
2011     PARCEL_UTIL_WRITE_RET_INT(data, String, libName);
2012     PARCEL_UTIL_WRITE_RET_INT(data, Int32, childProcessCount);
2013     PARCEL_UTIL_WRITE_RET_INT(data, RemoteObject, callback);
2014 
2015     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::START_NATIVE_CHILD_PROCESS, data, reply, option);
2016     return reply.ReadInt32();
2017 }
2018 #endif // SUPPORT_CHILD_PROCESS
2019 
CheckCallingIsUserTestMode(const pid_t pid,bool & isUserTest)2020 int32_t AppMgrProxy::CheckCallingIsUserTestMode(const pid_t pid, bool &isUserTest)
2021 {
2022     MessageParcel data;
2023     MessageParcel reply;
2024     MessageOption option(MessageOption::TF_SYNC);
2025     if (!WriteInterfaceToken(data)) {
2026         return ERR_FLATTEN_OBJECT;
2027     }
2028     PARCEL_UTIL_WRITE_RET_INT(data, Int32, pid);
2029     int32_t ret = SendRequest(AppMgrInterfaceCode::CHECK_CALLING_IS_USER_TEST_MODE, data, reply, option);
2030     if (ret != NO_ERROR) {
2031         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
2032         isUserTest = false;
2033         return ret;
2034     }
2035     isUserTest = reply.ReadBool();
2036     return reply.ReadInt32();
2037 }
2038 
NotifyProcessDependedOnWeb()2039 int32_t AppMgrProxy::NotifyProcessDependedOnWeb()
2040 {
2041     MessageParcel data;
2042     MessageParcel reply;
2043     MessageOption option;
2044     if (!WriteInterfaceToken(data)) {
2045         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
2046         return IPC_PROXY_ERR;
2047     }
2048 
2049     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::NOTIFY_PROCESS_DEPENDED_ON_WEB, data, reply, option);
2050     return reply.ReadInt32();
2051 }
2052 
KillProcessDependedOnWeb()2053 void AppMgrProxy::KillProcessDependedOnWeb()
2054 {
2055     MessageParcel data;
2056     MessageParcel reply;
2057     MessageOption option;
2058     if (!WriteInterfaceToken(data)) {
2059         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
2060         return;
2061     }
2062 
2063     PARCEL_UTIL_SENDREQ_NORET(AppMgrInterfaceCode::KILL_PROCESS_DEPENDED_ON_WEB, data, reply, option);
2064 }
2065 
RestartResidentProcessDependedOnWeb()2066 void AppMgrProxy::RestartResidentProcessDependedOnWeb()
2067 {
2068     MessageParcel data;
2069     MessageParcel reply;
2070     MessageOption option(MessageOption::TF_ASYNC);
2071     if (!WriteInterfaceToken(data)) {
2072         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
2073         return;
2074     }
2075 
2076     PARCEL_UTIL_SENDREQ_NORET(AppMgrInterfaceCode::RESTART_RESIDENT_PROCESS_DEPENDED_ON_WEB, data, reply, option);
2077 }
2078 
GetSupportedProcessCachePids(const std::string & bundleName,std::vector<int32_t> & pidList)2079 int32_t AppMgrProxy::GetSupportedProcessCachePids(const std::string &bundleName,
2080     std::vector<int32_t> &pidList)
2081 {
2082     TAG_LOGD(AAFwkTag::APPMGR, "Called.");
2083     MessageParcel data;
2084     MessageParcel reply;
2085     MessageOption option(MessageOption::TF_SYNC);
2086     if (!WriteInterfaceToken(data)) {
2087         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
2088         return ERR_FLATTEN_OBJECT;
2089     }
2090     if (!data.WriteString(bundleName)) {
2091         TAG_LOGE(AAFwkTag::APPMGR, "write bundleName failed.");
2092         return ERR_INVALID_VALUE;
2093     }
2094     auto ret = SendRequest(AppMgrInterfaceCode::GET_SUPPORTED_PROCESS_CACHE_PIDS, data, reply, option);
2095     if (ret != NO_ERROR) {
2096         TAG_LOGE(AAFwkTag::APPMGR, "Send request failed with error code %{public}d.", ret);
2097         return ret;
2098     }
2099 
2100     pidList.clear();
2101     int32_t infoSize = reply.ReadInt32();
2102     if (infoSize > CYCLE_LIMIT) {
2103         TAG_LOGE(AAFwkTag::APPMGR, "infoSize is too large");
2104         return ERR_INVALID_VALUE;
2105     }
2106     for (int32_t i = 0; i < infoSize; i++) {
2107         pidList.push_back(reply.ReadInt32());
2108     }
2109     return reply.ReadInt32();
2110 }
2111 
RegisterKiaInterceptor(const sptr<IKiaInterceptor> & interceptor)2112 int32_t AppMgrProxy::RegisterKiaInterceptor(const sptr<IKiaInterceptor> &interceptor)
2113 {
2114     if (interceptor == nullptr) {
2115         TAG_LOGE(AAFwkTag::APPMGR, "interceptor is nullptr.");
2116         return ERR_INVALID_VALUE;
2117     }
2118     MessageParcel data;
2119     MessageParcel reply;
2120     MessageOption option(MessageOption::TF_SYNC);
2121     if (!WriteInterfaceToken(data)) {
2122         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
2123         return ERR_INVALID_VALUE;
2124     }
2125 
2126     if (!data.WriteRemoteObject(interceptor->AsObject())) {
2127         TAG_LOGE(AAFwkTag::APPMGR, "write interceptor failed.");
2128         return ERR_INVALID_VALUE;
2129     }
2130 
2131     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::REGISTER_KIA_INTERCEPTOR, data, reply, option);
2132     return reply.ReadInt32();
2133 }
2134 
CheckIsKiaProcess(pid_t pid,bool & isKia)2135 int32_t AppMgrProxy::CheckIsKiaProcess(pid_t pid, bool &isKia)
2136 {
2137     MessageParcel data;
2138     MessageParcel reply;
2139     MessageOption option(MessageOption::TF_SYNC);
2140     if (!WriteInterfaceToken(data)) {
2141         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
2142         return ERR_INVALID_VALUE;
2143     }
2144 
2145     if (!data.WriteInt32(pid)) {
2146         TAG_LOGE(AAFwkTag::APPMGR, "write pid failed.");
2147         return ERR_INVALID_VALUE;
2148     }
2149 
2150     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::CHECK_IS_KIA_PROCESS, data, reply, option);
2151     int32_t ret = reply.ReadInt32();
2152     if (ret != ERR_OK) {
2153         TAG_LOGE(AAFwkTag::APPMGR, "failed,ret=%{public}d.", ret);
2154         return ret;
2155     }
2156     isKia = reply.ReadBool();
2157     return ERR_OK;
2158 }
2159 
KillAppSelfWithInstanceKey(const std::string & instanceKey,bool clearPageStack,const std::string & reason)2160 int32_t AppMgrProxy::KillAppSelfWithInstanceKey(const std::string &instanceKey, bool clearPageStack,
2161     const std::string& reason)
2162 {
2163     MessageParcel data;
2164     if (!WriteInterfaceToken(data)) {
2165         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
2166         return ERR_INVALID_VALUE;
2167     }
2168     PARCEL_UTIL_WRITE_RET_INT(data, String, instanceKey);
2169     PARCEL_UTIL_WRITE_RET_INT(data, Bool, clearPageStack);
2170     PARCEL_UTIL_WRITE_RET_INT(data, String, reason);
2171 
2172     MessageParcel reply;
2173     MessageOption option(MessageOption::TF_SYNC);
2174     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::KILL_APP_SELF_WITH_INSTANCE_KEY, data, reply, option);
2175     return reply.ReadInt32();
2176 }
2177 
UpdateInstanceKeyBySpecifiedId(int32_t specifiedId,std::string & instanceKey)2178 void AppMgrProxy::UpdateInstanceKeyBySpecifiedId(int32_t specifiedId, std::string &instanceKey)
2179 {
2180     MessageParcel data;
2181     if (!WriteInterfaceToken(data)) {
2182         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
2183         return;
2184     }
2185     PARCEL_UTIL_WRITE_NORET(data, Int32, specifiedId);
2186     PARCEL_UTIL_WRITE_NORET(data, String, instanceKey);
2187 
2188     MessageParcel reply;
2189     MessageOption option(MessageOption::TF_SYNC);
2190     PARCEL_UTIL_SENDREQ_NORET(AppMgrInterfaceCode::UPDATE_INSTANCE_KEY_BY_SPECIFIED_ID, data, reply, option);
2191 }
2192 
IsSpecifiedModuleLoaded(const AAFwk::Want & want,const AbilityInfo & abilityInfo,bool & result)2193 int32_t AppMgrProxy::IsSpecifiedModuleLoaded(const AAFwk::Want &want, const AbilityInfo &abilityInfo, bool &result)
2194 {
2195     MessageParcel data;
2196     MessageParcel reply;
2197     MessageOption option;
2198     if (!WriteInterfaceToken(data)) {
2199         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
2200         return ERR_FLATTEN_OBJECT;
2201     }
2202     PARCEL_UTIL_WRITE_RET_INT(data, Parcelable, &want);
2203     PARCEL_UTIL_WRITE_RET_INT(data, Parcelable, &abilityInfo);
2204 
2205     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::IS_SPECIFIED_MODULE_LOADED, data, reply, option);
2206     result = reply.ReadBool();
2207     return ERR_OK;
2208 }
2209 
UpdateProcessMemoryState(const std::vector<ProcessMemoryState> & procMemState)2210 int32_t AppMgrProxy::UpdateProcessMemoryState(const std::vector<ProcessMemoryState> &procMemState)
2211 {
2212     MessageParcel data;
2213     MessageParcel reply;
2214 
2215     auto size = procMemState.size();
2216     if (size == 0 || size > MAX_PROCESS_STATE_COUNT) {
2217         TAG_LOGE(AAFwkTag::APPMGR, "Invalid size");
2218         return ERR_FLATTEN_OBJECT;
2219     }
2220     if (!WriteInterfaceToken(data)) {
2221         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
2222         return IPC_PROXY_ERR;
2223     }
2224     if (!data.WriteUint32(size)) {
2225         TAG_LOGE(AAFwkTag::APPMGR, "Write size failed");
2226         return ERR_FLATTEN_OBJECT;
2227     }
2228     for (const auto &perState: procMemState) {
2229         if (!data.WriteParcelable(&perState)) {
2230             TAG_LOGE(AAFwkTag::APPMGR, "Write perState failed");
2231             return ERR_FLATTEN_OBJECT;
2232         }
2233     }
2234     if (!SendTransactCmd(AppMgrInterfaceCode::UPDATE_PROCESS_MEMORY_STATE, data, reply)) {
2235         TAG_LOGE(AAFwkTag::APPMGR, "SendRequest err");
2236         return IPC_PROXY_ERR;
2237     }
2238     return reply.ReadInt32();
2239 }
2240 
GetKilledProcessInfo(int pid,int uid,KilledProcessInfo & info)2241 int32_t AppMgrProxy::GetKilledProcessInfo(int pid, int uid, KilledProcessInfo &info)
2242 {
2243     MessageParcel data;
2244     MessageParcel reply;
2245     MessageOption option;
2246     if (!WriteInterfaceToken(data)) {
2247         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
2248         return AAFwk::ERR_WRITE_INTERFACE_TOKEN_FAILED;
2249     }
2250     PARCEL_UTIL_WRITE_RET_INT(data, Int32, pid);
2251     PARCEL_UTIL_WRITE_RET_INT(data, Int32, uid);
2252 
2253     PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::GET_KILLED_PROCESS_INFO, data, reply, option);
2254     std::unique_ptr<KilledProcessInfo> infoReply(reply.ReadParcelable<KilledProcessInfo>());
2255     if (infoReply == nullptr) {
2256         TAG_LOGE(AAFwkTag::APPMGR, "KilledProcessInfo ReadParcelable nullptr");
2257         return AAFwk::ERR_READ_RESULT_PARCEL_FAILED;
2258     }
2259 
2260     info = *infoReply;
2261     return ERR_OK;
2262 }
2263 }  // namespace AppExecFwk
2264 }  // namespace OHOS
2265