• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 PREFERENCES_H
17 #define PREFERENCES_H
18 
19 #include <unordered_map>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "preferences_errno.h"
25 #include "preferences_observer.h"
26 #include "preferences_value.h"
27 #include "preferences_visibility.h"
28 
29 namespace OHOS {
30 namespace NativePreferences {
31 using RegisterMode = PreferencesObserver::RegisterMode;
32 enum StorageType {
33     XML = 0,
34     GSKV
35 };
36 struct Options {
37 public:
OptionsOptions38     Options(const std::string inputFilePath) : filePath(inputFilePath)
39     {
40     }
41 
OptionsOptions42     Options(const char *inputFilePath) : filePath(inputFilePath)
43     {
44     }
45 
OptionsOptions46     Options(const std::string &inputFilePath, const std::string &inputbundleName, const std::string &inputdataGroupId)
47         : filePath(inputFilePath), bundleName(inputbundleName), dataGroupId(inputdataGroupId)
48     {
49     }
50 
OptionsOptions51     Options(const std::string &inputFilePath, const std::string &inputbundleName, const std::string &inputdataGroupId,
52         bool inputIsEnhance) : filePath(inputFilePath), bundleName(inputbundleName), dataGroupId(inputdataGroupId),
53         isEnhance(inputIsEnhance)
54     {
55     }
56 
57 public:
58     std::string filePath{ "" };
59     std::string bundleName{ "" };
60     std::string dataGroupId{ "" };
61     bool isEnhance = false;
62 };
63 /**
64  * The function class of the preference. Various operations on preferences instances are provided in this class.
65  */
66 class PREF_API_EXPORT Preferences {
67 public:
~Preferences()68     PREF_API_EXPORT virtual ~Preferences()
69     {
70     }
71 
72     /**
73     * @brief The constant Indicates the maximum length of the key in the preferences.
74     */
75     PREF_API_EXPORT static constexpr uint32_t MAX_KEY_LENGTH = 1024;
76 
77     /**
78      * @brief The constant Indicates the maximum length of the value in the preferences.
79      */
80     PREF_API_EXPORT static constexpr uint32_t MAX_VALUE_LENGTH = 16 * 1024 * 1024;
81 
82     /**
83      * @brief Obtains the value of a preferences.
84      *
85      * This function is used to get the value of the corresponding key in the preference.
86      *
87      * @param key Indicates the key of the preferences. It cannot be empty.
88      * @param defValue Indicates the default value of the preferences.
89      *
90      * @return Returns the value matching the specified key if it is found; returns the default value otherwise.
91      */
92     virtual PreferencesValue Get(const std::string &key, const PreferencesValue &defValue) = 0;
93 
94     /**
95      * @brief Sets a value for the key in the preferences.
96      *
97      *  This function is used to set or update the value of the corresponding key in the preferences.
98      *
99      * @param key Indicates the key of the preferences. It cannot be empty.
100      * @param value Indicates the default value of the preferences.
101      *
102      * @return Returns 0 for success, others for failure.
103      */
104     virtual int Put(const std::string &key, const PreferencesValue &value) = 0;
105 
106     /**
107      * @brief Obtains the int value of a preferences.
108      *
109      * This function is used to get an int value of the corresponding key in the preference.
110      *
111      * @param key Indicates the key of the preferences. It cannot be empty.
112      * @param defValue Indicates the default value of the preferences.
113      *
114      * @return Returns a int value matching the specified key if it is found; returns the default value otherwise.
115      */
116     virtual int GetInt(const std::string &key, const int &defValue = {}) = 0;
117 
118     /**
119      * @brief Obtains the string value of a preferences.
120      *
121      * This function is used to get string value of the corresponding key in the preference.
122      *
123      * @param key Indicates the key of the preferences. It cannot be empty.
124      * @param defValue Indicates the default value of the preferences.
125      *
126      * @return Returns string value matching the specified key if it is found; returns the default value otherwise.
127      */
128     virtual std::string GetString(const std::string &key, const std::string &defValue = {}) = 0;
129 
130     /**
131      * @brief Obtains the bool value of a preferences.
132      *
133      * This function is used to get a bool value of the corresponding key in the preference.
134      *
135      * @param key Indicates the key of the preferences. It cannot be empty.
136      * @param defValue Indicates the default value of the preferences.
137      *
138      * @return Returns a bool value matching the specified key if it is found; returns the default value otherwise.
139      */
140     virtual bool GetBool(const std::string &key, const bool &defValue = {}) = 0;
141 
142     /**
143      * @brief Obtains the float value of a preferences.
144      *
145      * This function is used to get a float value of the corresponding key in the preference.
146      *
147      * @param key Indicates the key of the preferences. It cannot be empty.
148      * @param defValue Indicates the default value of the preferences.
149      *
150      * @return Returns a float value matching the specified key if it is found; returns the default value otherwise.
151      */
152     virtual float GetFloat(const std::string &key, const float &defValue = {}) = 0;
153 
154     /**
155      * @brief Obtains the double value of a preferences.
156      *
157      * This function is used to get a double value of the corresponding key in the preference.
158      *
159      * @param key Indicates the key of the preferences. It cannot be empty.
160      * @param defValue Indicates the default value of the preferences.
161      *
162      * @return Returns a double value matching the specified key if it is found; returns the default value otherwise.
163      */
164     virtual double GetDouble(const std::string &key, const double &defValue = {}) = 0;
165 
166     /**
167      * @brief Obtains the long value of a preferences.
168      *
169      * This function is used to get a long value of the corresponding key in the preference.
170      *
171      * @param key Indicates the key of the preferences. It cannot be empty.
172      * @param defValue Indicates the default value of the preferences.
173      *
174      * @return Returns a long value matching the specified key if it is found; returns the default value otherwise.
175      */
176     virtual int64_t GetLong(const std::string &key, const int64_t &defValue = {}) = 0;
177 
178     /**
179      * @brief Obtains all the keys and values of a preferences.
180      *
181      * This function is used to get all keys and values in an object.
182      *
183      * @return Returns a map, the key is string type and the value is PreferencesValue type.
184      */
185     virtual std::map<std::string, PreferencesValue> GetAll() = 0;
186 
187     /**
188      * @brief Checks whether contains a preferences matching a specified key.
189      *
190      * This function is used to Checks whether contains a preferences matching a specified key.
191      *
192      * @param key Indicates the key of the preferences. It cannot be empty.
193      *
194      * @return Returning true means it contains, false means it doesn't.
195      */
196     virtual bool HasKey(const std::string &key) = 0;
197 
198     /**
199      * @brief Put or update an int value of a preferences.
200      *
201      * This function is used to put or update an int value of the corresponding key in the preference.
202      *
203      * @param key Indicates the key of the preferences. It cannot be empty.
204      * @param value Indicates the value of preferences to put or update.
205      *
206      * @return Returns 0 for success, others for failure.
207      */
208     virtual int PutInt(const std::string &key, int value) = 0;
209 
210     /**
211      * @brief Put or update an string value for the key.
212      *
213      * This function is used to put or update string value of the corresponding key in the preference.
214      *
215      * @param key Indicates the key of the preferences. It cannot be empty.
216      * @param value Indicates the value of preferences to put or update.
217      *
218      * @return Returns 0 for success, others for failure.
219      */
220     virtual int PutString(const std::string &key, const std::string &value) = 0;
221 
222     /**
223      * @brief Put or update bool string value for the key.
224      *
225      * This function is used to put or update a bool value of the corresponding key in the preference.
226      *
227      * @param key Indicates the key of the preferences. It cannot be empty.
228      * @param value Indicates the value of preferences to put or update.
229      *
230      * @return Returns 0 for success, others for failure.
231      */
232     virtual int PutBool(const std::string &key, bool value) = 0;
233 
234     /**
235      * @brief Put or update an long value for the key.
236      *
237      * This function is used to put or update a long value of the corresponding key in the preference.
238      *
239      * @param key Indicates the key of the preferences. It cannot be empty.
240      * @param value Indicates the value of preferences to put or update.
241      *
242      * @return Returns 0 for success, others for failure.
243      */
244     virtual int PutLong(const std::string &key, int64_t value) = 0;
245 
246     /**
247      * @brief Put or update an float value for the key.
248      *
249      * This function is used to put or update a float value of the corresponding key in the preference.
250      *
251      * @param key Indicates the key of the preferences. It cannot be empty.
252      * @param value Indicates the value of preferences to put or update.
253      *
254      * @return Returns 0 for success, others for failure.
255      */
256     virtual int PutFloat(const std::string &key, float value) = 0;
257 
258     /**
259      * @brief Put or update an double value for the key.
260      *
261      * This function is used to put or update a double value of the corresponding key in the preference.
262      *
263      * @param key Indicates the key of the preferences. It cannot be empty.
264      * @param value Indicates the value of preferences to put or update.
265      *
266      * @return Returns 0 for success, others for failure.
267      */
268     virtual int PutDouble(const std::string &key, double value) = 0;
269 
270     /**
271      * @brief Deletes the preferences with a specified key.
272      *
273      * This function is used to delete the preferences with a specified key in the preference.
274      *
275      * @param key Indicates the key of the preferences. It cannot be empty.
276      *
277      * @return Returns 0 for success, others for failure.
278      */
279     virtual int Delete(const std::string &key) = 0;
280 
281     /**
282      * @brief Clears all preferences.
283      *
284      * This function is used to clear all preferences in an object.
285      *
286      * @return Returns 0 for success, others for failure.
287      */
288     virtual int Clear() = 0;
289 
290     /**
291      * @brief Asynchronously saves the preferences to the file.
292      *
293      * This function is used to saves the preferences to the file. Files are written to disk only after
294      * this interface or {@link FlushSync}is called.
295      */
296     virtual void Flush() = 0;
297 
298     /**
299      * @brief Synchronously saves the preferences to the file.
300      *
301      * This function is used to saves the preferences to the file synchronously. Files are written to disk only after
302      * this interface or {@link Flush} is called.
303      *
304      * @return The result of write to disk. Returns 0 for success, others for failure.
305      */
306     virtual int FlushSync() = 0;
307 
308     /**
309      * @brief  Registers an observer.
310      *
311      * This function is used to registers an observer to listen for the change of a preferences.
312      *
313      * @param preferencesObserver Indicates callback function for data changes.
314      */
315     virtual int RegisterObserver(
316         std::shared_ptr<PreferencesObserver> preferencesObserver, RegisterMode mode = RegisterMode::LOCAL_CHANGE) = 0;
317 
318     int Subscribe(std::shared_ptr<PreferencesObserver> observer, RegisterMode mode = RegisterMode::LOCAL_CHANGE,
319         const std::vector<std::string> &keys = {})
320     {
321         switch (mode) {
322             case RegisterMode::LOCAL_CHANGE:
323             case RegisterMode::MULTI_PRECESS_CHANGE:
324                 return RegisterObserver(observer, mode);
325             case RegisterMode::DATA_CHANGE:
326                 return RegisterDataObserver(observer, keys);
327             default:
328                 break;
329         }
330         return E_INVALID_ARGS;
331     }
332 
333     /**
334      * @brief  Unregister an existing observer.
335      *
336      * This function is used to unregister an existing observer.
337      *
338      * @param preferencesObserver Indicates callback function for data changes.
339      */
340     virtual int UnRegisterObserver(
341         std::shared_ptr<PreferencesObserver> preferencesObserver, RegisterMode mode = RegisterMode::LOCAL_CHANGE) = 0;
342 
343     int Unsubscribe(std::shared_ptr<PreferencesObserver> observer, RegisterMode mode = RegisterMode::LOCAL_CHANGE,
344         const std::vector<std::string> &keys = {})
345     {
346         switch (mode) {
347             case RegisterMode::LOCAL_CHANGE:
348             case RegisterMode::MULTI_PRECESS_CHANGE:
349                 return UnRegisterObserver(observer, mode);
350             case RegisterMode::DATA_CHANGE:
351                 return UnRegisterDataObserver(observer, keys);
352             default:
353                 break;
354         }
355         return E_INVALID_ARGS;
356     }
357 
358     /**
359      * @brief  Get group id.
360      *
361      * This function is used to Get group id.
362      *
363      * @return Returns the groupId when it exists, otherwise returns an empty string.
364      */
GetGroupId()365     virtual std::string GetGroupId() const
366     {
367         return "";
368     }
369 
CloseDb()370     virtual int CloseDb()
371     {
372         return E_OK;
373     }
374 
375     /**
376      * @brief  Registers a data observer.
377      *
378      * This function is used to registers an observer to listen for changes in data based on the keys
379      *
380      * @param preferencesObserver Indicates callback function for data changes.
381      */
382     virtual int RegisterDataObserver(
383         std::shared_ptr<PreferencesObserver> preferencesObserver, const std::vector<std::string> &keys = {})
384     {
385         return E_OK;
386     }
387 
388     /**
389      * @brief  Unregister an existing observer.
390      *
391      * This function is used to unregister an existing observer based on the keys
392      *
393      * @param preferencesObserver Indicates callback function for data changes.
394      */
395     virtual int UnRegisterDataObserver(
396         std::shared_ptr<PreferencesObserver> preferencesObserver, const std::vector<std::string> &keys = {})
397     {
398         return E_OK;
399     }
400 
401     /**
402      * @brief Obtains the value of a preferences.
403      *
404      * This function is used to get the value of the corresponding key in the preference.
405      *
406      * @param key Indicates the key of the preferences. It cannot be empty.
407      * @param defValue Indicates the default value of the preferences.
408      *
409      * @return Returns a pair, the first is 0 for success, others for failure.
410      */
GetValue(const std::string & key,const PreferencesValue & defValue)411     virtual std::pair<int, PreferencesValue> GetValue(const std::string &key, const PreferencesValue &defValue)
412     {
413         return {E_OK, defValue};
414     }
415 
416     /**
417      * @brief Obtains all the keys and values of a preferences.
418      *
419      * This function is used to get all keys and values in an object.
420      *
421      * @return Returns a pair, the first is 0 for success, others for failure.
422      */
GetAllData()423     virtual std::pair<int, std::map<std::string, PreferencesValue>> GetAllData()
424     {
425         return {E_OK, {}};
426     }
427 
428     /**
429      * @brief  Get Bundle Name.
430      *
431      * This function is used to Get Bundle Name.
432      *
433      * @return Returns the bundleName when it exists, otherwise returns an empty string.
434      */
GetBundleName()435     virtual std::string GetBundleName() const
436     {
437         return "";
438     }
439 
440     /**
441      * @brief Obtains all the keys and values of a preferences.
442      *
443      * This function is used to get all keys and values in an object.
444      *
445      * @return Returns a unordered_map, the key is string type and the value is PreferencesValue type.
446      */
GetAllDatas()447     virtual std::unordered_map<std::string, PreferencesValue> GetAllDatas()
448     {
449         return {};
450     }
451 };
452 } // End of namespace NativePreferences
453 } // End of namespace OHOS
454 #endif // End of #ifndef PREFERENCES_H