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