• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "iremote_object.h"
24 #include "irender_scheduler.h"
25 #include "ability_running_record.h"
26 #include "ability_state_data.h"
27 #include "application_info.h"
28 #include "app_death_recipient.h"
29 #include "app_launch_data.h"
30 #include "app_mgr_constants.h"
31 #include "app_scheduler_proxy.h"
32 #include "app_record_id.h"
33 #include "profile.h"
34 #include "priority_object.h"
35 #include "app_lifecycle_deal.h"
36 #include "module_running_record.h"
37 #include "app_spawn_msg_wrapper.h"
38 
39 namespace OHOS {
40 namespace AppExecFwk {
41 class AbilityRunningRecord;
42 class AppMgrServiceInner;
43 class AppRunningRecord;
44 
45 /**
46  * @class RenderRecord
47  * Record nweb render process info.
48  */
49 class RenderRecord {
50 public:
51     RenderRecord(pid_t hostPid, const std::string &renderParam,
52         int32_t ipcFd, int32_t sharedFd, const std::shared_ptr<AppRunningRecord> &host);
53 
54     virtual ~RenderRecord();
55 
56     static std::shared_ptr<RenderRecord> CreateRenderRecord(pid_t hostPid, const std::string &renderParam,
57         int32_t ipcFd, int32_t sharedFd, const std::shared_ptr<AppRunningRecord> &host);
58 
59     void SetPid(pid_t pid);
60     pid_t GetPid() const ;
61     pid_t GetHostPid() const;
62     int32_t GetHostUid() const;
63     std::string GetHostBundleName() const;
64     std::string GetRenderParam() const;
65     int32_t GetIpcFd() const;
66     int32_t GetSharedFd() const;
67     std::shared_ptr<AppRunningRecord> GetHostRecord() const;
68     sptr<IRenderScheduler> GetScheduler() const;
69     void SetScheduler(const sptr<IRenderScheduler> &scheduler);
70     void SetDeathRecipient(const sptr<AppDeathRecipient> recipient);
71     void RegisterDeathRecipient();
72 
73 private:
74     void SetHostUid(const int32_t hostUid);
75     void SetHostBundleName(const std::string &hostBundleName);
76 
77     pid_t pid_ = 0;
78     pid_t hostPid_ = 0;
79     int32_t hostUid_ = 0;
80     std::string hostBundleName_;
81     std::string renderParam_;
82     int32_t ipcFd_ = 0;
83     int32_t sharedFd_ = 0;
84     std::weak_ptr<AppRunningRecord> host_; // nweb host
85     sptr<IRenderScheduler> renderScheduler_ = nullptr;
86     sptr<AppDeathRecipient> deathRecipient_ = nullptr;
87 };
88 
89 class AppRunningRecord : public std::enable_shared_from_this<AppRunningRecord> {
90 public:
91     static int64_t appEventId_;
92 public:
93     AppRunningRecord(
94         const std::shared_ptr<ApplicationInfo> &info, const int32_t recordId, const std::string &processName);
95     virtual ~AppRunningRecord() = default;
96 
97     /**
98      * @brief Obtains the app record bundleName.
99      *
100      * @return Returns app record bundleName.
101      */
102     const std::string &GetBundleName() const;
103 
104     /**
105      * @brief Obtains the app record isLauncherApp flag.
106      *
107      * @return Returns app record isLauncherApp flag.
108      */
109     bool IsLauncherApp() const;
110 
111     /**
112      * @brief Obtains the app record id.
113      *
114      * @return Returns app record id.
115      */
116     int32_t GetRecordId() const;
117 
118     /**
119      * @brief Obtains the app name.
120      *
121      * @return Returns the app name.
122      */
123     const std::string &GetName() const;
124 
125     /**
126      * @brief Obtains the process name.
127      *
128      * @return Returns the process name.
129      */
130     const std::string &GetProcessName() const;
131 
132     /**
133      * @brief Obtains the sign code.
134      *
135      * @return Returns the sign code.
136      */
137     const std::string &GetSignCode() const;
138 
139     /**
140      * @brief Setting the sign code.
141      *
142      * @param code, the sign code.
143      */
144     void SetSignCode(const std::string &signCode);
145 
146     /**
147      * @brief Obtains the jointUserId.
148      *
149      * @return Returns the jointUserId.
150      */
151     const std::string &GetJointUserId() const;
152 
153     /**
154      * @brief Setting the jointUserId.
155      *
156      * @param jointUserId, the jointUserId.
157      */
158     void SetJointUserId(const std::string &jointUserId);
159 
160     /**
161      * @brief Obtains the application uid.
162      *
163      * @return Returns the application uid.
164      */
165     int32_t GetUid() const;
166 
167     /**
168      * @brief Setting the application uid.
169      *
170      * @param state, the application uid.
171      */
172     void SetUid(const int32_t uid);
173 
174     // Get current state for this process
175 
176     /**
177      * @brief Obtains the application state.
178      *
179      * @return Returns the application state.
180      */
181     ApplicationState GetState() const;
182 
183     // Set current state for this process
184 
185     /**
186      * @brief Setting the application state.
187      *
188      * @param state, the application state.
189      */
190     void SetState(const ApplicationState state);
191 
192     // Get abilities_ for this process
193     /**
194      * @brief Obtains the abilities info for the application record.
195      *
196      * @return Returns the abilities info for the application record.
197      */
198     const std::map<const sptr<IRemoteObject>, std::shared_ptr<AbilityRunningRecord>> GetAbilities();
199     // Update appThread with appThread
200 
201     /**
202      * @brief Setting the application client.
203      *
204      * @param thread, the application client.
205      */
206     void SetApplicationClient(const sptr<IAppScheduler> &thread);
207 
208     /**
209      * @brief Obtains the application client.
210      *
211      * @return Returns the application client.
212      */
213     sptr<IAppScheduler> GetApplicationClient() const;
214 
215     void AddModule(const std::shared_ptr<ApplicationInfo> &appInfo, const std::shared_ptr<AbilityInfo> &abilityInfo,
216         const sptr<IRemoteObject> &token, const HapModuleInfo &hapModuleInfo,
217         const std::shared_ptr<AAFwk::Want> &want);
218 
219     void AddModules(const std::shared_ptr<ApplicationInfo> &appInfo, const std::vector<HapModuleInfo> &moduleInfos);
220 
221     std::shared_ptr<ModuleRunningRecord> GetModuleRecordByModuleName(
222         const std::string bundleName, const std::string &moduleName);
223 
224     std::shared_ptr<ModuleRunningRecord> GetModuleRunningRecordByToken(const sptr<IRemoteObject> &token) const;
225 
226     std::shared_ptr<ModuleRunningRecord> GetModuleRunningRecordByTerminateLists(const sptr<IRemoteObject> &token) const;
227 
228     std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecord(const int64_t eventId) const;
229 
230     /**
231      * @brief Setting the Trim Memory Level.
232      *
233      * @param level, the Memory Level.
234      */
235     void SetTrimMemoryLevel(int32_t level);
236 
237     // Kill this process with a given reason
238     /**
239      * ForceKillApp, Kill this process with a given reason.
240      *
241      * @param reason, The reason to kill the process.
242      *
243      * @return
244      */
245     void ForceKillApp(const std::string &reason) const;
246 
247     // Schedule to crash this app with a given description
248     /**
249      * ScheduleAppCrash, Schedule to crash this app with a given description.
250      *
251      * @param description, the given description.
252      *
253      * @return
254      */
255     void ScheduleAppCrash(const std::string &description) const;
256 
257     /**
258      * LaunchApplication, Notify application to launch application.
259      *
260      * @return
261      */
262     void LaunchApplication(const Configuration &config);
263 
264     /**
265      * AddAbilityStage, Notify application to ability stage.
266      *
267      * @return
268      */
269     void AddAbilityStage();
270 
271     void AddAbilityStageBySpecifiedAbility(const std::string &bundleName);
272 
273     /**
274      * AddAbilityStage Result returned.
275      *
276      * @return
277      */
278     void AddAbilityStageDone();
279 
280     /**
281      * update the application info after new module installed.
282      *
283      * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext.
284      *
285      * @return
286      */
287     void UpdateApplicationInfoInstalled(const ApplicationInfo &appInfo);
288 
289     /**
290      * LaunchAbility, Notify application to launch ability.
291      *
292      * @param ability, the ability record.
293      *
294      * @return
295      */
296     void LaunchAbility(const std::shared_ptr<AbilityRunningRecord> &ability);
297 
298     /**
299      * LaunchPendingAbilities, Launch Pending Abilities.
300      *
301      * @return
302      */
303     void LaunchPendingAbilities();
304 
305     /**
306      * LowMemoryWarning, Low memory warning.
307      *
308      * @return
309      */
310     void LowMemoryWarning();
311 
312     /**
313      * ScheduleTerminate, Notify application to terminate.
314      *
315      * @return
316      */
317     void ScheduleTerminate();
318 
319     /**
320      * ScheduleTerminate, Notify application process exit safely.
321      *
322      * @return
323      */
324     void ScheduleProcessSecurityExit();
325 
326     /**
327      * ScheduleTrimMemory, Notifies the application of the memory seen.
328      *
329      * @return
330      */
331     void ScheduleTrimMemory();
332 
333     /**
334      * ScheduleMemoryLevel, Notifies the application of the current memory.
335      *
336      * @return
337      */
338     void ScheduleMemoryLevel(int32_t level);
339 
340     /**
341      * GetAbilityRunningRecordByToken, Obtaining the ability record through token.
342      *
343      * @param token, the unique identification to the ability.
344      *
345      * @return
346      */
347     std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecordByToken(const sptr<IRemoteObject> &token) const;
348 
349     std::shared_ptr<AbilityRunningRecord> GetAbilityByTerminateLists(const sptr<IRemoteObject> &token) const;
350 
351     /**
352      * UpdateAbilityState, update the ability status.
353      *
354      * @param token, the unique identification to update the ability.
355      * @param state, ability status that needs to be updated.
356      *
357      * @return
358      */
359     void UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state);
360 
361     /**
362      * PopForegroundingAbilityTokens, Extract the token record from the foreground tokens list.
363      *
364      * @return
365      */
366     void PopForegroundingAbilityTokens();
367 
368     /**
369      * TerminateAbility, terminate the token ability.
370      *
371      * @param token, he unique identification to terminate the ability.
372      *
373      * @return
374      */
375     void TerminateAbility(const sptr<IRemoteObject> &token, const bool isForce);
376 
377     /**
378      * AbilityTerminated, terminate the ability.
379      *
380      * @param token, the unique identification to terminated the ability.
381      *
382      * @return
383      */
384     void AbilityTerminated(const sptr<IRemoteObject> &token);
385 
386     /**
387      * @brief Setting application service internal handler instance.
388      *
389      * @param serviceInner, application service internal handler instance.
390      */
391     void SetAppMgrServiceInner(const std::weak_ptr<AppMgrServiceInner> &inner);
392 
393     /**
394      * @brief Setting application death recipient.
395      *
396      * @param appDeathRecipient, application death recipient instance.
397      */
398     void SetAppDeathRecipient(const sptr<AppDeathRecipient> &appDeathRecipient);
399 
400     /**
401      * RegisterAppDeathRecipient, Register application death recipient.
402      *
403      * @return
404      */
405     void RegisterAppDeathRecipient() const;
406 
407     /**
408      * @brief Obtains application priority info.
409      *
410      * @return Returns the application priority info.
411      */
412     std::shared_ptr<PriorityObject> GetPriorityObject();
413 
414     /**
415      * RegisterAppDeathRecipient, Remove application death recipient record.
416      *
417      * @return
418      */
419     void RemoveAppDeathRecipient() const;
420 
421     /**
422     *  Notify application update system environment changes.
423     *
424     * @param config System environment change parameters.
425     * @return Returns ERR_OK on success, others on failure.
426     */
427     int32_t UpdateConfiguration(const Configuration &config);
428 
429     void SetEventHandler(const std::shared_ptr<AMSEventHandler> &handler);
430 
431     int64_t GetEventId() const;
432 
433     bool IsLastAbilityRecord(const sptr<IRemoteObject> &token);
434 
435     bool IsLastPageAbilityRecord(const sptr<IRemoteObject> &token);
436 
437     void SetTerminating();
438 
439     bool IsTerminating();
440 
441     bool IsKeepAliveApp() const;
442 
443     bool IsEmptyKeepAliveApp() const;
444 
445     void SetKeepAliveAppState(bool isKeepAlive, bool isEmptyKeepAliveApp);
446 
447     void SetEmptyKeepAliveAppState(bool isEmptyKeepAlive);
448 
449     void SetStageModelState(bool isStageBasedModel);
450 
451     std::list<std::shared_ptr<ModuleRunningRecord>> GetAllModuleRecord() const;
452 
453     const std::list<std::shared_ptr<ApplicationInfo>> GetAppInfoList();
454 
GetApplicationInfo()455     inline const std::shared_ptr<ApplicationInfo> GetApplicationInfo()
456     {
457         return appInfo_;
458     }
459 
460     void SetRestartResidentProcCount(int count);
461     void DecRestartResidentProcCount();
462     int GetRestartResidentProcCount() const;
463     bool CanRestartResidentProc();
464 
465     /**
466      * Notify observers when state change.
467      *
468      * @param ability, ability or extension record.
469      * @param state, ability or extension state.
470      */
471     void StateChangedNotifyObserver(
472         const std::shared_ptr<AbilityRunningRecord> &ability, int32_t state, bool isAbility);
473 
474     void insertAbilityStageInfo(std::vector<HapModuleInfo> moduleInfos);
475 
476     void GetBundleNames(std::vector<std::string> &bundleNames);
477 
478     void SetUserTestInfo(const std::shared_ptr<UserTestRecord> &record);
479     std::shared_ptr<UserTestRecord> GetUserTestInfo();
480 
481     void SetSpecifiedAbilityFlagAndWant(const bool flag, const AAFwk::Want &want, const std::string &moduleName);
482     bool IsStartSpecifiedAbility() const;
483     void ScheduleAcceptWant(const std::string &moduleName);
484     void ScheduleAcceptWantDone();
485     void ApplicationTerminated();
486     const AAFwk::Want &GetSpecifiedWant() const;
487     void SetDebugApp(bool isDebugApp);
488     bool IsDebugApp();
489     void SetRenderRecord(const std::shared_ptr<RenderRecord> &record);
490     std::shared_ptr<RenderRecord> GetRenderRecord();
491     void SetStartMsg(const AppSpawnStartMsg &msg);
492     AppSpawnStartMsg GetStartMsg();
493 
494     void SendEventForSpecifiedAbility(uint32_t msg, int64_t timeOut);
495 
496     void SetKilling();
497     bool IsKilling() const;
498     void SetAppIndex(const int32_t appIndex);
499     int32_t GetAppIndex() const;
500     void SetSecurityFlag(bool securityFlag);
501     bool GetSecurityFlag() const;
502 
503     using Closure = std::function<void()>;
504     void PostTask(std::string msg, int64_t timeOut, const Closure &task);
505     void RemoveTerminateAbilityTimeoutTask(const sptr<IRemoteObject>& token) const;
506 
507     int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback,
508         const int32_t recordId);
509 
510     int32_t NotifyHotReloadPage(const sptr<IQuickFixCallback> &callback, const int32_t recordId);
511 
512     int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback,
513         const int32_t recordId);
514 
515     bool IsContinuousTask();
516 
517     void SetContinuousTaskAppState(bool isContinuousTask);
518 
519     /**
520      * Update target ability focus state.
521      *
522      * @param token the token of target ability.
523      * @param isFocus focus state.
524      *
525      * @return true if process focus state changed, false otherwise.
526      */
527     bool UpdateAbilityFocusState(const sptr<IRemoteObject> &token, bool isFocus);
528 
529     bool GetFocusFlag() const;
530 
531     int64_t GetAppStartTime() const;
532 
533     void SetRestartTimeMillis(const int64_t restartTimeMills);
534 
535     void SetRequestProcCode(int32_t requestProcCode);
536 
537     int32_t GetRequestProcCode() const;
538 
539     void SetProcessChangeReason(ProcessChangeReason reason);
540 
541     ProcessChangeReason GetProcessChangeReason() const;
542 
543 private:
544     /**
545      * SearchTheModuleInfoNeedToUpdated, Get an uninitialized abilityStage data.
546      *
547      * @return If an uninitialized data is found return true,Otherwise return false.
548      */
549     bool GetTheModuleInfoNeedToUpdated(const std::string bundleName, HapModuleInfo &info);
550 
551     // drive application state changes when ability state changes.
552     /**
553      * ScheduleForegroundRunning, Notify application to switch to foreground.
554      *
555      * @return
556      */
557     void ScheduleForegroundRunning();
558 
559     /**
560      * ScheduleBackgroundRunning, Notify application to switch to background.
561      *
562      * @return
563      */
564     void ScheduleBackgroundRunning();
565 
566     /**
567      * AbilityForeground, Handling the ability process when switching to the foreground.
568      *
569      * @param ability, the ability info.
570      *
571      * @return
572      */
573     void AbilityForeground(const std::shared_ptr<AbilityRunningRecord> &ability);
574 
575     /**
576      * AbilityBackground, Handling the ability process when switching to the background.
577      *
578      * @param ability, the ability info.
579      *
580      * @return
581      */
582     void AbilityBackground(const std::shared_ptr<AbilityRunningRecord> &ability);
583     // drive application state changes when ability state changes.
584 
585     bool AbilityFocused(const std::shared_ptr<AbilityRunningRecord> &ability);
586 
587     bool AbilityUnfocused(const std::shared_ptr<AbilityRunningRecord> &ability);
588 
589     void SendEvent(uint32_t msg, int64_t timeOut);
590 
591     void RemoveModuleRecord(const std::shared_ptr<ModuleRunningRecord> &record);
592 
593 private:
594     bool isKeepAliveApp_ = false;  // Only resident processes can be set to true, please choose carefully
595     bool isEmptyKeepAliveApp_ = false;  // Only empty resident processes can be set to true, please choose carefully
596     bool isStageBasedModel_ = false;
597     ApplicationState curState_ = ApplicationState::APP_STATE_CREATE;  // current state of this process
598     bool isFocused_ = false; // if process is focused.
599 
600     std::shared_ptr<ApplicationInfo> appInfo_ = nullptr;  // the application's info of this process
601     int32_t appRecordId_ = 0;
602     std::string appName_;
603     std::string processName_;  // the name of this process
604     int64_t eventId_ = 0;
605     std::list<const sptr<IRemoteObject>> foregroundingAbilityTokens_;
606     std::weak_ptr<AppMgrServiceInner> appMgrServiceInner_;
607     sptr<AppDeathRecipient> appDeathRecipient_ = nullptr;
608     std::shared_ptr<PriorityObject> priorityObject_ = nullptr;
609     std::shared_ptr<AppLifeCycleDeal> appLifeCycleDeal_ = nullptr;
610     std::shared_ptr<AMSEventHandler> eventHandler_ = nullptr;
611     bool isTerminating = false;
612     std::string signCode_;  // the sign of this hap
613     std::string jointUserId_;
614     std::map<std::string, std::shared_ptr<ApplicationInfo>> appInfos_;
615     std::mutex appInfosLock_;
616     std::map<std::string, std::vector<std::shared_ptr<ModuleRunningRecord>>> hapModules_;
617     mutable std::mutex hapModulesLock_;
618     int32_t mainUid_;
619     std::string mainBundleName_;
620     bool isLauncherApp_;
621     std::string mainAppName_;
622     int restartResidentProcCount_ = 0;
623     bool isSpecifiedAbility_ = false;
624     AAFwk::Want SpecifiedWant_;
625     std::string moduleName_;
626     bool isDebugApp_ = false;
627     int64_t startTimeMillis_ = 0;   // The time of app start(CLOCK_MONOTONIC)
628     int64_t restartTimeMillis_ = 0; // The time of last trying app restart
629 
630     std::shared_ptr<UserTestRecord> userTestRecord_ = nullptr;
631 
632     bool isKilling_ = false;
633     bool isContinuousTask_ = false;    // Only continuesTask processes can be set to true, please choose carefully
634 
635     // render record
636     std::shared_ptr<RenderRecord> renderRecord_ = nullptr;
637     AppSpawnStartMsg startMsg_;
638     int32_t appIndex_ = 0;
639     bool securityFlag_ = false;
640     int32_t requestProcCode_ = 0;
641     ProcessChangeReason processChangeReason_ = ProcessChangeReason::REASON_NONE;
642 };
643 }  // namespace AppExecFwk
644 }  // namespace OHOS
645 #endif  // OHOS_ABILITY_RUNTIME_APP_RUNNING_RECORD_H
646