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/ui/prefs/prefs_tab_helper.h" | 5 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/prefs/overlay_user_pref_store.h" | 9 #include "base/prefs/overlay_user_pref_store.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/renderer_preferences_util.h" | 16 #include "chrome/browser/renderer_preferences_util.h" |
16 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 19 #include "chrome/common/pref_names_util.h" |
18 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
19 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
20 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
21 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
22 #include "grit/locale_settings.h" | 24 #include "grit/locale_settings.h" |
23 #include "grit/platform_locale_settings.h" | 25 #include "grit/platform_locale_settings.h" |
24 #include "unicode/uchar.h" | 26 #include "unicode/uchar.h" |
25 #include "unicode/uscript.h" | 27 #include "unicode/uscript.h" |
26 #include "webkit/glue/webpreferences.h" | 28 #include "webkit/glue/webpreferences.h" |
27 | 29 |
28 #if defined(OS_POSIX) && !defined(OS_MACOSX) && defined(ENABLE_THEMES) | 30 #if defined(OS_POSIX) && !defined(OS_MACOSX) && defined(ENABLE_THEMES) |
29 #include "chrome/browser/themes/theme_service.h" | 31 #include "chrome/browser/themes/theme_service.h" |
30 #include "chrome/browser/themes/theme_service_factory.h" | 32 #include "chrome/browser/themes/theme_service_factory.h" |
31 #endif | 33 #endif |
32 | 34 |
33 using content::WebContents; | 35 using content::WebContents; |
34 using webkit_glue::WebPreferences; | 36 using webkit_glue::WebPreferences; |
35 | 37 |
36 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PrefsTabHelper) | 38 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PrefsTabHelper) |
37 | 39 |
38 namespace { | 40 namespace { |
39 | 41 |
40 // Registers prefs only used for migration. | 42 // Registers prefs only used for migration. |
41 static void RegisterPrefsToMigrate(PrefService* prefs) { | 43 void RegisterPrefsToMigrate(PrefService* prefs) { |
42 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldStandardFontFamily, | 44 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldStandardFontFamily, |
43 IDS_STANDARD_FONT_FAMILY, | 45 IDS_STANDARD_FONT_FAMILY, |
44 PrefService::UNSYNCABLE_PREF); | 46 PrefService::UNSYNCABLE_PREF); |
45 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldFixedFontFamily, | 47 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldFixedFontFamily, |
46 IDS_FIXED_FONT_FAMILY, | 48 IDS_FIXED_FONT_FAMILY, |
47 PrefService::UNSYNCABLE_PREF); | 49 PrefService::UNSYNCABLE_PREF); |
48 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldSerifFontFamily, | 50 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldSerifFontFamily, |
49 IDS_SERIF_FONT_FAMILY, | 51 IDS_SERIF_FONT_FAMILY, |
50 PrefService::UNSYNCABLE_PREF); | 52 PrefService::UNSYNCABLE_PREF); |
51 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldSansSerifFontFamily, | 53 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldSansSerifFontFamily, |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 { prefs::kWebKitGlobalSansSerifFontFamily, | 327 { prefs::kWebKitGlobalSansSerifFontFamily, |
326 prefs::kWebKitSansSerifFontFamily }, | 328 prefs::kWebKitSansSerifFontFamily }, |
327 { prefs::kWebKitGlobalSerifFontFamily, | 329 { prefs::kWebKitGlobalSerifFontFamily, |
328 prefs::kWebKitSerifFontFamily }, | 330 prefs::kWebKitSerifFontFamily }, |
329 { prefs::kWebKitGlobalStandardFontFamily, | 331 { prefs::kWebKitGlobalStandardFontFamily, |
330 prefs::kWebKitStandardFontFamily } | 332 prefs::kWebKitStandardFontFamily } |
331 }; | 333 }; |
332 | 334 |
333 const int kPrefsToMigrateLength = ARRAYSIZE_UNSAFE(kPrefNamesToMigrate); | 335 const int kPrefsToMigrateLength = ARRAYSIZE_UNSAFE(kPrefNamesToMigrate); |
334 | 336 |
335 static void MigratePreferences(PrefService* prefs) { | 337 void MigratePreferences(PrefService* prefs) { |
336 RegisterPrefsToMigrate(prefs); | 338 RegisterPrefsToMigrate(prefs); |
337 for (int i = 0; i < kPrefsToMigrateLength; ++i) { | 339 for (int i = 0; i < kPrefsToMigrateLength; ++i) { |
338 const PrefService::Preference* pref = | 340 const PrefService::Preference* pref = |
339 prefs->FindPreference(kPrefNamesToMigrate[i].from); | 341 prefs->FindPreference(kPrefNamesToMigrate[i].from); |
340 if (!pref) continue; | 342 if (!pref) continue; |
341 if (!pref->IsDefaultValue()) { | 343 if (!pref->IsDefaultValue()) { |
342 prefs->Set(kPrefNamesToMigrate[i].to, *pref->GetValue()); | 344 prefs->Set(kPrefNamesToMigrate[i].to, *pref->GetValue()); |
343 } | 345 } |
344 prefs->ClearPref(kPrefNamesToMigrate[i].from); | 346 prefs->ClearPref(kPrefNamesToMigrate[i].from); |
345 prefs->UnregisterPreference(kPrefNamesToMigrate[i].from); | 347 prefs->UnregisterPreference(kPrefNamesToMigrate[i].from); |
346 } | 348 } |
347 } | 349 } |
348 | 350 |
| 351 // Sets a font family pref in |prefs| to |pref_value|. |
| 352 void OverrideFontFamily(WebPreferences* prefs, |
| 353 const std::string& generic_family, |
| 354 const std::string& script, |
| 355 const std::string& pref_value) { |
| 356 WebPreferences::ScriptFontFamilyMap* map = NULL; |
| 357 if (generic_family == "standard") |
| 358 map = &prefs->standard_font_family_map; |
| 359 else if (generic_family == "fixed") |
| 360 map = &prefs->fixed_font_family_map; |
| 361 else if (generic_family == "serif") |
| 362 map = &prefs->serif_font_family_map; |
| 363 else if (generic_family == "sansserif") |
| 364 map = &prefs->sans_serif_font_family_map; |
| 365 else if (generic_family == "cursive") |
| 366 map = &prefs->cursive_font_family_map; |
| 367 else if (generic_family == "fantasy") |
| 368 map = &prefs->fantasy_font_family_map; |
| 369 else if (generic_family == "pictograph") |
| 370 map = &prefs->pictograph_font_family_map; |
| 371 else |
| 372 NOTREACHED() << "Unknown generic font family: " << generic_family; |
| 373 (*map)[script] = UTF8ToUTF16(pref_value); |
| 374 } |
| 375 |
349 } // namespace | 376 } // namespace |
350 | 377 |
351 PrefsTabHelper::PrefsTabHelper(WebContents* contents) | 378 PrefsTabHelper::PrefsTabHelper(WebContents* contents) |
352 : web_contents_(contents) { | 379 : web_contents_(contents) { |
353 PrefService* prefs = GetProfile()->GetPrefs(); | 380 PrefService* prefs = GetProfile()->GetPrefs(); |
354 pref_change_registrar_.Init(prefs); | 381 pref_change_registrar_.Init(prefs); |
355 if (prefs) { | 382 if (prefs) { |
356 for (int i = 0; i < kPrefsToObserveLength; ++i) | 383 for (int i = 0; i < kPrefsToObserveLength; ++i) |
357 pref_change_registrar_.Add(kPrefsToObserve[i], this); | 384 pref_change_registrar_.Add(kPrefsToObserve[i], this); |
358 | 385 |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 UpdateRendererPreferences(); | 565 UpdateRendererPreferences(); |
539 break; | 566 break; |
540 } | 567 } |
541 #endif | 568 #endif |
542 case chrome::NOTIFICATION_PREF_CHANGED: { | 569 case chrome::NOTIFICATION_PREF_CHANGED: { |
543 std::string* pref_name_in = content::Details<std::string>(details).ptr(); | 570 std::string* pref_name_in = content::Details<std::string>(details).ptr(); |
544 DCHECK(content::Source<PrefService>(source).ptr() == | 571 DCHECK(content::Source<PrefService>(source).ptr() == |
545 GetProfile()->GetPrefs()); | 572 GetProfile()->GetPrefs()); |
546 if (*pref_name_in == prefs::kDefaultCharset || | 573 if (*pref_name_in == prefs::kDefaultCharset || |
547 StartsWithASCII(*pref_name_in, "webkit.webprefs.", true)) { | 574 StartsWithASCII(*pref_name_in, "webkit.webprefs.", true)) { |
548 UpdateWebPreferences(); | 575 OnWebPrefChanged(*pref_name_in); |
549 } else if (*pref_name_in == prefs::kDefaultZoomLevel || | 576 } else if (*pref_name_in == prefs::kDefaultZoomLevel || |
550 *pref_name_in == prefs::kEnableReferrers || | 577 *pref_name_in == prefs::kEnableReferrers || |
551 *pref_name_in == prefs::kEnableDoNotTrack) { | 578 *pref_name_in == prefs::kEnableDoNotTrack) { |
552 UpdateRendererPreferences(); | 579 UpdateRendererPreferences(); |
553 } else { | 580 } else { |
554 NOTREACHED() << "unexpected pref change notification" << *pref_name_in; | 581 NOTREACHED() << "unexpected pref change notification" << *pref_name_in; |
555 } | 582 } |
556 break; | 583 break; |
557 } | 584 } |
558 default: | 585 default: |
559 NOTREACHED(); | 586 NOTREACHED(); |
560 } | 587 } |
561 } | 588 } |
562 | 589 |
563 void PrefsTabHelper::UpdateWebPreferences() { | 590 void PrefsTabHelper::UpdateWebPreferences() { |
564 web_contents_->GetRenderViewHost()->UpdateWebkitPreferences( | 591 web_contents_->GetRenderViewHost()->UpdateWebkitPreferences( |
565 web_contents_->GetRenderViewHost()->GetWebkitPreferences()); | 592 web_contents_->GetRenderViewHost()->GetWebkitPreferences()); |
566 } | 593 } |
567 | 594 |
568 void PrefsTabHelper::UpdateRendererPreferences() { | 595 void PrefsTabHelper::UpdateRendererPreferences() { |
569 renderer_preferences_util::UpdateFromSystemSettings( | 596 renderer_preferences_util::UpdateFromSystemSettings( |
570 web_contents_->GetMutableRendererPrefs(), GetProfile()); | 597 web_contents_->GetMutableRendererPrefs(), GetProfile()); |
571 web_contents_->GetRenderViewHost()->SyncRendererPrefs(); | 598 web_contents_->GetRenderViewHost()->SyncRendererPrefs(); |
572 } | 599 } |
573 | 600 |
574 Profile* PrefsTabHelper::GetProfile() { | 601 Profile* PrefsTabHelper::GetProfile() { |
575 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | 602 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
576 } | 603 } |
| 604 |
| 605 void PrefsTabHelper::OnWebPrefChanged(const std::string& pref_name) { |
| 606 // When a font family pref's value goes from non-empty to the empty string, we |
| 607 // must add it to the usual WebPreferences struct passed to the renderer. |
| 608 // |
| 609 // The empty string means to fall back to the pref for the Common script |
| 610 // ("Zyyy"). For example, if chrome.fonts.serif.Cyrl is the empty string, it |
| 611 // means to use chrome.fonts.serif.Zyyy for Cyrillic script. Prefs that are |
| 612 // the empty string are normally not passed to WebKit, since there are so many |
| 613 // of them that it would cause a performance regression. Not passing the pref |
| 614 // is normally okay since WebKit does the desired fallback behavior regardless |
| 615 // of whether the empty string is passed or the pref is not passed at all. But |
| 616 // if the pref has changed from non-empty to the empty string, we must let |
| 617 // WebKit know. |
| 618 std::string generic_family; |
| 619 std::string script; |
| 620 if (pref_names_util::ParseFontNamePrefPath(pref_name, |
| 621 &generic_family, |
| 622 &script)) { |
| 623 PrefService* prefs = GetProfile()->GetPrefs(); |
| 624 std::string pref_value = prefs->GetString(pref_name.c_str()); |
| 625 if (pref_value.empty()) { |
| 626 WebPreferences web_prefs = |
| 627 web_contents_->GetRenderViewHost()->GetWebkitPreferences(); |
| 628 OverrideFontFamily(&web_prefs, generic_family, script, ""); |
| 629 web_contents_->GetRenderViewHost()->UpdateWebkitPreferences(web_prefs); |
| 630 return; |
| 631 } |
| 632 } |
| 633 |
| 634 UpdateWebPreferences(); |
| 635 } |
OLD | NEW |