• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,std::shared_ptr<EventManager> & manager)46 SetNetQuotaPoliciesContext::SetNetQuotaPoliciesContext(napi_env env, std::shared_ptr<EventManager>& manager)
47     : BaseContext(env, manager)
48 {
49 }
50 
ParseParams(napi_value * params,size_t paramsCount)51 void SetNetQuotaPoliciesContext::ParseParams(napi_value *params, size_t paramsCount)
52 {
53     if (!CheckParamsType(params, paramsCount)) {
54         NETMANAGER_BASE_LOGE("Check params failed");
55         SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
56         SetNeedThrowException(true);
57         return;
58     }
59 
60     uint32_t arrayLength = NapiUtils::GetArrayLength(GetEnv(), params[ARG_INDEX_0]);
61     arrayLength = arrayLength > ARRAY_LIMIT ? ARRAY_LIMIT : arrayLength;
62     napi_value elementValue = nullptr;
63     for (uint32_t i = 0; i < arrayLength; i++) {
64         elementValue = NapiUtils::GetArrayElement(GetEnv(), params[ARG_INDEX_0], i);
65         quotaPolicys_.push_back(ReadQuotaPolicy(GetEnv(), elementValue));
66     }
67 
68     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
69         SetParseOK(SetCallback(params[ARG_INDEX_1]) == napi_ok);
70         return;
71     }
72     SetParseOK(true);
73 }
74 
CheckParamsType(napi_value * params,size_t paramsCount)75 bool SetNetQuotaPoliciesContext::CheckParamsType(napi_value *params, size_t paramsCount)
76 {
77     if (paramsCount == PARAM_JUST_OPTIONS) {
78         return NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) == napi_object;
79     }
80 
81     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
82         return NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) == napi_object &&
83                NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_1]) == napi_function;
84     }
85     return false;
86 }
87 } // namespace NetManagerStandard
88 } // namespace OHOS
89