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 PreferencesImpl(const std::filesystem::path &path); 39 virtual ~PreferencesImpl(); 40 41 int Init(); 42 43 virtual int GetInt(const std::string &key, int defValue) override; 44 45 virtual std::string GetString(const std::string &key, const std::string &defValue) override; 46 47 virtual bool GetBool(const std::string &key, bool defValue) override; 48 49 virtual float GetFloat(const std::string &key, float defValue) override; 50 51 virtual double GetDouble(const std::string &key, double defValue) override; 52 53 virtual int64_t GetLong(const std::string &key, int64_t defValue) override; 54 55 virtual std::set<std::string> GetStringSet(const std::string &key, std::set<std::string> &defValue) override; 56 57 virtual bool HasKey(const std::string &key) override; 58 59 virtual int PutInt(const std::string &key, int value) override; 60 61 virtual int PutString(const std::string &key, const std::string &value) override; 62 63 virtual int PutBool(const std::string &key, bool value) override; 64 65 virtual int PutLong(const std::string &key, int64_t value) override; 66 67 virtual int PutFloat(const std::string &key, float value) override; 68 69 virtual int PutDouble(const std::string &key, double value) override; 70 71 virtual int PutStringSet(const std::string &key, const std::set<std::string> &value) override; 72 73 virtual std::map<std::string, PreferencesValue> GetAll() override; 74 75 virtual int Delete(const std::string &key) override; 76 77 virtual int Clear() override; 78 79 virtual void Flush() override; 80 81 virtual int FlushSync() override; 82 83 virtual void RegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver) override; 84 85 virtual void UnRegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver) override; 86 87 static std::filesystem::path MakeBackupPath(const std::filesystem::path &prefPath); 88 static std::filesystem::path MakeBrokenPath(const std::filesystem::path &prefPath); 89 90 private: 91 class MemoryToDiskRequest { 92 public: 93 MemoryToDiskRequest(const std::map<std::string, PreferencesValue> &writeToDiskMap, 94 const std::list<std::string> &keysModified, 95 const std::vector<std::weak_ptr<PreferencesObserver>> preferencesObservers, int64_t memStataGeneration); ~MemoryToDiskRequest()96 ~MemoryToDiskRequest(){}; 97 void SetDiskWriteResult(bool wasWritten, int result); 98 99 bool isSyncRequest_; 100 int64_t memoryStateGeneration_; 101 std::map<std::string, PreferencesValue> writeToDiskMap_; 102 std::mutex reqMutex_; 103 std::condition_variable reqCond_; 104 std::list<std::string> keysModified_; 105 std::vector<std::weak_ptr<PreferencesObserver>> preferencesObservers_; 106 107 int writeToDiskResult_; 108 bool wasWritten_; 109 bool handled_; 110 }; 111 112 std::shared_ptr<MemoryToDiskRequest> commitToMemory(); 113 void notifyPreferencesObserver(const MemoryToDiskRequest &request); 114 void PutPreferencesValue(const std::string &key, const PreferencesValue &value); 115 void StartLoadFromDisk(); 116 int CheckKey(const std::string &key); 117 int CheckStringValue(const std::string &value); 118 119 /* thread function */ 120 static void LoadFromDisk(PreferencesImpl &pref); 121 void AwaitLoadFile(); 122 void WriteToDiskFile(std::shared_ptr<MemoryToDiskRequest> mcr); 123 bool CheckRequestValidForStateGeneration(const MemoryToDiskRequest &mcr); 124 125 bool ReadSettingXml(const std::filesystem::path &prefPath, std::map<std::string, PreferencesValue> &prefMap); 126 bool WriteSettingXml(const std::filesystem::path &prefPath, const std::map<std::string, PreferencesValue> &prefMap); 127 128 bool loaded_; 129 130 /* Current memory state (always increasing) */ 131 int64_t currentMemoryStateGeneration_; 132 /* Latest memory state that was committed to disk */ 133 int64_t diskStateGeneration_; 134 135 std::mutex mutex_; 136 std::condition_variable cond_; 137 138 std::vector<std::weak_ptr<PreferencesObserver>> preferencesObservers_; 139 std::map<std::string, PreferencesValue> map_; 140 std::list<std::string> modifiedKeys_; 141 142 const std::filesystem::path filePath_; 143 const std::filesystem::path backupPath_; 144 const std::filesystem::path brokenPath_; 145 // Task pool 146 /* max threads of the task pool. */ 147 static constexpr int MAX_TP_THREADS = 10; 148 /* min threads of the task pool. */ 149 static constexpr int MIN_TP_THREADS = 1; 150 TaskPool taskPool_; 151 }; 152 } // End of namespace NativePreferences 153 } // End of namespace OHOS 154 #endif // End of #ifndef PREFERENCES_IMPL_H 155