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