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.
5 #ifndef CHROME_BROWSER_PREFS_TRACKED_SEGREGATED_PREF_STORE_H_
6 #define CHROME_BROWSER_PREFS_TRACKED_SEGREGATED_PREF_STORE_H_
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"
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.
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 {
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
39 // |on_initialization| will be invoked when both stores have been initialized,
40 // before observers of the combined store are notified.
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 const base::Closure& on_initialization);
47 // PrefStore implementation
48 virtual void AddObserver(Observer* observer) OVERRIDE;
49 virtual void RemoveObserver(Observer* observer) OVERRIDE;
50 virtual bool HasObservers() const OVERRIDE;
51 virtual bool IsInitializationComplete() const OVERRIDE;
52 virtual bool GetValue(const std::string& key,
53 const base::Value** result) const OVERRIDE;
55 // WriteablePrefStore implementation
56 virtual void SetValue(const std::string& key, base::Value* value) OVERRIDE;
57 virtual void RemoveValue(const std::string& key) OVERRIDE;
59 // PersistentPrefStore implementation
60 virtual bool GetMutableValue(const std::string& key,
61 base::Value** result) OVERRIDE;
62 virtual void ReportValueChanged(const std::string& key) OVERRIDE;
63 virtual void SetValueSilently(const std::string& key,
64 base::Value* value) OVERRIDE;
65 virtual bool ReadOnly() const OVERRIDE;
66 virtual PrefReadError GetReadError() const OVERRIDE;
67 virtual PrefReadError ReadPrefs() OVERRIDE;
68 virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE;
69 virtual void CommitPendingWrite() OVERRIDE;
72 // Aggregates events from the underlying stores and synthesizes external
73 // events via |on_initialization|, |read_error_delegate_|, and |observers_|.
74 class AggregatingObserver : public PrefStore::Observer {
76 explicit AggregatingObserver(SegregatedPrefStore* outer);
78 // PrefStore::Observer implementation
79 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE;
80 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE;
83 SegregatedPrefStore* outer_;
84 int failed_sub_initializations_;
85 int successful_sub_initializations_;
87 DISALLOW_COPY_AND_ASSIGN(AggregatingObserver);
90 virtual ~SegregatedPrefStore();
92 // Returns |selected_pref_store| if |key| is selected or a value is present
93 // in |selected_pref_store|. This could happen if |key| was previously
95 const PersistentPrefStore* StoreForKey(const std::string& key) const;
97 // Returns |selected_pref_store| if |key| is selected. If |key| is
98 // unselected but has a value in |selected_pref_store|, moves the value to
99 // |default_pref_store| before returning |default_pref_store|.
100 PersistentPrefStore* StoreForKey(const std::string& key);
102 scoped_refptr<PersistentPrefStore> default_pref_store_;
103 scoped_refptr<PersistentPrefStore> selected_pref_store_;
104 std::set<std::string> selected_preference_names_;
105 base::Closure on_initialization_;
107 scoped_ptr<PersistentPrefStore::ReadErrorDelegate> read_error_delegate_;
108 ObserverList<PrefStore::Observer, true> observers_;
109 AggregatingObserver aggregating_observer_;
111 DISALLOW_COPY_AND_ASSIGN(SegregatedPrefStore);
114 #endif // CHROME_BROWSER_PREFS_TRACKED_SEGREGATED_PREF_STORE_H_