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