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 158 static std::string MakeFilePath(const std::string &prefPath, const std::string &suffix); 159 160 private: 161 explicit PreferencesImpl(const Options &options); 162 class MemoryToDiskRequest { 163 public: 164 MemoryToDiskRequest(const std::map<std::string, PreferencesValue> &writeToDiskMap, 165 const std::list<std::string> &keysModified, 166 const std::vector<std::weak_ptr<PreferencesObserver>> preferencesObservers, int64_t memStataGeneration); ~MemoryToDiskRequest()167 ~MemoryToDiskRequest() {} 168 void SetDiskWriteResult(bool wasWritten, int result); 169 170 bool isSyncRequest_; 171 int64_t memoryStateGeneration_; 172 std::map<std::string, PreferencesValue> writeToDiskMap_; 173 std::mutex reqMutex_; 174 std::condition_variable reqCond_; 175 std::list<std::string> keysModified_; 176 std::vector<std::weak_ptr<PreferencesObserver>> localObservers_; 177 178 int writeToDiskResult_; 179 bool wasWritten_; 180 }; 181 182 std::shared_ptr<MemoryToDiskRequest> commitToMemory(); 183 void notifyPreferencesObserver(const MemoryToDiskRequest &request); 184 bool StartLoadFromDisk(); 185 int CheckKey(const std::string &key); 186 int CheckStringValue(const std::string &value); 187 Uri MakeUri(const std::string &key = ""); 188 189 /* thread function */ 190 static void LoadFromDisk(std::shared_ptr<PreferencesImpl> pref); 191 void AwaitLoadFile(); 192 static void WriteToDiskFile(std::shared_ptr<PreferencesImpl> pref, std::shared_ptr<MemoryToDiskRequest> mcr); 193 bool CheckRequestValidForStateGeneration(std::shared_ptr<MemoryToDiskRequest> mcr); 194 195 bool ReadSettingXml(const std::string &prefPath, std::map<std::string, PreferencesValue> &prefMap); 196 bool WriteSettingXml(const std::string &prefPath, const std::map<std::string, PreferencesValue> &prefMap); 197 198 bool loaded_; 199 200 /* Current memory state (always increasing) */ 201 int64_t currentMemoryStateGeneration_; 202 /* Latest memory state that was committed to disk */ 203 int64_t diskStateGeneration_; 204 205 std::mutex mutex_; 206 std::condition_variable cond_; 207 208 std::vector<std::weak_ptr<PreferencesObserver>> localObservers_; 209 std::vector<sptr<DataPreferencesObserverStub>> multiProcessObservers_; 210 std::map<std::string, PreferencesValue> map_; 211 std::list<std::string> modifiedKeys_; 212 static ExecutorPool executorPool_; 213 214 const Options options_; 215 }; 216 } // End of namespace NativePreferences 217 } // End of namespace OHOS 218 #endif // End of #ifndef PREFERENCES_IMPL_H 219