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_MANAGER_H 17 #define OHOS_ABILITY_RUNTIME_PENDING_WANT_MANAGER_H 18 19 #include <mutex> 20 #include <memory> 21 #include <map> 22 #include <vector> 23 #include <string> 24 25 #include "ability_manager_errors.h" 26 #include "ability_record.h" 27 #include "common_event.h" 28 #include "nocopyable.h" 29 #include "pending_want_key.h" 30 #include "pending_want_record.h" 31 #include "pending_want_common_event.h" 32 #include "sender_info.h" 33 #include "want_sender_info.h" 34 35 namespace OHOS { 36 namespace AAFwk { 37 enum class OperationType { 38 /** 39 * Unknown operation. 40 */ 41 UNKNOWN_TYPE, 42 43 /** 44 * Starts an ability with a UI. 45 */ 46 START_ABILITY, 47 48 /** 49 * Starts multiple abilities. 50 */ 51 START_ABILITIES, 52 53 /** 54 * Starts an ability without a UI. 55 */ 56 START_SERVICE, 57 58 /** 59 * Sends a common event. 60 */ 61 SEND_COMMON_EVENT, 62 63 /** 64 * Starts a foreground ability without a UI. 65 */ 66 START_FOREGROUND_SERVICE 67 }; 68 69 enum class Flags { 70 /** 71 * Indicates that the {@link WantAgent} can be used only once. 72 */ 73 ONE_TIME_FLAG = 1 << 30, 74 75 /** 76 * Indicates that {@code null} is returned if the {@link WantAgent} does not exist. 77 */ 78 NO_BUILD_FLAG = 1 << 29, 79 80 /** 81 * Indicates that the existing {@link WantAgent} should be canceled before the new object is generated. 82 */ 83 CANCEL_PRESENT_FLAG = 1 << 28, 84 85 /** 86 * Indicates that the system only replaces the extra data of the existing {@link WantAgent} 87 * with that of the new object. 88 */ 89 UPDATE_PRESENT_FLAG = 1 << 27, 90 91 /** 92 * Indicates that the created {@link WantAgent} should be immutable. 93 */ 94 CONSTANT_FLAG = 1 << 26, 95 96 /** 97 * Indicates that the current value of {@code element} can be replaced 98 * when the {@link WantAgent} is triggered. 99 */ 100 REPLACE_ELEMENT, 101 102 /** 103 * Indicates that the current value of {@code action} can be replaced 104 * when the {@link WantAgent} is triggered. 105 */ 106 REPLACE_ACTION, 107 108 /** 109 * Indicates that the current value of {@code uri} can be replaced when the {@link WantAgent} is triggered. 110 */ 111 REPLACE_URI, 112 113 /** 114 * Indicates that the current value of {@code entities} can be replaced 115 * when the {@link WantAgent} is triggered. 116 */ 117 REPLACE_ENTITIES, 118 119 /** 120 * Indicates that the current value of {@code bundleName} can be replaced 121 * when the {@link WantAgent} is triggered. 122 */ 123 REPLACE_BUNDLE 124 }; 125 126 class PendingWantManager : public std::enable_shared_from_this<PendingWantManager>, public NoCopyable { 127 public: 128 PendingWantManager(); PendingWantManager(const std::shared_ptr<PendingWantManager> & manager)129 explicit PendingWantManager(const std::shared_ptr<PendingWantManager> &manager) {}; 130 virtual ~PendingWantManager(); 131 132 public: 133 sptr<IWantSender> GetWantSender(int32_t callingUid, int32_t uid, const std::string &apl, 134 const WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken); 135 int32_t SendWantSender(const sptr<IWantSender> &target, const SenderInfo &senderInfo); 136 void CancelWantSender(std::string &apl, const sptr<IWantSender> &sender); 137 138 int32_t GetPendingWantUid(const sptr<IWantSender> &target); 139 int32_t GetPendingWantUserId(const sptr<IWantSender> &target); 140 std::string GetPendingWantBundleName(const sptr<IWantSender> &target); 141 int32_t GetPendingWantCode(const sptr<IWantSender> &target); 142 int32_t GetPendingWantType(const sptr<IWantSender> &target); 143 void RegisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &recevier); 144 void UnregisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &recevier); 145 int32_t GetPendingRequestWant(const sptr<IWantSender> &target, std::shared_ptr<Want> &want); 146 int32_t GetWantSenderInfo(const sptr<IWantSender> &target, std::shared_ptr<WantSenderInfo> &info); 147 148 void CancelWantSenderLocked(PendingWantRecord &record, bool cleanAbility); 149 int32_t PendingWantStartAbility( 150 const Want &want, const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid); 151 int32_t PendingWantStartAbilitys(const std::vector<WantsInfo> wnatsInfo, const sptr<IRemoteObject> &callerToken, 152 int32_t requestCode, const int32_t callerUid); 153 int32_t DeviceIdDetermine( 154 const Want &want, const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid); 155 int32_t PendingWantPublishCommonEvent(const Want &want, const SenderInfo &senderInfo, int32_t callerUid, 156 int32_t callerTokenId); 157 void ClearPendingWantRecord(const std::string &bundleName, int32_t uid); 158 159 void Dump(std::vector<std::string> &info); 160 void DumpByRecordId(std::vector<std::string> &info, const std::string &args); 161 162 private: 163 sptr<IWantSender> GetWantSenderLocked(const int32_t callingUid, const int32_t uid, const int32_t userId, 164 WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken); 165 void MakeWantSenderCanceledLocked(PendingWantRecord &record); 166 167 sptr<PendingWantRecord> GetPendingWantRecordByKey(const std::shared_ptr<PendingWantKey> &key); 168 bool CheckPendingWantRecordByKey( 169 const std::shared_ptr<PendingWantKey> &inputKey, const std::shared_ptr<PendingWantKey> &key); 170 171 sptr<PendingWantRecord> GetPendingWantRecordByCode(int32_t code); 172 static int32_t PendingRecordIdCreate(); 173 void ClearPendingWantRecordTask(const std::string &bundleName, int32_t uid); 174 175 private: 176 std::map<std::shared_ptr<PendingWantKey>, sptr<PendingWantRecord>> wantRecords_; 177 std::recursive_mutex mutex_; 178 }; 179 } // namespace AAFwk 180 } // namespace OHOS 181 182 #endif // OHOS_ABILITY_RUNTIME_PENDING_WANT_MANAGER_H 183