1 /* 2 * Copyright (c) 2024 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_FIREWALL_POLICY_MANAGER_H 17 #define NET_FIREWALL_POLICY_MANAGER_H 18 19 #include <string> 20 #include <shared_mutex> 21 22 #include "netfirewall_common.h" 23 #include "netfirewall_preference_helper.h" 24 25 namespace OHOS { 26 namespace NetManagerStandard { 27 namespace { 28 const std::string FIREWALL_PREFERENCE_PATH = "/data/service/el1/public/netmanager/netfirewall_status_"; 29 } // namespace 30 31 class NetFirewallPolicyManager { 32 public: 33 static NetFirewallPolicyManager &GetInstance(); 34 NetFirewallPolicyManager(); 35 ~NetFirewallPolicyManager(); 36 37 /** 38 * Turn on or off the firewall 39 * 40 * @param userId User id 41 * @param policy The firewall policy to be set 42 * @return Returns 0 success. Otherwise fail 43 */ 44 int32_t SetNetFirewallPolicy(const int32_t userId, const sptr<NetFirewallPolicy> &policy); 45 46 /** 47 * Query firewall policy 48 * 49 * @param userId User id 50 * @param policy Return to firewall policy 51 * @return Returns 0 success. Otherwise fail 52 */ 53 int32_t GetNetFirewallPolicy(const int32_t userId, sptr<NetFirewallPolicy> &policy); 54 55 /** 56 * true or false firewall status 57 * 58 * @return Returns true is open, Otherwise close 59 */ 60 bool IsFirewallOpen(); 61 62 /** 63 * Get user firewall open policy 64 * 65 * @param userId User id 66 * @return Returns true is open, Otherwise close 67 */ 68 bool IsNetFirewallOpen(const int32_t userId); 69 70 /** 71 * Clear user firewall policy 72 * 73 * @param userId Input User id 74 * @return Returns true is open, Otherwise close 75 */ 76 int32_t ClearFirewallPolicy(const int32_t userId); 77 78 /** 79 * Init netfirewall policy 80 */ 81 int32_t InitNetfirewallPolicy(); 82 83 /** 84 * Get user firewall open policy 85 * 86 * @param userId User id 87 * @return Returns true is open, Otherwise close 88 */ 89 bool GetNetFirewallStatus(const int32_t userId); 90 91 /** 92 * Load firewall policy from preference 93 * 94 * @param userId User id 95 * @param policy Return to firewall policy 96 */ 97 void LoadPolicyFormPreference(const int32_t userId, sptr<NetFirewallPolicy> &policy); 98 99 private: 100 void GetAllUserId(std::vector<int32_t> &accountIds); 101 102 private: 103 std::shared_mutex setPolicyMutex_; 104 std::shared_ptr<NetFirewallPreferenceHelper> preferencesHelper_ = nullptr; 105 }; 106 } // namespace NetManagerStandard 107 } // namespace OHOS 108 #endif /* NET_FIREWALL_POLICY_MANAGER_H */ 109