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_PENDING_WANT_H 17 #define OHOS_ABILITY_RUNTIME_PENDING_WANT_H 18 19 #include <string> 20 #include <vector> 21 #include <mutex> 22 #include <memory> 23 #include "cancel_listener.h" 24 #include "context/application_context.h" 25 #include "completed_dispatcher.h" 26 #include "event_handler.h" 27 #include "want.h" 28 #include "want_agent_constant.h" 29 #include "want_params.h" 30 #include "want_receiver_stub.h" 31 #include "want_sender_info.h" 32 #include "want_sender_stub.h" 33 34 namespace OHOS::AbilityRuntime::WantAgent { 35 class PendingWant final : public std::enable_shared_from_this<PendingWant>, public Parcelable { 36 public: PendingWant()37 PendingWant() {}; 38 explicit PendingWant(const sptr<AAFwk::IWantSender> &target); 39 PendingWant(const sptr<AAFwk::IWantSender> &target, const sptr<IRemoteObject> whitelistToken); 40 virtual ~PendingWant() = default; 41 42 WantAgentConstant::OperationType GetType(sptr<AAFwk::IWantSender> target); 43 44 /** 45 * Retrieve a PendingWant that will start a new ability. 46 * 47 * @param context The Context in which this PendingWant should start 48 * the ability. 49 * @param requestCode Private request code for the sender 50 * @param want Want of the ability to be launched. 51 * @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE}, 52 * FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT. 53 * 54 * @return Returns an existing or new PendingWant matching the given 55 * parameters. May return null only if FLAG_NO_CREATE has been 56 * supplied. 57 */ 58 static std::shared_ptr<PendingWant> GetAbility( 59 const std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> &context, int requestCode, 60 const std::shared_ptr<AAFwk::Want> &want, unsigned int flags); 61 62 /** 63 * Retrieve a PendingWant that will start a new ability. 64 * 65 * @param context The Context in which this PendingWant should start 66 * the ability. 67 * @param requestCode Private request code for the sender 68 * @param want Want of the ability to be launched. 69 * @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE, 70 * FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT. 71 * 72 * @return Returns ERR_OK If get PendingWant correctly. 73 */ 74 static ErrCode GetAbility( 75 const std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> &context, int requestCode, 76 const std::shared_ptr<AAFwk::Want> &want, unsigned int flags, 77 const std::shared_ptr<AAFwk::WantParams> &options, 78 std::shared_ptr<PendingWant> &pendingWant); 79 80 /** 81 * Like GetAbility(Context, int, Want, int)}, but allows an 82 * array of Wants to be supplied. The last Want in the array is 83 * taken as the primary key for the PendingWant, like the single Want 84 * given to GetAbility(Context, int, Want, int). 85 * 86 * @param context The Context in which this PendingWant should start 87 * the ability. 88 * @param requestCode Private request code for the sender 89 * @param wants Array of Wants of the abilities to be launched. 90 * @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE, 91 * FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT. 92 * 93 * @return Returns an existing or new PendingWant matching the given 94 * parameters. May return null only if FLAG_NO_CREATE has been 95 * supplied. 96 */ 97 static std::shared_ptr<PendingWant> GetAbilities( 98 const std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> &context, int requestCode, 99 std::vector<std::shared_ptr<AAFwk::Want>> &wants, unsigned int flags); 100 101 /** 102 * Like GetAbility(Context, int, Want, int)}, but allows an 103 * array of Wants to be supplied. The last Want in the array is 104 * taken as the primary key for the PendingWant, like the single Want 105 * given to GetAbility(Context, int, Want, int). 106 * 107 * @param context The Context in which this PendingWant should start 108 * the ability. 109 * @param requestCode Private request code for the sender 110 * @param wants Array of Wants of the abilities to be launched. 111 * @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE, 112 * FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT. 113 * 114 * @return Returns ERR_OK If get PendingWant correctly. 115 */ 116 static ErrCode GetAbilities( 117 const std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> &context, int requestCode, 118 std::vector<std::shared_ptr<AAFwk::Want>> &wants, unsigned int flags, 119 const std::shared_ptr<AAFwk::WantParams> &options, 120 std::shared_ptr<PendingWant> &pendingWant); 121 122 /** 123 * Retrieve a PendingWant that will perform a common event. 124 * 125 * @param context The Context in which this PendingWant should perform 126 * the common event. 127 * @param requestCode Private request code for the sender 128 * @param want The Want to be common event. 129 * @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE, 130 * FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, 131 * FLAG_IMMUTABLE. 132 * 133 * @return Returns ERR_OK If get PendingWant correctly. 134 */ 135 static ErrCode GetCommonEvent( 136 const std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> &context, int requestCode, 137 const std::shared_ptr<AAFwk::Want> &want, unsigned int flags, 138 std::shared_ptr<PendingWant> &pendingWant); 139 140 /** 141 * Note that current user will be interpreted at the time the 142 * common event is sent, not when the pending want is created. 143 */ 144 static ErrCode GetCommonEventAsUser( 145 const std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> &context, 146 int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags, int uid, 147 std::shared_ptr<PendingWant> &pendingWant); 148 149 /** 150 * Retrieve a PendingWant that will start a service. 151 * 152 * @param context The Context in which this PendingWant should start 153 * the service. 154 * @param requestCode Private request code for the sender 155 * @param want An Want describing the service to be started. 156 * @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE, 157 * FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, 158 * FLAG_IMMUTABLE. 159 * 160 * @return Returns ERR_OK If get PendingWant correctly. 161 */ 162 static ErrCode GetService( 163 const std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> &context, 164 int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags, 165 std::shared_ptr<PendingWant> &pendingWant); 166 167 static ErrCode GetServiceExtension( 168 const std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> &context, 169 int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags, 170 std::shared_ptr<PendingWant> &pendingWant); 171 172 /** 173 * Retrieve a PendingWant that will start a foreground service. 174 * 175 * @param context The Context in which this PendingWant should start 176 * the service. 177 * @param requestCode Private request code for the sender 178 * @param want An Want describing the service to be started. 179 * @param flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE, 180 * FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, 181 * FLAG_IMMUTABLE . 182 * 183 * @return Returns ERR_OK If get PendingWant correctly. 184 */ 185 static ErrCode GetForegroundService( 186 const std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> &context, 187 int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags, 188 std::shared_ptr<PendingWant> &pendingWant); 189 190 /** 191 * @description: Marshals a Want into a Parcel. 192 * Fields in the Want are marshalled separately. If any field fails to be marshalled, false is returned. 193 * @param parcel Indicates the Parcel object for marshalling. 194 * @return Returns true if the marshalling is successful; returns false otherwise. 195 */ 196 virtual bool Marshalling(Parcel &parcel) const; 197 198 /** 199 * @description: Unmarshals a Want from a Parcel. 200 * Fields in the Want are unmarshalled separately. If any field fails to be unmarshalled, false is returned. 201 * @param parcel Indicates the Parcel object for unmarshalling. 202 * @return Returns true if the unmarshalling is successful; returns false otherwise. 203 */ 204 static PendingWant *Unmarshalling(Parcel &parcel); 205 206 static ErrCode IsEquals( 207 const std::shared_ptr<PendingWant> &targetPendingWant, const std::shared_ptr<PendingWant> &otherPendingWant); 208 209 ErrCode Cancel(const sptr<AAFwk::IWantSender> &target, uint32_t flags = 0); 210 211 void Send(const sptr<AAFwk::IWantSender> &target); 212 213 void Send(int resultCode, const sptr<AAFwk::IWantSender> &target); 214 215 void Send(int resultCode, 216 const std::shared_ptr<AAFwk::Want> &want, const sptr<AAFwk::IWantSender> &target); 217 218 void Send(int resultCode, const sptr<CompletedDispatcher> &onCompleted, const sptr<AAFwk::IWantSender> &target); 219 220 void Send(int resultCode, 221 const std::shared_ptr<AAFwk::Want> &want, const sptr<CompletedDispatcher> &onCompleted, 222 const sptr<AAFwk::IWantSender> &target); 223 224 void Send(int resultCode, 225 const std::shared_ptr<AAFwk::Want> &want, const sptr<CompletedDispatcher> &onCompleted, 226 const std::string &requiredPermission, const sptr<AAFwk::IWantSender> &target); 227 228 ErrCode Send(int resultCode, const std::shared_ptr<AAFwk::Want> &want, 229 const sptr<CompletedDispatcher> &onCompleted, const std::string &requiredPermission, 230 const std::shared_ptr<AAFwk::WantParams> &options, const std::shared_ptr<AAFwk::StartOptions> &startOptions, 231 const sptr<AAFwk::IWantSender> &target); 232 233 int SendAndReturnResult(int resultCode, const std::shared_ptr<AAFwk::Want> &want, 234 const sptr<CompletedDispatcher> &onCompleted, const std::string &requiredPermission, 235 const std::shared_ptr<AAFwk::WantParams> &options, const std::shared_ptr<AAFwk::StartOptions> &startOptions, 236 const sptr<AAFwk::IWantSender> &target); 237 238 ErrCode GetBundleName(const sptr<AAFwk::IWantSender> &target, std::string &bundleName); 239 240 ErrCode GetUid(const sptr<AAFwk::IWantSender> &target, int32_t &uid); 241 242 sptr<AAFwk::IWantSender> GetTarget(); 243 244 void SetTarget(const sptr<AAFwk::IWantSender> &target); 245 246 void RegisterCancelListener( 247 const std::shared_ptr<CancelListener> &cancelListener, const sptr<AAFwk::IWantSender> &target); 248 249 void NotifyCancelListeners(int32_t resultCode); 250 251 void UnregisterCancelListener( 252 const std::shared_ptr<CancelListener> &cancelListener, const sptr<AAFwk::IWantSender> &target); 253 254 ErrCode GetHashCode(const sptr<AAFwk::IWantSender> &target, int &code); 255 256 std::shared_ptr<AAFwk::Want> GetWant(const sptr<AAFwk::IWantSender> &target); 257 258 std::shared_ptr<AAFwk::WantSenderInfo> GetWantSenderInfo(const sptr<AAFwk::IWantSender> &target); 259 260 // add return error code interface 261 ErrCode GetType(const sptr<AAFwk::IWantSender> &target, int32_t &operType); 262 ErrCode GetWant(const sptr<AAFwk::IWantSender> &target, std::shared_ptr<AAFwk::Want> &want); 263 264 private: 265 std::mutex lock_object; 266 sptr<AAFwk::IWantSender> target_ = nullptr; 267 sptr<AAFwk::IWantReceiver> cancelReceiver_ = nullptr; 268 sptr<IRemoteObject> whitelistToken_ = nullptr; 269 std::vector<std::shared_ptr<CancelListener>> cancelListeners_; 270 271 class CancelReceiver : public AAFwk::WantReceiverStub { 272 public: 273 explicit CancelReceiver(const std::weak_ptr<PendingWant> &outerInstance); 274 virtual ~CancelReceiver() = default; 275 276 void Send(const int32_t resultCode) override; 277 void PerformReceive(const AAFwk::Want &want, int resultCode, const std::string &data, 278 const AAFwk::WantParams &extras, bool serialized, bool sticky, int sendingUser) override; 279 280 private: 281 std::weak_ptr<PendingWant> outerInstance_; 282 }; 283 284 static ErrCode BuildServicePendingWant( 285 const std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> &context, 286 int requestCode, const std::shared_ptr<AAFwk::Want> &want, unsigned int flags, 287 WantAgentConstant::OperationType serviceKind, 288 std::shared_ptr<PendingWant> &pendingWant); 289 }; 290 } // namespace OHOS::AbilityRuntime::WantAgent 291 #endif // OHOS_ABILITY_RUNTIME_PENDING_WANT_H 292