• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <atomic>
22 
23 #include <uv.h>
24 
25 #include "event_listener.h"
26 
27 namespace OHOS {
28 namespace NetManagerStandard {
29 class EventManager {
30 public:
31     EventManager();
32 
33     void AddListener(napi_env env, const std::string &type, napi_value callback, bool once, bool asyncCallback);
34     void DeleteListener(const std::string &type, napi_value callback);
35     void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv);
36     void SetData(void *data);
37     void EmitByUv(const std::string &type, void *data, void(handler)(uv_work_t *, int status));
38     bool HasEventListener(const std::string &type);
39     void DeleteListener(const std::string &type);
40     void DeleteAllListener();
41     [[nodiscard]] void *GetData();
42 
IsListenerListEmpty()43     bool IsListenerListEmpty() const
44     {
45         return listeners_.empty();
46     }
47 
48     bool IsValid() const;
49     void SetInvalid();
50 
51 private:
52     std::mutex mutex_;
53     std::list<EventListener> listeners_;
54     void *data_;
55     std::atomic_bool isValid_;
56 };
57 
58 struct UvWorkWrapper {
59     UvWorkWrapper() = delete;
60     explicit UvWorkWrapper(void *theData, napi_env theEnv, std::string eventType, EventManager *eventManager);
61     void *data;
62     napi_env env;
63     std::string type;
64     EventManager *manager;
65 };
66 } // namespace NetManagerStandard
67 } // namespace OHOS
68 #endif // COMMUNICATIONNETMANAGER_BASE_EVENT_MANAGER_H
69