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 "netpolicy_exec.h"
17
18 #include "net_policy_client.h"
19 #include "net_policy_constants.h"
20 #include "netmanager_base_log.h"
21 #include "napi_utils.h"
22
23 namespace OHOS {
24 namespace NetManagerStandard {
ExecSetPolicyByUid(SetPolicyByUidContext * context)25 bool NetPolicyExec::ExecSetPolicyByUid(SetPolicyByUidContext *context)
26 {
27 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->SetPolicyByUid(context->uid_, context->policy_);
28 if (result != NETMANAGER_SUCCESS) {
29 NETMANAGER_BASE_LOGE("ExecSetPolicyByUid error, uid = %{public}s, policy = %{public}d, result = %{public}d",
30 std::to_string(context->uid_).c_str(), context->policy_, result);
31 context->SetErrorCode(result);
32 return false;
33 }
34 context->SetErrorCode(result);
35 return true;
36 }
37
ExecGetPolicyByUid(GetPolicyByUidContext * context)38 bool NetPolicyExec::ExecGetPolicyByUid(GetPolicyByUidContext *context)
39 {
40 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->GetPolicyByUid(context->uid_, context->policy_);
41 if (result != NETMANAGER_SUCCESS) {
42 context->SetErrorCode(result);
43 return false;
44 }
45 context->SetErrorCode(result);
46 return true;
47 }
48
ExecGetUidsByPolicy(GetUidsByPolicyContext * context)49 bool NetPolicyExec::ExecGetUidsByPolicy(GetUidsByPolicyContext *context)
50 {
51 int32_t result =
52 DelayedSingleton<NetPolicyClient>::GetInstance()->GetUidsByPolicy(context->policy_, context->uidTogether_);
53 if (result != NETMANAGER_SUCCESS) {
54 context->SetErrorCode(result);
55 return false;
56 }
57 context->SetErrorCode(result);
58 return true;
59 }
60
ExecSetBackgroundPolicy(SetBackgroundPolicyContext * context)61 bool NetPolicyExec::ExecSetBackgroundPolicy(SetBackgroundPolicyContext *context)
62 {
63 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->SetBackgroundPolicy(context->isAllowed_);
64 if (result != NETMANAGER_SUCCESS) {
65 NETMANAGER_BASE_LOGE("ExecSetBackgroundPolicy error, isAllowed = %{public}d, result = %{public}d",
66 context->isAllowed_, result);
67 context->SetErrorCode(result);
68 return false;
69 }
70 context->SetErrorCode(result);
71 return true;
72 }
73
ExecGetBackgroundPolicy(GetBackgroundPolicyContext * context)74 bool NetPolicyExec::ExecGetBackgroundPolicy(GetBackgroundPolicyContext *context)
75 {
76 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->GetBackgroundPolicy(context->backgroundPolicy_);
77 if (result != NETMANAGER_SUCCESS) {
78 context->SetErrorCode(result);
79 return false;
80 }
81 context->SetErrorCode(result);
82 return true;
83 }
84
ExecGetNetQuotaPolicies(GetNetQuotaPoliciesContext * context)85 bool NetPolicyExec::ExecGetNetQuotaPolicies(GetNetQuotaPoliciesContext *context)
86 {
87 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->GetNetQuotaPolicies(context->quotaPolicys_);
88 if (result != NETMANAGER_SUCCESS) {
89 NETMANAGER_BASE_LOGE("ExecGetNetQuotaPolicies error, result = %{public}d", result);
90 context->SetErrorCode(result);
91 return false;
92 }
93 context->SetErrorCode(result);
94 return true;
95 }
96
ExecSetNetQuotaPolicies(SetNetQuotaPoliciesContext * context)97 bool NetPolicyExec::ExecSetNetQuotaPolicies(SetNetQuotaPoliciesContext *context)
98 {
99 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->SetNetQuotaPolicies(context->quotaPolicys_);
100 if (result != NETMANAGER_SUCCESS) {
101 NETMANAGER_BASE_LOGE("ExecSetNetQuotaPolicies error, result = %{public}d, arr size = %{public}zu", result,
102 context->quotaPolicys_.size());
103 context->SetErrorCode(result);
104 return false;
105 }
106 context->SetErrorCode(result);
107 return true;
108 }
109
ExecRestoreAllPolicies(RestoreAllPoliciesContext * context)110 bool NetPolicyExec::ExecRestoreAllPolicies(RestoreAllPoliciesContext *context)
111 {
112 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->ResetPolicies(context->iccid_);
113 if (result != NETMANAGER_SUCCESS) {
114 NETMANAGER_BASE_LOGE("ExecRestoreAllPolicies error, result = %{public}d", result);
115 context->SetErrorCode(result);
116 return false;
117 }
118 context->SetErrorCode(result);
119 return true;
120 }
121
ExecIsUidNetAllowed(IsUidNetAllowedContext * context)122 bool NetPolicyExec::ExecIsUidNetAllowed(IsUidNetAllowedContext *context)
123 {
124 int32_t result = NETMANAGER_SUCCESS;
125 if (context->isBoolean_) {
126 result = DelayedSingleton<NetPolicyClient>::GetInstance()->IsUidNetAllowed(context->uid_, context->isMetered_,
127 context->isUidNet_);
128 } else {
129 result = DelayedSingleton<NetPolicyClient>::GetInstance()->IsUidNetAllowed(context->uid_, context->iface_,
130 context->isUidNet_);
131 }
132
133 if (result != NETMANAGER_SUCCESS) {
134 NETMANAGER_BASE_LOGE("ExecIsUidNetAllowed error, result = %{public}d", result);
135 context->SetErrorCode(result);
136 return false;
137 }
138 context->SetErrorCode(result);
139 return true;
140 }
141
ExecSetDeviceIdleAllowList(SetDeviceIdleAllowListContext * context)142 bool NetPolicyExec::ExecSetDeviceIdleAllowList(SetDeviceIdleAllowListContext *context)
143 {
144 int32_t result =
145 DelayedSingleton<NetPolicyClient>::GetInstance()->SetDeviceIdleAllowedList(context->uid_, context->isAllow_);
146 if (result != NETMANAGER_SUCCESS) {
147 NETMANAGER_BASE_LOGE("ExecSetDeviceIdleAllowList error: uid = %{public}s, result = %{public}d",
148 std::to_string(context->uid_).c_str(), result);
149 context->SetErrorCode(result);
150 return false;
151 }
152 context->SetErrorCode(result);
153 return true;
154 }
155
ExecGetDeviceIdleAllowList(GetDeviceIdleAllowListContext * context)156 bool NetPolicyExec::ExecGetDeviceIdleAllowList(GetDeviceIdleAllowListContext *context)
157 {
158 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->GetDeviceIdleAllowedList(context->uids_);
159 if (result != NETMANAGER_SUCCESS) {
160 NETMANAGER_BASE_LOGE("ExecGetDeviceIdleAllowList error: result = %{public}d, arr size = %{public}zu", result,
161 context->uids_.size());
162 context->SetErrorCode(result);
163 return false;
164 }
165 context->SetErrorCode(result);
166 return true;
167 }
168
ExecGetBackgroundPolicyByUid(GetBackgroundPolicyByUidContext * context)169 bool NetPolicyExec::ExecGetBackgroundPolicyByUid(GetBackgroundPolicyByUidContext *context)
170 {
171 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->GetBackgroundPolicyByUid(
172 context->uid_, context->backgroundPolicyOfUid_);
173 if (result != NETMANAGER_SUCCESS) {
174 NETMANAGER_BASE_LOGE("ExecGetBackgroundPolicyByUid error: result = %{public}d", result);
175 context->SetErrorCode(result);
176 return false;
177 }
178 context->SetErrorCode(result);
179 return true;
180 }
181
ExecResetPolicies(ResetPoliciesContext * context)182 bool NetPolicyExec::ExecResetPolicies(ResetPoliciesContext *context)
183 {
184 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->ResetPolicies(context->iccid_);
185 if (result != NETMANAGER_SUCCESS) {
186 NETMANAGER_BASE_LOGE("ExecResetPolicies error: result = %{public}d", result);
187 context->SetErrorCode(result);
188 return false;
189 }
190 context->SetErrorCode(result);
191 return true;
192 }
193
ExecUpdateRemindPolicy(UpdateRemindPolicyContext * context)194 bool NetPolicyExec::ExecUpdateRemindPolicy(UpdateRemindPolicyContext *context)
195 {
196 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->UpdateRemindPolicy(
197 context->netType_, context->iccid_, context->remindType_);
198 if (result != NETMANAGER_SUCCESS) {
199 NETMANAGER_BASE_LOGE("ExecUpdateRemindPolicy error: result = %{public}d", result);
200 context->SetErrorCode(result);
201 return false;
202 }
203 context->SetErrorCode(result);
204 return true;
205 }
206
SetPolicyByUidCallback(SetPolicyByUidContext * context)207 napi_value NetPolicyExec::SetPolicyByUidCallback(SetPolicyByUidContext *context)
208 {
209 return NapiUtils::GetUndefined(context->GetEnv());
210 }
211
GetPolicyByUidCallback(GetPolicyByUidContext * context)212 napi_value NetPolicyExec::GetPolicyByUidCallback(GetPolicyByUidContext *context)
213 {
214 return NapiUtils::CreateInt32(context->GetEnv(), context->policy_);
215 }
216
GetUidsByPolicyCallback(GetUidsByPolicyContext * context)217 napi_value NetPolicyExec::GetUidsByPolicyCallback(GetUidsByPolicyContext *context)
218 {
219 napi_value uids = NapiUtils::CreateArray(context->GetEnv(), context->uidTogether_.size());
220 uint32_t index = 0;
221 for (const auto &uid : context->uidTogether_) {
222 napi_value element = NapiUtils::CreateInt32(context->GetEnv(), uid);
223 NapiUtils::SetArrayElement(context->GetEnv(), uids, index++, element);
224 }
225 return uids;
226 }
227
SetBackgroundPolicyCallback(SetBackgroundPolicyContext * context)228 napi_value NetPolicyExec::SetBackgroundPolicyCallback(SetBackgroundPolicyContext *context)
229 {
230 return NapiUtils::GetUndefined(context->GetEnv());
231 }
232
GetBackgroundPolicyCallback(GetBackgroundPolicyContext * context)233 napi_value NetPolicyExec::GetBackgroundPolicyCallback(GetBackgroundPolicyContext *context)
234 {
235 return NapiUtils::GetBoolean(context->GetEnv(), context->backgroundPolicy_);
236 }
237
GetNetQuotaPoliciesCallback(GetNetQuotaPoliciesContext * context)238 napi_value NetPolicyExec::GetNetQuotaPoliciesCallback(GetNetQuotaPoliciesContext *context)
239 {
240 napi_value callbackValue = NapiUtils::CreateArray(context->GetEnv(), context->quotaPolicys_.size());
241 uint32_t index = 0;
242 for (const auto "aPolicy : context->quotaPolicys_) {
243 napi_value element = NetPolicyExec::CreateNetQuotaPolicy(context->GetEnv(), quotaPolicy);
244 NapiUtils::SetArrayElement(context->GetEnv(), callbackValue, index++, element);
245 }
246 return callbackValue;
247 }
248
CreateNetQuotaPolicy(napi_env env,const NetQuotaPolicy & item)249 napi_value NetPolicyExec::CreateNetQuotaPolicy(napi_env env, const NetQuotaPolicy &item)
250 {
251 napi_value elementObject = NapiUtils::CreateObject(env);
252 NapiUtils::SetInt32Property(env, elementObject, "netType", item.netType);
253 NapiUtils::SetStringPropertyUtf8(env, elementObject, "iccid", item.iccid);
254 NapiUtils::SetStringPropertyUtf8(env, elementObject, "ident", item.ident);
255 NapiUtils::SetStringPropertyUtf8(env, elementObject, "periodDuration", item.periodDuration);
256 NapiUtils::SetInt64Property(env, elementObject, "warningBytes", item.warningBytes);
257 NapiUtils::SetInt64Property(env, elementObject, "limitBytes", item.limitBytes);
258 NapiUtils::SetInt64Property(env, elementObject, "lastWarningRemind", item.lastWarningRemind);
259 NapiUtils::SetInt64Property(env, elementObject, "lastLimitRemind", item.lastLimitRemind);
260 NapiUtils::SetBooleanProperty(env, elementObject, "metered", item.metered);
261 NapiUtils::SetInt32Property(env, elementObject, "limitAction", item.limitAction);
262 return elementObject;
263 }
264
SetNetQuotaPoliciesCallback(SetNetQuotaPoliciesContext * context)265 napi_value NetPolicyExec::SetNetQuotaPoliciesCallback(SetNetQuotaPoliciesContext *context)
266 {
267 return NapiUtils::GetUndefined(context->GetEnv());
268 }
269
RestoreAllPoliciesCallback(RestoreAllPoliciesContext * context)270 napi_value NetPolicyExec::RestoreAllPoliciesCallback(RestoreAllPoliciesContext *context)
271 {
272 return NapiUtils::GetUndefined(context->GetEnv());
273 }
274
IsUidNetAllowedCallback(IsUidNetAllowedContext * context)275 napi_value NetPolicyExec::IsUidNetAllowedCallback(IsUidNetAllowedContext *context)
276 {
277 return NapiUtils::GetBoolean(context->GetEnv(), context->isUidNet_);
278 }
279
SetDeviceIdleAllowListCallback(SetDeviceIdleAllowListContext * context)280 napi_value NetPolicyExec::SetDeviceIdleAllowListCallback(SetDeviceIdleAllowListContext *context)
281 {
282 return NapiUtils::GetUndefined(context->GetEnv());
283 }
284
GetDeviceIdleAllowListCallback(GetDeviceIdleAllowListContext * context)285 napi_value NetPolicyExec::GetDeviceIdleAllowListCallback(GetDeviceIdleAllowListContext *context)
286 {
287 napi_value list = NapiUtils::CreateArray(context->GetEnv(), context->uids_.size());
288 uint32_t index = 0;
289 for (const auto &uid : context->uids_) {
290 napi_value element = NapiUtils::CreateUint32(context->GetEnv(), uid);
291 NapiUtils::SetArrayElement(context->GetEnv(), list, index++, element);
292 }
293 return list;
294 }
295
GetBackgroundPolicyByUidCallback(GetBackgroundPolicyByUidContext * context)296 napi_value NetPolicyExec::GetBackgroundPolicyByUidCallback(GetBackgroundPolicyByUidContext *context)
297 {
298 return NapiUtils::CreateInt32(context->GetEnv(), static_cast<int32_t>(context->backgroundPolicyOfUid_));
299 }
300
ResetPoliciesCallback(ResetPoliciesContext * context)301 napi_value NetPolicyExec::ResetPoliciesCallback(ResetPoliciesContext *context)
302 {
303 return NapiUtils::GetUndefined(context->GetEnv());
304 }
305
UpdateRemindPolicyCallback(UpdateRemindPolicyContext * context)306 napi_value NetPolicyExec::UpdateRemindPolicyCallback(UpdateRemindPolicyContext *context)
307 {
308 return NapiUtils::GetUndefined(context->GetEnv());
309 }
310 } // namespace NetManagerStandard
311 } // namespace OHOS
312