• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef NET_POLICY_CLIENT_H
17 #define NET_POLICY_CLIENT_H
18 
19 #include <map>
20 
21 #include "singleton.h"
22 
23 #include "i_net_policy_service.h"
24 #include "net_policy_constants.h"
25 #include "net_quota_policy.h"
26 
27 namespace OHOS {
28 namespace NetManagerStandard {
29 class NetPolicyClient {
30     DECLARE_DELAYED_SINGLETON(NetPolicyClient)
31 
32 public:
33     /**
34      * Set the network policy for the specified UID.
35      *
36      * @param uid The specified UID of app.
37      * @param policy The network policy for application.
38      *      For details, see {@link NetUidPolicy}.
39      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
40      * @permission ohos.permission.CONNECTIVITY_INTERNAL
41      * @systemapi Hide this for inner system use.
42      */
43     [[nodiscard]] int32_t SetPolicyByUid(uint32_t uid, uint32_t policy);
44 
45     /**
46      * Get the network policy of the specified UID.
47      *
48      * @param uid The specified UID of app.
49      * @param policy The network policy of the specified UID application, for details, see {@link NetUidPolicy}.
50      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
51      * @permission ohos.permission.CONNECTIVITY_INTERNAL
52      * @systemapi Hide this for inner system use.
53      */
54     [[nodiscard]] int32_t GetPolicyByUid(uint32_t uid, uint32_t &policy);
55 
56     /**
57      * Get the application UIDs of the specified policy.
58      *
59      * @param policy the network policy of the current UID of application.
60      *      For details, see {@link NetUidPolicy}.
61      * @param uids put the result into uids
62      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
63      * @permission ohos.permission.CONNECTIVITY_INTERNAL
64      * @systemapi Hide this for inner system use.
65      */
66     [[nodiscard]] int32_t GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids);
67 
68     /**
69      * Get the status whether the specified uid app can access the metered network or non-metered network.
70      *
71      * @param uid The specified UID of application.
72      * @param metered Indicates meterd network or non-metered network.
73      * @param isAllowed Put the result into "isAllowed".
74      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
75      * @permission ohos.permission.CONNECTIVITY_INTERNAL
76      * @systemapi Hide this for inner system use.
77      */
78     [[nodiscard]] int32_t IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed);
79 
80     /**
81      * Get the status whether the specified uid app can access the specified iface network.
82      *
83      * @param uid The specified UID of application.
84      * @param ifaceName Iface name.
85      * @param isAllowed Put the result into "isAllowed".
86      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
87      * @permission ohos.permission.CONNECTIVITY_INTERNAL
88      * @systemapi Hide this for inner system use.
89      */
90     [[nodiscard]] int32_t IsUidNetAllowed(uint32_t uid, const std::string &ifaceName, bool &isAllowed);
91 
92     /**
93      * @deprecated
94      */
95     [[nodiscard]] int32_t IsUidNetAccess(uint32_t uid, bool isMetered, bool &isAllowed);
96 
97     /**
98      * @deprecated
99      */
100     [[nodiscard]] int32_t IsUidNetAccess(uint32_t uid, const std::string &ifaceName, bool &isAllowed);
101 
102     /**
103      * Register network policy change callback.
104      *
105      * @param callback The callback of INetPolicyCallback interface.
106      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
107      * @permission ohos.permission.CONNECTIVITY_INTERNAL
108      * @systemapi Hide this for inner system use.
109      */
110     [[nodiscard]] int32_t RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback);
111 
112     /**
113      * Unregister network policy change callback.
114      *
115      * @param callback The callback of INetPolicyCallback interface.
116      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
117      * @permission ohos.permission.CONNECTIVITY_INTERNAL
118      * @systemapi Hide this for inner system use.
119      */
120     [[nodiscard]] int32_t UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback);
121 
122     /**
123      * Set network policies.
124      *
125      * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}.
126      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
127      * @permission ohos.permission.CONNECTIVITY_INTERNAL
128      * @systemapi Hide this for inner system use.
129      */
130     [[nodiscard]] int32_t SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies);
131 
132     /**
133      * Get network policies.
134      *
135      * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}.
136      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
137      * @permission ohos.permission.CONNECTIVITY_INTERNAL
138      * @systemapi Hide this for inner system use.
139      */
140     [[nodiscard]] int32_t GetNetQuotaPolicies(std::vector<NetQuotaPolicy> &quotaPolicies);
141 
142     /**
143      * SetFactoryPolicy reset policys for iccid.
144      *
145      * @param iccid ID, get from telephone module
146      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
147      * @permission ohos.permission.CONNECTIVITY_INTERNAL
148      * @systemapi Hide this for inner system use.
149      * @deprecated
150      */
151     NetPolicyResultCode SetFactoryPolicy(const std::string &iccid);
152 
153     /**
154      * Reset network policies\rules\quota policies\firewall rules.
155      *
156      * @param iccid Specify the matched iccid of quota policy.
157      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
158      * @permission ohos.permission.CONNECTIVITY_INTERNAL
159      * @systemapi Hide this for inner system use.
160      */
161     [[nodiscard]] int32_t ResetPolicies(const std::string &iccid);
162 
163     /**
164      * Control if apps can use data on background.
165      *
166      * @param isAllowed Allow apps to use data on background.
167      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
168      * @permission ohos.permission.CONNECTIVITY_INTERNAL
169      * @systemapi Hide this for inner system use.
170      */
171     [[nodiscard]] int32_t SetBackgroundPolicy(bool isAllowed);
172 
173     /**
174      * Get the status if apps can use data on background.
175      *
176      * @param backgroundPolicy Put the background policy's value
177      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
178      * @permission ohos.permission.CONNECTIVITY_INTERNAL
179      * @systemapi Hide this for inner system use.
180      */
181     [[nodiscard]] int32_t GetBackgroundPolicy(bool &backgroundPolicy);
182 
183     /**
184      * Get the background network restriction policy for the specified uid.
185      *
186      * @param uid The specified UID of application.
187      * @param backgroundPolicyOfUid The result of this uid's background policy,{@link NetBackgroundPolicy}
188      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
189      * @permission ohos.permission.CONNECTIVITY_INTERNAL
190      * @systemapi Hide this for inner system use.
191      */
192     [[nodiscard]] int32_t GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid);
193 
194     /**
195      * SetSnoozePolicy for Hibernate current policy
196      *
197      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
198      * @permission ohos.permission.CONNECTIVITY_INTERNAL
199      * @systemapi Hide this for inner system use.
200      * @deprecated
201      */
202     NetPolicyResultCode SetSnoozePolicy(int8_t netType, const std::string &iccid);
203 
204     /**
205      * Update the limit or warning remind time of quota policy.
206      *
207      * @param netType {@link NetBearType}.
208      * @param iccid Specify the matched iccid of quota policy when netType is cellular.
209      * @param remindType {@link RemindType}.
210      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
211      * @permission ohos.permission.CONNECTIVITY_INTERNAL
212      * @systemapi Hide this for inner system use.
213      */
214     [[nodiscard]] int32_t UpdateRemindPolicy(int32_t netType, const std::string &iccid, uint32_t remindType);
215 
216     /**
217      * SetIdleTrustlist for add trust list for Idle status
218      *
219      * @param uid uid
220      * @param isTrustlist true/false
221      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
222      * @permission ohos.permission.CONNECTIVITY_INTERNAL
223      * @systemapi Hide this for inner system use.
224      * @deprecated
225      */
226     NetPolicyResultCode SetIdleTrustlist(uint32_t uid, bool isTrustlist);
227 
228     /**
229      * Set the UID into device idle allow list.
230      *
231      * @param uid The specified UID of application.
232      * @param isAllowed The UID is into allow list or not.
233      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
234      * @permission ohos.permission.CONNECTIVITY_INTERNAL
235      * @systemapi Hide this for inner system use.
236      */
237     [[nodiscard]] int32_t SetDeviceIdleAllowedList(uint32_t uid, bool isAllowed);
238 
239     /**
240      * GetIdleTrustlist for get trust list for Idle status
241      *
242      * @param uids The uids are into into allow list
243      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
244      * @deprecated
245      */
246     NetPolicyResultCode GetIdleTrustlist(std::vector<uint32_t> &uids);
247 
248     /**
249      * Get the allow list of UID in device idle mode.
250      *
251      * @param uids The list of UIDs
252      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
253      * @permission ohos.permission.CONNECTIVITY_INTERNAL
254      * @systemapi Hide this for inner system use.
255      */
256     [[nodiscard]] int32_t GetDeviceIdleAllowedList(std::vector<uint32_t> &uids);
257 
258     /**
259      * Process network policy in device idle mode.
260      *
261      * @param enable Device idle mode is open or not.
262      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
263      * @permission ohos.permission.CONNECTIVITY_INTERNAL
264      * @systemapi Hide this for inner system use.
265      */
266     int32_t SetDeviceIdlePolicy(bool enable);
267 
268 private:
269     class NetPolicyDeathRecipient : public IRemoteObject::DeathRecipient {
270     public:
NetPolicyDeathRecipient(NetPolicyClient & client)271         explicit NetPolicyDeathRecipient(NetPolicyClient &client) : client_(client) {}
272         ~NetPolicyDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)273         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
274         {
275             client_.OnRemoteDied(remote);
276         }
277 
278     private:
279         NetPolicyClient &client_;
280     };
281 
282 private:
283     sptr<INetPolicyService> GetProxy();
284     void OnRemoteDied(const wptr<IRemoteObject> &remote);
285 
286 private:
287     std::mutex mutex_;
288     sptr<INetPolicyService> netPolicyService_;
289     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
290 };
291 } // namespace NetManagerStandard
292 } // namespace OHOS
293 #endif // NET_POLICY_CLIENT_H
294