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 const std::string &GetWebSocketTextData(); 80 81 void AppendWebSocketTextData(void *data, size_t length); 82 83 const std::string &GetWebSocketBinaryData(); 84 85 void AppendWebSocketBinaryData(void *data, size_t length); 86 87 void ClearWebSocketTextData(); 88 89 void ClearWebSocketBinaryData(); 90 91 #ifdef ENABLE_EVENT_HANDLER 92 bool InitNetstackEventHandler(); 93 std::shared_ptr<AppExecFwk::EventHandler> GetNetstackEventHandler(); 94 #endif 95 96 private: 97 std::mutex mutexForListenersAndEmitByUv_; 98 std::mutex mutexForEmitAndEmitByUv_; 99 std::mutex dataMutex_; 100 std::mutex dataQueueMutex_; 101 std::list<EventListener> listeners_; 102 void *data_; 103 std::queue<void *> dataQueue_; 104 static std::mutex mutexForManager_; 105 static std::unordered_set<EventManager *> validManager_; 106 napi_ref eventRef_; 107 std::atomic_bool isDestroy_; 108 std::string webSocketTextData_; 109 std::string webSocketBinaryData_; 110 111 #ifdef ENABLE_EVENT_HANDLER 112 std::shared_ptr<AppExecFwk::EventRunner> eventRunner_ = nullptr; 113 std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ = nullptr; 114 #endif 115 }; 116 117 struct UvWorkWrapper { 118 UvWorkWrapper() = delete; 119 120 explicit UvWorkWrapper(void *theData, napi_env theEnv, std::string eventType, EventManager *eventManager); 121 122 void *data; 123 napi_env env; 124 std::string type; 125 EventManager *manager; 126 }; 127 } // namespace OHOS::NetStack 128 #endif /* COMMUNICATIONNETSTACK_EVENT_MANAGER_H */ 129