1 /* 2 * Copyright (c) 2021-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_CONTEXT_CONTAINER_H 17 #define OHOS_ABILITY_RUNTIME_CONTEXT_CONTAINER_H 18 19 #include "context_deal.h" 20 21 namespace OHOS { 22 namespace AppExecFwk { 23 class ContextContainer : public Context { 24 public: 25 ContextContainer() = default; 26 virtual ~ContextContainer() = default; 27 28 /** 29 * Attaches a Context object to the current ability. 30 * Generally, this method is called after Ability is loaded to provide the application context for the current 31 * ability. 32 * 33 * @param base Indicates a Context object. 34 */ 35 void AttachBaseContext(const std::shared_ptr<Context> &base); 36 37 /** 38 * Called when getting the ProcessInfo 39 * 40 * @return ProcessInfo 41 */ 42 std::shared_ptr<ProcessInfo> GetProcessInfo() const override; 43 44 /** 45 * @brief Obtains information about the current application. The returned application information includes basic 46 * information such as the application name and application permissions. 47 * 48 * @return Returns the ApplicationInfo for the current application. 49 */ 50 std::shared_ptr<ApplicationInfo> GetApplicationInfo() const override; 51 52 /** 53 * @brief Obtains the Context object of the application. 54 * 55 * @return Returns the Context object of the application. 56 */ 57 std::shared_ptr<Context> GetApplicationContext() const override; 58 59 /** 60 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources, 61 * source code, and configuration files of a module. 62 * 63 * @return Returns the path of the package file. 64 */ 65 virtual std::string GetBundleCodePath() override; 66 67 /** 68 * @brief Obtains information about the current ability. 69 * The returned information includes the class name, bundle name, and other information about the current ability. 70 * 71 * @return Returns the AbilityInfo object for the current ability. 72 */ 73 virtual const std::shared_ptr<AbilityInfo> GetAbilityInfo() override; 74 75 /** 76 * @brief Checks whether the configuration of this ability is changing. 77 * 78 * @return Returns true if the configuration of this ability is changing and false otherwise. 79 */ 80 virtual bool IsUpdatingConfigurations() override; 81 82 /** 83 * @brief Informs the system of the time required for drawing this Page ability. 84 * 85 * @return Returns the notification is successful or fail 86 */ 87 virtual bool PrintDrawnCompleted() override; 88 89 /** 90 * @brief Obtains the Context object of the application. 91 * 92 * @return Returns the Context object of the application. 93 */ 94 std::shared_ptr<Context> GetContext() override; 95 96 /** 97 * @brief Obtains an IBundleMgr instance. 98 * You can use this instance to obtain information about the application bundle. 99 * 100 * @return Returns an IBundleMgr instance. 101 */ 102 sptr<IBundleMgr> GetBundleManager() const override; 103 104 /** 105 * @brief Obtains a resource manager. 106 * 107 * @return Returns a ResourceManager object. 108 */ 109 std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override; 110 111 /** 112 * @brief Deletes the specified private file associated with the application. 113 * 114 * @param fileName Indicates the name of the file to delete. The file name cannot contain path separators. 115 * 116 * @return Returns true if the file is deleted successfully; returns false otherwise. 117 */ 118 bool DeleteFile(const std::string &fileName) override; 119 120 /** 121 * @brief Obtains the application-specific cache directory on the device's internal storage. The system 122 * automatically deletes files from the cache directory if disk space is required elsewhere on the device. 123 * Older files are always deleted first. 124 * 125 * @return Returns the application-specific cache directory. 126 */ 127 std::string GetCacheDir() override; 128 129 /** 130 * @brief Obtains the application-specific code-cache directory on the device's internal storage. 131 * The system will delete any files stored in this location both when your specific application is upgraded, 132 * and when the entire platform is upgraded. 133 * 134 * @return Returns the application-specific code-cache directory. 135 */ 136 std::string GetCodeCacheDir() override; 137 138 /** 139 * @brief Obtains the local database path. 140 * If the local database path does not exist, the system creates one and returns the created path. 141 * 142 * @return Returns the local database file. 143 */ 144 std::string GetDatabaseDir() override; 145 146 /** 147 * @brief Obtains the absolute path where all private data files of this application are stored. 148 * 149 * @return Returns the absolute path storing all private data files of this application. 150 */ 151 std::string GetDataDir() override; 152 153 /** 154 * @brief Obtains the directory for storing custom data files of the application. 155 * You can use the returned File object to create and access files in this directory. The files 156 * can be accessible only by the current application. 157 * 158 * @param name Indicates the name of the directory to retrieve. This directory is created as part 159 * of your application data. 160 * @param mode Indicates the file operating mode. The value can be 0 or a combination of MODE_PRIVATE. 161 * 162 * @return Returns a File object for the requested directory. 163 */ 164 std::string GetDir(const std::string &name, int mode) override; 165 166 /** 167 * @brief Obtains the absolute path to the application-specific cache directory 168 * on the primary external or shared storage device. 169 * 170 * @return Returns the absolute path to the application-specific cache directory on the external or 171 * shared storage device; returns null if the external or shared storage device is temporarily unavailable. 172 */ 173 std::string GetExternalCacheDir() override; 174 175 /** 176 * @brief Obtains the absolute path to the directory for storing files for the application on the 177 * primary external or shared storage device. 178 * 179 * @param type Indicates the type of the file directory to return 180 * 181 * @return Returns the absolute path to the application file directory on the external or shared storage 182 * device; returns null if the external or shared storage device is temporarily unavailable. 183 */ 184 std::string GetExternalFilesDir(std::string &type) override; 185 186 /** 187 * @brief Obtains the directory for storing files for the application on the device's internal storage. 188 * 189 * @return Returns the application file directory. 190 */ 191 std::string GetFilesDir() override; 192 193 /** 194 * @brief Obtains the absolute path which app created and will be excluded from automatic backup to remote storage. 195 * The returned path maybe changed if the application is moved to an adopted storage device. 196 * 197 * @return The path of the directory holding application files that will not be automatically backed up to remote 198 * storage. 199 */ 200 std::string GetNoBackupFilesDir() override; 201 202 /** 203 * @brief Checks whether the current process has the given permission. 204 * You need to call requestPermissionsFromUser(java.lang.std::string[],int) to request a permission only 205 * if the current process does not have the specific permission. 206 * 207 * @param permission Indicates the permission to check. This parameter cannot be null. 208 * 209 * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission; 210 * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise. 211 */ 212 int VerifySelfPermission(const std::string &permission) override; 213 214 /** 215 * @brief Obtains the bundle name of the current ability. 216 * 217 * @return Returns the bundle name of the current ability. 218 */ 219 std::string GetBundleName() const override; 220 221 /** 222 * @brief Obtains the path of the OHOS Ability Package (HAP} containing this ability. 223 * 224 * @return Returns the path of the HAP containing this ability. 225 */ 226 std::string GetBundleResourcePath() override; 227 228 /** 229 * @brief Remove permissions for all users who have access to specific permissions 230 * 231 * @param permission Indicates the permission to unauth. This parameter cannot be null. 232 * @param uri Indicates the URI to unauth. This parameter cannot be null. 233 * @param uid Indicates the UID of the unauth to check. 234 * 235 */ 236 void UnauthUriPermission(const std::string &permission, const Uri &uri, int uid) override; 237 238 /** 239 * @brief Obtains an ability manager. 240 * The ability manager provides information about running processes and memory usage of an application. 241 * 242 * @return Returns an IAbilityManager instance. 243 */ 244 sptr<AAFwk::IAbilityManager> GetAbilityManager() override; 245 246 /** 247 * @brief Obtains the type of this application. 248 * 249 * @return Returns system if this application is a system application; 250 * returns normal if it is released in OHOS AppGallery; 251 * returns other if it is released by a third-party vendor; 252 * returns an empty string if the query fails. 253 */ 254 std::string GetAppType() override; 255 256 /** 257 * @brief Query whether the application of the specified PID and UID has been granted a certain permission 258 * 259 * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null. 260 * @param pid Process id 261 * @param uid 262 * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission; 263 * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise. 264 */ 265 int VerifyPermission(const std::string &permission, int pid, int uid) override; 266 267 /** 268 * @brief Obtains the distributed file path. 269 * If the distributed file path does not exist, the system creates one and returns the created path. This method is 270 * applicable only to the context of an ability rather than that of an application. 271 * 272 * @return Returns the distributed file. 273 */ 274 std::string GetDistributedDir() override; 275 276 /** 277 * @brief Sets the pattern of this Context based on the specified pattern ID. 278 * 279 * @param patternId Indicates the resource ID of the pattern to set. 280 */ 281 void SetPattern(int patternId) override; 282 283 /** 284 * @brief Obtains the Context object of this ability. 285 * 286 * @return Returns the Context object of this ability. 287 */ 288 std::shared_ptr<Context> GetAbilityPackageContext() override; 289 290 /** 291 * @brief Obtains the HapModuleInfo object of the application. 292 * 293 * @return Returns the HapModuleInfo object of the application. 294 */ 295 std::shared_ptr<HapModuleInfo> GetHapModuleInfo() override; 296 297 /** 298 * @brief Obtains the name of the current process. 299 * 300 * @return Returns the current process name. 301 */ 302 std::string GetProcessName() override; 303 304 /** 305 * @brief Requests certain permissions from the system. 306 * This method is called for permission request. This is an asynchronous method. When it is executed, 307 * the task will be called back. 308 * 309 * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null. 310 * @param permissionsState Indicates the list of permissions' state to be requested. This parameter cannot be null. 311 * @param task The callback or promise fo js interface. 312 */ 313 void RequestPermissionsFromUser(std::vector<std::string> &permissions, std::vector<int> &permissionsState, 314 PermissionRequestTask &&task) override; 315 316 /** 317 * @brief Creates a Context object for an application with the given bundle name. 318 * 319 * @param bundleName Indicates the bundle name of the application. 320 * @param flag Indicates the flag for creating a Context object. It can be 0, any of 321 * the following values, or any combination of the following values: CONTEXT_IGNORE_SECURITY, 322 * CONTEXT_INCLUDE_CODE, and CONTEXT_RESTRICTED. The value 0 indicates that there is no restriction 323 * on creating contexts for applications. 324 * @param accountId Indicates the account id. 325 * 326 * @return Returns a Context object created for the specified application. 327 */ 328 std::shared_ptr<Context> CreateBundleContext(std::string bundleName, int flag, int accountId = DEFAULT_ACCOUNT_ID); 329 330 /** 331 * @brief Obtains information about the caller of this ability. 332 * 333 * @return Returns the caller information. 334 */ 335 Uri GetCaller() override; 336 337 /** 338 * @brief InitResourceManager 339 * 340 * @param bundleInfo BundleInfo 341 */ 342 void InitResourceManager(BundleInfo &bundleInfo, std::shared_ptr<ContextDeal> &deal); 343 344 /** 345 * @brief Get the string of this Context based on the specified resource ID. 346 * 347 * @param resId Indicates the resource ID of the string to get. 348 * 349 * @return Returns the string of this Context. 350 */ 351 std::string GetString(int resId) override; 352 353 /** 354 * @brief Get the string array of this Context based on the specified resource ID. 355 * 356 * @param resId Indicates the resource ID of the string array to get. 357 * 358 * @return Returns the string array of this Context. 359 */ 360 std::vector<std::string> GetStringArray(int resId) override; 361 362 /** 363 * @brief Get the integer array of this Context based on the specified resource ID. 364 * 365 * @param resId Indicates the resource ID of the integer array to get. 366 * 367 * @return Returns the integer array of this Context. 368 */ 369 std::vector<int> GetIntArray(int resId) override; 370 371 /** 372 * @brief Obtains the theme of this Context. 373 * 374 * @return theme Returns the theme of this Context. 375 */ 376 std::map<std::string, std::string> GetTheme() override; 377 378 /** 379 * @brief Sets the theme of this Context based on the specified theme ID. 380 * 381 * @param themeId Indicates the resource ID of the theme to set. 382 */ 383 void SetTheme(int themeId) override; 384 385 /** 386 * @brief Obtains the pattern of this Context. 387 * 388 * @return getPattern in interface Context 389 */ 390 std::map<std::string, std::string> GetPattern() override; 391 392 /** 393 * @brief Get the color of this Context based on the specified resource ID. 394 * 395 * @param resId Indicates the resource ID of the color to get. 396 * 397 * @return Returns the color value of this Context. 398 */ 399 int GetColor(int resId) override; 400 401 /** 402 * @brief Obtains the theme id of this Context. 403 * 404 * @return int Returns the theme id of this Context. 405 */ 406 int GetThemeId() override; 407 408 /** 409 * @brief Obtains the current display orientation of this ability. 410 * 411 * @return Returns the current display orientation. 412 */ 413 int GetDisplayOrientation() override; 414 415 /** 416 * @brief Obtains the path storing the preference file of the application. 417 * If the preference file path does not exist, the system creates one and returns the created path. 418 * 419 * @return Returns the preference file path . 420 */ 421 std::string GetPreferencesDir() override; 422 423 /** 424 * @brief Set color mode 425 * 426 * @param the value of color mode. 427 */ 428 void SetColorMode(int mode) override; 429 430 /** 431 * @brief Obtains color mode. 432 * 433 * @return Returns the color mode value. 434 */ 435 int GetColorMode() override; 436 437 /** 438 * @brief Obtains the unique ID of the mission containing this ability. 439 * 440 * @return Returns the unique mission ID. 441 */ 442 int GetMissionId() override; 443 444 private: 445 std::shared_ptr<Context> baseContext_; 446 }; 447 } // namespace AppExecFwk 448 } // namespace OHOS 449 #endif // OHOS_ABILITY_RUNTIME_CONTEXT_CONTAINER_H 450