prefs_tab_helper.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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#include "chrome/browser/ui/prefs/prefs_tab_helper.h" 6 7#include <string> 8 9#include "base/prefs/overlay_user_pref_store.h" 10#include "base/string_util.h" 11#include "base/stringprintf.h" 12#include "base/utf_string_conversions.h" 13#include "chrome/browser/browser_process.h" 14#include "chrome/browser/prefs/pref_service.h" 15#include "chrome/browser/profiles/profile.h" 16#include "chrome/browser/renderer_preferences_util.h" 17#include "chrome/common/chrome_notification_types.h" 18#include "chrome/common/pref_names.h" 19#include "chrome/common/pref_names_util.h" 20#include "content/public/browser/notification_details.h" 21#include "content/public/browser/notification_service.h" 22#include "content/public/browser/render_view_host.h" 23#include "content/public/browser/web_contents.h" 24#include "grit/locale_settings.h" 25#include "grit/platform_locale_settings.h" 26#include "unicode/uchar.h" 27#include "unicode/uscript.h" 28#include "webkit/glue/webpreferences.h" 29 30#if defined(OS_POSIX) && !defined(OS_MACOSX) && defined(ENABLE_THEMES) 31#include "chrome/browser/themes/theme_service.h" 32#include "chrome/browser/themes/theme_service_factory.h" 33#endif 34 35using content::WebContents; 36using webkit_glue::WebPreferences; 37 38DEFINE_WEB_CONTENTS_USER_DATA_KEY(PrefsTabHelper) 39 40namespace { 41 42// Registers prefs only used for migration. 43void RegisterPrefsToMigrate(PrefService* prefs) { 44 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldStandardFontFamily, 45 IDS_STANDARD_FONT_FAMILY, 46 PrefService::UNSYNCABLE_PREF); 47 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldFixedFontFamily, 48 IDS_FIXED_FONT_FAMILY, 49 PrefService::UNSYNCABLE_PREF); 50 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldSerifFontFamily, 51 IDS_SERIF_FONT_FAMILY, 52 PrefService::UNSYNCABLE_PREF); 53 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldSansSerifFontFamily, 54 IDS_SANS_SERIF_FONT_FAMILY, 55 PrefService::UNSYNCABLE_PREF); 56 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldCursiveFontFamily, 57 IDS_CURSIVE_FONT_FAMILY, 58 PrefService::UNSYNCABLE_PREF); 59 prefs->RegisterLocalizedStringPref(prefs::kWebKitOldFantasyFontFamily, 60 IDS_FANTASY_FONT_FAMILY, 61 PrefService::UNSYNCABLE_PREF); 62 prefs->RegisterLocalizedStringPref(prefs::kGlobalDefaultCharset, 63 IDS_DEFAULT_ENCODING, 64 PrefService::SYNCABLE_PREF); 65 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitGlobalDefaultFontSize, 66 IDS_DEFAULT_FONT_SIZE, 67 PrefService::UNSYNCABLE_PREF); 68 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitGlobalDefaultFixedFontSize, 69 IDS_DEFAULT_FIXED_FONT_SIZE, 70 PrefService::UNSYNCABLE_PREF); 71 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitGlobalMinimumFontSize, 72 IDS_MINIMUM_FONT_SIZE, 73 PrefService::UNSYNCABLE_PREF); 74 prefs->RegisterLocalizedIntegerPref( 75 prefs::kWebKitGlobalMinimumLogicalFontSize, 76 IDS_MINIMUM_LOGICAL_FONT_SIZE, 77 PrefService::UNSYNCABLE_PREF); 78 prefs->RegisterLocalizedStringPref(prefs::kWebKitGlobalStandardFontFamily, 79 IDS_STANDARD_FONT_FAMILY, 80 PrefService::UNSYNCABLE_PREF); 81 prefs->RegisterLocalizedStringPref(prefs::kWebKitGlobalFixedFontFamily, 82 IDS_FIXED_FONT_FAMILY, 83 PrefService::UNSYNCABLE_PREF); 84 prefs->RegisterLocalizedStringPref(prefs::kWebKitGlobalSerifFontFamily, 85 IDS_SERIF_FONT_FAMILY, 86 PrefService::UNSYNCABLE_PREF); 87 prefs->RegisterLocalizedStringPref(prefs::kWebKitGlobalSansSerifFontFamily, 88 IDS_SANS_SERIF_FONT_FAMILY, 89 PrefService::UNSYNCABLE_PREF); 90 prefs->RegisterLocalizedStringPref(prefs::kWebKitGlobalCursiveFontFamily, 91 IDS_CURSIVE_FONT_FAMILY, 92 PrefService::UNSYNCABLE_PREF); 93 prefs->RegisterLocalizedStringPref(prefs::kWebKitGlobalFantasyFontFamily, 94 IDS_FANTASY_FONT_FAMILY, 95 PrefService::UNSYNCABLE_PREF); 96} 97 98// The list of prefs we want to observe. 99const char* kPrefsToObserve[] = { 100 prefs::kDefaultZoomLevel, 101 prefs::kDefaultCharset, 102 prefs::kEnableReferrers, 103 prefs::kEnableDoNotTrack, 104 prefs::kWebKitAllowDisplayingInsecureContent, 105 prefs::kWebKitAllowRunningInsecureContent, 106 prefs::kWebKitDefaultFixedFontSize, 107 prefs::kWebKitDefaultFontSize, 108#if defined(OS_ANDROID) 109 prefs::kWebKitFontScaleFactor, 110 prefs::kWebKitForceEnableZoom, 111#endif 112 prefs::kWebKitJavascriptEnabled, 113 prefs::kWebKitJavaEnabled, 114 prefs::kWebKitLoadsImagesAutomatically, 115 prefs::kWebKitMinimumFontSize, 116 prefs::kWebKitMinimumLogicalFontSize, 117 prefs::kWebKitPluginsEnabled, 118 prefs::kWebkitTabsToLinks, 119 prefs::kWebKitUsesUniversalDetector 120}; 121 122const int kPrefsToObserveLength = arraysize(kPrefsToObserve); 123 124// Registers a preference under the path |map_name| for each script used for 125// per-script font prefs. For example, if |map_name| is "fonts.serif", then 126// "fonts.serif.Arab", "fonts.serif.Hang", etc. are registered. 127void RegisterFontFamilyMap(PrefService* prefs, 128 const char* map_name, 129 const std::set<std::string>& fonts_with_defaults) { 130 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { 131 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; 132 std::string pref_name_str = base::StringPrintf("%s.%s", map_name, script); 133 const char* pref_name = pref_name_str.c_str(); 134 if (fonts_with_defaults.find(pref_name) == fonts_with_defaults.end()) { 135 // We haven't already set a default value for this font preference, so set 136 // an empty string as the default. 137 prefs->RegisterStringPref(pref_name, "", PrefService::UNSYNCABLE_PREF); 138 } 139 } 140} 141 142// Registers |obs| to observe per-script font prefs under the path |map_name|. 143void RegisterFontFamilyMapObserver(PrefChangeRegistrar* registrar, 144 const char* map_name, 145 PrefObserver* obs) { 146 for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) { 147 const char* script = prefs::kWebKitScriptsForFontFamilyMaps[i]; 148 std::string pref_name = base::StringPrintf("%s.%s", map_name, script); 149 registrar->Add(pref_name.c_str(), obs); 150 } 151} 152 153struct FontDefault { 154 const char* pref_name; 155 int resource_id; 156}; 157 158// Font pref defaults. The prefs that have defaults vary by platform, since not 159// all platforms have fonts for all scripts for all generic families. 160// TODO(falken): add proper defaults when possible for all 161// platforms/scripts/generic families. 162const FontDefault kFontDefaults[] = { 163 { prefs::kWebKitStandardFontFamily, IDS_STANDARD_FONT_FAMILY }, 164 { prefs::kWebKitFixedFontFamily, IDS_FIXED_FONT_FAMILY }, 165 { prefs::kWebKitSerifFontFamily, IDS_SERIF_FONT_FAMILY }, 166 { prefs::kWebKitSansSerifFontFamily, IDS_SANS_SERIF_FONT_FAMILY }, 167 { prefs::kWebKitCursiveFontFamily, IDS_CURSIVE_FONT_FAMILY }, 168 { prefs::kWebKitFantasyFontFamily, IDS_FANTASY_FONT_FAMILY }, 169 { prefs::kWebKitPictographFontFamily, IDS_PICTOGRAPH_FONT_FAMILY }, 170#if defined(OS_CHROMEOS) || defined(OS_MACOSX) || defined(OS_WIN) 171 { prefs::kWebKitStandardFontFamilyJapanese, 172 IDS_STANDARD_FONT_FAMILY_JAPANESE }, 173 { prefs::kWebKitFixedFontFamilyJapanese, IDS_FIXED_FONT_FAMILY_JAPANESE }, 174 { prefs::kWebKitSerifFontFamilyJapanese, IDS_SERIF_FONT_FAMILY_JAPANESE }, 175 { prefs::kWebKitSansSerifFontFamilyJapanese, 176 IDS_SANS_SERIF_FONT_FAMILY_JAPANESE }, 177 { prefs::kWebKitStandardFontFamilyKorean, IDS_STANDARD_FONT_FAMILY_KOREAN }, 178 { prefs::kWebKitSerifFontFamilyKorean, IDS_SERIF_FONT_FAMILY_KOREAN }, 179 { prefs::kWebKitSansSerifFontFamilyKorean, 180 IDS_SANS_SERIF_FONT_FAMILY_KOREAN }, 181 { prefs::kWebKitStandardFontFamilySimplifiedHan, 182 IDS_STANDARD_FONT_FAMILY_SIMPLIFIED_HAN }, 183 { prefs::kWebKitSerifFontFamilySimplifiedHan, 184 IDS_SERIF_FONT_FAMILY_SIMPLIFIED_HAN }, 185 { prefs::kWebKitSansSerifFontFamilySimplifiedHan, 186 IDS_SANS_SERIF_FONT_FAMILY_SIMPLIFIED_HAN }, 187 { prefs::kWebKitStandardFontFamilyTraditionalHan, 188 IDS_STANDARD_FONT_FAMILY_TRADITIONAL_HAN }, 189 { prefs::kWebKitSerifFontFamilyTraditionalHan, 190 IDS_SERIF_FONT_FAMILY_TRADITIONAL_HAN }, 191 { prefs::kWebKitSansSerifFontFamilyTraditionalHan, 192 IDS_SANS_SERIF_FONT_FAMILY_TRADITIONAL_HAN }, 193#endif 194#if defined(OS_CHROMEOS) 195 { prefs::kWebKitStandardFontFamilyArabic, IDS_STANDARD_FONT_FAMILY_ARABIC }, 196 { prefs::kWebKitSerifFontFamilyArabic, IDS_SERIF_FONT_FAMILY_ARABIC }, 197 { prefs::kWebKitSansSerifFontFamilyArabic, 198 IDS_SANS_SERIF_FONT_FAMILY_ARABIC }, 199 { prefs::kWebKitFixedFontFamilyKorean, IDS_FIXED_FONT_FAMILY_KOREAN }, 200 { prefs::kWebKitFixedFontFamilySimplifiedHan, 201 IDS_FIXED_FONT_FAMILY_SIMPLIFIED_HAN }, 202 { prefs::kWebKitFixedFontFamilyTraditionalHan, 203 IDS_FIXED_FONT_FAMILY_TRADITIONAL_HAN }, 204#elif defined(OS_WIN) 205 { prefs::kWebKitStandardFontFamilyCyrillic, 206 IDS_STANDARD_FONT_FAMILY_CYRILLIC }, 207 { prefs::kWebKitFixedFontFamilyCyrillic, IDS_FIXED_FONT_FAMILY_CYRILLIC }, 208 { prefs::kWebKitSerifFontFamilyCyrillic, IDS_SERIF_FONT_FAMILY_CYRILLIC }, 209 { prefs::kWebKitSansSerifFontFamilyCyrillic, 210 IDS_SANS_SERIF_FONT_FAMILY_CYRILLIC }, 211 { prefs::kWebKitStandardFontFamilyGreek, IDS_STANDARD_FONT_FAMILY_GREEK }, 212 { prefs::kWebKitFixedFontFamilyGreek, IDS_FIXED_FONT_FAMILY_GREEK }, 213 { prefs::kWebKitSerifFontFamilyGreek, IDS_SERIF_FONT_FAMILY_GREEK }, 214 { prefs::kWebKitSansSerifFontFamilyGreek, IDS_SANS_SERIF_FONT_FAMILY_GREEK }, 215 { prefs::kWebKitFixedFontFamilyKorean, IDS_FIXED_FONT_FAMILY_KOREAN }, 216 { prefs::kWebKitCursiveFontFamilyKorean, IDS_CURSIVE_FONT_FAMILY_KOREAN }, 217 { prefs::kWebKitFixedFontFamilySimplifiedHan, 218 IDS_FIXED_FONT_FAMILY_SIMPLIFIED_HAN }, 219 { prefs::kWebKitFixedFontFamilyTraditionalHan, 220 IDS_FIXED_FONT_FAMILY_TRADITIONAL_HAN }, 221#endif 222}; 223 224const size_t kFontDefaultsLength = arraysize(kFontDefaults); 225 226// Returns the script of the font pref |pref_name|. For example, suppose 227// |pref_name| is "webkit.webprefs.fonts.serif.Hant". Since the script code for 228// the script name "Hant" is USCRIPT_TRADITIONAL_HAN, the function returns 229// USCRIPT_TRADITIONAL_HAN. |pref_name| must be a valid font pref name. 230UScriptCode GetScriptOfFontPref(const char* pref_name) { 231 // ICU script names are four letters. 232 static const size_t kScriptNameLength = 4; 233 234 size_t len = strlen(pref_name); 235 DCHECK_GT(len, kScriptNameLength); 236 const char* scriptName = &pref_name[len - kScriptNameLength]; 237 int32 code = u_getPropertyValueEnum(UCHAR_SCRIPT, scriptName); 238 DCHECK(code >= 0 && code < USCRIPT_CODE_LIMIT); 239 return static_cast<UScriptCode>(code); 240} 241 242// If |scriptCode| is a member of a family of "similar" script codes, returns 243// the script code in that family that is used in font pref names. For example, 244// USCRIPT_HANGUL and USCRIPT_KOREAN are considered equivalent for the purposes 245// of font selection. Chrome uses the script code USCRIPT_HANGUL (script name 246// "Hang") in Korean font pref names (for example, 247// "webkit.webprefs.fonts.serif.Hang"). So, if |scriptCode| is USCRIPT_KOREAN, 248// the function returns USCRIPT_HANGUL. If |scriptCode| is not a member of such 249// a family, returns |scriptCode|. 250UScriptCode GetScriptForFontPrefMatching(UScriptCode scriptCode) { 251 switch (scriptCode) { 252 case USCRIPT_HIRAGANA: 253 case USCRIPT_KATAKANA: 254 case USCRIPT_KATAKANA_OR_HIRAGANA: 255 return USCRIPT_JAPANESE; 256 case USCRIPT_KOREAN: 257 return USCRIPT_HANGUL; 258 default: 259 return scriptCode; 260 } 261} 262 263// Returns the primary script used by the browser's UI locale. For example, if 264// the locale is "ru", the function returns USCRIPT_CYRILLIC, and if the locale 265// is "en", the function returns USCRIPT_LATIN. 266UScriptCode GetScriptOfBrowserLocale() { 267 std::string locale = g_browser_process->GetApplicationLocale(); 268 269 // For Chinese locales, uscript_getCode() just returns USCRIPT_HAN but our 270 // per-script fonts are for USCRIPT_SIMPLIFIED_HAN and 271 // USCRIPT_TRADITIONAL_HAN. 272 if (locale == "zh-CN") 273 return USCRIPT_SIMPLIFIED_HAN; 274 if (locale == "zh-TW") 275 return USCRIPT_TRADITIONAL_HAN; 276 277 UScriptCode code = USCRIPT_INVALID_CODE; 278 UErrorCode err = U_ZERO_ERROR; 279 uscript_getCode(locale.c_str(), &code, 1, &err); 280 281 // Ignore the error that multiple scripts could be returned, since we only 282 // want one script. 283 if (U_FAILURE(err) && err != U_BUFFER_OVERFLOW_ERROR) 284 code = USCRIPT_INVALID_CODE; 285 return GetScriptForFontPrefMatching(code); 286} 287 288const struct { 289 const char* from; 290 const char* to; 291} kPrefNamesToMigrate[] = { 292 // Migrate prefs like "webkit.webprefs.standard_font_family" to 293 // "webkit.webprefs.fonts.standard.Zyyy". This moves the formerly 294 // "non-per-script" font prefs into the per-script font pref maps, as the 295 // entry for the "Common" script (Zyyy is the ISO 15924 script code for the 296 // Common script). The |from| prefs will exist if the migration to global 297 // prefs (for the per-tab pref mechanism, which has since been removed) never 298 // occurred. 299 { prefs::kWebKitOldCursiveFontFamily, 300 prefs::kWebKitCursiveFontFamily }, 301 { prefs::kWebKitOldFantasyFontFamily, 302 prefs::kWebKitFantasyFontFamily }, 303 { prefs::kWebKitOldFixedFontFamily, 304 prefs::kWebKitFixedFontFamily }, 305 { prefs::kWebKitOldSansSerifFontFamily, 306 prefs::kWebKitSansSerifFontFamily }, 307 { prefs::kWebKitOldSerifFontFamily, 308 prefs::kWebKitSerifFontFamily }, 309 { prefs::kWebKitOldStandardFontFamily, 310 prefs::kWebKitStandardFontFamily }, 311 312 // Migrate "global" prefs. These will exist if the migration to global prefs 313 // (for the per-tab pref mechanism, which has since been removed) occurred. 314 // In addition, this moves the formerly "non-per-script" font prefs into the 315 // per-script font pref maps, as above. 316 { prefs::kGlobalDefaultCharset, 317 prefs::kDefaultCharset }, 318 { prefs::kWebKitGlobalDefaultFixedFontSize, 319 prefs::kWebKitDefaultFixedFontSize }, 320 { prefs::kWebKitGlobalDefaultFontSize, 321 prefs::kWebKitDefaultFontSize }, 322 { prefs::kWebKitGlobalMinimumFontSize, 323 prefs::kWebKitMinimumFontSize }, 324 { prefs::kWebKitGlobalMinimumLogicalFontSize, 325 prefs::kWebKitMinimumLogicalFontSize }, 326 { prefs::kWebKitGlobalCursiveFontFamily, 327 prefs::kWebKitCursiveFontFamily }, 328 { prefs::kWebKitGlobalFantasyFontFamily, 329 prefs::kWebKitFantasyFontFamily }, 330 { prefs::kWebKitGlobalFixedFontFamily, 331 prefs::kWebKitFixedFontFamily }, 332 { prefs::kWebKitGlobalSansSerifFontFamily, 333 prefs::kWebKitSansSerifFontFamily }, 334 { prefs::kWebKitGlobalSerifFontFamily, 335 prefs::kWebKitSerifFontFamily }, 336 { prefs::kWebKitGlobalStandardFontFamily, 337 prefs::kWebKitStandardFontFamily } 338}; 339 340const int kPrefsToMigrateLength = ARRAYSIZE_UNSAFE(kPrefNamesToMigrate); 341 342void MigratePreferences(PrefService* prefs) { 343 RegisterPrefsToMigrate(prefs); 344 for (int i = 0; i < kPrefsToMigrateLength; ++i) { 345 const PrefService::Preference* pref = 346 prefs->FindPreference(kPrefNamesToMigrate[i].from); 347 if (!pref) continue; 348 if (!pref->IsDefaultValue()) { 349 prefs->Set(kPrefNamesToMigrate[i].to, *pref->GetValue()); 350 } 351 prefs->ClearPref(kPrefNamesToMigrate[i].from); 352 prefs->UnregisterPreference(kPrefNamesToMigrate[i].from); 353 } 354} 355 356// Sets a font family pref in |prefs| to |pref_value|. 357void OverrideFontFamily(WebPreferences* prefs, 358 const std::string& generic_family, 359 const std::string& script, 360 const std::string& pref_value) { 361 WebPreferences::ScriptFontFamilyMap* map = NULL; 362 if (generic_family == "standard") 363 map = &prefs->standard_font_family_map; 364 else if (generic_family == "fixed") 365 map = &prefs->fixed_font_family_map; 366 else if (generic_family == "serif") 367 map = &prefs->serif_font_family_map; 368 else if (generic_family == "sansserif") 369 map = &prefs->sans_serif_font_family_map; 370 else if (generic_family == "cursive") 371 map = &prefs->cursive_font_family_map; 372 else if (generic_family == "fantasy") 373 map = &prefs->fantasy_font_family_map; 374 else if (generic_family == "pictograph") 375 map = &prefs->pictograph_font_family_map; 376 else 377 NOTREACHED() << "Unknown generic font family: " << generic_family; 378 (*map)[script] = UTF8ToUTF16(pref_value); 379} 380 381} // namespace 382 383PrefsTabHelper::PrefsTabHelper(WebContents* contents) 384 : web_contents_(contents) { 385 PrefService* prefs = GetProfile()->GetPrefs(); 386 pref_change_registrar_.Init(prefs); 387 if (prefs) { 388 for (int i = 0; i < kPrefsToObserveLength; ++i) 389 pref_change_registrar_.Add(kPrefsToObserve[i], this); 390 391 RegisterFontFamilyMapObserver(&pref_change_registrar_, 392 prefs::kWebKitStandardFontFamilyMap, this); 393 RegisterFontFamilyMapObserver(&pref_change_registrar_, 394 prefs::kWebKitFixedFontFamilyMap, this); 395 RegisterFontFamilyMapObserver(&pref_change_registrar_, 396 prefs::kWebKitSerifFontFamilyMap, this); 397 RegisterFontFamilyMapObserver(&pref_change_registrar_, 398 prefs::kWebKitSansSerifFontFamilyMap, this); 399 RegisterFontFamilyMapObserver(&pref_change_registrar_, 400 prefs::kWebKitCursiveFontFamilyMap, this); 401 RegisterFontFamilyMapObserver(&pref_change_registrar_, 402 prefs::kWebKitFantasyFontFamilyMap, this); 403 RegisterFontFamilyMapObserver(&pref_change_registrar_, 404 prefs::kWebKitPictographFontFamilyMap, this); 405 } 406 407 renderer_preferences_util::UpdateFromSystemSettings( 408 web_contents_->GetMutableRendererPrefs(), GetProfile()); 409 410 registrar_.Add(this, chrome::NOTIFICATION_USER_STYLE_SHEET_UPDATED, 411 content::NotificationService::AllSources()); 412#if defined(OS_POSIX) && !defined(OS_MACOSX) && defined(ENABLE_THEMES) 413 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, 414 content::Source<ThemeService>( 415 ThemeServiceFactory::GetForProfile(GetProfile()))); 416#endif 417} 418 419PrefsTabHelper::~PrefsTabHelper() { 420} 421 422// static 423void PrefsTabHelper::InitIncognitoUserPrefStore( 424 OverlayUserPrefStore* pref_store) { 425 // List of keys that cannot be changed in the user prefs file by the incognito 426 // profile. All preferences that store information about the browsing history 427 // or behavior of the user should have this property. 428 pref_store->RegisterOverlayPref(prefs::kBrowserWindowPlacement); 429#if defined(OS_ANDROID) 430 pref_store->RegisterOverlayPref(prefs::kProxy); 431#endif 432} 433 434// static 435void PrefsTabHelper::RegisterUserPrefs(PrefService* prefs) { 436 WebPreferences pref_defaults; 437 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled, 438 pref_defaults.javascript_enabled, 439 PrefService::UNSYNCABLE_PREF); 440 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled, 441 pref_defaults.web_security_enabled, 442 PrefService::UNSYNCABLE_PREF); 443 prefs->RegisterBooleanPref( 444 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, 445 true, 446 PrefService::UNSYNCABLE_PREF); 447 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically, 448 pref_defaults.loads_images_automatically, 449 PrefService::UNSYNCABLE_PREF); 450 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled, 451 pref_defaults.plugins_enabled, 452 PrefService::UNSYNCABLE_PREF); 453 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled, 454 pref_defaults.dom_paste_enabled, 455 PrefService::UNSYNCABLE_PREF); 456 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit, 457 pref_defaults.shrinks_standalone_images_to_fit, 458 PrefService::UNSYNCABLE_PREF); 459 prefs->RegisterDictionaryPref(prefs::kWebKitInspectorSettings, 460 PrefService::UNSYNCABLE_PREF); 461 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable, 462 pref_defaults.text_areas_are_resizable, 463 PrefService::UNSYNCABLE_PREF); 464 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled, 465 pref_defaults.java_enabled, 466 PrefService::UNSYNCABLE_PREF); 467 prefs->RegisterBooleanPref(prefs::kWebkitTabsToLinks, 468 pref_defaults.tabs_to_links, 469 PrefService::UNSYNCABLE_PREF); 470 prefs->RegisterBooleanPref(prefs::kWebKitAllowRunningInsecureContent, 471 false, 472 PrefService::UNSYNCABLE_PREF); 473 prefs->RegisterBooleanPref(prefs::kWebKitAllowDisplayingInsecureContent, 474 true, 475 PrefService::UNSYNCABLE_PREF); 476 prefs->RegisterBooleanPref(prefs::kEnableReferrers, 477 true, 478 PrefService::UNSYNCABLE_PREF); 479#if defined(OS_ANDROID) 480 prefs->RegisterDoublePref(prefs::kWebKitFontScaleFactor, 481 pref_defaults.font_scale_factor, 482 PrefService::UNSYNCABLE_PREF); 483 prefs->RegisterBooleanPref(prefs::kWebKitForceEnableZoom, 484 pref_defaults.force_enable_zoom, 485 PrefService::UNSYNCABLE_PREF); 486#endif 487 488#if !defined(OS_MACOSX) 489 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages, 490 IDS_ACCEPT_LANGUAGES, 491 PrefService::SYNCABLE_PREF); 492#else 493 // Not used in OSX. 494 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages, 495 IDS_ACCEPT_LANGUAGES, 496 PrefService::UNSYNCABLE_PREF); 497#endif 498 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset, 499 IDS_DEFAULT_ENCODING, 500 PrefService::SYNCABLE_PREF); 501 502 // Register font prefs that have defaults. 503 std::set<std::string> fonts_with_defaults; 504 UScriptCode browser_script = GetScriptOfBrowserLocale(); 505 for (size_t i = 0; i < kFontDefaultsLength; ++i) { 506 const FontDefault& pref = kFontDefaults[i]; 507 UScriptCode pref_script = GetScriptOfFontPref(pref.pref_name); 508 509 // Suppress this default font pref value if it is for the primary script of 510 // the browser's UI locale. For example, if the pref is for the sans-serif 511 // font for the Cyrillic script, and the browser locale is "ru" (Russian), 512 // the default is suppressed. Otherwise, the default would override the 513 // user's font preferences when viewing pages in their native language. 514 // This is because users have no way yet of customizing their per-script 515 // font preferences. The font prefs accessible in the options UI are for 516 // the default, unknown script; these prefs have less priority than the 517 // per-script font prefs when the script of the content is known. This code 518 // can possibly be removed later if users can easily access per-script font 519 // prefs (e.g., via the extensions workflow), or the problem turns out to 520 // not be really critical after all. 521 if (browser_script != pref_script) { 522 prefs->RegisterLocalizedStringPref(pref.pref_name, 523 pref.resource_id, 524 PrefService::UNSYNCABLE_PREF); 525 fonts_with_defaults.insert(pref.pref_name); 526 } 527 } 528 529 // Register font prefs that don't have defaults. 530 RegisterFontFamilyMap(prefs, prefs::kWebKitStandardFontFamilyMap, 531 fonts_with_defaults); 532 RegisterFontFamilyMap(prefs, prefs::kWebKitFixedFontFamilyMap, 533 fonts_with_defaults); 534 RegisterFontFamilyMap(prefs, prefs::kWebKitSerifFontFamilyMap, 535 fonts_with_defaults); 536 RegisterFontFamilyMap(prefs, prefs::kWebKitSansSerifFontFamilyMap, 537 fonts_with_defaults); 538 RegisterFontFamilyMap(prefs, prefs::kWebKitCursiveFontFamilyMap, 539 fonts_with_defaults); 540 RegisterFontFamilyMap(prefs, prefs::kWebKitFantasyFontFamilyMap, 541 fonts_with_defaults); 542 RegisterFontFamilyMap(prefs, prefs::kWebKitPictographFontFamilyMap, 543 fonts_with_defaults); 544 545 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize, 546 IDS_DEFAULT_FONT_SIZE, 547 PrefService::UNSYNCABLE_PREF); 548 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize, 549 IDS_DEFAULT_FIXED_FONT_SIZE, 550 PrefService::UNSYNCABLE_PREF); 551 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize, 552 IDS_MINIMUM_FONT_SIZE, 553 PrefService::UNSYNCABLE_PREF); 554 prefs->RegisterLocalizedIntegerPref( 555 prefs::kWebKitMinimumLogicalFontSize, 556 IDS_MINIMUM_LOGICAL_FONT_SIZE, 557 PrefService::UNSYNCABLE_PREF); 558 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector, 559 IDS_USES_UNIVERSAL_DETECTOR, 560 PrefService::SYNCABLE_PREF); 561 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings, 562 IDS_STATIC_ENCODING_LIST, 563 PrefService::UNSYNCABLE_PREF); 564 prefs->RegisterStringPref(prefs::kRecentlySelectedEncoding, 565 "", 566 PrefService::UNSYNCABLE_PREF); 567 MigratePreferences(prefs); 568} 569 570void PrefsTabHelper::Observe(int type, 571 const content::NotificationSource& source, 572 const content::NotificationDetails& details) { 573 switch (type) { 574 case chrome::NOTIFICATION_USER_STYLE_SHEET_UPDATED: 575 UpdateWebPreferences(); 576 break; 577#if defined(OS_POSIX) && !defined(OS_MACOSX) && defined(ENABLE_THEMES) 578 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { 579 UpdateRendererPreferences(); 580 break; 581 } 582#endif 583 default: 584 NOTREACHED(); 585 } 586} 587 588void PrefsTabHelper::OnPreferenceChanged(PrefServiceBase* service, 589 const std::string& pref_name_in) { 590 DCHECK_EQ(GetProfile()->GetPrefs(), service); 591 if (pref_name_in == prefs::kDefaultCharset || 592 StartsWithASCII(pref_name_in, "webkit.webprefs.", true)) { 593 OnWebPrefChanged(pref_name_in); 594 } else if (pref_name_in == prefs::kDefaultZoomLevel || 595 pref_name_in == prefs::kEnableReferrers || 596 pref_name_in == prefs::kEnableDoNotTrack) { 597 UpdateRendererPreferences(); 598 } else { 599 NOTREACHED() << "unexpected pref change notification" << pref_name_in; 600 } 601} 602 603void PrefsTabHelper::UpdateWebPreferences() { 604 web_contents_->GetRenderViewHost()->UpdateWebkitPreferences( 605 web_contents_->GetRenderViewHost()->GetWebkitPreferences()); 606} 607 608void PrefsTabHelper::UpdateRendererPreferences() { 609 renderer_preferences_util::UpdateFromSystemSettings( 610 web_contents_->GetMutableRendererPrefs(), GetProfile()); 611 web_contents_->GetRenderViewHost()->SyncRendererPrefs(); 612} 613 614Profile* PrefsTabHelper::GetProfile() { 615 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); 616} 617 618void PrefsTabHelper::OnWebPrefChanged(const std::string& pref_name) { 619 // When a font family pref's value goes from non-empty to the empty string, we 620 // must add it to the usual WebPreferences struct passed to the renderer. 621 // 622 // The empty string means to fall back to the pref for the Common script 623 // ("Zyyy"). For example, if chrome.fonts.serif.Cyrl is the empty string, it 624 // means to use chrome.fonts.serif.Zyyy for Cyrillic script. Prefs that are 625 // the empty string are normally not passed to WebKit, since there are so many 626 // of them that it would cause a performance regression. Not passing the pref 627 // is normally okay since WebKit does the desired fallback behavior regardless 628 // of whether the empty string is passed or the pref is not passed at all. But 629 // if the pref has changed from non-empty to the empty string, we must let 630 // WebKit know. 631 std::string generic_family; 632 std::string script; 633 if (pref_names_util::ParseFontNamePrefPath(pref_name, 634 &generic_family, 635 &script)) { 636 PrefService* prefs = GetProfile()->GetPrefs(); 637 std::string pref_value = prefs->GetString(pref_name.c_str()); 638 if (pref_value.empty()) { 639 WebPreferences web_prefs = 640 web_contents_->GetRenderViewHost()->GetWebkitPreferences(); 641 OverrideFontFamily(&web_prefs, generic_family, script, ""); 642 web_contents_->GetRenderViewHost()->UpdateWebkitPreferences(web_prefs); 643 return; 644 } 645 } 646 647 UpdateWebPreferences(); 648} 649