• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,const std::shared_ptr<EventManager> & manager)24 CommonContext::CommonContext(napi_env env, const std::shared_ptr<EventManager> &manager)
25     : BaseContext(env, manager) {}
26 
ParseParams(napi_value * params,size_t paramsCount)27 void CommonContext::ParseParams(napi_value *params, size_t paramsCount)
28 {
29     bool valid = CheckParamsType(params, paramsCount);
30     if (!valid) {
31         if (paramsCount == PARAM_JUST_CALLBACK) {
32             if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
33                 SetCallback(params[0]);
34             }
35             return;
36         }
37         return;
38     }
39 
40     if (paramsCount != PARAM_NONE) {
41         SetParseOK(SetCallback(params[0]) == napi_ok);
42         return;
43     }
44     SetParseOK(true);
45 }
46 
GetSocketFd() const47 int CommonContext::GetSocketFd() const
48 {
49     return sharedManager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(sharedManager_->GetData())) : -1;
50 }
51 
CheckParamsType(napi_value * params,size_t paramsCount)52 bool CommonContext::CheckParamsType(napi_value *params, size_t paramsCount)
53 {
54     if (paramsCount == PARAM_NONE) {
55         return true;
56     }
57 
58     if (paramsCount == PARAM_JUST_CALLBACK) {
59         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function;
60     }
61     return false;
62 }
63 
CloseContext(napi_env env,const std::shared_ptr<EventManager> & manager)64 CloseContext::CloseContext(napi_env env, const std::shared_ptr<EventManager> &manager)
65     : CommonContext(env, manager) {}
66 
SetSocketFd(int sock)67 void CloseContext::SetSocketFd(int sock)
68 {
69     sharedManager_->SetData(reinterpret_cast<void *>(sock));
70 }
71 
GetErrorCode() const72 int32_t CommonContext::GetErrorCode() const
73 {
74     if (BaseContext::IsPermissionDenied()) {
75         return PERMISSION_DENIED_CODE;
76     }
77 
78     auto err = BaseContext::GetErrorCode();
79     if (err == PARSE_ERROR_CODE) {
80         return PARSE_ERROR_CODE;
81     }
82 #if defined(IOS_PLATFORM)
83     err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
84 #endif
85     return err + SOCKET_ERROR_CODE_BASE;
86 }
87 
GetErrorMessage() const88 std::string CommonContext::GetErrorMessage() const
89 {
90     if (BaseContext::IsPermissionDenied()) {
91         return PERMISSION_DENIED_MSG;
92     }
93 
94     auto errCode = BaseContext::GetErrorCode();
95     if (errCode == PARSE_ERROR_CODE) {
96         return PARSE_ERROR_MSG;
97     }
98 #if defined(IOS_PLATFORM)
99     std::string errMessage;
100     ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
101     return errMessage;
102 #else
103     if (errCode == -1) {
104         return "Unknown Other Error";
105     }
106     char err[MAX_ERR_NUM] = {0};
107     (void)strerror_r(errCode, err, MAX_ERR_NUM);
108     return err;
109 #endif
110 }
111 } // namespace OHOS::NetStack::Socket
112