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 "get_netfirewall_rule_context.h"
17 #include "constant.h"
18 #include "napi_utils.h"
19 #include "net_firewall_rule_parse.h"
20 #include "net_manager_constants.h"
21 #include "netmgr_ext_log_wrapper.h"
22 #include "netfirewall_common.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
CheckParamsType(napi_env env,napi_value * params,size_t paramsCount)26 static bool CheckParamsType(napi_env env, napi_value *params, size_t paramsCount)
27 {
28 if (paramsCount != PARAM_DOUBLE_OPTIONS && paramsCount != PARAM_DOUBLE_OPTIONS_AND_CALLBACK) {
29 return false;
30 }
31 if (NapiUtils::GetValueType(env, params[ARG_INDEX_0]) != napi_number ||
32 NapiUtils::GetValueType(env, params[ARG_INDEX_1]) != napi_number) {
33 return false;
34 }
35 return true;
36 }
37
GetNetFirewallRuleContext(napi_env env,std::shared_ptr<EventManager> & manager)38 GetNetFirewallRuleContext::GetNetFirewallRuleContext(napi_env env, std::shared_ptr<EventManager>& manager)
39 : BaseContext(env, manager) {}
40
ParseParams(napi_value * params,size_t paramsCount)41 void GetNetFirewallRuleContext::ParseParams(napi_value *params, size_t paramsCount)
42 {
43 if (!CheckParamsType(GetEnv(), params, paramsCount)) {
44 SetErrorCode(FIREWALL_ERR_PARAMETER_ERROR);
45 return;
46 }
47
48 userId_ = NapiUtils::GetInt32FromValue(GetEnv(), params[ARG_INDEX_0]);
49 ruleId_ = NapiUtils::GetInt32FromValue(GetEnv(), params[ARG_INDEX_1]);
50 if (userId_ <= 0) {
51 NETMGR_EXT_LOG_E("userid param invalid.");
52 SetErrorCode(FIREWALL_ERR_INVALID_PARAMETER);
53 return;
54 }
55 if (ruleId_ <= 0) {
56 NETMGR_EXT_LOG_E("GetNetFirewallRuleContext ruleid param invalid.");
57 SetErrorCode(FIREWALL_ERR_INVALID_PARAMETER);
58 return;
59 }
60 if (paramsCount == PARAM_DOUBLE_OPTIONS_AND_CALLBACK) {
61 SetParseOK(SetCallback(params[ARG_INDEX_2]) == napi_ok);
62 return;
63 }
64 SetParseOK(true);
65 }
66 } // namespace NetManagerStandard
67 } // namespace OHOS