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 "add_netfirewall_rule_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 paramsCount)27 static bool CheckParamsType(napi_env env, napi_value *params, size_t paramsCount)
28 {
29 if (paramsCount == PARAM_JUST_OPTIONS || paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
30 if (NapiUtils::GetValueType(env, params[ARG_INDEX_0]) != napi_object) {
31 return false;
32 }
33 } else {
34 // if paramsCount is not 0 or 1, means count error.
35 return false;
36 }
37 return true;
38 }
39
AddNetFirewallRuleContext(napi_env env,EventManager * manager)40 AddNetFirewallRuleContext::AddNetFirewallRuleContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
41
ParseParams(napi_value * params,size_t paramsCount)42 void AddNetFirewallRuleContext::ParseParams(napi_value *params, size_t paramsCount)
43 {
44 if (!CheckParamsType(GetEnv(), params, paramsCount)) {
45 SetErrorCode(FIREWALL_ERR_PARAMETER_ERROR);
46 return;
47 }
48
49 int32_t ret = NetFirewallParamCheck::CheckAddFirewallRule(GetEnv(), params[ARG_INDEX_0]);
50 if (ret != FIREWALL_SUCCESS) {
51 NETMGR_EXT_LOG_E("firewall rule parma invalid.");
52 SetErrorCode(ret);
53 return;
54 }
55
56 rule_ = new (std::nothrow) NetFirewallRule();
57 if (rule_ == nullptr) {
58 NETMGR_EXT_LOG_E("firewall rule object is nullptr.");
59 SetErrorCode(FIREWALL_ERR_INTERNAL);
60 return;
61 }
62 NetFirewallRuleParse::ParseRuleParams(GetEnv(), params[ARG_INDEX_0], rule_);
63 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
64 SetParseOK(SetCallback(params[ARG_INDEX_1]) == napi_ok);
65 return;
66 }
67 SetParseOK(true);
68 }
69 } // namespace NetManagerStandard
70 } // namespace OHOS