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 <optional> 12 #include <set> 13 #include <string_view> 14 15 #include "base/compiler_specific.h" 16 #include "base/functional/callback.h" 17 #include "base/memory/raw_ptr.h" 18 #include "base/memory/ref_counted.h" 19 #include "base/observer_list.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(std::string_view key, 58 const base::Value** result) const override; 59 base::Value::Dict GetValues() const override; 60 61 // WriteablePrefStore implementation 62 void SetValue(std::string_view key, 63 base::Value value, 64 uint32_t flags) override; 65 void RemoveValue(std::string_view key, uint32_t flags) override; 66 void RemoveValuesByPrefixSilently(std::string_view prefix) override; 67 68 // PersistentPrefStore implementation 69 bool GetMutableValue(std::string_view key, base::Value** result) override; 70 void ReportValueChanged(std::string_view key, uint32_t flags) override; 71 void SetValueSilently(std::string_view 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 bool HasReadErrorDelegate() const override; 85 86 protected: 87 ~SegregatedPrefStore() override; 88 89 private: 90 // Caches event state from the underlying stores and exposes the state to the 91 // provided "outer" SegregatedPrefStore to synthesize external events via 92 // |read_error_delegate_| and |observers_|. 93 class UnderlyingPrefStoreObserver : public PrefStore::Observer { 94 public: 95 explicit UnderlyingPrefStoreObserver(SegregatedPrefStore* outer); 96 97 UnderlyingPrefStoreObserver(const UnderlyingPrefStoreObserver&) = delete; 98 UnderlyingPrefStoreObserver& operator=(const UnderlyingPrefStoreObserver&) = 99 delete; 100 101 // PrefStore::Observer implementation 102 void OnPrefValueChanged(std::string_view key) override; 103 void OnInitializationCompleted(bool succeeded) override; 104 initialization_succeeded()105 bool initialization_succeeded() const { return initialization_succeeded_; } 106 107 private: 108 const raw_ptr<SegregatedPrefStore> outer_; 109 bool initialization_succeeded_ = false; 110 }; 111 112 // Returns true only if all underlying PrefStores have initialized 113 // successfully, otherwise false. 114 bool IsInitializationSuccessful() const; 115 116 // Returns |selected_pref_store| if |key| is selected and 117 // |default_pref_store| otherwise. 118 PersistentPrefStore* StoreForKey(std::string_view key); 119 const PersistentPrefStore* StoreForKey(std::string_view key) const; 120 121 const scoped_refptr<PersistentPrefStore> default_pref_store_; 122 const scoped_refptr<PersistentPrefStore> selected_pref_store_; 123 const PrefNameSet selected_preference_names_; 124 125 // Optional so we can differentiate `nullopt` from `nullptr`. 126 std::optional<std::unique_ptr<PersistentPrefStore::ReadErrorDelegate>> 127 read_error_delegate_; 128 base::ObserverList<PrefStore::Observer, true> observers_; 129 UnderlyingPrefStoreObserver default_observer_; 130 UnderlyingPrefStoreObserver selected_observer_; 131 }; 132 133 #endif // COMPONENTS_PREFS_SEGREGATED_PREF_STORE_H_ 134