• 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_RUNNING_RECORD_H
17 #define OHOS_ABILITY_RUNTIME_APP_RUNNING_RECORD_H
18 
19 #include <list>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <unordered_set>
24 
25 #include "cpp/mutex.h"
26 #include "iremote_object.h"
27 #include "irender_scheduler.h"
28 #include "ability_running_record.h"
29 #include "render_record.h"
30 #include "ability_state_data.h"
31 #include "application_info.h"
32 #include "task_handler_wrap.h"
33 #include "app_mgr_service_event_handler.h"
34 #include "app_death_recipient.h"
35 #include "app_launch_data.h"
36 #include "app_mgr_constants.h"
37 #include "app_scheduler_proxy.h"
38 #include "app_record_id.h"
39 #ifdef SUPPORT_CHILD_PROCESS
40 #include "child_process_record.h"
41 #endif // SUPPORT_CHILD_PROCESS
42 #include "fault_data.h"
43 #include "fd_guard.h"
44 #include "profile.h"
45 #include "priority_object.h"
46 #include "app_lifecycle_deal.h"
47 #include "module_running_record.h"
48 #include "app_spawn_client.h"
49 #include "app_malloc_info.h"
50 #include "app_jsheap_mem_info.h"
51 #include "app_cjheap_mem_info.h"
52 #include "simple_process_info.h"
53 
54 namespace OHOS {
55 namespace Rosen {
56 class WindowVisibilityInfo;
57 }
58 namespace AppExecFwk {
59 using AAFwk::FdGuard;
60 class AbilityRunningRecord;
61 class AppMgrServiceInner;
62 class AppRunningRecord;
63 class AppRunningManager;
64 
65 struct SpecifiedRequest {
66     int32_t requestId = 0;
67     AAFwk::Want want;
68 };
69 
70 class AppRunningRecord : public std::enable_shared_from_this<AppRunningRecord> {
71 public:
72     static int64_t appEventId_;
73 public:
74     AppRunningRecord(
75         const std::shared_ptr<ApplicationInfo> &info, const int32_t recordId, const std::string &processName);
76     virtual ~AppRunningRecord() = default;
77 
78     /**
79      * @brief Obtains the app record bundleName.
80      *
81      * @return Returns app record bundleName.
82      */
83     const std::string &GetBundleName() const;
84 
85     /**
86      * @brief Obtains the app record CallerPid.
87      *
88      * @return Returns app record CallerPid.
89      */
90     int32_t GetCallerPid() const;
91 
92     /**
93      * @brief Setting the Caller pid.
94      *
95      * @param CallerUid, the Caller pid.
96      */
97     void SetCallerPid(int32_t pid);
98 
99     /**
100      * @brief Obtains the app record CallerUid.
101      *
102      * @return Returns app record CallerUid.
103      */
104     int32_t GetCallerUid() const;
105 
106     /**
107      * @brief Setting the Caller uid.
108      *
109      * @param CallerUid, the Caller uid.
110      */
111     void SetCallerUid(int32_t uid);
112 
113     /**
114      * @brief Obtains the app record CallerTokenId.
115      *
116      * @return Returns app record CallerTokenId.
117      */
118     int32_t GetCallerTokenId() const;
119 
120     /**
121      * @brief Setting the Caller tokenId.
122      *
123      * @param CallerToken, the Caller tokenId.
124      */
125     void SetCallerTokenId(int32_t tokenId);
126 
127     /**
128      * @brief Obtains the app record isLauncherApp flag.
129      *
130      * @return Returns app record isLauncherApp flag.
131      */
132     bool IsLauncherApp() const;
133 
134     /**
135      * @brief Obtains the app record id.
136      *
137      * @return Returns app record id.
138      */
139     int32_t GetRecordId() const;
140 
141     /**
142      * @brief Obtains the app name.
143      *
144      * @return Returns the app name.
145      */
146     const std::string &GetName() const;
147 
148     /**
149      * @brief Obtains the process name.
150      *
151      * @return Returns the process name.
152      */
153     const std::string &GetProcessName() const;
154 
155     /**
156      * @brief Obtains the flag of sandbox extension process.
157      *
158      * @return Returns true or false.
159      */
160     bool GetExtensionSandBoxFlag() const;
161 
162     /**
163      * @brief Setting the flag of sandbox extension process.
164      *
165      * @param extensionSandBoxFlag, the flag of sandbox process.
166      */
167     void SetExtensionSandBoxFlag(bool extensionSandBoxFlag);
168 
169     /**
170      * @brief Obtains the the flag of specified process.
171      *
172      * @return Returns the the flag of specified process.
173      */
174     const std::string &GetSpecifiedProcessFlag() const;
175 
176     /**
177      * @brief Setting the the flag of specified process.
178      *
179      * @param flag, the the flag of specified process.
180      */
181     void SetSpecifiedProcessFlag(const std::string &flag);
182 
183     /**
184      * @brief Obtains the the flag of custom process.
185      *
186      * @return Returns the the flag of custom process.
187      */
188     const std::string &GetCustomProcessFlag() const;
189 
190     /**
191      * @brief Setting the the flag of custom process.
192      *
193      * @param flag, the the flag of custom process.
194      */
195     void SetCustomProcessFlag(const std::string &flag);
196 
197     /**
198      * @brief Obtains the sign code.
199      *
200      * @return Returns the sign code.
201      */
202     const std::string &GetSignCode() const;
203 
204     /**
205      * @brief Setting the sign code.
206      *
207      * @param code, the sign code.
208      */
209     void SetSignCode(const std::string &signCode);
210 
211     /**
212      * @brief Obtains the jointUserId.
213      *
214      * @return Returns the jointUserId.
215      */
216     const std::string &GetJointUserId() const;
217 
218     /**
219      * @brief Setting the jointUserId.
220      *
221      * @param jointUserId, the jointUserId.
222      */
223     void SetJointUserId(const std::string &jointUserId);
224 
225     /**
226      * @brief Obtains the application uid.
227      *
228      * @return Returns the application uid.
229      */
230     int32_t GetUid() const;
231 
232     /**
233      * @brief Setting the application uid.
234      *
235      * @param state, the application uid.
236      */
237     void SetUid(const int32_t uid);
238 
239     /**
240      * @brief Obtains the application userid.
241      *
242      * @return Returns the application userid.
243      */
244     int32_t GetUserId() const;
245 
246     // Get current state for this process
247 
248     /**
249      * @brief Obtains the application state.
250      *
251      * @return Returns the application state.
252      */
253     ApplicationState GetState() const;
254 
255     // Set current state for this process
256 
257     /**
258      * @brief Setting the application state.
259      *
260      * @param state, the application state.
261      */
262     void SetState(const ApplicationState state);
263 
264     // Get abilities_ for this process
265     /**
266      * @brief Obtains the abilities info for the application record.
267      *
268      * @return Returns the abilities info for the application record.
269      */
270     const std::map<const sptr<IRemoteObject>, std::shared_ptr<AbilityRunningRecord>> GetAbilities();
271 
272     bool IsAlreadyHaveAbility();
273 
274     // Update appThread with appThread
275 
276     /**
277      * @brief Setting the application client.
278      *
279      * @param thread, the application client.
280      */
281     void SetApplicationClient(const sptr<IAppScheduler> &thread);
282 
283     /**
284      * @brief Obtains the application client.
285      *
286      * @return Returns the application client.
287      */
288     sptr<IAppScheduler> GetApplicationClient() const;
289 
290      /**
291      * @brief Load a module and schedule the stage's lifecycle
292      * @param appInfo app info
293      * @param abilityInfo ability info
294      * @param token the ability's token
295      * @param hapModuleInfo module info
296      * @param want starting ability param
297      * @param abilityRecordId the record id of the ability
298      */
299     void AddModule(std::shared_ptr<ApplicationInfo> appInfo, std::shared_ptr<AbilityInfo> abilityInfo,
300         sptr<IRemoteObject> token, const HapModuleInfo &hapModuleInfo,
301         std::shared_ptr<AAFwk::Want> want, int32_t abilityRecordId);
302 
303     /**
304      * @brief Batch adding modules whose stages will be loaded
305      * @param appInfo app info
306      * @param moduleInfos list of modules to be added
307      */
308     void AddModules(const std::shared_ptr<ApplicationInfo> &appInfo, const std::vector<HapModuleInfo> &moduleInfos);
309 
310     /**
311      * @brief Search a module record by bundleName and moduleName
312      * @param bundleName bundleName of the module
313      * @param moduleName moduleName of the module
314      * @return the module record matched the params or null
315      */
316     std::shared_ptr<ModuleRunningRecord> GetModuleRecordByModuleName(
317         const std::string &bundleName, const std::string &moduleName);
318 
319     /**
320      * @brief Get one ability's module record
321      * @param token represents the ability
322      */
323     std::shared_ptr<ModuleRunningRecord> GetModuleRunningRecordByToken(const sptr<IRemoteObject> &token) const;
324 
325     std::shared_ptr<ModuleRunningRecord> GetModuleRunningRecordByTerminateLists(const sptr<IRemoteObject> &token) const;
326 
327     std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecord(const int64_t eventId) const;
328 
329     /**
330      * @brief Setting the Trim Memory Level.
331      *
332      * @param level, the Memory Level.
333      */
334     void SetTrimMemoryLevel(int32_t level);
335 
336     /**
337      * LaunchApplication, Notify application to launch application.
338      *
339      * @return
340      */
341     void LaunchApplication(const Configuration &config);
342 
343     /**
344      * AddAbilityStage, Notify application to ability stage.
345      *
346      * @return
347      */
348     void AddAbilityStage();
349 
350     /**
351      * AddAbilityStageBySpecifiedAbility, Notify application to ability stage.
352      *
353      * @return If the abilityStage need to be add, return true.
354      */
355     bool AddAbilityStageBySpecifiedAbility(const std::string &bundleName);
356 
357     void AddAbilityStageBySpecifiedProcess(const std::string &bundleName);
358 
359     /**
360      * AddAbilityStage Result returned.
361      *
362      * @return
363      */
364     void AddAbilityStageDone();
365 
366     /**
367      * update the application info after new module installed.
368      *
369      * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext.
370      *
371      * @return
372      */
373     void UpdateApplicationInfoInstalled(const ApplicationInfo &appInfo, const std::string &moduleName);
374 
375     /**
376      * LaunchAbility, Notify application to launch ability.
377      *
378      * @param ability, the ability record.
379      *
380      * @return
381      */
382     void LaunchAbility(const std::shared_ptr<AbilityRunningRecord> &ability);
383 
384     /**
385      * LaunchPendingAbilities, Launch Pending Abilities.
386      *
387      * @return
388      */
389     void LaunchPendingAbilities();
390 
391     /**
392      * LowMemoryWarning, Low memory warning.
393      *
394      * @return
395      */
396     void LowMemoryWarning();
397 
398     /**
399      * ScheduleTerminate, Notify application to terminate.
400      *
401      * @return
402      */
403     void ScheduleTerminate();
404 
405     /**
406      * ScheduleTerminate, Notify application process exit safely.
407      *
408      * @return
409      */
410     void ScheduleProcessSecurityExit();
411 
412     /**
413      * ScheduleTerminate, Notify application clear page stack.
414      *
415      * @return
416      */
417     void ScheduleClearPageStack();
418 
419     /**
420      * ScheduleTrimMemory, Notifies the application of the memory seen.
421      *
422      * @return
423      */
424     void ScheduleTrimMemory();
425 
426     /**
427      * ScheduleMemoryLevel, Notifies the application of the current memory.
428      *
429      * @return
430      */
431     void ScheduleMemoryLevel(int32_t level);
432 
433     /**
434      * ScheduleHeapMemory, Get the application's memory allocation info.
435      *
436      * @param pid, pid input.
437      * @param mallocInfo, dynamic storage information output.
438      *
439      * @return
440      */
441     void ScheduleHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo);
442 
443     /**
444      * ScheduleJsHeapMemory, triggerGC and dump the application's jsheap memory info.
445      *
446      * @param info, pid, tid, needGc, needSnapshot
447      *
448      * @return
449      */
450     void ScheduleJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info);
451 
452     /**
453      * ScheduleCjHeapMemory, triggerGC and dump the application's cjheap memory info.
454      *
455      * @param info, pid, needGc, needSnapshot
456      *
457      * @return
458      */
459     void ScheduleCjHeapMemory(OHOS::AppExecFwk::CjHeapDumpInfo &info);
460 
461     /**
462      * GetAbilityRunningRecordByToken, Obtaining the ability record through token.
463      *
464      * @param token, the unique identification to the ability.
465      *
466      * @return ability running record
467      */
468     std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecordByToken(const sptr<IRemoteObject> &token) const;
469 
470     std::shared_ptr<AbilityRunningRecord> GetAbilityByTerminateLists(const sptr<IRemoteObject> &token) const;
471 
472     /**
473      * UpdateAbilityState, update the ability status.
474      *
475      * @param token, the unique identification to update the ability.
476      * @param state, ability status that needs to be updated.
477      *
478      * @return
479      */
480     void UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state);
481 
482     /**
483      * PopForegroundingAbilityTokens, Extract the token record from the foreground tokens list.
484      *
485      * @return
486      */
487     void PopForegroundingAbilityTokens();
488 
489     /**
490      * TerminateAbility, terminate the token ability.
491      *
492      * @param token, he unique identification to terminate the ability.
493      *
494      * @return
495      */
496     void TerminateAbility(const sptr<IRemoteObject> &token, const bool isForce, bool isTimeout = false);
497 
498     /**
499      * AbilityTerminated, terminate the ability.
500      *
501      * @param token, the unique identification to terminated the ability.
502      *
503      * @return
504      */
505     void AbilityTerminated(const sptr<IRemoteObject> &token);
506 
507     /**
508      * @brief Setting application service internal handler instance.
509      *
510      * @param serviceInner, application service internal handler instance.
511      */
512     void SetAppMgrServiceInner(const std::weak_ptr<AppMgrServiceInner> &inner);
513 
514     /**
515      * @brief Setting application death recipient.
516      *
517      * @param appDeathRecipient, application death recipient instance.
518      */
519     void SetAppDeathRecipient(const sptr<AppDeathRecipient> &appDeathRecipient);
520 
521     /**
522      * @brief Obtains application priority info.
523      *
524      * @return Returns the application priority info.
525      */
526     std::shared_ptr<PriorityObject> GetPriorityObject();
527 
528     /**
529      * Remove application death recipient record.
530      *
531      * @return
532      */
533     void RemoveAppDeathRecipient() const;
534 
535     /**
536     *  Notify application update system environment changes.
537     *
538     * @param config System environment change parameters.
539     * @return Returns ERR_OK on success, others on failure.
540     */
541     int32_t UpdateConfiguration(const Configuration &config);
542 
543     void SetTaskHandler(std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler);
544     void SetEventHandler(const std::shared_ptr<AMSEventHandler> &handler);
545 
546     /**
547      * When the one process has no ability, it will go dying.
548      */
549     bool IsLastAbilityRecord(const sptr<IRemoteObject> &token);
550 
551     bool IsLastPageAbilityRecord(const sptr<IRemoteObject> &token);
552 
553     bool ExtensionAbilityRecordExists();
554 
555     /**
556      * @brief indicates one process will go dying.
557      * Then the process won't be reused.
558      */
559     void SetTerminating();
560 
561     /**
562      * @brief Whether the process is dying.
563      */
564     bool IsTerminating();
565 
566     /**
567      * @brief Whether the process should keep alive.
568      */
569     bool IsKeepAliveApp() const;
570 
571     /**
572      * @brief Whether the process is non-resident keep-alive.
573      */
574     bool IsKeepAliveDkv() const;
575 
576     /**
577      * @brief Whether the process is non-resident keep-alive app service extension.
578      */
579     bool IsKeepAliveAppService() const;
580 
581     /**
582      * @brief Whether the process can keep empty alive.
583      */
584     bool IsEmptyKeepAliveApp() const;
585 
586     /**
587      * @brief Whether the process is main process.
588      */
589     bool IsMainProcess() const;
590 
591     /**
592      * @brief indicates one process can stay alive without any abilities.
593      * One case is that the process's stages being loaded
594      *
595      * @param isEmptyKeepAliveApp new value
596      */
597     void SetEmptyKeepAliveAppState(bool isEmptyKeepAliveApp);
598 
599     /**
600      * @brief A process can config itself to keep alive or not.
601      * when one process started, this method will be called from ability mgr with data selected from db.
602      *
603      * @param isKeepAliveEnable new value
604      */
605     void SetKeepAliveEnableState(bool isKeepAliveEnable);
606 
607     /**
608      * @brief A process can be configured non-resident keep-alive.
609      * when one process started, this method will be called from ability mgr with data selected from db.
610      *
611      * @param isKeepAliveDkv new value
612      */
613     void SetKeepAliveDkv(bool isKeepAliveDkv);
614 
615     void SetMainElementRunning(bool isMainElementRunning);
616 
617     bool IsMainElementRunning() const;
618 
619     void SetKeepAliveAppService(bool isKeepAliveAppService);
620 
621     /**
622      * @brief roughly considered as a value from the process's bundle info.
623      *
624      * @param isKeepAliveBundle new value
625      */
626     void SetKeepAliveBundle(bool isKeepAliveBundle);
627 
628     /**
629      * @brief only the bundle's main process can stay alive.
630      *
631      * @param isMainProcess new value
632      */
633     void SetMainProcess(bool isMainProcess);
634 
635     void SetSingleton(bool isSingleton);
636 
637     void SetStageModelState(bool isStageBasedModel);
638 
639     std::list<std::shared_ptr<ModuleRunningRecord>> GetAllModuleRecord() const;
640 
641     const std::list<std::shared_ptr<ApplicationInfo>> GetAppInfoList();
642 
643     void SetAppIdentifier(const std::string &appIdentifier);
644     const std::string &GetAppIdentifier() const;
645 
GetApplicationInfo()646     inline const std::shared_ptr<ApplicationInfo> GetApplicationInfo()
647     {
648         return appInfo_;
649     }
650 
651     void SetRestartResidentProcCount(int count);
652     void DecRestartResidentProcCount();
653     int GetRestartResidentProcCount() const;
654     bool CanRestartResidentProc();
655 
656     /**
657      * Notify observers when state change.
658      *
659      * @param ability, ability or extension record.
660      * @param state, ability or extension state.
661      */
662     void StateChangedNotifyObserver(
663         const std::shared_ptr<AbilityRunningRecord> &ability,
664         int32_t state,
665         bool isAbility,
666         bool isFromWindowFocusChanged);
667 
668     void insertAbilityStageInfo(std::vector<HapModuleInfo> moduleInfos);
669 
670     void GetBundleNames(std::vector<std::string> &bundleNames);
671 
672     void SetUserTestInfo(const std::shared_ptr<UserTestRecord> &record);
673     std::shared_ptr<UserTestRecord> GetUserTestInfo();
674 
675     void SetProcessAndExtensionType(const std::shared_ptr<AbilityInfo> &abilityInfo, uint32_t extensionProcessMode = 0);
676     void SetStartupTaskData(const AAFwk::Want &want);
677     void SetSpecifiedAbilityFlagAndWant(int requestId, const AAFwk::Want &want, const std::string &moduleName);
678     void SetScheduleNewProcessRequestState(int32_t requestId, const AAFwk::Want &want, const std::string &moduleName);
679     /**
680      * Is processing new process request
681      */
682     bool IsNewProcessRequest() const;
683     /**
684      * Is processing specified ability request
685      */
686     bool IsStartSpecifiedAbility() const;
687     /**
688      * Get the specified requestId, -1 for none specified
689      */
690     int32_t GetSpecifiedRequestId() const;
691     /**
692      * Called when one specified request is finished to clear the request
693      */
694     void ResetSpecifiedRequest();
695 
696     void SchedulePrepareTerminate(const std::string &moduleName);
697 
698     /**
699      * call the scheduler to go acceptWant procedure
700      */
701     void ScheduleAcceptWant(const std::string &moduleName);
702     /**
703      * Called when acceptWant complete
704      */
705     void ScheduleAcceptWantDone();
706     void ScheduleNewProcessRequest(const AAFwk::Want &want, const std::string &moduleName);
707     void ScheduleNewProcessRequestDone();
708     void ApplicationTerminated();
709     /**
710      * Get the want param for specified request
711      */
712     AAFwk::Want GetSpecifiedWant() const;
713     AAFwk::Want GetNewProcessRequestWant() const;
714     int32_t GetNewProcessRequestId() const;
715     void ResetNewProcessRequest();
716     bool IsDebug();
717     void SetDebugApp(bool isDebugApp);
718     /**
719      * Indicate whether the process is a debugging one
720      */
721     bool IsDebugApp();
722     /**
723      * debug flag or assert flag is set
724      */
725     bool IsDebugging() const;
726     void SetErrorInfoEnhance(const bool errorInfoEnhance);
727     void SetNativeDebug(bool isNativeDebug);
728     void SetPerfCmd(const std::string &perfCmd);
729     void SetMultiThread(const bool multiThread);
730     void AddRenderRecord(const std::shared_ptr<RenderRecord> &record);
731     void RemoveRenderRecord(const std::shared_ptr<RenderRecord> &record);
732     void RemoveRenderPid(pid_t pid);
733     void GetRenderProcessInfos(std::list<SimpleProcessInfo> &processInfos);
734     bool ConstainsRenderPid(pid_t renderPid);
735     std::shared_ptr<RenderRecord> GetRenderRecordByPid(const pid_t pid);
736     std::map<int32_t, std::shared_ptr<RenderRecord>> GetRenderRecordMap();
737     void SetStartMsg(const AppSpawnStartMsg &msg);
738     AppSpawnStartMsg GetStartMsg();
739 
740     void SendEventForSpecifiedAbility();
741 
742     void SendAppStartupTypeEvent(const std::shared_ptr<AbilityRunningRecord> &ability, const AppStartType startType);
743     void SetKilling();
744     bool IsKilling() const;
745     void SetPreForeground(bool isPreForeground);
746     bool IsPreForeground() const;
747     void SetAppIndex(const int32_t appIndex);
748     int32_t GetAppIndex() const;
749     void SetInstanceKey(const std::string& instanceKey);
750     std::string GetInstanceKey() const;
751     void SetSecurityFlag(bool securityFlag);
752     bool GetSecurityFlag() const;
753 
754     using Closure = std::function<void()>;
755     void PostTask(std::string msg, int64_t timeOut, const Closure &task);
756     bool CancelTask(std::string msg);
757     void RemoveTerminateAbilityTimeoutTask(const sptr<IRemoteObject>& token) const;
758 
759     int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback,
760         const int32_t recordId);
761 
762     int32_t NotifyHotReloadPage(const sptr<IQuickFixCallback> &callback, const int32_t recordId);
763 
764     int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback,
765         const int32_t recordId);
766 
767     bool IsContinuousTask();
768 
769     void SetContinuousTaskAppState(bool isContinuousTask);
770 
771     /**
772      * Update target ability focus state.
773      *
774      * @param token the token of target ability.
775      * @param isFocus focus state.
776      *
777      * @return true if process focus state changed, false otherwise.
778      */
779     bool UpdateAbilityFocusState(const sptr<IRemoteObject> &token, bool isFocus);
780 
781     bool GetFocusFlag() const;
782 
783     int64_t GetAppStartTime() const;
784 
785     void SetRestartTimeMillis(const int64_t restartTimeMillis);
786     void SetRequestProcCode(int32_t requestProcCode);
787 
788     int32_t GetRequestProcCode() const;
789 
790     void SetProcessChangeReason(ProcessChangeReason reason);
791 
792     bool NeedUpdateConfigurationBackground();
793 
794     ProcessChangeReason GetProcessChangeReason() const;
795 
796     ExtensionAbilityType GetExtensionType() const;
797     ProcessType GetProcessType() const;
798 
799     /**
800      * Notify Fault Data
801      *
802      * @param faultData the fault data.
803      * @return Returns ERR_OK on success, others on failure.
804      */
805     int32_t NotifyAppFault(const FaultData &faultData);
806 #ifdef SUPPORT_SCREEN
807     void ChangeWindowVisibility(const sptr<OHOS::Rosen::WindowVisibilityInfo> &info);
808     void OnWindowVisibilityChanged(const std::vector<sptr<OHOS::Rosen::WindowVisibilityInfo>> &windowVisibilityInfos);
809     void OnWindowVisibilityChangedWithPendingState();
810 #endif //SUPPORT_SCREEN
811     bool IsAbilitiesBackground();
812 
SetAbilityForegroundingFlag()813     inline void SetAbilityForegroundingFlag()
814     {
815         isAbilityForegrounding_.store(true);
816     }
817 
GetAbilityForegroundingFlag()818     inline bool GetAbilityForegroundingFlag()
819     {
820         return isAbilityForegrounding_.load();
821     }
822 
SetSpawned()823     inline void SetSpawned()
824     {
825         isSpawned_.store(true);
826     }
827 
GetSpawned()828     inline bool GetSpawned() const
829     {
830         return isSpawned_.load();
831     }
832 
833     std::map<pid_t, std::weak_ptr<AppRunningRecord>> GetChildAppRecordMap() const;
834     void AddChildAppRecord(pid_t pid, std::shared_ptr<AppRunningRecord> appRecord);
835     void RemoveChildAppRecord(pid_t pid);
836     void ClearChildAppRecordMap();
837 
838     void SetParentAppRecord(std::shared_ptr<AppRunningRecord> appRecord);
839     std::shared_ptr<AppRunningRecord> GetParentAppRecord();
840 
841     /**
842      * @brief Notify NativeEngine GC of status change.
843      *
844      * @param state GC state
845      *
846      * @return Is the status change completed.
847      */
848     int32_t ChangeAppGcState(int32_t state, uint64_t tid = 0);
849 
850     void SetAttachDebug(bool isAttachDebug, bool isDebugFromLocal);
851     bool IsAttachDebug() const;
852 
853     void SetApplicationPendingState(ApplicationPendingState pendingState);
854     ApplicationPendingState GetApplicationPendingState() const;
855 
856     void SetApplicationScheduleState(ApplicationScheduleState scheduleState);
857     ApplicationScheduleState GetApplicationScheduleState() const;
858 
859     void GetSplitModeAndFloatingMode(bool &isSplitScreenMode, bool &isFloatingWindowMode);
860 
861 #ifdef SUPPORT_CHILD_PROCESS
862     void AddChildProcessRecord(pid_t pid, std::shared_ptr<ChildProcessRecord> record);
863     void RemoveChildProcessRecord(std::shared_ptr<ChildProcessRecord> record);
864     std::shared_ptr<ChildProcessRecord> GetChildProcessRecordByPid(pid_t pid);
865     std::map<pid_t, std::shared_ptr<ChildProcessRecord>> GetChildProcessRecordMap();
866     int32_t GetChildProcessCount();
867     void GetChildProcessInfos(std::list<SimpleProcessInfo> &processInfos);
868 #endif //SUPPORT_CHILD_PROCESS
869 
870     void SetPreloadState(PreloadState state);
871 
872     void SetPreloadPhase(PreloadPhase phase);
873 
874     PreloadPhase GetPreloadPhase();
875 
876     bool IsPreloading() const;
877 
878     bool IsPreloaded() const;
879 
880     void SetPreloadMode(PreloadMode mode);
881 
882     PreloadMode GetPreloadMode();
883 
884     void SetPreloadModuleName(const std::string& preloadModuleName);
885 
886     std::string GetPreloadModuleName() const;
887 
888     /**
889      * @brief Obtains the app record assign tokenId.
890      *
891      * @return Returns app record AssignTokenId.
892      */
893     int32_t GetAssignTokenId() const;
894 
895     /**
896      * @brief Setting the assign tokenId.
897      *
898      * @param AssignTokenId, the assign tokenId.
899      */
900     void SetAssignTokenId(int32_t tokenId);
901     /**
902      * @brief Setting is aa start with native.
903      *
904      * @param isNativeStart, is aa start with native.
905      */
906     void SetNativeStart(bool isNativeStart);
907     /**
908      * @brief Obtains is native start.
909      *
910      * @return Returns is native start.
911      */
912     bool isNativeStart() const;
913 
914     void SetRestartAppFlag(bool isRestartApp);
915     bool GetRestartAppFlag() const;
916 
917     void SetAssertionPauseFlag(bool flag);
918     bool IsAssertionPause() const;
919 
920     void SetJITEnabled(const bool jitEnabled);
921     bool IsJITEnabled() const;
922 
923     int DumpIpcStart(std::string& result);
924     int DumpIpcStop(std::string& result);
925     int DumpIpcStat(std::string& result);
926 
927     int DumpFfrt(std::string &result);
928 
929     void SetExitReason(int32_t reason);
930     int32_t GetExitReason() const;
931 
932     void SetExitMsg(const std::string &exitMsg);
933     std::string GetExitMsg() const;
934 
935     bool SetSupportedProcessCache(bool isSupport);
936     SupportProcessCacheState GetSupportProcessCacheState();
937     void SetAttachedToStatusBar(bool isAttached);
938     bool IsAttachedToStatusBar();
939 
940     bool SetEnableProcessCache(bool enable);
941     bool GetEnableProcessCache();
942 
943     void ScheduleCacheProcess();
944 
945     void SetBrowserHost(sptr<IRemoteObject> browser);
946     sptr<IRemoteObject> GetBrowserHost();
947     void SetHasGPU(bool gpu);
948     bool HasGPU();
949     void SetGPUPid(pid_t gpuPid);
950     pid_t GetGPUPid();
951     pid_t GetPid();
952 
SetStrictMode(bool strictMode)953     inline void SetStrictMode(bool strictMode)
954     {
955         isStrictMode_ = strictMode;
956     }
957 
IsStrictMode()958     inline bool IsStrictMode() const
959     {
960         return isStrictMode_;
961     }
962 
SetNetworkEnableFlags(bool enable)963     inline void SetNetworkEnableFlags(bool enable)
964     {
965         networkEnableFlags_ = enable;
966     }
967 
GetNetworkEnableFlags()968     inline bool GetNetworkEnableFlags() const
969     {
970         return networkEnableFlags_;
971     }
972 
SetSAEnableFlags(bool enable)973     inline void SetSAEnableFlags(bool enable)
974     {
975         saEnableFlags_ = enable;
976     }
977 
GetSAEnableFlags()978     inline bool GetSAEnableFlags() const
979     {
980         return saEnableFlags_;
981     }
982 
SetIsDependedOnArkWeb(bool isDepend)983     inline void SetIsDependedOnArkWeb(bool isDepend)
984     {
985         isDependedOnArkWeb_ = isDepend;
986     }
987 
IsDependedOnArkWeb()988     inline bool IsDependedOnArkWeb()
989     {
990         return isDependedOnArkWeb_;
991     }
992 
993     void SetProcessCacheBlocked(bool isBlocked);
994     bool GetProcessCacheBlocked();
995 
996     void SetProcessCaching(bool isCaching);
997     bool IsCaching();
998     void SetNeedPreloadModule(bool isNeedPreloadModule);
999     bool GetNeedPreloadModule();
1000     void SetNeedLimitPrio(bool isNeedLimitPrio);
1001     bool GetNeedLimitPrio();
1002 
1003     /**
1004      * ScheduleForegroundRunning, Notify application to switch to foreground.
1005      *
1006      * @return bool operation status
1007      */
1008     bool ScheduleForegroundRunning();
1009 
1010     /**
1011      * ScheduleBackgroundRunning, Notify application to switch to background.
1012      *
1013      * @return
1014      */
1015     void ScheduleBackgroundRunning();
1016 
1017     /**
1018      * SetWatchdogBackgroundStatusRunning, Notify application to set watchdog background status.
1019      *
1020      * @return
1021      */
1022     void SetWatchdogBackgroundStatusRunning(bool status);
1023 
1024     void SetUserRequestCleaning();
1025     bool IsUserRequestCleaning() const;
1026     bool IsAllAbilityReadyToCleanedByUserRequest();
1027     bool IsProcessAttached() const;
1028     // records whether uiability has launched before.
1029     void SetUIAbilityLaunched(bool hasLaunched);
1030     bool HasUIAbilityLaunched();
1031 
SetIsKia(bool isKia)1032     inline void SetIsKia(bool isKia)
1033     {
1034         isKia_ = isKia;
1035     }
1036 
GetIsKia()1037     inline bool GetIsKia() const
1038     {
1039         return isKia_;
1040     }
1041 
ResetDelayConfiguration()1042     inline void ResetDelayConfiguration()
1043     {
1044         delayConfiguration_ = std::make_shared<Configuration>();
1045     }
1046 
GetDelayConfiguration()1047     inline std::shared_ptr<Configuration> GetDelayConfiguration()
1048     {
1049         return delayConfiguration_;
1050     }
1051 
SetKillReason(std::string killReason)1052     inline void SetKillReason(std::string killReason)
1053     {
1054         std::lock_guard<ffrt::mutex> lock(killReasonLock_);
1055         killReason_ = killReason;
1056     }
1057 
GetKillReason()1058     inline std::string GetKillReason() const
1059     {
1060         std::lock_guard<ffrt::mutex> lock(killReasonLock_);
1061         return killReason_;
1062     }
1063 
SetIsKillPrecedeStart(bool isKillPrecedeStart)1064     inline void SetIsKillPrecedeStart(bool isKillPrecedeStart)
1065     {
1066         isKillPrecedeStart_.store(isKillPrecedeStart);
1067     }
1068 
IsKillPrecedeStart()1069     inline bool IsKillPrecedeStart() const
1070     {
1071         return isKillPrecedeStart_.load();
1072     }
1073 
1074     void AddAppLifecycleEvent(const std::string &msg);
1075 
1076     void SetNWebPreload(const bool isAllowedNWebPreload);
1077 
1078     bool IsNWebPreload() const;
1079 
1080     void SetIsUnSetPermission(bool isUnSetPermission);
1081 
1082     bool IsUnSetPermission();
1083 
1084     void UnSetPolicy();
1085 
SetRssValue(int32_t rssValue)1086     inline void SetRssValue(int32_t rssValue)
1087     {
1088         rssValue_ = rssValue;
1089     }
1090 
GetRssValue()1091     inline int32_t GetRssValue() const
1092     {
1093         return rssValue_;
1094     }
1095 
SetPssValue(int32_t pssValue)1096     inline void SetPssValue(int32_t pssValue)
1097     {
1098         pssValue_ = pssValue;
1099     }
1100 
GetPssValue()1101     inline int32_t GetPssValue() const
1102     {
1103         return pssValue_;
1104     }
SetReasonExist(bool reasonExist)1105     inline void SetReasonExist(bool reasonExist)
1106     {
1107         reasonExist_ = reasonExist;
1108     }
GetReasonExist()1109     inline bool GetReasonExist() const
1110     {
1111         return reasonExist_;
1112     }
1113 
1114     void SetDebugFromLocal(bool isDebugFromLocal);
1115 
GetDebugFromLocal()1116     bool GetDebugFromLocal() const
1117     {
1118         return isDebugFromLocal_;
1119     }
1120 
1121     std::optional<bool> IsSupportMultiProcessDeviceFeature() const;
1122     void SetSupportMultiProcessDeviceFeature(bool support);
1123 
SetMasterProcess(bool isMasterProcess)1124     inline void SetMasterProcess(bool isMasterProcess)
1125     {
1126         isMasterProcess_ = isMasterProcess;
1127     }
1128 
GetIsMasterProcess()1129     inline bool GetIsMasterProcess() const
1130     {
1131         return isMasterProcess_;
1132     }
1133 
SetTimeStamp(int64_t timeStamp)1134     inline void SetTimeStamp(int64_t timeStamp)
1135     {
1136         timeStamp_ = timeStamp;
1137     }
1138 
GetTimeStamp()1139     inline int64_t GetTimeStamp() const
1140     {
1141         return timeStamp_;
1142     }
1143 
1144 private:
1145     /**
1146      * SearchTheModuleInfoNeedToUpdated, Get an uninitialized abilityStage data.
1147      *
1148      * @return If an uninitialized data is found return true,Otherwise return false.
1149      */
1150     bool GetTheModuleInfoNeedToUpdated(const std::string bundleName, HapModuleInfo &info);
1151 
1152     /**
1153      * AbilityForeground, Handling the ability process when switching to the foreground.
1154      *
1155      * @param ability, the ability info.
1156      *
1157      * @return
1158      */
1159     void AbilityForeground(const std::shared_ptr<AbilityRunningRecord> &ability);
1160 
1161     /**
1162      * AbilityBackground, Handling the ability process when switching to the background.
1163      *
1164      * @param ability, the ability info.
1165      *
1166      * @return
1167      */
1168     void AbilityBackground(const std::shared_ptr<AbilityRunningRecord> &ability);
1169     // drive application state changes when ability state changes.
1170 
1171     bool AbilityFocused(const std::shared_ptr<AbilityRunningRecord> &ability);
1172 
1173     bool AbilityUnfocused(const std::shared_ptr<AbilityRunningRecord> &ability);
1174 
1175     void SendEvent(uint32_t msg, int64_t timeOut);
1176     void RemoveEvent(uint32_t msg);
1177 
1178     void RemoveModuleRecord(const std::shared_ptr<ModuleRunningRecord> &record, bool isExtensionDebug = false);
1179     int32_t GetAddStageTimeout() const;
1180     void SetModuleLoaded(const std::string &moduleName) const;
1181 
1182 private:
1183     class RemoteObjHash {
1184     public:
operator()1185         size_t operator() (const sptr<IRemoteObject> remoteObj) const
1186         {
1187             return reinterpret_cast<size_t>(remoteObj.GetRefPtr());
1188         }
1189     };
1190     bool IsWindowIdsEmpty();
1191 
1192     bool isKeepAliveRdb_ = false;  // Only resident processes can be set to true, please choose carefully
1193     bool isKeepAliveBundle_ = false;
1194     bool isEmptyKeepAliveApp_ = false;  // Only empty resident processes can be set to true, please choose carefully
1195     bool isKeepAliveDkv_ = false; // Only non-resident keep-alive processes can be set to true, please choose carefully
1196     bool isMainElementRunning_ = false;
1197     bool isKeepAliveAppService_ = false;
1198     bool isMainProcess_ = true; // Only MainProcess can be keepalive
1199     bool isSingleton_ = false;
1200     bool isStageBasedModel_ = false;
1201     bool isFocused_ = false; // if process is focused.
1202     ApplicationState curState_ = ApplicationState::APP_STATE_CREATE;  // current state of this process
1203     ApplicationPendingState pendingState_ = ApplicationPendingState::READY;
1204     ApplicationScheduleState scheduleState_ = ApplicationScheduleState::SCHEDULE_READY;
1205     WatchdogVisibilityState watchdogVisibilityState_ = WatchdogVisibilityState::WATCHDOG_STATE_READY;
1206     /**
1207      * If there is an ability is foregrounding, this flag will be true,
1208      * and this flag will remain true until this application is background.
1209      */
1210     std::atomic_bool isAbilityForegrounding_ = false;
1211     bool isTerminating = false;
1212     bool isCaching_ = false;
1213     bool isLauncherApp_;
1214     bool isDebugApp_ = false;
1215     bool isNativeDebug_ = false;
1216     bool isAttachDebug_ = false;
1217     bool jitEnabled_ = false;
1218     bool securityFlag_ = false; // render record
1219     bool isContinuousTask_ = false;    // Only continuesTask processes can be set to true, please choose carefully
1220     bool isRestartApp_ = false; // Only app calling RestartApp can be set to true
1221     bool isAssertPause_ = false;
1222     bool isErrorInfoEnhance_ = false;
1223     bool isNativeStart_ = false;
1224     bool isMultiThread_ = false;
1225     bool enableProcessCache_ = true;
1226     bool processCacheBlocked = false; // temporarily block process cache feature
1227     bool hasGPU_ = false;
1228     bool isStrictMode_ = false;
1229     bool networkEnableFlags_ = true;
1230     bool saEnableFlags_ = true;
1231     bool isAttachedToStatusBar = false;
1232     bool isDependedOnArkWeb_ = false;
1233     bool isUserRequestCleaning_ = false;
1234     bool hasUIAbilityLaunched_ = false;
1235     bool isKia_ = false;
1236     bool isNeedPreloadModule_ = false;
1237     bool isNeedLimitPrio_ = false;
1238     bool isAllowedNWebPreload_ = false;
1239     bool isUnSetPermission_ = false;
1240     bool isExtensionSandBox_ = false;
1241     std::atomic<bool> isKilling_ = false;
1242     std::atomic_bool isSpawned_ = false;
1243     std::atomic<bool> isPreForeground_ = false;
1244 
1245     int32_t appRecordId_ = 0;
1246     int32_t mainUid_;
1247     int restartResidentProcCount_ = 0;
1248     int32_t exitReason_ = 0;
1249     int32_t appIndex_ = 0; // render record
1250     int32_t requestProcCode_ = 0; // render record
1251     int32_t callerPid_ = -1;
1252     int32_t callerUid_ = -1;
1253     int32_t callerTokenId_ = -1;
1254     int32_t assignTokenId_ = 0;
1255     pid_t gpuPid_ = 0;
1256     ProcessType processType_ = ProcessType::NORMAL;
1257     ExtensionAbilityType extensionType_ = ExtensionAbilityType::UNSPECIFIED;
1258     PreloadState preloadState_ = PreloadState::NONE;
1259     PreloadMode preloadMode_ = PreloadMode::PRELOAD_NONE;
1260     PreloadPhase preloadPhase_ = PreloadPhase::UNSPECIFIED;
1261     SupportProcessCacheState procCacheSupportState_ = SupportProcessCacheState::UNSPECIFIED;
1262     int64_t startTimeMillis_ = 0;   // The time of app start(CLOCK_MONOTONIC)
1263     int64_t restartTimeMillis_ = 0; // The time of last trying app restart
1264 
1265     std::shared_ptr<ApplicationInfo> appInfo_ = nullptr;  // the application's info of this process
1266     std::string processName_;  // the name of this process
1267     std::string specifiedProcessFlag_; // the flag of specified Process
1268     std::string customProcessFlag_; // the flag of custom process
1269     std::unordered_set<sptr<IRemoteObject>, RemoteObjHash> foregroundingAbilityTokens_;
1270     std::weak_ptr<AppMgrServiceInner> appMgrServiceInner_;
1271     sptr<AppDeathRecipient> appDeathRecipient_ = nullptr;
1272     std::shared_ptr<PriorityObject> priorityObject_;
1273     std::shared_ptr<AppLifeCycleDeal> appLifeCycleDeal_ = nullptr;
1274     std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
1275     std::shared_ptr<AMSEventHandler> eventHandler_;
1276     std::string signCode_;  // the sign of this hap
1277     std::string jointUserId_;
1278     std::map<std::string, std::shared_ptr<ApplicationInfo>> appInfos_;
1279     ffrt::mutex appInfosLock_;
1280     std::map<std::string, std::vector<std::shared_ptr<ModuleRunningRecord>>> hapModules_;
1281     mutable ffrt::mutex hapModulesLock_;
1282     std::string mainBundleName_;
1283     std::string mainAppName_;
1284     std::string appIdentifier_;
1285 
1286     mutable std::mutex specifiedMutex_;
1287     std::shared_ptr<SpecifiedRequest> specifiedAbilityRequest_;
1288     std::shared_ptr<SpecifiedRequest> specifiedProcessRequest_;
1289     std::string moduleName_;
1290 
1291     std::string perfCmd_;
1292     std::string preloadModuleName_;
1293     std::string exitMsg_ = "";
1294 
1295     std::shared_ptr<UserTestRecord> userTestRecord_ = nullptr;
1296 
1297     std::weak_ptr<AppRunningRecord> parentAppRecord_;
1298     std::map<pid_t, std::weak_ptr<AppRunningRecord>> childAppRecordMap_;
1299 
1300     std::map<int32_t, std::shared_ptr<RenderRecord>> renderRecordMap_; // render record
1301     ffrt::mutex renderRecordMapLock_; // render record lock
1302     std::set<pid_t> renderPidSet_; // Contains all render pid added, whether died or not
1303     ffrt::mutex renderPidSetLock_; // render pid set lock
1304     AppSpawnStartMsg startMsg_; // render record
1305     std::string instanceKey_; // render record
1306     ProcessChangeReason processChangeReason_ = ProcessChangeReason::REASON_NONE; // render record
1307 
1308     std::set<uint32_t> windowIds_;
1309     ffrt::mutex windowIdsLock_;
1310 #ifdef SUPPORT_CHILD_PROCESS
1311     std::map<pid_t, std::shared_ptr<ChildProcessRecord>> childProcessRecordMap_;
1312     ffrt::mutex childProcessRecordMapLock_;
1313 #endif //SUPPORT_CHILD_PROCESS
1314 
1315     sptr<IRemoteObject> browserHost_;
1316     std::shared_ptr<Configuration> delayConfiguration_ = std::make_shared<Configuration>();
1317     std::string killReason_ = "";
1318     std::atomic_bool isKillPrecedeStart_ = false;
1319     int32_t rssValue_ = 0;
1320     int32_t pssValue_ = 0;
1321     bool reasonExist_ = false;
1322     bool isDebugFromLocal_ = false;
1323     std::optional<bool> supportMultiProcessDeviceFeature_ = std::nullopt;
1324     mutable ffrt::mutex supportMultiProcessDeviceFeatureLock_;
1325     std::shared_ptr<StartupTaskData> startupTaskData_ = nullptr;
1326     ffrt::mutex startupTaskDataLock_;
1327     mutable ffrt::mutex killReasonLock_;
1328 
1329     bool isMasterProcess_ = false; // Only MasterProcess can be keepalive
1330     int64_t timeStamp_ = 0; // the flag of BackUpMainControlProcess
1331 };
1332 
1333 }  // namespace AppExecFwk
1334 }  // namespace OHOS
1335 #endif  // OHOS_ABILITY_RUNTIME_APP_RUNNING_RECORD_H
1336