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_PROTECTED_PREF_STORE_H_ | |
6 #define CHROME_BROWSER_PREFS_TRACKED_PROTECTED_PREF_STORE_H_ | |
7 | |
8 #include <set> | |
9 #include <vector> | |
10 | |
11 #include "base/callback.h" | |
12 #include "base/compiler_specific.h" | |
13 #include "base/macros.h" | |
14 #include "base/observer_list.h" | |
15 #include "base/prefs/persistent_pref_store.h" | |
16 #include "base/strings/string_piece.h" | |
17 #include "chrome/browser/prefs/pref_hash_filter.h" | |
18 | |
19 class PrefHashStore; | |
20 | |
21 | |
22 // Provides a unified PersistentPrefStore implementation that splits its storage | |
23 // and retrieval between two underlying PersistentPrefStore instances: one each | |
24 // for protected and unprotected preferences. | |
25 class ProtectedPrefStore : public PersistentPrefStore { | |
26 public: | |
27 // Creates an instance that delegates to |protected_pref_store| for the | |
28 // preferences named in |protected_pref_names| and to |unprotected_pref_store| | |
29 // for all others. If an unprotected preference is present in | |
30 // |protected_pref_store| (i.e. because it was previously protected) it will | |
31 // be migrated back to |unprotected_pref_store| upon access via a non-const | |
32 // method. | |
33 // |on_initialization| will be invoked when both stores have been initialized, | |
34 // before observers of the combined store are notified. | |
35 ProtectedPrefStore( | |
36 const scoped_refptr<PersistentPrefStore>& unprotected_pref_store, | |
37 const scoped_refptr<PersistentPrefStore>& protected_pref_store, | |
38 const std::set<std::string>& protected_pref_names, | |
39 const base::Closure& on_initialization); | |
40 | |
41 // PrefStore implementation | |
42 virtual void AddObserver(Observer* observer) OVERRIDE; | |
43 virtual void RemoveObserver(Observer* observer) OVERRIDE; | |
44 virtual bool HasObservers() const OVERRIDE; | |
45 virtual bool IsInitializationComplete() const OVERRIDE; | |
46 virtual bool GetValue(const std::string& key, | |
47 const base::Value** result) const OVERRIDE; | |
48 | |
49 // WriteablePrefStore implementation | |
50 virtual void SetValue(const std::string& key, base::Value* value) OVERRIDE; | |
51 virtual void RemoveValue(const std::string& key) OVERRIDE; | |
52 | |
53 // PersistentPrefStore implementation | |
54 virtual bool GetMutableValue(const std::string& key, | |
55 base::Value** result) OVERRIDE; | |
56 virtual void ReportValueChanged(const std::string& key) OVERRIDE; | |
57 virtual void SetValueSilently(const std::string& key, | |
58 base::Value* value) OVERRIDE; | |
59 virtual bool ReadOnly() const OVERRIDE; | |
60 virtual PrefReadError GetReadError() const OVERRIDE; | |
61 virtual PrefReadError ReadPrefs() OVERRIDE; | |
62 virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE; | |
63 virtual void CommitPendingWrite() OVERRIDE; | |
64 | |
65 private: | |
66 // Observes events from the underlying stores and synthesizes external events | |
67 // via |on_initialization|, |read_error_delegate_|, and |observers_|. | |
68 class TeeObserver : public PrefStore::Observer { | |
Bernhard Bauer
2014/03/26 15:05:50
Hmm... do you even need this class? The outer clas
erikwright (departed)
2014/03/26 15:17:14
In my personal opinion it breaks encapsulation by
Bernhard Bauer
2014/03/26 16:41:11
Hm, I would have been okay with that. Clients do s
erikwright (departed)
2014/03/26 21:08:12
I don't really agree with the reasoning.
I find t
Bernhard Bauer
2014/03/27 15:10:45
Some problems with the current design:
* You have
erikwright (departed)
2014/03/27 19:26:21
You mean the extra dereference? I suppose so, in t
| |
69 public: | |
70 explicit TeeObserver(ProtectedPrefStore* outer); | |
71 | |
72 // PrefStore::Observer implementation | |
73 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; | |
74 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; | |
75 | |
76 private: | |
77 ProtectedPrefStore* outer_; | |
78 uint8 failed_sub_initializations_; | |
Bernhard Bauer
2014/03/26 15:05:50
Nit: The style guide says to only use unsigned typ
erikwright (departed)
2014/03/26 21:08:12
Done.
| |
79 uint8 successful_sub_initializations_; | |
80 | |
81 DISALLOW_COPY_AND_ASSIGN(TeeObserver); | |
82 }; | |
83 | |
84 virtual ~ProtectedPrefStore(); | |
85 | |
86 // Returns |protected_pref_store| if |key| is protected or a value is present | |
87 // in |protected_pref_store|. This could happen if |key| was previously | |
88 // protected. | |
89 const PersistentPrefStore* StoreForKey(const std::string& key) const; | |
90 | |
91 // Returns |protected_pref_store| if |key| is protected. If |key| is | |
92 // unprotected but has a value in |protected_pref_store|, moves the value to | |
93 // |unprotected_pref_store| before returning |unprotected_pref_store|. | |
94 PersistentPrefStore* StoreForKey(const std::string& key); | |
95 | |
96 scoped_refptr<PersistentPrefStore> unprotected_pref_store_; | |
97 scoped_refptr<PersistentPrefStore> protected_pref_store_; | |
Bernhard Bauer
2014/03/26 15:05:50
It's a bit strange that the ProtectedPrefStore has
erikwright (departed)
2014/03/26 15:17:14
We all agree on this.
It's a bit more than a Unif
Bernhard Bauer
2014/03/26 16:41:11
SGTM.
| |
98 std::set<std::string> protected_preference_names_; | |
99 base::Closure on_initialization_; | |
100 | |
101 scoped_ptr<PersistentPrefStore::ReadErrorDelegate> read_error_delegate_; | |
102 ObserverList<PrefStore::Observer, true> observers_; | |
103 TeeObserver tee_observer_; | |
104 | |
105 DISALLOW_COPY_AND_ASSIGN(ProtectedPrefStore); | |
106 }; | |
107 | |
108 #endif // CHROME_BROWSER_PREFS_TRACKED_PROTECTED_PREF_STORE_H_ | |
OLD | NEW |