1 /*
2  * Copyright (c) 2022-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_send_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 TlsSocket {
28 static constexpr std::string_view PARSE_ERROR = "data is not string";
29 
TLSSendContext(napi_env env,EventManager * manager)30 TLSSendContext::TLSSendContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
31 
ParseParams(napi_value * params,size_t paramsCount)32 void TLSSendContext::ParseParams(napi_value *params, size_t paramsCount)
33 {
34     if (!CheckParamsType(params, paramsCount)) {
35         return;
36     }
37     if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) == napi_string) {
38         data_ = NapiUtils::GetStringFromValueUtf8(GetEnv(), params[ARG_INDEX_0]);
39         if (data_.empty()) {
40             NETSTACK_LOGE("string data is empty");
41             if (paramsCount == PARAM_OPTIONS_AND_CALLBACK && SetCallback(params[1]) != napi_ok) {
42                 NETSTACK_LOGE("failed to set callback");
43             }
44             return;
45         }
46     }
47 
48     if (NapiUtils::ValueIsArrayBuffer(GetEnv(), params[ARG_INDEX_0])) {
49         size_t length = 0;
50         void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), params[ARG_INDEX_0], &length);
51         if (data == nullptr || length == 0) {
52             NETSTACK_LOGE("arraybuffer data is empty");
53             if (paramsCount == PARAM_OPTIONS_AND_CALLBACK && SetCallback(params[1]) != napi_ok) {
54                 NETSTACK_LOGE("failed to set callback");
55             }
56             return;
57         }
58         data_.append(reinterpret_cast<char *>(data), length);
59     }
60 
61     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
62         SetParseOK(SetCallback(params[ARG_INDEX_1]) == napi_ok);
63         return;
64     }
65     SetParseOK(true);
66 }
67 
CheckParamsType(napi_value * params,size_t paramsCount)68 bool TLSSendContext::CheckParamsType(napi_value *params, size_t paramsCount)
69 {
70     if (paramsCount == PARAM_JUST_OPTIONS) {
71         if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) != napi_string &&
72             !NapiUtils::ValueIsArrayBuffer(GetEnv(), params[ARG_INDEX_0])) {
73             NETSTACK_LOGE("first param is not string or arraybuffer");
74             SetNeedThrowException(true);
75             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
76             return false;
77         }
78         return true;
79     }
80 
81     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
82         if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) != napi_string &&
83             !NapiUtils::ValueIsArrayBuffer(GetEnv(), params[ARG_INDEX_0])) {
84             NETSTACK_LOGE("first param is not string or arraybuffer");
85             SetNeedThrowException(true);
86             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
87             return false;
88         }
89         if (NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_1]) != napi_function) {
90             NETSTACK_LOGE("second param is not function");
91             return false;
92         }
93         return true;
94     }
95     return false;
96 }
97 } // namespace TlsSocket
98 } // namespace NetStack
99 } // namespace OHOS
100