/chromium-webcl/src/chrome/browser/extensions/api/font_settings/font_settings_api.h

https://bitbucket.org/peixuan/chromium_r197479_base · C Header · 323 lines · 184 code · 73 blank · 66 comment · 0 complexity · 577438f78bf2bca7d085f7a67a6606e0 MD5 · raw file

  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
  3. // found in the LICENSE file.
  4. // Defines the classes to realize the Font Settings Extension API as specified
  5. // in the extension API JSON.
  6. #ifndef CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H_
  7. #define CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H_
  8. #include <string>
  9. #include "base/memory/scoped_ptr.h"
  10. #include "base/prefs/pref_change_registrar.h"
  11. #include "base/prefs/pref_service.h"
  12. #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
  13. #include "chrome/browser/extensions/event_router.h"
  14. #include "chrome/browser/extensions/extension_function.h"
  15. class Profile;
  16. namespace extensions {
  17. // This class observes pref changed events on a profile and dispatches the
  18. // corresponding extension API events to extensions.
  19. class FontSettingsEventRouter {
  20. public:
  21. // Constructor for observing pref changed events on |profile|. Stores a
  22. // pointer to |profile| but does not take ownership. |profile| must be
  23. // non-NULL and remain alive for the lifetime of the instance.
  24. explicit FontSettingsEventRouter(Profile* profile);
  25. virtual ~FontSettingsEventRouter();
  26. private:
  27. // Observes browser pref |pref_name|. When a change is observed, dispatches
  28. // event |event_name| to extensions. A JavaScript object is passed to the
  29. // extension event function with the new value of the pref in property |key|.
  30. void AddPrefToObserve(const char* pref_name,
  31. const char* event_name,
  32. const char* key);
  33. // Decodes a preference change for a font family map and invokes
  34. // OnFontNamePrefChange with the right parameters.
  35. void OnFontFamilyMapPrefChanged(const std::string& pref_name);
  36. // Dispatches a changed event for the font setting for |generic_family| and
  37. // |script| to extensions. The new value of the setting is the value of
  38. // browser pref |pref_name|.
  39. void OnFontNamePrefChanged(const std::string& pref_name,
  40. const std::string& generic_family,
  41. const std::string& script);
  42. // Dispatches the setting changed event |event_name| to extensions. The new
  43. // value of the setting is the value of browser pref |pref_name|. This value
  44. // is passed in the JavaScript object argument to the extension event function
  45. // under the key |key|.
  46. void OnFontPrefChanged(const std::string& event_name,
  47. const std::string& key,
  48. const std::string& pref_name);
  49. // Manages pref observation registration.
  50. PrefChangeRegistrar registrar_;
  51. // Weak, owns us (transitively via ExtensionService).
  52. Profile* profile_;
  53. DISALLOW_COPY_AND_ASSIGN(FontSettingsEventRouter);
  54. };
  55. // The profile-keyed service that manages the font_settings extension API.
  56. // This is not an EventRouter::Observer (and does not lazily initialize) because
  57. // doing so caused a regression in perf tests. See crbug.com/163466.
  58. class FontSettingsAPI : public ProfileKeyedAPI {
  59. public:
  60. explicit FontSettingsAPI(Profile* profile);
  61. virtual ~FontSettingsAPI();
  62. // ProfileKeyedAPI implementation.
  63. static ProfileKeyedAPIFactory<FontSettingsAPI>* GetFactoryInstance();
  64. private:
  65. friend class ProfileKeyedAPIFactory<FontSettingsAPI>;
  66. // ProfileKeyedAPI implementation.
  67. static const char* service_name() {
  68. return "FontSettingsAPI";
  69. }
  70. static const bool kServiceIsNULLWhileTesting = true;
  71. scoped_ptr<FontSettingsEventRouter> font_settings_event_router_;
  72. };
  73. // fontSettings.clearFont API function.
  74. class FontSettingsClearFontFunction : public SyncExtensionFunction {
  75. public:
  76. DECLARE_EXTENSION_FUNCTION("fontSettings.clearFont", FONTSETTINGS_CLEARFONT)
  77. protected:
  78. // RefCounted types have non-public destructors, as with all extension
  79. // functions in this file.
  80. virtual ~FontSettingsClearFontFunction() {}
  81. // ExtensionFunction:
  82. virtual bool RunImpl() OVERRIDE;
  83. };
  84. // fontSettings.getFont API function.
  85. class FontSettingsGetFontFunction : public SyncExtensionFunction {
  86. public:
  87. DECLARE_EXTENSION_FUNCTION("fontSettings.getFont", FONTSETTINGS_GETFONT)
  88. protected:
  89. virtual ~FontSettingsGetFontFunction() {}
  90. // ExtensionFunction:
  91. virtual bool RunImpl() OVERRIDE;
  92. };
  93. // fontSettings.setFont API function.
  94. class FontSettingsSetFontFunction : public SyncExtensionFunction {
  95. public:
  96. DECLARE_EXTENSION_FUNCTION("fontSettings.setFont", FONTSETTINGS_SETFONT)
  97. protected:
  98. virtual ~FontSettingsSetFontFunction() {}
  99. // ExtensionFunction:
  100. virtual bool RunImpl() OVERRIDE;
  101. };
  102. // fontSettings.getFontList API function.
  103. class FontSettingsGetFontListFunction : public AsyncExtensionFunction {
  104. public:
  105. DECLARE_EXTENSION_FUNCTION("fontSettings.getFontList",
  106. FONTSETTINGS_GETFONTLIST)
  107. protected:
  108. virtual ~FontSettingsGetFontListFunction() {}
  109. // ExtensionFunction:
  110. virtual bool RunImpl() OVERRIDE;
  111. private:
  112. void FontListHasLoaded(scoped_ptr<base::ListValue> list);
  113. bool CopyFontsToResult(base::ListValue* fonts);
  114. };
  115. // Base class for extension API functions that clear a browser font pref.
  116. class ClearFontPrefExtensionFunction : public SyncExtensionFunction {
  117. protected:
  118. virtual ~ClearFontPrefExtensionFunction() {}
  119. // ExtensionFunction:
  120. virtual bool RunImpl() OVERRIDE;
  121. // Implementations should return the name of the preference to clear, like
  122. // "webkit.webprefs.default_font_size".
  123. virtual const char* GetPrefName() = 0;
  124. };
  125. // Base class for extension API functions that get a browser font pref.
  126. class GetFontPrefExtensionFunction : public SyncExtensionFunction {
  127. protected:
  128. virtual ~GetFontPrefExtensionFunction() {}
  129. // ExtensionFunction:
  130. virtual bool RunImpl() OVERRIDE;
  131. // Implementations should return the name of the preference to get, like
  132. // "webkit.webprefs.default_font_size".
  133. virtual const char* GetPrefName() = 0;
  134. // Implementations should return the key for the value in the extension API,
  135. // like "pixelSize".
  136. virtual const char* GetKey() = 0;
  137. };
  138. // Base class for extension API functions that set a browser font pref.
  139. class SetFontPrefExtensionFunction : public SyncExtensionFunction {
  140. protected:
  141. virtual ~SetFontPrefExtensionFunction() {}
  142. // ExtensionFunction:
  143. virtual bool RunImpl() OVERRIDE;
  144. // Implementations should return the name of the preference to set, like
  145. // "webkit.webprefs.default_font_size".
  146. virtual const char* GetPrefName() = 0;
  147. // Implementations should return the key for the value in the extension API,
  148. // like "pixelSize".
  149. virtual const char* GetKey() = 0;
  150. };
  151. // The following are get/set/clear API functions that act on a browser font
  152. // pref.
  153. class FontSettingsClearDefaultFontSizeFunction
  154. : public ClearFontPrefExtensionFunction {
  155. public:
  156. DECLARE_EXTENSION_FUNCTION("fontSettings.clearDefaultFontSize",
  157. FONTSETTINGS_CLEARDEFAULTFONTSIZE)
  158. protected:
  159. virtual ~FontSettingsClearDefaultFontSizeFunction() {}
  160. // ClearFontPrefExtensionFunction:
  161. virtual const char* GetPrefName() OVERRIDE;
  162. };
  163. class FontSettingsGetDefaultFontSizeFunction
  164. : public GetFontPrefExtensionFunction {
  165. public:
  166. DECLARE_EXTENSION_FUNCTION("fontSettings.getDefaultFontSize",
  167. FONTSETTINGS_GETDEFAULTFONTSIZE)
  168. protected:
  169. virtual ~FontSettingsGetDefaultFontSizeFunction() {}
  170. // GetFontPrefExtensionFunction:
  171. virtual const char* GetPrefName() OVERRIDE;
  172. virtual const char* GetKey() OVERRIDE;
  173. };
  174. class FontSettingsSetDefaultFontSizeFunction
  175. : public SetFontPrefExtensionFunction {
  176. public:
  177. DECLARE_EXTENSION_FUNCTION("fontSettings.setDefaultFontSize",
  178. FONTSETTINGS_SETDEFAULTFONTSIZE)
  179. protected:
  180. virtual ~FontSettingsSetDefaultFontSizeFunction() {}
  181. // SetFontPrefExtensionFunction:
  182. virtual const char* GetPrefName() OVERRIDE;
  183. virtual const char* GetKey() OVERRIDE;
  184. };
  185. class FontSettingsClearDefaultFixedFontSizeFunction
  186. : public ClearFontPrefExtensionFunction {
  187. public:
  188. DECLARE_EXTENSION_FUNCTION("fontSettings.clearDefaultFixedFontSize",
  189. FONTSETTINGS_CLEARDEFAULTFIXEDFONTSIZE)
  190. protected:
  191. virtual ~FontSettingsClearDefaultFixedFontSizeFunction() {}
  192. // ClearFontPrefExtensionFunction:
  193. virtual const char* GetPrefName() OVERRIDE;
  194. };
  195. class FontSettingsGetDefaultFixedFontSizeFunction
  196. : public GetFontPrefExtensionFunction {
  197. public:
  198. DECLARE_EXTENSION_FUNCTION("fontSettings.getDefaultFixedFontSize",
  199. FONTSETTINGS_GETDEFAULTFIXEDFONTSIZE)
  200. protected:
  201. virtual ~FontSettingsGetDefaultFixedFontSizeFunction() {}
  202. // GetFontPrefExtensionFunction:
  203. virtual const char* GetPrefName() OVERRIDE;
  204. virtual const char* GetKey() OVERRIDE;
  205. };
  206. class FontSettingsSetDefaultFixedFontSizeFunction
  207. : public SetFontPrefExtensionFunction {
  208. public:
  209. DECLARE_EXTENSION_FUNCTION("fontSettings.setDefaultFixedFontSize",
  210. FONTSETTINGS_SETDEFAULTFIXEDFONTSIZE)
  211. protected:
  212. virtual ~FontSettingsSetDefaultFixedFontSizeFunction() {}
  213. // SetFontPrefExtensionFunction:
  214. virtual const char* GetPrefName() OVERRIDE;
  215. virtual const char* GetKey() OVERRIDE;
  216. };
  217. class FontSettingsClearMinimumFontSizeFunction
  218. : public ClearFontPrefExtensionFunction {
  219. public:
  220. DECLARE_EXTENSION_FUNCTION("fontSettings.clearMinimumFontSize",
  221. FONTSETTINGS_CLEARMINIMUMFONTSIZE)
  222. protected:
  223. virtual ~FontSettingsClearMinimumFontSizeFunction() {}
  224. // ClearFontPrefExtensionFunction:
  225. virtual const char* GetPrefName() OVERRIDE;
  226. };
  227. class FontSettingsGetMinimumFontSizeFunction
  228. : public GetFontPrefExtensionFunction {
  229. public:
  230. DECLARE_EXTENSION_FUNCTION("fontSettings.getMinimumFontSize",
  231. FONTSETTINGS_GETMINIMUMFONTSIZE)
  232. protected:
  233. virtual ~FontSettingsGetMinimumFontSizeFunction() {}
  234. // GetFontPrefExtensionFunction:
  235. virtual const char* GetPrefName() OVERRIDE;
  236. virtual const char* GetKey() OVERRIDE;
  237. };
  238. class FontSettingsSetMinimumFontSizeFunction
  239. : public SetFontPrefExtensionFunction {
  240. public:
  241. DECLARE_EXTENSION_FUNCTION("fontSettings.setMinimumFontSize",
  242. FONTSETTINGS_SETMINIMUMFONTSIZE)
  243. protected:
  244. virtual ~FontSettingsSetMinimumFontSizeFunction() {}
  245. // SetFontPrefExtensionFunction:
  246. virtual const char* GetPrefName() OVERRIDE;
  247. virtual const char* GetKey() OVERRIDE;
  248. };
  249. } // namespace extensions
  250. #endif // CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H_