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 #include "module_template.h"
17
18 #include <algorithm>
19 #include <initializer_list>
20 #include <new>
21 #include <string>
22 #include <functional>
23
24 #include "event_manager.h"
25 #include "netstack_log.h"
26
27 namespace OHOS::NetStack::ModuleTemplate {
28 static constexpr const int EVENT_PARAM_NUM = 2;
29
On(napi_env env,napi_callback_info info,const std::initializer_list<std::string> & events,bool asyncCallback)30 napi_value On(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events,
31 bool asyncCallback)
32 {
33 napi_value thisVal = nullptr;
34 size_t paramsCount = MAX_PARAM_NUM;
35 napi_value params[MAX_PARAM_NUM] = {nullptr};
36 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
37
38 if (paramsCount != EVENT_PARAM_NUM || NapiUtils::GetValueType(env, params[0]) != napi_string ||
39 NapiUtils::GetValueType(env, params[1]) != napi_function) {
40 NETSTACK_LOGE("on off once interface para: [string, function]");
41 return NapiUtils::GetUndefined(env);
42 }
43
44 std::string event = NapiUtils::GetStringFromValueUtf8(env, params[0]);
45 if (std::find(events.begin(), events.end(), event) == events.end()) {
46 return NapiUtils::GetUndefined(env);
47 }
48
49 EventManager *manager = nullptr;
50 napi_unwrap(env, thisVal, reinterpret_cast<void **>(&manager));
51 if (manager != nullptr) {
52 manager->AddListener(env, event, params[1], false, asyncCallback);
53 }
54
55 return NapiUtils::GetUndefined(env);
56 }
57
Once(napi_env env,napi_callback_info info,const std::initializer_list<std::string> & events,bool asyncCallback)58 napi_value Once(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events,
59 bool asyncCallback)
60 {
61 napi_value thisVal = nullptr;
62 size_t paramsCount = MAX_PARAM_NUM;
63 napi_value params[MAX_PARAM_NUM] = {nullptr};
64 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
65
66 if (paramsCount != EVENT_PARAM_NUM || NapiUtils::GetValueType(env, params[0]) != napi_string ||
67 NapiUtils::GetValueType(env, params[1]) != napi_function) {
68 NETSTACK_LOGE("on off once interface para: [string, function]");
69 return NapiUtils::GetUndefined(env);
70 }
71
72 std::string event = NapiUtils::GetStringFromValueUtf8(env, params[0]);
73 if (std::find(events.begin(), events.end(), event) == events.end()) {
74 return NapiUtils::GetUndefined(env);
75 }
76
77 EventManager *manager = nullptr;
78 napi_unwrap(env, thisVal, reinterpret_cast<void **>(&manager));
79 if (manager != nullptr) {
80 manager->AddListener(env, event, params[1], true, asyncCallback);
81 }
82
83 return NapiUtils::GetUndefined(env);
84 }
85
Off(napi_env env,napi_callback_info info,const std::initializer_list<std::string> & events)86 napi_value Off(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events)
87 {
88 napi_value thisVal = nullptr;
89 size_t paramsCount = MAX_PARAM_NUM;
90 napi_value params[MAX_PARAM_NUM] = {nullptr};
91 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
92
93 if ((paramsCount != 1 && paramsCount != EVENT_PARAM_NUM) ||
94 NapiUtils::GetValueType(env, params[0]) != napi_string) {
95 NETSTACK_LOGE("on off once interface para: [string, function?]");
96 return NapiUtils::GetUndefined(env);
97 }
98
99 if (paramsCount == EVENT_PARAM_NUM && NapiUtils::GetValueType(env, params[1]) != napi_function) {
100 NETSTACK_LOGE("on off once interface para: [string, function]");
101 return NapiUtils::GetUndefined(env);
102 }
103
104 std::string event = NapiUtils::GetStringFromValueUtf8(env, params[0]);
105 if (std::find(events.begin(), events.end(), event) == events.end()) {
106 return NapiUtils::GetUndefined(env);
107 }
108
109 EventManager *manager = nullptr;
110 napi_unwrap(env, thisVal, reinterpret_cast<void **>(&manager));
111 if (manager != nullptr) {
112 if (paramsCount == EVENT_PARAM_NUM) {
113 manager->DeleteListener(event, params[1]);
114 } else {
115 manager->DeleteListener(event);
116 }
117 }
118
119 return NapiUtils::GetUndefined(env);
120 }
121
DefineClass(napi_env env,napi_value exports,const std::initializer_list<napi_property_descriptor> & properties,const std::string & className)122 void DefineClass(napi_env env, napi_value exports, const std::initializer_list<napi_property_descriptor> &properties,
123 const std::string &className)
124 {
125 auto constructor = [](napi_env env, napi_callback_info info) -> napi_value {
126 napi_value thisVal = nullptr;
127 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVal, nullptr));
128
129 return thisVal;
130 };
131
132 napi_value jsConstructor = nullptr;
133
134 napi_property_descriptor descriptors[properties.size()];
135 std::copy(properties.begin(), properties.end(), descriptors);
136
137 NAPI_CALL_RETURN_VOID(env, napi_define_class(env, className.c_str(), NAPI_AUTO_LENGTH, constructor, nullptr,
138 properties.size(), descriptors, &jsConstructor));
139
140 NapiUtils::SetNamedProperty(env, exports, className, jsConstructor);
141 }
142
NewInstance(napi_env env,napi_callback_info info,const std::string & className,Finalizer finalizer)143 napi_value NewInstance(napi_env env, napi_callback_info info, const std::string &className, Finalizer finalizer)
144 {
145 napi_value thisVal = nullptr;
146 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVal, nullptr));
147
148 napi_value jsConstructor = NapiUtils::GetNamedProperty(env, thisVal, className);
149 if (NapiUtils::GetValueType(env, jsConstructor) == napi_undefined) {
150 return nullptr;
151 }
152
153 napi_value result = nullptr;
154 NAPI_CALL(env, napi_new_instance(env, jsConstructor, 0, nullptr, &result));
155
156 auto manager = new EventManager();
157 napi_wrap(env, result, reinterpret_cast<void *>(manager), finalizer, nullptr, nullptr);
158
159 return result;
160 }
161
NewInstanceNoManager(napi_env env,napi_callback_info info,const std::string & name,Finalizer finalizer)162 napi_value NewInstanceNoManager(napi_env env, napi_callback_info info, const std::string &name, Finalizer finalizer)
163 {
164 napi_value thisVal = nullptr;
165 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVal, nullptr));
166
167 napi_value jsConstructor = NapiUtils::GetNamedProperty(env, thisVal, name);
168 if (NapiUtils::GetValueType(env, jsConstructor) == napi_undefined) {
169 return nullptr;
170 }
171
172 napi_value result = nullptr;
173 NAPI_CALL(env, napi_new_instance(env, jsConstructor, 0, nullptr, &result));
174
175 return result;
176 }
177 } // namespace OHOS::NetStack::ModuleTemplate
178