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"
10 #include "base/memory/singleton.h"
11 #include "base/prefs/overlay_user_pref_store.h"
12 #include "base/prefs/pref_change_registrar.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/profiles/incognito_helpers.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/renderer_preferences_util.h"
23 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h"
24 #include "chrome/common/pref_font_webkit_names.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/common/pref_names_util.h"
27 #include "chrome/grit/locale_settings.h"
28 #include "components/keyed_service/content/browser_context_dependency_manager.h"
29 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
30 #include "components/keyed_service/core/keyed_service.h"
31 #include "components/pref_registry/pref_registry_syncable.h"
32 #include "content/public/browser/notification_details.h"
33 #include "content/public/browser/notification_service.h"
34 #include "content/public/browser/render_view_host.h"
35 #include "content/public/browser/web_contents.h"
36 #include "content/public/common/renderer_preferences.h"
37 #include "content/public/common/web_preferences.h"
38 #include "grit/platform_locale_settings.h"
39 #include "third_party/icu/source/common/unicode/uchar.h"
40 #include "third_party/icu/source/common/unicode/uscript.h"
41 #include "ui/base/l10n/l10n_util.h"
43 #if defined(OS_POSIX) && !defined(OS_MACOSX) && defined(ENABLE_THEMES)
44 #include "chrome/browser/themes/theme_service.h"
45 #include "chrome/browser/themes/theme_service_factory.h"
49 #include "base/win/windows_version.h"
52 using content::WebContents
;
53 using content::WebPreferences
;
55 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PrefsTabHelper
);
59 // The list of prefs we want to observe.
60 const char* kPrefsToObserve
[] = {
61 #if defined(ENABLE_EXTENSIONS)
62 prefs::kAnimationPolicy
,
64 prefs::kDefaultCharset
,
65 prefs::kDisable3DAPIs
,
66 prefs::kEnableHyperlinkAuditing
,
67 prefs::kWebKitAllowDisplayingInsecureContent
,
68 prefs::kWebKitAllowRunningInsecureContent
,
69 prefs::kWebKitDefaultFixedFontSize
,
70 prefs::kWebKitDefaultFontSize
,
71 prefs::kWebKitDomPasteEnabled
,
72 #if defined(OS_ANDROID)
73 prefs::kWebKitFontScaleFactor
,
74 prefs::kWebKitForceEnableZoom
,
75 prefs::kWebKitPasswordEchoEnabled
,
77 prefs::kWebKitJavascriptCanOpenWindowsAutomatically
,
78 prefs::kWebKitJavascriptEnabled
,
79 prefs::kWebKitLoadsImagesAutomatically
,
80 prefs::kWebKitMinimumFontSize
,
81 prefs::kWebKitMinimumLogicalFontSize
,
82 prefs::kWebKitPluginsEnabled
,
83 prefs::kWebkitTabsToLinks
,
84 prefs::kWebKitTextAreasAreResizable
,
85 prefs::kWebKitUsesUniversalDetector
,
86 prefs::kWebKitWebSecurityEnabled
,
89 const int kPrefsToObserveLength
= arraysize(kPrefsToObserve
);
91 #if !defined(OS_ANDROID)
92 // Registers a preference under the path |pref_name| for each script used for
93 // per-script font prefs.
94 // For example, for WEBKIT_WEBPREFS_FONTS_SERIF ("fonts.serif"):
95 // "fonts.serif.Arab", "fonts.serif.Hang", etc. are registered.
96 // |fonts_with_defaults| contains all |pref_names| already registered since they
97 // have a specified default value.
98 // On Android there are no default values for these properties and there is no
99 // way to set them (because extensions are not supported so the Font Settings
100 // API cannot be used), so we can avoid registering them altogether.
101 void RegisterFontFamilyPrefs(user_prefs::PrefRegistrySyncable
* registry
,
102 const std::set
<std::string
>& fonts_with_defaults
) {
103 // Expand the font concatenated with script name so this stays at RO memory
104 // rather than allocated in heap.
105 static const char* const kFontFamilyMap
[] = {
106 #define EXPAND_SCRIPT_FONT(map_name, script_name) map_name "." script_name,
108 #include "chrome/common/pref_font_script_names-inl.h"
109 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_CURSIVE
)
110 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_FANTASY
)
111 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_FIXED
)
112 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_PICTOGRAPH
)
113 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_SANSERIF
)
114 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_SERIF
)
115 ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_STANDARD
)
117 #undef EXPAND_SCRIPT_FONT
120 for (size_t i
= 0; i
< arraysize(kFontFamilyMap
); ++i
) {
121 const char* pref_name
= kFontFamilyMap
[i
];
122 if (fonts_with_defaults
.find(pref_name
) == fonts_with_defaults
.end()) {
123 // We haven't already set a default value for this font preference, so set
124 // an empty string as the default.
125 registry
->RegisterStringPref(pref_name
, std::string());
129 #endif // !defined(OS_ANDROID)
131 // Registers |obs| to observe per-script font prefs under the path |map_name|.
132 // On android, there's no exposed way to change these prefs, so we can save
133 // ~715KB of heap and some startup cycles by avoiding observing these prefs
134 // since they will never change.
135 void RegisterFontFamilyMapObserver(
136 PrefChangeRegistrar
* registrar
,
137 const char* map_name
,
138 const PrefChangeRegistrar::NamedChangeCallback
& obs
) {
139 DCHECK(base::StartsWith(map_name
, "webkit.webprefs.",
140 base::CompareCase::SENSITIVE
));
142 for (size_t i
= 0; i
< prefs::kWebKitScriptsForFontFamilyMapsLength
; ++i
) {
143 const char* script
= prefs::kWebKitScriptsForFontFamilyMaps
[i
];
144 registrar
->Add(base::StringPrintf("%s.%s", map_name
, script
), obs
);
149 // On Windows with antialising we want to use an alternate fixed font like
150 // Consolas, which looks much better than Courier New.
151 bool ShouldUseAlternateDefaultFixedFont(const std::string
& script
) {
152 if (!base::StartsWith(script
, "courier",
153 base::CompareCase::INSENSITIVE_ASCII
))
155 UINT smooth_type
= 0;
156 SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE
, 0, &smooth_type
, 0);
157 return (base::win::GetVersion() >= base::win::VERSION_WIN7
) &&
158 (smooth_type
== FE_FONTSMOOTHINGCLEARTYPE
);
163 const char* pref_name
;
167 // Font pref defaults. The prefs that have defaults vary by platform, since not
168 // all platforms have fonts for all scripts for all generic families.
169 // TODO(falken): add proper defaults when possible for all
170 // platforms/scripts/generic families.
171 const FontDefault kFontDefaults
[] = {
172 { prefs::kWebKitStandardFontFamily
, IDS_STANDARD_FONT_FAMILY
},
173 { prefs::kWebKitFixedFontFamily
, IDS_FIXED_FONT_FAMILY
},
174 { prefs::kWebKitSerifFontFamily
, IDS_SERIF_FONT_FAMILY
},
175 { prefs::kWebKitSansSerifFontFamily
, IDS_SANS_SERIF_FONT_FAMILY
},
176 { prefs::kWebKitCursiveFontFamily
, IDS_CURSIVE_FONT_FAMILY
},
177 { prefs::kWebKitFantasyFontFamily
, IDS_FANTASY_FONT_FAMILY
},
178 { prefs::kWebKitPictographFontFamily
, IDS_PICTOGRAPH_FONT_FAMILY
},
179 #if defined(OS_CHROMEOS) || defined(OS_MACOSX) || defined(OS_WIN)
180 { prefs::kWebKitStandardFontFamilyJapanese
,
181 IDS_STANDARD_FONT_FAMILY_JAPANESE
},
182 { prefs::kWebKitFixedFontFamilyJapanese
, IDS_FIXED_FONT_FAMILY_JAPANESE
},
183 { prefs::kWebKitSerifFontFamilyJapanese
, IDS_SERIF_FONT_FAMILY_JAPANESE
},
184 { prefs::kWebKitSansSerifFontFamilyJapanese
,
185 IDS_SANS_SERIF_FONT_FAMILY_JAPANESE
},
186 { prefs::kWebKitStandardFontFamilyKorean
, IDS_STANDARD_FONT_FAMILY_KOREAN
},
187 { prefs::kWebKitSerifFontFamilyKorean
, IDS_SERIF_FONT_FAMILY_KOREAN
},
188 { prefs::kWebKitSansSerifFontFamilyKorean
,
189 IDS_SANS_SERIF_FONT_FAMILY_KOREAN
},
190 { prefs::kWebKitStandardFontFamilySimplifiedHan
,
191 IDS_STANDARD_FONT_FAMILY_SIMPLIFIED_HAN
},
192 { prefs::kWebKitSerifFontFamilySimplifiedHan
,
193 IDS_SERIF_FONT_FAMILY_SIMPLIFIED_HAN
},
194 { prefs::kWebKitSansSerifFontFamilySimplifiedHan
,
195 IDS_SANS_SERIF_FONT_FAMILY_SIMPLIFIED_HAN
},
196 { prefs::kWebKitStandardFontFamilyTraditionalHan
,
197 IDS_STANDARD_FONT_FAMILY_TRADITIONAL_HAN
},
198 { prefs::kWebKitSerifFontFamilyTraditionalHan
,
199 IDS_SERIF_FONT_FAMILY_TRADITIONAL_HAN
},
200 { prefs::kWebKitSansSerifFontFamilyTraditionalHan
,
201 IDS_SANS_SERIF_FONT_FAMILY_TRADITIONAL_HAN
},
203 #if defined(OS_CHROMEOS)
204 { prefs::kWebKitStandardFontFamilyArabic
, IDS_STANDARD_FONT_FAMILY_ARABIC
},
205 { prefs::kWebKitSerifFontFamilyArabic
, IDS_SERIF_FONT_FAMILY_ARABIC
},
206 { prefs::kWebKitSansSerifFontFamilyArabic
,
207 IDS_SANS_SERIF_FONT_FAMILY_ARABIC
},
208 { prefs::kWebKitFixedFontFamilyKorean
, IDS_FIXED_FONT_FAMILY_KOREAN
},
209 { prefs::kWebKitFixedFontFamilySimplifiedHan
,
210 IDS_FIXED_FONT_FAMILY_SIMPLIFIED_HAN
},
211 { prefs::kWebKitFixedFontFamilyTraditionalHan
,
212 IDS_FIXED_FONT_FAMILY_TRADITIONAL_HAN
},
213 #elif defined(OS_WIN)
214 { prefs::kWebKitFixedFontFamilyArabic
, IDS_FIXED_FONT_FAMILY_ARABIC
},
215 { prefs::kWebKitSansSerifFontFamilyArabic
,
216 IDS_SANS_SERIF_FONT_FAMILY_ARABIC
},
217 { prefs::kWebKitStandardFontFamilyCyrillic
,
218 IDS_STANDARD_FONT_FAMILY_CYRILLIC
},
219 { prefs::kWebKitFixedFontFamilyCyrillic
, IDS_FIXED_FONT_FAMILY_CYRILLIC
},
220 { prefs::kWebKitSerifFontFamilyCyrillic
, IDS_SERIF_FONT_FAMILY_CYRILLIC
},
221 { prefs::kWebKitSansSerifFontFamilyCyrillic
,
222 IDS_SANS_SERIF_FONT_FAMILY_CYRILLIC
},
223 { prefs::kWebKitStandardFontFamilyGreek
, IDS_STANDARD_FONT_FAMILY_GREEK
},
224 { prefs::kWebKitFixedFontFamilyGreek
, IDS_FIXED_FONT_FAMILY_GREEK
},
225 { prefs::kWebKitSerifFontFamilyGreek
, IDS_SERIF_FONT_FAMILY_GREEK
},
226 { prefs::kWebKitSansSerifFontFamilyGreek
, IDS_SANS_SERIF_FONT_FAMILY_GREEK
},
227 { prefs::kWebKitFixedFontFamilyKorean
, IDS_FIXED_FONT_FAMILY_KOREAN
},
228 { prefs::kWebKitCursiveFontFamilyKorean
, IDS_CURSIVE_FONT_FAMILY_KOREAN
},
229 { prefs::kWebKitFixedFontFamilySimplifiedHan
,
230 IDS_FIXED_FONT_FAMILY_SIMPLIFIED_HAN
},
231 { prefs::kWebKitFixedFontFamilyTraditionalHan
,
232 IDS_FIXED_FONT_FAMILY_TRADITIONAL_HAN
},
236 const size_t kFontDefaultsLength
= arraysize(kFontDefaults
);
238 // Returns the script of the font pref |pref_name|. For example, suppose
239 // |pref_name| is "webkit.webprefs.fonts.serif.Hant". Since the script code for
240 // the script name "Hant" is USCRIPT_TRADITIONAL_HAN, the function returns
241 // USCRIPT_TRADITIONAL_HAN. |pref_name| must be a valid font pref name.
242 UScriptCode
GetScriptOfFontPref(const char* pref_name
) {
243 // ICU script names are four letters.
244 static const size_t kScriptNameLength
= 4;
246 size_t len
= strlen(pref_name
);
247 DCHECK_GT(len
, kScriptNameLength
);
248 const char* scriptName
= &pref_name
[len
- kScriptNameLength
];
249 int32 code
= u_getPropertyValueEnum(UCHAR_SCRIPT
, scriptName
);
250 DCHECK(code
>= 0 && code
< USCRIPT_CODE_LIMIT
);
251 return static_cast<UScriptCode
>(code
);
254 // If |scriptCode| is a member of a family of "similar" script codes, returns
255 // the script code in that family that is used in font pref names. For example,
256 // USCRIPT_HANGUL and USCRIPT_KOREAN are considered equivalent for the purposes
257 // of font selection. Chrome uses the script code USCRIPT_HANGUL (script name
258 // "Hang") in Korean font pref names (for example,
259 // "webkit.webprefs.fonts.serif.Hang"). So, if |scriptCode| is USCRIPT_KOREAN,
260 // the function returns USCRIPT_HANGUL. If |scriptCode| is not a member of such
261 // a family, returns |scriptCode|.
262 UScriptCode
GetScriptForFontPrefMatching(UScriptCode scriptCode
) {
263 switch (scriptCode
) {
264 case USCRIPT_HIRAGANA
:
265 case USCRIPT_KATAKANA
:
266 case USCRIPT_KATAKANA_OR_HIRAGANA
:
267 return USCRIPT_JAPANESE
;
269 return USCRIPT_HANGUL
;
275 // Returns the primary script used by the browser's UI locale. For example, if
276 // the locale is "ru", the function returns USCRIPT_CYRILLIC, and if the locale
277 // is "en", the function returns USCRIPT_LATIN.
278 UScriptCode
GetScriptOfBrowserLocale() {
279 std::string locale
= g_browser_process
->GetApplicationLocale();
281 // For Chinese locales, uscript_getCode() just returns USCRIPT_HAN but our
282 // per-script fonts are for USCRIPT_SIMPLIFIED_HAN and
283 // USCRIPT_TRADITIONAL_HAN.
284 if (locale
== "zh-CN")
285 return USCRIPT_SIMPLIFIED_HAN
;
286 if (locale
== "zh-TW")
287 return USCRIPT_TRADITIONAL_HAN
;
289 UScriptCode code
= USCRIPT_INVALID_CODE
;
290 UErrorCode err
= U_ZERO_ERROR
;
291 uscript_getCode(locale
.c_str(), &code
, 1, &err
);
293 // Ignore the error that multiple scripts could be returned, since we only
295 if (U_FAILURE(err
) && err
!= U_BUFFER_OVERFLOW_ERROR
)
296 code
= USCRIPT_INVALID_CODE
;
297 return GetScriptForFontPrefMatching(code
);
300 // Sets a font family pref in |prefs| to |pref_value|.
301 void OverrideFontFamily(WebPreferences
* prefs
,
302 const std::string
& generic_family
,
303 const std::string
& script
,
304 const std::string
& pref_value
) {
305 content::ScriptFontFamilyMap
* map
= NULL
;
306 if (generic_family
== "standard")
307 map
= &prefs
->standard_font_family_map
;
308 else if (generic_family
== "fixed")
309 map
= &prefs
->fixed_font_family_map
;
310 else if (generic_family
== "serif")
311 map
= &prefs
->serif_font_family_map
;
312 else if (generic_family
== "sansserif")
313 map
= &prefs
->sans_serif_font_family_map
;
314 else if (generic_family
== "cursive")
315 map
= &prefs
->cursive_font_family_map
;
316 else if (generic_family
== "fantasy")
317 map
= &prefs
->fantasy_font_family_map
;
318 else if (generic_family
== "pictograph")
319 map
= &prefs
->pictograph_font_family_map
;
321 NOTREACHED() << "Unknown generic font family: " << generic_family
;
322 (*map
)[script
] = base::UTF8ToUTF16(pref_value
);
325 void RegisterLocalizedFontPref(user_prefs::PrefRegistrySyncable
* registry
,
327 int default_message_id
) {
329 bool success
= base::StringToInt(l10n_util::GetStringUTF8(
330 default_message_id
), &val
);
332 registry
->RegisterIntegerPref(path
, val
);
337 // Watching all these settings per tab is slow when a user has a lot of tabs and
338 // and they use session restore. So watch them once per profile.
339 // http://crbug.com/452693
340 class PrefWatcher
: public KeyedService
{
342 explicit PrefWatcher(Profile
* profile
) : profile_(profile
) {
343 pref_change_registrar_
.Init(profile_
->GetPrefs());
345 base::Closure renderer_callback
= base::Bind(
346 &PrefWatcher::UpdateRendererPreferences
, base::Unretained(this));
347 pref_change_registrar_
.Add(prefs::kAcceptLanguages
, renderer_callback
);
348 pref_change_registrar_
.Add(prefs::kEnableDoNotTrack
, renderer_callback
);
349 pref_change_registrar_
.Add(prefs::kEnableReferrers
, renderer_callback
);
351 #if defined(ENABLE_WEBRTC)
352 pref_change_registrar_
.Add(prefs::kWebRTCMultipleRoutesEnabled
,
356 #if !defined(OS_MACOSX)
357 pref_change_registrar_
.Add(prefs::kFullscreenAllowed
, renderer_callback
);
360 PrefChangeRegistrar::NamedChangeCallback webkit_callback
= base::Bind(
361 &PrefWatcher::OnWebPrefChanged
, base::Unretained(this));
362 for (int i
= 0; i
< kPrefsToObserveLength
; ++i
) {
363 const char* pref_name
= kPrefsToObserve
[i
];
364 pref_change_registrar_
.Add(pref_name
, webkit_callback
);
367 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
368 prefs::kWebKitStandardFontFamilyMap
,
370 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
371 prefs::kWebKitFixedFontFamilyMap
,
373 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
374 prefs::kWebKitSerifFontFamilyMap
,
376 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
377 prefs::kWebKitSansSerifFontFamilyMap
,
379 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
380 prefs::kWebKitCursiveFontFamilyMap
,
382 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
383 prefs::kWebKitFantasyFontFamilyMap
,
385 RegisterFontFamilyMapObserver(&pref_change_registrar_
,
386 prefs::kWebKitPictographFontFamilyMap
,
390 static PrefWatcher
* Get(Profile
* profile
);
392 void RegisterHelper(PrefsTabHelper
* helper
) {
393 helpers_
.insert(helper
);
396 void UnregisterHelper(PrefsTabHelper
* helper
) {
397 helpers_
.erase(helper
);
401 // KeyedService overrides:
402 void Shutdown() override
{
403 pref_change_registrar_
.RemoveAll();
406 void UpdateRendererPreferences() {
407 for (const auto& helper
: helpers_
)
408 helper
->UpdateRendererPreferences();
411 void OnWebPrefChanged(const std::string
& pref_name
) {
412 for (const auto& helper
: helpers_
)
413 helper
->OnWebPrefChanged(pref_name
);
417 PrefChangeRegistrar pref_change_registrar_
;
418 std::set
<PrefsTabHelper
*> helpers_
;
421 class PrefWatcherFactory
: public BrowserContextKeyedServiceFactory
{
423 static PrefWatcher
* GetForProfile(Profile
* profile
) {
424 return static_cast<PrefWatcher
*>(
425 GetInstance()->GetServiceForBrowserContext(profile
, true));
428 static PrefWatcherFactory
* GetInstance() {
429 return Singleton
<PrefWatcherFactory
>::get();
433 friend struct DefaultSingletonTraits
<PrefWatcherFactory
>;
435 PrefWatcherFactory() : BrowserContextKeyedServiceFactory(
437 BrowserContextDependencyManager::GetInstance()) {
440 ~PrefWatcherFactory() override
{}
442 // BrowserContextKeyedServiceFactory:
443 KeyedService
* BuildServiceInstanceFor(
444 content::BrowserContext
* browser_context
) const override
{
445 return new PrefWatcher(Profile::FromBrowserContext(browser_context
));
448 content::BrowserContext
* GetBrowserContextToUse(
449 content::BrowserContext
* context
) const override
{
450 return chrome::GetBrowserContextOwnInstanceInIncognito(context
);
455 PrefWatcher
* PrefWatcher::Get(Profile
* profile
) {
456 return PrefWatcherFactory::GetForProfile(profile
);
459 PrefsTabHelper::PrefsTabHelper(WebContents
* contents
)
460 : web_contents_(contents
),
461 profile_(Profile::FromBrowserContext(web_contents_
->GetBrowserContext())),
462 weak_ptr_factory_(this) {
463 PrefService
* prefs
= profile_
->GetPrefs();
465 // If the tab is in an incognito profile, we track changes in the default
466 // zoom level of the parent profile instead.
467 Profile
* profile_to_track
= profile_
->GetOriginalProfile();
468 chrome::ChromeZoomLevelPrefs
* zoom_level_prefs
=
469 profile_to_track
->GetZoomLevelPrefs();
471 base::Closure renderer_callback
= base::Bind(
472 &PrefsTabHelper::UpdateRendererPreferences
, base::Unretained(this));
473 // Tests should not need to create a ZoomLevelPrefs.
474 if (zoom_level_prefs
) {
475 default_zoom_level_subscription_
=
476 zoom_level_prefs
->RegisterDefaultZoomLevelCallback(renderer_callback
);
479 PrefWatcher::Get(profile_
)->RegisterHelper(this);
482 content::RendererPreferences
* render_prefs
=
483 web_contents_
->GetMutableRendererPrefs();
484 renderer_preferences_util::UpdateFromSystemSettings(render_prefs
,
488 #if defined(OS_POSIX) && !defined(OS_MACOSX) && defined(ENABLE_THEMES)
490 chrome::NOTIFICATION_BROWSER_THEME_CHANGED
,
491 content::Source
<ThemeService
>(
492 ThemeServiceFactory::GetForProfile(profile_
)));
494 #if defined(USE_AURA)
496 chrome::NOTIFICATION_BROWSER_FLING_CURVE_PARAMETERS_CHANGED
,
497 content::NotificationService::AllSources());
501 PrefsTabHelper::~PrefsTabHelper() {
502 PrefWatcher::Get(profile_
)->UnregisterHelper(this);
506 void PrefsTabHelper::InitIncognitoUserPrefStore(
507 OverlayUserPrefStore
* pref_store
) {
508 // List of keys that cannot be changed in the user prefs file by the incognito
509 // profile. All preferences that store information about the browsing history
510 // or behavior of the user should have this property.
511 pref_store
->RegisterOverlayPref(prefs::kBrowserWindowPlacement
);
512 pref_store
->RegisterOverlayPref(prefs::kSaveFileDefaultDirectory
);
513 #if defined(OS_ANDROID) || defined(OS_IOS)
514 pref_store
->RegisterOverlayPref(prefs::kProxy
);
515 #endif // defined(OS_ANDROID) || defined(OS_IOS)
519 void PrefsTabHelper::RegisterProfilePrefs(
520 user_prefs::PrefRegistrySyncable
* registry
) {
521 WebPreferences pref_defaults
;
522 registry
->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled
,
523 pref_defaults
.javascript_enabled
);
524 registry
->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled
,
525 pref_defaults
.web_security_enabled
);
526 registry
->RegisterBooleanPref(
527 prefs::kWebKitJavascriptCanOpenWindowsAutomatically
, true);
528 registry
->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically
,
529 pref_defaults
.loads_images_automatically
);
530 registry
->RegisterBooleanPref(prefs::kWebKitPluginsEnabled
,
531 pref_defaults
.plugins_enabled
);
532 registry
->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled
,
533 pref_defaults
.dom_paste_enabled
);
534 registry
->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable
,
535 pref_defaults
.text_areas_are_resizable
);
536 registry
->RegisterBooleanPref(prefs::kWebkitTabsToLinks
,
537 pref_defaults
.tabs_to_links
);
538 registry
->RegisterBooleanPref(prefs::kWebKitAllowRunningInsecureContent
,
540 registry
->RegisterBooleanPref(prefs::kWebKitAllowDisplayingInsecureContent
,
542 registry
->RegisterBooleanPref(prefs::kEnableReferrers
, true);
543 #if defined(OS_ANDROID)
544 registry
->RegisterDoublePref(prefs::kWebKitFontScaleFactor
, 1.0);
545 registry
->RegisterBooleanPref(prefs::kWebKitForceEnableZoom
,
546 pref_defaults
.force_enable_zoom
);
547 registry
->RegisterBooleanPref(prefs::kWebKitPasswordEchoEnabled
,
548 pref_defaults
.password_echo_enabled
);
550 registry
->RegisterStringPref(
551 prefs::kAcceptLanguages
,
552 l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES
),
553 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
554 registry
->RegisterStringPref(
555 prefs::kDefaultCharset
,
556 l10n_util::GetStringUTF8(IDS_DEFAULT_ENCODING
),
557 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
559 // Register font prefs that have defaults.
560 std::set
<std::string
> fonts_with_defaults
;
561 UScriptCode browser_script
= GetScriptOfBrowserLocale();
562 for (size_t i
= 0; i
< kFontDefaultsLength
; ++i
) {
563 FontDefault pref
= kFontDefaults
[i
];
566 if (pref
.pref_name
== prefs::kWebKitFixedFontFamily
) {
567 if (ShouldUseAlternateDefaultFixedFont(
568 l10n_util::GetStringUTF8(pref
.resource_id
)))
569 pref
.resource_id
= IDS_FIXED_FONT_FAMILY_ALT_WIN
;
573 UScriptCode pref_script
= GetScriptOfFontPref(pref
.pref_name
);
575 // Suppress this default font pref value if it is for the primary script of
576 // the browser's UI locale. For example, if the pref is for the sans-serif
577 // font for the Cyrillic script, and the browser locale is "ru" (Russian),
578 // the default is suppressed. Otherwise, the default would override the
579 // user's font preferences when viewing pages in their native language.
580 // This is because users have no way yet of customizing their per-script
581 // font preferences. The font prefs accessible in the options UI are for
582 // the default, unknown script; these prefs have less priority than the
583 // per-script font prefs when the script of the content is known. This code
584 // can possibly be removed later if users can easily access per-script font
585 // prefs (e.g., via the extensions workflow), or the problem turns out to
586 // not be really critical after all.
587 if (browser_script
!= pref_script
) {
588 registry
->RegisterStringPref(pref
.pref_name
,
589 l10n_util::GetStringUTF8(pref
.resource_id
));
590 fonts_with_defaults
.insert(pref
.pref_name
);
594 // Register per-script font prefs that don't have defaults.
595 #if !defined(OS_ANDROID)
596 RegisterFontFamilyPrefs(registry
, fonts_with_defaults
);
599 RegisterLocalizedFontPref(registry
, prefs::kWebKitDefaultFontSize
,
600 IDS_DEFAULT_FONT_SIZE
);
601 RegisterLocalizedFontPref(registry
, prefs::kWebKitDefaultFixedFontSize
,
602 IDS_DEFAULT_FIXED_FONT_SIZE
);
603 RegisterLocalizedFontPref(registry
, prefs::kWebKitMinimumFontSize
,
604 IDS_MINIMUM_FONT_SIZE
);
605 RegisterLocalizedFontPref(registry
, prefs::kWebKitMinimumLogicalFontSize
,
606 IDS_MINIMUM_LOGICAL_FONT_SIZE
);
607 registry
->RegisterBooleanPref(
608 prefs::kWebKitUsesUniversalDetector
,
609 l10n_util::GetStringUTF8(IDS_USES_UNIVERSAL_DETECTOR
) == "true",
610 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
611 registry
->RegisterStringPref(
612 prefs::kStaticEncodings
,
613 l10n_util::GetStringUTF8(IDS_STATIC_ENCODING_LIST
));
614 registry
->RegisterStringPref(prefs::kRecentlySelectedEncoding
, std::string());
618 void PrefsTabHelper::GetServiceInstance() {
619 PrefWatcherFactory::GetInstance();
622 void PrefsTabHelper::Observe(int type
,
623 const content::NotificationSource
& source
,
624 const content::NotificationDetails
& details
) {
625 #if defined(OS_POSIX) && !defined(OS_MACOSX) && defined(ENABLE_THEMES)
626 if (type
== chrome::NOTIFICATION_BROWSER_THEME_CHANGED
) {
627 UpdateRendererPreferences();
632 #if defined(USE_AURA)
633 if (type
== chrome::NOTIFICATION_BROWSER_FLING_CURVE_PARAMETERS_CHANGED
) {
634 UpdateRendererPreferences();
637 #endif // defined(USE_AURA)
642 void PrefsTabHelper::UpdateWebPreferences() {
643 web_contents_
->GetRenderViewHost()->UpdateWebkitPreferences(
644 web_contents_
->GetRenderViewHost()->GetWebkitPreferences());
647 void PrefsTabHelper::UpdateRendererPreferences() {
648 content::RendererPreferences
* prefs
=
649 web_contents_
->GetMutableRendererPrefs();
650 renderer_preferences_util::UpdateFromSystemSettings(
651 prefs
, profile_
, web_contents_
);
652 web_contents_
->GetRenderViewHost()->SyncRendererPrefs();
654 void PrefsTabHelper::OnFontFamilyPrefChanged(const std::string
& pref_name
) {
655 // When a font family pref's value goes from non-empty to the empty string, we
656 // must add it to the usual WebPreferences struct passed to the renderer.
658 // The empty string means to fall back to the pref for the Common script
659 // ("Zyyy"). For example, if chrome.fonts.serif.Cyrl is the empty string, it
660 // means to use chrome.fonts.serif.Zyyy for Cyrillic script. Prefs that are
661 // the empty string are normally not passed to WebKit, since there are so many
662 // of them that it would cause a performance regression. Not passing the pref
663 // is normally okay since WebKit does the desired fallback behavior regardless
664 // of whether the empty string is passed or the pref is not passed at all. But
665 // if the pref has changed from non-empty to the empty string, we must let
667 std::string generic_family
;
669 if (pref_names_util::ParseFontNamePrefPath(pref_name
,
672 PrefService
* prefs
= profile_
->GetPrefs();
673 std::string pref_value
= prefs
->GetString(pref_name
);
674 if (pref_value
.empty()) {
675 WebPreferences web_prefs
=
676 web_contents_
->GetRenderViewHost()->GetWebkitPreferences();
677 OverrideFontFamily(&web_prefs
, generic_family
, script
, std::string());
678 web_contents_
->GetRenderViewHost()->UpdateWebkitPreferences(web_prefs
);
684 void PrefsTabHelper::OnWebPrefChanged(const std::string
& pref_name
) {
685 #if !defined(OS_ANDROID)
686 OnFontFamilyPrefChanged(pref_name
);
689 web_contents_
->GetRenderViewHost()->OnWebkitPreferencesChanged();