1 /* 2 * Copyright (c) 2021-2025 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_ABILITY_CONTEXT_H 17 #define OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_H 18 19 #include "ability_connect_callback.h" 20 #include "ability_info.h" 21 #include "ability_lifecycle_observer_interface.h" 22 #include "caller_callback.h" 23 #include "context.h" 24 #include "free_install_observer_interface.h" 25 #include "iability_callback.h" 26 #include "js_ui_extension_callback.h" 27 #include "mission_info.h" 28 #include "native_engine/native_reference.h" 29 #include "native_engine/native_value.h" 30 #include "start_options.h" 31 #include "want.h" 32 #include <functional> 33 34 #ifdef SUPPORT_GRAPHICS 35 #ifdef SUPPORT_SCREEN 36 #include "pixel_map.h" 37 #endif 38 #endif 39 40 namespace OHOS { 41 namespace AppExecFwk { 42 class Configuration; 43 } 44 namespace Ace { 45 class UIContent; 46 } 47 48 namespace AbilityRuntime { 49 using RuntimeTask = std::function<void(int, const AAFwk::Want&, bool)>; 50 using PermissionRequestTask = std::function<void(const std::vector<std::string>&, const std::vector<int>&)>; 51 using RequestDialogResultTask = std::function<void(int32_t resultCode, const AAFwk::Want&)>; 52 using AbilityConfigUpdateCallback = std::function<void(AppExecFwk::Configuration &config)>; 53 class LocalCallContainer; 54 constexpr int32_t DEFAULT_INVAL_VALUE = -1; 55 class AbilityContext : public Context { 56 public: 57 virtual ~AbilityContext() = default; 58 59 /** 60 * @brief Starts a new ability. 61 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 62 * to start a specific ability. The system locates the target ability from installed abilities based on the value 63 * of the want parameter and then starts it. You can specify the ability to start using the want parameter. 64 * 65 * @param want Indicates the Want containing information about the target ability to start. 66 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 67 * template is started. You can define the request code to identify the results returned by abilities. The value 68 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 69 * template. 70 */ 71 virtual ErrCode StartAbility(const AAFwk::Want &want, int requestCode) = 0; 72 73 /** 74 * @brief Starts a new ability. 75 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 76 * to start a specific ability. The system locates the target ability from installed abilities based on the value 77 * of the want parameter and then starts it. You can specify the ability to start using the want parameter. 78 * 79 * @param want Indicates the Want containing information about the target ability to start. 80 * @param accountId ability caller accountId. 81 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 82 * template is started. You can define the request code to identify the results returned by abilities. The value 83 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 84 * template. 85 */ 86 virtual ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId, int requestCode) = 0; 87 88 /** 89 * @brief Starts a new ability. 90 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 91 * to start a specific ability. The system locates the target ability from installed abilities based on the value 92 * of the want parameter and the value of the start options and then starts it. You can specify the ability to 93 * start using the two parameters. 94 * 95 * @param want Indicates the Want containing application side information about the target ability to start. 96 * @param startOptions Indicates the StartOptions containing service side information about the target ability to 97 * start. 98 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 99 * template is started. You can define the request code to identify the results returned by abilities. The value 100 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 101 * template. 102 */ 103 virtual ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode) = 0; 104 105 /** 106 * @brief Starts a new ability using the original caller information. 107 * Start a new ability as if it was started by the ability that started current ability. This is for the confirm 108 * ability and selection ability, which passthrough their want to the target. 109 * 110 * @param want Indicates the Want containing information about the target ability to start. 111 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 112 * template is started. You can define the request code to identify the results returned by abilities. The value 113 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 114 * template. 115 */ 116 virtual ErrCode StartAbilityAsCaller(const AAFwk::Want &want, int requestCode) = 0; 117 118 /** 119 * @brief Starts a new ability using the original caller information. 120 * Start a new ability as if it was started by the ability that started current ability. This is for the confirm 121 * ability and selection ability, which passthrough their want to the target. 122 * 123 * @param want Indicates the Want containing information about the target ability to start. 124 * @param startOptions Indicates the StartOptions containing service side information about the target ability to 125 * start. 126 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 127 * template is started. You can define the request code to identify the results returned by abilities. The value 128 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 129 * template. 130 */ 131 virtual ErrCode StartAbilityAsCaller(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, 132 int requestCode) = 0; 133 134 /** 135 * @brief Starts a new ability. 136 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 137 * to start a specific ability. The system locates the target ability from installed abilities based on the value 138 * of the want parameter and the value of the start options and then starts it. You can specify the ability to 139 * start using the two parameters. 140 * 141 * @param want Indicates the Want containing application side information about the target ability to start. 142 * @param accountId caller userId. 143 * @param startOptions Indicates the StartOptions containing service side information about the target ability to 144 * start. 145 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 146 * template is started. You can define the request code to identify the results returned by abilities. The value 147 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 148 * template. 149 */ 150 virtual ErrCode StartAbilityWithAccount( 151 const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions, int requestCode) = 0; 152 153 virtual ErrCode StartAbilityForResult(const AAFwk::Want &Want, int requestCode, RuntimeTask &&task) = 0; 154 155 virtual ErrCode StartAbilityForResultWithAccount( 156 const AAFwk::Want &Want, int accountId, int requestCode, RuntimeTask &&task) = 0; 157 158 virtual ErrCode StartAbilityForResult(const AAFwk::Want &Want, const AAFwk::StartOptions &startOptions, 159 int requestCode, RuntimeTask &&task) = 0; 160 161 virtual ErrCode StartAbilityForResultWithAccount(const AAFwk::Want &Want, int accountId, 162 const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task) = 0; 163 164 virtual ErrCode StartServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1) = 0; 165 166 virtual ErrCode StartUIServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1) = 0; 167 168 virtual ErrCode StopServiceExtensionAbility(const AAFwk::Want& want, int32_t accountId = -1) = 0; 169 170 virtual ErrCode TerminateAbilityWithResult(const AAFwk::Want &want, int resultCode) = 0; 171 172 virtual ErrCode BackToCallerAbilityWithResult(const AAFwk::Want &want, int resultCode, int64_t requestCode) = 0; 173 174 virtual ErrCode RestoreWindowStage(napi_env env, napi_value contentStorage) = 0; 175 176 virtual void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData) = 0; 177 178 virtual ErrCode RequestModalUIExtension(const AAFwk::Want& want) = 0; 179 180 virtual ErrCode OpenLink(const AAFwk::Want& want, int requestCode) = 0; 181 182 virtual ErrCode OpenAtomicService(AAFwk::Want& want, const AAFwk::StartOptions &options, int requestCode, 183 RuntimeTask &&task) = 0; 184 185 virtual ErrCode AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> &observer) = 0; 186 ChangeAbilityVisibility(bool isShow)187 virtual ErrCode ChangeAbilityVisibility(bool isShow) { return 0; } 188 189 /** 190 * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. 191 * 192 * @param want Indicates the want containing information about the ability to connect 193 * @param connectCallback Indicates the callback object when the target ability is connected. 194 * @return True means success and false means failure 195 */ 196 virtual ErrCode ConnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) = 0; 197 198 /** 199 * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. 200 * @param want Indicates the want containing information about the ability to connect 201 * @param accountId caller userId. 202 * @param connectCallback Indicates the callback object when the target ability is connected. 203 * @return True means success and false means failure 204 */ 205 virtual ErrCode ConnectAbilityWithAccount(const AAFwk::Want &want, int accountId, 206 const sptr<AbilityConnectCallback> &connectCallback) = 0; 207 208 /** 209 * @brief Connects the current ability to an uiService ability using the AbilityInfo.AbilityType.SERVICE template. 210 * 211 * @param want Indicates the want containing information about the ability to connect 212 * @param connectCallback Indicates the callback object when the target ability is connected. 213 * @return True means success and false means failure 214 */ 215 virtual ErrCode ConnectUIServiceExtensionAbility(const AAFwk::Want& want, 216 const sptr<AbilityConnectCallback>& connectCallback) = 0; 217 StartExtensionAbilityWithExtensionType(const AAFwk::Want & want,AppExecFwk::ExtensionAbilityType extensionType)218 virtual ErrCode StartExtensionAbilityWithExtensionType(const AAFwk::Want &want, 219 AppExecFwk::ExtensionAbilityType extensionType) 220 { 221 return 0; 222 } StopExtensionAbilityWithExtensionType(const AAFwk::Want & want,AppExecFwk::ExtensionAbilityType extensionType)223 virtual ErrCode StopExtensionAbilityWithExtensionType(const AAFwk::Want& want, 224 AppExecFwk::ExtensionAbilityType extensionType) 225 { 226 return 0; 227 } 228 /** 229 * @brief Connects the current ability to an extension ability using the AbilityInfo.AbilityType.SERVICE template. 230 * 231 * @param want Indicates the want containing information about the ability to connect 232 * @param connectCallback Indicates the callback object when the target ability is connected. 233 * @return True means success and false means failure 234 */ ConnectExtensionAbilityWithExtensionType(const AAFwk::Want & want,const sptr<AbilityConnectCallback> & connectCallback,AppExecFwk::ExtensionAbilityType extensionType)235 virtual ErrCode ConnectExtensionAbilityWithExtensionType(const AAFwk::Want& want, 236 const sptr<AbilityConnectCallback>& connectCallback, AppExecFwk::ExtensionAbilityType extensionType) 237 { 238 return 0; 239 } 240 241 /** 242 * @brief Disconnects the current ability from an ability 243 * 244 * @param want Indicates the want containing information about the ability to disconnect 245 * @param connectCallback Indicates the callback object when the target ability is connected. 246 * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities. 247 */ 248 virtual void DisconnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback, 249 int32_t accountId = -1) = 0; 250 251 /** 252 * @brief get ability info of the current ability 253 * 254 * @return the ability info of the current ability 255 */ 256 virtual std::shared_ptr<AppExecFwk::AbilityInfo> GetAbilityInfo() const = 0; 257 258 /** 259 * @brief Minimize the current ability. 260 * 261 * @param fromUser mark the minimize operation source. 262 */ 263 virtual void MinimizeAbility(bool fromUser = false) = 0; 264 265 /** 266 * @brief Get OnBackPressedCallBack. 267 * @param needMoveToBackground true if ability will be moved to background; false if ability will be terminated. 268 * 269 * @return Returns ERR_OK on success, others on failure. 270 */ 271 virtual ErrCode OnBackPressedCallBack(bool &needMoveToBackground) = 0; 272 273 virtual ErrCode MoveAbilityToBackground() = 0; 274 275 virtual ErrCode MoveUIAbilityToBackground() = 0; 276 277 virtual ErrCode TerminateSelf() = 0; 278 279 virtual ErrCode CloseAbility() = 0; 280 281 /** 282 * @brief Get ContentStorage. 283 * 284 * @return Returns the ContentStorage. 285 */ 286 virtual std::unique_ptr<NativeReference>& GetContentStorage() = 0; 287 288 /** 289 * call function by callback object 290 * 291 * @param want Request info for ability. 292 * @param callback Indicates the callback object. 293 * @param accountId Indicates the account to start. 294 * 295 * @return Returns zero on success, others on failure. 296 */ 297 virtual ErrCode StartAbilityByCall(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback, 298 int32_t accountId = DEFAULT_INVAL_VALUE) = 0; 299 300 /** 301 * caller release by callback object 302 * 303 * @param callback Indicates the callback object. 304 * 305 * @return Returns zero on success, others on failure. 306 */ 307 virtual ErrCode ReleaseCall(const std::shared_ptr<CallerCallBack> &callback) = 0; 308 309 /** 310 * clear failed call connection by callback object 311 * 312 * @param callback Indicates the callback object. 313 * 314 * @return void. 315 */ 316 virtual void ClearFailedCallConnection(const std::shared_ptr<CallerCallBack> &callback) = 0; 317 318 /** 319 * @brief Get LocalCallContainer. 320 * 321 * @return Returns the LocalCallContainer. 322 */ 323 virtual std::shared_ptr<LocalCallContainer> GetLocalCallContainer() = 0; 324 325 virtual void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config) = 0; 326 327 virtual void RegisterAbilityCallback(std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback) = 0; 328 329 virtual void SetWeakSessionToken(const wptr<IRemoteObject>& sessionToken) = 0; 330 virtual void SetAbilityRecordId(int32_t abilityRecordId) = 0; 331 virtual int32_t GetAbilityRecordId() = 0; 332 333 /** 334 * @brief Requests dialogService from the system. 335 * This method is called for dialog request. This is an asynchronous method. When it is executed, 336 * the task will be called back. 337 * 338 * @param env js env. 339 * @param want Indicates the dialog service to be requested. 340 * @param task The callback or promise fo js interface. 341 * @return Returns ERR_OK if success. 342 */ 343 virtual ErrCode RequestDialogService(napi_env env, AAFwk::Want &want, RequestDialogResultTask &&task) = 0; 344 345 /** 346 * @brief Requests dialogService from the system. 347 * This method is called for dialog request. This is an asynchronous method. When it is executed, 348 * the task will be called back. 349 * 350 * @param want Indicates the dialog service to be requested. 351 * @param task The callback or promise fo js interface. 352 * @return Returns ERR_OK if success. 353 */ 354 virtual ErrCode RequestDialogService(AAFwk::Want &want, RequestDialogResultTask &&task) = 0; 355 356 /** 357 * @brief Report drawn completed. 358 * 359 * @return Returns ERR_OK on success, others on failure. 360 */ 361 virtual ErrCode ReportDrawnCompleted() = 0; 362 363 virtual ErrCode GetMissionId(int32_t &missionId) = 0; 364 365 /** 366 * Set mission continue state of this ability. 367 * 368 * @param state the mission continuation state of this ability. 369 * @return Returns ERR_OK if success. 370 */ 371 virtual ErrCode SetMissionContinueState(const AAFwk::ContinueState &state) = 0; 372 373 /** 374 * Register lifecycle observer on ability. 375 * 376 * @param observer the lifecycle observer to be registered on ability. 377 */ 378 virtual void RegisterAbilityLifecycleObserver(const std::shared_ptr<AppExecFwk::ILifecycleObserver> &observer) = 0; 379 380 /** 381 * Unregister lifecycle observer on ability. 382 * 383 * @param observer the lifecycle observer to be unregistered on ability. 384 */ 385 virtual void UnregisterAbilityLifecycleObserver( 386 const std::shared_ptr<AppExecFwk::ILifecycleObserver> &observer) = 0; 387 388 virtual void SetRestoreEnabled(bool enabled) = 0; 389 virtual bool GetRestoreEnabled() = 0; 390 virtual void RegisterAbilityConfigUpdateCallback(AbilityConfigUpdateCallback abilityConfigUpdateCallback) = 0; 391 virtual std::shared_ptr<AppExecFwk::Configuration> GetAbilityConfiguration() const = 0; 392 virtual void SetAbilityConfiguration(const AppExecFwk::Configuration &config) = 0; 393 virtual void SetAbilityColorMode(int32_t colorMode) = 0; 394 virtual void SetAbilityResourceManager(std::shared_ptr<Global::Resource::ResourceManager> abilityResourceMgr) = 0; 395 virtual bool GetHookOff() = 0; 396 virtual void SetHookOff(bool hookOff) = 0; 397 virtual ErrCode RevokeDelegator() = 0; 398 virtual ErrCode SetOnNewWantSkipScenarios(int32_t scenarios) = 0; SetScreenMode(int32_t screenMode)399 void SetScreenMode(int32_t screenMode) 400 { 401 screenMode_ = screenMode; 402 } 403 GetScreenMode()404 int32_t GetScreenMode() const 405 { 406 return screenMode_; 407 } 408 409 virtual std::shared_ptr<AAFwk::Want> GetWant() = 0; 410 411 #ifdef SUPPORT_GRAPHICS 412 #ifdef SUPPORT_SCREEN 413 /** 414 * @brief Set mission label of this ability. 415 * 416 * @param label the label of this ability. 417 * @return Returns ERR_OK if success. 418 */ 419 virtual ErrCode SetMissionLabel(const std::string &label) = 0; 420 421 /** 422 * @brief Set mission icon of this ability. 423 * 424 * @param icon the icon of this ability. 425 * @return Returns ERR_OK if success. 426 */ 427 virtual ErrCode SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon) = 0; 428 429 /** 430 * @brief Set ability label and icon of this ability. 431 * 432 * @param label the label of this ability. 433 * @param icon the icon of this ability. 434 * @return Returns ERR_OK if success. 435 */ 436 virtual ErrCode SetAbilityInstanceInfo(const std::string& label, std::shared_ptr<OHOS::Media::PixelMap> icon) = 0; 437 438 virtual int GetCurrentWindowMode() = 0; 439 440 /** 441 * @brief Get window rectangle of this ability. 442 * 443 * @param the left position of window rectangle. 444 * @param the top position of window rectangle. 445 * @param the width position of window rectangle. 446 * @param the height position of window rectangle. 447 */ 448 virtual void GetWindowRect(int32_t &left, int32_t &top, int32_t &width, int32_t &height) = 0; 449 450 /** 451 * @brief Get ui content object. 452 * 453 * @return UIContent object of ACE. 454 */ 455 virtual Ace::UIContent* GetUIContent() = 0; 456 virtual ErrCode StartAbilityByType(const std::string &type, AAFwk::WantParams &wantParam, 457 const std::shared_ptr<JsUIExtensionCallback> &uiExtensionCallbacks) = 0; 458 virtual ErrCode CreateModalUIExtensionWithApp(const AAFwk::Want &want) = 0; 459 virtual void EraseUIExtension(int32_t sessionId) = 0; 460 #endif 461 #endif 462 virtual bool IsTerminating() = 0; 463 virtual bool IsHook() = 0; 464 virtual void SetHook(bool isHook) = 0; 465 virtual void SetTerminating(bool state) = 0; 466 virtual void InsertResultCallbackTask(int requestCode, RuntimeTask&& task) = 0; 467 virtual void RemoveResultCallbackTask(int requestCode) = 0; 468 using SelfType = AbilityContext; 469 static const size_t CONTEXT_TYPE_ID; 470 471 /** 472 * @brief Add CompletioHandler. 473 * 474 * @param requestId, the requestId. 475 * @param onRequestSucc, the callback ot be called upon request success. 476 * @param onRequestFail, the callback ot be called upon request failure. 477 * @return ERR_OK on success, otherwise failure. 478 */ 479 virtual ErrCode AddCompletionHandler(const std::string &requestId, OnRequestResult onRequestSucc, 480 OnRequestResult onRequestFail) = 0; 481 482 /** 483 * @brief Callback on request success. 484 * 485 * @param requestId, the requestId. 486 * @param element, the want element of startAbility. 487 * @param message, the message returned to the callback. 488 */ 489 virtual void OnRequestSuccess(const std::string &requestId, const AppExecFwk::ElementName &element, 490 const std::string &message) = 0; 491 492 /** 493 * @brief Callback on request failure. 494 * 495 * @param requestId, the requestId. 496 * @param element, the want element of startAbility. 497 * @param message, the message returned to the callback. 498 */ 499 virtual void OnRequestFailure(const std::string &requestId, const AppExecFwk::ElementName &element, 500 const std::string &message, int32_t resultCode = 0) = 0; 501 502 virtual ErrCode AddCompletionHandlerForAtomicService(const std::string &requestId, 503 OnAtomicRequestSuccess onRequestSucc, OnAtomicRequestFailure onRequestFail, const std::string &appId) = 0; 504 505 protected: IsContext(size_t contextTypeId)506 bool IsContext(size_t contextTypeId) override 507 { 508 return contextTypeId == CONTEXT_TYPE_ID || Context::IsContext(contextTypeId); 509 } 510 511 private: 512 int32_t screenMode_ = AAFwk::ScreenMode::IDLE_SCREEN_MODE; 513 }; 514 } // namespace AbilityRuntime 515 } // namespace OHOS 516 #endif // OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_H 517