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