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