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