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 = std::make_shared<EventManager>();
36 auto observer = new NetworkObserver;
37 observer->SetManager(manager);
38 {
39 std::lock_guard<std::shared_mutex> lock(g_observerMapMtx);
40 g_observerMap[manager] = observer;
41 }
42
43 auto finalizer = [](napi_env, void *data, void *) {
44 auto sharedManager = reinterpret_cast<std::shared_ptr<EventManager> *>(data);
45 if (sharedManager != nullptr) {
46 delete sharedManager;
47 }
48 };
49 auto sharedManager = new (std::nothrow) std::shared_ptr<EventManager>();
50 if (sharedManager == nullptr) {
51 return exports;
52 }
53 *sharedManager = manager;
54 napi_wrap(env, exports, reinterpret_cast<void *>(sharedManager), finalizer, nullptr, nullptr);
55 NapiUtils::SetEnvValid(env);
56 auto envWrapper = new (std::nothrow) napi_env;
57 if (envWrapper == nullptr) {
58 NETMANAGER_BASE_LOGE("EnvWrapper create fail!");
59 return exports;
60 }
61 *envWrapper = env;
62 napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, envWrapper);
63 return exports;
64 }
65
GetType(napi_env env,napi_callback_info info)66 napi_value NetworkModule::GetType(napi_env env, napi_callback_info info)
67 {
68 NETMANAGER_BASE_LOGD("GetType is called");
69 return ModuleTemplate::InterfaceWithoutManager<GetTypeContext>(
70 env, info, "SystemNetworkGetType", nullptr, NetworkAsyncWork::ExecGetType, NetworkAsyncWork::GetTypeCallback);
71 }
72
Subscribe(napi_env env,napi_callback_info info)73 napi_value NetworkModule::Subscribe(napi_env env, napi_callback_info info)
74 {
75 NETMANAGER_BASE_LOGI("Subscribe is called");
76 return ModuleTemplate::Interface<SubscribeContext>(env, info, "SystemNetworkSubscribe", nullptr,
77 NetworkAsyncWork::ExecSubscribe,
78 NetworkAsyncWork::SubscribeCallback);
79 }
80
Unsubscribe(napi_env env,napi_callback_info info)81 napi_value NetworkModule::Unsubscribe(napi_env env, napi_callback_info info)
82 {
83 NETMANAGER_BASE_LOGI("Unsubscribe is called");
84 return ModuleTemplate::InterfaceSync<UnsubscribeContext>(
85 env, info, "SystemNetworkUnsubscribe", nullptr, NetworkExec::ExecUnsubscribe, NetworkExec::UnsubscribeCallback);
86 }
87
88 NAPI_MODULE(network, NetworkModule::InitNetworkModule)
89 } // namespace OHOS::NetManagerStandard