OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_STORE_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_STORE_H_ | |
7 #pragma once | |
8 | |
9 #include <list> | |
10 #include <map> | |
11 #include <string> | |
12 #include <utility> | |
13 #include <vector> | |
14 | |
15 #include "base/basictypes.h" | |
16 #include "base/scoped_ptr.h" | |
17 #include "base/stl_util-inl.h" | |
18 #include "chrome/browser/prefs/pref_notifier.h" | |
19 #include "chrome/common/notification_observer.h" | |
20 #include "chrome/common/notification_registrar.h" | |
21 #include "chrome/common/pref_store.h" | |
22 | |
23 class DictionaryValue; | |
24 class Extension; | |
25 class PrefService; | |
26 class Profile; | |
27 class Value; | |
28 | |
29 // This PrefStore keeps track of preferences set by extensions: for example, | |
30 // proxy settings. A stack of relevant extensions is stored in order of | |
31 // their addition to this PrefStore. For each preference, the last-added | |
32 // enabled extension that tries to set it overrules any others. | |
33 class ExtensionPrefStore : public PrefStore, | |
34 public NotificationObserver { | |
35 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. | |
40 // 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 | |
42 // std::pair out of the many places that include notification_type.h. | |
43 typedef std::pair<const Extension*, std::pair<const char*, Value*> > | |
44 ExtensionPrefDetails; | |
45 | |
46 ExtensionPrefStore(Profile* profile, PrefNotifier::PrefStoreType type); | |
47 virtual ~ExtensionPrefStore(); | |
48 | |
49 // 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. | |
51 // The ExtensionPrefStore will take ownership of the |pref_value|. | |
52 virtual void InstallExtensionPref(const Extension* extension, | |
53 const char* pref_path, | |
54 Value* pref_value); | |
55 | |
56 // Removes an extension and all its preference settings from this PrefStore. | |
57 // This must be called when an extension is uninstalled or disabled. | |
58 virtual void UninstallExtension(const Extension* extension); | |
59 | |
60 // PrefStore methods: | |
61 virtual DictionaryValue* prefs() const { return prefs_.get(); } | |
62 | |
63 virtual PrefReadError ReadPrefs() { return PREF_READ_ERROR_NONE; } | |
64 | |
65 protected: | |
66 // Returns a vector of the extension IDs in the extension_stack_. | |
67 // This should only be accessed by subclasses for unit-testing. | |
68 void GetExtensionIDs(std::vector<std::string>* result); | |
69 | |
70 // Returns the applicable pref service from the profile (if we have one) or | |
71 // the browser's local state. This should only be accessed or overridden by | |
72 // subclasses for unit-testing. | |
73 virtual PrefService* GetPrefService(); | |
74 | |
75 private: | |
76 // Associates an extension with the prefs it sets. Owns the pref values. | |
77 struct ExtensionPrefs { | |
78 ExtensionPrefs(const Extension* extension, PrefValueMap* values); | |
79 ~ExtensionPrefs(); | |
80 | |
81 const Extension* extension; | |
82 PrefValueMap* pref_values; | |
83 }; | |
84 | |
85 // A pseudo-stack of extensions and their preferences. Extensions are always | |
86 // added to the head, but may be removed from the middle. | |
87 typedef std::list<ExtensionPrefs*> ExtensionStack; | |
88 | |
89 // 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 | |
91 // wish to control it. | |
92 void UpdateOnePref(const char* path); | |
93 | |
94 // Updates each preference in the key set of the |pref_values| map. | |
95 void UpdatePrefs(const PrefValueMap* pref_values); | |
96 | |
97 // Registers this as an observer for relevant notifications. | |
98 void RegisterObservers(); | |
99 | |
100 // Responds to observed notifications. | |
101 void Observe(NotificationType type, | |
102 const NotificationSource& source, | |
103 const NotificationDetails& details); | |
104 | |
105 // A cache of the highest-priority values for each preference that any | |
106 // extension is controlling, for quick read access. Owns the stored values. | |
107 scoped_ptr<DictionaryValue> prefs_; | |
108 | |
109 ExtensionStack extension_stack_; | |
110 | |
111 NotificationRegistrar notification_registrar_; | |
112 | |
113 // 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 | |
115 // extensions. | |
116 Profile* profile_; | |
117 | |
118 // My PrefStore type, assigned by the PrefValueStore. | |
119 PrefNotifier::PrefStoreType type_; | |
120 | |
121 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefStore); | |
122 }; | |
123 | |
124 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_STORE_H_ | |
OLD | NEW |