• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "netinterface.h"
30 #include "netmanager_base_log.h"
31 #include "none_params_context.h"
32 #include "module_template.h"
33 #include "parse_nethandle_context.h"
34 #include "register_context.h"
35 #include "interfaceregister_context.h"
36 #include "setappnet_context.h"
37 #include "setglobalhttpproxy_context.h"
38 #include "setcustomdnsrule_context.h"
39 #include "deletecustomdnsrule_context.h"
40 #include "deletecustomdnsrules_context.h"
41 #include "getinterfaceconfig_context.h"
42 #include "registernetsupplier_context.h"
43 #include "unregisternetsupplier_context.h"
44 
45 static constexpr const char *CONNECTION_MODULE_NAME = "net.connection";
46 static thread_local uint64_t g_moduleId;
47 
48 #define DECLARE_NET_CAP(cap) \
49     DECLARE_NAPI_STATIC_PROPERTY(#cap, NapiUtils::CreateUint32(env, static_cast<uint32_t>(NetCap::cap)))
50 
51 #define DECLARE_NET_BEAR_TYPE(type) \
52     DECLARE_NAPI_STATIC_PROPERTY(#type, NapiUtils::CreateUint32(env, static_cast<uint32_t>(NetBearType::type)))
53 
54 namespace OHOS::NetManagerStandard {
55 
ParseTypesArray(napi_env env,napi_value obj,std::set<T> & typeArray,std::function<bool (uint32_t)> isValid)56 template <typename T> static bool ParseTypesArray(napi_env env, napi_value obj, std::set<T> &typeArray,
57     std::function<bool(uint32_t)> isValid)
58 {
59     if (!NapiUtils::IsArray(env, obj)) {
60         return false;
61     }
62     uint32_t arrayLength =
63         NapiUtils::GetArrayLength(env, obj) > MAX_ARRAY_LENGTH ? MAX_ARRAY_LENGTH : NapiUtils::GetArrayLength(env, obj);
64     for (uint32_t i = 0; i < arrayLength; ++i) {
65         napi_value val = NapiUtils::GetArrayElement(env, obj, i);
66         if (NapiUtils::GetValueType(env, val) == napi_number) {
67             uint32_t value = NapiUtils::GetUint32FromValue(env, val);
68             if (!isValid(value)) {
69                 NETMANAGER_BASE_LOGE("Invalid parameter value of array element!");
70                 return false;
71             }
72             typeArray.insert(static_cast<T>(value));
73         } else {
74             NETMANAGER_BASE_LOGE("Invalid parameter type of array element!");
75             return false;
76         }
77     }
78     return true;
79 }
80 
ParseCapabilities(napi_env env,napi_value obj,NetAllCapabilities & capabilities)81 static bool ParseCapabilities(napi_env env, napi_value obj, NetAllCapabilities &capabilities)
82 {
83     if (NapiUtils::GetValueType(env, obj) != napi_object) {
84         return false;
85     }
86 
87     capabilities.linkUpBandwidthKbps_ = NapiUtils::GetUint32Property(env, obj, KEY_LINK_UP_BAND_WIDTH_KPS);
88     capabilities.linkDownBandwidthKbps_ = NapiUtils::GetUint32Property(env, obj, KEY_LINK_DOWN_BAND_WIDTH_KPS);
89 
90     napi_value networkCap = NapiUtils::GetNamedProperty(env, obj, KEY_NETWORK_CAP);
91     (void)ParseTypesArray<NetCap>(env, networkCap, capabilities.netCaps_, [](uint32_t value) {
92         return value >= 0 && value <= static_cast<uint32_t>(NetCap::NET_CAPABILITY_END);
93     });
94 
95     napi_value bearerTypes = NapiUtils::GetNamedProperty(env, obj, KEY_BEARER_TYPE);
96     bool ret = ParseTypesArray<NetBearType>(env, bearerTypes, capabilities.bearerTypes_, [](uint32_t value) {
97         return value >= 0 && value <= static_cast<uint32_t>(NetBearType::BEARER_DEFAULT);
98     });
99     return ret;
100 }
101 
ParseNetSpecifier(napi_env env,napi_value obj,NetSpecifier & specifier)102 static bool ParseNetSpecifier(napi_env env, napi_value obj, NetSpecifier &specifier)
103 {
104     napi_value capabilitiesObj = NapiUtils::GetNamedProperty(env, obj, KEY_NET_CAPABILITIES);
105     if (!ParseCapabilities(env, capabilitiesObj, specifier.netCapabilities_)) {
106         return false;
107     }
108     specifier.ident_ = NapiUtils::GetStringPropertyUtf8(env, obj, KEY_BEARER_PRIVATE_IDENTIFIER);
109     return true;
110 }
111 
GetNetConnectionType(napi_env env,size_t argc,napi_value * argv)112 static NetConnectionType GetNetConnectionType(napi_env env, size_t argc, napi_value *argv)
113 {
114     if (argc == ARG_NUM_0) {
115         return NetConnectionType::PARAMETER_ZERO;
116     }
117     if (argc == ARG_NUM_1) {
118         if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_undefined) {
119             return NetConnectionType::PARAMETER_ZERO;
120         }
121         if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_object) {
122             return NetConnectionType::PARAMETER_SPECIFIER;
123         }
124         return NetConnectionType::PARAMETER_ERROR;
125     }
126     if (argc == ARG_NUM_2) {
127         if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_object &&
128             NapiUtils::GetValueType(env, argv[ARG_INDEX_1]) == napi_number) {
129             return NetConnectionType::PARAMETER_TIMEOUT;
130         }
131         if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_undefined &&
132             NapiUtils::GetValueType(env, argv[ARG_INDEX_1]) == napi_undefined) {
133             return NetConnectionType::PARAMETER_ZERO;
134         }
135         if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_object &&
136             NapiUtils::GetValueType(env, argv[ARG_INDEX_1]) == napi_undefined) {
137             return NetConnectionType::PARAMETER_SPECIFIER;
138         }
139     }
140     return NetConnectionType::PARAMETER_ERROR;
141 }
142 
ParseNetConnectionParams(napi_env env,size_t argc,napi_value * argv,std::shared_ptr<EventManager> & manager)143 static void *ParseNetConnectionParams(napi_env env, size_t argc, napi_value *argv,
144     std::shared_ptr<EventManager>& manager)
145 {
146     std::unique_ptr<NetConnection, decltype(&NetConnection::DeleteNetConnection)> netConnection(
147         NetConnection::MakeNetConnection(manager), NetConnection::DeleteNetConnection);
148     netConnection->moduleId_ = g_moduleId;
149 
150     auto netConnType = GetNetConnectionType(env, argc, argv);
151 
152     switch (netConnType) {
153         case NetConnectionType::PARAMETER_ZERO: {
154             NETMANAGER_BASE_LOGD("ParseNetConnectionParams no params");
155             return netConnection.release();
156         }
157         case NetConnectionType::PARAMETER_SPECIFIER: {
158             if (!ParseNetSpecifier(env, argv[ARG_INDEX_0], netConnection->netSpecifier_)) {
159                 NETMANAGER_BASE_LOGE("ParseNetSpecifier failed");
160                 return nullptr;
161             }
162             netConnection->hasNetSpecifier_ = true;
163             return netConnection.release();
164         }
165         case NetConnectionType::PARAMETER_TIMEOUT: {
166             if (!ParseNetSpecifier(env, argv[ARG_INDEX_0], netConnection->netSpecifier_)) {
167                 NETMANAGER_BASE_LOGE("ParseNetSpecifier failed, do not use params");
168                 return nullptr;
169             }
170             netConnection->hasNetSpecifier_ = true;
171             netConnection->hasTimeout_ = true;
172             netConnection->timeout_ = NapiUtils::GetUint32FromValue(env, argv[ARG_INDEX_1]);
173             return netConnection.release();
174         }
175         default:
176             NETMANAGER_BASE_LOGE("constructor params invalid, should be none or specifier or specifier+timeout_");
177             return nullptr;
178     }
179 }
180 
ParseNetInterfaceParams(napi_env env,size_t argc,napi_value * argv,std::shared_ptr<EventManager> & manager)181 static void *ParseNetInterfaceParams(napi_env env, size_t argc, napi_value *argv,
182     std::shared_ptr<EventManager>& manager)
183 {
184     std::unique_ptr<NetInterface, decltype(&NetInterface::DeleteNetInterface)> netInterface(
185         NetInterface::MakeNetInterface(manager), NetInterface::DeleteNetInterface);
186     netInterface->moduleId_ = g_moduleId;
187 
188     if (argc == ARG_NUM_0) {
189         return netInterface.release();
190     }
191     NETMANAGER_BASE_LOGE("constructor params invalid, should be none");
192     return nullptr;
193 }
194 
AddCleanupHook(napi_env env)195 static void AddCleanupHook(napi_env env)
196 {
197     NapiUtils::SetEnvValid(env);
198     auto envWrapper = new (std::nothrow) napi_env;
199     if (envWrapper == nullptr) {
200         NETMANAGER_BASE_LOGE("EnvWrapper create fail!");
201         return;
202     }
203     *envWrapper = env;
204     napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, envWrapper);
205 }
206 
207 #define DEFINE_PAC_FUNCTIONS \
208     DECLARE_NAPI_FUNCTION(FUNCTION_GET_PROXY_MODE, GetProxyMode),        \
209     DECLARE_NAPI_FUNCTION(FUNCTION_SET_PROXY_MODE, SetProxyMode),        \
210     DECLARE_NAPI_FUNCTION(FUNCTION_SET_FILE_PAC_URL, SetPacFileUrl),     \
211     DECLARE_NAPI_FUNCTION(FUNCTION_FIND_PROXY_FOR_URL, FindProxyForUrl), \
212     DECLARE_NAPI_FUNCTION(FUNCTION_GET_FILE_PAC_URL, GetPacFileUrl),
213 
createPropertyList()214 std::initializer_list<napi_property_descriptor> ConnectionModule::createPropertyList()
215 {
216     std::initializer_list<napi_property_descriptor> functions = {
217         DECLARE_NAPI_FUNCTION(FUNCTION_GET_DEFAULT_NET, GetDefaultNet),
218         DECLARE_NAPI_FUNCTION(FUNCTION_GET_DEFAULT_NET_SYNC, GetDefaultNetSync),
219         DECLARE_NAPI_FUNCTION(FUNCTION_CREATE_NET_CONNECTION, CreateNetConnection),
220         DECLARE_NAPI_FUNCTION(FUNCTION_GET_ADDRESSES_BY_NAME, GetAddressesByName),
221         DECLARE_NAPI_FUNCTION(FUNCTION_HAS_DEFAULT_NET, HasDefaultNet),
222         DECLARE_NAPI_FUNCTION(FUNCTION_HAS_DEFAULT_NET_SYNC, HasDefaultNetSync),
223         DECLARE_NAPI_FUNCTION(FUNCTION_IS_DEFAULT_NET_METERED, IsDefaultNetMetered),
224         DECLARE_NAPI_FUNCTION(FUNCTION_IS_DEFAULT_NET_METERED_SYNC, IsDefaultNetMeteredSync),
225         DECLARE_NAPI_FUNCTION(FUNCTION_GET_NET_CAPABILITIES, GetNetCapabilities),
226         DECLARE_NAPI_FUNCTION(FUNCTION_GET_NET_CAPABILITIES_SYNC, GetNetCapabilitiesSync),
227         DECLARE_NAPI_FUNCTION(FUNCTION_GET_CONNECTION_PROPERTIES, GetConnectionProperties),
228         DECLARE_NAPI_FUNCTION(FUNCTION_GET_CONNECTION_PROPERTIES_SYNC, GetConnectionPropertiesSync),
229         DECLARE_NAPI_FUNCTION(FUNCTION_GET_ALL_NETS, GetAllNets),
230         DECLARE_NAPI_FUNCTION(FUNCTION_GET_ALL_NETS_SYNC, GetAllNetsSync),
231         DECLARE_NAPI_FUNCTION(FUNCTION_ENABLE_AIRPLANE_MODE, EnableAirplaneMode),
232         DECLARE_NAPI_FUNCTION(FUNCTION_DISABLE_AIRPLANE_MODE, DisableAirplaneMode),
233         DECLARE_NAPI_FUNCTION(FUNCTION_REPORT_NET_CONNECTED, ReportNetConnected),
234         DECLARE_NAPI_FUNCTION(FUNCTION_REPORT_NET_DISCONNECTED, ReportNetDisconnected),
235         DECLARE_NAPI_FUNCTION(FUNCTION_GET_DEFAULT_HTTP_PROXY, GetDefaultHttpProxy),
236         DECLARE_NAPI_FUNCTION(FUNCTION_GET_GLOBAL_HTTP_PROXY, GetGlobalHttpProxy),
237         DECLARE_NAPI_FUNCTION(FUNCTION_SET_GLOBAL_HTTP_PROXY, SetGlobalHttpProxy),
238         DECLARE_NAPI_FUNCTION(FUNCTION_SET_CUSTOM_DNS_RULE, AddCustomDnsRule),
239         DECLARE_NAPI_FUNCTION(FUNCTION_DELETE_CUSTOM_DNS_RULE, RemoveCustomDnsRule),
240         DECLARE_NAPI_FUNCTION(FUNCTION_DELETE_CUSTOM_DNS_RULES, ClearCustomDnsRules),
241         DECLARE_NAPI_FUNCTION(FUNCTION_SET_APP_HTTP_PROXY, SetAppHttpProxy),
242         DECLARE_NAPI_FUNCTION(FUNCTION_GET_APP_NET, GetAppNet),
243         DECLARE_NAPI_FUNCTION(FUNCTION_GET_APP_NET_SYNC, GetAppNetSync),
244         DECLARE_NAPI_FUNCTION(FUNCTION_SET_APP_NET, SetAppNet),
245         DECLARE_NAPI_FUNCTION(FUNCTION_FACTORY_RESET_NETWORK, FactoryResetNetwork),
246         DECLARE_NAPI_FUNCTION(FUNCTION_FACTORY_RESET_NETWORK_SYNC, FactoryResetNetworkSync),
247         DECLARE_NAPI_FUNCTION(FUNCTION_SET_PAC_URL, SetPacUrl),
248         DECLARE_NAPI_FUNCTION(FUNCTION_GET_PAC_URL, GetPacUrl),
249         DECLARE_NAPI_FUNCTION(FUNCTION_SET_INTERFACE_UP, SetInterfaceUp),
250         DECLARE_NAPI_FUNCTION(FUNCTION_SET_INTERFACE_IP_ADDRESS, SetNetInterfaceIpAddress),
251         DECLARE_NAPI_FUNCTION(FUNCTION_ADD_NETWORK_ROUTE, AddNetworkRoute),
252         DECLARE_NAPI_FUNCTION(FUNCTION_CREATE_NET_INTERFACE, CreateNetInterface),
253         DECLARE_NAPI_FUNCTION(FUNCTION_GET_INTERFACE_CONFIG, GetNetInterfaceConfiguration),
254         DECLARE_NAPI_FUNCTION(FUNCTION_REGISTER_NET_SUPPLIER, RegisterNetSupplier),
255         DECLARE_NAPI_FUNCTION(FUNCTION_UNREGISTER_NET_SUPPLIER, UnregisterNetSupplier),
256         DECLARE_NAPI_FUNCTION(FUNCTION_SET_NET_EXT_ATTRIBUTE, SetNetExtAttribute),
257         DECLARE_NAPI_FUNCTION(FUNCTION_GET_NET_EXT_ATTRIBUTE, GetNetExtAttribute),
258         DECLARE_NAPI_FUNCTION(FUNCTION_SET_NET_EXT_ATTRIBUTE_SYNC, SetNetExtAttributeSync),
259         DECLARE_NAPI_FUNCTION(FUNCTION_GET_NET_EXT_ATTRIBUTE_SYNC, GetNetExtAttributeSync),
260         DEFINE_PAC_FUNCTIONS
261     };
262     return functions;
263 }
264 
InitConnectionModule(napi_env env,napi_value exports)265 napi_value ConnectionModule::InitConnectionModule(napi_env env, napi_value exports)
266 {
267     g_moduleId = NapiUtils::CreateUvHandlerQueue(env);
268     std::initializer_list<napi_property_descriptor> functions = ConnectionModule::createPropertyList();
269     NapiUtils::DefineProperties(env, exports, functions);
270     InitClasses(env, exports);
271     InitProperties(env, exports);
272     AddCleanupHook(env);
273     return exports;
274 }
275 
InitClasses(napi_env env,napi_value exports)276 void ConnectionModule::InitClasses(napi_env env, napi_value exports)
277 {
278     std::initializer_list<napi_property_descriptor> netConnectionFunctions = {
279         DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_ON, NetConnectionInterface::On),
280         DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_REGISTER, NetConnectionInterface::Register),
281         DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_UNREGISTER, NetConnectionInterface::Unregister),
282     };
283     ModuleTemplate::DefineClass(env, exports, netConnectionFunctions, INTERFACE_NET_CONNECTION);
284 
285     std::initializer_list<napi_property_descriptor> netInterfaceFunctions = {
286         DECLARE_NAPI_FUNCTION(NetInterfaceInterface::FUNCTION_ON, NetInterfaceInterface::On),
287         DECLARE_NAPI_FUNCTION(NetInterfaceInterface::FUNCTION_REGISTER, NetInterfaceInterface::Register),
288         DECLARE_NAPI_FUNCTION(NetInterfaceInterface::FUNCTION_UNREGISTER, NetInterfaceInterface::Unregister),
289     };
290     ModuleTemplate::DefineClass(env, exports, netInterfaceFunctions, INTERFACE_NET_INTERFACE);
291 }
292 
InitProperties(napi_env env,napi_value exports)293 void ConnectionModule::InitProperties(napi_env env, napi_value exports)
294 {
295     std::initializer_list<napi_property_descriptor> netCaps = {
296         DECLARE_NET_CAP(NET_CAPABILITY_MMS),
297         DECLARE_NET_CAP(NET_CAPABILITY_NOT_METERED),
298         DECLARE_NET_CAP(NET_CAPABILITY_INTERNET),
299         DECLARE_NET_CAP(NET_CAPABILITY_NOT_VPN),
300         DECLARE_NET_CAP(NET_CAPABILITY_VALIDATED),
301         DECLARE_NET_CAP(NET_CAPABILITY_PORTAL),
302         DECLARE_NET_CAP(NET_CAPABILITY_INTERNAL_DEFAULT),
303         DECLARE_NET_CAP(NET_CAPABILITY_CHECKING_CONNECTIVITY),
304     };
305     napi_value caps = NapiUtils::CreateObject(env);
306     NapiUtils::DefineProperties(env, caps, netCaps);
307     NapiUtils::SetNamedProperty(env, exports, INTERFACE_NET_CAP, caps);
308 
309     std::initializer_list<napi_property_descriptor> netBearTypes = {
310         DECLARE_NET_BEAR_TYPE(BEARER_CELLULAR),  DECLARE_NET_BEAR_TYPE(BEARER_WIFI),
311         DECLARE_NET_BEAR_TYPE(BEARER_BLUETOOTH), DECLARE_NET_BEAR_TYPE(BEARER_ETHERNET),
312         DECLARE_NET_BEAR_TYPE(BEARER_VPN),       DECLARE_NET_BEAR_TYPE(BEARER_WIFI_AWARE),
313         DECLARE_NET_BEAR_TYPE(BEARER_DEFAULT),
314     };
315     napi_value types = NapiUtils::CreateObject(env);
316     NapiUtils::DefineProperties(env, types, netBearTypes);
317     NapiUtils::SetNamedProperty(env, exports, INTERFACE_NET_BEAR_TYPE, types);
318 
319     std::initializer_list<napi_property_descriptor> proxyModeTypes = {
320         DECLARE_NAPI_STATIC_PROPERTY("PROXY_MODE_OFF",
321             NapiUtils::CreateUint32(env, static_cast<uint32_t>(ProxyModeType::PROXY_MODE_OFF))),
322         DECLARE_NAPI_STATIC_PROPERTY("PROXY_MODE_AUTO",
323             NapiUtils::CreateUint32(env, static_cast<uint32_t>(ProxyModeType::PROXY_MODE_AUTO))),
324     };
325 
326     napi_value pmtypes = NapiUtils::CreateObject(env);
327     NapiUtils::DefineProperties(env, pmtypes, proxyModeTypes);
328     NapiUtils::SetNamedProperty(env, exports, INTERFACE_PROXY_MODE_TYPE, pmtypes);
329 }
330 
GetAddressesByName(napi_env env,napi_callback_info info)331 napi_value ConnectionModule::GetAddressesByName(napi_env env, napi_callback_info info)
332 {
333     return ModuleTemplate::Interface<GetAddressByNameContext>(env, info, FUNCTION_GET_ADDRESSES_BY_NAME, nullptr,
334                                                               ConnectionAsyncWork::ExecGetAddressesByName,
335                                                               ConnectionAsyncWork::GetAddressesByNameCallback);
336 }
337 
HasDefaultNet(napi_env env,napi_callback_info info)338 napi_value ConnectionModule::HasDefaultNet(napi_env env, napi_callback_info info)
339 {
340     return ModuleTemplate::Interface<HasDefaultNetContext>(env, info, FUNCTION_HAS_DEFAULT_NET, nullptr,
341                                                            ConnectionAsyncWork::ExecHasDefaultNet,
342                                                            ConnectionAsyncWork::HasDefaultNetCallback);
343 }
344 
HasDefaultNetSync(napi_env env,napi_callback_info info)345 napi_value ConnectionModule::HasDefaultNetSync(napi_env env, napi_callback_info info)
346 {
347     return ModuleTemplate::InterfaceSync<HasDefaultNetContext>(env, info, FUNCTION_HAS_DEFAULT_NET, nullptr,
348                                                                ConnectionExec::ExecHasDefaultNet,
349                                                                ConnectionExec::HasDefaultNetCallback);
350 }
351 
IsDefaultNetMetered(napi_env env,napi_callback_info info)352 napi_value ConnectionModule::IsDefaultNetMetered(napi_env env, napi_callback_info info)
353 {
354     return ModuleTemplate::Interface<IsDefaultNetMeteredContext>(env, info, FUNCTION_IS_DEFAULT_NET_METERED, nullptr,
355                                                                  ConnectionAsyncWork::ExecIsDefaultNetMetered,
356                                                                  ConnectionAsyncWork::IsDefaultNetMeteredCallback);
357 }
358 
IsDefaultNetMeteredSync(napi_env env,napi_callback_info info)359 napi_value ConnectionModule::IsDefaultNetMeteredSync(napi_env env, napi_callback_info info)
360 {
361     return ModuleTemplate::InterfaceSync<IsDefaultNetMeteredContext>(env, info, FUNCTION_IS_DEFAULT_NET_METERED,
362         nullptr, ConnectionExec::ExecIsDefaultNetMetered, ConnectionExec::IsDefaultNetMeteredCallback);
363 }
364 
GetProxyMode(napi_env env,napi_callback_info info)365 napi_value ConnectionModule::GetProxyMode(napi_env env, napi_callback_info info)
366 {
367     return ModuleTemplate::Interface<ProxyModeContext>(env, info, FUNCTION_GET_PROXY_MODE, nullptr,
368                                                                 ConnectionAsyncWork::ExecGetProxyMode,
369                                                                 ConnectionAsyncWork::GetProxyModeCallback);
370 }
371 
SetProxyMode(napi_env env,napi_callback_info info)372 napi_value ConnectionModule::SetProxyMode(napi_env env, napi_callback_info info)
373 {
374     return ModuleTemplate::Interface<ProxyModeContext>(env, info, FUNCTION_GET_NET_CAPABILITIES, nullptr,
375                                                                 ConnectionAsyncWork::ExecSetProxyMode,
376                                                                 ConnectionAsyncWork::SetProxyModeCallback);
377 }
378 
GetNetCapabilities(napi_env env,napi_callback_info info)379 napi_value ConnectionModule::GetNetCapabilities(napi_env env, napi_callback_info info)
380 {
381     return ModuleTemplate::Interface<GetNetCapabilitiesContext>(env, info, FUNCTION_GET_NET_CAPABILITIES, nullptr,
382                                                                 ConnectionAsyncWork::ExecGetNetCapabilities,
383                                                                 ConnectionAsyncWork::GetNetCapabilitiesCallback);
384 }
385 
GetNetCapabilitiesSync(napi_env env,napi_callback_info info)386 napi_value ConnectionModule::GetNetCapabilitiesSync(napi_env env, napi_callback_info info)
387 {
388     return ModuleTemplate::InterfaceSync<GetNetCapabilitiesContext>(env, info, FUNCTION_GET_NET_CAPABILITIES, nullptr,
389                                                                     ConnectionExec::ExecGetNetCapabilities,
390                                                                     ConnectionExec::GetNetCapabilitiesCallback);
391 }
392 
GetConnectionProperties(napi_env env,napi_callback_info info)393 napi_value ConnectionModule::GetConnectionProperties(napi_env env, napi_callback_info info)
394 {
395     return ModuleTemplate::Interface<GetConnectionPropertiesContext>(
396         env, info, FUNCTION_GET_CONNECTION_PROPERTIES, nullptr, ConnectionAsyncWork::ExecGetConnectionProperties,
397         ConnectionAsyncWork::GetConnectionPropertiesCallback);
398 }
399 
GetConnectionPropertiesSync(napi_env env,napi_callback_info info)400 napi_value ConnectionModule::GetConnectionPropertiesSync(napi_env env, napi_callback_info info)
401 {
402     return ModuleTemplate::InterfaceSync<GetConnectionPropertiesContext>(
403         env, info, FUNCTION_GET_CONNECTION_PROPERTIES, nullptr, ConnectionExec::ExecGetConnectionProperties,
404         ConnectionExec::GetConnectionPropertiesCallback);
405 }
406 
CreateNetConnection(napi_env env,napi_callback_info info)407 napi_value ConnectionModule::CreateNetConnection(napi_env env, napi_callback_info info)
408 {
409     return ModuleTemplate::NewInstance(env, info, INTERFACE_NET_CONNECTION, ParseNetConnectionParams,
410         [](napi_env, void *data, void *) {
411             NETMANAGER_BASE_LOGI("finalize netConnection");
412             auto sharedManager = static_cast<std::shared_ptr<EventManager> *>(data);
413             if (sharedManager == nullptr || *sharedManager == nullptr) {
414                 return;
415             }
416             auto manager = *sharedManager;
417             auto netConnection = static_cast<NetConnection *>(manager->GetData());
418             delete sharedManager;
419             NetConnection::DeleteNetConnection(netConnection);
420         });
421 }
422 
CreateNetInterface(napi_env env,napi_callback_info info)423 napi_value ConnectionModule::CreateNetInterface(napi_env env, napi_callback_info info)
424 {
425     return ModuleTemplate::NewInstance(env, info, INTERFACE_NET_INTERFACE, ParseNetInterfaceParams,
426         [](napi_env, void *data, void *) {
427             NETMANAGER_BASE_LOGI("finalize netInterface");
428             auto sharedManager = static_cast<std::shared_ptr<EventManager> *>(data);
429             if (sharedManager == nullptr || *sharedManager == nullptr) {
430                 return;
431             }
432             auto manager = *sharedManager;
433             auto netInterface = static_cast<NetInterface *>(manager->GetData());
434             delete sharedManager;
435             NetInterface::DeleteNetInterface(netInterface);
436         });
437 }
438 
GetDefaultNet(napi_env env,napi_callback_info info)439 napi_value ConnectionModule::GetDefaultNet(napi_env env, napi_callback_info info)
440 {
441     return ModuleTemplate::Interface<GetDefaultNetContext>(env, info, FUNCTION_GET_DEFAULT_NET, nullptr,
442                                                            ConnectionAsyncWork::ExecGetDefaultNet,
443                                                            ConnectionAsyncWork::GetDefaultNetCallback);
444 }
445 
GetDefaultNetSync(napi_env env,napi_callback_info info)446 napi_value ConnectionModule::GetDefaultNetSync(napi_env env, napi_callback_info info)
447 {
448     return ModuleTemplate::InterfaceSync<GetDefaultNetContext>(env, info, FUNCTION_GET_DEFAULT_NET, nullptr,
449                                                                ConnectionExec::ExecGetDefaultNet,
450                                                                ConnectionExec::GetDefaultNetCallback);
451 }
452 
GetAllNets(napi_env env,napi_callback_info info)453 napi_value ConnectionModule::GetAllNets(napi_env env, napi_callback_info info)
454 {
455     return ModuleTemplate::Interface<GetAllNetsContext>(env, info, FUNCTION_GET_ALL_NETS, nullptr,
456                                                         ConnectionAsyncWork::ExecGetAllNets,
457                                                         ConnectionAsyncWork::GetAllNetsCallback);
458 }
459 
GetAllNetsSync(napi_env env,napi_callback_info info)460 napi_value ConnectionModule::GetAllNetsSync(napi_env env, napi_callback_info info)
461 {
462     return ModuleTemplate::InterfaceSync<GetAllNetsContext>(env, info, FUNCTION_GET_ALL_NETS, nullptr,
463                                                             ConnectionExec::ExecGetAllNets,
464                                                             ConnectionExec::GetAllNetsCallback);
465 }
466 
EnableAirplaneMode(napi_env env,napi_callback_info info)467 napi_value ConnectionModule::EnableAirplaneMode(napi_env env, napi_callback_info info)
468 {
469     return ModuleTemplate::Interface<EnableAirplaneModeContext>(env, info, FUNCTION_ENABLE_AIRPLANE_MODE, nullptr,
470                                                                 ConnectionAsyncWork::ExecEnableAirplaneMode,
471                                                                 ConnectionAsyncWork::EnableAirplaneModeCallback);
472 }
473 
DisableAirplaneMode(napi_env env,napi_callback_info info)474 napi_value ConnectionModule::DisableAirplaneMode(napi_env env, napi_callback_info info)
475 {
476     return ModuleTemplate::Interface<DisableAirplaneModeContext>(env, info, FUNCTION_DISABLE_AIRPLANE_MODE, nullptr,
477                                                                  ConnectionAsyncWork::ExecDisableAirplaneMode,
478                                                                  ConnectionAsyncWork::DisableAirplaneModeCallback);
479 }
480 
ReportNetConnected(napi_env env,napi_callback_info info)481 napi_value ConnectionModule::ReportNetConnected(napi_env env, napi_callback_info info)
482 {
483     return ModuleTemplate::Interface<ReportNetConnectedContext>(env, info, FUNCTION_REPORT_NET_CONNECTED, nullptr,
484                                                                 ConnectionAsyncWork::ExecReportNetConnected,
485                                                                 ConnectionAsyncWork::ReportNetConnectedCallback);
486 }
487 
ReportNetDisconnected(napi_env env,napi_callback_info info)488 napi_value ConnectionModule::ReportNetDisconnected(napi_env env, napi_callback_info info)
489 {
490     return ModuleTemplate::Interface<ReportNetDisconnectedContext>(env, info, FUNCTION_REPORT_NET_DISCONNECTED, nullptr,
491                                                                    ConnectionAsyncWork::ExecReportNetDisconnected,
492                                                                    ConnectionAsyncWork::ReportNetDisconnectedCallback);
493 }
494 
GetDefaultHttpProxy(napi_env env,napi_callback_info info)495 napi_value ConnectionModule::GetDefaultHttpProxy(napi_env env, napi_callback_info info)
496 {
497     return ModuleTemplate::Interface<GetHttpProxyContext>(env, info, FUNCTION_GET_DEFAULT_HTTP_PROXY, nullptr,
498                                                           ConnectionAsyncWork::ExecGetDefaultHttpProxy,
499                                                           ConnectionAsyncWork::GetDefaultHttpProxyCallback);
500 }
501 
GetGlobalHttpProxy(napi_env env,napi_callback_info info)502 napi_value ConnectionModule::GetGlobalHttpProxy(napi_env env, napi_callback_info info)
503 {
504     return ModuleTemplate::Interface<GetHttpProxyContext>(env, info, FUNCTION_GET_GLOBAL_HTTP_PROXY, nullptr,
505                                                           ConnectionAsyncWork::ExecGetGlobalHttpProxy,
506                                                           ConnectionAsyncWork::GetGlobalHttpProxyCallback);
507 }
508 
SetGlobalHttpProxy(napi_env env,napi_callback_info info)509 napi_value ConnectionModule::SetGlobalHttpProxy(napi_env env, napi_callback_info info)
510 {
511     return ModuleTemplate::Interface<SetGlobalHttpProxyContext>(env, info, FUNCTION_SET_GLOBAL_HTTP_PROXY, nullptr,
512                                                                 ConnectionAsyncWork::ExecSetGlobalHttpProxy,
513                                                                 ConnectionAsyncWork::SetGlobalHttpProxyCallback);
514 }
515 
SetAppHttpProxy(napi_env env,napi_callback_info info)516 napi_value ConnectionModule::SetAppHttpProxy(napi_env env, napi_callback_info info)
517 {
518     return ModuleTemplate::InterfaceSync<SetAppHttpProxyContext>(env, info, FUNCTION_SET_APP_HTTP_PROXY, nullptr,
519                                                                  ConnectionExec::ExecSetAppHttpProxy,
520                                                                  ConnectionExec::SetAppHttpProxyCallback);
521 }
522 
GetAppNet(napi_env env,napi_callback_info info)523 napi_value ConnectionModule::GetAppNet(napi_env env, napi_callback_info info)
524 {
525     return ModuleTemplate::Interface<GetAppNetContext>(env, info, FUNCTION_GET_APP_NET, nullptr,
526                                                        ConnectionAsyncWork::ExecGetAppNet,
527                                                        ConnectionAsyncWork::GetAppNetCallback);
528 }
529 
GetAppNetSync(napi_env env,napi_callback_info info)530 napi_value ConnectionModule::GetAppNetSync(napi_env env, napi_callback_info info)
531 {
532     return ModuleTemplate::InterfaceSync<GetAppNetContext>(env, info, FUNCTION_GET_APP_NET, nullptr,
533                                                            ConnectionExec::ExecGetAppNet,
534                                                            ConnectionExec::GetAppNetCallback);
535 }
536 
SetAppNet(napi_env env,napi_callback_info info)537 napi_value ConnectionModule::SetAppNet(napi_env env, napi_callback_info info)
538 {
539     return ModuleTemplate::Interface<SetAppNetContext>(env, info, FUNCTION_SET_APP_NET, nullptr,
540                                                        ConnectionAsyncWork::ExecSetAppNet,
541                                                        ConnectionAsyncWork::SetAppNetCallback);
542 }
543 
SetInterfaceUp(napi_env env,napi_callback_info info)544 napi_value ConnectionModule::SetInterfaceUp(napi_env env, napi_callback_info info)
545 {
546     return ModuleTemplate::Interface<SetInterfaceUpContext>(env, info, FUNCTION_SET_INTERFACE_UP, nullptr,
547                                                             ConnectionAsyncWork::ExecSetInterfaceUp,
548                                                             ConnectionAsyncWork::SetInterfaceUpCallback);
549 }
550 
SetNetInterfaceIpAddress(napi_env env,napi_callback_info info)551 napi_value ConnectionModule::SetNetInterfaceIpAddress(napi_env env, napi_callback_info info)
552 {
553     return ModuleTemplate::Interface<SetInterfaceIpAddrContext>(env, info, FUNCTION_SET_INTERFACE_IP_ADDRESS, nullptr,
554                                                                 ConnectionAsyncWork::ExecSetInterfaceIpAddr,
555                                                                 ConnectionAsyncWork::SetInterfaceIpAddrCallback);
556 }
557 
AddNetworkRoute(napi_env env,napi_callback_info info)558 napi_value ConnectionModule::AddNetworkRoute(napi_env env, napi_callback_info info)
559 {
560     return ModuleTemplate::Interface<AddNetworkRouteContext>(env, info, FUNCTION_ADD_NETWORK_ROUTE, nullptr,
561                                                              ConnectionAsyncWork::ExecAddNetworkRoute,
562                                                              ConnectionAsyncWork::AddNetworkRouteCallback);
563 }
564 
GetNetInterfaceConfiguration(napi_env env,napi_callback_info info)565 napi_value ConnectionModule::GetNetInterfaceConfiguration(napi_env env, napi_callback_info info)
566 {
567     return ModuleTemplate::Interface<GetNetInterfaceConfigurationContext>(
568         env, info, FUNCTION_GET_INTERFACE_CONFIG, nullptr,
569         ConnectionAsyncWork::ExecGetNetInterfaceConfiguration,
570         ConnectionAsyncWork::GetNetInterfaceConfigurationCallback);
571 }
572 
RegisterNetSupplier(napi_env env,napi_callback_info info)573 napi_value ConnectionModule::RegisterNetSupplier(napi_env env, napi_callback_info info)
574 {
575     return ModuleTemplate::Interface<RegisterNetSupplierContext>(
576         env, info, FUNCTION_REGISTER_NET_SUPPLIER, nullptr,
577         ConnectionAsyncWork::ExecRegisterNetSupplier,
578         ConnectionAsyncWork::RegisterNetSupplierCallback);
579 }
580 
UnregisterNetSupplier(napi_env env,napi_callback_info info)581 napi_value ConnectionModule::UnregisterNetSupplier(napi_env env, napi_callback_info info)
582 {
583     return ModuleTemplate::Interface<UnregisterNetSupplierContext>(
584         env, info, FUNCTION_UNREGISTER_NET_SUPPLIER, nullptr,
585         ConnectionAsyncWork::ExecUnregisterNetSupplier,
586         ConnectionAsyncWork::UnregisterNetSupplierCallback);
587 }
588 
AddCustomDnsRule(napi_env env,napi_callback_info info)589 napi_value ConnectionModule::AddCustomDnsRule(napi_env env, napi_callback_info info)
590 {
591     return ModuleTemplate::Interface<SetCustomDNSRuleContext>(env, info, FUNCTION_SET_CUSTOM_DNS_RULE, nullptr,
592                                                               ConnectionAsyncWork::ExecSetCustomDNSRule,
593                                                               ConnectionAsyncWork::SetCustomDNSRuleCallback);
594 }
595 
RemoveCustomDnsRule(napi_env env,napi_callback_info info)596 napi_value ConnectionModule::RemoveCustomDnsRule(napi_env env, napi_callback_info info)
597 {
598     return ModuleTemplate::Interface<DeleteCustomDNSRuleContext>(env, info, FUNCTION_DELETE_CUSTOM_DNS_RULE, nullptr,
599                                                                  ConnectionAsyncWork::ExecDeleteCustomDNSRule,
600                                                                  ConnectionAsyncWork::DeleteCustomDNSRuleCallback);
601 }
602 
ClearCustomDnsRules(napi_env env,napi_callback_info info)603 napi_value ConnectionModule::ClearCustomDnsRules(napi_env env, napi_callback_info info)
604 {
605     return ModuleTemplate::Interface<DeleteCustomDNSRulesContext>(env, info, FUNCTION_DELETE_CUSTOM_DNS_RULES, nullptr,
606                                                                  ConnectionAsyncWork::ExecDeleteCustomDNSRules,
607                                                                  ConnectionAsyncWork::DeleteCustomDNSRulesCallback);
608 }
609 
FactoryResetNetwork(napi_env env,napi_callback_info info)610 napi_value ConnectionModule::FactoryResetNetwork(napi_env env, napi_callback_info info)
611 {
612     return ModuleTemplate::Interface<FactoryResetNetworkContext>(env, info, FUNCTION_FACTORY_RESET_NETWORK, nullptr,
613                                                                  ConnectionAsyncWork::ExecFactoryResetNetwork,
614                                                                  ConnectionAsyncWork::FactoryResetNetworkCallback);
615 }
616 
FactoryResetNetworkSync(napi_env env,napi_callback_info info)617 napi_value ConnectionModule::FactoryResetNetworkSync(napi_env env, napi_callback_info info)
618 {
619     return ModuleTemplate::InterfaceSync<FactoryResetNetworkContext>(env, info, FUNCTION_FACTORY_RESET_NETWORK, nullptr,
620                                                                      ConnectionExec::ExecFactoryResetNetwork,
621                                                                      ConnectionExec::FactoryResetNetworkCallback);
622 }
623 
SetPacUrl(napi_env env,napi_callback_info info)624 napi_value ConnectionModule::SetPacUrl(napi_env env, napi_callback_info info)
625 {
626     return ModuleTemplate::InterfaceSync<SetPacUrlContext>(env, info, FUNCTION_SET_PAC_URL, nullptr,
627                                                                      ConnectionExec::ExecSetPacUrl,
628                                                                      ConnectionExec::SetPacUrlCallback);
629 }
630 
GetPacUrl(napi_env env,napi_callback_info info)631 napi_value ConnectionModule::GetPacUrl(napi_env env, napi_callback_info info)
632 {
633     return ModuleTemplate::InterfaceSync<GetPacUrlContext>(env, info, FUNCTION_GET_PAC_URL, nullptr,
634                                                                      ConnectionExec::ExecGetPacUrl,
635                                                                      ConnectionExec::GetPacUrlCallback);
636 }
637 
GetNetExtAttributeSync(napi_env env,napi_callback_info info)638 napi_value ConnectionModule::GetNetExtAttributeSync(napi_env env, napi_callback_info info)
639 {
640     NETMANAGER_BASE_LOGI("js invoke getNetExtAttributeSync");
641     return ModuleTemplate::InterfaceSync<GetNetExtAttributeContext>(env, info, FUNCTION_GET_NET_EXT_ATTRIBUTE_SYNC,
642         nullptr, ConnectionExec::ExecGetNetExtAttribute, ConnectionExec::GetNetExtAttributeCallback);
643 }
644 
SetNetExtAttributeSync(napi_env env,napi_callback_info info)645 napi_value ConnectionModule::SetNetExtAttributeSync(napi_env env, napi_callback_info info)
646 {
647     NETMANAGER_BASE_LOGI("js invoke setNetExtAttributeSync");
648     return ModuleTemplate::InterfaceSync<SetNetExtAttributeContext>(env, info, FUNCTION_SET_NET_EXT_ATTRIBUTE_SYNC,
649         nullptr, ConnectionExec::ExecSetNetExtAttribute, ConnectionExec::SetNetExtAttributeCallback);
650 }
651 
GetNetExtAttribute(napi_env env,napi_callback_info info)652 napi_value ConnectionModule::GetNetExtAttribute(napi_env env, napi_callback_info info)
653 {
654     NETMANAGER_BASE_LOGI("js invoke getNetExtAttribute");
655     return ModuleTemplate::Interface<GetNetExtAttributeContext>(env, info, FUNCTION_GET_NET_EXT_ATTRIBUTE, nullptr,
656                                                                  ConnectionAsyncWork::ExecGetNetExtAttribute,
657                                                                  ConnectionAsyncWork::GetNetExtAttributeCallback);
658 }
659 
SetNetExtAttribute(napi_env env,napi_callback_info info)660 napi_value ConnectionModule::SetNetExtAttribute(napi_env env, napi_callback_info info)
661 {
662     NETMANAGER_BASE_LOGI("js invoke setNetExtAttribute");
663     return ModuleTemplate::Interface<SetNetExtAttributeContext>(env, info, FUNCTION_SET_NET_EXT_ATTRIBUTE, nullptr,
664                                                                  ConnectionAsyncWork::ExecSetNetExtAttribute,
665                                                                  ConnectionAsyncWork::SetNetExtAttributeCallback);
666 }
667 
SetPacFileUrl(napi_env env,napi_callback_info info)668 napi_value ConnectionModule::SetPacFileUrl(napi_env env, napi_callback_info info)
669 {
670     return ModuleTemplate::InterfaceSync<SetPacFileUrlContext>(env, info, FUNCTION_SET_FILE_PAC_URL, nullptr,
671                                                          ConnectionExec::ExecSetPacFileUrl,
672                                                          ConnectionExec::SetPacFileUrlCallback);
673 }
674 
GetPacFileUrl(napi_env env,napi_callback_info info)675 napi_value ConnectionModule::GetPacFileUrl(napi_env env, napi_callback_info info)
676 {
677     return ModuleTemplate::InterfaceSync<GetPacFileUrlContext>(env, info, FUNCTION_GET_FILE_PAC_URL, nullptr,
678                                                                ConnectionExec::ExecGetPacFileUrl,
679                                                                ConnectionExec::GetPacFileUrlCallback);
680 }
681 
FindProxyForUrl(napi_env env,napi_callback_info info)682 napi_value ConnectionModule::FindProxyForUrl(napi_env env, napi_callback_info info)
683 {
684     return ModuleTemplate::InterfaceSync<FindPacFileUrlContext>(env, info, FUNCTION_GET_FILE_PAC_URL, nullptr,
685                                                                 ConnectionExec::ExecFindProxyForUrl,
686                                                                 ConnectionExec::FindProxyForUrlCallback);
687 }
688 
On(napi_env env,napi_callback_info info)689 napi_value ConnectionModule::NetConnectionInterface::On(napi_env env, napi_callback_info info)
690 {
691     std::initializer_list<std::string> events = {EVENT_NET_AVAILABLE,
692                                                  EVENT_NET_BLOCK_STATUS_CHANGE,
693                                                  EVENT_NET_CAPABILITIES_CHANGE,
694                                                  EVENT_NET_CONNECTION_PROPERTIES_CHANGE,
695                                                  EVENT_NET_LOST,
696                                                  EVENT_NET_UNAVAILABLE};
697     return ModuleTemplate::On(env, info, events, false);
698 }
699 
Register(napi_env env,napi_callback_info info)700 napi_value ConnectionModule::NetConnectionInterface::Register(napi_env env, napi_callback_info info)
701 {
702     return ModuleTemplate::Interface<RegisterContext>(
703         env, info, FUNCTION_REGISTER,
704         [](napi_env theEnv, napi_value thisVal, RegisterContext *context) -> bool {
705             if (context && context->GetManager() && !context->GetManager()->GetRef()) {
706                 context->GetManager()->SetRef(NapiUtils::CreateReference(theEnv, thisVal));
707             }
708             return true;
709         },
710         ConnectionAsyncWork::NetConnectionAsyncWork::ExecRegister,
711         ConnectionAsyncWork::NetConnectionAsyncWork::RegisterCallback);
712 }
713 
Unregister(napi_env env,napi_callback_info info)714 napi_value ConnectionModule::NetConnectionInterface::Unregister(napi_env env, napi_callback_info info)
715 {
716     return ModuleTemplate::Interface<UnregisterContext>(
717         env, info, FUNCTION_UNREGISTER,
718         [](napi_env theEnv, napi_value thisVal, UnregisterContext *context) -> bool {
719             if (context && context->GetManager()) {
720                 if (context->GetManager()->GetRef()) {
721                     NapiUtils::DeleteReference(theEnv, context->GetManager()->GetRef());
722                     context->GetManager()->SetRef(nullptr);
723                 }
724                 context->GetManager()->DeleteAllListener();
725             }
726             return true;
727         },
728         ConnectionAsyncWork::NetConnectionAsyncWork::ExecUnregister,
729         ConnectionAsyncWork::NetConnectionAsyncWork::UnregisterCallback);
730 }
731 
On(napi_env env,napi_callback_info info)732 napi_value ConnectionModule::NetInterfaceInterface::On(napi_env env, napi_callback_info info)
733 {
734     std::initializer_list<std::string> events = {EVENT_IFACE_ADDRESS_UPDATED,
735                                                  EVENT_IFACE_ADDRESS_REMOVED,
736                                                  EVENT_IFACE_ADDED,
737                                                  EVENT_IFACE_REMOVED,
738                                                  EVENT_IFACE_CHANGED,
739                                                  EVENT_IFACE_LINK_STATE_CHANGED,
740                                                  EVENT_IFACE_ROUTE_CHANGED};
741     return ModuleTemplate::On(env, info, events, false);
742 }
743 
Register(napi_env env,napi_callback_info info)744 napi_value ConnectionModule::NetInterfaceInterface::Register(napi_env env, napi_callback_info info)
745 {
746     return ModuleTemplate::Interface<IfaceRegisterContext>(
747         env, info, FUNCTION_REGISTER,
748         [](napi_env theEnv, napi_value thisVal, IfaceRegisterContext *context) -> bool {
749             if (context && context->GetManager() && !context->GetManager()->GetRef()) {
750                 context->GetManager()->SetRef(NapiUtils::CreateReference(theEnv, thisVal));
751             }
752             return true;
753         },
754         ConnectionAsyncWork::NetInterfaceAsyncWork::ExecIfaceRegister,
755         ConnectionAsyncWork::NetInterfaceAsyncWork::IfaceRegisterCallback);
756 }
757 
Unregister(napi_env env,napi_callback_info info)758 napi_value ConnectionModule::NetInterfaceInterface::Unregister(napi_env env, napi_callback_info info)
759 {
760     return ModuleTemplate::Interface<IfaceUnregisterContext>(
761         env, info, FUNCTION_UNREGISTER,
762         [](napi_env theEnv, napi_value thisVal, IfaceUnregisterContext *context) -> bool {
763             if (context && context->GetManager()) {
764                 if (context->GetManager()->GetRef()) {
765                     NapiUtils::DeleteReference(theEnv, context->GetManager()->GetRef());
766                     context->GetManager()->SetRef(nullptr);
767                 }
768                 context->GetManager()->DeleteAllListener();
769             }
770             return true;
771         },
772         ConnectionAsyncWork::NetInterfaceAsyncWork::ExecIfaceUnregister,
773         ConnectionAsyncWork::NetInterfaceAsyncWork::IfaceUnregisterCallback);
774 }
775 
776 static napi_module g_connectionModule = {
777     .nm_version = 1,
778     .nm_flags = 0,
779     .nm_filename = nullptr,
780     .nm_register_func = ConnectionModule::InitConnectionModule,
781     .nm_modname = CONNECTION_MODULE_NAME,
782     .nm_priv = nullptr,
783     .reserved = {nullptr},
784 };
785 
RegisterConnectionModule(void)786 extern "C" __attribute__((constructor)) void RegisterConnectionModule(void)
787 {
788     napi_module_register(&g_connectionModule);
789 }
790 } // namespace OHOS::NetManagerStandard
791