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 * Set current forground user Id 39 * 40 * @param userId User id 41 */ 42 void SetCurrentUserId(int32_t userId); 43 44 /** 45 * Turn on or off the firewall 46 * 47 * @param userId User id 48 * @param policy The firewall policy to be set 49 * @return Returns 0 success. Otherwise fail 50 */ 51 int32_t SetNetFirewallPolicy(const int32_t userId, const sptr<NetFirewallPolicy> &policy); 52 53 /** 54 * Query firewall policy 55 * 56 * @param userId User id 57 * @param policy Return to firewall policy 58 * @return Returns 0 success. Otherwise fail 59 */ 60 int32_t GetNetFirewallPolicy(const int32_t userId, sptr<NetFirewallPolicy> &policy); 61 62 /** 63 * Query current user firewall policy 64 * 65 * @param userId User id 66 * @param policy Return to firewall policy 67 * @return Returns 0 success. Otherwise fail 68 */ 69 int32_t GetCurrentNetFirewallPolicy(sptr<NetFirewallPolicy> &policy); 70 71 /** 72 * Get user firewall open policy 73 * 74 * @param userId User id 75 * @return Returns true is open, Otherwise close 76 */ 77 bool IsNetFirewallOpen(const int32_t userId); 78 79 /** 80 * Get current user firewall open policy 81 * 82 * @param userId User id 83 * @return Returns true is open, Otherwise close 84 */ 85 bool IsCurrentFirewallOpen(); 86 87 /** 88 * Clear user firewall policy 89 * 90 * @param userId Input User id 91 * @return Returns true is open, Otherwise close 92 */ 93 int32_t ClearFirewallPolicy(const int32_t userId); 94 95 /** 96 * Clear current user firewall policy 97 * 98 * @return Returns true is open, Otherwise close 99 */ 100 int32_t ClearCurrentFirewallPolicy(); 101 102 /** 103 * Get firewall policy inAction 104 * 105 * @return Returns FirewallRuleAction 106 */ 107 FirewallRuleAction GetFirewallPolicyInAction(); 108 109 /** 110 * Get firewall policy inAction 111 * 112 * @return Returns FirewallRuleAction 113 */ 114 FirewallRuleAction GetFirewallPolicyOutAction(); 115 116 /** 117 * Is firewall status change 118 * 119 * @param policy input policy status 120 * @return Returns true is change, Otherwise not change 121 */ 122 bool IsFirewallStatusChange(const sptr<NetFirewallPolicy> &policy); 123 124 /** 125 * Is firewall default action change 126 * 127 * @param policy input policy status 128 * @return Returns true is change, Otherwise not change 129 */ 130 bool IsFirewallActionChange(const sptr<NetFirewallPolicy> &policy); 131 132 /** 133 * Get firewall policy inAction 134 * 135 * @param policy input to firewall policy 136 */ 137 void SetCurrentUserFirewallPolicy(const sptr<NetFirewallPolicy> &policy); 138 139 private: 140 void RebuildFirewallPolicyCache(const int32_t userId); 141 void EnsureCurrentFirewallPolicyCached(); 142 void LoadPolicyFormPreference(const int32_t userId, sptr<NetFirewallPolicy> &policy); 143 144 private: 145 std::shared_mutex setPolicyMutex_; 146 std::shared_ptr<NetFirewallPreferenceHelper> preferencesHelper_ = nullptr; 147 // Cache the current state 148 std::atomic<int32_t> currentUserId_ = 0; 149 sptr<NetFirewallPolicy> currentFirewallPolicy_ = nullptr; 150 }; 151 } // namespace NetManagerStandard 152 } // namespace OHOS 153 #endif /* NET_FIREWALL_POLICY_MANAGER_H */ 154