• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "singleton.h"
20 
21 #include "i_net_policy_service.h"
22 #include "net_policy_cellular_policy.h"
23 #include "net_policy_constants.h"
24 #include "net_policy_quota_policy.h"
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 class NetPolicyClient {
29     DECLARE_DELAYED_SINGLETON(NetPolicyClient)
30 
31 public:
32     /**
33      * @brief The interface is set uid policy
34      *
35      * @param uid uid
36      * @param policy policy
37      *
38      * @return Returns 0 success, otherwise fail
39      */
40     NetPolicyResultCode SetPolicyByUid(uint32_t uid, NetUidPolicy policy);
41     /**
42      * @brief The interface is get uid policy
43      *
44      * @param uid uid
45      * @return Returns NetUidPolicy, otherwise fail
46      */
47     NetUidPolicy GetPolicyByUid(uint32_t uid);
48     /**
49      * @brief The interface is get uids by policy
50      *
51      * @param policy policy
52      * @return uids
53      */
54     std::vector<uint32_t> GetUidsByPolicy(NetUidPolicy policy);
55     /**
56      * @brief The interface determine whether you have access to the network
57      *
58      * @param uid uid
59      * @param metered true/false
60      * @return bool
61      */
62     bool IsUidNetAccess(uint32_t uid, bool metered);
63     /**
64      * @brief The interface determine whether you have access to the network
65      *
66      * @param uid uid
67      * @param ifaceName , you can get ifacename by GetIfaceNameByType
68      * @return bool
69      */
70     bool IsUidNetAccess(uint32_t uid, const std::string &ifaceName);
71     /**
72      * @brief Register net policy callback
73      *
74      * @param callback The callback of INetPolicyCallback interface
75      *
76      * @return Returns 0, successfully register net policy callback, otherwise it will failed
77      */
78     int32_t RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback);
79     /**
80      * @brief Unregister net policy callback
81      *
82      * @return Returns 0, successfully unregister net policy callback, otherwise it will fail
83      */
84     int32_t UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback);
85     /**
86      * @brief SetNetQuotaPolicies set policys by NetPolicyQuotaPolicy
87      *
88      * @return Returns 0, successfully
89      */
90     NetPolicyResultCode SetNetQuotaPolicies(const std::vector<NetPolicyQuotaPolicy> &quotaPolicies);
91     /**
92      * @brief GetNetQuotaPolicies get policys for NetPolicyQuotaPolicy
93      *
94      * @return Returns 0, successfully
95      */
96     NetPolicyResultCode GetNetQuotaPolicies(std::vector<NetPolicyQuotaPolicy> &quotaPolicies);
97     /**
98      * @brief SetCellularPolicies set policys by NetPolicyCellularPolicy
99      *
100      * @return Returns 0, successfully
101      */
102     NetPolicyResultCode SetCellularPolicies(const std::vector<NetPolicyCellularPolicy> &cellularPolicies);
103     /**
104      * @brief GetCellularPolicies get policys for NetPolicyCellularPolicy
105      *
106      * @return Returns 0, successfully
107      */
108     NetPolicyResultCode GetCellularPolicies(std::vector<NetPolicyCellularPolicy> &cellularPolicies);
109     /**
110      * @brief SetFactoryPolicy reset policys for simId
111      *
112      * @param simId ID, get from telephone module
113      * @return Returns 0, successfully
114      */
115     NetPolicyResultCode SetFactoryPolicy(const std::string &simId);
116     /**
117      * @brief SetBackgroundPolicy reset backgroundpolicy for all app
118      *
119      * @param backgroundPolicy refuse app visit network
120      * @return Returns 0, successfully
121      */
122     NetPolicyResultCode SetBackgroundPolicy(bool isBackgroundPolicyAllow);
123     /**
124      * @brief GetBackgroundPolicy get background policy
125      *
126      * @return bool
127      */
128     bool GetBackgroundPolicy();
129     /**
130      * @brief GetBackgroundPolicyByUid get background policy by uid
131      *
132      * @param uid uid
133      * @return bool
134      */
135     bool GetBackgroundPolicyByUid(uint32_t uid);
136     /**
137      * @brief GetCurrentBackgroundPolicy get background policy by current
138      *
139      * @return Returns NetBackgroundPolicy
140      */
141     NetBackgroundPolicy GetCurrentBackgroundPolicy();
142     /**
143      * @brief SetSnoozePolicy for Hibernate current policy
144      *
145      * @return Returns 0, successfully
146      */
147     NetPolicyResultCode SetSnoozePolicy(int8_t netType, const std::string &simId);
148     /**
149      * @brief SetIdleTrustlist for add trust list for Idle status
150      *
151      * @param uid uid
152      * @param isTrustlist true/false
153      *
154      * @return Returns 0, successfully
155      */
156     NetPolicyResultCode SetIdleTrustlist(uint32_t uid, bool isTrustlist);
157     /**
158      * @brief GetIdleTrustlist for get trust list for Idle status
159      *
160      * @param uid uid
161      * @param uids
162      *
163      * @return Returns 0, successfully
164      */
165     NetPolicyResultCode GetIdleTrustlist(std::vector<uint32_t> &uids);
166 
167 private:
168     class NetPolicyDeathRecipient : public IRemoteObject::DeathRecipient {
169     public:
NetPolicyDeathRecipient(NetPolicyClient & client)170         explicit NetPolicyDeathRecipient(NetPolicyClient &client) : client_(client) {}
171         ~NetPolicyDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)172         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
173         {
174             client_.OnRemoteDied(remote);
175         }
176 
177     private:
178         NetPolicyClient &client_;
179     };
180 
181 private:
182     sptr<INetPolicyService> GetProxy();
183     void OnRemoteDied(const wptr<IRemoteObject> &remote);
184 
185 private:
186     std::mutex mutex_;
187     sptr<INetPolicyService> netPolicyService_;
188     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
189 };
190 } // namespace NetManagerStandard
191 } // namespace OHOS
192 #endif // NET_POLICY_CLIENT_H
193