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