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