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