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 FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_SUBSCRIBER_MANAGER_H 17 #define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_SUBSCRIBER_MANAGER_H 18 19 #include <utility> 20 #include <vector> 21 22 #include "common_event_constant.h" 23 #include "common_event_record.h" 24 #include "common_event_subscribe_info.h" 25 #include "event_log_wrapper.h" 26 #include "ffrt.h" 27 #include "iremote_object.h" 28 #include "parameter.h" 29 #include "singleton.h" 30 31 namespace OHOS { 32 namespace EventFwk { 33 struct EventSubscriberRecord { 34 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo; 35 sptr<IRemoteObject> commonEventListener; 36 EventRecordInfo eventRecordInfo; 37 struct tm recordTime {0}; 38 bool isFreeze; 39 int64_t freezeTime; 40 EventSubscriberRecordEventSubscriberRecord41 EventSubscriberRecord() 42 : eventSubscribeInfo(nullptr), 43 commonEventListener(nullptr), 44 isFreeze(false), 45 freezeTime(0) 46 {} 47 48 bool operator<(const EventSubscriberRecord &other) const 49 { 50 if (commonEventListener == nullptr) { 51 EVENT_LOGE("commonEventListener is null"); 52 return false; 53 } 54 55 if (other.commonEventListener == nullptr) { 56 EVENT_LOGE("other's commonEventListener is null"); 57 return true; 58 } 59 return commonEventListener < other.commonEventListener; 60 } 61 }; 62 63 struct FrozenEventRecord { 64 std::shared_ptr<EventSubscriberRecord> subscriberRecordPtr; 65 std::vector<std::shared_ptr<CommonEventRecord>> eventRecordPtrs; 66 FrozenEventRecordFrozenEventRecord67 FrozenEventRecord() : subscriberRecordPtr(nullptr) 68 {} 69 }; 70 71 using SubscriberRecordPtr = std::shared_ptr<EventSubscriberRecord>; 72 using SubscribeInfoPtr = std::shared_ptr<CommonEventSubscribeInfo>; 73 using EventRecordPtr = std::shared_ptr<CommonEventRecord>; 74 using FrozenRecords = std::map<EventSubscriberRecord, std::vector<EventRecordPtr>>; 75 76 class CommonEventSubscriberManager : public DelayedSingleton<CommonEventSubscriberManager> { 77 public: 78 CommonEventSubscriberManager(); 79 80 virtual ~CommonEventSubscriberManager() override; 81 82 /** 83 * Inserts a specific subscriber. 84 * 85 * @param eventSubscribeInfo Indicates the subscribe information. 86 * @param commonEventListener Indicates the subscriber object. 87 * @param recordTime Indicates the time of record. 88 * @param eventRecordInfo Indicates the information of event record. 89 * @return Returns the subscribe record. 90 */ 91 std::shared_ptr<EventSubscriberRecord> InsertSubscriber(const SubscribeInfoPtr &eventSubscribeInfo, 92 const sptr<IRemoteObject> &commonEventListener, const struct tm &recordTime, 93 const EventRecordInfo &eventRecordInfo); 94 95 /** 96 * Removes subscriber. 97 * 98 * @param commonEventListener Indicates the subscriber object. 99 * @return Returns the result code. 100 */ 101 int RemoveSubscriber(const sptr<IRemoteObject> &commonEventListener); 102 103 /** 104 * Gets subscriber records. 105 * 106 * @param eventRecord Indicates the event record. 107 * @return Returns the subscriber records. 108 */ 109 std::vector<SubscriberRecordPtr> GetSubscriberRecords(const CommonEventRecord &eventRecord); 110 111 /** 112 * @brief Get the subscribe record by subscriber object. 113 * 114 * @param commonEventListener Indicates the subscriber object. 115 * @return std::shared_ptr<EventSubscriberRecord> 116 */ 117 std::shared_ptr<EventSubscriberRecord> GetSubscriberRecord(const sptr<IRemoteObject> &commonEventListener); 118 119 /** 120 * Updates freeze information. 121 * 122 * @param uid Indicates the uid of the application. 123 * @param freezeState Indicates the freeze state. 124 * @param freezeTime Indicates the freeze time. 125 */ 126 void UpdateFreezeInfo(const uid_t &uid, const bool &freezeState, const int64_t &freezeTime = 0); 127 128 /** 129 * Updates freeze information. 130 * 131 * @param pidList Indicates the list of process id. 132 * @param freezeState Indicates the freeze state. 133 * @param freezeTime Indicates the freeze time. 134 */ 135 void UpdateFreezeInfo(std::set<int> pidList, const bool &freezeState, const int64_t &freezeTime = 0); 136 137 /** 138 * Updates freeze information of all applications. 139 * 140 * @param freezeState Indicates the freeze state. 141 * @param freezeTime Indicates the freeze time. 142 */ 143 void UpdateAllFreezeInfos(const bool &freezeState, const int64_t &freezeTime = 0); 144 145 /** 146 * Inserts freeze events. 147 * 148 * @param eventListener Indicates the subscriber object. 149 * @param eventRecord Indicates the event record. 150 */ 151 void InsertFrozenEvents(const SubscriberRecordPtr &eventListener, const CommonEventRecord &eventRecord); 152 153 /** 154 * Gets the frozen events. 155 * 156 * @param uid Indicates the uid of the application. 157 * @return Returns the frozen events. 158 */ 159 FrozenRecords GetFrozenEvents(const uid_t &uid); 160 161 /** 162 * Gets all frozen events. 163 * 164 * @return Returns all frozen events. 165 */ 166 std::map<uid_t, FrozenRecords> GetAllFrozenEvents(); 167 168 /** 169 * Inserts freeze events. 170 * 171 * @param eventListener Indicates the subscriber object. 172 * @param eventRecord Indicates the event record. 173 */ 174 void InsertFrozenEventsMap(const SubscriberRecordPtr &eventListener, const CommonEventRecord &eventRecord); 175 176 /** 177 * Gets the frozen events. 178 * 179 * @param pid Indicates the process id. 180 * @return Returns the frozen events. 181 */ 182 FrozenRecords GetFrozenEventsMapByPid(const pid_t &pid); 183 184 /** 185 * Gets all frozen events. 186 * 187 * @return Returns all frozen events. 188 */ 189 std::map<pid_t, FrozenRecords> GetAllFrozenEventsMap(); 190 191 /** 192 * Dumps detailed information for specific subscriber record info. 193 * 194 * @param title Indicates the log tag. 195 * @param record Indicates the subscriber record. 196 * @param format Indicates the log format. 197 * @param dumpInfo Indicates the output information. 198 */ 199 void DumpDetailed( 200 const std::string &title, const SubscriberRecordPtr &record, const std::string format, std::string &dumpInfo); 201 202 /** 203 * Dumps state information. 204 * 205 * @param event Specifies the information for the common event. Set null string ("") if you want to dump all. 206 * @param userId Indicates the user ID. 207 * @param state Indicates the output information. 208 */ 209 void DumpState(const std::string &event, const int32_t &userId, std::vector<std::string> &state); 210 211 private: 212 bool CheckPublisherWhetherMatched(const SubscriberRecordPtr &subscriberRecord, 213 const CommonEventRecord &eventRecord); 214 bool CheckSubscriberWhetherMatched(const SubscriberRecordPtr &subscriberRecord, 215 const CommonEventRecord &eventRecord); 216 bool CheckSubscriberPermission(const SubscriberRecordPtr &subscriberRecord, 217 const CommonEventRecord &eventRecord); 218 bool CheckSubscriberRequiredPermission(const SubscriberRecordPtr &subscriberRecord, 219 const CommonEventRecord &eventRecord); 220 bool CheckPublisherRequiredPermissions(const SubscriberRecordPtr &subscriberRecord, 221 const CommonEventRecord &eventRecord); 222 bool InsertSubscriberRecordLocked(const std::vector<std::string> &events, const SubscriberRecordPtr &record); 223 224 int RemoveSubscriberRecordLocked(const sptr<IRemoteObject> &commonEventListener); 225 226 bool CheckSubscriberByUserId(const int32_t &subscriberUserId, const bool &isSystemApp, const int32_t &userId); 227 228 bool CheckSubscriberBySpecifiedUids(const int32_t &subscriberUid, 229 const std::vector<int32_t> &specifiedSubscriberUids); 230 231 bool CheckSubscriberBySpecifiedType(const int32_t &specifiedSubscriberType, const bool &isSystemApp); 232 233 void GetSubscriberRecordsByWantLocked(const CommonEventRecord &eventRecord, 234 std::vector<SubscriberRecordPtr> &records); 235 236 void GetSubscriberRecordsByEvent( 237 const std::string &event, const int32_t &userId, std::vector<SubscriberRecordPtr> &records); 238 239 void RemoveFrozenEventsBySubscriber(const SubscriberRecordPtr &subscriberRecord); 240 241 void RemoveFrozenEvents(const uid_t &uid); 242 243 void RemoveFrozenEventsMapBySubscriber(const SubscriberRecordPtr &subscriberRecord); 244 245 void RemoveFrozenEventsMapByPid(const pid_t &pid); 246 247 void SendSubscriberExceedMaximumHiSysEvent(int32_t userId, const std::string &eventName, uint32_t subscriberNum); 248 249 bool CheckSubscriberCountReachedMaxinum(); 250 251 std::vector<std::pair<pid_t, uint32_t>> GetTopSubscriberCounts(size_t topNum = 10); 252 253 void PrintSubscriberCounts(std::vector<std::pair<pid_t, uint32_t>> vtSubscriberCounts); 254 255 void SubscribeScreenEventToBlackListApp(const CommonEventRecord &eventRecord, std::string subscribeBundleName, 256 int subscribeUid, std::vector<SubscriberRecordPtr> &records, SubscriberRecordPtr it); 257 258 private: 259 ffrt::mutex mutex_; 260 sptr<IRemoteObject::DeathRecipient> death_; 261 std::map<std::string, std::set<SubscriberRecordPtr>> eventSubscribers_; 262 std::vector<SubscriberRecordPtr> subscribers_; 263 std::map<uid_t, FrozenRecords> frozenEvents_; 264 const time_t FREEZE_EVENT_TIMEOUT = 30; // How long we keep records. Unit: second 265 std::map<pid_t, uint32_t> subscriberCounts_; 266 std::map<pid_t, FrozenRecords> frozenEventsMap_; 267 }; 268 } // namespace EventFwk 269 } // namespace OHOS 270 #endif // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_SUBSCRIBER_MANAGER_H