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