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_PREFERENCES_HELPER_H 17 #define NET_FIREWALL_PREFERENCES_HELPER_H 18 19 #include <map> 20 21 #include "preferences.h" 22 23 namespace OHOS { 24 namespace NetManagerStandard { 25 class NetFirewallPreferenceHelper { 26 public: 27 NetFirewallPreferenceHelper() = default; 28 ~NetFirewallPreferenceHelper() = default; 29 30 /** 31 * Save int data using preferences 32 * 33 * @param key String 34 * @param value Integer value 35 * @return If get preferences success and save inner int value success, there is a return to true, 36 * otherwise it will be false 37 */ 38 bool SaveInt(const std::string &key, int32_t value); 39 40 /** 41 * Save bool data using preferences 42 * 43 * @param key String 44 * @param value Bool value 45 * @return If get preferences success and save inner bool value success, there is a return to true, 46 * otherwise it will be false 47 */ 48 bool SaveBool(const std::string &key, bool value); 49 50 /** 51 * Get integer data using preferences 52 * 53 * @param key String 54 * @param value Integer value 55 */ 56 int32_t ObtainInt(const std::string &key, int32_t defValue); 57 58 /** 59 * Get bool data 60 * 61 * @param key String 62 * @param defValue Value 63 * @return If get preferences success and obtain inner bool value success, 64 * there is a return to true, otherwise it will be false 65 */ 66 bool ObtainBool(const std::string &key, bool defValue); 67 68 /** 69 * Cleaning Data using preferences 70 * 71 * @param filePath File path 72 * @return If there is a preference file and get preferences not null, and delete preferences success, 73 * it will return to true, otherwise it will be false 74 */ 75 static bool Clear(const std::string &filePath); 76 77 /** 78 * Get Object Handle 79 * 80 * @param filePath File path 81 * @return If there is a preference file and get preferences not null, 82 * it will return instance of NetFirewallPreferenceHelper, otherwise it will be nullptr 83 */ 84 static std::shared_ptr<NetFirewallPreferenceHelper> CreateInstance(const std::string &filePath); 85 86 private: 87 bool RefreshSync(); 88 89 template <typename T> bool Save(const std::string &key, const T &defValue); 90 91 bool SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const int32_t &value); 92 93 bool SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const bool &value); 94 95 template <typename T> T Obtain(const std::string &key, const T &defValue); 96 97 int32_t ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, 98 const int32_t &defValue); 99 100 bool ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key, const bool &defValue); 101 102 std::shared_ptr<NativePreferences::Preferences> ptr_; 103 }; 104 } // namespace NetManagerStandard 105 } // namespace OHOS 106 #endif // NET_FIREWALL_PREFERENCES_HELPER_H