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.
5 // Defines the classes to realize the Font Settings Extension API as specified
6 // in the extension API JSON.
8 #ifndef CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H_
9 #define CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H_
13 #include "base/memory/scoped_ptr.h"
14 #include "base/prefs/pref_change_registrar.h"
15 #include "base/prefs/pref_service.h"
16 #include "chrome/browser/extensions/chrome_extension_function.h"
17 #include "extensions/browser/browser_context_keyed_api_factory.h"
18 #include "extensions/browser/event_router.h"
26 namespace extensions
{
28 // This class observes pref changed events on a profile and dispatches the
29 // corresponding extension API events to extensions.
30 class FontSettingsEventRouter
{
32 // Constructor for observing pref changed events on |profile|. Stores a
33 // pointer to |profile| but does not take ownership. |profile| must be
34 // non-NULL and remain alive for the lifetime of the instance.
35 explicit FontSettingsEventRouter(Profile
* profile
);
36 virtual ~FontSettingsEventRouter();
39 // Observes browser pref |pref_name|. When a change is observed, dispatches
40 // event |event_name| to extensions. A JavaScript object is passed to the
41 // extension event function with the new value of the pref in property |key|.
42 void AddPrefToObserve(const char* pref_name
,
43 const char* event_name
,
46 // Decodes a preference change for a font family map and invokes
47 // OnFontNamePrefChange with the right parameters.
48 void OnFontFamilyMapPrefChanged(const std::string
& pref_name
);
50 // Dispatches a changed event for the font setting for |generic_family| and
51 // |script| to extensions. The new value of the setting is the value of
52 // browser pref |pref_name|.
53 void OnFontNamePrefChanged(const std::string
& pref_name
,
54 const std::string
& generic_family
,
55 const std::string
& script
);
57 // Dispatches the setting changed event |event_name| to extensions. The new
58 // value of the setting is the value of browser pref |pref_name|. This value
59 // is passed in the JavaScript object argument to the extension event function
60 // under the key |key|.
61 void OnFontPrefChanged(const std::string
& event_name
,
62 const std::string
& key
,
63 const std::string
& pref_name
);
65 // Manages pref observation registration.
66 PrefChangeRegistrar registrar_
;
68 // Weak, owns us (transitively via ExtensionService).
71 DISALLOW_COPY_AND_ASSIGN(FontSettingsEventRouter
);
74 // The profile-keyed service that manages the font_settings extension API.
75 // This is not an EventRouter::Observer (and does not lazily initialize) because
76 // doing so caused a regression in perf tests. See crbug.com/163466.
77 class FontSettingsAPI
: public BrowserContextKeyedAPI
{
79 explicit FontSettingsAPI(content::BrowserContext
* context
);
80 virtual ~FontSettingsAPI();
82 // BrowserContextKeyedAPI implementation.
83 static BrowserContextKeyedAPIFactory
<FontSettingsAPI
>* GetFactoryInstance();
86 friend class BrowserContextKeyedAPIFactory
<FontSettingsAPI
>;
88 // BrowserContextKeyedAPI implementation.
89 static const char* service_name() {
90 return "FontSettingsAPI";
92 static const bool kServiceIsNULLWhileTesting
= true;
94 scoped_ptr
<FontSettingsEventRouter
> font_settings_event_router_
;
97 // fontSettings.clearFont API function.
98 class FontSettingsClearFontFunction
: public ChromeSyncExtensionFunction
{
100 DECLARE_EXTENSION_FUNCTION("fontSettings.clearFont", FONTSETTINGS_CLEARFONT
)
103 // RefCounted types have non-public destructors, as with all extension
104 // functions in this file.
105 virtual ~FontSettingsClearFontFunction() {}
107 // ExtensionFunction:
108 virtual bool RunSync() OVERRIDE
;
111 // fontSettings.getFont API function.
112 class FontSettingsGetFontFunction
: public ChromeSyncExtensionFunction
{
114 DECLARE_EXTENSION_FUNCTION("fontSettings.getFont", FONTSETTINGS_GETFONT
)
117 virtual ~FontSettingsGetFontFunction() {}
119 // ExtensionFunction:
120 virtual bool RunSync() OVERRIDE
;
123 // fontSettings.setFont API function.
124 class FontSettingsSetFontFunction
: public ChromeSyncExtensionFunction
{
126 DECLARE_EXTENSION_FUNCTION("fontSettings.setFont", FONTSETTINGS_SETFONT
)
129 virtual ~FontSettingsSetFontFunction() {}
131 // ExtensionFunction:
132 virtual bool RunSync() OVERRIDE
;
135 // fontSettings.getFontList API function.
136 class FontSettingsGetFontListFunction
: public ChromeAsyncExtensionFunction
{
138 DECLARE_EXTENSION_FUNCTION("fontSettings.getFontList",
139 FONTSETTINGS_GETFONTLIST
)
142 virtual ~FontSettingsGetFontListFunction() {}
144 // ExtensionFunction:
145 virtual bool RunImpl() OVERRIDE
;
148 void FontListHasLoaded(scoped_ptr
<base::ListValue
> list
);
149 bool CopyFontsToResult(base::ListValue
* fonts
);
152 // Base class for extension API functions that clear a browser font pref.
153 class ClearFontPrefExtensionFunction
: public ChromeSyncExtensionFunction
{
155 virtual ~ClearFontPrefExtensionFunction() {}
157 // ExtensionFunction:
158 virtual bool RunSync() OVERRIDE
;
160 // Implementations should return the name of the preference to clear, like
161 // "webkit.webprefs.default_font_size".
162 virtual const char* GetPrefName() = 0;
165 // Base class for extension API functions that get a browser font pref.
166 class GetFontPrefExtensionFunction
: public ChromeSyncExtensionFunction
{
168 virtual ~GetFontPrefExtensionFunction() {}
170 // ExtensionFunction:
171 virtual bool RunSync() OVERRIDE
;
173 // Implementations should return the name of the preference to get, like
174 // "webkit.webprefs.default_font_size".
175 virtual const char* GetPrefName() = 0;
177 // Implementations should return the key for the value in the extension API,
179 virtual const char* GetKey() = 0;
182 // Base class for extension API functions that set a browser font pref.
183 class SetFontPrefExtensionFunction
: public ChromeSyncExtensionFunction
{
185 virtual ~SetFontPrefExtensionFunction() {}
187 // ExtensionFunction:
188 virtual bool RunSync() OVERRIDE
;
190 // Implementations should return the name of the preference to set, like
191 // "webkit.webprefs.default_font_size".
192 virtual const char* GetPrefName() = 0;
194 // Implementations should return the key for the value in the extension API,
196 virtual const char* GetKey() = 0;
199 // The following are get/set/clear API functions that act on a browser font
202 class FontSettingsClearDefaultFontSizeFunction
203 : public ClearFontPrefExtensionFunction
{
205 DECLARE_EXTENSION_FUNCTION("fontSettings.clearDefaultFontSize",
206 FONTSETTINGS_CLEARDEFAULTFONTSIZE
)
209 virtual ~FontSettingsClearDefaultFontSizeFunction() {}
211 // ClearFontPrefExtensionFunction:
212 virtual const char* GetPrefName() OVERRIDE
;
215 class FontSettingsGetDefaultFontSizeFunction
216 : public GetFontPrefExtensionFunction
{
218 DECLARE_EXTENSION_FUNCTION("fontSettings.getDefaultFontSize",
219 FONTSETTINGS_GETDEFAULTFONTSIZE
)
222 virtual ~FontSettingsGetDefaultFontSizeFunction() {}
224 // GetFontPrefExtensionFunction:
225 virtual const char* GetPrefName() OVERRIDE
;
226 virtual const char* GetKey() OVERRIDE
;
229 class FontSettingsSetDefaultFontSizeFunction
230 : public SetFontPrefExtensionFunction
{
232 DECLARE_EXTENSION_FUNCTION("fontSettings.setDefaultFontSize",
233 FONTSETTINGS_SETDEFAULTFONTSIZE
)
236 virtual ~FontSettingsSetDefaultFontSizeFunction() {}
238 // SetFontPrefExtensionFunction:
239 virtual const char* GetPrefName() OVERRIDE
;
240 virtual const char* GetKey() OVERRIDE
;
243 class FontSettingsClearDefaultFixedFontSizeFunction
244 : public ClearFontPrefExtensionFunction
{
246 DECLARE_EXTENSION_FUNCTION("fontSettings.clearDefaultFixedFontSize",
247 FONTSETTINGS_CLEARDEFAULTFIXEDFONTSIZE
)
250 virtual ~FontSettingsClearDefaultFixedFontSizeFunction() {}
252 // ClearFontPrefExtensionFunction:
253 virtual const char* GetPrefName() OVERRIDE
;
256 class FontSettingsGetDefaultFixedFontSizeFunction
257 : public GetFontPrefExtensionFunction
{
259 DECLARE_EXTENSION_FUNCTION("fontSettings.getDefaultFixedFontSize",
260 FONTSETTINGS_GETDEFAULTFIXEDFONTSIZE
)
263 virtual ~FontSettingsGetDefaultFixedFontSizeFunction() {}
265 // GetFontPrefExtensionFunction:
266 virtual const char* GetPrefName() OVERRIDE
;
267 virtual const char* GetKey() OVERRIDE
;
270 class FontSettingsSetDefaultFixedFontSizeFunction
271 : public SetFontPrefExtensionFunction
{
273 DECLARE_EXTENSION_FUNCTION("fontSettings.setDefaultFixedFontSize",
274 FONTSETTINGS_SETDEFAULTFIXEDFONTSIZE
)
277 virtual ~FontSettingsSetDefaultFixedFontSizeFunction() {}
279 // SetFontPrefExtensionFunction:
280 virtual const char* GetPrefName() OVERRIDE
;
281 virtual const char* GetKey() OVERRIDE
;
284 class FontSettingsClearMinimumFontSizeFunction
285 : public ClearFontPrefExtensionFunction
{
287 DECLARE_EXTENSION_FUNCTION("fontSettings.clearMinimumFontSize",
288 FONTSETTINGS_CLEARMINIMUMFONTSIZE
)
291 virtual ~FontSettingsClearMinimumFontSizeFunction() {}
293 // ClearFontPrefExtensionFunction:
294 virtual const char* GetPrefName() OVERRIDE
;
297 class FontSettingsGetMinimumFontSizeFunction
298 : public GetFontPrefExtensionFunction
{
300 DECLARE_EXTENSION_FUNCTION("fontSettings.getMinimumFontSize",
301 FONTSETTINGS_GETMINIMUMFONTSIZE
)
304 virtual ~FontSettingsGetMinimumFontSizeFunction() {}
306 // GetFontPrefExtensionFunction:
307 virtual const char* GetPrefName() OVERRIDE
;
308 virtual const char* GetKey() OVERRIDE
;
311 class FontSettingsSetMinimumFontSizeFunction
312 : public SetFontPrefExtensionFunction
{
314 DECLARE_EXTENSION_FUNCTION("fontSettings.setMinimumFontSize",
315 FONTSETTINGS_SETMINIMUMFONTSIZE
)
318 virtual ~FontSettingsSetMinimumFontSizeFunction() {}
320 // SetFontPrefExtensionFunction:
321 virtual const char* GetPrefName() OVERRIDE
;
322 virtual const char* GetKey() OVERRIDE
;
325 } // namespace extensions
327 #endif // CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H_