• 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 FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_RUNNING_RECORD_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_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 namespace {
42 const int RESTART_RESIDENT_PROCESS_MAX_TIMES = 15;
43 }
44 class AbilityRunningRecord;
45 class AppMgrServiceInner;
46 class AppRunningRecord;
47 
48 /**
49  * @class RenderRecord
50  * Record nweb render process info.
51  */
52 class RenderRecord {
53 public:
54     RenderRecord(pid_t hostPid, const std::string& renderParam,
55         int32_t ipcFd, int32_t sharedFd, const std::shared_ptr<AppRunningRecord> &host);
56 
57     virtual ~RenderRecord();
58 
59     static std::shared_ptr<RenderRecord> CreateRenderRecord(pid_t hostPid, const std::string& renderParam,
60         int32_t ipcFd, int32_t sharedFd, const std::shared_ptr<AppRunningRecord> &host);
61 
62     void SetPid(pid_t pid);
63     pid_t GetPid();
64     pid_t GetHostPid();
65     std::string GetRenderParam();
66     int32_t GetIpcFd();
67     int32_t GetSharedFd();
68     std::shared_ptr<AppRunningRecord> GetHostRecord();
69     sptr<IRenderScheduler> GetScheduler();
70     void SetScheduler(const sptr<IRenderScheduler> &scheduler);
71     void SetDeathRecipient(const sptr<AppDeathRecipient> recipient);
72     void RegisterDeathRecipient();
73 
74 private:
75     pid_t pid_ = 0;
76     pid_t hostPid_ = 0;
77     std::string renderParam_;
78     int32_t ipcFd_ = 0;
79     int32_t sharedFd_ = 0;
80     std::weak_ptr<AppRunningRecord> host_; // nweb host
81     sptr<IRenderScheduler> renderScheduler_;
82     sptr<AppDeathRecipient> deathRecipient_ = nullptr;
83 };
84 
85 class AppRunningRecord : public std::enable_shared_from_this<AppRunningRecord> {
86 public:
87     static int64_t appEventId_;
88 public:
89     AppRunningRecord(
90         const std::shared_ptr<ApplicationInfo> &info, const int32_t recordId, const std::string &processName);
91     virtual ~AppRunningRecord() = default;
92 
93     /**
94      * @brief Obtains the app record bundleName.
95      *
96      * @return Returns app record bundleName.
97      */
98     const std::string &GetBundleName() const;
99 
100     /**
101      * @brief Obtains the app record isLauncherApp flag.
102      *
103      * @return Returns app record isLauncherApp flag.
104      */
105     bool IsLauncherApp() const;
106 
107     /**
108      * @brief Obtains the app record id.
109      *
110      * @return Returns app record id.
111      */
112     int32_t GetRecordId() const;
113 
114     /**
115      * @brief Obtains the app name.
116      *
117      * @return Returns the app name.
118      */
119     const std::string &GetName() const;
120 
121     /**
122      * @brief Obtains the app clone info.
123      *
124      * @return Returns the app isCloned.
125      */
126     bool GetCloneInfo() const;
127 
128     /**
129      * @brief Obtains the process name.
130      *
131      * @return Returns the process name.
132      */
133     const std::string &GetProcessName() const;
134 
135     /**
136      * @brief Obtains the sign code.
137      *
138      * @return Returns the sign code.
139      */
140     const std::string &GetSignCode() const;
141 
142     /**
143      * @brief Setting the sign code.
144      *
145      * @param code, the sign code.
146      */
147     void SetSignCode(const std::string &signCode);
148 
149     /**
150      * @brief Obtains the jointUserId.
151      *
152      * @return Returns the jointUserId.
153      */
154     const std::string &GetJointUserId() const;
155 
156     /**
157      * @brief Setting the jointUserId.
158      *
159      * @param jointUserId, the jointUserId.
160      */
161     void SetJointUserId(const std::string &jointUserId);
162 
163     /**
164      * @brief Obtains the application uid.
165      *
166      * @return Returns the application uid.
167      */
168     int32_t GetUid() const;
169 
170     /**
171      * @brief Setting the application uid.
172      *
173      * @param state, the application uid.
174      */
175     void SetUid(const int32_t uid);
176 
177     // Get current state for this process
178 
179     /**
180      * @brief Obtains the application state.
181      *
182      * @return Returns the application state.
183      */
184     ApplicationState GetState() const;
185 
186     // Set current state for this process
187 
188     /**
189      * @brief Setting the application state.
190      *
191      * @param state, the application state.
192      */
193     void SetState(const ApplicationState state);
194 
195     // Get abilities_ for this process
196     /**
197      * @brief Obtains the abilitys info for the application record.
198      *
199      * @return Returns the abilitys info for the application record.
200      */
201     const std::map<const sptr<IRemoteObject>, std::shared_ptr<AbilityRunningRecord>> GetAbilities();
202     // Update appThread with appThread
203 
204     /**
205      * @brief Setting the application client.
206      *
207      * @param thread, the application client.
208      */
209     void SetApplicationClient(const sptr<IAppScheduler> &thread);
210 
211     /**
212      * @brief Obtains the application client.
213      *
214      * @return Returns the application client.
215      */
216     sptr<IAppScheduler> GetApplicationClient() const;
217 
218     void AddModule(const std::shared_ptr<ApplicationInfo> &appInfo, const std::shared_ptr<AbilityInfo> &abilityInfo,
219         const sptr<IRemoteObject> &token, const HapModuleInfo &hapModuleInfo,
220         const std::shared_ptr<AAFwk::Want> &want);
221 
222     void AddModules(const std::shared_ptr<ApplicationInfo> &appInfo, const std::vector<HapModuleInfo> &moduleInfos);
223 
224     std::shared_ptr<ModuleRunningRecord> GetModuleRecordByModuleName(
225         const std::string bundleName, const std::string &moduleName);
226 
227     std::shared_ptr<ModuleRunningRecord> GetModuleRunningRecordByToken(const sptr<IRemoteObject> &token) const;
228 
229     std::shared_ptr<ModuleRunningRecord> GetModuleRunningRecordByTerminateLists(const sptr<IRemoteObject> &token) const;
230 
231     // It can only used in SINGLETON mode.
232     /**
233      * GetAbilityRunningRecord, Get ability record by the ability Name.
234      *
235      * @param abilityName, the ability name.
236      * @param ownerUserId, the owner userId of this ability.
237      *
238      * @return the ability record.
239      */
240     std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecord(
241         const std::string &abilityName, int32_t ownerUserId = -1) const;
242 
243     std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecord(const int64_t eventId) const;
244 
245     // Clear(remove) the specified ability record from the list
246 
247     /**
248      * ClearAbility, Clear ability record by record info.
249      *
250      * @param record, the ability record.
251      *
252      * @return
253      */
254     void ClearAbility(const std::shared_ptr<AbilityRunningRecord> &record);
255 
256     // Update the trim memory level value of this process
257     /**
258      * @brief Setting the Trim Memory Level.
259      *
260      * @param level, the Memory Level.
261      */
262     void SetTrimMemoryLevel(int32_t level);
263 
264     // Kill this process with a given reason
265     /**
266      * ForceKillApp, Kill this process with a given reason.
267      *
268      * @param reason, The reason to kill the process.
269      *
270      * @return
271      */
272     void ForceKillApp(const std::string &reason) const;
273 
274     // Schedule to crash this app with a given description
275     /**
276      * ScheduleAppCrash, Schedule to crash this app with a given description.
277      *
278      * @param description, the given description.
279      *
280      * @return
281      */
282     void ScheduleAppCrash(const std::string &description) const;
283 
284     /**
285      * LaunchApplication, Notify application to launch application.
286      *
287      * @return
288      */
289     void LaunchApplication(const Configuration &config);
290 
291     /**
292      * AddAbilityStage, Notify application to ability stage.
293      *
294      * @return
295      */
296     void AddAbilityStage();
297 
298     void AddAbilityStageBySpecifiedAbility(const std::string &bundleName);
299 
300     /**
301      * AddAbilityStage Result returned.
302      *
303      * @return
304      */
305     void AddAbilityStageDone();
306 
307     /**
308      * LaunchAbility, Notify application to launch ability.
309      *
310      * @param ability, the ability record.
311      *
312      * @return
313      */
314     void LaunchAbility(const std::shared_ptr<AbilityRunningRecord> &ability);
315 
316     /**
317      * LaunchPendingAbilities, Launch Pending Abilities.
318      *
319      * @return
320      */
321     void LaunchPendingAbilities();
322 
323     /**
324      * LowMemoryWarning, Low memory warning.
325      *
326      * @return
327      */
328     void LowMemoryWarning();
329 
330     /**
331      * ScheduleTerminate, Notify application to terminate.
332      *
333      * @return
334      */
335     void ScheduleTerminate();
336 
337     /**
338      * ScheduleTerminate, Notify application process exit safely.
339      *
340      * @return
341      */
342     void ScheduleProcessSecurityExit();
343 
344     /**
345      * ScheduleTrimMemory, Notifies the application of the memory seen.
346      *
347      * @return
348      */
349     void ScheduleTrimMemory();
350 
351     /**
352      * GetAbilityRunningRecordByToken, Obtaining the ability record through token.
353      *
354      * @param token, the unique identification to the ability.
355      *
356      * @return
357      */
358     std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecordByToken(const sptr<IRemoteObject> &token) const;
359 
360     std::shared_ptr<AbilityRunningRecord> GetAbilityByTerminateLists(const sptr<IRemoteObject> &token) const;
361 
362     /**
363      * UpdateAbilityState, update the ability status.
364      *
365      * @param token, the unique identification to update the ability.
366      * @param state, ability status that needs to be updated.
367      *
368      * @return
369      */
370     void UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state);
371 
372     /**
373      * PopForegroundingAbilityTokens, Extract the token record from the foreground tokens list.
374      *
375      * @return
376      */
377     void PopForegroundingAbilityTokens();
378 
379     /**
380      * TerminateAbility, terminate the token ability.
381      *
382      * @param token, he unique identification to terminate the ability.
383      *
384      * @return
385      */
386     void TerminateAbility(const sptr<IRemoteObject> &token, const bool isForce);
387 
388     /**
389      * AbilityTerminated, terminate the ability.
390      *
391      * @param token, the unique identification to terminated the ability.
392      *
393      * @return
394      */
395     void AbilityTerminated(const sptr<IRemoteObject> &token);
396 
397     /**
398      * @brief Setting application service internal handler instance.
399      *
400      * @param serviceInner, application service internal handler instance.
401      */
402     void SetAppMgrServiceInner(const std::weak_ptr<AppMgrServiceInner> &inner);
403 
404     /**
405      * @brief Setting application death recipient.
406      *
407      * @param appDeathRecipient, application death recipient instance.
408      */
409     void SetAppDeathRecipient(const sptr<AppDeathRecipient> &appDeathRecipient);
410 
411     /**
412      * RegisterAppDeathRecipient, Register application death recipient.
413      *
414      * @return
415      */
416     void RegisterAppDeathRecipient() const;
417 
418     /**
419      * @brief Obtains application priority info.
420      *
421      * @return Returns the application priority info.
422      */
423     std::shared_ptr<PriorityObject> GetPriorityObject();
424 
425     /**
426      * RegisterAppDeathRecipient, Remove application death recipient record.
427      *
428      * @return
429      */
430     void RemoveAppDeathRecipient() const;
431 
432     /**
433     *  Notify application update system environment changes.
434     *
435     * @param config System environment change parameters.
436     * @return
437     */
438     void UpdateConfiguration(const Configuration &config);
439 
440     void SetEventHandler(const std::shared_ptr<AMSEventHandler> &handler);
441 
442     int64_t GetEventId() const;
443 
444     bool IsLastAbilityRecord(const sptr<IRemoteObject> &token);
445 
446     void SetTerminating();
447 
448     bool IsTerminating();
449 
450     bool IsKeepAliveApp() const;
451 
452     // Please use with caution, it may affect the ability to start.
453     void SetKeepAliveAppState(bool isKeepAlive, bool isNewMission);
454 
455     std::list<std::shared_ptr<ModuleRunningRecord>> GetAllModuleRecord() const;
456 
457     std::map<std::string, std::vector<std::shared_ptr<ModuleRunningRecord>>> &GetModules();
458 
459     const std::list<std::shared_ptr<ApplicationInfo>> GetAppInfoList();
460 
GetApplicationInfo()461     inline const std::shared_ptr<ApplicationInfo> GetApplicationInfo()
462     {
463         return appInfo_;
464     }
465 
466     void SetRestartResidentProcCount(int count);
467     void DecRestartResidentProcCount();
468     int GetRestartResidentProcCount() const;
469     bool CanRestartResidentProc();
470 
471     /**
472      * Notify observers when state change.
473      *
474      * @param ability, ability or extension record.
475      * @param state, ability or extension state.
476      */
477     void StateChangedNotifyObserver(const std::shared_ptr<AbilityRunningRecord> &ability, int32_t state, bool isAbility);
478 
479     void insertAbilityStageInfo(std::vector<HapModuleInfo> moduleInfos);
480 
481     void GetBundleNames(std::vector<std::string> &bundleNames);
482 
483     void SetUserTestInfo(const std::shared_ptr<UserTestRecord> &record);
484     std::shared_ptr<UserTestRecord> GetUserTestInfo();
485 
486     void SetSpecifiedAbilityFlagAndWant(const bool flag, const AAFwk::Want &want, const std::string &moduleName);
487     bool IsStartSpecifiedAbility() const;
488     void ScheduleAcceptWant(const std::string &moduleName);
489     void ScheduleAcceptWantDone();
490     void ApplicationTerminated();
491     const AAFwk::Want &GetSpecifiedWant() const;
492     void SetDebugApp(bool isDebugApp);
493     void SetRenderRecord(const std::shared_ptr<RenderRecord> &record);
494     std::shared_ptr<RenderRecord> GetRenderRecord();
495     void SetStartMsg(const AppSpawnStartMsg &msg);
496     AppSpawnStartMsg GetStartMsg();
497 
498     void SendEventForSpecifiedAbility(uint32_t msg, int64_t timeOut);
499 
500     void SetKilling();
501     bool IsKilling() const;
502 private:
503     /**
504      * SearchTheModuleInfoNeedToUpdated, Get an uninitialized abilitystage data.
505      *
506      * @return If an uninitialized data is found return true,Otherwise return false.
507      */
508     bool GetTheModuleInfoNeedToUpdated(const std::string bundleName, HapModuleInfo &info);
509 
510     // drive application state changes when ability state changes.
511     /**
512      * ScheduleForegroundRunning, Notify application to switch to foreground.
513      *
514      * @return
515      */
516     void ScheduleForegroundRunning();
517 
518     /**
519      * ScheduleBackgroundRunning, Notify application to switch to background.
520      *
521      * @return
522      */
523     void ScheduleBackgroundRunning();
524 
525     /**
526      * AbilityForeground, Handling the ability process when switching to the foreground.
527      *
528      * @param ability, the ability info.
529      *
530      * @return
531      */
532     void AbilityForeground(const std::shared_ptr<AbilityRunningRecord> &ability);
533 
534     /**
535      * AbilityBackground, Handling the ability process when switching to the background.
536      *
537      * @param ability, the ability info.
538      *
539      * @return
540      */
541     void AbilityBackground(const std::shared_ptr<AbilityRunningRecord> &ability);
542     // drive application state changes when ability state changes.
543 
544     void SendEvent(uint32_t msg, int64_t timeOut);
545 
546     void RemoveModuleRecord(const std::shared_ptr<ModuleRunningRecord> &record);
547 
548 private:
549     bool isKeepAliveApp_ = false;  // Only resident processes can be set to true, please choose carefully
550     bool isNewMission_ = false;
551     ApplicationState curState_ = ApplicationState::APP_STATE_CREATE;  // current state of this process
552 
553     std::shared_ptr<ApplicationInfo> appInfo_ = nullptr;  // the application's info of this process
554     int32_t appRecordId_ = 0;
555     std::string appName_;
556     std::string processName_;  // the name of this process
557     int64_t eventId_ = 0;
558     std::list<const sptr<IRemoteObject>> foregroundingAbilityTokens_;
559     std::weak_ptr<AppMgrServiceInner> appMgrServiceInner_;
560     sptr<AppDeathRecipient> appDeathRecipient_ = nullptr;
561     std::shared_ptr<PriorityObject> priorityObject_ = nullptr;
562     std::shared_ptr<AppLifeCycleDeal> appLifeCycleDeal_ = nullptr;
563     std::shared_ptr<AMSEventHandler> eventHandler_ = nullptr;
564     bool isTerminating = false;
565     std::string signCode_;  // the sign of this hap
566     std::string jointUserId_;
567     std::map<std::string, std::shared_ptr<ApplicationInfo>> appInfos_;
568     std::map<std::string, std::vector<std::shared_ptr<ModuleRunningRecord>>> hapModules_;
569     int32_t mainUid_;
570     std::string mainBundleName_;
571     bool isLauncherApp_;
572     bool isClonedApp_;
573     std::string mainAppName_;
574     int restartResidentProcCount_ = RESTART_RESIDENT_PROCESS_MAX_TIMES;
575     bool isSpecifiedAbility_ = false;
576     AAFwk::Want SpecifiedWant_;
577     std::string moduleName_;
578     bool isDebugApp_ = false;
579 
580     std::shared_ptr<UserTestRecord> userTestRecord_ = nullptr;
581 
582     bool isKilling_ = false;
583 
584     // render record
585     std::shared_ptr<RenderRecord> renderRecord_ = nullptr;
586     AppSpawnStartMsg startMsg_;
587 };
588 }  // namespace AppExecFwk
589 }  // namespace OHOS
590 #endif  // FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_RUNNING_RECORD_H
591