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_HELPER_H
17 #define PREFERENCES_HELPER_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 
24 #include "preferences.h"
25 
26 namespace OHOS {
27 namespace NativePreferences {
28 /**
29  * The observer class of preferences. This class is used to obtain and delete preferences instances.
30  */
31 class PREF_API_EXPORT PreferencesHelper {
32 public:
33 
34     /**
35      * @brief Obtains a preferences instance matching a specified preferences file name.
36      *
37      * The preferences instance does not load data from the specified file every time. This is because if The
38      * preferences instance is being used in another thread, it will be cached until it will no longer be used to and
39      * performed {@link RemovePreferencesFromCache}.
40      *
41      * @param options Indicates the preferences configuration
42      * @param errCode Indicates the error code. Returns 0 for success, others for failure.
43      *
44      * @return Returns a Preferences instance matching the specified preferences file name.
45      */
46     PREF_API_EXPORT static std::shared_ptr<Preferences> GetPreferences(const Options &options, int &errCode);
47 
48     /**
49      * @brief Deletes a preferences instance matching a specified preferences file name.
50      *
51      * Calling this interface will delete both the preferences instance in the cache and the corresponding file on disk.
52      * If you only want to remove preferences instances from cache, call interface {@link RemovePreferencesFromCache}.
53      *
54      * @param path Indicates the preferences file name.
55      *
56      * @return Returns 0 for success, others for failure.
57      */
58     PREF_API_EXPORT static int DeletePreferences(const std::string &path);
59 
60     /**
61      * @brief Remove a preferences instance matching a specified preferences file name from cache.
62      *
63      * This function is used to remove a preferences instance matching a specified preferences file name from cache.
64      *
65      * @param path Indicates the preferences file name
66      *
67      * @return Returns 0 for success, others for failure.
68      */
69     PREF_API_EXPORT static int RemovePreferencesFromCache(const std::string &path);
70 
71 private:
72     // use bool to mark whether Preferences is EnhancePreferences or not
73     static std::map<std::string, std::pair<std::shared_ptr<Preferences>, bool>> prefsCache_;
74     static std::mutex prefsCacheMutex_;
75 
76     static std::string GetRealPath(const std::string &path, int &errorCode);
77 };
78 } // End of namespace NativePreferences
79 } // End of namespace OHOS
80 #endif // End of #ifndef PREFERENCES_HELPER_H
81