• 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 #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 ability mgr, then ability mgr 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 instance 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-instance 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     /**
354      * @brief called when the abilityStage's onAcceptWant lifecycle completed.
355      * @param recordId record id of the ability.
356      * @param want want has been accepted.
357      * @param flag flag get from OnAcceptWant.
358      */
359     virtual void ScheduleAcceptWantDone(
360         const int32_t recordId, const AAFwk::Want &want, const std::string &flag) override;
361 
362     virtual void ScheduleNewProcessRequestDone(
363         const int32_t recordId, const AAFwk::Want &want, const std::string &flag) override;
364 
365     /**
366      *  Get the token of ability records by process ID.
367      *
368      * @param pid The process id.
369      * @param tokens The token of ability records.
370      * @return Returns ERR_OK on success, others on failure.
371      */
372     virtual int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens) override;
373 
374     /**
375      * Prestart nwebspawn process.
376      *
377      * @return Returns ERR_OK on success, others on failure.
378      */
379     virtual int PreStartNWebSpawnProcess() override;
380 
381     /**
382      * Start nweb render process, called by nweb host.
383      *
384      * @param renderParam, params passed to renderProcess.
385      * @param ipcFd, ipc file descriptor for web browser and render process.
386      * @param sharedFd, shared memory file descriptor.
387      * @param crashFd, crash signal file descriptor.
388      * @param renderPid, created render pid.
389      * @param isGPU, is or not gpu process
390      * @return Returns ERR_OK on success, others on failure.
391      */
392     virtual int StartRenderProcess(const std::string &renderParam,
393                                    int32_t ipcFd, int32_t sharedFd,
394                                    int32_t crashFd, pid_t &renderPid, bool isGPU = false) override;
395 
396     /**
397      * Render process call this to attach to app manager service.
398      *
399      * @param renderScheduler scheduler of render process.
400      */
401     virtual void AttachRenderProcess(const sptr<IRemoteObject> &shceduler) override;
402 
403     /**
404      * Get render process termination status, called by nweb host.
405      *
406      * @param renderPid, target render pid.
407      * @param status, termination status of the render process.
408      * @return Returns ERR_OK on success, others on failure.
409      */
410     virtual int GetRenderProcessTerminationStatus(pid_t renderPid, int &status) override;
411 
412     /**
413      * GetConfiguration
414      *
415      * @param info to retrieve configuration data.
416      * @return ERR_OK ,return back success,others fail.
417      */
418     virtual int32_t GetConfiguration(Configuration& config) override;
419 
420     /**
421      * UpdateConfiguration, ANotify application update system environment changes.
422      *
423      * @param config System environment change parameters.
424      * @param userId configuration for the user
425      * @return Returns ERR_OK on success, others on failure.
426      */
427     virtual int32_t UpdateConfiguration(const Configuration &config, const int32_t userId = -1) override;
428 
429     /**
430      *  Update config by bundle name.
431      *
432      * @param config Application environment change parameters.
433      * @param name Application bundle name.
434      * @return Returns ERR_OK on success, others on failure.
435      */
436     virtual int32_t UpdateConfigurationByBundleName(const Configuration &config, const std::string &name,
437         int32_t appIndex = 0) override;
438 
439     /**
440      * @brief register a configuration observer which will receive notifies when updated.
441      * @param observer the configuration observer to receive notify.
442      *
443      * @return Returns ERR_OK on success, others on failure.
444      */
445     virtual int32_t RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override;
446 
447     /**
448      * @brief unregister a configuration observer registered before.
449      * @param observer the configuration observer registered before.
450      *
451      * @return Returns ERR_OK on success, others on failure.
452      */
453     virtual int32_t UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override;
454 
455     /**
456      * Register KIA interceptor.
457      * @param interceptor KIA interceptor.
458      * @return Returns ERR_OK on success, others on failure.
459      */
460     virtual int32_t RegisterKiaInterceptor(const sptr<IKiaInterceptor> &interceptor) override;
461 
462     /**
463      * Check if the given pid is a KIA process.
464      * @param pid process id.
465      * @return Returns true if it is a KIA process, false otherwise.
466      */
467     virtual int32_t CheckIsKiaProcess(pid_t pid, bool &isKia) override;
468 
469     /**
470      * @brief Get the running state of application by bundle name.
471      *
472      * @param bundleName Bundle name
473      * @return Returns true if process is running, false if process isn't running.
474      */
475     bool GetAppRunningStateByBundleName(const std::string &bundleName) override;
476 
477     /**
478      * @brief Notify application load patch.
479      *
480      * @param bundleName Bundle name
481      * @param callback called when LoadPatch finished.
482      * @return Returns ERR_OK on success, error code on failure.
483      */
484     int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override;
485 
486     /**
487      * @brief Notify application reload page.
488      *
489      * @param bundleName Bundle name
490      * @param callback called when HotReload finished.
491      * @return Returns ERR_OK on success, error code on failure.
492      */
493     int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override;
494 
495     /**
496      * @brief Notify application unload patch.
497      *
498      * @param bundleName Bundle name
499      * @param callback called when UnloadPatch finished.
500      * @return Returns ERR_OK on success, error code on failure.
501      */
502     int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override;
503 
504 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
505     int32_t SetContinuousTaskProcess(int32_t pid, bool isContinuousTask) override;
506 #endif
507 
508     /**
509      * @brief Check whether the shared bundle is running.
510      *
511      * @param bundleName Shared bundle name.
512      * @param versionCode Shared bundle version code.
513      * @return Returns the shared bundle running result. The result is true if running, false otherwise.
514      */
515     virtual bool IsSharedBundleRunning(const std::string &bundleName, uint32_t versionCode) override;
516 
517     /**
518      * start native process for debugger.
519      *
520      * @param want param to start a process.
521      */
522     virtual int32_t StartNativeProcessForDebugger(const AAFwk::Want &want) override;
523 
524     /**
525      * Get bundleName by pid.
526      *
527      * @param pid process id.
528      * @param bundleName Output parameters, return bundleName.
529      * @param uid Output parameters, return userId.
530      * @return Returns ERR_OK on success, others on failure.
531      */
532     virtual int32_t GetBundleNameByPid(const int32_t pid, std::string &bundleName, int32_t &uid) override;
533 
534     /**
535      * Get running process information by pid.
536      *
537      * @param pid process id.
538      * @param info Output parameters, return runningProcessInfo.
539      * @return Returns ERR_OK on success, others on failure.
540      */
541     virtual int32_t GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info) override;
542 
543     /**
544      * Get running process information by child process pid.
545      *
546      * @param childPid child process id.
547      * @param info Output parameters, return runningProcessInfo.
548      * @return Returns ERR_OK on success, others on failure.
549      */
550     virtual int32_t GetRunningProcessInfoByChildProcessPid(const pid_t childPid,
551         OHOS::AppExecFwk::RunningProcessInfo &info) override;
552 
553     /**
554      * Notify Fault Data
555      *
556      * @param faultData the fault data.
557      * @return Returns ERR_OK on success, others on failure.
558      */
559     int32_t NotifyAppFault(const FaultData &faultData) override;
560 
561     /**
562      * Notify Fault Data By SA
563      *
564      * @param faultData the fault data notified by SA.
565      * @return Returns ERR_OK on success, others on failure.
566      */
567     int32_t NotifyAppFaultBySA(const AppFaultDataBySA &faultData) override;
568 
569     /**
570      * Set Appfreeze Detect Filter
571      *
572      * @param pid the process pid.
573      * @return Returns true on success, others on failure.
574      */
575     bool SetAppFreezeFilter(int32_t pid) override;
576 
577     /**
578      * get memorySize by pid.
579      *
580      * @param pid process id.
581      * @param memorySize Output parameters, return memorySize in KB.
582      * @return Returns ERR_OK on success, others on failure.
583      */
584     virtual int32_t GetProcessMemoryByPid(const int32_t pid, int32_t &memorySize) override;
585 
586     /**
587      * get application processes information list by bundleName.
588      *
589      * @param bundleName Bundle name.
590      * @param userId user Id in Application record.
591      * @param info Output parameters, return running process info list.
592      * @return Returns ERR_OK on success, others on failure.
593      */
594     virtual int32_t GetRunningProcessInformation(
595         const std::string &bundleName, int32_t userId, std::vector<RunningProcessInfo> &info) override;
596 
597     /**
598      * on add systemAbility.
599      *
600      * @return
601      */
602     virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
603 
604     /**
605      * on remove systemAbility.
606      *
607      * @return
608      */
609     virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
610 
611     /**
612      * @brief Notify NativeEngine GC of status change.
613      *
614      * @param state GC state
615      * @param pid pid
616      *
617      * @return Is the status change completed.
618      */
619     virtual int32_t ChangeAppGcState(pid_t pid, int32_t state) override;
620 
621     /**
622      * Register appRunning status listener.
623      *
624      * @param listener Running status listener.
625      * @return Returns ERR_OK on success, others on failure.
626      */
627     int32_t RegisterAppRunningStatusListener(const sptr<IRemoteObject> &listener) override;
628 
629     /**
630      * Unregister appRunning status listener.
631      *
632      * @param listener Running status listener.
633      * @return Returns ERR_OK on success, others on failure.
634      */
635     int32_t UnregisterAppRunningStatusListener(const sptr<IRemoteObject> &listener) override;
636 
637     /**
638      * Register application foreground state observer.
639      * @param observer Is App Foreground Statue Observer
640      * @return Returns ERR_OK on success, others on failure.
641      */
642     int32_t RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer) override;
643 
644     /**
645      * Unregister application foreground state observer.
646      * @param observer Is App Foreground Statue Observer
647      * @return Returns ERR_OK on success, others on failure.
648      */
649     int32_t UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer) override;
650 
651 #ifdef SUPPORT_CHILD_PROCESS
652     /**
653      * Start child process, called by ChildProcessManager.
654      *
655      * @param childPid Created child process pid.
656      * @param request Child process start request params.
657      * @return Returns ERR_OK on success, others on failure.
658      */
659     int32_t StartChildProcess(pid_t &childPid, const ChildProcessRequest &request) override;
660 
661     /**
662      * Get child process record for self.
663      *
664      * @return child process info.
665      */
666     int32_t GetChildProcessInfoForSelf(ChildProcessInfo &info) override;
667 
668     /**
669      * Attach child process scheduler to app manager service.
670      *
671      * @param childScheduler scheduler of child process.
672      */
673     void AttachChildProcess(const sptr<IRemoteObject> &childScheduler) override;
674 
675     /**
676      * Exit child process, called by itself.
677      */
678     void ExitChildProcessSafely() override;
679 #endif // SUPPORT_CHILD_PROCESS
680 
681     /**
682      * @brief register a render process state observer to receive change.
683      * @param observer the render process state observer to receive notify.
684      *
685      * @return Returns ERR_OK on success, others on failure.
686      */
687     int32_t RegisterRenderStateObserver(const sptr<IRenderStateObserver> &observer) override;
688 
689     /**
690      * @brief unregister a render process state observer registered before.
691      * @param observer the render process state observer registered before.
692      *
693      * @return Returns ERR_OK on success, others on failure.
694      */
695     int32_t UnregisterRenderStateObserver(const sptr<IRenderStateObserver> &observer) override;
696 
697     /**
698      * @brief update a render process state.
699      * @param renderPid the pid of the render process to be updated.
700      * @param state the new state value of the render process.
701      *
702      * @return Returns ERR_OK on success, others on failure.
703      */
704     int32_t UpdateRenderState(pid_t renderPid, int32_t state) override;
705 
706     /**
707      * @brief mark a process which is going restart.
708      * @param uid the uid of the process.
709      * @param instanceKey the instance key of the process.
710      *
711      * @return Returns ERR_OK on success, others on failure.
712      */
713     int32_t SignRestartAppFlag(int32_t uid, const std::string &instanceKey) override;
714 
715     /**
716      * Get appRunningUniqueId by pid.
717      * @param pid pid.
718      * @param appRunningUniqueId appRunningUniqueId.
719      * @return Returns ERR_OK on success, others on failure.
720      */
721     int32_t GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId) override;
722 
723     /*
724      * Get all uiextension root host process id, need apply permission ohos.permission.GET_RUNNING_INFO.
725      * If specified pid mismatch UIExtensionAbility type, return empty vector.
726      * @param pid Process id.
727      * @param hostPids All host process id.
728      * @return Returns ERR_OK on success, others on failure.
729      */
730     int32_t GetAllUIExtensionRootHostPid(pid_t pid, std::vector<pid_t> &hostPids) override;
731 
732     /**
733      * Get all uiextension provider process id, need apply permission ohos.permission.GET_RUNNING_INFO.
734      * If specified hostPid didn't start any UIExtensionAbility, return empty vector.
735      * @param hostPid Host process id.
736      * @param providerPids All provider process id started by specified hostPid.
737      * @return Returns ERR_OK on success, others on failure.
738      */
739     int32_t GetAllUIExtensionProviderPid(pid_t hostPid, std::vector<pid_t> &providerPids) override;
740 
741     /**
742      * @brief Notify memory size state changed to sufficient or insufficient.
743      * @param isMemorySizeSufficient Indicates the memory size state.
744      * @return Returns ERR_OK on success, others on failure.
745      */
746     int32_t NotifyMemorySizeStateChanged(bool isMemorySizeSufficient) override;
747 
748     /**
749      * Set application assertion pause state.
750      *
751      * @param flag assertion pause state.
752      */
753     void SetAppAssertionPauseState(bool flag) override;
754 
755     /**
756      * @brief set support process cache by self
757      */
758     int32_t SetSupportedProcessCacheSelf(bool isSupport) override;
759 
760     int32_t SetSupportedProcessCache(int32_t pid, bool isSupport) override;
761 
762     /**
763      * set browser channel for caller
764      */
765     virtual void SaveBrowserChannel(sptr<IRemoteObject> browser) override;
766 
767     /**
768      * Check caller is test ability
769      *
770      * @param pid, the pid of ability.
771      * @return Returns ERR_OK is test ability, others is not test ability.
772      */
773     int32_t CheckCallingIsUserTestMode(const pid_t pid, bool &isUserTest) override;
774 
775 #ifdef SUPPORT_CHILD_PROCESS
776     /**
777      * Start native child process, callde by ChildProcessManager.
778      * @param libName lib file name to be load in child process
779      * @param childProcessCount current started child process count
780      * @param callback callback for notify start result
781      * @return Returns ERR_OK on success, others on failure.
782      */
783     int32_t StartNativeChildProcess(const std::string &libName, int32_t childProcessCount,
784         const sptr<IRemoteObject> &callback) override;
785 #endif // SUPPORT_CHILD_PROCESS
786 
787     /**
788      * Notify that the process depends on web by itself.
789      */
790     virtual int32_t NotifyProcessDependedOnWeb() override;
791 
792     /**
793      * Kill process depended on web by sa.
794      */
795     virtual void KillProcessDependedOnWeb() override;
796 
797     /**
798      * Restart resident process depended on web.
799      */
800     virtual void RestartResidentProcessDependedOnWeb() override;
801 
802     virtual int32_t KillAppSelfWithInstanceKey(const std::string &instanceKey, bool clearPageStack,
803         const std::string& reason) override;
804 
805     virtual void UpdateInstanceKeyBySpecifiedId(int32_t specifiedId, std::string &instanceKey) override;
806 
807     int32_t IsSpecifiedModuleLoaded(const AAFwk::Want &want, const AbilityInfo &abilityInfo, bool &result) override;
808 
809     /**
810      * update process rss and pss value.
811      *
812      * @param procMemStates, the memory states of all apps.
813      * @return ERR_OK, return back success, others fail.
814      */
815     virtual int32_t UpdateProcessMemoryState(const std::vector<ProcessMemoryState> &procMemState) override;
816 
817     /**
818      * Get killed process info by process id and uid.
819      *
820      * @param pid Process id.
821      * @param uid Process uid.
822      * @param info result KilledProcessInfo.
823      * @return Returns ERR_OK on success, others on failure.
824      */
825     int32_t GetKilledProcessInfo(int pid, int uid, KilledProcessInfo &info) override;
826 private:
827     /**
828      * Init, Initialize application services.
829      *
830      * @return ERR_OK, return back success, others fail.
831      */
832     ErrCode Init();
833 
834     // the function that overrode from SystemAbility
835     /**
836      * OnStart, Start application service.
837      *
838      * @return
839      */
840     virtual void OnStart() override;
841 
842     /**
843      * OnStop, Stop application service.
844      *
845      * @return
846      */
847     virtual void OnStop() override;
848 
849     /**
850      * @brief Judge whether the application service is ready.
851      *
852      * @return Returns true means service is ready, otherwise service is not ready.
853      */
854     bool IsReady() const;
855 
856     /**
857      * SetInnerService, Setting application service Inner instance.
858      *
859      * @return
860      */
861     void SetInnerService(const std::shared_ptr<AppMgrServiceInner> &innerService);
862 
863     /**
864      * Register application or process state observer.
865      * @param observer, ability token.
866      * @return Returns ERR_OK on success, others on failure.
867      */
868     virtual int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer,
869         const std::vector<std::string> &bundleNameList = {}) override;
870 
871     /**
872      * Unregister application or process state observer.
873      * @param observer, ability token.
874      * @return Returns ERR_OK on success, others on failure.
875      */
876     virtual int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer) override;
877 
878     /**
879      * Register application or process state observer.
880      * @param observer, Is ability foreground state observer
881      * @return Returns ERR_OK on success, others on failure.
882      */
883     int32_t RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer) override;
884 
885     /**
886      * Unregister application or process state observer.
887      * @param observer, Is ability foreground state observer
888      * @return Returns ERR_OK on success, others on failure.
889      */
890     int32_t UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer) override;
891 
892     /**
893      * Get foreground applications.
894      * @param list, foreground apps.
895      * @return Returns ERR_OK on success, others on failure.
896      */
897     virtual int32_t GetForegroundApplications(std::vector<AppStateData> &list) override;
898 
899     int Dump(const std::vector<std::u16string>& args, std::string& result);
900     int ShowHelp(const std::vector<std::u16string>& args, std::string& result);
901     int DumpIpc(const std::vector<std::u16string>& args, std::string& result);
902     int DumpIpcAllStart(std::string& result);
903     int DumpIpcAllStop(std::string& result);
904     int DumpIpcAllStat(std::string& result);
905     int DumpIpcStart(const int32_t pid, std::string& result);
906     int DumpIpcStop(const int32_t pid, std::string& result);
907     int DumpIpcStat(const int32_t pid, std::string& result);
908 
909     int DumpFfrt(const std::vector<std::u16string>& args, std::string& result);
910 
911     bool JudgeAppSelfCalled(int32_t recordId);
912 
913     /**
914      * @brief Notify AbilityManagerService the page show.
915      * @param token Ability identify.
916      * @param pageStateData The data of ability's page state.
917      * @return Returns ERR_OK on success, others on failure.
918      */
919     virtual int32_t NotifyPageShow(const sptr<IRemoteObject> &token, const PageStateData &pageStateData) override;
920 
921     /**
922      * @brief Notify AbilityManagerService the page hide.
923      * @param token Ability identify.
924      * @param pageStateData The data of ability's page state.
925      * @return Returns ERR_OK on success, others on failure.
926      */
927     virtual int32_t NotifyPageHide(const sptr<IRemoteObject> &token, const PageStateData &pageStateData) override;
928 
929     /**
930      * Check whether the bundle is running.
931      *
932      * @param bundleName Indicates the bundle name of the bundle.
933      * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise.
934      * @return Return ERR_OK if success, others fail.
935      */
936     int32_t IsApplicationRunning(const std::string &bundleName, bool &isRunning) override;
937 
938     /**
939      * Check whether the bundle is running.
940      *
941      * @param bundleName Indicates the bundle name of the bundle.
942      * @param appCloneIndex the appindex of the bundle.
943      * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise.
944      * @return Return ERR_OK if success, others fail.
945      */
946     int32_t IsAppRunning(const std::string &bundleName, int32_t appCloneIndex, bool &isRunning) override;
947 
948     /**
949      * Check whether the process of the application under the specified user exists.
950      *
951      * @param bundleName Indicates the bundle name of the bundle.
952      * @param userId the userId of the bundle.
953      * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise.
954      * @return Return ERR_OK if success, others fail.
955      */
956     int32_t IsAppRunningByBundleNameAndUserId(const std::string &bundleName, int32_t userId, bool &isRunning) override;
957 
958     /**
959      * Whether the current application process is the last surviving process.
960      *
961      * @return Returns true is final application process, others return false.
962      */
963     bool IsFinalAppProcess() override;
964 
965     /**
966      * Get pids of processes which belong to specific bundle name and support process cache feature.
967      *
968      * @param bundleName bundle name.
969      * @param pidList pid list of processes that support process cache.
970      * @return Returns ERR_OK on success, others on failure.
971      */
972     virtual int32_t GetSupportedProcessCachePids(const std::string &bundleName,
973         std::vector<int32_t> &pidList) override;
974 
975     enum DumpIpcKey {
976         KEY_DUMP_IPC_START = 0,
977         KEY_DUMP_IPC_STOP,
978         KEY_DUMP_IPC_STAT,
979     };
980 
981 private:
982     int DumpIpcAllInner(const AppMgrService::DumpIpcKey key, std::string& result);
983     int DumpIpcWithPidInner(const AppMgrService::DumpIpcKey key,
984         const std::string& optionPid, std::string& result);
985 
986     int DumpFfrtInner(const std::string& pidsRaw, std::string& result);
987 
988 private:
989     AppMgrServiceState appMgrServiceState_;
990     std::shared_ptr<AppMgrServiceInner> appMgrServiceInner_;
991     std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
992     std::shared_ptr<AMSEventHandler> eventHandler_;
993     sptr<ISystemAbilityManager> systemAbilityMgr_;
994     sptr<IAmsMgr> amsMgrScheduler_;
995 
996     bool GetDumpIpcKeyByOption(const std::string &option, DumpIpcKey &key);
997 
998     DISALLOW_COPY_AND_MOVE(AppMgrService);
999 };
1000 }  // namespace AppExecFwk
1001 }  // namespace OHOS
1002 #endif  // OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H
1003