OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_font_settings_api.h" | 5 #include "chrome/browser/extensions/extension_font_settings_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
22 #include "content/public/browser/notification_details.h" | 22 #include "content/public/browser/notification_details.h" |
23 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
24 | 24 |
25 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
26 #include "ui/gfx/font.h" | 26 #include "ui/gfx/font.h" |
27 #include "ui/gfx/platform_font_win.h" | 27 #include "ui/gfx/platform_font_win.h" |
28 #endif | 28 #endif |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 const char kCharsetKey[] = "charset"; | |
33 const char kFontNameKey[] = "fontName"; | |
32 const char kGenericFamilyKey[] = "genericFamily"; | 34 const char kGenericFamilyKey[] = "genericFamily"; |
33 const char kFontNameKey[] = "fontName"; | |
34 const char kLocalizedNameKey[] = "localizedName"; | 35 const char kLocalizedNameKey[] = "localizedName"; |
35 const char kPixelSizeKey[] = "pixelSize"; | 36 const char kPixelSizeKey[] = "pixelSize"; |
36 const char kScriptKey[] = "script"; | 37 const char kScriptKey[] = "script"; |
37 | 38 |
39 const char kOnDefaultCharacterSetChanged[] = | |
40 "experimental.fontSettings.onDefaultCharacterSetChanged"; | |
38 const char kOnDefaultFixedFontSizeChanged[] = | 41 const char kOnDefaultFixedFontSizeChanged[] = |
39 "experimental.fontSettings.onDefaultFixedFontSizeChanged"; | 42 "experimental.fontSettings.onDefaultFixedFontSizeChanged"; |
40 const char kOnDefaultFontSizeChanged[] = | 43 const char kOnDefaultFontSizeChanged[] = |
41 "experimental.fontSettings.onDefaultFontSizeChanged"; | 44 "experimental.fontSettings.onDefaultFontSizeChanged"; |
42 const char kOnFontChanged[] = "experimental.fontSettings.onFontChanged"; | 45 const char kOnFontChanged[] = "experimental.fontSettings.onFontChanged"; |
43 const char kOnMinimumFontSizeChanged[] = | 46 const char kOnMinimumFontSizeChanged[] = |
44 "experimental.fontSettings.onMinimumFontSizeChanged"; | 47 "experimental.fontSettings.onMinimumFontSizeChanged"; |
45 | 48 |
46 // Format for per-script font preference keys. | 49 // Format for per-script font preference keys. |
47 // E.g., "webkit.webprefs.fonts.standard.Hrkt" | 50 // E.g., "webkit.webprefs.fonts.standard.Hrkt" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 ExtensionFontSettingsEventRouter::ExtensionFontSettingsEventRouter( | 138 ExtensionFontSettingsEventRouter::ExtensionFontSettingsEventRouter( |
136 Profile* profile) : profile_(profile) {} | 139 Profile* profile) : profile_(profile) {} |
137 | 140 |
138 ExtensionFontSettingsEventRouter::~ExtensionFontSettingsEventRouter() {} | 141 ExtensionFontSettingsEventRouter::~ExtensionFontSettingsEventRouter() {} |
139 | 142 |
140 void ExtensionFontSettingsEventRouter::Init() { | 143 void ExtensionFontSettingsEventRouter::Init() { |
141 registrar_.Init(profile_->GetPrefs()); | 144 registrar_.Init(profile_->GetPrefs()); |
142 | 145 |
143 registrar_.Add(prefs::kWebKitGlobalDefaultFixedFontSize, this); | 146 registrar_.Add(prefs::kWebKitGlobalDefaultFixedFontSize, this); |
144 pref_event_map_[prefs::kWebKitGlobalDefaultFixedFontSize] = | 147 pref_event_map_[prefs::kWebKitGlobalDefaultFixedFontSize] = |
145 kOnDefaultFixedFontSizeChanged; | 148 std::make_pair(kOnDefaultFixedFontSizeChanged, kPixelSizeKey); |
146 | 149 |
147 registrar_.Add(prefs::kWebKitGlobalDefaultFontSize, this); | 150 registrar_.Add(prefs::kWebKitGlobalDefaultFontSize, this); |
148 pref_event_map_[prefs::kWebKitGlobalDefaultFontSize] = | 151 pref_event_map_[prefs::kWebKitGlobalDefaultFontSize] = |
149 kOnDefaultFontSizeChanged; | 152 std::make_pair(kOnDefaultFontSizeChanged, kPixelSizeKey); |
150 | 153 |
151 registrar_.Add(prefs::kWebKitGlobalMinimumFontSize, this); | 154 registrar_.Add(prefs::kWebKitGlobalMinimumFontSize, this); |
152 pref_event_map_[prefs::kWebKitGlobalMinimumFontSize] = | 155 pref_event_map_[prefs::kWebKitGlobalMinimumFontSize] = |
153 kOnMinimumFontSizeChanged; | 156 std::make_pair(kOnMinimumFontSizeChanged, kPixelSizeKey); |
157 | |
158 registrar_.Add(prefs::kGlobalDefaultCharset, this); | |
159 pref_event_map_[prefs::kGlobalDefaultCharset] = | |
160 std::make_pair(kOnDefaultCharacterSetChanged, kCharsetKey); | |
Matt Perry
2012/04/20 18:59:22
If there will be many more of these, I take back w
falken
2012/04/23 03:41:44
Done. I added a helper function.
| |
154 | 161 |
155 registrar_.Add(prefs::kWebKitGlobalStandardFontFamily, this); | 162 registrar_.Add(prefs::kWebKitGlobalStandardFontFamily, this); |
156 registrar_.Add(prefs::kWebKitGlobalSerifFontFamily, this); | 163 registrar_.Add(prefs::kWebKitGlobalSerifFontFamily, this); |
157 registrar_.Add(prefs::kWebKitGlobalSansSerifFontFamily, this); | 164 registrar_.Add(prefs::kWebKitGlobalSansSerifFontFamily, this); |
158 registrar_.Add(prefs::kWebKitGlobalFixedFontFamily, this); | 165 registrar_.Add(prefs::kWebKitGlobalFixedFontFamily, this); |
159 registrar_.Add(prefs::kWebKitGlobalCursiveFontFamily, this); | 166 registrar_.Add(prefs::kWebKitGlobalCursiveFontFamily, this); |
160 registrar_.Add(prefs::kWebKitGlobalFantasyFontFamily, this); | 167 registrar_.Add(prefs::kWebKitGlobalFantasyFontFamily, this); |
161 RegisterFontFamilyMapObserver(®istrar_, | 168 RegisterFontFamilyMapObserver(®istrar_, |
162 prefs::kWebKitStandardFontFamilyMap, this); | 169 prefs::kWebKitStandardFontFamilyMap, this); |
163 RegisterFontFamilyMapObserver(®istrar_, | 170 RegisterFontFamilyMapObserver(®istrar_, |
(...skipping 14 matching lines...) Expand all Loading... | |
178 const content::NotificationDetails& details) { | 185 const content::NotificationDetails& details) { |
179 if (type != chrome::NOTIFICATION_PREF_CHANGED) { | 186 if (type != chrome::NOTIFICATION_PREF_CHANGED) { |
180 NOTREACHED(); | 187 NOTREACHED(); |
181 return; | 188 return; |
182 } | 189 } |
183 | 190 |
184 PrefService* pref_service = content::Source<PrefService>(source).ptr(); | 191 PrefService* pref_service = content::Source<PrefService>(source).ptr(); |
185 bool incognito = (pref_service != profile_->GetPrefs()); | 192 bool incognito = (pref_service != profile_->GetPrefs()); |
186 // We're only observing pref changes on the regular profile. | 193 // We're only observing pref changes on the regular profile. |
187 DCHECK(!incognito); | 194 DCHECK(!incognito); |
188 const std::string* pref_key = | 195 const std::string* pref_name = |
189 content::Details<const std::string>(details).ptr(); | 196 content::Details<const std::string>(details).ptr(); |
190 | 197 |
191 PrefEventMap::iterator iter = pref_event_map_.find(*pref_key); | 198 PrefEventMap::iterator iter = pref_event_map_.find(*pref_name); |
192 if (iter != pref_event_map_.end()) { | 199 if (iter != pref_event_map_.end()) { |
193 OnFontSizePrefChanged(pref_service, *pref_key, iter->second, incognito); | 200 const std::string& event_name = iter->second.first; |
201 const std::string& key = iter->second.second; | |
202 OnFontPrefChanged(pref_service, *pref_name, event_name, key, incognito); | |
194 return; | 203 return; |
195 } | 204 } |
196 | 205 |
197 std::string generic_family; | 206 std::string generic_family; |
198 std::string script; | 207 std::string script; |
199 if (ParseFontNamePrefPath(*pref_key, &generic_family, &script)) { | 208 if (ParseFontNamePrefPath(*pref_name, &generic_family, &script)) { |
200 OnFontNamePrefChanged(pref_service, *pref_key, generic_family, script, | 209 OnFontNamePrefChanged(pref_service, *pref_name, generic_family, script, |
201 incognito); | 210 incognito); |
202 return; | 211 return; |
203 } | 212 } |
204 | 213 |
205 NOTREACHED(); | 214 NOTREACHED(); |
206 } | 215 } |
207 | 216 |
208 void ExtensionFontSettingsEventRouter::OnFontNamePrefChanged( | 217 void ExtensionFontSettingsEventRouter::OnFontNamePrefChanged( |
209 PrefService* pref_service, | 218 PrefService* pref_service, |
210 const std::string& pref_key, | 219 const std::string& pref_name, |
211 const std::string& generic_family, | 220 const std::string& generic_family, |
212 const std::string& script, | 221 const std::string& script, |
213 bool incognito) { | 222 bool incognito) { |
214 const PrefService::Preference* pref = pref_service->FindPreference( | 223 const PrefService::Preference* pref = pref_service->FindPreference( |
215 pref_key.c_str()); | 224 pref_name.c_str()); |
216 CHECK(pref); | 225 CHECK(pref); |
217 | 226 |
218 std::string font_name; | 227 std::string font_name; |
219 if (!pref->GetValue()->GetAsString(&font_name)) { | 228 if (!pref->GetValue()->GetAsString(&font_name)) { |
220 NOTREACHED(); | 229 NOTREACHED(); |
221 return; | 230 return; |
222 } | 231 } |
223 font_name = MaybeGetLocalizedFontName(font_name); | 232 font_name = MaybeGetLocalizedFontName(font_name); |
224 | 233 |
225 ListValue args; | 234 ListValue args; |
226 DictionaryValue* dict = new DictionaryValue(); | 235 DictionaryValue* dict = new DictionaryValue(); |
227 args.Append(dict); | 236 args.Append(dict); |
228 dict->SetString(kFontNameKey, font_name); | 237 dict->SetString(kFontNameKey, font_name); |
229 dict->SetString(kGenericFamilyKey, generic_family); | 238 dict->SetString(kGenericFamilyKey, generic_family); |
230 if (!script.empty()) | 239 if (!script.empty()) |
231 dict->SetString(kScriptKey, script); | 240 dict->SetString(kScriptKey, script); |
232 | 241 |
233 extension_preference_helpers::DispatchEventToExtensions( | 242 extension_preference_helpers::DispatchEventToExtensions( |
234 profile_, | 243 profile_, |
235 kOnFontChanged, | 244 kOnFontChanged, |
236 &args, | 245 &args, |
237 ExtensionAPIPermission::kExperimental, | 246 ExtensionAPIPermission::kExperimental, |
238 incognito, | 247 incognito, |
239 pref_key); | 248 pref_name); |
240 } | 249 } |
241 | 250 |
242 void ExtensionFontSettingsEventRouter::OnFontSizePrefChanged( | 251 void ExtensionFontSettingsEventRouter::OnFontPrefChanged( |
243 PrefService* pref_service, | 252 PrefService* pref_service, |
244 const std::string& pref_key, | 253 const std::string& pref_name, |
245 const std::string& event_name, | 254 const std::string& event_name, |
255 const std::string& key, | |
246 bool incognito) { | 256 bool incognito) { |
247 const PrefService::Preference* pref = pref_service->FindPreference( | 257 const PrefService::Preference* pref = pref_service->FindPreference( |
248 pref_key.c_str()); | 258 pref_name.c_str()); |
249 CHECK(pref); | 259 CHECK(pref); |
250 | 260 |
251 int size; | |
252 if (!pref->GetValue()->GetAsInteger(&size)) { | |
253 NOTREACHED(); | |
254 return; | |
255 } | |
256 | |
257 ListValue args; | 261 ListValue args; |
258 DictionaryValue* dict = new DictionaryValue(); | 262 DictionaryValue* dict = new DictionaryValue(); |
259 args.Append(dict); | 263 args.Append(dict); |
260 dict->SetInteger(kPixelSizeKey, size); | 264 dict->Set(key, pref->GetValue()->DeepCopy()); |
261 | 265 |
262 extension_preference_helpers::DispatchEventToExtensions( | 266 extension_preference_helpers::DispatchEventToExtensions( |
263 profile_, | 267 profile_, |
264 event_name, | 268 event_name, |
265 &args, | 269 &args, |
266 ExtensionAPIPermission::kExperimental, | 270 ExtensionAPIPermission::kExperimental, |
267 incognito, | 271 incognito, |
268 pref_key); | 272 pref_name); |
269 } | 273 } |
270 | 274 |
271 bool GetFontFunction::RunImpl() { | 275 bool GetFontFunction::RunImpl() { |
272 DictionaryValue* details = NULL; | 276 DictionaryValue* details = NULL; |
273 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | 277 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); |
274 | 278 |
275 std::string pref_path; | 279 std::string pref_path; |
276 EXTENSION_FUNCTION_VALIDATE(GetFontNamePrefPath(details, &pref_path)); | 280 EXTENSION_FUNCTION_VALIDATE(GetFontNamePrefPath(details, &pref_path)); |
277 | 281 |
278 PrefService* prefs = profile_->GetPrefs(); | 282 PrefService* prefs = profile_->GetPrefs(); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
346 DictionaryValue* font_name = new DictionaryValue(); | 350 DictionaryValue* font_name = new DictionaryValue(); |
347 font_name->Set(kFontNameKey, Value::CreateStringValue(name)); | 351 font_name->Set(kFontNameKey, Value::CreateStringValue(name)); |
348 font_name->Set(kLocalizedNameKey, Value::CreateStringValue(localized_name)); | 352 font_name->Set(kLocalizedNameKey, Value::CreateStringValue(localized_name)); |
349 result->Append(font_name); | 353 result->Append(font_name); |
350 } | 354 } |
351 | 355 |
352 result_.reset(result.release()); | 356 result_.reset(result.release()); |
353 return true; | 357 return true; |
354 } | 358 } |
355 | 359 |
356 bool GetFontSizeExtensionFunction::RunImpl() { | 360 bool GetFontPrefExtensionFunction::RunImpl() { |
357 PrefService* prefs = profile_->GetPrefs(); | 361 PrefService* prefs = profile_->GetPrefs(); |
358 int size = prefs->GetInteger(GetPrefName()); | 362 const PrefService::Preference* pref = prefs->FindPreference(GetPrefName()); |
363 EXTENSION_FUNCTION_VALIDATE(pref); | |
359 | 364 |
360 DictionaryValue* result = new DictionaryValue(); | 365 DictionaryValue* result = new DictionaryValue(); |
361 result->SetInteger(kPixelSizeKey, size); | 366 result->Set(GetKey(), pref->GetValue()->DeepCopy()); |
362 result_.reset(result); | 367 result_.reset(result); |
363 return true; | 368 return true; |
364 } | 369 } |
365 | 370 |
366 bool SetFontSizeExtensionFunction::RunImpl() { | 371 bool SetFontPrefExtensionFunction::RunImpl() { |
367 DictionaryValue* details = NULL; | 372 DictionaryValue* details = NULL; |
368 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | 373 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); |
369 | 374 |
370 int size; | 375 Value* value; |
371 EXTENSION_FUNCTION_VALIDATE(details->GetInteger(kPixelSizeKey, &size)); | 376 EXTENSION_FUNCTION_VALIDATE(details->Get(GetKey(), &value)); |
372 | 377 |
373 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); | 378 ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs(); |
374 prefs->SetExtensionControlledPref(extension_id(), | 379 prefs->SetExtensionControlledPref(extension_id(), |
375 GetPrefName(), | 380 GetPrefName(), |
376 kExtensionPrefsScopeRegular, | 381 kExtensionPrefsScopeRegular, |
377 Value::CreateIntegerValue(size)); | 382 value->DeepCopy()); |
378 return true; | 383 return true; |
379 } | 384 } |
380 | 385 |
381 const char* GetDefaultFontSizeFunction::GetPrefName() { | 386 const char* GetDefaultFontSizeFunction::GetPrefName() { |
382 return prefs::kWebKitGlobalDefaultFontSize; | 387 return prefs::kWebKitGlobalDefaultFontSize; |
383 } | 388 } |
384 | 389 |
390 const char* GetDefaultFontSizeFunction::GetKey() { | |
391 return kPixelSizeKey; | |
392 } | |
393 | |
385 const char* SetDefaultFontSizeFunction::GetPrefName() { | 394 const char* SetDefaultFontSizeFunction::GetPrefName() { |
386 return prefs::kWebKitGlobalDefaultFontSize; | 395 return prefs::kWebKitGlobalDefaultFontSize; |
387 } | 396 } |
388 | 397 |
398 const char* SetDefaultFontSizeFunction::GetKey() { | |
399 return kPixelSizeKey; | |
400 } | |
401 | |
389 const char* GetDefaultFixedFontSizeFunction::GetPrefName() { | 402 const char* GetDefaultFixedFontSizeFunction::GetPrefName() { |
390 return prefs::kWebKitGlobalDefaultFixedFontSize; | 403 return prefs::kWebKitGlobalDefaultFixedFontSize; |
391 } | 404 } |
392 | 405 |
406 const char* GetDefaultFixedFontSizeFunction::GetKey() { | |
407 return kPixelSizeKey; | |
408 } | |
409 | |
393 const char* SetDefaultFixedFontSizeFunction::GetPrefName() { | 410 const char* SetDefaultFixedFontSizeFunction::GetPrefName() { |
394 return prefs::kWebKitGlobalDefaultFixedFontSize; | 411 return prefs::kWebKitGlobalDefaultFixedFontSize; |
395 } | 412 } |
396 | 413 |
414 const char* SetDefaultFixedFontSizeFunction::GetKey() { | |
415 return kPixelSizeKey; | |
416 } | |
417 | |
397 const char* GetMinimumFontSizeFunction::GetPrefName() { | 418 const char* GetMinimumFontSizeFunction::GetPrefName() { |
398 return prefs::kWebKitGlobalMinimumFontSize; | 419 return prefs::kWebKitGlobalMinimumFontSize; |
399 } | 420 } |
400 | 421 |
422 const char* GetMinimumFontSizeFunction::GetKey() { | |
423 return kPixelSizeKey; | |
424 } | |
425 | |
401 const char* SetMinimumFontSizeFunction::GetPrefName() { | 426 const char* SetMinimumFontSizeFunction::GetPrefName() { |
402 return prefs::kWebKitGlobalMinimumFontSize; | 427 return prefs::kWebKitGlobalMinimumFontSize; |
403 } | 428 } |
429 | |
430 const char* SetMinimumFontSizeFunction::GetKey() { | |
431 return kPixelSizeKey; | |
432 } | |
433 | |
434 const char* GetDefaultCharacterSetFunction::GetPrefName() { | |
435 return prefs::kGlobalDefaultCharset; | |
436 } | |
437 | |
438 const char* GetDefaultCharacterSetFunction::GetKey() { | |
439 return kCharsetKey; | |
440 } | |
441 | |
442 const char* SetDefaultCharacterSetFunction::GetPrefName() { | |
443 return prefs::kGlobalDefaultCharset; | |
444 } | |
445 | |
446 const char* SetDefaultCharacterSetFunction::GetKey() { | |
447 return kCharsetKey; | |
448 } | |
OLD | NEW |