1 /*
2  * Copyright (c) 2023-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_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_CALLBACK) {
51         if (NapiUtils::GetValueType(GetEnv(), params[TlsSocket::ARG_INDEX_0]) != napi_function) {
52             NETSTACK_LOGE("first param is not function");
53             SetNeedThrowException(true);
54             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
55             return false;
56         }
57         return true;
58     } else if (paramsCount == TlsSocket::PARAM_NONE) {
59         return true;
60     }
61     return false;
62 }
63 } // namespace TlsSocketServer
64 } // namespace NetStack
65 } // namespace OHOS
66