| 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 // Defines the classes to realize the Font Settings Extension API as specified | 5 // Defines the classes to realize the Font Settings Extension API as specified |
| 6 // in the extension API JSON. | 6 // in the extension API JSON. |
| 7 | 7 |
| 8 #ifndef CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H__ | 8 #ifndef CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H__ |
| 9 #define CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H__ | 9 #define CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H__ |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 | 14 |
| 15 #include "base/prefs/public/pref_change_registrar.h" | 15 #include "base/prefs/public/pref_change_registrar.h" |
| 16 #include "base/prefs/public/pref_observer.h" | |
| 17 #include "chrome/browser/extensions/extension_function.h" | 16 #include "chrome/browser/extensions/extension_function.h" |
| 18 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
| 19 | 18 |
| 20 namespace extensions { | 19 namespace extensions { |
| 21 | 20 |
| 22 // This class observes pref changed events on a profile and dispatches the | 21 // This class observes pref changed events on a profile and dispatches the |
| 23 // corresponding extension API events to extensions. | 22 // corresponding extension API events to extensions. |
| 24 class FontSettingsEventRouter : public PrefObserver { | 23 class FontSettingsEventRouter { |
| 25 public: | 24 public: |
| 26 // Constructor for observing pref changed events on |profile|. Stores a | 25 // Constructor for observing pref changed events on |profile|. Stores a |
| 27 // pointer to |profile| but does not take ownership. |profile| must be | 26 // pointer to |profile| but does not take ownership. |profile| must be |
| 28 // non-NULL and remain alive for the lifetime of the instance. | 27 // non-NULL and remain alive for the lifetime of the instance. |
| 29 explicit FontSettingsEventRouter(Profile* profile); | 28 explicit FontSettingsEventRouter(Profile* profile); |
| 30 virtual ~FontSettingsEventRouter(); | 29 virtual ~FontSettingsEventRouter(); |
| 31 | 30 |
| 32 private: | 31 private: |
| 33 typedef std::pair<std::string, std::string> EventAndKeyPair; | 32 typedef std::pair<std::string, std::string> EventAndKeyPair; |
| 34 // Map of pref name to a pair of event name and key (defined in the extension | 33 // Map of pref name to a pair of event name and key (defined in the extension |
| 35 // API) for dispatching a pref changed event. For example, | 34 // API) for dispatching a pref changed event. For example, |
| 36 // "webkit.webprefs.default_font_size" to ("onDefaultFontSizedChanged", | 35 // "webkit.webprefs.default_font_size" to ("onDefaultFontSizedChanged", |
| 37 // "pixelSize"). | 36 // "pixelSize"). |
| 38 typedef std::map<std::string, EventAndKeyPair> PrefEventMap; | 37 typedef std::map<std::string, EventAndKeyPair> PrefEventMap; |
| 39 | 38 |
| 40 // Observes browser pref |pref_name|. When a change is observed, dispatches | 39 // Observes browser pref |pref_name|. When a change is observed, dispatches |
| 41 // event |event_name| to extensions. A JavaScript object is passed to the | 40 // event |event_name| to extensions. A JavaScript object is passed to the |
| 42 // extension event function with the new value of the pref in property |key|. | 41 // extension event function with the new value of the pref in property |key|. |
| 43 void AddPrefToObserve(const char* pref_name, | 42 void AddPrefToObserve(const char* pref_name, |
| 44 const char* event_name, | 43 const char* event_name, |
| 45 const char* key); | 44 const char* key); |
| 46 | 45 |
| 47 // PrefObserver implementation. | 46 void OnPreferenceChanged(const std::string& pref_name); |
| 48 virtual void OnPreferenceChanged(PrefServiceBase* service, | |
| 49 const std::string& pref_name) OVERRIDE; | |
| 50 | 47 |
| 51 // Dispatches a changed event for the font setting for |generic_family| and | 48 // Dispatches a changed event for the font setting for |generic_family| and |
| 52 // |script| to extensions. The new value of the setting is the value of | 49 // |script| to extensions. The new value of the setting is the value of |
| 53 // browser pref |pref_name|. If the pref changed on the incognito profile, | 50 // browser pref |pref_name|. If the pref changed on the incognito profile, |
| 54 // |incognito| must be set to true for extensions to get the appropriate | 51 // |incognito| must be set to true for extensions to get the appropriate |
| 55 // event. | 52 // event. |
| 56 void OnFontNamePrefChanged(PrefServiceBase* pref_service, | 53 void OnFontNamePrefChanged(const std::string& pref_name, |
| 57 const std::string& pref_name, | |
| 58 const std::string& generic_family, | 54 const std::string& generic_family, |
| 59 const std::string& script, | 55 const std::string& script, |
| 60 bool incognito); | 56 bool incognito); |
| 61 | 57 |
| 62 // Dispatches the setting changed event |event_name| to extensions. The new | 58 // Dispatches the setting changed event |event_name| to extensions. The new |
| 63 // value of the setting is the value of browser pref |pref_name|. This value | 59 // value of the setting is the value of browser pref |pref_name|. This value |
| 64 // is passed in the JavaScript object argument to the extension event function | 60 // is passed in the JavaScript object argument to the extension event function |
| 65 // under the key |key|. If the pref changed on the incognito profile, | 61 // under the key |key|. If the pref changed on the incognito profile, |
| 66 // |incognito| must be set to true for extensions to get the appropriate | 62 // |incognito| must be set to true for extensions to get the appropriate |
| 67 // event. | 63 // event. |
| 68 void OnFontPrefChanged(PrefServiceBase* pref_service, | 64 void OnFontPrefChanged(const std::string& pref_name, |
| 69 const std::string& pref_name, | |
| 70 const std::string& event_name, | 65 const std::string& event_name, |
| 71 const std::string& key, | 66 const std::string& key, |
| 72 bool incognito); | 67 bool incognito); |
| 73 | 68 |
| 74 // Manages pref observation registration. | 69 // Manages pref observation registration. |
| 75 PrefChangeRegistrar registrar_; | 70 PrefChangeRegistrar registrar_; |
| 76 | 71 |
| 77 // Maps browser pref names to extension API <event name, key> pairs. | 72 // Maps browser pref names to extension API <event name, key> pairs. |
| 78 PrefEventMap pref_event_map_; | 73 PrefEventMap pref_event_map_; |
| 79 | 74 |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 virtual ~SetMinimumFontSizeFunction() {} | 284 virtual ~SetMinimumFontSizeFunction() {} |
| 290 | 285 |
| 291 // SetFontPrefExtensionFunction: | 286 // SetFontPrefExtensionFunction: |
| 292 virtual const char* GetPrefName() OVERRIDE; | 287 virtual const char* GetPrefName() OVERRIDE; |
| 293 virtual const char* GetKey() OVERRIDE; | 288 virtual const char* GetKey() OVERRIDE; |
| 294 }; | 289 }; |
| 295 | 290 |
| 296 } // namespace extensions | 291 } // namespace extensions |
| 297 | 292 |
| 298 #endif // CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H__ | 293 #endif // CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H__ |
| OLD | NEW |