1 /*
2 * Copyright (c) 2022-2023 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 "connection_module.h"
17
18 #include "bindsocket_context.h"
19 #include "connection_async_work.h"
20 #include "connection_exec.h"
21 #include "constant.h"
22 #include "getaddressbyname_context.h"
23 #include "getappnet_context.h"
24 #include "getdefaultnet_context.h"
25 #include "gethttpproxy_context.h"
26 #include "napi_constant.h"
27 #include "net_all_capabilities.h"
28 #include "netconnection.h"
29 #include "netmanager_base_log.h"
30 #include "none_params_context.h"
31 #include "module_template.h"
32 #include "parse_nethandle_context.h"
33 #include "register_context.h"
34 #include "setappnet_context.h"
35 #include "setglobalhttpproxy_context.h"
36
37 static constexpr const char *CONNECTION_MODULE_NAME = "net.connection";
38
39 #define DECLARE_NET_CAP(cap) \
40 DECLARE_NAPI_STATIC_PROPERTY(#cap, NapiUtils::CreateUint32(env, static_cast<uint32_t>(NetCap::cap)))
41
42 #define DECLARE_NET_BEAR_TYPE(type) \
43 DECLARE_NAPI_STATIC_PROPERTY(#type, NapiUtils::CreateUint32(env, static_cast<uint32_t>(NetBearType::type)))
44
45 namespace OHOS::NetManagerStandard {
46
ParseTypesArray(napi_env env,napi_value obj,std::set<T> & typeArray)47 template <typename T> static bool ParseTypesArray(napi_env env, napi_value obj, std::set<T> &typeArray)
48 {
49 if (!NapiUtils::IsArray(env, obj)) {
50 return false;
51 }
52 uint32_t arrayLenght =
53 NapiUtils::GetArrayLength(env, obj) > MAX_ARRAY_LENGTH ? MAX_ARRAY_LENGTH : NapiUtils::GetArrayLength(env, obj);
54 for (uint32_t i = 0; i < arrayLenght; ++i) {
55 napi_value val = NapiUtils::GetArrayElement(env, obj, i);
56 if (NapiUtils::GetValueType(env, val) == napi_number) {
57 typeArray.insert(static_cast<T>(NapiUtils::GetUint32FromValue(env, val)));
58 } else {
59 NETMANAGER_BASE_LOGE("Invalid parameter type of array element!");
60 return false;
61 }
62 }
63 return true;
64 }
65
ParseCapabilities(napi_env env,napi_value obj,NetAllCapabilities & capabilities)66 static bool ParseCapabilities(napi_env env, napi_value obj, NetAllCapabilities &capabilities)
67 {
68 if (NapiUtils::GetValueType(env, obj) != napi_object) {
69 return false;
70 }
71
72 capabilities.linkUpBandwidthKbps_ = NapiUtils::GetUint32Property(env, obj, KEY_LINK_UP_BAND_WIDTH_KPS);
73 capabilities.linkDownBandwidthKbps_ = NapiUtils::GetUint32Property(env, obj, KEY_LINK_DOWN_BAND_WIDTH_KPS);
74
75 napi_value networkCap = NapiUtils::GetNamedProperty(env, obj, KEY_NETWORK_CAP);
76 (void)ParseTypesArray<NetCap>(env, networkCap, capabilities.netCaps_);
77
78 napi_value bearerTypes = NapiUtils::GetNamedProperty(env, obj, KEY_BEARER_TYPE);
79 if (!ParseTypesArray<NetBearType>(env, bearerTypes, capabilities.bearerTypes_)) {
80 return false;
81 }
82
83 return true;
84 }
85
ParseNetSpecifier(napi_env env,napi_value obj,NetSpecifier & specifier)86 static bool ParseNetSpecifier(napi_env env, napi_value obj, NetSpecifier &specifier)
87 {
88 napi_value capabilitiesObj = NapiUtils::GetNamedProperty(env, obj, KEY_NET_CAPABILITIES);
89 if (!ParseCapabilities(env, capabilitiesObj, specifier.netCapabilities_)) {
90 return false;
91 }
92 specifier.ident_ = NapiUtils::GetStringPropertyUtf8(env, obj, KEY_BEARER_PRIVATE_IDENTIFIER);
93 return true;
94 }
95
GetNetConnectionType(napi_env env,size_t argc,napi_value * argv)96 static NetConnectionType GetNetConnectionType(napi_env env, size_t argc, napi_value *argv)
97 {
98 if (argc == ARG_NUM_0) {
99 return NetConnectionType::PARAMETER_ZERO;
100 }
101 if (argc == ARG_NUM_1) {
102 if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_undefined) {
103 return NetConnectionType::PARAMETER_ZERO;
104 }
105 if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_object) {
106 return NetConnectionType::PARAMETER_SPECIFIER;
107 }
108 return NetConnectionType::PARAMETER_ERROR;
109 }
110 if (argc == ARG_NUM_2) {
111 if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_object &&
112 NapiUtils::GetValueType(env, argv[ARG_INDEX_1]) == napi_number) {
113 return NetConnectionType::PARAMETER_TIMEOUT;
114 }
115 if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_undefined &&
116 NapiUtils::GetValueType(env, argv[ARG_INDEX_1]) == napi_undefined) {
117 return NetConnectionType::PARAMETER_ZERO;
118 }
119 if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_object &&
120 NapiUtils::GetValueType(env, argv[ARG_INDEX_1]) == napi_undefined) {
121 return NetConnectionType::PARAMETER_SPECIFIER;
122 }
123 }
124 return NetConnectionType::PARAMETER_ERROR;
125 }
126
ParseNetConnectionParams(napi_env env,size_t argc,napi_value * argv,EventManager * manager)127 static void *ParseNetConnectionParams(napi_env env, size_t argc, napi_value *argv, EventManager *manager)
128 {
129 std::unique_ptr<NetConnection, decltype(&NetConnection::DeleteNetConnection)> netConnection(
130 NetConnection::MakeNetConnection(manager), NetConnection::DeleteNetConnection);
131
132 auto netConnType = GetNetConnectionType(env, argc, argv);
133
134 switch (netConnType) {
135 case NetConnectionType::PARAMETER_ZERO: {
136 NETMANAGER_BASE_LOGI("ParseNetConnectionParams no params");
137 return netConnection.release();
138 }
139 case NetConnectionType::PARAMETER_SPECIFIER: {
140 if (!ParseNetSpecifier(env, argv[ARG_INDEX_0], netConnection->netSpecifier_)) {
141 NETMANAGER_BASE_LOGE("ParseNetSpecifier failed");
142 return nullptr;
143 }
144 netConnection->hasNetSpecifier_ = true;
145 return netConnection.release();
146 }
147 case NetConnectionType::PARAMETER_TIMEOUT: {
148 if (!ParseNetSpecifier(env, argv[ARG_INDEX_0], netConnection->netSpecifier_)) {
149 NETMANAGER_BASE_LOGE("ParseNetSpecifier failed, do not use params");
150 return nullptr;
151 }
152 netConnection->hasNetSpecifier_ = true;
153 netConnection->hasTimeout_ = true;
154 netConnection->timeout_ = NapiUtils::GetUint32FromValue(env, argv[ARG_INDEX_1]);
155 return netConnection.release();
156 }
157 default:
158 NETMANAGER_BASE_LOGE("constructor params invalid, should be none or specifier or specifier+timeout_");
159 return nullptr;
160 }
161 }
162
InitConnectionModule(napi_env env,napi_value exports)163 napi_value ConnectionModule::InitConnectionModule(napi_env env, napi_value exports)
164 {
165 std::initializer_list<napi_property_descriptor> functions = {
166 DECLARE_NAPI_FUNCTION(FUNCTION_GET_DEFAULT_NET, GetDefaultNet),
167 DECLARE_NAPI_FUNCTION(FUNCTION_GET_DEFAULT_NET_SYNC, GetDefaultNetSync),
168 DECLARE_NAPI_FUNCTION(FUNCTION_CREATE_NET_CONNECTION, CreateNetConnection),
169 DECLARE_NAPI_FUNCTION(FUNCTION_GET_ADDRESSES_BY_NAME, GetAddressesByName),
170 DECLARE_NAPI_FUNCTION(FUNCTION_HAS_DEFAULT_NET, HasDefaultNet),
171 DECLARE_NAPI_FUNCTION(FUNCTION_HAS_DEFAULT_NET_SYNC, HasDefaultNetSync),
172 DECLARE_NAPI_FUNCTION(FUNCTION_IS_DEFAULT_NET_METERED, IsDefaultNetMetered),
173 DECLARE_NAPI_FUNCTION(FUNCTION_IS_DEFAULT_NET_METERED_SYNC, IsDefaultNetMeteredSync),
174 DECLARE_NAPI_FUNCTION(FUNCTION_GET_NET_CAPABILITIES, GetNetCapabilities),
175 DECLARE_NAPI_FUNCTION(FUNCTION_GET_NET_CAPABILITIES_SYNC, GetNetCapabilitiesSync),
176 DECLARE_NAPI_FUNCTION(FUNCTION_GET_CONNECTION_PROPERTIES, GetConnectionProperties),
177 DECLARE_NAPI_FUNCTION(FUNCTION_GET_CONNECTION_PROPERTIES_SYNC, GetConnectionPropertiesSync),
178 DECLARE_NAPI_FUNCTION(FUNCTION_GET_ALL_NETS, GetAllNets),
179 DECLARE_NAPI_FUNCTION(FUNCTION_GET_ALL_NETS_SYNC, GetAllNetsSync),
180 DECLARE_NAPI_FUNCTION(FUNCTION_ENABLE_AIRPLANE_MODE, EnableAirplaneMode),
181 DECLARE_NAPI_FUNCTION(FUNCTION_DISABLE_AIRPLANE_MODE, DisableAirplaneMode),
182 DECLARE_NAPI_FUNCTION(FUNCTION_REPORT_NET_CONNECTED, ReportNetConnected),
183 DECLARE_NAPI_FUNCTION(FUNCTION_REPORT_NET_DISCONNECTED, ReportNetDisconnected),
184 DECLARE_NAPI_FUNCTION(FUNCTION_GET_DEFAULT_HTTP_PROXY, GetDefaultHttpProxy),
185 DECLARE_NAPI_FUNCTION(FUNCTION_GET_GLOBAL_HTTP_PROXY, GetGlobalHttpProxy),
186 DECLARE_NAPI_FUNCTION(FUNCTION_SET_GLOBAL_HTTP_PROXY, SetGlobalHttpProxy),
187 DECLARE_NAPI_FUNCTION(FUNCTION_GET_APP_NET, GetAppNet),
188 DECLARE_NAPI_FUNCTION(FUNCTION_GET_APP_NET_SYNC, GetAppNetSync),
189 DECLARE_NAPI_FUNCTION(FUNCTION_SET_APP_NET, SetAppNet),
190 };
191 NapiUtils::DefineProperties(env, exports, functions);
192
193 std::initializer_list<napi_property_descriptor> netConnectionFunctions = {
194 DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_ON, NetConnectionInterface::On),
195 DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_REGISTER, NetConnectionInterface::Register),
196 DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_UNREGISTER, NetConnectionInterface::Unregister),
197 };
198 ModuleTemplate::DefineClass(env, exports, netConnectionFunctions, INTERFACE_NET_CONNECTION);
199
200 InitProperties(env, exports);
201 return exports;
202 }
203
InitProperties(napi_env env,napi_value exports)204 void ConnectionModule::InitProperties(napi_env env, napi_value exports)
205 {
206 std::initializer_list<napi_property_descriptor> netCaps = {
207 DECLARE_NET_CAP(NET_CAPABILITY_MMS),
208 DECLARE_NET_CAP(NET_CAPABILITY_NOT_METERED),
209 DECLARE_NET_CAP(NET_CAPABILITY_INTERNET),
210 DECLARE_NET_CAP(NET_CAPABILITY_NOT_VPN),
211 DECLARE_NET_CAP(NET_CAPABILITY_VALIDATED),
212 DECLARE_NET_CAP(NET_CAPABILITY_CAPTIVE_PORTAL),
213 DECLARE_NET_CAP(NET_CAPABILITY_INTERNAL_DEFAULT),
214 };
215 napi_value caps = NapiUtils::CreateObject(env);
216 NapiUtils::DefineProperties(env, caps, netCaps);
217 NapiUtils::SetNamedProperty(env, exports, INTERFACE_NET_CAP, caps);
218
219 std::initializer_list<napi_property_descriptor> netBearTypes = {
220 DECLARE_NET_BEAR_TYPE(BEARER_CELLULAR), DECLARE_NET_BEAR_TYPE(BEARER_WIFI),
221 DECLARE_NET_BEAR_TYPE(BEARER_BLUETOOTH), DECLARE_NET_BEAR_TYPE(BEARER_ETHERNET),
222 DECLARE_NET_BEAR_TYPE(BEARER_VPN), DECLARE_NET_BEAR_TYPE(BEARER_WIFI_AWARE),
223 DECLARE_NET_BEAR_TYPE(BEARER_DEFAULT),
224 };
225 napi_value types = NapiUtils::CreateObject(env);
226 NapiUtils::DefineProperties(env, types, netBearTypes);
227 NapiUtils::SetNamedProperty(env, exports, INTERFACE_NET_BEAR_TYPE, types);
228 }
229
GetAddressesByName(napi_env env,napi_callback_info info)230 napi_value ConnectionModule::GetAddressesByName(napi_env env, napi_callback_info info)
231 {
232 return ModuleTemplate::Interface<GetAddressByNameContext>(env, info, FUNCTION_GET_ADDRESSES_BY_NAME, nullptr,
233 ConnectionAsyncWork::ExecGetAddressesByName,
234 ConnectionAsyncWork::GetAddressesByNameCallback);
235 }
236
HasDefaultNet(napi_env env,napi_callback_info info)237 napi_value ConnectionModule::HasDefaultNet(napi_env env, napi_callback_info info)
238 {
239 return ModuleTemplate::Interface<HasDefaultNetContext>(env, info, FUNCTION_HAS_DEFAULT_NET, nullptr,
240 ConnectionAsyncWork::ExecHasDefaultNet,
241 ConnectionAsyncWork::HasDefaultNetCallback);
242 }
243
HasDefaultNetSync(napi_env env,napi_callback_info info)244 napi_value ConnectionModule::HasDefaultNetSync(napi_env env, napi_callback_info info)
245 {
246 return ModuleTemplate::InterfaceSync<HasDefaultNetContext>(env, info, FUNCTION_HAS_DEFAULT_NET, nullptr,
247 ConnectionExec::ExecHasDefaultNet,
248 ConnectionExec::HasDefaultNetCallback);
249 }
250
IsDefaultNetMetered(napi_env env,napi_callback_info info)251 napi_value ConnectionModule::IsDefaultNetMetered(napi_env env, napi_callback_info info)
252 {
253 return ModuleTemplate::Interface<IsDefaultNetMeteredContext>(env, info, FUNCTION_IS_DEFAULT_NET_METERED, nullptr,
254 ConnectionAsyncWork::ExecIsDefaultNetMetered,
255 ConnectionAsyncWork::IsDefaultNetMeteredCallback);
256 }
257
IsDefaultNetMeteredSync(napi_env env,napi_callback_info info)258 napi_value ConnectionModule::IsDefaultNetMeteredSync(napi_env env, napi_callback_info info)
259 {
260 return ModuleTemplate::InterfaceSync<IsDefaultNetMeteredContext>(env, info, FUNCTION_IS_DEFAULT_NET_METERED, nullptr,
261 ConnectionExec::ExecIsDefaultNetMetered,
262 ConnectionExec::IsDefaultNetMeteredCallback);
263 }
264
GetNetCapabilities(napi_env env,napi_callback_info info)265 napi_value ConnectionModule::GetNetCapabilities(napi_env env, napi_callback_info info)
266 {
267 return ModuleTemplate::Interface<GetNetCapabilitiesContext>(env, info, FUNCTION_GET_NET_CAPABILITIES, nullptr,
268 ConnectionAsyncWork::ExecGetNetCapabilities,
269 ConnectionAsyncWork::GetNetCapabilitiesCallback);
270 }
271
GetNetCapabilitiesSync(napi_env env,napi_callback_info info)272 napi_value ConnectionModule::GetNetCapabilitiesSync(napi_env env, napi_callback_info info)
273 {
274 return ModuleTemplate::InterfaceSync<GetNetCapabilitiesContext>(env, info, FUNCTION_GET_NET_CAPABILITIES, nullptr,
275 ConnectionExec::ExecGetNetCapabilities,
276 ConnectionExec::GetNetCapabilitiesCallback);
277 }
278
GetConnectionProperties(napi_env env,napi_callback_info info)279 napi_value ConnectionModule::GetConnectionProperties(napi_env env, napi_callback_info info)
280 {
281 return ModuleTemplate::Interface<GetConnectionPropertiesContext>(
282 env, info, FUNCTION_GET_CONNECTION_PROPERTIES, nullptr, ConnectionAsyncWork::ExecGetConnectionProperties,
283 ConnectionAsyncWork::GetConnectionPropertiesCallback);
284 }
285
GetConnectionPropertiesSync(napi_env env,napi_callback_info info)286 napi_value ConnectionModule::GetConnectionPropertiesSync(napi_env env, napi_callback_info info)
287 {
288 return ModuleTemplate::InterfaceSync<GetConnectionPropertiesContext>(
289 env, info, FUNCTION_GET_CONNECTION_PROPERTIES, nullptr, ConnectionExec::ExecGetConnectionProperties,
290 ConnectionExec::GetConnectionPropertiesCallback);
291 }
292
CreateNetConnection(napi_env env,napi_callback_info info)293 napi_value ConnectionModule::CreateNetConnection(napi_env env, napi_callback_info info)
294 {
295 return ModuleTemplate::NewInstance(env, info, INTERFACE_NET_CONNECTION, ParseNetConnectionParams,
296 [](napi_env, void *data, void *) {
297 NETMANAGER_BASE_LOGI("finalize netConnection");
298 auto manager = static_cast<EventManager *>(data);
299 auto netConnection = static_cast<NetConnection *>(manager->GetData());
300 delete manager;
301 NetConnection::DeleteNetConnection(netConnection);
302 });
303 }
304
GetDefaultNet(napi_env env,napi_callback_info info)305 napi_value ConnectionModule::GetDefaultNet(napi_env env, napi_callback_info info)
306 {
307 return ModuleTemplate::Interface<GetDefaultNetContext>(env, info, FUNCTION_GET_DEFAULT_NET, nullptr,
308 ConnectionAsyncWork::ExecGetDefaultNet,
309 ConnectionAsyncWork::GetDefaultNetCallback);
310 }
311
GetDefaultNetSync(napi_env env,napi_callback_info info)312 napi_value ConnectionModule::GetDefaultNetSync(napi_env env, napi_callback_info info)
313 {
314 GetDefaultNetContext context(env, nullptr);
315 if (ConnectionExec::ExecGetDefaultNet(&context)) {
316 return ConnectionExec::GetDefaultNetCallback(&context);
317 }
318 return NapiUtils::CreateErrorMessage(env, context.GetErrorCode(), context.GetErrorMessage());
319 }
320
GetAllNets(napi_env env,napi_callback_info info)321 napi_value ConnectionModule::GetAllNets(napi_env env, napi_callback_info info)
322 {
323 return ModuleTemplate::Interface<GetAllNetsContext>(env, info, FUNCTION_GET_ALL_NETS, nullptr,
324 ConnectionAsyncWork::ExecGetAllNets,
325 ConnectionAsyncWork::GetAllNetsCallback);
326 }
327
GetAllNetsSync(napi_env env,napi_callback_info info)328 napi_value ConnectionModule::GetAllNetsSync(napi_env env, napi_callback_info info)
329 {
330 return ModuleTemplate::InterfaceSync<GetAllNetsContext>(env, info, FUNCTION_GET_ALL_NETS, nullptr,
331 ConnectionExec::ExecGetAllNets,
332 ConnectionExec::GetAllNetsCallback);
333 }
334
EnableAirplaneMode(napi_env env,napi_callback_info info)335 napi_value ConnectionModule::EnableAirplaneMode(napi_env env, napi_callback_info info)
336 {
337 return ModuleTemplate::Interface<EnableAirplaneModeContext>(env, info, FUNCTION_ENABLE_AIRPLANE_MODE, nullptr,
338 ConnectionAsyncWork::ExecEnableAirplaneMode,
339 ConnectionAsyncWork::EnableAirplaneModeCallback);
340 }
341
DisableAirplaneMode(napi_env env,napi_callback_info info)342 napi_value ConnectionModule::DisableAirplaneMode(napi_env env, napi_callback_info info)
343 {
344 return ModuleTemplate::Interface<DisableAirplaneModeContext>(env, info, FUNCTION_DISABLE_AIRPLANE_MODE, nullptr,
345 ConnectionAsyncWork::ExecDisableAirplaneMode,
346 ConnectionAsyncWork::DisableAirplaneModeCallback);
347 }
348
ReportNetConnected(napi_env env,napi_callback_info info)349 napi_value ConnectionModule::ReportNetConnected(napi_env env, napi_callback_info info)
350 {
351 return ModuleTemplate::Interface<ReportNetConnectedContext>(env, info, FUNCTION_REPORT_NET_CONNECTED, nullptr,
352 ConnectionAsyncWork::ExecReportNetConnected,
353 ConnectionAsyncWork::ReportNetConnectedCallback);
354 }
355
ReportNetDisconnected(napi_env env,napi_callback_info info)356 napi_value ConnectionModule::ReportNetDisconnected(napi_env env, napi_callback_info info)
357 {
358 return ModuleTemplate::Interface<ReportNetDisconnectedContext>(env, info, FUNCTION_REPORT_NET_DISCONNECTED, nullptr,
359 ConnectionAsyncWork::ExecReportNetDisconnected,
360 ConnectionAsyncWork::ReportNetDisconnectedCallback);
361 }
362
GetDefaultHttpProxy(napi_env env,napi_callback_info info)363 napi_value ConnectionModule::GetDefaultHttpProxy(napi_env env, napi_callback_info info)
364 {
365 return ModuleTemplate::Interface<GetHttpProxyContext>(env, info, FUNCTION_GET_DEFAULT_HTTP_PROXY, nullptr,
366 ConnectionAsyncWork::ExecGetDefaultHttpProxy,
367 ConnectionAsyncWork::GetDefaultHttpProxyCallback);
368 }
369
GetGlobalHttpProxy(napi_env env,napi_callback_info info)370 napi_value ConnectionModule::GetGlobalHttpProxy(napi_env env, napi_callback_info info)
371 {
372 return ModuleTemplate::Interface<GetHttpProxyContext>(env, info, FUNCTION_GET_GLOBAL_HTTP_PROXY, nullptr,
373 ConnectionAsyncWork::ExecGetGlobalHttpProxy,
374 ConnectionAsyncWork::GetGlobalHttpProxyCallback);
375 }
376
SetGlobalHttpProxy(napi_env env,napi_callback_info info)377 napi_value ConnectionModule::SetGlobalHttpProxy(napi_env env, napi_callback_info info)
378 {
379 return ModuleTemplate::Interface<SetGlobalHttpProxyContext>(env, info, FUNCTION_SET_GLOBAL_HTTP_PROXY, nullptr,
380 ConnectionAsyncWork::ExecSetGlobalHttpProxy,
381 ConnectionAsyncWork::SetGlobalHttpProxyCallback);
382 }
383
GetAppNet(napi_env env,napi_callback_info info)384 napi_value ConnectionModule::GetAppNet(napi_env env, napi_callback_info info)
385 {
386 return ModuleTemplate::Interface<GetAppNetContext>(env, info, FUNCTION_GET_APP_NET, nullptr,
387 ConnectionAsyncWork::ExecGetAppNet,
388 ConnectionAsyncWork::GetAppNetCallback);
389 }
390
GetAppNetSync(napi_env env,napi_callback_info info)391 napi_value ConnectionModule::GetAppNetSync(napi_env env, napi_callback_info info)
392 {
393 return ModuleTemplate::InterfaceSync<GetAppNetContext>(env, info, FUNCTION_GET_APP_NET, nullptr,
394 ConnectionExec::ExecGetAppNet,
395 ConnectionExec::GetAppNetCallback);
396 }
397
SetAppNet(napi_env env,napi_callback_info info)398 napi_value ConnectionModule::SetAppNet(napi_env env, napi_callback_info info)
399 {
400 return ModuleTemplate::Interface<SetAppNetContext>(env, info, FUNCTION_SET_APP_NET, nullptr,
401 ConnectionAsyncWork::ExecSetAppNet,
402 ConnectionAsyncWork::SetAppNetCallback);
403 }
404
GetAddressesByName(napi_env env,napi_callback_info info)405 napi_value ConnectionModule::NetHandleInterface::GetAddressesByName(napi_env env, napi_callback_info info)
406 {
407 return ModuleTemplate::Interface<GetAddressByNameContext>(
408 env, info, FUNCTION_GET_ADDRESSES_BY_NAME, nullptr,
409 ConnectionAsyncWork::NetHandleAsyncWork::ExecGetAddressesByName,
410 ConnectionAsyncWork::NetHandleAsyncWork::GetAddressesByNameCallback);
411 }
412
GetAddressByName(napi_env env,napi_callback_info info)413 napi_value ConnectionModule::NetHandleInterface::GetAddressByName(napi_env env, napi_callback_info info)
414 {
415 return ModuleTemplate::Interface<GetAddressByNameContext>(
416 env, info, FUNCTION_GET_ADDRESSES_BY_NAME, nullptr,
417 ConnectionAsyncWork::NetHandleAsyncWork::ExecGetAddressByName,
418 ConnectionAsyncWork::NetHandleAsyncWork::GetAddressByNameCallback);
419 }
420
BindSocket(napi_env env,napi_callback_info info)421 napi_value ConnectionModule::NetHandleInterface::BindSocket(napi_env env, napi_callback_info info)
422 {
423 return ModuleTemplate::Interface<BindSocketContext>(
424 env, info, FUNCTION_BIND_SOCKET,
425 [](napi_env theEnv, napi_value thisVal, BindSocketContext *context) -> bool {
426 context->netId_ = NapiUtils::GetInt32Property(theEnv, thisVal, PROPERTY_NET_ID);
427 return true;
428 },
429 ConnectionAsyncWork::NetHandleAsyncWork::ExecBindSocket,
430 ConnectionAsyncWork::NetHandleAsyncWork::BindSocketCallback);
431 }
432
On(napi_env env,napi_callback_info info)433 napi_value ConnectionModule::NetConnectionInterface::On(napi_env env, napi_callback_info info)
434 {
435 std::initializer_list<std::string> events = {EVENT_NET_AVAILABLE,
436 EVENT_NET_BLOCK_STATUS_CHANGE,
437 EVENT_NET_CAPABILITIES_CHANGE,
438 EVENT_NET_CONNECTION_PROPERTIES_CHANGE,
439 EVENT_NET_LOST,
440 EVENT_NET_UNAVAILABLE};
441 return ModuleTemplate::On(env, info, events, false);
442 }
443
Register(napi_env env,napi_callback_info info)444 napi_value ConnectionModule::NetConnectionInterface::Register(napi_env env, napi_callback_info info)
445 {
446 return ModuleTemplate::Interface<RegisterContext>(env, info, FUNCTION_REGISTER, nullptr,
447 ConnectionAsyncWork::NetConnectionAsyncWork::ExecRegister,
448 ConnectionAsyncWork::NetConnectionAsyncWork::RegisterCallback);
449 }
450
Unregister(napi_env env,napi_callback_info info)451 napi_value ConnectionModule::NetConnectionInterface::Unregister(napi_env env, napi_callback_info info)
452 {
453 return ModuleTemplate::Interface<UnregisterContext>(
454 env, info, FUNCTION_UNREGISTER,
455 [](napi_env theEnv, napi_value thisVal, UnregisterContext *context) -> bool {
456 if (context && context->GetManager()) {
457 context->GetManager()->DeleteAllListener();
458 }
459 return true;
460 }, ConnectionAsyncWork::NetConnectionAsyncWork::ExecUnregister,
461 ConnectionAsyncWork::NetConnectionAsyncWork::UnregisterCallback);
462 }
463
464 static napi_module g_connectionModule = {
465 .nm_version = 1,
466 .nm_flags = 0,
467 .nm_filename = nullptr,
468 .nm_register_func = ConnectionModule::InitConnectionModule,
469 .nm_modname = CONNECTION_MODULE_NAME,
470 .nm_priv = nullptr,
471 .reserved = {nullptr},
472 };
473
RegisterConnectionModule(void)474 extern "C" __attribute__((constructor)) void RegisterConnectionModule(void)
475 {
476 napi_module_register(&g_connectionModule);
477 }
478 } // namespace OHOS::NetManagerStandard
479