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