OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_EXTENSIONS_EXTENSION_PREF_STORE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_STORE_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_STORE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_STORE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <list> | |
10 #include <map> | |
11 #include <string> | 9 #include <string> |
12 #include <utility> | 10 #include <utility> |
13 #include <vector> | 11 #include <vector> |
14 | 12 |
15 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/gtest_prod_util.h" |
16 #include "base/scoped_ptr.h" | 15 #include "base/scoped_ptr.h" |
17 #include "base/stl_util-inl.h" | |
18 #include "chrome/browser/prefs/pref_notifier.h" | 16 #include "chrome/browser/prefs/pref_notifier.h" |
19 #include "chrome/common/notification_observer.h" | 17 #include "chrome/common/notification_observer.h" |
20 #include "chrome/common/notification_registrar.h" | 18 #include "chrome/common/notification_registrar.h" |
21 #include "chrome/common/pref_store.h" | 19 #include "chrome/common/pref_store.h" |
22 | 20 |
23 class DictionaryValue; | 21 class DictionaryValue; |
24 class Extension; | 22 class Extension; |
| 23 class ListValue; |
25 class PrefService; | 24 class PrefService; |
26 class Profile; | 25 class Profile; |
27 class Value; | 26 class Value; |
28 | 27 |
29 // This PrefStore keeps track of preferences set by extensions: for example, | 28 // This PrefStore keeps track of preferences set by extensions: for example, |
30 // proxy settings. A stack of relevant extensions is stored in order of | 29 // proxy settings. A stack of relevant extensions is stored in order of |
31 // their addition to this PrefStore. For each preference, the last-added | 30 // their addition to this PrefStore. For each preference, the last-added |
32 // enabled extension that tries to set it overrules any others. | 31 // enabled extension that tries to set it overrules any others. |
| 32 // |
| 33 // The preferences of extensions are embedded into the user's persistent pref |
| 34 // store such that the precedence of extensions is persisted and such that |
| 35 // preferences are available immediately after the browser is restarted (i.e. |
| 36 // even before extensions have been started). |
| 37 // The ExtensionPrefStore hooks into the "extension_preferences" branch of the |
| 38 // persistent user prefs. The structure is as follows: |
| 39 // extension_preferences: ( |
| 40 // { id: "extension_id_1", preferences: { ...preferences of ext. 1... } }, |
| 41 // { id: "extension_id_2", preferences: { ...preferences of ext. 2... } }, |
| 42 // ... ) |
| 43 // In this example, extension 1 has higher precendence than extension 2. |
| 44 // Keys in extension_preferences/preferences are stored without path expansion. |
33 class ExtensionPrefStore : public PrefStore, | 45 class ExtensionPrefStore : public PrefStore, |
34 public NotificationObserver { | 46 public NotificationObserver { |
35 public: | 47 public: |
36 // Maps preference paths to their values. | |
37 typedef std::map<const char*, Value*> PrefValueMap; | |
38 | |
39 // The type passed as Details for an EXTENSION_PREF_CHANGED notification. | 48 // The type passed as Details for an EXTENSION_PREF_CHANGED notification. |
40 // The nested pairs are <extension, <pref_path, pref_value> >. This is here, | 49 // The nested pairs are <extension, <pref_path, pref_value> >. This is here, |
41 // rather than in (say) notification_type.h, to keep the dependency on | 50 // rather than in (say) notification_type.h, to keep the dependency on |
42 // std::pair out of the many places that include notification_type.h. | 51 // std::pair out of the many places that include notification_type.h. |
43 typedef std::pair<const Extension*, std::pair<const char*, Value*> > | 52 typedef std::pair<const Extension*, std::pair<const char*, Value*> > |
44 ExtensionPrefDetails; | 53 ExtensionPrefDetails; |
45 | 54 |
| 55 // The local-state ExtensionPrefStore (shared among all profiles on a |
| 56 // machine), indicated by a NULL |profile|, is prohibited from storing |
| 57 // preferences. Since extensions are installed per profile, allowing them to |
| 58 // control machine-wide settings could lead to unsolvable conflicts, and |
| 59 // should not be necessary anyway. |
46 ExtensionPrefStore(Profile* profile, PrefNotifier::PrefStoreType type); | 60 ExtensionPrefStore(Profile* profile, PrefNotifier::PrefStoreType type); |
47 virtual ~ExtensionPrefStore(); | 61 virtual ~ExtensionPrefStore(); |
48 | 62 |
49 // Begins tracking the preference and value an extension wishes to set. This | 63 // Begins tracking the preference and value an extension wishes to set. This |
50 // must be called each time an extension API tries to set a preference. | 64 // must be called each time an extension API tries to set a preference. |
51 // The ExtensionPrefStore will take ownership of the |pref_value|. | 65 // The ExtensionPrefStore will take ownership of the |pref_value|. |
52 virtual void InstallExtensionPref(const Extension* extension, | 66 virtual void InstallExtensionPref(const Extension* extension, |
53 const char* pref_path, | 67 const char* pref_path, |
54 Value* pref_value); | 68 Value* pref_value); |
55 | 69 |
56 // Removes an extension and all its preference settings from this PrefStore. | 70 // Removes an extension and all its preference settings from this PrefStore. |
57 // This must be called when an extension is uninstalled or disabled. | 71 // This must be called when an extension is uninstalled or disabled. |
58 virtual void UninstallExtension(const Extension* extension); | 72 virtual void UninstallExtension(const Extension* extension); |
59 | 73 |
60 // PrefStore methods: | 74 // PrefStore methods: |
61 virtual DictionaryValue* prefs() const { return prefs_.get(); } | 75 virtual DictionaryValue* prefs() const; |
62 | |
63 virtual PrefReadError ReadPrefs() { return PREF_READ_ERROR_NONE; } | 76 virtual PrefReadError ReadPrefs() { return PREF_READ_ERROR_NONE; } |
64 | 77 |
| 78 static const char kExtensionPreferencesKey[]; |
65 protected: | 79 protected: |
66 // Returns a vector of the extension IDs in the extension_stack_. | 80 // Returns a vector of the extension IDs in the extension_stack_. |
67 // This should only be accessed by subclasses for unit-testing. | 81 // This should only be accessed by subclasses for unit-testing. |
68 void GetExtensionIDs(std::vector<std::string>* result); | 82 void GetExtensionIDs(std::vector<std::string>* result); |
69 | 83 |
70 // Returns the applicable pref service from the profile (if we have one) or | 84 // For PrefService injection for unit-testing |
71 // the browser's local state. This should only be accessed or overridden by | 85 virtual PrefService* GetPrefService() const; |
72 // subclasses for unit-testing. | 86 |
73 virtual PrefService* GetPrefService(); | 87 // Schedules a write of the backing user-pref store. |
| 88 virtual void SchedulePersist(); |
74 | 89 |
75 private: | 90 private: |
76 // Associates an extension with the prefs it sets. Owns the pref values. | 91 // For visibility of helper classes: |
77 struct ExtensionPrefs { | 92 FRIEND_TEST_ALL_PREFIXES(ExtensionPrefStoreTest, ExtensionPrefs); |
78 ExtensionPrefs(const Extension* extension, PrefValueMap* values); | 93 FRIEND_TEST_ALL_PREFIXES(ExtensionPrefStoreTest, ExtensionStack); |
| 94 FRIEND_TEST_ALL_PREFIXES(ExtensionPrefStoreTest, InstallRegisteredExtensions); |
| 95 FRIEND_TEST_ALL_PREFIXES(ExtensionPrefStoreTest, |
| 96 UninstallRegisteredExtensions); |
| 97 FRIEND_TEST_ALL_PREFIXES(ExtensionPrefStoreTest, |
| 98 ReadFromRegisteredExtensions); |
| 99 |
| 100 // Wrapper around one Dictionary embedded in the "extension_preferences" |
| 101 // list. Contains an "id"->String and "preferences"->Dictionary entry. |
| 102 class ExtensionPrefs { |
| 103 public: |
| 104 // Wraps itself around the DictionaryValue that is supposed to follow |
| 105 // the predefined structure (see ExtensionPrefStore). The ownership |
| 106 // of dict remains at the caller. |
| 107 explicit ExtensionPrefs(DictionaryValue* dict); |
79 ~ExtensionPrefs(); | 108 ~ExtensionPrefs(); |
80 | 109 |
81 const Extension* extension; | 110 // Creates a DictionaryValue { id: |extension_id|, preferences: {} } |
82 PrefValueMap* pref_values; | 111 // and passes ownership to the caller. |
| 112 static DictionaryValue* Create(const std::string& extension_id); |
| 113 |
| 114 // Returns the id of the extension managed by this ExtensionPrefs |
| 115 std::string extension_id() const; |
| 116 |
| 117 // Returns a pointer to the preference values, the ownership remains |
| 118 // at |dict| passed to the constructor. |
| 119 DictionaryValue* pref_values(); |
| 120 |
| 121 // Sets a |value| and passes ownership to |dict|. Deletes the value, if |
| 122 // |value| is NULL. |
| 123 void Set(const std::string& key, Value* value); |
| 124 |
| 125 // Retrieves a value but retains ownership at |dict|. Returns NULL if the |
| 126 // key is not contained in the dictionary. |
| 127 Value* Get(const std::string& key); |
| 128 |
| 129 // Keys in |dict_|: |
| 130 static const char kIdKey[]; |
| 131 static const char kPreferencesKey[]; |
| 132 private: |
| 133 // Weak reference |
| 134 DictionaryValue* dict_; |
83 }; | 135 }; |
84 | 136 |
85 // A pseudo-stack of extensions and their preferences. Extensions are always | 137 // Wrapper around a list in a persistent PrefStore in order to manage and |
86 // added to the head, but may be removed from the middle. | 138 // persist preferences of extensions. Each individual extension's preferences |
87 typedef std::list<ExtensionPrefs*> ExtensionStack; | 139 // are managed by ExtensionPrefs. |
| 140 class ExtensionStack { |
| 141 public: |
| 142 // Preliminary construction, still requires init-call. |
| 143 ExtensionStack(); |
| 144 ~ExtensionStack(); |
| 145 |
| 146 // Attaches the |list| to this object without taking ownership. |
| 147 void Init(ListValue* list); |
| 148 bool IsInitialized() const; |
| 149 |
| 150 // returns number of registered extensions |
| 151 size_t Size() const; |
| 152 ExtensionPrefs Get(int index); |
| 153 void Remove(int index); |
| 154 |
| 155 // Creates and registeres a new entry for the extension with |extension_id|. |
| 156 // This extension gains highest precedence and can override other |
| 157 // extensions' preference values. |
| 158 ExtensionPrefs CreateEntry(const std::string& extension_id); |
| 159 private: |
| 160 // Weak reference |
| 161 ListValue* list_; |
| 162 }; |
88 | 163 |
89 // Applies the highest-priority extension's setting for the given preference | 164 // Applies the highest-priority extension's setting for the given preference |
90 // path to the |prefs_| store, or clears the setting there if no extensions | 165 // path to the |prefs_| store, or clears the setting there if no extensions |
91 // wish to control it. | 166 // wish to control it. |
92 void UpdateOnePref(const char* path); | 167 void UpdateOnePref(const char* path); |
93 | 168 |
94 // Updates each preference in the key set of the |pref_values| map. | 169 // Updates each preference in the key set of the |pref_values| map. |
95 void UpdatePrefs(const PrefValueMap* pref_values); | 170 void UpdatePrefs(const DictionaryValue* pref_values); |
96 | 171 |
97 // Registers this as an observer for relevant notifications. | 172 // Registers this as an observer for relevant notifications. |
98 void RegisterObservers(); | 173 void RegisterObservers(); |
99 | 174 |
100 // Responds to observed notifications. | 175 // Responds to observed notifications. |
101 void Observe(NotificationType type, | 176 void Observe(NotificationType type, |
102 const NotificationSource& source, | 177 const NotificationSource& source, |
103 const NotificationDetails& details); | 178 const NotificationDetails& details); |
104 | 179 |
| 180 // Initializes the extension_stack_ with empty dictionaries for all |
| 181 // extensions registered in the precedence list. |
| 182 // Declared const for "DictionaryValue* prefs() const" |
| 183 void LazyInit() const; |
| 184 |
105 // A cache of the highest-priority values for each preference that any | 185 // A cache of the highest-priority values for each preference that any |
106 // extension is controlling, for quick read access. Owns the stored values. | 186 // extension is controlling, for quick read access. Owns the stored values. |
107 scoped_ptr<DictionaryValue> prefs_; | 187 scoped_ptr<DictionaryValue> prefs_; |
108 | 188 |
109 ExtensionStack extension_stack_; | 189 // Accessor to preference values stored in user's pref store. |
| 190 mutable ExtensionStack extension_stack_; |
110 | 191 |
111 NotificationRegistrar notification_registrar_; | 192 NotificationRegistrar notification_registrar_; |
112 | 193 |
113 // Weak reference to the profile whose extensions we're interested in. May be | 194 // Weak reference to the profile whose extensions we're interested in. May be |
114 // NULL (for the local-state preferences), in which case we watch all | 195 // NULL (for the local-state preferences), in which case we watch all |
115 // extensions. | 196 // extensions. |
116 Profile* profile_; | 197 Profile* profile_; |
117 | 198 |
| 199 // Empty list that is only used for "local state" (when there is no user pref |
| 200 // store). |
| 201 scoped_ptr<ListValue> dummy_prefs; |
| 202 |
118 // My PrefStore type, assigned by the PrefValueStore. | 203 // My PrefStore type, assigned by the PrefValueStore. |
119 PrefNotifier::PrefStoreType type_; | 204 PrefNotifier::PrefStoreType type_; |
120 | 205 |
121 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefStore); | 206 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefStore); |
122 }; | 207 }; |
123 | 208 |
124 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_STORE_H_ | 209 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_STORE_H_ |
OLD | NEW |