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_H 17 #define OHOS_ABILITY_RUNTIME_CONTEXT_H 18 19 #include <memory> 20 21 #include "bundle_mgr_interface.h" 22 #include "ability_manager_interface.h" 23 #include "ability_info.h" 24 #include "process_info.h" 25 #include "resource_manager.h" 26 #include "dummy_hap_module_info.h" 27 #include "hap_module_info.h" 28 #include "task/task_priority.h" 29 #include "mission_information.h" 30 31 namespace OHOS { 32 namespace DataShare { 33 class DataShareHelper; 34 } 35 namespace AppExecFwk { 36 using Want = OHOS::AAFwk::Want; 37 using AbilityStartSetting = AAFwk::AbilityStartSetting; 38 using PermissionRequestTask = std::function<void(const std::vector<std::string>&, const std::vector<int>&)>; 39 // Request permissions for user 40 #define OHOS_REQUEST_PERMISSION_BUNDLENAME "com.ohos.systemui" 41 #define OHOS_REQUEST_PERMISSION_ABILITY_NAME "com.ohos.systemui.systemdialog.MainAbility" 42 43 #define OHOS_REQUEST_PERMISSION_KEY "OHOS_REQUEST_PERMISSION_KEY" 44 #define OHOS_REQUEST_PERMISSIONS_LIST "OHOS_REQUEST_PERMISSIONS_LIST" 45 #define OHOS_REQUEST_PERMISSIONS_DES_LIST "OHOS_REQUEST_PERMISSIONS_DES_LIST" 46 #define OHOS_REQUEST_CALLER_BUNDLERNAME "OHOS_REQUEST_CALLER_BUNDLERNAME" 47 48 #define OHOS_RESULT_PERMISSION_KEY "OHOS_RESULT_PERMISSION_KEY" 49 #define OHOS_RESULT_PERMISSIONS_LIST "OHOS_RESULT_PERMISSIONS_LIST" 50 #define OHOS_RESULT_PERMISSIONS_LIST_YES "OHOS_RESULT_PERMISSIONS_LIST_YES" 51 #define OHOS_RESULT_PERMISSIONS_LIST_NO "OHOS_RESULT_PERMISSIONS_LIST_NO" 52 #define OHOS_RESULT_CALLER_BUNDLERNAME "OHOS_RESULT_CALLER_BUNDLERNAME" 53 54 #define OHOS_REQUEST_PERMISSION_VALUE 1 55 56 constexpr int INVALID_RESOURCE_VALUE = -1; // GetColor() Failed return Value 57 constexpr int DEFAULT_ACCOUNT_ID = -1; 58 59 class DataAbilityHelperImpl; 60 class ContinuationConnector; 61 class IAbilityManager; 62 class EventRunner; 63 class Context { 64 public: 65 Context() = default; 66 virtual ~Context() = default; 67 68 /** 69 * The value 0 indicates that there is no restriction on creating contexts for applications. 70 */ 71 int MODE_PRIVATE = 0x0000; 72 73 /** 74 * static final int CONTEXT_INCLUDE_CODE 75 * Indicates the flag used with createBundleContext(java.lang.String,int) for creating a Context 76 * object that includes the application code. 77 * 78 * The value is 0x00000001. 79 * 80 * @since 3 81 */ 82 int CONTEXT_INCLUDE_CODE = 0x00000001; 83 84 /** 85 * static final int CONTEXT_IGNORE_SECURITY 86 * Indicates the flag used with createBundleContext(java.lang.String,int) for creating a Context 87 * object that can always be loaded with all security restrictions ignored. 88 * 89 * The value is 0x00000002. 90 * 91 * @since 3 92 */ 93 int CONTEXT_IGNORE_SECURITY = 0x00000002; 94 95 /** 96 * static final int CONTEXT_RESTRICTED 97 * Indicates the flag used with createBundleContext(java.lang.String,int) for creating a Context 98 * object in which particular features may be disabled. 99 * 100 * The value is 0x00000004. 101 * 102 * @since 3 103 */ 104 int CONTEXT_RESTRICTED = 0x00000004; 105 106 int CONTEXT_RESOUCE_ONLY = 0x00000008; 107 108 /** 109 * Called when getting the ProcessInfo 110 * 111 * @return ProcessInfo 112 */ 113 virtual std::shared_ptr<ProcessInfo> GetProcessInfo() const = 0; 114 115 /** 116 * @brief Obtains information about the current application. The returned application information includes basic 117 * information such as the application name and application permissions. 118 * 119 * @return Returns the ApplicationInfo for the current application. 120 */ 121 virtual std::shared_ptr<ApplicationInfo> GetApplicationInfo() const = 0; 122 123 /** 124 * @brief Obtains the Context object of the application. 125 * 126 * @return Returns the Context object of the application. 127 */ 128 virtual std::shared_ptr<Context> GetApplicationContext() const = 0; 129 130 /** 131 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources, 132 * source code, and configuration files of a module. 133 * 134 * @return Returns the path of the package file. 135 */ 136 virtual std::string GetBundleCodePath() = 0; 137 138 /** 139 * @brief Obtains information about the current ability. 140 * The returned information includes the class name, bundle name, and other information about the current ability. 141 * 142 * @return Returns the AbilityInfo object for the current ability. 143 */ 144 virtual const std::shared_ptr<AbilityInfo> GetAbilityInfo() = 0; 145 146 /** 147 * @brief Obtains the Context object of the application. 148 * 149 * @return Returns the Context object of the application. 150 */ 151 virtual std::shared_ptr<Context> GetContext() = 0; 152 153 /** 154 * @brief Obtains an IBundleMgr instance. 155 * You can use this instance to obtain information about the application bundle. 156 * 157 * @return Returns an IBundleMgr instance. 158 */ 159 virtual sptr<IBundleMgr> GetBundleManager() const = 0; 160 161 /** 162 * @brief Obtains a resource manager. 163 * 164 * @return Returns a ResourceManager object. 165 */ 166 virtual std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const = 0; 167 168 /** 169 * @brief Deletes the specified private file associated with the application. 170 * 171 * @param fileName Indicates the name of the file to delete. The file name cannot contain path separators. 172 * 173 * @return Returns true if the file is deleted successfully; returns false otherwise. 174 */ 175 virtual bool DeleteFile(const std::string &fileName) = 0; 176 177 /** 178 * @brief Destroys another ability that uses the AbilityInfo.AbilityType.SERVICE template. 179 * The current ability using either the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE 180 * template can call this method to destroy another ability that uses the AbilityInfo.AbilityType.SERVICE 181 * template. The current ability itself can be destroyed by calling the terminateAbility() method. 182 * 183 * @param want Indicates the Want containing information about the ability to destroy. 184 * 185 * @return Returns true if the ability is destroyed successfully; returns false otherwise. 186 */ 187 virtual bool StopAbility(const AAFwk::Want &want) = 0; 188 189 /** 190 * @brief Obtains the application-specific cache directory on the device's internal storage. The system 191 * automatically deletes files from the cache directory if disk space is required elsewhere on the device. 192 * Older files are always deleted first. 193 * 194 * @return Returns the application-specific cache directory. 195 */ 196 virtual std::string GetCacheDir() = 0; 197 198 /** 199 * @brief Obtains the application-specific code-cache directory on the device's internal storage. 200 * The system will delete any files stored in this location both when your specific application is upgraded, 201 * and when the entire platform is upgraded. 202 * 203 * @return Returns the application-specific code-cache directory. 204 */ 205 virtual std::string GetCodeCacheDir() = 0; 206 207 /** 208 * @brief Obtains the local database path. 209 * If the local database path does not exist, the system creates one and returns the created path. 210 * 211 * @return Returns the local database file. 212 */ 213 virtual std::string GetDatabaseDir() = 0; 214 215 /** 216 * @brief Obtains the absolute path where all private data files of this application are stored. 217 * 218 * @return Returns the absolute path storing all private data files of this application. 219 */ 220 virtual std::string GetDataDir() = 0; 221 222 /** 223 * @brief Obtains the directory for storing custom data files of the application. 224 * You can use the returned File object to create and access files in this directory. The files 225 * can be accessible only by the current application. 226 * 227 * @param name Indicates the name of the directory to retrieve. This directory is created as part 228 * of your application data. 229 * @param mode Indicates the file operating mode. The value can be 0 or a combination of MODE_PRIVATE. 230 * 231 * @return Returns a File object for the requested directory. 232 */ 233 virtual std::string GetDir(const std::string &name, int mode) = 0; 234 235 /** 236 * @brief Obtains the absolute path to the application-specific cache directory 237 * on the primary external or shared storage device. 238 * 239 * @return Returns the absolute path to the application-specific cache directory on the external or 240 * shared storage device; returns null if the external or shared storage device is temporarily unavailable. 241 */ 242 virtual std::string GetExternalCacheDir() = 0; 243 244 /** 245 * @brief Obtains the absolute path to the directory for storing files for the application on the 246 * primary external or shared storage device. 247 * 248 * @param type Indicates the type of the file directory to return 249 * 250 * @return Returns the absolute path to the application file directory on the external or shared storage 251 * device; returns null if the external or shared storage device is temporarily unavailable. 252 */ 253 virtual std::string GetExternalFilesDir(std::string &type) = 0; 254 255 /** 256 * @brief Obtains the directory for storing files for the application on the device's internal storage. 257 * 258 * @return Returns the application file directory. 259 */ 260 virtual std::string GetFilesDir() = 0; 261 262 /** 263 * @brief Obtains the absolute path which app created and will be excluded from automatic backup to remote storage. 264 * The returned path maybe changed if the application is moved to an adopted storage device. 265 * 266 * @return The path of the directory holding application files that will not be automatically backed up to remote 267 * storage. 268 */ 269 virtual std::string GetNoBackupFilesDir() = 0; 270 271 /** 272 * @brief Checks whether the current process has the given permission. 273 * You need to call requestPermissionsFromUser(std::vector<std::string>,std::vector<int>, int) to request 274 * a permission only if the current process does not have the specific permission. 275 * 276 * @param permission Indicates the permission to check. This parameter cannot be null. 277 * 278 * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission; 279 * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise. 280 */ 281 virtual int VerifySelfPermission(const std::string &permission) = 0; 282 283 /** 284 * @brief Obtains the bundle name of the current ability. 285 * 286 * @return Returns the bundle name of the current ability. 287 */ 288 virtual std::string GetBundleName() const = 0; 289 290 /** 291 * @brief Obtains the path of the OHOS Ability Package (HAP} containing this ability. 292 * 293 * @return Returns the path of the HAP containing this ability. 294 */ 295 virtual std::string GetBundleResourcePath() = 0; 296 297 /** 298 * @brief Starts a new ability. 299 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 300 * to start a specific ability. The system locates the target ability from installed abilities based on the value 301 * of the want parameter and then starts it. You can specify the ability to start using the want parameter. 302 * 303 * @param want Indicates the Want containing information about the target ability to start. 304 * 305 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 306 * template is started. You can define the request code to identify the results returned by abilities. The value 307 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 308 * template. 309 * 310 * @return errCode ERR_OK on success, others on failure. 311 */ 312 virtual ErrCode StartAbility(const AAFwk::Want &want, int requestCode) = 0; 313 314 /** 315 * @brief Remove permissions for all users who have access to specific permissions 316 * 317 * @param permission Indicates the permission to unauth. This parameter cannot be null. 318 * @param uri Indicates the URI to unauth. This parameter cannot be null. 319 * @param uid Indicates the UID of the unauth to check. 320 * 321 */ 322 virtual void UnauthUriPermission(const std::string &permission, const Uri &uri, int uid) = 0; 323 324 /** 325 * @brief Obtains an ability manager. 326 * The ability manager provides information about running processes and memory usage of an application. 327 * 328 * @return Returns an IAbilityManager instance. 329 */ 330 virtual sptr<AAFwk::IAbilityManager> GetAbilityManager() = 0; 331 332 /** 333 * @brief Obtains the type of this application. 334 * 335 * @return Returns system if this application is a system application; 336 * returns normal if it is released in OHOS AppGallery; 337 * returns other if it is released by a third-party vendor; 338 * returns an empty string if the query fails. 339 */ 340 virtual std::string GetAppType() = 0; 341 342 /** 343 * @brief Destroys another ability you had previously started by calling Ability.startAbilityForResult 344 * (ohos.aafwk.content.Want, int, ohos.aafwk.ability.startsetting.AbilityStartSetting) with the same requestCode 345 * passed. 346 * 347 * @param requestCode Indicates the request code passed for starting the ability. 348 * 349 * @return errCode ERR_OK on success, others on failure. 350 */ 351 virtual ErrCode TerminateAbility(int requestCode) = 0; 352 353 /** 354 * @brief Destroys the current ability. 355 * 356 * @return errCode ERR_OK on success, others on failure. 357 */ 358 virtual ErrCode TerminateAbility() = 0; 359 360 /** 361 * @brief Query whether the application of the specified PID and UID has been granted a certain permission 362 * 363 * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null. 364 * @param pid Process id 365 * @param uid 366 * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission; 367 * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise. 368 */ 369 virtual int VerifyPermission(const std::string &permission, int pid, int uid) = 0; 370 371 /** 372 * @brief Obtains the distributed file path. 373 * If the distributed file path does not exist, the system creates one and returns the created path. This method is 374 * applicable only to the context of an ability rather than that of an application. 375 * 376 * @return Returns the distributed file. 377 */ 378 virtual std::string GetDistributedDir() = 0; 379 380 /** 381 * @brief Sets the pattern of this Context based on the specified pattern ID. 382 * 383 * @param patternId Indicates the resource ID of the pattern to set. 384 */ 385 virtual void SetPattern(int patternId) = 0; 386 387 /** 388 * @brief Obtains the Context object of this ability. 389 * 390 * @return Returns the Context object of this ability. 391 */ 392 virtual std::shared_ptr<Context> GetAbilityPackageContext() = 0; 393 394 /** 395 * @brief Obtains the HapModuleInfo object of the application. 396 * 397 * @return Returns the HapModuleInfo object of the application. 398 */ 399 virtual std::shared_ptr<HapModuleInfo> GetHapModuleInfo() = 0; 400 401 /** 402 * @brief Obtains the name of the current process. 403 * 404 * @return Returns the current process name. 405 */ 406 virtual std::string GetProcessName() = 0; 407 408 /** 409 * @brief Obtains the bundle name of the ability that called the current ability. 410 * You can use the obtained bundle name to check whether the calling ability is allowed to receive the data you will 411 * send. If you did not use Ability.startAbilityForResult(ohos.aafwk.content.Want, int, 412 * ohos.aafwk.ability.startsetting.AbilityStartSetting) to start the calling ability, null is returned. 413 * 414 * @return Returns the bundle name of the calling ability; returns null if no calling ability is available. 415 */ 416 virtual std::string GetCallingBundle() = 0; 417 418 /** 419 * @brief Requests certain permissions from the system. 420 * This method is called for permission request. This is an asynchronous method. When it is executed, 421 * the task will be called back. 422 * 423 * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null. 424 * @param permissionsState Indicates the list of permissions' state to be requested. This parameter cannot be null. 425 * @param task The callback or promise fo js interface. 426 */ 427 virtual void RequestPermissionsFromUser(std::vector<std::string> &permissions, std::vector<int> &permissionsState, 428 PermissionRequestTask &&task) = 0; 429 430 /** 431 * @brief Starts a new ability with special ability start setting. 432 * 433 * @param want Indicates the Want containing information about the target ability to start. 434 * @param requestCode Indicates the request code returned after the ability is started. You can define the request 435 * code to identify the results returned by abilities. The value ranges from 0 to 65535. 436 * @param abilityStartSetting Indicates the special start setting used in starting ability. 437 * 438 * @return errCode ERR_OK on success, others on failure. 439 */ 440 virtual ErrCode StartAbility(const Want &want, int requestCode, const AbilityStartSetting &abilityStartSetting) = 0; 441 442 /** 443 * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. 444 * 445 * @param want Indicates the want containing information about the ability to connect 446 * 447 * @param conn Indicates the callback object when the target ability is connected. 448 * 449 * @return True means success and false means failure 450 */ 451 virtual bool ConnectAbility(const Want &want, const sptr<AAFwk::IAbilityConnection> &conn) = 0; 452 453 /** 454 * @brief Disconnects the current ability from an ability 455 * 456 * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection 457 * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities. 458 * 459 * @return errCode ERR_OK on success, others on failure. 460 */ 461 virtual ErrCode DisconnectAbility(const sptr<AAFwk::IAbilityConnection> &conn) = 0; 462 463 /** 464 * @brief Obtains information about the caller of this ability. 465 * 466 * @return Returns the caller information. 467 */ 468 virtual Uri GetCaller() = 0; 469 470 /** 471 * @brief Get the string of this Context based on the specified resource ID. 472 * 473 * @param resId Indicates the resource ID of the string to get. 474 * 475 * @return Returns the string of this Context. 476 */ 477 virtual std::string GetString(int resId) = 0; 478 479 /** 480 * @brief Get the string array of this Context based on the specified resource ID. 481 * 482 * @param resId Indicates the resource ID of the string array to get. 483 * 484 * @return Returns the string array of this Context. 485 */ 486 virtual std::vector<std::string> GetStringArray(int resId) = 0; 487 488 /** 489 * @brief Get the integer array of this Context based on the specified resource ID. 490 * 491 * @param resId Indicates the resource ID of the integer array to get. 492 * 493 * @return Returns the integer array of this Context. 494 */ 495 virtual std::vector<int> GetIntArray(int resId) = 0; 496 497 /** 498 * @brief Obtains the theme of this Context. 499 * 500 * @return theme Returns the theme of this Context. 501 */ 502 virtual std::map<std::string, std::string> GetTheme() = 0; 503 504 /** 505 * @brief Sets the theme of this Context based on the specified theme ID. 506 * 507 * @param themeId Indicates the resource ID of the theme to set. 508 */ 509 virtual void SetTheme(int themeId) = 0; 510 511 /** 512 * @brief Obtains the pattern of this Context. 513 * 514 * @return getPattern in interface Context 515 */ 516 virtual std::map<std::string, std::string> GetPattern() = 0; 517 518 /** 519 * @brief Get the color of this Context based on the specified resource ID. 520 * 521 * @param resId Indicates the resource ID of the color to get. 522 * 523 * @return Returns the color value of this Context. 524 */ 525 virtual int GetColor(int resId) = 0; 526 527 /** 528 * @brief Obtains the theme id of this Context. 529 * 530 * @return int Returns the theme id of this Context. 531 */ 532 virtual int GetThemeId() = 0; 533 534 /** 535 * @brief 536 * Destroys this Service ability if the number of times it has been started equals the number represented by the 537 * given {@code startId}. This method is the same as calling {@link #terminateAbility} to destroy this Service 538 * ability, except that this method helps you avoid destroying it if a client has requested a Service 539 * ability startup in {@link ohos.aafwk.ability.Ability#onCommand} but you are unaware of it. 540 * 541 * @param startId Indicates the number of startup times of this Service ability passed to 542 * {@link ohos.aafwk.ability.Ability#onCommand}. The {@code startId} is 543 * incremented by 1 every time this ability is started. For example, 544 * if this ability has been started for six times, the value of {@code startId} is {@code 6}. 545 * 546 * @return Returns {@code true} if the {@code startId} matches the number of startup times 547 * and this Service ability will be destroyed; returns {@code false} otherwise. 548 */ 549 virtual bool TerminateAbilityResult(int startId) = 0; 550 551 /** 552 * @brief Obtains the current display orientation of this ability. 553 * 554 * @return Returns the current display orientation. 555 */ 556 virtual int GetDisplayOrientation() = 0; 557 558 /** 559 * @brief Obtains the path storing the preference file of the application. 560 * If the preference file path does not exist, the system creates one and returns the created path. 561 * 562 * @return Returns the preference file path . 563 */ 564 virtual std::string GetPreferencesDir() = 0; 565 566 /** 567 * @brief Set color mode 568 * 569 * @param the value of color mode. 570 */ 571 virtual void SetColorMode(int mode) = 0; 572 573 /** 574 * @brief Obtains color mode. 575 * 576 * @return Returns the color mode value. 577 */ 578 virtual int GetColorMode() = 0; 579 580 /** 581 * @brief Obtains the unique ID of the mission containing this ability. 582 * 583 * @return Returns the unique mission ID. 584 */ 585 virtual int GetMissionId() = 0; 586 587 /** 588 * @brief Starts multiple abilities. 589 * 590 * @param wants Indicates the Want containing information array about the target ability to start. 591 */ 592 virtual void StartAbilities(const std::vector<AAFwk::Want> &wants) = 0; 593 594 /** 595 * @brief Checks whether the configuration of this ability is changing. 596 * 597 * @return Returns true if the configuration of this ability is changing and false otherwise. 598 */ 599 virtual bool IsUpdatingConfigurations() = 0; 600 601 /** 602 * @brief Informs the system of the time required for drawing this Page ability. 603 * 604 * @return Returns the notification is successful or fail 605 */ 606 virtual bool PrintDrawnCompleted() = 0; 607 608 friend DataAbilityHelperImpl; 609 friend OHOS::DataShare::DataShareHelper; 610 friend ContinuationConnector; 611 612 protected: 613 virtual sptr<IRemoteObject> GetToken() = 0; 614 }; 615 } // namespace AppExecFwk 616 } // namespace OHOS 617 #endif // OHOS_ABILITY_RUNTIME_CONTEXT_H 618