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