• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::Websocket {
CloseContext(napi_env env,const std::shared_ptr<EventManager> & manager)24 CloseContext::CloseContext(napi_env env, const std::shared_ptr<EventManager> &manager)
25     : BaseContext(env, manager), code(CLOSE_REASON_NORMAL_CLOSE), reason("CLOSE_NORMAL")
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     std::string tempReason = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[0], ContextKey::REASON);
64     if (!tempReason.empty()) {
65         reason = tempReason;
66     }
67 
68     if (paramsCount == FUNCTION_PARAM_TWO) {
69         NETSTACK_LOGI("CloseContext Parse OK3");
70         return SetParseOK(SetCallback(params[1]) == napi_ok);
71     }
72     NETSTACK_LOGI("CloseContext Parse OK4");
73     return SetParseOK(true);
74 }
75 
CheckParamsType(napi_value * params,size_t paramsCount)76 bool CloseContext::CheckParamsType(napi_value *params, size_t paramsCount)
77 {
78     if (paramsCount == FUNCTION_PARAM_ZERO) {
79         return true;
80     }
81 
82     if (paramsCount == FUNCTION_PARAM_ONE) {
83         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object ||
84                NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function;
85     }
86 
87     if (paramsCount == FUNCTION_PARAM_TWO) {
88         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
89                NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
90     }
91 
92     return false;
93 }
94 
GetErrorCode() const95 int32_t CloseContext::GetErrorCode() const
96 {
97     if (BaseContext::IsPermissionDenied()) {
98         return PERMISSION_DENIED_CODE;
99     }
100 
101     auto err = BaseContext::GetErrorCode();
102     if (err == PARSE_ERROR_CODE) {
103         return PARSE_ERROR_CODE;
104     }
105     if (WEBSOCKET_ERR_MAP.find(err) != WEBSOCKET_ERR_MAP.end()) {
106         return err;
107     }
108     return WEBSOCKET_UNKNOWN_OTHER_ERROR;
109 }
110 
GetErrorMessage() const111 std::string CloseContext::GetErrorMessage() const
112 {
113     if (BaseContext::IsPermissionDenied()) {
114         return PERMISSION_DENIED_MSG;
115     }
116 
117     auto err = BaseContext::GetErrorCode();
118     if (err == PARSE_ERROR_CODE) {
119         return PARSE_ERROR_MSG;
120     }
121     auto it = WEBSOCKET_ERR_MAP.find(err);
122     if (it != WEBSOCKET_ERR_MAP.end()) {
123         return it->second;
124     }
125     it = WEBSOCKET_ERR_MAP.find(WEBSOCKET_UNKNOWN_OTHER_ERROR);
126     if (it != WEBSOCKET_ERR_MAP.end()) {
127         return it->second;
128     }
129     return {};
130 }
131 } // namespace OHOS::NetStack::Websocket
132