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