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 constexpr int32_t EVENT_LISTENER_DIFF = -1; 40 constexpr int32_t EVENT_LISTENER_SAME = 0; 41 constexpr int32_t EVENT_LISTENER_SLOTID_AND_EVENTTYPE_SAME = 1; 42 43 class EventListenerHandler : public AppExecFwk::EventHandler { 44 DECLARE_DELAYED_SINGLETON(EventListenerHandler) 45 public: 46 EventListenerHandler(const EventListenerHandler &) = delete; 47 EventListenerHandler &operator=(const EventListenerHandler &) = delete; 48 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 49 int32_t RegisterEventListener(EventListener &eventListener); 50 int32_t UnregisterEventListener( 51 napi_env env, TelephonyUpdateEventType eventType, napi_ref ref, std::list<EventListener> &removeListenerList); 52 int32_t UnregisterEventListener( 53 napi_env env, TelephonyUpdateEventType eventType, std::list<EventListener> &removeListenerList); 54 void UnRegisterAllListener(napi_env env); 55 56 private: 57 using HandleFuncType = void (EventListenerHandler::*)(const AppExecFwk::InnerEvent::Pointer &event); 58 std::map<TelephonyCallbackEventId, HandleFuncType> handleFuncMap_; 59 static std::map<TelephonyUpdateEventType, void (*)(uv_work_t *work)> workFuncMap_; 60 static std::mutex operatorMutex_; 61 std::list<EventListener> listenerList_; 62 63 private: 64 bool IsCallBackRegister(napi_env env, napi_ref ref, napi_ref registeredRef) const; 65 bool CheckEventTypeExist(int32_t slotId, TelephonyUpdateEventType eventType); 66 void RemoveEventListenerRegister(napi_env env, TelephonyUpdateEventType eventType, napi_ref ref, 67 std::list<EventListener> &removeListenerList, std::set<int32_t> &soltIdSet); 68 void RemoveEventListenerRegister(napi_env env, TelephonyUpdateEventType eventType, 69 std::list<EventListener> &removeListenerList, std::set<int32_t> &soltIdSet); 70 void CheckRemoveStateObserver(TelephonyUpdateEventType eventType, int32_t slotId, int32_t &result); 71 int32_t CheckEventListenerRegister(EventListener &eventListener); 72 73 static void WorkCallStateUpdated(uv_work_t *work); 74 static void WorkSignalUpdated(uv_work_t *work); 75 static void WorkNetworkStateUpdated(uv_work_t *work); 76 static void WorkSimStateUpdated(uv_work_t *work); 77 static void WorkCellInfomationUpdated(uv_work_t *work); 78 static void WorkCellularDataConnectStateUpdate(uv_work_t *work); 79 static void WorkCellularDataFlowUpdate(uv_work_t *work); 80 static void WorkUpdated(uv_work_t *work, int status); 81 static void SetEventListenerDeleting(std::shared_ptr<bool> isDeleting); 82 83 template<typename T, typename D, TelephonyUpdateEventType eventType> 84 void HandleCallbackInfoUpdate(const AppExecFwk::InnerEvent::Pointer &event); 85 }; 86 } // namespace Telephony 87 } // namespace OHOS 88 #endif // EVENT_LISTENER_MANAGER_H