• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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 "preferences_util.h"
17 #include "common/sharing_log.h"
18 
19 namespace OHOS {
20 namespace Sharing {
21 const std::string SPLIT = ",";
22 
PreferencesUtil(const std::string & path)23 PreferencesUtil::PreferencesUtil(const std::string &path)
24 {
25     path_ = path;
26 }
27 
GetProfiles(const std::string & path,int & errCode)28 std::shared_ptr<NativePreferences::Preferences> PreferencesUtil::GetProfiles(const std::string &path, int &errCode)
29 {
30     return NativePreferences::PreferencesHelper::GetPreferences(path_, errCode);
31 }
32 
PutString(const std::string & key,const std::string & value)33 int PreferencesUtil::PutString(const std::string &key, const std::string &value)
34 {
35     std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
36     if (ptr == nullptr) {
37         SHARING_LOGE("get profiles failed. errcode_ %{public}d.", errCode_);
38         return NativePreferences::E_ERROR;
39     }
40     std::lock_guard<std::mutex> lock(preferencesMutex_);
41     int result = ptr->PutString(key, value);
42     ptr->FlushSync();
43     return result;
44 }
45 
GetString(const std::string & key)46 std::string PreferencesUtil::GetString(const std::string &key)
47 {
48     std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
49     if (ptr == nullptr) {
50         SHARING_LOGE("get profiles failed. errcode_ %{public}d.", errCode_);
51         return "";
52     }
53     std::string defaultValue = "";
54     std::lock_guard<std::mutex> lock(preferencesMutex_);
55     std::string result = ptr->GetString(key, defaultValue);
56     return result;
57 }
58 
DeleteKey(const std::string & key)59 int PreferencesUtil::DeleteKey(const std::string &key)
60 {
61     std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
62     if (ptr == nullptr) {
63         SHARING_LOGE("get profiles failed. errcode_ %{public}d.", errCode_);
64         return NativePreferences::E_ERROR;
65     }
66     std::lock_guard<std::mutex> lock(preferencesMutex_);
67     int result = ptr->Delete(key);
68     ptr->FlushSync();
69     return result;
70 }
71 
Clear()72 int PreferencesUtil::Clear()
73 {
74     std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
75     if (ptr == nullptr) {
76         SHARING_LOGE("get profiles failed. errcode_ %{public}d.", errCode_);
77         return NativePreferences::E_ERROR;
78     }
79     std::lock_guard<std::mutex> lock(preferencesMutex_);
80     int result = ptr->Clear();
81     ptr->FlushSync();
82     return result;
83 }
84 
Flush()85 void PreferencesUtil::Flush()
86 {
87     std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
88     if (ptr != nullptr) {
89         ptr->Flush();
90     }
91 }
92 
FlushSync()93 int PreferencesUtil::FlushSync()
94 {
95     std::shared_ptr<NativePreferences::Preferences> ptr = GetProfiles(path_, errCode_);
96     if (ptr == nullptr) {
97         SHARING_LOGE("get profiles failed. errcode_ %{public}d.", errCode_);
98         return NativePreferences::E_ERROR;
99     }
100     return ptr->FlushSync();
101 }
102 
103 } // namespace Sharing
104 } // namespace OHOS