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 | 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> | 
| 
 
falken
2012/11/14 14:09:24
Can we get rid of <map> and <utility> now?
 
Jói
2012/11/14 14:40:21
Done.
 
 | |
| 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; | |
| 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 | 32 // Observes browser pref |pref_name|. When a change is observed, dispatches | 
| 41 // event |event_name| to extensions. A JavaScript object is passed to the | 33 // 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|. | 34 // extension event function with the new value of the pref in property |key|. | 
| 43 void AddPrefToObserve(const char* pref_name, | 35 void AddPrefToObserve(const char* pref_name, | 
| 44 const char* event_name, | 36 const char* event_name, | 
| 45 const char* key); | 37 const char* key); | 
| 46 | 38 | 
| 47 // PrefObserver implementation. | 39 // Decodes a preference change for a font family map and invokes | 
| 48 virtual void OnPreferenceChanged(PrefServiceBase* service, | 40 // OnFontNamePrefChange with the right parameters. | 
| 49 const std::string& pref_name) OVERRIDE; | 41 void OnFontFamilyPrefChanged(const std::string& pref_name); | 
| 
 
falken
2012/11/14 14:09:24
How about "OnFontFamilyMapPrefChanged"?
It's a bi
 
Jói
2012/11/14 14:40:21
Done.
 
 | |
| 50 | 42 | 
| 51 // Dispatches a changed event for the font setting for |generic_family| and | 43 // 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 | 44 // |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, | 45 // browser pref |pref_name|. | 
| 54 // |incognito| must be set to true for extensions to get the appropriate | 46 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, | 47 const std::string& generic_family, | 
| 59 const std::string& script, | 48 const std::string& script); | 
| 60 bool incognito); | |
| 61 | 49 | 
| 62 // Dispatches the setting changed event |event_name| to extensions. The new | 50 // 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 | 51 // 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 | 52 // 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, | 53 // under the key |key|. | 
| 66 // |incognito| must be set to true for extensions to get the appropriate | 54 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, | 55 const std::string& key, | 
| 72 bool incognito); | 56 const std::string& pref_name); | 
| 73 | 57 | 
| 74 // Manages pref observation registration. | 58 // Manages pref observation registration. | 
| 75 PrefChangeRegistrar registrar_; | 59 PrefChangeRegistrar registrar_; | 
| 76 | 60 | 
| 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). | 61 // Weak, owns us (transitively via ExtensionService). | 
| 81 Profile* profile_; | 62 Profile* profile_; | 
| 82 | 63 | 
| 83 DISALLOW_COPY_AND_ASSIGN(FontSettingsEventRouter); | 64 DISALLOW_COPY_AND_ASSIGN(FontSettingsEventRouter); | 
| 84 }; | 65 }; | 
| 85 | 66 | 
| 86 // fontSettings.clearFont API function. | 67 // fontSettings.clearFont API function. | 
| 87 class ClearFontFunction : public SyncExtensionFunction { | 68 class ClearFontFunction : public SyncExtensionFunction { | 
| 88 public: | 69 public: | 
| 89 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.clearFont") | 70 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.clearFont") | 
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 virtual ~SetMinimumFontSizeFunction() {} | 270 virtual ~SetMinimumFontSizeFunction() {} | 
| 290 | 271 | 
| 291 // SetFontPrefExtensionFunction: | 272 // SetFontPrefExtensionFunction: | 
| 292 virtual const char* GetPrefName() OVERRIDE; | 273 virtual const char* GetPrefName() OVERRIDE; | 
| 293 virtual const char* GetKey() OVERRIDE; | 274 virtual const char* GetKey() OVERRIDE; | 
| 294 }; | 275 }; | 
| 295 | 276 | 
| 296 } // namespace extensions | 277 } // namespace extensions | 
| 297 | 278 | 
| 298 #endif // CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H__ | 279 #endif // CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H__ | 
| OLD | NEW |