1 // Copyright 2014 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_SEGREGATED_PREF_STORE_H_ 6 #define COMPONENTS_PREFS_SEGREGATED_PREF_STORE_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 #include <set> 12 #include <string> 13 14 #include "base/compiler_specific.h" 15 #include "base/functional/callback.h" 16 #include "base/memory/raw_ptr.h" 17 #include "base/memory/ref_counted.h" 18 #include "base/observer_list.h" 19 #include "base/strings/string_piece.h" 20 #include "components/prefs/persistent_pref_store.h" 21 #include "components/prefs/pref_name_set.h" 22 #include "components/prefs/prefs_export.h" 23 24 // Provides a unified PersistentPrefStore implementation that splits its storage 25 // and retrieval between two underlying PersistentPrefStore instances: a set of 26 // preference names is used to partition the preferences. 27 // 28 // Combines properties of the two stores as follows: 29 // * The unified read error will be: 30 // Selected Store Error 31 // Default Store Error | NO_ERROR | NO_FILE | other selected | 32 // NO_ERROR | NO_ERROR | NO_ERROR | other selected | 33 // NO_FILE | NO_FILE | NO_FILE | NO_FILE | 34 // other default | other default | other default | other default | 35 // * The unified initialization success, initialization completion, and 36 // read-only state are the boolean OR of the underlying stores' properties. 37 class COMPONENTS_PREFS_EXPORT SegregatedPrefStore : public PersistentPrefStore { 38 public: 39 // Creates an instance that delegates to |selected_pref_store| for the 40 // preferences named in |selected_pref_names| and to |default_pref_store| 41 // for all others. If an unselected preference is present in 42 // |selected_pref_store| (i.e. because it was previously selected) it will 43 // be migrated back to |default_pref_store| upon access via a non-const 44 // method. 45 SegregatedPrefStore(scoped_refptr<PersistentPrefStore> default_pref_store, 46 scoped_refptr<PersistentPrefStore> selected_pref_store, 47 PrefNameSet selected_pref_names); 48 49 SegregatedPrefStore(const SegregatedPrefStore&) = delete; 50 SegregatedPrefStore& operator=(const SegregatedPrefStore&) = delete; 51 52 // PrefStore implementation 53 void AddObserver(Observer* observer) override; 54 void RemoveObserver(Observer* observer) override; 55 bool HasObservers() const override; 56 bool IsInitializationComplete() const override; 57 bool GetValue(base::StringPiece key, 58 const base::Value** result) const override; 59 base::Value::Dict GetValues() const override; 60 61 // WriteablePrefStore implementation 62 void SetValue(const std::string& key, 63 base::Value value, 64 uint32_t flags) override; 65 void RemoveValue(const std::string& key, uint32_t flags) override; 66 void RemoveValuesByPrefixSilently(const std::string& prefix) override; 67 68 // PersistentPrefStore implementation 69 bool GetMutableValue(const std::string& key, base::Value** result) override; 70 void ReportValueChanged(const std::string& key, uint32_t flags) override; 71 void SetValueSilently(const std::string& key, 72 base::Value value, 73 uint32_t flags) override; 74 bool ReadOnly() const override; 75 PrefReadError GetReadError() const override; 76 PrefReadError ReadPrefs() override; 77 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override; 78 void CommitPendingWrite( 79 base::OnceClosure reply_callback = base::OnceClosure(), 80 base::OnceClosure synchronous_done_callback = 81 base::OnceClosure()) override; 82 void SchedulePendingLossyWrites() override; 83 void OnStoreDeletionFromDisk() override; 84 85 protected: 86 ~SegregatedPrefStore() override; 87 88 private: 89 // Caches event state from the underlying stores and exposes the state to the 90 // provided "outer" SegregatedPrefStore to synthesize external events via 91 // |read_error_delegate_| and |observers_|. 92 class UnderlyingPrefStoreObserver : public PrefStore::Observer { 93 public: 94 explicit UnderlyingPrefStoreObserver(SegregatedPrefStore* outer); 95 96 UnderlyingPrefStoreObserver(const UnderlyingPrefStoreObserver&) = delete; 97 UnderlyingPrefStoreObserver& operator=(const UnderlyingPrefStoreObserver&) = 98 delete; 99 100 // PrefStore::Observer implementation 101 void OnPrefValueChanged(const std::string& key) override; 102 void OnInitializationCompleted(bool succeeded) override; 103 initialization_succeeded()104 bool initialization_succeeded() const { return initialization_succeeded_; } 105 106 private: 107 const raw_ptr<SegregatedPrefStore> outer_; 108 bool initialization_succeeded_ = false; 109 }; 110 111 // Returns true only if all underlying PrefStores have initialized 112 // successfully, otherwise false. 113 bool IsInitializationSuccessful() const; 114 115 // Returns |selected_pref_store| if |key| is selected and 116 // |default_pref_store| otherwise. 117 PersistentPrefStore* StoreForKey(base::StringPiece key); 118 const PersistentPrefStore* StoreForKey(base::StringPiece key) const; 119 120 const scoped_refptr<PersistentPrefStore> default_pref_store_; 121 const scoped_refptr<PersistentPrefStore> selected_pref_store_; 122 const PrefNameSet selected_preference_names_; 123 124 std::unique_ptr<PersistentPrefStore::ReadErrorDelegate> read_error_delegate_; 125 base::ObserverList<PrefStore::Observer, true>::Unchecked observers_; 126 UnderlyingPrefStoreObserver default_observer_; 127 UnderlyingPrefStoreObserver selected_observer_; 128 }; 129 130 #endif // COMPONENTS_PREFS_SEGREGATED_PREF_STORE_H_ 131