1fc223305Sopenharmony_ci/*
2fc223305Sopenharmony_ci * Copyright (c) 2024 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_STORE_IMPL_H
17fc223305Sopenharmony_ci#define PREFERENCES_STORE_IMPL_H
18fc223305Sopenharmony_ci
19fc223305Sopenharmony_ci#include <cstdint>
20fc223305Sopenharmony_ci#include <shared_mutex>
21fc223305Sopenharmony_ci#include "oh_preferences.h"
22fc223305Sopenharmony_ci#include "preferences_observer.h"
23fc223305Sopenharmony_ci#include "preferences.h"
24fc223305Sopenharmony_ci#include "log_print.h"
25fc223305Sopenharmony_ci
26fc223305Sopenharmony_cienum PreferencesNdkStructId : std::int64_t {
27fc223305Sopenharmony_ci    PREFERENCES_OH_OPTION_CID = 1002931,
28fc223305Sopenharmony_ci    PREFERENCES_OH_PREFERENCES_CID,
29fc223305Sopenharmony_ci    PREFERENCES_OH_VALUE_CID,
30fc223305Sopenharmony_ci    PREFERENCES_OH_PAIR_CID
31fc223305Sopenharmony_ci};
32fc223305Sopenharmony_ci
33fc223305Sopenharmony_ciclass NDKPreferencesObserver : public OHOS::NativePreferences::PreferencesObserver {
34fc223305Sopenharmony_cipublic:
35fc223305Sopenharmony_ci    NDKPreferencesObserver(OH_PreferencesDataObserver observer, void *context);
36fc223305Sopenharmony_ci    ~NDKPreferencesObserver() noexcept override = default;
37fc223305Sopenharmony_ci
38fc223305Sopenharmony_ci    void OnChange(const std::string &key) override;
39fc223305Sopenharmony_ci    void OnChange(const std::map<std::string, OHOS::NativePreferences::PreferencesValue> &records) override;
40fc223305Sopenharmony_ci
41fc223305Sopenharmony_ci    bool ObserverCompare(OH_PreferencesDataObserver other);
42fc223305Sopenharmony_ci
43fc223305Sopenharmony_ci    OH_PreferencesDataObserver dataObserver_;
44fc223305Sopenharmony_ci    void *context_;
45fc223305Sopenharmony_ci};
46fc223305Sopenharmony_ci
47fc223305Sopenharmony_cistruct OH_Preferences {
48fc223305Sopenharmony_ci    int64_t cid;
49fc223305Sopenharmony_ci};
50fc223305Sopenharmony_ci
51fc223305Sopenharmony_ciclass OH_PreferencesImpl : public OH_Preferences {
52fc223305Sopenharmony_cipublic:
53fc223305Sopenharmony_ci    explicit OH_PreferencesImpl(std::shared_ptr<OHOS::NativePreferences::Preferences> preferences);
54fc223305Sopenharmony_ci    ~OH_PreferencesImpl()
55fc223305Sopenharmony_ci    {
56fc223305Sopenharmony_ci        preferences_ = nullptr;
57fc223305Sopenharmony_ci    }
58fc223305Sopenharmony_ci    std::shared_ptr<OHOS::NativePreferences::Preferences> GetNativePreferences()
59fc223305Sopenharmony_ci    {
60fc223305Sopenharmony_ci        return preferences_;
61fc223305Sopenharmony_ci    }
62fc223305Sopenharmony_ci
63fc223305Sopenharmony_ci    int RegisterDataObserver(
64fc223305Sopenharmony_ci        OH_PreferencesDataObserver observer, void *context, const std::vector<std::string> &keys = {});
65fc223305Sopenharmony_ci    int UnregisterDataObserver(OH_PreferencesDataObserver observer, void *context,
66fc223305Sopenharmony_ci        const std::vector<std::string> &keys = {});
67fc223305Sopenharmony_ci
68fc223305Sopenharmony_ci    void SetPreferencesStoreFilePath(const std::string &filePath);
69fc223305Sopenharmony_ci    std::string GetPreferencesStoreFilePath();
70fc223305Sopenharmony_ci
71fc223305Sopenharmony_ciprivate:
72fc223305Sopenharmony_ci    std::shared_ptr<OHOS::NativePreferences::Preferences> preferences_;
73fc223305Sopenharmony_ci    std::shared_mutex mutex_;
74fc223305Sopenharmony_ci    std::string filePath_ = "";
75fc223305Sopenharmony_ci    std::shared_mutex obsMutex_;
76fc223305Sopenharmony_ci    std::vector<std::pair<std::shared_ptr<NDKPreferencesObserver>, void *>> dataObservers_;
77fc223305Sopenharmony_ci};
78fc223305Sopenharmony_ci
79fc223305Sopenharmony_cistruct OH_PreferencesOption {
80fc223305Sopenharmony_ci    int64_t cid;
81fc223305Sopenharmony_ci    std::string fileName = "";
82fc223305Sopenharmony_ci    std::string bundleName = "";
83fc223305Sopenharmony_ci    std::string dataGroupId = "";
84fc223305Sopenharmony_ci    std::shared_mutex opMutex_;
85fc223305Sopenharmony_ci    int SetFileName(const std::string &str);
86fc223305Sopenharmony_ci    void SetBundleName(const std::string &str);
87fc223305Sopenharmony_ci    void SetDataGroupId(const std::string &str);
88fc223305Sopenharmony_ci    std::string GetFileName();
89fc223305Sopenharmony_ci    std::string GetBundleName();
90fc223305Sopenharmony_ci    std::string GetDataGroupId();
91fc223305Sopenharmony_ci};
92fc223305Sopenharmony_ci
93fc223305Sopenharmony_ciclass NDKPreferencesUtils {
94fc223305Sopenharmony_cipublic:
95fc223305Sopenharmony_ci    static bool PreferencesStructValidCheck(int64_t cid, int64_t structCid);
96fc223305Sopenharmony_ci};
97fc223305Sopenharmony_ci
98fc223305Sopenharmony_ci#endif // PREFERENCES_STORE_IMPL_H