• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
CreateInstance(const std::string & filePath)28 std::shared_ptr<NetFirewallPreferenceHelper> NetFirewallPreferenceHelper::CreateInstance(
29     const std::string &filePath)
30 {
31     int32_t errCode = -1;
32     auto ptr = NativePreferences::PreferencesHelper::GetPreferences(filePath, errCode);
33     if (ptr == nullptr) {
34         NETMGR_EXT_LOG_E("GetPreference error code is %{public}d", errCode);
35         return nullptr;
36     }
37     auto instance = std::make_shared<NetFirewallPreferenceHelper>();
38     instance->ptr_ = ptr;
39     return instance;
40 }
41 
SaveInt(const std::string & key,int32_t value)42 bool NetFirewallPreferenceHelper::SaveInt(const std::string &key, int32_t value)
43 {
44     return Save(key, value);
45 }
46 
SaveBool(const std::string & key,bool value)47 bool NetFirewallPreferenceHelper::SaveBool(const std::string &key, bool value)
48 {
49     return Save(key, value);
50 }
51 
Save(const std::string & key,const T & value)52 template <typename T> bool NetFirewallPreferenceHelper::Save(const std::string &key, const T &value)
53 {
54     if (!SaveInner(ptr_, key, value)) {
55         NETMGR_EXT_LOG_E("SaveInner error");
56         return false;
57     }
58     return RefreshSync();
59 }
60 
SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr,const std::string & key,const int32_t & value)61 bool NetFirewallPreferenceHelper::SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key,
62     const int32_t &value)
63 {
64     return ptr->PutInt(key, value) == NativePreferences::E_OK;
65 }
66 
SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr,const std::string & key,const bool & value)67 bool NetFirewallPreferenceHelper::SaveInner(std::shared_ptr<NativePreferences::Preferences> ptr, const std::string &key,
68     const bool &value)
69 {
70     return ptr->PutBool(key, value) == NativePreferences::E_OK;
71 }
72 
ObtainInt(const std::string & key,int32_t defValue)73 int32_t NetFirewallPreferenceHelper::ObtainInt(const std::string &key, int32_t defValue)
74 {
75     return Obtain(key, defValue);
76 }
77 
ObtainBool(const std::string & key,bool defValue)78 bool NetFirewallPreferenceHelper::ObtainBool(const std::string &key, bool defValue)
79 {
80     return Obtain(key, defValue);
81 }
82 
Obtain(const std::string & key,const T & defValue)83 template <typename T> T NetFirewallPreferenceHelper::Obtain(const std::string &key, const T &defValue)
84 {
85     return ObtainInner(ptr_, key, defValue);
86 }
87 
ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr,const std::string & key,const int32_t & defValue)88 int32_t NetFirewallPreferenceHelper::ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr,
89     const std::string &key, const int32_t &defValue)
90 {
91     return ptr->GetInt(key, defValue);
92 }
93 
ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr,const std::string & key,const bool & defValue)94 bool NetFirewallPreferenceHelper::ObtainInner(std::shared_ptr<NativePreferences::Preferences> ptr,
95     const std::string &key, const bool &defValue)
96 {
97     return ptr->GetBool(key, defValue);
98 }
99 
RefreshSync()100 bool NetFirewallPreferenceHelper::RefreshSync()
101 {
102     if (ptr_->FlushSync() != NativePreferences::E_OK) {
103         NETMGR_EXT_LOG_E("RefreshSync error");
104         return false;
105     }
106     return true;
107 }
108 
Clear(const std::string & filePath)109 bool NetFirewallPreferenceHelper::Clear(const std::string &filePath)
110 {
111     int ret = NativePreferences::PreferencesHelper::DeletePreferences(filePath);
112     if (ret) {
113         NETMGR_EXT_LOG_E("Preferences not exist");
114     }
115     return true;
116 }
117 } // namespace NetManagerStandard
118 } // namespace OHOS