Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / font_settings / font_settings_api.h
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
5 // Defines the classes to realize the Font Settings Extension API as specified
6 // in the extension API JSON.
7
8 #ifndef CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H_
9 #define CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H_
10
11 #include <string>
12
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
20 class Profile;
21
22 namespace content {
23 class BrowserContext;
24 }
25
26 namespace extensions {
27
28 // This class observes pref changed events on a profile and dispatches the
29 // corresponding extension API events to extensions.
30 class FontSettingsEventRouter {
31  public:
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();
37
38  private:
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,
44                         const char* key);
45
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);
49
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);
56
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);
64
65   // Manages pref observation registration.
66   PrefChangeRegistrar registrar_;
67
68   // Weak, owns us (transitively via ExtensionService).
69   Profile* profile_;
70
71   DISALLOW_COPY_AND_ASSIGN(FontSettingsEventRouter);
72 };
73
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 {
78  public:
79   explicit FontSettingsAPI(content::BrowserContext* context);
80   virtual ~FontSettingsAPI();
81
82   // BrowserContextKeyedAPI implementation.
83   static BrowserContextKeyedAPIFactory<FontSettingsAPI>* GetFactoryInstance();
84
85  private:
86   friend class BrowserContextKeyedAPIFactory<FontSettingsAPI>;
87
88   // BrowserContextKeyedAPI implementation.
89   static const char* service_name() {
90     return "FontSettingsAPI";
91   }
92   static const bool kServiceIsNULLWhileTesting = true;
93
94   scoped_ptr<FontSettingsEventRouter> font_settings_event_router_;
95 };
96
97 // fontSettings.clearFont API function.
98 class FontSettingsClearFontFunction : public ChromeSyncExtensionFunction {
99  public:
100   DECLARE_EXTENSION_FUNCTION("fontSettings.clearFont", FONTSETTINGS_CLEARFONT)
101
102  protected:
103   // RefCounted types have non-public destructors, as with all extension
104   // functions in this file.
105   virtual ~FontSettingsClearFontFunction() {}
106
107   // ExtensionFunction:
108   virtual bool RunSync() OVERRIDE;
109 };
110
111 // fontSettings.getFont API function.
112 class FontSettingsGetFontFunction : public ChromeSyncExtensionFunction {
113  public:
114   DECLARE_EXTENSION_FUNCTION("fontSettings.getFont", FONTSETTINGS_GETFONT)
115
116  protected:
117   virtual ~FontSettingsGetFontFunction() {}
118
119   // ExtensionFunction:
120   virtual bool RunSync() OVERRIDE;
121 };
122
123 // fontSettings.setFont API function.
124 class FontSettingsSetFontFunction : public ChromeSyncExtensionFunction {
125  public:
126   DECLARE_EXTENSION_FUNCTION("fontSettings.setFont", FONTSETTINGS_SETFONT)
127
128  protected:
129   virtual ~FontSettingsSetFontFunction() {}
130
131   // ExtensionFunction:
132   virtual bool RunSync() OVERRIDE;
133 };
134
135 // fontSettings.getFontList API function.
136 class FontSettingsGetFontListFunction : public ChromeAsyncExtensionFunction {
137  public:
138   DECLARE_EXTENSION_FUNCTION("fontSettings.getFontList",
139                              FONTSETTINGS_GETFONTLIST)
140
141  protected:
142   virtual ~FontSettingsGetFontListFunction() {}
143
144   // ExtensionFunction:
145   virtual bool RunAsync() OVERRIDE;
146
147  private:
148   void FontListHasLoaded(scoped_ptr<base::ListValue> list);
149   bool CopyFontsToResult(base::ListValue* fonts);
150 };
151
152 // Base class for extension API functions that clear a browser font pref.
153 class ClearFontPrefExtensionFunction : public ChromeSyncExtensionFunction {
154  protected:
155   virtual ~ClearFontPrefExtensionFunction() {}
156
157   // ExtensionFunction:
158   virtual bool RunSync() OVERRIDE;
159
160   // Implementations should return the name of the preference to clear, like
161   // "webkit.webprefs.default_font_size".
162   virtual const char* GetPrefName() = 0;
163 };
164
165 // Base class for extension API functions that get a browser font pref.
166 class GetFontPrefExtensionFunction : public ChromeSyncExtensionFunction {
167  protected:
168   virtual ~GetFontPrefExtensionFunction() {}
169
170   // ExtensionFunction:
171   virtual bool RunSync() OVERRIDE;
172
173   // Implementations should return the name of the preference to get, like
174   // "webkit.webprefs.default_font_size".
175   virtual const char* GetPrefName() = 0;
176
177   // Implementations should return the key for the value in the extension API,
178   // like "pixelSize".
179   virtual const char* GetKey() = 0;
180 };
181
182 // Base class for extension API functions that set a browser font pref.
183 class SetFontPrefExtensionFunction : public ChromeSyncExtensionFunction {
184  protected:
185   virtual ~SetFontPrefExtensionFunction() {}
186
187   // ExtensionFunction:
188   virtual bool RunSync() OVERRIDE;
189
190   // Implementations should return the name of the preference to set, like
191   // "webkit.webprefs.default_font_size".
192   virtual const char* GetPrefName() = 0;
193
194   // Implementations should return the key for the value in the extension API,
195   // like "pixelSize".
196   virtual const char* GetKey() = 0;
197 };
198
199 // The following are get/set/clear API functions that act on a browser font
200 // pref.
201
202 class FontSettingsClearDefaultFontSizeFunction
203     : public ClearFontPrefExtensionFunction {
204  public:
205   DECLARE_EXTENSION_FUNCTION("fontSettings.clearDefaultFontSize",
206                              FONTSETTINGS_CLEARDEFAULTFONTSIZE)
207
208  protected:
209   virtual ~FontSettingsClearDefaultFontSizeFunction() {}
210
211   // ClearFontPrefExtensionFunction:
212   virtual const char* GetPrefName() OVERRIDE;
213 };
214
215 class FontSettingsGetDefaultFontSizeFunction
216     : public GetFontPrefExtensionFunction {
217  public:
218   DECLARE_EXTENSION_FUNCTION("fontSettings.getDefaultFontSize",
219                              FONTSETTINGS_GETDEFAULTFONTSIZE)
220
221  protected:
222   virtual ~FontSettingsGetDefaultFontSizeFunction() {}
223
224   // GetFontPrefExtensionFunction:
225   virtual const char* GetPrefName() OVERRIDE;
226   virtual const char* GetKey() OVERRIDE;
227 };
228
229 class FontSettingsSetDefaultFontSizeFunction
230     : public SetFontPrefExtensionFunction {
231  public:
232   DECLARE_EXTENSION_FUNCTION("fontSettings.setDefaultFontSize",
233                              FONTSETTINGS_SETDEFAULTFONTSIZE)
234
235  protected:
236   virtual ~FontSettingsSetDefaultFontSizeFunction() {}
237
238   // SetFontPrefExtensionFunction:
239   virtual const char* GetPrefName() OVERRIDE;
240   virtual const char* GetKey() OVERRIDE;
241 };
242
243 class FontSettingsClearDefaultFixedFontSizeFunction
244     : public ClearFontPrefExtensionFunction {
245  public:
246   DECLARE_EXTENSION_FUNCTION("fontSettings.clearDefaultFixedFontSize",
247                              FONTSETTINGS_CLEARDEFAULTFIXEDFONTSIZE)
248
249  protected:
250   virtual ~FontSettingsClearDefaultFixedFontSizeFunction() {}
251
252   // ClearFontPrefExtensionFunction:
253   virtual const char* GetPrefName() OVERRIDE;
254 };
255
256 class FontSettingsGetDefaultFixedFontSizeFunction
257     : public GetFontPrefExtensionFunction {
258  public:
259   DECLARE_EXTENSION_FUNCTION("fontSettings.getDefaultFixedFontSize",
260                              FONTSETTINGS_GETDEFAULTFIXEDFONTSIZE)
261
262  protected:
263   virtual ~FontSettingsGetDefaultFixedFontSizeFunction() {}
264
265   // GetFontPrefExtensionFunction:
266   virtual const char* GetPrefName() OVERRIDE;
267   virtual const char* GetKey() OVERRIDE;
268 };
269
270 class FontSettingsSetDefaultFixedFontSizeFunction
271     : public SetFontPrefExtensionFunction {
272  public:
273   DECLARE_EXTENSION_FUNCTION("fontSettings.setDefaultFixedFontSize",
274                              FONTSETTINGS_SETDEFAULTFIXEDFONTSIZE)
275
276  protected:
277   virtual ~FontSettingsSetDefaultFixedFontSizeFunction() {}
278
279   // SetFontPrefExtensionFunction:
280   virtual const char* GetPrefName() OVERRIDE;
281   virtual const char* GetKey() OVERRIDE;
282 };
283
284 class FontSettingsClearMinimumFontSizeFunction
285     : public ClearFontPrefExtensionFunction {
286  public:
287   DECLARE_EXTENSION_FUNCTION("fontSettings.clearMinimumFontSize",
288                              FONTSETTINGS_CLEARMINIMUMFONTSIZE)
289
290  protected:
291   virtual ~FontSettingsClearMinimumFontSizeFunction() {}
292
293   // ClearFontPrefExtensionFunction:
294   virtual const char* GetPrefName() OVERRIDE;
295 };
296
297 class FontSettingsGetMinimumFontSizeFunction
298     : public GetFontPrefExtensionFunction {
299  public:
300   DECLARE_EXTENSION_FUNCTION("fontSettings.getMinimumFontSize",
301                              FONTSETTINGS_GETMINIMUMFONTSIZE)
302
303  protected:
304   virtual ~FontSettingsGetMinimumFontSizeFunction() {}
305
306   // GetFontPrefExtensionFunction:
307   virtual const char* GetPrefName() OVERRIDE;
308   virtual const char* GetKey() OVERRIDE;
309 };
310
311 class FontSettingsSetMinimumFontSizeFunction
312     : public SetFontPrefExtensionFunction {
313  public:
314   DECLARE_EXTENSION_FUNCTION("fontSettings.setMinimumFontSize",
315                              FONTSETTINGS_SETMINIMUMFONTSIZE)
316
317  protected:
318   virtual ~FontSettingsSetMinimumFontSizeFunction() {}
319
320   // SetFontPrefExtensionFunction:
321   virtual const char* GetPrefName() OVERRIDE;
322   virtual const char* GetKey() OVERRIDE;
323 };
324
325 }  // namespace extensions
326
327 #endif  // CHROME_BROWSER_EXTENSIONS_API_FONT_SETTINGS_FONT_SETTINGS_API_H_