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 <set> 24 #include <string> 25 #include <utility> 26 27 #include "event_listener.h" 28 #include "napi/native_api.h" 29 #include "uv.h" 30 31 namespace OHOS::NetStack { 32 class EventManager { 33 public: 34 EventManager(); 35 36 ~EventManager(); 37 38 void AddListener(napi_env env, const std::string &type, napi_value callback, bool once, bool asyncCallback); 39 40 void DeleteListener(const std::string &type, napi_value callback); 41 42 void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv); 43 44 void SetData(void *data); 45 46 [[nodiscard]] void *GetData(); 47 48 void EmitByUv(const std::string &type, void *data, void(Handler)(uv_work_t *, int status)); 49 50 bool HasEventListener(const std::string &type); 51 52 void DeleteListener(const std::string &type); 53 54 void SetInvalid(); 55 56 [[nodiscard]] bool IsManagerValid() const; 57 58 private: 59 std::mutex mutex_; 60 std::list<EventListener> listeners_; 61 void *data_; 62 std::atomic_bool isValid_; 63 }; 64 65 struct UvWorkWrapper { 66 UvWorkWrapper() = delete; 67 68 explicit UvWorkWrapper(void *theData, napi_env theEnv, std::string eventType, EventManager *eventManager); 69 70 void *data; 71 napi_env env; 72 std::string type; 73 EventManager *manager; 74 }; 75 } // namespace OHOS::NetStack 76 #endif /* COMMUNICATIONNETSTACK_EVENT_MANAGER_H */ 77