OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_PREFS_OVERLAY_USER_PREF_STORE_H_ | 5 #ifndef CHROME_BROWSER_PREFS_OVERLAY_USER_PREF_STORE_H_ |
6 #define CHROME_BROWSER_PREFS_OVERLAY_USER_PREF_STORE_H_ | 6 #define CHROME_BROWSER_PREFS_OVERLAY_USER_PREF_STORE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
15 #include "chrome/browser/prefs/pref_value_map.h" | 15 #include "chrome/browser/prefs/pref_value_map.h" |
16 #include "chrome/common/persistent_pref_store.h" | 16 #include "chrome/common/persistent_pref_store.h" |
17 | 17 |
18 // PersistentPrefStore that directs all write operations into an in-memory | 18 // PersistentPrefStore that directs all write operations into an in-memory |
19 // PrefValueMap. Read operations are first answered by the PrefValueMap. | 19 // PrefValueMap. Read operations are first answered by the PrefValueMap. |
20 // If the PrefValueMap does not contain a value for the requested key, | 20 // If the PrefValueMap does not contain a value for the requested key, |
21 // the look-up is passed on to an underlying PersistentPrefStore |underlay_|. | 21 // the look-up is passed on to an underlying PersistentPrefStore |underlay_|. |
22 class OverlayUserPrefStore : public PersistentPrefStore, | 22 class OverlayUserPrefStore : public PersistentPrefStore, |
23 public PrefStore::Observer { | 23 public PrefStore::Observer { |
24 public: | 24 public: |
25 explicit OverlayUserPrefStore(PersistentPrefStore* underlay); | 25 explicit OverlayUserPrefStore(PersistentPrefStore* underlay); |
26 virtual ~OverlayUserPrefStore(); | |
27 | 26 |
28 // Returns true if a value has been set for the |key| in this | 27 // Returns true if a value has been set for the |key| in this |
29 // OverlayUserPrefStore, i.e. if it potentially overrides a value | 28 // OverlayUserPrefStore, i.e. if it potentially overrides a value |
30 // from the |underlay_|. | 29 // from the |underlay_|. |
31 virtual bool IsSetInOverlay(const std::string& key) const; | 30 virtual bool IsSetInOverlay(const std::string& key) const; |
32 | 31 |
33 // Methods of PrefStore. | 32 // Methods of PrefStore. |
34 virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE; | 33 virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE; |
35 virtual void RemoveObserver(PrefStore::Observer* observer) OVERRIDE; | 34 virtual void RemoveObserver(PrefStore::Observer* observer) OVERRIDE; |
36 virtual size_t NumberOfObservers() const OVERRIDE; | 35 virtual size_t NumberOfObservers() const OVERRIDE; |
37 virtual bool IsInitializationComplete() const OVERRIDE; | 36 virtual bool IsInitializationComplete() const OVERRIDE; |
38 virtual ReadResult GetValue(const std::string& key, | 37 virtual ReadResult GetValue(const std::string& key, |
39 const base::Value** result) const OVERRIDE; | 38 const base::Value** result) const OVERRIDE; |
40 | 39 |
41 // Methods of PersistentPrefStore. | 40 // Methods of PersistentPrefStore. |
42 virtual ReadResult GetMutableValue(const std::string& key, | 41 virtual ReadResult GetMutableValue(const std::string& key, |
43 base::Value** result) OVERRIDE; | 42 base::Value** result) OVERRIDE; |
44 virtual void SetValue(const std::string& key, base::Value* value) OVERRIDE; | 43 virtual void SetValue(const std::string& key, base::Value* value) OVERRIDE; |
45 virtual void SetValueSilently(const std::string& key, | 44 virtual void SetValueSilently(const std::string& key, |
46 base::Value* value) OVERRIDE; | 45 base::Value* value) OVERRIDE; |
47 virtual void RemoveValue(const std::string& key) OVERRIDE; | 46 virtual void RemoveValue(const std::string& key) OVERRIDE; |
48 virtual void MarkNeedsEmptyValue(const std::string& key) OVERRIDE; | 47 virtual void MarkNeedsEmptyValue(const std::string& key) OVERRIDE; |
49 virtual bool ReadOnly() const OVERRIDE; | 48 virtual bool ReadOnly() const OVERRIDE; |
50 virtual PrefReadError GetReadError() const OVERRIDE; | 49 virtual PrefReadError GetReadError() const OVERRIDE; |
51 virtual PrefReadError ReadPrefs() OVERRIDE; | 50 virtual PrefReadError ReadPrefs() OVERRIDE; |
52 virtual void ReadPrefsAsync(ReadErrorDelegate* delegate) OVERRIDE; | 51 virtual void ReadPrefsAsync(ReadErrorDelegate* delegate) OVERRIDE; |
53 virtual void CommitPendingWrite() OVERRIDE; | 52 virtual void CommitPendingWrite() OVERRIDE; |
54 virtual void ReportValueChanged(const std::string& key) OVERRIDE; | 53 virtual void ReportValueChanged(const std::string& key) OVERRIDE; |
55 | 54 |
| 55 // Methods of PrefStore::Observer. |
| 56 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; |
| 57 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; |
| 58 |
56 void RegisterOverlayPref(const std::string& key); | 59 void RegisterOverlayPref(const std::string& key); |
57 void RegisterOverlayPref(const std::string& overlay_key, | 60 void RegisterOverlayPref(const std::string& overlay_key, |
58 const std::string& underlay_key); | 61 const std::string& underlay_key); |
59 | 62 |
| 63 protected: |
| 64 virtual ~OverlayUserPrefStore(); |
| 65 |
60 private: | 66 private: |
61 typedef std::map<std::string, std::string> NamesMap; | 67 typedef std::map<std::string, std::string> NamesMap; |
62 | 68 |
63 // Methods of PrefStore::Observer. | |
64 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; | |
65 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; | |
66 | |
67 const std::string& GetOverlayKey(const std::string& underlay_key) const; | 69 const std::string& GetOverlayKey(const std::string& underlay_key) const; |
68 const std::string& GetUnderlayKey(const std::string& overlay_key) const; | 70 const std::string& GetUnderlayKey(const std::string& overlay_key) const; |
69 | 71 |
70 // Returns true if |key| corresponds to a preference that shall be stored in | 72 // Returns true if |key| corresponds to a preference that shall be stored in |
71 // an in-memory PrefStore that is not persisted to disk. | 73 // an in-memory PrefStore that is not persisted to disk. |
72 bool ShallBeStoredInOverlay(const std::string& key) const; | 74 bool ShallBeStoredInOverlay(const std::string& key) const; |
73 | 75 |
74 ObserverList<PrefStore::Observer, true> observers_; | 76 ObserverList<PrefStore::Observer, true> observers_; |
75 PrefValueMap overlay_; | 77 PrefValueMap overlay_; |
76 scoped_refptr<PersistentPrefStore> underlay_; | 78 scoped_refptr<PersistentPrefStore> underlay_; |
77 NamesMap overlay_to_underlay_names_map_; | 79 NamesMap overlay_to_underlay_names_map_; |
78 NamesMap underlay_to_overlay_names_map_; | 80 NamesMap underlay_to_overlay_names_map_; |
79 | 81 |
80 DISALLOW_COPY_AND_ASSIGN(OverlayUserPrefStore); | 82 DISALLOW_COPY_AND_ASSIGN(OverlayUserPrefStore); |
81 }; | 83 }; |
82 | 84 |
83 #endif // CHROME_BROWSER_PREFS_OVERLAY_USER_PREF_STORE_H_ | 85 #endif // CHROME_BROWSER_PREFS_OVERLAY_USER_PREF_STORE_H_ |
OLD | NEW |