1 /* 2 * Copyright (c) 2025 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 "net_websocket_close_context.h" 17 18 namespace OHOS::NetStack::NetWebSocket { WebSocketCloseContext(CJWebsocketProxy * websocketProxy)19WebSocketCloseContext::WebSocketCloseContext(CJWebsocketProxy *websocketProxy) 20 : WebSocketBaseContext(websocketProxy), code(CLOSE_REASON_NORMAL_CLOSE), reason("CLOSE_NORMAL") 21 { 22 } 23 ParseParams(CWebSocketCloseOptions * opt)24void WebSocketCloseContext::ParseParams(CWebSocketCloseOptions *opt) 25 { 26 if (opt != nullptr) { 27 code = opt->code; 28 if (opt->reason != nullptr) { 29 reason = std::string{opt->reason}; 30 } 31 } 32 SetParseOK(true); 33 } 34 GetErrorCode() const35int32_t WebSocketCloseContext::GetErrorCode() const 36 { 37 if (WebSocketBaseContext::IsPermissionDenied()) { 38 return WEBSOCKET_PERMISSION_DENIED_CODE; 39 } 40 auto err = WebSocketBaseContext::GetErrorCode(); 41 if (WEBSOCKET_ERR_MAP.find(err) != WEBSOCKET_ERR_MAP.end()) { 42 return err; 43 } 44 return WEBSOCKET_CONNECT_FAILED; 45 } 46 GetErrorMessage() const47std::string WebSocketCloseContext::GetErrorMessage() const 48 { 49 auto err = WebSocketBaseContext::GetErrorCode(); 50 auto it = WEBSOCKET_ERR_MAP.find(err); 51 if (it != WEBSOCKET_ERR_MAP.end()) { 52 return it->second; 53 } 54 it = WEBSOCKET_ERR_MAP.find(WEBSOCKET_UNKNOWN_OTHER_ERROR); 55 if (it != WEBSOCKET_ERR_MAP.end()) { 56 return it->second; 57 } 58 return {}; 59 } 60 }