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