1 /* 2 * Copyright (c) 2024 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_BASE_H 17 #define PREFERENCES_BASE_H 18 19 #include <any> 20 #include <condition_variable> 21 #include <list> 22 #include <map> 23 #include <memory> 24 #include <mutex> 25 #include <set> 26 #include <string> 27 #include <vector> 28 #include <shared_mutex> 29 30 #include "preferences.h" 31 #include "preferences_observer.h" 32 #include "preferences_utils.h" 33 34 namespace OHOS { 35 template <typename T> class sptr; 36 class Uri; 37 namespace NativePreferences { 38 class ExecutorPool; 39 class DataPreferencesObserverStub; 40 /** 41 * The function class of the preference. Various operations on preferences instances are provided in this class. 42 */ 43 class PreferencesBase : public Preferences { 44 public: 45 PreferencesBase(const Options &options); 46 ~PreferencesBase(); 47 48 PreferencesValue Get(const std::string &key, const PreferencesValue &defValue) override; 49 50 int Put(const std::string &key, const PreferencesValue &value) override; 51 52 int GetInt(const std::string &key, const int &defValue) override; 53 54 std::string GetString(const std::string &key, const std::string &defValue) override; 55 56 bool GetBool(const std::string &key, const bool &defValue) override; 57 58 float GetFloat(const std::string &key, const float &defValue) override; 59 60 double GetDouble(const std::string &key, const double &defValue) override; 61 62 int64_t GetLong(const std::string &key, const int64_t &defValue) override; 63 64 std::map<std::string, PreferencesValue> GetAll() override; 65 66 bool HasKey(const std::string &key) override; 67 68 int PutInt(const std::string &key, int value) override; 69 70 int PutString(const std::string &key, const std::string &value) override; 71 72 int PutBool(const std::string &key, bool value) override; 73 74 int PutLong(const std::string &key, int64_t value) override; 75 76 int PutFloat(const std::string &key, float value) override; 77 78 int PutDouble(const std::string &key, double value) override; 79 80 int Delete(const std::string &key) override; 81 82 int Clear() override; 83 84 void Flush() override; 85 86 int FlushSync() override; 87 88 int RegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver, 89 RegisterMode mode = RegisterMode::LOCAL_CHANGE) override; 90 91 int UnRegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver, 92 RegisterMode mode = RegisterMode::LOCAL_CHANGE) override; 93 94 int RegisterDataObserver(std::shared_ptr<PreferencesObserver> preferencesObserver, 95 const std::vector<std::string> &keys = {}) override; 96 97 int UnRegisterDataObserver(std::shared_ptr<PreferencesObserver> preferencesObserver, 98 const std::vector<std::string> &keys = {}) override; 99 100 std::string GetGroupId() const override; 101 102 int CloseDb() override; 103 104 std::pair<int, PreferencesValue> GetValue(const std::string &key, const PreferencesValue &defValue) override; 105 106 std::pair<int, std::map<std::string, PreferencesValue>> GetAllData() override; 107 108 protected: 109 Uri MakeUri(const std::string &key = ""); 110 struct WeakPtrCompare { operatorWeakPtrCompare111 bool operator()(const std::weak_ptr<PreferencesObserver> &a, const std::weak_ptr<PreferencesObserver> &b) const 112 { 113 return a.owner_before(b); 114 } 115 }; 116 using DataObserverMap = std::map<std::weak_ptr<PreferencesObserver>, std::set<std::string>, WeakPtrCompare>; 117 std::mutex mutex_; 118 std::shared_mutex obseverMetux_; 119 std::condition_variable cond_; 120 121 std::vector<std::weak_ptr<PreferencesObserver>> localObservers_; 122 std::vector<sptr<DataPreferencesObserverStub>> multiProcessObservers_; 123 DataObserverMap dataObserversMap_; 124 125 const Options options_; 126 127 static ExecutorPool executorPool_; 128 }; 129 } // End of namespace NativePreferences 130 } // End of namespace OHOS 131 #endif // End of #ifndef PREFERENCES_H