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