• 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_INNER_H
17 #define OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_INNER_H
18 
19 #include <list>
20 #include <map>
21 #include <mutex>
22 #include <regex>
23 #include <unordered_map>
24 #include <unordered_set>
25 #include <vector>
26 
27 #include "ability_debug_response_interface.h"
28 #include "ability_foreground_state_observer_interface.h"
29 #include "ability_info.h"
30 #include "advanced_security_mode_manager.h"
31 #include "app_death_recipient.h"
32 #include "app_debug_listener_interface.h"
33 #include "app_debug_manager.h"
34 #include "app_foreground_state_observer_interface.h"
35 #include "app_malloc_info.h"
36 #include "app_mgr_constants.h"
37 #include "app_mgr_event.h"
38 #include "app_preloader.h"
39 #include "app_record_id.h"
40 #include "app_running_manager.h"
41 #include "app_running_record.h"
42 #include "app_running_status_listener_interface.h"
43 #include "app_running_status_module.h"
44 #include "app_scheduler_interface.h"
45 #include "app_spawn_client.h"
46 #include "appexecfwk_errors.h"
47 #include "bundle_info.h"
48 #include "bundle_mgr_helper.h"
49 #include "child_process_info.h"
50 #include "child_process_request.h"
51 #include "cpp/mutex.h"
52 #include "event_report.h"
53 #include "fault_data.h"
54 #include "hisysevent.h"
55 #include "iapp_state_callback.h"
56 #include "iapplication_state_observer.h"
57 #include "iconfiguration_observer.h"
58 #include "iremote_object.h"
59 #include "irender_state_observer.h"
60 #include "istart_specified_ability_response.h"
61 #include "record_query_result.h"
62 #include "refbase.h"
63 #include "remote_client_manager.h"
64 #include "render_process_info.h"
65 #include "running_process_info.h"
66 #include "shared/base_shared_bundle_info.h"
67 #include "task_handler_wrap.h"
68 #include "want.h"
69 #include "app_jsheap_mem_info.h"
70 #include "running_multi_info.h"
71 
72 namespace OHOS {
73 namespace AbilityRuntime {
74 struct LoadParam;
75 }
76 namespace Rosen {
77 class WindowVisibilityInfo;
78 class FocusChangeInfo;
79 }
80 namespace AppExecFwk {
81 using OHOS::AAFwk::Want;
82 class WindowFocusChangedListener;
83 class WindowVisibilityChangedListener;
84 using LoadAbilityTaskFunc = std::function<void()>;
85 constexpr int32_t BASE_USER_RANGE = 200000;
86 
87 class AppMgrServiceInner : public std::enable_shared_from_this<AppMgrServiceInner> {
88 public:
89     struct ConfigurationObserverWithUserId {
90         sptr<IConfigurationObserver> observer;
91         int32_t userId = -1;
92     };
93     struct AppStateCallbackWithUserId {
94         sptr<IAppStateCallback> callback;
95         int32_t userId = -1;
96     };
97 
98     AppMgrServiceInner();
99     virtual ~AppMgrServiceInner();
100 
101     /**
102      * Initialize the object.
103      */
104     void Init();
105     /**
106      * LoadAbility, load the ability that needed to be started.
107      *
108      * @param token, the unique identification to start the ability.
109      * @param preToken, the unique identification to call the ability.
110      * @param abilityInfo, the ability information.
111      * @param appInfo, the app information.
112      * @param want the ability want.
113      *
114      * @return
115      */
116     virtual void LoadAbility(std::shared_ptr<AbilityInfo> abilityInfo, std::shared_ptr<ApplicationInfo> appInfo,
117         std::shared_ptr<AAFwk::Want> want, std::shared_ptr<AbilityRuntime::LoadParam> loadParam);
118 
119     /**
120      * TerminateAbility, terminate the token ability.
121      *
122      * @param token, he unique identification to terminate the ability.
123      * @param clearMissionFlag, indicates whether terminate the ability when clearMission.
124      * @return
125      */
126     virtual void TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag);
127 
128     /**
129      * UpdateAbilityState, update the ability status.
130      *
131      * @param token, the unique identification to update the ability.
132      * @param state, ability status that needs to be updated.
133      *
134      * @return
135      */
136     virtual void UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state);
137 
138     /**
139      * UpdateExtensionState, call UpdateExtensionState() through the proxy object, update the extension status.
140      *
141      * @param token, the unique identification to update the extension.
142      * @param state, extension status that needs to be updated.
143      */
144     virtual void UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state);
145 
146     /**
147      * StateChangedNotifyObserver, Call ability state change.
148      *
149      * @param ability, the ability info.
150      * @param state, the ability state.
151      *
152      * @return
153      */
154     void StateChangedNotifyObserver(
155         const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged);
156 
157     /**
158      * RegisterAppStateCallback, register the callback.
159      *
160      * @param callback, Ams register the callback.
161      *
162      * @return
163      */
164     virtual void RegisterAppStateCallback(const sptr<IAppStateCallback> &callback);
165     void RemoveDeadAppStateCallback(const wptr<IRemoteObject> &remote);
166 
167     /**
168      * AbilityBehaviorAnalysis, ability behavior analysis assistant process optimization.
169      *
170      * @param token, the unique identification to start the ability.
171      * @param preToken, the unique identification to call the ability.
172      * @param visibility, the visibility information about windows info.
173      * @param perceptibility, the Perceptibility information about windows info.
174      * @param connectionState, the service ability connection state.
175      * @return
176      */
177     virtual void AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
178         const int32_t visibility, const int32_t perceptibility, const int32_t connectionState);
179 
180     /**
181      * KillProcessByAbilityToken, kill the process by ability token.
182      *
183      * @param token, the unique identification to the ability.
184      * @return
185      */
186     virtual void KillProcessByAbilityToken(const sptr<IRemoteObject> &token);
187 
188     /**
189      * KillProcessesByUserId, kill the processes by userId.
190      *
191      * @param userId, the user id.
192      * @return
193      */
194     virtual void KillProcessesByUserId(int32_t userId);
195 
196     virtual void KillProcessesByPids(std::vector<int32_t> &pids);
197 
198     virtual void AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken);
199 
200     /**
201      * AttachApplication, get all the information needed to start the Application
202      * (data related to the Application ).
203      *
204      * @param appScheduler, information needed to start the Application.
205      *
206      * @return
207      */
208     virtual void AttachApplication(const pid_t pid, const sptr<IAppScheduler> &appScheduler);
209 
210     /**
211      * Preload application.
212      *
213      * @param bundleName The bundle name of the application to preload.
214      * @param userId Indicates the user identification.
215      * @param preloadMode Preload application mode.
216      * @param appIndex The index of application clone.
217      * @return Returns ERR_OK on success, others on failure.
218      */
219     virtual int32_t PreloadApplication(const std::string &bundleName, int32_t userId,
220         AppExecFwk::PreloadMode preloadMode, int32_t appIndex);
221 
222     /**
223      * ApplicationForegrounded, set the application to Foreground State.
224      *
225      * @param recordId, a unique record that identifies this Application from others.
226      *
227      * @return
228      */
229     virtual void ApplicationForegrounded(const int32_t recordId);
230 
231     /**
232      * ApplicationBackgrounded, set the application to Backgrounded State.
233      *
234      * @param recordId, a unique record that identifies this Application from others.
235      *
236      * @return
237      */
238     virtual void ApplicationBackgrounded(const int32_t recordId);
239 
240     /**
241      * ApplicationTerminated, terminate the application.
242      *
243      * @param recordId, a unique record that identifies this Application from others.
244      *
245      * @return
246      */
247     virtual void ApplicationTerminated(const int32_t recordId);
248 
249     /**
250      * AbilityTerminated, terminate the ability.
251      *
252      * @param token, the unique identification to terminated the ability.
253      *
254      * @return
255      */
256     virtual void AbilityTerminated(const sptr<IRemoteObject> &token);
257 
258      /**
259      * UpdateApplicationInfoInstalled, update the application info after new module installed.
260      *
261      * @param bundleName, bundle name in Application record.
262      * @param  uid, uid.
263      * @return ERR_OK, return back success, others fail.
264      */
265     virtual int32_t UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid);
266 
267     /**
268      * KillApplication, kill the application.
269      *
270      * @param  bundleName, bundle name in Application record.
271      *
272      * @return ERR_OK, return back success, others fail.
273      */
274     virtual int32_t KillApplication(const std::string &bundleName);
275 
276     /**
277      * ForceKillApplication, force kill the application.
278      *
279      * @param  bundleName, bundle name in Application record.
280      * @param  userId, userId.
281      * @param  appIndex, appIndex.
282      * @return ERR_OK, return back success, others fail.
283      */
284     virtual int32_t ForceKillApplication(const std::string &bundleName, const int userId = -1,
285         const int appIndex = 0);
286 
287     /**
288      * KillProcessesByAccessTokenId.
289      *
290      * @param  accessTokenId, accessTokenId.
291      * @return ERR_OK, return back success, others fail.
292      */
293     virtual int32_t KillProcessesByAccessTokenId(const uint32_t accessTokenId);
294 
295     /**
296      * KillApplicationByUid, call KillApplicationByUid() through proxy object, kill the application.
297      *
298      * @param  bundleName, bundle name in Application record.
299      * @param  uid, uid.
300      * @param  reason, caller function name.
301      * @return ERR_OK, return back success, others fail.
302      */
303     virtual int32_t KillApplicationByUid(const std::string &bundleName, const int uid,
304         const std::string& reason = "KillApplicationByUid");
305 
306     virtual int32_t KillApplicationSelf(const std::string& reason = "KillApplicationSelf");
307 
308     /**
309      * KillApplicationByUserId, kill the application by user ID.
310      *
311      * @param bundleName, bundle name in Application record.
312      * @param appCloneIndex the app clone id.
313      * @param userId, user ID.
314      * @param  reason, caller function name.
315      *
316      * @return ERR_OK, return back success, others fail.
317      */
318     virtual int32_t KillApplicationByUserId(const std::string &bundleName, int32_t appCloneIndex, int userId,
319         const std::string& reason = "KillApplicationByUserId");
320 
321     /**
322      * ClearUpApplicationData, clear the application data.
323      *
324      * @param bundleName, bundle name in Application record.
325      * @param callerUid, app uid in Application record.
326      * @param callerPid, app pid in Application record.
327      * @param appCloneIndex the app clone id.
328      * @param userId the user id
329      * @return ERR_OK, return back success, others fail.
330      */
331     virtual int32_t ClearUpApplicationData(const std::string &bundleName,
332         int32_t callerUid, pid_t callerPid, int32_t appCloneIndex, int32_t userId = -1);
333 
334     /**
335      * ClearUpApplicationDataBySelf, clear the application data.
336      *
337      * @param callerUid, app uid in Application record.
338      * @param callerPid, app pid in Application record.
339      * @param userId, user ID.
340      *
341      * @return ERR_OK, return back success, others fail.
342      */
343     virtual int32_t ClearUpApplicationDataBySelf(int32_t callerUid, pid_t callerPid,  int32_t userId = -1);
344 
345     /**
346      * GetAllRunningProcesses, Obtains information about application processes that are running on the device.
347      *
348      * @param info, app name in Application record.
349      *
350      * @return ERR_OK ,return back success,others fail.
351      */
352     virtual int32_t GetAllRunningProcesses(std::vector<RunningProcessInfo> &info);
353 
354     /**
355      * GetRunningMultiAppInfoByBundleName, call GetRunningMultiAppInfoByBundleName through proxy project.
356      * Obtains information about TwinApp that are running on the device.
357      *
358      * @param bundleName, input.
359      * @param info, output multiapp information.
360      * @return void.
361      */
362     virtual int32_t GetRunningMultiAppInfoByBundleName(const std::string &bundleName,
363         RunningMultiAppInfo &info);
364 
365     /**
366      * GetRunningProcessesByBundleType, Obtains information about application processes by bundle type.
367      *
368      * @param bundleType, the bundle type of the application process
369      * @param info, app name in Application record.
370      *
371      * @return ERR_OK ,return back success,others fail.
372      */
373     virtual int32_t GetRunningProcessesByBundleType(BundleType bundleType, std::vector<RunningProcessInfo> &info);
374 
375     /**
376      * GetProcessRunningInfosByUserId, Obtains information about application processes that are running on the device.
377      *
378      * @param info, app name in Application record.
379      * @param userId, userId.
380      *
381      * @return ERR_OK ,return back success,others fail.
382      */
383     virtual int32_t GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId);
384 
385     /**
386      * GetProcessRunningInformation, Obtains information about current application process
387      * which is running on the device.
388      * @param info, app name in Application record.
389      *
390      * @return ERR_OK ,return back success,others fail.
391      */
392     virtual int32_t GetProcessRunningInformation(RunningProcessInfo &info);
393 
394     /**
395      * GetAllRenderProcesses, Obtains information about render processes that are running on the device.
396      *
397      * @param info, render process record.
398      *
399      * @return ERR_OK, return back success, others fail.
400      */
401     virtual int32_t GetAllRenderProcesses(std::vector<RenderProcessInfo> &info);
402 
403     /**
404      * GetAllChildrenProcesses, call GetAllChildrenProcesses() through proxy project.
405      * Obtains information about children processes that are running on the device.
406      *
407      * @param info, child process info.
408      * @return ERR_OK, return back success, others fail.
409      */
410     virtual int GetAllChildrenProcesses(std::vector<ChildProcessInfo> &info);
411 
412     /**
413      * NotifyMemoryLevel, Notify applications background the current memory level.
414      *
415      * @param level, current memory level.
416      *
417      * @return ERR_OK ,return back success,others fail.
418      */
419     virtual int32_t NotifyMemoryLevel(int32_t level);
420 
421     /**
422      * NotifyProcMemoryLevel, Notify applications background the current memory level.
423      *
424      * @param procLevelMap , <pid_t, MemoryLevel>.
425      *
426      * @return ERR_OK ,return back success,others fail.
427      */
428     virtual int32_t NotifyProcMemoryLevel(const std::map<pid_t, MemoryLevel> &procLevelMap);
429 
430     /**
431      * DumpHeapMemory, get the application's memory info.
432      * Get the application's memory allocation info.
433      *
434      * @param pid, pid input.
435      * @param mallocInfo, dynamic storage information output.
436      *
437      * @return ERR_OK, return back success,others fail.
438      */
439     virtual int32_t DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo);
440 
441     /**
442      * DumpJsHeapMemory, call DumpJsHeapMemory() through proxy project.
443      * triggerGC and dump the application's jsheap memory info.
444      *
445      * @param info, pid, tid, needGc, needSnapshot
446      * @return Returns ERR_OK on success, others on failure.
447      */
448     virtual int32_t DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info);
449 
450     /**
451      * @brief Check whether the shared bundle is running.
452      *
453      * @param bundleName Shared bundle name.
454      * @param versionCode Shared bundle version code.
455      * @return Returns the shared bundle running result. The result is true if running, false otherwise.
456      */
457     virtual bool IsSharedBundleRunning(const std::string &bundleName, uint32_t versionCode);
458 
459     /**
460      * Check whether the bundle is running.
461      *
462      * @param bundleName Indicates the bundle name of the bundle.
463      * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise.
464      * @return Return ERR_OK if success, others fail.
465      */
466     int32_t IsApplicationRunning(const std::string &bundleName, bool &isRunning);
467 
468     /**
469      * Check whether the bundle is running.
470      *
471      * @param bundleName Indicates the bundle name of the bundle.
472      * @param appCloneIndex the appindex of the bundle.
473      * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise.
474      * @return Return ERR_OK if success, others fail.
475      */
476     int32_t IsAppRunning(const std::string &bundleName, int32_t appCloneIndex, bool &isRunning);
477 
478     int32_t StartNativeProcessForDebugger(const AAFwk::Want &want);
479 
480     std::shared_ptr<AppRunningRecord> CreateAppRunningRecord(
481         sptr<IRemoteObject> token,
482         sptr<IRemoteObject> preToken,
483         std::shared_ptr<ApplicationInfo> appInfo,
484         std::shared_ptr<AbilityInfo> abilityInfo,
485         const std::string &processName,
486         const BundleInfo &bundleInfo,
487         const HapModuleInfo &hapModuleInfo,
488         std::shared_ptr<AAFwk::Want> want,
489         int32_t abilityRecordId);
490 
491     /**
492      * OnStop, Application management service stopped.
493      *
494      * @return
495      */
496     void OnStop();
497 
498     /**
499      * OpenAppSpawnConnection, Open connection with appspawn.
500      *
501      * @return ERR_OK ,return back success,others fail.
502      */
503     virtual ErrCode OpenAppSpawnConnection();
504 
505     /**
506      * CloseAppSpawnConnection, Close connection with appspawn.
507      *
508      * @return
509      */
510     virtual void CloseAppSpawnConnection() const;
511 
512     /**
513      * QueryAppSpawnConnectionState, Query the connection status with appspawn.
514      *
515      * @return Returns the connection status with appspawn.
516      */
517     virtual SpawnConnectionState QueryAppSpawnConnectionState() const;
518 
519     /**
520      * SetAppSpawnClient, Setting the client to connect with appspawn.
521      *
522      * @param spawnClient, the client to connect with appspawn.
523      *
524      * @return
525      */
526     void SetAppSpawnClient(std::shared_ptr<AppSpawnClient> spawnClient);
527 
528     // Schedule launch application with specified |appRecord|
529 
530     /**
531      * LaunchApplication, Notify application to launch application.
532      *
533      * @param appRecord, the application record.
534      *
535      * @return
536      */
537     void LaunchApplication(const std::shared_ptr<AppRunningRecord> &appRecord);
538 
539     /**
540      * LaunchApplicationExt, to solve the nbnc of method over 50.
541      *
542      * @param appRecord, the application record.
543      *
544      * @return
545      */
546     void LaunchApplicationExt(const std::shared_ptr<AppRunningRecord> &appRecord);
547 
548     /**
549      * Notice of AddAbilityStageInfo()
550      *
551      * @param recordId, the application record.
552      */
553     virtual void AddAbilityStageDone(const int32_t recordId);
554 
555     /**
556      * GetAppRunningRecordByPid, Get process record by application pid.
557      *
558      * @param pid, the application pid.
559      *
560      * @return process record.
561      */
562     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByPid(const pid_t pid) const;
563 
564     /**
565      * GetAppRunningRecordByAbilityToken, Get process record by ability token.
566      *
567      * @param abilityToken, the ability token.
568      *
569      * @return process record.
570      */
571     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAbilityToken(const sptr<IRemoteObject> &abilityToken) const;
572 
573     /**
574      * GetTerminatingAppRunningRecord, Get process record by ability token.
575      *
576      * @param abilityToken, the ability token.
577      *
578      * @return process record.
579      */
580     std::shared_ptr<AppRunningRecord> GetTerminatingAppRunningRecord(const sptr<IRemoteObject> &token) const;
581 
582     /**
583      * GetAppRunningRecordByAppRecordId, Get process record by application id.
584      *
585      * @param recordId, the application id.
586      *
587      * @return process record.
588      */
589     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAppRecordId(const int32_t recordId) const;
590 
591     /**
592      * OnAbilityStateChanged, Call ability state change.
593      *
594      * @param ability, the ability info.
595      * @param state, the ability state.
596      *
597      * @return
598      */
599     void OnAbilityStateChanged(const std::shared_ptr<AbilityRunningRecord> &ability, const AbilityState state);
600 
601     /**
602      * OnRemoteDied, Equipment death notification.
603      *
604      * @param remote, Death client.
605      * @param isRenderProcess is render process died.
606      * @param isChildProcess is child process died.
607      * @return
608      */
609     void OnRemoteDied(const wptr<IRemoteObject> &remote, bool isRenderProcess = false, bool isChildProcess = false);
610 
611     void HandleTimeOut(const AAFwk::EventWrap &event);
612 
DecreaseWillKillPidsNum()613     void DecreaseWillKillPidsNum()
614     {
615         willKillPidsNum_ -= 1;
616     }
617 
SetTaskHandler(std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler)618     void SetTaskHandler(std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler)
619     {
620         taskHandler_ = taskHandler;
621     }
622 
SetEventHandler(const std::shared_ptr<AMSEventHandler> & eventHandler)623     void SetEventHandler(const std::shared_ptr<AMSEventHandler> &eventHandler)
624     {
625         eventHandler_ = eventHandler;
626     }
627 
628     void HandleAbilityAttachTimeOut(const sptr<IRemoteObject> &token);
629 
630     void PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag = false);
631 
632     void OnAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, const ApplicationState state,
633         bool needNotifyApp, bool isFromWindowFocusChanged);
634 
635     void OnAppStarted(const std::shared_ptr<AppRunningRecord> &appRecord);
636 
637     void OnAppStopped(const std::shared_ptr<AppRunningRecord> &appRecord);
638 
639     void GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info);
640 
641     int32_t GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info) const;
642 
643     /**
644      * Set AbilityForegroundingFlag of an app-record to true.
645      *
646      * @param pid, pid.
647      *
648      */
649     void SetAbilityForegroundingFlagToAppRecord(const pid_t pid) const;
650 
651      /**
652      * UpdateConfiguration, ANotify application update system environment changes.
653      *
654      * @param config, System environment change parameters.
655      * @return Returns ERR_OK on success, others on failure.
656      */
657     int32_t UpdateConfiguration(const Configuration &config, const int32_t userId = -1);
658 
659     int32_t UpdateConfigurationByBundleName(const Configuration &config, const std::string &name);
660 
661     std::shared_ptr<AppExecFwk::Configuration> GetConfiguration();
662 
663     int32_t RegisterConfigurationObserver(const sptr<IConfigurationObserver>& observer);
664 
665     int32_t UnregisterConfigurationObserver(const sptr<IConfigurationObserver>& observer);
666 
667     /**
668      * Start empty process
669      */
670     void LoadResidentProcess(const std::vector<BundleInfo> &infos);
671 
672     void StartResidentProcess(const std::vector<BundleInfo> &infos,  int restartCount, bool isEmptyKeepAliveApp);
673 
674     bool CheckRemoteClient();
675 
676     /**
677      * Register application or process state observer.
678      * @param observer, ability token.
679      * @return Returns ERR_OK on success, others on failure.
680      */
681     int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer,
682         const std::vector<std::string> &bundleNameList = {});
683 
684     /**
685      * Unregister application or process state observer.
686      * @param observer, ability token.
687      * @return Returns ERR_OK on success, others on failure.
688      */
689     int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer);
690 
691     /**
692      * Register application or process state observer.
693      * @param observer, Is ability foreground state observer
694      * @return Returns ERR_OK on success, others on failure.
695      */
696     int32_t RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer);
697 
698     /**
699      * Unregister application or process state observer.
700      * @param observer, Is ability foreground state observer
701      * @return Returns ERR_OK on success, others on failure.
702      */
703     int32_t UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer);
704 
705     /**
706      * Get Foreground Applications.
707      *
708      * @return Foreground Applications.
709      */
710     int32_t GetForegroundApplications(std::vector<AppStateData> &list);
711 
712     /**
713      * Start user test process.
714      * @param want, want object.
715      * @param observer, test observer remote object.
716      * @param bundleInfo, bundle info.
717      * @param userId the user id.
718      * @return Returns ERR_OK on success, others on failure.
719      */
720     int StartUserTestProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer,
721         const AppExecFwk::BundleInfo &bundleInfo, int32_t userId);
722 
723     /**
724      * @brief Finish user test.
725      * @param msg user test message.
726      * @param resultCode user test result Code.
727      * @param bundleName user test bundleName.
728      * @param pid the user test process id.
729      *
730      * @return Returns ERR_OK on success, others on failure.
731      */
732     int FinishUserTest(
733         const std::string &msg, const int64_t &resultCode, const std::string &bundleName, const pid_t &pid);
734 
735     void StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
736         int32_t requestId = 0);
737 
738     void StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
739         int32_t requestId = 0);
740 
741     void RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response);
742 
743     void ScheduleAcceptWantDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag);
744 
745     void ScheduleNewProcessRequestDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag);
746 
747     /**
748      *  Get the token of ability records by process ID.
749      *
750      * @param pid The process id.
751      * @param tokens The token of ability records.
752      * @return Returns true on success, others on failure.
753      */
754     int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens);
755 
756     virtual int32_t PreStartNWebSpawnProcess(const pid_t hostPid);
757 
758     virtual int32_t StartRenderProcess(const pid_t hostPid,
759                                        const std::string &renderParam,
760                                        int32_t ipcFd, int32_t sharedFd,
761                                        int32_t crashFd, pid_t &renderPid, bool isGPU = false);
762 
763     virtual void AttachRenderProcess(const pid_t pid, const sptr<IRenderScheduler> &scheduler);
764 
765     virtual int GetRenderProcessTerminationStatus(pid_t renderPid, int &status);
766 
767     int VerifyKillProcessPermission(const sptr<IRemoteObject> &token) const;
768 
769     int VerifyAccountPermission(const std::string &permissionName, const int userId) const;
770 
771     int VerifyRequestPermission() const;
772 
773     void ClearAppRunningData(const std::shared_ptr<AppRunningRecord> &appRecord, bool containsApp);
774 
775     void TerminateApplication(const std::shared_ptr<AppRunningRecord> &appRecord);
776 
777     int GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug);
778 
779     int32_t NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg);
780 
781     /**
782      * Notify application status.
783      *
784      * @param bundleName Indicates the name of the bundle.
785      * @param eventData Indicates the event defined by CommonEventSupport
786      *
787      * @return
788      */
789     void NotifyAppStatus(const std::string &bundleName, const std::string &eventData);
790 
791     int32_t KillProcessByPid(const pid_t pid, const std::string& reason = "foundation");
792 
793     bool GetAppRunningStateByBundleName(const std::string &bundleName);
794 
795     int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback);
796 
797     int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback);
798 
799     int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback);
800 
801     void HandleFocused(const sptr<OHOS::Rosen::FocusChangeInfo> &focusChangeInfo);
802     void HandleUnfocused(const sptr<OHOS::Rosen::FocusChangeInfo> &focusChangeInfo);
803 
804     /**
805      * Handle window visibility changed.
806      */
807     void HandleWindowVisibilityChanged(
808             const std::vector<sptr<OHOS::Rosen::WindowVisibilityInfo>> &windowVisibilityInfos);
809 
810     /**
811      * Set the current userId, only used by abilityMgr.
812      *
813      * @param userId the user id.
814      *
815      * @return
816      */
817     void SetCurrentUserId(const int32_t userId);
818 
819     /**
820      * Set enable start process flag by userId
821      * @param userId the user id.
822      * @param enableStartProcess enable start process.
823      * @return
824      */
825     void SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess);
826 
827 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
828     int32_t SetContinuousTaskProcess(int32_t pid, bool isContinuousTask);
829 #endif
830 
831     /**
832      * Get bundleName by pid.
833      *
834      * @param pid process id.
835      * @param bundleName Output parameters, return bundleName.
836      * @param uid Output parameters, return userId.
837      * @return Returns ERR_OK on success, others on failure.
838      */
839     int32_t GetBundleNameByPid(const int32_t pid, std::string &bundleName, int32_t &uid);
840 
841     /**
842      * Notify Fault Data
843      *
844      * @param faultData the fault data.
845      * @return Returns ERR_OK on success, others on failure.
846      */
847     int32_t NotifyAppFault(const FaultData &faultData);
848 
849     /**
850      * Transformed Notify Fault Data
851      *
852      * @param faultData Transformed the fault data.
853      * @return Returns ERR_OK on success, others on failure.
854      */
855 
856     int32_t TransformedNotifyAppFault(const AppFaultDataBySA &faultData);
857 
858     /**
859      * Notify Fault Data By SA
860      *
861      * @param faultData the fault data notified by SA.
862      * @return Returns ERR_OK on success, others on failure.
863      */
864     int32_t NotifyAppFaultBySA(const AppFaultDataBySA &faultData);
865 
866     /**
867      * Set Appfreeze Detect Filter
868      *
869      * @param pid the process pid.
870      * @return Returns true on success, others on failure.
871      */
872     bool SetAppFreezeFilter(int32_t pid);
873 
874     /**
875      * get memorySize by pid.
876      *
877      * @param pid process id.
878      * @param memorySize Output parameters, return memorySize in KB.
879      * @return Returns ERR_OK on success, others on failure.
880      */
881     virtual int32_t GetProcessMemoryByPid(const int32_t pid, int32_t &memorySize);
882 
883     /**
884      * get application processes information list by bundleName.
885      *
886      * @param bundleName Bundle name.
887      * @param userId user Id in Application record.
888      * @param info Output parameters, return running process info list.
889      * @return Returns ERR_OK on success, others on failure.
890      */
891     virtual int32_t GetRunningProcessInformation(
892         const std::string &bundleName, int32_t userId, std::vector<RunningProcessInfo> &info);
893 
894     /**
895      * init focusListener.
896      *
897      * @return
898      */
899     void InitFocusListener();
900 
901     /**
902      * free focusListener.
903      *
904      * @return
905      */
906     void FreeFocusListener();
907 
908     /**
909      * Init window visibility changed listener.
910      */
911     void InitWindowVisibilityChangedListener();
912 
913     /**
914      * Free window visibility changed listener.
915      */
916     void FreeWindowVisibilityChangedListener();
917 
918     /*
919      * @brief Notify NativeEngine GC of status change.
920      *
921      * @param state GC state
922      * @param pid pid
923      *
924      * @return Is the status change completed.
925      */
926     int32_t ChangeAppGcState(pid_t pid, int32_t state);
927 
928     /**
929      * @brief Register app debug listener.
930      * @param listener App debug listener.
931      * @return Returns ERR_OK on success, others on failure.
932      */
933     int32_t RegisterAppDebugListener(const sptr<IAppDebugListener> &listener);
934 
935     /**
936      * @brief Unregister app debug listener.
937      * @param listener App debug listener.
938      * @return Returns ERR_OK on success, others on failure.
939      */
940     int32_t UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener);
941 
942     /**
943      * @brief Attach app debug.
944      * @param bundleName The application bundle name.
945      * @return Returns ERR_OK on success, others on failure.
946      */
947     int32_t AttachAppDebug(const std::string &bundleName);
948 
949     /**
950      * @brief Detach app debug.
951      * @param bundleName The application bundle name.
952      * @return Returns ERR_OK on success, others on failure.
953      */
954     int32_t DetachAppDebug(const std::string &bundleName);
955 
956     /**
957      * @brief Set app waiting debug mode.
958      * @param bundleName The application bundle name.
959      * @param isPersist The persist flag.
960      * @return Returns ERR_OK on success, others on failure.
961      */
962     int32_t SetAppWaitingDebug(const std::string &bundleName, bool isPersist);
963 
964     /**
965      * @brief Cancel app waiting debug mode.
966      * @return Returns ERR_OK on success, others on failure.
967      */
968     int32_t CancelAppWaitingDebug();
969 
970     /**
971      * @brief Get waiting debug mode application.
972      * @param bundleNameList The application bundle name list.
973      * @return Returns ERR_OK on success, others on failure.
974      */
975     int32_t GetWaitingDebugApp(std::vector<std::string> &bundleNameList);
976 
977     /**
978      * @brief Determine whether it is a waiting debug application based on the bundle name.
979      * @return Returns true if it is a waiting debug application, otherwise it returns false.
980      */
981     bool IsWaitingDebugApp(const std::string &bundleName);
982 
983     /**
984      * @brief Clear non persist waiting debug flag.
985      */
986     void ClearNonPersistWaitingDebugFlag();
987 
988     /**
989      * @brief Registering ability debug mode response.
990      * @param response Response for ability debug object.
991      * @return Returns ERR_OK on success, others on failure.
992      */
993     int32_t RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response);
994 
995     /**
996      * @brief Determine whether it is an attachment debug application based on the bundle name.
997      * @param bundleName The application bundle name.
998      * @return Returns true if it is an attach debug application, otherwise it returns false.
999      */
1000     bool IsAttachDebug(const std::string &bundleName);
1001 
1002     /**
1003      * @brief Notify AbilityManagerService the page show.
1004      * @param token Ability identify.
1005      * @param pageStateData The data of ability's page state.
1006      * @return Returns ERR_OK on success, others on failure.
1007      */
1008     virtual int32_t NotifyPageShow(const sptr<IRemoteObject> &token, const PageStateData &pageStateData);
1009 
1010     /**
1011      * @brief Notify AbilityManagerService the page hide.
1012      * @param token Ability identify.
1013      * @param pageStateData The data of ability's page state.
1014      * @return Returns ERR_OK on success, others on failure.
1015      */
1016     virtual int32_t NotifyPageHide(const sptr<IRemoteObject> &token, const PageStateData &pageStateData);
1017 
1018     /**
1019      * Register appRunning status listener.
1020      *
1021      * @param listener Running status listener.
1022      * @return Returns ERR_OK on success, others on failure.
1023      */
1024     int32_t RegisterAppRunningStatusListener(const sptr<IRemoteObject> &listener);
1025 
1026     /**
1027      * Unregister appRunning status listener.
1028      *
1029      * @param listener Running status listener.
1030      * @return Returns ERR_OK on success, others on failure.
1031      */
1032     int32_t UnregisterAppRunningStatusListener(const sptr<IRemoteObject> &listener);
1033 
1034     /**
1035      * Register application foreground state observer.
1036      * @param observer Is app foreground statue observer
1037      * @return Returns ERR_OK on success, others on failure.
1038      */
1039     int32_t RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer);
1040 
1041     /**
1042      * Unregister application foreground state observer.
1043      * @param observer Is app foreground statue observer
1044      * @return Returns ERR_OK on success, others on failure.
1045      */
1046     int32_t UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer);
1047 
1048     /**
1049      * Start child process, called by ChildProcessManager.
1050      *
1051      * @param callingPid Calling process pid.
1052      * @param childPid Created child process pid.
1053      * @param request Child process start request params.
1054      * @return Returns ERR_OK on success, others on failure.
1055      */
1056     virtual int32_t StartChildProcess(const pid_t callingPid, pid_t &childPid, const ChildProcessRequest &request);
1057 
1058     /**
1059      * Get child process record for self.
1060      *
1061      * @return child process record.
1062      */
1063     virtual int32_t GetChildProcessInfoForSelf(ChildProcessInfo &info);
1064 
1065     /**
1066      * Attach child process scheduler to app manager service.
1067      *
1068      * @param pid the child process pid to exit.
1069      * @param childScheduler scheduler of child process.
1070      */
1071     virtual void AttachChildProcess(const pid_t pid, const sptr<IChildScheduler> &childScheduler);
1072 
1073     /**
1074      * Exit child process safely by child process pid.
1075      *
1076      * @param pid child process pid.
1077      */
1078     virtual void ExitChildProcessSafelyByChildPid(const pid_t pid);
1079 
1080     /**
1081      * Start native child process, callde by ChildProcessManager.
1082      * @param hostPid Host process pid.
1083      * @param childProcessCount current started child process count
1084      * @param libName lib file name to be load in child process
1085      * @param callback callback for notify start result
1086      * @return Returns ERR_OK on success, others on failure.
1087      */
1088     virtual int32_t StartNativeChildProcess(const pid_t hostPid,
1089         const std::string &libName, int32_t childProcessCount, const sptr<IRemoteObject> &callback);
1090 
1091     /**
1092      * To clear the process by ability token.
1093      *
1094      * @param token the unique identification to the ability.
1095      */
1096     void ClearProcessByToken(sptr<IRemoteObject> token);
1097 
1098     /**
1099      * Whether the current application process is the last surviving process.
1100      * @param bundleName To query the bundle name of a process.
1101      * @return Returns true is final application process, others return false.
1102      */
1103     bool IsFinalAppProcessByBundleName(const std::string &bundleName);
1104 
1105     /**
1106      * @brief Notify memory size state changed to sufficient or insufficent.
1107      * @param isMemorySizeSufficent Indicates the memory size state.
1108      * @return Returns ERR_OK on success, others on failure.
1109      */
1110     int32_t NotifyMemorySizeStateChanged(bool isMemorySizeSufficent);
1111 
1112     /**
1113      * whether memory size is sufficent.
1114      * @return Returns true is sufficent memory size, others return false.
1115      */
1116     bool IsMemorySizeSufficent();
1117 
1118     int32_t RegisterRenderStateObserver(const sptr<IRenderStateObserver> &observer);
1119 
1120     int32_t UnregisterRenderStateObserver(const sptr<IRenderStateObserver> &observer);
1121 
1122     int32_t UpdateRenderState(pid_t renderPid, int32_t state);
1123 
1124     int32_t SignRestartAppFlag(int32_t uid);
1125 
1126     int32_t GetAppIndexByPid(pid_t pid, int32_t &appIndex) const;
1127 
1128     void SetAppAssertionPauseState(bool flag);
1129 
1130     void SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid);
1131 
1132     int32_t GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId);
1133 
1134     int32_t GetAllUIExtensionRootHostPid(pid_t pid, std::vector<pid_t> &hostPids);
1135 
1136     int32_t GetAllUIExtensionProviderPid(pid_t hostPid, std::vector<pid_t> &providerPids);
1137 
1138     virtual int DumpIpcAllStart(std::string& result);
1139 
1140     virtual int DumpIpcAllStop(std::string& result);
1141 
1142     virtual int DumpIpcAllStat(std::string& result);
1143 
1144     virtual int DumpIpcStart(const int32_t pid, std::string& result);
1145 
1146     virtual int DumpIpcStop(const int32_t pid, std::string& result);
1147 
1148     virtual int DumpIpcStat(const int32_t pid, std::string& result);
1149 
1150     virtual int DumpFfrt(const std::vector<int32_t>& pids, std::string& result);
1151 
1152     int32_t SetSupportedProcessCacheSelf(bool isSupport);
1153 
1154     int32_t SetSupportedProcessCache(int32_t pid, bool isSupport);
1155 
1156     void OnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, ApplicationState state);
1157 
1158     virtual void SaveBrowserChannel(const pid_t hostPid, sptr<IRemoteObject> browser);
1159 
1160     bool IsAppProcessesAllCached(const std::string &bundleName, int32_t uid,
1161         const std::set<std::shared_ptr<AppRunningRecord>> &cachedSet);
1162 
1163     /**
1164      * Check caller is test ability
1165      *
1166      * @param pid, the pid of ability.
1167      * @return Returns ERR_OK is test ability, others is not test ability.
1168      */
1169     int32_t CheckCallingIsUserTestModeInner(const pid_t pid, bool &isUserTest);
1170 
1171     bool GetSceneBoardAttachFlag() const;
1172 
1173     void SetSceneBoardAttachFlag(bool flag);
1174 
1175     void CacheLoadAbilityTask(const LoadAbilityTaskFunc& func);
1176 
1177     void SubmitCacheLoadAbilityTask();
1178     /**
1179      * Notifies that one ability is attached to status bar.
1180      *
1181      * @param token the token of the abilityRecord that is attached to status bar.
1182      */
1183     void AttachedToStatusBar(const sptr<IRemoteObject> &token);
1184     void KillApplicationByRecord(const std::shared_ptr<AppRunningRecord> &appRecord);
1185 
1186     int32_t NotifyProcessDependedOnWeb();
1187 
1188     void KillProcessDependedOnWeb();
1189 
1190     void RestartResidentProcessDependedOnWeb();
1191 
1192     void BlockProcessCacheByPids(const std::vector<int32_t>& pids);
1193 
1194     bool IsKilledForUpgradeWeb(const std::string &bundleName) const;
1195 
1196     bool CleanAbilityByUserRequest(const sptr<IRemoteObject> &token);
1197 
1198     bool IsProcessContainsOnlyUIAbility(const pid_t pid);
1199 
1200     bool IsProcessAttached(sptr<IRemoteObject> token) const;
1201 
1202     void NotifyAppPreCache(int32_t pid, int32_t userId);
1203 
1204 private:
1205     int32_t ForceKillApplicationInner(const std::string &bundleName, const int userId = -1,
1206         const int appIndex = 0);
1207 
1208     std::string FaultTypeToString(FaultDataType type);
1209 
1210     FaultData ConvertDataTypes(const AppFaultDataBySA &faultData);
1211 
1212     void StartEmptyResidentProcess(const BundleInfo &info, const std::string &processName, int restartCount,
1213         bool isEmptyKeepAliveApp);
1214 
1215     void RestartResidentProcess(std::shared_ptr<AppRunningRecord> appRecord);
1216 
1217     bool CheckLoadAbilityConditions(const sptr<IRemoteObject> &token,
1218         const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo);
1219 
1220     bool GetBundleInfo(const std::string &bundleName, BundleInfo &bundleInfo);
1221 
1222     bool GenerateRenderUid(int32_t &renderUid);
1223 
1224     void MakeServiceExtProcessName(const std::shared_ptr<AbilityInfo> &abilityInfo,
1225         const std::shared_ptr<ApplicationInfo> &appInfo, std::string &processName) const;
1226 
1227     void MakeProcessName(const std::shared_ptr<AbilityInfo> &abilityInfo,
1228         const std::shared_ptr<ApplicationInfo> &appInfo, const HapModuleInfo &hapModuleInfo, int32_t appIndex,
1229         const std::string &specifiedProcessFlag, std::string &processName) const;
1230 
1231     void MakeProcessName(const std::shared_ptr<ApplicationInfo> &appInfo, const HapModuleInfo &hapModuleInfo,
1232         std::string &processName) const;
1233 
1234     bool CheckIsolationMode(const HapModuleInfo &hapModuleInfo) const;
1235 
1236     bool IsMainProcess(const std::shared_ptr<ApplicationInfo> &appInfo, const std::string &processName) const;
1237 
1238     /**
1239      * StartAbility, load the ability that needed to be started(Start on the basis of the original process).
1240      *  Start on a new boot process
1241      * @param token, the unique identification to start the ability.
1242      * @param preToken, the unique identification to call the ability.
1243      * @param abilityInfo, the ability information.
1244      * @param appInfo, the app information.
1245      *
1246      * @return
1247      */
1248     void StartAbility(sptr<IRemoteObject> token, sptr<IRemoteObject> preToken,
1249         std::shared_ptr<AbilityInfo> abilityInfo, std::shared_ptr<AppRunningRecord> appRecord,
1250         const HapModuleInfo &hapModuleInfo, std::shared_ptr<AAFwk::Want> want, int32_t abilityRecordId);
1251 
1252     int32_t StartPerfProcess(const std::shared_ptr<AppRunningRecord> &appRecord, const std::string& perfCmd,
1253         const std::string& debugCmd, bool isSandboxApp);
1254 
1255     void StartProcessVerifyPermission(const BundleInfo &bundleInfo, bool &hasAccessBundleDirReq,
1256         uint8_t &setAllowInternet, uint8_t &allowInternet, std::vector<int32_t> &gids);
1257 
1258     void AddMountPermission(uint32_t accessTokenId, std::set<std::string> &permissions);
1259 
1260     /**
1261      * StartProcess, load the ability that needed to be started(Start on a new boot process).
1262      *
1263      * @param appName, the app name.
1264      * @param processName, the process name.
1265      * @param appRecord, the app information.
1266      * @param uid, the process uid.
1267      * @param bundleName, the app bundleName.
1268      *
1269      * @return
1270      */
1271     void StartProcess(const std::string &appName, const std::string &processName, uint32_t startFlags,
1272                       std::shared_ptr<AppRunningRecord> appRecord, const int uid, const BundleInfo &bundleInfo,
1273                       const std::string &bundleName, const int32_t bundleIndex, bool appExistFlag = true,
1274                       bool isPreload = false, const std::string &moduleName = "", const std::string &abilityName = "",
1275                       bool strictMode = false, int32_t maxChildProcess = 0, sptr<IRemoteObject> token = nullptr,
1276                       std::shared_ptr<AAFwk::Want> want = nullptr,
1277                       ExtensionAbilityType ExtensionAbilityType = ExtensionAbilityType::UNSPECIFIED);
1278 
1279     /**
1280      * KillApplicationByUserId, kill the application by user ID.
1281      *
1282      * @param bundleName, bundle name in Application record.
1283      * @param appCloneIndex the app clone id.
1284      * @param userId, user ID.
1285      * @param  reason, caller function name.
1286      *
1287      * @return ERR_OK, return back success, others fail.
1288      */
1289     int32_t KillApplicationByUserIdLocked(const std::string &bundleName, int32_t appCloneIndex, int32_t userId,
1290         const std::string& reason = "KillApplicationByUserIdLocked");
1291 
1292     /**
1293      * WaitForRemoteProcessExit, Wait for the process to exit normally.
1294      *
1295      * @param pids, process number collection to exit.
1296      * @param startTime, execution process security exit start time.
1297      *
1298      * @return true, return back success,others fail.
1299      */
1300     bool WaitForRemoteProcessExit(std::list<pid_t> &pids, const int64_t startTime);
1301 
1302     /**
1303      * ProcessExist, Judge whether the process exists.
1304      *
1305      * @param pids, process number collection to exit.
1306      *
1307      * @return true, return back existed,others non-existent.
1308      */
1309     bool ProcessExist(pid_t pid);
1310 
1311     /**
1312      * CheckAllProcessExist, Determine whether all processes exist .
1313      *
1314      * @param pids, process number collection to exit.
1315      *
1316      * @return true, Returns that a process exists and all other processes do not exist.
1317      */
1318     bool CheckAllProcessExist(std::list<pid_t> &pids);
1319 
1320     /**
1321      * SystemTimeMillisecond, Get system time.
1322      *
1323      * @return the system time.
1324      */
1325     int64_t SystemTimeMillisecond();
1326 
1327     // Test add the bundle manager instance.
1328     void SetBundleManagerHelper(const std::shared_ptr<BundleMgrHelper> &bundleMgrHelper);
1329 
1330     void HandleTerminateApplicationTimeOut(const int64_t eventId);
1331 
1332     void HandleAddAbilityStageTimeOut(const int64_t eventId);
1333 
1334     void ClipStringContent(const std::regex &re, const std::string &source, std::string &afterCutStr);
1335 
1336     bool GetBundleAndHapInfo(const AbilityInfo &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo,
1337         BundleInfo &bundleInfo, HapModuleInfo &hapModuleInfo, int32_t appIndex = 0) const;
1338     AppProcessData WrapAppProcessData(const std::shared_ptr<AppRunningRecord> &appRecord,
1339         const ApplicationState state);
1340 
1341     int UserTestAbnormalFinish(const sptr<IRemoteObject> &observer, const std::string &msg);
1342     int GetHapModuleInfoForTestRunner(const AAFwk::Want &want, const sptr<IRemoteObject> &observer,
1343         const BundleInfo &bundleInfo, HapModuleInfo &hapModuleInfo);
1344     int StartEmptyProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer, const BundleInfo &info,
1345         const std::string &processName, const int userId);
1346 
1347     void HandleStartSpecifiedAbilityTimeOut(const int64_t eventId);
1348 
1349     void HandleStartSpecifiedProcessTimeout(const int64_t eventId);
1350 
1351     void InitGlobalConfiguration();
1352 
1353     void GetRunningProcesses(const std::shared_ptr<AppRunningRecord> &appRecord, std::vector<RunningProcessInfo> &info);
1354     void GetRunningProcess(const std::shared_ptr<AppRunningRecord> &appRecord, RunningProcessInfo &info);
1355 
1356     void GetRenderProcesses(const std::shared_ptr<AppRunningRecord> &appRecord, std::vector<RenderProcessInfo> &info);
1357 
1358     void GetChildrenProcesses(const std::shared_ptr<AppRunningRecord> &appRecord, std::vector<ChildProcessInfo> &info);
1359 
1360     int StartRenderProcessImpl(const std::shared_ptr<RenderRecord> &renderRecord,
1361         const std::shared_ptr<AppRunningRecord> appRecord, pid_t &renderPid, bool isGPU = false);
1362 
1363     void SetRenderStartMsg(AppSpawnStartMsg &startMsg, std::shared_ptr<RenderRecord> renderRecord,
1364         const int32_t renderUid, const bool isGPU);
1365 
1366     void OnRenderRemoteDied(const wptr<IRemoteObject> &remote);
1367 
1368     void AddWatchParameter();
1369 
1370     bool VerifyAPL() const;
1371 
1372     void RegisterFocusListener();
1373 
1374     static void PointerDeviceEventCallback(const char *key, const char *value, void *context);
1375 
1376     int VerifyKillProcessPermission(const std::string &bundleName) const;
1377 
1378     int32_t VerifyKillProcessPermissionCommon() const;
1379 
1380     bool CheckCallerIsAppGallery();
1381 
1382     void ApplicationTerminatedSendProcessEvent(const std::shared_ptr<AppRunningRecord> &appRecord);
1383     void ClearAppRunningDataForKeepAlive(const std::shared_ptr<AppRunningRecord> &appRecord);
1384 
1385     int32_t StartChildProcessPreCheck(pid_t callingPid, int32_t childProcessType);
1386 
1387     int32_t StartChildProcessImpl(const std::shared_ptr<ChildProcessRecord> childProcessRecord,
1388         const std::shared_ptr<AppRunningRecord> appRecord, pid_t &childPid, const ChildProcessArgs &args,
1389         const ChildProcessOptions &options);
1390 
1391     int32_t GetChildProcessInfo(const std::shared_ptr<ChildProcessRecord> childProcessRecord,
1392         const std::shared_ptr<AppRunningRecord> appRecord, ChildProcessInfo &info,
1393         bool isCallFromGetChildrenProcesses = false);
1394 
1395     void OnChildProcessRemoteDied(const wptr<IRemoteObject> &remote);
1396 
1397     void KillChildProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
1398 
1399     void KillAttachedChildProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
1400 
1401     void PresetMaxChildProcess(const std::shared_ptr<AbilityInfo> &abilityInfo, int32_t &maxChildProcess);
1402 
1403     void AfterLoadAbility(std::shared_ptr<AppRunningRecord> appRecord, std::shared_ptr<AbilityInfo> abilityInfo,
1404         std::shared_ptr<AbilityRuntime::LoadParam> loadParam);
1405 
1406     void RemoveRenderRecordNoAttach(const std::shared_ptr<AppRunningRecord> &hostRecord, int32_t renderPid);
1407 
1408 private:
1409     /**
1410      * ClearUpApplicationData, clear the application data.
1411      *
1412      * @param bundleName, bundle name in Application record.
1413      * @param uid, app uid in Application record.
1414      * @param pid, app pid in Application record.
1415      * @param appCloneIndex the app clone id.
1416      * @param userId, userId.
1417      * @param isBySelf, clear data by application self.
1418      * @param reason, caller function.
1419      *
1420      * @return Returns ERR_OK on success, others on failure.
1421      */
1422     int32_t ClearUpApplicationDataByUserId(const std::string &bundleName,
1423         int32_t callerUid, pid_t callerPid, int32_t appCloneIndex, int32_t userId, bool isBySelf = false,
1424         const std::string& reason = "ClearUpApplicationDataByUserId");
1425 
1426     bool CheckGetRunningInfoPermission() const;
1427 
1428     /**
1429      * kill all processes of a bundleName
1430      * @param bundleName bundleName of which to be killed
1431      * @param reason caller function name
1432      */
1433     int32_t KillApplicationByBundleName(const std::string &bundleName,
1434         const std::string& reason = "KillApplicationByBundleName");
1435 
1436     bool SendProcessStartEvent(const std::shared_ptr<AppRunningRecord> &appRecord);
1437 
1438     bool SendProcessStartFailedEvent(std::shared_ptr<AppRunningRecord> appRecord, ProcessStartFailedReason reason,
1439         int32_t subReason);
1440 
1441     void SendAppStartupTypeEvent(const std::shared_ptr<AppRunningRecord> &appRecord,
1442         const std::shared_ptr<AbilityInfo> &abilityInfo, const AppStartType startType);
1443 
1444     bool SendCreateAtomicServiceProcessEvent(const std::shared_ptr<AppRunningRecord> &appRecord,
1445         const BundleType &bundleType, const std::string &moduleName = "", const std::string &abilityName = "");
1446 
1447     void SendProcessExitEvent(const std::shared_ptr<AppRunningRecord> &appRecord);
1448 
1449     void SendProcessExitEventTask(const std::shared_ptr<AppRunningRecord> &appRecord, time_t exitTime, int32_t count);
1450 
1451     void SetRunningSharedBundleList(const std::string &bundleName,
1452         const std::vector<BaseSharedBundleInfo> baseSharedBundleInfoList);
1453 
1454     void RemoveRunningSharedBundleList(const std::string &bundleName);
1455 
1456     void KillRenderProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
1457 
1458     void SetOverlayInfo(const std::string& bundleName, const int32_t userId, AppSpawnStartMsg& startMsg);
1459     void SetAppEnvInfo(const BundleInfo &bundleInfo, AppSpawnStartMsg& startMsg);
1460 
1461     void TimeoutNotifyApp(int32_t pid, int32_t uid, const std::string& bundleName, const std::string& processName,
1462         const FaultData &faultData);
1463 
1464     void AppRecoveryNotifyApp(int32_t pid, const std::string& bundleName,
1465         FaultDataType faultType, const std::string& markers);
1466 
1467     void ProcessAppDebug(const std::shared_ptr<AppRunningRecord> &appRecord, const bool &isDebugStart);
1468     AppDebugInfo MakeAppDebugInfo(const std::shared_ptr<AppRunningRecord> &appRecord, const bool &isDebugStart);
1469     int32_t NotifyAbilitysDebugChange(const std::string &bundleName, const bool &isAppDebug);
1470     int32_t NotifyAbilitysAssertDebugChange(const std::shared_ptr<AppRunningRecord> &appRecord, bool isAssertDebug);
1471 
1472     void SetProcessJITState(const std::shared_ptr<AppRunningRecord> appRecord);
1473 
1474     bool JudgeSelfCalledByToken(const sptr<IRemoteObject> &token, const PageStateData &pageStateData);
1475 
1476     void ParseServiceExtMultiProcessWhiteList();
1477     void ClearData(std::shared_ptr<AppRunningRecord> appRecord);
1478 
1479     /**
1480      * Notify the app running status.
1481      *
1482      * @param bundle Bundle name in application record.
1483      * @param uid Uid of bundle.
1484      * @param runningStatus The app running status.
1485      *
1486      * @return
1487      */
1488     void NotifyAppRunningStatusEvent(
1489         const std::string &bundle, int32_t uid, AbilityRuntime::RunningStatus runningStatus);
1490 
1491     void GetRunningCloneAppInfo(const std::shared_ptr<AppRunningRecord> &appRecord,
1492         RunningMultiAppInfo &info);
1493 
1494     /**
1495      * To Prevent process being killed when ability is starting in an existing process,
1496      * we need notify memmgr to increase process priority.
1497      *
1498      * @param appRecord Current app running record.
1499      *
1500      * @return Whether improve priority succeed.
1501      */
1502     bool NotifyMemMgrPriorityChanged(const std::shared_ptr<AppRunningRecord> appRecord);
1503 
1504     void HandlePreloadApplication(const PreloadRequest &request);
1505 
1506     std::string GetSpecifiedProcessFlag(std::shared_ptr<AbilityInfo> abilityInfo, std::shared_ptr<AAFwk::Want> want);
1507 
1508     void LoadAbilityNoAppRecord(const std::shared_ptr<AppRunningRecord> appRecord,
1509         bool isShellCall, std::shared_ptr<ApplicationInfo> appInfo,
1510         std::shared_ptr<AbilityInfo> abilityInfo, const std::string &processName,
1511         const std::string &specifiedProcessFlag, const BundleInfo &bundleInfo,
1512         const HapModuleInfo &hapModuleInfo, std::shared_ptr<AAFwk::Want> want,
1513         bool appExistFlag, bool isPreload, sptr<IRemoteObject> token = nullptr);
1514 
1515     int32_t CreatNewStartMsg(const Want &want, const AbilityInfo &abilityInfo,
1516         const std::shared_ptr<ApplicationInfo> &appInfo, const std::string &processName,
1517         AppSpawnStartMsg &startMsg);
1518 
1519     int32_t CreateStartMsg(const std::string &processName, uint32_t startFlags, const int uid,
1520         const BundleInfo &bundleInfo, const int32_t bundleIndex, BundleType bundleType, AppSpawnStartMsg &startMsg,
1521         std::shared_ptr<AAFwk::Want> want, const std::string &moduleName = "", const std::string &abilityName = "",
1522         bool strictMode = false);
1523 
1524     void QueryExtensionSandBox(const std::string &moduleName, const std::string &abilityName,
1525         const BundleInfo &bundleInfo, AppSpawnStartMsg &startMsg, DataGroupInfoList& dataGroupInfoList,
1526         bool strictMode, std::shared_ptr<AAFwk::Want> want);
1527 
1528     int32_t StartPerfProcessByStartMsg(AppSpawnStartMsg &startMsg, const std::string& perfCmd,
1529         const std::string& debugCmd, bool isSandboxApp);
1530 
1531     void SetAtomicServiceInfo(BundleType bundleType, AppSpawnStartMsg &startMsg);
1532 
1533     void SetAppInfo(const BundleInfo &bundleInfo, AppSpawnStartMsg &startMsg);
1534 
1535     bool CreateAbilityInfo(const AAFwk::Want &want, AbilityInfo &abilityInfo);
1536 
1537     AAFwk::EventInfo BuildEventInfo(std::shared_ptr<AppRunningRecord> appRecord) const;
1538 
1539     void NotifyAppAttachFailed(std::shared_ptr<AppRunningRecord> appRecord);
1540 private:
1541     /**
1542      * Notify application status.
1543      *
1544      * @param bundleName Indicates the name of the bundle.
1545      * @param bundleName Indicates the name of the bundle.
1546      * @param eventData Indicates the event defined by CommonEventSupport
1547      *
1548      * @return
1549      */
1550     void NotifyAppStatusByCallerUid(const std::string &bundleName, const int32_t tokenId, const int32_t userId,
1551         const int32_t callerUid, const std::string &eventData);
1552     void SendHiSysEvent(const int32_t innerEventId, const int64_t eventId);
1553     int FinishUserTestLocked(
1554         const std::string &msg, const int64_t &resultCode, const std::shared_ptr<AppRunningRecord> &appRecord);
1555     int32_t GetCurrentAccountId() const;
1556     void SendReStartProcessEvent(AAFwk::EventInfo &eventInfo, int32_t appUid);
1557     void SendAppLaunchEvent(const std::shared_ptr<AppRunningRecord> &appRecord);
1558     void InitAppWaitingDebugList();
1559     void HandleConfigurationChange(const Configuration &config, const int32_t userId = -1);
1560     bool CheckIsThreadInFoundation(pid_t pid);
1561     bool CheckAppFault(const std::shared_ptr<AppRunningRecord> &appRecord, const FaultData &faultData);
1562     int32_t KillFaultApp(int32_t pid, const std::string &bundleName, const FaultData &faultData,
1563         bool isNeedExit = false);
1564     void AddUIExtensionLauncherItem(std::shared_ptr<AAFwk::Want> want, std::shared_ptr<AppRunningRecord> appRecord,
1565         sptr<IRemoteObject> token);
1566     void NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos);
1567     void RemoveUIExtensionLauncherItem(std::shared_ptr<AppRunningRecord> appRecord, sptr<IRemoteObject> token);
1568     bool IsSceneBoardCall();
1569     void CheckCleanAbilityByUserRequest(const std::shared_ptr<AppRunningRecord> &appRecord,
1570         const std::shared_ptr<AbilityRunningRecord> &abilityRecord, const AbilityState state);
1571     void GetPidsByAccessTokenId(const uint32_t accessTokenId, std::vector<pid_t> &pids);
1572     void DealMultiUserConfig(const Configuration &config, const int32_t userId);
1573     int32_t KillProcessByPidInner(const pid_t pid, const std::string& reason,
1574         const std::string& killReason, std::shared_ptr<AppRunningRecord> appRecord);
1575     bool IsAllowedNWebPreload(const std::string &processName);
1576     const std::string TASK_ON_CALLBACK_DIED = "OnCallbackDiedTask";
1577     std::vector<AppStateCallbackWithUserId> appStateCallbacks_;
1578     std::shared_ptr<RemoteClientManager> remoteClientManager_;
1579     std::shared_ptr<AppRunningManager> appRunningManager_;
1580     std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
1581     std::shared_ptr<AMSEventHandler> eventHandler_;
1582     std::shared_ptr<Configuration> configuration_;
1583     ffrt::mutex userTestLock_;
1584     ffrt::mutex appStateCallbacksLock_;
1585     ffrt::mutex renderUidSetLock_;
1586     ffrt::mutex exceptionLock_;
1587     ffrt::mutex browserHostLock_;
1588     sptr<IStartSpecifiedAbilityResponse> startSpecifiedAbilityResponse_;
1589     ffrt::mutex configurationObserverLock_;
1590     std::vector<ConfigurationObserverWithUserId> configurationObservers_;
1591     sptr<WindowFocusChangedListener> focusListener_;
1592     sptr<WindowVisibilityChangedListener> windowVisibilityChangedListener_;
1593     std::vector<std::shared_ptr<AppRunningRecord>> restartResedentTaskList_;
1594     std::map<std::string, std::vector<BaseSharedBundleInfo>> runningSharedBundleList_;
1595     std::map<std::string, bool> waitingDebugBundleList_;
1596     ffrt::mutex waitingDebugLock_;
1597     bool isInitAppWaitingDebugListExecuted_ = false;
1598     std::unordered_set<int32_t> renderUidSet_;
1599     std::string supportIsolationMode_ {"false"};
1600     std::string supportServiceExtMultiProcess_ {"false"};
1601     int32_t currentUserId_ = 0;
1602     int32_t lastRenderUid_ = Constants::START_UID_FOR_RENDER_PROCESS;
1603     sptr<IAbilityDebugResponse> abilityDebugResponse_;
1604     std::shared_ptr<AppDebugManager> appDebugManager_;
1605     ffrt::mutex killpedProcessMapLock_;
1606     mutable std::map<int64_t, std::string> killedProcessMap_;
1607     ffrt::mutex startChildProcessLock_;
1608     std::vector<std::string> serviceExtensionWhiteList_;
1609     std::shared_ptr<AbilityRuntime::AppRunningStatusModule> appRunningStatusModule_;
1610     std::shared_ptr<AdvancedSecurityModeManager> securityModeManager_;
1611     std::shared_ptr<AAFwk::TaskHandlerWrap> dfxTaskHandler_;
1612     std::shared_ptr<AAFwk::TaskHandlerWrap> otherTaskHandler_;
1613     std::shared_ptr<AppPreloader> appPreloader_;
1614     std::atomic<bool> sceneBoardAttachFlag_ = true;
1615 
1616     std::mutex loadTaskListMutex_;
1617     std::vector<LoadAbilityTaskFunc> loadAbilityTaskFuncList_;
1618     std::atomic<int32_t> willKillPidsNum_ = 0;
1619     std::shared_ptr<AAFwk::TaskHandlerWrap> delayKillTaskHandler_;
1620 
1621     std::shared_ptr<MultiUserConfigurationMgr> multiUserConfigurationMgr_;
1622     std::unordered_set<std::string> nwebPreloadSet_ {};
1623 };
1624 }  // namespace AppExecFwk
1625 }  // namespace OHOS
1626 #endif  // OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_INNER_H
1627