1 /* 2 * Copyright (C) 2021 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 EVENT_LISTENER_HANDLER_H 17 #define EVENT_LISTENER_HANDLER_H 18 19 #include <list> 20 #include <map> 21 #include <memory> 22 #include <optional> 23 #include <set> 24 #include <uv.h> 25 26 #include "event_handler.h" 27 #include "event_listener.h" 28 #include "event_runner.h" 29 #include "napi_util.h" 30 #include "network_state.h" 31 #include "refbase.h" 32 #include "signal_information.h" 33 #include "singleton.h" 34 #include "telephony_callback_event_id.h" 35 #include "telephony_update_event_type.h" 36 37 namespace OHOS { 38 namespace Telephony { 39 class EventListenerHandler : public AppExecFwk::EventHandler { 40 DECLARE_DELAYED_SINGLETON(EventListenerHandler) 41 public: 42 EventListenerHandler(const EventListenerHandler &) = delete; 43 EventListenerHandler &operator=(const EventListenerHandler &) = delete; 44 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 45 std::optional<int32_t> RegisterEventListener(EventListener &eventListener); 46 std::optional<int32_t> UnregisterEventListener(int32_t slotId, TelephonyUpdateEventType eventType); 47 void RemoveListener(TelephonyUpdateEventType eventType); 48 void SetCallbackCompleteToListener(napi_ref ref, bool flag = true); 49 50 private: 51 using HandleFuncType = void (EventListenerHandler::*)(const AppExecFwk::InnerEvent::Pointer &event); 52 std::map<TelephonyCallbackEventId, HandleFuncType> handleFuncMap_; 53 std::map<TelephonyUpdateEventType, void (*)(uv_work_t *work, int status)> workFuncMap_; 54 std::mutex operatorMutex_; 55 std::list<EventListener> listenerList_; 56 std::map<uint32_t, std::set<TelephonyUpdateEventType>> registerStateMap_; 57 58 private: 59 void ManageRegistrants(uint32_t slotId, const TelephonyUpdateEventType eventType, bool isRegister); 60 bool IsEventTypeRegistered(uint32_t slotId, const TelephonyUpdateEventType eventType) const; 61 62 static void WorkCallStateUpdated(uv_work_t *work, int status); 63 static void WorkSignalUpdated(uv_work_t *work, int status); 64 static void WorkNetworkStateUpdated(uv_work_t *work, int status); 65 static void WorkSimStateUpdated(uv_work_t *work, int status); 66 static void WorkCellInfomationUpdated(uv_work_t *work, int status); 67 static void WorkCellularDataConnectStateUpdate(uv_work_t *work, int status); 68 static void WorkCellularDataFlowUpdate(uv_work_t *work, int status); 69 70 template<typename T, typename D, TelephonyUpdateEventType eventType> 71 void HandleCallbackInfoUpdate(const AppExecFwk::InnerEvent::Pointer &event); 72 }; 73 } // namespace Telephony 74 } // namespace OHOS 75 #endif // EVENT_LISTENER_MANAGER_H