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