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 DECLARE_NAPI_FUNCTION(FUNCTION_SET_PAC_URL, SetPacUrl),
200 DECLARE_NAPI_FUNCTION(FUNCTION_GET_PAC_URL, GetPacUrl),
201 };
202 NapiUtils::DefineProperties(env, exports, functions);
203
204 std::initializer_list<napi_property_descriptor> netConnectionFunctions = {
205 DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_ON, NetConnectionInterface::On),
206 DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_REGISTER, NetConnectionInterface::Register),
207 DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_UNREGISTER, NetConnectionInterface::Unregister),
208 };
209 ModuleTemplate::DefineClass(env, exports, netConnectionFunctions, INTERFACE_NET_CONNECTION);
210
211 InitProperties(env, exports);
212 NapiUtils::SetEnvValid(env);
213 napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, env);
214 return exports;
215 }
216
InitProperties(napi_env env,napi_value exports)217 void ConnectionModule::InitProperties(napi_env env, napi_value exports)
218 {
219 std::initializer_list<napi_property_descriptor> netCaps = {
220 DECLARE_NET_CAP(NET_CAPABILITY_MMS),
221 DECLARE_NET_CAP(NET_CAPABILITY_NOT_METERED),
222 DECLARE_NET_CAP(NET_CAPABILITY_INTERNET),
223 DECLARE_NET_CAP(NET_CAPABILITY_NOT_VPN),
224 DECLARE_NET_CAP(NET_CAPABILITY_VALIDATED),
225 DECLARE_NET_CAP(NET_CAPABILITY_PORTAL),
226 DECLARE_NET_CAP(NET_CAPABILITY_INTERNAL_DEFAULT),
227 DECLARE_NET_CAP(NET_CAPABILITY_CHECKING_CONNECTIVITY),
228 };
229 napi_value caps = NapiUtils::CreateObject(env);
230 NapiUtils::DefineProperties(env, caps, netCaps);
231 NapiUtils::SetNamedProperty(env, exports, INTERFACE_NET_CAP, caps);
232
233 std::initializer_list<napi_property_descriptor> netBearTypes = {
234 DECLARE_NET_BEAR_TYPE(BEARER_CELLULAR), DECLARE_NET_BEAR_TYPE(BEARER_WIFI),
235 DECLARE_NET_BEAR_TYPE(BEARER_BLUETOOTH), DECLARE_NET_BEAR_TYPE(BEARER_ETHERNET),
236 DECLARE_NET_BEAR_TYPE(BEARER_VPN), DECLARE_NET_BEAR_TYPE(BEARER_WIFI_AWARE),
237 DECLARE_NET_BEAR_TYPE(BEARER_DEFAULT),
238 };
239 napi_value types = NapiUtils::CreateObject(env);
240 NapiUtils::DefineProperties(env, types, netBearTypes);
241 NapiUtils::SetNamedProperty(env, exports, INTERFACE_NET_BEAR_TYPE, types);
242 }
243
GetAddressesByName(napi_env env,napi_callback_info info)244 napi_value ConnectionModule::GetAddressesByName(napi_env env, napi_callback_info info)
245 {
246 return ModuleTemplate::Interface<GetAddressByNameContext>(env, info, FUNCTION_GET_ADDRESSES_BY_NAME, nullptr,
247 ConnectionAsyncWork::ExecGetAddressesByName,
248 ConnectionAsyncWork::GetAddressesByNameCallback);
249 }
250
HasDefaultNet(napi_env env,napi_callback_info info)251 napi_value ConnectionModule::HasDefaultNet(napi_env env, napi_callback_info info)
252 {
253 return ModuleTemplate::Interface<HasDefaultNetContext>(env, info, FUNCTION_HAS_DEFAULT_NET, nullptr,
254 ConnectionAsyncWork::ExecHasDefaultNet,
255 ConnectionAsyncWork::HasDefaultNetCallback);
256 }
257
HasDefaultNetSync(napi_env env,napi_callback_info info)258 napi_value ConnectionModule::HasDefaultNetSync(napi_env env, napi_callback_info info)
259 {
260 return ModuleTemplate::InterfaceSync<HasDefaultNetContext>(env, info, FUNCTION_HAS_DEFAULT_NET, nullptr,
261 ConnectionExec::ExecHasDefaultNet,
262 ConnectionExec::HasDefaultNetCallback);
263 }
264
IsDefaultNetMetered(napi_env env,napi_callback_info info)265 napi_value ConnectionModule::IsDefaultNetMetered(napi_env env, napi_callback_info info)
266 {
267 return ModuleTemplate::Interface<IsDefaultNetMeteredContext>(env, info, FUNCTION_IS_DEFAULT_NET_METERED, nullptr,
268 ConnectionAsyncWork::ExecIsDefaultNetMetered,
269 ConnectionAsyncWork::IsDefaultNetMeteredCallback);
270 }
271
IsDefaultNetMeteredSync(napi_env env,napi_callback_info info)272 napi_value ConnectionModule::IsDefaultNetMeteredSync(napi_env env, napi_callback_info info)
273 {
274 return ModuleTemplate::InterfaceSync<IsDefaultNetMeteredContext>(env, info, FUNCTION_IS_DEFAULT_NET_METERED,
275 nullptr, ConnectionExec::ExecIsDefaultNetMetered, ConnectionExec::IsDefaultNetMeteredCallback);
276 }
277
GetNetCapabilities(napi_env env,napi_callback_info info)278 napi_value ConnectionModule::GetNetCapabilities(napi_env env, napi_callback_info info)
279 {
280 return ModuleTemplate::Interface<GetNetCapabilitiesContext>(env, info, FUNCTION_GET_NET_CAPABILITIES, nullptr,
281 ConnectionAsyncWork::ExecGetNetCapabilities,
282 ConnectionAsyncWork::GetNetCapabilitiesCallback);
283 }
284
GetNetCapabilitiesSync(napi_env env,napi_callback_info info)285 napi_value ConnectionModule::GetNetCapabilitiesSync(napi_env env, napi_callback_info info)
286 {
287 return ModuleTemplate::InterfaceSync<GetNetCapabilitiesContext>(env, info, FUNCTION_GET_NET_CAPABILITIES, nullptr,
288 ConnectionExec::ExecGetNetCapabilities,
289 ConnectionExec::GetNetCapabilitiesCallback);
290 }
291
GetConnectionProperties(napi_env env,napi_callback_info info)292 napi_value ConnectionModule::GetConnectionProperties(napi_env env, napi_callback_info info)
293 {
294 return ModuleTemplate::Interface<GetConnectionPropertiesContext>(
295 env, info, FUNCTION_GET_CONNECTION_PROPERTIES, nullptr, ConnectionAsyncWork::ExecGetConnectionProperties,
296 ConnectionAsyncWork::GetConnectionPropertiesCallback);
297 }
298
GetConnectionPropertiesSync(napi_env env,napi_callback_info info)299 napi_value ConnectionModule::GetConnectionPropertiesSync(napi_env env, napi_callback_info info)
300 {
301 return ModuleTemplate::InterfaceSync<GetConnectionPropertiesContext>(
302 env, info, FUNCTION_GET_CONNECTION_PROPERTIES, nullptr, ConnectionExec::ExecGetConnectionProperties,
303 ConnectionExec::GetConnectionPropertiesCallback);
304 }
305
CreateNetConnection(napi_env env,napi_callback_info info)306 napi_value ConnectionModule::CreateNetConnection(napi_env env, napi_callback_info info)
307 {
308 return ModuleTemplate::NewInstance(env, info, INTERFACE_NET_CONNECTION, ParseNetConnectionParams,
309 [](napi_env, void *data, void *) {
310 NETMANAGER_BASE_LOGI("finalize netConnection");
311 auto manager = static_cast<EventManager *>(data);
312 auto netConnection = static_cast<NetConnection *>(manager->GetData());
313 delete manager;
314 NetConnection::DeleteNetConnection(netConnection);
315 });
316 }
317
GetDefaultNet(napi_env env,napi_callback_info info)318 napi_value ConnectionModule::GetDefaultNet(napi_env env, napi_callback_info info)
319 {
320 return ModuleTemplate::Interface<GetDefaultNetContext>(env, info, FUNCTION_GET_DEFAULT_NET, nullptr,
321 ConnectionAsyncWork::ExecGetDefaultNet,
322 ConnectionAsyncWork::GetDefaultNetCallback);
323 }
324
GetDefaultNetSync(napi_env env,napi_callback_info info)325 napi_value ConnectionModule::GetDefaultNetSync(napi_env env, napi_callback_info info)
326 {
327 return ModuleTemplate::InterfaceSync<GetDefaultNetContext>(env, info, FUNCTION_GET_DEFAULT_NET, nullptr,
328 ConnectionExec::ExecGetDefaultNet,
329 ConnectionExec::GetDefaultNetCallback);
330 }
331
GetAllNets(napi_env env,napi_callback_info info)332 napi_value ConnectionModule::GetAllNets(napi_env env, napi_callback_info info)
333 {
334 return ModuleTemplate::Interface<GetAllNetsContext>(env, info, FUNCTION_GET_ALL_NETS, nullptr,
335 ConnectionAsyncWork::ExecGetAllNets,
336 ConnectionAsyncWork::GetAllNetsCallback);
337 }
338
GetAllNetsSync(napi_env env,napi_callback_info info)339 napi_value ConnectionModule::GetAllNetsSync(napi_env env, napi_callback_info info)
340 {
341 return ModuleTemplate::InterfaceSync<GetAllNetsContext>(env, info, FUNCTION_GET_ALL_NETS, nullptr,
342 ConnectionExec::ExecGetAllNets,
343 ConnectionExec::GetAllNetsCallback);
344 }
345
EnableAirplaneMode(napi_env env,napi_callback_info info)346 napi_value ConnectionModule::EnableAirplaneMode(napi_env env, napi_callback_info info)
347 {
348 return ModuleTemplate::Interface<EnableAirplaneModeContext>(env, info, FUNCTION_ENABLE_AIRPLANE_MODE, nullptr,
349 ConnectionAsyncWork::ExecEnableAirplaneMode,
350 ConnectionAsyncWork::EnableAirplaneModeCallback);
351 }
352
DisableAirplaneMode(napi_env env,napi_callback_info info)353 napi_value ConnectionModule::DisableAirplaneMode(napi_env env, napi_callback_info info)
354 {
355 return ModuleTemplate::Interface<DisableAirplaneModeContext>(env, info, FUNCTION_DISABLE_AIRPLANE_MODE, nullptr,
356 ConnectionAsyncWork::ExecDisableAirplaneMode,
357 ConnectionAsyncWork::DisableAirplaneModeCallback);
358 }
359
ReportNetConnected(napi_env env,napi_callback_info info)360 napi_value ConnectionModule::ReportNetConnected(napi_env env, napi_callback_info info)
361 {
362 return ModuleTemplate::Interface<ReportNetConnectedContext>(env, info, FUNCTION_REPORT_NET_CONNECTED, nullptr,
363 ConnectionAsyncWork::ExecReportNetConnected,
364 ConnectionAsyncWork::ReportNetConnectedCallback);
365 }
366
ReportNetDisconnected(napi_env env,napi_callback_info info)367 napi_value ConnectionModule::ReportNetDisconnected(napi_env env, napi_callback_info info)
368 {
369 return ModuleTemplate::Interface<ReportNetDisconnectedContext>(env, info, FUNCTION_REPORT_NET_DISCONNECTED, nullptr,
370 ConnectionAsyncWork::ExecReportNetDisconnected,
371 ConnectionAsyncWork::ReportNetDisconnectedCallback);
372 }
373
GetDefaultHttpProxy(napi_env env,napi_callback_info info)374 napi_value ConnectionModule::GetDefaultHttpProxy(napi_env env, napi_callback_info info)
375 {
376 return ModuleTemplate::Interface<GetHttpProxyContext>(env, info, FUNCTION_GET_DEFAULT_HTTP_PROXY, nullptr,
377 ConnectionAsyncWork::ExecGetDefaultHttpProxy,
378 ConnectionAsyncWork::GetDefaultHttpProxyCallback);
379 }
380
GetGlobalHttpProxy(napi_env env,napi_callback_info info)381 napi_value ConnectionModule::GetGlobalHttpProxy(napi_env env, napi_callback_info info)
382 {
383 return ModuleTemplate::Interface<GetHttpProxyContext>(env, info, FUNCTION_GET_GLOBAL_HTTP_PROXY, nullptr,
384 ConnectionAsyncWork::ExecGetGlobalHttpProxy,
385 ConnectionAsyncWork::GetGlobalHttpProxyCallback);
386 }
387
SetGlobalHttpProxy(napi_env env,napi_callback_info info)388 napi_value ConnectionModule::SetGlobalHttpProxy(napi_env env, napi_callback_info info)
389 {
390 return ModuleTemplate::Interface<SetGlobalHttpProxyContext>(env, info, FUNCTION_SET_GLOBAL_HTTP_PROXY, nullptr,
391 ConnectionAsyncWork::ExecSetGlobalHttpProxy,
392 ConnectionAsyncWork::SetGlobalHttpProxyCallback);
393 }
394
SetAppHttpProxy(napi_env env,napi_callback_info info)395 napi_value ConnectionModule::SetAppHttpProxy(napi_env env, napi_callback_info info)
396 {
397 return ModuleTemplate::InterfaceSync<SetAppHttpProxyContext>(env, info, FUNCTION_SET_APP_HTTP_PROXY, nullptr,
398 ConnectionExec::ExecSetAppHttpProxy,
399 ConnectionExec::SetAppHttpProxyCallback);
400 }
401
GetAppNet(napi_env env,napi_callback_info info)402 napi_value ConnectionModule::GetAppNet(napi_env env, napi_callback_info info)
403 {
404 return ModuleTemplate::Interface<GetAppNetContext>(env, info, FUNCTION_GET_APP_NET, nullptr,
405 ConnectionAsyncWork::ExecGetAppNet,
406 ConnectionAsyncWork::GetAppNetCallback);
407 }
408
GetAppNetSync(napi_env env,napi_callback_info info)409 napi_value ConnectionModule::GetAppNetSync(napi_env env, napi_callback_info info)
410 {
411 return ModuleTemplate::InterfaceSync<GetAppNetContext>(env, info, FUNCTION_GET_APP_NET, nullptr,
412 ConnectionExec::ExecGetAppNet,
413 ConnectionExec::GetAppNetCallback);
414 }
415
SetAppNet(napi_env env,napi_callback_info info)416 napi_value ConnectionModule::SetAppNet(napi_env env, napi_callback_info info)
417 {
418 return ModuleTemplate::Interface<SetAppNetContext>(env, info, FUNCTION_SET_APP_NET, nullptr,
419 ConnectionAsyncWork::ExecSetAppNet,
420 ConnectionAsyncWork::SetAppNetCallback);
421 }
422
AddCustomDnsRule(napi_env env,napi_callback_info info)423 napi_value ConnectionModule::AddCustomDnsRule(napi_env env, napi_callback_info info)
424 {
425 return ModuleTemplate::Interface<SetCustomDNSRuleContext>(env, info, FUNCTION_SET_CUSTOM_DNS_RULE, nullptr,
426 ConnectionAsyncWork::ExecSetCustomDNSRule,
427 ConnectionAsyncWork::SetCustomDNSRuleCallback);
428 }
429
RemoveCustomDnsRule(napi_env env,napi_callback_info info)430 napi_value ConnectionModule::RemoveCustomDnsRule(napi_env env, napi_callback_info info)
431 {
432 return ModuleTemplate::Interface<DeleteCustomDNSRuleContext>(env, info, FUNCTION_DELETE_CUSTOM_DNS_RULE, nullptr,
433 ConnectionAsyncWork::ExecDeleteCustomDNSRule,
434 ConnectionAsyncWork::DeleteCustomDNSRuleCallback);
435 }
436
ClearCustomDnsRules(napi_env env,napi_callback_info info)437 napi_value ConnectionModule::ClearCustomDnsRules(napi_env env, napi_callback_info info)
438 {
439 return ModuleTemplate::Interface<DeleteCustomDNSRulesContext>(env, info, FUNCTION_DELETE_CUSTOM_DNS_RULES, nullptr,
440 ConnectionAsyncWork::ExecDeleteCustomDNSRules,
441 ConnectionAsyncWork::DeleteCustomDNSRulesCallback);
442 }
443
FactoryResetNetwork(napi_env env,napi_callback_info info)444 napi_value ConnectionModule::FactoryResetNetwork(napi_env env, napi_callback_info info)
445 {
446 return ModuleTemplate::Interface<FactoryResetNetworkContext>(env, info, FUNCTION_FACTORY_RESET_NETWORK, nullptr,
447 ConnectionAsyncWork::ExecFactoryResetNetwork,
448 ConnectionAsyncWork::FactoryResetNetworkCallback);
449 }
450
FactoryResetNetworkSync(napi_env env,napi_callback_info info)451 napi_value ConnectionModule::FactoryResetNetworkSync(napi_env env, napi_callback_info info)
452 {
453 return ModuleTemplate::InterfaceSync<FactoryResetNetworkContext>(env, info, FUNCTION_FACTORY_RESET_NETWORK, nullptr,
454 ConnectionExec::ExecFactoryResetNetwork,
455 ConnectionExec::FactoryResetNetworkCallback);
456 }
457
SetPacUrl(napi_env env,napi_callback_info info)458 napi_value ConnectionModule::SetPacUrl(napi_env env, napi_callback_info info)
459 {
460 return ModuleTemplate::InterfaceSync<SetPacUrlContext>(env, info, FUNCTION_SET_PAC_URL, nullptr,
461 ConnectionExec::ExecSetPacUrl,
462 ConnectionExec::SetPacUrlCallback);
463 }
464
GetPacUrl(napi_env env,napi_callback_info info)465 napi_value ConnectionModule::GetPacUrl(napi_env env, napi_callback_info info)
466 {
467 return ModuleTemplate::InterfaceSync<GetPacUrlContext>(env, info, FUNCTION_GET_PAC_URL, nullptr,
468 ConnectionExec::ExecGetPacUrl,
469 ConnectionExec::GetPacUrlCallback);
470 }
471
On(napi_env env,napi_callback_info info)472 napi_value ConnectionModule::NetConnectionInterface::On(napi_env env, napi_callback_info info)
473 {
474 std::initializer_list<std::string> events = {EVENT_NET_AVAILABLE,
475 EVENT_NET_BLOCK_STATUS_CHANGE,
476 EVENT_NET_CAPABILITIES_CHANGE,
477 EVENT_NET_CONNECTION_PROPERTIES_CHANGE,
478 EVENT_NET_LOST,
479 EVENT_NET_UNAVAILABLE};
480 return ModuleTemplate::On(env, info, events, false);
481 }
482
Register(napi_env env,napi_callback_info info)483 napi_value ConnectionModule::NetConnectionInterface::Register(napi_env env, napi_callback_info info)
484 {
485 return ModuleTemplate::Interface<RegisterContext>(
486 env, info, FUNCTION_REGISTER,
487 [](napi_env theEnv, napi_value thisVal, RegisterContext *context) -> bool {
488 if (context && context->GetManager() && !context->GetManager()->GetRef()) {
489 context->GetManager()->SetRef(NapiUtils::CreateReference(theEnv, thisVal));
490 }
491 return true;
492 },
493 ConnectionAsyncWork::NetConnectionAsyncWork::ExecRegister,
494 ConnectionAsyncWork::NetConnectionAsyncWork::RegisterCallback);
495 }
496
Unregister(napi_env env,napi_callback_info info)497 napi_value ConnectionModule::NetConnectionInterface::Unregister(napi_env env, napi_callback_info info)
498 {
499 return ModuleTemplate::Interface<UnregisterContext>(
500 env, info, FUNCTION_UNREGISTER,
501 [](napi_env theEnv, napi_value thisVal, UnregisterContext *context) -> bool {
502 if (context && context->GetManager()) {
503 if (context->GetManager()->GetRef()) {
504 NapiUtils::DeleteReference(theEnv, context->GetManager()->GetRef());
505 context->GetManager()->SetRef(nullptr);
506 }
507 context->GetManager()->DeleteAllListener();
508 }
509 return true;
510 },
511 ConnectionAsyncWork::NetConnectionAsyncWork::ExecUnregister,
512 ConnectionAsyncWork::NetConnectionAsyncWork::UnregisterCallback);
513 }
514
515 static napi_module g_connectionModule = {
516 .nm_version = 1,
517 .nm_flags = 0,
518 .nm_filename = nullptr,
519 .nm_register_func = ConnectionModule::InitConnectionModule,
520 .nm_modname = CONNECTION_MODULE_NAME,
521 .nm_priv = nullptr,
522 .reserved = {nullptr},
523 };
524
RegisterConnectionModule(void)525 extern "C" __attribute__((constructor)) void RegisterConnectionModule(void)
526 {
527 napi_module_register(&g_connectionModule);
528 }
529 } // namespace OHOS::NetManagerStandard
530