• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "net_policy_client.h"
17 #include <thread>
18 
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 #include "net_mgr_log_wrapper.h"
22 
23 static constexpr uint32_t WAIT_FOR_SERVICE_TIME_MS = 500;
24 static constexpr uint32_t MAX_GET_SERVICE_COUNT = 10;
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
NetPolicyClient()28 NetPolicyClient::NetPolicyClient() : netPolicyService_(nullptr), deathRecipient_(nullptr), callback_(nullptr) {}
29 
~NetPolicyClient()30 NetPolicyClient::~NetPolicyClient()
31 {
32     NETMGR_LOG_I("~NetPolicyClient : Destroy NetPolicyClient");
33     sptr<INetPolicyService> proxy = GetProxy();
34     if (proxy == nullptr) {
35         return;
36     }
37 
38     auto serviceRemote = proxy->AsObject();
39     if (serviceRemote == nullptr) {
40         return;
41     }
42     serviceRemote->RemoveDeathRecipient(deathRecipient_);
43 }
44 
GetInstance()45 NetPolicyClient& NetPolicyClient::GetInstance()
46 {
47     static std::shared_ptr<NetPolicyClient> instance = std::make_shared<NetPolicyClient>();
48     return *instance;
49 }
50 
SetPolicyByUid(uint32_t uid,uint32_t policy)51 int32_t NetPolicyClient::SetPolicyByUid(uint32_t uid, uint32_t policy)
52 {
53     sptr<INetPolicyService> proxy = GetProxy();
54     if (proxy == nullptr) {
55         NETMGR_LOG_E("proxy is nullptr");
56         return NETMANAGER_ERR_GET_PROXY_FAIL;
57     }
58 
59     return proxy->SetPolicyByUid(uid, policy);
60 }
61 
GetPolicyByUid(uint32_t uid,uint32_t & policy)62 int32_t NetPolicyClient::GetPolicyByUid(uint32_t uid, uint32_t &policy)
63 {
64     sptr<INetPolicyService> proxy = GetProxy();
65     if (proxy == nullptr) {
66         NETMGR_LOG_E("proxy is nullptr");
67         return NETMANAGER_ERR_GET_PROXY_FAIL;
68     }
69     return proxy->GetPolicyByUid(uid, policy);
70 }
71 
GetUidsByPolicy(uint32_t policy,std::vector<uint32_t> & uids)72 int32_t NetPolicyClient::GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids)
73 {
74     sptr<INetPolicyService> proxy = GetProxy();
75     if (proxy == nullptr) {
76         NETMGR_LOG_E("proxy is nullptr");
77         return NETMANAGER_ERR_GET_PROXY_FAIL;
78     }
79 
80     return proxy->GetUidsByPolicy(policy, uids);
81 }
82 
IsUidNetAllowed(uint32_t uid,bool metered,bool & isAllowed)83 int32_t NetPolicyClient::IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed)
84 {
85     sptr<INetPolicyService> proxy = GetProxy();
86     if (proxy == nullptr) {
87         NETMGR_LOG_E("proxy is nullptr");
88         return NETMANAGER_ERR_GET_PROXY_FAIL;
89     }
90     return proxy->IsUidNetAllowed(uid, metered, isAllowed);
91 }
92 
IsUidNetAllowed(uint32_t uid,const std::string & ifaceName,bool & isAllowed)93 int32_t NetPolicyClient::IsUidNetAllowed(uint32_t uid, const std::string &ifaceName, bool &isAllowed)
94 {
95     sptr<INetPolicyService> proxy = GetProxy();
96     if (proxy == nullptr) {
97         NETMGR_LOG_E("proxy is nullptr");
98         return NETMANAGER_ERR_GET_PROXY_FAIL;
99     }
100 
101     return proxy->IsUidNetAllowed(uid, ifaceName, isAllowed);
102 }
103 
IsUidNetAccess(uint32_t uid,bool isMetered,bool & isAllowed)104 int32_t NetPolicyClient::IsUidNetAccess(uint32_t uid, bool isMetered, bool &isAllowed)
105 {
106     return IsUidNetAllowed(uid, isMetered, isAllowed);
107 }
108 
IsUidNetAccess(uint32_t uid,const std::string & ifaceName,bool & isAllowed)109 int32_t NetPolicyClient::IsUidNetAccess(uint32_t uid, const std::string &ifaceName, bool &isAllowed)
110 {
111     return IsUidNetAllowed(uid, ifaceName, isAllowed);
112 }
113 
GetProxy()114 sptr<INetPolicyService> NetPolicyClient::GetProxy()
115 {
116     std::lock_guard lock(mutex_);
117 
118     if (netPolicyService_ != nullptr) {
119         NETMGR_LOG_D("get proxy is ok");
120         return netPolicyService_;
121     }
122 
123     NETMGR_LOG_I("execute GetSystemAbilityManager");
124     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
125     if (sam == nullptr) {
126         NETMGR_LOG_E("NetPolicyManager::GetProxy(), get SystemAbilityManager failed");
127         return nullptr;
128     }
129 
130     sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID);
131     if (remote == nullptr) {
132         NETMGR_LOG_E("get Remote service failed");
133         return nullptr;
134     }
135 
136     deathRecipient_ = new (std::nothrow) NetPolicyDeathRecipient(*this);
137     if (deathRecipient_ == nullptr) {
138         NETMGR_LOG_E("get deathRecipient_ failed");
139         return nullptr;
140     }
141     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
142         NETMGR_LOG_E("add death recipient failed");
143         return nullptr;
144     }
145 
146     netPolicyService_ = iface_cast<INetPolicyService>(remote);
147     if (netPolicyService_ == nullptr) {
148         NETMGR_LOG_E("get Remote service proxy failed");
149         return nullptr;
150     }
151 
152     return netPolicyService_;
153 }
154 
RecoverCallback()155 void NetPolicyClient::RecoverCallback()
156 {
157     uint32_t count = 0;
158     while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
159         std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_SERVICE_TIME_MS));
160         count++;
161     }
162     auto proxy = GetProxy();
163     NETMGR_LOG_W("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
164     if (proxy != nullptr && callback_ != nullptr) {
165         int32_t ret = proxy->RegisterNetPolicyCallback(callback_);
166         NETMGR_LOG_D("Register result %{public}d", ret);
167     }
168 }
169 
OnRemoteDied(const wptr<IRemoteObject> & remote)170 void NetPolicyClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
171 {
172     NETMGR_LOG_D("on remote died");
173     if (remote == nullptr) {
174         NETMGR_LOG_E("remote object is nullptr");
175         return;
176     }
177     {
178         std::lock_guard lock(mutex_);
179         if (netPolicyService_ == nullptr) {
180             NETMGR_LOG_E("netPolicyService_ is nullptr");
181             return;
182         }
183 
184         sptr<IRemoteObject> local = netPolicyService_->AsObject();
185         if (local != remote.promote()) {
186             NETMGR_LOG_E("proxy and stub is not same remote object");
187             return;
188         }
189 
190         local->RemoveDeathRecipient(deathRecipient_);
191         netPolicyService_ = nullptr;
192     }
193 
194     if (callback_ != nullptr) {
195         NETMGR_LOG_D("on remote died recover callback");
196         std::thread t([sp = shared_from_this()]() { sp->RecoverCallback(); });
197         std::string threadName = "netpolicyRecoverCallback";
198         pthread_setname_np(t.native_handle(), threadName.c_str());
199         t.detach();
200     }
201 }
202 
RegisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)203 int32_t NetPolicyClient::RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
204 {
205     NETMGR_LOG_D("RegisterNetPolicyCallback client in");
206     sptr<INetPolicyService> proxy = GetProxy();
207     if (proxy == nullptr) {
208         NETMGR_LOG_E("proxy is nullptr");
209         return NETMANAGER_ERR_GET_PROXY_FAIL;
210     }
211     int32_t ret = proxy->RegisterNetPolicyCallback(callback);
212     if (ret == NETMANAGER_SUCCESS) {
213         NETMGR_LOG_D("RegisterNetPolicyCallback success, save callback");
214         callback_ = callback;
215     }
216 
217     return ret;
218 }
219 
UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)220 int32_t NetPolicyClient::UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
221 {
222     sptr<INetPolicyService> proxy = GetProxy();
223     if (proxy == nullptr) {
224         NETMGR_LOG_E("proxy is nullptr");
225         return NETMANAGER_ERR_GET_PROXY_FAIL;
226     }
227     int32_t ret = proxy->UnregisterNetPolicyCallback(callback);
228     if (ret == NETMANAGER_SUCCESS) {
229         NETMGR_LOG_D("UnRegisterNetPolicyCallback success, delete callback");
230         callback_ = nullptr;
231     }
232 
233     return ret;
234 }
235 
SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> & quotaPolicies)236 int32_t NetPolicyClient::SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies)
237 {
238     if (quotaPolicies.empty()) {
239         NETMGR_LOG_E("quotaPolicies is empty");
240         return NetPolicyResultCode::POLICY_ERR_INVALID_QUOTA_POLICY;
241     }
242 
243     if (quotaPolicies.size() > QUOTA_POLICY_MAX_SIZE) {
244         NETMGR_LOG_E("quotaPolicies's size is greater than the maximum, size is [%{public}zu]", quotaPolicies.size());
245         return NetPolicyResultCode::POLICY_ERR_INVALID_QUOTA_POLICY;
246     }
247 
248     sptr<INetPolicyService> proxy = GetProxy();
249     if (proxy == nullptr) {
250         NETMGR_LOG_E("proxy is nullptr");
251         return NETMANAGER_ERR_GET_PROXY_FAIL;
252     }
253 
254     return proxy->SetNetQuotaPolicies(quotaPolicies);
255 }
256 
GetNetQuotaPolicies(std::vector<NetQuotaPolicy> & quotaPolicies)257 int32_t NetPolicyClient::GetNetQuotaPolicies(std::vector<NetQuotaPolicy> &quotaPolicies)
258 {
259     sptr<INetPolicyService> proxy = GetProxy();
260     if (proxy == nullptr) {
261         NETMGR_LOG_E("proxy is nullptr");
262         return NETMANAGER_ERR_GET_PROXY_FAIL;
263     }
264 
265     return proxy->GetNetQuotaPolicies(quotaPolicies);
266 }
267 
SetFactoryPolicy(const std::string & simId)268 NetPolicyResultCode NetPolicyClient::SetFactoryPolicy(const std::string &simId)
269 {
270     return static_cast<NetPolicyResultCode>(ResetPolicies(simId));
271 }
272 
ResetPolicies(const std::string & simId)273 int32_t NetPolicyClient::ResetPolicies(const std::string &simId)
274 {
275     sptr<INetPolicyService> proxy = GetProxy();
276     if (proxy == nullptr) {
277         NETMGR_LOG_E("proxy is nullptr");
278         return NETMANAGER_ERR_GET_PROXY_FAIL;
279     }
280 
281     return proxy->ResetPolicies(simId);
282 }
283 
SetBackgroundPolicy(bool isBackgroundPolicyAllow)284 int32_t NetPolicyClient::SetBackgroundPolicy(bool isBackgroundPolicyAllow)
285 {
286     sptr<INetPolicyService> proxy = GetProxy();
287     if (proxy == nullptr) {
288         NETMGR_LOG_E("proxy is nullptr");
289         return NETMANAGER_ERR_GET_PROXY_FAIL;
290     }
291 
292     return proxy->SetBackgroundPolicy(isBackgroundPolicyAllow);
293 }
294 
GetBackgroundPolicy(bool & backgroundPolicy)295 int32_t NetPolicyClient::GetBackgroundPolicy(bool &backgroundPolicy)
296 {
297     sptr<INetPolicyService> proxy = GetProxy();
298     if (proxy == nullptr) {
299         NETMGR_LOG_E("proxy is nullptr");
300         return NETMANAGER_ERR_GET_PROXY_FAIL;
301     }
302 
303     return proxy->GetBackgroundPolicy(backgroundPolicy);
304 }
305 
GetBackgroundPolicyByUid(uint32_t uid,uint32_t & backgroundPolicyOfUid)306 int32_t NetPolicyClient::GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid)
307 {
308     sptr<INetPolicyService> proxy = GetProxy();
309     if (proxy == nullptr) {
310         NETMGR_LOG_E("proxy is nullptr");
311         return NETMANAGER_ERR_GET_PROXY_FAIL;
312     }
313     return proxy->GetBackgroundPolicyByUid(uid, backgroundPolicyOfUid);
314 }
315 
SetSnoozePolicy(int8_t netType,const std::string & simId)316 NetPolicyResultCode NetPolicyClient::SetSnoozePolicy(int8_t netType, const std::string &simId)
317 {
318     return static_cast<NetPolicyResultCode>(UpdateRemindPolicy(netType, simId, RemindType::REMIND_TYPE_LIMIT));
319 }
320 
UpdateRemindPolicy(int32_t netType,const std::string & simId,uint32_t remindType)321 int32_t NetPolicyClient::UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType)
322 {
323     sptr<INetPolicyService> proxy = GetProxy();
324     if (proxy == nullptr) {
325         NETMGR_LOG_E("proxy is nullptr");
326         return NETMANAGER_ERR_GET_PROXY_FAIL;
327     }
328 
329     return proxy->UpdateRemindPolicy(netType, simId, remindType);
330 }
331 
SetIdleTrustlist(uint32_t uid,bool isTrustlist)332 NetPolicyResultCode NetPolicyClient::SetIdleTrustlist(uint32_t uid, bool isTrustlist)
333 {
334     return static_cast<NetPolicyResultCode>(SetDeviceIdleTrustlist({uid}, isTrustlist));
335 }
336 
SetDeviceIdleTrustlist(const std::vector<uint32_t> & uids,bool isAllowed)337 int32_t NetPolicyClient::SetDeviceIdleTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)
338 {
339     sptr<INetPolicyService> proxy = GetProxy();
340     if (proxy == nullptr) {
341         NETMGR_LOG_E("proxy is nullptr");
342         return NETMANAGER_ERR_GET_PROXY_FAIL;
343     }
344 
345     return proxy->SetDeviceIdleTrustlist(uids, isAllowed);
346 }
347 
GetIdleTrustlist(std::vector<uint32_t> & uids)348 NetPolicyResultCode NetPolicyClient::GetIdleTrustlist(std::vector<uint32_t> &uids)
349 {
350     return static_cast<NetPolicyResultCode>(GetDeviceIdleTrustlist(uids));
351 }
352 
GetDeviceIdleTrustlist(std::vector<uint32_t> & uids)353 int32_t NetPolicyClient::GetDeviceIdleTrustlist(std::vector<uint32_t> &uids)
354 {
355     sptr<INetPolicyService> proxy = GetProxy();
356     if (proxy == nullptr) {
357         NETMGR_LOG_E("proxy is nullptr");
358         return NETMANAGER_ERR_GET_PROXY_FAIL;
359     }
360 
361     return proxy->GetDeviceIdleTrustlist(uids);
362 }
363 
SetDeviceIdlePolicy(bool enable)364 int32_t NetPolicyClient::SetDeviceIdlePolicy(bool enable)
365 {
366     sptr<INetPolicyService> proxy = GetProxy();
367     if (proxy == nullptr) {
368         NETMGR_LOG_E("proxy is nullptr");
369         return NETMANAGER_ERR_GET_PROXY_FAIL;
370     }
371 
372     return proxy->SetDeviceIdlePolicy(enable);
373 }
374 
GetPowerSaveTrustlist(std::vector<uint32_t> & uids)375 int32_t NetPolicyClient::GetPowerSaveTrustlist(std::vector<uint32_t> &uids)
376 {
377     sptr<INetPolicyService> proxy = GetProxy();
378     if (proxy == nullptr) {
379         NETMGR_LOG_E("proxy is nullptr");
380         return NETMANAGER_ERR_GET_PROXY_FAIL;
381     }
382 
383     return proxy->GetPowerSaveTrustlist(uids);
384 }
385 
SetPowerSaveTrustlist(const std::vector<uint32_t> & uids,bool isAllowed)386 int32_t NetPolicyClient::SetPowerSaveTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)
387 {
388     sptr<INetPolicyService> proxy = GetProxy();
389     if (proxy == nullptr) {
390         NETMGR_LOG_E("proxy is nullptr");
391         return NETMANAGER_ERR_GET_PROXY_FAIL;
392     }
393 
394     return proxy->SetPowerSaveTrustlist(uids, isAllowed);
395 }
396 
SetPowerSavePolicy(bool enable)397 int32_t NetPolicyClient::SetPowerSavePolicy(bool enable)
398 {
399     sptr<INetPolicyService> proxy = GetProxy();
400     if (proxy == nullptr) {
401         NETMGR_LOG_E("proxy is nullptr");
402         return NETMANAGER_ERR_GET_PROXY_FAIL;
403     }
404 
405     return proxy->SetPowerSavePolicy(enable);
406 }
407 
CheckPermission()408 int32_t NetPolicyClient::CheckPermission()
409 {
410     sptr<INetPolicyService> proxy = GetProxy();
411     if (proxy == nullptr) {
412         NETMGR_LOG_E("proxy is nullptr");
413         return NETMANAGER_ERR_GET_PROXY_FAIL;
414     }
415 
416     return proxy->CheckPermission();
417 }
418 
SetNetworkAccessPolicy(uint32_t uid,NetworkAccessPolicy policy,bool reconfirmFlag)419 int32_t NetPolicyClient::SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag)
420 {
421     sptr<INetPolicyService> proxy = GetProxy();
422     if (proxy == nullptr) {
423         NETMGR_LOG_E("proxy is nullptr");
424         return NETMANAGER_ERR_GET_PROXY_FAIL;
425     }
426 
427     return proxy->SetNetworkAccessPolicy(uid, policy, reconfirmFlag);
428 }
429 
GetNetworkAccessPolicy(AccessPolicyParameter parameter,AccessPolicySave & policy)430 int32_t NetPolicyClient::GetNetworkAccessPolicy(AccessPolicyParameter parameter, AccessPolicySave& policy)
431 {
432     sptr<INetPolicyService> proxy = GetProxy();
433     if (proxy == nullptr) {
434         NETMGR_LOG_E("proxy is nullptr");
435         return NETMANAGER_ERR_GET_PROXY_FAIL;
436     }
437     return proxy->GetNetworkAccessPolicy(parameter, policy);
438 }
439 
NotifyNetAccessPolicyDiag(uint32_t uid)440 int32_t NetPolicyClient::NotifyNetAccessPolicyDiag(uint32_t uid)
441 {
442     sptr<INetPolicyService> proxy = GetProxy();
443     if (proxy == nullptr) {
444         NETMGR_LOG_E("proxy is nullptr");
445         return NETMANAGER_ERR_GET_PROXY_FAIL;
446     }
447 
448     return proxy->NotifyNetAccessPolicyDiag(uid);
449 }
450 
SetNicTrafficAllowed(const std::vector<std::string> & ifaceNames,bool status)451 int32_t NetPolicyClient::SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status)
452 {
453     sptr<INetPolicyService> proxy = GetProxy();
454     if (proxy == nullptr) {
455         NETMGR_LOG_E("proxy is nullptr");
456         return NETMANAGER_ERR_GET_PROXY_FAIL;
457     }
458 
459     return proxy->SetNicTrafficAllowed(ifaceNames, status);
460 }
461 } // namespace NetManagerStandard
462 } // namespace OHOS
463