1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_PREFS_OVERLAY_USER_PREF_STORE_H_ 6 #define COMPONENTS_PREFS_OVERLAY_USER_PREF_STORE_H_ 7 8 #include <stdint.h> 9 10 #include <map> 11 #include <string_view> 12 13 #include "base/memory/ref_counted.h" 14 #include "base/observer_list.h" 15 #include "base/values.h" 16 #include "components/prefs/persistent_pref_store.h" 17 #include "components/prefs/pref_name_set.h" 18 #include "components/prefs/pref_value_map.h" 19 #include "components/prefs/prefs_export.h" 20 21 // PersistentPrefStore that directs all write operations into an in-memory 22 // PrefValueMap. Read operations are first answered by the PrefValueMap. 23 // If the PrefValueMap does not contain a value for the requested key, 24 // the look-up is passed on to an underlying PersistentPrefStore 25 // |persistent_user_pref_store_|. 26 class COMPONENTS_PREFS_EXPORT OverlayUserPrefStore 27 : public PersistentPrefStore { 28 public: 29 explicit OverlayUserPrefStore(PersistentPrefStore* persistent); 30 // The |ephemeral| store must already be initialized. 31 OverlayUserPrefStore(PersistentPrefStore* ephemeral, 32 PersistentPrefStore* persistent); 33 34 OverlayUserPrefStore(const OverlayUserPrefStore&) = delete; 35 OverlayUserPrefStore& operator=(const OverlayUserPrefStore&) = delete; 36 37 // Returns true if a value has been set for the |key| in this 38 // OverlayUserPrefStore, i.e. if it potentially overrides a value 39 // from the |persistent_user_pref_store_|. 40 virtual bool IsSetInOverlay(std::string_view key) const; 41 42 // Methods of PrefStore. 43 void AddObserver(PrefStore::Observer* observer) override; 44 void RemoveObserver(PrefStore::Observer* observer) override; 45 bool HasObservers() const override; 46 bool IsInitializationComplete() const override; 47 bool GetValue(std::string_view key, 48 const base::Value** result) const override; 49 base::Value::Dict GetValues() const override; 50 51 // Methods of PersistentPrefStore. 52 bool GetMutableValue(std::string_view key, base::Value** result) override; 53 void SetValue(std::string_view key, 54 base::Value value, 55 uint32_t flags) override; 56 void SetValueSilently(std::string_view key, 57 base::Value value, 58 uint32_t flags) override; 59 void RemoveValue(std::string_view key, uint32_t flags) override; 60 void RemoveValuesByPrefixSilently(std::string_view prefix) override; 61 bool ReadOnly() const override; 62 PrefReadError GetReadError() const override; 63 PrefReadError ReadPrefs() override; 64 void ReadPrefsAsync(ReadErrorDelegate* delegate) override; 65 void CommitPendingWrite(base::OnceClosure reply_callback, 66 base::OnceClosure synchronous_done_callback) override; 67 void SchedulePendingLossyWrites() override; 68 void ReportValueChanged(std::string_view key, uint32_t flags) override; 69 70 // Registers preferences that should be stored in the persistent preferences 71 // (|persistent_user_pref_store_|). 72 void RegisterPersistentPref(std::string_view key); 73 74 void OnStoreDeletionFromDisk() override; 75 bool HasReadErrorDelegate() const override; 76 77 protected: 78 ~OverlayUserPrefStore() override; 79 80 private: 81 class ObserverAdapter; 82 83 void OnPrefValueChanged(bool ephemeral, std::string_view key); 84 void OnInitializationCompleted(bool ephemeral, bool succeeded); 85 86 // Returns true if |key| corresponds to a preference that shall be stored in 87 // persistent PrefStore. 88 bool ShallBeStoredInPersistent(std::string_view key) const; 89 90 base::ObserverList<PrefStore::Observer, true> observers_; 91 std::unique_ptr<ObserverAdapter> ephemeral_pref_store_observer_; 92 std::unique_ptr<ObserverAdapter> persistent_pref_store_observer_; 93 scoped_refptr<PersistentPrefStore> ephemeral_user_pref_store_; 94 scoped_refptr<PersistentPrefStore> persistent_user_pref_store_; 95 PrefNameSet persistent_names_set_; 96 }; 97 98 #endif // COMPONENTS_PREFS_OVERLAY_USER_PREF_STORE_H_ 99