• 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 "app_mgr_client.h"
17 
18 #include <cstdio>
19 #include <string>
20 #include <unistd.h>
21 
22 #include "if_system_ability_manager.h"
23 #include "ipc_skeleton.h"
24 
25 #include "app_mem_info.h"
26 #include "app_mgr_interface.h"
27 #include "app_service_manager.h"
28 #include "hilog_tag_wrapper.h"
29 #include "hitrace_meter.h"
30 #include "param.h"
31 
32 namespace OHOS {
33 namespace AppExecFwk {
34 class AppMgrRemoteHolder : public std::enable_shared_from_this<AppMgrRemoteHolder> {
35 public:
36     AppMgrRemoteHolder() = default;
37 
~AppMgrRemoteHolder()38     ~AppMgrRemoteHolder()
39     {
40         RemoveDeathRecipient();
41     }
42 
SetServiceManager(std::unique_ptr<AppServiceManager> serviceMgr)43     void SetServiceManager(std::unique_ptr<AppServiceManager> serviceMgr)
44     {
45         std::lock_guard<std::mutex> lock(mutex_);
46         serviceManager_ = std::move(serviceMgr);
47     }
48 
ConnectAppMgrService()49     AppMgrResultCode ConnectAppMgrService()
50     {
51         std::lock_guard<std::mutex> lock(mutex_);
52         return ConnectAppMgrServiceInner();
53     }
54 
GetRemoteObject()55     sptr<IRemoteObject> GetRemoteObject()
56     {
57         std::lock_guard<std::mutex> lock(mutex_);
58         if (!remote_) {
59             (void) ConnectAppMgrServiceInner();
60         }
61         return remote_;
62     }
63 
64 private:
HandleRemoteDied(const wptr<IRemoteObject> & remote)65     void HandleRemoteDied(const wptr<IRemoteObject>& remote)
66     {
67         std::lock_guard<std::mutex> lock(mutex_);
68         if (!remote_) {
69             return;
70         }
71 
72         if (remote_ == remote.promote()) {
73             remote_->RemoveDeathRecipient(deathRecipient_);
74             remote_ = nullptr;
75             deathRecipient_ = nullptr;
76         }
77     }
78 
ConnectAppMgrServiceInner()79     AppMgrResultCode ConnectAppMgrServiceInner()
80     {
81         if (!serviceManager_) {
82             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
83         }
84         if (remote_) {
85             return AppMgrResultCode::RESULT_OK;
86         }
87         TAG_LOGD(AAFwkTag::APPMGR, "get AppMgrRemote object");
88         remote_ = serviceManager_->GetAppMgrService();
89         if (!remote_) {
90             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
91         }
92 
93         auto me = shared_from_this();
94         deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new AppMgrDeathRecipient(me));
95         if (deathRecipient_ == nullptr) {
96             TAG_LOGE(AAFwkTag::APPMGR, "create AppMgrDeathRecipient failed");
97             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
98         }
99         if ((remote_->IsProxyObject()) && (!remote_->AddDeathRecipient(deathRecipient_))) {
100             TAG_LOGE(AAFwkTag::APPMGR, "AddDeathRecipient to AppMs failed");
101             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
102         }
103 
104         return AppMgrResultCode::RESULT_OK;
105     }
106 
RemoveDeathRecipient()107     void RemoveDeathRecipient()
108     {
109         TAG_LOGD(AAFwkTag::APPMGR, "RemoveDeathRecipient");
110         std::lock_guard<std::mutex> lock(mutex_);
111         if (remote_ == nullptr) {
112             TAG_LOGI(AAFwkTag::APPMGR, "null remote_");
113             return;
114         }
115         if (deathRecipient_ == nullptr) {
116             TAG_LOGI(AAFwkTag::APPMGR, "null deathRecipient_");
117             return;
118         }
119         bool ret = remote_->RemoveDeathRecipient(deathRecipient_);
120         if (!ret) {
121             TAG_LOGW(AAFwkTag::APPMGR, "RemoveDeathRecipient fail");
122             return;
123         }
124         remote_ = nullptr;
125         deathRecipient_ = nullptr;
126         TAG_LOGD(AAFwkTag::APPMGR, "RemoveDeathRecipient success");
127     }
128 
129     class AppMgrDeathRecipient : public IRemoteObject::DeathRecipient {
130     public:
AppMgrDeathRecipient(const std::shared_ptr<AppMgrRemoteHolder> & holder)131         explicit AppMgrDeathRecipient(const std::shared_ptr<AppMgrRemoteHolder>& holder) : owner_(holder) {}
132 
133         virtual ~AppMgrDeathRecipient() = default;
134 
OnRemoteDied(const wptr<IRemoteObject> & remote)135         void OnRemoteDied(const wptr<IRemoteObject>& remote) override
136         {
137             std::shared_ptr<AppMgrRemoteHolder> holder = owner_.lock();
138             if (holder) {
139                 holder->HandleRemoteDied(remote);
140             }
141         }
142 
143     private:
144         std::weak_ptr<AppMgrRemoteHolder> owner_;
145     };
146 
147 private:
148     std::unique_ptr<AppServiceManager> serviceManager_;
149     sptr<IRemoteObject> remote_;
150     std::mutex mutex_;
151     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
152 };
153 
AppMgrClient()154 AppMgrClient::AppMgrClient()
155 {
156     SetServiceManager(std::make_unique<AppServiceManager>());
157 }
158 
~AppMgrClient()159 AppMgrClient::~AppMgrClient()
160 {}
161 
LoadAbility(const AbilityInfo & abilityInfo,const ApplicationInfo & appInfo,const AAFwk::Want & want,AbilityRuntime::LoadParam loadParam)162 AppMgrResultCode AppMgrClient::LoadAbility(const AbilityInfo &abilityInfo, const ApplicationInfo &appInfo,
163     const AAFwk::Want &want, AbilityRuntime::LoadParam loadParam)
164 {
165     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
166     if (service != nullptr) {
167         sptr<IAmsMgr> amsService = service->GetAmsMgr();
168         if (amsService != nullptr) {
169             // From here, separate AbilityInfo and ApplicationInfo from AA.
170             std::shared_ptr<AbilityInfo> abilityInfoPtr = std::make_shared<AbilityInfo>(abilityInfo);
171             std::shared_ptr<ApplicationInfo> appInfoPtr = std::make_shared<ApplicationInfo>(appInfo);
172             std::shared_ptr<AAFwk::Want> wantPtr = std::make_shared<AAFwk::Want>(want);
173             auto loadParamPtr = std::make_shared<AbilityRuntime::LoadParam>(loadParam);
174             amsService->LoadAbility(abilityInfoPtr, appInfoPtr, wantPtr, loadParamPtr);
175             return AppMgrResultCode::RESULT_OK;
176         }
177     }
178     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
179 }
180 
TerminateAbility(const sptr<IRemoteObject> & token,bool clearMissionFlag)181 AppMgrResultCode AppMgrClient::TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag)
182 {
183     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
184     if (service != nullptr) {
185         sptr<IAmsMgr> amsService = service->GetAmsMgr();
186         if (amsService != nullptr) {
187             amsService->TerminateAbility(token, clearMissionFlag);
188             return AppMgrResultCode::RESULT_OK;
189         }
190     }
191     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
192 }
193 
UpdateAbilityState(const sptr<IRemoteObject> & token,const AbilityState state)194 AppMgrResultCode AppMgrClient::UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state)
195 {
196     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
197     if (service != nullptr) {
198         sptr<IAmsMgr> amsService = service->GetAmsMgr();
199         if (amsService != nullptr) {
200             amsService->UpdateAbilityState(token, state);
201             return AppMgrResultCode::RESULT_OK;
202         }
203     }
204     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
205 }
206 
UpdateExtensionState(const sptr<IRemoteObject> & token,const ExtensionState state)207 AppMgrResultCode AppMgrClient::UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state)
208 {
209     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
210     if (service != nullptr) {
211         sptr<IAmsMgr> amsService = service->GetAmsMgr();
212         if (amsService != nullptr) {
213             amsService->UpdateExtensionState(token, state);
214             return AppMgrResultCode::RESULT_OK;
215         }
216     }
217     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
218 }
219 
RegisterAppStateCallback(const sptr<IAppStateCallback> & callback)220 AppMgrResultCode AppMgrClient::RegisterAppStateCallback(const sptr<IAppStateCallback> &callback)
221 {
222     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
223     if (service != nullptr) {
224         sptr<IAmsMgr> amsService = service->GetAmsMgr();
225         if (amsService != nullptr) {
226             amsService->RegisterAppStateCallback(callback);
227             return AppMgrResultCode::RESULT_OK;
228         }
229     }
230     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
231 }
232 
KillProcessByAbilityToken(const sptr<IRemoteObject> & token)233 AppMgrResultCode AppMgrClient::KillProcessByAbilityToken(const sptr<IRemoteObject> &token)
234 {
235     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
236     if (service != nullptr) {
237         sptr<IAmsMgr> amsService = service->GetAmsMgr();
238         if (amsService != nullptr) {
239             amsService->KillProcessByAbilityToken(token);
240             return AppMgrResultCode::RESULT_OK;
241         }
242     }
243     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
244 }
245 
KillProcessesByUserId(int32_t userId,bool isNeedSendAppSpawnMsg,sptr<AAFwk::IUserCallback> callback)246 AppMgrResultCode AppMgrClient::KillProcessesByUserId(int32_t userId, bool isNeedSendAppSpawnMsg,
247     sptr<AAFwk::IUserCallback> callback)
248 {
249     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
250     if (service != nullptr) {
251         sptr<IAmsMgr> amsService = service->GetAmsMgr();
252         if (amsService != nullptr) {
253             amsService->KillProcessesByUserId(userId, isNeedSendAppSpawnMsg, callback);
254             return AppMgrResultCode::RESULT_OK;
255         }
256     }
257     if (callback) {
258         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
259         callback->OnLogoutUserDone(userId, AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED);
260     }
261     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
262 }
263 
KillProcessesByPids(const std::vector<int32_t> & pids,const std::string & reason,bool subProcess,bool isKillPrecedeStart)264 AppMgrResultCode AppMgrClient::KillProcessesByPids(const std::vector<int32_t> &pids, const std::string &reason,
265     bool subProcess, bool isKillPrecedeStart)
266 {
267     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
268     if (service != nullptr) {
269         sptr<IAmsMgr> amsService = service->GetAmsMgr();
270         if (amsService != nullptr) {
271             int32_t ret = amsService->KillProcessesByPids(pids, reason, subProcess, isKillPrecedeStart);
272             if (ret == ERR_OK) {
273                 return AppMgrResultCode::RESULT_OK;
274             }
275             return AppMgrResultCode::ERROR_KILL_PROCESSES_BY_PIDS;
276         }
277     }
278     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
279 }
280 
AttachPidToParent(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & callerToken)281 AppMgrResultCode AppMgrClient::AttachPidToParent(const sptr<IRemoteObject> &token,
282     const sptr<IRemoteObject> &callerToken)
283 {
284     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
285     if (service != nullptr) {
286         sptr<IAmsMgr> amsService = service->GetAmsMgr();
287         if (amsService != nullptr) {
288             amsService->AttachPidToParent(token, callerToken);
289             return AppMgrResultCode::RESULT_OK;
290         }
291     }
292     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
293 }
294 
UpdateApplicationInfoInstalled(const std::string & bundleName,const int uid,const std::string & moduleName,bool isPlugin)295 AppMgrResultCode AppMgrClient::UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid,
296     const std::string &moduleName, bool isPlugin)
297 {
298     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
299     if (service != nullptr) {
300         sptr<IAmsMgr> amsService = service->GetAmsMgr();
301         if (amsService != nullptr) {
302             int32_t result = amsService->UpdateApplicationInfoInstalled(bundleName, uid, moduleName, isPlugin);
303             if (result == ERR_OK) {
304                 return AppMgrResultCode::RESULT_OK;
305             }
306             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
307         }
308     }
309     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
310 }
311 
KillApplication(const std::string & bundleName,bool clearPageStack,int32_t appIndex)312 AppMgrResultCode AppMgrClient::KillApplication(const std::string &bundleName, bool clearPageStack, int32_t appIndex)
313 {
314     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
315     if (service != nullptr) {
316         sptr<IAmsMgr> amsService = service->GetAmsMgr();
317         if (amsService != nullptr) {
318             int32_t result = amsService->KillApplication(bundleName, clearPageStack, appIndex);
319             if (result == ERR_OK) {
320                 return AppMgrResultCode::RESULT_OK;
321             }
322             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
323         }
324     }
325     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
326 }
327 
ForceKillApplication(const std::string & bundleName,const int userId,const int appIndex)328 AppMgrResultCode AppMgrClient::ForceKillApplication(const std::string &bundleName,
329     const int userId, const int appIndex)
330 {
331     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
332     if (service != nullptr) {
333         sptr<IAmsMgr> amsService = service->GetAmsMgr();
334         if (amsService != nullptr) {
335             int32_t result = amsService->ForceKillApplication(bundleName, userId, appIndex);
336             if (result == ERR_OK) {
337                 return AppMgrResultCode::RESULT_OK;
338             }
339             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
340         }
341     }
342     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
343 }
344 
KillProcessesByAccessTokenId(const uint32_t accessTokenId)345 AppMgrResultCode AppMgrClient::KillProcessesByAccessTokenId(const uint32_t accessTokenId)
346 {
347     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
348     if (service != nullptr) {
349         sptr<IAmsMgr> amsService = service->GetAmsMgr();
350         if (amsService != nullptr) {
351             int32_t result = amsService->KillProcessesByAccessTokenId(accessTokenId);
352             if (result == ERR_OK) {
353                 return AppMgrResultCode::RESULT_OK;
354             }
355             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
356         }
357     }
358     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
359 }
360 
KillApplicationByUid(const std::string & bundleName,const int uid,const std::string & reason)361 AppMgrResultCode AppMgrClient::KillApplicationByUid(const std::string &bundleName, const int uid,
362     const std::string& reason)
363 {
364     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
365     if (service != nullptr) {
366         sptr<IAmsMgr> amsService = service->GetAmsMgr();
367         if (amsService != nullptr) {
368             int32_t result = amsService->KillApplicationByUid(bundleName, uid, reason);
369             if (result == ERR_OK) {
370                 return AppMgrResultCode::RESULT_OK;
371             }
372             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
373         }
374     }
375     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
376 }
377 
NotifyUninstallOrUpgradeApp(const std::string & bundleName,int32_t uid,bool isUpgrade)378 AppMgrResultCode AppMgrClient::NotifyUninstallOrUpgradeApp(const std::string &bundleName, int32_t uid, bool isUpgrade)
379 {
380     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
381     if (service != nullptr) {
382         sptr<IAmsMgr> amsService = service->GetAmsMgr();
383         if (amsService != nullptr) {
384             int32_t result = amsService->NotifyUninstallOrUpgradeApp(bundleName, uid, isUpgrade);
385             if (result == ERR_OK) {
386                 return AppMgrResultCode::RESULT_OK;
387             }
388             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
389         }
390     }
391     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
392 }
393 
NotifyUninstallOrUpgradeAppEnd(int32_t uid)394 void AppMgrClient::NotifyUninstallOrUpgradeAppEnd(int32_t uid)
395 {
396     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
397     if (service == nullptr) {
398         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr");
399         return;
400     }
401     sptr<IAmsMgr> amsService = service->GetAmsMgr();
402     if (amsService == nullptr) {
403         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr");
404         return;
405     }
406     amsService->NotifyUninstallOrUpgradeAppEnd(uid);
407 }
408 
KillApplicationSelf(const bool clearPageStack,const std::string & reason)409 AppMgrResultCode AppMgrClient::KillApplicationSelf(const bool clearPageStack, const std::string& reason)
410 {
411     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
412     if (service != nullptr) {
413         sptr<IAmsMgr> amsService = service->GetAmsMgr();
414         if (amsService != nullptr) {
415             int32_t result = amsService->KillApplicationSelf(clearPageStack, reason);
416             if (result == ERR_OK) {
417                 return AppMgrResultCode::RESULT_OK;
418             }
419             return AppMgrResultCode::ERROR_KILL_APPLICATION;
420         }
421     }
422     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
423 }
424 
UpdateProcessMemoryState(const std::vector<AppExecFwk::ProcessMemoryState> & procMemState)425 int32_t AppMgrClient::UpdateProcessMemoryState(const std::vector<AppExecFwk::ProcessMemoryState> &procMemState)
426 {
427     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
428     if (service == nullptr) {
429         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
430         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
431     }
432     return service->UpdateProcessMemoryState(procMemState);
433 }
434 
ClearUpApplicationData(const std::string & bundleName,int32_t appCloneIndex,int32_t userId)435 AppMgrResultCode AppMgrClient::ClearUpApplicationData(const std::string &bundleName, int32_t appCloneIndex,
436     int32_t userId)
437 {
438     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
439     if (service != nullptr) {
440         int32_t result = service->ClearUpApplicationData(bundleName, appCloneIndex, userId);
441         if (result == ERR_OK) {
442             return AppMgrResultCode::RESULT_OK;
443         }
444         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
445     }
446     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
447 }
448 
ClearUpApplicationDataBySelf(int32_t userId)449 AppMgrResultCode AppMgrClient::ClearUpApplicationDataBySelf(int32_t userId)
450 {
451     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
452     if (service != nullptr) {
453         int32_t result = service->ClearUpApplicationDataBySelf(userId);
454         if (result == ERR_OK) {
455             return AppMgrResultCode::RESULT_OK;
456         }
457         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
458     }
459     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
460 }
461 
GetAllRunningProcesses(std::vector<RunningProcessInfo> & info)462 AppMgrResultCode AppMgrClient::GetAllRunningProcesses(std::vector<RunningProcessInfo> &info)
463 {
464     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
465     if (service != nullptr) {
466         int32_t result = service->GetAllRunningProcesses(info);
467         if (result == ERR_OK) {
468             return AppMgrResultCode::RESULT_OK;
469         }
470         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
471     }
472     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
473 }
474 
GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> & info,int32_t userId)475 AppMgrResultCode AppMgrClient::GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId)
476 {
477     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
478     if (service != nullptr) {
479         int32_t result = service->GetProcessRunningInfosByUserId(info, userId);
480         if (result == ERR_OK) {
481             return AppMgrResultCode::RESULT_OK;
482         }
483         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
484     }
485     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
486 }
487 
GetProcessRunningInformation(AppExecFwk::RunningProcessInfo & info)488 AppMgrResultCode AppMgrClient::GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info)
489 {
490     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
491     if (service != nullptr) {
492         int32_t result = service->GetProcessRunningInformation(info);
493         if (result == ERR_OK) {
494             return AppMgrResultCode::RESULT_OK;
495         }
496         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
497     }
498     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
499 }
500 
GetAllRunningInstanceKeysBySelf(std::vector<std::string> & instanceKeys)501 AppMgrResultCode AppMgrClient::GetAllRunningInstanceKeysBySelf(std::vector<std::string> &instanceKeys)
502 {
503     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
504     if (service != nullptr) {
505         int32_t result = service->GetAllRunningInstanceKeysBySelf(instanceKeys);
506         if (result == ERR_OK) {
507             return AppMgrResultCode::RESULT_OK;
508         }
509         TAG_LOGE(AAFwkTag::APPMGR, "GetAllRunningInstanceKeysBySelf returns result=%{public}d", result);
510         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
511     }
512     TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
513     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
514 }
515 
GetAllRunningInstanceKeysByBundleName(const std::string & bundleName,std::vector<std::string> & instanceKeys,int32_t userId)516 AppMgrResultCode AppMgrClient::GetAllRunningInstanceKeysByBundleName(const std::string &bundleName,
517     std::vector<std::string> &instanceKeys, int32_t userId)
518 {
519     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
520     if (service != nullptr) {
521         int32_t result = service->GetAllRunningInstanceKeysByBundleName(bundleName, instanceKeys, userId);
522         if (result == ERR_OK) {
523             return AppMgrResultCode::RESULT_OK;
524         }
525         TAG_LOGE(AAFwkTag::APPMGR, "GetAllRunningInstanceKeysByBundleName returns result=%{public}d", result);
526         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
527     }
528     TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
529     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
530 }
531 
GetAllRenderProcesses(std::vector<RenderProcessInfo> & info)532 AppMgrResultCode AppMgrClient::GetAllRenderProcesses(std::vector<RenderProcessInfo> &info)
533 {
534     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
535     if (service != nullptr) {
536         int32_t result = service->GetAllRenderProcesses(info);
537         if (result == ERR_OK) {
538             return AppMgrResultCode::RESULT_OK;
539         }
540         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
541     }
542     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
543 }
544 
GetAllChildrenProcesses(std::vector<ChildProcessInfo> & info)545 AppMgrResultCode AppMgrClient::GetAllChildrenProcesses(std::vector<ChildProcessInfo> &info)
546 {
547 #ifdef SUPPORT_CHILD_PROCESS
548     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
549     if (service == nullptr) {
550         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
551         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
552     }
553     int32_t result = service->GetAllChildrenProcesses(info);
554     if (result != ERR_OK) {
555         TAG_LOGE(AAFwkTag::APPMGR, "service->GetAllChildrenProcesses failed,result=%{public}d", result);
556         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
557     }
558 #endif // SUPPORT_CHILD_PROCESS
559     return AppMgrResultCode::RESULT_OK;
560 }
561 
NotifyMemoryLevel(MemoryLevel level)562 AppMgrResultCode AppMgrClient::NotifyMemoryLevel(MemoryLevel level)
563 {
564     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
565 
566     if (service == nullptr) {
567         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
568         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
569     }
570     return AppMgrResultCode(service->NotifyMemoryLevel(level));
571 }
572 
NotifyProcMemoryLevel(const std::map<pid_t,MemoryLevel> & procLevelMap) const573 AppMgrResultCode AppMgrClient::NotifyProcMemoryLevel(const std::map<pid_t, MemoryLevel> &procLevelMap) const
574 {
575     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
576 
577     if (service == nullptr) {
578         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
579         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
580     }
581     return AppMgrResultCode(service->NotifyProcMemoryLevel(procLevelMap));
582 }
583 
DumpHeapMemory(const int32_t pid,OHOS::AppExecFwk::MallocInfo & mallocInfo)584 AppMgrResultCode AppMgrClient::DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo)
585 {
586     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
587     if (service == nullptr) {
588         TAG_LOGE(AAFwkTag::APPMGR, "DumpHeapMemory: service is nullptr");
589         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
590     }
591     return AppMgrResultCode(service->DumpHeapMemory(pid, mallocInfo));
592 }
593 
DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo & info)594 AppMgrResultCode AppMgrClient::DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info)
595 {
596     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
597     if (service == nullptr) {
598         TAG_LOGE(AAFwkTag::APPMGR, "DumpJsHeapMemory: service is nullptr");
599         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
600     }
601     return AppMgrResultCode(service->DumpJsHeapMemory(info));
602 }
603 
DumpCjHeapMemory(OHOS::AppExecFwk::CjHeapDumpInfo & info)604 AppMgrResultCode AppMgrClient::DumpCjHeapMemory(OHOS::AppExecFwk::CjHeapDumpInfo &info)
605 {
606     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
607     if (service == nullptr) {
608         TAG_LOGE(AAFwkTag::APPMGR, "DumpCjHeapMemory: service is nullptr");
609         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
610     }
611     int32_t result = service->DumpCjHeapMemory(info);
612     switch (result) {
613         case static_cast<int32_t>(AppMgrResultCode::RESULT_OK):
614             return AppMgrResultCode::RESULT_OK;
615         case static_cast<int32_t>(AppMgrResultCode::ERROR_SERVICE_NOT_READY):
616             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
617         case static_cast<int32_t>(AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED):
618             return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
619         case static_cast<int32_t>(AppMgrResultCode::ERROR_KILL_APPLICATION):
620             return AppMgrResultCode::ERROR_KILL_APPLICATION;
621         case static_cast<int32_t>(AppMgrResultCode::ERROR_KILL_PROCESSES_BY_PIDS):
622             return AppMgrResultCode::ERROR_KILL_PROCESSES_BY_PIDS;
623         default:
624             return static_cast<AppMgrResultCode>(result);
625     }
626 }
627 
GetConfiguration(Configuration & config)628 AppMgrResultCode AppMgrClient::GetConfiguration(Configuration& config)
629 {
630     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
631     if (service != nullptr) {
632         int32_t result = service->GetConfiguration(config);
633         if (result == ERR_OK) {
634             return AppMgrResultCode::RESULT_OK;
635         }
636         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
637     }
638     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
639 }
640 
ConnectAppMgrService()641 AppMgrResultCode AppMgrClient::ConnectAppMgrService()
642 {
643     if (mgrHolder_) {
644         return mgrHolder_->ConnectAppMgrService();
645     }
646     return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
647 }
648 
IsProcessContainsOnlyUIAbility(const pid_t pid)649 bool AppMgrClient::IsProcessContainsOnlyUIAbility(const pid_t pid)
650 {
651     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
652     if (service != nullptr) {
653         sptr<IAmsMgr> amsService = service->GetAmsMgr();
654         if (amsService != nullptr) {
655             return amsService->IsProcessContainsOnlyUIAbility(pid);
656         }
657     }
658     return false;
659 }
660 
SetServiceManager(std::unique_ptr<AppServiceManager> serviceMgr)661 void AppMgrClient::SetServiceManager(std::unique_ptr<AppServiceManager> serviceMgr)
662 {
663     if (!mgrHolder_) {
664         mgrHolder_ = std::make_shared<AppMgrRemoteHolder>();
665     }
666     mgrHolder_->SetServiceManager(std::move(serviceMgr));
667 }
668 
AbilityAttachTimeOut(const sptr<IRemoteObject> & token)669 void AppMgrClient::AbilityAttachTimeOut(const sptr<IRemoteObject> &token)
670 {
671     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
672     if (service == nullptr) {
673         return;
674     }
675     sptr<IAmsMgr> amsService = service->GetAmsMgr();
676     if (amsService == nullptr) {
677         return;
678     }
679     amsService->AbilityAttachTimeOut(token);
680 }
681 
PrepareTerminate(const sptr<IRemoteObject> & token,bool clearMissionFlag)682 void AppMgrClient::PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag)
683 {
684     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
685     if (service == nullptr) {
686         return;
687     }
688     sptr<IAmsMgr> amsService = service->GetAmsMgr();
689     if (amsService == nullptr) {
690         return;
691     }
692     amsService->PrepareTerminate(token, clearMissionFlag);
693 }
694 
GetRunningProcessInfoByToken(const sptr<IRemoteObject> & token,AppExecFwk::RunningProcessInfo & info)695 void AppMgrClient::GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info)
696 {
697     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
698     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
699     if (service != nullptr) {
700         sptr<IAmsMgr> amsService = service->GetAmsMgr();
701         if (amsService != nullptr) {
702             amsService->GetRunningProcessInfoByToken(token, info);
703         }
704     }
705 }
706 
GetRunningProcessInfoByPid(const pid_t pid,OHOS::AppExecFwk::RunningProcessInfo & info) const707 int32_t AppMgrClient::GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info) const
708 {
709     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
710     if (service == nullptr) {
711         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
712         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
713     }
714     return service->GetRunningProcessInfoByPid(pid, info);
715 }
716 
GetRunningProcessInfoByChildProcessPid(const pid_t childPid,OHOS::AppExecFwk::RunningProcessInfo & info) const717 int32_t AppMgrClient::GetRunningProcessInfoByChildProcessPid(const pid_t childPid,
718     OHOS::AppExecFwk::RunningProcessInfo &info) const
719 {
720     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
721     if (service == nullptr) {
722         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
723         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
724     }
725     return service->GetRunningProcessInfoByChildProcessPid(childPid, info);
726 }
727 
SetAbilityForegroundingFlagToAppRecord(const pid_t pid) const728 void AppMgrClient::SetAbilityForegroundingFlagToAppRecord(const pid_t pid) const
729 {
730     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
731     if (service != nullptr) {
732         sptr<IAmsMgr> amsService = service->GetAmsMgr();
733         if (amsService != nullptr) {
734             amsService->SetAbilityForegroundingFlagToAppRecord(pid);
735         }
736     }
737 }
738 
AddAbilityStageDone(const int32_t recordId)739 void AppMgrClient::AddAbilityStageDone(const int32_t recordId)
740 {
741     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
742     if (service == nullptr) {
743         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
744         return;
745     }
746 
747     service->AddAbilityStageDone(recordId);
748 }
749 
StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> & bundleInfos)750 void AppMgrClient::StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos)
751 {
752     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
753     if (service == nullptr) {
754         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
755         return;
756     }
757 
758     service->StartupResidentProcess(bundleInfos);
759 }
760 
StartUserTestProcess(const AAFwk::Want & want,const sptr<IRemoteObject> & observer,const BundleInfo & bundleInfo,int32_t userId)761 int AppMgrClient::StartUserTestProcess(
762     const AAFwk::Want &want, const sptr<IRemoteObject> &observer, const BundleInfo &bundleInfo, int32_t userId)
763 {
764     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
765     if (service == nullptr) {
766         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
767         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
768     }
769     return service->StartUserTestProcess(want, observer, bundleInfo, userId);
770 }
771 
FinishUserTest(const std::string & msg,const int64_t & resultCode,const std::string & bundleName)772 int AppMgrClient::FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName)
773 {
774     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
775     if (service == nullptr) {
776         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
777         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
778     }
779     return service->FinishUserTest(msg, resultCode, bundleName);
780 }
781 
StartSpecifiedAbility(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId)782 void AppMgrClient::StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
783     int32_t requestId)
784 {
785     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
786     if (service == nullptr) {
787         return;
788     }
789     sptr<IAmsMgr> amsService = service->GetAmsMgr();
790     if (amsService == nullptr) {
791         return;
792     }
793     amsService->StartSpecifiedAbility(want, abilityInfo, requestId);
794 }
795 
PrepareTerminateApp(const pid_t pid,const std::string & moduleName)796 void AppMgrClient::PrepareTerminateApp(const pid_t pid, const std::string &moduleName)
797 {
798     TAG_LOGD(AAFwkTag::APPMGR, "called");
799     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
800     if (service == nullptr) {
801         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
802         return;
803     }
804     sptr<IAmsMgr> amsService = service->GetAmsMgr();
805     if (amsService == nullptr) {
806         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr");
807         return;
808     }
809     amsService->PrepareTerminateApp(pid, moduleName);
810 }
811 
SetKeepAliveEnableState(const std::string & bundleName,bool enable,int32_t uid)812 void AppMgrClient::SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid)
813 {
814     if (!IsAmsServiceReady()) {
815         return;
816     }
817     amsService_->SetKeepAliveEnableState(bundleName, enable, uid);
818 }
819 
SetKeepAliveDkv(const std::string & bundleName,bool enable,int32_t uid)820 void AppMgrClient::SetKeepAliveDkv(const std::string &bundleName, bool enable, int32_t uid)
821 {
822     if (!IsAmsServiceReady()) {
823         return;
824     }
825     amsService_->SetKeepAliveDkv(bundleName, enable, uid);
826 }
827 
SetKeepAliveAppService(const std::string & bundleName,bool enable,int32_t uid)828 void AppMgrClient::SetKeepAliveAppService(const std::string &bundleName, bool enable, int32_t uid)
829 {
830     if (!IsAmsServiceReady()) {
831         return;
832     }
833     amsService_->SetKeepAliveAppService(bundleName, enable, uid);
834 }
835 
StartSpecifiedProcess(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId,const std::string & customProcess)836 void AppMgrClient::StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
837     int32_t requestId, const std::string &customProcess)
838 {
839     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
840     if (service == nullptr) {
841         return;
842     }
843     sptr<IAmsMgr> amsService = service->GetAmsMgr();
844     if (amsService == nullptr) {
845         return;
846     }
847     amsService->StartSpecifiedProcess(want, abilityInfo, requestId, customProcess);
848 }
849 
RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> & response)850 void AppMgrClient::RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response)
851 {
852     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
853     if (service == nullptr) {
854         return;
855     }
856     sptr<IAmsMgr> amsService = service->GetAmsMgr();
857     if (amsService == nullptr) {
858         return;
859     }
860     amsService->RegisterStartSpecifiedAbilityResponse(response);
861 }
862 
ScheduleAcceptWantDone(const int32_t recordId,const AAFwk::Want & want,const std::string & flag)863 void AppMgrClient::ScheduleAcceptWantDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag)
864 {
865     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
866     if (service == nullptr) {
867         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
868         return;
869     }
870 
871     service->ScheduleAcceptWantDone(recordId, want, flag);
872 }
873 
UpdateConfiguration(const Configuration & config,const int32_t userId)874 AppMgrResultCode AppMgrClient::UpdateConfiguration(const Configuration &config, const int32_t userId)
875 {
876     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
877     if (service == nullptr) {
878         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
879     }
880     service->UpdateConfiguration(config, userId);
881     return AppMgrResultCode::RESULT_OK;
882 }
883 
UpdateConfigurationByBundleName(const Configuration & config,const std::string & name,int32_t appIndex)884 AppMgrResultCode AppMgrClient::UpdateConfigurationByBundleName(const Configuration &config, const std::string &name,
885     int32_t appIndex)
886 {
887     if (!mgrHolder_) {
888         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
889     }
890     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
891     if (service == nullptr) {
892         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
893     }
894     service->UpdateConfigurationByBundleName(config, name, appIndex);
895     return AppMgrResultCode::RESULT_OK;
896 }
897 
RegisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)898 AppMgrResultCode AppMgrClient::RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
899 {
900     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
901     if (service != nullptr) {
902         int32_t result = service->RegisterConfigurationObserver(observer);
903         if (result == ERR_OK) {
904             return AppMgrResultCode::RESULT_OK;
905         }
906         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
907     }
908     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
909 }
910 
UnregisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)911 AppMgrResultCode AppMgrClient::UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
912 {
913     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
914     if (service != nullptr) {
915         int32_t result = service->UnregisterConfigurationObserver(observer);
916         if (result == ERR_OK) {
917             return AppMgrResultCode::RESULT_OK;
918         }
919         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
920     }
921     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
922 }
923 
GetAbilityRecordsByProcessID(const int pid,std::vector<sptr<IRemoteObject>> & tokens)924 int AppMgrClient::GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens)
925 {
926     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
927     if (service == nullptr) {
928         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
929         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
930     }
931 
932     return service->GetAbilityRecordsByProcessID(pid, tokens);
933 }
934 
GetApplicationInfoByProcessID(const int pid,AppExecFwk::ApplicationInfo & application,bool & debug)935 int AppMgrClient::GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug)
936 {
937     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
938     if (service == nullptr) {
939         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
940         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
941     }
942     sptr<IAmsMgr> amsService = service->GetAmsMgr();
943     if (amsService == nullptr) {
944         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr");
945         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
946     }
947     return amsService->GetApplicationInfoByProcessID(pid, application, debug);
948 }
949 
NotifyAppMgrRecordExitReason(int32_t pid,int32_t reason,const std::string & exitMsg)950 int32_t AppMgrClient::NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg)
951 {
952     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
953     if (service == nullptr) {
954         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
955         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
956     }
957     sptr<IAmsMgr> amsService = service->GetAmsMgr();
958     if (amsService == nullptr) {
959         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr");
960         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
961     }
962     return amsService->NotifyAppMgrRecordExitReason(pid, reason, exitMsg);
963 }
964 
StartNativeProcessForDebugger(const AAFwk::Want & want)965 int32_t AppMgrClient::StartNativeProcessForDebugger(const AAFwk::Want &want)
966 {
967     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
968     if (service == nullptr) {
969         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
970         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
971     }
972     return service->StartNativeProcessForDebugger(want);
973 }
974 
PreStartNWebSpawnProcess()975 int AppMgrClient::PreStartNWebSpawnProcess()
976 {
977     TAG_LOGI(AAFwkTag::APPMGR, "PreStartNWebSpawnProcess");
978 
979     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
980     if (service != nullptr) {
981         return service->PreStartNWebSpawnProcess();
982     }
983     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
984 }
985 
StartRenderProcess(const std::string & renderParam,int32_t ipcFd,int32_t sharedFd,int32_t crashFd,pid_t & renderPid,bool isGPU)986 int AppMgrClient::StartRenderProcess(const std::string &renderParam,
987                                      int32_t ipcFd, int32_t sharedFd,
988                                      int32_t crashFd, pid_t &renderPid, bool isGPU)
989 {
990     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
991     if (service != nullptr) {
992         return service->StartRenderProcess(renderParam, ipcFd, sharedFd, crashFd,
993                                            renderPid, isGPU);
994     }
995     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
996 }
997 
AttachRenderProcess(const sptr<IRenderScheduler> & renderScheduler)998 void AppMgrClient::AttachRenderProcess(const sptr<IRenderScheduler> &renderScheduler)
999 {
1000     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1001     if (!renderScheduler) {
1002         TAG_LOGI(AAFwkTag::APPMGR, "renderScheduler is nullptr");
1003         return;
1004     }
1005 
1006     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1007     if (service != nullptr) {
1008         TAG_LOGI(AAFwkTag::APPMGR, "AttachRenderProcess");
1009         service->AttachRenderProcess(renderScheduler->AsObject());
1010     }
1011 }
1012 
GetRenderProcessTerminationStatus(pid_t renderPid,int & status)1013 int AppMgrClient::GetRenderProcessTerminationStatus(pid_t renderPid, int &status)
1014 {
1015     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1016     if (service != nullptr) {
1017         return service->GetRenderProcessTerminationStatus(renderPid, status);
1018     }
1019     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1020 }
1021 
GetRemoteObject()1022 sptr<IRemoteObject> AppMgrClient::GetRemoteObject()
1023 {
1024     return mgrHolder_->GetRemoteObject();
1025 }
1026 
SetCurrentUserId(const int32_t userId)1027 void AppMgrClient::SetCurrentUserId(const int32_t userId)
1028 {
1029     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1030     if (service == nullptr) {
1031         return;
1032     }
1033     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1034     if (amsService == nullptr) {
1035         return;
1036     }
1037     amsService->SetCurrentUserId(userId);
1038 }
1039 
SetEnableStartProcessFlagByUserId(int32_t userId,bool enableStartProcess)1040 void AppMgrClient::SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess)
1041 {
1042     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1043     if (service == nullptr) {
1044         return;
1045     }
1046     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1047     if (amsService == nullptr) {
1048         return;
1049     }
1050     amsService->SetEnableStartProcessFlagByUserId(userId, enableStartProcess);
1051 }
1052 
GetBundleNameByPid(const int pid,std::string & bundleName,int32_t & uid)1053 int32_t AppMgrClient::GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid)
1054 {
1055     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1056     if (service == nullptr) {
1057         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1058     }
1059 
1060     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1061     if (amsService != nullptr) {
1062         return amsService->GetBundleNameByPid(pid, bundleName, uid);
1063     }
1064     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1065 }
1066 
NotifyAppFault(const FaultData & faultData)1067 int32_t AppMgrClient::NotifyAppFault(const FaultData &faultData)
1068 {
1069     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1070     if (service == nullptr) {
1071         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1072     }
1073     return service->NotifyAppFault(faultData);
1074 }
1075 
NotifyAppFaultBySA(const AppFaultDataBySA & faultData)1076 int32_t AppMgrClient::NotifyAppFaultBySA(const AppFaultDataBySA &faultData)
1077 {
1078     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1079     if (service == nullptr) {
1080         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1081     }
1082     return service->NotifyAppFaultBySA(faultData);
1083 }
1084 
SetAppFreezeFilter(int32_t pid)1085 bool AppMgrClient::SetAppFreezeFilter(int32_t pid)
1086 {
1087     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1088     if (service == nullptr) {
1089         return false;
1090     }
1091     return service->SetAppFreezeFilter(pid);
1092 }
1093 
ChangeAppGcState(pid_t pid,int32_t state,uint64_t tid)1094 int32_t AppMgrClient::ChangeAppGcState(pid_t pid, int32_t state, uint64_t tid)
1095 {
1096     TAG_LOGD(AAFwkTag::APPMGR, "tid is %{private}" PRIu64, tid);
1097     if (mgrHolder_ == nullptr) {
1098         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1099     }
1100     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1101     if (service == nullptr) {
1102         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1103     }
1104     return service->ChangeAppGcState(pid, state, tid);
1105 }
1106 
RegisterAppDebugListener(const sptr<IAppDebugListener> & listener)1107 int32_t AppMgrClient::RegisterAppDebugListener(const sptr<IAppDebugListener> &listener)
1108 {
1109     if (!IsAmsServiceReady()) {
1110         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1111     }
1112     return amsService_->RegisterAppDebugListener(listener);
1113 }
1114 
UnregisterAppDebugListener(const sptr<IAppDebugListener> & listener)1115 int32_t AppMgrClient::UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener)
1116 {
1117     if (!IsAmsServiceReady()) {
1118         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1119     }
1120     return amsService_->UnregisterAppDebugListener(listener);
1121 }
1122 
AttachAppDebug(const std::string & bundleName,bool isDebugFromLocal)1123 int32_t AppMgrClient::AttachAppDebug(const std::string &bundleName, bool isDebugFromLocal)
1124 {
1125     if (!IsAmsServiceReady()) {
1126         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1127     }
1128     return amsService_->AttachAppDebug(bundleName, isDebugFromLocal);
1129 }
1130 
DetachAppDebug(const std::string & bundleName)1131 int32_t AppMgrClient::DetachAppDebug(const std::string &bundleName)
1132 {
1133     if (!IsAmsServiceReady()) {
1134         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1135     }
1136     return amsService_->DetachAppDebug(bundleName);
1137 }
1138 
SetAppWaitingDebug(const std::string & bundleName,bool isPersist)1139 int32_t AppMgrClient::SetAppWaitingDebug(const std::string &bundleName, bool isPersist)
1140 {
1141     if (!IsAmsServiceReady()) {
1142         TAG_LOGE(AAFwkTag::APPMGR, "App manager service is not ready.");
1143         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1144     }
1145     return amsService_->SetAppWaitingDebug(bundleName, isPersist);
1146 }
1147 
CancelAppWaitingDebug()1148 int32_t AppMgrClient::CancelAppWaitingDebug()
1149 {
1150     if (!IsAmsServiceReady()) {
1151         TAG_LOGE(AAFwkTag::APPMGR, "App manager service is not ready.");
1152         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1153     }
1154     return amsService_->CancelAppWaitingDebug();
1155 }
1156 
GetWaitingDebugApp(std::vector<std::string> & debugInfoList)1157 int32_t AppMgrClient::GetWaitingDebugApp(std::vector<std::string> &debugInfoList)
1158 {
1159     if (!IsAmsServiceReady()) {
1160         TAG_LOGE(AAFwkTag::APPMGR, "App manager service is not ready.");
1161         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1162     }
1163     return amsService_->GetWaitingDebugApp(debugInfoList);
1164 }
1165 
IsWaitingDebugApp(const std::string & bundleName)1166 bool AppMgrClient::IsWaitingDebugApp(const std::string &bundleName)
1167 {
1168     if (!IsAmsServiceReady()) {
1169         TAG_LOGE(AAFwkTag::APPMGR, "App manager service is not ready.");
1170         return false;
1171     }
1172     return amsService_->IsWaitingDebugApp(bundleName);
1173 }
1174 
ClearNonPersistWaitingDebugFlag()1175 void AppMgrClient::ClearNonPersistWaitingDebugFlag()
1176 {
1177     if (!IsAmsServiceReady()) {
1178         TAG_LOGE(AAFwkTag::APPMGR, "App manager service is not ready.");
1179         return;
1180     }
1181     amsService_->ClearNonPersistWaitingDebugFlag();
1182 }
1183 
RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> & response)1184 int32_t AppMgrClient::RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response)
1185 {
1186     if (!IsAmsServiceReady()) {
1187         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1188     }
1189     return amsService_->RegisterAbilityDebugResponse(response);
1190 }
1191 
IsAttachDebug(const std::string & bundleName)1192 bool AppMgrClient::IsAttachDebug(const std::string &bundleName)
1193 {
1194     if (!IsAmsServiceReady()) {
1195         return false;
1196     }
1197     return amsService_->IsAttachDebug(bundleName);
1198 }
1199 
IsAmsServiceReady()1200 bool AppMgrClient::IsAmsServiceReady()
1201 {
1202     if (mgrHolder_ == nullptr) {
1203         TAG_LOGE(AAFwkTag::APPMGR, "mgrHolder_ is nullptr.");
1204         return false;
1205     }
1206 
1207     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1208     if (service == nullptr) {
1209         TAG_LOGE(AAFwkTag::APPMGR, "App manager service is nullptr.");
1210         return false;
1211     }
1212 
1213     amsService_ = service->GetAmsMgr();
1214     if (amsService_ == nullptr) {
1215         TAG_LOGE(AAFwkTag::APPMGR, "amsService_ is nullptr.");
1216         return false;
1217     }
1218     return true;
1219 }
1220 
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer,const std::vector<std::string> & bundleNameList)1221 int32_t AppMgrClient::RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer,
1222     const std::vector<std::string> &bundleNameList)
1223 {
1224     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1225     if (service == nullptr) {
1226         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1227     }
1228     return service->RegisterApplicationStateObserver(observer, bundleNameList);
1229 }
1230 
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)1231 int32_t AppMgrClient::UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
1232 {
1233     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1234     if (service == nullptr) {
1235         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1236     }
1237     return service->UnregisterApplicationStateObserver(observer);
1238 }
1239 
RegisterNativeChildExitNotify(sptr<INativeChildNotify> notify)1240 int32_t AppMgrClient::RegisterNativeChildExitNotify(sptr<INativeChildNotify> notify)
1241 {
1242     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1243     if (service == nullptr) {
1244         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1245     }
1246     return service->RegisterNativeChildExitNotify(notify);
1247 }
1248 
UnregisterNativeChildExitNotify(sptr<INativeChildNotify> notify)1249 int32_t AppMgrClient::UnregisterNativeChildExitNotify(sptr<INativeChildNotify> notify)
1250 {
1251     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1252     if (service == nullptr) {
1253         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1254     }
1255     return service->UnregisterNativeChildExitNotify(notify);
1256 }
1257 
NotifyPageShow(const sptr<IRemoteObject> & token,const PageStateData & pageStateData)1258 int32_t AppMgrClient::NotifyPageShow(const sptr<IRemoteObject> &token, const PageStateData &pageStateData)
1259 {
1260     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1261     if (service == nullptr) {
1262         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1263     }
1264     return service->NotifyPageShow(token, pageStateData);
1265 }
1266 
NotifyPageHide(const sptr<IRemoteObject> & token,const PageStateData & pageStateData)1267 int32_t AppMgrClient::NotifyPageHide(const sptr<IRemoteObject> &token, const PageStateData &pageStateData)
1268 {
1269     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1270     if (service == nullptr) {
1271         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1272     }
1273     return service->NotifyPageHide(token, pageStateData);
1274 }
1275 
RegisterAppRunningStatusListener(const sptr<IRemoteObject> & listener)1276 int32_t AppMgrClient::RegisterAppRunningStatusListener(const sptr<IRemoteObject> &listener)
1277 {
1278     if (listener == nullptr) {
1279         TAG_LOGE(AAFwkTag::APPMGR, "Listener is nullptr.");
1280         return ERR_INVALID_DATA;
1281     }
1282 
1283     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1284     if (service == nullptr) {
1285         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1286         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1287     }
1288     return service->RegisterAppRunningStatusListener(listener);
1289 }
1290 
UnregisterAppRunningStatusListener(const sptr<IRemoteObject> & listener)1291 int32_t AppMgrClient::UnregisterAppRunningStatusListener(const sptr<IRemoteObject> &listener)
1292 {
1293     if (listener == nullptr) {
1294         TAG_LOGE(AAFwkTag::APPMGR, "Listener is nullptr.");
1295         return ERR_INVALID_DATA;
1296     }
1297 
1298     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1299     if (service == nullptr) {
1300         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1301         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1302     }
1303     return service->UnregisterAppRunningStatusListener(listener);
1304 }
1305 
ClearProcessByToken(sptr<IRemoteObject> token) const1306 void AppMgrClient::ClearProcessByToken(sptr<IRemoteObject> token) const
1307 {
1308     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1309     if (service == nullptr) {
1310         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1311         return;
1312     }
1313     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1314     if (amsService == nullptr) {
1315         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr.");
1316         return;
1317     }
1318     amsService->ClearProcessByToken(token);
1319 }
1320 
IsFinalAppProcess()1321 bool AppMgrClient::IsFinalAppProcess()
1322 {
1323     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1324     if (service == nullptr) {
1325         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1326         return false;
1327     }
1328     return service->IsFinalAppProcess();
1329 }
1330 
RegisterRenderStateObserver(const sptr<IRenderStateObserver> & observer)1331 int32_t AppMgrClient::RegisterRenderStateObserver(const sptr<IRenderStateObserver> &observer)
1332 {
1333     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1334     if (service == nullptr) {
1335         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1336     }
1337     return service->RegisterRenderStateObserver(observer);
1338 }
1339 
UnregisterRenderStateObserver(const sptr<IRenderStateObserver> & observer)1340 int32_t AppMgrClient::UnregisterRenderStateObserver(const sptr<IRenderStateObserver> &observer)
1341 {
1342     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1343     if (service == nullptr) {
1344         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1345     }
1346     return service->UnregisterRenderStateObserver(observer);
1347 }
1348 
UpdateRenderState(pid_t renderPid,int32_t state)1349 int32_t AppMgrClient::UpdateRenderState(pid_t renderPid, int32_t state)
1350 {
1351     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1352     if (service == nullptr) {
1353         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1354     }
1355     return service->UpdateRenderState(renderPid, state);
1356 }
1357 
GetAppRunningUniqueIdByPid(pid_t pid,std::string & appRunningUniqueId)1358 int32_t AppMgrClient::GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId)
1359 {
1360     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1361     if (service == nullptr) {
1362         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1363         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1364     }
1365     return service->GetAppRunningUniqueIdByPid(pid, appRunningUniqueId);
1366 }
1367 
GetAllUIExtensionRootHostPid(pid_t pid,std::vector<pid_t> & hostPids)1368 int32_t AppMgrClient::GetAllUIExtensionRootHostPid(pid_t pid, std::vector<pid_t> &hostPids)
1369 {
1370     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1371     if (service == nullptr) {
1372         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1373         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1374     }
1375     return service->GetAllUIExtensionRootHostPid(pid, hostPids);
1376 }
1377 
GetAllUIExtensionProviderPid(pid_t hostPid,std::vector<pid_t> & providerPids)1378 int32_t AppMgrClient::GetAllUIExtensionProviderPid(pid_t hostPid, std::vector<pid_t> &providerPids)
1379 {
1380     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1381     if (service == nullptr) {
1382         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1383         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1384     }
1385     return service->GetAllUIExtensionProviderPid(hostPid, providerPids);
1386 }
1387 
NotifyMemorySizeStateChanged(int32_t memorySizeState)1388 int32_t AppMgrClient::NotifyMemorySizeStateChanged(int32_t memorySizeState)
1389 {
1390     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1391     if (service == nullptr) {
1392         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1393         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1394     }
1395     return service->NotifyMemorySizeStateChanged(memorySizeState);
1396 }
1397 
IsMemorySizeSufficent() const1398 bool AppMgrClient::IsMemorySizeSufficent() const
1399 {
1400     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1401     if (service == nullptr) {
1402         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1403         return true;
1404     }
1405     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1406     if (amsService == nullptr) {
1407         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr.");
1408         return true;
1409     }
1410     return amsService->IsMemorySizeSufficent();
1411 }
1412 
IsNoRequireBigMemory() const1413 bool AppMgrClient::IsNoRequireBigMemory() const
1414 {
1415     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1416     if (service == nullptr) {
1417         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1418         return true;
1419     }
1420     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1421     if (amsService == nullptr) {
1422         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr.");
1423         return true;
1424     }
1425     return amsService->IsNoRequireBigMemory();
1426 }
1427 
PreloadApplication(const std::string & bundleName,int32_t userId,AppExecFwk::PreloadMode preloadMode,int32_t appIndex)1428 int32_t AppMgrClient::PreloadApplication(const std::string &bundleName, int32_t userId,
1429     AppExecFwk::PreloadMode preloadMode, int32_t appIndex)
1430 {
1431     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1432     if (service == nullptr) {
1433         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1434     }
1435     return service->PreloadApplication(bundleName, userId, preloadMode, appIndex);
1436 }
1437 
SetSupportedProcessCacheSelf(bool isSupport)1438 int32_t AppMgrClient::SetSupportedProcessCacheSelf(bool isSupport)
1439 {
1440     TAG_LOGI(AAFwkTag::APPMGR, "Called");
1441     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1442     if (service == nullptr) {
1443         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1444         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1445     }
1446     return service->SetSupportedProcessCacheSelf(isSupport);
1447 }
1448 
SetSupportedProcessCache(int32_t pid,bool isSupport)1449 int32_t AppMgrClient::SetSupportedProcessCache(int32_t pid, bool isSupport)
1450 {
1451     TAG_LOGI(AAFwkTag::APPMGR, "Called");
1452     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1453     if (service == nullptr) {
1454         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1455         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1456     }
1457     return service->SetSupportedProcessCache(pid, isSupport);
1458 }
1459 
IsProcessCacheSupported(int32_t pid,bool & isSupported)1460 int32_t AppMgrClient::IsProcessCacheSupported(int32_t pid, bool &isSupported)
1461 {
1462     TAG_LOGD(AAFwkTag::APPMGR, "IsProcessCacheSupported called");
1463     if (mgrHolder_ == nullptr) {
1464         TAG_LOGE(AAFwkTag::APPMGR, "mgrHolder_ is nullptr.");
1465         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
1466     }
1467     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1468     if (service == nullptr) {
1469         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1470         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1471     }
1472     return service->IsProcessCacheSupported(pid, isSupported);
1473 }
1474 
SetProcessCacheEnable(int32_t pid,bool enable)1475 int32_t AppMgrClient::SetProcessCacheEnable(int32_t pid, bool enable)
1476 {
1477     TAG_LOGD(AAFwkTag::APPMGR, "SetProcessCacheEnable called");
1478     if (mgrHolder_ == nullptr) {
1479         TAG_LOGE(AAFwkTag::APPMGR, "mgrHolder_ is nullptr.");
1480         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
1481     }
1482     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1483     if (service == nullptr) {
1484         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1485         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1486     }
1487     return service->SetProcessCacheEnable(pid, enable);
1488 }
1489 
SaveBrowserChannel(sptr<IRemoteObject> browser)1490 void AppMgrClient::SaveBrowserChannel(sptr<IRemoteObject> browser)
1491 {
1492     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1493     if (service == nullptr) {
1494         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1495         return;
1496     }
1497     service->SaveBrowserChannel(browser);
1498 }
1499 
CheckCallingIsUserTestMode(const pid_t pid,bool & isUserTest)1500 int32_t AppMgrClient::CheckCallingIsUserTestMode(const pid_t pid, bool &isUserTest)
1501 {
1502     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1503     if (service != nullptr) {
1504         return service->CheckCallingIsUserTestMode(pid, isUserTest);
1505     }
1506     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1507 }
1508 
AttachedToStatusBar(const sptr<IRemoteObject> & token)1509 AppMgrResultCode AppMgrClient::AttachedToStatusBar(const sptr<IRemoteObject> &token)
1510 {
1511     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1512     if (service != nullptr) {
1513         sptr<IAmsMgr> amsService = service->GetAmsMgr();
1514         if (amsService != nullptr) {
1515             amsService->AttachedToStatusBar(token);
1516             return AppMgrResultCode::RESULT_OK;
1517         }
1518     }
1519     TAG_LOGE(AAFwkTag::APPMGR, "Service is not connected.");
1520     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1521 }
1522 
NotifyProcessDependedOnWeb()1523 int32_t AppMgrClient::NotifyProcessDependedOnWeb()
1524 {
1525     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1526     if (service == nullptr) {
1527         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1528         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1529     }
1530     TAG_LOGD(AAFwkTag::APPMGR, "call");
1531     return service->NotifyProcessDependedOnWeb();
1532 }
1533 
KillProcessDependedOnWeb()1534 void AppMgrClient::KillProcessDependedOnWeb()
1535 {
1536     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1537     if (service == nullptr) {
1538         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1539         return;
1540     }
1541     TAG_LOGD(AAFwkTag::APPMGR, "call");
1542     service->KillProcessDependedOnWeb();
1543 }
1544 
BlockProcessCacheByPids(const std::vector<int32_t> & pids)1545 AppMgrResultCode AppMgrClient::BlockProcessCacheByPids(const std::vector<int32_t> &pids)
1546 {
1547     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1548     if (service != nullptr) {
1549         sptr<IAmsMgr> amsService = service->GetAmsMgr();
1550         if (amsService != nullptr) {
1551             amsService->BlockProcessCacheByPids(pids);
1552             return AppMgrResultCode::RESULT_OK;
1553         }
1554     }
1555     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1556 }
1557 
IsKilledForUpgradeWeb(const std::string & bundleName)1558 bool AppMgrClient::IsKilledForUpgradeWeb(const std::string &bundleName)
1559 {
1560     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1561     if (service == nullptr) {
1562         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1563         return false;
1564     }
1565     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1566     if (amsService == nullptr) {
1567         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr.");
1568         return false;
1569     }
1570     TAG_LOGD(AAFwkTag::APPMGR, "call");
1571     return amsService->IsKilledForUpgradeWeb(bundleName);
1572 }
1573 
CleanAbilityByUserRequest(const sptr<IRemoteObject> & token)1574 bool AppMgrClient::CleanAbilityByUserRequest(const sptr<IRemoteObject> &token)
1575 {
1576     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1577     if (service == nullptr) {
1578         TAG_LOGE(AAFwkTag::APPMGR, "get appmgrservice is nullptr.");
1579         return false;
1580     }
1581     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1582     if (amsService == nullptr) {
1583         TAG_LOGE(AAFwkTag::APPMGR, "get abilityms service is nullptr.");
1584         return false;
1585     }
1586     TAG_LOGD(AAFwkTag::APPMGR, "call");
1587     return amsService->CleanAbilityByUserRequest(token);
1588 }
1589 
IsProcessAttached(sptr<IRemoteObject> token) const1590 bool AppMgrClient::IsProcessAttached(sptr<IRemoteObject> token) const
1591 {
1592     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1593     if (service == nullptr) {
1594         return false;
1595     }
1596     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1597     if (amsService == nullptr) {
1598         return false;
1599     }
1600     return amsService->IsProcessAttached(token);
1601 }
1602 
IsCallerKilling(const std::string & callerKey) const1603 bool AppMgrClient::IsCallerKilling(const std::string& callerKey) const
1604 {
1605     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1606     if (service == nullptr) {
1607         return false;
1608     }
1609     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1610     if (amsService == nullptr) {
1611         return false;
1612     }
1613     return amsService->IsCallerKilling(callerKey);
1614 }
1615 
PreloadApplicationByPhase(const std::string & bundleName,int32_t userId,int32_t appIndex,AppExecFwk::PreloadPhase preloadPhase)1616 int32_t AppMgrClient::PreloadApplicationByPhase(const std::string &bundleName, int32_t userId, int32_t appIndex,
1617     AppExecFwk::PreloadPhase preloadPhase)
1618 {
1619     if (mgrHolder_ == nullptr) {
1620         TAG_LOGE(AAFwkTag::APPMGR, "mgrHolder is nullptr.");
1621         return ERROR_SERVICE_NOT_CONNECTED;
1622     }
1623     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1624     if (service == nullptr) {
1625         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr.");
1626         return ERROR_SERVICE_NOT_CONNECTED;
1627     }
1628     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1629     if (amsService == nullptr) {
1630         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr.");
1631         return ERROR_SERVICE_NOT_CONNECTED;
1632     }
1633     return amsService->PreloadApplicationByPhase(bundleName, userId, appIndex, preloadPhase);
1634 }
1635 
NotifyPreloadAbilityStateChanged(sptr<IRemoteObject> token,bool isPreForeground)1636 int32_t AppMgrClient::NotifyPreloadAbilityStateChanged(sptr<IRemoteObject> token, bool isPreForeground)
1637 {
1638     if (mgrHolder_ == nullptr) {
1639         TAG_LOGE(AAFwkTag::APPMGR, "mgrHolder is nullptr.");
1640         return ERROR_SERVICE_NOT_CONNECTED;
1641     }
1642     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1643     if (service == nullptr) {
1644         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr.");
1645         return ERROR_SERVICE_NOT_CONNECTED;
1646     }
1647     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1648     if (amsService == nullptr) {
1649         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr.");
1650         return ERROR_SERVICE_NOT_CONNECTED;
1651     }
1652     return amsService->NotifyPreloadAbilityStateChanged(token, isPreForeground);
1653 }
1654 
CheckPreloadAppRecordExist(const std::string & bundleName,int32_t userId,int32_t appIndex,bool & isExist)1655 int32_t AppMgrClient::CheckPreloadAppRecordExist(const std::string &bundleName, int32_t userId, int32_t appIndex,
1656     bool &isExist)
1657 {
1658     if (mgrHolder_ == nullptr) {
1659         TAG_LOGE(AAFwkTag::APPMGR, "mgrHolder is nullptr.");
1660         return ERROR_SERVICE_NOT_CONNECTED;
1661     }
1662     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1663     if (service == nullptr) {
1664         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr.");
1665         return ERROR_SERVICE_NOT_CONNECTED;
1666     }
1667     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1668     if (amsService == nullptr) {
1669         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr.");
1670         return ERROR_SERVICE_NOT_CONNECTED;
1671     }
1672     return amsService->CheckPreloadAppRecordExist(bundleName, userId, appIndex, isExist);
1673 }
1674 
IsAppRunningByBundleNameAndUserId(const std::string & bundleName,int32_t userId,bool & isRunning)1675 AppMgrResultCode AppMgrClient::IsAppRunningByBundleNameAndUserId(const std::string &bundleName, int32_t userId,
1676     bool &isRunning)
1677 {
1678     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1679     if (service != nullptr) {
1680         return AppMgrResultCode(service->IsAppRunningByBundleNameAndUserId(bundleName, userId, isRunning));
1681     }
1682     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1683 }
1684 
PromoteCurrentToCandidateMasterProcess(bool isInsertToHead)1685 int32_t AppMgrClient::PromoteCurrentToCandidateMasterProcess(bool isInsertToHead)
1686 {
1687     TAG_LOGI(AAFwkTag::APPMGR, "Called");
1688     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1689     if (service == nullptr) {
1690         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1691         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1692     }
1693     return service->PromoteCurrentToCandidateMasterProcess(isInsertToHead);
1694 }
1695 
DemoteCurrentFromCandidateMasterProcess()1696 int32_t AppMgrClient::DemoteCurrentFromCandidateMasterProcess()
1697 {
1698     TAG_LOGI(AAFwkTag::APPMGR, "Called");
1699     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1700     if (service == nullptr) {
1701         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1702         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1703     }
1704     return service->DemoteCurrentFromCandidateMasterProcess();
1705 }
1706 
QueryRunningSharedBundles(pid_t pid,std::map<std::string,uint32_t> & sharedBundles)1707 int32_t AppMgrClient::QueryRunningSharedBundles(pid_t pid, std::map<std::string, uint32_t> &sharedBundles)
1708 {
1709     if (mgrHolder_ == nullptr) {
1710         TAG_LOGE(AAFwkTag::APPMGR, "mgrHolder is nullptr.");
1711         return ERROR_SERVICE_NOT_CONNECTED;
1712     }
1713     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1714     if (service == nullptr) {
1715         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1716         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1717     }
1718     return service->QueryRunningSharedBundles(pid, sharedBundles);
1719 }
1720 
VerifyKillProcessPermission(const std::string & bundleName) const1721 int32_t AppMgrClient::VerifyKillProcessPermission(const std::string &bundleName) const
1722 {
1723     if (mgrHolder_ == nullptr) {
1724         TAG_LOGE(AAFwkTag::APPMGR, "mgrHolder is nullptr.");
1725         return ERROR_SERVICE_NOT_CONNECTED;
1726     }
1727     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1728     if (service == nullptr) {
1729         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1730         return ERROR_SERVICE_NOT_CONNECTED;
1731     }
1732     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1733     if (amsService == nullptr) {
1734         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr.");
1735         return ERROR_SERVICE_NOT_CONNECTED;
1736     }
1737     return amsService->VerifyKillProcessPermission(bundleName);
1738 }
1739 }  // namespace AppExecFwk
1740 }  // namespace OHOS
1741