• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 
18 #include "ability_manager_errors.h"
19 #include "appexecfwk_errors.h"
20 #include "freeze_util.h"
21 #include "hilog_tag_wrapper.h"
22 #include "ipc_types.h"
23 #include "iremote_object.h"
24 #include "param.h"
25 #include "parcel_util.h"
26 #include "string_ex.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 constexpr int32_t MAX_APP_DEBUG_COUNT = 100;
32 }
AmsMgrProxy(const sptr<IRemoteObject> & impl)33 AmsMgrProxy::AmsMgrProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IAmsMgr>(impl)
34 {}
35 
IsProcessContainsOnlyUIAbility(const pid_t pid)36 bool AmsMgrProxy::IsProcessContainsOnlyUIAbility(const pid_t pid)
37 {
38     TAG_LOGD(AAFwkTag::APPMGR, "IsProcessContainsOnlyUIAbility start");
39     MessageParcel data;
40     MessageParcel reply;
41     MessageOption option(MessageOption::TF_SYNC);
42     if (!WriteInterfaceToken(data)) {
43         return false;
44     }
45     if (!data.WriteInt32(static_cast<int32_t>(pid))) {
46         TAG_LOGE(AAFwkTag::APPMGR, "write pid failed");
47         return false;
48     }
49     int32_t ret =
50         SendTransactCmd(static_cast<uint32_t>(
51             IAmsMgr::Message::IS_PROCESS_CONTAINS_ONLY_UI_EXTENSION), data, reply, option);
52     if (ret != NO_ERROR) {
53         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
54         return false;
55     }
56     TAG_LOGD(AAFwkTag::APPMGR, "IsProcessContainsOnlyUIAbility end");
57     return reply.ReadBool();
58 }
59 
WriteInterfaceToken(MessageParcel & data)60 bool AmsMgrProxy::WriteInterfaceToken(MessageParcel &data)
61 {
62     if (!data.WriteInterfaceToken(AmsMgrProxy::GetDescriptor())) {
63         TAG_LOGE(AAFwkTag::APPMGR, "write token failed");
64         return false;
65     }
66     return true;
67 }
68 
LoadAbility(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<ApplicationInfo> & appInfo,const std::shared_ptr<AAFwk::Want> & want,std::shared_ptr<AbilityRuntime::LoadParam> loadParam)69 void AmsMgrProxy::LoadAbility(const std::shared_ptr<AbilityInfo> &abilityInfo,
70     const std::shared_ptr<ApplicationInfo> &appInfo,
71     const std::shared_ptr<AAFwk::Want> &want, std::shared_ptr<AbilityRuntime::LoadParam> loadParam)
72 {
73     TAG_LOGD(AAFwkTag::APPMGR, "start");
74     if (!abilityInfo || !appInfo) {
75         TAG_LOGE(AAFwkTag::APPMGR, "param error");
76         return;
77     }
78 
79     MessageParcel data;
80     MessageParcel reply;
81     MessageOption option(MessageOption::TF_SYNC);
82     if (!WriteInterfaceToken(data)) {
83         return;
84     }
85     if (!data.WriteParcelable(abilityInfo.get())) {
86         TAG_LOGE(AAFwkTag::APPMGR, "Write abilityInfo failed");
87         return;
88     }
89     if (!data.WriteParcelable(appInfo.get())) {
90         TAG_LOGE(AAFwkTag::APPMGR, "Write abilityInfo failed");
91         return;
92     }
93     if (!data.WriteParcelable(want.get())) {
94         TAG_LOGE(AAFwkTag::APPMGR, "Write want failed");
95         return;
96     }
97     if (!data.WriteParcelable(loadParam.get())) {
98         TAG_LOGE(AAFwkTag::APPMGR, "Write data loadParam failed");
99         return;
100     }
101 
102     int32_t ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::LOAD_ABILITY), data, reply, option);
103     if (ret != NO_ERROR) {
104         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
105         AbilityRuntime::FreezeUtil::GetInstance().AddLifecycleEvent(loadParam->token,
106             "AmsMgrProxy::LoadAbility fail, ipc error " + std::to_string(ret));
107     }
108     TAG_LOGD(AAFwkTag::APPMGR, "end");
109 }
110 
TerminateAbility(const sptr<IRemoteObject> & token,bool clearMissionFlag)111 void AmsMgrProxy::TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag)
112 {
113     TAG_LOGD(AAFwkTag::APPMGR, "start");
114     MessageParcel data;
115     MessageParcel reply;
116     MessageOption option(MessageOption::TF_SYNC);
117     if (!WriteInterfaceToken(data)) {
118         return;
119     }
120     if (!data.WriteRemoteObject(token.GetRefPtr())) {
121         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
122         return;
123     }
124     if (!data.WriteBool(clearMissionFlag)) {
125         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write clearMissionFlag");
126         return;
127     }
128     int32_t ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::TERMINATE_ABILITY), data, reply, option);
129     if (ret != NO_ERROR) {
130         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
131     }
132     TAG_LOGD(AAFwkTag::APPMGR, "end");
133 }
134 
UpdateAbilityState(const sptr<IRemoteObject> & token,const AbilityState state)135 void AmsMgrProxy::UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state)
136 {
137     TAG_LOGD(AAFwkTag::APPMGR, "start");
138     MessageParcel data;
139     MessageParcel reply;
140     MessageOption option(MessageOption::TF_SYNC);
141     if (!WriteInterfaceToken(data)) {
142         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write data");
143         return;
144     }
145     if (!data.WriteRemoteObject(token.GetRefPtr())) {
146         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
147         return;
148     }
149     if (!data.WriteInt32(static_cast<int32_t>(state))) {
150         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
151         return;
152     }
153     int32_t ret =
154         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::UPDATE_ABILITY_STATE), data, reply, option);
155     if (ret != NO_ERROR) {
156         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
157     }
158     TAG_LOGD(AAFwkTag::APPMGR, "end");
159 }
160 
UpdateExtensionState(const sptr<IRemoteObject> & token,const ExtensionState state)161 void AmsMgrProxy::UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state)
162 {
163     TAG_LOGD(AAFwkTag::APPMGR, "UpdateExtensionState begin");
164     MessageParcel data;
165     MessageParcel reply;
166     MessageOption option(MessageOption::TF_SYNC);
167     if (!WriteInterfaceToken(data)) {
168         return;
169     }
170     if (!data.WriteRemoteObject(token.GetRefPtr())) {
171         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
172         return;
173     }
174     if (!data.WriteInt32(static_cast<int32_t>(state))) {
175         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write state");
176         return;
177     }
178     int32_t ret =
179         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::UPDATE_EXTENSION_STATE), data, reply, option);
180     if (ret != NO_ERROR) {
181         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
182     }
183     TAG_LOGD(AAFwkTag::APPMGR, "end");
184 }
185 
RegisterAppStateCallback(const sptr<IAppStateCallback> & callback)186 void AmsMgrProxy::RegisterAppStateCallback(const sptr<IAppStateCallback> &callback)
187 {
188     TAG_LOGD(AAFwkTag::APPMGR, "begin");
189     if (!callback) {
190         TAG_LOGE(AAFwkTag::APPMGR, "callback null");
191         return;
192     }
193 
194     MessageParcel data;
195     MessageParcel reply;
196     MessageOption option(MessageOption::TF_SYNC);
197     if (!WriteInterfaceToken(data)) {
198         return;
199     }
200 
201     if (callback->AsObject()) {
202         if (!data.WriteBool(true) || !data.WriteRemoteObject(callback->AsObject())) {
203             TAG_LOGE(AAFwkTag::APPMGR, "Failed to write flag and callback");
204             return;
205         }
206     } else {
207         if (!data.WriteBool(false)) {
208             TAG_LOGE(AAFwkTag::APPMGR, "Failed to write flag");
209             return;
210         }
211     }
212 
213     int32_t ret = SendTransactCmd(
214         static_cast<uint32_t>(IAmsMgr::Message::REGISTER_APP_STATE_CALLBACK), data, reply, option);
215     if (ret != NO_ERROR) {
216         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
217     }
218     TAG_LOGD(AAFwkTag::APPMGR, "end");
219 }
220 
KillProcessByAbilityToken(const sptr<IRemoteObject> & token)221 void AmsMgrProxy::KillProcessByAbilityToken(const sptr<IRemoteObject> &token)
222 {
223     TAG_LOGI(AAFwkTag::APPMGR, "start");
224     MessageParcel data;
225     MessageParcel reply;
226     MessageOption option(MessageOption::TF_SYNC);
227     if (!WriteInterfaceToken(data)) {
228         return;
229     }
230     if (!data.WriteRemoteObject(token.GetRefPtr())) {
231         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
232         return;
233     }
234     int32_t ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::KILL_PEOCESS_BY_ABILITY_TOKEN),
235         data, reply, option);
236     if (ret != NO_ERROR) {
237         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
238     }
239     TAG_LOGD(AAFwkTag::APPMGR, "end");
240 }
241 
KillProcessesByUserId(int32_t userId,bool isNeedSendAppSpawnMsg,sptr<AAFwk::IUserCallback> callback)242 void AmsMgrProxy::KillProcessesByUserId(int32_t userId, bool isNeedSendAppSpawnMsg,
243     sptr<AAFwk::IUserCallback> callback)
244 {
245     TAG_LOGI(AAFwkTag::APPMGR, "start");
246     MessageParcel data;
247     MessageParcel reply;
248     MessageOption option(MessageOption::TF_SYNC);
249     if (!WriteInterfaceToken(data)) {
250         return;
251     }
252     if (!data.WriteInt32(userId)) {
253         TAG_LOGE(AAFwkTag::APPMGR, "parcel WriteInt32 failed");
254         return;
255     }
256     if (!data.WriteBool(isNeedSendAppSpawnMsg)) {
257         TAG_LOGE(AAFwkTag::APPMGR, "parcel WriteBool failed");
258         return;
259     }
260     if (callback == nullptr) {
261         TAG_LOGD(AAFwkTag::APPMGR, "callback is nullptr");
262         data.WriteBool(false);
263     } else {
264         data.WriteBool(true);
265         if (!data.WriteRemoteObject(callback->AsObject())) {
266             TAG_LOGE(AAFwkTag::ABILITYMGR, "write IUserCallback fail");
267             return;
268         }
269     }
270     int32_t ret =
271         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::KILL_PROCESSES_BY_USERID), data, reply, option);
272     if (ret != NO_ERROR) {
273         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
274     }
275     TAG_LOGD(AAFwkTag::APPMGR, "ending");
276 }
277 
KillProcessesByPids(const std::vector<int32_t> & pids,const std::string & reason,bool subProcess,bool isKillPrecedeStart)278 int32_t AmsMgrProxy::KillProcessesByPids(const std::vector<int32_t> &pids, const std::string &reason,
279     bool subProcess, bool isKillPrecedeStart)
280 {
281     TAG_LOGI(AAFwkTag::APPMGR, "start");
282     MessageParcel data;
283     MessageParcel reply;
284     MessageOption option;
285 
286     if (!WriteInterfaceToken(data)) {
287         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
288         return ERR_INVALID_DATA;
289     }
290     if (!data.WriteUint32(pids.size())) {
291         TAG_LOGE(AAFwkTag::APPMGR, "Write size failed");
292         return ERR_FLATTEN_OBJECT;
293     }
294     for (const auto &pid: pids) {
295         if (!data.WriteInt32(pid)) {
296             TAG_LOGE(AAFwkTag::APPMGR, "Write pid failed");
297             return ERR_FLATTEN_OBJECT;
298         }
299     }
300     if (!data.WriteString(reason)) {
301         TAG_LOGE(AAFwkTag::APPMGR, "Write reason failed");
302         return ERR_FLATTEN_OBJECT;
303     }
304     if (!data.WriteBool(subProcess)) {
305         TAG_LOGE(AAFwkTag::APPMGR, "Write subProcess failed");
306         return ERR_FLATTEN_OBJECT;
307     }
308     if (!data.WriteBool(isKillPrecedeStart)) {
309         TAG_LOGE(AAFwkTag::APPMGR, "Write isKillPrecedeStart failed");
310         return ERR_FLATTEN_OBJECT;
311     }
312     int32_t ret =
313         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::KILL_PROCESSES_BY_PIDS), data, reply, option);
314     if (ret != NO_ERROR) {
315         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
316         return ret;
317     }
318     TAG_LOGD(AAFwkTag::APPMGR, "end");
319     return reply.ReadInt32();
320 }
321 
AttachPidToParent(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & callerToken)322 void AmsMgrProxy::AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken)
323 {
324     TAG_LOGD(AAFwkTag::APPMGR, "start");
325     MessageParcel data;
326     MessageParcel reply;
327     MessageOption option;
328     if (!WriteInterfaceToken(data)) {
329         return;
330     }
331     if (!data.WriteRemoteObject(token)) {
332         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
333         return;
334     }
335     if (!data.WriteRemoteObject(callerToken)) {
336         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write callerToken");
337         return;
338     }
339     int32_t ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::ATTACH_PID_TO_PARENT),
340         data, reply, option);
341     if (ret != NO_ERROR) {
342         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
343     }
344     TAG_LOGD(AAFwkTag::APPMGR, "end");
345 }
346 
KillProcessWithAccount(const std::string & bundleName,const int accountId,const bool clearPageStack,int32_t appIndex)347 int32_t AmsMgrProxy::KillProcessWithAccount(
348     const std::string &bundleName, const int accountId, const bool clearPageStack, int32_t appIndex)
349 {
350     TAG_LOGI(AAFwkTag::APPMGR, "start");
351 
352     MessageParcel data;
353     MessageParcel reply;
354     MessageOption option(MessageOption::TF_SYNC);
355     if (!WriteInterfaceToken(data)) {
356         return ERR_INVALID_DATA;
357     }
358 
359     if (!data.WriteString(bundleName)) {
360         TAG_LOGE(AAFwkTag::APPMGR, "WriteString failed");
361         return ERR_FLATTEN_OBJECT;
362     }
363 
364     if (!data.WriteInt32(accountId)) {
365         TAG_LOGE(AAFwkTag::APPMGR, "parcel WriteInt32 failed");
366         return ERR_FLATTEN_OBJECT;
367     }
368 
369     if (!data.WriteBool(clearPageStack)) {
370         TAG_LOGE(AAFwkTag::APPMGR, "parcel bool failed");
371         return ERR_FLATTEN_OBJECT;
372     }
373 
374     if (!data.WriteInt32(appIndex)) {
375         TAG_LOGE(AAFwkTag::APPMGR, "parcel appIndex failed");
376         return ERR_FLATTEN_OBJECT;
377     }
378 
379     int32_t ret =
380         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::KILL_PROCESS_WITH_ACCOUNT), data, reply, option);
381     if (ret != NO_ERROR) {
382         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
383         return ret;
384     }
385 
386     TAG_LOGD(AAFwkTag::APPMGR, "end");
387 
388     return reply.ReadInt32();
389 }
390 
KillProcessesInBatch(const std::vector<int32_t> & pids)391 int32_t AmsMgrProxy::KillProcessesInBatch(const std::vector<int32_t> &pids)
392 {
393     MessageParcel data;
394     MessageParcel reply;
395     MessageOption option(MessageOption::TF_SYNC);
396     if (!WriteInterfaceToken(data)) {
397         return ERR_INVALID_DATA;
398     }
399 
400     if (!data.WriteUint32(pids.size())) {
401         TAG_LOGE(AAFwkTag::APPMGR, "Write size failed");
402         return ERR_FLATTEN_OBJECT;
403     }
404     for (const auto &pid: pids) {
405         if (!data.WriteInt32(pid)) {
406             TAG_LOGE(AAFwkTag::APPMGR, "Write pid failed");
407             return ERR_FLATTEN_OBJECT;
408         }
409     }
410 
411     int32_t ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::KILL_PROCESSES_IN_BATCH),
412         data, reply, option);
413     if (ret != NO_ERROR) {
414         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
415         return ret;
416     }
417 
418     return reply.ReadInt32();
419 }
420 
KillApplication(const std::string & bundleName,bool clearPageStack,int32_t appIndex)421 int32_t AmsMgrProxy::KillApplication(const std::string &bundleName, bool clearPageStack, int32_t appIndex)
422 {
423     TAG_LOGI(AAFwkTag::APPMGR, "start");
424     MessageParcel data;
425     MessageParcel reply;
426     MessageOption option(MessageOption::TF_SYNC);
427     if (!WriteInterfaceToken(data)) {
428         return ERR_INVALID_DATA;
429     }
430 
431     if (!data.WriteString(bundleName)) {
432         TAG_LOGE(AAFwkTag::APPMGR, "WriteString failed");
433         return ERR_FLATTEN_OBJECT;
434     }
435 
436     if (!data.WriteBool(clearPageStack)) {
437         TAG_LOGE(AAFwkTag::APPMGR, "parcel bool failed");
438         return ERR_FLATTEN_OBJECT;
439     }
440 
441     if (!data.WriteInt32(appIndex)) {
442         TAG_LOGE(AAFwkTag::APPMGR, "parcel appIndex failed");
443         return ERR_FLATTEN_OBJECT;
444     }
445 
446     int32_t ret =
447         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::KILL_APPLICATION), data, reply, option);
448     if (ret != NO_ERROR) {
449         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d.", ret);
450         return ret;
451     }
452     return reply.ReadInt32();
453 }
454 
ForceKillApplication(const std::string & bundleName,const int userId,const int appIndex)455 int32_t AmsMgrProxy::ForceKillApplication(const std::string &bundleName,
456     const int userId, const int appIndex)
457 {
458     TAG_LOGI(AAFwkTag::APPMGR, "start");
459     MessageParcel data;
460     MessageParcel reply;
461     MessageOption option(MessageOption::TF_SYNC);
462     if (!WriteInterfaceToken(data)) {
463         return ERR_INVALID_DATA;
464     }
465 
466     if (!data.WriteString(bundleName)) {
467         TAG_LOGE(AAFwkTag::APPMGR, "parcel bundleName failed.");
468         return ERR_FLATTEN_OBJECT;
469     }
470 
471     if (!data.WriteInt32(userId)) {
472         TAG_LOGE(AAFwkTag::APPMGR, "parcel userId failed");
473         return ERR_FLATTEN_OBJECT;
474     }
475 
476     if (!data.WriteInt32(appIndex)) {
477         TAG_LOGE(AAFwkTag::APPMGR, "parcel appIndex failed");
478         return ERR_FLATTEN_OBJECT;
479     }
480 
481     int32_t ret =
482         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::FORCE_KILL_APPLICATION), data, reply, option);
483     if (ret != NO_ERROR) {
484         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d.", ret);
485         return ret;
486     }
487     return reply.ReadInt32();
488 }
489 
KillProcessesByAccessTokenId(const uint32_t accessTokenId)490 int32_t AmsMgrProxy::KillProcessesByAccessTokenId(const uint32_t accessTokenId)
491 {
492     TAG_LOGI(AAFwkTag::APPMGR, "start");
493     MessageParcel data;
494     MessageParcel reply;
495     MessageOption option(MessageOption::TF_SYNC);
496     if (!WriteInterfaceToken(data)) {
497         return ERR_INVALID_DATA;
498     }
499 
500     if (!data.WriteInt32(accessTokenId)) {
501         TAG_LOGE(AAFwkTag::APPMGR, "parcel accessTokenId failed");
502         return ERR_FLATTEN_OBJECT;
503     }
504 
505     int32_t ret =
506         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::FORCE_KILL_APPLICATION_BY_ACCESS_TOKEN_ID),
507             data, reply, option);
508     if (ret != NO_ERROR) {
509         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d.", ret);
510         return ret;
511     }
512     return reply.ReadInt32();
513 }
514 
UpdateApplicationInfoInstalled(const std::string & bundleName,const int uid,const std::string & moduleName,bool isPlugin)515 int32_t AmsMgrProxy::UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid,
516     const std::string &moduleName, bool isPlugin)
517 {
518     TAG_LOGD(AAFwkTag::APPMGR, "start.");
519     MessageParcel data;
520     MessageParcel reply;
521     MessageOption option(MessageOption::TF_SYNC);
522     if (!WriteInterfaceToken(data)) {
523         return ERR_INVALID_DATA;
524     }
525     if (!data.WriteString(bundleName)) {
526         TAG_LOGE(AAFwkTag::APPMGR, "WriteString failed");
527         return ERR_FLATTEN_OBJECT;
528     }
529     if (!data.WriteInt32(uid)) {
530         TAG_LOGE(AAFwkTag::APPMGR, "uid write failed.");
531         return ERR_FLATTEN_OBJECT;
532     }
533     if (!data.WriteString(moduleName)) {
534         TAG_LOGE(AAFwkTag::APPMGR, "moduleName WriteString failed");
535         return ERR_FLATTEN_OBJECT;
536     }
537     if (!data.WriteBool(isPlugin)) {
538         TAG_LOGE(AAFwkTag::APPMGR, "isPlugin Write failed");
539         return ERR_FLATTEN_OBJECT;
540     }
541     int32_t ret =
542         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::UPDATE_APPLICATION_INFO_INSTALLED),
543         data, reply, option);
544     if (ret != NO_ERROR) {
545         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
546         return ret;
547     }
548     return reply.ReadInt32();
549 }
550 
KillApplicationByUid(const std::string & bundleName,const int uid,const std::string & reason)551 int32_t AmsMgrProxy::KillApplicationByUid(const std::string &bundleName, const int uid,
552     const std::string& reason)
553 {
554     TAG_LOGI(AAFwkTag::APPMGR, "start");
555     MessageParcel data;
556     MessageParcel reply;
557     MessageOption option(MessageOption::TF_SYNC);
558     if (!WriteInterfaceToken(data)) {
559         return ERR_INVALID_DATA;
560     }
561     if (!data.WriteString(bundleName)) {
562         TAG_LOGE(AAFwkTag::APPMGR, "failed to write bundle name");
563         return ERR_FLATTEN_OBJECT;
564     }
565     if (!data.WriteInt32(uid)) {
566         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write uid");
567         return ERR_FLATTEN_OBJECT;
568     }
569     if (!data.WriteString(reason)) {
570         TAG_LOGE(AAFwkTag::APPMGR, "failedto write reason");
571         return ERR_FLATTEN_OBJECT;
572     }
573     int32_t ret =
574         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::KILL_APPLICATION_BYUID), data, reply, option);
575     if (ret != NO_ERROR) {
576         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
577         return ret;
578     }
579     return reply.ReadInt32();
580 }
581 
NotifyUninstallOrUpgradeApp(const std::string & bundleName,int32_t uid,bool isUpgrade)582 int32_t AmsMgrProxy::NotifyUninstallOrUpgradeApp(const std::string &bundleName, int32_t uid, bool isUpgrade)
583 {
584     TAG_LOGI(AAFwkTag::APPMGR, "start");
585     MessageParcel data;
586     MessageParcel reply;
587     MessageOption option(MessageOption::TF_SYNC);
588     if (!WriteInterfaceToken(data)) {
589         return ERR_INVALID_DATA;
590     }
591     if (!data.WriteString(bundleName)) {
592         TAG_LOGE(AAFwkTag::APPMGR, "failed to write bundle name");
593         return ERR_FLATTEN_OBJECT;
594     }
595     if (!data.WriteInt32(uid)) {
596         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write uid");
597         return ERR_FLATTEN_OBJECT;
598     }
599     if (!data.WriteBool(isUpgrade)) {
600         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write isUpgradeFlag");
601         return ERR_FLATTEN_OBJECT;
602     }
603     int32_t ret =
604         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::NOTIFY_UNINSTALL_OR_UPGRADE_APP), data, reply, option);
605     if (ret != NO_ERROR) {
606         TAG_LOGE(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
607         return ret;
608     }
609     return reply.ReadInt32();
610 }
611 
NotifyUninstallOrUpgradeAppEnd(int32_t uid)612 void AmsMgrProxy::NotifyUninstallOrUpgradeAppEnd(int32_t uid)
613 {
614     TAG_LOGI(AAFwkTag::APPMGR, "start");
615     MessageParcel data;
616     MessageParcel reply;
617     MessageOption option(MessageOption::TF_SYNC);
618     if (!WriteInterfaceToken(data)) {
619         return;
620     }
621     if (!data.WriteInt32(uid)) {
622         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write uid");
623         return;
624     }
625     int32_t ret = SendTransactCmd(
626         static_cast<uint32_t>(IAmsMgr::Message::NOTIFY_UNINSTALL_OR_UPGRADE_APP_END), data, reply, option);
627     if (ret != NO_ERROR) {
628         TAG_LOGE(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
629     }
630     TAG_LOGD(AAFwkTag::APPMGR, "end");
631 }
632 
633 
KillApplicationSelf(const bool clearPageStack,const std::string & reason)634 int32_t AmsMgrProxy::KillApplicationSelf(const bool clearPageStack, const std::string& reason)
635 {
636     TAG_LOGI(AAFwkTag::APPMGR, "call");
637     MessageParcel data;
638     MessageParcel reply;
639     MessageOption option(MessageOption::TF_SYNC);
640     if (!WriteInterfaceToken(data)) {
641         return ERR_INVALID_DATA;
642     }
643 
644     if (!data.WriteBool(clearPageStack)) {
645         TAG_LOGE(AAFwkTag::APPMGR, "parcel bool failed");
646         return ERR_FLATTEN_OBJECT;
647     }
648 
649     if (!data.WriteString(reason)) {
650         TAG_LOGE(AAFwkTag::APPMGR, "failed to write reason");
651         return ERR_FLATTEN_OBJECT;
652     }
653 
654     int32_t ret =
655         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::KILL_APPLICATION_SELF), data, reply, option);
656     if (ret != NO_ERROR) {
657         TAG_LOGE(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
658         return ret;
659     }
660     return reply.ReadInt32();
661 }
662 
AbilityAttachTimeOut(const sptr<IRemoteObject> & token)663 void AmsMgrProxy::AbilityAttachTimeOut(const sptr<IRemoteObject> &token)
664 {
665     TAG_LOGD(AAFwkTag::APPMGR, "beginning");
666     MessageParcel data;
667     MessageParcel reply;
668     MessageOption option(MessageOption::TF_SYNC);
669     if (!WriteInterfaceToken(data)) {
670         return;
671     }
672     if (!data.WriteRemoteObject(token.GetRefPtr())) {
673         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
674         return;
675     }
676     int32_t ret =
677         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::ABILITY_ATTACH_TIMEOUT), data, reply, option);
678     if (ret != NO_ERROR) {
679         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
680     }
681     TAG_LOGD(AAFwkTag::APPMGR, "end");
682 }
683 
PrepareTerminate(const sptr<IRemoteObject> & token,bool clearMissionFlag)684 void AmsMgrProxy::PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag)
685 {
686     TAG_LOGD(AAFwkTag::APPMGR, "start");
687     MessageParcel data;
688     MessageParcel reply;
689     MessageOption option(MessageOption::TF_SYNC);
690     if (!WriteInterfaceToken(data)) {
691         return;
692     }
693     if (!data.WriteRemoteObject(token.GetRefPtr())) {
694         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
695         return;
696     }
697     if (!data.WriteBool(clearMissionFlag)) {
698         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write clearMissionFlag");
699         return;
700     }
701     int32_t ret =
702         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::PREPARE_TERMINATE_ABILITY),
703             data, reply, option);
704     if (ret != NO_ERROR) {
705         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
706     }
707     TAG_LOGD(AAFwkTag::APPMGR, "end");
708 }
709 
GetRunningProcessInfoByToken(const sptr<IRemoteObject> & token,AppExecFwk::RunningProcessInfo & info)710 void AmsMgrProxy::GetRunningProcessInfoByToken(
711     const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info)
712 {
713     MessageParcel data;
714     MessageParcel reply;
715     MessageOption option(MessageOption::TF_SYNC);
716     if (!WriteInterfaceToken(data)) {
717         return;
718     }
719 
720     if (!data.WriteRemoteObject(token.GetRefPtr())) {
721         return;
722     }
723 
724     auto ret = SendTransactCmd(
725         static_cast<uint32_t>(IAmsMgr::Message::GET_RUNNING_PROCESS_INFO_BY_TOKEN), data, reply, option);
726     if (ret != NO_ERROR) {
727         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
728         return;
729     }
730 
731     std::unique_ptr<AppExecFwk::RunningProcessInfo> processInfo(reply.ReadParcelable<AppExecFwk::RunningProcessInfo>());
732     if (processInfo == nullptr) {
733         TAG_LOGE(AAFwkTag::APPMGR, "recv process info faild");
734         return;
735     }
736 
737     info = *processInfo;
738 }
739 
SetAbilityForegroundingFlagToAppRecord(const pid_t pid)740 void AmsMgrProxy::SetAbilityForegroundingFlagToAppRecord(const pid_t pid)
741 {
742     TAG_LOGD(AAFwkTag::APPMGR, "calling");
743     MessageParcel data;
744     MessageParcel reply;
745     MessageOption option(MessageOption::TF_SYNC);
746     if (!WriteInterfaceToken(data)) {
747         return;
748     }
749 
750     if (!data.WriteInt32(static_cast<int32_t>(pid))) {
751         TAG_LOGE(AAFwkTag::APPMGR, "parcel WriteInt32 failed");
752         return;
753     }
754 
755     auto ret = SendTransactCmd(
756         static_cast<uint32_t>(IAmsMgr::Message::SET_ABILITY_FOREGROUNDING_FLAG), data, reply, option);
757     if (ret != NO_ERROR) {
758         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
759     }
760 }
761 
PrepareTerminateApp(const pid_t pid,const std::string & moduleName)762 void AmsMgrProxy::PrepareTerminateApp(const pid_t pid, const std::string &moduleName)
763 {
764     MessageParcel data;
765     MessageParcel reply;
766     MessageOption option(MessageOption::TF_SYNC);
767     if (!WriteInterfaceToken(data)) {
768         TAG_LOGE(AAFwkTag::APPMGR, "token write error");
769         return;
770     }
771     if (!data.WriteInt32(pid)) {
772         TAG_LOGE(AAFwkTag::APPMGR, "Write PrepareTerminateApp pid failed.");
773         return;
774     }
775     if (!data.WriteString(moduleName)) {
776         TAG_LOGE(AAFwkTag::APPMGR, "Write PrepareTerminateApp moduleName failed.");
777         return;
778     }
779     auto ret = SendTransactCmd(
780         static_cast<uint32_t>(IAmsMgr::Message::PREPARE_TERMINATE_APP), data, reply, option);
781     if (ret != NO_ERROR) {
782         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest PrepareTerminateApp err: %{public}d", ret);
783         return;
784     }
785     TAG_LOGD(AAFwkTag::APPMGR, "Get PrepareTerminateApp reply success");
786 }
787 
StartSpecifiedAbility(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId)788 void AmsMgrProxy::StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
789     int32_t requestId)
790 {
791     MessageParcel data;
792     MessageParcel reply;
793     MessageOption option(MessageOption::TF_SYNC);
794     if (!WriteInterfaceToken(data)) {
795         return;
796     }
797 
798     if (!data.WriteParcelable(&want) || !data.WriteParcelable(&abilityInfo) ||
799         !data.WriteInt32(requestId)) {
800         TAG_LOGE(AAFwkTag::APPMGR, "Write data failed.");
801         return;
802     }
803 
804     auto ret = SendTransactCmd(
805         static_cast<uint32_t>(IAmsMgr::Message::START_SPECIFIED_ABILITY), data, reply, option);
806     if (ret != NO_ERROR) {
807         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
808     }
809 }
810 
StartSpecifiedProcess(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId,const std::string & customProcess)811 void AmsMgrProxy::StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
812     int32_t requestId, const std::string &customProcess)
813 
814 {
815     MessageParcel data;
816     MessageParcel reply;
817     MessageOption option(MessageOption::TF_SYNC);
818     if (!WriteInterfaceToken(data)) {
819         TAG_LOGE(AAFwkTag::APPMGR, "Write data failed.");
820         return;
821     }
822 
823     if (!data.WriteParcelable(&want) || !data.WriteParcelable(&abilityInfo) ||
824         data.WriteInt32(requestId)) {
825         TAG_LOGE(AAFwkTag::APPMGR, "Write data failed.");
826         return;
827     }
828 
829     sptr<IRemoteObject> remote = Remote();
830     if (remote == nullptr) {
831         TAG_LOGE(AAFwkTag::APPMGR, "Remote is nullptr.");
832         return;
833     }
834     auto ret = remote->SendRequest(
835         static_cast<uint32_t>(IAmsMgr::Message::START_SPECIFIED_PROCESS), data, reply, option);
836     if (ret != NO_ERROR) {
837         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
838     }
839 }
840 
RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> & response)841 void AmsMgrProxy::RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response)
842 {
843     TAG_LOGD(AAFwkTag::APPMGR, "Register multi instances response by proxy.");
844     if (!response) {
845         TAG_LOGE(AAFwkTag::APPMGR, "response null");
846         return;
847     }
848 
849     MessageParcel data;
850     MessageParcel reply;
851     MessageOption option(MessageOption::TF_SYNC);
852     if (!WriteInterfaceToken(data)) {
853         return;
854     }
855     if (!data.WriteRemoteObject(response->AsObject())) {
856         TAG_LOGE(AAFwkTag::APPMGR, "write remote obj failed");
857         return;
858     }
859 
860     int32_t ret = SendTransactCmd(
861         static_cast<uint32_t>(IAmsMgr::Message::REGISTER_START_SPECIFIED_ABILITY_RESPONSE), data, reply, option);
862     if (ret != NO_ERROR) {
863         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
864     }
865 }
866 
GetApplicationInfoByProcessID(const int pid,AppExecFwk::ApplicationInfo & application,bool & debug)867 int AmsMgrProxy::GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug)
868 {
869     MessageParcel data;
870     MessageParcel reply;
871     MessageOption option(MessageOption::TF_SYNC);
872     if (!WriteInterfaceToken(data)) {
873         TAG_LOGE(AAFwkTag::APPMGR, "token write error");
874         return ERR_FLATTEN_OBJECT;
875     }
876     if (!data.WriteInt32(pid)) {
877         TAG_LOGE(AAFwkTag::APPMGR, "pid write error");
878         return ERR_FLATTEN_OBJECT;
879     }
880     int32_t ret = SendTransactCmd(
881         static_cast<uint32_t>(IAmsMgr::Message::GET_APPLICATION_INFO_BY_PROCESS_ID), data, reply, option);
882     if (ret != NO_ERROR) {
883         TAG_LOGE(AAFwkTag::APPMGR, "send request fail");
884         return ret;
885     }
886     auto result = reply.ReadInt32();
887     if (result != NO_ERROR) {
888         TAG_LOGE(AAFwkTag::APPMGR, "reply result false");
889         return result;
890     }
891     std::unique_ptr<AppExecFwk::ApplicationInfo> info(reply.ReadParcelable<AppExecFwk::ApplicationInfo>());
892     if (!info) {
893         TAG_LOGE(AAFwkTag::APPMGR, "readParcelableInfo failed");
894         return ERR_NAME_NOT_FOUND;
895     }
896     application = *info;
897     debug = reply.ReadBool();
898     TAG_LOGD(AAFwkTag::APPMGR, "get parcelable info success");
899     return NO_ERROR;
900 }
901 
NotifyAppMgrRecordExitReason(int32_t pid,int32_t reason,const std::string & exitMsg)902 int32_t AmsMgrProxy::NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg)
903 {
904     TAG_LOGD(AAFwkTag::APPMGR, "called");
905     MessageParcel data;
906     MessageParcel reply;
907     MessageOption option(MessageOption::TF_SYNC);
908     if (!WriteInterfaceToken(data)) {
909         TAG_LOGE(AAFwkTag::APPMGR, "token write error");
910         return IPC_PROXY_ERR;
911     }
912     if (!data.WriteInt32(pid)) {
913         TAG_LOGE(AAFwkTag::APPMGR, "Write pid failed");
914         return IPC_PROXY_ERR;
915     }
916     if (!data.WriteInt32(reason)) {
917         TAG_LOGE(AAFwkTag::APPMGR, "Write reason failed");
918         return IPC_PROXY_ERR;
919     }
920     if (!data.WriteString16(Str8ToStr16(exitMsg))) {
921         TAG_LOGE(AAFwkTag::APPMGR, "Write exitMsg failed");
922         return IPC_PROXY_ERR;
923     }
924     int32_t ret = SendTransactCmd(
925         static_cast<uint32_t>(IAmsMgr::Message::NOTIFY_APP_MGR_RECORD_EXIT_REASON), data, reply, option);
926     if (ret != NO_ERROR) {
927         TAG_LOGE(AAFwkTag::APPMGR, "send request fail");
928         return ret;
929     }
930     return reply.ReadInt32();
931 }
932 
SetCurrentUserId(const int32_t userId)933 void AmsMgrProxy::SetCurrentUserId(const int32_t userId)
934 {
935     TAG_LOGD(AAFwkTag::APPMGR, "start");
936     MessageParcel data;
937     MessageParcel reply;
938     MessageOption option(MessageOption::TF_SYNC);
939     if (!WriteInterfaceToken(data)) {
940         return;
941     }
942     if (!data.WriteInt32(userId)) {
943         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write userId");
944         return;
945     }
946     int32_t ret =
947         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::SET_CURRENT_USER_ID),
948             data, reply, option);
949     if (ret != NO_ERROR) {
950         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
951     }
952     TAG_LOGD(AAFwkTag::APPMGR, "end");
953 }
954 
SetEnableStartProcessFlagByUserId(int32_t userId,bool enableStartProcess)955 void AmsMgrProxy::SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess)
956 {
957     TAG_LOGD(AAFwkTag::APPMGR, "called");
958     MessageParcel data;
959     MessageParcel reply;
960     MessageOption option(MessageOption::TF_SYNC);
961     if (!WriteInterfaceToken(data)) {
962         return;
963     }
964     if (!data.WriteInt32(userId)) {
965         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write userId");
966         return;
967     }
968     if (!data.WriteBool(enableStartProcess)) {
969         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write enableStartProcess");
970         return;
971     }
972     int32_t ret =
973         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::ENABLE_START_PROCESS_FLAG_BY_USER_ID),
974             data, reply, option);
975     if (ret != NO_ERROR) {
976         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
977     }
978 }
979 
GetBundleNameByPid(const int pid,std::string & bundleName,int32_t & uid)980 int32_t AmsMgrProxy::GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid)
981 {
982     MessageParcel data;
983     MessageParcel reply;
984     MessageOption option(MessageOption::TF_SYNC);
985     if (!WriteInterfaceToken(data)) {
986         return ERR_INVALID_DATA;
987     }
988     if (!data.WriteInt32(pid)) {
989         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write pid");
990         return ERR_INVALID_DATA;
991     }
992     int32_t ret =
993         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::Get_BUNDLE_NAME_BY_PID),
994             data, reply, option);
995     if (ret != NO_ERROR) {
996         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
997     }
998     bundleName = reply.ReadString();
999     uid = reply.ReadInt32();
1000     return NO_ERROR;
1001 }
1002 
RegisterAppDebugListener(const sptr<IAppDebugListener> & listener)1003 int32_t AmsMgrProxy::RegisterAppDebugListener(const sptr<IAppDebugListener> &listener)
1004 {
1005     TAG_LOGD(AAFwkTag::APPMGR, "called");
1006     MessageParcel data;
1007     if (!WriteInterfaceToken(data)) {
1008         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1009         return ERR_INVALID_DATA;
1010     }
1011 
1012     if (listener == nullptr || !data.WriteRemoteObject(listener->AsObject())) {
1013         TAG_LOGE(AAFwkTag::APPMGR, "Write listener failed");
1014         return ERR_INVALID_DATA;
1015     }
1016 
1017     MessageParcel reply;
1018     MessageOption option(MessageOption::TF_SYNC);
1019     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::REGISTER_APP_DEBUG_LISTENER),
1020         data, reply, option);
1021     if (ret != NO_ERROR) {
1022         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1023         return ret;
1024     }
1025     return reply.ReadInt32();
1026 }
1027 
UnregisterAppDebugListener(const sptr<IAppDebugListener> & listener)1028 int32_t AmsMgrProxy::UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener)
1029 {
1030     TAG_LOGD(AAFwkTag::APPMGR, "called");
1031     MessageParcel data;
1032     if (!WriteInterfaceToken(data)) {
1033         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1034         return ERR_INVALID_DATA;
1035     }
1036 
1037     if (listener == nullptr || !data.WriteRemoteObject(listener->AsObject())) {
1038         TAG_LOGE(AAFwkTag::APPMGR, "Write listener failed");
1039         return ERR_INVALID_DATA;
1040     }
1041 
1042     MessageParcel reply;
1043     MessageOption option(MessageOption::TF_SYNC);
1044     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::UNREGISTER_APP_DEBUG_LISTENER),
1045         data, reply, option);
1046     if (ret != NO_ERROR) {
1047         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1048         return ret;
1049     }
1050     return reply.ReadInt32();
1051 }
1052 
AttachAppDebug(const std::string & bundleName,bool isDebugFromLocal)1053 int32_t AmsMgrProxy::AttachAppDebug(const std::string &bundleName, bool isDebugFromLocal)
1054 {
1055     TAG_LOGD(AAFwkTag::APPMGR, "called");
1056     MessageParcel data;
1057     if (!WriteInterfaceToken(data)) {
1058         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1059         return ERR_INVALID_DATA;
1060     }
1061 
1062     if (bundleName.empty() || !data.WriteString(bundleName)) {
1063         TAG_LOGE(AAFwkTag::APPMGR, "Write bundleName failed");
1064         return ERR_INVALID_DATA;
1065     }
1066 
1067     if (!data.WriteBool(isDebugFromLocal)) {
1068         TAG_LOGE(AAFwkTag::APPMGR, "Write isDebugFromLocal failed");
1069         return ERR_INVALID_DATA;
1070     }
1071 
1072     MessageParcel reply;
1073     MessageOption option(MessageOption::TF_SYNC);
1074     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::ATTACH_APP_DEBUG),
1075         data, reply, option);
1076     if (ret != NO_ERROR) {
1077         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1078         return ret;
1079     }
1080     return reply.ReadInt32();
1081 }
1082 
DetachAppDebug(const std::string & bundleName)1083 int32_t AmsMgrProxy::DetachAppDebug(const std::string &bundleName)
1084 {
1085     TAG_LOGD(AAFwkTag::APPMGR, "called");
1086     MessageParcel data;
1087     if (!WriteInterfaceToken(data)) {
1088         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1089         return ERR_INVALID_DATA;
1090     }
1091 
1092     if (bundleName.empty() || !data.WriteString(bundleName)) {
1093         TAG_LOGE(AAFwkTag::APPMGR, "Write bundleName failed");
1094         return ERR_INVALID_DATA;
1095     }
1096 
1097     MessageParcel reply;
1098     MessageOption option(MessageOption::TF_SYNC);
1099     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::DETACH_APP_DEBUG),
1100         data, reply, option);
1101     if (ret != NO_ERROR) {
1102         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1103         return ret;
1104     }
1105     return reply.ReadInt32();
1106 }
1107 
SetKeepAliveEnableState(const std::string & bundleName,bool enable,int32_t uid)1108 void AmsMgrProxy::SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid)
1109 {
1110     TAG_LOGD(AAFwkTag::APPMGR, "called");
1111     MessageParcel data;
1112     if (!WriteInterfaceToken(data)) {
1113         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1114         return;
1115     }
1116     if (bundleName.empty() || !data.WriteString(bundleName)) {
1117         TAG_LOGE(AAFwkTag::APPMGR, "Write bundleName fail");
1118         return;
1119     }
1120     if (!data.WriteBool(enable) || !data.WriteInt32(uid)) {
1121         TAG_LOGE(AAFwkTag::APPMGR, "Write flag or uid fail");
1122         return;
1123     }
1124     MessageParcel reply;
1125     MessageOption option;
1126     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::SET_KEEP_ALIVE_ENABLE_STATE),
1127         data, reply, option);
1128     if (ret != NO_ERROR) {
1129         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1130     }
1131 }
1132 
SetKeepAliveDkv(const std::string & bundleName,bool enable,int32_t uid)1133 void AmsMgrProxy::SetKeepAliveDkv(const std::string &bundleName, bool enable, int32_t uid)
1134 {
1135     TAG_LOGD(AAFwkTag::APPMGR, "called");
1136     MessageParcel data;
1137     if (!WriteInterfaceToken(data)) {
1138         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1139         return;
1140     }
1141     if (bundleName.empty() || !data.WriteString(bundleName)) {
1142         TAG_LOGE(AAFwkTag::APPMGR, "Write bundleName fail");
1143         return;
1144     }
1145     if (!data.WriteBool(enable) || !data.WriteInt32(uid)) {
1146         TAG_LOGE(AAFwkTag::APPMGR, "Write flag or uid fail");
1147         return;
1148     }
1149     MessageParcel reply;
1150     MessageOption option;
1151     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::SET_KEEP_ALIVE_DKV),
1152         data, reply, option);
1153     if (ret != NO_ERROR) {
1154         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1155     }
1156 }
1157 
SetKeepAliveAppService(const std::string & bundleName,bool enable,int32_t uid)1158 void AmsMgrProxy::SetKeepAliveAppService(const std::string &bundleName, bool enable, int32_t uid)
1159 {
1160     TAG_LOGD(AAFwkTag::APPMGR, "SetKeepAliveAppService called");
1161     MessageParcel data;
1162     if (!WriteInterfaceToken(data)) {
1163         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1164         return;
1165     }
1166     if (bundleName.empty() || !data.WriteString(bundleName)) {
1167         TAG_LOGE(AAFwkTag::APPMGR, "Write bundleName fail");
1168         return;
1169     }
1170     if (!data.WriteBool(enable) || !data.WriteInt32(uid)) {
1171         TAG_LOGE(AAFwkTag::APPMGR, "Write flag or uid fail");
1172         return;
1173     }
1174     MessageParcel reply;
1175     MessageOption option;
1176     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::SET_KEEP_ALIVE_APP_SERVICE),
1177         data, reply, option);
1178     if (ret != NO_ERROR) {
1179         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1180     }
1181 }
1182 
SetAppWaitingDebug(const std::string & bundleName,bool isPersist)1183 int32_t AmsMgrProxy::SetAppWaitingDebug(const std::string &bundleName, bool isPersist)
1184 {
1185     TAG_LOGD(AAFwkTag::APPMGR, "called");
1186     MessageParcel data;
1187     if (!WriteInterfaceToken(data)) {
1188         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1189         return ERR_INVALID_DATA;
1190     }
1191 
1192     if (bundleName.empty() || !data.WriteString(bundleName)) {
1193         TAG_LOGE(AAFwkTag::APPMGR, "Write bundleName failed");
1194         return ERR_INVALID_DATA;
1195     }
1196 
1197     if (!data.WriteBool(isPersist)) {
1198         TAG_LOGE(AAFwkTag::APPMGR, "Write persist flag failed.");
1199         return ERR_INVALID_DATA;
1200     }
1201 
1202     MessageParcel reply;
1203     MessageOption option(MessageOption::TF_SYNC);
1204     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::SET_APP_WAITING_DEBUG), data, reply, option);
1205     if (ret != NO_ERROR) {
1206         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1207         return ret;
1208     }
1209     return reply.ReadInt32();
1210 }
1211 
CancelAppWaitingDebug()1212 int32_t AmsMgrProxy::CancelAppWaitingDebug()
1213 {
1214     TAG_LOGD(AAFwkTag::APPMGR, "called");
1215     MessageParcel data;
1216     if (!WriteInterfaceToken(data)) {
1217         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1218         return ERR_INVALID_DATA;
1219     }
1220 
1221     MessageParcel reply;
1222     MessageOption option(MessageOption::TF_SYNC);
1223     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::CANCEL_APP_WAITING_DEBUG), data, reply, option);
1224     if (ret != NO_ERROR) {
1225         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1226         return ret;
1227     }
1228     return reply.ReadInt32();
1229 }
1230 
GetWaitingDebugApp(std::vector<std::string> & debugInfoList)1231 int32_t AmsMgrProxy::GetWaitingDebugApp(std::vector<std::string> &debugInfoList)
1232 {
1233     TAG_LOGD(AAFwkTag::APPMGR, "called");
1234     MessageParcel data;
1235     if (!WriteInterfaceToken(data)) {
1236         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1237         return ERR_INVALID_DATA;
1238     }
1239 
1240     MessageParcel reply;
1241     MessageOption option(MessageOption::TF_SYNC);
1242     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::GET_WAITING_DEBUG_APP), data, reply, option);
1243     if (ret != NO_ERROR) {
1244         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1245         return ret;
1246     }
1247 
1248     auto resultCode = reply.ReadInt32();
1249     if (resultCode != ERR_OK) {
1250         TAG_LOGE(AAFwkTag::APPMGR, "Reply err: %{public}d", resultCode);
1251         return resultCode;
1252     }
1253 
1254     auto infoSize = reply.ReadInt32();
1255     if (infoSize > MAX_APP_DEBUG_COUNT) {
1256         TAG_LOGE(AAFwkTag::APPMGR, "Max app debug count: %{public}d", infoSize);
1257         return ERR_INVALID_DATA;
1258     }
1259 
1260     if (!reply.ReadStringVector(&debugInfoList)) {
1261         TAG_LOGE(AAFwkTag::APPMGR, "ReadStringVector failed");
1262         return ERR_INVALID_DATA;
1263     }
1264 
1265     return NO_ERROR;
1266 }
1267 
IsWaitingDebugApp(const std::string & bundleName)1268 bool AmsMgrProxy::IsWaitingDebugApp(const std::string &bundleName)
1269 {
1270     TAG_LOGD(AAFwkTag::APPMGR, "called");
1271     MessageParcel data;
1272     if (!WriteInterfaceToken(data)) {
1273         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1274         return false;
1275     }
1276 
1277     if (bundleName.empty() || !data.WriteString(bundleName)) {
1278         TAG_LOGE(AAFwkTag::APPMGR, "Write bundleName failed");
1279         return false;
1280     }
1281 
1282     MessageParcel reply;
1283     MessageOption option(MessageOption::TF_SYNC);
1284     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::IS_WAITING_DEBUG_APP), data, reply, option);
1285     if (ret != NO_ERROR) {
1286         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1287         return false;
1288     }
1289     return reply.ReadBool();
1290 }
1291 
ClearNonPersistWaitingDebugFlag()1292 void AmsMgrProxy::ClearNonPersistWaitingDebugFlag()
1293 {
1294     TAG_LOGD(AAFwkTag::APPMGR, "called");
1295     MessageParcel data;
1296     if (!WriteInterfaceToken(data)) {
1297         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1298         return;
1299     }
1300 
1301     MessageParcel reply;
1302     MessageOption option(MessageOption::TF_SYNC);
1303     auto ret = SendTransactCmd(
1304         static_cast<uint32_t>(IAmsMgr::Message::CLEAR_NON_PERSIST_WAITING_DEBUG_FLAG), data, reply, option);
1305     if (ret != NO_ERROR) {
1306         TAG_LOGW(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1307     }
1308 }
1309 
RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> & response)1310 int32_t AmsMgrProxy::RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response)
1311 {
1312     TAG_LOGD(AAFwkTag::APPMGR, "called");
1313     MessageParcel data;
1314     if (!WriteInterfaceToken(data)) {
1315         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1316         return ERR_INVALID_DATA;
1317     }
1318 
1319     if (response == nullptr || !data.WriteRemoteObject(response->AsObject())) {
1320         TAG_LOGE(AAFwkTag::APPMGR, "write remote obj failed");
1321         return ERR_INVALID_DATA;
1322     }
1323 
1324     MessageParcel reply;
1325     MessageOption option(MessageOption::TF_SYNC);
1326     int32_t ret = SendTransactCmd(
1327         static_cast<uint32_t>(IAmsMgr::Message::REGISTER_ABILITY_DEBUG_RESPONSE), data, reply, option);
1328     if (ret != NO_ERROR) {
1329         TAG_LOGE(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
1330         return ret;
1331     }
1332     return reply.ReadInt32();
1333 }
1334 
IsAttachDebug(const std::string & bundleName)1335 bool AmsMgrProxy::IsAttachDebug(const std::string &bundleName)
1336 {
1337     TAG_LOGD(AAFwkTag::APPMGR, "called");
1338     MessageParcel data;
1339     if (!WriteInterfaceToken(data)) {
1340         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1341         return false;
1342     }
1343 
1344     if (bundleName.empty() || !data.WriteString(bundleName)) {
1345         TAG_LOGE(AAFwkTag::APPMGR, "Write bundleName fail");
1346         return false;
1347     }
1348 
1349     MessageParcel reply;
1350     MessageOption option(MessageOption::TF_SYNC);
1351     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::IS_ATTACH_DEBUG),
1352         data, reply, option);
1353     if (ret != NO_ERROR) {
1354         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1355         return false;
1356     }
1357     return reply.ReadBool();
1358 }
1359 
ClearProcessByToken(sptr<IRemoteObject> token)1360 void AmsMgrProxy::ClearProcessByToken(sptr<IRemoteObject> token)
1361 {
1362     MessageParcel data;
1363     if (!WriteInterfaceToken(data)) {
1364         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
1365         return;
1366     }
1367     if (!data.WriteRemoteObject(token)) {
1368         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
1369         return;
1370     }
1371     MessageParcel reply;
1372     MessageOption option;
1373     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::CLEAR_PROCESS_BY_TOKEN), data, reply, option);
1374     if (ret != NO_ERROR) {
1375         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
1376     }
1377 }
1378 
IsMemorySizeSufficent()1379 bool AmsMgrProxy::IsMemorySizeSufficent()
1380 {
1381     MessageParcel data;
1382     if (!WriteInterfaceToken(data)) {
1383         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1384         return true;
1385     }
1386 
1387     MessageParcel reply;
1388     MessageOption option(MessageOption::TF_SYNC);
1389     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::IS_MEMORY_SIZE_SUFFICIENT), data, reply, option);
1390     if (ret != NO_ERROR) {
1391         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1392         return true;
1393     }
1394     return reply.ReadBool();
1395 }
1396 
IsNoRequireBigMemory()1397 bool AmsMgrProxy::IsNoRequireBigMemory()
1398 {
1399     MessageParcel data;
1400     if (!WriteInterfaceToken(data)) {
1401         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1402         return true;
1403     }
1404 
1405     MessageParcel reply;
1406     MessageOption option(MessageOption::TF_SYNC);
1407     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::IS_NO_REQUIRE_BIG_MEMORY), data, reply, option);
1408     if (ret != NO_ERROR) {
1409         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1410         return true;
1411     }
1412     return reply.ReadBool();
1413 }
1414 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1415 int32_t AmsMgrProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
1416     MessageParcel &reply, MessageOption &option)
1417 {
1418     sptr<IRemoteObject> remote = Remote();
1419     if (remote == nullptr) {
1420         TAG_LOGE(AAFwkTag::APPMGR, "null remote");
1421         return ERR_NULL_OBJECT;
1422     }
1423 
1424     int32_t ret = remote->SendRequest(code, data, reply, option);
1425     if (ret != NO_ERROR) {
1426         TAG_LOGE(AAFwkTag::APPMGR, "SendRequest err: %{public}d, ret: %{public}d", code, ret);
1427         return ret;
1428     }
1429     return ret;
1430 }
1431 
AttachedToStatusBar(const sptr<IRemoteObject> & token)1432 void AmsMgrProxy::AttachedToStatusBar(const sptr<IRemoteObject> &token)
1433 {
1434     TAG_LOGD(AAFwkTag::APPMGR, "start");
1435     MessageParcel data;
1436     MessageParcel reply;
1437     MessageOption option;
1438     if (!WriteInterfaceToken(data)) {
1439         return;
1440     }
1441     if (!data.WriteRemoteObject(token)) {
1442         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
1443         return;
1444     }
1445     int32_t ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::ATTACHED_TO_STATUS_BAR),
1446         data, reply, option);
1447     if (ret != NO_ERROR) {
1448         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
1449     }
1450     TAG_LOGD(AAFwkTag::APPMGR, "end");
1451 }
1452 
BlockProcessCacheByPids(const std::vector<int32_t> & pids)1453 void AmsMgrProxy::BlockProcessCacheByPids(const std::vector<int32_t> &pids)
1454 {
1455     TAG_LOGD(AAFwkTag::APPMGR, "start");
1456     MessageParcel data;
1457     MessageParcel reply;
1458     MessageOption option;
1459 
1460     if (!WriteInterfaceToken(data)) {
1461         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1462         return;
1463     }
1464     if (!data.WriteUint32(pids.size())) {
1465         TAG_LOGE(AAFwkTag::APPMGR, "Write size failed");
1466         return;
1467     }
1468     for (const auto &pid: pids) {
1469         if (!data.WriteInt32(pid)) {
1470             TAG_LOGE(AAFwkTag::APPMGR, "Write pid failed");
1471             return;
1472         }
1473     }
1474     int32_t ret =
1475         SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::BLOCK_PROCESS_CACHE_BY_PIDS), data, reply, option);
1476     if (ret != NO_ERROR) {
1477         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
1478     }
1479     TAG_LOGD(AAFwkTag::APPMGR, "end");
1480 }
1481 
IsKilledForUpgradeWeb(const std::string & bundleName)1482 bool AmsMgrProxy::IsKilledForUpgradeWeb(const std::string &bundleName)
1483 {
1484     MessageParcel data;
1485     MessageParcel reply;
1486     MessageOption option;
1487     if (!WriteInterfaceToken(data)) {
1488         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1489         return false;
1490     }
1491     if (!data.WriteString(bundleName)) {
1492         TAG_LOGE(AAFwkTag::APPMGR, "WriteString failed");
1493         return false;
1494     }
1495 
1496     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::IS_KILLED_FOR_UPGRADE_WEB), data, reply, option);
1497     if (ret != NO_ERROR) {
1498         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1499         return false;
1500     }
1501     return reply.ReadBool();
1502 }
1503 
CleanAbilityByUserRequest(const sptr<IRemoteObject> & token)1504 bool AmsMgrProxy::CleanAbilityByUserRequest(const sptr<IRemoteObject> &token)
1505 {
1506     MessageParcel data;
1507     MessageParcel reply;
1508     MessageOption option;
1509     if (!WriteInterfaceToken(data)) {
1510         TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
1511         return false;
1512     }
1513     if (!data.WriteRemoteObject(token.GetRefPtr())) {
1514         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
1515         return false;
1516     }
1517 
1518     int32_t ret = SendTransactCmd(
1519         static_cast<uint32_t>(IAmsMgr::Message::CLEAN_UIABILITY_BY_USER_REQUEST), data, reply, option);
1520     if (ret != NO_ERROR) {
1521         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
1522     }
1523     return reply.ReadBool();
1524 }
1525 
IsProcessAttached(sptr<IRemoteObject> token)1526 bool AmsMgrProxy::IsProcessAttached(sptr<IRemoteObject> token)
1527 {
1528     MessageParcel data;
1529     MessageParcel reply;
1530     MessageOption option;
1531     if (!WriteInterfaceToken(data)) {
1532         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1533         return false;
1534     }
1535     if (!data.WriteRemoteObject(token.GetRefPtr())) {
1536         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write token");
1537         return false;
1538     }
1539 
1540     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::IS_PROCESS_ATTACHED), data, reply, option);
1541     if (ret != NO_ERROR) {
1542         TAG_LOGE(AAFwkTag::APPMGR, "Send request failed, error code is %{public}d.", ret);
1543         return false;
1544     }
1545     return reply.ReadBool();
1546 }
1547 
IsCallerKilling(const std::string & callerKey)1548 bool AmsMgrProxy::IsCallerKilling(const std::string& callerKey)
1549 {
1550     MessageParcel data;
1551     MessageParcel reply;
1552     MessageOption option;
1553     if (!WriteInterfaceToken(data)) {
1554         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1555         return false;
1556     }
1557     if (!data.WriteString(callerKey)) {
1558         TAG_LOGE(AAFwkTag::APPMGR, "Failed to write callerKey");
1559         return false;
1560     }
1561 
1562     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::IS_CALLER_KILLING), data, reply, option);
1563     if (ret != NO_ERROR) {
1564         TAG_LOGE(AAFwkTag::APPMGR, "Send request failed, error code is %{public}d.", ret);
1565         return false;
1566     }
1567     return reply.ReadBool();
1568 }
1569 
PreloadApplicationByPhase(const std::string & bundleName,int32_t userId,int32_t appIndex,AppExecFwk::PreloadPhase preloadPhase)1570 int32_t AmsMgrProxy::PreloadApplicationByPhase(const std::string &bundleName, int32_t userId, int32_t appIndex,
1571     AppExecFwk::PreloadPhase preloadPhase)
1572 {
1573     MessageParcel data;
1574     MessageParcel reply;
1575     MessageOption option;
1576     if (!WriteInterfaceToken(data)) {
1577         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1578         return AAFwk::ERR_WRITE_INTERFACE_TOKEN_FAILED;
1579     }
1580     PARCEL_UTIL_WRITE_RET_INT(data, String, bundleName);
1581     PARCEL_UTIL_WRITE_RET_INT(data, Int32, userId);
1582     PARCEL_UTIL_WRITE_RET_INT(data, Int32, appIndex);
1583     PARCEL_UTIL_WRITE_RET_INT(data, Int32, static_cast<int32_t>(preloadPhase));
1584 
1585     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::PRELOAD_APPLICATION_BY_PHASE), data, reply,
1586         option);
1587     if (ret != NO_ERROR) {
1588         TAG_LOGE(AAFwkTag::APPMGR, "Send request failed, err: %{public}d.", ret);
1589         return ret;
1590     }
1591     return reply.ReadInt32();
1592 }
1593 
NotifyPreloadAbilityStateChanged(sptr<IRemoteObject> token,bool isPreForeground)1594 int32_t AmsMgrProxy::NotifyPreloadAbilityStateChanged(sptr<IRemoteObject> token, bool isPreForeground)
1595 {
1596     if (token == nullptr) {
1597         TAG_LOGE(AAFwkTag::APPMGR, "token null.");
1598         return AAFwk::INVALID_CALLER_TOKEN;
1599     }
1600     MessageParcel data;
1601     MessageParcel reply;
1602     MessageOption option;
1603     if (!WriteInterfaceToken(data)) {
1604         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
1605         return AAFwk::ERR_WRITE_INTERFACE_TOKEN_FAILED;
1606     }
1607     if (!data.WriteRemoteObject(token)) {
1608         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed.");
1609         return AAFwk::ERR_WRITE_CALLER_TOKEN_FAILED;
1610     }
1611     PARCEL_UTIL_WRITE_RET_INT(data, Bool, isPreForeground);
1612 
1613     auto ret = SendTransactCmd(
1614         static_cast<uint32_t>(IAmsMgr::Message::NOTIFY_PRELOAD_ABILITY_STATE_CHANGED), data, reply, option);
1615     if (ret != NO_ERROR) {
1616         TAG_LOGE(AAFwkTag::APPMGR, "Send request failed, err: %{public}d.", ret);
1617         return ret;
1618     }
1619     return reply.ReadInt32();
1620 }
1621 
CheckPreloadAppRecordExist(const std::string & bundleName,int32_t userId,int32_t appIndex,bool & isExist)1622 int32_t AmsMgrProxy::CheckPreloadAppRecordExist(const std::string &bundleName, int32_t userId, int32_t appIndex,
1623     bool &isExist)
1624 {
1625     TAG_LOGD(AAFwkTag::APPMGR, "CheckPreloadAppRecordExist start");
1626     MessageParcel data;
1627     MessageParcel reply;
1628     MessageOption option;
1629     if (!WriteInterfaceToken(data)) {
1630         return AAFwk::ERR_WRITE_CALLER_TOKEN_FAILED;
1631     }
1632     PARCEL_UTIL_WRITE_RET_BOOL(data, String, bundleName);
1633     PARCEL_UTIL_WRITE_RET_BOOL(data, Int32, userId);
1634     PARCEL_UTIL_WRITE_RET_BOOL(data, Int32, appIndex);
1635 
1636     int32_t ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::CHECK_PRELOAD_APP_RECORD_EXIST),
1637         data, reply, option);
1638     if (ret != NO_ERROR) {
1639         TAG_LOGE(AAFwkTag::APPMGR, "Send request failed, err: %{public}d.", ret);
1640         return ret;
1641     }
1642     auto replyRet = reply.ReadInt32();
1643     if (replyRet != NO_ERROR) {
1644         return replyRet;
1645     }
1646     isExist = reply.ReadBool();
1647     return NO_ERROR;
1648 }
1649 
VerifyKillProcessPermission(const std::string & bundleName)1650 int32_t AmsMgrProxy::VerifyKillProcessPermission(const std::string &bundleName)
1651 {
1652     TAG_LOGD(AAFwkTag::APPMGR, "called");
1653     MessageParcel data;
1654     if (!WriteInterfaceToken(data)) {
1655         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
1656         return ERR_INVALID_DATA;
1657     }
1658 
1659     if (bundleName.empty() || !data.WriteString(bundleName)) {
1660         TAG_LOGE(AAFwkTag::APPMGR, "Write bundleName failed");
1661         return ERR_INVALID_DATA;
1662     }
1663 
1664     MessageParcel reply;
1665     MessageOption option(MessageOption::TF_SYNC);
1666     auto ret = SendTransactCmd(static_cast<uint32_t>(IAmsMgr::Message::VERIFY_KILL_PROCESS_PERMISSION),
1667         data, reply, option);
1668     if (ret != NO_ERROR) {
1669         TAG_LOGE(AAFwkTag::APPMGR, "Send request err: %{public}d", ret);
1670         return ret;
1671     }
1672     return reply.ReadInt32();
1673 }
1674 } // namespace AppExecFwk
1675 } // namespace OHOS
1676