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 <functional> 20 #include <string> 21 #include <list> 22 #include <memory> 23 24 #include "ability_stage.h" 25 #include "app_context.h" 26 #include "context.h" 27 #include "ability_stage_context.h" 28 #include "application_configuration_manager.h" 29 #include "app_launch_data.h" 30 #include "runtime.h" 31 32 namespace OHOS { 33 namespace AbilityRuntime { 34 class Runtime; 35 } // namespace AbilityRuntime 36 namespace AppExecFwk { 37 class ApplicationImpl; 38 class Configuration; 39 class AbilityRecordMgr; 40 class OHOSApplication : public AppContext { 41 public: 42 OHOSApplication(); 43 virtual ~OHOSApplication(); 44 45 /** 46 * @brief dump OHOSApplication info 47 * 48 * @param extra dump OHOSApplication info 49 */ 50 void DumpApplication(); 51 52 /** 53 * @brief Set Runtime 54 * 55 * @param runtime Runtime instance. 56 */ 57 void SetRuntime(std::unique_ptr<AbilityRuntime::Runtime> &&runtime); 58 59 /** 60 * @brief Set ApplicationContext 61 * 62 * @param context ApplicationContext instance. 63 */ 64 void SetApplicationContext(const std::shared_ptr<AbilityRuntime::ApplicationContext> &abilityRuntimeContext); 65 66 /** 67 * 68 * @brief Set the abilityRecordMgr to the OHOSApplication. 69 * 70 * @param abilityRecordMgr 71 */ 72 void SetAbilityRecordMgr(const std::shared_ptr<AbilityRecordMgr> &abilityRecordMgr); 73 74 /** 75 * 76 * @brief Will be Called when the system configuration of the device changes. 77 * 78 * @param config Indicates the new Configuration object. 79 */ 80 virtual void OnConfigurationUpdated(Configuration config, 81 AbilityRuntime::SetLevel level = AbilityRuntime::SetLevel::System); 82 83 /** 84 * 85 * @brief Will be Called when the application font of the device changes. 86 * 87 * @param config Indicates the new Configuration object. 88 */ 89 virtual void OnUpdateConfigurationForAll(Configuration config); 90 91 /** 92 * 93 * @brief Called when the system has determined to trim the memory, for example, 94 * when the ability is running in the background and there is no enough memory for 95 * running as many background processes as possible. 96 * 97 * @param level Indicates the memory trim level, which shows the current memory usage status. 98 */ 99 virtual void OnMemoryLevel(int32_t level); 100 101 /** 102 * 103 * @brief Will be called the application foregrounds 104 * 105 */ 106 virtual void OnForeground(); 107 108 /** 109 * 110 * @brief Will be called the application backgrounds 111 * 112 */ 113 virtual void OnBackground(); 114 115 /** 116 * 117 * @brief Will be called the application starts 118 * 119 */ 120 virtual void OnStart(); 121 122 /** 123 * 124 * @brief Will be called the application ends 125 */ 126 virtual void OnTerminate(); 127 128 /** 129 * @brief add the ability stage when a hap first load 130 * 131 * @param abilityRecord 132 * @return abilityStage context 133 */ 134 std::shared_ptr<AbilityRuntime::Context> AddAbilityStage( 135 const std::shared_ptr<AbilityLocalRecord> &abilityRecord, 136 const std::function<void(const std::shared_ptr<AbilityRuntime::Context> &)> &callback, bool &isAsyncCallback); 137 138 /** 139 * 140 * @brief update the application info after new module installed. 141 * 142 * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext. 143 * 144 */ 145 void UpdateApplicationInfoInstalled(const AppExecFwk::ApplicationInfo &appInfo); 146 147 /** 148 * @brief add the ability stage when a hap first load 149 * 150 * @param hapModuleInfo 151 * @return Returns true on success, false on failure 152 */ 153 bool AddAbilityStage( 154 const AppExecFwk::HapModuleInfo &hapModuleInfo, 155 const std::function<void()> &callback, bool &isAsyncCallback); 156 157 /** 158 * @brief remove the ability stage when all of the abilities in the hap have been removed 159 * 160 * @param abilityInfo 161 */ 162 void CleanAbilityStage(const sptr<IRemoteObject> &token, const std::shared_ptr<AbilityInfo> &abilityInfo, 163 bool isCacheProcess); 164 165 /** 166 * @brief return the application context 167 * 168 * @param context 169 */ 170 std::shared_ptr<AbilityRuntime::Context> GetAppContext() const; 171 172 /** 173 * @brief return the application runtime 174 * 175 * @param runtime 176 */ GetRuntime()177 const std::unique_ptr<AbilityRuntime::Runtime> &GetRuntime() const 178 { 179 return runtime_; 180 } 181 182 /** 183 * @brief return the specified runtime 184 * 185 * @param runtime 186 */ 187 const std::unique_ptr<AbilityRuntime::Runtime> &GetSpecifiedRuntime(const std::string &arkTSMode) const; 188 189 /* 190 * 191 * @brief Will be called the application ends 192 * 193 */ 194 virtual void SetConfiguration(const Configuration &config); 195 196 void ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName, 197 std::function<void(std::string)> callback, bool &isAsync); 198 199 void SchedulePrepareTerminate(const std::string &moduleName, 200 std::function<void(AppExecFwk::OnPrepareTerminationResult)> callback, bool &isAsync); 201 202 void ScheduleNewProcessRequest(const AAFwk::Want &want, const std::string &moduleName, 203 std::function<void(std::string)> callback, bool &isAsync); 204 205 virtual std::shared_ptr<Configuration> GetConfiguration() const; 206 GetExtensionNameByType(int32_t type,std::string & name)207 void GetExtensionNameByType(int32_t type, std::string &name) 208 { 209 std::map<int32_t, std::string>::iterator it = extensionTypeMap_.find(type); 210 if (it == extensionTypeMap_.end()) { 211 return; 212 } 213 name = it->second; 214 } 215 216 /** 217 * @brief Set extension types. 218 * 219 * @param map The extension types. 220 */ 221 void SetExtensionTypeMap(std::map<int32_t, std::string> map); 222 223 bool NotifyLoadRepairPatch(const std::string &hqfFile, const std::string &hapPath); 224 225 bool NotifyHotReloadPage(); 226 227 bool NotifyUnLoadRepairPatch(const std::string &hqfFile); 228 229 void CleanAppTempData(bool isLastProcess = false); 230 231 void CleanUselessTempData(); 232 233 void SetAppEnv(const std::vector<AppEnvironment>& appEnvironments); 234 235 void AutoStartupDone(const std::shared_ptr<AbilityLocalRecord> &abilityRecord, 236 const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage, const std::string &moduleName); 237 238 void AutoStartupDone(const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage, 239 const AppExecFwk::HapModuleInfo &hapModuleInfo); 240 241 void CleanEmptyAbilityStage(); 242 243 #ifdef SUPPORT_GRAPHICS 244 bool GetDisplayConfig(uint64_t displayId, float &density, std::string &directionStr); 245 #endif 246 247 void PreloadAppStartup(const BundleInfo &bundleInfo, const std::string &preloadModuleName, 248 std::shared_ptr<AppExecFwk::StartupTaskData> startupTaskData); 249 250 private: 251 void UpdateAppContextResMgr(const Configuration &config); 252 bool IsUpdateColorNeeded(Configuration &config, AbilityRuntime::SetLevel level); 253 bool isUpdateFontSize(Configuration &config, AbilityRuntime::SetLevel level); 254 bool IsUpdateLanguageNeeded(Configuration &config, AbilityRuntime::SetLevel level); 255 bool IsUpdateLocaleNeeded(const Configuration& updatedConfig, Configuration &config); 256 const std::function<void()> CreateAutoStartupCallback( 257 const std::shared_ptr<AbilityRuntime::AbilityStage> abilityStage, 258 const std::shared_ptr<AbilityLocalRecord> abilityRecord, 259 const std::function<void(const std::shared_ptr<AbilityRuntime::Context>&)>& callback); 260 const std::function<void()> CreateAutoStartupCallback( 261 const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage, 262 const AppExecFwk::HapModuleInfo &hapModuleInfo, 263 const std::function<void()>& callback); 264 bool IsMainProcess(const std::string &bundleName, const std::string &process); 265 void PreloadHybridModule(const HapModuleInfo &hapModuleInfo) const; 266 267 private: 268 std::shared_ptr<AbilityRecordMgr> abilityRecordMgr_ = nullptr; 269 std::shared_ptr<AbilityRuntime::ApplicationContext> abilityRuntimeContext_ = nullptr; 270 std::unordered_map<std::string, std::shared_ptr<AbilityRuntime::AbilityStage>> abilityStages_; 271 std::unique_ptr<AbilityRuntime::Runtime> runtime_ = nullptr; 272 std::shared_ptr<Configuration> configuration_ = nullptr; 273 std::map<int32_t, std::string> extensionTypeMap_; 274 }; 275 } // namespace AppExecFwk 276 } // namespace OHOS 277 #endif // OHOS_ABILITY_RUNTIME_OHOS_APPLICATION_H 278