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 "server_stop_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 {
ServerStopContext(napi_env env,const std::shared_ptr<EventManager> & sharedManager)24 ServerStopContext::ServerStopContext(napi_env env, const std::shared_ptr<EventManager> &sharedManager)
25 : BaseContext(env, sharedManager) {}
26
27 ServerStopContext::~ServerStopContext() = default;
28
ParseParams(napi_value * params,size_t paramsCount)29 void ServerStopContext::ParseParams(napi_value *params, size_t paramsCount)
30 {
31 if (!CheckParamsType(params, paramsCount)) {
32 return;
33 }
34 if (paramsCount != FUNCTION_PARAM_ZERO) {
35 SetParseOK(SetCallback(params[0]) == napi_ok);
36 return;
37 }
38 SetParseOK(true);
39 }
40
CheckParamsType(napi_value * params,size_t paramsCount)41 bool ServerStopContext::CheckParamsType(napi_value *params, size_t paramsCount)
42 {
43 if (paramsCount == FUNCTION_PARAM_ZERO) {
44 return true;
45 }
46 return false;
47 }
48
GetErrorCode() const49 int32_t ServerStopContext::GetErrorCode() const
50 {
51 if (BaseContext::IsPermissionDenied()) {
52 return PERMISSION_DENIED_CODE;
53 }
54 return WEBSOCKET_UNKNOWN_OTHER_ERROR;
55 }
56
GetErrorMessage() const57 std::string ServerStopContext::GetErrorMessage() const
58 {
59 if (BaseContext::IsPermissionDenied()) {
60 return PERMISSION_DENIED_MSG;
61 }
62 auto it = WEBSOCKET_ERR_MAP.find(WEBSOCKET_UNKNOWN_OTHER_ERROR);
63 if (it != WEBSOCKET_ERR_MAP.end()) {
64 return it->second;
65 }
66 return {};
67 }
68 } // namespace OHOS::NetStack::Websocket