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 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 bool NotifyFreezeEvents(const EventSubscriberRecord &subscriberRecord, const CommonEventRecord &eventRecord); 153 154 void GetOrderedEventRecords( 155 const std::string &event, const int32_t &userId, std::vector<std::shared_ptr<OrderedEventRecord>> &records); 156 157 void GetUnorderedEventRecords( 158 const std::string &event, const int32_t &userId, std::vector<std::shared_ptr<OrderedEventRecord>> &records); 159 160 void DumpStateByCommonEventRecord(const std::shared_ptr<OrderedEventRecord> &record, std::string &dumpInfo); 161 162 void DumpStateBySubscriberRecord(const std::shared_ptr<OrderedEventRecord> &record, std::string &dumpInfo); 163 164 void PublishFrozenEventsInner(const FrozenRecords &frozenEventRecords); 165 166 void SendOrderedEventProcTimeoutHiSysEvent(const std::shared_ptr<EventSubscriberRecord> &subscriberRecord, 167 const std::string &eventName); 168 169 void NotifyUnorderedEventLocked(std::shared_ptr<OrderedEventRecord> &eventRecord); 170 private: 171 std::shared_ptr<EventHandler> handler_; 172 std::shared_ptr<OrderedEventHandler> handlerOrdered_; 173 std::vector<std::shared_ptr<OrderedEventRecord>> orderedEventQueue_; 174 std::vector<std::shared_ptr<OrderedEventRecord>> unorderedEventQueue_; 175 bool pendingTimeoutMessage_; 176 bool scheduled_; 177 const int64_t TIMEOUT = 10000; // How long we allow a receiver to run before giving up on it. Unit: ms 178 ffrt::mutex orderedMutex_; 179 ffrt::mutex unorderedMutex_; 180 181 std::shared_ptr<ffrt::queue> orderedQueue_ = nullptr; 182 std::shared_ptr<ffrt::queue> unorderedQueue_ = nullptr; 183 std::shared_ptr<ffrt::queue> unorderedImmediateQueue_ = nullptr; 184 ffrt::task_handle orderedHandler = nullptr; 185 }; 186 } // namespace EventFwk 187 } // namespace OHOS 188 189 #endif // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_CONTROL_MANAGER_H