| 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> | 9 #include <list> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 class Profile; | 26 class Profile; |
| 27 class Value; | 27 class Value; |
| 28 | 28 |
| 29 // This PrefStore keeps track of preferences set by extensions: for example, | 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 | 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 | 31 // their addition to this PrefStore. For each preference, the last-added |
| 32 // enabled extension that tries to set it overrules any others. | 32 // enabled extension that tries to set it overrules any others. |
| 33 class ExtensionPrefStore : public PrefStore, | 33 class ExtensionPrefStore : public PrefStore, |
| 34 public NotificationObserver { | 34 public NotificationObserver { |
| 35 public: | 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 |
| 36 ExtensionPrefStore(Profile* profile, PrefNotifier::PrefStoreType type); | 46 ExtensionPrefStore(Profile* profile, PrefNotifier::PrefStoreType type); |
| 37 virtual ~ExtensionPrefStore(); | 47 virtual ~ExtensionPrefStore(); |
| 38 | 48 |
| 39 // Begins tracking the preference and value an extension wishes to set. This | 49 // Begins tracking the preference and value an extension wishes to set. This |
| 40 // must be called each time an extension API tries to set a preference. | 50 // must be called each time an extension API tries to set a preference. |
| 41 // The ExtensionPrefStore will take ownership of the |pref_value|. | 51 // The ExtensionPrefStore will take ownership of the |pref_value|. |
| 42 virtual void InstallExtensionPref(const Extension* extension, | 52 virtual void InstallExtensionPref(const Extension* extension, |
| 43 const char* pref_path, | 53 const char* pref_path, |
| 44 Value* pref_value); | 54 Value* pref_value); |
| 45 | 55 |
| 46 // Removes an extension and all its preference settings from this PrefStore. | 56 // Removes an extension and all its preference settings from this PrefStore. |
| 47 // This must be called when an extension is uninstalled or disabled. | 57 // This must be called when an extension is uninstalled or disabled. |
| 48 virtual void UninstallExtension(const Extension* extension); | 58 virtual void UninstallExtension(const Extension* extension); |
| 49 | 59 |
| 50 // PrefStore methods: | 60 // PrefStore methods: |
| 51 virtual DictionaryValue* prefs() { return prefs_.get(); } | 61 virtual DictionaryValue* prefs() const { return prefs_.get(); } |
| 52 | 62 |
| 53 virtual PrefReadError ReadPrefs() { return PREF_READ_ERROR_NONE; } | 63 virtual PrefReadError ReadPrefs() { return PREF_READ_ERROR_NONE; } |
| 54 | 64 |
| 55 // The type passed as Details for an EXTENSION_PREF_CHANGED notification. | |
| 56 // The nested pairs are <extension, <pref_path, pref_value> >. This is here, | |
| 57 // rather than in (say) notification_type.h, to keep the dependency on | |
| 58 // std::pair out of the many places that include notification_type.h. | |
| 59 typedef std::pair<const Extension*, std::pair<const char*, Value*> > | |
| 60 ExtensionPrefDetails; | |
| 61 | |
| 62 protected: | 65 protected: |
| 63 // Returns a vector of the extension IDs in the extension_stack_. | 66 // Returns a vector of the extension IDs in the extension_stack_. |
| 64 // This should only be accessed by subclasses for unit-testing. | 67 // This should only be accessed by subclasses for unit-testing. |
| 65 void GetExtensionIDs(std::vector<std::string>* result); | 68 void GetExtensionIDs(std::vector<std::string>* result); |
| 66 | 69 |
| 67 // Returns the applicable pref service from the profile (if we have one) or | 70 // Returns the applicable pref service from the profile (if we have one) or |
| 68 // the browser's local state. This should only be accessed or overridden by | 71 // the browser's local state. This should only be accessed or overridden by |
| 69 // subclasses for unit-testing. | 72 // subclasses for unit-testing. |
| 70 virtual PrefService* GetPrefService(); | 73 virtual PrefService* GetPrefService(); |
| 71 | 74 |
| 72 private: | 75 private: |
| 73 // Maps preference paths to their values. | 76 // Associates an extension with the prefs it sets. Owns the pref values. |
| 74 typedef std::map<const char*, Value*> PrefValueMap; | 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; |
| 75 | 88 |
| 76 // Applies the highest-priority extension's setting for the given preference | 89 // Applies the highest-priority extension's setting for the given preference |
| 77 // path to the |prefs_| store, or clears the setting there if no extensions | 90 // path to the |prefs_| store, or clears the setting there if no extensions |
| 78 // wish to control it. | 91 // wish to control it. |
| 79 void UpdateOnePref(const char* path); | 92 void UpdateOnePref(const char* path); |
| 80 | 93 |
| 81 // Updates each preference in the key set of the |pref_values| map. | 94 // Updates each preference in the key set of the |pref_values| map. |
| 82 void UpdatePrefs(const PrefValueMap* pref_values); | 95 void UpdatePrefs(const PrefValueMap* pref_values); |
| 83 | 96 |
| 84 // Registers this as an observer for relevant notifications. | 97 // Registers this as an observer for relevant notifications. |
| 85 void RegisterObservers(); | 98 void RegisterObservers(); |
| 86 | 99 |
| 87 // Responds to observed notifications. | 100 // Responds to observed notifications. |
| 88 void Observe(NotificationType type, | 101 void Observe(NotificationType type, |
| 89 const NotificationSource& source, | 102 const NotificationSource& source, |
| 90 const NotificationDetails& details); | 103 const NotificationDetails& details); |
| 91 | 104 |
| 92 // A cache of the highest-priority values for each preference that any | 105 // A cache of the highest-priority values for each preference that any |
| 93 // extension is controlling, for quick read access. Owns the stored values. | 106 // extension is controlling, for quick read access. Owns the stored values. |
| 94 scoped_ptr<DictionaryValue> prefs_; | 107 scoped_ptr<DictionaryValue> prefs_; |
| 95 | 108 |
| 96 // Associates an extension with the prefs it sets. Owns the pref values. | |
| 97 struct ExtensionPrefs { | |
| 98 ExtensionPrefs(const Extension* extension, PrefValueMap* values); | |
| 99 ~ExtensionPrefs(); | |
| 100 | |
| 101 const Extension* extension; | |
| 102 PrefValueMap* pref_values; | |
| 103 }; | |
| 104 | |
| 105 // A pseudo-stack of extensions and their preferences. Extensions are always | |
| 106 // added to the head, but may be removed from the middle. | |
| 107 typedef std::list<ExtensionPrefs*> ExtensionStack; | |
| 108 ExtensionStack extension_stack_; | 109 ExtensionStack extension_stack_; |
| 109 | 110 |
| 110 NotificationRegistrar notification_registrar_; | 111 NotificationRegistrar notification_registrar_; |
| 111 | 112 |
| 112 // Weak reference to the profile whose extensions we're interested in. May be | 113 // Weak reference to the profile whose extensions we're interested in. May be |
| 113 // NULL (for the local-state preferences), in which case we watch all | 114 // NULL (for the local-state preferences), in which case we watch all |
| 114 // extensions. | 115 // extensions. |
| 115 Profile* profile_; | 116 Profile* profile_; |
| 116 | 117 |
| 117 // My PrefStore type, assigned by the PrefValueStore. | 118 // My PrefStore type, assigned by the PrefValueStore. |
| 118 PrefNotifier::PrefStoreType type_; | 119 PrefNotifier::PrefStoreType type_; |
| 119 | 120 |
| 120 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefStore); | 121 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefStore); |
| 121 }; | 122 }; |
| 122 | 123 |
| 123 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_STORE_H_ | 124 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_STORE_H_ |
| OLD | NEW |