 Chromium Code Reviews
 Chromium Code Reviews Issue 11336008:
  When a font family pref changes to the empty string, pass it to WebKit.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 11336008:
  When a font family pref changes to the empty string, pass it to WebKit.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| 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..54c2407121b2bee981566e238740b618051ca6de 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; | 
| 
Nico
2012/10/30 04:24:29
= NULL
Else it won't be uninitialized in the NOTR
 
falken
2012/10/30 07:01:34
Done.
 | 
| + 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); | 
| +} | 
| + | 
| } // 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 | 
| + // 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(); | 
| +} |