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     NapiUtils::SetEnvValid(env);
46     napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, env);
47     return exports;
48 }
49 
GetType(napi_env env,napi_callback_info info)50 napi_value NetworkModule::GetType(napi_env env, napi_callback_info info)
51 {
52     NETMANAGER_BASE_LOGD("GetType is called");
53     return ModuleTemplate::InterfaceWithoutManager<GetTypeContext>(
54         env, info, "SystemNetworkGetType", nullptr, NetworkAsyncWork::ExecGetType, NetworkAsyncWork::GetTypeCallback);
55 }
56 
Subscribe(napi_env env,napi_callback_info info)57 napi_value NetworkModule::Subscribe(napi_env env, napi_callback_info info)
58 {
59     NETMANAGER_BASE_LOGI("Subscribe is called");
60     return ModuleTemplate::Interface<SubscribeContext>(env, info, "SystemNetworkSubscribe", nullptr,
61                                                        NetworkAsyncWork::ExecSubscribe,
62                                                        NetworkAsyncWork::SubscribeCallback);
63 }
64 
Unsubscribe(napi_env env,napi_callback_info info)65 napi_value NetworkModule::Unsubscribe(napi_env env, napi_callback_info info)
66 {
67     NETMANAGER_BASE_LOGI("Unsubscribe is called");
68     return ModuleTemplate::InterfaceSync<UnsubscribeContext>(
69         env, info, "SystemNetworkUnsubscribe", nullptr, NetworkExec::ExecUnsubscribe, NetworkExec::UnsubscribeCallback);
70 }
71 
72 NAPI_MODULE(network, NetworkModule::InitNetworkModule)
73 } // namespace OHOS::NetManagerStandard