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