| 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 |
| 6 // in the extension API JSON. |
| 7 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FONT_SETTINGS_API_H__ | 8 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FONT_SETTINGS_API_H__ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FONT_SETTINGS_API_H__ | 9 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FONT_SETTINGS_API_H__ |
| 7 #pragma once | 10 #pragma once |
| 8 | 11 |
| 9 #include <map> | 12 #include <map> |
| 13 #include <string> |
| 14 #include <utility> |
| 10 | 15 |
| 11 #include "chrome/browser/extensions/extension_function.h" | 16 #include "chrome/browser/extensions/extension_function.h" |
| 12 #include "chrome/browser/prefs/pref_change_registrar.h" | 17 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 13 | 18 |
| 19 // This class observes pref changed events on a profile and dispatches the |
| 20 // corresponding extension API events to extensions. |
| 14 class ExtensionFontSettingsEventRouter : public content::NotificationObserver { | 21 class ExtensionFontSettingsEventRouter : public content::NotificationObserver { |
| 15 public: | 22 public: |
| 23 // Constructor for observing pref changed events on |profile|. Stores a |
| 24 // pointer to |profile| but does not take ownership. |profile| must be |
| 25 // non-NULL and remain alive for the lifetime of the instance. Init() must be |
| 26 // called to start observing pref changed events. |
| 16 explicit ExtensionFontSettingsEventRouter(Profile* profile); | 27 explicit ExtensionFontSettingsEventRouter(Profile* profile); |
| 17 virtual ~ExtensionFontSettingsEventRouter(); | 28 virtual ~ExtensionFontSettingsEventRouter(); |
| 18 | 29 |
| 30 // Starts observing pref changed events on the profile. Must not be called |
| 31 // more than once. |
| 19 void Init(); | 32 void Init(); |
| 20 | 33 |
| 21 private: | 34 private: |
| 22 typedef std::pair<std::string, std::string> EventAndKeyPair; | 35 typedef std::pair<std::string, std::string> EventAndKeyPair; |
| 23 // Map of pref name to a pair of event name and key (defined in the API) for | 36 // Map of pref name to a pair of event name and key (defined in the extension |
| 24 // dispatching a pref changed event. For example, | 37 // API) for dispatching a pref changed event. For example, |
| 25 // "webkit.webprefs.default_font_size" to ("onDefaultFontSizedChanged", | 38 // "webkit.webprefs.default_font_size" to ("onDefaultFontSizedChanged", |
| 26 // "pixelSize"). | 39 // "pixelSize"). |
| 27 typedef std::map<std::string, EventAndKeyPair> PrefEventMap; | 40 typedef std::map<std::string, EventAndKeyPair> PrefEventMap; |
| 28 | 41 |
| 42 // Observes browser pref |pref_name|. When a change is observed, dispatches |
| 43 // event |event_name| to extensions. A JavaScript object is passed to the |
| 44 // extension event function with the new value of the pref in property |key|. |
| 29 void AddPrefToObserve(const char* pref_name, | 45 void AddPrefToObserve(const char* pref_name, |
| 30 const char* event_name, | 46 const char* event_name, |
| 31 const char* key); | 47 const char* key); |
| 32 | 48 |
| 33 // content::NotificationObserver implementation. | 49 // content::NotificationObserver implementation. |
| 34 virtual void Observe(int type, | 50 virtual void Observe(int type, |
| 35 const content::NotificationSource& source, | 51 const content::NotificationSource& source, |
| 36 const content::NotificationDetails& details) OVERRIDE; | 52 const content::NotificationDetails& details) OVERRIDE; |
| 37 | 53 |
| 54 // Dispatches a changed event for the font setting for |generic_family| and |
| 55 // |script| to extensions. The new value of the setting is the value of |
| 56 // browser pref |pref_name|. If the pref changed on the incognito profile, |
| 57 // |incognito| must be set to true for extensions to get the appropriate |
| 58 // event. |
| 38 void OnFontNamePrefChanged(PrefService* pref_service, | 59 void OnFontNamePrefChanged(PrefService* pref_service, |
| 39 const std::string& pref_name, | 60 const std::string& pref_name, |
| 40 const std::string& generic_family, | 61 const std::string& generic_family, |
| 41 const std::string& script, | 62 const std::string& script, |
| 42 bool incognito); | 63 bool incognito); |
| 64 |
| 65 // Dispatches the setting changed event |event_name| to extensions. The new |
| 66 // value of the setting is the value of browser pref |pref_name|. This value |
| 67 // is passed in the JavaScript object argument to the extension event function |
| 68 // under the key |key|. If the pref changed on the incognito profile, |
| 69 // |incognito| must be set to true for extensions to get the appropriate |
| 70 // event. |
| 43 void OnFontPrefChanged(PrefService* pref_service, | 71 void OnFontPrefChanged(PrefService* pref_service, |
| 44 const std::string& pref_name, | 72 const std::string& pref_name, |
| 45 const std::string& event_name, | 73 const std::string& event_name, |
| 46 const std::string& key, | 74 const std::string& key, |
| 47 bool incognito); | 75 bool incognito); |
| 48 | 76 |
| 77 // Manages pref observation registration. |
| 49 PrefChangeRegistrar registrar_; | 78 PrefChangeRegistrar registrar_; |
| 50 | 79 |
| 80 // Maps browser pref names to extension API <event name, key> pairs. |
| 51 PrefEventMap pref_event_map_; | 81 PrefEventMap pref_event_map_; |
| 52 | 82 |
| 53 // Weak, owns us (transitively via ExtensionService). | 83 // Weak, owns us (transitively via ExtensionService). |
| 54 Profile* profile_; | 84 Profile* profile_; |
| 55 | 85 |
| 56 DISALLOW_COPY_AND_ASSIGN(ExtensionFontSettingsEventRouter); | 86 DISALLOW_COPY_AND_ASSIGN(ExtensionFontSettingsEventRouter); |
| 57 }; | 87 }; |
| 58 | 88 |
| 89 // fontSettings.clearFont API function. |
| 59 class ClearFontFunction : public SyncExtensionFunction { | 90 class ClearFontFunction : public SyncExtensionFunction { |
| 60 public: | 91 public: |
| 61 DECLARE_EXTENSION_FUNCTION_NAME("experimental.fontSettings.clearFont") | 92 DECLARE_EXTENSION_FUNCTION_NAME("experimental.fontSettings.clearFont") |
| 62 | 93 |
| 63 protected: | 94 protected: |
| 95 // RefCounted types have non-public destructors, as with all extension |
| 96 // functions in this file. |
| 64 virtual ~ClearFontFunction() {} | 97 virtual ~ClearFontFunction() {} |
| 65 | 98 |
| 66 // ExtensionFunction: | 99 // ExtensionFunction: |
| 67 virtual bool RunImpl() OVERRIDE; | 100 virtual bool RunImpl() OVERRIDE; |
| 68 }; | 101 }; |
| 69 | 102 |
| 103 // fontSettings.getFont API function. |
| 70 class GetFontFunction : public SyncExtensionFunction { | 104 class GetFontFunction : public SyncExtensionFunction { |
| 71 public: | 105 public: |
| 72 DECLARE_EXTENSION_FUNCTION_NAME("experimental.fontSettings.getFont") | 106 DECLARE_EXTENSION_FUNCTION_NAME("experimental.fontSettings.getFont") |
| 73 | 107 |
| 74 protected: | 108 protected: |
| 75 virtual ~GetFontFunction() {} | 109 virtual ~GetFontFunction() {} |
| 76 | 110 |
| 77 // ExtensionFunction: | 111 // ExtensionFunction: |
| 78 virtual bool RunImpl() OVERRIDE; | 112 virtual bool RunImpl() OVERRIDE; |
| 79 }; | 113 }; |
| 80 | 114 |
| 115 // fontSettings.setFont API function. |
| 81 class SetFontFunction : public SyncExtensionFunction { | 116 class SetFontFunction : public SyncExtensionFunction { |
| 82 public: | 117 public: |
| 83 DECLARE_EXTENSION_FUNCTION_NAME("experimental.fontSettings.setFont") | 118 DECLARE_EXTENSION_FUNCTION_NAME("experimental.fontSettings.setFont") |
| 84 | 119 |
| 85 protected: | 120 protected: |
| 86 virtual ~SetFontFunction() {} | 121 virtual ~SetFontFunction() {} |
| 87 | 122 |
| 88 // ExtensionFunction: | 123 // ExtensionFunction: |
| 89 virtual bool RunImpl() OVERRIDE; | 124 virtual bool RunImpl() OVERRIDE; |
| 90 }; | 125 }; |
| 91 | 126 |
| 127 // fontSettings.getFontList API function. |
| 92 class GetFontListFunction : public AsyncExtensionFunction { | 128 class GetFontListFunction : public AsyncExtensionFunction { |
| 93 public: | 129 public: |
| 94 DECLARE_EXTENSION_FUNCTION_NAME("experimental.fontSettings.getFontList") | 130 DECLARE_EXTENSION_FUNCTION_NAME("experimental.fontSettings.getFontList") |
| 95 | 131 |
| 96 protected: | 132 protected: |
| 97 virtual ~GetFontListFunction() {} | 133 virtual ~GetFontListFunction() {} |
| 98 | 134 |
| 99 // ExtensionFunction: | 135 // ExtensionFunction: |
| 100 virtual bool RunImpl() OVERRIDE; | 136 virtual bool RunImpl() OVERRIDE; |
| 101 | 137 |
| 102 private: | 138 private: |
| 103 void FontListHasLoaded(scoped_ptr<base::ListValue> list); | 139 void FontListHasLoaded(scoped_ptr<base::ListValue> list); |
| 104 bool CopyFontsToResult(base::ListValue* fonts); | 140 bool CopyFontsToResult(base::ListValue* fonts); |
| 105 }; | 141 }; |
| 106 | 142 |
| 107 // Base class for functions that clear a font pref. | 143 // Base class for extension API functions that clear a browser font pref. |
| 108 class ClearFontPrefExtensionFunction : public SyncExtensionFunction { | 144 class ClearFontPrefExtensionFunction : public SyncExtensionFunction { |
| 109 protected: | 145 protected: |
| 110 virtual ~ClearFontPrefExtensionFunction() {} | 146 virtual ~ClearFontPrefExtensionFunction() {} |
| 111 | 147 |
| 112 // ExtensionFunction: | 148 // ExtensionFunction: |
| 113 virtual bool RunImpl() OVERRIDE; | 149 virtual bool RunImpl() OVERRIDE; |
| 114 | 150 |
| 115 // Implementations should return the name of the preference to clear, like | 151 // Implementations should return the name of the preference to clear, like |
| 116 // "webkit.webprefs.default_font_size". | 152 // "webkit.webprefs.default_font_size". |
| 117 virtual const char* GetPrefName() = 0; | 153 virtual const char* GetPrefName() = 0; |
| 118 }; | 154 }; |
| 119 | 155 |
| 120 // Base class for functions that get a font pref. | 156 // Base class for extension API functions that get a browser font pref. |
| 121 class GetFontPrefExtensionFunction : public SyncExtensionFunction { | 157 class GetFontPrefExtensionFunction : public SyncExtensionFunction { |
| 122 protected: | 158 protected: |
| 123 virtual ~GetFontPrefExtensionFunction() {} | 159 virtual ~GetFontPrefExtensionFunction() {} |
| 124 | 160 |
| 125 // ExtensionFunction: | 161 // ExtensionFunction: |
| 126 virtual bool RunImpl() OVERRIDE; | 162 virtual bool RunImpl() OVERRIDE; |
| 127 | 163 |
| 128 // Implementations should return the name of the preference to get, like | 164 // Implementations should return the name of the preference to get, like |
| 129 // "webkit.webprefs.default_font_size". | 165 // "webkit.webprefs.default_font_size". |
| 130 virtual const char* GetPrefName() = 0; | 166 virtual const char* GetPrefName() = 0; |
| 131 | 167 |
| 132 // Implementations should return the key for the value in the extension API, | 168 // Implementations should return the key for the value in the extension API, |
| 133 // like "pixelSize". | 169 // like "pixelSize". |
| 134 virtual const char* GetKey() = 0; | 170 virtual const char* GetKey() = 0; |
| 135 }; | 171 }; |
| 136 | 172 |
| 137 // Base class for functions that set a font pref. | 173 // Base class for extension API functions that set a browser font pref. |
| 138 class SetFontPrefExtensionFunction : public SyncExtensionFunction { | 174 class SetFontPrefExtensionFunction : public SyncExtensionFunction { |
| 139 protected: | 175 protected: |
| 140 virtual ~SetFontPrefExtensionFunction() {} | 176 virtual ~SetFontPrefExtensionFunction() {} |
| 141 | 177 |
| 142 // ExtensionFunction: | 178 // ExtensionFunction: |
| 143 virtual bool RunImpl() OVERRIDE; | 179 virtual bool RunImpl() OVERRIDE; |
| 144 | 180 |
| 145 // Implementations should return the name of the preference to set, like | 181 // Implementations should return the name of the preference to set, like |
| 146 // "webkit.webprefs.default_font_size". | 182 // "webkit.webprefs.default_font_size". |
| 147 virtual const char* GetPrefName() = 0; | 183 virtual const char* GetPrefName() = 0; |
| 148 | 184 |
| 149 // Implementations should return the key for the value in the extension API, | 185 // Implementations should return the key for the value in the extension API, |
| 150 // like "pixelSize". | 186 // like "pixelSize". |
| 151 virtual const char* GetKey() = 0; | 187 virtual const char* GetKey() = 0; |
| 152 }; | 188 }; |
| 153 | 189 |
| 190 // The following are get/set/clear API functions that act on a browser font |
| 191 // pref. |
| 192 |
| 154 class ClearDefaultFontSizeFunction : public ClearFontPrefExtensionFunction { | 193 class ClearDefaultFontSizeFunction : public ClearFontPrefExtensionFunction { |
| 155 public: | 194 public: |
| 156 DECLARE_EXTENSION_FUNCTION_NAME( | 195 DECLARE_EXTENSION_FUNCTION_NAME( |
| 157 "experimental.fontSettings.clearDefaultFontSize") | 196 "experimental.fontSettings.clearDefaultFontSize") |
| 158 | 197 |
| 159 protected: | 198 protected: |
| 160 virtual ~ClearDefaultFontSizeFunction() {} | 199 virtual ~ClearDefaultFontSizeFunction() {} |
| 161 | 200 |
| 162 // ClearFontPrefExtensionFunction: | 201 // ClearFontPrefExtensionFunction: |
| 163 virtual const char* GetPrefName() OVERRIDE; | 202 virtual const char* GetPrefName() OVERRIDE; |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 | 337 |
| 299 protected: | 338 protected: |
| 300 virtual ~SetDefaultCharacterSetFunction() {} | 339 virtual ~SetDefaultCharacterSetFunction() {} |
| 301 | 340 |
| 302 // SetFontPrefExtensionFunction: | 341 // SetFontPrefExtensionFunction: |
| 303 virtual const char* GetPrefName() OVERRIDE; | 342 virtual const char* GetPrefName() OVERRIDE; |
| 304 virtual const char* GetKey() OVERRIDE; | 343 virtual const char* GetKey() OVERRIDE; |
| 305 }; | 344 }; |
| 306 | 345 |
| 307 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FONT_SETTINGS_API_H__ | 346 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FONT_SETTINGS_API_H__ |
| OLD | NEW |