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 "network_module.h"
17 #include "gettype_context.h"
18 #include "module_template.h"
19 #include "netmanager_base_log.h"
20 #include "network_async_work.h"
21 #include "network_exec.h"
22 #include "network_observer.h"
23 #include "subscribe_context.h"
24 #include "unsubscribe_context.h"
25
26 namespace OHOS::NetManagerStandard {
InitNetworkModule(napi_env env,napi_value exports)27 napi_value NetworkModule::InitNetworkModule(napi_env env, napi_value exports)
28 {
29 std::initializer_list<napi_property_descriptor> properties = {
30 DECLARE_NAPI_FUNCTION(FUNCTION_GET_TYPE, GetType),
31 DECLARE_NAPI_FUNCTION(FUNCTION_SUBSCRIBE, Subscribe),
32 DECLARE_NAPI_FUNCTION(FUNCTION_UNSUBSCRIBE, Unsubscribe),
33 };
34 NapiUtils::DefineProperties(env, exports, properties);
35 auto manager = new EventManager;
36 auto observer = new NetworkObserver;
37 observer->SetManager(manager);
38 g_observerMap[manager] = observer;
39
40 auto finalizer = [](napi_env, void *data, void *) {
41 auto manager = reinterpret_cast<EventManager *>(data);
42 manager->SetInvalid();
43 };
44 napi_wrap(env, exports, reinterpret_cast<void *>(manager), finalizer, nullptr, nullptr);
45 return exports;
46 }
47
GetType(napi_env env,napi_callback_info info)48 napi_value NetworkModule::GetType(napi_env env, napi_callback_info info)
49 {
50 NETMANAGER_BASE_LOGI("NetworkModule::GetType is called");
51 return ModuleTemplate::InterfaceWithoutManager<GetTypeContext>(
52 env, info, "SystemNetworkGetType", nullptr, NetworkAsyncWork::ExecGetType, NetworkAsyncWork::GetTypeCallback);
53 }
54
Subscribe(napi_env env,napi_callback_info info)55 napi_value NetworkModule::Subscribe(napi_env env, napi_callback_info info)
56 {
57 NETMANAGER_BASE_LOGI("NetworkModule::Subscribe is called");
58 return ModuleTemplate::Interface<SubscribeContext>(env, info, "SystemNetworkSubscribe", nullptr,
59 NetworkAsyncWork::ExecSubscribe,
60 NetworkAsyncWork::SubscribeCallback);
61 }
62
Unsubscribe(napi_env env,napi_callback_info info)63 napi_value NetworkModule::Unsubscribe(napi_env env, napi_callback_info info)
64 {
65 NETMANAGER_BASE_LOGI("NetworkModule::Unsubscribe is called");
66 return ModuleTemplate::InterfaceSync<UnsubscribeContext>(
67 env, info, "SystemNetworkUnsubscribe", nullptr, NetworkExec::ExecUnsubscribe, NetworkExec::UnsubscribeCallback);
68 }
69
70 NAPI_MODULE(network, NetworkModule::InitNetworkModule)
71 } // namespace OHOS::NetManagerStandard