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 enum class Flags { 71 /** 72 * Indicates that the {@link WantAgent} can be used only once. 73 */ 74 ONE_TIME_FLAG = 1 << 30, 75 76 /** 77 * Indicates that {@code null} is returned if the {@link WantAgent} does not exist. 78 */ 79 NO_BUILD_FLAG = 1 << 29, 80 81 /** 82 * Indicates that the existing {@link WantAgent} should be canceled before the new object is generated. 83 */ 84 CANCEL_PRESENT_FLAG = 1 << 28, 85 86 /** 87 * Indicates that the system only replaces the extra data of the existing {@link WantAgent} 88 * with that of the new object. 89 */ 90 UPDATE_PRESENT_FLAG = 1 << 27, 91 92 /** 93 * Indicates that the created {@link WantAgent} should be immutable. 94 */ 95 CONSTANT_FLAG = 1 << 26, 96 97 /** 98 * Indicates that the current value of {@code element} can be replaced 99 * when the {@link WantAgent} is triggered. 100 */ 101 REPLACE_ELEMENT, 102 103 /** 104 * Indicates that the current value of {@code action} can be replaced 105 * when the {@link WantAgent} is triggered. 106 */ 107 REPLACE_ACTION, 108 109 /** 110 * Indicates that the current value of {@code uri} can be replaced when the {@link WantAgent} is triggered. 111 */ 112 REPLACE_URI, 113 114 /** 115 * Indicates that the current value of {@code entities} can be replaced 116 * when the {@link WantAgent} is triggered. 117 */ 118 REPLACE_ENTITIES, 119 120 /** 121 * Indicates that the current value of {@code bundleName} can be replaced 122 * when the {@link WantAgent} is triggered. 123 */ 124 REPLACE_BUNDLE 125 }; 126 127 class PendingWantManager : public std::enable_shared_from_this<PendingWantManager>, public NoCopyable { 128 public: 129 PendingWantManager(); PendingWantManager(const std::shared_ptr<PendingWantManager> & manager)130 explicit PendingWantManager(const std::shared_ptr<PendingWantManager> &manager) {}; 131 virtual ~PendingWantManager(); 132 133 public: 134 sptr<IWantSender> GetWantSender(int32_t callingUid, int32_t uid, const bool isSystemApp, 135 const WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken); 136 int32_t SendWantSender(sptr<IWantSender> target, const SenderInfo &senderInfo); 137 void CancelWantSender(const bool isSystemApp, const sptr<IWantSender> &sender); 138 139 int32_t GetPendingWantUid(const sptr<IWantSender> &target); 140 int32_t GetPendingWantUserId(const sptr<IWantSender> &target); 141 std::string GetPendingWantBundleName(const sptr<IWantSender> &target); 142 int32_t GetPendingWantCode(const sptr<IWantSender> &target); 143 int32_t GetPendingWantType(const sptr<IWantSender> &target); 144 void RegisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &recevier); 145 void UnregisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &recevier); 146 int32_t GetPendingRequestWant(const sptr<IWantSender> &target, std::shared_ptr<Want> &want); 147 int32_t GetWantSenderInfo(const sptr<IWantSender> &target, std::shared_ptr<WantSenderInfo> &info); 148 149 void CancelWantSenderLocked(PendingWantRecord &record, bool cleanAbility); 150 int32_t PendingWantStartAbility( 151 const Want &want, const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid); 152 int32_t PendingWantStartAbilitys(const std::vector<WantsInfo> wnatsInfo, const sptr<IRemoteObject> &callerToken, 153 int32_t requestCode, const int32_t callerUid); 154 int32_t DeviceIdDetermine( 155 const Want &want, const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid); 156 int32_t PendingWantPublishCommonEvent(const Want &want, const SenderInfo &senderInfo, int32_t callerUid, 157 int32_t callerTokenId); 158 void ClearPendingWantRecord(const std::string &bundleName, int32_t uid); 159 160 void Dump(std::vector<std::string> &info); 161 void DumpByRecordId(std::vector<std::string> &info, const std::string &args); 162 163 private: 164 sptr<IWantSender> GetWantSenderLocked(const int32_t callingUid, const int32_t uid, const int32_t userId, 165 WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken); 166 void MakeWantSenderCanceledLocked(PendingWantRecord &record); 167 168 sptr<PendingWantRecord> GetPendingWantRecordByKey(const std::shared_ptr<PendingWantKey> &key); 169 bool CheckPendingWantRecordByKey( 170 const std::shared_ptr<PendingWantKey> &inputKey, const std::shared_ptr<PendingWantKey> &key); 171 172 sptr<PendingWantRecord> GetPendingWantRecordByCode(int32_t code); 173 static int32_t PendingRecordIdCreate(); 174 void ClearPendingWantRecordTask(const std::string &bundleName, int32_t uid); 175 176 private: 177 std::map<std::shared_ptr<PendingWantKey>, sptr<PendingWantRecord>> wantRecords_; 178 ffrt::mutex mutex_; 179 }; 180 } // namespace AAFwk 181 } // namespace OHOS 182 183 #endif // OHOS_ABILITY_RUNTIME_PENDING_WANT_MANAGER_H 184