1 // Copyright 2013 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 "content/public/renderer/web_preferences.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "third_party/WebKit/public/platform/WebString.h"
9 #include "third_party/WebKit/public/platform/WebURL.h"
10 #include "third_party/WebKit/public/web/WebKit.h"
11 #include "third_party/WebKit/public/web/WebNetworkStateNotifier.h"
12 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
13 #include "third_party/WebKit/public/web/WebSettings.h"
14 #include "third_party/WebKit/public/web/WebView.h"
15 #include "third_party/icu/source/common/unicode/uchar.h"
16 #include "third_party/icu/source/common/unicode/uscript.h"
17 #include "webkit/common/webpreferences.h"
19 using blink::WebNetworkStateNotifier
;
20 using blink::WebRuntimeFeatures
;
21 using blink::WebSettings
;
22 using blink::WebString
;
30 typedef void (*SetFontFamilyWrapper
)(blink::WebSettings
*,
31 const base::string16
&,
34 void setStandardFontFamilyWrapper(WebSettings
* settings
,
35 const base::string16
& font
,
37 settings
->setStandardFontFamily(font
, script
);
40 void setFixedFontFamilyWrapper(WebSettings
* settings
,
41 const base::string16
& font
,
43 settings
->setFixedFontFamily(font
, script
);
46 void setSerifFontFamilyWrapper(WebSettings
* settings
,
47 const base::string16
& font
,
49 settings
->setSerifFontFamily(font
, script
);
52 void setSansSerifFontFamilyWrapper(WebSettings
* settings
,
53 const base::string16
& font
,
55 settings
->setSansSerifFontFamily(font
, script
);
58 void setCursiveFontFamilyWrapper(WebSettings
* settings
,
59 const base::string16
& font
,
61 settings
->setCursiveFontFamily(font
, script
);
64 void setFantasyFontFamilyWrapper(WebSettings
* settings
,
65 const base::string16
& font
,
67 settings
->setFantasyFontFamily(font
, script
);
70 void setPictographFontFamilyWrapper(WebSettings
* settings
,
71 const base::string16
& font
,
73 settings
->setPictographFontFamily(font
, script
);
76 // If |scriptCode| is a member of a family of "similar" script codes, returns
77 // the script code in that family that is used by WebKit for font selection
78 // purposes. For example, USCRIPT_KATAKANA_OR_HIRAGANA and USCRIPT_JAPANESE are
79 // considered equivalent for the purposes of font selection. WebKit uses the
80 // script code USCRIPT_KATAKANA_OR_HIRAGANA. So, if |scriptCode| is
81 // USCRIPT_JAPANESE, the function returns USCRIPT_KATAKANA_OR_HIRAGANA. WebKit
82 // uses different scripts than the ones in Chrome pref names because the version
83 // of ICU included on certain ports does not have some of the newer scripts. If
84 // |scriptCode| is not a member of such a family, returns |scriptCode|.
85 UScriptCode
GetScriptForWebSettings(UScriptCode scriptCode
) {
87 case USCRIPT_HIRAGANA
:
88 case USCRIPT_KATAKANA
:
89 case USCRIPT_JAPANESE
:
90 return USCRIPT_KATAKANA_OR_HIRAGANA
;
92 return USCRIPT_HANGUL
;
98 void ApplyFontsFromMap(const webkit_glue::ScriptFontFamilyMap
& map
,
99 SetFontFamilyWrapper setter
,
100 WebSettings
* settings
) {
101 for (webkit_glue::ScriptFontFamilyMap::const_iterator it
= map
.begin();
104 int32 script
= u_getPropertyValueEnum(UCHAR_SCRIPT
, (it
->first
).c_str());
105 if (script
>= 0 && script
< USCRIPT_CODE_LIMIT
) {
106 UScriptCode code
= static_cast<UScriptCode
>(script
);
107 (*setter
)(settings
, it
->second
, GetScriptForWebSettings(code
));
114 void ApplyWebPreferences(const WebPreferences
& prefs
, WebView
* web_view
) {
115 WebSettings
* settings
= web_view
->settings();
116 ApplyFontsFromMap(prefs
.standard_font_family_map
,
117 setStandardFontFamilyWrapper
, settings
);
118 ApplyFontsFromMap(prefs
.fixed_font_family_map
,
119 setFixedFontFamilyWrapper
, settings
);
120 ApplyFontsFromMap(prefs
.serif_font_family_map
,
121 setSerifFontFamilyWrapper
, settings
);
122 ApplyFontsFromMap(prefs
.sans_serif_font_family_map
,
123 setSansSerifFontFamilyWrapper
, settings
);
124 ApplyFontsFromMap(prefs
.cursive_font_family_map
,
125 setCursiveFontFamilyWrapper
, settings
);
126 ApplyFontsFromMap(prefs
.fantasy_font_family_map
,
127 setFantasyFontFamilyWrapper
, settings
);
128 ApplyFontsFromMap(prefs
.pictograph_font_family_map
,
129 setPictographFontFamilyWrapper
, settings
);
130 settings
->setDefaultFontSize(prefs
.default_font_size
);
131 settings
->setDefaultFixedFontSize(prefs
.default_fixed_font_size
);
132 settings
->setMinimumFontSize(prefs
.minimum_font_size
);
133 settings
->setMinimumLogicalFontSize(prefs
.minimum_logical_font_size
);
134 settings
->setDefaultTextEncodingName(
135 base::ASCIIToUTF16(prefs
.default_encoding
));
136 settings
->setJavaScriptEnabled(prefs
.javascript_enabled
);
137 settings
->setWebSecurityEnabled(prefs
.web_security_enabled
);
138 settings
->setJavaScriptCanOpenWindowsAutomatically(
139 prefs
.javascript_can_open_windows_automatically
);
140 settings
->setLoadsImagesAutomatically(prefs
.loads_images_automatically
);
141 settings
->setImagesEnabled(prefs
.images_enabled
);
142 settings
->setPluginsEnabled(prefs
.plugins_enabled
);
143 settings
->setDOMPasteAllowed(prefs
.dom_paste_enabled
);
144 settings
->setNeedsSiteSpecificQuirks(prefs
.site_specific_quirks_enabled
);
145 settings
->setShrinksStandaloneImagesToFit(
146 prefs
.shrinks_standalone_images_to_fit
);
147 settings
->setUsesEncodingDetector(prefs
.uses_universal_detector
);
148 settings
->setTextAreasAreResizable(prefs
.text_areas_are_resizable
);
149 settings
->setAllowScriptsToCloseWindows(prefs
.allow_scripts_to_close_windows
);
150 settings
->setDownloadableBinaryFontsEnabled(prefs
.remote_fonts_enabled
);
151 settings
->setJavaScriptCanAccessClipboard(
152 prefs
.javascript_can_access_clipboard
);
153 WebRuntimeFeatures::enableXSLT(prefs
.xslt_enabled
);
154 settings
->setXSSAuditorEnabled(prefs
.xss_auditor_enabled
);
155 settings
->setDNSPrefetchingEnabled(prefs
.dns_prefetching_enabled
);
156 settings
->setLocalStorageEnabled(prefs
.local_storage_enabled
);
157 settings
->setSyncXHRInDocumentsEnabled(prefs
.sync_xhr_in_documents_enabled
);
158 WebRuntimeFeatures::enableDatabase(prefs
.databases_enabled
);
159 settings
->setOfflineWebApplicationCacheEnabled(
160 prefs
.application_cache_enabled
);
161 settings
->setCaretBrowsingEnabled(prefs
.caret_browsing_enabled
);
162 settings
->setHyperlinkAuditingEnabled(prefs
.hyperlink_auditing_enabled
);
163 settings
->setCookieEnabled(prefs
.cookie_enabled
);
164 settings
->setNavigateOnDragDrop(prefs
.navigate_on_drag_drop
);
166 settings
->setJavaEnabled(prefs
.java_enabled
);
168 // By default, allow_universal_access_from_file_urls is set to false and thus
169 // we mitigate attacks from local HTML files by not granting file:// URLs
170 // universal access. Only test shell will enable this.
171 settings
->setAllowUniversalAccessFromFileURLs(
172 prefs
.allow_universal_access_from_file_urls
);
173 settings
->setAllowFileAccessFromFileURLs(
174 prefs
.allow_file_access_from_file_urls
);
176 // Enable the web audio API if requested on the command line.
177 settings
->setWebAudioEnabled(prefs
.webaudio_enabled
);
179 // Enable experimental WebGL support if requested on command line
180 // and support is compiled in.
181 settings
->setExperimentalWebGLEnabled(prefs
.experimental_webgl_enabled
);
183 // Disable GL multisampling if requested on command line.
184 settings
->setOpenGLMultisamplingEnabled(prefs
.gl_multisampling_enabled
);
186 // Enable WebGL errors to the JS console if requested.
187 settings
->setWebGLErrorsToConsoleEnabled(
188 prefs
.webgl_errors_to_console_enabled
);
190 // Uses the mock theme engine for scrollbars.
191 settings
->setMockScrollbarsEnabled(prefs
.mock_scrollbars_enabled
);
193 settings
->setLayerSquashingEnabled(prefs
.layer_squashing_enabled
);
195 // Enable gpu-accelerated compositing always.
196 settings
->setAcceleratedCompositingEnabled(true);
198 // Enable gpu-accelerated 2d canvas if requested on the command line.
199 settings
->setAccelerated2dCanvasEnabled(prefs
.accelerated_2d_canvas_enabled
);
201 settings
->setMinimumAccelerated2dCanvasSize(
202 prefs
.minimum_accelerated_2d_canvas_size
);
204 // Disable antialiasing for 2d canvas if requested on the command line.
205 settings
->setAntialiased2dCanvasEnabled(
206 !prefs
.antialiased_2d_canvas_disabled
);
208 // Set MSAA sample count for 2d canvas if requested on the command line (or
209 // default value if not).
210 settings
->setAccelerated2dCanvasMSAASampleCount(
211 prefs
.accelerated_2d_canvas_msaa_sample_count
);
213 // Enable deferred filter rendering if requested on the command line.
214 settings
->setDeferredFiltersEnabled(prefs
.deferred_filters_enabled
);
216 // Enable container culling if requested on the command line.
217 settings
->setContainerCullingEnabled(prefs
.container_culling_enabled
);
219 // Enable gesture tap highlight if requested on the command line.
220 settings
->setGestureTapHighlightEnabled(prefs
.gesture_tap_highlight_enabled
);
222 // Enabling accelerated layers from the command line enabled accelerated
224 settings
->setAcceleratedCompositingForVideoEnabled(
225 prefs
.accelerated_compositing_for_video_enabled
);
227 // WebGL and accelerated 2D canvas are always gpu composited.
228 settings
->setAcceleratedCompositingForCanvasEnabled(
229 prefs
.experimental_webgl_enabled
|| prefs
.accelerated_2d_canvas_enabled
);
231 settings
->setAsynchronousSpellCheckingEnabled(
232 prefs
.asynchronous_spell_checking_enabled
);
233 settings
->setUnifiedTextCheckerEnabled(prefs
.unified_textchecker_enabled
);
235 for (webkit_glue::WebInspectorPreferences::const_iterator it
=
236 prefs
.inspector_settings
.begin();
237 it
!= prefs
.inspector_settings
.end();
239 web_view
->setInspectorSetting(WebString::fromUTF8(it
->first
),
240 WebString::fromUTF8(it
->second
));
243 // Tabs to link is not part of the settings. WebCore calls
244 // ChromeClient::tabsToLinks which is part of the glue code.
245 web_view
->setTabsToLinks(prefs
.tabs_to_links
);
247 settings
->setAllowDisplayOfInsecureContent(
248 prefs
.allow_displaying_insecure_content
);
249 settings
->setAllowRunningOfInsecureContent(
250 prefs
.allow_running_insecure_content
);
251 settings
->setPasswordEchoEnabled(prefs
.password_echo_enabled
);
252 settings
->setShouldPrintBackgrounds(prefs
.should_print_backgrounds
);
253 settings
->setShouldClearDocumentBackground(
254 prefs
.should_clear_document_background
);
255 settings
->setEnableScrollAnimator(prefs
.enable_scroll_animator
);
257 settings
->setRegionBasedColumnsEnabled(prefs
.region_based_columns_enabled
);
259 WebRuntimeFeatures::enableLazyLayout(prefs
.lazy_layout_enabled
);
260 WebRuntimeFeatures::enableTouch(prefs
.touch_enabled
);
261 settings
->setMaxTouchPoints(prefs
.pointer_events_max_touch_points
);
262 settings
->setDeviceSupportsTouch(prefs
.device_supports_touch
);
263 settings
->setDeviceSupportsMouse(prefs
.device_supports_mouse
);
264 settings
->setEnableTouchAdjustment(prefs
.touch_adjustment_enabled
);
266 settings
->setDeferredImageDecodingEnabled(
267 prefs
.deferred_image_decoding_enabled
);
268 settings
->setShouldRespectImageOrientation(
269 prefs
.should_respect_image_orientation
);
271 settings
->setUnsafePluginPastingEnabled(false);
272 settings
->setEditingBehavior(
273 static_cast<WebSettings::EditingBehavior
>(prefs
.editing_behavior
));
275 settings
->setSupportsMultipleWindows(prefs
.supports_multiple_windows
);
277 settings
->setViewportEnabled(prefs
.viewport_enabled
);
278 settings
->setLoadWithOverviewMode(prefs
.initialize_at_minimum_page_scale
);
279 settings
->setViewportMetaEnabled(prefs
.viewport_meta_enabled
);
280 settings
->setMainFrameResizesAreOrientationChanges(
281 prefs
.main_frame_resizes_are_orientation_changes
);
283 settings
->setSmartInsertDeleteEnabled(prefs
.smart_insert_delete_enabled
);
285 settings
->setSpatialNavigationEnabled(prefs
.spatial_navigation_enabled
);
287 settings
->setSelectionIncludesAltImageText(true);
289 #if defined(OS_ANDROID)
290 settings
->setAllowCustomScrollbarInMainFrame(false);
291 settings
->setTextAutosizingEnabled(prefs
.text_autosizing_enabled
);
292 settings
->setAccessibilityFontScaleFactor(prefs
.font_scale_factor
);
293 settings
->setDeviceScaleAdjustment(prefs
.device_scale_adjustment
);
294 web_view
->setIgnoreViewportTagScaleLimits(prefs
.force_enable_zoom
);
295 settings
->setAutoZoomFocusedNodeToLegibleScale(true);
296 settings
->setDoubleTapToZoomEnabled(prefs
.double_tap_to_zoom_enabled
);
297 settings
->setMediaControlsOverlayPlayButtonEnabled(true);
298 settings
->setMediaPlaybackRequiresUserGesture(
299 prefs
.user_gesture_required_for_media_playback
);
300 settings
->setDefaultVideoPosterURL(
301 base::ASCIIToUTF16(prefs
.default_video_poster_url
.spec()));
302 settings
->setSupportDeprecatedTargetDensityDPI(
303 prefs
.support_deprecated_target_density_dpi
);
304 settings
->setUseLegacyBackgroundSizeShorthandBehavior(
305 prefs
.use_legacy_background_size_shorthand_behavior
);
306 settings
->setWideViewportQuirkEnabled(prefs
.wide_viewport_quirk
);
307 settings
->setUseWideViewport(prefs
.use_wide_viewport
);
308 settings
->setViewportMetaLayoutSizeQuirk(
309 prefs
.viewport_meta_layout_size_quirk
);
310 settings
->setViewportMetaMergeContentQuirk(
311 prefs
.viewport_meta_merge_content_quirk
);
312 settings
->setViewportMetaNonUserScalableQuirk(
313 prefs
.viewport_meta_non_user_scalable_quirk
);
314 settings
->setViewportMetaZeroValuesQuirk(
315 prefs
.viewport_meta_zero_values_quirk
);
316 settings
->setClobberUserAgentInitialScaleQuirk(
317 prefs
.clobber_user_agent_initial_scale_quirk
);
318 settings
->setIgnoreMainFrameOverflowHiddenQuirk(
319 prefs
.ignore_main_frame_overflow_hidden_quirk
);
320 settings
->setReportScreenSizeInPhysicalPixelsQuirk(
321 prefs
.report_screen_size_in_physical_pixels_quirk
);
322 settings
->setMainFrameClipsContent(false);
323 settings
->setShrinksStandaloneImagesToFit(false);
324 settings
->setShrinksViewportContentToFit(true);
327 WebNetworkStateNotifier::setOnLine(prefs
.is_online
);
328 settings
->setPinchVirtualViewportEnabled(
329 prefs
.pinch_virtual_viewport_enabled
);
331 settings
->setPinchOverlayScrollbarThickness(
332 prefs
.pinch_overlay_scrollbar_thickness
);
333 settings
->setUseSolidColorScrollbars(prefs
.use_solid_color_scrollbars
);
334 settings
->setCompositorTouchHitTesting(prefs
.compositor_touch_hit_testing
);
337 } // namespace content