1 /*
2 * Copyright (c) 2022 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 "network_module.h"
17 #include "constant.h"
18 #include "gettype_context.h"
19 #include "netconnection.h"
20 #include "netmanager_base_log.h"
21 #include "netmanager_base_module_template.h"
22 #include "network_async_work.h"
23 #include "subscribe_context.h"
24 #include "unsubscribe_context.h"
25
26 namespace OHOS::NetManagerStandard {
27 EventManager GLOBAL_MANAGER_WRAPPER;
28 EventManager *GLOBAL_MANAGER = &GLOBAL_MANAGER_WRAPPER;
29
InitNetworkModule(napi_env env,napi_value exports)30 napi_value NetworkModule::InitNetworkModule(napi_env env, napi_value exports)
31 {
32 std::initializer_list<napi_property_descriptor> properties = {
33 DECLARE_NAPI_FUNCTION(FUNCTION_GET_TYPE, GetType),
34 DECLARE_NAPI_FUNCTION(FUNCTION_SUBSCRIBE, Subscribe),
35 DECLARE_NAPI_FUNCTION(FUNCTION_UNSUBSCRIBE, Unsubscribe),
36 };
37 NapiUtils::DefineProperties(env, exports, properties);
38
39 auto finalizer = [](napi_env, void *data, void *) {
40 NETMANAGER_BASE_LOGI("finalize netConnection");
41 if (GLOBAL_MANAGER_WRAPPER.GetData()) {
42 auto netConnection = reinterpret_cast<NetConnection *>(GLOBAL_MANAGER_WRAPPER.GetData());
43 delete netConnection;
44 }
45 GLOBAL_MANAGER_WRAPPER.SetData(nullptr);
46 };
47 GLOBAL_MANAGER_WRAPPER.SetData(new NetConnection);
48 napi_wrap(env, exports, reinterpret_cast<void *>(GLOBAL_MANAGER), finalizer, nullptr, nullptr);
49
50 return exports;
51 }
52
GetType(napi_env env,napi_callback_info info)53 napi_value NetworkModule::GetType(napi_env env, napi_callback_info info)
54 {
55 NETMANAGER_BASE_LOGI("NetworkModule::GetType is called");
56 return ModuleTemplate::Interface<GetTypeContext>(env, info, "SystemNetworkGetType", nullptr,
57 NetworkAsyncWork::ExecGetType, NetworkAsyncWork::GetTypeCallback);
58 }
59
Subscribe(napi_env env,napi_callback_info info)60 napi_value NetworkModule::Subscribe(napi_env env, napi_callback_info info)
61 {
62 NETMANAGER_BASE_LOGI("NetworkModule::Subscribe is called");
63 return ModuleTemplate::Interface<SubscribeContext>(env, info, "SystemNetworkSubscribe", nullptr,
64 NetworkAsyncWork::ExecSubscribe,
65 NetworkAsyncWork::SubscribeCallback);
66 }
67
Unsubscribe(napi_env env,napi_callback_info info)68 napi_value NetworkModule::Unsubscribe(napi_env env, napi_callback_info info)
69 {
70 NETMANAGER_BASE_LOGI("NetworkModule::Unsubscribe is called");
71 return ModuleTemplate::Interface<UnsubscribeContext>(env, info, "SystemNetworkUnsubscribe", nullptr,
72 NetworkAsyncWork::ExecUnsubscribe,
73 NetworkAsyncWork::UnsubscribeCallback);
74 }
75
76 NAPI_MODULE(network, NetworkModule::InitNetworkModule)
77 } // namespace OHOS::NetManagerStandard