1 /* 2 * Copyright (c) 2022-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_SERVICE_EXTENSION_CONTEXT_H 17 #define OHOS_ABILITY_RUNTIME_SERVICE_EXTENSION_CONTEXT_H 18 19 #include "extension_context.h" 20 21 #include "ability_connect_callback.h" 22 #include "connection_manager.h" 23 #include "local_call_container.h" 24 #include "start_options.h" 25 #include "want.h" 26 27 namespace OHOS { 28 namespace AbilityRuntime { 29 /** 30 * @brief context supply for service 31 * 32 */ 33 class ServiceExtensionContext : public ExtensionContext { 34 public: 35 ServiceExtensionContext() = default; 36 virtual ~ServiceExtensionContext() = default; 37 38 /** 39 * @brief Starts a new ability. 40 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 41 * to start a specific ability. The system locates the target ability from installed abilities based on the value 42 * of the want parameter and then starts it. You can specify the ability to start using the want parameter. 43 * 44 * @param want Indicates the Want containing information about the target ability to start. 45 * 46 * @return errCode ERR_OK on success, others on failure. 47 */ 48 ErrCode StartAbility(const AAFwk::Want &want) const; 49 50 ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions) const; 51 52 /** 53 * @brief Starts a new ability using the original caller information. 54 * Start a new ability as if it was started by the ability that started current ability. This is for the confirm 55 * ability and selection ability, which passthrough their want to the target. 56 * 57 * @param want Indicates the Want containing information about the target ability to start. 58 * 59 * @return errCode ERR_OK on success, others on failure. 60 */ 61 ErrCode StartAbilityAsCaller(const AAFwk::Want &want) const; 62 63 ErrCode StartAbilityAsCaller(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions) const; 64 65 /** 66 * call function by callback object 67 * 68 * @param want Request info for ability. 69 * @param callback Indicates the callback object. 70 * @param accountId Indicates the account to start. 71 * 72 * @return Returns zero on success, others on failure. 73 */ 74 ErrCode StartAbilityByCall(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback, 75 int32_t accountId = DEFAULT_INVAL_VALUE); 76 77 /** 78 * caller release by callback object 79 * 80 * @param callback Indicates the callback object. 81 * 82 * @return Returns zero on success, others on failure. 83 */ 84 ErrCode ReleaseCall(const std::shared_ptr<CallerCallBack> &callback) const; 85 86 /** 87 * clear failed call connection by callback object 88 * 89 * @param callback Indicates the callback object. 90 * 91 * @return void. 92 */ 93 void ClearFailedCallConnection(const std::shared_ptr<CallerCallBack> &callback) const; 94 95 /** 96 * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. 97 * 98 * @param want Indicates the want containing information about the ability to connect 99 * 100 * @param conn Indicates the callback object when the target ability is connected. 101 * 102 * @return Returns zero on success, others on failure. 103 */ 104 ErrCode ConnectAbility( 105 const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const; 106 107 /** 108 * @brief Starts a new ability. 109 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 110 * to start a specific ability. The system locates the target ability from installed abilities based on the value 111 * of the want parameter and then starts it. You can specify the ability to start using the want parameter. 112 * 113 * @param want Indicates the Want containing information about the target ability to start. 114 * @param accountId caller user. 115 * 116 * @return errCode ERR_OK on success, others on failure. 117 */ 118 ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId) const; 119 120 ErrCode StartAbilityWithAccount( 121 const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions) const; 122 123 ErrCode StartServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1) const; 124 125 ErrCode StopServiceExtensionAbility(const AAFwk::Want& want, int32_t accountId = -1) const; 126 127 /** 128 * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. 129 * 130 * @param want Indicates the want containing information about the ability to connect. 131 * 132 * @param accountId caller user. 133 * 134 * @param conn Indicates the callback object when the target ability is connected. 135 * 136 * @return Returns zero on success, others on failure. 137 */ 138 ErrCode ConnectAbilityWithAccount( 139 const AAFwk::Want &want, int accountId, const sptr<AbilityConnectCallback> &connectCallback) const; 140 141 /** 142 * @brief Disconnects the current ability from an ability. 143 * 144 * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection 145 * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities. 146 * 147 * @return errCode ERR_OK on success, others on failure. 148 */ 149 ErrCode DisconnectAbility( 150 const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const; 151 152 /** 153 * @brief Destroys the current ability. 154 * 155 * @return errCode ERR_OK on success, others on failure. 156 */ 157 ErrCode TerminateAbility(); 158 159 using SelfType = ServiceExtensionContext; 160 static const size_t CONTEXT_TYPE_ID; 161 162 protected: IsContext(size_t contextTypeId)163 bool IsContext(size_t contextTypeId) override 164 { 165 return contextTypeId == CONTEXT_TYPE_ID || ExtensionContext::IsContext(contextTypeId); 166 } 167 168 private: 169 static int ILLEGAL_REQUEST_CODE; 170 std::shared_ptr<LocalCallContainer> localCallContainer_ = nullptr; 171 172 /** 173 * @brief Get Current Ability Type 174 * 175 * @return Current Ability Type 176 */ 177 OHOS::AppExecFwk::AbilityType GetAbilityInfoType() const; 178 }; 179 } // namespace AbilityRuntime 180 } // namespace OHOS 181 #endif // OHOS_ABILITY_RUNTIME_SERVICE_EXTENSION_CONTEXT_H 182