1 /* 2 * Copyright (c) 2021-2024 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_STAGE_CONTEXT_CONTEXT_H 17 #define OHOS_ABILITY_RUNTIME_STAGE_CONTEXT_CONTEXT_H 18 19 #include <memory> 20 #include <mutex> 21 22 #include "application_info.h" 23 #include "bindable.h" 24 #include "configuration.h" 25 #include "hap_module_info.h" 26 #include "iremote_object.h" 27 #include "resource_manager.h" 28 29 using IRemoteObject = OHOS::IRemoteObject; 30 31 namespace OHOS { 32 namespace AbilityRuntime { 33 class ApplicationContext; 34 35 class Context : public Bindable, public std::enable_shared_from_this<Context> { 36 public: 37 Context() = default; 38 ~Context() override = default; 39 40 /** 41 * @brief Obtains the Context object of the application. 42 * 43 * @return Returns the Context object of the application. 44 */ 45 static std::shared_ptr<ApplicationContext> GetApplicationContext(); 46 47 /** 48 * @brief Obtains the bundle name of the current ability. 49 * 50 * @return Returns the bundle name of the current ability. 51 */ 52 virtual std::string GetBundleName() const = 0; 53 54 /** 55 * @brief Creates a Context object for an application with the given bundle name. 56 * 57 * @param bundleName Indicates the bundle name of the application. 58 * 59 * @return Returns a Context object created for the specified application. 60 */ 61 virtual std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) = 0; 62 63 /** 64 * @brief Obtains information about the current application. The returned application information includes basic 65 * information such as the application name and application permissions. 66 * 67 * @return Returns the ApplicationInfo for the current application. 68 */ 69 virtual std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const = 0; 70 71 /** 72 * @brief Obtains a resource manager. 73 * 74 * @return Returns a ResourceManager object. 75 */ 76 virtual std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const = 0; 77 78 /** 79 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources, 80 * source code, and configuration files of a module. 81 * 82 * @return Returns the path of the package file. 83 */ 84 virtual std::string GetBundleCodePath() const = 0; 85 86 /** 87 * @brief Obtains the HapModuleInfo object of the application. 88 * 89 * @return Returns the HapModuleInfo object of the application. 90 */ 91 virtual std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const = 0; 92 93 /** 94 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources, 95 * source code, and configuration files of a module. 96 * 97 * @return Returns the path of the package file. 98 */ 99 virtual std::string GetBundleCodeDir() = 0; 100 101 /** 102 * @brief Obtains the application-specific cache directory on the device's internal storage. The system 103 * automatically deletes files from the cache directory if disk space is required elsewhere on the device. 104 * Older files are always deleted first. 105 * 106 * @return Returns the application-specific cache directory. 107 */ 108 virtual std::string GetCacheDir() = 0; 109 110 /** 111 * @brief Obtains the temporary directory. 112 * 113 * @return Returns the application temporary directory. 114 */ 115 virtual std::string GetTempDir() = 0; 116 117 /** 118 * @brief Obtains the directory for storing files for the application on the device's internal storage. 119 * 120 * @return Returns the application file directory. 121 */ 122 virtual std::string GetFilesDir() = 0; 123 124 virtual std::string GetResourceDir() = 0; 125 126 /** 127 * @brief Checks whether the configuration of this ability is changing. 128 * 129 * @return Returns true if the configuration of this ability is changing and false otherwise. 130 */ 131 virtual bool IsUpdatingConfigurations() = 0; 132 133 /** 134 * @brief Informs the system of the time required for drawing this Page ability. 135 * 136 * @return Returns the notification is successful or fail 137 */ 138 virtual bool PrintDrawnCompleted() = 0; 139 140 /** 141 * @brief Obtains the local database path. 142 * If the local database path does not exist, the system creates one and returns the created path. 143 * 144 * @return Returns the local database file. 145 */ 146 virtual std::string GetDatabaseDir() = 0; 147 148 /** 149 * @brief Obtains the local system database path. 150 * If the local system database path does not exist, the system creates one and returns the created path. 151 * 152 * @return Returns the local database file. 153 */ 154 virtual int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) = 0; 155 156 /** 157 * @brief Obtains the path storing the storage file of the application. 158 * 159 * @return Returns the local storage file. 160 */ 161 virtual std::string GetPreferencesDir() = 0; 162 163 /** 164 * @brief Obtains the path storing the system storage file of the application. 165 * 166 * @return Returns the local system storage file. 167 */ 168 virtual int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, 169 std::string &preferencesDir) = 0; 170 171 /** 172 * @brief Obtains the path storing the group file of the application by the groupId. 173 * 174 * @return Returns the local group file. 175 */ 176 virtual std::string GetGroupDir(std::string groupId) = 0; 177 178 /** 179 * @brief Obtains the path distributed file of the application 180 * 181 * @return Returns the distributed file. 182 */ 183 virtual std::string GetDistributedFilesDir() = 0; 184 185 virtual std::string GetCloudFileDir() = 0; 186 187 /** 188 * @brief Obtains token. 189 * 190 * @return Returns the token. 191 */ 192 virtual sptr<IRemoteObject> GetToken() = 0; 193 194 /** 195 * @brief Attachs ability's token. 196 * 197 * @param token The token represents ability. 198 */ 199 virtual void SetToken(const sptr<IRemoteObject> &token) = 0; 200 201 /** 202 * @brief Switch file area 203 * 204 * @param mode file area. 205 */ 206 virtual void SwitchArea(int mode) = 0; 207 208 /** 209 * @brief Creates a Context object for a hap with the given module name. 210 * 211 * @param moduleName Indicates the module name of the hap. 212 * 213 * @return Returns a Context object created for the specified hap and app. 214 */ 215 virtual std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) = 0; 216 217 /** 218 * @brief Creates a Context object for a hap with the given hap name and app name. 219 * 220 * @param bundleName Indicates the app name of the application. 221 * 222 * @param moduleName Indicates the module name of the hap. 223 * 224 * @return Returns a Context object created for the specified hap and app. 225 */ 226 virtual std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, 227 const std::string &moduleName) = 0; 228 /** 229 * @brief Creates a ResourceManager object for a hap with the given hap name and app name. 230 * 231 * @param bundleName Indicates the app name of the application. 232 233 * @param moduleName Indicates the module name of the hap. 234 * 235 * @return Returns a ResourceManager object created for the specified hap and app. 236 */ 237 virtual std::shared_ptr<Global::Resource::ResourceManager> CreateModuleResourceManager( 238 const std::string &bundleName, const std::string &moduleName) = 0; 239 240 virtual int32_t CreateSystemHspModuleResourceManager(const std::string &bundleName, 241 const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager) = 0; 242 CreateHspModuleResourceManager(const std::string & bundleName,const std::string & moduleName,std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)243 virtual int32_t CreateHspModuleResourceManager(const std::string &bundleName, 244 const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager) 245 { 246 return ERR_INVALID_VALUE; 247 } 248 /** 249 * @brief Get file area 250 * 251 * @return file area. 252 */ 253 virtual int GetArea() = 0; 254 255 /** 256 * @brief Get process name 257 * 258 * @return process name. 259 */ 260 virtual std::string GetProcessName() = 0; 261 262 /** 263 * @brief Obtains the configuration of application. 264 * 265 * @return configuration of application. 266 */ 267 virtual std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const = 0; 268 269 /** 270 * @brief Obtains the application base directory on the device's internal storage. 271 * 272 * @return Returns the application base directory. 273 */ 274 virtual std::string GetBaseDir() const = 0; 275 276 /** 277 * @brief Obtains the Device Type. 278 * 279 * @return Returns the Device Type. 280 */ 281 virtual Global::Resource::DeviceType GetDeviceType() const = 0; 282 283 /** 284 * @brief Create a area mode context. 285 * 286 * @param areaMode Indicates the area mode. 287 * 288 * @return Returns the context with the specified area mode. 289 */ 290 virtual std::shared_ptr<Context> CreateAreaModeContext(int areaMode) = 0; 291 292 #ifdef SUPPORT_GRAPHICS 293 /** 294 * @brief Create a context by displayId. This Context updates the density and direction properties 295 * based on the displayId, while other property values remain the same as in the original Context. 296 * 297 * @param displayId Indicates the displayId. 298 * 299 * @return Returns the context with the specified displayId. 300 */ 301 virtual std::shared_ptr<Context> CreateDisplayContext(uint64_t displayId) = 0; 302 #endif 303 304 /** 305 * @brief Getting derived class 306 * 307 * @tparam T template 308 * @param context the context object 309 * @return std::shared_ptr<T> derived class 310 */ 311 template<class T> ConvertTo(const std::shared_ptr<Context> & context)312 static std::shared_ptr<T> ConvertTo(const std::shared_ptr<Context>& context) 313 { 314 if constexpr (!std::is_same_v<T, typename T::SelfType>) { 315 return nullptr; 316 } 317 318 if (context && context->IsContext(T::CONTEXT_TYPE_ID)) { 319 return std::static_pointer_cast<T>(context); 320 } 321 322 return nullptr; 323 } 324 325 using SelfType = Context; 326 static const size_t CONTEXT_TYPE_ID; 327 328 protected: IsContext(size_t contextTypeId)329 virtual bool IsContext(size_t contextTypeId) 330 { 331 return contextTypeId == CONTEXT_TYPE_ID; 332 } 333 334 static std::shared_ptr<ApplicationContext> applicationContext_; 335 static std::mutex contextMutex_; 336 }; 337 } // namespace AbilityRuntime 338 } // namespace OHOS 339 #endif // OHOS_ABILITY_RUNTIME_STAGE_CONTEXT_CONTEXT_H 340