1 /*
2 * Copyright (c) 2025-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 "list_all_connections_context.h"
17 #include "constant.h"
18 #include "netstack_log.h"
19 #include "napi_utils.h"
20
21 namespace OHOS::NetStack::Websocket {
ListAllConnectionsContext(napi_env env,const std::shared_ptr<EventManager> & sharedManager)22 ListAllConnectionsContext::ListAllConnectionsContext(napi_env env, const std::shared_ptr<EventManager> &sharedManager)
23 : BaseContext(env, sharedManager) {}
24
25 ListAllConnectionsContext::~ListAllConnectionsContext() = default;
26
ParseParams(napi_value * params,size_t paramsCount)27 void ListAllConnectionsContext::ParseParams(napi_value *params, size_t paramsCount)
28 {
29 if (!CheckParamsType(params, paramsCount)) {
30 return;
31 }
32 if (paramsCount != FUNCTION_PARAM_ZERO) {
33 SetParseOK(SetCallback(params[0]) == napi_ok);
34 return;
35 }
36 SetParseOK(true);
37 }
38
CheckParamsType(napi_value * params,size_t paramsCount)39 bool ListAllConnectionsContext::CheckParamsType(napi_value *params, size_t paramsCount)
40 {
41 if (paramsCount == FUNCTION_PARAM_ZERO) {
42 return true;
43 }
44 return false;
45 }
46
SetAllConnections(std::vector<OHOS::NetStack::Websocket::WebSocketConnection> & connections)47 void ListAllConnectionsContext::SetAllConnections(std::vector<OHOS::NetStack::Websocket::WebSocketConnection>
48 &connections)
49 {
50 webSocketConnections_ = connections;
51 }
52
GetAllConnections() const53 std::vector<OHOS::NetStack::Websocket::WebSocketConnection> ListAllConnectionsContext::GetAllConnections() const
54 {
55 return webSocketConnections_;
56 }
57
GetErrorCode() const58 int32_t ListAllConnectionsContext::GetErrorCode() const
59 {
60 if (BaseContext::IsPermissionDenied()) {
61 return PERMISSION_DENIED_CODE;
62 }
63 return WEBSOCKET_UNKNOWN_OTHER_ERROR;
64 }
65
GetErrorMessage() const66 std::string ListAllConnectionsContext::GetErrorMessage() const
67 {
68 if (BaseContext::IsPermissionDenied()) {
69 return PERMISSION_DENIED_MSG;
70 }
71 auto it = WEBSOCKET_ERR_MAP.find(WEBSOCKET_UNKNOWN_OTHER_ERROR);
72 if (it != WEBSOCKET_ERR_MAP.end()) {
73 return it->second;
74 }
75 return {};
76 }
77 } // namespace OHOS::NetStack::Websocket