1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "app_mgr_proxy.h"
17
18 #include "appexecfwk_errors.h"
19 #include "hilog_wrapper.h"
20 #include "hitrace_meter.h"
21 #include "ipc_types.h"
22 #include "iremote_object.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 constexpr int32_t CYCLE_LIMIT = 1000;
AppMgrProxy(const sptr<IRemoteObject> & impl)27 AppMgrProxy::AppMgrProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IAppMgr>(impl)
28 {}
29
WriteInterfaceToken(MessageParcel & data)30 bool AppMgrProxy::WriteInterfaceToken(MessageParcel &data)
31 {
32 if (!data.WriteInterfaceToken(AppMgrProxy::GetDescriptor())) {
33 HILOG_ERROR("write interface token failed");
34 return false;
35 }
36 return true;
37 }
38
AttachApplication(const sptr<IRemoteObject> & obj)39 void AppMgrProxy::AttachApplication(const sptr<IRemoteObject> &obj)
40 {
41 MessageParcel data;
42 MessageParcel reply;
43 MessageOption option(MessageOption::TF_SYNC);
44 if (!WriteInterfaceToken(data)) {
45 return;
46 }
47 if (!data.WriteRemoteObject(obj.GetRefPtr())) {
48 HILOG_ERROR("Failed to write remote object");
49 return;
50 }
51 sptr<IRemoteObject> remote = Remote();
52 if (remote == nullptr) {
53 HILOG_ERROR("Remote() is NULL");
54 return;
55 }
56 int32_t ret =
57 remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::APP_ATTACH_APPLICATION), data, reply, option);
58 if (ret != NO_ERROR) {
59 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
60 }
61 }
62
ApplicationForegrounded(const int32_t recordId)63 void AppMgrProxy::ApplicationForegrounded(const int32_t recordId)
64 {
65 MessageParcel data;
66 MessageParcel reply;
67 MessageOption option(MessageOption::TF_SYNC);
68 if (!WriteInterfaceToken(data)) {
69 return;
70 }
71 data.WriteInt32(recordId);
72 sptr<IRemoteObject> remote = Remote();
73 if (remote == nullptr) {
74 HILOG_ERROR("Remote() is NULL");
75 return;
76 }
77 int32_t ret = remote->SendRequest(
78 static_cast<uint32_t>(AppMgrInterfaceCode::APP_APPLICATION_FOREGROUNDED), data, reply, option);
79 if (ret != NO_ERROR) {
80 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
81 }
82 }
83
ApplicationBackgrounded(const int32_t recordId)84 void AppMgrProxy::ApplicationBackgrounded(const int32_t recordId)
85 {
86 MessageParcel data;
87 MessageParcel reply;
88 MessageOption option(MessageOption::TF_SYNC);
89 if (!WriteInterfaceToken(data)) {
90 return;
91 }
92 data.WriteInt32(recordId);
93 sptr<IRemoteObject> remote = Remote();
94 if (remote == nullptr) {
95 HILOG_ERROR("Remote() is NULL");
96 return;
97 }
98 int32_t ret = remote->SendRequest(
99 static_cast<uint32_t>(AppMgrInterfaceCode::APP_APPLICATION_BACKGROUNDED), data, reply, option);
100 if (ret != NO_ERROR) {
101 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
102 }
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 data.WriteInt32(recordId);
114 sptr<IRemoteObject> remote = Remote();
115 if (remote == nullptr) {
116 HILOG_ERROR("Remote() is NULL");
117 return;
118 }
119 int32_t ret = remote->SendRequest(
120 static_cast<uint32_t>(AppMgrInterfaceCode::APP_APPLICATION_TERMINATED), data, reply, option);
121 if (ret != NO_ERROR) {
122 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
123 }
124 }
125
CheckPermission(const int32_t recordId,const std::string & permission)126 int32_t AppMgrProxy::CheckPermission(const int32_t recordId, const std::string &permission)
127 {
128 MessageParcel data;
129 MessageParcel reply;
130 MessageOption option(MessageOption::TF_SYNC);
131 if (!WriteInterfaceToken(data)) {
132 return ERR_PERMISSION_DENIED;
133 }
134 data.WriteInt32(recordId);
135 data.WriteString(permission);
136 sptr<IRemoteObject> remote = Remote();
137 if (remote == nullptr) {
138 HILOG_ERROR("Remote() is NULL");
139 return ERR_PERMISSION_DENIED;
140 }
141 int32_t ret =
142 remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::APP_CHECK_PERMISSION), data, reply, option);
143 if (ret != NO_ERROR) {
144 HILOG_ERROR("SendRequest is failed, error code: %{public}d", ret);
145 return ERR_PERMISSION_DENIED;
146 }
147 return reply.ReadInt32();
148 }
149
AbilityCleaned(const sptr<IRemoteObject> & token)150 void AppMgrProxy::AbilityCleaned(const sptr<IRemoteObject> &token)
151 {
152 MessageParcel data;
153 MessageParcel reply;
154 MessageOption option(MessageOption::TF_SYNC);
155 if (!WriteInterfaceToken(data)) {
156 return;
157 }
158 if (!data.WriteRemoteObject(token.GetRefPtr())) {
159 HILOG_ERROR("Failed to write token");
160 return;
161 }
162 sptr<IRemoteObject> remote = Remote();
163 if (remote == nullptr) {
164 HILOG_ERROR("Remote() is NULL");
165 return;
166 }
167 int32_t ret =
168 remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::APP_ABILITY_CLEANED), data, reply, option);
169 if (ret != NO_ERROR) {
170 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
171 }
172 }
173
GetAmsMgr()174 sptr<IAmsMgr> AppMgrProxy::GetAmsMgr()
175 {
176 MessageParcel data;
177 MessageParcel reply;
178 if (!WriteInterfaceToken(data)) {
179 return nullptr;
180 }
181 if (!SendTransactCmd(AppMgrInterfaceCode::APP_GET_MGR_INSTANCE, data, reply)) {
182 return nullptr;
183 }
184 sptr<IRemoteObject> object = reply.ReadRemoteObject();
185 sptr<IAmsMgr> amsMgr = iface_cast<IAmsMgr>(object);
186 if (!amsMgr) {
187 HILOG_ERROR("ams instance is nullptr");
188 return nullptr;
189 }
190 return amsMgr;
191 }
192
ClearUpApplicationData(const std::string & bundleName)193 int32_t AppMgrProxy::ClearUpApplicationData(const std::string &bundleName)
194 {
195 MessageParcel data;
196 MessageParcel reply;
197 MessageOption option(MessageOption::TF_SYNC);
198 if (!WriteInterfaceToken(data)) {
199 return ERR_FLATTEN_OBJECT;
200 }
201 sptr<IRemoteObject> remote = Remote();
202 if (remote == nullptr) {
203 HILOG_ERROR("Remote() is NULL");
204 return ERR_NULL_OBJECT;
205 }
206 if (!data.WriteString(bundleName)) {
207 HILOG_ERROR("parcel WriteString failed");
208 return ERR_FLATTEN_OBJECT;
209 }
210 int32_t ret = remote->SendRequest(
211 static_cast<uint32_t>(AppMgrInterfaceCode::APP_CLEAR_UP_APPLICATION_DATA), data, reply, option);
212 if (ret != NO_ERROR) {
213 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
214 return ret;
215 }
216 return reply.ReadInt32();
217 }
218
GetAllRunningProcesses(std::vector<RunningProcessInfo> & info)219 int32_t AppMgrProxy::GetAllRunningProcesses(std::vector<RunningProcessInfo> &info)
220 {
221 MessageParcel data;
222 MessageParcel reply;
223 MessageOption option(MessageOption::TF_SYNC);
224 if (!WriteInterfaceToken(data)) {
225 return ERR_FLATTEN_OBJECT;
226 }
227 sptr<IRemoteObject> remote = Remote();
228 if (remote == nullptr) {
229 HILOG_ERROR("Remote() is NULL");
230 return ERR_NULL_OBJECT;
231 }
232 if (!SendTransactCmd(AppMgrInterfaceCode::APP_GET_ALL_RUNNING_PROCESSES, data, reply)) {
233 return ERR_NULL_OBJECT;
234 }
235 auto error = GetParcelableInfos<RunningProcessInfo>(reply, info);
236 if (error != NO_ERROR) {
237 HILOG_ERROR("GetParcelableInfos fail, error: %{public}d", error);
238 return error;
239 }
240 int result = reply.ReadInt32();
241 return result;
242 }
243
GetAllRenderProcesses(std::vector<RenderProcessInfo> & info)244 int32_t AppMgrProxy::GetAllRenderProcesses(std::vector<RenderProcessInfo> &info)
245 {
246 MessageParcel data;
247 MessageParcel reply;
248 MessageOption option(MessageOption::TF_SYNC);
249 if (!WriteInterfaceToken(data)) {
250 return ERR_FLATTEN_OBJECT;
251 }
252 sptr<IRemoteObject> remote = Remote();
253 if (remote == nullptr) {
254 HILOG_ERROR("Remote() is NULL");
255 return ERR_NULL_OBJECT;
256 }
257 if (!SendTransactCmd(AppMgrInterfaceCode::APP_GET_ALL_RENDER_PROCESSES, data, reply)) {
258 return ERR_NULL_OBJECT;
259 }
260 auto error = GetParcelableInfos<RenderProcessInfo>(reply, info);
261 if (error != NO_ERROR) {
262 HILOG_ERROR("GetParcelableInfos fail, error: %{public}d", error);
263 return error;
264 }
265 int result = reply.ReadInt32();
266 return result;
267 }
268
JudgeSandboxByPid(pid_t pid,bool & isSandbox)269 int32_t AppMgrProxy::JudgeSandboxByPid(pid_t pid, bool &isSandbox)
270 {
271 MessageParcel data;
272 MessageParcel reply;
273 MessageOption option;
274 if (!WriteInterfaceToken(data)) {
275 return ERR_FLATTEN_OBJECT;
276 }
277 if (!data.WriteInt32(pid)) {
278 HILOG_ERROR("Pid write failed.");
279 return ERR_FLATTEN_OBJECT;
280 }
281 sptr<IRemoteObject> remote = Remote();
282 if (remote == nullptr) {
283 HILOG_ERROR("Remote() is NULL");
284 return ERR_NULL_OBJECT;
285 }
286 int32_t ret = remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::JUDGE_SANDBOX_BY_PID),
287 data, reply, option);
288 if (ret != NO_ERROR) {
289 HILOG_ERROR("SendRequest is failed, error code: %{public}d", ret);
290 return ret;
291 }
292 isSandbox = reply.ReadBool();
293 return reply.ReadInt32();
294 }
295
GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> & info,int32_t userId)296 int32_t AppMgrProxy::GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId)
297 {
298 MessageParcel data;
299 MessageParcel reply;
300 MessageOption option(MessageOption::TF_SYNC);
301
302 if (!WriteInterfaceToken(data)) {
303 return ERR_FLATTEN_OBJECT;
304 }
305 data.WriteInt32(userId);
306 sptr<IRemoteObject> remote = Remote();
307 if (remote == nullptr) {
308 HILOG_ERROR("Remote() is NULL");
309 return ERR_NULL_OBJECT;
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 HILOG_ERROR("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 info = *infoReply;
336 return reply.ReadInt32();
337 }
338
NotifyMemoryLevel(int32_t level)339 int32_t AppMgrProxy::NotifyMemoryLevel(int32_t level)
340 {
341 MessageParcel data;
342 MessageParcel reply;
343 MessageOption option(MessageOption::TF_SYNC);
344
345 if (!WriteInterfaceToken(data)) {
346 return ERR_FLATTEN_OBJECT;
347 }
348 data.WriteInt32(level);
349 sptr<IRemoteObject> remote = Remote();
350 if (remote == nullptr) {
351 HILOG_ERROR("Remote() is NULL");
352 return ERR_NULL_OBJECT;
353 }
354 int32_t ret =
355 remote->SendRequest(
356 static_cast<uint32_t>(AppMgrInterfaceCode::APP_NOTIFY_MEMORY_LEVEL), data, reply, option);
357 if (ret != NO_ERROR) {
358 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
359 }
360 int result = reply.ReadInt32();
361 return result;
362 }
363
DumpHeapMemory(const int32_t pid,OHOS::AppExecFwk::MallocInfo & mallocInfo)364 int32_t AppMgrProxy::DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo)
365 {
366 HILOG_DEBUG("AppMgrProxy::DumpHeapMemory.");
367 MessageParcel data;
368 MessageParcel reply;
369 if (!WriteInterfaceToken(data)) {
370 return ERR_FLATTEN_OBJECT;
371 }
372 data.WriteInt32(pid);
373 sptr<IRemoteObject> remote = Remote();
374 if (remote == nullptr) {
375 HILOG_ERROR("Remote() is NULL");
376 return ERR_NULL_OBJECT;
377 }
378
379 MessageOption option(MessageOption::TF_SYNC);
380 int32_t ret =
381 remote->SendRequest(
382 static_cast<uint32_t>(AppMgrInterfaceCode::DUMP_HEAP_MEMORY_PROCESS), data, reply, option);
383 if (ret != NO_ERROR) {
384 HILOG_ERROR("AppMgrProxy SendRequest is failed, error code: %{public}d", ret);
385 return ret;
386 }
387
388 std::unique_ptr<MallocInfo> info(reply.ReadParcelable<MallocInfo>());
389 if (info == nullptr) {
390 HILOG_ERROR("MallocInfo ReadParcelable nullptr");
391 return ERR_NULL_OBJECT;
392 }
393 mallocInfo = *info;
394 return ret;
395 }
396
SendTransactCmd(AppMgrInterfaceCode code,MessageParcel & data,MessageParcel & reply)397 bool AppMgrProxy::SendTransactCmd(AppMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply)
398 {
399 MessageOption option(MessageOption::TF_SYNC);
400 sptr<IRemoteObject> remote = Remote();
401 if (!remote) {
402 HILOG_ERROR("fail to send transact cmd %{public}d due to remote object", code);
403 return false;
404 }
405 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
406 if (result != NO_ERROR) {
407 HILOG_ERROR("receive error transact code %{public}d in transact cmd %{public}d", result, code);
408 return false;
409 }
410 return true;
411 }
412
AddAbilityStageDone(const int32_t recordId)413 void AppMgrProxy::AddAbilityStageDone(const int32_t recordId)
414 {
415 MessageParcel data;
416 MessageParcel reply;
417 if (!WriteInterfaceToken(data)) {
418 HILOG_ERROR("WriteInterfaceToken failed");
419 return;
420 }
421
422 if (!data.WriteInt32(recordId)) {
423 HILOG_ERROR("want write failed.");
424 return;
425 }
426
427 if (!SendTransactCmd(AppMgrInterfaceCode::APP_ADD_ABILITY_STAGE_INFO_DONE, data, reply)) {
428 HILOG_ERROR("SendTransactCmd failed");
429 return;
430 }
431 return;
432 }
433
StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> & bundleInfos)434 void AppMgrProxy::StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos)
435 {
436 MessageParcel data;
437 MessageParcel reply;
438 if (!WriteInterfaceToken(data)) {
439 HILOG_ERROR("WriteInterfaceToken failed");
440 return;
441 }
442
443 if (!data.WriteInt32(bundleInfos.size())) {
444 HILOG_ERROR("write bundle info size failed.");
445 return;
446 }
447
448 for (auto &bundleInfo : bundleInfos) {
449 if (!data.WriteParcelable(&bundleInfo)) {
450 HILOG_ERROR("write bundle info failed");
451 return;
452 }
453 }
454
455 if (!SendTransactCmd(AppMgrInterfaceCode::STARTUP_RESIDENT_PROCESS, data, reply)) {
456 HILOG_ERROR("SendTransactCmd failed");
457 return;
458 }
459 return;
460 }
461
462 template<typename T>
GetParcelableInfos(MessageParcel & reply,std::vector<T> & parcelableInfos)463 int AppMgrProxy::GetParcelableInfos(MessageParcel &reply, std::vector<T> &parcelableInfos)
464 {
465 int32_t infoSize = reply.ReadInt32();
466 if (infoSize > CYCLE_LIMIT) {
467 HILOG_ERROR("infoSize is too large");
468 return ERR_INVALID_VALUE;
469 }
470 for (int32_t i = 0; i < infoSize; i++) {
471 std::unique_ptr<T> info(reply.ReadParcelable<T>());
472 if (!info) {
473 HILOG_ERROR("Read Parcelable infos failed");
474 return ERR_INVALID_VALUE;
475 }
476 parcelableInfos.emplace_back(*info);
477 }
478 HILOG_DEBUG("get parcelable infos success");
479 return NO_ERROR;
480 }
481
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer,const std::vector<std::string> & bundleNameList)482 int AppMgrProxy::RegisterApplicationStateObserver(
483 const sptr<IApplicationStateObserver> &observer, const std::vector<std::string> &bundleNameList)
484 {
485 if (!observer) {
486 HILOG_ERROR("observer null");
487 return ERR_INVALID_VALUE;
488 }
489 HILOG_DEBUG("RegisterApplicationStateObserver start");
490 MessageParcel data;
491 MessageParcel reply;
492 MessageOption option;
493 if (!WriteInterfaceToken(data)) {
494 return ERR_FLATTEN_OBJECT;
495 }
496 if (!data.WriteRemoteObject(observer->AsObject())) {
497 HILOG_ERROR("observer write failed.");
498 return ERR_FLATTEN_OBJECT;
499 }
500 if (!data.WriteStringVector(bundleNameList)) {
501 HILOG_ERROR("bundleNameList write failed.");
502 return ERR_FLATTEN_OBJECT;
503 }
504
505 auto error = Remote()->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::REGISTER_APPLICATION_STATE_OBSERVER),
506 data, reply, option);
507 if (error != NO_ERROR) {
508 HILOG_ERROR("Send request error: %{public}d", error);
509 return error;
510 }
511 return reply.ReadInt32();
512 }
513
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)514 int AppMgrProxy::UnregisterApplicationStateObserver(
515 const sptr<IApplicationStateObserver> &observer)
516 {
517 if (!observer) {
518 HILOG_ERROR("observer null");
519 return ERR_INVALID_VALUE;
520 }
521 HILOG_DEBUG("UnregisterApplicationStateObserver start");
522 MessageParcel data;
523 MessageParcel reply;
524 MessageOption option;
525 if (!WriteInterfaceToken(data)) {
526 return ERR_FLATTEN_OBJECT;
527 }
528 if (!data.WriteRemoteObject(observer->AsObject())) {
529 HILOG_ERROR("observer write failed.");
530 return ERR_FLATTEN_OBJECT;
531 }
532
533 auto error = Remote()->SendRequest(
534 static_cast<uint32_t>(AppMgrInterfaceCode::UNREGISTER_APPLICATION_STATE_OBSERVER),
535 data, reply, option);
536 if (error != NO_ERROR) {
537 HILOG_ERROR("Send request error: %{public}d", error);
538 return error;
539 }
540 return reply.ReadInt32();
541 }
542
GetForegroundApplications(std::vector<AppStateData> & list)543 int AppMgrProxy::GetForegroundApplications(std::vector<AppStateData> &list)
544 {
545 MessageParcel data;
546 MessageParcel reply;
547 MessageOption option;
548 if (!WriteInterfaceToken(data)) {
549 return ERR_FLATTEN_OBJECT;
550 }
551 auto error = Remote()->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::GET_FOREGROUND_APPLICATIONS),
552 data, reply, option);
553 if (error != NO_ERROR) {
554 HILOG_ERROR("GetForegroundApplications fail, error: %{public}d", error);
555 return error;
556 }
557 int32_t infoSize = reply.ReadInt32();
558 if (infoSize > CYCLE_LIMIT) {
559 HILOG_ERROR("infoSize is too large");
560 return ERR_INVALID_VALUE;
561 }
562 for (int32_t i = 0; i < infoSize; i++) {
563 std::unique_ptr<AppStateData> info(reply.ReadParcelable<AppStateData>());
564 if (!info) {
565 HILOG_ERROR("Read Parcelable infos failed.");
566 return ERR_INVALID_VALUE;
567 }
568 list.emplace_back(*info);
569 }
570 return reply.ReadInt32();
571 }
572
StartUserTestProcess(const AAFwk::Want & want,const sptr<IRemoteObject> & observer,const BundleInfo & bundleInfo,int32_t userId)573 int AppMgrProxy::StartUserTestProcess(
574 const AAFwk::Want &want, const sptr<IRemoteObject> &observer, const BundleInfo &bundleInfo, int32_t userId)
575 {
576 MessageParcel data;
577 MessageParcel reply;
578 MessageOption option;
579
580 if (!WriteInterfaceToken(data)) {
581 return ERR_FLATTEN_OBJECT;
582 }
583 if (!data.WriteParcelable(&want)) {
584 HILOG_ERROR("want write failed.");
585 return ERR_FLATTEN_OBJECT;
586 }
587 if (!data.WriteRemoteObject(observer)) {
588 HILOG_ERROR("observer write failed.");
589 return ERR_FLATTEN_OBJECT;
590 }
591 if (!data.WriteParcelable(&bundleInfo)) {
592 HILOG_ERROR("bundleInfo write failed.");
593 return ERR_FLATTEN_OBJECT;
594 }
595 if (!data.WriteInt32(userId)) {
596 HILOG_ERROR("userId write failed.");
597 return ERR_FLATTEN_OBJECT;
598 }
599 int32_t ret =
600 Remote()->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::START_USER_TEST_PROCESS),
601 data, reply, option);
602 if (ret != NO_ERROR) {
603 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
604 return ret;
605 }
606 return reply.ReadInt32();
607 }
608
FinishUserTest(const std::string & msg,const int64_t & resultCode,const std::string & bundleName)609 int AppMgrProxy::FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName)
610 {
611 MessageParcel data;
612 MessageParcel reply;
613 MessageOption option;
614
615 if (!WriteInterfaceToken(data)) {
616 return ERR_FLATTEN_OBJECT;
617 }
618 if (!data.WriteString(msg)) {
619 HILOG_ERROR("msg write failed.");
620 return ERR_FLATTEN_OBJECT;
621 }
622 if (!data.WriteInt64(resultCode)) {
623 HILOG_ERROR("resultCode:WriteInt32 fail.");
624 return ERR_FLATTEN_OBJECT;
625 }
626 if (!data.WriteString(bundleName)) {
627 HILOG_ERROR("bundleName write failed.");
628 return ERR_FLATTEN_OBJECT;
629 }
630 int32_t ret =
631 Remote()->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::FINISH_USER_TEST), data, reply, option);
632 if (ret != NO_ERROR) {
633 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
634 return ret;
635 }
636 return reply.ReadInt32();
637 }
638
ScheduleAcceptWantDone(const int32_t recordId,const AAFwk::Want & want,const std::string & flag)639 void AppMgrProxy::ScheduleAcceptWantDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag)
640 {
641 MessageParcel data;
642 MessageParcel reply;
643 if (!WriteInterfaceToken(data)) {
644 HILOG_ERROR("WriteInterfaceToken failed");
645 return;
646 }
647
648 if (!data.WriteInt32(recordId) || !data.WriteParcelable(&want) || !data.WriteString(flag)) {
649 HILOG_ERROR("want write failed.");
650 return;
651 }
652
653 if (!SendTransactCmd(AppMgrInterfaceCode::SCHEDULE_ACCEPT_WANT_DONE, data, reply)) {
654 HILOG_ERROR("SendTransactCmd failed");
655 return;
656 }
657 }
658
GetAbilityRecordsByProcessID(const int pid,std::vector<sptr<IRemoteObject>> & tokens)659 int AppMgrProxy::GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens)
660 {
661 MessageParcel data;
662 MessageParcel reply;
663 MessageOption option(MessageOption::TF_SYNC);
664
665 if (!WriteInterfaceToken(data)) {
666 return ERR_FLATTEN_OBJECT;
667 }
668 data.WriteInt32(pid);
669 if (!SendTransactCmd(AppMgrInterfaceCode::APP_GET_ABILITY_RECORDS_BY_PROCESS_ID, data, reply)) {
670 return ERR_NULL_OBJECT;
671 }
672 int32_t infoSize = reply.ReadInt32();
673 if (infoSize > CYCLE_LIMIT) {
674 HILOG_ERROR("infoSize is too large");
675 return ERR_INVALID_VALUE;
676 }
677 for (int32_t i = 0; i < infoSize; i++) {
678 auto iRemote = reply.ReadRemoteObject();
679 tokens.emplace_back(iRemote);
680 }
681 return reply.ReadInt32();
682 }
683
PreStartNWebSpawnProcess()684 int AppMgrProxy::PreStartNWebSpawnProcess()
685 {
686 HILOG_INFO("PreStartNWebSpawnProcess");
687 MessageParcel data;
688 MessageParcel reply;
689 MessageOption option;
690 if (!WriteInterfaceToken(data)) {
691 HILOG_ERROR("WriteInterfaceToken failed");
692 return ERR_FLATTEN_OBJECT;
693 }
694
695 sptr<IRemoteObject> remote = Remote();
696 if (remote == nullptr) {
697 HILOG_ERROR("Remote() is NULL");
698 return ERR_FLATTEN_OBJECT;
699 }
700 int32_t ret = remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::PRE_START_NWEBSPAWN_PROCESS),
701 data, reply, option);
702 if (ret != NO_ERROR) {
703 HILOG_WARN("PreStartNWebSpawnProcess failed, result: %{public}d", ret);
704 return ret;
705 }
706
707 auto result = reply.ReadInt32();
708 if (result != 0) {
709 HILOG_WARN("PreStartNWebSpawnProcess failed, result: %{public}d", ret);
710 return ret;
711 }
712 return 0;
713 }
714
StartRenderProcess(const std::string & renderParam,int32_t ipcFd,int32_t sharedFd,int32_t crashFd,pid_t & renderPid)715 int AppMgrProxy::StartRenderProcess(const std::string &renderParam,
716 int32_t ipcFd, int32_t sharedFd,
717 int32_t crashFd, pid_t &renderPid)
718 {
719 if (renderParam.empty() || ipcFd <= 0 || sharedFd <= 0 || crashFd <= 0) {
720 HILOG_ERROR("Invalid params, renderParam:%{private}s, ipcFd:%{public}d, "
721 "sharedFd:%{public}d, crashFd:%{public}d",
722 renderParam.c_str(), ipcFd, sharedFd, crashFd);
723 return -1;
724 }
725
726 MessageParcel data;
727 MessageParcel reply;
728 MessageOption option;
729 if (!WriteInterfaceToken(data)) {
730 HILOG_ERROR("WriteInterfaceToken failed");
731 return ERR_FLATTEN_OBJECT;
732 }
733
734 if (!data.WriteString(renderParam)) {
735 HILOG_ERROR("want paramSize failed.");
736 return -1;
737 }
738
739 if (!data.WriteFileDescriptor(ipcFd) || !data.WriteFileDescriptor(sharedFd) ||
740 !data.WriteFileDescriptor(crashFd)) {
741 HILOG_ERROR("want fd failed, ipcFd:%{public}d, sharedFd:%{public}d, "
742 "crashFd:%{public}d",
743 ipcFd, sharedFd, crashFd);
744 return -1;
745 }
746
747 int32_t ret = Remote()->SendRequest(
748 static_cast<uint32_t>(AppMgrInterfaceCode::START_RENDER_PROCESS), data,
749 reply, option);
750 if (ret != NO_ERROR) {
751 HILOG_WARN(
752 "StartRenderProcess SendRequest is failed, error code: %{public}d",
753 ret);
754 return ret;
755 }
756
757 auto result = reply.ReadInt32();
758 renderPid = reply.ReadInt32();
759 if (result != 0) {
760 HILOG_WARN("StartRenderProcess failed, result: %{public}d", result);
761 return result;
762 }
763 return 0;
764 }
765
AttachRenderProcess(const sptr<IRemoteObject> & renderScheduler)766 void AppMgrProxy::AttachRenderProcess(const sptr<IRemoteObject> &renderScheduler)
767 {
768 if (!renderScheduler) {
769 HILOG_ERROR("renderScheduler is null");
770 return;
771 }
772
773 HILOG_DEBUG("AttachRenderProcess start");
774 MessageParcel data;
775 MessageParcel reply;
776 MessageOption option;
777 if (!WriteInterfaceToken(data)) {
778 return;
779 }
780 if (!data.WriteRemoteObject(renderScheduler)) {
781 HILOG_ERROR("renderScheduler write failed.");
782 return;
783 }
784
785 if (!SendTransactCmd(AppMgrInterfaceCode::ATTACH_RENDER_PROCESS, data, reply)) {
786 HILOG_ERROR("SendTransactCmd ATTACH_RENDER_PROCESS failed");
787 return;
788 }
789 }
790
GetRenderProcessTerminationStatus(pid_t renderPid,int & status)791 int AppMgrProxy::GetRenderProcessTerminationStatus(pid_t renderPid, int &status)
792 {
793 MessageParcel data;
794 MessageParcel reply;
795 MessageOption option;
796 if (!WriteInterfaceToken(data)) {
797 HILOG_ERROR("WriteInterfaceToken failed");
798 return ERR_FLATTEN_OBJECT;
799 }
800
801 if (!data.WriteInt32(renderPid)) {
802 HILOG_ERROR("write renderPid failed.");
803 return -1;
804 }
805
806 int32_t ret = Remote()->SendRequest(
807 static_cast<uint32_t>(AppMgrInterfaceCode::GET_RENDER_PROCESS_TERMINATION_STATUS), data, reply, option);
808 if (ret != NO_ERROR) {
809 HILOG_WARN("GetRenderProcessTerminationStatus SendRequest is failed, error code: %{public}d", ret);
810 return ret;
811 }
812
813 auto result = reply.ReadInt32();
814 if (result != 0) {
815 HILOG_WARN("GetRenderProcessTerminationStatus failed, result: %{public}d", result);
816 return result;
817 }
818 status = reply.ReadInt32();
819 return 0;
820 }
821
UpdateConfiguration(const Configuration & config)822 int32_t AppMgrProxy::UpdateConfiguration(const Configuration &config)
823 {
824 HILOG_INFO("AppMgrProxy UpdateConfiguration");
825 MessageParcel data;
826 MessageParcel reply;
827 MessageOption option(MessageOption::TF_SYNC);
828 if (!WriteInterfaceToken(data)) {
829 return ERR_INVALID_DATA;
830 }
831 if (!data.WriteParcelable(&config)) {
832 HILOG_ERROR("parcel config failed");
833 return ERR_INVALID_DATA;
834 }
835 sptr<IRemoteObject> remote = Remote();
836 if (remote == nullptr) {
837 HILOG_ERROR("Remote() is NULL");
838 return ERR_INVALID_DATA;
839 }
840 int32_t ret =
841 remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::UPDATE_CONFIGURATION), data, reply, option);
842 if (ret != NO_ERROR) {
843 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
844 return ret;
845 }
846 return reply.ReadInt32();
847 }
848
GetConfiguration(Configuration & config)849 int32_t AppMgrProxy::GetConfiguration(Configuration &config)
850 {
851 HILOG_INFO("AppMgrProxy GetConfiguration");
852 MessageParcel data;
853 MessageParcel reply;
854 MessageOption option(MessageOption::TF_SYNC);
855 if (!WriteInterfaceToken(data)) {
856 HILOG_ERROR("parcel data failed");
857 return ERR_INVALID_DATA;
858 }
859 sptr<IRemoteObject> remote = Remote();
860 if (remote == nullptr) {
861 HILOG_ERROR("Remote() is NULL");
862 return ERR_INVALID_DATA;
863 }
864 int32_t ret =
865 remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::GET_CONFIGURATION), data, reply, option);
866 if (ret != NO_ERROR) {
867 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
868 return ret;
869 }
870
871 std::unique_ptr<Configuration> info(reply.ReadParcelable<Configuration>());
872 if (!info) {
873 HILOG_ERROR("read configuration failed.");
874 return ERR_UNKNOWN_OBJECT;
875 }
876 config = *info;
877 return reply.ReadInt32();
878 }
879
RegisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)880 int32_t AppMgrProxy::RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
881 {
882 if (!observer) {
883 HILOG_ERROR("observer null");
884 return ERR_INVALID_VALUE;
885 }
886 HILOG_DEBUG("RegisterConfigurationObserver start");
887 MessageParcel data;
888 MessageParcel reply;
889 MessageOption option;
890 if (!WriteInterfaceToken(data)) {
891 return ERR_FLATTEN_OBJECT;
892 }
893
894 if (!data.WriteRemoteObject(observer->AsObject())) {
895 HILOG_ERROR("observer write failed.");
896 return ERR_FLATTEN_OBJECT;
897 }
898
899 auto error = Remote()->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::REGISTER_CONFIGURATION_OBSERVER),
900 data, reply, option);
901 if (error != NO_ERROR) {
902 HILOG_ERROR("Send request error: %{public}d", error);
903 return error;
904 }
905 return reply.ReadInt32();
906 }
907
UnregisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)908 int32_t AppMgrProxy::UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
909 {
910 HILOG_DEBUG("UnregisterConfigurationObserver start");
911 MessageParcel data;
912 MessageParcel reply;
913 MessageOption option;
914 if (!WriteInterfaceToken(data)) {
915 return ERR_FLATTEN_OBJECT;
916 }
917
918 if (!data.WriteRemoteObject(observer->AsObject())) {
919 HILOG_ERROR("observer write failed.");
920 return ERR_FLATTEN_OBJECT;
921 }
922
923 auto error = Remote()->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::UNREGISTER_CONFIGURATION_OBSERVER),
924 data, reply, option);
925 if (error != NO_ERROR) {
926 HILOG_ERROR("Send request error: %{public}d", error);
927 return error;
928 }
929 return reply.ReadInt32();
930 }
931
932 #ifdef ABILITY_COMMAND_FOR_TEST
BlockAppService()933 int AppMgrProxy::BlockAppService()
934 {
935 MessageParcel data;
936 MessageParcel reply;
937 MessageOption option;
938
939 if (!WriteInterfaceToken(data)) {
940 return ERR_FLATTEN_OBJECT;
941 }
942
943 int32_t ret =
944 Remote()->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::BLOCK_APP_SERVICE), data, reply, option);
945 if (ret != NO_ERROR) {
946 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
947 return ret;
948 }
949 return reply.ReadInt32();
950 }
951 #endif
952
GetAppRunningStateByBundleName(const std::string & bundleName)953 bool AppMgrProxy::GetAppRunningStateByBundleName(const std::string &bundleName)
954 {
955 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
956 HILOG_DEBUG("function called.");
957 MessageParcel data;
958 if (!WriteInterfaceToken(data)) {
959 HILOG_ERROR("Write interface token failed.");
960 return false;
961 }
962
963 if (!data.WriteString(bundleName)) {
964 HILOG_ERROR("Write bundle name failed.");
965 return false;
966 }
967
968 sptr<IRemoteObject> remote = Remote();
969 if (remote == nullptr) {
970 HILOG_ERROR("Remote is nullptr.");
971 return false;
972 }
973
974 MessageParcel reply;
975 MessageOption option;
976 auto ret = remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::GET_APP_RUNNING_STATE),
977 data, reply, option);
978 if (ret != 0) {
979 HILOG_WARN("Send request failed with error code %{public}d.", ret);
980 return false;
981 }
982
983 return reply.ReadBool();
984 }
985
NotifyLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)986 int32_t AppMgrProxy::NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
987 {
988 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
989 HILOG_DEBUG("NotifyLoadRepairPatch, function called.");
990 MessageParcel data;
991 if (!WriteInterfaceToken(data)) {
992 HILOG_ERROR("NotifyLoadRepairPatch, Write interface token failed.");
993 return ERR_INVALID_DATA;
994 }
995
996 if (!data.WriteString(bundleName)) {
997 HILOG_ERROR("NotifyLoadRepairPatch, Write bundle name failed.");
998 return ERR_INVALID_DATA;
999 }
1000
1001 if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
1002 HILOG_ERROR("Write callback failed.");
1003 return ERR_INVALID_DATA;
1004 }
1005
1006 sptr<IRemoteObject> remote = Remote();
1007 if (remote == nullptr) {
1008 HILOG_ERROR("NotifyLoadRepairPatch, Remote is nullptr.");
1009 return ERR_NULL_OBJECT;
1010 }
1011
1012 MessageParcel reply;
1013 MessageOption option;
1014 auto ret = remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_LOAD_REPAIR_PATCH),
1015 data, reply, option);
1016 if (ret != 0) {
1017 HILOG_WARN("NotifyLoadRepairPatch, Send request failed with error code %{public}d.", ret);
1018 return ret;
1019 }
1020
1021 return reply.ReadInt32();
1022 }
1023
NotifyHotReloadPage(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)1024 int32_t AppMgrProxy::NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
1025 {
1026 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1027 HILOG_DEBUG("function called.");
1028 MessageParcel data;
1029 if (!WriteInterfaceToken(data)) {
1030 HILOG_ERROR("Write interface token failed.");
1031 return ERR_INVALID_DATA;
1032 }
1033
1034 if (!data.WriteString(bundleName)) {
1035 HILOG_ERROR("Write bundle name failed.");
1036 return ERR_INVALID_DATA;
1037 }
1038
1039 if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
1040 HILOG_ERROR("Write callback failed.");
1041 return ERR_INVALID_DATA;
1042 }
1043
1044 sptr<IRemoteObject> remote = Remote();
1045 if (remote == nullptr) {
1046 HILOG_ERROR("Remote is nullptr.");
1047 return ERR_NULL_OBJECT;
1048 }
1049
1050 MessageParcel reply;
1051 MessageOption option;
1052 auto ret = remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_HOT_RELOAD_PAGE),
1053 data, reply, option);
1054 if (ret != 0) {
1055 HILOG_WARN("Send request failed with error code %{public}d.", ret);
1056 return ret;
1057 }
1058
1059 return reply.ReadInt32();
1060 }
1061
1062 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
SetContinuousTaskProcess(int32_t pid,bool isContinuousTask)1063 int32_t AppMgrProxy::SetContinuousTaskProcess(int32_t pid, bool isContinuousTask)
1064 {
1065 HILOG_DEBUG("SetContinuousTaskProcess start.");
1066 MessageParcel data;
1067 MessageParcel reply;
1068 MessageOption option;
1069
1070 if (!WriteInterfaceToken(data)) {
1071 HILOG_ERROR("Write interface token failed.");
1072 return ERR_INVALID_DATA;
1073 }
1074
1075 if (!data.WriteInt32(pid)) {
1076 HILOG_ERROR("uid write failed.");
1077 return ERR_INVALID_DATA;
1078 }
1079
1080 if (!data.WriteBool(isContinuousTask)) {
1081 HILOG_ERROR("isContinuousTask write failed.");
1082 return ERR_INVALID_DATA;
1083 }
1084
1085 sptr<IRemoteObject> remote = Remote();
1086 if (remote == nullptr) {
1087 HILOG_ERROR("Remote is nullptr.");
1088 return ERR_NULL_OBJECT;
1089 }
1090
1091 auto ret = remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::SET_CONTINUOUSTASK_PROCESS),
1092 data, reply, option);
1093 if (ret != NO_ERROR) {
1094 HILOG_WARN("Send request failed with error code %{public}d.", ret);
1095 return ret;
1096 }
1097
1098 return reply.ReadInt32();
1099 }
1100 #endif
1101
NotifyUnLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)1102 int32_t AppMgrProxy::NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
1103 {
1104 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1105 HILOG_DEBUG("function called.");
1106 MessageParcel data;
1107 if (!WriteInterfaceToken(data)) {
1108 HILOG_ERROR("Notify unload patch, Write interface token failed.");
1109 return ERR_INVALID_DATA;
1110 }
1111
1112 if (!data.WriteString(bundleName)) {
1113 HILOG_ERROR("Notify unload patch, Write bundle name failed.");
1114 return ERR_INVALID_DATA;
1115 }
1116
1117 if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
1118 HILOG_ERROR("Write callback failed.");
1119 return ERR_INVALID_DATA;
1120 }
1121
1122 sptr<IRemoteObject> remote = Remote();
1123 if (remote == nullptr) {
1124 HILOG_ERROR("Notify unload patch, Remote is nullptr.");
1125 return ERR_NULL_OBJECT;
1126 }
1127
1128 MessageParcel reply;
1129 MessageOption option;
1130 auto ret = remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_UNLOAD_REPAIR_PATCH),
1131 data, reply, option);
1132 if (ret != 0) {
1133 HILOG_WARN("Notify unload patch, Send request failed with error code %{public}d.", ret);
1134 return ret;
1135 }
1136
1137 return reply.ReadInt32();
1138 }
1139
IsSharedBundleRunning(const std::string & bundleName,uint32_t versionCode)1140 bool AppMgrProxy::IsSharedBundleRunning(const std::string &bundleName, uint32_t versionCode)
1141 {
1142 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1143 HILOG_DEBUG("function called.");
1144 MessageParcel data;
1145 if (!WriteInterfaceToken(data)) {
1146 HILOG_ERROR("Write interface token failed.");
1147 return false;
1148 }
1149 if (!data.WriteString(bundleName) || !data.WriteUint32(versionCode)) {
1150 HILOG_ERROR("Write bundle name or version code failed.");
1151 return false;
1152 }
1153 sptr<IRemoteObject> remote = Remote();
1154 if (remote == nullptr) {
1155 HILOG_ERROR("Remote is nullptr.");
1156 return false;
1157 }
1158
1159 MessageParcel reply;
1160 MessageOption option;
1161 auto ret = remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::IS_SHARED_BUNDLE_RUNNING),
1162 data, reply, option);
1163 if (ret != NO_ERROR) {
1164 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
1165 return false;
1166 }
1167
1168 return reply.ReadBool();
1169 }
1170
StartNativeProcessForDebugger(const AAFwk::Want & want)1171 int32_t AppMgrProxy::StartNativeProcessForDebugger(const AAFwk::Want &want)
1172 {
1173 MessageParcel data;
1174 if (!WriteInterfaceToken(data)) {
1175 HILOG_ERROR("Write interface token failed.");
1176 return ERR_FLATTEN_OBJECT;
1177 }
1178 if (!data.WriteParcelable(&want)) {
1179 HILOG_ERROR("want write failed.");
1180 return ERR_FLATTEN_OBJECT;
1181 }
1182 sptr<IRemoteObject> remote = Remote();
1183 if (remote == nullptr) {
1184 HILOG_ERROR("Notify unload patch, Remote is nullptr.");
1185 return ERR_NULL_OBJECT;
1186 }
1187
1188 MessageParcel reply;
1189 MessageOption option;
1190 auto ret = remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::START_NATIVE_PROCESS_FOR_DEBUGGER),
1191 data, reply, option);
1192 if (ret != NO_ERROR) {
1193 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
1194 return ret;
1195 }
1196
1197 return reply.ReadInt32();
1198 }
1199
GetBundleNameByPid(const int pid,std::string & bundleName,int32_t & uid)1200 int32_t AppMgrProxy::GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid)
1201 {
1202 MessageParcel data;
1203 MessageParcel reply;
1204 MessageOption option;
1205
1206 if (!WriteInterfaceToken(data)) {
1207 HILOG_ERROR("Write interface token failed.");
1208 return ERR_INVALID_DATA;
1209 }
1210
1211 if (!data.WriteInt32(pid)) {
1212 HILOG_ERROR("pid write failed.");
1213 return ERR_INVALID_DATA;
1214 }
1215
1216 sptr<IRemoteObject> remote = Remote();
1217 if (remote == nullptr) {
1218 HILOG_ERROR("Remote is nullptr.");
1219 return ERR_NULL_OBJECT;
1220 }
1221
1222 auto ret =
1223 remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::GET_BUNDLE_NAME_BY_PID), data, reply, option);
1224 if (ret != NO_ERROR) {
1225 HILOG_WARN("Send request failed with error code %{public}d.", ret);
1226 return ret;
1227 }
1228 bundleName = reply.ReadString();
1229 uid = reply.ReadInt32();
1230 return ERR_NONE;
1231 }
1232
NotifyAppFault(const FaultData & faultData)1233 int32_t AppMgrProxy::NotifyAppFault(const FaultData &faultData)
1234 {
1235 HILOG_DEBUG("called.");
1236 MessageParcel data;
1237
1238 if (!WriteInterfaceToken(data)) {
1239 HILOG_ERROR("Write interface token failed.");
1240 return ERR_FLATTEN_OBJECT;
1241 }
1242
1243 if (!data.WriteParcelable(&faultData)) {
1244 HILOG_ERROR("Write FaultData error.");
1245 return ERR_FLATTEN_OBJECT;
1246 }
1247
1248 sptr<IRemoteObject> remote = Remote();
1249 if (remote == nullptr) {
1250 HILOG_ERROR("Remote is nullptr.");
1251 return ERR_NULL_OBJECT;
1252 }
1253
1254 MessageParcel reply;
1255 MessageOption option;
1256 auto ret = remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_APP_FAULT),
1257 data, reply, option);
1258 if (ret != NO_ERROR) {
1259 HILOG_ERROR("Send request failed with error code %{public}d.", ret);
1260 return ret;
1261 }
1262
1263 return reply.ReadInt32();
1264 }
1265
NotifyAppFaultBySA(const AppFaultDataBySA & faultData)1266 int32_t AppMgrProxy::NotifyAppFaultBySA(const AppFaultDataBySA &faultData)
1267 {
1268 HILOG_DEBUG("called.");
1269 MessageParcel data;
1270
1271 if (!WriteInterfaceToken(data)) {
1272 HILOG_ERROR("Write interface token failed.");
1273 return ERR_FLATTEN_OBJECT;
1274 }
1275
1276 if (!data.WriteParcelable(&faultData)) {
1277 HILOG_ERROR("Write FaultDataBySA error.");
1278 return ERR_FLATTEN_OBJECT;
1279 }
1280
1281 sptr<IRemoteObject> remote = Remote();
1282 if (remote == nullptr) {
1283 HILOG_ERROR("Remote is nullptr.");
1284 return ERR_NULL_OBJECT;
1285 }
1286
1287 MessageParcel reply;
1288 MessageOption option;
1289 auto ret = remote->SendRequest(static_cast<uint32_t>(AppMgrInterfaceCode::NOTIFY_APP_FAULT_BY_SA),
1290 data, reply, option);
1291 if (ret != NO_ERROR) {
1292 HILOG_ERROR("Send request failed with error code %{public}d.", ret);
1293 return ret;
1294 }
1295
1296 return reply.ReadInt32();
1297 }
1298
GetProcessMemoryByPid(const int32_t pid,int32_t & memorySize)1299 int32_t AppMgrProxy::GetProcessMemoryByPid(const int32_t pid, int32_t &memorySize)
1300 {
1301 HILOG_DEBUG("GetProcessMemoryByPid start");
1302 MessageParcel data;
1303 MessageParcel reply;
1304 MessageOption option;
1305 if (!WriteInterfaceToken(data)) {
1306 HILOG_ERROR("Write interface token failed.");
1307 return ERR_FLATTEN_OBJECT;
1308 }
1309
1310 if (!data.WriteInt32(pid)) {
1311 HILOG_ERROR("write pid failed.");
1312 return ERR_INVALID_DATA;
1313 }
1314
1315 sptr<IRemoteObject> remote = Remote();
1316 if (remote == nullptr) {
1317 HILOG_ERROR("Remote is nullptr.");
1318 return ERR_NULL_OBJECT;
1319 }
1320
1321 auto ret = remote->SendRequest(static_cast<uint32_t>(IAppMgr::Message::GET_PROCESS_MEMORY_BY_PID),
1322 data, reply, option);
1323 if (ret != NO_ERROR) {
1324 HILOG_ERROR("Send request failed with error code %{public}d.", ret);
1325 return ret;
1326 }
1327 memorySize = reply.ReadInt32();
1328 auto result = reply.ReadInt32();
1329 return result;
1330 }
1331
GetRunningProcessInformation(const std::string & bundleName,int32_t userId,std::vector<RunningProcessInfo> & info)1332 int32_t AppMgrProxy::GetRunningProcessInformation(
1333 const std::string &bundleName, int32_t userId, std::vector<RunningProcessInfo> &info)
1334 {
1335 HILOG_DEBUG("GetRunningProcessInformation start");
1336 MessageParcel data;
1337 MessageParcel reply;
1338 if (!WriteInterfaceToken(data)) {
1339 HILOG_ERROR("Write interface token failed.");
1340 return ERR_FLATTEN_OBJECT;
1341 }
1342
1343 if (!data.WriteString(bundleName)) {
1344 HILOG_ERROR("write bundleName failed.");
1345 return ERR_INVALID_DATA;
1346 }
1347
1348 if (!data.WriteInt32(userId)) {
1349 HILOG_ERROR("write userId failed.");
1350 return ERR_INVALID_DATA;
1351 }
1352 MessageOption option(MessageOption::TF_SYNC);
1353 sptr<IRemoteObject> remote = Remote();
1354 if (remote == nullptr) {
1355 HILOG_ERROR("Remote is nullptr.");
1356 return ERR_NULL_OBJECT;
1357 }
1358
1359 auto ret = remote->SendRequest(static_cast<uint32_t>(IAppMgr::Message::GET_PIDS_BY_BUNDLENAME),
1360 data, reply, option);
1361 if (ret != NO_ERROR) {
1362 HILOG_ERROR("Send request failed with error code %{public}d.", ret);
1363 return ret;
1364 }
1365
1366 auto error = GetParcelableInfos<RunningProcessInfo>(reply, info);
1367 if (error != NO_ERROR) {
1368 HILOG_ERROR("GetParcelableInfos fail, error: %{public}d", error);
1369 return error;
1370 }
1371 int result = reply.ReadInt32();
1372 return result;
1373 }
1374 } // namespace AppExecFwk
1375 } // namespace OHOS
1376