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