1 /*
2 * Copyright (c) 2021-2022 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 "tcp_send_context.h"
17
18 #include "context_key.h"
19 #include "socket_constant.h"
20 #include "event_manager.h"
21 #include "netstack_log.h"
22 #include "napi_utils.h"
23
24 namespace OHOS::NetStack {
TcpSendContext(napi_env env,EventManager * manager)25 TcpSendContext::TcpSendContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
26
ParseParams(napi_value * params,size_t paramsCount)27 void TcpSendContext::ParseParams(napi_value *params, size_t paramsCount)
28 {
29 bool valid = CheckParamsType(params, paramsCount);
30 if (!valid) {
31 if (paramsCount == PARAM_JUST_CALLBACK) {
32 if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
33 SetCallback(params[0]);
34 }
35 return;
36 }
37 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
38 if (NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function) {
39 SetCallback(params[1]);
40 }
41 return;
42 }
43 return;
44 }
45
46 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_ENCODING)) {
47 std::string encoding = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[0], KEY_ENCODING);
48 options.SetEncoding(encoding);
49 }
50 if (!GetData(params[0])) {
51 return;
52 }
53
54 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
55 SetParseOK(SetCallback(params[1]) == napi_ok);
56 return;
57 }
58 SetParseOK(true);
59 }
60
GetSocketFd() const61 int TcpSendContext::GetSocketFd() const
62 {
63 return (int)(uint64_t)manager_->GetData();
64 }
65
CheckParamsType(napi_value * params,size_t paramsCount)66 bool TcpSendContext::CheckParamsType(napi_value *params, size_t paramsCount)
67 {
68 if (paramsCount == PARAM_JUST_OPTIONS) {
69 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
70 }
71
72 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
73 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
74 NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
75 }
76 return false;
77 }
78
GetData(napi_value udpSendOptions)79 bool TcpSendContext::GetData(napi_value udpSendOptions)
80 {
81 napi_value jsData = NapiUtils::GetNamedProperty(GetEnv(), udpSendOptions, KEY_DATA);
82 if (NapiUtils::GetValueType(GetEnv(), jsData) == napi_string) {
83 std::string data = NapiUtils::GetStringFromValueUtf8(GetEnv(), jsData);
84 if (data.empty()) {
85 NETSTACK_LOGE("string data is empty");
86 return false;
87 }
88 options.SetData(data);
89 return true;
90 }
91
92 if (NapiUtils::ValueIsArrayBuffer(GetEnv(), jsData)) {
93 size_t length = 0;
94 void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length);
95 if (data == nullptr) {
96 NETSTACK_LOGE("arraybuffer data is empty");
97 return false;
98 }
99 options.SetData(data, length);
100 return true;
101 }
102 return false;
103 }
104
GetErrorCode() const105 int32_t TcpSendContext::GetErrorCode() const
106 {
107 if (BaseContext::IsPermissionDenied()) {
108 return PERMISSION_DENIED_CODE;
109 }
110
111 auto err = BaseContext::GetErrorCode();
112 if (err == PARSE_ERROR_CODE) {
113 return PARSE_ERROR_CODE;
114 }
115 return err + SOCKET_ERROR_CODE_BASE;
116 }
117
GetErrorMessage() const118 std::string TcpSendContext::GetErrorMessage() const
119 {
120 if (BaseContext::IsPermissionDenied()) {
121 return PERMISSION_DENIED_MSG;
122 }
123
124 auto errCode = BaseContext::GetErrorCode();
125 if (errCode == PARSE_ERROR_CODE) {
126 return PARSE_ERROR_MSG;
127 }
128 char err[MAX_ERR_NUM] = {0};
129 (void)strerror_r(errCode, err, MAX_ERR_NUM);
130 return err;
131 }
132 } // namespace OHOS::NetStack
133