• 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_MODULE_RUNNING_RECORD_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_MODULE_RUNNING_RECORD_H
18 
19 #include <string>
20 #include <list>
21 #include <map>
22 #include <memory>
23 #include "iremote_object.h"
24 
25 #include "ability_info.h"
26 #include "application_info.h"
27 #include "ability_running_record.h"
28 #include "app_mgr_constants.h"
29 #include "app_lifecycle_deal.h"
30 #include "app_mgr_service_event_handler.h"
31 
32 namespace OHOS {
33 namespace AppExecFwk {
34 enum class ModuleRecordState {
35     UNKNOWN_STATE,
36     INITIALIZED_STATE,
37     RUNNING_STATE,
38 };
39 
40 class AppMgrServiceInner;
41 class ModuleRunningRecord {
42 public:
43     ModuleRunningRecord(
44         const std::shared_ptr<ApplicationInfo> &info, const std::shared_ptr<AMSEventHandler> &eventHandler);
45     virtual ~ModuleRunningRecord();
46 
47     /**
48      * @brief Obtains module moduleName.
49      *
50      * @return Returns module moduleName.
51      */
52     const std::string &GetModuleName() const;
53 
54     /**
55      * @param name, the module mainability name.
56      *
57      * @return
58      */
59     void GetMainAbilityName(const std::string &name);
60 
61     void Init(const HapModuleInfo &info);
62 
63     const HapModuleInfo GetModuleInfo();
64 
65     /**
66      * GetAbilityRunningRecordByToken, Obtaining the ability record through token.
67      *
68      * @param token, the unique identification to the ability.
69      *
70      * @return
71      */
72     std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecordByToken(const sptr<IRemoteObject> &token) const;
73 
74     std::shared_ptr<AbilityRunningRecord> AddAbility(const sptr<IRemoteObject> &token,
75         const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<AAFwk::Want> &want);
76 
77     bool IsLastAbilityRecord(const sptr<IRemoteObject> &token);
78 
79     // Get abilities_ for this process
80     /**
81      * @brief Obtains the abilitys info for the application record.
82      *
83      * @return Returns the abilitys info for the application record.
84      */
85     const std::map<const sptr<IRemoteObject>, std::shared_ptr<AbilityRunningRecord>> &GetAbilities() const;
86 
87     std::shared_ptr<AbilityRunningRecord> GetAbilityByTerminateLists(const sptr<IRemoteObject> &token) const;
88 
89     // Clear(remove) the specified ability record from the list
90 
91     /**
92      * ClearAbility, Clear ability record by record info.
93      *
94      * @param record, the ability record.
95      *
96      * @return
97      */
98     void ClearAbility(const std::shared_ptr<AbilityRunningRecord> &record);
99     // It can only used in SINGLETON mode.
100     /**
101      * GetAbilityRunningRecord, Get ability record by the ability Name.
102      *
103      * @param abilityName, the ability name.
104      * @param ownerUserId, the owner userId of this ability.
105      *
106      * @return the ability record.
107      */
108     std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecord(
109         const std::string &abilityName, int32_t ownerUserId = -1) const;
110 
111     std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecord(const int64_t eventId) const;
112 
113     /**
114      * LaunchAbility, Notify application to launch ability.
115      *
116      * @param ability, the ability record.
117      *
118      * @return
119      */
120     void LaunchAbility(const std::shared_ptr<AbilityRunningRecord> &ability);
121 
122     /**
123      * LaunchPendingAbilities, Launch Pending Abilities.
124      *
125      * @return
126      */
127     void LaunchPendingAbilities();
128 
129     /**
130      * TerminateAbility, terminate the token ability.
131      *
132      * @param token, he unique identification to terminate the ability.
133      *
134      * @return
135      */
136     void TerminateAbility(const sptr<IRemoteObject> &token, const bool isForce);
137 
138     /**
139      * AbilityTerminated, terminate the ability.
140      *
141      * @param token, the unique identification to terminated the ability.
142      *
143      * @return
144      */
145     void AbilityTerminated(const sptr<IRemoteObject> &token);
146 
147     /**
148      * @brief Setting application service internal handler instance.
149      *
150      * @param serviceInner, application service internal handler instance.
151      */
152     void SetAppMgrServiceInner(const std::weak_ptr<AppMgrServiceInner> &inner);
153 
154     // drive application state changes when ability state changes.
155     /**
156      * OnAbilityStateChanged, Call ability state change.
157      *
158      * @param ability, the ability info.
159      * @param state, the ability state.
160      *
161      * @return
162      */
163     void OnAbilityStateChanged(const std::shared_ptr<AbilityRunningRecord> &ability, const AbilityState state);
164 
165     ModuleRecordState GetModuleRecordState();
166 
167     void SetModuleRecordState(const ModuleRecordState &state);
168 
169     void GetHapModuleInfo(HapModuleInfo &info);
170 
171     void SetApplicationClient(std::shared_ptr<AppLifeCycleDeal> &appLifeCycleDeal);
172 
173     const std::shared_ptr<ApplicationInfo> GetAppInfo();
174 
175 private:
176     void SendEvent(uint32_t msg, int64_t timeOut, const std::shared_ptr<AbilityRunningRecord> &abilityRecord);
177 
178     ModuleRecordState GetState() const;
179 
180 private:
181     std::map<const sptr<IRemoteObject>, std::shared_ptr<AbilityRunningRecord>> abilities_;
182     std::map<const sptr<IRemoteObject>, std::shared_ptr<AbilityRunningRecord>> terminateAbilitys_;
183     std::weak_ptr<AppMgrServiceInner> appMgrServiceInner_;
184     std::shared_ptr<AppLifeCycleDeal> appLifeCycleDeal_;
185     std::shared_ptr<ApplicationInfo> appInfo_;  // the application's info
186     std::shared_ptr<AMSEventHandler> eventHandler_;
187     HapModuleInfo owenInfo_;
188     ModuleRecordState owenState_ = ModuleRecordState::UNKNOWN_STATE;
189 };
190 }  // namespace AppExecFwk
191 }  // namespace OHOS
192 #endif  // FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_MODULE_RUNNING_RECORD_H
193