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