• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_exec.h"
17 
18 #include "net_conn_client.h"
19 #include "net_manager_constants.h"
20 #include "network_constant.h"
21 
22 #include "napi_utils.h"
23 #include "netmanager_base_log.h"
24 #include "network_observer.h"
25 #include "securec.h"
26 
27 namespace OHOS::NetManagerStandard {
28 static constexpr const int ERROR_PARAM_NUM = 2;
29 static constexpr const char *ERROR_MSG = "failed";
30 static constexpr const char *NETWORK_NONE = "none";
31 static constexpr const char *NETWORK_WIFI = "WiFi";
32 static constexpr const uint32_t DEFAULT_TIMEOUT_MS = 1000;
33 
MakeNetworkResponse(napi_env env,const std::set<NetBearType> & bearerTypes)34 static napi_value MakeNetworkResponse(napi_env env, const std::set<NetBearType> &bearerTypes)
35 {
36     napi_value obj = NapiUtils::CreateObject(env);
37     if (bearerTypes.find(BEARER_WIFI) != bearerTypes.end()) {
38         NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, NETWORK_WIFI);
39         NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, false);
40         return obj;
41     }
42 
43     if (bearerTypes.find(BEARER_CELLULAR) != bearerTypes.end()) {
44         std::string type = "";
45         int32_t ret = NetConnClient::GetInstance().GetSlotType(type);
46         if (ret != NETMANAGER_SUCCESS || type.empty()) {
47             type = "none";
48         }
49         NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, type);
50         NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, true);
51         return obj;
52     }
53 
54     NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, NETWORK_NONE);
55     NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, false);
56     return obj;
57 }
58 
ExecGetType(GetTypeContext * context)59 bool NetworkExec::ExecGetType(GetTypeContext *context)
60 {
61     NETMANAGER_BASE_LOGD("ExecGetType");
62     NetHandle handle;
63     auto ret = NetConnClient::GetInstance().GetDefaultNet(handle);
64     if (ret != NETMANAGER_SUCCESS) {
65         context->SetErrorCode(ret);
66         return ret == NETMANAGER_SUCCESS;
67     }
68 
69     if (handle.GetNetId() == 0) {
70         return true;
71     }
72 
73     NetAllCapabilities cap;
74     ret = NetConnClient::GetInstance().GetNetCapabilities(handle, cap);
75     if (ret == NETMANAGER_SUCCESS) {
76         context->SetCap(cap);
77     }
78 
79     context->SetErrorCode(ret);
80     return ret == NETMANAGER_SUCCESS;
81 }
82 
GetTypeCallback(GetTypeContext * context)83 napi_value NetworkExec::GetTypeCallback(GetTypeContext *context)
84 {
85     if (!context->IsExecOK()) {
86         napi_value fail = context->GetFailCallback();
87         if (NapiUtils::GetValueType(context->GetEnv(), fail) == napi_function) {
88             napi_value argv[ERROR_PARAM_NUM] = {
89                 NapiUtils::CreateStringUtf8(context->GetEnv(), ERROR_MSG),
90                 NapiUtils::CreateInt32(context->GetEnv(), context->GetErrorCode()),
91             };
92             NapiUtils::CallFunction(context->GetEnv(), NapiUtils::GetUndefined(context->GetEnv()), fail,
93                                     ERROR_PARAM_NUM, argv);
94         }
95 
96         napi_value complete = context->GetCompleteCallback();
97         // if ok complete will be called in observer
98         if (NapiUtils::GetValueType(context->GetEnv(), complete) == napi_function) {
99             NapiUtils::CallFunction(context->GetEnv(), NapiUtils::GetUndefined(context->GetEnv()), complete, 0,
100                                     nullptr);
101         }
102     } else {
103         napi_value success = context->GetSuccessCallback();
104         if (NapiUtils::GetValueType(context->GetEnv(), success) == napi_function) {
105             auto cap = context->GetCap();
106             auto obj = MakeNetworkResponse(context->GetEnv(), cap.bearerTypes_);
107             NapiUtils::CallFunction(context->GetEnv(), NapiUtils::GetUndefined(context->GetEnv()), success, 1, &obj);
108         }
109     }
110 
111     return NapiUtils::GetUndefined(context->GetEnv());
112 }
113 
ExecSubscribe(SubscribeContext * context)114 bool NetworkExec::ExecSubscribe(SubscribeContext *context)
115 {
116     NETMANAGER_BASE_LOGI("ExecSubscribe");
117     auto manager = context->GetManager();
118 
119     std::shared_lock<std::shared_mutex> lock(g_observerMapMtx);
120     auto iter = g_observerMap.find(manager);
121     if (iter == g_observerMap.end() || iter->second == nullptr) {
122         NETMANAGER_BASE_LOGE("callback is null");
123         return false;
124     }
125     auto callback = iter->second;
126     lock.unlock();
127     sptr<NetSpecifier> specifier = new NetSpecifier;
128     specifier->netCapabilities_.netCaps_.insert(NET_CAPABILITY_INTERNET);
129     NetConnClient::GetInstance().UnregisterNetConnCallback(callback);
130     int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, callback, DEFAULT_TIMEOUT_MS);
131 
132     context->SetErrorCode(ret);
133     return ret == NETMANAGER_SUCCESS;
134 }
135 
SubscribeCallback(SubscribeContext * context)136 napi_value NetworkExec::SubscribeCallback(SubscribeContext *context)
137 {
138     if (!context->IsExecOK()) {
139         napi_value fail = context->GetFailCallback();
140         if (NapiUtils::GetValueType(context->GetEnv(), fail) == napi_function) {
141             napi_value argv[ERROR_PARAM_NUM] = {
142                 NapiUtils::CreateStringUtf8(context->GetEnv(), ERROR_MSG),
143                 NapiUtils::CreateInt32(context->GetEnv(), context->GetErrorCode()),
144             };
145             NapiUtils::CallFunction(context->GetEnv(), NapiUtils::GetUndefined(context->GetEnv()), fail,
146                                     ERROR_PARAM_NUM, argv);
147         }
148     }
149 
150     return NapiUtils::GetUndefined(context->GetEnv());
151 }
152 
ExecUnsubscribe(UnsubscribeContext * context)153 bool NetworkExec::ExecUnsubscribe(UnsubscribeContext *context)
154 {
155     NETMANAGER_BASE_LOGI("ExecUnsubscribe");
156     auto manager = context->GetManager();
157     std::shared_lock<std::shared_mutex> lock(g_observerMapMtx);
158     auto iter = g_observerMap.find(manager);
159     if (iter == g_observerMap.end() || iter->second == nullptr) {
160         NETMANAGER_BASE_LOGE("callback is null");
161         return false;
162     }
163     auto callback = iter->second;
164     lock.unlock();
165     int32_t ret = NetConnClient::GetInstance().UnregisterNetConnCallback(callback);
166     context->SetErrorCode(ret);
167     return ret == NETMANAGER_SUCCESS;
168 }
169 
UnsubscribeCallback(UnsubscribeContext * context)170 napi_value NetworkExec::UnsubscribeCallback(UnsubscribeContext *context)
171 {
172     context->GetManager()->DeleteListener(EVENT_SUBSCRIBE);
173     return NapiUtils::GetUndefined(context->GetEnv());
174 }
175 } // namespace OHOS::NetManagerStandard