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