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 #include "set_network_access_policy_context.h"
17
18 #include "napi_constant.h"
19 #include "napi_utils.h"
20 #include "netmanager_base_log.h"
21
22 namespace OHOS {
23 namespace NetManagerStandard {
SetNetworkAccessPolicyContext(napi_env env,EventManager * manager)24 SetNetworkAccessPolicyContext::SetNetworkAccessPolicyContext(napi_env env, EventManager *manager)
25 : BaseContext(env, manager)
26 {
27 }
28
ParseParams(napi_value * params,size_t paramsCount)29 void SetNetworkAccessPolicyContext::ParseParams(napi_value *params, size_t paramsCount)
30 {
31 if (!CheckParamsType(GetEnv(), params, paramsCount)) {
32 NETMANAGER_BASE_LOGE("check params type failed");
33 SetNeedThrowException(true);
34 SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
35 return;
36 }
37
38 uid_ = NapiUtils::GetUint32FromValue(GetEnv(), params[ARG_INDEX_0]);
39
40 if (NapiUtils::HasNamedProperty(GetEnv(), params[ARG_INDEX_1], "allowWiFi")) {
41 policy_.wifiAllow = NapiUtils::GetBooleanProperty(GetEnv(), params[ARG_INDEX_1], "allowWiFi");
42 }
43
44 if (NapiUtils::HasNamedProperty(GetEnv(), params[ARG_INDEX_1], "allowCellular")) {
45 policy_.cellularAllow = NapiUtils::GetBooleanProperty(GetEnv(), params[ARG_INDEX_1], "allowCellular");
46 }
47
48 if (paramsCount == PARAM_TRIPLE_OPTIONS) {
49 isReconfirmFlag_ = NapiUtils::GetBooleanValue(GetEnv(), params[ARG_INDEX_2]);
50 }
51
52 SetParseOK(true);
53 }
54
CheckParamsType(napi_env env,napi_value * params,size_t paramsCount)55 bool SetNetworkAccessPolicyContext::CheckParamsType(napi_env env, napi_value *params, size_t paramsCount)
56 {
57 if (paramsCount == PARAM_DOUBLE_OPTIONS) {
58 if (NapiUtils::GetValueType(env, params[ARG_INDEX_0]) == napi_number) {
59 }
60
61 if (NapiUtils::GetValueType(env, params[ARG_INDEX_1]) == napi_object) {
62 }
63
64 return NapiUtils::GetValueType(env, params[ARG_INDEX_0]) == napi_number &&
65 NapiUtils::GetValueType(env, params[ARG_INDEX_1]) == napi_object;
66 }
67
68 if (paramsCount == PARAM_TRIPLE_OPTIONS) {
69 if (NapiUtils::GetValueType(env, params[ARG_INDEX_0]) == napi_number) {
70 }
71
72 if (NapiUtils::GetValueType(env, params[ARG_INDEX_1]) == napi_object) {
73 }
74
75 if (NapiUtils::GetValueType(env, params[ARG_INDEX_2]) == napi_boolean) {
76 }
77
78 return NapiUtils::GetValueType(env, params[ARG_INDEX_0]) == napi_number &&
79 NapiUtils::GetValueType(env, params[ARG_INDEX_1]) == napi_object &&
80 NapiUtils::GetValueType(env, params[ARG_INDEX_2]) == napi_boolean;
81 }
82
83 return false;
84 }
85 } // namespace NetManagerStandard
86 } // namespace OHOS
87