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 FOUNDATION_APPEXECFWK_OHOS_CONTEXT_CONTAINER_H 17 #define FOUNDATION_APPEXECFWK_OHOS_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 Ability.onRequestPermissionsFromUserResult(int, String[], int[]) method 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 requestCode Indicates the request code to be passed to the Ability.onRequestPermissionsFromUserResult(int, 312 * String[], int[]) callback method. This code cannot be a negative number. 313 * 314 */ 315 void RequestPermissionsFromUser(std::vector<std::string> &permissions, std::vector<int> &permissionsState, 316 int requestCode) override; 317 318 /** 319 * @brief Creates a Context object for an application with the given bundle name. 320 * 321 * @param bundleName Indicates the bundle name of the application. 322 * @param flag Indicates the flag for creating a Context object. It can be 0, any of 323 * the following values, or any combination of the following values: CONTEXT_IGNORE_SECURITY, 324 * CONTEXT_INCLUDE_CODE, and CONTEXT_RESTRICTED. The value 0 indicates that there is no restriction 325 * on creating contexts for applications. 326 * @param accountId Indicates the account id. 327 * 328 * @return Returns a Context object created for the specified application. 329 */ 330 std::shared_ptr<Context> CreateBundleContext(std::string bundleName, int flag, int accountId = DEFAULT_ACCOUNT_ID); 331 332 /** 333 * @brief Obtains information about the caller of this ability. 334 * 335 * @return Returns the caller information. 336 */ 337 Uri GetCaller() override; 338 339 /** 340 * @brief InitResourceManager 341 * 342 * @param bundleInfo BundleInfo 343 */ 344 void InitResourceManager(BundleInfo &bundleInfo, std::shared_ptr<ContextDeal> &deal); 345 346 /** 347 * @brief Get the string of this Context based on the specified resource ID. 348 * 349 * @param resId Indicates the resource ID of the string to get. 350 * 351 * @return Returns the string of this Context. 352 */ 353 std::string GetString(int resId) override; 354 355 /** 356 * @brief Get the string array of this Context based on the specified resource ID. 357 * 358 * @param resId Indicates the resource ID of the string array to get. 359 * 360 * @return Returns the string array of this Context. 361 */ 362 std::vector<std::string> GetStringArray(int resId) override; 363 364 /** 365 * @brief Get the integer array of this Context based on the specified resource ID. 366 * 367 * @param resId Indicates the resource ID of the integer array to get. 368 * 369 * @return Returns the integer array of this Context. 370 */ 371 std::vector<int> GetIntArray(int resId) override; 372 373 /** 374 * @brief Obtains the theme of this Context. 375 * 376 * @return theme Returns the theme of this Context. 377 */ 378 std::map<std::string, std::string> GetTheme() override; 379 380 /** 381 * @brief Sets the theme of this Context based on the specified theme ID. 382 * 383 * @param themeId Indicates the resource ID of the theme to set. 384 */ 385 void SetTheme(int themeId) override; 386 387 /** 388 * @brief Obtains the pattern of this Context. 389 * 390 * @return getPattern in interface Context 391 */ 392 std::map<std::string, std::string> GetPattern() override; 393 394 /** 395 * @brief Get the color of this Context based on the specified resource ID. 396 * 397 * @param resId Indicates the resource ID of the color to get. 398 * 399 * @return Returns the color value of this Context. 400 */ 401 int GetColor(int resId) override; 402 403 /** 404 * @brief Obtains the theme id of this Context. 405 * 406 * @return int Returns the theme id of this Context. 407 */ 408 int GetThemeId() override; 409 410 /** 411 * @brief Obtains the current display orientation of this ability. 412 * 413 * @return Returns the current display orientation. 414 */ 415 int GetDisplayOrientation() override; 416 417 /** 418 * @brief Obtains the path storing the preference file of the application. 419 * If the preference file path does not exist, the system creates one and returns the created path. 420 * 421 * @return Returns the preference file path . 422 */ 423 std::string GetPreferencesDir() override; 424 425 /** 426 * @brief Set color mode 427 * 428 * @param the value of color mode. 429 */ 430 void SetColorMode(int mode) override; 431 432 /** 433 * @brief Obtains color mode. 434 * 435 * @return Returns the color mode value. 436 */ 437 int GetColorMode() override; 438 439 /** 440 * @brief Obtains the unique ID of the mission containing this ability. 441 * 442 * @return Returns the unique mission ID. 443 */ 444 int GetMissionId() override; 445 446 /** 447 * @brief Call this when your ability should be closed and the mission should be completely removed as a part of 448 * finishing the root ability of the mission. 449 */ 450 void TerminateAndRemoveMission() override; 451 452 /** 453 * @brief Obtains a task dispatcher that is bound to the UI thread. 454 * 455 * @return Returns the task dispatcher that is bound to the UI thread. 456 */ 457 std::shared_ptr<TaskDispatcher> GetUITaskDispatcher() override; 458 459 /** 460 * @brief Obtains a task dispatcher that is bound to the application main thread. 461 * 462 * @return Returns the task dispatcher that is bound to the application main thread. 463 */ 464 std::shared_ptr<TaskDispatcher> GetMainTaskDispatcher() override; 465 466 /** 467 * @brief Creates a parallel task dispatcher with a specified priority. 468 * 469 * @param name Indicates the task dispatcher name. This parameter is used to locate problems. 470 * @param priority Indicates the priority of all tasks dispatched by the parallel task dispatcher. 471 * 472 * @return Returns a parallel task dispatcher. 473 */ 474 std::shared_ptr<TaskDispatcher> CreateParallelTaskDispatcher( 475 const std::string &name, const TaskPriority &priority) override; 476 477 /** 478 * @brief Creates a serial task dispatcher with a specified priority. 479 * 480 * @param name Indicates the task dispatcher name. This parameter is used to locate problems. 481 * @param priority Indicates the priority of all tasks dispatched by the created task dispatcher. 482 * 483 * @return Returns a serial task dispatcher. 484 */ 485 std::shared_ptr<TaskDispatcher> CreateSerialTaskDispatcher( 486 const std::string &name, const TaskPriority &priority) override; 487 488 /** 489 * @brief Obtains a global task dispatcher with a specified priority. 490 * 491 * @param priority Indicates the priority of all tasks dispatched by the global task dispatcher. 492 * 493 * @return Returns a global task dispatcher. 494 */ 495 std::shared_ptr<TaskDispatcher> GetGlobalTaskDispatcher(const TaskPriority &priority) override; 496 497 /** 498 * @brief Requires that tasks associated with a given capability token be moved to the background 499 * 500 * @param nonFirst If nonfirst is false and not the lowest ability of the mission, you cannot move mission to end 501 * 502 * @return Returns true on success, others on failure. 503 */ 504 bool MoveMissionToEnd(bool nonFirst) override; 505 506 /** 507 * @brief Sets the application to start its ability in lock mission mode. 508 */ 509 void LockMission() override; 510 511 /** 512 * @brief Unlocks this ability by exiting the lock mission mode. 513 */ 514 void UnlockMission() override; 515 516 /** 517 * @brief Sets description information about the mission containing this ability. 518 * 519 * @param MissionInformation Indicates the object containing information about the 520 * mission. This parameter cannot be null. 521 * @return Returns true on success, others on failure. 522 */ 523 bool SetMissionInformation(const MissionInformation &missionInformation) override; 524 525 private: 526 std::shared_ptr<Context> baseContext_; 527 }; 528 } // namespace AppExecFwk 529 } // namespace OHOS 530 #endif // FOUNDATION_APPEXECFWK_OHOS_CONTEXT_CONTAINER_H 531