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"
19 #include "extensions/browser/extension_event_histogram_value.h"
27 namespace extensions
{
29 // This class observes pref changed events on a profile and dispatches the
30 // corresponding extension API events to extensions.
31 class FontSettingsEventRouter
{
33 // Constructor for observing pref changed events on |profile|. Stores a
34 // pointer to |profile| but does not take ownership. |profile| must be
35 // non-NULL and remain alive for the lifetime of the instance.
36 explicit FontSettingsEventRouter(Profile
* profile
);
37 virtual ~FontSettingsEventRouter();
40 // Observes browser pref |pref_name|. When a change is observed, dispatches
41 // 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|.
43 void AddPrefToObserve(const char* pref_name
,
44 events::HistogramValue histogram_value
,
45 const char* event_name
,
48 // Decodes a preference change for a font family map and invokes
49 // OnFontNamePrefChange with the right parameters.
50 void OnFontFamilyMapPrefChanged(const std::string
& pref_name
);
52 // Dispatches a changed event for the font setting for |generic_family| and
53 // |script| to extensions. The new value of the setting is the value of
54 // browser pref |pref_name|.
55 void OnFontNamePrefChanged(const std::string
& pref_name
,
56 const std::string
& generic_family
,
57 const std::string
& script
);
59 // Dispatches the setting changed event |event_name| to extensions. The new
60 // value of the setting is the value of browser pref |pref_name|. This value
61 // is passed in the JavaScript object argument to the extension event function
62 // under the key |key|.
63 void OnFontPrefChanged(events::HistogramValue histogram_value
,
64 const std::string
& event_name
,
65 const std::string
& key
,
66 const std::string
& pref_name
);
68 // Manages pref observation registration.
69 PrefChangeRegistrar registrar_
;
71 // Weak, owns us (transitively via ExtensionService).
74 DISALLOW_COPY_AND_ASSIGN(FontSettingsEventRouter
);
77 // The profile-keyed service that manages the font_settings extension API.
78 // This is not an EventRouter::Observer (and does not lazily initialize) because
79 // doing so caused a regression in perf tests. See crbug.com/163466.
80 class FontSettingsAPI
: public BrowserContextKeyedAPI
{
82 explicit FontSettingsAPI(content::BrowserContext
* context
);
83 ~FontSettingsAPI() override
;
85 // BrowserContextKeyedAPI implementation.
86 static BrowserContextKeyedAPIFactory
<FontSettingsAPI
>* GetFactoryInstance();
89 friend class BrowserContextKeyedAPIFactory
<FontSettingsAPI
>;
91 // BrowserContextKeyedAPI implementation.
92 static const char* service_name() {
93 return "FontSettingsAPI";
95 static const bool kServiceIsNULLWhileTesting
= true;
97 scoped_ptr
<FontSettingsEventRouter
> font_settings_event_router_
;
100 // fontSettings.clearFont API function.
101 class FontSettingsClearFontFunction
: public ChromeSyncExtensionFunction
{
103 DECLARE_EXTENSION_FUNCTION("fontSettings.clearFont", FONTSETTINGS_CLEARFONT
)
106 // RefCounted types have non-public destructors, as with all extension
107 // functions in this file.
108 ~FontSettingsClearFontFunction() override
{}
110 // ExtensionFunction:
111 bool RunSync() override
;
114 // fontSettings.getFont API function.
115 class FontSettingsGetFontFunction
: public ChromeSyncExtensionFunction
{
117 DECLARE_EXTENSION_FUNCTION("fontSettings.getFont", FONTSETTINGS_GETFONT
)
120 ~FontSettingsGetFontFunction() override
{}
122 // ExtensionFunction:
123 bool RunSync() override
;
126 // fontSettings.setFont API function.
127 class FontSettingsSetFontFunction
: public ChromeSyncExtensionFunction
{
129 DECLARE_EXTENSION_FUNCTION("fontSettings.setFont", FONTSETTINGS_SETFONT
)
132 ~FontSettingsSetFontFunction() override
{}
134 // ExtensionFunction:
135 bool RunSync() override
;
138 // fontSettings.getFontList API function.
139 class FontSettingsGetFontListFunction
: public ChromeAsyncExtensionFunction
{
141 DECLARE_EXTENSION_FUNCTION("fontSettings.getFontList",
142 FONTSETTINGS_GETFONTLIST
)
145 ~FontSettingsGetFontListFunction() override
{}
147 // ExtensionFunction:
148 bool RunAsync() override
;
151 void FontListHasLoaded(scoped_ptr
<base::ListValue
> list
);
152 bool CopyFontsToResult(base::ListValue
* fonts
);
155 // Base class for extension API functions that clear a browser font pref.
156 class ClearFontPrefExtensionFunction
: public ChromeSyncExtensionFunction
{
158 ~ClearFontPrefExtensionFunction() override
{}
160 // ExtensionFunction:
161 bool RunSync() override
;
163 // Implementations should return the name of the preference to clear, like
164 // "webkit.webprefs.default_font_size".
165 virtual const char* GetPrefName() = 0;
168 // Base class for extension API functions that get a browser font pref.
169 class GetFontPrefExtensionFunction
: public ChromeSyncExtensionFunction
{
171 ~GetFontPrefExtensionFunction() override
{}
173 // ExtensionFunction:
174 bool RunSync() override
;
176 // Implementations should return the name of the preference to get, like
177 // "webkit.webprefs.default_font_size".
178 virtual const char* GetPrefName() = 0;
180 // Implementations should return the key for the value in the extension API,
182 virtual const char* GetKey() = 0;
185 // Base class for extension API functions that set a browser font pref.
186 class SetFontPrefExtensionFunction
: public ChromeSyncExtensionFunction
{
188 ~SetFontPrefExtensionFunction() override
{}
190 // ExtensionFunction:
191 bool RunSync() override
;
193 // Implementations should return the name of the preference to set, like
194 // "webkit.webprefs.default_font_size".
195 virtual const char* GetPrefName() = 0;
197 // Implementations should return the key for the value in the extension API,
199 virtual const char* GetKey() = 0;
202 // The following are get/set/clear API functions that act on a browser font
205 class FontSettingsClearDefaultFontSizeFunction
206 : public ClearFontPrefExtensionFunction
{
208 DECLARE_EXTENSION_FUNCTION("fontSettings.clearDefaultFontSize",
209 FONTSETTINGS_CLEARDEFAULTFONTSIZE
)
212 ~FontSettingsClearDefaultFontSizeFunction() override
{}
214 // ClearFontPrefExtensionFunction:
215 const char* GetPrefName() override
;
218 class FontSettingsGetDefaultFontSizeFunction
219 : public GetFontPrefExtensionFunction
{
221 DECLARE_EXTENSION_FUNCTION("fontSettings.getDefaultFontSize",
222 FONTSETTINGS_GETDEFAULTFONTSIZE
)
225 ~FontSettingsGetDefaultFontSizeFunction() override
{}
227 // GetFontPrefExtensionFunction:
228 const char* GetPrefName() override
;
229 const char* GetKey() override
;
232 class FontSettingsSetDefaultFontSizeFunction
233 : public SetFontPrefExtensionFunction
{
235 DECLARE_EXTENSION_FUNCTION("fontSettings.setDefaultFontSize",
236 FONTSETTINGS_SETDEFAULTFONTSIZE
)
239 ~FontSettingsSetDefaultFontSizeFunction() override
{}
241 // SetFontPrefExtensionFunction:
242 const char* GetPrefName() override
;
243 const char* GetKey() override
;
246 class FontSettingsClearDefaultFixedFontSizeFunction
247 : public ClearFontPrefExtensionFunction
{
249 DECLARE_EXTENSION_FUNCTION("fontSettings.clearDefaultFixedFontSize",
250 FONTSETTINGS_CLEARDEFAULTFIXEDFONTSIZE
)
253 ~FontSettingsClearDefaultFixedFontSizeFunction() override
{}
255 // ClearFontPrefExtensionFunction:
256 const char* GetPrefName() override
;
259 class FontSettingsGetDefaultFixedFontSizeFunction
260 : public GetFontPrefExtensionFunction
{
262 DECLARE_EXTENSION_FUNCTION("fontSettings.getDefaultFixedFontSize",
263 FONTSETTINGS_GETDEFAULTFIXEDFONTSIZE
)
266 ~FontSettingsGetDefaultFixedFontSizeFunction() override
{}
268 // GetFontPrefExtensionFunction:
269 const char* GetPrefName() override
;
270 const char* GetKey() override
;
273 class FontSettingsSetDefaultFixedFontSizeFunction
274 : public SetFontPrefExtensionFunction
{
276 DECLARE_EXTENSION_FUNCTION("fontSettings.setDefaultFixedFontSize",
277 FONTSETTINGS_SETDEFAULTFIXEDFONTSIZE
)
280 ~FontSettingsSetDefaultFixedFontSizeFunction() override
{}
282 // SetFontPrefExtensionFunction:
283 const char* GetPrefName() override
;
284 const char* GetKey() override
;
287 class FontSettingsClearMinimumFontSizeFunction
288 : public ClearFontPrefExtensionFunction
{
290 DECLARE_EXTENSION_FUNCTION("fontSettings.clearMinimumFontSize",
291 FONTSETTINGS_CLEARMINIMUMFONTSIZE
)
294 ~FontSettingsClearMinimumFontSizeFunction() override
{}
296 // ClearFontPrefExtensionFunction:
297 const char* GetPrefName() override
;
300 class FontSettingsGetMinimumFontSizeFunction
301 : public GetFontPrefExtensionFunction
{
303 DECLARE_EXTENSION_FUNCTION("fontSettings.getMinimumFontSize",
304 FONTSETTINGS_GETMINIMUMFONTSIZE
)
307 ~FontSettingsGetMinimumFontSizeFunction() override
{}
309 // GetFontPrefExtensionFunction:
310 const char* GetPrefName() override
;
311 const char* GetKey() override
;
314 class FontSettingsSetMinimumFontSizeFunction
315 : public SetFontPrefExtensionFunction
{
317 DECLARE_EXTENSION_FUNCTION("fontSettings.setMinimumFontSize",
318 FONTSETTINGS_SETMINIMUMFONTSIZE
)
321 ~FontSettingsSetMinimumFontSizeFunction() override
{}
323 // SetFontPrefExtensionFunction:
324 const char* GetPrefName() override
;
325 const char* GetKey() override
;
328 } // namespace extensions
330 #endif // CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H_