1 /* 2 * Copyright (c) 2021-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_OHOS_APPLICATION_H 17 #define OHOS_ABILITY_RUNTIME_OHOS_APPLICATION_H 18 19 #include <string> 20 #include <list> 21 #include <memory> 22 23 #include "ability_lifecycle_callbacks.h" 24 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h" 25 #include "ability_stage.h" 26 #include "app_context.h" 27 #include "element_callback.h" 28 29 namespace OHOS { 30 namespace AbilityRuntime { 31 class Runtime; 32 } // namespace AbilityRuntime 33 namespace AppExecFwk { 34 class ElementsCallback; 35 class ApplicationImpl; 36 class Configuration; 37 class AbilityRecordMgr; 38 class OHOSApplication : public AppContext, 39 public AbilityLifecycleCallbacks { 40 public: 41 OHOSApplication(); 42 virtual ~OHOSApplication(); 43 44 /** 45 * @brief dump OHOSApplication info 46 * 47 * @param extra dump OHOSApplication info 48 */ 49 void DumpApplication(); 50 51 /** 52 * @brief Set Runtime 53 * 54 * @param runtime Runtime instance. 55 */ 56 void SetRuntime(std::unique_ptr<AbilityRuntime::Runtime>&& runtime); 57 58 /** 59 * @brief Set ApplicationContext 60 * 61 * @param context ApplicationContext instance. 62 */ 63 void SetApplicationContext(const std::shared_ptr<AbilityRuntime::ApplicationContext> &abilityRuntimeContext); 64 65 /** 66 * 67 * @brief Set the abilityRecordMgr to the OHOSApplication. 68 * 69 * @param abilityRecordMgr 70 */ 71 void SetAbilityRecordMgr(const std::shared_ptr<AbilityRecordMgr> &abilityRecordMgr); 72 73 /** 74 * 75 * @brief Register AbilityLifecycleCallbacks with OHOSApplication 76 * 77 * @param callBack callBack When the life cycle of the ability in the application changes, 78 */ 79 void RegisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> &callBack); 80 81 /** 82 * 83 * @brief Unregister AbilityLifecycleCallbacks with OHOSApplication 84 * 85 * @param callBack RegisterAbilityLifecycleCallbacks`s callBack 86 */ 87 void UnregisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> &callBack); 88 89 /** 90 * 91 * @brief Will be called when the given ability calls Ability->onStart 92 * 93 * @param Ability Indicates the ability object that calls the onStart() method. 94 */ 95 void OnAbilityStart(const std::shared_ptr<AbilityRuntime::UIAbility> &ability); 96 97 /** 98 * 99 * @brief Will be called when the given ability calls Ability->onInactive 100 * 101 * @param Ability Indicates the Ability object that calls the onInactive() method. 102 */ 103 void OnAbilityInactive(const std::shared_ptr<AbilityRuntime::UIAbility> &ability); 104 105 /** 106 * 107 * @brief Will be called when the given ability calls Ability->onBackground 108 * 109 * @param Ability Indicates the Ability object that calls the onBackground() method. 110 */ 111 void OnAbilityBackground(const std::shared_ptr<AbilityRuntime::UIAbility> &ability); 112 113 /** 114 * 115 * @brief Will be called when the given ability calls Ability->onForeground 116 * 117 * @param Ability Indicates the Ability object that calls the onForeground() method. 118 */ 119 void OnAbilityForeground(const std::shared_ptr<AbilityRuntime::UIAbility> &ability); 120 121 /** 122 * 123 * @brief Will be called when the given ability calls Ability->onActive 124 * 125 * @param Ability Indicates the Ability object that calls the onActive() method. 126 */ 127 void OnAbilityActive(const std::shared_ptr<AbilityRuntime::UIAbility> &ability); 128 129 /** 130 * 131 * @brief Will be called when the given ability calls Ability->onStop 132 * 133 * @param Ability Indicates the Ability object that calls the onStop() method. 134 */ 135 void OnAbilityStop(const std::shared_ptr<AbilityRuntime::UIAbility> &ability); 136 137 /** 138 * 139 * Called when Ability#onSaveAbilityState(PacMap) was called on an ability. 140 * 141 * @param outState Indicates the PacMap object passed to Ability#onSaveAbilityState(PacMap) 142 * for storing user data and states. This parameter cannot be null. 143 */ 144 void DispatchAbilitySavedState(const PacMap &outState); 145 146 /** 147 * 148 * @brief Called when an ability calls Ability#onSaveAbilityState(PacMap). 149 * You can implement your own logic in this method. 150 * @param outState IIndicates the {@link PacMap} object passed to the onSaveAbilityState() callback. 151 * 152 */ 153 void OnAbilitySaveState(const PacMap &outState); 154 155 /** 156 * 157 * @brief Register ElementsCallback with OHOSApplication 158 * 159 * @param callBack callBack when the system configuration of the device changes. 160 */ 161 void RegisterElementsCallbacks(const std::shared_ptr<ElementsCallback> &callback); 162 163 /** 164 * 165 * @brief Unregister ElementsCallback with OHOSApplication 166 * 167 * @param callback RegisterElementsCallbacks`s callback 168 */ 169 void UnregisterElementsCallbacks(const std::shared_ptr<ElementsCallback> &callback); 170 171 /** 172 * 173 * @brief Will be Called when the system configuration of the device changes. 174 * 175 * @param config Indicates the new Configuration object. 176 */ 177 virtual void OnConfigurationUpdated(const Configuration &config); 178 179 /** 180 * 181 * @brief Called when the system has determined to trim the memory, for example, 182 * when the ability is running in the background and there is no enough memory for 183 * running as many background processes as possible. 184 * 185 * @param level Indicates the memory trim level, which shows the current memory usage status. 186 */ 187 virtual void OnMemoryLevel(int level); 188 189 /** 190 * 191 * @brief Will be called the application foregrounds 192 * 193 */ 194 virtual void OnForeground(); 195 196 /** 197 * 198 * @brief Will be called the application backgrounds 199 * 200 */ 201 virtual void OnBackground(); 202 203 /** 204 * 205 * @brief Will be called the application starts 206 * 207 */ 208 virtual void OnStart(); 209 210 /** 211 * 212 * @brief Will be called the application ends 213 */ 214 virtual void OnTerminate(); 215 216 /** 217 * @brief add the ability stage when a hap first load 218 * 219 * @param abilityRecord 220 * @return abilityStage context 221 */ 222 std::shared_ptr<AbilityRuntime::Context> AddAbilityStage( 223 const std::shared_ptr<AbilityLocalRecord> &abilityRecord); 224 225 /** 226 * 227 * @brief update the application info after new module installed. 228 * 229 * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext. 230 * 231 */ 232 void UpdateApplicationInfoInstalled(const AppExecFwk::ApplicationInfo &appInfo); 233 234 /** 235 * @brief add the ability stage when a hap first load 236 * 237 * @param hapModuleInfo 238 * @return Returns true on success, false on failure 239 */ 240 bool AddAbilityStage(const AppExecFwk::HapModuleInfo &hapModuleInfo); 241 242 /** 243 * @brief remove the ability stage when all of the abilities in the hap have been removed 244 * 245 * @param abilityInfo 246 */ 247 void CleanAbilityStage(const sptr<IRemoteObject> &token, const std::shared_ptr<AbilityInfo> &abilityInfo); 248 249 /** 250 * @brief return the application context 251 * 252 * @param context 253 */ 254 std::shared_ptr<AbilityRuntime::Context> GetAppContext() const; 255 256 /** 257 * @brief return the application runtime 258 * 259 * @param runtime 260 */ 261 const std::unique_ptr<AbilityRuntime::Runtime>& GetRuntime(); 262 263 /* 264 * 265 * @brief Will be called the application ends 266 * 267 */ 268 virtual void SetConfiguration(const Configuration &config); 269 270 void ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName, std::string &flag); 271 272 void ScheduleNewProcessRequest(const AAFwk::Want &want, const std::string &moduleName, std::string &flag); 273 274 virtual std::shared_ptr<Configuration> GetConfiguration(); 275 GetExtensionNameByType(int32_t type,std::string & name)276 void GetExtensionNameByType(int32_t type, std::string &name) 277 { 278 std::map<int32_t, std::string>::iterator it = extensionTypeMap_.find(type); 279 if (it == extensionTypeMap_.end()) { 280 return; 281 } 282 name = it->second; 283 } 284 285 /** 286 * @brief Set extension types. 287 * 288 * @param map The extension types. 289 */ 290 void SetExtensionTypeMap(std::map<int32_t, std::string> map); 291 292 bool NotifyLoadRepairPatch(const std::string &hqfFile, const std::string &hapPath); 293 294 bool NotifyHotReloadPage(); 295 296 bool NotifyUnLoadRepairPatch(const std::string &hqfFile); 297 298 void CleanAppTempData(bool isLastProcess = false); 299 300 void CleanUselessTempData(); 301 302 private: 303 void DoCleanWorkAfterStageCleaned(const AbilityInfo &abilityInfo); 304 void UpdateAppContextResMgr(const Configuration &config); 305 306 private: 307 std::list<std::shared_ptr<AbilityLifecycleCallbacks>> abilityLifecycleCallbacks_; 308 std::list<std::shared_ptr<ElementsCallback>> elementsCallbacks_; 309 std::shared_ptr<AbilityRecordMgr> abilityRecordMgr_ = nullptr; 310 std::shared_ptr<AbilityRuntime::ApplicationContext> abilityRuntimeContext_ = nullptr; 311 std::unordered_map<std::string, std::shared_ptr<AbilityRuntime::AbilityStage>> abilityStages_; 312 std::unique_ptr<AbilityRuntime::Runtime> runtime_; 313 std::shared_ptr<Configuration> configuration_ = nullptr; 314 std::map<int32_t, std::string> extensionTypeMap_; 315 }; 316 } // namespace AppExecFwk 317 } // namespace OHOS 318 #endif // OHOS_ABILITY_RUNTIME_OHOS_APPLICATION_H 319