• 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 namespace OHOS {
33 template <typename T> class sptr;
34 class Uri;
35 namespace NativePreferences {
36 class DataPreferencesObserverStub;
37 class ExecutorPool;
38 static const char *STR_BROKEN = ".broken";
39 static const char *STR_BACKUP = ".bak";
40 static const char *STR_LOCK = ".lock";
41 static const char *STR_QUERY = "?";
42 static const char *STR_SLASH = "/";
43 static const char *STR_SCHEME = "sharepreferences://";
44 class PreferencesImpl : public Preferences, public std::enable_shared_from_this<PreferencesImpl> {
45 public:
GetPreferences(const Options & options)46     static std::shared_ptr<PreferencesImpl> GetPreferences(const Options &options)
47     {
48         return std::shared_ptr<PreferencesImpl>(new PreferencesImpl(options));
49     }
50     virtual ~PreferencesImpl();
51 
52     int Init();
53 
54     PreferencesValue Get(const std::string &key, const PreferencesValue &defValue) override;
55 
56     int Put(const std::string &key, const PreferencesValue &value) override;
57 
GetInt(const std::string & key,const int & defValue)58     int GetInt(const std::string &key, const int &defValue) override
59     {
60         PreferencesValue preferencesValue = Get(key, defValue);
61         if (!preferencesValue.IsInt()) {
62             return defValue;
63         }
64         return preferencesValue;
65     }
66 
GetString(const std::string & key,const std::string & defValue)67     std::string GetString(const std::string &key, const std::string &defValue) override
68     {
69         PreferencesValue preferencesValue = Get(key, defValue);
70         if (!preferencesValue.IsString()) {
71             return defValue;
72         }
73         return preferencesValue;
74     }
75 
GetBool(const std::string & key,const bool & defValue)76     bool GetBool(const std::string &key, const bool &defValue) override
77     {
78         PreferencesValue preferencesValue = Get(key, defValue);
79         if (!preferencesValue.IsBool()) {
80             return defValue;
81         }
82         return preferencesValue;
83     }
84 
GetFloat(const std::string & key,const float & defValue)85     float GetFloat(const std::string &key, const float &defValue) override
86     {
87         PreferencesValue preferencesValue = Get(key, defValue);
88         if (!preferencesValue.IsFloat()) {
89             return defValue;
90         }
91         return preferencesValue;
92     }
93 
GetDouble(const std::string & key,const double & defValue)94     double GetDouble(const std::string &key, const double &defValue) override
95     {
96         PreferencesValue preferencesValue = Get(key, defValue);
97         if (!preferencesValue.IsDouble()) {
98             return defValue;
99         }
100         return preferencesValue;
101     }
102 
GetLong(const std::string & key,const int64_t & defValue)103     int64_t GetLong(const std::string &key, const int64_t &defValue) override
104     {
105         PreferencesValue preferencesValue = Get(key, defValue);
106         if (!preferencesValue.IsLong()) {
107             return defValue;
108         }
109         return preferencesValue;
110     }
111 
112     bool HasKey(const std::string &key) override;
113 
PutInt(const std::string & key,int value)114     int PutInt(const std::string &key, int value) override
115     {
116         return Put(key, value);
117     }
118 
PutString(const std::string & key,const std::string & value)119     int PutString(const std::string &key, const std::string &value) override
120     {
121         return Put(key, value);
122     }
123 
PutBool(const std::string & key,bool value)124     int PutBool(const std::string &key, bool value) override
125     {
126         return Put(key, value);
127     }
128 
PutLong(const std::string & key,int64_t value)129     int PutLong(const std::string &key, int64_t value) override
130     {
131         return Put(key, value);
132     }
133 
PutFloat(const std::string & key,float value)134     int PutFloat(const std::string &key, float value) override
135     {
136         return Put(key, value);
137     }
138 
PutDouble(const std::string & key,double value)139     int PutDouble(const std::string &key, double value) override
140     {
141         return Put(key, value);
142     }
143 
144     std::map<std::string, PreferencesValue> GetAll() override;
145 
146     int Delete(const std::string &key) override;
147 
148     int Clear() override;
149 
150     void Flush() override;
151 
152     int FlushSync() override;
153 
154     int RegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver, RegisterMode mode) override;
155 
156     int UnRegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver, RegisterMode mode) override;
157 
GetGroupId()158     std::string GetGroupId() const override
159     {
160         return options_.dataGroupId;
161     }
162 
163     static std::string MakeFilePath(const std::string &prefPath, const std::string &suffix);
164 
165 private:
166     explicit PreferencesImpl(const Options &options);
167     class MemoryToDiskRequest {
168     public:
169         MemoryToDiskRequest(const std::map<std::string, PreferencesValue> &writeToDiskMap,
170             const std::list<std::string> &keysModified,
171             const std::vector<std::weak_ptr<PreferencesObserver>> preferencesObservers, int64_t memStataGeneration);
~MemoryToDiskRequest()172         ~MemoryToDiskRequest() {}
173         void SetDiskWriteResult(bool wasWritten, int result);
174 
175         bool isSyncRequest_;
176         int64_t memoryStateGeneration_;
177         std::map<std::string, PreferencesValue> writeToDiskMap_;
178         std::condition_variable reqCond_;
179         std::list<std::string> keysModified_;
180         std::vector<std::weak_ptr<PreferencesObserver>> localObservers_;
181 
182         int writeToDiskResult_;
183         bool wasWritten_;
184     };
185 
186     std::shared_ptr<MemoryToDiskRequest> commitToMemory();
187     void notifyPreferencesObserver(const MemoryToDiskRequest &request);
188     bool StartLoadFromDisk();
189     int CheckKey(const std::string &key);
190     int CheckStringValue(const std::string &value);
191     Uri MakeUri(const std::string &key = "");
192 
193     /* thread function */
194     static void LoadFromDisk(std::shared_ptr<PreferencesImpl> pref);
195     void AwaitLoadFile();
196     static void WriteToDiskFile(std::shared_ptr<PreferencesImpl> pref, std::shared_ptr<MemoryToDiskRequest> mcr);
197     bool CheckRequestValidForStateGeneration(std::shared_ptr<MemoryToDiskRequest> mcr);
198     bool ReadSettingXml(std::shared_ptr<PreferencesImpl> pref);
199     bool WriteSettingXml(std::shared_ptr<PreferencesImpl> pref, const std::map<std::string, PreferencesValue> &prefMap);
200 
201     bool loaded_;
202 
203     /* Current memory state (always increasing) */
204     int64_t currentMemoryStateGeneration_;
205     /* Latest memory state that was committed to disk */
206     int64_t diskStateGeneration_;
207 
208     std::mutex mutex_;
209     std::condition_variable cond_;
210 
211     std::vector<std::weak_ptr<PreferencesObserver>> localObservers_;
212     std::vector<sptr<DataPreferencesObserverStub>> multiProcessObservers_;
213     std::map<std::string, PreferencesValue> map_;
214     std::list<std::string> modifiedKeys_;
215     static ExecutorPool executorPool_;
216 
217     const Options options_;
218 };
219 } // End of namespace NativePreferences
220 } // End of namespace OHOS
221 #endif // End of #ifndef PREFERENCES_IMPL_H
222