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_IMPL_H
17 #define PREFERENCES_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 #include "safe_block_queue.h"
28 #include "concurrent_map.h"
29 #include "preferences_base.h"
30 
31 namespace OHOS {
32 namespace NativePreferences {
33 class PreferencesImpl : public PreferencesBase, public std::enable_shared_from_this<PreferencesImpl> {
34 public:
GetPreferences(const Options & options)35     static std::shared_ptr<PreferencesImpl> GetPreferences(const Options &options)
36     {
37         return std::shared_ptr<PreferencesImpl>(new PreferencesImpl(options));
38     }
39     virtual ~PreferencesImpl();
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     void Flush() override;
56 
57     int FlushSync() override;
58 
59     std::pair<int, PreferencesValue> GetValue(const std::string &key, const PreferencesValue &defValue) override;
60 
61     std::pair<int, std::map<std::string, PreferencesValue>> GetAllData() override;
62 private:
63     explicit PreferencesImpl(const Options &options);
64 
65     void NotifyPreferencesObserver(const std::list<std::string> &keysModified,
66         const std::map<std::string, PreferencesValue> &writeToDiskMap);
67     bool StartLoadFromDisk();
68 
69     /* thread function */
70     static void LoadFromDisk(std::shared_ptr<PreferencesImpl> pref);
71     void AwaitLoadFile();
72     bool WriteSettingXml(const Options &options, const std::map<std::string, PreferencesValue> &writeToDiskMap);
73     static int WriteToDiskFile(std::shared_ptr<PreferencesImpl> pref);
74     static bool ReadSettingXml(std::shared_ptr<PreferencesImpl> pref);
75 
76     std::atomic<bool> loaded_;
77 
78     /* Current memory state (always increasing) */
79     int64_t currentMemoryStateGeneration_;
80     /* Latest memory state that was committed to disk */
81     int64_t diskStateGeneration_;
82 
83     std::list<std::string> modifiedKeys_;
84 
85     ConcurrentMap<std::string, PreferencesValue> valuesCache_;
86 
87     std::shared_ptr<SafeBlockQueue<uint64_t>> queue_;
88 };
89 } // End of namespace NativePreferences
90 } // End of namespace OHOS
91 #endif // End of #ifndef PREFERENCES_IMPL_H