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 #include "event_manager.h"
17
18 #include "netmanager_base_log.h"
19 #include "securec.h"
20
21 namespace OHOS {
22 namespace NetManagerStandard {
23 namespace {
24 static constexpr const int CALLBACK_PARAM_NUM = 1;
25 static constexpr const int ASYNC_CALLBACK_PARAM_NUM = 2;
26 } // namespace
27
EventManager()28 EventManager::EventManager() : data_(nullptr), isValid_(true) {}
29
SetInvalid()30 void EventManager::SetInvalid()
31 {
32 isValid_ = false;
33 }
34
IsValid() const35 bool EventManager::IsValid() const
36 {
37 return isValid_;
38 }
39
AddListener(napi_env env,const std::string & type,napi_value callback,bool once,bool asyncCallback)40 void EventManager::AddListener(napi_env env, const std::string &type, napi_value callback, bool once,
41 bool asyncCallback)
42 {
43 std::lock_guard<std::mutex> lock(mutex_);
44 auto it = std::remove_if(listeners_.begin(), listeners_.end(),
45 [type](const EventListener &listener) -> bool { return listener.MatchType(type); });
46 if (it != listeners_.end()) {
47 listeners_.erase(it, listeners_.end());
48 }
49 listeners_.emplace_back(EventListener(env, type, callback, once, asyncCallback));
50 }
51
DeleteListener(const std::string & type,napi_value callback)52 void EventManager::DeleteListener(const std::string &type, napi_value callback)
53 {
54 std::lock_guard<std::mutex> lock(mutex_);
55 auto it =
56 std::remove_if(listeners_.begin(), listeners_.end(), [type, callback](const EventListener &listener) -> bool {
57 return listener.Match(type, callback);
58 });
59 listeners_.erase(it, listeners_.end());
60 }
61
Emit(const std::string & type,const std::pair<napi_value,napi_value> & argv)62 void EventManager::Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv)
63 {
64 std::for_each(listeners_.begin(), listeners_.end(), [type, argv](const EventListener &listener) {
65 if (listener.IsAsyncCallback()) {
66 /* AsyncCallback(BusinessError error, T data) */
67 napi_value arg[ASYNC_CALLBACK_PARAM_NUM] = {argv.first, argv.second};
68 listener.Emit(type, ASYNC_CALLBACK_PARAM_NUM, arg);
69 } else {
70 /* Callback(T data) */
71 napi_value arg[CALLBACK_PARAM_NUM] = {argv.second};
72 listener.Emit(type, CALLBACK_PARAM_NUM, arg);
73 }
74 });
75
76 auto it = std::remove_if(listeners_.begin(), listeners_.end(),
77 [type](const EventListener &listener) -> bool { return listener.MatchOnce(type); });
78 listeners_.erase(it, listeners_.end());
79 }
80
SetData(void * data)81 void EventManager::SetData(void *data)
82 {
83 std::lock_guard<std::mutex> lock(mutex_);
84 data_ = data;
85 }
86
GetData()87 void *EventManager::GetData()
88 {
89 std::lock_guard<std::mutex> lock(mutex_);
90 return data_;
91 }
92
EmitByUv(const std::string & type,void * data,void (handler)(uv_work_t *,int status))93 void EventManager::EmitByUv(const std::string &type, void *data, void(handler)(uv_work_t *, int status))
94 {
95 std::lock_guard<std::mutex> lock(mutex_);
96 if (!IsValid()) {
97 return;
98 }
99
100 std::for_each(listeners_.begin(), listeners_.end(), [type, data, handler, this](const EventListener &listener) {
101 auto workWrapper = new UvWorkWrapper(data, listener.GetEnv(), type, this);
102 listener.EmitByUv(type, workWrapper, handler);
103 });
104 }
105
HasEventListener(const std::string & type)106 bool EventManager::HasEventListener(const std::string &type)
107 {
108 std::lock_guard<std::mutex> lock(mutex_);
109
110 return std::any_of(listeners_.begin(), listeners_.end(),
111 [&type](const EventListener &listener) -> bool { return listener.MatchType(type); });
112 }
113
DeleteListener(const std::string & type)114 void EventManager::DeleteListener(const std::string &type)
115 {
116 std::lock_guard<std::mutex> lock(mutex_);
117 auto it = std::remove_if(listeners_.begin(), listeners_.end(),
118 [type](const EventListener &listener) -> bool { return listener.MatchType(type); });
119 listeners_.erase(it, listeners_.end());
120 }
121
DeleteAllListener()122 void EventManager::DeleteAllListener()
123 {
124 std::lock_guard<std::mutex> lock(mutex_);
125 listeners_.clear();
126 }
127
UvWorkWrapper(void * theData,napi_env theEnv,std::string eventType,EventManager * eventManager)128 UvWorkWrapper::UvWorkWrapper(void *theData, napi_env theEnv, std::string eventType, EventManager *eventManager)
129 : data(theData), env(theEnv), type(std::move(eventType)), manager(eventManager)
130 {
131 }
132 } // namespace NetManagerStandard
133 } // namespace OHOS
134