Chromium Code Reviews| Index: chrome/browser/ui/prefs/prefs_tab_helper.cc |
| diff --git a/chrome/browser/ui/prefs/prefs_tab_helper.cc b/chrome/browser/ui/prefs/prefs_tab_helper.cc |
| index 903cb9db07a159696e6323e9bb02e5fab2215af4..ce996135c2b6cfdff267b906631b7079aa03f340 100644 |
| --- a/chrome/browser/ui/prefs/prefs_tab_helper.cc |
| +++ b/chrome/browser/ui/prefs/prefs_tab_helper.cc |
| @@ -9,12 +9,14 @@ |
| #include "base/prefs/overlay_user_pref_store.h" |
| #include "base/string_util.h" |
| #include "base/stringprintf.h" |
| +#include "base/utf_string_conversions.h" |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/prefs/pref_service.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/renderer_preferences_util.h" |
| #include "chrome/common/chrome_notification_types.h" |
| #include "chrome/common/pref_names.h" |
| +#include "chrome/common/pref_names_util.h" |
| #include "content/public/browser/notification_details.h" |
| #include "content/public/browser/notification_service.h" |
| #include "content/public/browser/render_view_host.h" |
| @@ -38,7 +40,7 @@ DEFINE_WEB_CONTENTS_USER_DATA_KEY(PrefsTabHelper) |
| namespace { |
| // Registers prefs only used for migration. |
| -static void RegisterPrefsToMigrate(PrefService* prefs) { |
| +void RegisterPrefsToMigrate(PrefService* prefs) { |
| prefs->RegisterLocalizedStringPref(prefs::kWebKitOldStandardFontFamily, |
| IDS_STANDARD_FONT_FAMILY, |
| PrefService::UNSYNCABLE_PREF); |
| @@ -332,7 +334,7 @@ const struct { |
| const int kPrefsToMigrateLength = ARRAYSIZE_UNSAFE(kPrefNamesToMigrate); |
| -static void MigratePreferences(PrefService* prefs) { |
| +void MigratePreferences(PrefService* prefs) { |
| RegisterPrefsToMigrate(prefs); |
| for (int i = 0; i < kPrefsToMigrateLength; ++i) { |
| const PrefService::Preference* pref = |
| @@ -346,6 +348,31 @@ static void MigratePreferences(PrefService* prefs) { |
| } |
| } |
| +// Sets a font family pref in |prefs| to |pref_value|. |
| +void OverrideFontFamily(WebPreferences* prefs, |
| + const std::string& generic_family, |
| + const std::string& script, |
| + const std::string& pref_value) { |
| + WebPreferences::ScriptFontFamilyMap* map = NULL; |
| + if (generic_family == "standard") |
| + map = &prefs->standard_font_family_map; |
| + else if (generic_family == "fixed") |
| + map = &prefs->fixed_font_family_map; |
| + else if (generic_family == "serif") |
| + map = &prefs->serif_font_family_map; |
| + else if (generic_family == "sansserif") |
| + map = &prefs->sans_serif_font_family_map; |
| + else if (generic_family == "cursive") |
| + map = &prefs->cursive_font_family_map; |
| + else if (generic_family == "fantasy") |
| + map = &prefs->fantasy_font_family_map; |
| + else if (generic_family == "pictograph") |
| + map = &prefs->pictograph_font_family_map; |
| + else |
| + NOTREACHED(); |
| + (*map)[script] = UTF8ToUTF16(pref_value); |
|
Bernhard Bauer
2012/10/30 20:57:51
If somebody passes in an invalid value for |generi
falken
2012/10/31 06:27:59
Maybe it's useful as documentation? I've added a s
|
| +} |
| + |
| } // namespace |
| PrefsTabHelper::PrefsTabHelper(WebContents* contents) |
| @@ -545,7 +572,7 @@ void PrefsTabHelper::Observe(int type, |
| GetProfile()->GetPrefs()); |
| if (*pref_name_in == prefs::kDefaultCharset || |
| StartsWithASCII(*pref_name_in, "webkit.webprefs.", true)) { |
| - UpdateWebPreferences(); |
| + OnWebPrefChanged(*pref_name_in); |
| } else if (*pref_name_in == prefs::kDefaultZoomLevel || |
| *pref_name_in == prefs::kEnableReferrers || |
| *pref_name_in == prefs::kEnableDoNotTrack) { |
| @@ -574,3 +601,35 @@ void PrefsTabHelper::UpdateRendererPreferences() { |
| Profile* PrefsTabHelper::GetProfile() { |
| return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| } |
| + |
| +void PrefsTabHelper::OnWebPrefChanged(const std::string& pref_name) { |
| + // When a font family pref's value goes from non-empty to the empty string, we |
| + // must add it to the usual WebPreferences struct passed to the renderer. |
| + // |
| + // The empty string means to fall back to the pref for the Common script |
| + // ("Zyyy"). For example, if chrome.fonts.serif.Cyrl is the empty string, it |
| + // means to use chrome.fonts.serif.Zyyy for Cyrllic script. Prefs that are the |
|
Bernhard Bauer
2012/10/30 20:57:51
Nit: "Cyrillic script"
falken
2012/10/31 06:27:59
Done.
|
| + // empty string are normally not passed to WebKit, since there are so many of |
| + // them that it would cause a performance regression. Not passing the pref is |
| + // normally okay since WebKit does the desired fallback behavior regardless of |
| + // whether the empty string is passed or the pref is not passed at all. But if |
| + // the pref has changed from non-empty to the empty string, we must let WebKit |
| + // know. |
| + std::string generic_family; |
| + std::string script; |
| + if (pref_names_util::ParseFontNamePrefPath(pref_name, |
| + &generic_family, |
| + &script)) { |
| + PrefService* prefs = GetProfile()->GetPrefs(); |
| + std::string pref_value = prefs->GetString(pref_name.c_str()); |
| + if (pref_value.empty()) { |
| + WebPreferences web_prefs = |
| + web_contents_->GetRenderViewHost()->GetWebkitPreferences(); |
| + OverrideFontFamily(&web_prefs, generic_family, script, ""); |
| + web_contents_->GetRenderViewHost()->UpdateWebkitPreferences(web_prefs); |
| + return; |
| + } |
| + } |
| + |
| + UpdateWebPreferences(); |
| +} |