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.
5 #include "chrome/browser/ui/prefs/prefs_tab_helper.h"
9 #include "base/prefs/overlay_user_pref_store.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/renderer_preferences_util.h"
18 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h"
19 #include "chrome/browser/ui/zoom/zoom_controller.h"
20 #include "chrome/common/pref_font_webkit_names.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/common/pref_names_util.h"
23 #include "chrome/grit/locale_settings.h"
24 #include "components/pref_registry/pref_registry_syncable.h"
25 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/render_view_host.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/common/renderer_preferences.h"
30 #include "content/public/common/web_preferences.h"
31 #include "grit/platform_locale_settings.h"
32 #include "third_party/icu/source/common/unicode/uchar.h"
33 #include "third_party/icu/source/common/unicode/uscript.h"
34 #include "ui/base/l10n/l10n_util.h"
36 #if defined(OS_POSIX) && !defined(OS_MACOSX) && defined(ENABLE_THEMES)
37 #include "chrome/browser/themes/theme_service.h"
38 #include "chrome/browser/themes/theme_service_factory.h"
42 #include "base/win/windows_version.h"
45 using content::WebContents
;
46 using content::WebPreferences
;
48 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PrefsTabHelper
);
52 // The list of prefs we want to observe.
53 const char* kPrefsToObserve
[] = {
54 prefs::kDefaultCharset
,
55 prefs::kDisable3DAPIs
,
56 prefs::kEnableHyperlinkAuditing
,
57 prefs::kWebKitAllowDisplayingInsecureContent
,
58 prefs::kWebKitAllowRunningInsecureContent
,
59 prefs::kWebKitDefaultFixedFontSize
,
60 prefs::kWebKitDefaultFontSize
,
61 prefs::kWebKitDomPasteEnabled
,
62 #if defined(OS_ANDROID)
63 prefs::kWebKitFontScaleFactor
,
64 prefs::kWebKitForceEnableZoom
,
65 prefs::kWebKitPasswordEchoEnabled
,
67 prefs::kWebKitJavascriptCanOpenWindowsAutomatically
,
68 prefs::kWebKitJavascriptEnabled
,
69 prefs::kWebKitJavaEnabled
,
70 prefs::kWebKitLoadsImagesAutomatically
,
71 prefs::kWebKitMinimumFontSize
,
72 prefs::kWebKitMinimumLogicalFontSize
,
73 prefs::kWebKitPluginsEnabled
,
74 prefs::kWebKitShrinksStandaloneImagesToFit
,
75 prefs::kWebkitTabsToLinks
,
76 prefs::kWebKitTextAreasAreResizable
,
77 prefs::kWebKitUsesUniversalDetector
,
78 prefs::kWebKitWebSecurityEnabled
,
81 const int kPrefsToObserveLength
= arraysize(kPrefsToObserve
);
83 #if !defined(OS_ANDROID)
84 // Registers a preference under the path |pref_name| for each script used for
85 // per-script font prefs.
86 // For example, for WEBKIT_WEBPREFS_FONTS_SERIF ("fonts.serif"):
87 // "fonts.serif.Arab", "fonts.serif.Hang", etc. are registered.
88 // |fonts_with_defaults| contains all |pref_names| already registered since they
89 // have a specified default value.
90 // On Android there are no default values for these properties and there is no
91 // way to set them (because extensions are not supported so the Font Settings
92 // API cannot be used), so we can avoid registering them altogether.
93 void RegisterFontFamilyPrefs(user_prefs::PrefRegistrySyncable
* registry
,
94 const std::set
<std::string
>& fonts_with_defaults
) {
96 // Expand the font concatenated with script name so this stays at RO memory
97 // rather than allocated in heap.
98 static const char* const kFontFamilyMap
[] = {
99 #define EXPAND_SCRIPT_FONT(map_name, script_name) map_name "." script_name,
101 #include "chrome/common/pref_font_script_names-inl.h"
102 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_CURSIVE
)
103 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_FANTASY
)
104 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_FIXED
)
105 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_PICTOGRAPH
)
106 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_SANSERIF
)
107 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_SERIF
)
108 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_STANDARD
)
110 #undef EXPAND_SCRIPT_FONT
113 for (size_t i
= 0; i
< arraysize(kFontFamilyMap
); ++i
) {
114 const char* pref_name
= kFontFamilyMap
[i
];
115 if (fonts_with_defaults
.find(pref_name
) == fonts_with_defaults
.end()) {
116 // We haven't already set a default value for this font preference, so set
117 // an empty string as the default.
118 registry
->RegisterStringPref(
121 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
125 #endif // !defined(OS_ANDROID)
127 // Registers |obs| to observe per-script font prefs under the path |map_name|.
128 // On android, there's no exposed way to change these prefs, so we can save
129 // ~715KB of heap and some startup cycles by avoiding observing these prefs
130 // since they will never change.
131 void RegisterFontFamilyMapObserver(
132 PrefChangeRegistrar
* registrar
,
133 const char* map_name
,
134 const PrefChangeRegistrar::NamedChangeCallback
& obs
) {
135 DCHECK(StartsWithASCII(map_name
, "webkit.webprefs.", true));
136 for (size_t i
= 0; i
< prefs::kWebKitScriptsForFontFamilyMapsLength
; ++i
) {
137 const char* script
= prefs::kWebKitScriptsForFontFamilyMaps
[i
];
138 std::string pref_name
= base::StringPrintf("%s.%s", map_name
, script
);
139 registrar
->Add(pref_name
.c_str(), obs
);
144 // On Windows with antialising we want to use an alternate fixed font like
145 // Consolas, which looks much better than Courier New.
146 bool ShouldUseAlternateDefaultFixedFont(const std::string
& script
) {
147 if (!StartsWithASCII(script
, "courier", false))
149 UINT smooth_type
= 0;
150 SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE
, 0, &smooth_type
, 0);
151 return (base::win::GetVersion() >= base::win::VERSION_WIN7
) &&
152 (smooth_type
== FE_FONTSMOOTHINGCLEARTYPE
);
157 const char* pref_name
;
161 // Font pref defaults. The prefs that have defaults vary by platform, since not
162 // all platforms have fonts for all scripts for all generic families.
163 // TODO(falken): add proper defaults when possible for all
164 // platforms/scripts/generic families.
165 const FontDefault kFontDefaults
[] = {
166 { prefs::kWebKitStandardFontFamily
, IDS_STANDARD_FONT_FAMILY
},
167 { prefs::kWebKitFixedFontFamily
, IDS_FIXED_FONT_FAMILY
},
168 { prefs::kWebKitSerifFontFamily
, IDS_SERIF_FONT_FAMILY
},
169 { prefs::kWebKitSansSerifFontFamily
, IDS_SANS_SERIF_FONT_FAMILY
},
170 { prefs::kWebKitCursiveFontFamily
, IDS_CURSIVE_FONT_FAMILY
},
171 { prefs::kWebKitFantasyFontFamily
, IDS_FANTASY_FONT_FAMILY
},
172 { prefs::kWebKitPictographFontFamily
, IDS_PICTOGRAPH_FONT_FAMILY
},
173 #if defined(OS_CHROMEOS) || defined(OS_MACOSX) || defined(OS_WIN)
174 { prefs::kWebKitStandardFontFamilyJapanese
,
175 IDS_STANDARD_FONT_FAMILY_JAPANESE
},
176 { prefs::kWebKitFixedFontFamilyJapanese
, IDS_FIXED_FONT_FAMILY_JAPANESE
},
177 { prefs::kWebKitSerifFontFamilyJapanese
, IDS_SERIF_FONT_FAMILY_JAPANESE
},
178 { prefs::kWebKitSansSerifFontFamilyJapanese
,
179 IDS_SANS_SERIF_FONT_FAMILY_JAPANESE
},
180 { prefs::kWebKitStandardFontFamilyKorean
, IDS_STANDARD_FONT_FAMILY_KOREAN
},
181 { prefs::kWebKitSerifFontFamilyKorean
, IDS_SERIF_FONT_FAMILY_KOREAN
},
182 { prefs::kWebKitSansSerifFontFamilyKorean
,
183 IDS_SANS_SERIF_FONT_FAMILY_KOREAN
},
184 { prefs::kWebKitStandardFontFamilySimplifiedHan
,
185 IDS_STANDARD_FONT_FAMILY_SIMPLIFIED_HAN
},
186 { prefs::kWebKitSerifFontFamilySimplifiedHan
,
187 IDS_SERIF_FONT_FAMILY_SIMPLIFIED_HAN
},
188 { prefs::kWebKitSansSerifFontFamilySimplifiedHan
,
189 IDS_SANS_SERIF_FONT_FAMILY_SIMPLIFIED_HAN
},
190 { prefs::kWebKitStandardFontFamilyTraditionalHan
,
191 IDS_STANDARD_FONT_FAMILY_TRADITIONAL_HAN
},
192 { prefs::kWebKitSerifFontFamilyTraditionalHan
,
193 IDS_SERIF_FONT_FAMILY_TRADITIONAL_HAN
},
194 { prefs::kWebKitSansSerifFontFamilyTraditionalHan
,
195 IDS_SANS_SERIF_FONT_FAMILY_TRADITIONAL_HAN
},
197 #if defined(OS_CHROMEOS)
198 { prefs::kWebKitStandardFontFamilyArabic
, IDS_STANDARD_FONT_FAMILY_ARABIC
},
199 { prefs::kWebKitSerifFontFamilyArabic
, IDS_SERIF_FONT_FAMILY_ARABIC
},
200 { prefs::kWebKitSansSerifFontFamilyArabic
,
201 IDS_SANS_SERIF_FONT_FAMILY_ARABIC
},
202 { prefs::kWebKitFixedFontFamilyKorean
, IDS_FIXED_FONT_FAMILY_KOREAN
},
203 { prefs::kWebKitFixedFontFamilySimplifiedHan
,
204 IDS_FIXED_FONT_FAMILY_SIMPLIFIED_HAN
},
205 { prefs::kWebKitFixedFontFamilyTraditionalHan
,
206 IDS_FIXED_FONT_FAMILY_TRADITIONAL_HAN
},
207 #elif defined(OS_WIN)
208 { prefs::kWebKitStandardFontFamilyCyrillic
,
209 IDS_STANDARD_FONT_FAMILY_CYRILLIC
},
210 { prefs::kWebKitFixedFontFamilyCyrillic
, IDS_FIXED_FONT_FAMILY_CYRILLIC
},
211 { prefs::kWebKitSerifFontFamilyCyrillic
, IDS_SERIF_FONT_FAMILY_CYRILLIC
},
212 { prefs::kWebKitSansSerifFontFamilyCyrillic
,
213 IDS_SANS_SERIF_FONT_FAMILY_CYRILLIC
},
214 { prefs::kWebKitStandardFontFamilyGreek
, IDS_STANDARD_FONT_FAMILY_GREEK
},
215 { prefs::kWebKitFixedFontFamilyGreek
, IDS_FIXED_FONT_FAMILY_GREEK
},
216 { prefs::kWebKitSerifFontFamilyGreek
, IDS_SERIF_FONT_FAMILY_GREEK
},
217 { prefs::kWebKitSansSerifFontFamilyGreek
, IDS_SANS_SERIF_FONT_FAMILY_GREEK
},
218 { prefs::kWebKitFixedFontFamilyKorean
, IDS_FIXED_FONT_FAMILY_KOREAN
},
219 { prefs::kWebKitCursiveFontFamilyKorean
, IDS_CURSIVE_FONT_FAMILY_KOREAN
},
220 { prefs::kWebKitFixedFontFamilySimplifiedHan
,
221 IDS_FIXED_FONT_FAMILY_SIMPLIFIED_HAN
},
222 { prefs::kWebKitFixedFontFamilyTraditionalHan
,
223 IDS_FIXED_FONT_FAMILY_TRADITIONAL_HAN
},
227 const size_t kFontDefaultsLength
= arraysize(kFontDefaults
);
229 // Returns the script of the font pref |pref_name|. For example, suppose
230 // |pref_name| is "webkit.webprefs.fonts.serif.Hant". Since the script code for
231 // the script name "Hant" is USCRIPT_TRADITIONAL_HAN, the function returns
232 // USCRIPT_TRADITIONAL_HAN. |pref_name| must be a valid font pref name.
233 UScriptCode
GetScriptOfFontPref(const char* pref_name
) {
234 // ICU script names are four letters.
235 static const size_t kScriptNameLength
= 4;
237 size_t len
= strlen(pref_name
);
238 DCHECK_GT(len
, kScriptNameLength
);
239 const char* scriptName
= &pref_name
[len
- kScriptNameLength
];
240 int32 code
= u_getPropertyValueEnum(UCHAR_SCRIPT
, scriptName
);
241 DCHECK(code
>= 0 && code
< USCRIPT_CODE_LIMIT
);
242 return static_cast<UScriptCode
>(code
);
245 // If |scriptCode| is a member of a family of "similar" script codes, returns
246 // the script code in that family that is used in font pref names. For example,
247 // USCRIPT_HANGUL and USCRIPT_KOREAN are considered equivalent for the purposes
248 // of font selection. Chrome uses the script code USCRIPT_HANGUL (script name
249 // "Hang") in Korean font pref names (for example,
250 // "webkit.webprefs.fonts.serif.Hang"). So, if |scriptCode| is USCRIPT_KOREAN,
251 // the function returns USCRIPT_HANGUL. If |scriptCode| is not a member of such
252 // a family, returns |scriptCode|.
253 UScriptCode
GetScriptForFontPrefMatching(UScriptCode scriptCode
) {
254 switch (scriptCode
) {
255 case USCRIPT_HIRAGANA
:
256 case USCRIPT_KATAKANA
:
257 case USCRIPT_KATAKANA_OR_HIRAGANA
:
258 return USCRIPT_JAPANESE
;
260 return USCRIPT_HANGUL
;
266 // Returns the primary script used by the browser's UI locale. For example, if
267 // the locale is "ru", the function returns USCRIPT_CYRILLIC, and if the locale
268 // is "en", the function returns USCRIPT_LATIN.
269 UScriptCode
GetScriptOfBrowserLocale() {
270 std::string locale
= g_browser_process
->GetApplicationLocale();
272 // For Chinese locales, uscript_getCode() just returns USCRIPT_HAN but our
273 // per-script fonts are for USCRIPT_SIMPLIFIED_HAN and
274 // USCRIPT_TRADITIONAL_HAN.
275 if (locale
== "zh-CN")
276 return USCRIPT_SIMPLIFIED_HAN
;
277 if (locale
== "zh-TW")
278 return USCRIPT_TRADITIONAL_HAN
;
280 UScriptCode code
= USCRIPT_INVALID_CODE
;
281 UErrorCode err
= U_ZERO_ERROR
;
282 uscript_getCode(locale
.c_str(), &code
, 1, &err
);
284 // Ignore the error that multiple scripts could be returned, since we only
286 if (U_FAILURE(err
) && err
!= U_BUFFER_OVERFLOW_ERROR
)
287 code
= USCRIPT_INVALID_CODE
;
288 return GetScriptForFontPrefMatching(code
);
291 // Sets a font family pref in |prefs| to |pref_value|.
292 void OverrideFontFamily(WebPreferences
* prefs
,
293 const std::string
& generic_family
,
294 const std::string
& script
,
295 const std::string
& pref_value
) {
296 content::ScriptFontFamilyMap
* map
= NULL
;
297 if (generic_family
== "standard")
298 map
= &prefs
->standard_font_family_map
;
299 else if (generic_family
== "fixed")
300 map
= &prefs
->fixed_font_family_map
;
301 else if (generic_family
== "serif")
302 map
= &prefs
->serif_font_family_map
;
303 else if (generic_family
== "sansserif")
304 map
= &prefs
->sans_serif_font_family_map
;
305 else if (generic_family
== "cursive")
306 map
= &prefs
->cursive_font_family_map
;
307 else if (generic_family
== "fantasy")
308 map
= &prefs
->fantasy_font_family_map
;
309 else if (generic_family
== "pictograph")
310 map
= &prefs
->pictograph_font_family_map
;
312 NOTREACHED() << "Unknown generic font family: " << generic_family
;
313 (*map
)[script
] = base::UTF8ToUTF16(pref_value
);
318 PrefsTabHelper::PrefsTabHelper(WebContents
* contents
)
319 : web_contents_(contents
),
320 weak_ptr_factory_(this) {
321 PrefService
* prefs
= GetProfile()->GetPrefs();
322 pref_change_registrar_
.Init(prefs
);
324 // TODO(wjmaclean): Convert this to use the content-specific zoom-level
325 // prefs when HostZoomMap moves to StoragePartition.
326 chrome::ChromeZoomLevelPrefs
* zoom_level_prefs
=
327 GetProfile()->GetZoomLevelPrefs();
329 base::Closure renderer_callback
= base::Bind(
330 &PrefsTabHelper::UpdateRendererPreferences
, base::Unretained(this));
331 // Incognito mode does not have a zoom_level_prefs, and not all tests
332 // should need to create one either.
333 if (zoom_level_prefs
) {
334 default_zoom_level_subscription_
=
335 zoom_level_prefs
->RegisterDefaultZoomLevelCallback(renderer_callback
);
337 pref_change_registrar_
.Add(prefs::kAcceptLanguages
, renderer_callback
);
338 pref_change_registrar_
.Add(prefs::kEnableDoNotTrack
, renderer_callback
);
339 pref_change_registrar_
.Add(prefs::kEnableReferrers
, renderer_callback
);
341 #if !defined(OS_MACOSX)
342 pref_change_registrar_
.Add(prefs::kFullscreenAllowed
, renderer_callback
);
345 PrefChangeRegistrar::NamedChangeCallback webkit_callback
= base::Bind(
346 &PrefsTabHelper::OnWebPrefChanged
, base::Unretained(this));
347 for (int i
= 0; i
< kPrefsToObserveLength
; ++i
) {
348 const char* pref_name
= kPrefsToObserve
[i
];
349 pref_change_registrar_
.Add(pref_name
, webkit_callback
);
352 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
353 prefs::kWebKitStandardFontFamilyMap
,
355 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
356 prefs::kWebKitFixedFontFamilyMap
,
358 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
359 prefs::kWebKitSerifFontFamilyMap
,
361 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
362 prefs::kWebKitSansSerifFontFamilyMap
,
364 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
365 prefs::kWebKitCursiveFontFamilyMap
,
367 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
368 prefs::kWebKitFantasyFontFamilyMap
,
370 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
371 prefs::kWebKitPictographFontFamilyMap
,
375 content::RendererPreferences
* render_prefs
=
376 web_contents_
->GetMutableRendererPrefs();
377 renderer_preferences_util::UpdateFromSystemSettings(render_prefs
,
381 #if defined(OS_POSIX) && !defined(OS_MACOSX) && defined(ENABLE_THEMES)
383 chrome::NOTIFICATION_BROWSER_THEME_CHANGED
,
384 content::Source
<ThemeService
>(
385 ThemeServiceFactory::GetForProfile(GetProfile())));
387 #if defined(USE_AURA)
389 chrome::NOTIFICATION_BROWSER_FLING_CURVE_PARAMETERS_CHANGED
,
390 content::NotificationService::AllSources());
394 PrefsTabHelper::~PrefsTabHelper() {
398 void PrefsTabHelper::InitIncognitoUserPrefStore(
399 OverlayUserPrefStore
* pref_store
) {
400 // List of keys that cannot be changed in the user prefs file by the incognito
401 // profile. All preferences that store information about the browsing history
402 // or behavior of the user should have this property.
403 pref_store
->RegisterOverlayPref(prefs::kBrowserWindowPlacement
);
404 pref_store
->RegisterOverlayPref(prefs::kSaveFileDefaultDirectory
);
405 #if defined(OS_ANDROID) || defined(OS_IOS)
406 pref_store
->RegisterOverlayPref(prefs::kProxy
);
407 #endif // defined(OS_ANDROID) || defined(OS_IOS)
411 void PrefsTabHelper::RegisterProfilePrefs(
412 user_prefs::PrefRegistrySyncable
* registry
) {
413 WebPreferences pref_defaults
;
414 registry
->RegisterBooleanPref(
415 prefs::kWebKitJavascriptEnabled
,
416 pref_defaults
.javascript_enabled
,
417 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
418 registry
->RegisterBooleanPref(
419 prefs::kWebKitWebSecurityEnabled
,
420 pref_defaults
.web_security_enabled
,
421 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
422 registry
->RegisterBooleanPref(
423 prefs::kWebKitJavascriptCanOpenWindowsAutomatically
,
425 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
426 registry
->RegisterBooleanPref(
427 prefs::kWebKitLoadsImagesAutomatically
,
428 pref_defaults
.loads_images_automatically
,
429 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
430 registry
->RegisterBooleanPref(
431 prefs::kWebKitPluginsEnabled
,
432 pref_defaults
.plugins_enabled
,
433 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
434 registry
->RegisterBooleanPref(
435 prefs::kWebKitDomPasteEnabled
,
436 pref_defaults
.dom_paste_enabled
,
437 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
438 registry
->RegisterBooleanPref(
439 prefs::kWebKitShrinksStandaloneImagesToFit
,
440 pref_defaults
.shrinks_standalone_images_to_fit
,
441 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
442 registry
->RegisterBooleanPref(
443 prefs::kWebKitTextAreasAreResizable
,
444 pref_defaults
.text_areas_are_resizable
,
445 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
446 registry
->RegisterBooleanPref(
447 prefs::kWebKitJavaEnabled
,
448 pref_defaults
.java_enabled
,
449 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
450 registry
->RegisterBooleanPref(
451 prefs::kWebkitTabsToLinks
,
452 pref_defaults
.tabs_to_links
,
453 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
454 registry
->RegisterBooleanPref(
455 prefs::kWebKitAllowRunningInsecureContent
,
457 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
458 registry
->RegisterBooleanPref(
459 prefs::kWebKitAllowDisplayingInsecureContent
,
461 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
462 registry
->RegisterBooleanPref(
463 prefs::kEnableReferrers
,
465 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
466 #if defined(OS_ANDROID)
467 registry
->RegisterDoublePref(
468 prefs::kWebKitFontScaleFactor
,
470 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
471 registry
->RegisterBooleanPref(
472 prefs::kWebKitForceEnableZoom
,
473 pref_defaults
.force_enable_zoom
,
474 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
475 registry
->RegisterBooleanPref(
476 prefs::kWebKitPasswordEchoEnabled
,
477 pref_defaults
.password_echo_enabled
,
478 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
480 registry
->RegisterLocalizedStringPref(
481 prefs::kAcceptLanguages
,
482 IDS_ACCEPT_LANGUAGES
,
483 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
484 registry
->RegisterLocalizedStringPref(
485 prefs::kDefaultCharset
,
486 IDS_DEFAULT_ENCODING
,
487 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
489 // Register font prefs that have defaults.
490 std::set
<std::string
> fonts_with_defaults
;
491 UScriptCode browser_script
= GetScriptOfBrowserLocale();
492 for (size_t i
= 0; i
< kFontDefaultsLength
; ++i
) {
493 FontDefault pref
= kFontDefaults
[i
];
496 if (pref
.pref_name
== prefs::kWebKitFixedFontFamily
) {
497 if (ShouldUseAlternateDefaultFixedFont(
498 l10n_util::GetStringUTF8(pref
.resource_id
)))
499 pref
.resource_id
= IDS_FIXED_FONT_FAMILY_ALT_WIN
;
503 UScriptCode pref_script
= GetScriptOfFontPref(pref
.pref_name
);
505 // Suppress this default font pref value if it is for the primary script of
506 // the browser's UI locale. For example, if the pref is for the sans-serif
507 // font for the Cyrillic script, and the browser locale is "ru" (Russian),
508 // the default is suppressed. Otherwise, the default would override the
509 // user's font preferences when viewing pages in their native language.
510 // This is because users have no way yet of customizing their per-script
511 // font preferences. The font prefs accessible in the options UI are for
512 // the default, unknown script; these prefs have less priority than the
513 // per-script font prefs when the script of the content is known. This code
514 // can possibly be removed later if users can easily access per-script font
515 // prefs (e.g., via the extensions workflow), or the problem turns out to
516 // not be really critical after all.
517 if (browser_script
!= pref_script
) {
518 registry
->RegisterLocalizedStringPref(
521 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
522 fonts_with_defaults
.insert(pref
.pref_name
);
526 // Register per-script font prefs that don't have defaults.
527 #if !defined(OS_ANDROID)
528 RegisterFontFamilyPrefs(registry
, fonts_with_defaults
);
531 registry
->RegisterLocalizedIntegerPref(
532 prefs::kWebKitDefaultFontSize
,
533 IDS_DEFAULT_FONT_SIZE
,
534 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
535 registry
->RegisterLocalizedIntegerPref(
536 prefs::kWebKitDefaultFixedFontSize
,
537 IDS_DEFAULT_FIXED_FONT_SIZE
,
538 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
539 registry
->RegisterLocalizedIntegerPref(
540 prefs::kWebKitMinimumFontSize
,
541 IDS_MINIMUM_FONT_SIZE
,
542 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
543 registry
->RegisterLocalizedIntegerPref(
544 prefs::kWebKitMinimumLogicalFontSize
,
545 IDS_MINIMUM_LOGICAL_FONT_SIZE
,
546 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
547 registry
->RegisterLocalizedBooleanPref(
548 prefs::kWebKitUsesUniversalDetector
,
549 IDS_USES_UNIVERSAL_DETECTOR
,
550 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
551 registry
->RegisterLocalizedStringPref(
552 prefs::kStaticEncodings
,
553 IDS_STATIC_ENCODING_LIST
,
554 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
555 registry
->RegisterStringPref(
556 prefs::kRecentlySelectedEncoding
,
558 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
561 void PrefsTabHelper::Observe(int type
,
562 const content::NotificationSource
& source
,
563 const content::NotificationDetails
& details
) {
564 #if defined(OS_POSIX) && !defined(OS_MACOSX) && defined(ENABLE_THEMES)
565 if (type
== chrome::NOTIFICATION_BROWSER_THEME_CHANGED
) {
566 UpdateRendererPreferences();
571 #if defined(USE_AURA)
572 if (type
== chrome::NOTIFICATION_BROWSER_FLING_CURVE_PARAMETERS_CHANGED
) {
573 UpdateRendererPreferences();
576 #endif // defined(USE_AURA)
581 void PrefsTabHelper::UpdateWebPreferences() {
582 web_contents_
->GetRenderViewHost()->UpdateWebkitPreferences(
583 web_contents_
->GetRenderViewHost()->GetWebkitPreferences());
586 void PrefsTabHelper::UpdateRendererPreferences() {
587 content::RendererPreferences
* prefs
=
588 web_contents_
->GetMutableRendererPrefs();
589 renderer_preferences_util::UpdateFromSystemSettings(
590 prefs
, GetProfile(), web_contents_
);
591 web_contents_
->GetRenderViewHost()->SyncRendererPrefs();
594 Profile
* PrefsTabHelper::GetProfile() {
595 return Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
598 void PrefsTabHelper::OnFontFamilyPrefChanged(const std::string
& pref_name
) {
599 // When a font family pref's value goes from non-empty to the empty string, we
600 // must add it to the usual WebPreferences struct passed to the renderer.
602 // The empty string means to fall back to the pref for the Common script
603 // ("Zyyy"). For example, if chrome.fonts.serif.Cyrl is the empty string, it
604 // means to use chrome.fonts.serif.Zyyy for Cyrillic script. Prefs that are
605 // the empty string are normally not passed to WebKit, since there are so many
606 // of them that it would cause a performance regression. Not passing the pref
607 // is normally okay since WebKit does the desired fallback behavior regardless
608 // of whether the empty string is passed or the pref is not passed at all. But
609 // if the pref has changed from non-empty to the empty string, we must let
611 std::string generic_family
;
613 if (pref_names_util::ParseFontNamePrefPath(pref_name
,
616 PrefService
* prefs
= GetProfile()->GetPrefs();
617 std::string pref_value
= prefs
->GetString(pref_name
.c_str());
618 if (pref_value
.empty()) {
619 WebPreferences web_prefs
=
620 web_contents_
->GetRenderViewHost()->GetWebkitPreferences();
621 OverrideFontFamily(&web_prefs
, generic_family
, script
, std::string());
622 web_contents_
->GetRenderViewHost()->UpdateWebkitPreferences(web_prefs
);
628 void PrefsTabHelper::OnWebPrefChanged(const std::string
& pref_name
) {
629 #if !defined(OS_ANDROID)
630 OnFontFamilyPrefChanged(pref_name
);
633 web_contents_
->GetRenderViewHost()->OnWebkitPreferencesChanged();