1 /*
2  * Copyright (c) 2022-2023 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_net_quota_policies_context.h"
17 
18 #include <cstdint>
19 
20 #include "constant.h"
21 #include "napi_constant.h"
22 #include "napi_utils.h"
23 #include "netmanager_base_log.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 namespace {
ReadQuotaPolicy(napi_env env,napi_value value)28 NetQuotaPolicy ReadQuotaPolicy(napi_env env, napi_value value)
29 {
30     NetQuotaPolicy data;
31     napi_value netWorkMatchRule = NapiUtils::GetNamedProperty(env, value, "networkMatchRule");
32     napi_value quotaPolicy = NapiUtils::GetNamedProperty(env, value, "quotaPolicy");
33     data.networkmatchrule.netType = NapiUtils::GetInt32Property(env, netWorkMatchRule, "netType");
34     data.networkmatchrule.simId = NapiUtils::GetStringPropertyUtf8(env, netWorkMatchRule, "simId");
35     data.networkmatchrule.ident = NapiUtils::GetStringPropertyUtf8(env, netWorkMatchRule, "identity");
36     data.quotapolicy.periodDuration = NapiUtils::GetStringPropertyUtf8(env, quotaPolicy, "periodDuration");
37     data.quotapolicy.warningBytes = NapiUtils::GetInt64Property(env, quotaPolicy, "warningBytes");
38     data.quotapolicy.limitBytes = NapiUtils::GetInt64Property(env, quotaPolicy, "limitBytes");
39     data.quotapolicy.lastWarningRemind = NapiUtils::GetInt64Property(env, quotaPolicy, "lastWarningRemind");
40     data.quotapolicy.lastLimitRemind = NapiUtils::GetInt64Property(env, quotaPolicy, "lastLimitRemind");
41     data.quotapolicy.metered = NapiUtils::GetBooleanProperty(env, quotaPolicy, "metered");
42     data.quotapolicy.limitAction = NapiUtils::GetInt32Property(env, quotaPolicy, "limitAction");
43     return data;
44 }
45 } // namespace
SetNetQuotaPoliciesContext(napi_env env,EventManager * manager)46 SetNetQuotaPoliciesContext::SetNetQuotaPoliciesContext(napi_env env, EventManager *manager) : BaseContext(env, manager)
47 {
48 }
49 
ParseParams(napi_value * params,size_t paramsCount)50 void SetNetQuotaPoliciesContext::ParseParams(napi_value *params, size_t paramsCount)
51 {
52     if (!CheckParamsType(params, paramsCount)) {
53         NETMANAGER_BASE_LOGE("Check params failed");
54         SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
55         SetNeedThrowException(true);
56         return;
57     }
58 
59     uint32_t arrayLength = NapiUtils::GetArrayLength(GetEnv(), params[ARG_INDEX_0]);
60     arrayLength = arrayLength > ARRAY_LIMIT ? ARRAY_LIMIT : arrayLength;
61     napi_value elementValue = nullptr;
62     for (uint32_t i = 0; i < arrayLength; i++) {
63         elementValue = NapiUtils::GetArrayElement(GetEnv(), params[ARG_INDEX_0], i);
64         quotaPolicys_.push_back(ReadQuotaPolicy(GetEnv(), elementValue));
65     }
66 
67     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
68         SetParseOK(SetCallback(params[ARG_INDEX_1]) == napi_ok);
69         return;
70     }
71     SetParseOK(true);
72 }
73 
CheckParamsType(napi_value * params,size_t paramsCount)74 bool SetNetQuotaPoliciesContext::CheckParamsType(napi_value *params, size_t paramsCount)
75 {
76     if (paramsCount == PARAM_JUST_OPTIONS) {
77         return NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) == napi_object;
78     }
79 
80     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
81         return NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) == napi_object &&
82                NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_1]) == napi_function;
83     }
84     return false;
85 }
86 } // namespace NetManagerStandard
87 } // namespace OHOS
88