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