OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
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_USER_PREFS_TRACKED_SEGREGATED_PREF_STORE_H_ | |
6 #define COMPONENTS_USER_PREFS_TRACKED_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/macros.h" | |
16 #include "base/memory/ref_counted.h" | |
17 #include "base/observer_list.h" | |
18 #include "components/prefs/persistent_pref_store.h" | |
19 | |
20 // Provides a unified PersistentPrefStore implementation that splits its storage | |
21 // and retrieval between two underlying PersistentPrefStore instances: a set of | |
22 // preference names is used to partition the preferences. | |
23 // | |
24 // Combines properties of the two stores as follows: | |
25 // * The unified read error will be: | |
26 // Selected Store Error | |
27 // Default Store Error | NO_ERROR | NO_FILE | other selected | | |
28 // NO_ERROR | NO_ERROR | NO_ERROR | other selected | | |
29 // NO_FILE | NO_FILE | NO_FILE | NO_FILE | | |
30 // other default | other default | other default | other default | | |
31 // * The unified initialization success, initialization completion, and | |
32 // read-only state are the boolean OR of the underlying stores' properties. | |
33 class SegregatedPrefStore : public PersistentPrefStore { | |
34 public: | |
35 // Creates an instance that delegates to |selected_pref_store| for the | |
36 // preferences named in |selected_pref_names| and to |default_pref_store| | |
37 // for all others. If an unselected preference is present in | |
38 // |selected_pref_store| (i.e. because it was previously selected) it will | |
39 // be migrated back to |default_pref_store| upon access via a non-const | |
40 // method. | |
41 // |on_initialization| will be invoked when both stores have been initialized, | |
42 // before observers of the SegregatedPrefStore store are notified. | |
43 SegregatedPrefStore( | |
44 const scoped_refptr<PersistentPrefStore>& default_pref_store, | |
45 const scoped_refptr<PersistentPrefStore>& selected_pref_store, | |
46 const std::set<std::string>& selected_pref_names); | |
47 | |
48 // PrefStore implementation | |
49 void AddObserver(Observer* observer) override; | |
50 void RemoveObserver(Observer* observer) override; | |
51 bool HasObservers() const override; | |
52 bool IsInitializationComplete() const override; | |
53 bool GetValue(const std::string& key, | |
54 const base::Value** result) const override; | |
55 std::unique_ptr<base::DictionaryValue> GetValues() const override; | |
56 | |
57 // WriteablePrefStore implementation | |
58 void SetValue(const std::string& key, | |
59 std::unique_ptr<base::Value> value, | |
60 uint32_t flags) override; | |
61 void RemoveValue(const std::string& key, uint32_t flags) override; | |
62 | |
63 // PersistentPrefStore implementation | |
64 bool GetMutableValue(const std::string& key, base::Value** result) override; | |
65 void ReportValueChanged(const std::string& key, uint32_t flags) override; | |
66 void SetValueSilently(const std::string& key, | |
67 std::unique_ptr<base::Value> value, | |
68 uint32_t flags) override; | |
69 bool ReadOnly() const override; | |
70 PrefReadError GetReadError() const override; | |
71 PrefReadError ReadPrefs() override; | |
72 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override; | |
73 void CommitPendingWrite() override; | |
74 void SchedulePendingLossyWrites() override; | |
75 | |
76 void ClearMutableValues() override; | |
77 | |
78 private: | |
79 // Aggregates events from the underlying stores and synthesizes external | |
80 // events via |on_initialization|, |read_error_delegate_|, and |observers_|. | |
81 class AggregatingObserver : public PrefStore::Observer { | |
82 public: | |
83 explicit AggregatingObserver(SegregatedPrefStore* outer); | |
84 | |
85 // PrefStore::Observer implementation | |
86 void OnPrefValueChanged(const std::string& key) override; | |
87 void OnInitializationCompleted(bool succeeded) override; | |
88 | |
89 private: | |
90 SegregatedPrefStore* outer_; | |
91 int failed_sub_initializations_; | |
92 int successful_sub_initializations_; | |
93 | |
94 DISALLOW_COPY_AND_ASSIGN(AggregatingObserver); | |
95 }; | |
96 | |
97 ~SegregatedPrefStore() override; | |
98 | |
99 // Returns |selected_pref_store| if |key| is selected and |default_pref_store| | |
100 // otherwise. | |
101 PersistentPrefStore* StoreForKey(const std::string& key); | |
102 const PersistentPrefStore* StoreForKey(const std::string& key) const; | |
103 | |
104 scoped_refptr<PersistentPrefStore> default_pref_store_; | |
105 scoped_refptr<PersistentPrefStore> selected_pref_store_; | |
106 std::set<std::string> selected_preference_names_; | |
107 | |
108 std::unique_ptr<PersistentPrefStore::ReadErrorDelegate> read_error_delegate_; | |
109 base::ObserverList<PrefStore::Observer, true> observers_; | |
110 AggregatingObserver aggregating_observer_; | |
111 | |
112 DISALLOW_COPY_AND_ASSIGN(SegregatedPrefStore); | |
113 }; | |
114 | |
115 #endif // COMPONENTS_PREFS_TRACKED_SEGREGATED_PREF_STORE_H_ | |
OLD | NEW |