• 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 "ams_mgr_proxy.h"
17 #include "ipc_types.h"
18 #include "iremote_object.h"
19 #include "string_ex.h"
20 
21 #include "appexecfwk_errors.h"
22 #include "hilog_wrapper.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
AmsMgrProxy(const sptr<IRemoteObject> & impl)26 AmsMgrProxy::AmsMgrProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IAmsMgr>(impl)
27 {}
28 
WriteInterfaceToken(MessageParcel & data)29 bool AmsMgrProxy::WriteInterfaceToken(MessageParcel &data)
30 {
31     if (!data.WriteInterfaceToken(AmsMgrProxy::GetDescriptor())) {
32         HILOG_ERROR("write interface token failed");
33         return false;
34     }
35     return true;
36 }
37 
LoadAbility(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & preToken,const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<ApplicationInfo> & appInfo,const std::shared_ptr<AAFwk::Want> & want)38 void AmsMgrProxy::LoadAbility(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
39     const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo,
40     const std::shared_ptr<AAFwk::Want> &want)
41 {
42     HILOG_DEBUG("start");
43     if (!abilityInfo || !appInfo) {
44         HILOG_ERROR("param error");
45         return;
46     }
47 
48     MessageParcel data;
49     MessageParcel reply;
50     MessageOption option(MessageOption::TF_SYNC);
51     if (!WriteInterfaceToken(data)) {
52         return;
53     }
54 
55     if (token) {
56         if (!data.WriteBool(true) || !data.WriteRemoteObject(token)) {
57             HILOG_ERROR("Failed to write flag and token");
58             return;
59         }
60     } else {
61         if (!data.WriteBool(false)) {
62             HILOG_ERROR("Failed to write flag");
63             return;
64         }
65     }
66 
67     if (preToken) {
68         if (!data.WriteBool(true) || !data.WriteRemoteObject(preToken)) {
69             HILOG_ERROR("Failed to write flag and preToken");
70             return;
71         }
72     } else {
73         if (!data.WriteBool(false)) {
74             HILOG_ERROR("Failed to write flag");
75             return;
76         }
77     }
78 
79     data.WriteParcelable(abilityInfo.get());
80     data.WriteParcelable(appInfo.get());
81     if (!data.WriteParcelable(want.get())) {
82         HILOG_ERROR("Write data want failed.");
83         return;
84     }
85     sptr<IRemoteObject> remote = Remote();
86     if (remote == nullptr) {
87         HILOG_ERROR("Remote() is NULL");
88         return;
89     }
90     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::LOAD_ABILITY), data, reply, option);
91     if (ret != NO_ERROR) {
92         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
93     }
94     HILOG_DEBUG("end");
95 }
96 
TerminateAbility(const sptr<IRemoteObject> & token,bool clearMissionFlag)97 void AmsMgrProxy::TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag)
98 {
99     HILOG_DEBUG("start");
100     MessageParcel data;
101     MessageParcel reply;
102     MessageOption option(MessageOption::TF_SYNC);
103     if (!WriteInterfaceToken(data)) {
104         return;
105     }
106     if (!data.WriteRemoteObject(token.GetRefPtr())) {
107         HILOG_ERROR("Failed to write token");
108         return;
109     }
110     if (!data.WriteBool(clearMissionFlag)) {
111         HILOG_ERROR("Failed to write clearMissionFlag");
112         return;
113     }
114     sptr<IRemoteObject> remote = Remote();
115     if (remote == nullptr) {
116         HILOG_ERROR("Remote() is NULL");
117         return;
118     }
119     int32_t ret =
120         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::TERMINATE_ABILITY), data, reply, option);
121     if (ret != NO_ERROR) {
122         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
123     }
124     HILOG_DEBUG("end");
125 }
126 
UpdateAbilityState(const sptr<IRemoteObject> & token,const AbilityState state)127 void AmsMgrProxy::UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state)
128 {
129     HILOG_DEBUG("start");
130     MessageParcel data;
131     MessageParcel reply;
132     MessageOption option(MessageOption::TF_SYNC);
133     if (!WriteInterfaceToken(data)) {
134         return;
135     }
136     if (!data.WriteRemoteObject(token.GetRefPtr())) {
137         HILOG_ERROR("Failed to write token");
138         return;
139     }
140     data.WriteInt32(static_cast<int32_t>(state));
141     sptr<IRemoteObject> remote = Remote();
142     if (remote == nullptr) {
143         HILOG_ERROR("Remote() is NULL");
144         return;
145     }
146     int32_t ret =
147         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::UPDATE_ABILITY_STATE), data, reply, option);
148     if (ret != NO_ERROR) {
149         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
150     }
151     HILOG_DEBUG("end");
152 }
153 
UpdateExtensionState(const sptr<IRemoteObject> & token,const ExtensionState state)154 void AmsMgrProxy::UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state)
155 {
156     HILOG_DEBUG("UpdateExtensionState start");
157     MessageParcel data;
158     MessageParcel reply;
159     MessageOption option(MessageOption::TF_SYNC);
160     if (!WriteInterfaceToken(data)) {
161         return;
162     }
163     if (!data.WriteRemoteObject(token.GetRefPtr())) {
164         HILOG_ERROR("Failed to write token");
165         return;
166     }
167     data.WriteInt32(static_cast<int32_t>(state));
168     sptr<IRemoteObject> remote = Remote();
169     if (remote == nullptr) {
170         HILOG_ERROR("Remote() is NULL");
171         return;
172     }
173     int32_t ret =
174         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::UPDATE_EXTENSION_STATE), data, reply, option);
175     if (ret != NO_ERROR) {
176         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
177     }
178     HILOG_DEBUG("end");
179 }
180 
RegisterAppStateCallback(const sptr<IAppStateCallback> & callback)181 void AmsMgrProxy::RegisterAppStateCallback(const sptr<IAppStateCallback> &callback)
182 {
183     HILOG_DEBUG("start");
184     if (!callback) {
185         HILOG_ERROR("callback is nullptr");
186         return;
187     }
188 
189     MessageParcel data;
190     MessageParcel reply;
191     MessageOption option(MessageOption::TF_SYNC);
192     if (!WriteInterfaceToken(data)) {
193         return;
194     }
195 
196     if (callback->AsObject()) {
197         if (!data.WriteBool(true) || !data.WriteRemoteObject(callback->AsObject())) {
198             HILOG_ERROR("Failed to write flag and callback");
199             return;
200         }
201     } else {
202         if (!data.WriteBool(false)) {
203             HILOG_ERROR("Failed to write flag");
204             return;
205         }
206     }
207 
208     sptr<IRemoteObject> remote = Remote();
209     if (remote == nullptr) {
210         HILOG_ERROR("Remote() is nullptr");
211         return;
212     }
213     int32_t ret = remote->SendRequest(
214         static_cast<uint32_t>(IAmsMgr::Message::REGISTER_APP_STATE_CALLBACK), data, reply, option);
215     if (ret != NO_ERROR) {
216         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
217     }
218     HILOG_DEBUG("end");
219 }
220 
AbilityBehaviorAnalysis(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & preToken,const int32_t visibility,const int32_t perceptibility,const int32_t connectionState)221 void AmsMgrProxy::AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
222     const int32_t visibility, const int32_t perceptibility, const int32_t connectionState)
223 {
224     HILOG_DEBUG("start");
225     MessageParcel data;
226     MessageParcel reply;
227     MessageOption option(MessageOption::TF_SYNC);
228     if (!WriteInterfaceToken(data)) {
229         return;
230     }
231 
232     if (!data.WriteRemoteObject(token.GetRefPtr())) {
233         HILOG_ERROR("Failed to write token");
234         return;
235     }
236 
237     if (preToken) {
238         if (!data.WriteBool(true) || !data.WriteRemoteObject(preToken.GetRefPtr())) {
239             HILOG_ERROR("Failed to write flag and preToken");
240             return;
241         }
242     } else {
243         if (!data.WriteBool(false)) {
244             HILOG_ERROR("Failed to write flag");
245             return;
246         }
247     }
248 
249     data.WriteInt32(static_cast<int32_t>(visibility));
250     data.WriteInt32(static_cast<int32_t>(perceptibility));
251     data.WriteInt32(static_cast<int32_t>(connectionState));
252     sptr<IRemoteObject> remote = Remote();
253     if (remote == nullptr) {
254         HILOG_ERROR("Remote() is NULL");
255         return;
256     }
257     int32_t ret =
258         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::ABILITY_BEHAVIOR_ANALYSIS), data, reply, option);
259     if (ret != NO_ERROR) {
260         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
261     }
262     HILOG_DEBUG("end");
263 }
264 
KillProcessByAbilityToken(const sptr<IRemoteObject> & token)265 void AmsMgrProxy::KillProcessByAbilityToken(const sptr<IRemoteObject> &token)
266 {
267     HILOG_DEBUG("start");
268     MessageParcel data;
269     MessageParcel reply;
270     MessageOption option(MessageOption::TF_SYNC);
271     if (!WriteInterfaceToken(data)) {
272         return;
273     }
274     if (!data.WriteRemoteObject(token.GetRefPtr())) {
275         HILOG_ERROR("Failed to write token");
276         return;
277     }
278     sptr<IRemoteObject> remote = Remote();
279     if (remote == nullptr) {
280         HILOG_ERROR("Remote() is NULL");
281         return;
282     }
283     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::KILL_PEOCESS_BY_ABILITY_TOKEN),
284         data, reply, option);
285     if (ret != NO_ERROR) {
286         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
287     }
288     HILOG_DEBUG("end");
289 }
290 
KillProcessesByUserId(int32_t userId)291 void AmsMgrProxy::KillProcessesByUserId(int32_t userId)
292 {
293     HILOG_DEBUG("start");
294     MessageParcel data;
295     MessageParcel reply;
296     MessageOption option(MessageOption::TF_SYNC);
297     if (!WriteInterfaceToken(data)) {
298         return;
299     }
300     if (!data.WriteInt32(userId)) {
301         HILOG_ERROR("parcel WriteInt32 failed");
302         return;
303     }
304     sptr<IRemoteObject> remote = Remote();
305     if (remote == nullptr) {
306         HILOG_ERROR("Remote() is NULL");
307         return;
308     }
309     int32_t ret =
310         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::KILL_PROCESSES_BY_USERID), data, reply, option);
311     if (ret != NO_ERROR) {
312         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
313     }
314     HILOG_DEBUG("end");
315 }
316 
KillProcessWithAccount(const std::string & bundleName,const int accountId)317 int32_t AmsMgrProxy::KillProcessWithAccount(const std::string &bundleName, const int accountId)
318 {
319     HILOG_DEBUG("start");
320 
321     MessageParcel data;
322     MessageParcel reply;
323     MessageOption option(MessageOption::TF_SYNC);
324     if (!WriteInterfaceToken(data)) {
325         return ERR_INVALID_DATA;
326     }
327 
328     sptr<IRemoteObject> remote = Remote();
329     if (remote == nullptr) {
330         HILOG_ERROR("Remote() is NULL");
331         return ERR_NULL_OBJECT;
332     }
333 
334     if (!data.WriteString(bundleName)) {
335         HILOG_ERROR("parcel WriteString failed");
336         return ERR_FLATTEN_OBJECT;
337     }
338 
339     if (!data.WriteInt32(accountId)) {
340         HILOG_ERROR("parcel WriteInt32 failed");
341         return ERR_FLATTEN_OBJECT;
342     }
343 
344     int32_t ret =
345         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::KILL_PROCESS_WITH_ACCOUNT), data, reply, option);
346     if (ret != NO_ERROR) {
347         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
348         return ret;
349     }
350 
351     HILOG_DEBUG("end");
352 
353     return reply.ReadInt32();
354 }
355 
KillApplication(const std::string & bundleName)356 int32_t AmsMgrProxy::KillApplication(const std::string &bundleName)
357 {
358     HILOG_DEBUG("start");
359     MessageParcel data;
360     MessageParcel reply;
361     MessageOption option(MessageOption::TF_SYNC);
362     if (!WriteInterfaceToken(data)) {
363         return ERR_INVALID_DATA;
364     }
365     sptr<IRemoteObject> remote = Remote();
366     if (remote == nullptr) {
367         HILOG_ERROR("Remote() is NULL");
368         return ERR_NULL_OBJECT;
369     }
370     if (!data.WriteString(bundleName)) {
371         HILOG_ERROR("parcel WriteString failed");
372         return ERR_FLATTEN_OBJECT;
373     }
374     int32_t ret =
375         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::KILL_APPLICATION), data, reply, option);
376     if (ret != NO_ERROR) {
377         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
378         return ret;
379     }
380     return reply.ReadInt32();
381 }
382 
UpdateApplicationInfoInstalled(const std::string & bundleName,const int uid)383 int32_t AmsMgrProxy::UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid)
384 {
385     HILOG_DEBUG("start");
386     MessageParcel data;
387     MessageParcel reply;
388     MessageOption option(MessageOption::TF_SYNC);
389     if (!WriteInterfaceToken(data)) {
390         return ERR_INVALID_DATA;
391     }
392     sptr<IRemoteObject> remote = Remote();
393     if (remote == nullptr) {
394         HILOG_ERROR("Remote() is NULL");
395         return ERR_NULL_OBJECT;
396     }
397     if (!data.WriteString(bundleName)) {
398         HILOG_ERROR("parcel WriteString failed");
399         return ERR_FLATTEN_OBJECT;
400     }
401     if (!data.WriteInt32(uid)) {
402         HILOG_ERROR("uid write failed.");
403         return ERR_FLATTEN_OBJECT;
404     }
405     int32_t ret =
406         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::UPDATE_APPLICATION_INFO_INSTALLED),
407         data, reply, option);
408     if (ret != NO_ERROR) {
409         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
410         return ret;
411     }
412     return reply.ReadInt32();
413 }
414 
KillApplicationByUid(const std::string & bundleName,const int uid)415 int32_t AmsMgrProxy::KillApplicationByUid(const std::string &bundleName, const int uid)
416 {
417     HILOG_DEBUG("start");
418     MessageParcel data;
419     MessageParcel reply;
420     MessageOption option(MessageOption::TF_SYNC);
421     if (!WriteInterfaceToken(data)) {
422         return ERR_INVALID_DATA;
423     }
424     sptr<IRemoteObject> remote = Remote();
425     if (remote == nullptr) {
426         HILOG_ERROR("Remote() is NULL");
427         return ERR_NULL_OBJECT;
428     }
429     if (!data.WriteString(bundleName)) {
430         HILOG_ERROR("parcel WriteString failed");
431         return ERR_FLATTEN_OBJECT;
432     }
433     if (!data.WriteInt32(uid)) {
434         HILOG_ERROR("uid write failed.");
435         return ERR_FLATTEN_OBJECT;
436     }
437     int32_t ret =
438         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::KILL_APPLICATION_BYUID), data, reply, option);
439     if (ret != NO_ERROR) {
440         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
441         return ret;
442     }
443     return reply.ReadInt32();
444 }
445 
KillApplicationSelf()446 int32_t AmsMgrProxy::KillApplicationSelf()
447 {
448     MessageParcel data;
449     MessageParcel reply;
450     MessageOption option(MessageOption::TF_SYNC);
451     if (!WriteInterfaceToken(data)) {
452         return ERR_INVALID_DATA;
453     }
454     sptr<IRemoteObject> remote = Remote();
455     if (remote == nullptr) {
456         HILOG_ERROR("Remote is NULL");
457         return ERR_NULL_OBJECT;
458     }
459     int32_t ret =
460         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::KILL_APPLICATION_SELF), data, reply, option);
461     if (ret != NO_ERROR) {
462         HILOG_ERROR("SendRequest is failed, error code: %{public}d", ret);
463         return ret;
464     }
465     return reply.ReadInt32();
466 }
467 
AbilityAttachTimeOut(const sptr<IRemoteObject> & token)468 void AmsMgrProxy::AbilityAttachTimeOut(const sptr<IRemoteObject> &token)
469 {
470     HILOG_DEBUG("start");
471     MessageParcel data;
472     MessageParcel reply;
473     MessageOption option(MessageOption::TF_SYNC);
474     if (!WriteInterfaceToken(data)) {
475         return;
476     }
477     if (!data.WriteRemoteObject(token.GetRefPtr())) {
478         HILOG_ERROR("Failed to write token");
479         return;
480     }
481     sptr<IRemoteObject> remote = Remote();
482     if (remote == nullptr) {
483         HILOG_ERROR("Remote() is NULL");
484         return;
485     }
486     int32_t ret =
487         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::ABILITY_ATTACH_TIMEOUT), data, reply, option);
488     if (ret != NO_ERROR) {
489         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
490     }
491     HILOG_DEBUG("end");
492 }
493 
PrepareTerminate(const sptr<IRemoteObject> & token)494 void AmsMgrProxy::PrepareTerminate(const sptr<IRemoteObject> &token)
495 {
496     HILOG_DEBUG("start");
497     MessageParcel data;
498     MessageParcel reply;
499     MessageOption option(MessageOption::TF_SYNC);
500     if (!WriteInterfaceToken(data)) {
501         return;
502     }
503     if (!data.WriteRemoteObject(token.GetRefPtr())) {
504         HILOG_ERROR("Failed to write token");
505         return;
506     }
507     sptr<IRemoteObject> remote = Remote();
508     if (remote == nullptr) {
509         HILOG_ERROR("Remote() is NULL");
510         return;
511     }
512     int32_t ret =
513         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::PREPARE_TERMINATE_ABILITY),
514             data, reply, option);
515     if (ret != NO_ERROR) {
516         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
517     }
518     HILOG_DEBUG("end");
519 }
520 
GetRunningProcessInfoByToken(const sptr<IRemoteObject> & token,AppExecFwk::RunningProcessInfo & info)521 void AmsMgrProxy::GetRunningProcessInfoByToken(
522     const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info)
523 {
524     MessageParcel data;
525     MessageParcel reply;
526     MessageOption option(MessageOption::TF_SYNC);
527     if (!WriteInterfaceToken(data)) {
528         return;
529     }
530 
531     if (!data.WriteRemoteObject(token.GetRefPtr())) {
532         return;
533     }
534 
535     auto remote = Remote();
536     if (remote == nullptr) {
537         HILOG_ERROR("Remote() is NULL");
538         return;
539     }
540     auto ret = remote->SendRequest(
541         static_cast<uint32_t>(IAmsMgr::Message::GET_RUNNING_PROCESS_INFO_BY_TOKEN), data, reply, option);
542     if (ret != NO_ERROR) {
543         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
544         return;
545     }
546 
547     std::unique_ptr<AppExecFwk::RunningProcessInfo> processInfo(reply.ReadParcelable<AppExecFwk::RunningProcessInfo>());
548     if (processInfo == nullptr) {
549         HILOG_ERROR("recv process info faild");
550         return;
551     }
552 
553     info = *processInfo;
554 }
555 
GetRunningProcessInfoByPid(const pid_t pid,OHOS::AppExecFwk::RunningProcessInfo & info)556 void AmsMgrProxy::GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info)
557 {
558     MessageParcel data;
559     MessageParcel reply;
560     MessageOption option(MessageOption::TF_SYNC);
561     if (!WriteInterfaceToken(data)) {
562         return;
563     }
564 
565     if (!data.WriteInt32(static_cast<int32_t>(pid))) {
566         HILOG_ERROR("parcel WriteInt32 failed");
567         return;
568     }
569 
570     auto remote = Remote();
571     if (remote == nullptr) {
572         HILOG_ERROR("Remote() is NULL");
573         return;
574     }
575     auto ret = remote->SendRequest(
576         static_cast<uint32_t>(IAmsMgr::Message::GET_RUNNING_PROCESS_INFO_BY_PID), data, reply, option);
577     if (ret != NO_ERROR) {
578         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
579         return;
580     }
581 
582     std::unique_ptr<AppExecFwk::RunningProcessInfo> processInfo(reply.ReadParcelable<AppExecFwk::RunningProcessInfo>());
583     if (processInfo == nullptr) {
584         HILOG_ERROR("recv process info faild");
585         return;
586     }
587 
588     info = *processInfo;
589 }
590 
SetAbilityForegroundingFlagToAppRecord(const pid_t pid)591 void AmsMgrProxy::SetAbilityForegroundingFlagToAppRecord(const pid_t pid)
592 {
593     MessageParcel data;
594     MessageParcel reply;
595     MessageOption option(MessageOption::TF_SYNC);
596     if (!WriteInterfaceToken(data)) {
597         return;
598     }
599 
600     if (!data.WriteInt32(static_cast<int32_t>(pid))) {
601         HILOG_ERROR("parcel WriteInt32 failed");
602         return;
603     }
604 
605     auto remote = Remote();
606     if (remote == nullptr) {
607         HILOG_ERROR("Remote() is NULL");
608         return;
609     }
610     auto ret = remote->SendRequest(
611         static_cast<uint32_t>(IAmsMgr::Message::SET_ABILITY_FOREGROUNDING_FLAG), data, reply, option);
612     if (ret != NO_ERROR) {
613         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
614     }
615 }
616 
StartSpecifiedAbility(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo)617 void AmsMgrProxy::StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo)
618 
619 {
620     MessageParcel data;
621     MessageParcel reply;
622     MessageOption option(MessageOption::TF_SYNC);
623     if (!WriteInterfaceToken(data)) {
624         return;
625     }
626 
627     if (!data.WriteParcelable(&want) || !data.WriteParcelable(&abilityInfo)) {
628         HILOG_ERROR("Write data failed.");
629         return;
630     }
631 
632     sptr<IRemoteObject> remote = Remote();
633     if (remote == nullptr) {
634         HILOG_ERROR("Remote is nullptr.");
635         return;
636     }
637     auto ret = remote->SendRequest(
638         static_cast<uint32_t>(IAmsMgr::Message::START_SPECIFIED_ABILITY), data, reply, option);
639     if (ret != NO_ERROR) {
640         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
641     }
642 }
643 
RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> & response)644 void AmsMgrProxy::RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response)
645 {
646     HILOG_DEBUG("Register multi instances response by proxy.");
647     if (!response) {
648         HILOG_ERROR("response is nullptr");
649         return;
650     }
651 
652     MessageParcel data;
653     MessageParcel reply;
654     MessageOption option(MessageOption::TF_SYNC);
655     if (!WriteInterfaceToken(data)) {
656         return;
657     }
658     if (!data.WriteRemoteObject(response->AsObject())) {
659         HILOG_ERROR("Failed to write remote object.");
660         return;
661     }
662 
663     sptr<IRemoteObject> remote = Remote();
664     if (remote == nullptr) {
665         HILOG_ERROR("Remote is nullptr.");
666         return;
667     }
668     int32_t ret = remote->SendRequest(
669         static_cast<uint32_t>(IAmsMgr::Message::REGISTER_START_SPECIFIED_ABILITY_RESPONSE), data, reply, option);
670     if (ret != NO_ERROR) {
671         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
672     }
673 }
674 
GetApplicationInfoByProcessID(const int pid,AppExecFwk::ApplicationInfo & application,bool & debug)675 int AmsMgrProxy::GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug)
676 {
677     MessageParcel data;
678     MessageParcel reply;
679     MessageOption option(MessageOption::TF_SYNC);
680     if (!WriteInterfaceToken(data)) {
681         HILOG_ERROR("token write error.");
682         return ERR_FLATTEN_OBJECT;
683     }
684     sptr<IRemoteObject> remote = Remote();
685     if (remote == nullptr) {
686         HILOG_ERROR("Remote is nullptr.");
687         return ERR_NULL_OBJECT;
688     }
689     data.WriteInt32(pid);
690     int32_t ret = remote->SendRequest(
691         static_cast<uint32_t>(IAmsMgr::Message::GET_APPLICATION_INFO_BY_PROCESS_ID), data, reply, option);
692     if (ret != NO_ERROR) {
693         HILOG_ERROR("send request fail.");
694         return ret;
695     }
696     auto result = reply.ReadInt32();
697     if (result != NO_ERROR) {
698         HILOG_ERROR("reply result false");
699         return result;
700     }
701     std::unique_ptr<AppExecFwk::ApplicationInfo> info(reply.ReadParcelable<AppExecFwk::ApplicationInfo>());
702     if (!info) {
703         HILOG_ERROR("readParcelableInfo failed");
704         return ERR_NAME_NOT_FOUND;
705     }
706     application = *info;
707     debug = reply.ReadBool();
708     HILOG_DEBUG("get parcelable info success");
709     return NO_ERROR;
710 }
711 
SetCurrentUserId(const int32_t userId)712 void AmsMgrProxy::SetCurrentUserId(const int32_t userId)
713 {
714     HILOG_DEBUG("start");
715     MessageParcel data;
716     MessageParcel reply;
717     MessageOption option(MessageOption::TF_SYNC);
718     if (!WriteInterfaceToken(data)) {
719         return;
720     }
721     if (!data.WriteInt32(userId)) {
722         HILOG_ERROR("Failed to write userId");
723         return;
724     }
725     sptr<IRemoteObject> remote = Remote();
726     if (remote == nullptr) {
727         HILOG_ERROR("Remote() is NULL");
728         return;
729     }
730     int32_t ret =
731         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::SET_CURRENT_USER_ID),
732             data, reply, option);
733     if (ret != NO_ERROR) {
734         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
735     }
736     HILOG_DEBUG("end");
737 }
738 
GetBundleNameByPid(const int pid,std::string & bundleName,int32_t & uid)739 int32_t AmsMgrProxy::GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid)
740 {
741     MessageParcel data;
742     MessageParcel reply;
743     MessageOption option(MessageOption::TF_SYNC);
744     if (!WriteInterfaceToken(data)) {
745         return ERR_INVALID_DATA;
746     }
747     if (!data.WriteInt32(pid)) {
748         HILOG_ERROR("Failed to write pid");
749         return ERR_INVALID_DATA;
750     }
751     sptr<IRemoteObject> remote = Remote();
752     if (remote == nullptr) {
753         HILOG_ERROR("Remote() is NULL");
754         return ERR_NULL_OBJECT;
755     }
756     int32_t ret =
757         remote->SendRequest(static_cast<uint32_t>(IAmsMgr::Message::Get_BUNDLE_NAME_BY_PID),
758             data, reply, option);
759     if (ret != NO_ERROR) {
760         HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
761     }
762     bundleName = reply.ReadString();
763     uid = reply.ReadInt32();
764     return NO_ERROR;
765 }
766 }  // namespace AppExecFwk
767 }  // namespace OHOS
768