• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
17 #include "add_context.h"
18 #include "net_manager_constants.h"
19 #include "napi_utils.h"
20 #include "netmgr_ext_log_wrapper.h"
21 #include "net_manager_ext_constants.h"
22 #include "netmanager_base_common_utils.h"
23 #include "vpn_config_utils.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 constexpr int32_t PARAM_JUST_OPTIONS = 1;
28 constexpr int32_t PARAM_OPTIONS_AND_CALLBACK = 2;
29 
AddContext(napi_env env,EventManager * manager)30 AddContext::AddContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
31 
CheckParamsType(napi_env env,napi_value * params,size_t paramsCount)32 bool AddContext::CheckParamsType(napi_env env, napi_value *params, size_t paramsCount)
33 {
34     switch (paramsCount) {
35         case PARAM_JUST_OPTIONS:
36             return (NapiUtils::GetValueType(env, params[0]) == napi_object);
37         case PARAM_OPTIONS_AND_CALLBACK:
38             return ((NapiUtils::GetValueType(env, params[0]) == napi_object) &&
39                 (NapiUtils::GetValueType(env, params[1]) == napi_function));
40         default:
41             return false;
42     }
43 }
44 
ParseParams(napi_value * params,size_t paramsCount)45 void AddContext::ParseParams(napi_value *params, size_t paramsCount)
46 {
47     if (!CheckParamsType(GetEnv(), params, paramsCount)) {
48         NETMGR_EXT_LOG_E("params type is mismatch");
49         SetNeedThrowException(true);
50         SetErrorCode(NETMANAGER_EXT_ERR_PARAMETER_ERROR);
51         return;
52     }
53 
54     if (!VpnConfigUtils::ParseSysVpnConfig(GetEnv(), params, vpnConfig_)) {
55         NETMGR_EXT_LOG_E("parse vpn config from js failed");
56         SetNeedThrowException(true);
57         SetErrorCode(NETMANAGER_EXT_ERR_PARAMETER_ERROR);
58         return;
59     }
60     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
61         SetParseOK(SetCallback(params[1]) == napi_ok);
62         return;
63     }
64     SetParseOK(true);
65 }
66 } // namespace NetManagerStandard
67 } // namespace OHOS
68