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_ENHANCE_IMPL_H
17 #define PREFERENCES_ENHANCE_IMPL_H
18 
19 #include <any>
20 #include <condition_variable>
21 #include <filesystem>
22 #include <list>
23 #include <map>
24 #include <string>
25 #include <vector>
26 #include <shared_mutex>
27 
28 #include "preferences_base.h"
29 #include "preferences_db_adapter.h"
30 
31 namespace OHOS {
32 namespace NativePreferences {
33 class PreferencesEnhanceImpl : public PreferencesBase, public std::enable_shared_from_this<PreferencesEnhanceImpl> {
34 public:
GetPreferences(const Options & options)35     static std::shared_ptr<PreferencesEnhanceImpl> GetPreferences(const Options &options)
36     {
37         return std::shared_ptr<PreferencesEnhanceImpl>(new PreferencesEnhanceImpl(options));
38     }
39     virtual ~PreferencesEnhanceImpl();
40 
41     int Init();
42 
43     PreferencesValue Get(const std::string &key, const PreferencesValue &defValue) override;
44 
45     int Put(const std::string &key, const PreferencesValue &value) override;
46 
47     bool HasKey(const std::string &key) override;
48 
49     std::map<std::string, PreferencesValue> GetAll() override;
50 
51     int Delete(const std::string &key) override;
52 
53     int Clear() override;
54 
55     int CloseDb() override;
56 
57     std::pair<int, PreferencesValue> GetValue(const std::string &key, const PreferencesValue &defValue) override;
58 
59     std::pair<int, std::map<std::string, PreferencesValue>> GetAllData() override;
60 private:
61     explicit PreferencesEnhanceImpl(const Options &options);
62     static void NotifyPreferencesObserver(std::shared_ptr<PreferencesEnhanceImpl> pref, const std::string &key,
63         const PreferencesValue &value);
64     static void NotifyPreferencesObserverBatchKeys(std::shared_ptr<PreferencesEnhanceImpl> pref,
65         const std::map<std::string, PreferencesValue> &data);
66     std::pair<int, std::map<std::string, PreferencesValue>> GetAllInner();
67 
68     std::shared_mutex dbMutex_;
69     std::shared_ptr<PreferencesDb> db_;
70     std::shared_mutex mapSharedMutex_;
71 };
72 } // End of namespace NativePreferences
73 } // End of namespace OHOS
74 #endif // End of #ifndef PREFERENCES_IMPL_H
75