OLD | NEW |
| (Empty) |
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_EXTENSION_FONT_SETTINGS_API_H__ | |
9 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FONT_SETTINGS_API_H__ | |
10 | |
11 #include <map> | |
12 #include <string> | |
13 #include <utility> | |
14 | |
15 #include "chrome/browser/extensions/extension_function.h" | |
16 #include "chrome/browser/prefs/pref_change_registrar.h" | |
17 | |
18 // This class observes pref changed events on a profile and dispatches the | |
19 // corresponding extension API events to extensions. | |
20 class ExtensionFontSettingsEventRouter : public content::NotificationObserver { | |
21 public: | |
22 // Constructor for observing pref changed events on |profile|. Stores a | |
23 // pointer to |profile| but does not take ownership. |profile| must be | |
24 // non-NULL and remain alive for the lifetime of the instance. Init() must be | |
25 // called to start observing pref changed events. | |
26 explicit ExtensionFontSettingsEventRouter(Profile* profile); | |
27 virtual ~ExtensionFontSettingsEventRouter(); | |
28 | |
29 // Starts observing pref changed events on the profile. Must not be called | |
30 // more than once. | |
31 void Init(); | |
32 | |
33 private: | |
34 typedef std::pair<std::string, std::string> EventAndKeyPair; | |
35 // Map of pref name to a pair of event name and key (defined in the extension | |
36 // API) for dispatching a pref changed event. For example, | |
37 // "webkit.webprefs.default_font_size" to ("onDefaultFontSizedChanged", | |
38 // "pixelSize"). | |
39 typedef std::map<std::string, EventAndKeyPair> PrefEventMap; | |
40 | |
41 // Observes browser pref |pref_name|. When a change is observed, dispatches | |
42 // event |event_name| to extensions. A JavaScript object is passed to the | |
43 // extension event function with the new value of the pref in property |key|. | |
44 void AddPrefToObserve(const char* pref_name, | |
45 const char* event_name, | |
46 const char* key); | |
47 | |
48 // content::NotificationObserver implementation. | |
49 virtual void Observe(int type, | |
50 const content::NotificationSource& source, | |
51 const content::NotificationDetails& details) OVERRIDE; | |
52 | |
53 // Dispatches a changed event for the font setting for |generic_family| and | |
54 // |script| to extensions. The new value of the setting is the value of | |
55 // browser pref |pref_name|. If the pref changed on the incognito profile, | |
56 // |incognito| must be set to true for extensions to get the appropriate | |
57 // event. | |
58 void OnFontNamePrefChanged(PrefService* pref_service, | |
59 const std::string& pref_name, | |
60 const std::string& generic_family, | |
61 const std::string& script, | |
62 bool incognito); | |
63 | |
64 // Dispatches the setting changed event |event_name| to extensions. The new | |
65 // value of the setting is the value of browser pref |pref_name|. This value | |
66 // is passed in the JavaScript object argument to the extension event function | |
67 // under the key |key|. If the pref changed on the incognito profile, | |
68 // |incognito| must be set to true for extensions to get the appropriate | |
69 // event. | |
70 void OnFontPrefChanged(PrefService* pref_service, | |
71 const std::string& pref_name, | |
72 const std::string& event_name, | |
73 const std::string& key, | |
74 bool incognito); | |
75 | |
76 // Manages pref observation registration. | |
77 PrefChangeRegistrar registrar_; | |
78 | |
79 // Maps browser pref names to extension API <event name, key> pairs. | |
80 PrefEventMap pref_event_map_; | |
81 | |
82 // Weak, owns us (transitively via ExtensionService). | |
83 Profile* profile_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(ExtensionFontSettingsEventRouter); | |
86 }; | |
87 | |
88 // fontSettings.clearFont API function. | |
89 class ClearFontFunction : public SyncExtensionFunction { | |
90 public: | |
91 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.clearFont") | |
92 | |
93 protected: | |
94 // RefCounted types have non-public destructors, as with all extension | |
95 // functions in this file. | |
96 virtual ~ClearFontFunction() {} | |
97 | |
98 // ExtensionFunction: | |
99 virtual bool RunImpl() OVERRIDE; | |
100 }; | |
101 | |
102 // fontSettings.getFont API function. | |
103 class GetFontFunction : public SyncExtensionFunction { | |
104 public: | |
105 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.getFont") | |
106 | |
107 protected: | |
108 virtual ~GetFontFunction() {} | |
109 | |
110 // ExtensionFunction: | |
111 virtual bool RunImpl() OVERRIDE; | |
112 }; | |
113 | |
114 // fontSettings.setFont API function. | |
115 class SetFontFunction : public SyncExtensionFunction { | |
116 public: | |
117 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.setFont") | |
118 | |
119 protected: | |
120 virtual ~SetFontFunction() {} | |
121 | |
122 // ExtensionFunction: | |
123 virtual bool RunImpl() OVERRIDE; | |
124 }; | |
125 | |
126 // fontSettings.getFontList API function. | |
127 class GetFontListFunction : public AsyncExtensionFunction { | |
128 public: | |
129 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.getFontList") | |
130 | |
131 protected: | |
132 virtual ~GetFontListFunction() {} | |
133 | |
134 // ExtensionFunction: | |
135 virtual bool RunImpl() OVERRIDE; | |
136 | |
137 private: | |
138 void FontListHasLoaded(scoped_ptr<base::ListValue> list); | |
139 bool CopyFontsToResult(base::ListValue* fonts); | |
140 }; | |
141 | |
142 // Base class for extension API functions that clear a browser font pref. | |
143 class ClearFontPrefExtensionFunction : public SyncExtensionFunction { | |
144 protected: | |
145 virtual ~ClearFontPrefExtensionFunction() {} | |
146 | |
147 // ExtensionFunction: | |
148 virtual bool RunImpl() OVERRIDE; | |
149 | |
150 // Implementations should return the name of the preference to clear, like | |
151 // "webkit.webprefs.default_font_size". | |
152 virtual const char* GetPrefName() = 0; | |
153 }; | |
154 | |
155 // Base class for extension API functions that get a browser font pref. | |
156 class GetFontPrefExtensionFunction : public SyncExtensionFunction { | |
157 protected: | |
158 virtual ~GetFontPrefExtensionFunction() {} | |
159 | |
160 // ExtensionFunction: | |
161 virtual bool RunImpl() OVERRIDE; | |
162 | |
163 // Implementations should return the name of the preference to get, like | |
164 // "webkit.webprefs.default_font_size". | |
165 virtual const char* GetPrefName() = 0; | |
166 | |
167 // Implementations should return the key for the value in the extension API, | |
168 // like "pixelSize". | |
169 virtual const char* GetKey() = 0; | |
170 }; | |
171 | |
172 // Base class for extension API functions that set a browser font pref. | |
173 class SetFontPrefExtensionFunction : public SyncExtensionFunction { | |
174 protected: | |
175 virtual ~SetFontPrefExtensionFunction() {} | |
176 | |
177 // ExtensionFunction: | |
178 virtual bool RunImpl() OVERRIDE; | |
179 | |
180 // Implementations should return the name of the preference to set, like | |
181 // "webkit.webprefs.default_font_size". | |
182 virtual const char* GetPrefName() = 0; | |
183 | |
184 // Implementations should return the key for the value in the extension API, | |
185 // like "pixelSize". | |
186 virtual const char* GetKey() = 0; | |
187 }; | |
188 | |
189 // The following are get/set/clear API functions that act on a browser font | |
190 // pref. | |
191 | |
192 class ClearDefaultFontSizeFunction : public ClearFontPrefExtensionFunction { | |
193 public: | |
194 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.clearDefaultFontSize") | |
195 | |
196 protected: | |
197 virtual ~ClearDefaultFontSizeFunction() {} | |
198 | |
199 // ClearFontPrefExtensionFunction: | |
200 virtual const char* GetPrefName() OVERRIDE; | |
201 }; | |
202 | |
203 class GetDefaultFontSizeFunction : public GetFontPrefExtensionFunction { | |
204 public: | |
205 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.getDefaultFontSize") | |
206 | |
207 protected: | |
208 virtual ~GetDefaultFontSizeFunction() {} | |
209 | |
210 // GetFontPrefExtensionFunction: | |
211 virtual const char* GetPrefName() OVERRIDE; | |
212 virtual const char* GetKey() OVERRIDE; | |
213 }; | |
214 | |
215 class SetDefaultFontSizeFunction : public SetFontPrefExtensionFunction { | |
216 public: | |
217 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.setDefaultFontSize") | |
218 | |
219 protected: | |
220 virtual ~SetDefaultFontSizeFunction() {} | |
221 | |
222 // SetFontPrefExtensionFunction: | |
223 virtual const char* GetPrefName() OVERRIDE; | |
224 virtual const char* GetKey() OVERRIDE; | |
225 }; | |
226 | |
227 class ClearDefaultFixedFontSizeFunction | |
228 : public ClearFontPrefExtensionFunction { | |
229 public: | |
230 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.clearDefaultFixedFontSize") | |
231 | |
232 protected: | |
233 virtual ~ClearDefaultFixedFontSizeFunction() {} | |
234 | |
235 // ClearFontPrefExtensionFunction: | |
236 virtual const char* GetPrefName() OVERRIDE; | |
237 }; | |
238 | |
239 class GetDefaultFixedFontSizeFunction : public GetFontPrefExtensionFunction { | |
240 public: | |
241 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.getDefaultFixedFontSize") | |
242 | |
243 protected: | |
244 virtual ~GetDefaultFixedFontSizeFunction() {} | |
245 | |
246 // GetFontPrefExtensionFunction: | |
247 virtual const char* GetPrefName() OVERRIDE; | |
248 virtual const char* GetKey() OVERRIDE; | |
249 }; | |
250 | |
251 class SetDefaultFixedFontSizeFunction : public SetFontPrefExtensionFunction { | |
252 public: | |
253 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.setDefaultFixedFontSize") | |
254 | |
255 protected: | |
256 virtual ~SetDefaultFixedFontSizeFunction() {} | |
257 | |
258 // SetFontPrefExtensionFunction: | |
259 virtual const char* GetPrefName() OVERRIDE; | |
260 virtual const char* GetKey() OVERRIDE; | |
261 }; | |
262 | |
263 class ClearMinimumFontSizeFunction : public ClearFontPrefExtensionFunction { | |
264 public: | |
265 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.clearMinimumFontSize") | |
266 | |
267 protected: | |
268 virtual ~ClearMinimumFontSizeFunction() {} | |
269 | |
270 // ClearFontPrefExtensionFunction: | |
271 virtual const char* GetPrefName() OVERRIDE; | |
272 }; | |
273 | |
274 class GetMinimumFontSizeFunction : public GetFontPrefExtensionFunction { | |
275 public: | |
276 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.getMinimumFontSize") | |
277 | |
278 protected: | |
279 virtual ~GetMinimumFontSizeFunction() {} | |
280 | |
281 // GetFontPrefExtensionFunction: | |
282 virtual const char* GetPrefName() OVERRIDE; | |
283 virtual const char* GetKey() OVERRIDE; | |
284 }; | |
285 | |
286 class SetMinimumFontSizeFunction : public SetFontPrefExtensionFunction { | |
287 public: | |
288 DECLARE_EXTENSION_FUNCTION_NAME("fontSettings.setMinimumFontSize") | |
289 | |
290 protected: | |
291 virtual ~SetMinimumFontSizeFunction() {} | |
292 | |
293 // SetFontPrefExtensionFunction: | |
294 virtual const char* GetPrefName() OVERRIDE; | |
295 virtual const char* GetKey() OVERRIDE; | |
296 }; | |
297 | |
298 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FONT_SETTINGS_API_H__ | |
OLD | NEW |