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