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_SERVICE_H 17 #define NET_POLICY_SERVICE_H 18 19 #include <atomic> 20 #include <mutex> 21 22 #include "event_runner.h" 23 #include "singleton.h" 24 #include "system_ability.h" 25 #include "system_ability_definition.h" 26 27 #include "net_policy_callback.h" 28 #include "net_policy_event_handler.h" 29 #include "net_policy_firewall.h" 30 #include "net_policy_rule.h" 31 #include "net_policy_service_common.h" 32 #include "net_policy_service_stub.h" 33 #include "net_policy_traffic.h" 34 35 namespace OHOS { 36 namespace NetManagerStandard { 37 class NetPolicyService : public SystemAbility, 38 public NetPolicyServiceStub, 39 public std::enable_shared_from_this<NetPolicyService> { 40 DECLARE_DELAYED_SINGLETON(NetPolicyService) 41 DECLARE_SYSTEM_ABILITY(NetPolicyService) 42 43 public: 44 void OnStart() override; 45 void OnStop() override; 46 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 47 48 /** 49 * Set the network policy for the specified UID. 50 * @param uid The specified UID of app. 51 * @param policy The network policy for application. 52 * For details, see {@link NetUidPolicy}. 53 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 54 */ 55 int32_t SetPolicyByUid(uint32_t uid, uint32_t policy) override; 56 57 /** 58 * Get the network policy of the specified UID. 59 * @param uid The specified UID of app. 60 * @param policy Return this uid's policy. 61 * For details, see {@link NetUidPolicy}. 62 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 63 */ 64 int32_t GetPolicyByUid(uint32_t uid, uint32_t &policy) override; 65 66 /** 67 * Get the application UIDs of the specified policy. 68 * @param policy the network policy of the current UID of application. 69 * For details, see {@link NetUidPolicy}. 70 * @param uids The list of UIDs 71 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 72 */ 73 int32_t GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids) override; 74 75 /** 76 * Get the status whether the specified uid app can access the metered network or non-metered network. 77 * @param uid The specified UID of application. 78 * @param metered Indicates metered network or non-metered network. 79 * @param isAllowed Return true means it's allowed to access the network. 80 * Return false means it's not allowed to access the network. 81 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 82 */ 83 int32_t IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed) override; 84 85 /** 86 * Get the status whether the specified uid app can access the specified iface network. 87 * @param uid The specified UID of application. 88 * @param ifaceName Iface name. 89 * @param isAllowed Return true means it's allowed to access the network. 90 * Return false means it's not allowed to access the network. 91 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 92 */ 93 int32_t IsUidNetAllowed(uint32_t uid, const std::string &ifaceName, bool &isAllowed) override; 94 95 /** 96 * Register network policy change callback. 97 * @param callback Interface type pointer. 98 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 99 */ 100 int32_t RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback) override; 101 102 /** 103 * Unregister network policy change callback. 104 * @param callback Interface type pointer. 105 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 106 */ 107 int32_t UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback) override; 108 109 /** 110 * Set policies by NetPolicyQuotaPolicy. 111 * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}. 112 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 113 */ 114 int32_t SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> "aPolicies) override; 115 116 /** 117 * Get network policies. 118 * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}. 119 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 120 */ 121 int32_t GetNetQuotaPolicies(std::vector<NetQuotaPolicy> "aPolicies) override; 122 123 /** 124 * Reset network policies\rules\quota policies\firewall rules. 125 * @param iccid Specify the matched iccid of quota policy. 126 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 127 */ 128 int32_t ResetPolicies(const std::string &iccid) override; 129 130 /** 131 * Control if apps can use data on background. 132 * @param allowBackground Allow apps to use data on background. 133 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 134 */ 135 int32_t SetBackgroundPolicy(bool allowBackground) override; 136 137 /** 138 * Get the status if apps can use data on background. 139 * @param backgroundPolicy True is allowed to use data on background. 140 * False is not allowed to use data on background. 141 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 142 */ 143 int32_t GetBackgroundPolicy(bool &backgroundPolicy) override; 144 145 /** 146 * Get the background network restriction policy for the specified uid. 147 * @param uid uid The specified UID of application. 148 * @param backgroundPolicyOfUid The specified UID of backgroundPolicy. 149 * For details, see {@link NetBackgroundPolicy}. 150 * @return uint32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 151 */ 152 int32_t GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid) override; 153 154 /** 155 * Update the limit or warning remind time of quota policy. 156 * @param netType {@link NetBearType}. 157 * @param iccid Specify the matched iccid of quota policy when netType is cellular. 158 * @param remindType {@link RemindType}. 159 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 160 */ 161 int32_t UpdateRemindPolicy(int32_t netType, const std::string &iccid, uint32_t remindType) override; 162 163 /** 164 * Set the UID into device idle allow list. 165 * @param uid The specified UID of application. 166 * @param isAllowed The UID is into allowed list or not. 167 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 168 */ 169 int32_t SetDeviceIdleAllowedList(uint32_t uid, bool isAllowed) override; 170 171 /** 172 * Get the allow list of UID in device idle mode. 173 * @param uids The list of UIDs 174 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 175 */ 176 int32_t GetDeviceIdleAllowedList(std::vector<uint32_t> &uids) override; 177 178 /** 179 * Process network policy in device idle mode. 180 * @param enable Device idle mode is open or not. 181 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 182 */ 183 int32_t SetDeviceIdlePolicy(bool enable) override; 184 185 private: 186 void Init(); 187 int32_t GetDumpMessage(std::string &message); 188 189 private: 190 enum ServiceRunningState { 191 STATE_STOPPED = 0, 192 STATE_RUNNING, 193 }; 194 195 std::shared_ptr<NetPolicyTraffic> netPolicyTraffic_ = nullptr; 196 std::shared_ptr<NetPolicyFirewall> netPolicyFirewall_ = nullptr; 197 std::shared_ptr<NetPolicyRule> netPolicyRule_ = nullptr; 198 std::shared_ptr<NetPolicyFile> netPolicyFile_ = nullptr; 199 ServiceRunningState state_ = ServiceRunningState::STATE_STOPPED; 200 std::shared_ptr<NetPolicyCore> netPolicyCore_ = nullptr; 201 std::shared_ptr<NetPolicyCallback> netPolicyCallback_ = nullptr; 202 sptr<NetPolicyServiceCommon> serviceComm_ = nullptr; 203 std::mutex mutex_; 204 std::vector<uint16_t> monthDay_; 205 }; 206 } // namespace NetManagerStandard 207 } // namespace OHOS 208 #endif // NET_POLICY_SERVICE_H 209