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     EventManager *manager = context->GetManager();
118 
119     sptr<INetConnCallback> callback = g_observerMap[manager];
120     if (callback == nullptr) {
121         return false;
122     }
123     sptr<NetSpecifier> specifier = new NetSpecifier;
124     specifier->netCapabilities_.netCaps_.insert(NET_CAPABILITY_INTERNET);
125     NetConnClient::GetInstance().UnregisterNetConnCallback(callback);
126     int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, callback, DEFAULT_TIMEOUT_MS);
127 
128     context->SetErrorCode(ret);
129     return ret == NETMANAGER_SUCCESS;
130 }
131 
SubscribeCallback(SubscribeContext * context)132 napi_value NetworkExec::SubscribeCallback(SubscribeContext *context)
133 {
134     if (!context->IsExecOK()) {
135         napi_value fail = context->GetFailCallback();
136         if (NapiUtils::GetValueType(context->GetEnv(), fail) == napi_function) {
137             napi_value argv[ERROR_PARAM_NUM] = {
138                 NapiUtils::CreateStringUtf8(context->GetEnv(), ERROR_MSG),
139                 NapiUtils::CreateInt32(context->GetEnv(), context->GetErrorCode()),
140             };
141             NapiUtils::CallFunction(context->GetEnv(), NapiUtils::GetUndefined(context->GetEnv()), fail,
142                                     ERROR_PARAM_NUM, argv);
143         }
144     }
145 
146     return NapiUtils::GetUndefined(context->GetEnv());
147 }
148 
ExecUnsubscribe(UnsubscribeContext * context)149 bool NetworkExec::ExecUnsubscribe(UnsubscribeContext *context)
150 {
151     NETMANAGER_BASE_LOGI("ExecUnsubscribe");
152     EventManager *manager = context->GetManager();
153     sptr<INetConnCallback> callback = g_observerMap[manager];
154     if (callback == nullptr) {
155         NETMANAGER_BASE_LOGE("callback is null");
156         return false;
157     }
158 
159     int32_t ret = NetConnClient::GetInstance().UnregisterNetConnCallback(callback);
160     context->SetErrorCode(ret);
161     return ret == NETMANAGER_SUCCESS;
162 }
163 
UnsubscribeCallback(UnsubscribeContext * context)164 napi_value NetworkExec::UnsubscribeCallback(UnsubscribeContext *context)
165 {
166     context->GetManager()->DeleteListener(EVENT_SUBSCRIBE);
167     return NapiUtils::GetUndefined(context->GetEnv());
168 }
169 } // namespace OHOS::NetManagerStandard