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