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      * GetProcessRunningInfosByUserId, call GetProcessRunningInfosByUserId() through proxy project.
225      * Obtains information about application processes that are running on the device.
226      *
227      * @param info, app name in Application record.
228      * @param userId, userId.
229      *
230      * @return Returns ERR_OK on success, others on failure.
231      */
232     virtual int32_t GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId) override;
233 
234     /**
235      * GetProcessRunningInformation, call GetProcessRunningInformation() through proxy project.
236      * Obtains information about current application process which is running on the device.
237      *
238      * @param info, app name in Application record.
239      *
240      * @return Returns ERR_OK on success, others on failure.
241      */
242     virtual int32_t GetProcessRunningInformation(RunningProcessInfo &info) override;
243 
244     /**
245      * NotifyMemoryLevel, call NotifyMemoryLevel() through proxy project.
246      * Notify applications background the current memory level.
247      *
248      * @param level, current memory level.
249      * @return Returns ERR_OK on success, others on failure.
250      */
251     virtual int32_t NotifyMemoryLevel(int32_t level) override;
252 
253     /**
254      * NotifyProcMemoryLevel, call NotifyMemoryLevel() through proxy project.
255      * Notify applications the current memory level.
256      *
257      * @param  procLevelMap , <pid_t, MemoryLevel> map.
258      * @return Returns ERR_OK on success, others on failure.
259      */
260     virtual int32_t NotifyProcMemoryLevel(const std::map<pid_t, MemoryLevel> &procLevelMap) override;
261 
262     /**
263      * DumpHeapMemory, call DumpHeapMemory() through proxy project.
264      * Get the application's memory allocation info.
265      *
266      * @param pid, pid input.
267      * @param mallocInfo, dynamic storage information output.
268      * @return Returns ERR_OK on success, others on failure.
269      */
270     virtual int32_t DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo) override;
271 
272     /**
273      * Authenticate dump permissions
274      *
275      * @return Returns true on permission, others on false
276      */
277     bool HasDumpPermission() const;
278     /**
279      * DumpJsHeapMemory, call DumpJsHeapMemory() through proxy project.
280      * triggerGC and dump the application's jsheap memory info.
281      *
282      * @param info, pid, tid, needGc, needSnapshot
283      * @return Returns ERR_OK on success, others on failure.
284      */
285     virtual int32_t DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info) override;
286 
287     // the function about service running info
288     /**
289      * QueryServiceState, Query application service status.
290      *
291      * @return the application service status.
292      */
293     AppMgrServiceState QueryServiceState();
294 
295     /**
296      * GetAmsMgr, call GetAmsMgr() through proxy object, get AMS interface instance.
297      *
298      * @return sptr<IAmsMgr>, return to AMS interface instance.
299      */
300     virtual sptr<IAmsMgr> GetAmsMgr() override;
301 
302     /**
303      * Notify that the ability stage has been updated
304      * @param recordId, the app record.
305      */
306     virtual void AddAbilityStageDone(const int32_t recordId) override;
307 
308     /**
309      * Start all resident process
310      */
311     virtual void StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos) override;
312 
313     /**
314      * Start user test process.
315      * @param want, want object.
316      * @param observer, test observer remote object.
317      * @param bundleInfo, bundle info.
318      * @param userId the user id.
319      * @return Returns ERR_OK on success, others on failure.
320      */
321     virtual int StartUserTestProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer,
322         const AppExecFwk::BundleInfo &bundleInfo, int32_t userId) override;
323 
324     /**
325      * @brief Finish user test.
326      * @param msg user test message.
327      * @param resultCode user test result Code.
328      * @param bundleName user test bundleName.
329      *
330      * @return Returns ERR_OK on success, others on failure.
331      */
332     virtual int FinishUserTest(
333         const std::string &msg, const int64_t &resultCode, const std::string &bundleName) override;
334 
335     /**
336      * @brief Application hidumper.
337      * @param fd Indicates the fd.
338      * @param args Indicates the params.
339      * @return Returns the dump result.
340      */
341     int Dump(int fd, const std::vector<std::u16string>& args) override;
342 
343     /**
344      * @brief called when the abilityStage's onAcceptWant lifecycle completed.
345      * @param recordId record id of the ability.
346      * @param want want has been accepted.
347      * @param flag flag get from OnAcceptWant.
348      */
349     virtual void ScheduleAcceptWantDone(
350         const int32_t recordId, const AAFwk::Want &want, const std::string &flag) override;
351 
352     virtual void ScheduleNewProcessRequestDone(
353         const int32_t recordId, const AAFwk::Want &want, const std::string &flag) override;
354 
355     /**
356      *  Get the token of ability records by process ID.
357      *
358      * @param pid The process id.
359      * @param tokens The token of ability records.
360      * @return Returns ERR_OK on success, others on failure.
361      */
362     virtual int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens) override;
363 
364     /**
365      * Prestart nwebspawn process.
366      *
367      * @return Returns ERR_OK on success, others on failure.
368      */
369     virtual int PreStartNWebSpawnProcess() override;
370 
371     /**
372      * Start nweb render process, called by nweb host.
373      *
374      * @param renderParam, params passed to renderProcess.
375      * @param ipcFd, ipc file descriptor for web browser and render process.
376      * @param sharedFd, shared memory file descriptor.
377      * @param crashFd, crash signal file descriptor.
378      * @param renderPid, created render pid.
379      * @param isGPU, is or not gpu process
380      * @return Returns ERR_OK on success, others on failure.
381      */
382     virtual int StartRenderProcess(const std::string &renderParam,
383                                    int32_t ipcFd, int32_t sharedFd,
384                                    int32_t crashFd, pid_t &renderPid, bool isGPU = false) override;
385 
386     /**
387      * Render process call this to attach to app manager service.
388      *
389      * @param renderScheduler scheduler of render process.
390      */
391     virtual void AttachRenderProcess(const sptr<IRemoteObject> &shceduler) override;
392 
393     /**
394      * Get render process termination status, called by nweb host.
395      *
396      * @param renderPid, target render pid.
397      * @param status, termination status of the render process.
398      * @return Returns ERR_OK on success, others on failure.
399      */
400     virtual int GetRenderProcessTerminationStatus(pid_t renderPid, int &status) override;
401 
402     /**
403      * GetConfiguration
404      *
405      * @param info to retrieve configuration data.
406      * @return ERR_OK ,return back success,others fail.
407      */
408     virtual int32_t GetConfiguration(Configuration& config) override;
409 
410     /**
411      * UpdateConfiguration, ANotify application update system environment changes.
412      *
413      * @param config System environment change parameters.
414      * @param userId configuration for the user
415      * @return Returns ERR_OK on success, others on failure.
416      */
417     virtual int32_t UpdateConfiguration(const Configuration &config, const int32_t userId = -1) override;
418 
419     /**
420      *  Update config by bundle name.
421      *
422      * @param config Application environment change parameters.
423      * @param name Application bundle name.
424      * @return Returns ERR_OK on success, others on failure.
425      */
426     virtual int32_t UpdateConfigurationByBundleName(const Configuration &config, const std::string &name) override;
427 
428     /**
429      * @brief register a configuration observer which will receive notifies when updated.
430      * @param observer the configuration observer to receive notify.
431      *
432      * @return Returns ERR_OK on success, others on failure.
433      */
434     virtual int32_t RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override;
435 
436     /**
437      * @brief unregister a configuration observer registered before.
438      * @param observer the configuration observer registered before.
439      *
440      * @return Returns ERR_OK on success, others on failure.
441      */
442     virtual int32_t UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override;
443 
444     /**
445      * Register KIA interceptor.
446      * @param interceptor KIA interceptor.
447      * @return Returns ERR_OK on success, others on failure.
448      */
449     virtual int32_t RegisterKiaInterceptor(const sptr<IKiaInterceptor> &interceptor) override;
450 
451     /**
452      * Check if the given pid is a KIA process.
453      * @param pid process id.
454      * @return Returns true if it is a KIA process, false otherwise.
455      */
456     virtual int32_t CheckIsKiaProcess(pid_t pid, bool &isKia) override;
457 
458     /**
459      * @brief Get the running state of application by bundle name.
460      *
461      * @param bundleName Bundle name
462      * @return Returns true if process is running, false if process isn't running.
463      */
464     bool GetAppRunningStateByBundleName(const std::string &bundleName) override;
465 
466     /**
467      * @brief Notify application load patch.
468      *
469      * @param bundleName Bundle name
470      * @param callback called when LoadPatch finished.
471      * @return Returns ERR_OK on success, error code on failure.
472      */
473     int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override;
474 
475     /**
476      * @brief Notify application reload page.
477      *
478      * @param bundleName Bundle name
479      * @param callback called when HotReload finished.
480      * @return Returns ERR_OK on success, error code on failure.
481      */
482     int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override;
483 
484     /**
485      * @brief Notify application unload patch.
486      *
487      * @param bundleName Bundle name
488      * @param callback called when UnloadPatch finished.
489      * @return Returns ERR_OK on success, error code on failure.
490      */
491     int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override;
492 
493 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
494     int32_t SetContinuousTaskProcess(int32_t pid, bool isContinuousTask) override;
495 #endif
496 
497     /**
498      * @brief Check whether the shared bundle is running.
499      *
500      * @param bundleName Shared bundle name.
501      * @param versionCode Shared bundle version code.
502      * @return Returns the shared bundle running result. The result is true if running, false otherwise.
503      */
504     virtual bool IsSharedBundleRunning(const std::string &bundleName, uint32_t versionCode) override;
505 
506     /**
507      * start native process for debugger.
508      *
509      * @param want param to start a process.
510      */
511     virtual int32_t StartNativeProcessForDebugger(const AAFwk::Want &want) override;
512 
513     /**
514      * Get bundleName by pid.
515      *
516      * @param pid process id.
517      * @param bundleName Output parameters, return bundleName.
518      * @param uid Output parameters, return userId.
519      * @return Returns ERR_OK on success, others on failure.
520      */
521     virtual int32_t GetBundleNameByPid(const int32_t pid, std::string &bundleName, int32_t &uid) override;
522 
523     /**
524      * Get running process information by pid.
525      *
526      * @param pid process id.
527      * @param info Output parameters, return runningProcessInfo.
528      * @return Returns ERR_OK on success, others on failure.
529      */
530     virtual int32_t GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info) override;
531 
532     /**
533      * Notify Fault Data
534      *
535      * @param faultData the fault data.
536      * @return Returns ERR_OK on success, others on failure.
537      */
538     int32_t NotifyAppFault(const FaultData &faultData) override;
539 
540     /**
541      * Notify Fault Data By SA
542      *
543      * @param faultData the fault data notified by SA.
544      * @return Returns ERR_OK on success, others on failure.
545      */
546     int32_t NotifyAppFaultBySA(const AppFaultDataBySA &faultData) override;
547 
548     /**
549      * Set Appfreeze Detect Filter
550      *
551      * @param pid the process pid.
552      * @return Returns true on success, others on failure.
553      */
554     bool SetAppFreezeFilter(int32_t pid) override;
555 
556     /**
557      * get memorySize by pid.
558      *
559      * @param pid process id.
560      * @param memorySize Output parameters, return memorySize in KB.
561      * @return Returns ERR_OK on success, others on failure.
562      */
563     virtual int32_t GetProcessMemoryByPid(const int32_t pid, int32_t &memorySize) override;
564 
565     /**
566      * get application processes information list by bundleName.
567      *
568      * @param bundleName Bundle name.
569      * @param userId user Id in Application record.
570      * @param info Output parameters, return running process info list.
571      * @return Returns ERR_OK on success, others on failure.
572      */
573     virtual int32_t GetRunningProcessInformation(
574         const std::string &bundleName, int32_t userId, std::vector<RunningProcessInfo> &info) override;
575 
576     /**
577      * on add systemAbility.
578      *
579      * @return
580      */
581     virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
582 
583     /**
584      * on remove systemAbility.
585      *
586      * @return
587      */
588     virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
589 
590     /**
591      * @brief Notify NativeEngine GC of status change.
592      *
593      * @param state GC state
594      * @param pid pid
595      *
596      * @return Is the status change completed.
597      */
598     virtual int32_t ChangeAppGcState(pid_t pid, int32_t state) override;
599 
600     /**
601      * Register appRunning status listener.
602      *
603      * @param listener Running status listener.
604      * @return Returns ERR_OK on success, others on failure.
605      */
606     int32_t RegisterAppRunningStatusListener(const sptr<IRemoteObject> &listener) override;
607 
608     /**
609      * Unregister appRunning status listener.
610      *
611      * @param listener Running status listener.
612      * @return Returns ERR_OK on success, others on failure.
613      */
614     int32_t UnregisterAppRunningStatusListener(const sptr<IRemoteObject> &listener) override;
615 
616     /**
617      * Register application foreground state observer.
618      * @param observer Is App Foreground Statue Observer
619      * @return Returns ERR_OK on success, others on failure.
620      */
621     int32_t RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer) override;
622 
623     /**
624      * Unregister application foreground state observer.
625      * @param observer Is App Foreground Statue Observer
626      * @return Returns ERR_OK on success, others on failure.
627      */
628     int32_t UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer) override;
629 
630     /**
631      * Start child process, called by ChildProcessManager.
632      *
633      * @param childPid Created child process pid.
634      * @param request Child process start request params.
635      * @return Returns ERR_OK on success, others on failure.
636      */
637     int32_t StartChildProcess(pid_t &childPid, const ChildProcessRequest &request) override;
638 
639     /**
640      * Get child process record for self.
641      *
642      * @return child process info.
643      */
644     int32_t GetChildProcessInfoForSelf(ChildProcessInfo &info) override;
645 
646     /**
647      * Attach child process scheduler to app manager service.
648      *
649      * @param childScheduler scheduler of child process.
650      */
651     void AttachChildProcess(const sptr<IRemoteObject> &childScheduler) override;
652 
653     /**
654      * Exit child process, called by itself.
655      */
656     void ExitChildProcessSafely() override;
657 
658     /**
659      * @brief register a render process state observer to receive change.
660      * @param observer the render process state observer to receive notify.
661      *
662      * @return Returns ERR_OK on success, others on failure.
663      */
664     int32_t RegisterRenderStateObserver(const sptr<IRenderStateObserver> &observer) override;
665 
666     /**
667      * @brief unregister a render process state observer registered before.
668      * @param observer the render process state observer registered before.
669      *
670      * @return Returns ERR_OK on success, others on failure.
671      */
672     int32_t UnregisterRenderStateObserver(const sptr<IRenderStateObserver> &observer) override;
673 
674     /**
675      * @brief update a render process state.
676      * @param renderPid the pid of the render process to be updated.
677      * @param state the new state value of the render process.
678      *
679      * @return Returns ERR_OK on success, others on failure.
680      */
681     int32_t UpdateRenderState(pid_t renderPid, int32_t state) override;
682 
683     /**
684      * @brief mark a process which is going restart.
685      * @param uid the uid of the process.
686      *
687      * @return Returns ERR_OK on success, others on failure.
688      */
689     int32_t SignRestartAppFlag(int32_t uid) override;
690 
691     /**
692      * Get appRunningUniqueId by pid.
693      * @param pid pid.
694      * @param appRunningUniqueId appRunningUniqueId.
695      * @return Returns ERR_OK on success, others on failure.
696      */
697     int32_t GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId) override;
698 
699     /*
700      * Get all uiextension root host process id, need apply permission ohos.permission.GET_RUNNING_INFO.
701      * If specified pid mismatch UIExtensionAbility type, return empty vector.
702      * @param pid Process id.
703      * @param hostPids All host process id.
704      * @return Returns ERR_OK on success, others on failure.
705      */
706     int32_t GetAllUIExtensionRootHostPid(pid_t pid, std::vector<pid_t> &hostPids) override;
707 
708     /**
709      * Get all uiextension provider process id, need apply permission ohos.permission.GET_RUNNING_INFO.
710      * If specified hostPid didn't start any UIExtensionAbility, return empty vector.
711      * @param hostPid Host process id.
712      * @param providerPids All provider process id started by specified hostPid.
713      * @return Returns ERR_OK on success, others on failure.
714      */
715     int32_t GetAllUIExtensionProviderPid(pid_t hostPid, std::vector<pid_t> &providerPids) override;
716 
717     /**
718      * @brief Notify memory size state changed to sufficient or insufficient.
719      * @param isMemorySizeSufficient Indicates the memory size state.
720      * @return Returns ERR_OK on success, others on failure.
721      */
722     int32_t NotifyMemorySizeStateChanged(bool isMemorySizeSufficient) override;
723 
724     /**
725      * Set application assertion pause state.
726      *
727      * @param flag assertion pause state.
728      */
729     void SetAppAssertionPauseState(bool flag) override;
730 
731     /**
732      * @brief set support process cache by self
733      */
734     int32_t SetSupportedProcessCacheSelf(bool isSupport) override;
735 
736     int32_t SetSupportedProcessCache(int32_t pid, bool isSupport) override;
737 
738     /**
739      * set browser channel for caller
740      */
741     virtual void SaveBrowserChannel(sptr<IRemoteObject> browser) override;
742 
743     /**
744      * Check caller is test ability
745      *
746      * @param pid, the pid of ability.
747      * @return Returns ERR_OK is test ability, others is not test ability.
748      */
749     int32_t CheckCallingIsUserTestMode(const pid_t pid, bool &isUserTest) override;
750     /**
751      * Start native child process, callde by ChildProcessManager.
752      * @param libName lib file name to be load in child process
753      * @param childProcessCount current started child process count
754      * @param callback callback for notify start result
755      * @return Returns ERR_OK on success, others on failure.
756      */
757     int32_t StartNativeChildProcess(const std::string &libName, int32_t childProcessCount,
758         const sptr<IRemoteObject> &callback) override;
759 
760     /**
761      * Notify that the process depends on web by itself.
762      */
763     virtual int32_t NotifyProcessDependedOnWeb() override;
764 
765     /**
766      * Kill process depended on web by sa.
767      */
768     virtual void KillProcessDependedOnWeb() override;
769 
770     /**
771      * Restart resident process depended on web.
772      */
773     virtual void RestartResidentProcessDependedOnWeb() override;
774 
775     int32_t GetAppIndexByPid(pid_t pid, int32_t &appIndex) override;
776 private:
777     /**
778      * Init, Initialize application services.
779      *
780      * @return ERR_OK, return back success, others fail.
781      */
782     ErrCode Init();
783 
784     // the function that overrode from SystemAbility
785     /**
786      * OnStart, Start application service.
787      *
788      * @return
789      */
790     virtual void OnStart() override;
791 
792     /**
793      * OnStop, Stop application service.
794      *
795      * @return
796      */
797     virtual void OnStop() override;
798 
799     /**
800      * @brief Judge whether the application service is ready.
801      *
802      * @return Returns true means service is ready, otherwise service is not ready.
803      */
804     bool IsReady() const;
805 
806     /**
807      * SetInnerService, Setting application service Inner instance.
808      *
809      * @return
810      */
811     void SetInnerService(const std::shared_ptr<AppMgrServiceInner> &innerService);
812 
813     /**
814      * Register application or process state observer.
815      * @param observer, ability token.
816      * @return Returns ERR_OK on success, others on failure.
817      */
818     virtual int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer,
819         const std::vector<std::string> &bundleNameList = {}) override;
820 
821     /**
822      * Unregister application or process state observer.
823      * @param observer, ability token.
824      * @return Returns ERR_OK on success, others on failure.
825      */
826     virtual int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer) override;
827 
828     /**
829      * Register application or process state observer.
830      * @param observer, Is ability foreground state observer
831      * @return Returns ERR_OK on success, others on failure.
832      */
833     int32_t RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer) override;
834 
835     /**
836      * Unregister application or process state observer.
837      * @param observer, Is ability foreground state observer
838      * @return Returns ERR_OK on success, others on failure.
839      */
840     int32_t UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer) override;
841 
842     /**
843      * Get foreground applications.
844      * @param list, foreground apps.
845      * @return Returns ERR_OK on success, others on failure.
846      */
847     virtual int32_t GetForegroundApplications(std::vector<AppStateData> &list) override;
848 
849     int Dump(const std::vector<std::u16string>& args, std::string& result);
850     int ShowHelp(const std::vector<std::u16string>& args, std::string& result);
851     int DumpIpc(const std::vector<std::u16string>& args, std::string& result);
852     int DumpIpcAllStart(std::string& result);
853     int DumpIpcAllStop(std::string& result);
854     int DumpIpcAllStat(std::string& result);
855     int DumpIpcStart(const int32_t pid, std::string& result);
856     int DumpIpcStop(const int32_t pid, std::string& result);
857     int DumpIpcStat(const int32_t pid, std::string& result);
858 
859     int DumpFfrt(const std::vector<std::u16string>& args, std::string& result);
860 
861     bool JudgeAppSelfCalled(int32_t recordId);
862 
863     /**
864      * @brief Notify AbilityManagerService the page show.
865      * @param token Ability identify.
866      * @param pageStateData The data of ability's page state.
867      * @return Returns ERR_OK on success, others on failure.
868      */
869     virtual int32_t NotifyPageShow(const sptr<IRemoteObject> &token, const PageStateData &pageStateData) override;
870 
871     /**
872      * @brief Notify AbilityManagerService the page hide.
873      * @param token Ability identify.
874      * @param pageStateData The data of ability's page state.
875      * @return Returns ERR_OK on success, others on failure.
876      */
877     virtual int32_t NotifyPageHide(const sptr<IRemoteObject> &token, const PageStateData &pageStateData) override;
878 
879     /**
880      * Check whether the bundle is running.
881      *
882      * @param bundleName Indicates the bundle name of the bundle.
883      * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise.
884      * @return Return ERR_OK if success, others fail.
885      */
886     int32_t IsApplicationRunning(const std::string &bundleName, bool &isRunning) override;
887 
888     /**
889      * Check whether the bundle is running.
890      *
891      * @param bundleName Indicates the bundle name of the bundle.
892      * @param appCloneIndex the appindex of the bundle.
893      * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise.
894      * @return Return ERR_OK if success, others fail.
895      */
896     int32_t IsAppRunning(const std::string &bundleName, int32_t appCloneIndex, bool &isRunning) override;
897 
898     /**
899      * Whether the current application process is the last surviving process.
900      *
901      * @return Returns true is final application process, others return false.
902      */
903     bool IsFinalAppProcess() override;
904 
905     /**
906      * Get pids of processes which belong to specific bundle name and support process cache feature.
907      *
908      * @param bundleName bundle name.
909      * @param pidList pid list of processes that support process cache.
910      * @return Returns ERR_OK on success, others on failure.
911      */
912     virtual int32_t GetSupportedProcessCachePids(const std::string &bundleName,
913         std::vector<int32_t> &pidList) override;
914 
915     enum DumpIpcKey {
916         KEY_DUMP_IPC_START = 0,
917         KEY_DUMP_IPC_STOP,
918         KEY_DUMP_IPC_STAT,
919     };
920 
921 private:
922     int DumpIpcAllInner(const AppMgrService::DumpIpcKey key, std::string& result);
923     int DumpIpcWithPidInner(const AppMgrService::DumpIpcKey key,
924         const std::string& optionPid, std::string& result);
925 
926     int DumpFfrtInner(const std::string& pidsRaw, std::string& result);
927 
928 private:
929     std::shared_ptr<AppMgrServiceInner> appMgrServiceInner_;
930     AppMgrServiceState appMgrServiceState_;
931     std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
932     std::shared_ptr<AMSEventHandler> eventHandler_;
933     sptr<ISystemAbilityManager> systemAbilityMgr_;
934     sptr<IAmsMgr> amsMgrScheduler_;
935 
936     bool GetDumpIpcKeyByOption(const std::string &option, DumpIpcKey &key);
937 
938     DISALLOW_COPY_AND_MOVE(AppMgrService);
939 };
940 }  // namespace AppExecFwk
941 }  // namespace OHOS
942 #endif  // OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H
943