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 #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 : public Singleton<NetPolicyClient> { 30 public: 31 NetPolicyClient(); 32 ~NetPolicyClient(); 33 34 /** 35 * Set the network policy for the specified UID. 36 * 37 * @param uid The specified UID of app. 38 * @param policy The network policy for application, 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, for details, see {@link NetUidPolicy}. 60 * @param uids put the result into uids 61 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 62 * @permission ohos.permission.CONNECTIVITY_INTERNAL 63 * @systemapi Hide this for inner system use. 64 */ 65 [[nodiscard]] int32_t GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids); 66 67 /** 68 * Get the status whether the specified uid app can access the metered network or non-metered network. 69 * 70 * @param uid The specified UID of application. 71 * @param metered Indicates meterd network or non-metered network. 72 * @param isAllowed Put the result into "isAllowed". 73 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 74 * @permission ohos.permission.CONNECTIVITY_INTERNAL 75 * @systemapi Hide this for inner system use. 76 */ 77 [[nodiscard]] int32_t IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed); 78 79 /** 80 * Get the status whether the specified uid app can access the specified iface network. 81 * 82 * @param uid The specified UID of application. 83 * @param ifaceName Iface name. 84 * @param isAllowed Put the result into "isAllowed". 85 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 86 * @permission ohos.permission.CONNECTIVITY_INTERNAL 87 * @systemapi Hide this for inner system use. 88 */ 89 [[nodiscard]] int32_t IsUidNetAllowed(uint32_t uid, const std::string &ifaceName, bool &isAllowed); 90 91 /** 92 * @deprecated 93 */ 94 [[nodiscard]] int32_t IsUidNetAccess(uint32_t uid, bool isMetered, bool &isAllowed); 95 96 /** 97 * @deprecated 98 */ 99 [[nodiscard]] int32_t IsUidNetAccess(uint32_t uid, const std::string &ifaceName, bool &isAllowed); 100 101 /** 102 * Register network policy change callback. 103 * 104 * @param callback The callback of INetPolicyCallback interface. 105 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 106 * @permission ohos.permission.CONNECTIVITY_INTERNAL 107 * @systemapi Hide this for inner system use. 108 */ 109 [[nodiscard]] int32_t RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback); 110 111 /** 112 * Unregister network policy change callback. 113 * 114 * @param callback The callback of INetPolicyCallback interface. 115 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 116 * @permission ohos.permission.CONNECTIVITY_INTERNAL 117 * @systemapi Hide this for inner system use. 118 */ 119 [[nodiscard]] int32_t UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback); 120 121 /** 122 * Set network policies. 123 * 124 * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}. 125 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 126 * @permission ohos.permission.CONNECTIVITY_INTERNAL 127 * @systemapi Hide this for inner system use. 128 */ 129 [[nodiscard]] int32_t SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> "aPolicies); 130 131 /** 132 * Get network policies. 133 * 134 * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}. 135 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 136 * @permission ohos.permission.CONNECTIVITY_INTERNAL 137 * @systemapi Hide this for inner system use. 138 */ 139 [[nodiscard]] int32_t GetNetQuotaPolicies(std::vector<NetQuotaPolicy> "aPolicies); 140 141 /** 142 * SetFactoryPolicy reset policys for simId. 143 * 144 * @param simId ID, get from telephone module 145 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 146 * @permission ohos.permission.CONNECTIVITY_INTERNAL 147 * @systemapi Hide this for inner system use. 148 * @deprecated 149 */ 150 NetPolicyResultCode SetFactoryPolicy(const std::string &simId); 151 152 /** 153 * Reset network policies\rules\quota policies\firewall rules. 154 * 155 * @param simId Specify the matched simId of quota policy. 156 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 157 * @permission ohos.permission.CONNECTIVITY_INTERNAL 158 * @systemapi Hide this for inner system use. 159 */ 160 [[nodiscard]] int32_t ResetPolicies(const std::string &simId); 161 162 /** 163 * Control if apps can use data on background. 164 * 165 * @param isAllowed Allow apps to use data on background or not. 166 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 167 * @permission ohos.permission.CONNECTIVITY_INTERNAL 168 * @systemapi Hide this for inner system use. 169 */ 170 [[nodiscard]] int32_t SetBackgroundPolicy(bool isAllowed); 171 172 /** 173 * Get the status if apps can use data on background. 174 * 175 * @param backgroundPolicy Put the background policy's value 176 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 177 * @permission ohos.permission.CONNECTIVITY_INTERNAL 178 * @systemapi Hide this for inner system use. 179 */ 180 [[nodiscard]] int32_t GetBackgroundPolicy(bool &backgroundPolicy); 181 182 /** 183 * Get the background network restriction policy for the specified uid. 184 * 185 * @param uid The specified UID of application. 186 * @param backgroundPolicyOfUid The result of this uid's background policy,{@link NetBackgroundPolicy} 187 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 188 * @permission ohos.permission.CONNECTIVITY_INTERNAL 189 * @systemapi Hide this for inner system use. 190 */ 191 [[nodiscard]] int32_t GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid); 192 193 /** 194 * SetSnoozePolicy for Hibernate current policy 195 * 196 * @param netType {@link NetBearType}. 197 * @param simId Specify the matched simId of quota policy when netType is cellular. 198 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 199 * @permission ohos.permission.CONNECTIVITY_INTERNAL 200 * @systemapi Hide this for inner system use. 201 * @deprecated 202 */ 203 NetPolicyResultCode SetSnoozePolicy(int8_t netType, const std::string &simId); 204 205 /** 206 * Update the limit or warning remind time of quota policy. 207 * 208 * @param netType {@link NetBearType}. 209 * @param simId Specify the matched simId of quota policy when netType is cellular. 210 * @param remindType {@link RemindType}. 211 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 212 * @permission ohos.permission.CONNECTIVITY_INTERNAL 213 * @systemapi Hide this for inner system use. 214 */ 215 [[nodiscard]] int32_t UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType); 216 217 /** 218 * SetIdleTrustlist for add trust list for Idle status 219 * 220 * @param uid uid 221 * @param isTrustlist true/false 222 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 223 * @permission ohos.permission.CONNECTIVITY_INTERNAL 224 * @systemapi Hide this for inner system use. 225 * @deprecated 226 */ 227 NetPolicyResultCode SetIdleTrustlist(uint32_t uid, bool isTrustlist); 228 229 /** 230 * Set the UID into device idle allow list. 231 * 232 * @param uid The specified UID of application. 233 * @param isAllowed The UID is into allow list or not. 234 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 235 * @permission ohos.permission.CONNECTIVITY_INTERNAL 236 * @systemapi Hide this for inner system use. 237 */ 238 [[nodiscard]] int32_t SetDeviceIdleTrustlist(const std::vector<uint32_t> &uid, bool isAllowed); 239 240 /** 241 * GetIdleTrustlist for get trust list for Idle status 242 * 243 * @param uids The uids are into into allow list 244 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 245 * @permission ohos.permission.CONNECTIVITY_INTERNAL 246 * @systemapi Hide this for inner system use. 247 * @deprecated 248 */ 249 NetPolicyResultCode GetIdleTrustlist(std::vector<uint32_t> &uids); 250 251 /** 252 * Get the allow list of UID in device idle mode. 253 * 254 * @param uids The list of UIDs 255 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 256 * @permission ohos.permission.CONNECTIVITY_INTERNAL 257 * @systemapi Hide this for inner system use. 258 */ 259 [[nodiscard]] int32_t GetDeviceIdleTrustlist(std::vector<uint32_t> &uids); 260 261 /** 262 * Process network policy in device idle mode. 263 * 264 * @param enable Device idle mode is open or not. 265 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 266 * @permission ohos.permission.CONNECTIVITY_INTERNAL 267 * @systemapi Hide this for inner system use. 268 */ 269 int32_t SetDeviceIdlePolicy(bool enable); 270 271 /** 272 * Get the allow list of UID in power save mode. 273 * 274 * @param uids The list of UIDs 275 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 276 * @permission ohos.permission.CONNECTIVITY_INTERNAL 277 * @systemapi Hide this for inner system use. 278 */ 279 [[nodiscard]] int32_t GetPowerSaveTrustlist(std::vector<uint32_t> &uids); 280 281 /** 282 * Get the allow list of UID in power save mode. 283 * 284 * @param uid The list of UIDs 285 * @param isAllowed The UID is into allow list or not. 286 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 287 * @permission ohos.permission.CONNECTIVITY_INTERNAL 288 * @systemapi Hide this for inner system use. 289 */ 290 [[nodiscard]] int32_t SetPowerSaveTrustlist(const std::vector<uint32_t> &uid, bool isAllowed); 291 292 /** 293 * Set the Power Save Policy object 294 * 295 * @param enable Power save mode is open or not. 296 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 297 * @permission ohos.permission.CONNECTIVITY_INTERNAL 298 * @systemapi Hide this for inner system use. 299 */ 300 [[nodiscard]] int32_t SetPowerSavePolicy(bool enable); 301 302 /** 303 * Check if you have permission 304 * 305 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 306 */ 307 [[nodiscard]] int32_t CheckPermission(); 308 309 /** 310 * Set the policy to access the network of the specified application. 311 * 312 * @param uid The specified UID of application. 313 * @param policy The network access policy of application, {@link NetworkAccessPolicy}. 314 * @param reconfirmFlag true means a reconfirm diaglog trigger while policy deny network access. 315 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 316 * @permission ohos.permission.CONNECTIVITY_INTERNAL 317 * @systemapi Hide this for inner system use. 318 */ 319 int32_t SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag); 320 321 /** 322 * Query the network access policy of the specified application or all applications. 323 * 324 * @param parameter Indicate to get all or an application network access policy, {@link AccessPolicyParameter}. 325 * @param policy The network access policy of application, {@link AccessPolicySave}. 326 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 327 * @permission ohos.permission.CONNECTIVITY_INTERNAL 328 * @systemapi Hide this for inner system use. 329 */ 330 int32_t GetNetworkAccessPolicy(AccessPolicyParameter parameter, AccessPolicySave& policy); 331 332 int32_t NotifyNetAccessPolicyDiag(uint32_t uid); 333 334 /** 335 * Set NIC Traffic allowed or disallowed 336 * 337 * @param ifaceNames ifaceNames 338 * @param status true for allowed, false for disallowed 339 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 340 */ 341 int32_t SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status); 342 343 private: 344 class NetPolicyDeathRecipient : public IRemoteObject::DeathRecipient { 345 public: NetPolicyDeathRecipient(NetPolicyClient & client)346 explicit NetPolicyDeathRecipient(NetPolicyClient &client) : client_(client) {} 347 ~NetPolicyDeathRecipient() override = default; OnRemoteDied(const wptr<IRemoteObject> & remote)348 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 349 { 350 client_.OnRemoteDied(remote); 351 } 352 353 private: 354 NetPolicyClient &client_; 355 }; 356 357 private: 358 sptr<INetPolicyService> GetProxy(); 359 void RecoverCallback(); 360 void OnRemoteDied(const wptr<IRemoteObject> &remote); 361 362 private: 363 std::mutex mutex_; 364 sptr<INetPolicyService> netPolicyService_; 365 sptr<IRemoteObject::DeathRecipient> deathRecipient_; 366 sptr<INetPolicyCallback> callback_; 367 }; 368 } // namespace NetManagerStandard 369 } // namespace OHOS 370 #endif // NET_POLICY_CLIENT_H 371