• 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 <map>
24 #include <mutex>
25 #include <set>
26 #include <string>
27 #include <vector>
28 
29 #include "preferences.h"
30 #include "preferences_observer.h"
31 #include "preferences_value.h"
32 #include "task_pool.h"
33 
34 namespace OHOS {
35 namespace NativePreferences {
36 class PreferencesImpl : public Preferences {
37 public:
38     explicit PreferencesImpl(const std::string &path);
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 
GetInt(const std::string & key,const int & defValue)47     int GetInt(const std::string &key, const int &defValue) override
48     {
49         PreferencesValue preferencesValue = Get(key, defValue);
50         if (!preferencesValue.IsInt()) {
51             return defValue;
52         }
53         return preferencesValue;
54     }
55 
GetString(const std::string & key,const std::string & defValue)56     std::string GetString(const std::string &key, const std::string &defValue) override
57     {
58         PreferencesValue preferencesValue = Get(key, defValue);
59         if (!preferencesValue.IsString()) {
60             return defValue;
61         }
62         return preferencesValue;
63     }
64 
GetBool(const std::string & key,const bool & defValue)65     bool GetBool(const std::string &key, const bool &defValue) override
66     {
67         PreferencesValue preferencesValue = Get(key, defValue);
68         if (!preferencesValue.IsBool()) {
69             return defValue;
70         }
71         return preferencesValue;
72     }
73 
GetFloat(const std::string & key,const float & defValue)74     float GetFloat(const std::string &key, const float &defValue) override
75     {
76         PreferencesValue preferencesValue = Get(key, defValue);
77         if (!preferencesValue.IsFloat()) {
78             return defValue;
79         }
80         return preferencesValue;
81     }
82 
GetDouble(const std::string & key,const double & defValue)83     double GetDouble(const std::string &key, const double &defValue) override
84     {
85         PreferencesValue preferencesValue = Get(key, defValue);
86         if (!preferencesValue.IsDouble()) {
87             return defValue;
88         }
89         return preferencesValue;
90     }
91 
GetLong(const std::string & key,const int64_t & defValue)92     int64_t GetLong(const std::string &key, const int64_t &defValue) override
93     {
94         PreferencesValue preferencesValue = Get(key, defValue);
95         if (!preferencesValue.IsLong()) {
96             return defValue;
97         }
98         return preferencesValue;
99     }
100 
101     bool HasKey(const std::string &key) override;
102 
PutInt(const std::string & key,int value)103     int PutInt(const std::string &key, int value) override
104     {
105         return Put(key, value);
106     }
107 
PutString(const std::string & key,const std::string & value)108     int PutString(const std::string &key, const std::string &value) override
109     {
110         return Put(key, value);
111     }
112 
PutBool(const std::string & key,bool value)113     int PutBool(const std::string &key, bool value) override
114     {
115         return Put(key, value);
116     }
117 
PutLong(const std::string & key,int64_t value)118     int PutLong(const std::string &key, int64_t value) override
119     {
120         return Put(key, value);
121     }
122 
PutFloat(const std::string & key,float value)123     int PutFloat(const std::string &key, float value) override
124     {
125         return Put(key, value);
126     }
127 
PutDouble(const std::string & key,double value)128     int PutDouble(const std::string &key, double value) override
129     {
130         return Put(key, value);
131     }
132 
133     std::map<std::string, PreferencesValue> GetAll() override;
134 
135     int Delete(const std::string &key) override;
136 
137     int Clear() override;
138 
139     void Flush() override;
140 
141     int FlushSync() override;
142 
143     void RegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver) override;
144 
145     void UnRegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver) override;
146 
147     static std::string MakeBackupPath(const std::string &prefPath);
148     static std::string MakeBrokenPath(const std::string &prefPath);
149 
150 private:
151     class MemoryToDiskRequest {
152     public:
153         MemoryToDiskRequest(const std::map<std::string, PreferencesValue> &writeToDiskMap,
154             const std::list<std::string> &keysModified,
155             const std::vector<std::weak_ptr<PreferencesObserver>> preferencesObservers, int64_t memStataGeneration);
~MemoryToDiskRequest()156         ~MemoryToDiskRequest()
157         {
158         }
159         void SetDiskWriteResult(bool wasWritten, int result);
160 
161         bool isSyncRequest_;
162         int64_t memoryStateGeneration_;
163         std::map<std::string, PreferencesValue> writeToDiskMap_;
164         std::mutex reqMutex_;
165         std::condition_variable reqCond_;
166         std::list<std::string> keysModified_;
167         std::vector<std::weak_ptr<PreferencesObserver>> preferencesObservers_;
168 
169         int writeToDiskResult_;
170         bool wasWritten_;
171         bool handled_;
172     };
173 
174     std::shared_ptr<MemoryToDiskRequest> commitToMemory();
175     void notifyPreferencesObserver(const MemoryToDiskRequest &request);
176     void StartLoadFromDisk();
177     int CheckKey(const std::string &key);
178     int CheckStringValue(const std::string &value);
179 
180     /* thread function */
181     static void LoadFromDisk(PreferencesImpl &pref);
182     void AwaitLoadFile();
183     void WriteToDiskFile(std::shared_ptr<MemoryToDiskRequest> mcr);
184     bool CheckRequestValidForStateGeneration(const MemoryToDiskRequest &mcr);
185 
186     bool ReadSettingXml(const std::string &prefPath, std::map<std::string, PreferencesValue> &prefMap);
187     bool WriteSettingXml(const std::string &prefPath, const std::map<std::string, PreferencesValue> &prefMap);
188 
189     bool loaded_;
190 
191     /* Current memory state (always increasing) */
192     int64_t currentMemoryStateGeneration_;
193     /* Latest memory state that was committed to disk */
194     int64_t diskStateGeneration_;
195 
196     std::mutex mutex_;
197     std::condition_variable cond_;
198 
199     std::vector<std::weak_ptr<PreferencesObserver>> preferencesObservers_;
200     std::map<std::string, PreferencesValue> map_;
201     std::list<std::string> modifiedKeys_;
202 
203     const std::string filePath_;
204     const std::string backupPath_;
205     const std::string brokenPath_;
206     // Task pool
207     /* max threads of the task pool. */
208     static constexpr int MAX_TP_THREADS = 10;
209     /* min threads of the task pool. */
210     static constexpr int MIN_TP_THREADS = 1;
211     TaskPool taskPool_;
212 };
213 } // End of namespace NativePreferences
214 } // End of namespace OHOS
215 #endif // End of #ifndef PREFERENCES_IMPL_H
216