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 "get_iface_uid_stats_context.h"
17
18 #include "constant.h"
19 #include "napi_constant.h"
20 #include "napi_utils.h"
21 #include "net_manager_constants.h"
22 #include "netmanager_base_log.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
26 namespace {
27 constexpr const char *IFACE_INFO = "ifaceInfo";
28 constexpr const char *UID = "uid";
29 constexpr const char *IFACE = "iface";
30 constexpr const char *START_TIME = "startTime";
31 constexpr const char *END_TIME = "endTime";
32 } // namespace
33
GetIfaceUidStatsContext(napi_env env,EventManager * manager)34 GetIfaceUidStatsContext::GetIfaceUidStatsContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
35
ParseParams(napi_value * params,size_t paramsCount)36 void GetIfaceUidStatsContext::ParseParams(napi_value *params, size_t paramsCount)
37 {
38 if (!CheckParamsType(params, paramsCount)) {
39 SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
40 SetNeedThrowException(true);
41 return;
42 }
43
44 napi_value ifaceInfo = NapiUtils::GetNamedProperty(GetEnv(), params[ARG_INDEX_0], IFACE_INFO);
45 bool hasUid = NapiUtils::HasNamedProperty(GetEnv(), params[ARG_INDEX_0], UID);
46 bool hasIface = NapiUtils::HasNamedProperty(GetEnv(), ifaceInfo, IFACE);
47 bool hasStart = NapiUtils::HasNamedProperty(GetEnv(), ifaceInfo, START_TIME);
48 bool hasEnd = NapiUtils::HasNamedProperty(GetEnv(), ifaceInfo, END_TIME);
49 if (!(hasUid && hasIface && hasStart && hasEnd)) {
50 NETMANAGER_BASE_LOGE(
51 "param error hasIface is %{public}d, hasStart is %{public}d, hasEnd is %{public}d"
52 "hasUid is %{public}d",
53 hasIface, hasStart, hasEnd, hasUid);
54 SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
55 SetNeedThrowException(true);
56 return;
57 }
58 bool checkUidType = NapiUtils::GetValueType(
59 GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[ARG_INDEX_0], UID)) == napi_number;
60 bool checkIfaceType =
61 NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), ifaceInfo, IFACE)) == napi_string;
62 bool checkStartType =
63 NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), ifaceInfo, START_TIME)) == napi_number;
64 bool checkEndType =
65 NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), ifaceInfo, END_TIME)) == napi_number;
66 if (!(checkUidType && checkIfaceType && checkStartType && checkEndType)) {
67 NETMANAGER_BASE_LOGE("param napi_type error");
68 SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
69 SetNeedThrowException(true);
70 return;
71 }
72 interfaceName_ = NapiUtils::GetStringPropertyUtf8(GetEnv(), ifaceInfo, IFACE);
73 start_ = NapiUtils::GetUint32Property(GetEnv(), ifaceInfo, START_TIME);
74 end_ = NapiUtils::GetUint32Property(GetEnv(), ifaceInfo, END_TIME);
75 uid_ = NapiUtils::GetInt32Property(GetEnv(), params[ARG_INDEX_0], UID);
76
77 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
78 SetParseOK(SetCallback(params[ARG_INDEX_1]) == napi_ok);
79 return;
80 }
81 SetParseOK(true);
82 }
83
CheckParamsType(napi_value * params,size_t paramsCount)84 bool GetIfaceUidStatsContext::CheckParamsType(napi_value *params, size_t paramsCount)
85 {
86 if (paramsCount == PARAM_JUST_OPTIONS) {
87 return NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) == napi_object;
88 }
89 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
90 return NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_1]) == napi_function &&
91 NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) == napi_object;
92 }
93 return false;
94 }
95
SetUid(int32_t uid)96 void GetIfaceUidStatsContext::SetUid(int32_t uid)
97 {
98 uid_ = uid;
99 }
100
SetInterfaceName(std::string interfaceName)101 void GetIfaceUidStatsContext::SetInterfaceName(std::string interfaceName)
102 {
103 interfaceName_ = interfaceName;
104 }
105
SetStatsInfo(NetStatsInfo statsInfo)106 void GetIfaceUidStatsContext::SetStatsInfo(NetStatsInfo statsInfo)
107 {
108 statsInfo_ = statsInfo;
109 }
110
SetStart(uint32_t start)111 void GetIfaceUidStatsContext::SetStart(uint32_t start)
112 {
113 start_ = start;
114 }
115
SetEnd(uint32_t end)116 void GetIfaceUidStatsContext::SetEnd(uint32_t end)
117 {
118 end_ = end;
119 }
120
GetUid() const121 int32_t GetIfaceUidStatsContext::GetUid() const
122 {
123 return uid_;
124 }
GetInterfaceName() const125 std::string GetIfaceUidStatsContext::GetInterfaceName() const
126 {
127 return interfaceName_;
128 }
129
GetStatsInfo()130 NetStatsInfo &GetIfaceUidStatsContext::GetStatsInfo()
131 {
132 return statsInfo_;
133 }
134
GetStart() const135 uint32_t GetIfaceUidStatsContext::GetStart() const
136 {
137 return start_;
138 }
139
GetEnd() const140 uint32_t GetIfaceUidStatsContext::GetEnd() const
141 {
142 return end_;
143 }
144 } // namespace NetManagerStandard
145 } // namespace OHOS
146