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 #include <sys/stat.h>
17 #include <sys/types.h>
18
19 #include "netfirewall_preference_helper.h"
20 #include "netmgr_ext_log_wrapper.h"
21 #include "preferences_errno.h"
22 #include "preferences_helper.h"
23 #include "preferences_value.h"
24 using namespace std;
25
26 namespace OHOS {
27 namespace NetManagerStandard {
GetInstance()28 shared_ptr<NetFirewallPreferenceHelper> NetFirewallPreferenceHelper::GetInstance()
29 {
30 static std::shared_ptr<NetFirewallPreferenceHelper> instance = make_shared<NetFirewallPreferenceHelper>();
31 return instance;
32 }
33
GetPreference()34 bool NetFirewallPreferenceHelper::GetPreference()
35 {
36 int32_t errCode = -1;
37 ptr_ = NativePreferences::PreferencesHelper::GetPreferences(filePath_, errCode);
38 if (ptr_ == nullptr) {
39 NETMGR_EXT_LOG_E("GetPreference error code is %{public}d", errCode);
40 return false;
41 }
42 return true;
43 }
44
GetPreference(const std::string & filePath)45 bool NetFirewallPreferenceHelper::GetPreference(const std::string &filePath)
46 {
47 filePath_ = filePath;
48 int32_t errCode = -1;
49 ptr_ = NativePreferences::PreferencesHelper::GetPreferences(filePath_, errCode);
50 if (ptr_ == nullptr) {
51 NETMGR_EXT_LOG_E("GetPreference error code is %{public}d", errCode);
52 return false;
53 }
54 return true;
55 }
56
SaveInt(const std::string & key,int32_t value)57 bool NetFirewallPreferenceHelper::SaveInt(const std::string &key, int32_t value)
58 {
59 return Save(key, value);
60 }
61
SaveBool(const std::string & key,bool value)62 bool NetFirewallPreferenceHelper::SaveBool(const std::string &key, bool value)
63 {
64 return Save(key, value);
65 }
66
Save(const std::string & key,const T & value)67 template <typename T> bool NetFirewallPreferenceHelper::Save(const std::string &key, const T &value)
68 {
69 if (!GetPreference()) {
70 return false;
71 }
72 if (!SaveInner(ptr_, key, value)) {
73 NETMGR_EXT_LOG_E("SaveInner error");
74 return false;
75 }
76 return RefreshSync();
77 }
78
SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr,const std::string & key,const int32_t & value)79 bool NetFirewallPreferenceHelper::SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key,
80 const int32_t &value)
81 {
82 if (ptr == nullptr) {
83 NETMGR_EXT_LOG_E("ptr is nullptr");
84 return false;
85 }
86 return ptr->PutInt(key, value) == NativePreferences::E_OK;
87 }
88
SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr,const std::string & key,const bool & value)89 bool NetFirewallPreferenceHelper::SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key,
90 const bool &value)
91 {
92 if (ptr == nullptr) {
93 NETMGR_EXT_LOG_E("ptr is nullptr");
94 return false;
95 }
96 return ptr->PutBool(key, value) == NativePreferences::E_OK;
97 }
98
ObtainInt(const std::string & key,int32_t defValue)99 int32_t NetFirewallPreferenceHelper::ObtainInt(const std::string &key, int32_t defValue)
100 {
101 return Obtain(key, defValue);
102 }
103
ObtainBool(const std::string & key,bool defValue)104 bool NetFirewallPreferenceHelper::ObtainBool(const std::string &key, bool defValue)
105 {
106 return Obtain(key, defValue);
107 }
108
Obtain(const std::string & key,const T & defValue)109 template <typename T> T NetFirewallPreferenceHelper::Obtain(const std::string &key, const T &defValue)
110 {
111 if (!GetPreference()) {
112 NETMGR_EXT_LOG_I("Obtain GetPreference failed");
113 return defValue;
114 }
115 return ObtainInner(ptr_, key, defValue);
116 }
117
ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr,const std::string & key,const int32_t & defValue)118 int32_t NetFirewallPreferenceHelper::ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr,
119 const std::string &key, const int32_t &defValue)
120 {
121 return ptr->GetInt(key, defValue);
122 }
123
ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr,const std::string & key,const bool & defValue)124 bool NetFirewallPreferenceHelper::ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr,
125 const std::string &key, const bool &defValue)
126 {
127 return ptr->GetBool(key, defValue);
128 }
129
RefreshSync()130 bool NetFirewallPreferenceHelper::RefreshSync()
131 {
132 if (!GetPreference()) {
133 NETMGR_EXT_LOG_I("RefreshSync GetPreference failed");
134 return false;
135 }
136 if (ptr_ == nullptr) {
137 NETMGR_EXT_LOG_E("ptr_ is nullptr");
138 return false;
139 }
140 if (ptr_->FlushSync() != NativePreferences::E_OK) {
141 NETMGR_EXT_LOG_E("RefreshSync error");
142 return false;
143 }
144 return true;
145 }
146
Clear(const std::string & filePath)147 bool NetFirewallPreferenceHelper::Clear(const std::string &filePath)
148 {
149 if (!GetPreference(filePath)) {
150 NETMGR_EXT_LOG_I("Clear GetPreference failed");
151 return false;
152 }
153 if (ptr_ == nullptr) {
154 NETMGR_EXT_LOG_E("ptr_ is nullptr");
155 return false;
156 }
157 int ret = NativePreferences::PreferencesHelper::DeletePreferences(filePath);
158 if (ret) {
159 NETMGR_EXT_LOG_E("ptr_ is nullptr");
160 }
161 ptr_ = nullptr;
162 return true;
163 }
164 } // namespace NetManagerStandard
165 } // namespace OHOS