• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unordered_map>
24 #include <unordered_set>
25 #include <string>
26 #include <vector>
27 #include <shared_mutex>
28 #include "safe_block_queue.h"
29 #include "preferences_base.h"
30 #include "preferences_observer_stub.h"
31 
32 namespace OHOS {
33 namespace NativePreferences {
34 
35 class PreferencesImpl : public PreferencesBase, public std::enable_shared_from_this<PreferencesImpl> {
36 public:
GetPreferences(const Options & options)37     static std::shared_ptr<PreferencesImpl> GetPreferences(const Options &options)
38     {
39         return std::shared_ptr<PreferencesImpl>(new PreferencesImpl(options));
40     }
41     virtual ~PreferencesImpl();
42 
43     int Init();
44 
45     PreferencesValue Get(const std::string &key, const PreferencesValue &defValue) override;
46 
47     int Put(const std::string &key, const PreferencesValue &value) override;
48 
49     bool HasKey(const std::string &key) override;
50 
51     std::map<std::string, PreferencesValue> GetAll() override;
52 
53     int Delete(const std::string &key) override;
54 
55     int Clear() override;
56 
57     void Flush() override;
58 
59     int FlushSync() override;
60 
61     int Close() override;
62 
63     bool IsClose(const std::string &name) override;
64 
65     std::pair<int, PreferencesValue> GetValue(const std::string &key, const PreferencesValue &defValue) override;
66 
67     std::pair<int, std::map<std::string, PreferencesValue>> GetAllData() override;
68 
69     std::unordered_map<std::string, PreferencesValue> GetAllDatas() override;
70 private:
71     explicit PreferencesImpl(const Options &options);
72 
73     static void NotifyPreferencesObserver(std::shared_ptr<PreferencesImpl> pref,
74         std::shared_ptr<std::unordered_set<std::string>> keysModified,
75         std::shared_ptr<std::unordered_map<std::string, PreferencesValue>> writeToDisk);
76     bool StartLoadFromDisk();
77     bool PreLoad();
78 
79     /* thread function */
80     static void LoadFromDisk(std::shared_ptr<PreferencesImpl> pref);
81     bool ReloadFromDisk();
82     inline void AwaitLoadFile();
83     static int WriteToDiskFile(std::shared_ptr<PreferencesImpl> pref);
84     bool ReadSettingXml(std::unordered_map<std::string, PreferencesValue> &conMap);
85 
86     std::atomic<bool> loaded_;
87     bool isNeverUnlock_;
88     bool loadResult_;
89 
90     std::unordered_set<std::string> modifiedKeys_;
91 
92     std::atomic<bool> isCleared_;
93 
94     std::atomic<bool> isActive_;
95 
96     std::shared_mutex cacheMutex_;
97 
98     std::unordered_map<std::string, PreferencesValue> valuesCache_;
99 
100     std::shared_ptr<SafeBlockQueue<uint64_t>> queue_;
101 
102     std::shared_ptr<DataObsMgrClient> dataObsMgrClient_;
103 };
104 } // End of namespace NativePreferences
105 } // End of namespace OHOS
106 #endif // End of #ifndef PREFERENCES_IMPL_H