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> 12 13 #include "base/memory/ref_counted.h" 14 #include "base/observer_list.h" 15 #include "base/strings/string_piece.h" 16 #include "base/values.h" 17 #include "components/prefs/persistent_pref_store.h" 18 #include "components/prefs/pref_name_set.h" 19 #include "components/prefs/pref_value_map.h" 20 #include "components/prefs/prefs_export.h" 21 22 // PersistentPrefStore that directs all write operations into an in-memory 23 // PrefValueMap. Read operations are first answered by the PrefValueMap. 24 // If the PrefValueMap does not contain a value for the requested key, 25 // the look-up is passed on to an underlying PersistentPrefStore 26 // |persistent_user_pref_store_|. 27 class COMPONENTS_PREFS_EXPORT OverlayUserPrefStore 28 : public PersistentPrefStore { 29 public: 30 explicit OverlayUserPrefStore(PersistentPrefStore* persistent); 31 // The |ephemeral| store must already be initialized. 32 OverlayUserPrefStore(PersistentPrefStore* ephemeral, 33 PersistentPrefStore* persistent); 34 35 OverlayUserPrefStore(const OverlayUserPrefStore&) = delete; 36 OverlayUserPrefStore& operator=(const OverlayUserPrefStore&) = delete; 37 38 // Returns true if a value has been set for the |key| in this 39 // OverlayUserPrefStore, i.e. if it potentially overrides a value 40 // from the |persistent_user_pref_store_|. 41 virtual bool IsSetInOverlay(const std::string& key) const; 42 43 // Methods of PrefStore. 44 void AddObserver(PrefStore::Observer* observer) override; 45 void RemoveObserver(PrefStore::Observer* observer) override; 46 bool HasObservers() const override; 47 bool IsInitializationComplete() const override; 48 bool GetValue(base::StringPiece key, 49 const base::Value** result) const override; 50 base::Value::Dict GetValues() const override; 51 52 // Methods of PersistentPrefStore. 53 bool GetMutableValue(const std::string& key, base::Value** result) override; 54 void SetValue(const std::string& key, 55 base::Value value, 56 uint32_t flags) override; 57 void SetValueSilently(const std::string& key, 58 base::Value value, 59 uint32_t flags) override; 60 void RemoveValue(const std::string& key, uint32_t flags) override; 61 void RemoveValuesByPrefixSilently(const std::string& prefix) override; 62 bool ReadOnly() const override; 63 PrefReadError GetReadError() const override; 64 PrefReadError ReadPrefs() override; 65 void ReadPrefsAsync(ReadErrorDelegate* delegate) override; 66 void CommitPendingWrite(base::OnceClosure reply_callback, 67 base::OnceClosure synchronous_done_callback) override; 68 void SchedulePendingLossyWrites() override; 69 void ReportValueChanged(const std::string& key, uint32_t flags) override; 70 71 // Registers preferences that should be stored in the persistent preferences 72 // (|persistent_user_pref_store_|). 73 void RegisterPersistentPref(const std::string& key); 74 75 void OnStoreDeletionFromDisk() override; 76 77 protected: 78 ~OverlayUserPrefStore() override; 79 80 private: 81 class ObserverAdapter; 82 83 void OnPrefValueChanged(bool ephemeral, const std::string& 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(base::StringPiece key) const; 89 90 base::ObserverList<PrefStore::Observer, true>::Unchecked 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