• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "setglobalhttpproxy_context.h"
17 
18 #include <set>
19 #include <string>
20 
21 #include "napi_constant.h"
22 #include "napi_utils.h"
23 #include "netmanager_base_log.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 namespace {
28 
CheckParamsType(napi_env env,napi_value * params,size_t paramsCount)29 bool CheckParamsType(napi_env env, napi_value *params, size_t paramsCount)
30 {
31     if (paramsCount == PARAM_JUST_OPTIONS) {
32         return NapiUtils::GetValueType(env, params[ARG_INDEX_0]) == napi_object;
33     }
34 
35     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
36         return NapiUtils::GetValueType(env, params[ARG_INDEX_0]) == napi_object &&
37                NapiUtils::GetValueType(env, params[ARG_INDEX_1]) == napi_function;
38     }
39     return false;
40 }
41 } // namespace
42 
SetGlobalHttpProxyContext(napi_env env,EventManager * manager)43 SetGlobalHttpProxyContext::SetGlobalHttpProxyContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
44 
ParseParams(napi_value * params,size_t paramsCount)45 void SetGlobalHttpProxyContext::ParseParams(napi_value *params, size_t paramsCount)
46 {
47     if (!CheckParamsType(GetEnv(), params, paramsCount)) {
48         NETMANAGER_BASE_LOGE("check params type failed");
49         SetNeedThrowException(true);
50         SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
51         return;
52     }
53 
54     httpProxy_.SetHost(NapiUtils::GetStringPropertyUtf8(GetEnv(), params[0], "host"));
55     httpProxy_.SetPort(static_cast<uint16_t>(NapiUtils::GetUint32Property(GetEnv(), params[0], "port")));
56 
57     std::set<std::string> list;
58     napi_value exclusionList = NapiUtils::GetNamedProperty(GetEnv(), params[0], "exclusionList");
59     uint32_t listLength = NapiUtils::GetArrayLength(GetEnv(), exclusionList);
60     for (uint32_t i = 0; i < listLength; ++i) {
61         napi_value element = NapiUtils::GetArrayElement(GetEnv(), exclusionList, i);
62         list.insert(NapiUtils::GetStringFromValueUtf8(GetEnv(), element));
63     }
64     httpProxy_.SetExclusionList(list);
65 
66     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
67         SetParseOK(SetCallback(params[ARG_INDEX_1]) == napi_ok);
68         return;
69     }
70 
71     SetParseOK(true);
72 }
73 } // namespace NetManagerStandard
74 } // namespace OHOS
75