• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "tls_server_send_context.h"
17 
18 #include <cstdint>
19 #include <string>
20 #include <string_view>
21 #include <vector>
22 
23 #include "constant.h"
24 #include "napi_utils.h"
25 #include "netstack_log.h"
26 
27 namespace OHOS {
28 namespace NetStack {
29 namespace TlsSocketServer {
30 namespace {
31 constexpr std::string_view PARSE_ERROR = "options is not type of TLSServerSendOptions";
32 } // namespace
33 
TLSServerSendContext(napi_env env,EventManager * manager)34 TLSServerSendContext::TLSServerSendContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
35 
ParseParams(napi_value * params,size_t paramsCount)36 void TLSServerSendContext::ParseParams(napi_value *params, size_t paramsCount)
37 {
38     if (!CheckParamsType(params, paramsCount)) {
39         return;
40     }
41 
42     m_sendData = NapiUtils::GetStringFromValueUtf8(GetEnv(), params[0]);
43 
44     if (paramsCount == TlsSocket::PARAM_OPTIONS_AND_CALLBACK) {
45         SetParseOK(SetCallback(params[TlsSocket::ARG_INDEX_1]) == napi_ok);
46         return;
47     }
48     SetParseOK(true);
49 }
50 
CheckParamsType(napi_value * params,size_t paramsCount)51 bool TLSServerSendContext::CheckParamsType(napi_value *params, size_t paramsCount)
52 {
53     if (paramsCount == TlsSocket::PARAM_JUST_OPTIONS) {
54         if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_0]) != napi_string) {
55             NETSTACK_LOGE("first param is not string");
56             SetNeedThrowException(true);
57             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
58             return false;
59         }
60         return true;
61     }
62 
63     if (paramsCount == TlsSocket::PARAM_OPTIONS_AND_CALLBACK) {
64         if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_0]) != napi_string) {
65             NETSTACK_LOGE("first param is not string");
66             SetNeedThrowException(true);
67             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
68             return false;
69         }
70         if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_1]) != napi_function) {
71             NETSTACK_LOGE("second param is not function");
72             return false;
73         }
74         return true;
75     }
76     return false;
77 }
78 } // namespace TlsSocketServer
79 } // namespace NetStack
80 } // namespace OHOS
81