• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 COMMUNICATIONNETSTACK_EVENT_MANAGER_H
17 #define COMMUNICATIONNETSTACK_EVENT_MANAGER_H
18 
19 #include <atomic>
20 #include <iosfwd>
21 #include <list>
22 #include <mutex>
23 #include <unordered_set>
24 #include <string>
25 #include <utility>
26 #include <queue>
27 
28 #include "event_listener.h"
29 #include "napi/native_api.h"
30 #include "uv.h"
31 
32 #ifdef ENABLE_EVENT_HANDLER
33 #include "event_handler.h"
34 #endif
35 
36 namespace OHOS::NetStack {
37 class EventManager {
38 public:
39     EventManager();
40 
41     ~EventManager();
42 
43     void AddListener(napi_env env, const std::string &type, napi_value callback, bool once, bool asyncCallback);
44 
45     void DeleteListener(const std::string &type, napi_value callback);
46 
47     void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv);
48 
49     void SetData(void *data);
50 
51     [[nodiscard]] void *GetData();
52 
53     void EmitByUv(const std::string &type, void *data, void(Handler)(uv_work_t *, int status));
54 
55     bool HasEventListener(const std::string &type);
56 
57     void DeleteListener(const std::string &type);
58 
59     static void SetInvalid(EventManager *manager);
60 
61     static bool IsManagerValid(EventManager *manager);
62 
63     static void SetValid(EventManager *manager);
64 
65     void SetQueueData(void *data);
66 
67     void *GetQueueData();
68 
69     void PopQueueData();
70 
71     void CreateEventReference(napi_env env, napi_value value);
72 
73     void DeleteEventReference(napi_env env);
74 
75     void SetEventDestroy(bool flag);
76 
77     bool IsEventDestroy();
78 
79 #ifdef ENABLE_EVENT_HANDLER
80     bool InitNetstackEventHandler();
81     std::shared_ptr<AppExecFwk::EventHandler> GetNetstackEventHandler();
82 #endif
83 
84 private:
85     std::mutex mutexForListenersAndEmitByUv_;
86     std::mutex mutexForEmitAndEmitByUv_;
87     std::mutex dataMutex_;
88     std::mutex dataQueueMutex_;
89     std::list<EventListener> listeners_;
90     void *data_;
91     std::queue<void *> dataQueue_;
92     static std::mutex mutexForManager_;
93     static std::unordered_set<EventManager *> validManager_;
94     napi_ref eventRef_;
95     std::atomic_bool isDestroy_;
96 
97 #ifdef ENABLE_EVENT_HANDLER
98     std::shared_ptr<AppExecFwk::EventRunner> eventRunner_ = nullptr;
99     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ = nullptr;
100 #endif
101 };
102 
103 struct UvWorkWrapper {
104     UvWorkWrapper() = delete;
105 
106     explicit UvWorkWrapper(void *theData, napi_env theEnv, std::string eventType, EventManager *eventManager);
107 
108     void *data;
109     napi_env env;
110     std::string type;
111     EventManager *manager;
112 };
113 } // namespace OHOS::NetStack
114 #endif /* COMMUNICATIONNETSTACK_EVENT_MANAGER_H */
115