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