| 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> | |
| 12 #include <string> | 11 #include <string> |
| 13 #include <utility> | |
| 14 | 12 |
| 15 #include "base/prefs/public/pref_change_registrar.h" | 13 #include "base/prefs/public/pref_change_registrar.h" |
| 16 #include "base/prefs/public/pref_observer.h" | |
| 17 #include "chrome/browser/extensions/extension_function.h" | 14 #include "chrome/browser/extensions/extension_function.h" |
| 18 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
| 19 | 16 |
| 20 namespace extensions { | 17 namespace extensions { |
| 21 | 18 |
| 22 // This class observes pref changed events on a profile and dispatches the | 19 // This class observes pref changed events on a profile and dispatches the |
| 23 // corresponding extension API events to extensions. | 20 // corresponding extension API events to extensions. |
| 24 class FontSettingsEventRouter : public PrefObserver { | 21 class FontSettingsEventRouter { |
| 25 public: | 22 public: |
| 26 // Constructor for observing pref changed events on |profile|. Stores a | 23 // Constructor for observing pref changed events on |profile|. Stores a |
| 27 // pointer to |profile| but does not take ownership. |profile| must be | 24 // pointer to |profile| but does not take ownership. |profile| must be |
| 28 // non-NULL and remain alive for the lifetime of the instance. | 25 // non-NULL and remain alive for the lifetime of the instance. |
| 29 explicit FontSettingsEventRouter(Profile* profile); | 26 explicit FontSettingsEventRouter(Profile* profile); |
| 30 virtual ~FontSettingsEventRouter(); | 27 virtual ~FontSettingsEventRouter(); |
| 31 | 28 |
| 32 private: | 29 private: |
| 33 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 | |
| 35 // API) for dispatching a pref changed event. For example, | |
| 36 // "webkit.webprefs.default_font_size" to ("onDefaultFontSizedChanged", | |
| 37 // "pixelSize"). | |
| 38 typedef std::map<std::string, EventAndKeyPair> PrefEventMap; | |
| 39 | |
| 40 // Observes browser pref |pref_name|. When a change is observed, dispatches | 30 // Observes browser pref |pref_name|. When a change is observed, dispatches |
| 41 // event |event_name| to extensions. A JavaScript object is passed to the | 31 // 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|. | 32 // extension event function with the new value of the pref in property |key|. |
| 43 void AddPrefToObserve(const char* pref_name, | 33 void AddPrefToObserve(const char* pref_name, |
| 44 const char* event_name, | 34 const char* event_name, |
| 45 const char* key); | 35 const char* key); |
| 46 | 36 |
| 47 // PrefObserver implementation. | 37 // Decodes a preference change for a font family map and invokes |
| 48 virtual void OnPreferenceChanged(PrefServiceBase* service, | 38 // OnFontNamePrefChange with the right parameters. |
| 49 const std::string& pref_name) OVERRIDE; | 39 void OnFontFamilyMapPrefChanged(const std::string& pref_name); |
| 50 | 40 |
| 51 // Dispatches a changed event for the font setting for |generic_family| and | 41 // 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 | 42 // |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, | 43 // browser pref |pref_name|. |
| 54 // |incognito| must be set to true for extensions to get the appropriate | 44 void OnFontNamePrefChanged(const std::string& pref_name, |
| 55 // event. | |
| 56 void OnFontNamePrefChanged(PrefServiceBase* pref_service, | |
| 57 const std::string& pref_name, | |
| 58 const std::string& generic_family, | 45 const std::string& generic_family, |
| 59 const std::string& script, | 46 const std::string& script); |
| 60 bool incognito); | |
| 61 | 47 |
| 62 // Dispatches the setting changed event |event_name| to extensions. The new | 48 // 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 | 49 // 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 | 50 // 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, | 51 // under the key |key|. |
| 66 // |incognito| must be set to true for extensions to get the appropriate | 52 void OnFontPrefChanged(const std::string& event_name, |
| 67 // event. | |
| 68 void OnFontPrefChanged(PrefServiceBase* pref_service, | |
| 69 const std::string& pref_name, | |
| 70 const std::string& event_name, | |
| 71 const std::string& key, | 53 const std::string& key, |
| 72 bool incognito); | 54 const std::string& pref_name); |
| 73 | 55 |
| 74 // Manages pref observation registration. | 56 // Manages pref observation registration. |
| 75 PrefChangeRegistrar registrar_; | 57 PrefChangeRegistrar registrar_; |
| 76 | 58 |
| 77 // Maps browser pref names to extension API <event name, key> pairs. | |
| 78 PrefEventMap pref_event_map_; | |
| 79 | |
| 80 // Weak, owns us (transitively via ExtensionService). | 59 // Weak, owns us (transitively via ExtensionService). |
| 81 Profile* profile_; | 60 Profile* profile_; |
| 82 | 61 |
| 83 DISALLOW_COPY_AND_ASSIGN(FontSettingsEventRouter); | 62 DISALLOW_COPY_AND_ASSIGN(FontSettingsEventRouter); |
| 84 }; | 63 }; |
| 85 | 64 |
| 86 // fontSettings.clearFont API function. | 65 // fontSettings.clearFont API function. |
| 87 class ClearFontFunction : public SyncExtensionFunction { | 66 class ClearFontFunction : public SyncExtensionFunction { |
| 88 public: | 67 public: |
| 89 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.clearFont") | 68 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.clearFont") |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 virtual ~SetMinimumFontSizeFunction() {} | 268 virtual ~SetMinimumFontSizeFunction() {} |
| 290 | 269 |
| 291 // SetFontPrefExtensionFunction: | 270 // SetFontPrefExtensionFunction: |
| 292 virtual const char* GetPrefName() OVERRIDE; | 271 virtual const char* GetPrefName() OVERRIDE; |
| 293 virtual const char* GetKey() OVERRIDE; | 272 virtual const char* GetKey() OVERRIDE; |
| 294 }; | 273 }; |
| 295 | 274 |
| 296 } // namespace extensions | 275 } // namespace extensions |
| 297 | 276 |
| 298 #endif // CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H__ | 277 #endif // CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H__ |
| OLD | NEW |