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