/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PREFERENCES_IMPL_H #define PREFERENCES_IMPL_H #include #include #include #include #include #include #include #include #include #include "preferences.h" #include "preferences_observer.h" #include "preferences_value.h" #include "task_pool.h" namespace OHOS { namespace NativePreferences { class PreferencesImpl : public Preferences { public: PreferencesImpl(const std::filesystem::path &path); virtual ~PreferencesImpl(); int Init(); virtual int GetInt(const std::string &key, int defValue) override; virtual std::string GetString(const std::string &key, const std::string &defValue) override; virtual bool GetBool(const std::string &key, bool defValue) override; virtual float GetFloat(const std::string &key, float defValue) override; virtual double GetDouble(const std::string &key, double defValue) override; virtual int64_t GetLong(const std::string &key, int64_t defValue) override; virtual std::set GetStringSet(const std::string &key, std::set &defValue) override; virtual bool HasKey(const std::string &key) override; virtual int PutInt(const std::string &key, int value) override; virtual int PutString(const std::string &key, const std::string &value) override; virtual int PutBool(const std::string &key, bool value) override; virtual int PutLong(const std::string &key, int64_t value) override; virtual int PutFloat(const std::string &key, float value) override; virtual int PutDouble(const std::string &key, double value) override; virtual int PutStringSet(const std::string &key, const std::set &value) override; virtual std::map GetAll() override; virtual int Delete(const std::string &key) override; virtual int Clear() override; virtual void Flush() override; virtual int FlushSync() override; virtual void RegisterObserver(std::shared_ptr preferencesObserver) override; virtual void UnRegisterObserver(std::shared_ptr preferencesObserver) override; static std::filesystem::path MakeBackupPath(const std::filesystem::path &prefPath); static std::filesystem::path MakeBrokenPath(const std::filesystem::path &prefPath); private: class MemoryToDiskRequest { public: MemoryToDiskRequest(const std::map &writeToDiskMap, const std::list &keysModified, const std::vector> preferencesObservers, int64_t memStataGeneration); ~MemoryToDiskRequest(){}; void SetDiskWriteResult(bool wasWritten, int result); bool isSyncRequest_; int64_t memoryStateGeneration_; std::map writeToDiskMap_; std::mutex reqMutex_; std::condition_variable reqCond_; std::list keysModified_; std::vector> preferencesObservers_; int writeToDiskResult_; bool wasWritten_; bool handled_; }; std::shared_ptr commitToMemory(); void notifyPreferencesObserver(const MemoryToDiskRequest &request); void PutPreferencesValue(const std::string &key, const PreferencesValue &value); void StartLoadFromDisk(); int CheckKey(const std::string &key); int CheckStringValue(const std::string &value); /* thread function */ static void LoadFromDisk(PreferencesImpl &pref); void AwaitLoadFile(); void WriteToDiskFile(std::shared_ptr mcr); bool CheckRequestValidForStateGeneration(const MemoryToDiskRequest &mcr); bool ReadSettingXml(const std::filesystem::path &prefPath, std::map &prefMap); bool WriteSettingXml(const std::filesystem::path &prefPath, const std::map &prefMap); bool loaded_; /* Current memory state (always increasing) */ int64_t currentMemoryStateGeneration_; /* Latest memory state that was committed to disk */ int64_t diskStateGeneration_; std::mutex mutex_; std::condition_variable cond_; std::vector> preferencesObservers_; std::map map_; std::list modifiedKeys_; const std::filesystem::path filePath_; const std::filesystem::path backupPath_; const std::filesystem::path brokenPath_; // Task pool /* max threads of the task pool. */ static constexpr int MAX_TP_THREADS = 10; /* min threads of the task pool. */ static constexpr int MIN_TP_THREADS = 1; TaskPool taskPool_; }; } // End of namespace NativePreferences } // End of namespace OHOS #endif // End of #ifndef PREFERENCES_IMPL_H