• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 
38     virtual ~AppMgrRemoteHolder() = default;
39 
SetServiceManager(std::unique_ptr<AppServiceManager> serviceMgr)40     void SetServiceManager(std::unique_ptr<AppServiceManager> serviceMgr)
41     {
42         std::lock_guard<std::mutex> lock(mutex_);
43         serviceManager_ = std::move(serviceMgr);
44     }
45 
ConnectAppMgrService()46     AppMgrResultCode ConnectAppMgrService()
47     {
48         std::lock_guard<std::mutex> lock(mutex_);
49         return ConnectAppMgrServiceInner();
50     }
51 
GetRemoteObject()52     sptr<IRemoteObject> GetRemoteObject()
53     {
54         std::lock_guard<std::mutex> lock(mutex_);
55         if (!remote_) {
56             (void) ConnectAppMgrServiceInner();
57         }
58         return remote_;
59     }
60 
61 private:
HandleRemoteDied(const wptr<IRemoteObject> & remote)62     void HandleRemoteDied(const wptr<IRemoteObject>& remote)
63     {
64         std::lock_guard<std::mutex> lock(mutex_);
65         if (!remote_) {
66             return;
67         }
68 
69         if (remote_ == remote.promote()) {
70             remote_->RemoveDeathRecipient(deathRecipient_);
71             remote_ = nullptr;
72         }
73     }
74 
ConnectAppMgrServiceInner()75     AppMgrResultCode ConnectAppMgrServiceInner()
76     {
77         if (!serviceManager_) {
78             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
79         }
80         remote_ = serviceManager_->GetAppMgrService();
81         if (!remote_) {
82             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
83         }
84 
85         auto me = shared_from_this();
86         deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new AppMgrDeathRecipient(me));
87         if (deathRecipient_ == nullptr) {
88             TAG_LOGE(AAFwkTag::APPMGR, "create AppMgrDeathRecipient failed");
89             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
90         }
91         if ((remote_->IsProxyObject()) && (!remote_->AddDeathRecipient(deathRecipient_))) {
92             TAG_LOGE(AAFwkTag::APPMGR, "AddDeathRecipient to AppMs failed");
93             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
94         }
95 
96         return AppMgrResultCode::RESULT_OK;
97     }
98 
99     class AppMgrDeathRecipient : public IRemoteObject::DeathRecipient {
100     public:
AppMgrDeathRecipient(const std::shared_ptr<AppMgrRemoteHolder> & holder)101         explicit AppMgrDeathRecipient(const std::shared_ptr<AppMgrRemoteHolder>& holder) : owner_(holder) {}
102 
103         virtual ~AppMgrDeathRecipient() = default;
104 
OnRemoteDied(const wptr<IRemoteObject> & remote)105         void OnRemoteDied(const wptr<IRemoteObject>& remote) override
106         {
107             std::shared_ptr<AppMgrRemoteHolder> holder = owner_.lock();
108             if (holder) {
109                 holder->HandleRemoteDied(remote);
110             }
111         }
112 
113     private:
114         std::weak_ptr<AppMgrRemoteHolder> owner_;
115     };
116 
117 private:
118     std::unique_ptr<AppServiceManager> serviceManager_;
119     sptr<IRemoteObject> remote_;
120     std::mutex mutex_;
121     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
122 };
123 
AppMgrClient()124 AppMgrClient::AppMgrClient()
125 {
126     SetServiceManager(std::make_unique<AppServiceManager>());
127 }
128 
~AppMgrClient()129 AppMgrClient::~AppMgrClient()
130 {}
131 
LoadAbility(const AbilityInfo & abilityInfo,const ApplicationInfo & appInfo,const AAFwk::Want & want,AbilityRuntime::LoadParam loadParam)132 AppMgrResultCode AppMgrClient::LoadAbility(const AbilityInfo &abilityInfo, const ApplicationInfo &appInfo,
133     const AAFwk::Want &want, AbilityRuntime::LoadParam loadParam)
134 {
135     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
136     if (service != nullptr) {
137         sptr<IAmsMgr> amsService = service->GetAmsMgr();
138         if (amsService != nullptr) {
139             // From here, separate AbilityInfo and ApplicationInfo from AA.
140             std::shared_ptr<AbilityInfo> abilityInfoPtr = std::make_shared<AbilityInfo>(abilityInfo);
141             std::shared_ptr<ApplicationInfo> appInfoPtr = std::make_shared<ApplicationInfo>(appInfo);
142             std::shared_ptr<AAFwk::Want> wantPtr = std::make_shared<AAFwk::Want>(want);
143             auto loadParamPtr = std::make_shared<AbilityRuntime::LoadParam>(loadParam);
144             amsService->LoadAbility(abilityInfoPtr, appInfoPtr, wantPtr, loadParamPtr);
145             return AppMgrResultCode::RESULT_OK;
146         }
147     }
148     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
149 }
150 
TerminateAbility(const sptr<IRemoteObject> & token,bool clearMissionFlag)151 AppMgrResultCode AppMgrClient::TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag)
152 {
153     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
154     if (service != nullptr) {
155         sptr<IAmsMgr> amsService = service->GetAmsMgr();
156         if (amsService != nullptr) {
157             amsService->TerminateAbility(token, clearMissionFlag);
158             return AppMgrResultCode::RESULT_OK;
159         }
160     }
161     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
162 }
163 
UpdateAbilityState(const sptr<IRemoteObject> & token,const AbilityState state)164 AppMgrResultCode AppMgrClient::UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state)
165 {
166     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
167     if (service != nullptr) {
168         sptr<IAmsMgr> amsService = service->GetAmsMgr();
169         if (amsService != nullptr) {
170             amsService->UpdateAbilityState(token, state);
171             return AppMgrResultCode::RESULT_OK;
172         }
173     }
174     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
175 }
176 
UpdateExtensionState(const sptr<IRemoteObject> & token,const ExtensionState state)177 AppMgrResultCode AppMgrClient::UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state)
178 {
179     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
180     if (service != nullptr) {
181         sptr<IAmsMgr> amsService = service->GetAmsMgr();
182         if (amsService != nullptr) {
183             amsService->UpdateExtensionState(token, state);
184             return AppMgrResultCode::RESULT_OK;
185         }
186     }
187     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
188 }
189 
RegisterAppStateCallback(const sptr<IAppStateCallback> & callback)190 AppMgrResultCode AppMgrClient::RegisterAppStateCallback(const sptr<IAppStateCallback> &callback)
191 {
192     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
193     if (service != nullptr) {
194         sptr<IAmsMgr> amsService = service->GetAmsMgr();
195         if (amsService != nullptr) {
196             amsService->RegisterAppStateCallback(callback);
197             return AppMgrResultCode::RESULT_OK;
198         }
199     }
200     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
201 }
202 
AbilityBehaviorAnalysis(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & preToken,const int32_t visibility,const int32_t perceptibility,const int32_t connectionState)203 AppMgrResultCode AppMgrClient::AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token,
204     const sptr<IRemoteObject> &preToken, const int32_t visibility, const int32_t perceptibility,
205     const int32_t connectionState)
206 {
207     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
208     if (service != nullptr) {
209         sptr<IAmsMgr> amsService = service->GetAmsMgr();
210         if (amsService != nullptr) {
211             amsService->AbilityBehaviorAnalysis(token, preToken, visibility, perceptibility, connectionState);
212             return AppMgrResultCode::RESULT_OK;
213         }
214     }
215     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
216 }
217 
KillProcessByAbilityToken(const sptr<IRemoteObject> & token)218 AppMgrResultCode AppMgrClient::KillProcessByAbilityToken(const sptr<IRemoteObject> &token)
219 {
220     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
221     if (service != nullptr) {
222         sptr<IAmsMgr> amsService = service->GetAmsMgr();
223         if (amsService != nullptr) {
224             amsService->KillProcessByAbilityToken(token);
225             return AppMgrResultCode::RESULT_OK;
226         }
227     }
228     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
229 }
230 
KillProcessesByUserId(int32_t userId)231 AppMgrResultCode AppMgrClient::KillProcessesByUserId(int32_t userId)
232 {
233     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
234     if (service != nullptr) {
235         sptr<IAmsMgr> amsService = service->GetAmsMgr();
236         if (amsService != nullptr) {
237             amsService->KillProcessesByUserId(userId);
238             return AppMgrResultCode::RESULT_OK;
239         }
240     }
241     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
242 }
243 
KillProcessesByPids(std::vector<int32_t> & pids)244 AppMgrResultCode AppMgrClient::KillProcessesByPids(std::vector<int32_t> &pids)
245 {
246     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
247     if (service != nullptr) {
248         sptr<IAmsMgr> amsService = service->GetAmsMgr();
249         if (amsService != nullptr) {
250             amsService->KillProcessesByPids(pids);
251             return AppMgrResultCode::RESULT_OK;
252         }
253     }
254     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
255 }
256 
AttachPidToParent(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & callerToken)257 AppMgrResultCode AppMgrClient::AttachPidToParent(const sptr<IRemoteObject> &token,
258     const sptr<IRemoteObject> &callerToken)
259 {
260     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
261     if (service != nullptr) {
262         sptr<IAmsMgr> amsService = service->GetAmsMgr();
263         if (amsService != nullptr) {
264             amsService->AttachPidToParent(token, callerToken);
265             return AppMgrResultCode::RESULT_OK;
266         }
267     }
268     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
269 }
270 
UpdateApplicationInfoInstalled(const std::string & bundleName,const int uid)271 AppMgrResultCode AppMgrClient::UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid)
272 {
273     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
274     if (service != nullptr) {
275         sptr<IAmsMgr> amsService = service->GetAmsMgr();
276         if (amsService != nullptr) {
277             int32_t result = amsService->UpdateApplicationInfoInstalled(bundleName, uid);
278             if (result == ERR_OK) {
279                 return AppMgrResultCode::RESULT_OK;
280             }
281             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
282         }
283     }
284     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
285 }
286 
KillApplication(const std::string & bundleName)287 AppMgrResultCode AppMgrClient::KillApplication(const std::string &bundleName)
288 {
289     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
290     if (service != nullptr) {
291         sptr<IAmsMgr> amsService = service->GetAmsMgr();
292         if (amsService != nullptr) {
293             int32_t result = amsService->KillApplication(bundleName);
294             if (result == ERR_OK) {
295                 return AppMgrResultCode::RESULT_OK;
296             }
297             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
298         }
299     }
300     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
301 }
302 
ForceKillApplication(const std::string & bundleName,const int userId,const int appIndex)303 AppMgrResultCode AppMgrClient::ForceKillApplication(const std::string &bundleName,
304     const int userId, const int appIndex)
305 {
306     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
307     if (service != nullptr) {
308         sptr<IAmsMgr> amsService = service->GetAmsMgr();
309         if (amsService != nullptr) {
310             int32_t result = amsService->ForceKillApplication(bundleName, userId, appIndex);
311             if (result == ERR_OK) {
312                 return AppMgrResultCode::RESULT_OK;
313             }
314             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
315         }
316     }
317     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
318 }
319 
KillProcessesByAccessTokenId(const uint32_t accessTokenId)320 AppMgrResultCode AppMgrClient::KillProcessesByAccessTokenId(const uint32_t accessTokenId)
321 {
322     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
323     if (service != nullptr) {
324         sptr<IAmsMgr> amsService = service->GetAmsMgr();
325         if (amsService != nullptr) {
326             int32_t result = amsService->KillProcessesByAccessTokenId(accessTokenId);
327             if (result == ERR_OK) {
328                 return AppMgrResultCode::RESULT_OK;
329             }
330             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
331         }
332     }
333     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
334 }
335 
KillApplicationByUid(const std::string & bundleName,const int uid,const std::string & reason)336 AppMgrResultCode AppMgrClient::KillApplicationByUid(const std::string &bundleName, const int uid,
337     const std::string& reason)
338 {
339     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
340     if (service != nullptr) {
341         sptr<IAmsMgr> amsService = service->GetAmsMgr();
342         if (amsService != nullptr) {
343             int32_t result = amsService->KillApplicationByUid(bundleName, uid, reason);
344             if (result == ERR_OK) {
345                 return AppMgrResultCode::RESULT_OK;
346             }
347             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
348         }
349     }
350     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
351 }
352 
KillApplicationSelf(const std::string & reason)353 AppMgrResultCode AppMgrClient::KillApplicationSelf(const std::string& reason)
354 {
355     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
356     if (service != nullptr) {
357         sptr<IAmsMgr> amsService = service->GetAmsMgr();
358         if (amsService != nullptr) {
359             int32_t result = amsService->KillApplicationSelf(reason);
360             if (result == ERR_OK) {
361                 return AppMgrResultCode::RESULT_OK;
362             }
363             return AppMgrResultCode::ERROR_KILL_APPLICATION;
364         }
365     }
366     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
367 }
368 
ClearUpApplicationData(const std::string & bundleName,int32_t appCloneIndex,int32_t userId)369 AppMgrResultCode AppMgrClient::ClearUpApplicationData(const std::string &bundleName, int32_t appCloneIndex,
370     int32_t userId)
371 {
372     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
373     if (service != nullptr) {
374         int32_t result = service->ClearUpApplicationData(bundleName, appCloneIndex, userId);
375         if (result == ERR_OK) {
376             return AppMgrResultCode::RESULT_OK;
377         }
378         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
379     }
380     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
381 }
382 
ClearUpApplicationDataBySelf(int32_t userId)383 AppMgrResultCode AppMgrClient::ClearUpApplicationDataBySelf(int32_t userId)
384 {
385     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
386     if (service != nullptr) {
387         int32_t result = service->ClearUpApplicationDataBySelf(userId);
388         if (result == ERR_OK) {
389             return AppMgrResultCode::RESULT_OK;
390         }
391         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
392     }
393     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
394 }
395 
GetAllRunningProcesses(std::vector<RunningProcessInfo> & info)396 AppMgrResultCode AppMgrClient::GetAllRunningProcesses(std::vector<RunningProcessInfo> &info)
397 {
398     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
399     if (service != nullptr) {
400         int32_t result = service->GetAllRunningProcesses(info);
401         if (result == ERR_OK) {
402             return AppMgrResultCode::RESULT_OK;
403         }
404         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
405     }
406     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
407 }
408 
GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> & info,int32_t userId)409 AppMgrResultCode AppMgrClient::GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId)
410 {
411     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
412     if (service != nullptr) {
413         int32_t result = service->GetProcessRunningInfosByUserId(info, userId);
414         if (result == ERR_OK) {
415             return AppMgrResultCode::RESULT_OK;
416         }
417         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
418     }
419     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
420 }
421 
GetProcessRunningInformation(AppExecFwk::RunningProcessInfo & info)422 AppMgrResultCode AppMgrClient::GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info)
423 {
424     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
425     if (service != nullptr) {
426         int32_t result = service->GetProcessRunningInformation(info);
427         if (result == ERR_OK) {
428             return AppMgrResultCode::RESULT_OK;
429         }
430         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
431     }
432     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
433 }
434 
GetAllRenderProcesses(std::vector<RenderProcessInfo> & info)435 AppMgrResultCode AppMgrClient::GetAllRenderProcesses(std::vector<RenderProcessInfo> &info)
436 {
437     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
438     if (service != nullptr) {
439         int32_t result = service->GetAllRenderProcesses(info);
440         if (result == ERR_OK) {
441             return AppMgrResultCode::RESULT_OK;
442         }
443         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
444     }
445     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
446 }
447 
GetAllChildrenProcesses(std::vector<ChildProcessInfo> & info)448 AppMgrResultCode AppMgrClient::GetAllChildrenProcesses(std::vector<ChildProcessInfo> &info)
449 {
450     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
451     if (service == nullptr) {
452         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
453         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
454     }
455     int32_t result = service->GetAllChildrenProcesses(info);
456     if (result != ERR_OK) {
457         TAG_LOGE(AAFwkTag::APPMGR, "service->GetAllChildrenProcesses failed,result=%{public}d", result);
458         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
459     }
460     return AppMgrResultCode::RESULT_OK;
461 }
462 
NotifyMemoryLevel(MemoryLevel level)463 AppMgrResultCode AppMgrClient::NotifyMemoryLevel(MemoryLevel level)
464 {
465     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
466 
467     if (service == nullptr) {
468         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
469         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
470     }
471     return AppMgrResultCode(service->NotifyMemoryLevel(level));
472 }
473 
NotifyProcMemoryLevel(const std::map<pid_t,MemoryLevel> & procLevelMap) const474 AppMgrResultCode AppMgrClient::NotifyProcMemoryLevel(const std::map<pid_t, MemoryLevel> &procLevelMap) const
475 {
476     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
477 
478     if (service == nullptr) {
479         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
480         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
481     }
482     return AppMgrResultCode(service->NotifyProcMemoryLevel(procLevelMap));
483 }
484 
DumpHeapMemory(const int32_t pid,OHOS::AppExecFwk::MallocInfo & mallocInfo)485 AppMgrResultCode AppMgrClient::DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo)
486 {
487     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
488     if (service == nullptr) {
489         TAG_LOGE(AAFwkTag::APPMGR, "DumpHeapMemory: service is nullptr");
490         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
491     }
492     return AppMgrResultCode(service->DumpHeapMemory(pid, mallocInfo));
493 }
494 
DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo & info)495 AppMgrResultCode AppMgrClient::DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info)
496 {
497     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
498     if (service == nullptr) {
499         TAG_LOGE(AAFwkTag::APPMGR, "DumpJsHeapMemory: service is nullptr");
500         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
501     }
502     return AppMgrResultCode(service->DumpJsHeapMemory(info));
503 }
504 
GetConfiguration(Configuration & config)505 AppMgrResultCode AppMgrClient::GetConfiguration(Configuration& config)
506 {
507     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
508     if (service != nullptr) {
509         int32_t result = service->GetConfiguration(config);
510         if (result == ERR_OK) {
511             return AppMgrResultCode::RESULT_OK;
512         }
513         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
514     }
515     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
516 }
517 
ConnectAppMgrService()518 AppMgrResultCode AppMgrClient::ConnectAppMgrService()
519 {
520     if (mgrHolder_) {
521         return mgrHolder_->ConnectAppMgrService();
522     }
523     return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
524 }
525 
IsProcessContainsOnlyUIAbility(const pid_t pid)526 bool AppMgrClient::IsProcessContainsOnlyUIAbility(const pid_t pid)
527 {
528     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
529     if (service != nullptr) {
530         sptr<IAmsMgr> amsService = service->GetAmsMgr();
531         if (amsService != nullptr) {
532             return amsService->IsProcessContainsOnlyUIAbility(pid);
533         }
534     }
535     return false;
536 }
537 
SetServiceManager(std::unique_ptr<AppServiceManager> serviceMgr)538 void AppMgrClient::SetServiceManager(std::unique_ptr<AppServiceManager> serviceMgr)
539 {
540     if (!mgrHolder_) {
541         mgrHolder_ = std::make_shared<AppMgrRemoteHolder>();
542     }
543     mgrHolder_->SetServiceManager(std::move(serviceMgr));
544 }
545 
AbilityAttachTimeOut(const sptr<IRemoteObject> & token)546 void AppMgrClient::AbilityAttachTimeOut(const sptr<IRemoteObject> &token)
547 {
548     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
549     if (service == nullptr) {
550         return;
551     }
552     sptr<IAmsMgr> amsService = service->GetAmsMgr();
553     if (amsService == nullptr) {
554         return;
555     }
556     amsService->AbilityAttachTimeOut(token);
557 }
558 
PrepareTerminate(const sptr<IRemoteObject> & token,bool clearMissionFlag)559 void AppMgrClient::PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag)
560 {
561     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
562     if (service == nullptr) {
563         return;
564     }
565     sptr<IAmsMgr> amsService = service->GetAmsMgr();
566     if (amsService == nullptr) {
567         return;
568     }
569     amsService->PrepareTerminate(token, clearMissionFlag);
570 }
571 
GetRunningProcessInfoByToken(const sptr<IRemoteObject> & token,AppExecFwk::RunningProcessInfo & info)572 void AppMgrClient::GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info)
573 {
574     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
575     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
576     if (service != nullptr) {
577         sptr<IAmsMgr> amsService = service->GetAmsMgr();
578         if (amsService != nullptr) {
579             amsService->GetRunningProcessInfoByToken(token, info);
580         }
581     }
582 }
583 
GetRunningProcessInfoByPid(const pid_t pid,OHOS::AppExecFwk::RunningProcessInfo & info) const584 int32_t AppMgrClient::GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info) const
585 {
586     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
587     if (service == nullptr) {
588         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
589         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
590     }
591     return service->GetRunningProcessInfoByPid(pid, info);
592 }
593 
SetAbilityForegroundingFlagToAppRecord(const pid_t pid) const594 void AppMgrClient::SetAbilityForegroundingFlagToAppRecord(const pid_t pid) const
595 {
596     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
597     if (service != nullptr) {
598         sptr<IAmsMgr> amsService = service->GetAmsMgr();
599         if (amsService != nullptr) {
600             amsService->SetAbilityForegroundingFlagToAppRecord(pid);
601         }
602     }
603 }
604 
AddAbilityStageDone(const int32_t recordId)605 void AppMgrClient::AddAbilityStageDone(const int32_t recordId)
606 {
607     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
608     if (service == nullptr) {
609         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
610         return;
611     }
612 
613     service->AddAbilityStageDone(recordId);
614 }
615 
StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> & bundleInfos)616 void AppMgrClient::StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos)
617 {
618     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
619     if (service == nullptr) {
620         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
621         return;
622     }
623 
624     service->StartupResidentProcess(bundleInfos);
625 }
626 
StartUserTestProcess(const AAFwk::Want & want,const sptr<IRemoteObject> & observer,const BundleInfo & bundleInfo,int32_t userId)627 int AppMgrClient::StartUserTestProcess(
628     const AAFwk::Want &want, const sptr<IRemoteObject> &observer, const BundleInfo &bundleInfo, int32_t userId)
629 {
630     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
631     if (service == nullptr) {
632         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
633         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
634     }
635     return service->StartUserTestProcess(want, observer, bundleInfo, userId);
636 }
637 
FinishUserTest(const std::string & msg,const int64_t & resultCode,const std::string & bundleName)638 int AppMgrClient::FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName)
639 {
640     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
641     if (service == nullptr) {
642         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
643         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
644     }
645     return service->FinishUserTest(msg, resultCode, bundleName);
646 }
647 
StartSpecifiedAbility(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId)648 void AppMgrClient::StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
649     int32_t requestId)
650 {
651     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
652     if (service == nullptr) {
653         return;
654     }
655     sptr<IAmsMgr> amsService = service->GetAmsMgr();
656     if (amsService == nullptr) {
657         return;
658     }
659     amsService->StartSpecifiedAbility(want, abilityInfo, requestId);
660 }
661 
SetKeepAliveEnableState(const std::string & bundleName,bool enable,int32_t uid)662 void AppMgrClient::SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid)
663 {
664     if (!IsAmsServiceReady()) {
665         return;
666     }
667     amsService_->SetKeepAliveEnableState(bundleName, enable, uid);
668 }
669 
StartSpecifiedProcess(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId)670 void AppMgrClient::StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
671     int32_t requestId)
672 {
673     TAG_LOGD(AAFwkTag::APPMGR, "call.");
674     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
675     if (service == nullptr) {
676         return;
677     }
678     sptr<IAmsMgr> amsService = service->GetAmsMgr();
679     if (amsService == nullptr) {
680         return;
681     }
682     amsService->StartSpecifiedProcess(want, abilityInfo, requestId);
683 }
684 
RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> & response)685 void AppMgrClient::RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response)
686 {
687     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
688     if (service == nullptr) {
689         return;
690     }
691     sptr<IAmsMgr> amsService = service->GetAmsMgr();
692     if (amsService == nullptr) {
693         return;
694     }
695     amsService->RegisterStartSpecifiedAbilityResponse(response);
696 }
697 
ScheduleAcceptWantDone(const int32_t recordId,const AAFwk::Want & want,const std::string & flag)698 void AppMgrClient::ScheduleAcceptWantDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag)
699 {
700     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
701     if (service == nullptr) {
702         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
703         return;
704     }
705 
706     service->ScheduleAcceptWantDone(recordId, want, flag);
707 }
708 
UpdateConfiguration(const Configuration & config,const int32_t userId)709 AppMgrResultCode AppMgrClient::UpdateConfiguration(const Configuration &config, const int32_t userId)
710 {
711     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
712     if (service == nullptr) {
713         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
714     }
715     service->UpdateConfiguration(config, userId);
716     return AppMgrResultCode::RESULT_OK;
717 }
718 
UpdateConfigurationByBundleName(const Configuration & config,const std::string & name)719 AppMgrResultCode AppMgrClient::UpdateConfigurationByBundleName(const Configuration &config, const std::string &name)
720 {
721     if (!mgrHolder_) {
722         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
723     }
724     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
725     if (service == nullptr) {
726         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
727     }
728     service->UpdateConfigurationByBundleName(config, name);
729     return AppMgrResultCode::RESULT_OK;
730 }
731 
RegisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)732 AppMgrResultCode AppMgrClient::RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
733 {
734     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
735     if (service != nullptr) {
736         int32_t result = service->RegisterConfigurationObserver(observer);
737         if (result == ERR_OK) {
738             return AppMgrResultCode::RESULT_OK;
739         }
740         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
741     }
742     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
743 }
744 
UnregisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)745 AppMgrResultCode AppMgrClient::UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
746 {
747     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
748     if (service != nullptr) {
749         int32_t result = service->UnregisterConfigurationObserver(observer);
750         if (result == ERR_OK) {
751             return AppMgrResultCode::RESULT_OK;
752         }
753         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
754     }
755     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
756 }
757 
GetAbilityRecordsByProcessID(const int pid,std::vector<sptr<IRemoteObject>> & tokens)758 int AppMgrClient::GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens)
759 {
760     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
761     if (service == nullptr) {
762         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
763         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
764     }
765 
766     return service->GetAbilityRecordsByProcessID(pid, tokens);
767 }
768 
GetApplicationInfoByProcessID(const int pid,AppExecFwk::ApplicationInfo & application,bool & debug)769 int AppMgrClient::GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug)
770 {
771     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
772     if (service == nullptr) {
773         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
774         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
775     }
776     sptr<IAmsMgr> amsService = service->GetAmsMgr();
777     if (amsService == nullptr) {
778         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr");
779         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
780     }
781     return amsService->GetApplicationInfoByProcessID(pid, application, debug);
782 }
783 
NotifyAppMgrRecordExitReason(int32_t pid,int32_t reason,const std::string & exitMsg)784 int32_t AppMgrClient::NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg)
785 {
786     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
787     if (service == nullptr) {
788         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
789         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
790     }
791     sptr<IAmsMgr> amsService = service->GetAmsMgr();
792     if (amsService == nullptr) {
793         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr");
794         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
795     }
796     return amsService->NotifyAppMgrRecordExitReason(pid, reason, exitMsg);
797 }
798 
StartNativeProcessForDebugger(const AAFwk::Want & want)799 int32_t AppMgrClient::StartNativeProcessForDebugger(const AAFwk::Want &want)
800 {
801     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
802     if (service == nullptr) {
803         TAG_LOGE(AAFwkTag::APPMGR, "service is nullptr");
804         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
805     }
806     return service->StartNativeProcessForDebugger(want);
807 }
808 
PreStartNWebSpawnProcess()809 int AppMgrClient::PreStartNWebSpawnProcess()
810 {
811     TAG_LOGI(AAFwkTag::APPMGR, "PreStartNWebSpawnProcess");
812 
813     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
814     if (service != nullptr) {
815         return service->PreStartNWebSpawnProcess();
816     }
817     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
818 }
819 
StartRenderProcess(const std::string & renderParam,int32_t ipcFd,int32_t sharedFd,int32_t crashFd,pid_t & renderPid,bool isGPU)820 int AppMgrClient::StartRenderProcess(const std::string &renderParam,
821                                      int32_t ipcFd, int32_t sharedFd,
822                                      int32_t crashFd, pid_t &renderPid, bool isGPU)
823 {
824     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
825     if (service != nullptr) {
826         return service->StartRenderProcess(renderParam, ipcFd, sharedFd, crashFd,
827                                            renderPid, isGPU);
828     }
829     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
830 }
831 
AttachRenderProcess(const sptr<IRenderScheduler> & renderScheduler)832 void AppMgrClient::AttachRenderProcess(const sptr<IRenderScheduler> &renderScheduler)
833 {
834     if (!renderScheduler) {
835         TAG_LOGI(AAFwkTag::APPMGR, "renderScheduler is nullptr");
836         return;
837     }
838 
839     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
840     if (service != nullptr) {
841         TAG_LOGI(AAFwkTag::APPMGR, "AttachRenderProcess");
842         service->AttachRenderProcess(renderScheduler->AsObject());
843     }
844 }
845 
GetRenderProcessTerminationStatus(pid_t renderPid,int & status)846 int AppMgrClient::GetRenderProcessTerminationStatus(pid_t renderPid, int &status)
847 {
848     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
849     if (service != nullptr) {
850         return service->GetRenderProcessTerminationStatus(renderPid, status);
851     }
852     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
853 }
854 
GetRemoteObject()855 sptr<IRemoteObject> AppMgrClient::GetRemoteObject()
856 {
857     return mgrHolder_->GetRemoteObject();
858 }
859 
SetCurrentUserId(const int32_t userId)860 void AppMgrClient::SetCurrentUserId(const int32_t userId)
861 {
862     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
863     if (service == nullptr) {
864         return;
865     }
866     sptr<IAmsMgr> amsService = service->GetAmsMgr();
867     if (amsService == nullptr) {
868         return;
869     }
870     amsService->SetCurrentUserId(userId);
871 }
872 
SetEnableStartProcessFlagByUserId(int32_t userId,bool enableStartProcess)873 void AppMgrClient::SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess)
874 {
875     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
876     if (service == nullptr) {
877         return;
878     }
879     sptr<IAmsMgr> amsService = service->GetAmsMgr();
880     if (amsService == nullptr) {
881         return;
882     }
883     amsService->SetEnableStartProcessFlagByUserId(userId, enableStartProcess);
884 }
885 
GetBundleNameByPid(const int pid,std::string & bundleName,int32_t & uid)886 int32_t AppMgrClient::GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid)
887 {
888     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
889     if (service == nullptr) {
890         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
891     }
892 
893     sptr<IAmsMgr> amsService = service->GetAmsMgr();
894     if (amsService != nullptr) {
895         return amsService->GetBundleNameByPid(pid, bundleName, uid);
896     }
897     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
898 }
899 
NotifyAppFault(const FaultData & faultData)900 int32_t AppMgrClient::NotifyAppFault(const FaultData &faultData)
901 {
902     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
903     if (service == nullptr) {
904         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
905     }
906     return service->NotifyAppFault(faultData);
907 }
908 
NotifyAppFaultBySA(const AppFaultDataBySA & faultData)909 int32_t AppMgrClient::NotifyAppFaultBySA(const AppFaultDataBySA &faultData)
910 {
911     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
912     if (service == nullptr) {
913         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
914     }
915     return service->NotifyAppFaultBySA(faultData);
916 }
917 
SetAppFreezeFilter(int32_t pid)918 bool AppMgrClient::SetAppFreezeFilter(int32_t pid)
919 {
920     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
921     if (service == nullptr) {
922         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
923     }
924     return service->SetAppFreezeFilter(pid);
925 }
926 
ChangeAppGcState(pid_t pid,int32_t state)927 int32_t AppMgrClient::ChangeAppGcState(pid_t pid, int32_t state)
928 {
929     if (mgrHolder_ == nullptr) {
930         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
931     }
932     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
933     if (service == nullptr) {
934         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
935     }
936     return service->ChangeAppGcState(pid, state);
937 }
938 
RegisterAppDebugListener(const sptr<IAppDebugListener> & listener)939 int32_t AppMgrClient::RegisterAppDebugListener(const sptr<IAppDebugListener> &listener)
940 {
941     if (!IsAmsServiceReady()) {
942         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
943     }
944     return amsService_->RegisterAppDebugListener(listener);
945 }
946 
UnregisterAppDebugListener(const sptr<IAppDebugListener> & listener)947 int32_t AppMgrClient::UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener)
948 {
949     if (!IsAmsServiceReady()) {
950         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
951     }
952     return amsService_->UnregisterAppDebugListener(listener);
953 }
954 
AttachAppDebug(const std::string & bundleName)955 int32_t AppMgrClient::AttachAppDebug(const std::string &bundleName)
956 {
957     if (!IsAmsServiceReady()) {
958         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
959     }
960     return amsService_->AttachAppDebug(bundleName);
961 }
962 
DetachAppDebug(const std::string & bundleName)963 int32_t AppMgrClient::DetachAppDebug(const std::string &bundleName)
964 {
965     if (!IsAmsServiceReady()) {
966         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
967     }
968     return amsService_->DetachAppDebug(bundleName);
969 }
970 
SetAppWaitingDebug(const std::string & bundleName,bool isPersist)971 int32_t AppMgrClient::SetAppWaitingDebug(const std::string &bundleName, bool isPersist)
972 {
973     if (!IsAmsServiceReady()) {
974         TAG_LOGE(AAFwkTag::APPMGR, "App manager service is not ready.");
975         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
976     }
977     return amsService_->SetAppWaitingDebug(bundleName, isPersist);
978 }
979 
CancelAppWaitingDebug()980 int32_t AppMgrClient::CancelAppWaitingDebug()
981 {
982     if (!IsAmsServiceReady()) {
983         TAG_LOGE(AAFwkTag::APPMGR, "App manager service is not ready.");
984         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
985     }
986     return amsService_->CancelAppWaitingDebug();
987 }
988 
GetWaitingDebugApp(std::vector<std::string> & debugInfoList)989 int32_t AppMgrClient::GetWaitingDebugApp(std::vector<std::string> &debugInfoList)
990 {
991     if (!IsAmsServiceReady()) {
992         TAG_LOGE(AAFwkTag::APPMGR, "App manager service is not ready.");
993         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
994     }
995     return amsService_->GetWaitingDebugApp(debugInfoList);
996 }
997 
IsWaitingDebugApp(const std::string & bundleName)998 bool AppMgrClient::IsWaitingDebugApp(const std::string &bundleName)
999 {
1000     if (!IsAmsServiceReady()) {
1001         TAG_LOGE(AAFwkTag::APPMGR, "App manager service is not ready.");
1002         return false;
1003     }
1004     return amsService_->IsWaitingDebugApp(bundleName);
1005 }
1006 
ClearNonPersistWaitingDebugFlag()1007 void AppMgrClient::ClearNonPersistWaitingDebugFlag()
1008 {
1009     if (!IsAmsServiceReady()) {
1010         TAG_LOGE(AAFwkTag::APPMGR, "App manager service is not ready.");
1011         return;
1012     }
1013     amsService_->ClearNonPersistWaitingDebugFlag();
1014 }
1015 
RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> & response)1016 int32_t AppMgrClient::RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response)
1017 {
1018     if (!IsAmsServiceReady()) {
1019         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1020     }
1021     return amsService_->RegisterAbilityDebugResponse(response);
1022 }
1023 
IsAttachDebug(const std::string & bundleName)1024 bool AppMgrClient::IsAttachDebug(const std::string &bundleName)
1025 {
1026     if (!IsAmsServiceReady()) {
1027         return false;
1028     }
1029     return amsService_->IsAttachDebug(bundleName);
1030 }
1031 
IsAmsServiceReady()1032 bool AppMgrClient::IsAmsServiceReady()
1033 {
1034     if (mgrHolder_ == nullptr) {
1035         TAG_LOGE(AAFwkTag::APPMGR, "mgrHolder_ is nullptr.");
1036         return false;
1037     }
1038 
1039     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1040     if (service == nullptr) {
1041         TAG_LOGE(AAFwkTag::APPMGR, "App manager service is nullptr.");
1042         return false;
1043     }
1044 
1045     amsService_ = service->GetAmsMgr();
1046     if (amsService_ == nullptr) {
1047         TAG_LOGE(AAFwkTag::APPMGR, "amsService_ is nullptr.");
1048         return false;
1049     }
1050     return true;
1051 }
1052 
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer,const std::vector<std::string> & bundleNameList)1053 int32_t AppMgrClient::RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer,
1054     const std::vector<std::string> &bundleNameList)
1055 {
1056     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1057     if (service == nullptr) {
1058         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1059     }
1060     return service->RegisterApplicationStateObserver(observer, bundleNameList);
1061 }
1062 
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)1063 int32_t AppMgrClient::UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
1064 {
1065     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1066     if (service == nullptr) {
1067         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1068     }
1069     return service->UnregisterApplicationStateObserver(observer);
1070 }
1071 
NotifyPageShow(const sptr<IRemoteObject> & token,const PageStateData & pageStateData)1072 int32_t AppMgrClient::NotifyPageShow(const sptr<IRemoteObject> &token, const PageStateData &pageStateData)
1073 {
1074     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1075     if (service == nullptr) {
1076         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1077     }
1078     return service->NotifyPageShow(token, pageStateData);
1079 }
1080 
NotifyPageHide(const sptr<IRemoteObject> & token,const PageStateData & pageStateData)1081 int32_t AppMgrClient::NotifyPageHide(const sptr<IRemoteObject> &token, const PageStateData &pageStateData)
1082 {
1083     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1084     if (service == nullptr) {
1085         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1086     }
1087     return service->NotifyPageHide(token, pageStateData);
1088 }
1089 
RegisterAppRunningStatusListener(const sptr<IRemoteObject> & listener)1090 int32_t AppMgrClient::RegisterAppRunningStatusListener(const sptr<IRemoteObject> &listener)
1091 {
1092     if (listener == nullptr) {
1093         TAG_LOGE(AAFwkTag::APPMGR, "Listener is nullptr.");
1094         return ERR_INVALID_DATA;
1095     }
1096 
1097     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1098     if (service == nullptr) {
1099         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1100         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1101     }
1102     return service->RegisterAppRunningStatusListener(listener);
1103 }
1104 
UnregisterAppRunningStatusListener(const sptr<IRemoteObject> & listener)1105 int32_t AppMgrClient::UnregisterAppRunningStatusListener(const sptr<IRemoteObject> &listener)
1106 {
1107     if (listener == nullptr) {
1108         TAG_LOGE(AAFwkTag::APPMGR, "Listener is nullptr.");
1109         return ERR_INVALID_DATA;
1110     }
1111 
1112     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1113     if (service == nullptr) {
1114         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1115         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1116     }
1117     return service->UnregisterAppRunningStatusListener(listener);
1118 }
1119 
ClearProcessByToken(sptr<IRemoteObject> token) const1120 void AppMgrClient::ClearProcessByToken(sptr<IRemoteObject> token) const
1121 {
1122     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1123     if (service == nullptr) {
1124         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1125         return;
1126     }
1127     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1128     if (amsService == nullptr) {
1129         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr.");
1130         return;
1131     }
1132     amsService->ClearProcessByToken(token);
1133 }
1134 
IsFinalAppProcess()1135 bool AppMgrClient::IsFinalAppProcess()
1136 {
1137     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1138     if (service == nullptr) {
1139         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1140         return false;
1141     }
1142     return service->IsFinalAppProcess();
1143 }
1144 
RegisterRenderStateObserver(const sptr<IRenderStateObserver> & observer)1145 int32_t AppMgrClient::RegisterRenderStateObserver(const sptr<IRenderStateObserver> &observer)
1146 {
1147     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1148     if (service == nullptr) {
1149         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1150     }
1151     return service->RegisterRenderStateObserver(observer);
1152 }
1153 
UnregisterRenderStateObserver(const sptr<IRenderStateObserver> & observer)1154 int32_t AppMgrClient::UnregisterRenderStateObserver(const sptr<IRenderStateObserver> &observer)
1155 {
1156     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1157     if (service == nullptr) {
1158         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1159     }
1160     return service->UnregisterRenderStateObserver(observer);
1161 }
1162 
UpdateRenderState(pid_t renderPid,int32_t state)1163 int32_t AppMgrClient::UpdateRenderState(pid_t renderPid, int32_t state)
1164 {
1165     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1166     if (service == nullptr) {
1167         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1168     }
1169     return service->UpdateRenderState(renderPid, state);
1170 }
1171 
GetAppRunningUniqueIdByPid(pid_t pid,std::string & appRunningUniqueId)1172 int32_t AppMgrClient::GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId)
1173 {
1174     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1175     if (service == nullptr) {
1176         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1177         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1178     }
1179     return service->GetAppRunningUniqueIdByPid(pid, appRunningUniqueId);
1180 }
1181 
GetAllUIExtensionRootHostPid(pid_t pid,std::vector<pid_t> & hostPids)1182 int32_t AppMgrClient::GetAllUIExtensionRootHostPid(pid_t pid, std::vector<pid_t> &hostPids)
1183 {
1184     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1185     if (service == nullptr) {
1186         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1187         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1188     }
1189     return service->GetAllUIExtensionRootHostPid(pid, hostPids);
1190 }
1191 
GetAllUIExtensionProviderPid(pid_t hostPid,std::vector<pid_t> & providerPids)1192 int32_t AppMgrClient::GetAllUIExtensionProviderPid(pid_t hostPid, std::vector<pid_t> &providerPids)
1193 {
1194     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1195     if (service == nullptr) {
1196         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1197         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1198     }
1199     return service->GetAllUIExtensionProviderPid(hostPid, providerPids);
1200 }
1201 
NotifyMemorySizeStateChanged(bool isMemorySizeSufficent)1202 int32_t AppMgrClient::NotifyMemorySizeStateChanged(bool isMemorySizeSufficent)
1203 {
1204     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1205     if (service == nullptr) {
1206         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1207         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1208     }
1209     return service->NotifyMemorySizeStateChanged(isMemorySizeSufficent);
1210 }
1211 
IsMemorySizeSufficent() const1212 bool AppMgrClient::IsMemorySizeSufficent() const
1213 {
1214     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1215     if (service == nullptr) {
1216         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1217         return true;
1218     }
1219     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1220     if (amsService == nullptr) {
1221         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr.");
1222         return true;
1223     }
1224     return amsService->IsMemorySizeSufficent();
1225 }
1226 
PreloadApplication(const std::string & bundleName,int32_t userId,AppExecFwk::PreloadMode preloadMode,int32_t appIndex)1227 int32_t AppMgrClient::PreloadApplication(const std::string &bundleName, int32_t userId,
1228     AppExecFwk::PreloadMode preloadMode, int32_t appIndex)
1229 {
1230     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1231     if (service == nullptr) {
1232         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1233     }
1234     return service->PreloadApplication(bundleName, userId, preloadMode, appIndex);
1235 }
1236 
SetSupportedProcessCacheSelf(bool isSupport)1237 int32_t AppMgrClient::SetSupportedProcessCacheSelf(bool isSupport)
1238 {
1239     TAG_LOGI(AAFwkTag::APPMGR, "Called");
1240     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1241     if (service == nullptr) {
1242         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1243         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1244     }
1245     return service->SetSupportedProcessCacheSelf(isSupport);
1246 }
1247 
SetSupportedProcessCache(int32_t pid,bool isSupport)1248 int32_t AppMgrClient::SetSupportedProcessCache(int32_t pid, bool isSupport)
1249 {
1250     TAG_LOGI(AAFwkTag::APPMGR, "Called");
1251     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1252     if (service == nullptr) {
1253         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1254         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1255     }
1256     return service->SetSupportedProcessCache(pid, isSupport);
1257 }
1258 
SaveBrowserChannel(sptr<IRemoteObject> browser)1259 void AppMgrClient::SaveBrowserChannel(sptr<IRemoteObject> browser)
1260 {
1261     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1262     if (service == nullptr) {
1263         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1264         return;
1265     }
1266     service->SaveBrowserChannel(browser);
1267 }
1268 
CheckCallingIsUserTestMode(const pid_t pid,bool & isUserTest)1269 int32_t AppMgrClient::CheckCallingIsUserTestMode(const pid_t pid, bool &isUserTest)
1270 {
1271     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1272     if (service != nullptr) {
1273         return service->CheckCallingIsUserTestMode(pid, isUserTest);
1274     }
1275     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1276 }
1277 
AttachedToStatusBar(const sptr<IRemoteObject> & token)1278 AppMgrResultCode AppMgrClient::AttachedToStatusBar(const sptr<IRemoteObject> &token)
1279 {
1280     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1281     if (service != nullptr) {
1282         sptr<IAmsMgr> amsService = service->GetAmsMgr();
1283         if (amsService != nullptr) {
1284             amsService->AttachedToStatusBar(token);
1285             return AppMgrResultCode::RESULT_OK;
1286         }
1287     }
1288     TAG_LOGE(AAFwkTag::APPMGR, "Service is not connected.");
1289     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1290 }
1291 
NotifyProcessDependedOnWeb()1292 int32_t AppMgrClient::NotifyProcessDependedOnWeb()
1293 {
1294     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1295     if (service == nullptr) {
1296         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1297         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1298     }
1299     TAG_LOGD(AAFwkTag::APPMGR, "call");
1300     return service->NotifyProcessDependedOnWeb();
1301 }
1302 
KillProcessDependedOnWeb()1303 void AppMgrClient::KillProcessDependedOnWeb()
1304 {
1305     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1306     if (service == nullptr) {
1307         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1308         return;
1309     }
1310     TAG_LOGD(AAFwkTag::APPMGR, "call");
1311     service->KillProcessDependedOnWeb();
1312 }
1313 
BlockProcessCacheByPids(const std::vector<int32_t> & pids)1314 AppMgrResultCode AppMgrClient::BlockProcessCacheByPids(const std::vector<int32_t> &pids)
1315 {
1316     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1317     if (service != nullptr) {
1318         sptr<IAmsMgr> amsService = service->GetAmsMgr();
1319         if (amsService != nullptr) {
1320             amsService->BlockProcessCacheByPids(pids);
1321             return AppMgrResultCode::RESULT_OK;
1322         }
1323     }
1324     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
1325 }
1326 
IsKilledForUpgradeWeb(const std::string & bundleName)1327 bool AppMgrClient::IsKilledForUpgradeWeb(const std::string &bundleName)
1328 {
1329     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1330     if (service == nullptr) {
1331         TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
1332         return false;
1333     }
1334     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1335     if (amsService == nullptr) {
1336         TAG_LOGE(AAFwkTag::APPMGR, "amsService is nullptr.");
1337         return false;
1338     }
1339     TAG_LOGD(AAFwkTag::APPMGR, "call");
1340     return amsService->IsKilledForUpgradeWeb(bundleName);
1341 }
1342 
CleanAbilityByUserRequest(const sptr<IRemoteObject> & token)1343 bool AppMgrClient::CleanAbilityByUserRequest(const sptr<IRemoteObject> &token)
1344 {
1345     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1346     if (service == nullptr) {
1347         TAG_LOGE(AAFwkTag::APPMGR, "get appmgrservice is nullptr.");
1348         return false;
1349     }
1350     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1351     if (amsService == nullptr) {
1352         TAG_LOGE(AAFwkTag::APPMGR, "get abilityms service is nullptr.");
1353         return false;
1354     }
1355     TAG_LOGD(AAFwkTag::APPMGR, "call");
1356     return amsService->CleanAbilityByUserRequest(token);
1357 }
1358 
IsProcessAttached(sptr<IRemoteObject> token) const1359 bool AppMgrClient::IsProcessAttached(sptr<IRemoteObject> token) const
1360 {
1361     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
1362     if (service == nullptr) {
1363         return false;
1364     }
1365     sptr<IAmsMgr> amsService = service->GetAmsMgr();
1366     if (amsService == nullptr) {
1367         return false;
1368     }
1369     return amsService->IsProcessAttached(token);
1370 }
1371 }  // namespace AppExecFwk
1372 }  // namespace OHOS
1373