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