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_all_netfirewall_rules_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         if (NapiUtils::GetValueType(env, params[ARG_INDEX_0]) != napi_number ||
30             NapiUtils::GetValueType(env, params[ARG_INDEX_1]) != napi_object) {
31             return false;
32         }
33         return true;
34     }
35     // if paramsCount is not 2 or 3, means count error.
36     return false;
37 }
38 
GetNetFirewallRulesContext(napi_env env,EventManager * manager)39 GetNetFirewallRulesContext::GetNetFirewallRulesContext(napi_env env, EventManager *manager)
40     : BaseContext(env, manager)
41 {}
42 
ParseParams(napi_value * params,size_t paramsCount)43 void GetNetFirewallRulesContext::ParseParams(napi_value *params, size_t paramsCount)
44 {
45     if (!CheckParamsType(GetEnv(), params, paramsCount)) {
46         SetErrorCode(FIREWALL_ERR_PARAMETER_ERROR);
47         return;
48     }
49 
50     userId_ = NapiUtils::GetInt32FromValue(GetEnv(), params[ARG_INDEX_0]);
51     if (userId_ <= 0) {
52         NETMGR_EXT_LOG_E("GetNetFirewallRulesContext userid param invalid.");
53         SetErrorCode(FIREWALL_ERR_INVALID_PARAMETER);
54         return;
55     }
56     requestParam_ = new (std::nothrow) RequestParam();
57     if (requestParam_ == nullptr) {
58         NETMGR_EXT_LOG_E("firewall requestParam_ object is nullptr.");
59         SetErrorCode(FIREWALL_ERR_INTERNAL);
60         return;
61     }
62     int32_t ret = NetFirewallRuleParse::ParsePageParam(GetEnv(), params[ARG_INDEX_1], requestParam_, true);
63     if (ret != FIREWALL_SUCCESS) {
64         SetErrorCode(FIREWALL_ERR_INVALID_PARAMETER);
65         return;
66     }
67     if (paramsCount == PARAM_DOUBLE_OPTIONS_AND_CALLBACK) {
68         SetParseOK(SetCallback(params[ARG_INDEX_2]) == napi_ok);
69         return;
70     }
71     SetParseOK(true);
72 }
73 } // namespace NetManagerStandard
74 } // namespace OHOS