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