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