• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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>(IAppMgr::Message::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>(IAppMgr::Message::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>(IAppMgr::Message::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>(IAppMgr::Message::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>(IAppMgr::Message::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>(IAppMgr::Message::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(IAppMgr::Message::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>(IAppMgr::Message::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(IAppMgr::Message::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 
GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> & info,int32_t userId)244 int32_t AppMgrProxy::GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId)
245 {
246     MessageParcel data;
247     MessageParcel reply;
248     MessageOption option(MessageOption::TF_SYNC);
249 
250     if (!WriteInterfaceToken(data)) {
251         return ERR_FLATTEN_OBJECT;
252     }
253     data.WriteInt32(userId);
254     sptr<IRemoteObject> remote = Remote();
255     if (remote == nullptr) {
256         HILOG_ERROR("Remote() is NULL");
257         return ERR_NULL_OBJECT;
258     }
259     if (!SendTransactCmd(IAppMgr::Message::APP_GET_RUNNING_PROCESSES_BY_USER_ID, data, reply)) {
260         return ERR_NULL_OBJECT;
261     }
262     auto error = GetParcelableInfos<RunningProcessInfo>(reply, info);
263     if (error != NO_ERROR) {
264         HILOG_ERROR("GetParcelableInfos fail, error: %{public}d", error);
265         return error;
266     }
267     int result = reply.ReadInt32();
268     return result;
269 }
270 
GetProcessRunningInformation(RunningProcessInfo & info)271 int32_t AppMgrProxy::GetProcessRunningInformation(RunningProcessInfo &info)
272 {
273     MessageParcel data;
274     MessageParcel reply;
275 
276     if (!WriteInterfaceToken(data)) {
277         return ERR_FLATTEN_OBJECT;
278     }
279     if (!SendTransactCmd(IAppMgr::Message::APP_GET_PROCESS_RUNNING_INFORMATION, data, reply)) {
280         return ERR_NULL_OBJECT;
281     }
282     std::unique_ptr<RunningProcessInfo> infoReply(reply.ReadParcelable<RunningProcessInfo>());
283     info = *infoReply;
284     return reply.ReadInt32();
285 }
286 
NotifyMemoryLevel(int32_t level)287 int32_t AppMgrProxy::NotifyMemoryLevel(int32_t level)
288 {
289     MessageParcel data;
290     MessageParcel reply;
291     MessageOption option(MessageOption::TF_SYNC);
292 
293     if (!WriteInterfaceToken(data)) {
294         return ERR_FLATTEN_OBJECT;
295     }
296     data.WriteInt32(level);
297     sptr<IRemoteObject> remote = Remote();
298     if (remote == nullptr) {
299         HILOG_ERROR("Remote() is NULL");
300         return ERR_NULL_OBJECT;
301     }
302     int32_t ret =
303         remote->SendRequest(
304             static_cast<uint32_t>(IAppMgr::Message::APP_NOTIFY_MEMORY_LEVEL), data, reply, option);
305     if (ret != NO_ERROR) {
306         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
307     }
308     int result = reply.ReadInt32();
309     return result;
310 }
311 
SendTransactCmd(IAppMgr::Message code,MessageParcel & data,MessageParcel & reply)312 bool AppMgrProxy::SendTransactCmd(IAppMgr::Message code, MessageParcel &data, MessageParcel &reply)
313 {
314     MessageOption option(MessageOption::TF_SYNC);
315     sptr<IRemoteObject> remote = Remote();
316     if (!remote) {
317         HILOG_ERROR("fail to send transact cmd %{public}d due to remote object", code);
318         return false;
319     }
320     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
321     if (result != NO_ERROR) {
322         HILOG_ERROR("receive error transact code %{public}d in transact cmd %{public}d", result, code);
323         return false;
324     }
325     return true;
326 }
327 
AddAbilityStageDone(const int32_t recordId)328 void AppMgrProxy::AddAbilityStageDone(const int32_t recordId)
329 {
330     MessageParcel data;
331     MessageParcel reply;
332     if (!WriteInterfaceToken(data)) {
333         HILOG_ERROR("WriteInterfaceToken failed");
334         return;
335     }
336 
337     if (!data.WriteInt32(recordId)) {
338         HILOG_ERROR("want write failed.");
339         return;
340     }
341 
342     if (!SendTransactCmd(IAppMgr::Message::APP_ADD_ABILITY_STAGE_INFO_DONE, data, reply)) {
343         HILOG_ERROR("SendTransactCmd failed");
344         return;
345     }
346     return;
347 }
348 
StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> & bundleInfos)349 void AppMgrProxy::StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos)
350 {
351     MessageParcel data;
352     MessageParcel reply;
353     if (!WriteInterfaceToken(data)) {
354         HILOG_ERROR("WriteInterfaceToken failed");
355         return;
356     }
357 
358     if (!data.WriteInt32(bundleInfos.size())) {
359         HILOG_ERROR("write bundle info size failed.");
360         return;
361     }
362 
363     for (auto &bundleInfo : bundleInfos) {
364         if (!data.WriteParcelable(&bundleInfo)) {
365             HILOG_ERROR("write bundle info failed");
366             return;
367         }
368     }
369 
370     if (!SendTransactCmd(IAppMgr::Message::STARTUP_RESIDENT_PROCESS, data, reply)) {
371         HILOG_ERROR("SendTransactCmd failed");
372         return;
373     }
374     return;
375 }
376 
377 template<typename T>
GetParcelableInfos(MessageParcel & reply,std::vector<T> & parcelableInfos)378 int AppMgrProxy::GetParcelableInfos(MessageParcel &reply, std::vector<T> &parcelableInfos)
379 {
380     int32_t infoSize = reply.ReadInt32();
381     if (infoSize > CYCLE_LIMIT) {
382         HILOG_ERROR("infoSize is too large");
383         return ERR_INVALID_VALUE;
384     }
385     for (int32_t i = 0; i < infoSize; i++) {
386         std::unique_ptr<T> info(reply.ReadParcelable<T>());
387         if (!info) {
388             HILOG_ERROR("Read Parcelable infos failed");
389             return ERR_INVALID_VALUE;
390         }
391         parcelableInfos.emplace_back(*info);
392     }
393     HILOG_DEBUG("get parcelable infos success");
394     return NO_ERROR;
395 }
396 
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer,const std::vector<std::string> & bundleNameList)397 int AppMgrProxy::RegisterApplicationStateObserver(
398     const sptr<IApplicationStateObserver> &observer, const std::vector<std::string> &bundleNameList)
399 {
400     if (!observer) {
401         HILOG_ERROR("observer null");
402         return ERR_INVALID_VALUE;
403     }
404     HILOG_DEBUG("RegisterApplicationStateObserver start");
405     MessageParcel data;
406     MessageParcel reply;
407     MessageOption option;
408     if (!WriteInterfaceToken(data)) {
409         return ERR_FLATTEN_OBJECT;
410     }
411     if (!data.WriteRemoteObject(observer->AsObject())) {
412         HILOG_ERROR("observer write failed.");
413         return ERR_FLATTEN_OBJECT;
414     }
415     if (!data.WriteStringVector(bundleNameList)) {
416         HILOG_ERROR("bundleNameList write failed.");
417         return ERR_FLATTEN_OBJECT;
418     }
419 
420     auto error = Remote()->SendRequest(static_cast<uint32_t>(IAppMgr::Message::REGISTER_APPLICATION_STATE_OBSERVER),
421         data, reply, option);
422     if (error != NO_ERROR) {
423         HILOG_ERROR("Send request error: %{public}d", error);
424         return error;
425     }
426     return reply.ReadInt32();
427 }
428 
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)429 int AppMgrProxy::UnregisterApplicationStateObserver(
430     const sptr<IApplicationStateObserver> &observer)
431 {
432     if (!observer) {
433         HILOG_ERROR("observer null");
434         return ERR_INVALID_VALUE;
435     }
436     HILOG_DEBUG("UnregisterApplicationStateObserver start");
437     MessageParcel data;
438     MessageParcel reply;
439     MessageOption option;
440     if (!WriteInterfaceToken(data)) {
441         return ERR_FLATTEN_OBJECT;
442     }
443     if (!data.WriteRemoteObject(observer->AsObject())) {
444         HILOG_ERROR("observer write failed.");
445         return ERR_FLATTEN_OBJECT;
446     }
447 
448     auto error = Remote()->SendRequest(static_cast<uint32_t>(IAppMgr::Message::UNREGISTER_APPLICATION_STATE_OBSERVER),
449         data, reply, option);
450     if (error != NO_ERROR) {
451         HILOG_ERROR("Send request error: %{public}d", error);
452         return error;
453     }
454     return reply.ReadInt32();
455 }
456 
GetForegroundApplications(std::vector<AppStateData> & list)457 int AppMgrProxy::GetForegroundApplications(std::vector<AppStateData> &list)
458 {
459     MessageParcel data;
460     MessageParcel reply;
461     MessageOption option;
462     if (!WriteInterfaceToken(data)) {
463         return ERR_FLATTEN_OBJECT;
464     }
465     auto error = Remote()->SendRequest(static_cast<uint32_t>(IAppMgr::Message::GET_FOREGROUND_APPLICATIONS),
466         data, reply, option);
467     if (error != NO_ERROR) {
468         HILOG_ERROR("GetForegroundApplications fail, error: %{public}d", error);
469         return error;
470     }
471     int32_t infoSize = reply.ReadInt32();
472     if (infoSize > CYCLE_LIMIT) {
473         HILOG_ERROR("infoSize is too large");
474         return ERR_INVALID_VALUE;
475     }
476     for (int32_t i = 0; i < infoSize; i++) {
477         std::unique_ptr<AppStateData> info(reply.ReadParcelable<AppStateData>());
478         if (!info) {
479             HILOG_ERROR("Read Parcelable infos failed.");
480             return ERR_INVALID_VALUE;
481         }
482         list.emplace_back(*info);
483     }
484     return reply.ReadInt32();
485 }
486 
StartUserTestProcess(const AAFwk::Want & want,const sptr<IRemoteObject> & observer,const BundleInfo & bundleInfo,int32_t userId)487 int AppMgrProxy::StartUserTestProcess(
488     const AAFwk::Want &want, const sptr<IRemoteObject> &observer, const BundleInfo &bundleInfo, int32_t userId)
489 {
490     MessageParcel data;
491     MessageParcel reply;
492     MessageOption option;
493 
494     if (!WriteInterfaceToken(data)) {
495         return ERR_FLATTEN_OBJECT;
496     }
497     if (!data.WriteParcelable(&want)) {
498         HILOG_ERROR("want write failed.");
499         return ERR_FLATTEN_OBJECT;
500     }
501     if (!data.WriteRemoteObject(observer)) {
502         HILOG_ERROR("observer write failed.");
503         return ERR_FLATTEN_OBJECT;
504     }
505     if (!data.WriteParcelable(&bundleInfo)) {
506         HILOG_ERROR("bundleInfo write failed.");
507         return ERR_FLATTEN_OBJECT;
508     }
509     if (!data.WriteInt32(userId)) {
510         HILOG_ERROR("userId write failed.");
511         return ERR_FLATTEN_OBJECT;
512     }
513     int32_t ret =
514         Remote()->SendRequest(static_cast<uint32_t>(IAppMgr::Message::START_USER_TEST_PROCESS), data, reply, option);
515     if (ret != NO_ERROR) {
516         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
517         return ret;
518     }
519     return reply.ReadInt32();
520 }
521 
FinishUserTest(const std::string & msg,const int64_t & resultCode,const std::string & bundleName)522 int AppMgrProxy::FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName)
523 {
524     MessageParcel data;
525     MessageParcel reply;
526     MessageOption option;
527 
528     if (!WriteInterfaceToken(data)) {
529         return ERR_FLATTEN_OBJECT;
530     }
531     if (!data.WriteString(msg)) {
532         HILOG_ERROR("msg write failed.");
533         return ERR_FLATTEN_OBJECT;
534     }
535     if (!data.WriteInt64(resultCode)) {
536         HILOG_ERROR("resultCode:WriteInt32 fail.");
537         return ERR_FLATTEN_OBJECT;
538     }
539     if (!data.WriteString(bundleName)) {
540         HILOG_ERROR("bundleName write failed.");
541         return ERR_FLATTEN_OBJECT;
542     }
543     int32_t ret =
544         Remote()->SendRequest(static_cast<uint32_t>(IAppMgr::Message::FINISH_USER_TEST), data, reply, option);
545     if (ret != NO_ERROR) {
546         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
547         return ret;
548     }
549     return reply.ReadInt32();
550 }
551 
ScheduleAcceptWantDone(const int32_t recordId,const AAFwk::Want & want,const std::string & flag)552 void AppMgrProxy::ScheduleAcceptWantDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag)
553 {
554     MessageParcel data;
555     MessageParcel reply;
556     if (!WriteInterfaceToken(data)) {
557         HILOG_ERROR("WriteInterfaceToken failed");
558         return;
559     }
560 
561     if (!data.WriteInt32(recordId) || !data.WriteParcelable(&want) || !data.WriteString(flag)) {
562         HILOG_ERROR("want write failed.");
563         return;
564     }
565 
566     if (!SendTransactCmd(IAppMgr::Message::SCHEDULE_ACCEPT_WANT_DONE, data, reply)) {
567         HILOG_ERROR("SendTransactCmd failed");
568         return;
569     }
570 }
571 
GetAbilityRecordsByProcessID(const int pid,std::vector<sptr<IRemoteObject>> & tokens)572 int AppMgrProxy::GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens)
573 {
574     MessageParcel data;
575     MessageParcel reply;
576     MessageOption option(MessageOption::TF_SYNC);
577 
578     if (!WriteInterfaceToken(data)) {
579         return ERR_FLATTEN_OBJECT;
580     }
581     data.WriteInt32(pid);
582     if (!SendTransactCmd(IAppMgr::Message::APP_GET_ABILITY_RECORDS_BY_PROCESS_ID, data, reply)) {
583         return ERR_NULL_OBJECT;
584     }
585     int32_t infoSize = reply.ReadInt32();
586     if (infoSize > CYCLE_LIMIT) {
587         HILOG_ERROR("infoSize is too large");
588         return ERR_INVALID_VALUE;
589     }
590     for (int32_t i = 0; i < infoSize; i++) {
591         auto iRemote = reply.ReadRemoteObject();
592         tokens.emplace_back(iRemote);
593     }
594     return reply.ReadInt32();
595 }
596 
PreStartNWebSpawnProcess()597 int AppMgrProxy::PreStartNWebSpawnProcess()
598 {
599     HILOG_INFO("PreStartNWebSpawnProcess");
600     MessageParcel data;
601     MessageParcel reply;
602     MessageOption option;
603     if (!WriteInterfaceToken(data)) {
604         HILOG_ERROR("WriteInterfaceToken failed");
605         return ERR_FLATTEN_OBJECT;
606     }
607 
608     sptr<IRemoteObject> remote = Remote();
609     if (remote == nullptr) {
610         HILOG_ERROR("Remote() is NULL");
611         return ERR_FLATTEN_OBJECT;
612     }
613     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IAppMgr::Message::PRE_START_NWEBSPAWN_PROCESS),
614         data, reply, option);
615     if (ret != NO_ERROR) {
616         HILOG_WARN("PreStartNWebSpawnProcess failed, result: %{public}d", ret);
617         return ret;
618     }
619 
620     auto result = reply.ReadInt32();
621     if (result != 0) {
622         HILOG_WARN("PreStartNWebSpawnProcess failed, result: %{public}d", ret);
623         return ret;
624     }
625     return 0;
626 }
627 
StartRenderProcess(const std::string & renderParam,int32_t ipcFd,int32_t sharedFd,pid_t & renderPid)628 int AppMgrProxy::StartRenderProcess(const std::string &renderParam, int32_t ipcFd,
629     int32_t sharedFd, pid_t &renderPid)
630 {
631     if (renderParam.empty() || ipcFd <= 0 || sharedFd <= 0) {
632         HILOG_ERROR("Invalid params, renderParam:%{private}s, ipcFd:%{public}d, sharedFd:%{public}d",
633             renderParam.c_str(), ipcFd, sharedFd);
634         return -1;
635     }
636 
637     MessageParcel data;
638     MessageParcel reply;
639     MessageOption option;
640     if (!WriteInterfaceToken(data)) {
641         HILOG_ERROR("WriteInterfaceToken failed");
642         return ERR_FLATTEN_OBJECT;
643     }
644 
645     if (!data.WriteString(renderParam)) {
646         HILOG_ERROR("want paramSize failed.");
647         return -1;
648     }
649 
650     if (!data.WriteFileDescriptor(ipcFd) || !data.WriteFileDescriptor(sharedFd)) {
651         HILOG_ERROR("want fd failed, ipcFd:%{public}d, sharedFd:%{public}d", ipcFd, sharedFd);
652         return -1;
653     }
654 
655     int32_t ret =
656         Remote()->SendRequest(static_cast<uint32_t>(IAppMgr::Message::START_RENDER_PROCESS), data, reply, option);
657     if (ret != NO_ERROR) {
658         HILOG_WARN("StartRenderProcess SendRequest is failed, error code: %{public}d", ret);
659         return ret;
660     }
661 
662     auto result = reply.ReadInt32();
663     renderPid = reply.ReadInt32();
664     if (result != 0) {
665         HILOG_WARN("StartRenderProcess failed, result: %{public}d", ret);
666         return ret;
667     }
668     return 0;
669 }
670 
AttachRenderProcess(const sptr<IRemoteObject> & renderScheduler)671 void AppMgrProxy::AttachRenderProcess(const sptr<IRemoteObject> &renderScheduler)
672 {
673     if (!renderScheduler) {
674         HILOG_ERROR("renderScheduler is null");
675         return;
676     }
677 
678     HILOG_DEBUG("AttachRenderProcess start");
679     MessageParcel data;
680     MessageParcel reply;
681     MessageOption option;
682     if (!WriteInterfaceToken(data)) {
683         return;
684     }
685     if (!data.WriteRemoteObject(renderScheduler)) {
686         HILOG_ERROR("renderScheduler write failed.");
687         return;
688     }
689 
690     if (!SendTransactCmd(IAppMgr::Message::ATTACH_RENDER_PROCESS, data, reply)) {
691         HILOG_ERROR("SendTransactCmd ATTACH_RENDER_PROCESS failed");
692         return;
693     }
694 }
695 
GetRenderProcessTerminationStatus(pid_t renderPid,int & status)696 int AppMgrProxy::GetRenderProcessTerminationStatus(pid_t renderPid, int &status)
697 {
698     MessageParcel data;
699     MessageParcel reply;
700     MessageOption option;
701     if (!WriteInterfaceToken(data)) {
702         HILOG_ERROR("WriteInterfaceToken failed");
703         return ERR_FLATTEN_OBJECT;
704     }
705 
706     if (!data.WriteInt32(renderPid)) {
707         HILOG_ERROR("write renderPid failed.");
708         return -1;
709     }
710 
711     int32_t ret = Remote()->SendRequest(
712         static_cast<uint32_t>(IAppMgr::Message::GET_RENDER_PROCESS_TERMINATION_STATUS), data, reply, option);
713     if (ret != NO_ERROR) {
714         HILOG_WARN("GetRenderProcessTerminationStatus SendRequest is failed, error code: %{public}d", ret);
715         return ret;
716     }
717 
718     auto result = reply.ReadInt32();
719     if (result != 0) {
720         HILOG_WARN("GetRenderProcessTerminationStatus failed, result: %{public}d", result);
721         return result;
722     }
723     status = reply.ReadInt32();
724     return 0;
725 }
726 
UpdateConfiguration(const Configuration & config)727 int32_t AppMgrProxy::UpdateConfiguration(const Configuration &config)
728 {
729     HILOG_INFO("AppMgrProxy UpdateConfiguration");
730     MessageParcel data;
731     MessageParcel reply;
732     MessageOption option(MessageOption::TF_SYNC);
733     if (!WriteInterfaceToken(data)) {
734         return ERR_INVALID_DATA;
735     }
736     if (!data.WriteParcelable(&config)) {
737         HILOG_ERROR("parcel config failed");
738         return ERR_INVALID_DATA;
739     }
740     sptr<IRemoteObject> remote = Remote();
741     if (remote == nullptr) {
742         HILOG_ERROR("Remote() is NULL");
743         return ERR_INVALID_DATA;
744     }
745     int32_t ret =
746         remote->SendRequest(static_cast<uint32_t>(IAppMgr::Message::UPDATE_CONFIGURATION), data, reply, option);
747     if (ret != NO_ERROR) {
748         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
749         return ret;
750     }
751     return reply.ReadInt32();
752 }
753 
GetConfiguration(Configuration & config)754 int32_t AppMgrProxy::GetConfiguration(Configuration &config)
755 {
756     HILOG_INFO("AppMgrProxy GetConfiguration");
757     MessageParcel data;
758     MessageParcel reply;
759     MessageOption option(MessageOption::TF_SYNC);
760     if (!WriteInterfaceToken(data)) {
761         HILOG_ERROR("parcel data failed");
762         return ERR_INVALID_DATA;
763     }
764     sptr<IRemoteObject> remote = Remote();
765     if (remote == nullptr) {
766         HILOG_ERROR("Remote() is NULL");
767         return ERR_INVALID_DATA;
768     }
769     int32_t ret =
770         remote->SendRequest(static_cast<uint32_t>(IAppMgr::Message::GET_CONFIGURATION), data, reply, option);
771     if (ret != NO_ERROR) {
772         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
773         return ret;
774     }
775 
776     std::unique_ptr<Configuration> info(reply.ReadParcelable<Configuration>());
777     if (!info) {
778         HILOG_ERROR("read configuration failed.");
779         return ERR_UNKNOWN_OBJECT;
780     }
781     config = *info;
782     return reply.ReadInt32();
783 }
784 
RegisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)785 int32_t AppMgrProxy::RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
786 {
787     if (!observer) {
788         HILOG_ERROR("observer null");
789         return ERR_INVALID_VALUE;
790     }
791     HILOG_DEBUG("RegisterConfigurationObserver start");
792     MessageParcel data;
793     MessageParcel reply;
794     MessageOption option;
795     if (!WriteInterfaceToken(data)) {
796         return ERR_FLATTEN_OBJECT;
797     }
798 
799     if (!data.WriteRemoteObject(observer->AsObject())) {
800         HILOG_ERROR("observer write failed.");
801         return ERR_FLATTEN_OBJECT;
802     }
803 
804     auto error = Remote()->SendRequest(static_cast<uint32_t>(IAppMgr::Message::REGISTER_CONFIGURATION_OBSERVER),
805         data, reply, option);
806     if (error != NO_ERROR) {
807         HILOG_ERROR("Send request error: %{public}d", error);
808         return error;
809     }
810     return reply.ReadInt32();
811 }
812 
UnregisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)813 int32_t AppMgrProxy::UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
814 {
815     HILOG_DEBUG("UnregisterConfigurationObserver start");
816     MessageParcel data;
817     MessageParcel reply;
818     MessageOption option;
819     if (!WriteInterfaceToken(data)) {
820         return ERR_FLATTEN_OBJECT;
821     }
822 
823     if (!data.WriteRemoteObject(observer->AsObject())) {
824         HILOG_ERROR("observer write failed.");
825         return ERR_FLATTEN_OBJECT;
826     }
827 
828     auto error = Remote()->SendRequest(static_cast<uint32_t>(IAppMgr::Message::UNREGISTER_CONFIGURATION_OBSERVER),
829         data, reply, option);
830     if (error != NO_ERROR) {
831         HILOG_ERROR("Send request error: %{public}d", error);
832         return error;
833     }
834     return reply.ReadInt32();
835 }
836 
837 #ifdef ABILITY_COMMAND_FOR_TEST
BlockAppService()838 int AppMgrProxy::BlockAppService()
839 {
840     MessageParcel data;
841     MessageParcel reply;
842     MessageOption option;
843 
844     if (!WriteInterfaceToken(data)) {
845         return ERR_FLATTEN_OBJECT;
846     }
847 
848     int32_t ret =
849         Remote()->SendRequest(static_cast<uint32_t>(IAppMgr::Message::BLOCK_APP_SERVICE), data, reply, option);
850     if (ret != NO_ERROR) {
851         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
852         return ret;
853     }
854     return reply.ReadInt32();
855 }
856 #endif
857 
GetAppRunningStateByBundleName(const std::string & bundleName)858 bool AppMgrProxy::GetAppRunningStateByBundleName(const std::string &bundleName)
859 {
860     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
861     HILOG_DEBUG("function called.");
862     MessageParcel data;
863     if (!WriteInterfaceToken(data)) {
864         HILOG_ERROR("Write interface token failed.");
865         return false;
866     }
867 
868     if (!data.WriteString(bundleName)) {
869         HILOG_ERROR("Write bundle name failed.");
870         return false;
871     }
872 
873     sptr<IRemoteObject> remote = Remote();
874     if (remote == nullptr) {
875         HILOG_ERROR("Remote is nullptr.");
876         return false;
877     }
878 
879     MessageParcel reply;
880     MessageOption option;
881     auto ret = remote->SendRequest(static_cast<uint32_t>(IAppMgr::Message::GET_APP_RUNNING_STATE),
882         data, reply, option);
883     if (ret != 0) {
884         HILOG_WARN("Send request failed with error code %{public}d.", ret);
885         return false;
886     }
887 
888     return reply.ReadBool();
889 }
890 
NotifyLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)891 int32_t AppMgrProxy::NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
892 {
893     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
894     HILOG_DEBUG("NotifyLoadRepairPatch, function called.");
895     MessageParcel data;
896     if (!WriteInterfaceToken(data)) {
897         HILOG_ERROR("NotifyLoadRepairPatch, Write interface token failed.");
898         return ERR_INVALID_DATA;
899     }
900 
901     if (!data.WriteString(bundleName)) {
902         HILOG_ERROR("NotifyLoadRepairPatch, Write bundle name failed.");
903         return ERR_INVALID_DATA;
904     }
905 
906     if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
907         HILOG_ERROR("Write callback failed.");
908         return ERR_INVALID_DATA;
909     }
910 
911     sptr<IRemoteObject> remote = Remote();
912     if (remote == nullptr) {
913         HILOG_ERROR("NotifyLoadRepairPatch, Remote is nullptr.");
914         return ERR_NULL_OBJECT;
915     }
916 
917     MessageParcel reply;
918     MessageOption option;
919     auto ret = remote->SendRequest(static_cast<uint32_t>(IAppMgr::Message::NOTIFY_LOAD_REPAIR_PATCH),
920         data, reply, option);
921     if (ret != 0) {
922         HILOG_WARN("NotifyLoadRepairPatch, Send request failed with error code %{public}d.", ret);
923         return ret;
924     }
925 
926     return reply.ReadInt32();
927 }
928 
NotifyHotReloadPage(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)929 int32_t AppMgrProxy::NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
930 {
931     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
932     HILOG_DEBUG("function called.");
933     MessageParcel data;
934     if (!WriteInterfaceToken(data)) {
935         HILOG_ERROR("Write interface token failed.");
936         return ERR_INVALID_DATA;
937     }
938 
939     if (!data.WriteString(bundleName)) {
940         HILOG_ERROR("Write bundle name failed.");
941         return ERR_INVALID_DATA;
942     }
943 
944     if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
945         HILOG_ERROR("Write callback failed.");
946         return ERR_INVALID_DATA;
947     }
948 
949     sptr<IRemoteObject> remote = Remote();
950     if (remote == nullptr) {
951         HILOG_ERROR("Remote is nullptr.");
952         return ERR_NULL_OBJECT;
953     }
954 
955     MessageParcel reply;
956     MessageOption option;
957     auto ret = remote->SendRequest(static_cast<uint32_t>(IAppMgr::Message::NOTIFY_HOT_RELOAD_PAGE),
958         data, reply, option);
959     if (ret != 0) {
960         HILOG_WARN("Send request failed with error code %{public}d.", ret);
961         return ret;
962     }
963 
964     return reply.ReadInt32();
965 }
966 
967 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
SetContinuousTaskProcess(int32_t pid,bool isContinuousTask)968 int32_t AppMgrProxy::SetContinuousTaskProcess(int32_t pid, bool isContinuousTask)
969 {
970     HILOG_DEBUG("SetContinuousTaskProcess start.");
971     MessageParcel data;
972     MessageParcel reply;
973     MessageOption option;
974 
975     if (!WriteInterfaceToken(data)) {
976         HILOG_ERROR("Write interface token failed.");
977         return ERR_INVALID_DATA;
978     }
979 
980     if (!data.WriteInt32(pid)) {
981         HILOG_ERROR("uid write failed.");
982         return ERR_INVALID_DATA;
983     }
984 
985     if (!data.WriteBool(isContinuousTask)) {
986         HILOG_ERROR("isContinuousTask write failed.");
987         return ERR_INVALID_DATA;
988     }
989 
990     sptr<IRemoteObject> remote = Remote();
991     if (remote == nullptr) {
992         HILOG_ERROR("Remote is nullptr.");
993         return ERR_NULL_OBJECT;
994     }
995 
996     auto ret = remote->SendRequest(static_cast<uint32_t>(IAppMgr::Message::SET_CONTINUOUSTASK_PROCESS),
997         data, reply, option);
998     if (ret != NO_ERROR) {
999         HILOG_WARN("Send request failed with error code %{public}d.", ret);
1000         return ret;
1001     }
1002 
1003     return reply.ReadInt32();
1004 }
1005 #endif
1006 
NotifyUnLoadRepairPatch(const std::string & bundleName,const sptr<IQuickFixCallback> & callback)1007 int32_t AppMgrProxy::NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)
1008 {
1009     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1010     HILOG_DEBUG("function called.");
1011     MessageParcel data;
1012     if (!WriteInterfaceToken(data)) {
1013         HILOG_ERROR("Notify unload patch, Write interface token failed.");
1014         return ERR_INVALID_DATA;
1015     }
1016 
1017     if (!data.WriteString(bundleName)) {
1018         HILOG_ERROR("Notify unload patch, Write bundle name failed.");
1019         return ERR_INVALID_DATA;
1020     }
1021 
1022     if (callback == nullptr || !data.WriteRemoteObject(callback->AsObject())) {
1023         HILOG_ERROR("Write callback failed.");
1024         return ERR_INVALID_DATA;
1025     }
1026 
1027     sptr<IRemoteObject> remote = Remote();
1028     if (remote == nullptr) {
1029         HILOG_ERROR("Notify unload patch, Remote is nullptr.");
1030         return ERR_NULL_OBJECT;
1031     }
1032 
1033     MessageParcel reply;
1034     MessageOption option;
1035     auto ret = remote->SendRequest(static_cast<uint32_t>(IAppMgr::Message::NOTIFY_UNLOAD_REPAIR_PATCH),
1036         data, reply, option);
1037     if (ret != 0) {
1038         HILOG_WARN("Notify unload patch, Send request failed with error code %{public}d.", ret);
1039         return ret;
1040     }
1041 
1042     return reply.ReadInt32();
1043 }
1044 }  // namespace AppExecFwk
1045 }  // namespace OHOS
1046