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_CONTROL_MANAGER_H
17 #define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_CONTROL_MANAGER_H
18 
19 #include <list>
20 
21 #include "common_event_permission_manager.h"
22 #include "common_event_subscriber_manager.h"
23 #include "history_event_record.h"
24 #include "ordered_event_handler.h"
25 #include "ordered_event_record.h"
26 #include "ffrt.h"
27 
28 namespace OHOS {
29 namespace EventFwk {
30 class CommonEventControlManager : public std::enable_shared_from_this<CommonEventControlManager> {
31 public:
32     CommonEventControlManager();
33 
34     ~CommonEventControlManager();
35 
36     /**
37      * Publishes the common event.
38      *
39      * @param eventRecord Indicates the event record.
40      * @param commonEventListener Indicates the last subscriber object.
41      * @return Returns true if success; false otherwise.
42      */
43     bool PublishCommonEvent(const CommonEventRecord &eventRecord,
44         const sptr<IRemoteObject> &commonEventListener);
45 
46     /**
47      * Publishes the sticky common event.
48      *
49      * @param eventRecord Indicates the event record.
50      * @param subscriberRecord Indicates the subscriber object.
51      * @return Returns true if success; false otherwise.
52      */
53     bool PublishStickyCommonEvent(
54         const CommonEventRecord &eventRecord, const std::shared_ptr<EventSubscriberRecord> &subscriberRecord);
55 
56     /**
57      * Gets the matching ordered receiver.
58      *
59      * @param proxy Indicates the current ordered receiver.
60      * @return Returns the ordered event record.
61      */
62     std::shared_ptr<OrderedEventRecord> GetMatchingOrderedReceiver(const sptr<IRemoteObject> &proxy);
63 
64     /**
65      * Finishes the action of the current receiver.
66      *
67      * @param recordPtr Indicates the ordered event record.
68      * @param code Indicates the result code.
69      * @param receiverData Indicates the result data.
70      * @param abortEvent Indicates whether to cancel the current common event.
71      * @return Returns true if success; false otherwise.
72      */
73     bool FinishReceiverAction(std::shared_ptr<OrderedEventRecord> recordPtr, const int32_t &code,
74         const std::string &receiverData, const bool &abortEvent);
75 
76     /**
77      * Processes the current ordered event when it is timeout.
78      *
79      * @param isFromMsg Indicates whether triggered by message.
80      */
81     void CurrentOrderedEventTimeout(bool isFromMsg);
82 
83     /**
84      * Processes the next ordered event.
85      *
86      * @param isSendMsg Indicates whether triggered by message.
87      */
88     void ProcessNextOrderedEvent(bool isSendMsg);
89 
90     /**
91      * Publishes freeze common event.
92      *
93      * @param uid Indicates the uid of unfreeze application.
94      * @return Returns true if success; false otherwise.
95      */
96     bool PublishFreezeCommonEvent(const uid_t &uid);
97 
98     /**
99     * Publishes freeze common event.
100     *
101     * @param uid Indicates the list of process id.
102     * @return Returns true if success; false otherwise.
103     */
104     bool PublishFreezeCommonEvent(std::set<int> pidList);
105 
106     /**
107      * Publishes all freeze common events.
108      *
109      * @return Returns true if success; false otherwise.
110      */
111     bool PublishAllFreezeCommonEvents();
112 
113     /**
114      * Dumps state of common event service.
115      *
116      * @param event Specifies the information for the common event. Set null string ("") if you want to dump all.
117      * @param userId Indicates the user ID.
118      * @param state Indicates the state of common event service.
119      */
120     void DumpState(const std::string &event, const int32_t &userId, std::vector<std::string> &state);
121 
122     /**
123      * Dumps state of history common event.
124      *
125      * @param event Specifies the information for the common event. Set null string ("") if you want to dump all.
126      * @param userId Indicates the user ID.
127      * @param state Indicates the state of common event service.
128      */
129     void DumpHistoryState(const std::string &event, const int32_t &userId, std::vector<std::string> &state);
130 
131 private:
132     bool ProcessUnorderedEvent(
133         const CommonEventRecord &eventRecord, const std::shared_ptr<EventSubscriberRecord> &subscriberRecord = nullptr);
134 
135     bool GetUnorderedEventHandler();
136 
137     bool NotifyUnorderedEvent(std::shared_ptr<OrderedEventRecord> &eventRecord);
138 
139     bool ProcessOrderedEvent(
140         const CommonEventRecord &commonEventRecord, const sptr<IRemoteObject> &commonEventListener);
141 
142     bool GetOrderedEventHandler();
143 
144     bool EnqueueOrderedRecord(const std::shared_ptr<OrderedEventRecord> &eventRecordPtr);
145 
146     bool EnqueueUnorderedRecord(const std::shared_ptr<OrderedEventRecord> &eventRecordPtr);
147 
148     bool ScheduleOrderedCommonEvent();
149 
150     bool NotifyOrderedEvent(std::shared_ptr<OrderedEventRecord> &eventRecordPtr, size_t index);
151 
152     void SetTime(size_t recIdx, std::shared_ptr<OrderedEventRecord> &sp, bool timeoutMessage);
153 
154     bool SetTimeout();
155 
156     bool CancelTimeout();
157 
158     bool FinishReceiver(std::shared_ptr<OrderedEventRecord> recordPtr, const int32_t &code,
159         const std::string &receiverData, const bool &abortEvent);
160 
161     int8_t CheckPermission(const EventSubscriberRecord &subscriberRecord, const CommonEventRecord &eventRecord);
162 
163     bool CheckSubscriberPermission(const EventSubscriberRecord &subscriberRecord,
164         const CommonEventRecord &eventRecord);
165 
166     bool CheckSubscriberRequiredPermission(const std::string &subscriberRequiredPermission,
167         const CommonEventRecord &eventRecord, const EventSubscriberRecord &subscriberRecord);
168 
169     bool CheckPublisherRequiredPermissions(const std::vector<std::string> &publisherRequiredPermissions,
170         const EventSubscriberRecord &subscriberRecord, const CommonEventRecord &eventRecord);
171 
172     bool NotifyFreezeEvents(const EventSubscriberRecord &subscriberRecord, const CommonEventRecord &eventRecord);
173 
174     void GetOrderedEventRecords(
175         const std::string &event, const int32_t &userId, std::vector<std::shared_ptr<OrderedEventRecord>> &records);
176 
177     void GetUnorderedEventRecords(
178         const std::string &event, const int32_t &userId, std::vector<std::shared_ptr<OrderedEventRecord>> &records);
179 
180     void GetHistoryEventRecords(
181         const std::string &event, const int32_t &userId, std::list<HistoryEventRecord> &records);
182 
183     void DumpStateByCommonEventRecord(const std::shared_ptr<OrderedEventRecord> &record, std::string &dumpInfo);
184 
185     void DumpStateBySubscriberRecord(const std::shared_ptr<OrderedEventRecord> &record, std::string &dumpInfo);
186 
187     void DumpHistoryStateBySubscriberRecord(const HistoryEventRecord &record, std::string &dumpInfo);
188 
189     void DumpHistoryStateByCommonEventRecord(const HistoryEventRecord &record, std::string &dumpInfo);
190 
191     void EnqueueHistoryEventRecord(const std::shared_ptr<OrderedEventRecord> &eventRecordPtr,
192         bool hasLastSubscribe);
193 
194     void PublishFrozenEventsInner(const FrozenRecords &frozenEventRecords);
195 
196     void SendOrderedEventProcTimeoutHiSysEvent(const std::shared_ptr<EventSubscriberRecord> &subscriberRecord,
197         const std::string &eventName);
198 
199     void NotifyUnorderedEventLocked(std::shared_ptr<OrderedEventRecord> &eventRecord);
200 private:
201     std::shared_ptr<EventHandler> handler_;
202     std::shared_ptr<OrderedEventHandler> handlerOrdered_;
203     std::vector<std::shared_ptr<OrderedEventRecord>> orderedEventQueue_;
204     std::vector<std::shared_ptr<OrderedEventRecord>> unorderedEventQueue_;
205     std::list<HistoryEventRecord> historyEventRecords_;
206     bool pendingTimeoutMessage_;
207     bool scheduled_;
208     const int64_t TIMEOUT = 10000;  // How long we allow a receiver to run before giving up on it. Unit: ms
209     std::mutex orderedMutex_;
210     std::mutex unorderedMutex_;
211     std::mutex historyMutex_;
212     const size_t HISTORY_MAX_SIZE = 100;
213 
214     std::shared_ptr<ffrt::queue> orderedQueue_ = nullptr;
215     std::shared_ptr<ffrt::queue> unorderedQueue_ = nullptr;
216     std::shared_ptr<ffrt::queue> unorderedImmediateQueue_ = nullptr;
217     ffrt::task_handle orderedHandler  = nullptr;
218 };
219 }  // namespace EventFwk
220 }  // namespace OHOS
221 
222 #endif  // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_CONTROL_MANAGER_H