1 /*
2 * Copyright (c) 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 "close_context.h"
17
18 #include "constant.h"
19 #include "netstack_common_utils.h"
20 #include "netstack_log.h"
21 #include "napi_utils.h"
22
23 namespace OHOS::NetStack {
CloseContext(napi_env env,EventManager * manager)24 CloseContext::CloseContext(napi_env env, EventManager *manager)
25 : BaseContext(env, manager), code(CLOSE_REASON_NORMAL_CLOSE)
26 {
27 }
28
ParseParams(napi_value * params,size_t paramsCount)29 void CloseContext::ParseParams(napi_value *params, size_t paramsCount)
30 {
31 if (!CheckParamsType(params, paramsCount)) {
32 NETSTACK_LOGE("CloseContext Parse Failed");
33 if (paramsCount == FUNCTION_PARAM_ONE) {
34 if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
35 SetCallback(params[0]);
36 }
37 return;
38 }
39
40 if (paramsCount == FUNCTION_PARAM_TWO) {
41 if (NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function) {
42 SetCallback(params[1]);
43 }
44 return;
45 }
46 return;
47 }
48
49 if (paramsCount == FUNCTION_PARAM_ZERO) {
50 NETSTACK_LOGI("CloseContext Parse OK1");
51 return SetParseOK(true);
52 }
53
54 if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
55 NETSTACK_LOGI("CloseContext Parse OK2");
56 return SetParseOK(SetCallback(params[0]) == napi_ok);
57 }
58
59 uint32_t closeCode = NapiUtils::GetUint32Property(GetEnv(), params[0], ContextKey::CODE);
60 if (closeCode >= CLOSE_REASON_NORMAL_CLOSE && closeCode <= CLOSE_REASON_RESERVED12) {
61 code = closeCode;
62 }
63 reason = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[0], ContextKey::REASON);
64
65 if (paramsCount == FUNCTION_PARAM_TWO) {
66 NETSTACK_LOGI("CloseContext Parse OK3");
67 return SetParseOK(SetCallback(params[1]) == napi_ok);
68 }
69 NETSTACK_LOGI("CloseContext Parse OK4");
70 return SetParseOK(true);
71 }
72
CheckParamsType(napi_value * params,size_t paramsCount)73 bool CloseContext::CheckParamsType(napi_value *params, size_t paramsCount)
74 {
75 if (paramsCount == FUNCTION_PARAM_ZERO) {
76 return true;
77 }
78
79 if (paramsCount == FUNCTION_PARAM_ONE) {
80 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object ||
81 NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function;
82 }
83
84 if (paramsCount == FUNCTION_PARAM_TWO) {
85 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
86 NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
87 }
88
89 return false;
90 }
91
GetErrorCode() const92 int32_t CloseContext::GetErrorCode() const
93 {
94 if (BaseContext::IsPermissionDenied()) {
95 return PERMISSION_DENIED_CODE;
96 }
97
98 auto err = BaseContext::GetErrorCode();
99 if (err == PARSE_ERROR_CODE) {
100 return PARSE_ERROR_CODE;
101 }
102 if (WEBSOCKET_ERR_MAP.find(err) != WEBSOCKET_ERR_MAP.end()) {
103 return err;
104 }
105 return WEBSOCKET_UNKNOWN_OTHER_ERROR;
106 }
107
GetErrorMessage() const108 std::string CloseContext::GetErrorMessage() const
109 {
110 if (BaseContext::IsPermissionDenied()) {
111 return PERMISSION_DENIED_MSG;
112 }
113
114 auto err = BaseContext::GetErrorCode();
115 if (err == PARSE_ERROR_CODE) {
116 return PARSE_ERROR_MSG;
117 }
118 auto it = WEBSOCKET_ERR_MAP.find(err);
119 if (it != WEBSOCKET_ERR_MAP.end()) {
120 return it->second;
121 }
122 it = WEBSOCKET_ERR_MAP.find(WEBSOCKET_UNKNOWN_OTHER_ERROR);
123 if (it != WEBSOCKET_ERR_MAP.end()) {
124 return it->second;
125 }
126 return {};
127 }
128 } // namespace OHOS::NetStack
129