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