• 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 "preferences_base.h"
17 
18 #include <cinttypes>
19 #include <climits>
20 #include <cstdint>
21 #include <cstdlib>
22 #include <functional>
23 #include <sstream>
24 
25 #include "executor_pool.h"
26 #include "log_print.h"
27 #include "preferences_utils.h"
28 #include "preferences_dfx_adapter.h"
29 #include "preferences_file_operation.h"
30 #include "preferences_observer_stub.h"
31 
32 namespace OHOS {
33 namespace NativePreferences {
34 
35 ExecutorPool PreferencesBase::executorPool_ = ExecutorPool(1, 0);
36 
PreferencesBase(const Options & options)37 PreferencesBase::PreferencesBase(const Options &options) : options_(options)
38 {
39 }
40 
~PreferencesBase()41 PreferencesBase::~PreferencesBase()
42 {
43 }
44 
Get(const std::string & key,const PreferencesValue & defValue)45 PreferencesValue PreferencesBase::Get(const std::string &key, const PreferencesValue &defValue)
46 {
47     return defValue;
48 }
49 
Put(const std::string & key,const PreferencesValue & value)50 int PreferencesBase::Put(const std::string &key, const PreferencesValue &value)
51 {
52     return E_OK;
53 }
54 
GetInt(const std::string & key,const int & defValue={})55 int PreferencesBase::GetInt(const std::string &key, const int &defValue = {})
56 {
57     PreferencesValue preferencesValue = Get(key, defValue);
58     if (!preferencesValue.IsInt()) {
59         return defValue;
60     }
61     return preferencesValue;
62 }
63 
GetString(const std::string & key,const std::string & defValue={})64 std::string PreferencesBase::GetString(const std::string &key, const std::string &defValue = {})
65 {
66     PreferencesValue preferencesValue = Get(key, defValue);
67     if (!preferencesValue.IsString()) {
68         return defValue;
69     }
70     return preferencesValue;
71 }
GetBool(const std::string & key,const bool & defValue={})72 bool PreferencesBase::GetBool(const std::string &key, const bool &defValue = {})
73 {
74     PreferencesValue preferencesValue = Get(key, defValue);
75     if (!preferencesValue.IsBool()) {
76         return defValue;
77     }
78     return preferencesValue;
79 }
GetFloat(const std::string & key,const float & defValue={})80 float PreferencesBase::GetFloat(const std::string &key, const float &defValue = {})
81 {
82     PreferencesValue preferencesValue = Get(key, defValue);
83     if (!preferencesValue.IsFloat()) {
84         return defValue;
85     }
86     return preferencesValue;
87 }
88 
GetDouble(const std::string & key,const double & defValue={})89 double PreferencesBase::GetDouble(const std::string &key, const double &defValue = {})
90 {
91     PreferencesValue preferencesValue = Get(key, defValue);
92     if (!preferencesValue.IsDouble()) {
93         return defValue;
94     }
95     return preferencesValue;
96 }
97 
GetLong(const std::string & key,const int64_t & defValue={})98 int64_t PreferencesBase::GetLong(const std::string &key, const int64_t &defValue = {})
99 {
100     PreferencesValue preferencesValue = Get(key, defValue);
101     if (!preferencesValue.IsLong()) {
102         return defValue;
103     }
104     return preferencesValue;
105 }
106 
GetAll()107 std::map<std::string, PreferencesValue> PreferencesBase::GetAll()
108 {
109     return {};
110 }
111 
HasKey(const std::string & key)112 bool PreferencesBase::HasKey(const std::string &key)
113 {
114     return true;
115 }
116 
PutInt(const std::string & key,int value)117 int PreferencesBase::PutInt(const std::string &key, int value)
118 {
119     return Put(key, value);
120 }
121 
PutString(const std::string & key,const std::string & value)122 int PreferencesBase::PutString(const std::string &key, const std::string &value)
123 {
124     return Put(key, value);
125 }
126 
PutBool(const std::string & key,bool value)127 int PreferencesBase::PutBool(const std::string &key, bool value)
128 {
129     return Put(key, value);
130 }
131 
PutLong(const std::string & key,int64_t value)132 int PreferencesBase::PutLong(const std::string &key, int64_t value)
133 {
134     return Put(key, value);
135 }
136 
PutFloat(const std::string & key,float value)137 int PreferencesBase::PutFloat(const std::string &key, float value)
138 {
139     return Put(key, value);
140 }
PutDouble(const std::string & key,double value)141 int PreferencesBase::PutDouble(const std::string &key, double value)
142 {
143     return Put(key, value);
144 }
Delete(const std::string & key)145 int PreferencesBase::Delete(const std::string &key)
146 {
147     return E_OK;
148 }
149 
Clear()150 int PreferencesBase::Clear()
151 {
152     return E_OK;
153 }
154 
Flush()155 void PreferencesBase::Flush()
156 {
157 }
158 
FlushSync()159 int PreferencesBase::FlushSync()
160 {
161     return E_OK;
162 }
163 
RegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver,RegisterMode mode)164 int PreferencesBase::RegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver, RegisterMode mode)
165 {
166     IsClose(std::string(__FUNCTION__));
167     std::unique_lock<std::shared_mutex> writeLock(obseverMetux_);
168     if (mode == RegisterMode::LOCAL_CHANGE) {
169         std::weak_ptr<PreferencesObserver> weakPreferencesObserver = preferencesObserver;
170         localObservers_.push_back(weakPreferencesObserver);
171     } else if (mode == RegisterMode::MULTI_PRECESS_CHANGE) {
172         auto dataObsMgrClient = DataObsMgrClient::GetInstance();
173         if (dataObsMgrClient == nullptr) {
174             return E_GET_DATAOBSMGRCLIENT_FAIL;
175         }
176         sptr<DataPreferencesObserverStub> observer(new (std::nothrow) DataPreferencesObserverStub(preferencesObserver));
177         int errcode = dataObsMgrClient->RegisterObserver(MakeUri(), observer);
178         if (errcode != 0) {
179             LOG_ERROR("RegisterObserver multiProcessChange failed, errCode %{public}d", errcode);
180             ReportFaultParam param = { "subscribe error", options_.bundleName, NORMAL_DB,
181                 ExtractFileName(options_.filePath), E_SUBSCRIBE_FAILED,
182                 "subscribe failed, the reason is " + std::to_string(errcode) };
183             PreferencesDfxManager::ReportFault(param);
184             return errcode;
185         }
186         multiProcessObservers_.push_back(observer);
187     }
188     LOG_INFO("The local observer subscribed succeeded.");
189     return E_OK;
190 }
191 
UnRegisterDataObserver(std::shared_ptr<PreferencesObserver> preferencesObserver,const std::vector<std::string> & keys)192 int PreferencesBase::UnRegisterDataObserver(std::shared_ptr<PreferencesObserver> preferencesObserver,
193     const std::vector<std::string> &keys)
194 {
195     IsClose(std::string(__FUNCTION__));
196     std::unique_lock<std::shared_mutex> writeLock(obseverMetux_);
197     auto it = dataObserversMap_.find(preferencesObserver);
198     if (it == dataObserversMap_.end()) {
199         return E_OK;
200     }
201     for (const auto &key : keys) {
202         auto keyIt = it->second.find(key);
203         if (keyIt != it->second.end()) {
204             it->second.erase(key);
205         }
206     }
207     LOG_DEBUG("UnRegisterObserver keysSize:%{public}zu, curSize:%{public}zu", keys.size(), it->second.size());
208     if (keys.empty()) {
209         it->second.clear();
210     }
211     if (it->second.empty()) {
212         it = dataObserversMap_.erase(it);
213         LOG_DEBUG("UnRegisterObserver finish. obSize:%{public}zu", dataObserversMap_.size());
214         return E_OK;
215     }
216     LOG_DEBUG("UnRegisterObserver finish, observer need reserve. obSize:%{public}zu", dataObserversMap_.size());
217     return E_OBSERVER_RESERVE;
218 }
219 
GetGroupId() const220 std::string PreferencesBase::GetGroupId() const
221 {
222     return options_.dataGroupId;
223 }
224 
CloseDb()225 int PreferencesBase::CloseDb()
226 {
227     return E_OK;
228 }
229 
GetValue(const std::string & key,const PreferencesValue & defValue)230 std::pair<int, PreferencesValue> PreferencesBase::GetValue(const std::string &key, const PreferencesValue &defValue)
231 {
232     return std::make_pair(E_OK, defValue);
233 }
234 
GetAllData()235 std::pair<int, std::map<std::string, PreferencesValue>> PreferencesBase::GetAllData()
236 {
237     return {};
238 }
239 
GetBundleName() const240 std::string PreferencesBase::GetBundleName() const
241 {
242     return options_.bundleName;
243 }
244 
RegisterDataObserver(std::shared_ptr<PreferencesObserver> preferencesObserver,const std::vector<std::string> & keys)245 int PreferencesBase::RegisterDataObserver(std::shared_ptr<PreferencesObserver> preferencesObserver,
246     const std::vector<std::string> &keys)
247 {
248     IsClose(std::string(__FUNCTION__));
249     std::unique_lock<std::shared_mutex> writeLock(obseverMetux_);
250     auto it = dataObserversMap_.find(preferencesObserver);
251     if (it == dataObserversMap_.end()) {
252         std::set<std::string> callKeys(keys.begin(), keys.end());
253         std::weak_ptr<PreferencesObserver> weakPreferencesObserver = preferencesObserver;
254         dataObserversMap_.insert({weakPreferencesObserver, std::move(callKeys)});
255     } else {
256         it->second.insert(keys.begin(), keys.end());
257     }
258     return E_OK;
259 }
260 
GetAllDatas()261 std::unordered_map<std::string, PreferencesValue> PreferencesBase::GetAllDatas()
262 {
263     return {};
264 }
265 
UnRegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver,RegisterMode mode)266 int PreferencesBase::UnRegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver, RegisterMode mode)
267 {
268     IsClose(std::string(__FUNCTION__));
269     std::unique_lock<std::shared_mutex> writeLock(obseverMetux_);
270     if (mode == RegisterMode::LOCAL_CHANGE) {
271         for (auto it = localObservers_.begin(); it != localObservers_.end(); ++it) {
272             std::weak_ptr<PreferencesObserver> weakPreferencesObserver = *it;
273             std::shared_ptr<PreferencesObserver> sharedObserver = weakPreferencesObserver.lock();
274             if (!sharedObserver || sharedObserver == preferencesObserver) {
275                 localObservers_.erase(it);
276                 break;
277             }
278         }
279         return E_OK;
280     }
281     for (auto it = multiProcessObservers_.begin(); it != multiProcessObservers_.end(); ++it) {
282         std::shared_ptr<PreferencesObserver> sharedObserver = (*it)->preferencesObserver_.lock();
283         if (!sharedObserver || sharedObserver == preferencesObserver) {
284             auto dataObsMgrClient = DataObsMgrClient::GetInstance();
285             if (dataObsMgrClient == nullptr) {
286                 return E_GET_DATAOBSMGRCLIENT_FAIL;
287             }
288             int errcode = dataObsMgrClient->UnregisterObserver(MakeUri(), *it);
289             if (errcode != 0) {
290                 LOG_ERROR("UnRegisterObserver multiProcessChange failed, errCode %{public}d", errcode);
291                 return errcode;
292             }
293             multiProcessObservers_.erase(it);
294             break;
295         }
296     }
297     LOG_INFO("The observer unsubscribed succeeded.");
298     return E_OK;
299 }
300 
MakeUri(const std::string & key)301 Uri PreferencesBase::MakeUri(const std::string &key)
302 {
303     std::string uriStr;
304     if (options_.dataGroupId.empty()) {
305         uriStr = STR_SCHEME + options_.bundleName + STR_SLASH + options_.filePath;
306     } else {
307         uriStr = STR_SCHEME + options_.dataGroupId + STR_SLASH + options_.filePath;
308     }
309 
310     if (!key.empty()) {
311         uriStr = uriStr + STR_QUERY + key;
312     }
313     return Uri(uriStr);
314 }
315 } // End of namespace NativePreferences
316 } // End of namespace OHOS
317