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.
5 #ifndef COMPONENTS_PREFS_SEGREGATED_PREF_STORE_H_
6 #define COMPONENTS_PREFS_SEGREGATED_PREF_STORE_H_
14 #include "base/callback.h"
15 #include "base/compiler_specific.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"
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.
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 {
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
45 SegregatedPrefStore(scoped_refptr<PersistentPrefStore> default_pref_store,
46 scoped_refptr<PersistentPrefStore> selected_pref_store,
47 PrefNameSet selected_pref_names);
49 SegregatedPrefStore(const SegregatedPrefStore&) = delete;
50 SegregatedPrefStore& operator=(const SegregatedPrefStore&) = delete;
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;
61 // WriteablePrefStore implementation
62 void SetValue(const std::string& key,
64 uint32_t flags) override;
65 void RemoveValue(const std::string& key, uint32_t flags) override;
66 void RemoveValuesByPrefixSilently(const std::string& prefix) override;
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,
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 ClearMutableValues() override;
84 void OnStoreDeletionFromDisk() override;
87 ~SegregatedPrefStore() override;
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 {
95 explicit UnderlyingPrefStoreObserver(SegregatedPrefStore* outer);
97 UnderlyingPrefStoreObserver(const UnderlyingPrefStoreObserver&) = delete;
98 UnderlyingPrefStoreObserver& operator=(const UnderlyingPrefStoreObserver&) =
101 // PrefStore::Observer implementation
102 void OnPrefValueChanged(const std::string& key) override;
103 void OnInitializationCompleted(bool succeeded) override;
105 bool initialization_succeeded() const { return initialization_succeeded_; }
108 const raw_ptr<SegregatedPrefStore> outer_;
109 bool initialization_succeeded_ = false;
112 // Returns true only if all underlying PrefStores have initialized
113 // successfully, otherwise false.
114 bool IsInitializationSuccessful() const;
116 // Returns |selected_pref_store| if |key| is selected and
117 // |default_pref_store| otherwise.
118 PersistentPrefStore* StoreForKey(base::StringPiece key);
119 const PersistentPrefStore* StoreForKey(base::StringPiece key) const;
121 const scoped_refptr<PersistentPrefStore> default_pref_store_;
122 const scoped_refptr<PersistentPrefStore> selected_pref_store_;
123 const PrefNameSet selected_preference_names_;
125 std::unique_ptr<PersistentPrefStore::ReadErrorDelegate> read_error_delegate_;
126 base::ObserverList<PrefStore::Observer, true>::Unchecked observers_;
127 UnderlyingPrefStoreObserver default_observer_;
128 UnderlyingPrefStoreObserver selected_observer_;
131 #endif // COMPONENTS_PREFS_SEGREGATED_PREF_STORE_H_