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_netfirewall_policy_context.h"
17 #include "constant.h"
18 #include "napi_utils.h"
19 #include "net_firewall_param_check.h"
20 #include "net_firewall_rule_parse.h"
21 #include "net_manager_constants.h"
22 #include "netmgr_ext_log_wrapper.h"
23 #include "netfirewall_common.h"
24
25 namespace OHOS {
26 namespace NetManagerStandard {
CheckParamsType(napi_env env,napi_value * params,size_t count)27 static bool CheckParamsType(napi_env env, napi_value *params, size_t count)
28 {
29 if (count == PARAM_DOUBLE_OPTIONS || count == PARAM_DOUBLE_OPTIONS_AND_CALLBACK) {
30 if (NapiUtils::GetValueType(env, params[ARG_INDEX_0]) != napi_number ||
31 NapiUtils::GetValueType(env, params[ARG_INDEX_1]) != napi_object) {
32 return false;
33 }
34 } else {
35 // if count is not 2 or 3, means count error.
36 return false;
37 }
38 return true;
39 }
40
SetNetFirewallPolicyContext(napi_env env,EventManager * manager)41 SetNetFirewallPolicyContext::SetNetFirewallPolicyContext(napi_env env, EventManager *manager)
42 : BaseContext(env, manager)
43 {}
44
ParseParams(napi_value * params,size_t paramsCount)45 void SetNetFirewallPolicyContext::ParseParams(napi_value *params, size_t paramsCount)
46 {
47 if (!CheckParamsType(GetEnv(), params, paramsCount)) {
48 SetErrorCode(FIREWALL_ERR_PARAMETER_ERROR);
49 return;
50 }
51
52 userId_ = NapiUtils::GetInt32FromValue(GetEnv(), params[ARG_INDEX_0]);
53 if (userId_ < 0) {
54 NETMGR_EXT_LOG_E("SetNetFirewallPolicyContext userId parma invalid.");
55 SetErrorCode(FIREWALL_ERR_INVALID_PARAMETER);
56 return;
57 }
58
59 int ret = NetFirewallParamCheck::CheckFirewallRulePolicy(GetEnv(), params[ARG_INDEX_1]);
60 if (ret != FIREWALL_SUCCESS) {
61 NETMGR_EXT_LOG_E("rule action parma invalid.");
62 SetErrorCode(ret);
63 return;
64 }
65
66 status_ = new (std::nothrow) NetFirewallPolicy();
67 if (status_ == nullptr) {
68 NETMGR_EXT_LOG_E("firewall rule object is nullptr.");
69 SetErrorCode(FIREWALL_ERR_INTERNAL);
70 return;
71 }
72 status_->isOpen = NapiUtils::GetBooleanProperty(GetEnv(), params[ARG_INDEX_1], NET_FIREWALL_IS_OPEN);
73 status_->inAction = static_cast<FirewallRuleAction>(
74 NapiUtils::GetInt32Property(GetEnv(), params[ARG_INDEX_1], NET_FIREWALL_IN_ACTION));
75 status_->outAction = static_cast<FirewallRuleAction>(
76 NapiUtils::GetInt32Property(GetEnv(), params[ARG_INDEX_1], NET_FIREWALL_OUT_ACTION));
77 if (paramsCount == PARAM_DOUBLE_OPTIONS_AND_CALLBACK) {
78 SetParseOK(SetCallback(params[ARG_INDEX_2]) == napi_ok);
79 return;
80 }
81 SetParseOK(true);
82 }
83 } // namespace NetManagerStandard
84 } // namespace OHOS