• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "constant.h"
19 #include "net_conn_callback_observer.h"
20 #include "net_conn_client.h"
21 #include "netconnection.h"
22 #include "netmanager_base_log.h"
23 #include "netmanager_base_napi_utils.h"
24 #include "securec.h"
25 
26 static constexpr const int ERROR_PARAM_NUM = 2;
27 
28 static constexpr const char *ERROR_MSG = "failed";
29 
30 static constexpr const uint32_t DEFAULT_TIMEOUT_MS = 1000;
31 
32 static constexpr const int32_t NETWORK_NO_PERMISSION = 602;
33 
34 namespace OHOS::NetManagerStandard {
ExecGetType(GetTypeContext * context)35 bool NetworkExec::ExecGetType(GetTypeContext *context)
36 {
37     NETMANAGER_BASE_LOGI("NetworkExec::ExecGetType");
38     EventManager *manager = context->GetManager();
39     auto conn = static_cast<NetConnection *>(manager->GetData());
40     if (!conn) {
41         NETMANAGER_BASE_LOGI("NetworkExec::ExecGetType no conn");
42         return true;
43     }
44     sptr<INetConnCallback> callback = conn->GetObserver();
45 
46     sptr<NetSpecifier> specifier = new NetSpecifier;
47     specifier->netCapabilities_.netCaps_.insert(NET_CAPABILITY_INTERNET);
48     DelayedSingleton<NetConnClient>::GetInstance()->UnregisterNetConnCallback(callback);
49     int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->RegisterNetConnCallback(specifier, callback,
50                                                                                           DEFAULT_TIMEOUT_MS);
51     if (ret == NET_CONN_ERR_SAME_CALLBACK) {
52         ret = 0;
53     }
54     NETMANAGER_BASE_LOGI("ExecGetType result %{public}d", ret);
55     if (ret == NET_CONN_ERR_PERMISSION_CHECK_FAILED) {
56         ret = NETWORK_NO_PERMISSION;
57     }
58     context->SetErrorCode(ret);
59     return ret == 0;
60 }
61 
GetTypeCallback(GetTypeContext * context)62 napi_value NetworkExec::GetTypeCallback(GetTypeContext *context)
63 {
64     if (!context->IsExecOK()) {
65         napi_value fail = context->GetFailCallback();
66         if (NapiUtils::GetValueType(context->GetEnv(), fail) == napi_function) {
67             napi_value argv[ERROR_PARAM_NUM] = {
68                 NapiUtils::CreateStringUtf8(context->GetEnv(), ERROR_MSG),
69                 NapiUtils::CreateInt32(context->GetEnv(), context->GetErrorCode()),
70             };
71             NapiUtils::CallFunction(context->GetEnv(), NapiUtils::GetUndefined(context->GetEnv()), fail,
72                                     ERROR_PARAM_NUM, argv);
73         }
74 
75         napi_value complete = context->GetCompleteCallback();
76         // if ok complete will be called in observer
77         if (NapiUtils::GetValueType(context->GetEnv(), complete) == napi_function) {
78             NapiUtils::CallFunction(context->GetEnv(), NapiUtils::GetUndefined(context->GetEnv()), complete, 0,
79                                     nullptr);
80         }
81 
82         auto manager = context->GetManager();
83         napi_value success = context->GetSuccessCallback();
84         if (NapiUtils::GetValueType(context->GetEnv(), success) == napi_function) {
85             manager->DeleteListener(EVENT_GET_TYPE, success);
86         }
87     }
88 
89     return NapiUtils::GetUndefined(context->GetEnv());
90 }
91 
ExecSubscribe(SubscribeContext * context)92 bool NetworkExec::ExecSubscribe(SubscribeContext *context)
93 {
94     NETMANAGER_BASE_LOGI("NetworkExec::ExecSubscribe");
95     EventManager *manager = context->GetManager();
96     auto conn = static_cast<NetConnection *>(manager->GetData());
97     if (!conn) {
98         NETMANAGER_BASE_LOGI("NetworkExec::ExecSubscribe no conn");
99         return true;
100     }
101     sptr<INetConnCallback> callback = conn->GetObserver();
102 
103     sptr<NetSpecifier> specifier = new NetSpecifier;
104     specifier->netCapabilities_.netCaps_.insert(NET_CAPABILITY_INTERNET);
105     DelayedSingleton<NetConnClient>::GetInstance()->UnregisterNetConnCallback(callback);
106     int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->RegisterNetConnCallback(specifier, callback,
107                                                                                           DEFAULT_TIMEOUT_MS);
108     if (ret == NET_CONN_ERR_SAME_CALLBACK) {
109         ret = 0;
110     }
111     NETMANAGER_BASE_LOGI("ExecSubscribe result %{public}d", ret);
112     if (ret == NET_CONN_ERR_PERMISSION_CHECK_FAILED) {
113         ret = NETWORK_NO_PERMISSION;
114     }
115     context->SetErrorCode(ret);
116     return ret == 0;
117 }
118 
SubscribeCallback(SubscribeContext * context)119 napi_value NetworkExec::SubscribeCallback(SubscribeContext *context)
120 {
121     if (!context->IsExecOK()) {
122         napi_value fail = context->GetFailCallback();
123         if (NapiUtils::GetValueType(context->GetEnv(), fail) == napi_function) {
124             napi_value argv[ERROR_PARAM_NUM] = {
125                 NapiUtils::CreateStringUtf8(context->GetEnv(), ERROR_MSG),
126                 NapiUtils::CreateInt32(context->GetEnv(), context->GetErrorCode()),
127             };
128             NapiUtils::CallFunction(context->GetEnv(), NapiUtils::GetUndefined(context->GetEnv()), fail,
129                                     ERROR_PARAM_NUM, argv);
130         }
131     }
132 
133     return NapiUtils::GetUndefined(context->GetEnv());
134 }
135 
ExecUnsubscribe(UnsubscribeContext * context)136 bool NetworkExec::ExecUnsubscribe(UnsubscribeContext *context)
137 {
138     EventManager *manager = context->GetManager();
139     auto conn = static_cast<NetConnection *>(manager->GetData());
140     sptr<INetConnCallback> callback = conn->GetObserver();
141 
142     int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->UnregisterNetConnCallback(callback);
143     if (ret == NET_CONN_ERR_CALLBACK_NOT_FOUND || ret == NET_CONN_ERR_REQ_ID_NOT_FOUND) {
144         ret = 0;
145     }
146     NETMANAGER_BASE_LOGI("ExecUnsubscribe result %{public}d", ret);
147     if (ret == NET_CONN_ERR_PERMISSION_CHECK_FAILED) {
148         ret = NETWORK_NO_PERMISSION;
149     }
150     context->SetErrorCode(ret);
151     return ret == 0;
152 }
153 
UnsubscribeCallback(UnsubscribeContext * context)154 napi_value NetworkExec::UnsubscribeCallback(UnsubscribeContext *context)
155 {
156     context->GetManager()->DeleteListener(EVENT_GET_TYPE);
157     context->GetManager()->DeleteListener(EVENT_SUBSCRIBE);
158     return NapiUtils::GetUndefined(context->GetEnv());
159 }
160 } // namespace OHOS::NetManagerStandard