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