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 | |
10 #include "base/callback.h" | |
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 class SegregatedPrefStore : public PersistentPrefStore { | |
22 public: | |
23 // Creates an instance that delegates to |selected_pref_store| for the | |
24 // preferences named in |selected_pref_names| and to |default_pref_store| | |
25 // for all others. If an unselected preference is present in | |
26 // |selected_pref_store| (i.e. because it was previously selected) it will | |
27 // be migrated back to |default_pref_store| upon access via a non-const | |
28 // method. | |
29 // |on_initialization| will be invoked when both stores have been initialized, | |
30 // before observers of the combined store are notified. | |
gab
2014/04/01 18:55:06
What's "the combined store", this SegregatedPrefSt
| |
31 SegregatedPrefStore( | |
32 const scoped_refptr<PersistentPrefStore>& default_pref_store, | |
33 const scoped_refptr<PersistentPrefStore>& selected_pref_store, | |
34 const std::set<std::string>& selected_pref_names, | |
35 const base::Closure& on_initialization); | |
36 | |
37 // PrefStore implementation | |
38 virtual void AddObserver(Observer* observer) OVERRIDE; | |
39 virtual void RemoveObserver(Observer* observer) OVERRIDE; | |
40 virtual bool HasObservers() const OVERRIDE; | |
41 virtual bool IsInitializationComplete() const OVERRIDE; | |
42 virtual bool GetValue(const std::string& key, | |
43 const base::Value** result) const OVERRIDE; | |
44 | |
45 // WriteablePrefStore implementation | |
46 virtual void SetValue(const std::string& key, base::Value* value) OVERRIDE; | |
47 virtual void RemoveValue(const std::string& key) OVERRIDE; | |
48 | |
49 // PersistentPrefStore implementation | |
50 virtual bool GetMutableValue(const std::string& key, | |
51 base::Value** result) OVERRIDE; | |
52 virtual void ReportValueChanged(const std::string& key) OVERRIDE; | |
53 virtual void SetValueSilently(const std::string& key, | |
54 base::Value* value) OVERRIDE; | |
55 virtual bool ReadOnly() const OVERRIDE; | |
56 virtual PrefReadError GetReadError() const OVERRIDE; | |
57 virtual PrefReadError ReadPrefs() OVERRIDE; | |
58 virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE; | |
59 virtual void CommitPendingWrite() OVERRIDE; | |
60 | |
61 private: | |
62 // Aggregates events from the underlying stores and synthesizes external | |
63 // events via |on_initialization|, |read_error_delegate_|, and |observers_|. | |
64 class AggregatingObserver : public PrefStore::Observer { | |
65 public: | |
66 explicit AggregatingObserver(SegregatedPrefStore* outer); | |
67 | |
68 // PrefStore::Observer implementation | |
69 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; | |
70 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; | |
71 | |
72 private: | |
73 SegregatedPrefStore* outer_; | |
74 int failed_sub_initializations_; | |
75 int successful_sub_initializations_; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(AggregatingObserver); | |
78 }; | |
79 | |
80 virtual ~SegregatedPrefStore(); | |
81 | |
82 // Returns |selected_pref_store| if |key| is selected or a value is present | |
83 // in |selected_pref_store|. This could happen if |key| was previously | |
84 // selected. | |
85 const PersistentPrefStore* StoreForKey(const std::string& key) const; | |
86 | |
87 // Returns |selected_pref_store| if |key| is selected. If |key| is | |
88 // unselected but has a value in |selected_pref_store|, moves the value to | |
89 // |default_pref_store| before returning |default_pref_store|. | |
90 PersistentPrefStore* StoreForKey(const std::string& key); | |
91 | |
92 scoped_refptr<PersistentPrefStore> default_pref_store_; | |
93 scoped_refptr<PersistentPrefStore> selected_pref_store_; | |
94 std::set<std::string> selected_preference_names_; | |
95 base::Closure on_initialization_; | |
96 | |
97 scoped_ptr<PersistentPrefStore::ReadErrorDelegate> read_error_delegate_; | |
98 ObserverList<PrefStore::Observer, true> observers_; | |
99 AggregatingObserver aggregating_observer_; | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(SegregatedPrefStore); | |
102 }; | |
103 | |
104 #endif // CHROME_BROWSER_PREFS_TRACKED_SEGREGATED_PREF_STORE_H_ | |
OLD | NEW |