/* * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef NET_POLICY_CLIENT_H #define NET_POLICY_CLIENT_H #include #include "singleton.h" #include "i_net_policy_service.h" #include "net_policy_constants.h" #include "net_quota_policy.h" namespace OHOS { namespace NetManagerStandard { class NetPolicyClient : public Singleton { public: NetPolicyClient(); ~NetPolicyClient(); /** * Set the network policy for the specified UID. * * @param uid The specified UID of app. * @param policy The network policy for application, for details, see {@link NetUidPolicy}. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t SetPolicyByUid(uint32_t uid, uint32_t policy); /** * Get the network policy of the specified UID. * * @param uid The specified UID of app. * @param policy The network policy of the specified UID application, for details, see {@link NetUidPolicy}. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t GetPolicyByUid(uint32_t uid, uint32_t &policy); /** * Get the application UIDs of the specified policy. * * @param policy the network policy of the current UID of application, for details, see {@link NetUidPolicy}. * @param uids put the result into uids * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t GetUidsByPolicy(uint32_t policy, std::vector &uids); /** * Get the status whether the specified uid app can access the metered network or non-metered network. * * @param uid The specified UID of application. * @param metered Indicates meterd network or non-metered network. * @param isAllowed Put the result into "isAllowed". * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed); /** * Get the status whether the specified uid app can access the specified iface network. * * @param uid The specified UID of application. * @param ifaceName Iface name. * @param isAllowed Put the result into "isAllowed". * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t IsUidNetAllowed(uint32_t uid, const std::string &ifaceName, bool &isAllowed); /** * @deprecated */ [[nodiscard]] int32_t IsUidNetAccess(uint32_t uid, bool isMetered, bool &isAllowed); /** * @deprecated */ [[nodiscard]] int32_t IsUidNetAccess(uint32_t uid, const std::string &ifaceName, bool &isAllowed); /** * Register network policy change callback. * * @param callback The callback of INetPolicyCallback interface. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t RegisterNetPolicyCallback(const sptr &callback); /** * Unregister network policy change callback. * * @param callback The callback of INetPolicyCallback interface. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t UnregisterNetPolicyCallback(const sptr &callback); /** * Set network policies. * * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t SetNetQuotaPolicies(const std::vector "aPolicies); /** * Get network policies. * * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t GetNetQuotaPolicies(std::vector "aPolicies); /** * SetFactoryPolicy reset policys for simId. * * @param simId ID, get from telephone module * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. * @deprecated */ NetPolicyResultCode SetFactoryPolicy(const std::string &simId); /** * Reset network policies\rules\quota policies\firewall rules. * * @param simId Specify the matched simId of quota policy. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t ResetPolicies(const std::string &simId); /** * Control if apps can use data on background. * * @param isAllowed Allow apps to use data on background or not. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t SetBackgroundPolicy(bool isAllowed); /** * Get the status if apps can use data on background. * * @param backgroundPolicy Put the background policy's value * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t GetBackgroundPolicy(bool &backgroundPolicy); /** * Get the background network restriction policy for the specified uid. * * @param uid The specified UID of application. * @param backgroundPolicyOfUid The result of this uid's background policy,{@link NetBackgroundPolicy} * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid); /** * SetSnoozePolicy for Hibernate current policy * * @param netType {@link NetBearType}. * @param simId Specify the matched simId of quota policy when netType is cellular. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. * @deprecated */ NetPolicyResultCode SetSnoozePolicy(int8_t netType, const std::string &simId); /** * Update the limit or warning remind time of quota policy. * * @param netType {@link NetBearType}. * @param simId Specify the matched simId of quota policy when netType is cellular. * @param remindType {@link RemindType}. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType); /** * SetIdleTrustlist for add trust list for Idle status * * @param uid uid * @param isTrustlist true/false * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. * @deprecated */ NetPolicyResultCode SetIdleTrustlist(uint32_t uid, bool isTrustlist); /** * Set the UID into device idle allow list. * * @param uid The specified UID of application. * @param isAllowed The UID is into allow list or not. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t SetDeviceIdleTrustlist(const std::vector &uid, bool isAllowed); /** * GetIdleTrustlist for get trust list for Idle status * * @param uids The uids are into into allow list * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. * @deprecated */ NetPolicyResultCode GetIdleTrustlist(std::vector &uids); /** * Get the allow list of UID in device idle mode. * * @param uids The list of UIDs * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t GetDeviceIdleTrustlist(std::vector &uids); /** * Process network policy in device idle mode. * * @param enable Device idle mode is open or not. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ int32_t SetDeviceIdlePolicy(bool enable); /** * Get the allow list of UID in power save mode. * * @param uids The list of UIDs * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t GetPowerSaveTrustlist(std::vector &uids); /** * Get the allow list of UID in power save mode. * * @param uid The list of UIDs * @param isAllowed The UID is into allow list or not. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t SetPowerSaveTrustlist(const std::vector &uid, bool isAllowed); /** * Set the Power Save Policy object * * @param enable Power save mode is open or not. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ [[nodiscard]] int32_t SetPowerSavePolicy(bool enable); /** * Check if you have permission * * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. */ [[nodiscard]] int32_t CheckPermission(); /** * Set the policy to access the network of the specified application. * * @param uid The specified UID of application. * @param policy The network access policy of application, {@link NetworkAccessPolicy}. * @param reconfirmFlag true means a reconfirm diaglog trigger while policy deny network access. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ int32_t SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag); /** * Query the network access policy of the specified application or all applications. * * @param parameter Indicate to get all or an application network access policy, {@link AccessPolicyParameter}. * @param policy The network access policy of application, {@link AccessPolicySave}. * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. * @permission ohos.permission.CONNECTIVITY_INTERNAL * @systemapi Hide this for inner system use. */ int32_t GetNetworkAccessPolicy(AccessPolicyParameter parameter, AccessPolicySave& policy); int32_t NotifyNetAccessPolicyDiag(uint32_t uid); /** * Set NIC Traffic allowed or disallowed * * @param ifaceNames ifaceNames * @param status true for allowed, false for disallowed * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. */ int32_t SetNicTrafficAllowed(const std::vector &ifaceNames, bool status); private: class NetPolicyDeathRecipient : public IRemoteObject::DeathRecipient { public: explicit NetPolicyDeathRecipient(NetPolicyClient &client) : client_(client) {} ~NetPolicyDeathRecipient() override = default; void OnRemoteDied(const wptr &remote) override { client_.OnRemoteDied(remote); } private: NetPolicyClient &client_; }; private: sptr GetProxy(); void RecoverCallback(); void OnRemoteDied(const wptr &remote); private: std::mutex mutex_; sptr netPolicyService_; sptr deathRecipient_; sptr callback_; }; } // namespace NetManagerStandard } // namespace OHOS #endif // NET_POLICY_CLIENT_H