1 /*
2 * Copyright (c) 2021-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 "common_context.h"
17
18 #include "context_key.h"
19 #include "socket_constant.h"
20 #include "event_manager.h"
21 #include "napi_utils.h"
22
23 namespace OHOS::NetStack::Socket {
CommonContext(napi_env env,EventManager * manager)24 CommonContext::CommonContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
25
ParseParams(napi_value * params,size_t paramsCount)26 void CommonContext::ParseParams(napi_value *params, size_t paramsCount)
27 {
28 bool valid = CheckParamsType(params, paramsCount);
29 if (!valid) {
30 if (paramsCount == PARAM_JUST_CALLBACK) {
31 if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
32 SetCallback(params[0]);
33 }
34 return;
35 }
36 return;
37 }
38
39 if (paramsCount != PARAM_NONE) {
40 SetParseOK(SetCallback(params[0]) == napi_ok);
41 return;
42 }
43 SetParseOK(true);
44 }
45
GetSocketFd() const46 int CommonContext::GetSocketFd() const
47 {
48 if (manager_->GetData() == nullptr) {
49 return -1;
50 }
51 return (int)(uint64_t)manager_->GetData();
52 }
53
CheckParamsType(napi_value * params,size_t paramsCount)54 bool CommonContext::CheckParamsType(napi_value *params, size_t paramsCount)
55 {
56 if (paramsCount == PARAM_NONE) {
57 return true;
58 }
59
60 if (paramsCount == PARAM_JUST_CALLBACK) {
61 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function;
62 }
63 return false;
64 }
65
CloseContext(napi_env env,EventManager * manager)66 CloseContext::CloseContext(napi_env env, EventManager *manager) : CommonContext(env, manager) {}
67
SetSocketFd(int sock)68 void CloseContext::SetSocketFd(int sock)
69 {
70 manager_->SetData(reinterpret_cast<void *>(sock));
71 }
72
GetErrorCode() const73 int32_t CommonContext::GetErrorCode() const
74 {
75 if (BaseContext::IsPermissionDenied()) {
76 return PERMISSION_DENIED_CODE;
77 }
78
79 auto err = BaseContext::GetErrorCode();
80 if (err == PARSE_ERROR_CODE) {
81 return PARSE_ERROR_CODE;
82 }
83 #if defined(IOS_PLATFORM)
84 err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
85 #endif
86 return err + SOCKET_ERROR_CODE_BASE;
87 }
88
GetErrorMessage() const89 std::string CommonContext::GetErrorMessage() const
90 {
91 if (BaseContext::IsPermissionDenied()) {
92 return PERMISSION_DENIED_MSG;
93 }
94
95 auto errCode = BaseContext::GetErrorCode();
96 if (errCode == PARSE_ERROR_CODE) {
97 return PARSE_ERROR_MSG;
98 }
99 #if defined(IOS_PLATFORM)
100 std::string errMessage;
101 ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
102 return errMessage;
103 #else
104 if (errCode == -1) {
105 return "Unknown Other Error";
106 }
107 char err[MAX_ERR_NUM] = {0};
108 (void)strerror_r(errCode, err, MAX_ERR_NUM);
109 return err;
110 #endif
111 }
112 } // namespace OHOS::NetStack::Socket
113