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