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