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 = std::function<void(const AppExecFwk::InnerEvent::Pointer &event)>;
58     std::map<TelephonyCallbackEventId, HandleFuncType> handleFuncMap_;
59     static std::map<TelephonyUpdateEventType,
60         void (*)(uv_work_t *work, std::unique_lock<std::mutex> &lock)> workFuncMap_;
61     static std::mutex operatorMutex_;
62     std::list<EventListener> listenerList_;
63 
64 private:
65     void AddBasicHandlerToMap();
66     void AddNetworkHandlerToMap();
67     void AddWorkFuncToMap();
68     bool IsCallBackRegister(napi_env env, napi_ref ref, napi_ref registeredRef) const;
69     bool CheckEventTypeExist(int32_t slotId, TelephonyUpdateEventType eventType);
70     void RemoveEventListenerRegister(napi_env env, TelephonyUpdateEventType eventType, napi_ref ref,
71         std::list<EventListener> &removeListenerList, std::set<int32_t> &soltIdSet);
72     void RemoveEventListenerRegister(napi_env env, TelephonyUpdateEventType eventType,
73         std::list<EventListener> &removeListenerList, std::set<int32_t> &soltIdSet);
74     void CheckRemoveStateObserver(TelephonyUpdateEventType eventType, int32_t slotId, int32_t &result);
75     int32_t CheckEventListenerRegister(EventListener &eventListener);
76 
77     static void WorkCallStateUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock);
78     static void WorkSignalUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock);
79     static void WorkNetworkStateUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock);
80     static void WorkSimStateUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock);
81     static void WorkCellInfomationUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock);
82     static void WorkCellularDataConnectStateUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock);
83     static void WorkCellularDataFlowUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock);
84     static void WorkUpdated(uv_work_t *work, int status);
85     static void SetEventListenerDeleting(std::shared_ptr<bool> isDeleting);
86     static void WorkCfuIndicatorUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock);
87     static void WorkVoiceMailMsgIndicatorUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock);
88     static void WorkIccAccountUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock);
89 
90     template<typename T, typename D, TelephonyUpdateEventType eventType>
91     void HandleCallbackInfoUpdate(const AppExecFwk::InnerEvent::Pointer &event);
92     template<TelephonyUpdateEventType eventType>
93     void HandleCallbackVoidUpdate(const AppExecFwk::InnerEvent::Pointer &event);
94 };
95 } // namespace Telephony
96 } // namespace OHOS
97 #endif // EVENT_LISTENER_MANAGER_H