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