• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "cleartext_context.h"
17 #include "netstack_log.h"
18 #include "napi_utils.h"
19 #if HAS_NETMANAGER_BASE
20 #include "net_conn_client.h"
21 #endif // HAS_NETMANAGER_BASE
22 
23 namespace OHOS::NetStack::Ssl {
CleartextContext(napi_env env,const std::shared_ptr<EventManager> & manager)24 CleartextContext::CleartextContext(napi_env env, const std::shared_ptr<EventManager> &manager)
25     : BaseContext(env, manager) {}
26 
ParseParams(napi_value * params,size_t paramsCount)27 void CleartextContext::ParseParams(napi_value *params, size_t paramsCount)
28 {
29     if (paramsCount != PARAM_NONE) {
30         NETSTACK_LOGE("check params type failed");
31         SetNeedThrowException(true);
32         SetErrorCode(PARSE_ERROR_CODE);
33         return;
34     }
35     SetParseOK(true);
36 }
37 
GetErrorMessage() const38 std::string CleartextContext::GetErrorMessage() const
39 {
40     auto errCode = BaseContext::GetErrorCode();
41     if (errCode == PARSE_ERROR_CODE) {
42         return PARSE_ERROR_MSG;
43     }
44     if (errCode == PERMISSION_DENIED_CODE) {
45         return PERMISSION_DENIED_MSG;
46     }
47     char err[MAX_ERR_NUM] = {0};
48     (void)strerror_r(errCode, err, MAX_ERR_NUM);
49     return err;
50 }
51 
CleartextForHostContext(napi_env env,const std::shared_ptr<EventManager> & manager)52 CleartextForHostContext::CleartextForHostContext(napi_env env, const std::shared_ptr<EventManager> &manager)
53     : BaseContext(env, manager) {}
54 
55 
ParseParams(napi_value * params,size_t paramsCount)56 void CleartextForHostContext::ParseParams(napi_value *params, size_t paramsCount)
57 {
58     if (!CheckParamsType(params, paramsCount)) {
59         NETSTACK_LOGE("check params type failed");
60         SetNeedThrowException(true);
61         SetErrorCode(PARSE_ERROR_CODE);
62         return;
63     }
64     hostname_ = NapiUtils::GetStringFromValueUtf8(GetEnv(), params[0]);
65     SetParseOK(true);
66 }
67 
CheckParamsType(napi_value * params,size_t paramsCount)68 bool CleartextForHostContext::CheckParamsType(napi_value *params, size_t paramsCount)
69 {
70     if (paramsCount == PARAM_JUST_OPTIONS) {
71         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_string;
72     }
73     return false;
74 }
75 
GetErrorMessage() const76 std::string CleartextForHostContext::GetErrorMessage() const
77 {
78     auto errCode = BaseContext::GetErrorCode();
79     if (errCode == PARSE_ERROR_CODE) {
80         return PARSE_ERROR_MSG;
81     }
82     if (errCode == PERMISSION_DENIED_CODE) {
83         return PERMISSION_DENIED_MSG;
84     }
85     char err[MAX_ERR_NUM] = {0};
86     (void)strerror_r(errCode, err, MAX_ERR_NUM);
87     return err;
88 }
89 
90 } // namespace OHOS::NetManagerStandard
91