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