• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_mgr_interface.h"
26 #include "app_service_manager.h"
27 #include "hilog_wrapper.h"
28 #include "app_mem_info.h"
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 class AppMgrRemoteHolder : public std::enable_shared_from_this<AppMgrRemoteHolder> {
33 public:
34     AppMgrRemoteHolder() = default;
35 
36     virtual ~AppMgrRemoteHolder() = default;
37 
SetServiceManager(std::unique_ptr<AppServiceManager> serviceMgr)38     void SetServiceManager(std::unique_ptr<AppServiceManager> serviceMgr)
39     {
40         std::lock_guard<std::recursive_mutex> lock(mutex_);
41         serviceManager_ = std::move(serviceMgr);
42     }
43 
ConnectAppMgrService()44     AppMgrResultCode ConnectAppMgrService()
45     {
46         std::lock_guard<std::recursive_mutex> lock(mutex_);
47         if (!serviceManager_) {
48             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
49         }
50         remote_ = serviceManager_->GetAppMgrService();
51         if (!remote_) {
52             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
53         }
54 
55         auto me = shared_from_this();
56         deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new AppMgrDeathRecipient(me));
57         if (deathRecipient_ == nullptr) {
58             HILOG_ERROR("%{public}s :Failed to create AppMgrDeathRecipient!", __func__);
59             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
60         }
61         if ((remote_->IsProxyObject()) && (!remote_->AddDeathRecipient(deathRecipient_))) {
62             HILOG_ERROR("%{public}s :Add death recipient to AppMgrService failed.", __func__);
63             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
64         }
65 
66         return AppMgrResultCode::RESULT_OK;
67     }
68 
GetRemoteObject()69     sptr<IRemoteObject> GetRemoteObject()
70     {
71         std::lock_guard<std::recursive_mutex> lock(mutex_);
72         if (!remote_) {
73             (void) ConnectAppMgrService();
74         }
75         return remote_;
76     }
77 
78 private:
HandleRemoteDied(const wptr<IRemoteObject> & remote)79     void HandleRemoteDied(const wptr<IRemoteObject>& remote)
80     {
81         std::lock_guard<std::recursive_mutex> lock(mutex_);
82         if (!remote_) {
83             return;
84         }
85 
86         if (remote_ == remote.promote()) {
87             remote_->RemoveDeathRecipient(deathRecipient_);
88             remote_ = nullptr;
89         }
90     }
91 
92     class AppMgrDeathRecipient : public IRemoteObject::DeathRecipient {
93     public:
AppMgrDeathRecipient(const std::shared_ptr<AppMgrRemoteHolder> & holder)94         explicit AppMgrDeathRecipient(const std::shared_ptr<AppMgrRemoteHolder>& holder) : owner_(holder) {}
95 
96         virtual ~AppMgrDeathRecipient() = default;
97 
OnRemoteDied(const wptr<IRemoteObject> & remote)98         void OnRemoteDied(const wptr<IRemoteObject>& remote) override
99         {
100             std::shared_ptr<AppMgrRemoteHolder> holder = owner_.lock();
101             if (holder) {
102                 holder->HandleRemoteDied(remote);
103             }
104         }
105 
106     private:
107         std::weak_ptr<AppMgrRemoteHolder> owner_;
108     };
109 
110 private:
111     std::unique_ptr<AppServiceManager> serviceManager_;
112     sptr<IRemoteObject> remote_;
113     std::recursive_mutex mutex_;
114     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
115 };
116 
AppMgrClient()117 AppMgrClient::AppMgrClient()
118 {
119     SetServiceManager(std::make_unique<AppServiceManager>());
120 }
121 
~AppMgrClient()122 AppMgrClient::~AppMgrClient()
123 {}
124 
LoadAbility(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & preToken,const AbilityInfo & abilityInfo,const ApplicationInfo & appInfo,const AAFwk::Want & want)125 AppMgrResultCode AppMgrClient::LoadAbility(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
126     const AbilityInfo &abilityInfo, const ApplicationInfo &appInfo, const AAFwk::Want &want)
127 {
128     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
129     if (service != nullptr) {
130         sptr<IAmsMgr> amsService = service->GetAmsMgr();
131         if (amsService != nullptr) {
132             // From here, separate AbilityInfo and ApplicationInfo from AA.
133             std::shared_ptr<AbilityInfo> abilityInfoPtr = std::make_shared<AbilityInfo>(abilityInfo);
134             std::shared_ptr<ApplicationInfo> appInfoPtr = std::make_shared<ApplicationInfo>(appInfo);
135             std::shared_ptr<AAFwk::Want> wantPtr = std::make_shared<AAFwk::Want>(want);
136             amsService->LoadAbility(token, preToken, abilityInfoPtr, appInfoPtr, wantPtr);
137             return AppMgrResultCode::RESULT_OK;
138         }
139     }
140     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
141 }
142 
TerminateAbility(const sptr<IRemoteObject> & token,bool clearMissionFlag)143 AppMgrResultCode AppMgrClient::TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag)
144 {
145     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
146     if (service != nullptr) {
147         sptr<IAmsMgr> amsService = service->GetAmsMgr();
148         if (amsService != nullptr) {
149             amsService->TerminateAbility(token, clearMissionFlag);
150             return AppMgrResultCode::RESULT_OK;
151         }
152     }
153     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
154 }
155 
UpdateAbilityState(const sptr<IRemoteObject> & token,const AbilityState state)156 AppMgrResultCode AppMgrClient::UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state)
157 {
158     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
159     if (service != nullptr) {
160         sptr<IAmsMgr> amsService = service->GetAmsMgr();
161         if (amsService != nullptr) {
162             amsService->UpdateAbilityState(token, state);
163             return AppMgrResultCode::RESULT_OK;
164         }
165     }
166     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
167 }
168 
UpdateExtensionState(const sptr<IRemoteObject> & token,const ExtensionState state)169 AppMgrResultCode AppMgrClient::UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state)
170 {
171     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
172     if (service != nullptr) {
173         sptr<IAmsMgr> amsService = service->GetAmsMgr();
174         if (amsService != nullptr) {
175             amsService->UpdateExtensionState(token, state);
176             return AppMgrResultCode::RESULT_OK;
177         }
178     }
179     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
180 }
181 
RegisterAppStateCallback(const sptr<IAppStateCallback> & callback)182 AppMgrResultCode AppMgrClient::RegisterAppStateCallback(const sptr<IAppStateCallback> &callback)
183 {
184     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
185     if (service != nullptr) {
186         sptr<IAmsMgr> amsService = service->GetAmsMgr();
187         if (amsService != nullptr) {
188             amsService->RegisterAppStateCallback(callback);
189             return AppMgrResultCode::RESULT_OK;
190         }
191     }
192     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
193 }
194 
AbilityBehaviorAnalysis(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & preToken,const int32_t visibility,const int32_t perceptibility,const int32_t connectionState)195 AppMgrResultCode AppMgrClient::AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token,
196     const sptr<IRemoteObject> &preToken, const int32_t visibility, const int32_t perceptibility,
197     const int32_t connectionState)
198 {
199     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
200     if (service != nullptr) {
201         sptr<IAmsMgr> amsService = service->GetAmsMgr();
202         if (amsService != nullptr) {
203             amsService->AbilityBehaviorAnalysis(token, preToken, visibility, perceptibility, connectionState);
204             return AppMgrResultCode::RESULT_OK;
205         }
206     }
207     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
208 }
209 
KillProcessByAbilityToken(const sptr<IRemoteObject> & token)210 AppMgrResultCode AppMgrClient::KillProcessByAbilityToken(const sptr<IRemoteObject> &token)
211 {
212     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
213     if (service != nullptr) {
214         sptr<IAmsMgr> amsService = service->GetAmsMgr();
215         if (amsService != nullptr) {
216             amsService->KillProcessByAbilityToken(token);
217             return AppMgrResultCode::RESULT_OK;
218         }
219     }
220     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
221 }
222 
KillProcessesByUserId(int32_t userId)223 AppMgrResultCode AppMgrClient::KillProcessesByUserId(int32_t userId)
224 {
225     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
226     if (service != nullptr) {
227         sptr<IAmsMgr> amsService = service->GetAmsMgr();
228         if (amsService != nullptr) {
229             amsService->KillProcessesByUserId(userId);
230             return AppMgrResultCode::RESULT_OK;
231         }
232     }
233     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
234 }
235 
UpdateApplicationInfoInstalled(const std::string & bundleName,const int uid)236 AppMgrResultCode AppMgrClient::UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid)
237 {
238     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
239     if (service != nullptr) {
240         sptr<IAmsMgr> amsService = service->GetAmsMgr();
241         if (amsService != nullptr) {
242             int32_t result = amsService->UpdateApplicationInfoInstalled(bundleName, uid);
243             if (result == ERR_OK) {
244                 return AppMgrResultCode::RESULT_OK;
245             }
246             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
247         }
248     }
249     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
250 }
251 
KillApplication(const std::string & bundleName)252 AppMgrResultCode AppMgrClient::KillApplication(const std::string &bundleName)
253 {
254     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
255     if (service != nullptr) {
256         sptr<IAmsMgr> amsService = service->GetAmsMgr();
257         if (amsService != nullptr) {
258             int32_t result = amsService->KillApplication(bundleName);
259             if (result == ERR_OK) {
260                 return AppMgrResultCode::RESULT_OK;
261             }
262             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
263         }
264     }
265     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
266 }
267 
KillApplicationByUid(const std::string & bundleName,const int uid)268 AppMgrResultCode AppMgrClient::KillApplicationByUid(const std::string &bundleName, const int uid)
269 {
270     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
271     if (service != nullptr) {
272         sptr<IAmsMgr> amsService = service->GetAmsMgr();
273         if (amsService != nullptr) {
274             int32_t result = amsService->KillApplicationByUid(bundleName, uid);
275             if (result == ERR_OK) {
276                 return AppMgrResultCode::RESULT_OK;
277             }
278             return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
279         }
280     }
281     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
282 }
283 
KillApplicationSelf()284 AppMgrResultCode AppMgrClient::KillApplicationSelf()
285 {
286     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
287     if (service != nullptr) {
288         sptr<IAmsMgr> amsService = service->GetAmsMgr();
289         if (amsService != nullptr) {
290             int32_t result = amsService->KillApplicationSelf();
291             if (result == ERR_OK) {
292                 return AppMgrResultCode::RESULT_OK;
293             }
294             return AppMgrResultCode::ERROR_KILL_APPLICATION;
295         }
296     }
297     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
298 }
299 
ClearUpApplicationData(const std::string & bundleName)300 AppMgrResultCode AppMgrClient::ClearUpApplicationData(const std::string &bundleName)
301 {
302     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
303     if (service != nullptr) {
304         int32_t result = service->ClearUpApplicationData(bundleName);
305         if (result == ERR_OK) {
306             return AppMgrResultCode::RESULT_OK;
307         }
308         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
309     }
310     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
311 }
312 
GetAllRunningProcesses(std::vector<RunningProcessInfo> & info)313 AppMgrResultCode AppMgrClient::GetAllRunningProcesses(std::vector<RunningProcessInfo> &info)
314 {
315     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
316     if (service != nullptr) {
317         int32_t result = service->GetAllRunningProcesses(info);
318         if (result == ERR_OK) {
319             return AppMgrResultCode::RESULT_OK;
320         }
321         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
322     }
323     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
324 }
325 
GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> & info,int32_t userId)326 AppMgrResultCode AppMgrClient::GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId)
327 {
328     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
329     if (service != nullptr) {
330         int32_t result = service->GetProcessRunningInfosByUserId(info, userId);
331         if (result == ERR_OK) {
332             return AppMgrResultCode::RESULT_OK;
333         }
334         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
335     }
336     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
337 }
338 
GetProcessRunningInformation(AppExecFwk::RunningProcessInfo & info)339 AppMgrResultCode AppMgrClient::GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info)
340 {
341     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
342     if (service != nullptr) {
343         int32_t result = service->GetProcessRunningInformation(info);
344         if (result == ERR_OK) {
345             return AppMgrResultCode::RESULT_OK;
346         }
347         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
348     }
349     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
350 }
351 
NotifyMemoryLevel(MemoryLevel level)352 AppMgrResultCode AppMgrClient::NotifyMemoryLevel(MemoryLevel level)
353 {
354     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
355 
356     if (service == nullptr) {
357         HILOG_ERROR("service is nullptr");
358         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
359     }
360     return AppMgrResultCode(service->NotifyMemoryLevel(level));
361 }
362 
GetConfiguration(Configuration & config)363 AppMgrResultCode AppMgrClient::GetConfiguration(Configuration& config)
364 {
365     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
366     if (service != nullptr) {
367         int32_t result = service->GetConfiguration(config);
368         if (result == ERR_OK) {
369             return AppMgrResultCode::RESULT_OK;
370         }
371         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
372     }
373     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
374 }
375 
ConnectAppMgrService()376 AppMgrResultCode AppMgrClient::ConnectAppMgrService()
377 {
378     if (mgrHolder_) {
379         return mgrHolder_->ConnectAppMgrService();
380     }
381     return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
382 }
383 
SetServiceManager(std::unique_ptr<AppServiceManager> serviceMgr)384 void AppMgrClient::SetServiceManager(std::unique_ptr<AppServiceManager> serviceMgr)
385 {
386     if (!mgrHolder_) {
387         mgrHolder_ = std::make_shared<AppMgrRemoteHolder>();
388     }
389     mgrHolder_->SetServiceManager(std::move(serviceMgr));
390 }
391 
AbilityAttachTimeOut(const sptr<IRemoteObject> & token)392 void AppMgrClient::AbilityAttachTimeOut(const sptr<IRemoteObject> &token)
393 {
394     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
395     if (service == nullptr) {
396         return;
397     }
398     sptr<IAmsMgr> amsService = service->GetAmsMgr();
399     if (amsService == nullptr) {
400         return;
401     }
402     amsService->AbilityAttachTimeOut(token);
403 }
404 
PrepareTerminate(const sptr<IRemoteObject> & token)405 void AppMgrClient::PrepareTerminate(const sptr<IRemoteObject> &token)
406 {
407     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
408     if (service == nullptr) {
409         return;
410     }
411     sptr<IAmsMgr> amsService = service->GetAmsMgr();
412     if (amsService == nullptr) {
413         return;
414     }
415     amsService->PrepareTerminate(token);
416 }
417 
GetRunningProcessInfoByToken(const sptr<IRemoteObject> & token,AppExecFwk::RunningProcessInfo & info)418 void AppMgrClient::GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info)
419 {
420     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
421     if (service != nullptr) {
422         sptr<IAmsMgr> amsService = service->GetAmsMgr();
423         if (amsService != nullptr) {
424             amsService->GetRunningProcessInfoByToken(token, info);
425         }
426     }
427 }
428 
GetRunningProcessInfoByPid(const pid_t pid,OHOS::AppExecFwk::RunningProcessInfo & info) const429 void AppMgrClient::GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info) const
430 {
431     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
432     if (service != nullptr) {
433         sptr<IAmsMgr> amsService = service->GetAmsMgr();
434         if (amsService != nullptr) {
435             amsService->GetRunningProcessInfoByPid(pid, info);
436         }
437     }
438 }
439 
AddAbilityStageDone(const int32_t recordId)440 void AppMgrClient::AddAbilityStageDone(const int32_t recordId)
441 {
442     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
443     if (service == nullptr) {
444         HILOG_ERROR("service is nullptr");
445         return;
446     }
447 
448     service->AddAbilityStageDone(recordId);
449 }
450 
StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> & bundleInfos)451 void AppMgrClient::StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos)
452 {
453     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
454     if (service == nullptr) {
455         HILOG_ERROR("service is nullptr");
456         return;
457     }
458 
459     service->StartupResidentProcess(bundleInfos);
460 }
461 
StartUserTestProcess(const AAFwk::Want & want,const sptr<IRemoteObject> & observer,const BundleInfo & bundleInfo,int32_t userId)462 int AppMgrClient::StartUserTestProcess(
463     const AAFwk::Want &want, const sptr<IRemoteObject> &observer, const BundleInfo &bundleInfo, int32_t userId)
464 {
465     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
466     if (service == nullptr) {
467         HILOG_ERROR("service is nullptr");
468         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
469     }
470     return service->StartUserTestProcess(want, observer, bundleInfo, userId);
471 }
472 
FinishUserTest(const std::string & msg,const int64_t & resultCode,const std::string & bundleName)473 int AppMgrClient::FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName)
474 {
475     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
476     if (service == nullptr) {
477         HILOG_ERROR("service is nullptr");
478         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
479     }
480     return service->FinishUserTest(msg, resultCode, bundleName);
481 }
482 
StartSpecifiedAbility(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo)483 void AppMgrClient::StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo)
484 {
485     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
486     if (service == nullptr) {
487         return;
488     }
489     sptr<IAmsMgr> amsService = service->GetAmsMgr();
490     if (amsService == nullptr) {
491         return;
492     }
493     amsService->StartSpecifiedAbility(want, abilityInfo);
494 }
495 
RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> & response)496 void AppMgrClient::RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response)
497 {
498     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
499     if (service == nullptr) {
500         return;
501     }
502     sptr<IAmsMgr> amsService = service->GetAmsMgr();
503     if (amsService == nullptr) {
504         return;
505     }
506     amsService->RegisterStartSpecifiedAbilityResponse(response);
507 }
508 
ScheduleAcceptWantDone(const int32_t recordId,const AAFwk::Want & want,const std::string & flag)509 void AppMgrClient::ScheduleAcceptWantDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag)
510 {
511     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
512     if (service == nullptr) {
513         HILOG_ERROR("service is nullptr");
514         return;
515     }
516 
517     service->ScheduleAcceptWantDone(recordId, want, flag);
518 }
519 
UpdateConfiguration(const Configuration & config)520 AppMgrResultCode AppMgrClient::UpdateConfiguration(const Configuration &config)
521 {
522     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
523     if (service == nullptr) {
524         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
525     }
526     service->UpdateConfiguration(config);
527     return AppMgrResultCode::RESULT_OK;
528 }
529 
RegisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)530 AppMgrResultCode AppMgrClient::RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
531 {
532     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
533     if (service != nullptr) {
534         int32_t result = service->RegisterConfigurationObserver(observer);
535         if (result == ERR_OK) {
536             return AppMgrResultCode::RESULT_OK;
537         }
538         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
539     }
540     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
541 }
542 
UnregisterConfigurationObserver(const sptr<IConfigurationObserver> & observer)543 AppMgrResultCode AppMgrClient::UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer)
544 {
545     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
546     if (service != nullptr) {
547         int32_t result = service->UnregisterConfigurationObserver(observer);
548         if (result == ERR_OK) {
549             return AppMgrResultCode::RESULT_OK;
550         }
551         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
552     }
553     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
554 }
555 
GetAbilityRecordsByProcessID(const int pid,std::vector<sptr<IRemoteObject>> & tokens)556 int AppMgrClient::GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens)
557 {
558     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
559     if (service == nullptr) {
560         HILOG_ERROR("service is nullptr");
561         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
562     }
563 
564     return service->GetAbilityRecordsByProcessID(pid, tokens);
565 }
566 
GetApplicationInfoByProcessID(const int pid,AppExecFwk::ApplicationInfo & application,bool & debug)567 int AppMgrClient::GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug)
568 {
569     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
570     if (service == nullptr) {
571         HILOG_ERROR("service is nullptr");
572         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
573     }
574     sptr<IAmsMgr> amsService = service->GetAmsMgr();
575     if (amsService == nullptr) {
576         HILOG_ERROR("amsService is nullptr");
577         return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
578     }
579     return amsService->GetApplicationInfoByProcessID(pid, application, debug);
580 }
581 
PreStartNWebSpawnProcess()582 int AppMgrClient::PreStartNWebSpawnProcess()
583 {
584     HILOG_INFO("PreStartNWebSpawnProcess");
585 
586     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
587     if (service != nullptr) {
588         return service->PreStartNWebSpawnProcess();
589     }
590     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
591 }
592 
StartRenderProcess(const std::string & renderParam,int32_t ipcFd,int32_t sharedFd,pid_t & renderPid)593 int AppMgrClient::StartRenderProcess(const std::string &renderParam, int32_t ipcFd,
594     int32_t sharedFd, pid_t &renderPid)
595 {
596     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
597     if (service != nullptr) {
598         return service->StartRenderProcess(renderParam, ipcFd, sharedFd, renderPid);
599     }
600     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
601 }
602 
AttachRenderProcess(const sptr<IRenderScheduler> & renderScheduler)603 void AppMgrClient::AttachRenderProcess(const sptr<IRenderScheduler> &renderScheduler)
604 {
605     if (!renderScheduler) {
606         HILOG_INFO("renderScheduler is nullptr");
607         return;
608     }
609 
610     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
611     if (service != nullptr) {
612         HILOG_INFO("AttachRenderProcess");
613         service->AttachRenderProcess(renderScheduler->AsObject());
614     }
615 }
616 
GetRenderProcessTerminationStatus(pid_t renderPid,int & status)617 int AppMgrClient::GetRenderProcessTerminationStatus(pid_t renderPid, int &status)
618 {
619     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
620     if (service != nullptr) {
621         return service->GetRenderProcessTerminationStatus(renderPid, status);
622     }
623     return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
624 }
625 
GetRemoteObject()626 sptr<IRemoteObject> AppMgrClient::GetRemoteObject()
627 {
628     return mgrHolder_->GetRemoteObject();
629 }
630 
631 #ifdef ABILITY_COMMAND_FOR_TEST
BlockAppService()632 int AppMgrClient::BlockAppService()
633 {
634     HILOG_INFO("%{public}s", __func__);
635     sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
636     if (service == nullptr) {
637         HILOG_ERROR("service is nullptr");
638         return AppMgrResultCode::ERROR_SERVICE_NOT_READY;
639     }
640     return service->BlockAppService();
641 }
642 #endif
643 }  // namespace AppExecFwk
644 }  // namespace OHOS
645