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 "common_event_constant.h" 20 #include "common_event_record.h" 21 #include "common_event_subscribe_info.h" 22 #include "iremote_object.h" 23 #include "singleton.h" 24 25 namespace OHOS { 26 namespace EventFwk { 27 struct EventSubscriberRecord { 28 std::shared_ptr<CommonEventSubscribeInfo> eventSubscribeInfo; 29 sptr<IRemoteObject> commonEventListener; 30 EventRecordInfo eventRecordInfo; 31 struct tm recordTime {0}; 32 bool isFreeze; 33 int64_t freezeTime; 34 EventSubscriberRecordEventSubscriberRecord35 EventSubscriberRecord() 36 : eventSubscribeInfo(nullptr), 37 commonEventListener(nullptr), 38 isFreeze(false), 39 freezeTime(0) 40 {} 41 }; 42 43 struct FrozenEventRecord { 44 std::shared_ptr<EventSubscriberRecord> subscriberRecordPtr; 45 std::vector<std::shared_ptr<CommonEventRecord>> eventRecordPtrs; 46 FrozenEventRecordFrozenEventRecord47 FrozenEventRecord() : subscriberRecordPtr(nullptr) 48 {} 49 }; 50 51 inline bool operator<(const std::shared_ptr<EventSubscriberRecord> &a, const std::shared_ptr<EventSubscriberRecord> &b) 52 { 53 if (a == nullptr || a->eventSubscribeInfo == nullptr) { 54 return true; 55 } 56 57 if (b == nullptr || b->eventSubscribeInfo == nullptr) { 58 return false; 59 } 60 return a->eventSubscribeInfo->GetPriority() > b->eventSubscribeInfo->GetPriority(); 61 } 62 63 using SubscriberRecordPtr = std::shared_ptr<EventSubscriberRecord>; 64 using SubscribeInfoPtr = std::shared_ptr<CommonEventSubscribeInfo>; 65 using EventRecordPtr = std::shared_ptr<CommonEventRecord>; 66 using FrozenRecords = std::map<SubscriberRecordPtr, std::vector<EventRecordPtr>>; 67 68 class CommonEventSubscriberManager : public DelayedSingleton<CommonEventSubscriberManager> { 69 public: 70 CommonEventSubscriberManager(); 71 72 virtual ~CommonEventSubscriberManager() override; 73 74 /** 75 * Inserts a specific subscriber. 76 * 77 * @param eventSubscribeInfo Indicates the subscribe information. 78 * @param commonEventListener Indicates the subscriber object. 79 * @param recordTime Indicates the time of record. 80 * @param eventRecordInfo Indicates the information of event record. 81 * @return Returns the subscribe record. 82 */ 83 std::shared_ptr<EventSubscriberRecord> InsertSubscriber(const SubscribeInfoPtr &eventSubscribeInfo, 84 const sptr<IRemoteObject> &commonEventListener, const struct tm &recordTime, 85 const EventRecordInfo &eventRecordInfo); 86 87 /** 88 * Removes subscriber. 89 * 90 * @param commonEventListener Indicates the subscriber object. 91 * @return Returns the result code. 92 */ 93 int RemoveSubscriber(const sptr<IRemoteObject> &commonEventListener); 94 95 /** 96 * Gets subscriber records. 97 * 98 * @param eventRecord Indicates the event record. 99 * @return Returns the subscriber records. 100 */ 101 std::vector<SubscriberRecordPtr> GetSubscriberRecords(const CommonEventRecord &eventRecord); 102 103 /** 104 * @brief Get the subscribe record by subscriber object. 105 * 106 * @param commonEventListener Indicates the subscriber object. 107 * @return std::shared_ptr<EventSubscriberRecord> 108 */ 109 std::shared_ptr<EventSubscriberRecord> GetSubscriberRecord(const sptr<IRemoteObject> &commonEventListener); 110 111 /** 112 * Updates freeze information. 113 * 114 * @param uid Indicates the uid of the application. 115 * @param freezeState Indicates the freeze state. 116 * @param freezeTime Indicates the freeze time. 117 */ 118 void UpdateFreezeInfo(const uid_t &uid, const bool &freezeState, const int64_t &freezeTime = 0); 119 120 /** 121 * Updates freeze information of all applications. 122 * 123 * @param freezeState Indicates the freeze state. 124 * @param freezeTime Indicates the freeze time. 125 */ 126 void UpdateAllFreezeInfos(const bool &freezeState, const int64_t &freezeTime = 0); 127 128 /** 129 * Inserts freeze events. 130 * 131 * @param eventListener Indicates the subscriber object. 132 * @param eventRecord Indicates the event record. 133 */ 134 void InsertFrozenEvents(const SubscriberRecordPtr &eventListener, const CommonEventRecord &eventRecord); 135 136 /** 137 * Gets the frozen events. 138 * 139 * @param uid Indicates the uid of the application. 140 * @return Returns the frozen events. 141 */ 142 FrozenRecords GetFrozenEvents(const uid_t &uid); 143 144 /** 145 * Gets all frozen events. 146 * 147 * @return Returns all frozen events. 148 */ 149 std::map<uid_t, FrozenRecords> GetAllFrozenEvents(); 150 151 /** 152 * Dumps detailed information for specific subscriber record info. 153 * 154 * @param title Indicates the log tag. 155 * @param record Indicates the subscriber record. 156 * @param format Indicates the log format. 157 * @param dumpInfo Indicates the output information. 158 */ 159 void DumpDetailed( 160 const std::string &title, const SubscriberRecordPtr &record, const std::string format, std::string &dumpInfo); 161 162 /** 163 * Dumps state information. 164 * 165 * @param event Specifies the information for the common event. Set null string ("") if you want to dump all. 166 * @param userId Indicates the user ID. 167 * @param state Indicates the output information. 168 */ 169 void DumpState(const std::string &event, const int32_t &userId, std::vector<std::string> &state); 170 171 private: 172 bool InsertSubscriberRecordLocked(const std::vector<std::string> &events, const SubscriberRecordPtr &record); 173 174 int RemoveSubscriberRecordLocked(const sptr<IRemoteObject> &commonEventListener); 175 176 bool CheckSubscriberByUserId(const int32_t &subscriberUserId, const bool &isSystemApp, const int32_t &userId); 177 178 void GetSubscriberRecordsByWantLocked(const CommonEventRecord &eventRecord, 179 std::vector<SubscriberRecordPtr> &records); 180 181 void GetSubscriberRecordsByEvent( 182 const std::string &event, const int32_t &userId, std::vector<SubscriberRecordPtr> &records); 183 184 void RemoveFrozenEventsBySubscriber(const SubscriberRecordPtr &subscriberRecord); 185 186 void RemoveFrozenEvents(const uid_t &uid); 187 188 void SendSubscriberExceedMaximumHiSysEvent(int32_t userId, const std::string &eventName, uint32_t subscriberNum); 189 190 private: 191 std::mutex mutex_; 192 sptr<IRemoteObject::DeathRecipient> death_; 193 std::map<std::string, std::multiset<SubscriberRecordPtr>> eventSubscribers_; 194 std::vector<SubscriberRecordPtr> subscribers_; 195 std::map<uid_t, FrozenRecords> frozenEvents_; 196 const time_t FREEZE_EVENT_TIMEOUT = 30; // How long we keep records. Unit: second 197 }; 198 } // namespace EventFwk 199 } // namespace OHOS 200 #endif // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_SUBSCRIBER_MANAGER_H