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 #ifndef OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H
17 #define OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H
18 
19 #include <list>
20 #include <string>
21 #include <vector>
22 
23 #include "ability_info.h"
24 #include "ability_running_record.h"
25 #include "ams_mgr_scheduler.h"
26 #include "app_malloc_info.h"
27 #include "app_mgr_constants.h"
28 #include "app_mgr_service_event_handler.h"
29 #include "app_mgr_service_inner.h"
30 #include "app_mgr_stub.h"
31 #include "app_record_id.h"
32 #include "app_running_record.h"
33 #include "app_running_status_listener_interface.h"
34 #include "app_scheduler_proxy.h"
35 #include "appexecfwk_errors.h"
36 #include "application_info.h"
37 #include "if_system_ability_manager.h"
38 #include "nocopyable.h"
39 #include "system_ability.h"
40 #include "task_handler_wrap.h"
41 #include "app_jsheap_mem_info.h"
42 
43 namespace OHOS {
44 namespace AppExecFwk {
45 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
46 
47 struct AppMgrServiceState {
48     ServiceRunningState serviceRunningState = ServiceRunningState::STATE_NOT_START;
49     SpawnConnectionState connectionState = SpawnConnectionState::STATE_NOT_CONNECT;
50 };
51 
52 class AMSEventHandler;
53 
54 class AppMgrService : public SystemAbility, public AppMgrStub {
55 public:
56     DECLEAR_SYSTEM_ABILITY(AppMgrService);
57 
58     AppMgrService();
59     explicit AppMgrService(const int32_t serviceId, bool runOnCreate = false);
60     virtual ~AppMgrService() override;
61 
62     // the function about application
63     // attach the application to ams, then ams can control it.
64     /**
65      * AttachApplication, call AttachApplication() through proxy object,
66      * get all the information needed to start the Application (data related to the Application ).
67      *
68      * @param app, information needed to start the Application.
69      * @return
70      */
71     virtual void AttachApplication(const sptr<IRemoteObject> &app) override;
72 
73     /**
74      * Preload application.
75      *
76      * @param bundleName The bundle name of the application to preload.
77      * @param userId Indicates the user identification.
78      * @param preloadMode Preload application mode.
79      * @param appIndex The index of application clone.
80      * @return Returns ERR_OK on success, others on failure.
81      */
82     virtual int32_t PreloadApplication(const std::string &bundleName, int32_t userId,
83         AppExecFwk::PreloadMode preloadMode, int32_t appIndex) override;
84 
85     // notify the ams update the state of an app, when it entered foreground.
86 
87     /**
88      * ApplicationForegrounded, call ApplicationForegrounded() through proxy object,
89      * set the application to Foreground State.
90      *
91      * @param recordId, a unique record that identifies this Application from others.
92      * @return
93      */
94     virtual void ApplicationForegrounded(const int32_t recordId) override;
95 
96     /**
97      * ApplicationBackgrounded, call ApplicationBackgrounded() through proxy object,
98      * set the application to Backgrounded State.
99      *
100      * @param recordId, a unique record that identifies this Application from others.
101      * @return
102      */
103     virtual void ApplicationBackgrounded(const int32_t recordId) override;
104 
105     /**
106      * ApplicationTerminated, call ApplicationTerminated() through proxy object,
107      * terminate the application.
108      *
109      * @param recordId, a unique record that identifies this Application from others.
110      * @return
111      */
112     virtual void ApplicationTerminated(const int32_t recordId) override;
113 
114     /**
115      * AbilityCleaned,call through AbilityCleaned() proxy project, clean Ability record.
116      *
117      * @param token, a unique record that identifies AbilityCleaned from others.
118      * @return
119      */
120     virtual void AbilityCleaned(const sptr<IRemoteObject> &token) override;
121 
122     /**
123      * ClearUpApplicationData, call ClearUpApplicationData() through proxy project,
124      * clear the application data.
125      *
126      * @param bundleName, bundle name in Application record.
127      * @param appCloneIndex the app clone id.
128      * @param userId the user id.
129      * @return Returns ERR_OK on success, others on failure.
130      */
131     virtual int32_t ClearUpApplicationData(const std::string &bundleName, int32_t appCloneIndex,
132         int32_t userId = -1) override;
133 
134     /**
135      * ClearUpApplicationDataBySelf, call ClearUpApplicationDataBySelf() through proxy project,
136      * clear the application data by self.
137      *
138      * @param userId, user ID.
139      * @return Returns ERR_OK on success, others on failure.
140      */
141     virtual int32_t ClearUpApplicationDataBySelf(int32_t userId = -1) override;
142 
143     /**
144      * GetAllRunningProcesses, call GetAllRunningProcesses() through proxy project.
145      * Obtains information about application processes that are running on the device.
146      *
147      * @param info, app name in Application record.
148      * @return Returns ERR_OK on success, others on failure.
149      */
150     virtual int32_t GetAllRunningProcesses(std::vector<RunningProcessInfo> &info) override;
151 
152     /**
153      * GetRunningMultiAppInfoByBundleName, call GetRunningMultiAppInfoByBundleName() through proxy project.
154      * Obtains information about multiapp that are running on the device.
155      *
156      * @param bundlename, input.
157      * @param info, output multiapp information.
158      * @return ERR_OK ,return back success,others fail.
159      */
160     virtual int32_t GetRunningMultiAppInfoByBundleName(const std::string &bundleName,
161         RunningMultiAppInfo &info) override;
162 
163     /**
164      * GetAllRunningInstanceKeysBySelf, call GetAllRunningInstanceKeysBySelf() through proxy project.
165      * Obtains running instance keys of multi-instance app that are running on the device.
166      *
167      * @param instanceKeys, output instance keys of the multi-instance app.
168      * @return ERR_OK ,return back success,others fail.
169      */
170     virtual int32_t GetAllRunningInstanceKeysBySelf(std::vector<std::string> &instanceKeys) override;
171 
172     /**
173      * GetAllRunningInstanceKeysByBundleName, call GetAllRunningInstanceKeysByBundleName() through proxy project.
174      * Obtains running isntance keys of multi-instance app that are running on the device.
175      *
176      * @param bundlename, bundle name in Application record.
177      * @param instanceKeys, output instance keys of the multi-insatnce app.
178      * @param userId, user id.
179      * @return ERR_OK ,return back success,others fail.
180      */
181     virtual int32_t GetAllRunningInstanceKeysByBundleName(const std::string &bundleName,
182         std::vector<std::string> &instanceKeys, int32_t userId = -1) override;
183 
184     /**
185      * GetRunningProcessesByBundleType, call GetRunningProcessesByBundleType() through proxy project.
186      * Obtains information about application processes by bundle type that are running on the device.
187      *
188      * @param bundleType, bundle type of the processes
189      * @param info, app name in Application record.
190      * @return ERR_OK ,return back success,others fail.
191      */
192     virtual int GetRunningProcessesByBundleType(const BundleType bundleType,
193         std::vector<RunningProcessInfo> &info) override;
194 
195     /**
196      * GetAllRenderProcesses, call GetAllRenderProcesses() through proxy project.
197      * Obtains information about render processes that are running on the device.
198      *
199      * @param info, render process info.
200      * @return ERR_OK, return back success, others fail.
201      */
202     virtual int32_t GetAllRenderProcesses(std::vector<RenderProcessInfo> &info) override;
203 
204     /**
205      * GetAllChildrenProcesses, call GetAllChildrenProcesses() through proxy project.
206      * Obtains information about children processes that are running on the device.
207      *
208      * @param info, child process info.
209      * @return ERR_OK, return back success, others fail.
210      */
211     virtual int GetAllChildrenProcesses(std::vector<ChildProcessInfo> &info) override;
212 
213     /**
214      * JudgeSandboxByPid, call JudgeSandboxByPid() through proxy project.
215      * Obtains information about application processes that are running on the device.
216      *
217      * @param pid, the pid of current app running record.
218      * @param isSandbox, current app is or not a sandbox.
219      * @return Returns ERR_OK on success, others on failure.
220      */
221     virtual int32_t JudgeSandboxByPid(pid_t pid, bool &isSandbox) override;
222 
223     /**
224      * IsTerminatingByPid, call IsTerminatingByPid() through proxy project.
225      * Obtains information about application processes that are running on the device.
226      *
227      * @param pid, the pid of current app running record.
228      * @param isTerminating, current app is or not terminating.
229      * @return Returns ERR_OK on success, others on failure.
230      */
231     virtual int32_t IsTerminatingByPid(pid_t pid, bool &isTerminating) override;
232 
233     /**
234      * GetProcessRunningInfosByUserId, call GetProcessRunningInfosByUserId() through proxy project.
235      * Obtains information about application processes that are running on the device.
236      *
237      * @param info, app name in Application record.
238      * @param userId, userId.
239      *
240      * @return Returns ERR_OK on success, others on failure.
241      */
242     virtual int32_t GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId) override;
243 
244     /**
245      * GetProcessRunningInformation, call GetProcessRunningInformation() through proxy project.
246      * Obtains information about current application process which is running on the device.
247      *
248      * @param info, app name in Application record.
249      *
250      * @return Returns ERR_OK on success, others on failure.
251      */
252     virtual int32_t GetProcessRunningInformation(RunningProcessInfo &info) override;
253 
254     /**
255      * NotifyMemoryLevel, call NotifyMemoryLevel() through proxy project.
256      * Notify applications background the current memory level.
257      *
258      * @param level, current memory level.
259      * @return Returns ERR_OK on success, others on failure.
260      */
261     virtual int32_t NotifyMemoryLevel(int32_t level) override;
262 
263     /**
264      * NotifyProcMemoryLevel, call NotifyMemoryLevel() through proxy project.
265      * Notify applications the current memory level.
266      *
267      * @param  procLevelMap , <pid_t, MemoryLevel> map.
268      * @return Returns ERR_OK on success, others on failure.
269      */
270     virtual int32_t NotifyProcMemoryLevel(const std::map<pid_t, MemoryLevel> &procLevelMap) override;
271 
272     /**
273      * DumpHeapMemory, call DumpHeapMemory() through proxy project.
274      * Get the application's memory allocation info.
275      *
276      * @param pid, pid input.
277      * @param mallocInfo, dynamic storage information output.
278      * @return Returns ERR_OK on success, others on failure.
279      */
280     virtual int32_t DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo) override;
281 
282     /**
283      * Authenticate dump permissions
284      *
285      * @return Returns true on permission, others on false
286      */
287     bool HasDumpPermission() const;
288     /**
289      * DumpJsHeapMemory, call DumpJsHeapMemory() through proxy project.
290      * triggerGC and dump the application's jsheap memory info.
291      *
292      * @param info, pid, tid, needGc, needSnapshot
293      * @return Returns ERR_OK on success, others on failure.
294      */
295     virtual int32_t DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info) override;
296 
297     // the function about service running info
298     /**
299      * QueryServiceState, Query application service status.
300      *
301      * @return the application service status.
302      */
303     AppMgrServiceState QueryServiceState();
304 
305     /**
306      * GetAmsMgr, call GetAmsMgr() through proxy object, get AMS interface instance.
307      *
308      * @return sptr<IAmsMgr>, return to AMS interface instance.
309      */
310     virtual sptr<IAmsMgr> GetAmsMgr() override;
311 
312     /**
313      * Notify that the ability stage has been updated
314      * @param recordId, the app record.
315      */
316     virtual void AddAbilityStageDone(const int32_t recordId) override;
317 
318     /**
319      * Start all resident process
320      */
321     virtual void StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos) override;
322 
323     /**
324      * Start user test process.
325      * @param want, want object.
326      * @param observer, test observer remote object.
327      * @param bundleInfo, bundle info.
328      * @param userId the user id.
329      * @return Returns ERR_OK on success, others on failure.
330      */
331     virtual int StartUserTestProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer,
332         const AppExecFwk::BundleInfo &bundleInfo, int32_t userId) override;
333 
334     /**
335      * @brief Finish user test.
336      * @param msg user test message.
337      * @param resultCode user test result Code.
338      * @param bundleName user test bundleName.
339      *
340      * @return Returns ERR_OK on success, others on failure.
341      */
342     virtual int FinishUserTest(
343         const std::string &msg, const int64_t &resultCode, const std::string &bundleName) override;
344 
345     /**
346      * @brief Application hidumper.
347      * @param fd Indicates the fd.
348      * @param args Indicates the params.
349      * @return Returns the dump result.
350      */
351     int Dump(int fd, const std::vector<std::u16string>& args) override;
352 
353     virtual void ScheduleAcceptWantDone(
354         const int32_t recordId, const AAFwk::Want &want, const std::string &flag) override;
355 
356     virtual void ScheduleNewProcessRequestDone(
357         const int32_t recordId, const AAFwk::Want &want, const std::string &flag) override;
358 
359     virtual int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens) override;
360 
361     virtual int PreStartNWebSpawnProcess() override;
362 
363     virtual int StartRenderProcess(const std::string &renderParam,
364                                    int32_t ipcFd, int32_t sharedFd,
365                                    int32_t crashFd, pid_t &renderPid, bool isGPU = false) override;
366 
367     virtual void AttachRenderProcess(const sptr<IRemoteObject> &shceduler) override;
368 
369     virtual int GetRenderProcessTerminationStatus(pid_t renderPid, int &status) override;
370 
371     virtual int32_t GetConfiguration(Configuration& config) override;
372 
373     virtual int32_t UpdateConfiguration(const Configuration &config, const int32_t userId = -1) override;
374 
375     virtual int32_t UpdateConfigurationByBundleName(const Configuration &config, const std::string &name) override;
376 
377     virtual int32_t RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override;
378 
379     virtual int32_t UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override;
380 
381     bool GetAppRunningStateByBundleName(const std::string &bundleName) override;
382 
383     int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override;
384 
385     int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override;
386 
387     int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override;
388 
389 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
390     int32_t SetContinuousTaskProcess(int32_t pid, bool isContinuousTask) override;
391 #endif
392 
393     /**
394      * @brief Check whether the shared bundle is running.
395      *
396      * @param bundleName Shared bundle name.
397      * @param versionCode Shared bundle version code.
398      * @return Returns the shared bundle running result. The result is true if running, false otherwise.
399      */
400     virtual bool IsSharedBundleRunning(const std::string &bundleName, uint32_t versionCode) override;
401 
402     virtual int32_t StartNativeProcessForDebugger(const AAFwk::Want &want) override;
403 
404     /**
405      * Get bundleName by pid.
406      *
407      * @param pid process id.
408      * @param bundleName Output parameters, return bundleName.
409      * @param uid Output parameters, return userId.
410      * @return Returns ERR_OK on success, others on failure.
411      */
412     virtual int32_t GetBundleNameByPid(const int32_t pid, std::string &bundleName, int32_t &uid) override;
413 
414     /**
415      * Get running process information by pid.
416      *
417      * @param pid process id.
418      * @param info Output parameters, return runningProcessInfo.
419      * @return Returns ERR_OK on success, others on failure.
420      */
421     virtual int32_t GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info) override;
422 
423     /**
424      * Notify Fault Data
425      *
426      * @param faultData the fault data.
427      * @return Returns ERR_OK on success, others on failure.
428      */
429     int32_t NotifyAppFault(const FaultData &faultData) override;
430 
431     /**
432      * Notify Fault Data By SA
433      *
434      * @param faultData the fault data notified by SA.
435      * @return Returns ERR_OK on success, others on failure.
436      */
437     int32_t NotifyAppFaultBySA(const AppFaultDataBySA &faultData) override;
438 
439     /**
440      * Set Appfreeze Detect Filter
441      *
442      * @param pid the process pid.
443      * @return Returns true on success, others on failure.
444      */
445     bool SetAppFreezeFilter(int32_t pid) override;
446 
447     /**
448      * get memorySize by pid.
449      *
450      * @param pid process id.
451      * @param memorySize Output parameters, return memorySize in KB.
452      * @return Returns ERR_OK on success, others on failure.
453      */
454     virtual int32_t GetProcessMemoryByPid(const int32_t pid, int32_t &memorySize) override;
455 
456     /**
457      * get application processes information list by bundleName.
458      *
459      * @param bundleName Bundle name.
460      * @param userId user Id in Application record.
461      * @param info Output parameters, return running process info list.
462      * @return Returns ERR_OK on success, others on failure.
463      */
464     virtual int32_t GetRunningProcessInformation(
465         const std::string &bundleName, int32_t userId, std::vector<RunningProcessInfo> &info) override;
466 
467     /**
468      * on add systemAbility.
469      *
470      * @return
471      */
472     virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
473 
474     /**
475      * on remove systemAbility.
476      *
477      * @return
478      */
479     virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
480 
481     /**
482      * @brief Notify NativeEngine GC of status change.
483      *
484      * @param state GC state
485      * @param pid pid
486      *
487      * @return Is the status change completed.
488      */
489     virtual int32_t ChangeAppGcState(pid_t pid, int32_t state) override;
490 
491     /**
492      * Register appRunning status listener.
493      *
494      * @param listener Running status listener.
495      * @return Returns ERR_OK on success, others on failure.
496      */
497     int32_t RegisterAppRunningStatusListener(const sptr<IRemoteObject> &listener) override;
498 
499     /**
500      * Unregister appRunning status listener.
501      *
502      * @param listener Running status listener.
503      * @return Returns ERR_OK on success, others on failure.
504      */
505     int32_t UnregisterAppRunningStatusListener(const sptr<IRemoteObject> &listener) override;
506 
507     /**
508      * Register application foreground state observer.
509      * @param observer Is App Foreground Statue Observer
510      * @return Returns ERR_OK on success, others on failure.
511      */
512     int32_t RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer) override;
513 
514     /**
515      * Unregister application foreground state observer.
516      * @param observer Is App Foreground Statue Observer
517      * @return Returns ERR_OK on success, others on failure.
518      */
519     int32_t UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer) override;
520 
521     /**
522      * Start child process, called by ChildProcessManager.
523      *
524      * @param childPid Created child process pid.
525      * @param request Child process start request params.
526      * @return Returns ERR_OK on success, others on failure.
527      */
528     int32_t StartChildProcess(pid_t &childPid, const ChildProcessRequest &request) override;
529 
530     /**
531      * Get child process record for self.
532      *
533      * @return child process info.
534      */
535     int32_t GetChildProcessInfoForSelf(ChildProcessInfo &info) override;
536 
537     /**
538      * Attach child process scheduler to app manager service.
539      *
540      * @param childScheduler scheduler of child process.
541      */
542     void AttachChildProcess(const sptr<IRemoteObject> &childScheduler) override;
543 
544     /**
545      * Exit child process, called by itself.
546      */
547     void ExitChildProcessSafely() override;
548 
549     int32_t RegisterRenderStateObserver(const sptr<IRenderStateObserver> &observer) override;
550 
551     int32_t UnregisterRenderStateObserver(const sptr<IRenderStateObserver> &observer) override;
552 
553     int32_t UpdateRenderState(pid_t renderPid, int32_t state) override;
554 
555     int32_t SignRestartAppFlag(int32_t uid) override;
556 
557     int32_t GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId) override;
558 
559     int32_t GetAllUIExtensionRootHostPid(pid_t pid, std::vector<pid_t> &hostPids) override;
560 
561     int32_t GetAllUIExtensionProviderPid(pid_t hostPid, std::vector<pid_t> &providerPids) override;
562 
563     int32_t NotifyMemorySizeStateChanged(bool isMemorySizeSufficent) override;
564 
565     void SetAppAssertionPauseState(bool flag) override;
566 
567     int32_t SetSupportedProcessCacheSelf(bool isSupport) override;
568 
569     int32_t SetSupportedProcessCache(int32_t pid, bool isSupport) override;
570 
571     virtual void SaveBrowserChannel(sptr<IRemoteObject> browser) override;
572 
573     /**
574      * Check caller is test ability
575      *
576      * @param pid, the pid of ability.
577      * @return Returns ERR_OK is test ability, others is not test ability.
578      */
579     int32_t CheckCallingIsUserTestMode(const pid_t pid, bool &isUserTest) override;
580     /**
581      * Start native child process, callde by ChildProcessManager.
582      * @param libName lib file name to be load in child process
583      * @param childProcessCount current started child process count
584      * @param callback callback for notify start result
585      * @return Returns ERR_OK on success, others on failure.
586      */
587     int32_t StartNativeChildProcess(const std::string &libName, int32_t childProcessCount,
588         const sptr<IRemoteObject> &callback) override;
589 
590     virtual int32_t NotifyProcessDependedOnWeb() override;
591 
592     virtual void KillProcessDependedOnWeb() override;
593 
594     virtual void RestartResidentProcessDependedOnWeb() override;
595 
596     int32_t GetAppIndexByPid(pid_t pid, int32_t &appIndex) override;
597 private:
598     /**
599      * Init, Initialize application services.
600      *
601      * @return ERR_OK, return back success, others fail.
602      */
603     ErrCode Init();
604 
605     // the function that overrode from SystemAbility
606     /**
607      * OnStart, Start application service.
608      *
609      * @return
610      */
611     virtual void OnStart() override;
612 
613     /**
614      * OnStop, Stop application service.
615      *
616      * @return
617      */
618     virtual void OnStop() override;
619 
620     /**
621      * @brief Judge whether the application service is ready.
622      *
623      * @return Returns true means service is ready, otherwise service is not ready.
624      */
625     bool IsReady() const;
626 
627     /**
628      * SetInnerService, Setting application service Inner instance.
629      *
630      * @return
631      */
632     void SetInnerService(const std::shared_ptr<AppMgrServiceInner> &innerService);
633 
634     /**
635      * Register application or process state observer.
636      * @param observer, ability token.
637      * @return Returns ERR_OK on success, others on failure.
638      */
639     virtual int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer,
640         const std::vector<std::string> &bundleNameList = {}) override;
641 
642     /**
643      * Unregister application or process state observer.
644      * @param observer, ability token.
645      * @return Returns ERR_OK on success, others on failure.
646      */
647     virtual int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer) override;
648 
649     /**
650      * Register application or process state observer.
651      * @param observer, Is ability foreground state observer
652      * @return Returns ERR_OK on success, others on failure.
653      */
654     int32_t RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer) override;
655 
656     /**
657      * Unregister application or process state observer.
658      * @param observer, Is ability foreground state observer
659      * @return Returns ERR_OK on success, others on failure.
660      */
661     int32_t UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer) override;
662 
663     /**
664      * Get foreground applications.
665      * @param list, foreground apps.
666      * @return Returns ERR_OK on success, others on failure.
667      */
668     virtual int32_t GetForegroundApplications(std::vector<AppStateData> &list) override;
669 
670     int Dump(const std::vector<std::u16string>& args, std::string& result);
671     int ShowHelp(const std::vector<std::u16string>& args, std::string& result);
672     int DumpIpc(const std::vector<std::u16string>& args, std::string& result);
673     int DumpIpcAllStart(std::string& result);
674     int DumpIpcAllStop(std::string& result);
675     int DumpIpcAllStat(std::string& result);
676     int DumpIpcStart(const int32_t pid, std::string& result);
677     int DumpIpcStop(const int32_t pid, std::string& result);
678     int DumpIpcStat(const int32_t pid, std::string& result);
679 
680     int DumpFfrt(const std::vector<std::u16string>& args, std::string& result);
681 
682     bool JudgeAppSelfCalled(int32_t recordId);
683 
684     /**
685      * @brief Notify AbilityManagerService the page show.
686      * @param token Ability identify.
687      * @param pageStateData The data of ability's page state.
688      * @return Returns ERR_OK on success, others on failure.
689      */
690     virtual int32_t NotifyPageShow(const sptr<IRemoteObject> &token, const PageStateData &pageStateData) override;
691 
692     /**
693      * @brief Notify AbilityManagerService the page hide.
694      * @param token Ability identify.
695      * @param pageStateData The data of ability's page state.
696      * @return Returns ERR_OK on success, others on failure.
697      */
698     virtual int32_t NotifyPageHide(const sptr<IRemoteObject> &token, const PageStateData &pageStateData) override;
699 
700     /**
701      * Check whether the bundle is running.
702      *
703      * @param bundleName Indicates the bundle name of the bundle.
704      * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise.
705      * @return Return ERR_OK if success, others fail.
706      */
707     int32_t IsApplicationRunning(const std::string &bundleName, bool &isRunning) override;
708 
709     /**
710      * Check whether the bundle is running.
711      *
712      * @param bundleName Indicates the bundle name of the bundle.
713      * @param appCloneIndex the appindex of the bundle.
714      * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise.
715      * @return Return ERR_OK if success, others fail.
716      */
717     int32_t IsAppRunning(const std::string &bundleName, int32_t appCloneIndex, bool &isRunning) override;
718 
719     /**
720      * Whether the current application process is the last surviving process.
721      *
722      * @return Returns true is final application process, others return false.
723      */
724     bool IsFinalAppProcess() override;
725 
726     /**
727      * Get pids of processes which belong to specific bundle name and support process cache feature.
728      *
729      * @param bundleName bundle name.
730      * @param pidList pid list of processes that support process cache.
731      * @return Returns ERR_OK on success, others on failure.
732      */
733     virtual int32_t GetSupportedProcessCachePids(const std::string &bundleName,
734         std::vector<int32_t> &pidList) override;
735 
736     enum DumpIpcKey {
737         KEY_DUMP_IPC_START = 0,
738         KEY_DUMP_IPC_STOP,
739         KEY_DUMP_IPC_STAT,
740     };
741 
742 private:
743     int DumpIpcAllInner(const AppMgrService::DumpIpcKey key, std::string& result);
744     int DumpIpcWithPidInner(const AppMgrService::DumpIpcKey key,
745         const std::string& optionPid, std::string& result);
746 
747     int DumpFfrtInner(const std::string& pidsRaw, std::string& result);
748 
749 private:
750     std::shared_ptr<AppMgrServiceInner> appMgrServiceInner_;
751     AppMgrServiceState appMgrServiceState_;
752     std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
753     std::shared_ptr<AMSEventHandler> eventHandler_;
754     sptr<ISystemAbilityManager> systemAbilityMgr_;
755     sptr<IAmsMgr> amsMgrScheduler_;
756 
757     bool GetDumpIpcKeyByOption(const std::string &option, DumpIpcKey &key);
758 
759     DISALLOW_COPY_AND_MOVE(AppMgrService);
760 };
761 }  // namespace AppExecFwk
762 }  // namespace OHOS
763 #endif  // OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H
764