• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "background_app_info.h"
48 #include "bundle_info.h"
49 #include "bundle_mgr_helper.h"
50 #ifdef SUPPORT_CHILD_PROCESS
51 #include "child_process_info.h"
52 #include "child_process_request.h"
53 #endif // SUPPORT_CHILD_PROCESS
54 #include "configuration_policy.h"
55 #include "cpp/mutex.h"
56 #include "event_report.h"
57 #include "exit_resident_process_manager.h"
58 #include "fault_data.h"
59 #include "fd_guard.h"
60 #include "hisysevent.h"
61 #include "iapp_state_callback.h"
62 #include "iapplication_state_observer.h"
63 #include "iconfiguration_observer.h"
64 #include "iremote_object.h"
65 #include "irender_state_observer.h"
66 #include "istart_specified_ability_response.h"
67 #include "kia_interceptor_interface.h"
68 #include "kill_process_config.h"
69 #include "process_memory_state.h"
70 #include "process_util.h"
71 #include "record_query_result.h"
72 #include "refbase.h"
73 #include "remote_client_manager.h"
74 #include "render_process_info.h"
75 #include "running_process_info.h"
76 #include "shared/base_shared_bundle_info.h"
77 #include "task_handler_wrap.h"
78 #include "want.h"
79 #include "app_jsheap_mem_info.h"
80 #include "app_cjheap_mem_info.h"
81 #include "running_multi_info.h"
82 #include "multi_user_config_mgr.h"
83 #include "user_callback.h"
84 #include "native_child_notify_interface.h"
85 
86 namespace OHOS {
87 namespace AbilityRuntime {
88 struct LoadParam;
89 }
90 namespace Rosen {
91 class WindowVisibilityInfo;
92 class WindowPidVisibilityInfo;
93 class FocusChangeInfo;
94 }
95 namespace AppExecFwk {
96 using OHOS::AAFwk::Want;
97 using AAFwk::FdGuard;
98 class WindowFocusChangedListener;
99 class WindowVisibilityChangedListener;
100 class WindowPidVisibilityChangedListener;
101 using LoadAbilityTaskFunc = std::function<void()>;
102 constexpr int32_t BASE_USER_RANGE = 200000;
103 
104 class AppMgrServiceInner : public std::enable_shared_from_this<AppMgrServiceInner> {
105 public:
106     struct ConfigurationObserverWithUserId {
107         sptr<IConfigurationObserver> observer;
108         int32_t userId = -1;
109     };
110     struct AppStateCallbackWithUserId {
111         sptr<IAppStateCallback> callback;
112         int32_t userId = -1;
113     };
114 
115     AppMgrServiceInner();
116     virtual ~AppMgrServiceInner();
117 
118     /**
119      * Initialize the object.
120      */
121     void Init();
122     /**
123      * LoadAbility, load the ability that needed to be started.
124      *
125      * @param token, the unique identification to start the ability.
126      * @param preToken, the unique identification to call the ability.
127      * @param abilityInfo, the ability information.
128      * @param appInfo, the app information.
129      * @param want the ability want.
130      *
131      * @return
132      */
133     virtual void LoadAbility(std::shared_ptr<AbilityInfo> abilityInfo, std::shared_ptr<ApplicationInfo> appInfo,
134         std::shared_ptr<AAFwk::Want> want, std::shared_ptr<AbilityRuntime::LoadParam> loadParam);
135 
136     /**
137      * TerminateAbility, terminate the token ability.
138      *
139      * @param token, he unique identification to terminate the ability.
140      * @param clearMissionFlag, indicates whether terminate the ability when clearMission.
141      * @return
142      */
143     virtual void TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag);
144 
145     /**
146      * UpdateAbilityState, update the ability status.
147      *
148      * @param token, the unique identification to update the ability.
149      * @param state, ability status that needs to be updated.
150      *
151      * @return
152      */
153     virtual void UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state);
154 
155     /**
156      * UpdateExtensionState, call UpdateExtensionState() through the proxy object, update the extension status.
157      *
158      * @param token, the unique identification to update the extension.
159      * @param state, extension status that needs to be updated.
160      */
161     virtual void UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state);
162 
163     /**
164      * StateChangedNotifyObserver, Call ability state change.
165      *
166      * @param ability, the ability info.
167      * @param state, the ability state.
168      *
169      * @return
170      */
171     void StateChangedNotifyObserver(
172         const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged);
173 
174     /**
175      * RegisterAppStateCallback, register the callback.
176      *
177      * @param callback, Ams register the callback.
178      *
179      * @return
180      */
181     virtual void RegisterAppStateCallback(const sptr<IAppStateCallback> &callback);
182     void RemoveDeadAppStateCallback(const wptr<IRemoteObject> &remote);
183 
184     /**
185      * KillProcessByAbilityToken, kill the process by ability token.
186      *
187      * @param token, the unique identification to the ability.
188      * @return
189      */
190     virtual void KillProcessByAbilityToken(const sptr<IRemoteObject> &token);
191 
192     /**
193      * KillProcessesByUserId, kill the processes by userId.
194      * Send appSpawn uninstall debug hap message.
195      *
196      * @param userId, the user id.
197      * @param isNeedSendAppSpawnMsg, true send appSpawn message otherwise not send.
198      * @return
199      */
200     virtual void KillProcessesByUserId(int32_t userId, bool isNeedSendAppSpawnMsg = false,
201         sptr<AAFwk::IUserCallback> callback = nullptr);
202 
203     /**
204      * KillProcessesByPids, only in process call is allowed,
205      * kill the processes by pid list given.
206      *
207      * @param pids, the pid list of processes are going to be killed.
208      * @param reason, the reason to kill the processes.
209      * @param subProcess, kill SubProcess or not.
210      * @return ERR_OK, return back success, others fail.
211      */
212     virtual int32_t KillProcessesByPids(const std::vector<int32_t> &pids,
213         const std::string &reason = "KillProcessesByPids", bool subProcess = false, bool isKillPrecedeStart = false);
214 
215     /**
216      * KillProcessesInBatch, kill processes in batch;
217      * the killed bundle won't be started by the watcher.
218      *
219      * @param pids, the pid list of processes are going to be killed.
220      * @return ERR_OK, return back success, others fail.
221      */
222     virtual int32_t KillProcessesInBatch(const std::vector<int32_t> &pids);
223 
224     /**
225      * Set child and parent relationship
226      * @param token child process
227      * @param callerToken parent process
228      */
229     virtual void AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken);
230 
231     /**
232      * AttachApplication, get all the information needed to start the Application
233      * (data related to the Application ).
234      *
235      * @param appScheduler, information needed to start the Application.
236      *
237      * @return
238      */
239     virtual void AttachApplication(const pid_t pid, const sptr<IAppScheduler> &appScheduler);
240 
241     virtual bool CheckPreloadAppRecordExist(const std::string &bundleName, int32_t userId, int32_t appIndex);
242 
243     virtual int32_t PreloadApplicationByPhase(const std::string &bundleName, int32_t userId, int32_t appIndex,
244         AppExecFwk::PreloadPhase preloadPhase);
245 
246     /**
247      * Preload application.
248      *
249      * @param bundleName The bundle name of the application to preload.
250      * @param userId Indicates the user identification.
251      * @param preloadMode Preload application mode.
252      * @param appIndex The index of application clone.
253      * @return Returns ERR_OK on success, others on failure.
254      */
255     virtual int32_t PreloadApplication(const std::string &bundleName, int32_t userId,
256         AppExecFwk::PreloadMode preloadMode, int32_t appIndex);
257 
258     virtual int32_t NotifyPreloadAbilityStateChanged(sptr<IRemoteObject> token, bool isPreForeground);
259 
260     /**
261      * Launch Application Finished
262      * @param recordId id of the app record
263      */
264     virtual void PreloadModuleFinished(const int32_t pid);
265 
266     /**
267      * ApplicationForegrounded, set the application to Foreground State.
268      *
269      * @param recordId, a unique record that identifies this Application from others.
270      *
271      * @return
272      */
273     virtual void ApplicationForegrounded(const int32_t recordId);
274 
275     /**
276      * ApplicationBackgrounded, set the application to Backgrounded State.
277      *
278      * @param recordId, a unique record that identifies this Application from others.
279      *
280      * @return
281      */
282     virtual void ApplicationBackgrounded(const int32_t recordId);
283 
284     /**
285      * ApplicationTerminated, terminate the application.
286      *
287      * @param recordId, a unique record that identifies this Application from others.
288      *
289      * @return
290      */
291     virtual void ApplicationTerminated(const int32_t recordId);
292 
293     /**
294      * AbilityTerminated, terminate the ability.
295      *
296      * @param token, the unique identification to terminated the ability.
297      *
298      * @return
299      */
300     virtual void AbilityTerminated(const sptr<IRemoteObject> &token);
301 
302      /**
303      * UpdateApplicationInfoInstalled, update the application info after new module installed.
304      *
305      * @param bundleName, bundle name in Application record.
306      * @param  uid, uid.
307      * @return ERR_OK, return back success, others fail.
308      */
309     virtual int32_t UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid,
310         const std::string &moduleName, bool isPlugin);
311 
312     /**
313      * KillApplication, kill the application.
314      *
315      * @param  bundleName, bundle name in Application record.
316      *
317      * @return ERR_OK, return back success, others fail.
318      */
319     virtual int32_t KillApplication(const std::string &bundleName, bool clearPageStack = false, int32_t appIndex = 0);
320 
321     /**
322      * ForceKillApplication, force kill the application.
323      *
324      * @param  bundleName, bundle name in Application record.
325      * @param  userId, userId.
326      * @param  appIndex, appIndex.
327      * @return ERR_OK, return back success, others fail.
328      */
329     virtual int32_t ForceKillApplication(const std::string &bundleName, const int userId = -1,
330         const int appIndex = 0);
331 
332     /**
333      * KillProcessesByAccessTokenId.
334      *
335      * @param  accessTokenId, accessTokenId.
336      * @return ERR_OK, return back success, others fail.
337      */
338     virtual int32_t KillProcessesByAccessTokenId(const uint32_t accessTokenId);
339 
340     /**
341      * KillApplicationByUid, call KillApplicationByUid() through proxy object, kill the application.
342      *
343      * @param  bundleName, bundle name in Application record.
344      * @param  uid, uid.
345      * @param  reason, caller function name.
346      * @return ERR_OK, return back success, others fail.
347      */
348     virtual int32_t KillApplicationByUid(const std::string &bundleName, const int uid,
349         const std::string& reason = "KillApplicationByUid");
350 
351     virtual int32_t NotifyUninstallOrUpgradeApp(const std::string &bundleName, int32_t uid, bool isUpgrade);
352 
353     virtual void NotifyUninstallOrUpgradeAppEnd(int32_t uid);
354 
355     /**
356      * KillApplicationSelf, this allows app to terminate itself.
357      *
358      * @param clearPageStack, the flag indicates if ClearPageStack lifecycle should be scheduled.
359      * @return ERR_OK for success call, others for failure.
360      */
361     virtual int32_t KillApplicationSelf(const bool clearPageStack = false,
362         const std::string& reason = "KillApplicationSelf");
363 
364     int32_t KillAppSelfWithInstanceKey(const std::string &instanceKey, bool clearPageStack, const std::string& reason);
365 
366     /**
367      * KillApplicationByUserId, kill the application by user ID.
368      *
369      * @param bundleName, bundle name in Application record.
370      * @param appCloneIndex the app clone id.
371      * @param userId, user ID.
372      * @param  reason, caller function name.
373      *
374      * @return ERR_OK, return back success, others fail.
375      */
376     virtual int32_t KillApplicationByUserId(const std::string &bundleName, int32_t appCloneIndex, int userId,
377         const bool clearPageStack = false, const std::string& reason = "KillApplicationByUserId");
378 
379     /**
380      * update process rss and pss value.
381      *
382      * @param procMemStates, the memory states of all apps.
383      * @return ERR_OK, return back success, others fail.
384      */
385     virtual int32_t UpdateProcessMemoryState(const std::vector<ProcessMemoryState> &procMemState);
386 
387     /**
388      * ClearUpApplicationData, clear the application data.
389      *
390      * @param bundleName, bundle name in Application record.
391      * @param callerUid, app uid in Application record.
392      * @param callerPid, app pid in Application record.
393      * @param appCloneIndex the app clone id.
394      * @param userId the user id
395      * @return ERR_OK, return back success, others fail.
396      */
397     virtual int32_t ClearUpApplicationData(const std::string &bundleName,
398         int32_t callerUid, pid_t callerPid, int32_t appCloneIndex, int32_t userId = -1);
399 
400     /**
401      * ClearUpApplicationDataBySelf, clear the application data.
402      *
403      * @param callerUid, app uid in Application record.
404      * @param callerPid, app pid in Application record.
405      * @param userId, user ID.
406      *
407      * @return ERR_OK, return back success, others fail.
408      */
409     virtual int32_t ClearUpApplicationDataBySelf(int32_t callerUid, pid_t callerPid,  int32_t userId = -1);
410 
411     /**
412      * GetAllRunningProcesses, Obtains information about application processes that are running on the device.
413      *
414      * @param info, app name in Application record.
415      *
416      * @return ERR_OK ,return back success,others fail.
417      */
418     virtual int32_t GetAllRunningProcesses(std::vector<RunningProcessInfo> &info);
419 
420     /**
421      * GetRunningMultiAppInfoByBundleName, call GetRunningMultiAppInfoByBundleName through proxy project.
422      * Obtains information about TwinApp that are running on the device.
423      *
424      * @param bundleName, input.
425      * @param info, output multiapp information.
426      * @return void.
427      */
428     virtual int32_t GetRunningMultiAppInfoByBundleName(const std::string &bundleName,
429         RunningMultiAppInfo &info);
430 
431     /**
432      * GetAllRunningInstanceKeysBySelf, call GetAllRunningInstanceKeysBySelf() through proxy project.
433      * Obtains running instance keys of multi-instance app that are running on the device.
434      *
435      * @param instanceKeys, output instance keys of the multi-instance app.
436      * @return ERR_OK ,return back success,others fail.
437      */
438     virtual int32_t GetAllRunningInstanceKeysBySelf(std::vector<std::string> &instanceKeys);
439 
440     /**
441      * GetAllRunningInstanceKeysByBundleName, call GetAllRunningInstanceKeysByBundleName() through proxy project.
442      * Obtains running instance keys of multi-instance app that are running on the device.
443      *
444      * @param bundlename, bundle name in Application record.
445      * @param instanceKeys, output instance keys of the multi-instance app.
446      * @param userId, user id.
447      * @return ERR_OK ,return back success,others fail.
448      */
449     virtual int32_t GetAllRunningInstanceKeysByBundleName(const std::string &bundleName,
450         std::vector<std::string> &instanceKeys, int32_t userId = -1);
451 
452     /**
453      * GetRunningProcessesByBundleType, Obtains information about application processes by bundle type.
454      *
455      * @param bundleType, the bundle type of the application process
456      * @param info, app name in Application record.
457      *
458      * @return ERR_OK ,return back success,others fail.
459      */
460     virtual int32_t GetRunningProcessesByBundleType(BundleType bundleType, std::vector<RunningProcessInfo> &info);
461 
462     /**
463      * GetProcessRunningInfosByUserId, Obtains information about application processes that are running on the device.
464      *
465      * @param info, app name in Application record.
466      * @param userId, userId.
467      *
468      * @return ERR_OK ,return back success,others fail.
469      */
470     virtual int32_t GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId);
471 
472     /**
473      * GetProcessRunningInformation, Obtains information about current application process
474      * which is running on the device.
475      * @param info, app name in Application record.
476      *
477      * @return ERR_OK ,return back success,others fail.
478      */
479     virtual int32_t GetProcessRunningInformation(RunningProcessInfo &info);
480 
481     /**
482      * GetAllRenderProcesses, Obtains information about render processes that are running on the device.
483      *
484      * @param info, render process record.
485      *
486      * @return ERR_OK, return back success, others fail.
487      */
488     virtual int32_t GetAllRenderProcesses(std::vector<RenderProcessInfo> &info);
489 
490 #ifdef SUPPORT_CHILD_PROCESS
491     /**
492      * GetAllChildrenProcesses, call GetAllChildrenProcesses() through proxy project.
493      * Obtains information about children processes that are running on the device.
494      *
495      * @param info, child process info.
496      * @return ERR_OK, return back success, others fail.
497      */
498     virtual int GetAllChildrenProcesses(std::vector<ChildProcessInfo> &info);
499 #endif // SUPPORT_CHILD_PROCESS
500 
501     /**
502      * NotifyMemoryLevel, Notify applications background the current memory level.
503      *
504      * @param level, current memory level.
505      *
506      * @return ERR_OK ,return back success,others fail.
507      */
508     virtual int32_t NotifyMemoryLevel(int32_t level);
509 
510     /**
511      * NotifyProcMemoryLevel, Notify applications background the current memory level.
512      *
513      * @param procLevelMap , <pid_t, MemoryLevel>.
514      *
515      * @return ERR_OK ,return back success,others fail.
516      */
517     virtual int32_t NotifyProcMemoryLevel(const std::map<pid_t, MemoryLevel> &procLevelMap);
518 
519     /**
520      * DumpHeapMemory, get the application's memory info.
521      * Get the application's memory allocation info.
522      *
523      * @param pid, pid input.
524      * @param mallocInfo, dynamic storage information output.
525      *
526      * @return ERR_OK, return back success,others fail.
527      */
528     virtual int32_t DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo);
529 
530     /**
531      * DumpJsHeapMemory, call DumpJsHeapMemory() through proxy project.
532      * triggerGC and dump the application's jsheap memory info.
533      *
534      * @param info, pid, tid, needGc, needSnapshot
535      * @return Returns ERR_OK on success, others on failure.
536      */
537     virtual int32_t DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info);
538 
539     /**
540      * DumpCjHeapMemory, call DumpCjHeapMemory() through proxy project.
541      * triggerGC and dump the application's cjheap memory info.
542      *
543      * @param info, pid, needGc, needSnapshot
544      * @return Returns ERR_OK on success, others on failure.
545      */
546     virtual int32_t DumpCjHeapMemory(OHOS::AppExecFwk::CjHeapDumpInfo &info);
547 
548     /**
549      * @brief Check whether the shared bundle is running.
550      *
551      * @param bundleName Shared bundle name.
552      * @param versionCode Shared bundle version code.
553      * @return Returns the shared bundle running result. The result is true if running, false otherwise.
554      */
555     virtual bool IsSharedBundleRunning(const std::string &bundleName, uint32_t versionCode);
556 
557     /**
558      * Check whether the bundle is running.
559      *
560      * @param bundleName Indicates the bundle name of the bundle.
561      * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise.
562      * @return Return ERR_OK if success, others fail.
563      */
564     int32_t IsApplicationRunning(const std::string &bundleName, bool &isRunning);
565 
566     /**
567      * Check whether the bundle is running.
568      *
569      * @param bundleName Indicates the bundle name of the bundle.
570      * @param appCloneIndex the appindex of the bundle.
571      * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise.
572      * @return Return ERR_OK if success, others fail.
573      */
574     int32_t IsAppRunning(const std::string &bundleName, int32_t appCloneIndex, bool &isRunning);
575 
576     /**
577      * Check whether the process of the application under the specified user exists.
578      *
579      * @param bundleName Indicates the bundle name of the bundle.
580      * @param userId the userId of the bundle.
581      * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise.
582      * @return Return ERR_OK if success, others fail.
583      */
584     virtual int32_t IsAppRunningByBundleNameAndUserId(const std::string &bundleName, int32_t userId,
585         bool &isRunning);
586 
587     /**
588      * start native process for debugger.
589      *
590      * @param want param to start a process.
591      */
592     int32_t StartNativeProcessForDebugger(const AAFwk::Want &want);
593 
594     std::shared_ptr<AppRunningRecord> CreateAppRunningRecord(
595         std::shared_ptr<AbilityRuntime::LoadParam> loadParam,
596         std::shared_ptr<ApplicationInfo> appInfo,
597         std::shared_ptr<AbilityInfo> abilityInfo,
598         const std::string &processName,
599         const BundleInfo &bundleInfo,
600         const HapModuleInfo &hapModuleInfo,
601         std::shared_ptr<AAFwk::Want> want,
602         bool isKia = false);
603 
604     /**
605      * OnStop, Application management service stopped.
606      *
607      * @return
608      */
609     void OnStop();
610 
611     /**
612      * OpenAppSpawnConnection, Open connection with appspawn.
613      *
614      * @return ERR_OK ,return back success,others fail.
615      */
616     virtual ErrCode OpenAppSpawnConnection();
617 
618     /**
619      * CloseAppSpawnConnection, Close connection with appspawn.
620      *
621      * @return
622      */
623     virtual void CloseAppSpawnConnection() const;
624 
625     /**
626      * QueryAppSpawnConnectionState, Query the connection status with appspawn.
627      *
628      * @return Returns the connection status with appspawn.
629      */
630     virtual SpawnConnectionState QueryAppSpawnConnectionState() const;
631 
632     /**
633      * SetAppSpawnClient, Setting the client to connect with appspawn.
634      *
635      * @param spawnClient, the client to connect with appspawn.
636      *
637      * @return
638      */
639     void SetAppSpawnClient(std::shared_ptr<AppSpawnClient> spawnClient);
640 
641     // Schedule launch application with specified |appRecord|
642 
643     /**
644      * LaunchApplication, Notify application to launch application.
645      *
646      * @param appRecord, the application record.
647      *
648      * @return
649      */
650     void LaunchApplication(const std::shared_ptr<AppRunningRecord> &appRecord);
651 
652     /**
653      * LaunchApplicationExt, to solve the nbnc of method over 50.
654      *
655      * @param appRecord, the application record.
656      *
657      * @return
658      */
659     void LaunchApplicationExt(const std::shared_ptr<AppRunningRecord> &appRecord);
660 
661     /**
662      * Notice of AddAbilityStageInfo()
663      *
664      * @param recordId, the application record.
665      */
666     virtual void AddAbilityStageDone(const int32_t recordId);
667 
668     /**
669      * GetAppRunningRecordByPid, Get process record by application pid.
670      *
671      * @param pid, the application pid.
672      *
673      * @return process record.
674      */
675     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByPid(const pid_t pid) const;
676 
677     /**
678      * GetAppRunningRecordByAbilityToken, Get process record by ability token.
679      *
680      * @param abilityToken, the ability token.
681      *
682      * @return process record.
683      */
684     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAbilityToken(const sptr<IRemoteObject> &abilityToken) const;
685 
686     /**
687      * GetTerminatingAppRunningRecord, Get process record by ability token.
688      *
689      * @param abilityToken, the ability token.
690      *
691      * @return process record.
692      */
693     std::shared_ptr<AppRunningRecord> GetTerminatingAppRunningRecord(const sptr<IRemoteObject> &token) const;
694 
695     /**
696      * GetAppRunningRecordByAppRecordId, Get process record by application id.
697      *
698      * @param recordId, the application id.
699      *
700      * @return process record.
701      */
702     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAppRecordId(const int32_t recordId) const;
703 
704     /**
705      * OnAbilityStateChanged, Call ability state change.
706      *
707      * @param ability, the ability info.
708      * @param state, the ability state.
709      *
710      * @return
711      */
712     void OnAbilityStateChanged(const std::shared_ptr<AbilityRunningRecord> &ability, const AbilityState state);
713 
714     /**
715      * OnRemoteDied, Equipment death notification.
716      *
717      * @param remote, Death client.
718      * @param isRenderProcess is render process died.
719      * @param isChildProcess is child process died.
720      * @return
721      */
722     void OnRemoteDied(const wptr<IRemoteObject> &remote, bool isRenderProcess = false, bool isChildProcess = false);
723 
724     /**
725      * @brief Call the callbacks to notify one process should go dying due fatal error.
726      * @param appRecord indicates the process is going to die.
727      */
728     void NotifyStartProcessFailed(std::shared_ptr<AppRunningRecord> appRecord);
729 
730     void HandleTimeOut(const AAFwk::EventWrap &event);
731 
732     void CacheExitInfo(const std::shared_ptr<AppRunningRecord> &appRecord);
733 
DecreaseWillKillPidsNum()734     void DecreaseWillKillPidsNum()
735     {
736         willKillPidsNum_ -= 1;
737     }
738 
SetTaskHandler(std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler)739     void SetTaskHandler(std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler)
740     {
741         taskHandler_ = taskHandler;
742     }
743 
SetEventHandler(const std::shared_ptr<AMSEventHandler> & eventHandler)744     void SetEventHandler(const std::shared_ptr<AMSEventHandler> &eventHandler)
745     {
746         eventHandler_ = eventHandler;
747     }
748 
749     /**
750      * HandleAbilityAttachTimeOut, called by ability manager service when an ability is loaded timeout.
751      *
752      * @param token indicates the token of the ability which is timeout.
753      */
754     void HandleAbilityAttachTimeOut(const sptr<IRemoteObject> &token);
755 
756     /**
757      * PrepareTerminate, called before terminating one ability by ability manager service to notify application.
758      *
759      * @param token indicates the token of the ability to be terminated.
760      * @param clearMissionFlag indicates whether it is caused by cleaning mission.
761      */
762     void PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag = false);
763 
764     void OnAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, const ApplicationState state,
765         bool needNotifyApp, bool isFromWindowFocusChanged);
766 
767     void OnAppStarted(const std::shared_ptr<AppRunningRecord> &appRecord);
768 
769     void OnAppStopped(const std::shared_ptr<AppRunningRecord> &appRecord);
770 
771     /**
772      * GetRunningProcessInfoByToken, get process info for one ability.
773      *
774      * @param token indicates the token of the ability requested.
775      * @param info output of the information.
776      */
777     void GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info);
778 
779     int32_t GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info) const;
780 
781     int32_t GetRunningProcessInfoByChildProcessPid(const pid_t childPid,
782         OHOS::AppExecFwk::RunningProcessInfo &info) const;
783 
784     /**
785      * Set AbilityForegroundingFlag of an app-record to true.
786      *
787      * @param pid, pid.
788      *
789      */
790     void SetAbilityForegroundingFlagToAppRecord(const pid_t pid) const;
791 
792      /**
793      * UpdateConfiguration, ANotify application update system environment changes.
794      *
795      * @param config, System environment change parameters.
796      * @return Returns ERR_OK on success, others on failure.
797      */
798     int32_t UpdateConfiguration(const Configuration &config, const int32_t userId = -1);
799 
800     /**
801      * UpdateConfigurationForBackgroundApp
802      * @param appInfos Background application information.
803      * @param policy Update policy.
804      * @param userId configuration for the user
805      * @return Returns ERR_OK on success, others on failure.
806      */
807     int32_t UpdateConfigurationForBackgroundApp(const std::vector<BackgroundAppInfo> &allowAppList,
808         const AppExecFwk::ConfigurationPolicy &policy, const int32_t userId);
809 
810     int32_t UpdateConfigurationByBundleName(const Configuration &config, const std::string &name, int32_t appIndex);
811 
812     std::shared_ptr<AppExecFwk::Configuration> GetConfiguration();
813 
814     int32_t RegisterConfigurationObserver(const sptr<IConfigurationObserver>& observer);
815 
816     int32_t UnregisterConfigurationObserver(const sptr<IConfigurationObserver>& observer);
817 
818     /**
819      * Start empty process
820      */
821     void LoadResidentProcess(const std::vector<BundleInfo> &infos);
822 
823     void StartResidentProcess(const std::vector<BundleInfo> &infos,  int restartCount, bool isEmptyKeepAliveApp);
824 
825     bool CheckRemoteClient();
826 
827     /**
828      * Register application or process state observer.
829      * @param observer, ability token.
830      * @return Returns ERR_OK on success, others on failure.
831      */
832     int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer,
833         const std::vector<std::string> &bundleNameList = {});
834 
835     /**
836      * Unregister application or process state observer.
837      * @param observer, ability token.
838      * @return Returns ERR_OK on success, others on failure.
839      */
840     int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer);
841 
842     /**
843      * Register application or process state observer.
844      * @param observer, Is ability foreground state observer
845      * @return Returns ERR_OK on success, others on failure.
846      */
847     int32_t RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer);
848 
849     /**
850      * Unregister application or process state observer.
851      * @param observer, Is ability foreground state observer
852      * @return Returns ERR_OK on success, others on failure.
853      */
854     int32_t UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer);
855 
856     /**
857      * Get Foreground Applications.
858      *
859      * @return Foreground Applications.
860      */
861     int32_t GetForegroundApplications(std::vector<AppStateData> &list);
862 
863     /**
864      * Start user test process.
865      * @param want, want object.
866      * @param observer, test observer remote object.
867      * @param bundleInfo, bundle info.
868      * @param userId the user id.
869      * @return Returns ERR_OK on success, others on failure.
870      */
871     int StartUserTestProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer,
872         const AppExecFwk::BundleInfo &bundleInfo, int32_t userId);
873 
874     /**
875      * @brief Finish user test.
876      * @param msg user test message.
877      * @param resultCode user test result Code.
878      * @param bundleName user test bundleName.
879      * @param pid the user test process id.
880      *
881      * @return Returns ERR_OK on success, others on failure.
882      */
883     int FinishUserTest(
884         const std::string &msg, const int64_t &resultCode, const std::string &bundleName, const pid_t &pid);
885 
886     /**
887      * Start specified ability.
888      *
889      * @param want Want contains information of the ability to start.
890      * @param abilityInfo Ability information.
891      * @param requestId request id to callback
892      */
893     void StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
894         int32_t requestId = 0);
895 
896     /**
897      * Start specified process.
898      *
899      * @param want Want contains information wish to start.
900      * @param abilityInfo Ability information.
901      * @param requestId for callback
902      */
903     void StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
904         int32_t requestId = 0, const std::string &customProcess = "");
905 
906     void RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response);
907 
908     /**
909      * @brief called when the abilityStage's onAcceptWant lifecycle completed.
910      * @param recordId record id of the ability.
911      * @param want want has been accepted.
912      * @param flag flag get from OnAcceptWant.
913      */
914     void ScheduleAcceptWantDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag);
915 
916     void SchedulePrepareTerminate(const pid_t pid, const std::string &moduleName);
917 
918     void ScheduleNewProcessRequestDone(const int32_t recordId, const AAFwk::Want &want, const std::string &flag);
919 
920     /**
921      *  Get the token of ability records by process ID.
922      *
923      * @param pid The process id.
924      * @param tokens The token of ability records.
925      * @return Returns true on success, others on failure.
926      */
927     int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens);
928 
929     virtual int32_t PreStartNWebSpawnProcess(const pid_t hostPid);
930 
931     virtual int32_t StartRenderProcess(const pid_t hostPid,
932                                        const std::string &renderParam,
933                                        FdGuard &&ipcFd, FdGuard &&sharedFd,
934                                        FdGuard &&crashFd, pid_t &renderPid, bool isGPU = false);
935 
936     virtual void AttachRenderProcess(const pid_t pid, const sptr<IRenderScheduler> &scheduler);
937 
938     virtual int GetRenderProcessTerminationStatus(pid_t renderPid, int &status);
939 
940     int VerifyKillProcessPermission(const sptr<IRemoteObject> &token) const;
941 
942     int VerifyAccountPermission(const std::string &permissionName, const int userId) const;
943 
944     int VerifyRequestPermission() const;
945 
946     virtual bool IsFoundationCall() const;
947 
948     void ClearAppRunningData(const std::shared_ptr<AppRunningRecord> &appRecord);
949 
950     void TerminateApplication(const std::shared_ptr<AppRunningRecord> &appRecord);
951 
952     int GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug);
953 
954     /**
955      * Record process exit reason to appRunningRecord
956      * @param pid pid
957      * @param reason reason enum
958      * @param exitMsg exitMsg
959      * @return Returns ERR_OK on success, others on failure.
960      */
961     int32_t NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg);
962 
963     /**
964      * Notify application status.
965      *
966      * @param bundleName Indicates the name of the bundle.
967      * @param eventData Indicates the event defined by CommonEventSupport
968      *
969      * @return
970      */
971     void NotifyAppStatus(const std::string &bundleName, int32_t appIndex, const std::string &eventData);
972 
973     /**
974      * Notify application status by event name.
975      *
976      * @param bundleName Indicates the name of the bundle.
977      * @param eventName Indicates the event defined by CommonEventSupport
978      * @param want Want contains information of the event wish to notify.
979      *
980      * @return Returns 0 if the event is normal, 1 if the event needs to be managed.
981      */
982     int32_t NotifyAppStatusByCommonEventName(const std::string &bundleName, const std::string &eventName,
983         const Want &want);
984 
985     int32_t KillProcessByPid(const pid_t pid, const std::string& reason = "foundation",
986         bool isKillPrecedeStart = false);
987 
988     int32_t KillSubProcessBypidInner(const pid_t pid, const std::string &reason,
989         AAFwk::EventInfo &eventInfo);
990 
991     int32_t KillSubProcessBypid(const pid_t pid, const std::string &reason);
992 
993     /**
994      * @brief Get the running state of application by bundle name.
995      *
996      * @param bundleName Bundle name
997      * @return Returns true if process is running, false if process isn't running.
998      */
999     bool GetAppRunningStateByBundleName(const std::string &bundleName);
1000 
1001     /**
1002      * @brief Notify application load patch.
1003      *
1004      * @param bundleName Bundle name
1005      * @param callback called when LoadPatch finished.
1006      * @return Returns ERR_OK on success, error code on failure.
1007      */
1008     int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback);
1009 
1010     int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback);
1011 
1012     int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback);
1013 #ifdef SUPPORT_SCREEN
1014     void HandleFocused(const sptr<OHOS::Rosen::FocusChangeInfo> &focusChangeInfo);
1015     void HandleUnfocused(const sptr<OHOS::Rosen::FocusChangeInfo> &focusChangeInfo);
1016 
1017     /**
1018      * Handle window visibility changed.
1019      */
1020     void HandleWindowVisibilityChanged(
1021             const std::vector<sptr<OHOS::Rosen::WindowVisibilityInfo>> &windowVisibilityInfos);
1022 
1023     /**
1024      * Handle window pid visibility changed.
1025      */
1026     void HandleWindowPidVisibilityChanged(const sptr<OHOS::Rosen::WindowPidVisibilityInfo>& windowPidVisibilityInfo);
1027 #endif //SUPPORT_SCREEN
1028     /**
1029      * Set the current userId, only used by abilityMgr.
1030      *
1031      * @param userId the user id.
1032      *
1033      * @return
1034      */
1035     void SetCurrentUserId(const int32_t userId);
1036 
1037     /**
1038      * Set enable start process flag by userId
1039      * @param userId the user id.
1040      * @param enableStartProcess enable start process.
1041      * @return
1042      */
1043     void SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess);
1044 
1045 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
1046     int32_t SetContinuousTaskProcess(int32_t pid, bool isContinuousTask);
1047 #endif
1048 
1049     /**
1050      * Get bundleName by pid.
1051      *
1052      * @param pid process id.
1053      * @param bundleName Output parameters, return bundleName.
1054      * @param uid Output parameters, return userId.
1055      * @return Returns ERR_OK on success, others on failure.
1056      */
1057     int32_t GetBundleNameByPid(const int32_t pid, std::string &bundleName, int32_t &uid);
1058 
1059     /**
1060      * Notify Fault Data
1061      *
1062      * @param faultData the fault data.
1063      * @return Returns ERR_OK on success, others on failure.
1064      */
1065     int32_t NotifyAppFault(const FaultData &faultData);
1066 
1067     /**
1068      * Transformed Notify Fault Data
1069      *
1070      * @param faultData Transformed the fault data.
1071      * @return Returns ERR_OK on success, others on failure.
1072      */
1073 
1074     int32_t TransformedNotifyAppFault(const AppFaultDataBySA &faultData);
1075 
1076     /**
1077      * Notify Fault Data By SA
1078      *
1079      * @param faultData the fault data notified by SA.
1080      * @return Returns ERR_OK on success, others on failure.
1081      */
1082     int32_t NotifyAppFaultBySA(const AppFaultDataBySA &faultData);
1083 
1084     /**
1085      * Set Appfreeze Detect Filter
1086      *
1087      * @param pid the process pid.
1088      * @return Returns true on success, others on failure.
1089      */
1090     bool SetAppFreezeFilter(int32_t pid);
1091 
1092     /**
1093      * get memorySize by pid.
1094      *
1095      * @param pid process id.
1096      * @param memorySize Output parameters, return memorySize in KB.
1097      * @return Returns ERR_OK on success, others on failure.
1098      */
1099     virtual int32_t GetProcessMemoryByPid(const int32_t pid, int32_t &memorySize);
1100 
1101     /**
1102      * get application processes information list by bundleName.
1103      *
1104      * @param bundleName Bundle name.
1105      * @param userId user Id in Application record.
1106      * @param info Output parameters, return running process info list.
1107      * @return Returns ERR_OK on success, others on failure.
1108      */
1109     virtual int32_t GetRunningProcessInformation(
1110         const std::string &bundleName, int32_t userId, std::vector<RunningProcessInfo> &info);
1111 
1112     /**
1113      * init focusListener.
1114      *
1115      * @return
1116      */
1117     void InitFocusListener();
1118 
1119     /**
1120      * free focusListener.
1121      *
1122      * @return
1123      */
1124     void FreeFocusListener();
1125 
1126     /**
1127      * Init window visibility changed listener.
1128      */
1129     void InitWindowVisibilityChangedListener();
1130 
1131     /**
1132      * Free window visibility changed listener.
1133      */
1134     void FreeWindowVisibilityChangedListener();
1135 
1136     /**
1137      * Init window pid visibility changed listener.
1138      */
1139     void InitWindowPidVisibilityChangedListener();
1140 
1141     /**
1142      * Free window pid visibility changed listener.
1143      */
1144     void FreeWindowPidVisibilityChangedListener();
1145 
1146     /*
1147      * @brief Notify NativeEngine GC of status change.
1148      *
1149      * @param state GC state
1150      * @param pid pid
1151      *
1152      * @return Is the status change completed.
1153      */
1154     int32_t ChangeAppGcState(pid_t pid, int32_t state, uint64_t tid = 0);
1155 
1156     /**
1157      * @brief Register app debug listener.
1158      * @param listener App debug listener.
1159      * @return Returns ERR_OK on success, others on failure.
1160      */
1161     int32_t RegisterAppDebugListener(const sptr<IAppDebugListener> &listener);
1162 
1163     /**
1164      * @brief Unregister app debug listener.
1165      * @param listener App debug listener.
1166      * @return Returns ERR_OK on success, others on failure.
1167      */
1168     int32_t UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener);
1169 
1170     /**
1171      * @brief Attach app debug.
1172      * @param bundleName The application bundle name.
1173      * @return Returns ERR_OK on success, others on failure.
1174      */
1175     int32_t AttachAppDebug(const std::string &bundleName, bool isDebugFromLocal);
1176 
1177     /**
1178      * @brief Detach app debug.
1179      * @param bundleName The application bundle name.
1180      * @return Returns ERR_OK on success, others on failure.
1181      */
1182     int32_t DetachAppDebug(const std::string &bundleName);
1183 
1184     /**
1185      * @brief Set app waiting debug mode.
1186      * @param bundleName The application bundle name.
1187      * @param isPersist The persist flag.
1188      * @return Returns ERR_OK on success, others on failure.
1189      */
1190     int32_t SetAppWaitingDebug(const std::string &bundleName, bool isPersist);
1191 
1192     /**
1193      * @brief Cancel app waiting debug mode.
1194      * @return Returns ERR_OK on success, others on failure.
1195      */
1196     int32_t CancelAppWaitingDebug();
1197 
1198     /**
1199      * @brief Get waiting debug mode application.
1200      * @param bundleNameList The application bundle name list.
1201      * @return Returns ERR_OK on success, others on failure.
1202      */
1203     int32_t GetWaitingDebugApp(std::vector<std::string> &bundleNameList);
1204 
1205     /**
1206      * @brief Determine whether it is a waiting debug application based on the bundle name.
1207      * @return Returns true if it is a waiting debug application, otherwise it returns false.
1208      */
1209     bool IsWaitingDebugApp(const std::string &bundleName);
1210 
1211     /**
1212      * @brief Clear non persist waiting debug flag.
1213      */
1214     void ClearNonPersistWaitingDebugFlag();
1215 
1216     /**
1217      * @brief Registering ability debug mode response.
1218      * @param response Response for ability debug object.
1219      * @return Returns ERR_OK on success, others on failure.
1220      */
1221     int32_t RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response);
1222 
1223     /**
1224      * @brief Determine whether it is an attachment debug application based on the bundle name.
1225      * @param bundleName The application bundle name.
1226      * @return Returns true if it is an attach debug application, otherwise it returns false.
1227      */
1228     bool IsAttachDebug(const std::string &bundleName);
1229 
1230     /**
1231      * @brief Notify AbilityManagerService the page show.
1232      * @param token Ability identify.
1233      * @param pageStateData The data of ability's page state.
1234      * @return Returns ERR_OK on success, others on failure.
1235      */
1236     virtual int32_t NotifyPageShow(const sptr<IRemoteObject> &token, const PageStateData &pageStateData);
1237 
1238     /**
1239      * @brief Notify AbilityManagerService the page hide.
1240      * @param token Ability identify.
1241      * @param pageStateData The data of ability's page state.
1242      * @return Returns ERR_OK on success, others on failure.
1243      */
1244     virtual int32_t NotifyPageHide(const sptr<IRemoteObject> &token, const PageStateData &pageStateData);
1245 
1246     /**
1247      * Register appRunning status listener.
1248      *
1249      * @param listener Running status listener.
1250      * @return Returns ERR_OK on success, others on failure.
1251      */
1252     int32_t RegisterAppRunningStatusListener(const sptr<IRemoteObject> &listener);
1253 
1254     /**
1255      * Unregister appRunning status listener.
1256      *
1257      * @param listener Running status listener.
1258      * @return Returns ERR_OK on success, others on failure.
1259      */
1260     int32_t UnregisterAppRunningStatusListener(const sptr<IRemoteObject> &listener);
1261 
1262     /**
1263      * Register application foreground state observer.
1264      * @param observer Is app foreground statue observer
1265      * @return Returns ERR_OK on success, others on failure.
1266      */
1267     int32_t RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer);
1268 
1269     /**
1270      * Unregister application foreground state observer.
1271      * @param observer Is app foreground statue observer
1272      * @return Returns ERR_OK on success, others on failure.
1273      */
1274     int32_t UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer);
1275 
1276 #ifdef SUPPORT_CHILD_PROCESS
1277     /**
1278      * Start child process, called by ChildProcessManager.
1279      *
1280      * @param callingPid Calling process pid.
1281      * @param childPid Created child process pid.
1282      * @param request Child process start request params.
1283      * @return Returns ERR_OK on success, others on failure.
1284      */
1285     virtual int32_t StartChildProcess(const pid_t callingPid, pid_t &childPid, const ChildProcessRequest &request);
1286 
1287     /**
1288      * Get child process record for self.
1289      *
1290      * @return child process record.
1291      */
1292     virtual int32_t GetChildProcessInfoForSelf(ChildProcessInfo &info);
1293 
1294     /**
1295      * Attach child process scheduler to app manager service.
1296      *
1297      * @param pid the child process pid to exit.
1298      * @param childScheduler scheduler of child process.
1299      */
1300     virtual void AttachChildProcess(const pid_t pid, const sptr<IChildScheduler> &childScheduler);
1301 
1302     /**
1303      * Exit child process safely by child process pid.
1304      *
1305      * @param pid child process pid.
1306      */
1307     virtual void ExitChildProcessSafelyByChildPid(const pid_t pid);
1308 
1309     /**
1310      * Start native child process, callde by ChildProcessManager.
1311      * @param hostPid Host process pid.
1312      * @param childProcessCount current started child process count
1313      * @param libName lib file name to be load in child process
1314      * @param callback callback for notify start result
1315      * @return Returns ERR_OK on success, others on failure.
1316      */
1317     virtual int32_t CreateNativeChildProcess(const pid_t hostPid, const std::string &libName, int32_t childProcessCount,
1318         const sptr<IRemoteObject> &callback, const std::string &customProcessName);
1319 #endif // SUPPORT_CHILD_PROCESS
1320 
1321     virtual int32_t RegisterNativeChildExitNotify(const sptr<INativeChildNotify> &callback);
1322 
1323     virtual int32_t UnregisterNativeChildExitNotify(const sptr<INativeChildNotify> &callback);
1324 
1325     /**
1326      * Elevate the current process to be a candidate master process.
1327      *
1328      * @param isInsertToHead Whether inset current process to the head of candidate master process list.
1329      * @return Return ERR_OK if success, others fail.
1330      */
1331     int32_t PromoteCurrentToCandidateMasterProcess(bool isInsertToHead);
1332 
1333     /**
1334      * Revoke current process as a candidate master process.
1335      *
1336      * @return Return ERR_OK if success, others fail.
1337      */
1338     int32_t DemoteCurrentFromCandidateMasterProcess();
1339 
1340     /**
1341      * To clear the process by ability token.
1342      *
1343      * @param token the unique identification to the ability.
1344      */
1345     void ClearProcessByToken(sptr<IRemoteObject> token);
1346 
1347     /**
1348      * Whether the current application process is the last surviving process.
1349      * @param bundleName To query the bundle name of a process.
1350      * @return Returns true is final application process, others return false.
1351      */
1352     bool IsFinalAppProcessByBundleName(const std::string &bundleName);
1353 
1354     /**
1355      * @brief Notify memory size state changed: LOW_MEMORY, MEMORY_RECOVERY, REQUIRE_BIG_MEMORY, NO_REQUIRE_BIG_MEMORY.
1356      * @param memorySizeState Indicates the memory size state.
1357      * @return Returns ERR_OK on success, others on failure.
1358      */
1359     int32_t NotifyMemorySizeStateChanged(int32_t memorySizeState);
1360 
1361     /**
1362      * whether memory size is sufficient.
1363      * @return Returns true is sufficient memory size, others return false.
1364      */
1365     bool IsMemorySizeSufficient();
1366 
1367     /**
1368      * whether or not requier a big memory
1369      * @return Returens true is no big memory, others return false.
1370      */
1371     bool IsNoRequireBigMemory();
1372 
1373     /**
1374      * Register render state observer.
1375      * @param observer Render process state observer.
1376      * @return Returns ERR_OK on success, others on failure.
1377      */
1378     int32_t RegisterRenderStateObserver(const sptr<IRenderStateObserver> &observer);
1379 
1380     /**
1381      * Unregister render state observer.
1382      * @param observer Render process state observer.
1383      * @return Returns ERR_OK on success, others on failure.
1384      */
1385     int32_t UnregisterRenderStateObserver(const sptr<IRenderStateObserver> &observer);
1386 
1387     /**
1388      * Update render state.
1389      * @param renderPid Render pid.
1390      * @param state foreground or background state.
1391      * @return Returns ERR_OK on success, others on failure.
1392      */
1393     int32_t UpdateRenderState(pid_t renderPid, int32_t state);
1394 
1395     /**
1396      * Mark processes of the uid as the app is going to be restarted.
1397      */
1398     int32_t SignRestartAppFlag(int32_t uid, const std::string &instanceKey);
1399 
1400     /**
1401      * Set application assertion pause state.
1402      *
1403      * @param flag assertion pause state.
1404      */
1405     void SetAppAssertionPauseState(bool flag);
1406 
1407     /**
1408      * @brief A process can config itself to keep alive or not.
1409      * when one process started, this method will be called from ability mgr with data selected from db.
1410      *
1411      * @param bundleName processed of witch to be configed
1412      * @param enable config value
1413      * @param uid indicates user, 0 for all users
1414      */
1415     void SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid);
1416 
1417     /**
1418      * @brief Set non-resident keep-alive process status.
1419      * when one process started, this method will be called from ability mgr with data selected from db.
1420      *
1421      * @param bundleName processed of witch to be configed
1422      * @param enable config value
1423      * @param uid indicates user, 0 for all users
1424      */
1425     void SetKeepAliveDkv(const std::string &bundleName, bool enable, int32_t uid);
1426 
1427     void SetKeepAliveAppService(const std::string &bundleName, bool enable, int32_t uid);
1428 
1429     int32_t GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId);
1430 
1431     int32_t GetAllUIExtensionRootHostPid(pid_t pid, std::vector<pid_t> &hostPids);
1432 
1433     int32_t GetAllUIExtensionProviderPid(pid_t hostPid, std::vector<pid_t> &providerPids);
1434 
1435     virtual int DumpIpcAllStart(std::string& result);
1436 
1437     virtual int DumpIpcAllStop(std::string& result);
1438 
1439     virtual int DumpIpcAllStat(std::string& result);
1440 
1441     virtual int DumpIpcStart(const int32_t pid, std::string& result);
1442 
1443     virtual int DumpIpcStop(const int32_t pid, std::string& result);
1444 
1445     virtual int DumpIpcStat(const int32_t pid, std::string& result);
1446 
1447     virtual int DumpFfrt(const std::vector<int32_t>& pids, std::string& result);
1448 
1449     /**
1450      * @brief set support process cache by self
1451      */
1452     int32_t SetSupportedProcessCacheSelf(bool isSupport);
1453 
1454     int32_t SetSupportedProcessCache(int32_t pid, bool isSupport);
1455 
1456     /**
1457       * @brief Get supported process cache.
1458       * @param pid Process pid.
1459       * @param isSupport Supported process cache.
1460       * @return Returns ERR_OK on success, others on failure.
1461       */
1462     int32_t IsProcessCacheSupported(int32_t pid, bool &isSupported);
1463 
1464     int32_t SetProcessCacheEnable(int32_t pid, bool enable);
1465 
1466     void OnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord, ApplicationState state);
1467 
1468     /**
1469      * set browser channel for caller
1470      * @param hostPid caller pid
1471      */
1472     virtual void SaveBrowserChannel(const pid_t hostPid, sptr<IRemoteObject> browser);
1473 
1474     bool IsAppProcessesAllCached(const std::string &bundleName, int32_t uid,
1475         const std::set<std::shared_ptr<AppRunningRecord>> &cachedSet);
1476 
1477     /**
1478      * Check caller is test ability
1479      *
1480      * @param pid, the pid of ability.
1481      * @return Returns ERR_OK is test ability, others is not test ability.
1482      */
1483     int32_t CheckCallingIsUserTestModeInner(const pid_t pid, bool &isUserTest);
1484 
1485     bool GetSceneBoardAttachFlag() const;
1486 
1487     void SetSceneBoardAttachFlag(bool flag);
1488 
1489     /**
1490      * Scene board has the highest priority. If Scene board is loading cache other apps' request.
1491      */
1492     void CacheLoadAbilityTask(const LoadAbilityTaskFunc&& func);
1493 
1494     void SubmitCacheLoadAbilityTask();
1495     /**
1496      * Notifies that one ability is attached to status bar.
1497      *
1498      * @param token the token of the abilityRecord that is attached to status bar.
1499      */
1500     void AttachedToStatusBar(const sptr<IRemoteObject> &token);
1501     void KillApplicationByRecord(const std::shared_ptr<AppRunningRecord> &appRecord);
1502 
1503     int32_t NotifyProcessDependedOnWeb();
1504 
1505     void KillProcessDependedOnWeb();
1506 
1507     void RestartResidentProcessDependedOnWeb();
1508 
1509     void BlockProcessCacheByPids(const std::vector<int32_t>& pids);
1510 
1511     bool CleanAbilityByUserRequest(const sptr<IRemoteObject> &token);
1512 
1513     /**
1514      * whether killed for upgrade web.
1515      *
1516      * @param bundleName the bundle name is killed for upgrade web.
1517      * @return Returns true is killed for upgrade web, others return false.
1518      */
1519     bool IsKilledForUpgradeWeb(const std::string &bundleName) const;
1520 
1521     bool IsProcessContainsOnlyUIAbility(const pid_t pid);
1522 
1523     /**
1524      * Whether a process is attached, refer to AttachApplication
1525      */
1526     bool IsProcessAttached(sptr<IRemoteObject> token) const;
1527 
1528     /**
1529      * Is a process of a ability killing
1530      * @param indicates the ability
1531      */
1532     bool IsCallerKilling(const std::string& callerKey) const;
1533 
1534     /**
1535      * @brief Notify abilityms app process pre cache
1536      * @param pid process pid.
1537      * @param userId userId Designation User ID.
1538      */
1539     void NotifyAppPreCache(int32_t pid, int32_t userId);
1540 
1541     /**
1542      * Get pids of processes which belong to specific bundle name and support process cache feature.
1543      *
1544      * @param bundleName bundle name.
1545      * @param pidList pid list of processes that support process cache.
1546      * @return Returns ERR_OK on success, others on failure.
1547      */
1548     virtual int32_t GetSupportedProcessCachePids(const std::string &bundleName, std::vector<int32_t> &pidList);
1549 
1550     /**
1551      * Register KIA interceptor.
1552      * @param interceptor KIA interceptor.
1553      * @return Returns ERR_OK on success, others on failure.
1554      */
1555     virtual int32_t RegisterKiaInterceptor(const sptr<IKiaInterceptor> &interceptor);
1556 
1557     /**
1558      * Check if the given pid is a KIA process.
1559      * @param pid process id.
1560      * @return Returns true if it is a KIA process, false otherwise.
1561      */
1562     virtual int32_t CheckIsKiaProcess(pid_t pid, bool &isKia);
1563 
1564     void UpdateInstanceKeyBySpecifiedId(int32_t specifiedId, std::string &instanceKey);
1565 
1566     bool IsSpecifiedModuleLoaded(const AAFwk::Want &want, const AbilityInfo &abilityInfo, bool &isDebug);
1567 
GetTaskHandler()1568     std::shared_ptr<AAFwk::TaskHandlerWrap> GetTaskHandler() const
1569     {
1570         return taskHandler_;
1571     }
1572 
1573     int32_t LaunchAbility(sptr<IRemoteObject> token);
1574 
1575     virtual int32_t QueryRunningSharedBundles(pid_t pid, std::map<std::string, uint32_t> &sharedBundles);
1576 
1577     /**
1578      * Verify whether the caller has the permission to kill processes of a bundleName.
1579      */
1580     int VerifyKillProcessPermission(const std::string &bundleName) const;
1581 
1582 private:
1583     int32_t ForceKillApplicationInner(const std::string &bundleName, const int userId = -1,
1584         const int appIndex = 0);
1585 
1586     std::string FaultTypeToString(FaultDataType type);
1587 
1588     FaultData ConvertDataTypes(const AppFaultDataBySA &faultData);
1589 
1590     /**
1591      * Start a empty resident process
1592      */
1593     void StartEmptyResidentProcess(const BundleInfo &info, const std::string &processName, int restartCount,
1594         bool isEmptyKeepAliveApp);
1595 
1596     /**
1597      * If one app needs keepalive and dies, restart the app again
1598      */
1599     void RestartResidentProcess(std::shared_ptr<AppRunningRecord> appRecord);
1600 
1601     /**
1602      * If one app needs keepalive and dies, restart the app again
1603      */
1604     void RestartKeepAliveProcess(std::shared_ptr<AppRunningRecord> appRecord);
1605 
1606     bool CheckLoadAbilityConditions(const sptr<IRemoteObject> &token,
1607         const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo);
1608 
1609     /**
1610      * query bundle info for the given bundleName
1611      */
1612     bool GetBundleInfo(const std::string &bundleName, BundleInfo &bundleInfo);
1613 
1614     bool GenerateRenderUid(int32_t &renderUid);
1615 
1616     /**
1617      * Build a process's name for service extension
1618      */
1619     void MakeServiceExtProcessName(const std::shared_ptr<AbilityInfo> &abilityInfo,
1620         const std::shared_ptr<ApplicationInfo> &appInfo, std::string &processName) const;
1621 
1622     /**
1623      * Build a process's name for the info given
1624      */
1625     void MakeProcessName(const std::shared_ptr<AbilityInfo> &abilityInfo,
1626         const std::shared_ptr<ApplicationInfo> &appInfo, const HapModuleInfo &hapModuleInfo, int32_t appIndex,
1627         const std::string &specifiedProcessFlag, std::string &processName, bool isCallerSetProcess) const;
1628 
1629     /**
1630      * Build a process's name based on the info given
1631      */
1632     void MakeProcessName(const std::shared_ptr<ApplicationInfo> &appInfo, const HapModuleInfo &hapModuleInfo,
1633         std::string &processName) const;
1634 
1635     bool CheckIsolationMode(const HapModuleInfo &hapModuleInfo) const;
1636 
1637     bool IsMainProcess(const std::shared_ptr<ApplicationInfo> &appInfo, const std::string &processName) const;
1638 
1639     /**
1640      * StartAbility, load the ability that needed to be started(Start on the basis of the original process).
1641      *  Start on a new boot process
1642      * @param token, the unique identification to start the ability.
1643      * @param preToken, the unique identification to call the ability.
1644      * @param abilityInfo, the ability information.
1645      * @param appInfo, the app information.
1646      *
1647      * @return
1648      */
1649     void StartAbility(sptr<IRemoteObject> token, sptr<IRemoteObject> preToken,
1650         std::shared_ptr<AbilityInfo> abilityInfo, std::shared_ptr<AppRunningRecord> appRecord,
1651         const HapModuleInfo &hapModuleInfo, std::shared_ptr<AAFwk::Want> want, int32_t abilityRecordId);
1652 
1653     int32_t StartPerfProcess(const std::shared_ptr<AppRunningRecord> &appRecord, const std::string& perfCmd,
1654         const std::string& debugCmd, bool isSandboxApp);
1655 
1656     void StartProcessVerifyPermission(const BundleInfo &bundleInfo, bool &hasAccessBundleDirReq,
1657         uint8_t &setAllowInternet, uint8_t &allowInternet, std::vector<int32_t> &gids);
1658 
1659     void AddMountPermission(uint32_t accessTokenId, std::set<std::string> &permissions);
1660 
1661     /**
1662      * StartProcess, load the ability that needed to be started(Start on a new boot process).
1663      *
1664      * @param appName, the app name.
1665      * @param processName, the process name.
1666      * @param appRecord, the app information.
1667      * @param uid, the process uid.
1668      * @param bundleName, the app bundleName.
1669      *
1670      * @return
1671      */
1672     int32_t StartProcess(const std::string &appName, const std::string &processName, uint32_t startFlags,
1673                       std::shared_ptr<AppRunningRecord> appRecord, const int uid, const BundleInfo &bundleInfo,
1674                       const std::string &bundleName, const int32_t bundleIndex, bool appExistFlag = true,
1675                       bool isPreload = false,  AppExecFwk::PreloadMode preloadMode = AppExecFwk::PreloadMode::PRE_MAKE,
1676                       const std::string &moduleName = "", const std::string &abilityName = "",
1677                       sptr<IRemoteObject> token = nullptr, std::shared_ptr<AAFwk::Want> want = nullptr,
1678                       ExtensionAbilityType extensionAbilityType = ExtensionAbilityType::UNSPECIFIED);
1679 
1680     /**
1681      * KillApplicationByUserId, kill the application by user ID.
1682      *
1683      * @param bundleName, bundle name in Application record.
1684      * @param appCloneIndex the app clone id.
1685      * @param userId, user ID.
1686      * @param  reason, caller function name.
1687      *
1688      * @return ERR_OK, return back success, others fail.
1689      */
1690     int32_t KillApplicationByUserIdLocked(const std::string &bundleName, int32_t appCloneIndex, int32_t userId,
1691         const KillProcessConfig &config = {});
1692 
1693     /**
1694      * WaitForRemoteProcessExit, Wait for the process to exit normally.
1695      *
1696      * @param pids, process number collection to exit.
1697      * @param startTime, execution process security exit start time.
1698      *
1699      * @return true, return back success,others fail.
1700      */
1701     bool WaitForRemoteProcessExit(std::list<pid_t> &pids, const int64_t startTime);
1702 
1703     bool WaitForRemoteProcessExit(std::list<SimpleProcessInfo> &processInfos, const int64_t startTime);
1704 
1705      /**
1706      * WaitProcessesExitAndKill, Wait for the process to exit normally, and kill it if time out.
1707      *
1708      * @param pids, process number collection to exit.
1709      * @param startTime, execution process security exit start time.
1710      * @param reason caller function name
1711      *
1712      * @return ERR_OK, return back success, others fail.
1713      */
1714     int32_t WaitProcessesExitAndKill(std::list<pid_t> &pids, const int64_t startTime, const std::string& reason);
1715 
1716     int32_t WaitProcessesExitAndKill(std::list<SimpleProcessInfo> &processInfos, const int64_t startTime,
1717         const std::string& reason, int32_t userId, sptr<AAFwk::IUserCallback> callback);
1718 
1719     bool DoAllProcessExitCallback(std::list<SimpleProcessInfo> &processInfos, int32_t userId,
1720         sptr<AAFwk::IUserCallback> callback, int64_t startTime);
1721 
1722     /**
1723      * SystemTimeMillisecond, Get system time.
1724      *
1725      * @return the system time.
1726      */
1727     int64_t SystemTimeMillisecond();
1728 
1729     // Test add the bundle manager instance.
1730     void SetBundleManagerHelper(const std::shared_ptr<BundleMgrHelper> &bundleMgrHelper);
1731 
1732     void HandleTerminateApplicationTimeOut(std::shared_ptr<AppRunningRecord> appRecord);
1733 
1734     void HandleAddAbilityStageTimeOut(std::shared_ptr<AppRunningRecord> appRecord);
1735 
1736     void ClipStringContent(const std::regex &re, const std::string &source, std::string &afterCutStr);
1737 
1738     bool GetBundleAndHapInfo(const AbilityInfo &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo,
1739         BundleInfo &bundleInfo, HapModuleInfo &hapModuleInfo, int32_t appIndex = 0) const;
1740     AppProcessData WrapAppProcessData(const std::shared_ptr<AppRunningRecord> &appRecord,
1741         const ApplicationState state, bool isFromWindowFocusChanged = false);
1742 
1743     int UserTestAbnormalFinish(const sptr<IRemoteObject> &observer, const std::string &msg);
1744     int GetHapModuleInfoForTestRunner(const AAFwk::Want &want, const sptr<IRemoteObject> &observer,
1745         const BundleInfo &bundleInfo, HapModuleInfo &hapModuleInfo);
1746     int StartEmptyProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer, const BundleInfo &info,
1747         const std::string &processName, const int userId);
1748 
1749     void HandleStartSpecifiedAbilityTimeOut(std::shared_ptr<AppRunningRecord> appRecord);
1750 
1751     void HandleStartSpecifiedProcessTimeout(std::shared_ptr<AppRunningRecord> appRecord);
1752 
1753     void InitGlobalConfiguration();
1754 
1755     /**
1756      * @brief Another way of GetRunningProcess, only one item will be filled into the info
1757      */
1758     void GetRunningProcesses(const std::shared_ptr<AppRunningRecord> &appRecord, std::vector<RunningProcessInfo> &info);
1759     /**
1760      * @brief Retrieve a process's running info
1761      */
1762     void GetRunningProcess(const std::shared_ptr<AppRunningRecord> &appRecord, RunningProcessInfo &info);
1763 
1764     /**
1765      * @brief query a process's render process info
1766      * @param appRecord the main process
1767      * @param info to retrieve render process info
1768      */
1769     void GetRenderProcesses(const std::shared_ptr<AppRunningRecord> &appRecord, std::vector<RenderProcessInfo> &info);
1770 
1771 #ifdef SUPPORT_CHILD_PROCESS
1772     void GetChildrenProcesses(const std::shared_ptr<AppRunningRecord> &appRecord, std::vector<ChildProcessInfo> &info);
1773 #endif // SUPPORT_CHILD_PROCESS
1774 
1775 #ifdef ABILITY_PLATFORM_CHECK_PERMISSION
1776     int CheckStablePermission(const BundleInfo &bundleInfo);
1777     bool CheckDeviceStatus(Security::AccessToken::PermissionStateFull &status,
1778         std::string deviceid);
1779 #endif //ABILITY_PLATFORM_CHECK_PERMISSION
1780 
1781     int StartRenderProcessImpl(const std::shared_ptr<RenderRecord> &renderRecord,
1782         const std::shared_ptr<AppRunningRecord> appRecord, pid_t &renderPid, bool isGPU = false);
1783 
1784     void SetRenderStartMsg(AppSpawnStartMsg &startMsg, std::shared_ptr<RenderRecord> renderRecord,
1785         const int32_t renderUid, const bool isGPU);
1786 
1787     void OnRenderRemoteDied(const wptr<IRemoteObject> &remote);
1788 
1789     void AddWatchParameter();
1790 
1791     bool VerifyAPL() const;
1792 
1793     /**
1794      * Register a focus listener to window manager
1795      */
1796     void RegisterFocusListener();
1797 
1798     static void PointerDeviceEventCallback(const char *key, const char *value, void *context);
1799 
1800     int32_t VerifyKillProcessPermissionCommon() const;
1801 
1802     void ApplicationTerminatedSendProcessEvent(const std::shared_ptr<AppRunningRecord> &appRecord);
1803     void ClearAppRunningDataForKeepAlive(const std::shared_ptr<AppRunningRecord> &appRecord);
1804     void ClearResidentProcessAppRunningData(const std::shared_ptr<AppRunningRecord> &appRecord);
1805     void ClearNonResidentKeepAliveAppRunningData(const std::shared_ptr<AppRunningRecord> &appRecord);
1806 
1807 #ifdef SUPPORT_CHILD_PROCESS
1808     int32_t StartChildProcessPreCheck(pid_t callingPid, int32_t childProcessType);
1809 
1810     bool CheckCustomProcessName(const std::string &customProcessName);
1811 
1812     bool AllowNativeChildProcess(int32_t childProcessType, const std::string appIdentifier);
1813 
1814     bool AllowChildProcessInMultiProcessFeatureApp(std::shared_ptr<AppRunningRecord> appRecord);
1815 
1816     int32_t StartChildProcessImpl(const std::shared_ptr<ChildProcessRecord> childProcessRecord,
1817         const std::shared_ptr<AppRunningRecord> appRecord, pid_t &childPid, const ChildProcessArgs &args,
1818         const ChildProcessOptions &options);
1819 
1820     int32_t GetChildProcessInfo(const std::shared_ptr<ChildProcessRecord> childProcessRecord,
1821         const std::shared_ptr<AppRunningRecord> appRecord, ChildProcessInfo &info,
1822         bool isCallFromGetChildrenProcesses = false);
1823 
1824     void OnChildProcessRemoteDied(const wptr<IRemoteObject> &remote);
1825 
1826     /**
1827      * kill all child processed of a main process
1828      * @param appRecord indicates the main process
1829      */
1830     void KillChildProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
1831 
1832     void KillAttachedChildProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
1833 
1834     void PresetMaxChildProcess(std::shared_ptr<AppRunningRecord> appRecord,
1835         int32_t &maxChildProcess);
1836 #endif // SUPPORT_CHILD_PROCESS
1837 
1838     void AfterLoadAbility(std::shared_ptr<AppRunningRecord> appRecord, std::shared_ptr<AbilityInfo> abilityInfo,
1839         std::shared_ptr<AbilityRuntime::LoadParam> loadParam);
1840 
1841     void RemoveRenderRecordNoAttach(const std::shared_ptr<AppRunningRecord> &hostRecord, int32_t renderPid);
1842 
1843 private:
1844     /**
1845      * ClearUpApplicationData, clear the application data.
1846      *
1847      * @param bundleName, bundle name in Application record.
1848      * @param uid, app uid in Application record.
1849      * @param pid, app pid in Application record.
1850      * @param appCloneIndex the app clone id.
1851      * @param userId, userId.
1852      * @param isBySelf, clear data by application self.
1853      * @param reason, caller function.
1854      *
1855      * @return Returns ERR_OK on success, others on failure.
1856      */
1857     int32_t ClearUpApplicationDataByUserId(const std::string &bundleName,
1858         int32_t callerUid, pid_t callerPid, int32_t appCloneIndex, int32_t userId, bool isBySelf = false,
1859         const std::string& reason = "ClearUpApplicationDataByUserId");
1860 
1861     bool CheckGetRunningInfoPermission() const;
1862 
1863     /**
1864      * kill all processes of a bundleName
1865      * @param bundleName bundleName of which to be killed
1866      * @param clearPageStack should schedule clearPage lifecycle
1867      * @param reason caller function name
1868      */
1869     int32_t KillApplicationByBundleName(const std::string &bundleName, int32_t appIndex, bool clearPageStack = false,
1870         const std::string& reason = "KillApplicationByBundleName");
1871 
1872     bool SendProcessStartEvent(const std::shared_ptr<AppRunningRecord> &appRecord, bool isPreload,
1873         AppExecFwk::PreloadMode preloadMode);
1874 
1875     bool SendProcessStartFailedEvent(std::shared_ptr<AppRunningRecord> appRecord, ProcessStartFailedReason reason,
1876         int32_t subReason);
1877 
1878     void SendPreloadAppStartupTypeEvent(const std::shared_ptr<AppRunningRecord> &appRecord,
1879         const std::shared_ptr<AbilityInfo> &abilityInfo);
1880 
1881     void SendAppStartupTypeEvent(const std::shared_ptr<AppRunningRecord> &appRecord,
1882         const std::shared_ptr<AbilityInfo> &abilityInfo, const AppStartType startType, const AppStartReason reason);
1883 
1884     bool SendCreateAtomicServiceProcessEvent(const std::shared_ptr<AppRunningRecord> &appRecord,
1885         const BundleType &bundleType, const std::string &moduleName = "", const std::string &abilityName = "");
1886 
1887     void SendProcessExitEvent(std::shared_ptr<AppRunningRecord> appRecord);
1888 
1889     void SendProcessExitEventTask(pid_t pid, const std::string &processName, int32_t extensionType,
1890         int32_t exitReason, time_t exitTime, int32_t count);
1891 
1892     void SetRunningSharedBundleList(const std::string &bundleName,
1893         const std::vector<BaseSharedBundleInfo> baseSharedBundleInfoList);
1894 
1895     void RemoveRunningSharedBundleList(const std::string &bundleName);
1896 
1897     /**
1898      * kill one process's render process
1899      * @param appRecord the main process
1900      */
1901     void KillRenderProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
1902 
1903     void SetOverlayInfo(const std::string& bundleName, const int32_t userId, AppSpawnStartMsg& startMsg);
1904     void SetAppEnvInfo(const BundleInfo &bundleInfo, AppSpawnStartMsg& startMsg);
1905 
1906     void TimeoutNotifyApp(int32_t pid, int32_t uid, const std::string& bundleName, const std::string& processName,
1907         const FaultData &faultData);
1908 
1909     void AppRecoveryNotifyApp(int32_t pid, const std::string& bundleName,
1910         FaultDataType faultType, const std::string& markers);
1911 
1912     void ProcessAppDebug(const std::shared_ptr<AppRunningRecord> &appRecord, const bool &isDebugStart);
1913     AppDebugInfo MakeAppDebugInfo(const std::shared_ptr<AppRunningRecord> &appRecord, const bool &isDebugStart);
1914     int32_t NotifyAbilitiesDebugChange(const std::string &bundleName, const bool &isAppDebug);
1915     int32_t NotifyAbilitiesAssertDebugChange(const std::shared_ptr<AppRunningRecord> &appRecord, bool isAssertDebug);
1916 
1917     void SetProcessJITState(const std::shared_ptr<AppRunningRecord> appRecord);
1918 
1919     bool JudgeSelfCalledByToken(const sptr<IRemoteObject> &token, const PageStateData &pageStateData);
1920 
1921     void ParseServiceExtMultiProcessWhiteList();
1922     void ClearData(std::shared_ptr<AppRunningRecord> appRecord);
1923 
1924     /**
1925      * Notify the app running status.
1926      *
1927      * @param bundle Bundle name in application record.
1928      * @param uid Uid of bundle.
1929      * @param runningStatus The app running status.
1930      *
1931      * @return
1932      */
1933     void NotifyAppRunningStatusEvent(
1934         const std::string &bundle, int32_t uid, AbilityRuntime::RunningStatus runningStatus);
1935 
1936 #ifdef SUPPORT_CHILD_PROCESS
1937     void GetRunningCloneAppInfo(const std::shared_ptr<AppRunningRecord> &appRecord,
1938         RunningMultiAppInfo &info);
1939 #endif // SUPPORT_CHILD_PROCESS
1940 
1941     void GetRunningMultiInstanceKeys(const std::shared_ptr<AppRunningRecord> &appRecord,
1942         std::vector<std::string> &instanceKeys);
1943     /**
1944      * To Prevent process being killed when ability is starting in an existing process,
1945      * we need notify memmgr to increase process priority.
1946      *
1947      * @param appRecord Current app running record.
1948      *
1949      * @return Whether improve priority succeed.
1950      */
1951     bool NotifyMemMgrPriorityChanged(const std::shared_ptr<AppRunningRecord> appRecord);
1952 
1953     int32_t PreloadApplication(const std::string &bundleName, int32_t userId, int32_t appIndex,
1954         AppExecFwk::PreloadMode preloadMode, AppExecFwk::PreloadPhase preloadPhase);
1955     void HandlePreloadApplication(const PreloadRequest &request);
1956     void SetPreloadDebugApp(std::shared_ptr<AAFwk::Want> want, std::shared_ptr<ApplicationInfo> appInfo);
1957     bool CheckAppRecordExistByPreloadRequest(const PreloadRequest &request, std::string &processName,
1958         std::string &specifiedProcessFlag);
1959 
1960     void reportpreLoadTask(const std::shared_ptr<AppRunningRecord> appRecord);
1961 
1962     std::string GetSpecifiedProcessFlag(std::shared_ptr<AbilityInfo> abilityInfo, std::shared_ptr<AAFwk::Want> want);
1963     std::string GetSpecifiedProcessFlag(const AbilityInfo &abilityInfo, const AAFwk::Want &want);
1964 
1965     void LoadAbilityNoAppRecord(const std::shared_ptr<AppRunningRecord> appRecord,
1966         bool isShellCall, std::shared_ptr<ApplicationInfo> appInfo,
1967         std::shared_ptr<AbilityInfo> abilityInfo, const std::string &processName,
1968         const std::string &specifiedProcessFlag, const BundleInfo &bundleInfo,
1969         const HapModuleInfo &hapModuleInfo, std::shared_ptr<AAFwk::Want> want,
1970         bool appExistFlag, bool isPreload, AppExecFwk::PreloadMode preloadMode,
1971         sptr<IRemoteObject> token = nullptr, const std::string &customProcessFlag = "",
1972         bool isStartupHide = false);
1973 
1974     int32_t CreatNewStartMsg(const Want &want, const AbilityInfo &abilityInfo,
1975         const std::shared_ptr<ApplicationInfo> &appInfo, const std::string &processName,
1976         AppSpawnStartMsg &startMsg);
1977 
1978     /**
1979      * Build message body for app spawn
1980      */
1981     int32_t CreateStartMsg(const CreateStartMsgParam &param, AppSpawnStartMsg &startMsg);
1982 
1983     void SetStartMsgStrictMode(AppSpawnStartMsg &startMsg, const CreateStartMsgParam &param);
1984 
1985     void SetAppRunningRecordStrictMode(std::shared_ptr<AppRunningRecord> appRecord,
1986         std::shared_ptr<AbilityRuntime::LoadParam> loadParam);
1987 
1988     void QueryExtensionSandBox(const std::string &moduleName, const std::string &abilityName,
1989         const BundleInfo &bundleInfo, AppSpawnStartMsg &startMsg, DataGroupInfoList& dataGroupInfoList,
1990         std::shared_ptr<AAFwk::Want> want);
1991 
1992     int32_t StartPerfProcessByStartMsg(AppSpawnStartMsg &startMsg, const std::string& perfCmd,
1993         const std::string& debugCmd, bool isSandboxApp);
1994 
1995     void SetAtomicServiceInfo(BundleType bundleType, AppSpawnStartMsg &startMsg);
1996 
1997     void SetAppInfo(const BundleInfo &bundleInfo, AppSpawnStartMsg &startMsg);
1998 
1999     void SetStartMsgCustomSandboxFlag(AppSpawnStartMsg &startMsg, uint32_t accessTokenId);
2000 
2001     /**
2002      * Query ability for one want param
2003      */
2004     bool CreateAbilityInfo(const AAFwk::Want &want, AbilityInfo &abilityInfo);
2005 
2006     /**
2007      * Build event info for one app record
2008      */
2009     AAFwk::EventInfo BuildEventInfo(std::shared_ptr<AppRunningRecord> appRecord) const;
2010 
2011     /**
2012      * @brief Call the callbacks to notify one process should go dying due fatal error.
2013      * @param appRecord indicates the process is going to die.
2014      */
2015     void NotifyAppAttachFailed(std::shared_ptr<AppRunningRecord> appRecord);
2016 
2017     void NotifyLoadAbilityFailed(sptr<IRemoteObject> token);
2018 
2019     void NotifyStartProcessFailed(sptr<IRemoteObject> token);
2020 
2021     std::vector<BackgroundAppInfo> GetBackgroundAppInfo(const std::vector<BackgroundAppInfo>& allowAppList);
2022 private:
2023     /**
2024      * Notify application status.
2025      *
2026      * @param bundleName Indicates the name of the bundle.
2027      * @param bundleName Indicates the name of the bundle.
2028      * @param eventData Indicates the event defined by CommonEventSupport
2029      *
2030      * @return
2031      */
2032     void NotifyAppStatusByCallerUid(const std::string &bundleName, const int32_t tokenId, const int32_t userId,
2033         const int32_t callerUid, const int32_t targetUid, const std::string &eventData);
2034     void UpdateAllProviderConfig(const std::shared_ptr<AppRunningRecord> &appRecord);
2035     void SendHiSysEvent(int32_t innerEventId, std::shared_ptr<AppRunningRecord> appRecord);
2036     int FinishUserTestLocked(
2037         const std::string &msg, const int64_t &resultCode, const std::shared_ptr<AppRunningRecord> &appRecord);
2038     int32_t GetCurrentAccountId() const;
2039     void SendReStartProcessEvent(AAFwk::EventInfo &eventInfo, int32_t appUid);
2040     void SendAppLaunchEvent(const std::shared_ptr<AppRunningRecord> &appRecord);
2041     void InitAppWaitingDebugList();
2042     bool IsWaitingDebugAppInner(const std::string &bundleName);
2043     void HandleConfigurationChange(const Configuration &config, const int32_t userId = -1);
2044     bool CheckIsThreadInFoundation(pid_t pid);
2045     bool CheckAppFault(const std::shared_ptr<AppRunningRecord> &appRecord, const FaultData &faultData);
2046     int32_t KillFaultApp(int32_t pid, const std::string &bundleName, const FaultData &faultData,
2047         bool isNeedExit = false);
2048     void AddUIExtensionLauncherItem(std::shared_ptr<AAFwk::Want> want, std::shared_ptr<AppRunningRecord> appRecord,
2049         sptr<IRemoteObject> token);
2050     void NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos);
2051     void NotifyStartKeepAliveProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos);
2052     void RemoveUIExtensionLauncherItem(std::shared_ptr<AppRunningRecord> appRecord, sptr<IRemoteObject> token);
2053     bool IsSceneBoardCall();
2054     void CheckCleanAbilityByUserRequest(const std::shared_ptr<AppRunningRecord> &appRecord,
2055         const std::shared_ptr<AbilityRunningRecord> &abilityRecord, const AbilityState state);
2056     int32_t GetPidsByAccessTokenId(const uint32_t accessTokenId, std::vector<pid_t> &pids);
2057     bool IsIsolateExtensionSandBox(const std::shared_ptr<AbilityInfo> &abilityInfo,
2058         const HapModuleInfo &hapModuleInfo) const;
2059     int32_t DealWithUserConfiguration(const Configuration &config, const int32_t userId, int32_t &notifyUserId);
2060     bool CheckIsDebugApp(const std::string &bundleName);
2061     int32_t MakeKiaProcess(std::shared_ptr<AAFwk::Want> want, bool &isKia, std::string &watermarkBusinessName,
2062         bool &isWatermarkEnabled, bool &isFileUri, std::string &processName);
2063     int32_t ProcessKia(bool isKia, std::shared_ptr<AppRunningRecord> appRecord,
2064         const std::string& watermarkBusinessName, bool isWatermarkEnabled);
2065     bool CheckAppRecordAndPriorityObject(const std::shared_ptr<AppRunningRecord> &appRecord);
2066 #ifdef SUPPORT_CHILD_PROCESS
2067     void GetAppCloneInfo(const std::shared_ptr<AppRunningRecord> &appRecord,
2068         RunningMultiAppInfo &info);
2069     void GetMultiInstanceInfo(const std::shared_ptr<AppRunningRecord> &appRecord,
2070         RunningMultiAppInfo &info);
2071 #endif //SUPPORT_CHILD_PROCESS
2072     void SubscribeScreenOffEvent();
2073     void UnSubscribeScreenOffEvent();
2074     int32_t RestartExitKeepAliveProcess(const std::vector<ExitResidentProcessInfo> &exitProcessInfos);
2075     bool IsNeedRestartKeepAliveProcess(const std::string &bundleName, int32_t uid);
2076     int32_t GetAllRunningInstanceKeysByBundleNameInner(const std::string &bundleName,
2077         std::vector<std::string> &instanceKeys, int32_t userId);
2078     int32_t KillProcessByPidInner(const pid_t pid, const std::string& reason,
2079         const std::string& killReason, std::shared_ptr<AppRunningRecord> appRecord, bool isKillPrecedeStart);
2080     void SetKilledEventInfo(std::shared_ptr<AppRunningRecord> appRecord, AAFwk::EventInfo &eventInfo);
2081     void AddToKillProcessMap(const std::string &processName);
2082     bool IsAllowedNWebPreload(const std::string &processName);
2083     void ParseInfoToAppfreeze(const FaultData &faultData, int32_t pid, int32_t uid, const std::string &bundleName,
2084         const std::string &processName, const bool isOccurException = false);
2085     int GetExceptionTimerId(const FaultData &faultData, const std::string &bundleName,
2086         const std::shared_ptr<AppRunningRecord> &appRecord, const int32_t pid, const int32_t callerUid);
2087     int32_t SubmitDfxFaultTask(const FaultData &faultData, const std::string &bundleName,
2088         const std::shared_ptr<AppRunningRecord> &appRecord, const int32_t pid);
2089     void SendAbilityEvent(const std::shared_ptr<AbilityRunningRecord> &abilityRecord, const AbilityState &state);
2090     void AddAbilityStageForSpecified(std::shared_ptr<AppRunningRecord> appRecord);
2091     void GetKernelPermissions(uint32_t accessTokenId, JITPermissionsMap &permissionsMap);
2092     void SendAppSpawnUninstallDebugHapMsg(int32_t userId);
2093     std::shared_ptr<AppRunningRecord> CreateAppRunningRecord(std::shared_ptr<ApplicationInfo> appInfo,
2094         const std::string &processName, const BundleInfo &bundleInfo);
2095     void AddUIExtensionBindItem(
2096         std::shared_ptr<AAFwk::Want> want, std::shared_ptr<AppRunningRecord> appRecord, sptr<IRemoteObject> token);
2097     void RemoveUIExtensionBindItem(std::shared_ptr<AppRunningRecord> appRecord, sptr<IRemoteObject> token);
2098     void BindUIExtensionProcess(
2099         const std::shared_ptr<AppRunningRecord> &appRecord, const UIExtensionProcessBindInfo &bindInfo);
2100     void UnBindUIExtensionProcess(
2101         const std::shared_ptr<AppRunningRecord> &appRecord, const UIExtensionProcessBindInfo &bindInfo);
2102     bool WrapBindInfo(std::shared_ptr<AAFwk::Want> &want, std::shared_ptr<AppRunningRecord> &appRecord,
2103         UIExtensionProcessBindInfo &bindInfo);
2104     void ReportEventToRSS(const AppExecFwk::AbilityInfo &abilityInfo,
2105         const std::shared_ptr<AppRunningRecord> &appRecord);
2106     void SetKeepAliveEnableStateAndNotify(const std::shared_ptr<AppRunningRecord>& appRecord, bool enable);
2107     void SetKeepAliveAppServiceAndNotify(const std::shared_ptr<AppRunningRecord>& appRecord, bool enable);
2108     bool GetKeepAliveState(const std::shared_ptr<AppRunningRecord> &appRecord);
2109     void OnProcessDied(std::shared_ptr<AppRunningRecord> appRecord);
2110     bool IsBlockedByDisposeRules(const std::string &bundleName, int32_t userId,
2111         int32_t appIndex);
2112     int32_t PreCheckStartProcess(const std::string &bundleName, int32_t uid,
2113         std::shared_ptr<AAFwk::Want> &want);
2114     void InsertUninstallOrUpgradeUidSet(int32_t uid);
2115     void RemoveUninstallOrUpgradeUidSet(int32_t uid);
2116     bool IsUninstallingOrUpgrading(int32_t uid);
2117     bool isInitAppWaitingDebugListExecuted_ = false;
2118     std::atomic<bool> sceneBoardAttachFlag_ = true;
2119     std::atomic<int32_t> willKillPidsNum_ = 0;
2120     int32_t currentUserId_ = 0;
2121     int32_t lastRenderUid_ = Constants::START_UID_FOR_RENDER_PROCESS;
2122     const std::string TASK_ON_CALLBACK_DIED = "OnCallbackDiedTask";
2123     std::vector<AppStateCallbackWithUserId> appStateCallbacks_;
2124     std::shared_ptr<RemoteClientManager> remoteClientManager_;
2125     std::shared_ptr<AppRunningManager> appRunningManager_;
2126     std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
2127     std::shared_ptr<AAFwk::TaskHandlerWrap> rssTaskHandler_;
2128     std::shared_ptr<AMSEventHandler> eventHandler_;
2129     ffrt::mutex userTestLock_;
2130     ffrt::mutex appStateCallbacksLock_;
2131     ffrt::mutex renderUidSetLock_;
2132     ffrt::mutex exceptionLock_;
2133     ffrt::mutex browserHostLock_;
2134     ffrt::mutex restartResidentTaskListMutex_;
2135     sptr<IStartSpecifiedAbilityResponse> startSpecifiedAbilityResponse_;
2136     ffrt::mutex configurationObserverLock_;
2137     std::vector<ConfigurationObserverWithUserId> configurationObservers_;
2138 
2139 #ifdef SUPPORT_SCREEN
2140     sptr<WindowFocusChangedListener> focusListener_;
2141     sptr<WindowVisibilityChangedListener> windowVisibilityChangedListener_;
2142     sptr<WindowPidVisibilityChangedListener> windowPidVisibilityChangedListener_;
2143 #endif //SUPPORT_SCREEN
2144     std::vector<std::shared_ptr<AppRunningRecord>> restartResidentTaskList_;
2145     ffrt::mutex runningSharedBundleListMutex_;
2146     std::map<std::string, std::vector<BaseSharedBundleInfo>> runningSharedBundleList_;
2147     std::map<std::string, bool> waitingDebugBundleList_;
2148     ffrt::mutex waitingDebugLock_;
2149     std::unordered_set<int32_t> renderUidSet_;
2150     std::string supportIsolationMode_ {"false"};
2151     std::string supportServiceExtMultiProcess_ {"false"};
2152     sptr<IAbilityDebugResponse> abilityDebugResponse_;
2153     std::shared_ptr<AppDebugManager> appDebugManager_;
2154     ffrt::mutex killedProcessMapLock_;
2155     mutable std::map<int64_t, std::string> killedProcessMap_;
2156 #ifdef SUPPORT_CHILD_PROCESS
2157     ffrt::mutex startChildProcessLock_;
2158 #endif //SUPPORT_CHILD_PROCESS
2159     std::vector<std::string> serviceExtensionWhiteList_;
2160     std::shared_ptr<AbilityRuntime::AppRunningStatusModule> appRunningStatusModule_;
2161     std::shared_ptr<AdvancedSecurityModeManager> securityModeManager_;
2162     std::shared_ptr<AAFwk::TaskHandlerWrap> dfxTaskHandler_;
2163     std::shared_ptr<AppPreloader> appPreloader_;
2164 
2165     std::mutex loadTaskListMutex_;
2166     std::vector<LoadAbilityTaskFunc> loadAbilityTaskFuncList_;
2167     sptr<IKiaInterceptor> kiaInterceptor_;
2168     std::shared_ptr<MultiUserConfigurationMgr> multiUserConfigurationMgr_;
2169     std::unordered_set<std::string> nwebPreloadSet_ {};
2170     std::shared_ptr<AppExecFwk::AppMgrEventSubscriber> screenOffSubscriber_;
2171 
2172     std::mutex screenOffSubscriberMutex_;
2173     std::mutex childProcessRecordMapMutex_;
2174 
2175     std::mutex uiExtensionBindReleationsLock_;
2176     std::map<int32_t, std::unordered_map<pid_t, int32_t>> uiExtensionBindReleations_;
2177 
2178     std::shared_mutex startProcessLock_;
2179     ffrt::mutex uninstallOrUpgradeUidSetLock_;
2180     std::unordered_set<int32_t> uninstallOrUpgradeUidSet_ {};
2181 };
2182 }  // namespace AppExecFwk
2183 }  // namespace OHOS
2184 #endif  // OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_INNER_H
2185