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