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, int32_t appIndex, std::weak_ptr<AppMgrServiceInner> appMgrService, 65 std::shared_ptr<AppLifeCycleDeal> appLifeCycleDeal); SetAppIndex(int32_t appIndex)66 void SetAppIndex(int32_t appIndex) 67 { 68 appIndex_ = appIndex; 69 } 70 71 /** 72 * GetAbilityRunningRecordByToken, Obtaining the ability record through token. 73 * 74 * @param token, the unique identification to the ability. 75 * 76 * @return 77 */ 78 std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecordByToken(const sptr<IRemoteObject> &token) const; 79 80 std::shared_ptr<AbilityRunningRecord> AddAbility(sptr<IRemoteObject> token, 81 std::shared_ptr<AbilityInfo> abilityInfo, std::shared_ptr<AAFwk::Want> want, int32_t abilityRecordId); 82 83 bool IsLastAbilityRecord(const sptr<IRemoteObject> &token); 84 85 int32_t GetPageAbilitySize(); 86 87 bool ExtensionAbilityRecordExists(); 88 89 // Get abilities_ for this process 90 /** 91 * @brief Obtains the abilities info for the application record. 92 * 93 * @return Returns the abilities info for the application record. 94 */ 95 const std::map<const sptr<IRemoteObject>, std::shared_ptr<AbilityRunningRecord>> GetAbilities() const; 96 97 std::shared_ptr<AbilityRunningRecord> GetAbilityByTerminateLists(const sptr<IRemoteObject> &token) const; 98 99 std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecord(const int64_t eventId) const; 100 101 /** 102 * LaunchAbility, Notify application to launch ability. 103 * 104 * @param ability, the ability record. 105 * 106 * @return 107 */ 108 void LaunchAbility(const std::shared_ptr<AbilityRunningRecord> &ability); 109 110 /** 111 * LaunchPendingAbilities, Launch Pending Abilities. 112 * 113 * @return 114 */ 115 void LaunchPendingAbilities(); 116 117 /** 118 * TerminateAbility, terminate the token ability. 119 * 120 * @param token, he unique identification to terminate the ability. 121 * 122 * @return 123 */ 124 void TerminateAbility(const std::shared_ptr<AppRunningRecord> &appRecord, 125 const sptr<IRemoteObject> &token, const bool isForce); 126 127 /** 128 * AbilityTerminated, terminate the ability. 129 * 130 * @param token, the unique identification to terminated the ability. 131 * 132 * @return 133 */ 134 void AbilityTerminated(const sptr<IRemoteObject> &token); 135 136 /** 137 * @brief Setting application service internal handler instance. 138 * 139 * @param serviceInner, application service internal handler instance. 140 */ 141 void SetAppMgrServiceInner(const std::weak_ptr<AppMgrServiceInner> &inner); 142 143 // drive application state changes when ability state changes. 144 /** 145 * OnAbilityStateChanged, Call ability state change. 146 * 147 * @param ability, the ability info. 148 * @param state, the ability state. 149 * 150 * @return 151 */ 152 void OnAbilityStateChanged(const std::shared_ptr<AbilityRunningRecord> &ability, const AbilityState state); 153 154 ModuleRecordState GetModuleRecordState(); 155 156 void SetModuleRecordState(const ModuleRecordState &state); 157 158 void GetHapModuleInfo(HapModuleInfo &info); 159 160 void SetApplicationClient(std::shared_ptr<AppLifeCycleDeal> &appLifeCycleDeal); 161 162 const std::shared_ptr<ApplicationInfo> GetAppInfo(); 163 164 bool RemoveTerminateAbilityTimeoutTask(const sptr<IRemoteObject>& token) const; 165 166 bool IsAbilitiesBackgrounded(); 167 168 bool IsAllAbilityReadyToCleanedByUserRequest(); 169 SetLoaded()170 void SetLoaded() 171 { 172 isLoaded_ = true; 173 } 174 IsLoaded()175 bool IsLoaded() const 176 { 177 return isLoaded_; 178 } 179 private: 180 void SendEvent(uint32_t msg, int64_t timeOut, const std::shared_ptr<AbilityRunningRecord> &abilityRecord); 181 182 ModuleRecordState GetState() const; 183 184 private: 185 bool isLoaded_ = false; 186 int32_t appIndex_ = 0; 187 ModuleRecordState owenState_ = ModuleRecordState::UNKNOWN_STATE; 188 std::weak_ptr<AppMgrServiceInner> appMgrServiceInner_; 189 std::shared_ptr<AppLifeCycleDeal> appLifeCycleDeal_; 190 std::shared_ptr<ApplicationInfo> appInfo_; // the application's info 191 std::shared_ptr<AMSEventHandler> eventHandler_; 192 std::string moduleName_; 193 mutable ffrt::mutex abilitiesMutex_; 194 std::map<const sptr<IRemoteObject>, std::shared_ptr<AbilityRunningRecord>> abilities_; 195 std::map<const sptr<IRemoteObject>, std::shared_ptr<AbilityRunningRecord>> terminateAbilities_; 196 }; 197 } // namespace AppExecFwk 198 } // namespace OHOS 199 #endif // OHOS_ABILITY_RUNTIME_MODULE_RUNNING_RECORD_H 200