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 lock(mutexForListenersAndEmitByUv_);
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 lock(mutexForListenersAndEmitByUv_);
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::lock_guard lock(mutexForEmitAndEmitByUv_);
65 std::for_each(listeners_.begin(), listeners_.end(), [type, argv](const EventListener &listener) {
66 if (listener.IsAsyncCallback()) {
67 /* AsyncCallback(BusinessError error, T data) */
68 napi_value arg[ASYNC_CALLBACK_PARAM_NUM] = {argv.first, argv.second};
69 listener.Emit(type, ASYNC_CALLBACK_PARAM_NUM, arg);
70 } else {
71 /* Callback(T data) */
72 napi_value arg[CALLBACK_PARAM_NUM] = {argv.second};
73 listener.Emit(type, CALLBACK_PARAM_NUM, arg);
74 }
75 });
76
77 auto it = std::remove_if(listeners_.begin(), listeners_.end(),
78 [type](const EventListener &listener) -> bool { return listener.MatchOnce(type); });
79 listeners_.erase(it, listeners_.end());
80 }
81
SetData(void * data)82 void EventManager::SetData(void *data)
83 {
84 std::lock_guard<std::mutex> lock(mutex_);
85 data_ = data;
86 }
87
GetData()88 void *EventManager::GetData()
89 {
90 std::lock_guard<std::mutex> lock(mutex_);
91 return data_;
92 }
93
EmitByUv(const std::string & type,void * data,void (handler)(uv_work_t *,int status))94 void EventManager::EmitByUv(const std::string &type, void *data, void(handler)(uv_work_t *, int status))
95 {
96 std::lock_guard lock1(mutexForEmitAndEmitByUv_);
97 std::lock_guard lock2(mutexForListenersAndEmitByUv_);
98 if (!IsValid()) {
99 return;
100 }
101
102 std::for_each(listeners_.begin(), listeners_.end(), [type, data, handler, this](const EventListener &listener) {
103 auto workWrapper = new UvWorkWrapper(data, listener.GetEnv(), type, this);
104 listener.EmitByUv(type, workWrapper, handler);
105 });
106 }
107
HasEventListener(const std::string & type)108 bool EventManager::HasEventListener(const std::string &type)
109 {
110 std::lock_guard lock2(mutexForListenersAndEmitByUv_);
111 return std::any_of(listeners_.begin(), listeners_.end(),
112 [&type](const EventListener &listener) -> bool { return listener.MatchType(type); });
113 }
114
DeleteListener(const std::string & type)115 void EventManager::DeleteListener(const std::string &type)
116 {
117 std::lock_guard lock2(mutexForListenersAndEmitByUv_);
118 auto it = std::remove_if(listeners_.begin(), listeners_.end(),
119 [type](const EventListener &listener) -> bool { return listener.MatchType(type); });
120 listeners_.erase(it, listeners_.end());
121 }
122
DeleteAllListener()123 void EventManager::DeleteAllListener()
124 {
125 std::lock_guard<std::mutex> lock(mutex_);
126 listeners_.clear();
127 }
128
UvWorkWrapper(void * theData,napi_env theEnv,std::string eventType,EventManager * eventManager)129 UvWorkWrapper::UvWorkWrapper(void *theData, napi_env theEnv, std::string eventType, EventManager *eventManager)
130 : data(theData), env(theEnv), type(std::move(eventType)), manager(eventManager)
131 {
132 }
133 } // namespace NetManagerStandard
134 } // namespace OHOS
135