• 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 "getglobalhttpproxy_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_IS_DEFAULT_NET_METERED, IsDefaultNetMetered),
172         DECLARE_NAPI_FUNCTION(FUNCTION_GET_NET_CAPABILITIES, GetNetCapabilities),
173         DECLARE_NAPI_FUNCTION(FUNCTION_GET_CONNECTION_PROPERTIES, GetConnectionProperties),
174         DECLARE_NAPI_FUNCTION(FUNCTION_GET_ALL_NETS, GetAllNets),
175         DECLARE_NAPI_FUNCTION(FUNCTION_ENABLE_AIRPLANE_MODE, EnableAirplaneMode),
176         DECLARE_NAPI_FUNCTION(FUNCTION_DISABLE_AIRPLANE_MODE, DisableAirplaneMode),
177         DECLARE_NAPI_FUNCTION(FUNCTION_REPORT_NET_CONNECTED, ReportNetConnected),
178         DECLARE_NAPI_FUNCTION(FUNCTION_REPORT_NET_DISCONNECTED, ReportNetDisconnected),
179         DECLARE_NAPI_FUNCTION(FUNCTION_GET_GLOBAL_HTTP_PROXY, GetGlobalHttpProxy),
180         DECLARE_NAPI_FUNCTION(FUNCTION_SET_GLOBAL_HTTP_PROXY, SetGlobalHttpProxy),
181         DECLARE_NAPI_FUNCTION(FUNCTION_GET_APP_NET, GetAppNet),
182         DECLARE_NAPI_FUNCTION(FUNCTION_SET_APP_NET, SetAppNet),
183     };
184     NapiUtils::DefineProperties(env, exports, functions);
185 
186     std::initializer_list<napi_property_descriptor> netConnectionFunctions = {
187         DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_ON, NetConnectionInterface::On),
188         DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_REGISTER, NetConnectionInterface::Register),
189         DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_UNREGISTER, NetConnectionInterface::Unregister),
190     };
191     ModuleTemplate::DefineClass(env, exports, netConnectionFunctions, INTERFACE_NET_CONNECTION);
192 
193     InitProperties(env, exports);
194     return exports;
195 }
196 
InitProperties(napi_env env,napi_value exports)197 void ConnectionModule::InitProperties(napi_env env, napi_value exports)
198 {
199     std::initializer_list<napi_property_descriptor> netCaps = {
200         DECLARE_NET_CAP(NET_CAPABILITY_MMS),
201         DECLARE_NET_CAP(NET_CAPABILITY_NOT_METERED),
202         DECLARE_NET_CAP(NET_CAPABILITY_INTERNET),
203         DECLARE_NET_CAP(NET_CAPABILITY_NOT_VPN),
204         DECLARE_NET_CAP(NET_CAPABILITY_VALIDATED),
205         DECLARE_NET_CAP(NET_CAPABILITY_CAPTIVE_PORTAL),
206         DECLARE_NET_CAP(NET_CAPABILITY_INTERNAL_DEFAULT),
207     };
208     napi_value caps = NapiUtils::CreateObject(env);
209     NapiUtils::DefineProperties(env, caps, netCaps);
210     NapiUtils::SetNamedProperty(env, exports, INTERFACE_NET_CAP, caps);
211 
212     std::initializer_list<napi_property_descriptor> netBearTypes = {
213         DECLARE_NET_BEAR_TYPE(BEARER_CELLULAR),  DECLARE_NET_BEAR_TYPE(BEARER_WIFI),
214         DECLARE_NET_BEAR_TYPE(BEARER_BLUETOOTH), DECLARE_NET_BEAR_TYPE(BEARER_ETHERNET),
215         DECLARE_NET_BEAR_TYPE(BEARER_VPN),       DECLARE_NET_BEAR_TYPE(BEARER_WIFI_AWARE),
216         DECLARE_NET_BEAR_TYPE(BEARER_DEFAULT),
217     };
218     napi_value types = NapiUtils::CreateObject(env);
219     NapiUtils::DefineProperties(env, types, netBearTypes);
220     NapiUtils::SetNamedProperty(env, exports, INTERFACE_NET_BEAR_TYPE, types);
221 }
222 
GetAddressesByName(napi_env env,napi_callback_info info)223 napi_value ConnectionModule::GetAddressesByName(napi_env env, napi_callback_info info)
224 {
225     return ModuleTemplate::Interface<GetAddressByNameContext>(env, info, FUNCTION_GET_ADDRESSES_BY_NAME, nullptr,
226                                                               ConnectionAsyncWork::ExecGetAddressesByName,
227                                                               ConnectionAsyncWork::GetAddressesByNameCallback);
228 }
229 
HasDefaultNet(napi_env env,napi_callback_info info)230 napi_value ConnectionModule::HasDefaultNet(napi_env env, napi_callback_info info)
231 {
232     return ModuleTemplate::Interface<HasDefaultNetContext>(env, info, FUNCTION_HAS_DEFAULT_NET, nullptr,
233                                                            ConnectionAsyncWork::ExecHasDefaultNet,
234                                                            ConnectionAsyncWork::HasDefaultNetCallback);
235 }
236 
IsDefaultNetMetered(napi_env env,napi_callback_info info)237 napi_value ConnectionModule::IsDefaultNetMetered(napi_env env, napi_callback_info info)
238 {
239     return ModuleTemplate::Interface<IsDefaultNetMeteredContext>(env, info, FUNCTION_IS_DEFAULT_NET_METERED, nullptr,
240                                                                  ConnectionAsyncWork::ExecIsDefaultNetMetered,
241                                                                  ConnectionAsyncWork::IsDefaultNetMeteredCallback);
242 }
243 
GetNetCapabilities(napi_env env,napi_callback_info info)244 napi_value ConnectionModule::GetNetCapabilities(napi_env env, napi_callback_info info)
245 {
246     return ModuleTemplate::Interface<GetNetCapabilitiesContext>(env, info, FUNCTION_GET_NET_CAPABILITIES, nullptr,
247                                                                 ConnectionAsyncWork::ExecGetNetCapabilities,
248                                                                 ConnectionAsyncWork::GetNetCapabilitiesCallback);
249 }
250 
GetConnectionProperties(napi_env env,napi_callback_info info)251 napi_value ConnectionModule::GetConnectionProperties(napi_env env, napi_callback_info info)
252 {
253     return ModuleTemplate::Interface<GetConnectionPropertiesContext>(
254         env, info, FUNCTION_GET_CONNECTION_PROPERTIES, nullptr, ConnectionAsyncWork::ExecGetConnectionProperties,
255         ConnectionAsyncWork::GetConnectionPropertiesCallback);
256 }
257 
CreateNetConnection(napi_env env,napi_callback_info info)258 napi_value ConnectionModule::CreateNetConnection(napi_env env, napi_callback_info info)
259 {
260     return ModuleTemplate::NewInstance(env, info, INTERFACE_NET_CONNECTION, ParseNetConnectionParams,
261                                        [](napi_env, void *data, void *) {
262                                            NETMANAGER_BASE_LOGI("finalize netConnection");
263                                            auto manager = static_cast<EventManager *>(data);
264                                            auto netConnection = static_cast<NetConnection *>(manager->GetData());
265                                            delete manager;
266                                            NetConnection::DeleteNetConnection(netConnection);
267                                        });
268 }
269 
GetDefaultNet(napi_env env,napi_callback_info info)270 napi_value ConnectionModule::GetDefaultNet(napi_env env, napi_callback_info info)
271 {
272     return ModuleTemplate::Interface<GetDefaultNetContext>(env, info, FUNCTION_GET_DEFAULT_NET, nullptr,
273                                                            ConnectionAsyncWork::ExecGetDefaultNet,
274                                                            ConnectionAsyncWork::GetDefaultNetCallback);
275 }
276 
GetDefaultNetSync(napi_env env,napi_callback_info info)277 napi_value ConnectionModule::GetDefaultNetSync(napi_env env, napi_callback_info info)
278 {
279     GetDefaultNetContext context(env, nullptr);
280     if (ConnectionExec::ExecGetDefaultNet(&context)) {
281         return ConnectionExec::GetDefaultNetCallback(&context);
282     }
283     return NapiUtils::CreateErrorMessage(env, context.GetErrorCode(), context.GetErrorMessage());
284 }
285 
GetAllNets(napi_env env,napi_callback_info info)286 napi_value ConnectionModule::GetAllNets(napi_env env, napi_callback_info info)
287 {
288     return ModuleTemplate::Interface<GetAllNetsContext>(env, info, FUNCTION_GET_ALL_NETS, nullptr,
289                                                         ConnectionAsyncWork::ExecGetAllNets,
290                                                         ConnectionAsyncWork::GetAllNetsCallback);
291 }
292 
EnableAirplaneMode(napi_env env,napi_callback_info info)293 napi_value ConnectionModule::EnableAirplaneMode(napi_env env, napi_callback_info info)
294 {
295     return ModuleTemplate::Interface<EnableAirplaneModeContext>(env, info, FUNCTION_ENABLE_AIRPLANE_MODE, nullptr,
296                                                                 ConnectionAsyncWork::ExecEnableAirplaneMode,
297                                                                 ConnectionAsyncWork::EnableAirplaneModeCallback);
298 }
299 
DisableAirplaneMode(napi_env env,napi_callback_info info)300 napi_value ConnectionModule::DisableAirplaneMode(napi_env env, napi_callback_info info)
301 {
302     return ModuleTemplate::Interface<DisableAirplaneModeContext>(env, info, FUNCTION_DISABLE_AIRPLANE_MODE, nullptr,
303                                                                  ConnectionAsyncWork::ExecDisableAirplaneMode,
304                                                                  ConnectionAsyncWork::DisableAirplaneModeCallback);
305 }
306 
ReportNetConnected(napi_env env,napi_callback_info info)307 napi_value ConnectionModule::ReportNetConnected(napi_env env, napi_callback_info info)
308 {
309     return ModuleTemplate::Interface<ReportNetConnectedContext>(env, info, FUNCTION_REPORT_NET_CONNECTED, nullptr,
310                                                                 ConnectionAsyncWork::ExecReportNetConnected,
311                                                                 ConnectionAsyncWork::ReportNetConnectedCallback);
312 }
313 
ReportNetDisconnected(napi_env env,napi_callback_info info)314 napi_value ConnectionModule::ReportNetDisconnected(napi_env env, napi_callback_info info)
315 {
316     return ModuleTemplate::Interface<ReportNetDisconnectedContext>(env, info, FUNCTION_REPORT_NET_DISCONNECTED, nullptr,
317                                                                    ConnectionAsyncWork::ExecReportNetDisconnected,
318                                                                    ConnectionAsyncWork::ReportNetDisconnectedCallback);
319 }
320 
GetGlobalHttpProxy(napi_env env,napi_callback_info info)321 napi_value ConnectionModule::GetGlobalHttpProxy(napi_env env, napi_callback_info info)
322 {
323     return ModuleTemplate::Interface<GetGlobalHttpProxyContext>(env, info, FUNCTION_GET_GLOBAL_HTTP_PROXY, nullptr,
324                                                              ConnectionAsyncWork::ExecGetGlobalHttpProxy,
325                                                              ConnectionAsyncWork::GetGlobalHttpProxyCallback);
326 }
327 
SetGlobalHttpProxy(napi_env env,napi_callback_info info)328 napi_value ConnectionModule::SetGlobalHttpProxy(napi_env env, napi_callback_info info)
329 {
330     return ModuleTemplate::Interface<SetGlobalHttpProxyContext>(env, info, FUNCTION_SET_GLOBAL_HTTP_PROXY, nullptr,
331                                                              ConnectionAsyncWork::ExecSetGlobalHttpProxy,
332                                                              ConnectionAsyncWork::SetGlobalHttpProxyCallback);
333 }
334 
GetAppNet(napi_env env,napi_callback_info info)335 napi_value ConnectionModule::GetAppNet(napi_env env, napi_callback_info info)
336 {
337     return ModuleTemplate::Interface<GetAppNetContext>(env, info, FUNCTION_GET_APP_NET, nullptr,
338                                                     ConnectionAsyncWork::ExecGetAppNet,
339                                                     ConnectionAsyncWork::GetAppNetCallback);
340 }
341 
SetAppNet(napi_env env,napi_callback_info info)342 napi_value ConnectionModule::SetAppNet(napi_env env, napi_callback_info info)
343 {
344     return ModuleTemplate::Interface<SetAppNetContext>(env, info, FUNCTION_SET_APP_NET, nullptr,
345                                                     ConnectionAsyncWork::ExecSetAppNet,
346                                                     ConnectionAsyncWork::SetAppNetCallback);
347 }
348 
GetAddressesByName(napi_env env,napi_callback_info info)349 napi_value ConnectionModule::NetHandleInterface::GetAddressesByName(napi_env env, napi_callback_info info)
350 {
351     return ModuleTemplate::Interface<GetAddressByNameContext>(
352         env, info, FUNCTION_GET_ADDRESSES_BY_NAME, nullptr,
353         ConnectionAsyncWork::NetHandleAsyncWork::ExecGetAddressesByName,
354         ConnectionAsyncWork::NetHandleAsyncWork::GetAddressesByNameCallback);
355 }
356 
GetAddressByName(napi_env env,napi_callback_info info)357 napi_value ConnectionModule::NetHandleInterface::GetAddressByName(napi_env env, napi_callback_info info)
358 {
359     return ModuleTemplate::Interface<GetAddressByNameContext>(
360         env, info, FUNCTION_GET_ADDRESSES_BY_NAME, nullptr,
361         ConnectionAsyncWork::NetHandleAsyncWork::ExecGetAddressByName,
362         ConnectionAsyncWork::NetHandleAsyncWork::GetAddressByNameCallback);
363 }
364 
BindSocket(napi_env env,napi_callback_info info)365 napi_value ConnectionModule::NetHandleInterface::BindSocket(napi_env env, napi_callback_info info)
366 {
367     return ModuleTemplate::Interface<BindSocketContext>(
368         env, info, FUNCTION_BIND_SOCKET,
369         [](napi_env theEnv, napi_value thisVal, BindSocketContext *context) -> bool {
370             context->netId_ = NapiUtils::GetInt32Property(theEnv, thisVal, PROPERTY_NET_ID);
371             return true;
372         },
373         ConnectionAsyncWork::NetHandleAsyncWork::ExecBindSocket,
374         ConnectionAsyncWork::NetHandleAsyncWork::BindSocketCallback);
375 }
376 
On(napi_env env,napi_callback_info info)377 napi_value ConnectionModule::NetConnectionInterface::On(napi_env env, napi_callback_info info)
378 {
379     std::initializer_list<std::string> events = {EVENT_NET_AVAILABLE,
380                                                  EVENT_NET_BLOCK_STATUS_CHANGE,
381                                                  EVENT_NET_CAPABILITIES_CHANGE,
382                                                  EVENT_NET_CONNECTION_PROPERTIES_CHANGE,
383                                                  EVENT_NET_LOST,
384                                                  EVENT_NET_UNAVAILABLE};
385     return ModuleTemplate::On(env, info, events, false);
386 }
387 
Register(napi_env env,napi_callback_info info)388 napi_value ConnectionModule::NetConnectionInterface::Register(napi_env env, napi_callback_info info)
389 {
390     return ModuleTemplate::Interface<RegisterContext>(env, info, FUNCTION_REGISTER, nullptr,
391                                                       ConnectionAsyncWork::NetConnectionAsyncWork::ExecRegister,
392                                                       ConnectionAsyncWork::NetConnectionAsyncWork::RegisterCallback);
393 }
394 
Unregister(napi_env env,napi_callback_info info)395 napi_value ConnectionModule::NetConnectionInterface::Unregister(napi_env env, napi_callback_info info)
396 {
397     return ModuleTemplate::Interface<UnregisterContext>(
398         env, info, FUNCTION_UNREGISTER,
399         [](napi_env theEnv, napi_value thisVal, UnregisterContext *context) -> bool {
400             if (context && context->GetManager()) {
401                 context->GetManager()->DeleteAllListener();
402             }
403             return true;
404         }, ConnectionAsyncWork::NetConnectionAsyncWork::ExecUnregister,
405         ConnectionAsyncWork::NetConnectionAsyncWork::UnregisterCallback);
406 }
407 
408 static napi_module g_connectionModule = {
409     .nm_version = 1,
410     .nm_flags = 0,
411     .nm_filename = nullptr,
412     .nm_register_func = ConnectionModule::InitConnectionModule,
413     .nm_modname = CONNECTION_MODULE_NAME,
414     .nm_priv = nullptr,
415     .reserved = {nullptr},
416 };
417 
RegisterConnectionModule(void)418 extern "C" __attribute__((constructor)) void RegisterConnectionModule(void)
419 {
420     napi_module_register(&g_connectionModule);
421 }
422 } // namespace OHOS::NetManagerStandard
423