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