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 "webkit/glue/webpreferences.h"
7 #include "base/basictypes.h"
8 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h"
10 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h"
11 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
12 #include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNetworkStateNotifier.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRuntimeFeatures.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSettings.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
18 #include "third_party/icu/public/common/unicode/uchar.h"
19 #include "webkit/glue/webkit_glue.h"
21 using WebKit::WebNetworkStateNotifier
;
22 using WebKit::WebRuntimeFeatures
;
23 using WebKit::WebSettings
;
24 using WebKit::WebSize
;
25 using WebKit::WebString
;
27 using WebKit::WebView
;
29 namespace webkit_glue
{
31 // "Zyyy" is the ISO 15924 script code for undetermined script aka Common.
32 const char WebPreferences::kCommonScript
[] = "Zyyy";
34 WebPreferences::WebPreferences()
35 : default_font_size(16),
36 default_fixed_font_size(13),
38 minimum_logical_font_size(6),
39 default_encoding("ISO-8859-1"),
40 apply_default_device_scale_factor_in_compositor(false),
41 apply_page_scale_factor_in_compositor(false),
42 javascript_enabled(true),
43 web_security_enabled(true),
44 javascript_can_open_windows_automatically(true),
45 loads_images_automatically(true),
47 plugins_enabled(true),
48 dom_paste_enabled(false), // enables execCommand("paste")
49 site_specific_quirks_enabled(false),
50 shrinks_standalone_images_to_fit(true),
51 uses_universal_detector(false), // Disabled: page cycler regression
52 text_areas_are_resizable(true),
54 allow_scripts_to_close_windows(false),
55 uses_page_cache(false),
56 page_cache_supports_plugins(false),
57 remote_fonts_enabled(true),
58 javascript_can_access_clipboard(false),
59 xss_auditor_enabled(true),
60 dns_prefetching_enabled(true),
61 local_storage_enabled(false),
62 databases_enabled(false),
63 application_cache_enabled(false),
65 caret_browsing_enabled(false),
66 hyperlink_auditing_enabled(true),
68 user_style_sheet_enabled(false),
69 author_and_user_styles_enabled(true),
70 frame_flattening_enabled(false),
71 allow_universal_access_from_file_urls(false),
72 allow_file_access_from_file_urls(false),
73 webaudio_enabled(false),
74 experimental_webgl_enabled(false),
75 flash_3d_enabled(true),
76 flash_stage3d_enabled(false),
77 flash_stage3d_baseline_enabled(false),
78 gl_multisampling_enabled(true),
79 privileged_webgl_extensions_enabled(false),
80 webgl_errors_to_console_enabled(true),
81 accelerated_compositing_for_overflow_scroll_enabled(false),
82 accelerated_compositing_for_scrollable_frames_enabled(false),
83 composited_scrolling_for_frames_enabled(false),
84 mock_scrollbars_enabled(false),
85 threaded_html_parser(true),
86 show_paint_rects(false),
87 asynchronous_spell_checking_enabled(true),
88 unified_textchecker_enabled(false),
89 accelerated_compositing_enabled(false),
90 force_compositing_mode(false),
91 accelerated_compositing_for_3d_transforms_enabled(false),
92 accelerated_compositing_for_animation_enabled(false),
93 accelerated_compositing_for_video_enabled(false),
94 accelerated_2d_canvas_enabled(false),
95 minimum_accelerated_2d_canvas_size(257 * 256),
96 antialiased_2d_canvas_disabled(false),
97 accelerated_filters_enabled(false),
98 gesture_tap_highlight_enabled(false),
99 accelerated_compositing_for_plugins_enabled(false),
100 memory_info_enabled(false),
101 fullscreen_enabled(false),
102 allow_displaying_insecure_content(true),
103 allow_running_insecure_content(false),
104 password_echo_enabled(false),
105 should_print_backgrounds(false),
106 enable_scroll_animator(false),
107 visual_word_movement_enabled(false),
108 css_sticky_position_enabled(false),
109 css_shaders_enabled(false),
110 css_variables_enabled(false),
111 css_grid_layout_enabled(false),
112 touch_enabled(false),
113 device_supports_touch(false),
114 device_supports_mouse(true),
115 touch_adjustment_enabled(true),
116 touch_drag_drop_enabled(false),
117 fixed_position_creates_stacking_context(false),
118 sync_xhr_in_documents_enabled(true),
119 deferred_image_decoding_enabled(false),
120 should_respect_image_orientation(false),
121 number_of_cpu_cores(1),
122 #if defined(OS_MACOSX)
123 editing_behavior(EDITING_BEHAVIOR_MAC
),
124 #elif defined(OS_WIN)
125 editing_behavior(EDITING_BEHAVIOR_WIN
),
126 #elif defined(OS_ANDROID)
127 editing_behavior(EDITING_BEHAVIOR_ANDROID
),
128 #elif defined(OS_POSIX)
129 editing_behavior(EDITING_BEHAVIOR_UNIX
),
131 editing_behavior(EDITING_BEHAVIOR_MAC
),
133 supports_multiple_windows(true),
134 viewport_enabled(false),
135 initialize_at_minimum_page_scale(true),
136 #if defined(OS_MACOSX)
137 smart_insert_delete_enabled(true),
139 smart_insert_delete_enabled(false),
142 #if defined(OS_ANDROID)
144 text_autosizing_enabled(true),
145 font_scale_factor(1.0f
),
146 force_enable_zoom(false),
147 double_tap_to_zoom_enabled(true),
148 user_gesture_required_for_media_playback(true)
151 standard_font_family_map
[kCommonScript
] =
152 ASCIIToUTF16("Times New Roman");
153 fixed_font_family_map
[kCommonScript
] =
154 ASCIIToUTF16("Courier New");
155 serif_font_family_map
[kCommonScript
] =
156 ASCIIToUTF16("Times New Roman");
157 sans_serif_font_family_map
[kCommonScript
] =
158 ASCIIToUTF16("Arial");
159 cursive_font_family_map
[kCommonScript
] =
160 ASCIIToUTF16("Script");
161 fantasy_font_family_map
[kCommonScript
] =
162 ASCIIToUTF16("Impact");
163 pictograph_font_family_map
[kCommonScript
] =
164 ASCIIToUTF16("Times New Roman");
167 WebPreferences::~WebPreferences() {
172 void setStandardFontFamilyWrapper(WebSettings
* settings
,
173 const base::string16
& font
,
174 UScriptCode script
) {
175 settings
->setStandardFontFamily(font
, script
);
178 void setFixedFontFamilyWrapper(WebSettings
* settings
,
179 const base::string16
& font
,
180 UScriptCode script
) {
181 settings
->setFixedFontFamily(font
, script
);
184 void setSerifFontFamilyWrapper(WebSettings
* settings
,
185 const base::string16
& font
,
186 UScriptCode script
) {
187 settings
->setSerifFontFamily(font
, script
);
190 void setSansSerifFontFamilyWrapper(WebSettings
* settings
,
191 const base::string16
& font
,
192 UScriptCode script
) {
193 settings
->setSansSerifFontFamily(font
, script
);
196 void setCursiveFontFamilyWrapper(WebSettings
* settings
,
197 const base::string16
& font
,
198 UScriptCode script
) {
199 settings
->setCursiveFontFamily(font
, script
);
202 void setFantasyFontFamilyWrapper(WebSettings
* settings
,
203 const base::string16
& font
,
204 UScriptCode script
) {
205 settings
->setFantasyFontFamily(font
, script
);
208 void setPictographFontFamilyWrapper(WebSettings
* settings
,
209 const base::string16
& font
,
210 UScriptCode script
) {
211 settings
->setPictographFontFamily(font
, script
);
214 typedef void (*SetFontFamilyWrapper
)(
215 WebKit::WebSettings
*, const base::string16
&, UScriptCode
);
217 // If |scriptCode| is a member of a family of "similar" script codes, returns
218 // the script code in that family that is used by WebKit for font selection
219 // purposes. For example, USCRIPT_KATAKANA_OR_HIRAGANA and USCRIPT_JAPANESE are
220 // considered equivalent for the purposes of font selection. WebKit uses the
221 // script code USCRIPT_KATAKANA_OR_HIRAGANA. So, if |scriptCode| is
222 // USCRIPT_JAPANESE, the function returns USCRIPT_KATAKANA_OR_HIRAGANA. WebKit
223 // uses different scripts than the ones in Chrome pref names because the version
224 // of ICU included on certain ports does not have some of the newer scripts. If
225 // |scriptCode| is not a member of such a family, returns |scriptCode|.
226 UScriptCode
GetScriptForWebSettings(UScriptCode scriptCode
) {
227 switch (scriptCode
) {
228 case USCRIPT_HIRAGANA
:
229 case USCRIPT_KATAKANA
:
230 case USCRIPT_JAPANESE
:
231 return USCRIPT_KATAKANA_OR_HIRAGANA
;
233 return USCRIPT_HANGUL
;
239 void ApplyFontsFromMap(const WebPreferences::ScriptFontFamilyMap
& map
,
240 SetFontFamilyWrapper setter
,
241 WebSettings
* settings
) {
242 for (WebPreferences::ScriptFontFamilyMap::const_iterator it
= map
.begin();
243 it
!= map
.end(); ++it
) {
244 int32 script
= u_getPropertyValueEnum(UCHAR_SCRIPT
, (it
->first
).c_str());
245 if (script
>= 0 && script
< USCRIPT_CODE_LIMIT
) {
246 UScriptCode code
= static_cast<UScriptCode
>(script
);
247 (*setter
)(settings
, it
->second
, GetScriptForWebSettings(code
));
254 void WebPreferences::Apply(WebView
* web_view
) const {
255 WebSettings
* settings
= web_view
->settings();
256 ApplyFontsFromMap(standard_font_family_map
, setStandardFontFamilyWrapper
,
258 ApplyFontsFromMap(fixed_font_family_map
, setFixedFontFamilyWrapper
, settings
);
259 ApplyFontsFromMap(serif_font_family_map
, setSerifFontFamilyWrapper
, settings
);
260 ApplyFontsFromMap(sans_serif_font_family_map
, setSansSerifFontFamilyWrapper
,
262 ApplyFontsFromMap(cursive_font_family_map
, setCursiveFontFamilyWrapper
,
264 ApplyFontsFromMap(fantasy_font_family_map
, setFantasyFontFamilyWrapper
,
266 ApplyFontsFromMap(pictograph_font_family_map
, setPictographFontFamilyWrapper
,
268 settings
->setDefaultFontSize(default_font_size
);
269 settings
->setDefaultFixedFontSize(default_fixed_font_size
);
270 settings
->setMinimumFontSize(minimum_font_size
);
271 settings
->setMinimumLogicalFontSize(minimum_logical_font_size
);
272 settings
->setDefaultTextEncodingName(ASCIIToUTF16(default_encoding
));
273 settings
->setApplyDefaultDeviceScaleFactorInCompositor(
274 apply_default_device_scale_factor_in_compositor
);
275 settings
->setApplyPageScaleFactorInCompositor(
276 apply_page_scale_factor_in_compositor
);
277 settings
->setJavaScriptEnabled(javascript_enabled
);
278 settings
->setWebSecurityEnabled(web_security_enabled
);
279 settings
->setJavaScriptCanOpenWindowsAutomatically(
280 javascript_can_open_windows_automatically
);
281 settings
->setLoadsImagesAutomatically(loads_images_automatically
);
282 settings
->setImagesEnabled(images_enabled
);
283 settings
->setPluginsEnabled(plugins_enabled
);
284 settings
->setDOMPasteAllowed(dom_paste_enabled
);
285 settings
->setNeedsSiteSpecificQuirks(site_specific_quirks_enabled
);
286 settings
->setShrinksStandaloneImagesToFit(shrinks_standalone_images_to_fit
);
287 settings
->setUsesEncodingDetector(uses_universal_detector
);
288 settings
->setTextAreasAreResizable(text_areas_are_resizable
);
289 settings
->setAllowScriptsToCloseWindows(allow_scripts_to_close_windows
);
290 if (user_style_sheet_enabled
)
291 settings
->setUserStyleSheetLocation(user_style_sheet_location
);
293 settings
->setUserStyleSheetLocation(WebURL());
294 settings
->setAuthorAndUserStylesEnabled(author_and_user_styles_enabled
);
295 settings
->setUsesPageCache(uses_page_cache
);
296 settings
->setPageCacheSupportsPlugins(page_cache_supports_plugins
);
297 settings
->setDownloadableBinaryFontsEnabled(remote_fonts_enabled
);
298 settings
->setJavaScriptCanAccessClipboard(javascript_can_access_clipboard
);
299 settings
->setXSSAuditorEnabled(xss_auditor_enabled
);
300 settings
->setDNSPrefetchingEnabled(dns_prefetching_enabled
);
301 settings
->setLocalStorageEnabled(local_storage_enabled
);
302 settings
->setSyncXHRInDocumentsEnabled(sync_xhr_in_documents_enabled
);
303 WebRuntimeFeatures::enableDatabase(databases_enabled
);
304 settings
->setOfflineWebApplicationCacheEnabled(application_cache_enabled
);
305 settings
->setCaretBrowsingEnabled(caret_browsing_enabled
);
306 settings
->setHyperlinkAuditingEnabled(hyperlink_auditing_enabled
);
307 settings
->setCookieEnabled(cookie_enabled
);
309 // This setting affects the behavior of links in an editable region:
310 // clicking the link should select it rather than navigate to it.
311 // Safari uses the same default. It is unlikley an embedder would want to
312 // change this, since it would break existing rich text editors.
313 settings
->setEditableLinkBehaviorNeverLive();
315 settings
->setFrameFlatteningEnabled(frame_flattening_enabled
);
317 settings
->setFontRenderingModeNormal();
318 settings
->setJavaEnabled(java_enabled
);
320 // By default, allow_universal_access_from_file_urls is set to false and thus
321 // we mitigate attacks from local HTML files by not granting file:// URLs
322 // universal access. Only test shell will enable this.
323 settings
->setAllowUniversalAccessFromFileURLs(
324 allow_universal_access_from_file_urls
);
325 settings
->setAllowFileAccessFromFileURLs(allow_file_access_from_file_urls
);
327 // We prevent WebKit from checking if it needs to add a "text direction"
328 // submenu to a context menu. it is not only because we don't need the result
329 // but also because it cause a possible crash in Editor::hasBidiSelection().
330 settings
->setTextDirectionSubmenuInclusionBehaviorNeverIncluded();
332 // Enable the web audio API if requested on the command line.
333 settings
->setWebAudioEnabled(webaudio_enabled
);
335 // Enable experimental WebGL support if requested on command line
336 // and support is compiled in.
337 settings
->setExperimentalWebGLEnabled(experimental_webgl_enabled
);
339 // Disable GL multisampling if requested on command line.
340 settings
->setOpenGLMultisamplingEnabled(gl_multisampling_enabled
);
342 // Enable privileged WebGL extensions for Chrome extensions or if requested
344 settings
->setPrivilegedWebGLExtensionsEnabled(
345 privileged_webgl_extensions_enabled
);
347 // Enable WebGL errors to the JS console if requested.
348 settings
->setWebGLErrorsToConsoleEnabled(webgl_errors_to_console_enabled
);
350 // Enables accelerated compositing for overflow scroll.
351 settings
->setAcceleratedCompositingForOverflowScrollEnabled(
352 accelerated_compositing_for_overflow_scroll_enabled
);
354 // Enables accelerated compositing for scrollable frames if requested on
356 settings
->setAcceleratedCompositingForScrollableFramesEnabled(
357 accelerated_compositing_for_scrollable_frames_enabled
);
359 // Enables composited scrolling for frames if requested on command line.
360 settings
->setCompositedScrollingForFramesEnabled(
361 composited_scrolling_for_frames_enabled
);
363 // Uses the mock theme engine for scrollbars.
364 settings
->setMockScrollbarsEnabled(mock_scrollbars_enabled
);
366 settings
->setThreadedHTMLParser(threaded_html_parser
);
368 // Display visualization of what has changed on the screen using an
369 // overlay of rects, if requested on the command line.
370 settings
->setShowPaintRects(show_paint_rects
);
372 // Enable gpu-accelerated compositing if requested on the command line.
373 settings
->setAcceleratedCompositingEnabled(accelerated_compositing_enabled
);
375 // Enable gpu-accelerated 2d canvas if requested on the command line.
376 settings
->setAccelerated2dCanvasEnabled(accelerated_2d_canvas_enabled
);
378 settings
->setMinimumAccelerated2dCanvasSize(
379 minimum_accelerated_2d_canvas_size
);
381 // Disable antialiasing for 2d canvas if requested on the command line.
382 settings
->setAntialiased2dCanvasEnabled(!antialiased_2d_canvas_disabled
);
384 // Enable gpu-accelerated filters if requested on the command line.
385 settings
->setAcceleratedFiltersEnabled(accelerated_filters_enabled
);
387 // Enable gesture tap highlight if requested on the command line.
388 settings
->setGestureTapHighlightEnabled(gesture_tap_highlight_enabled
);
390 // Enabling accelerated layers from the command line enabled accelerated
391 // 3D CSS, Video, and Animations.
392 settings
->setAcceleratedCompositingFor3DTransformsEnabled(
393 accelerated_compositing_for_3d_transforms_enabled
);
394 settings
->setAcceleratedCompositingForVideoEnabled(
395 accelerated_compositing_for_video_enabled
);
396 settings
->setAcceleratedCompositingForAnimationEnabled(
397 accelerated_compositing_for_animation_enabled
);
399 // Enabling accelerated plugins if specified from the command line.
400 settings
->setAcceleratedCompositingForPluginsEnabled(
401 accelerated_compositing_for_plugins_enabled
);
403 // WebGL and accelerated 2D canvas are always gpu composited.
404 settings
->setAcceleratedCompositingForCanvasEnabled(
405 experimental_webgl_enabled
|| accelerated_2d_canvas_enabled
);
407 // Enable memory info reporting to page if requested on the command line.
408 settings
->setMemoryInfoEnabled(memory_info_enabled
);
410 settings
->setAsynchronousSpellCheckingEnabled(
411 asynchronous_spell_checking_enabled
);
412 settings
->setUnifiedTextCheckerEnabled(unified_textchecker_enabled
);
414 for (WebInspectorPreferences::const_iterator it
= inspector_settings
.begin();
415 it
!= inspector_settings
.end(); ++it
)
416 web_view
->setInspectorSetting(WebString::fromUTF8(it
->first
),
417 WebString::fromUTF8(it
->second
));
419 // Tabs to link is not part of the settings. WebCore calls
420 // ChromeClient::tabsToLinks which is part of the glue code.
421 web_view
->setTabsToLinks(tabs_to_links
);
423 settings
->setInteractiveFormValidationEnabled(true);
425 settings
->setFullScreenEnabled(fullscreen_enabled
);
426 settings
->setAllowDisplayOfInsecureContent(allow_displaying_insecure_content
);
427 settings
->setAllowRunningOfInsecureContent(allow_running_insecure_content
);
428 settings
->setPasswordEchoEnabled(password_echo_enabled
);
429 settings
->setShouldPrintBackgrounds(should_print_backgrounds
);
430 settings
->setEnableScrollAnimator(enable_scroll_animator
);
431 settings
->setVisualWordMovementEnabled(visual_word_movement_enabled
);
433 settings
->setCSSStickyPositionEnabled(css_sticky_position_enabled
);
434 settings
->setExperimentalCSSCustomFilterEnabled(css_shaders_enabled
);
435 settings
->setExperimentalCSSVariablesEnabled(css_variables_enabled
);
436 settings
->setExperimentalCSSGridLayoutEnabled(css_grid_layout_enabled
);
438 WebRuntimeFeatures::enableTouch(touch_enabled
);
439 settings
->setDeviceSupportsTouch(device_supports_touch
);
440 settings
->setDeviceSupportsMouse(device_supports_mouse
);
441 settings
->setEnableTouchAdjustment(touch_adjustment_enabled
);
442 settings
->setTouchDragDropEnabled(touch_drag_drop_enabled
);
444 settings
->setFixedPositionCreatesStackingContext(
445 fixed_position_creates_stacking_context
);
447 settings
->setDeferredImageDecodingEnabled(deferred_image_decoding_enabled
);
448 settings
->setShouldRespectImageOrientation(should_respect_image_orientation
);
450 settings
->setUnsafePluginPastingEnabled(false);
451 settings
->setEditingBehavior(
452 static_cast<WebSettings::EditingBehavior
>(editing_behavior
));
454 settings
->setSupportsMultipleWindows(supports_multiple_windows
);
456 settings
->setViewportEnabled(viewport_enabled
);
457 settings
->setInitializeAtMinimumPageScale(initialize_at_minimum_page_scale
);
459 settings
->setSmartInsertDeleteEnabled(smart_insert_delete_enabled
);
460 settings
->setSelectionIncludesAltImageText(true);
462 #if defined(OS_ANDROID)
463 settings
->setAllowCustomScrollbarInMainFrame(false);
464 settings
->setTextAutosizingEnabled(text_autosizing_enabled
);
465 settings
->setTextAutosizingFontScaleFactor(font_scale_factor
);
466 web_view
->setIgnoreViewportTagMaximumScale(force_enable_zoom
);
467 settings
->setAutoZoomFocusedNodeToLegibleScale(true);
468 settings
->setDoubleTapToZoomEnabled(double_tap_to_zoom_enabled
);
469 settings
->setMediaPlaybackRequiresUserGesture(
470 user_gesture_required_for_media_playback
);
471 settings
->setDefaultVideoPosterURL(
472 ASCIIToUTF16(default_video_poster_url
.spec()));
475 WebNetworkStateNotifier::setOnLine(is_online
);
478 #define COMPILE_ASSERT_MATCHING_ENUMS(webkit_glue_name, webkit_name) \
480 static_cast<int>(webkit_glue_name) == static_cast<int>(webkit_name), \
483 COMPILE_ASSERT_MATCHING_ENUMS(
484 WebPreferences::EDITING_BEHAVIOR_MAC
, WebSettings::EditingBehaviorMac
);
485 COMPILE_ASSERT_MATCHING_ENUMS(
486 WebPreferences::EDITING_BEHAVIOR_WIN
, WebSettings::EditingBehaviorWin
);
487 COMPILE_ASSERT_MATCHING_ENUMS(
488 WebPreferences::EDITING_BEHAVIOR_UNIX
, WebSettings::EditingBehaviorUnix
);
489 COMPILE_ASSERT_MATCHING_ENUMS(
490 WebPreferences::EDITING_BEHAVIOR_ANDROID
,
491 WebSettings::EditingBehaviorAndroid
);
493 } // namespace webkit_glue