1 /*
2 * Copyright (c) 2024 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 "tls_init_context.h"
17
18 #include <string_view>
19
20 #include "context_key.h"
21 #include "event_manager.h"
22 #include "netstack_log.h"
23 #include "napi_utils.h"
24
25 namespace OHOS::NetStack::TlsSocket {
26 static constexpr std::string_view PARSE_ERROR = "Parameter error";
27 static constexpr const char *INTERFACE_TCP_SOCKET = "TCPSocket";
28
TLSInitContext(napi_env env,EventManager * manager)29 TLSInitContext::TLSInitContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
30
ParseParams(napi_value * params,size_t paramsCount)31 void TLSInitContext::ParseParams(napi_value *params, size_t paramsCount)
32 {
33 bool valid = CheckParamsType(params, paramsCount);
34 if (!valid) {
35 return;
36 }
37
38 auto napiRet = napi_unwrap(GetEnv(), params[0], reinterpret_cast<void **>(&extManager_));
39 if (napiRet != napi_ok) {
40 NETSTACK_LOGE("get event manager in napi_unwrap failed, napiRet is %{public}d", napiRet);
41 return;
42 }
43
44 SetParseOK(true);
45 }
46
CheckParamsType(napi_value * params,size_t paramsCount)47 bool TLSInitContext::CheckParamsType(napi_value *params, size_t paramsCount)
48 {
49 if (paramsCount == PARAM_JUST_OPTIONS) {
50 if (!NapiUtils::IsInstanceOf(GetEnv(), params[0], INTERFACE_TCP_SOCKET)) {
51 NETSTACK_LOGE("param is not TCPSocket");
52 SetNeedThrowException(true);
53 SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
54 return false;
55 }
56 return true;
57 }
58
59 NETSTACK_LOGE("invalid param number");
60 SetNeedThrowException(true);
61 SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
62 return false;
63 }
64 } // namespace OHOS::NetStack::TlsSocket
65