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