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/string_util.h"
8 #include "base/utf_string_conversions.h"
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNetworkStateNotifier.h"
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRuntimeFeatures.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSettings.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
17 #include "unicode/uchar.h"
18 #include "webkit/glue/webkit_glue.h"
20 using WebKit::WebNetworkStateNotifier
;
21 using WebKit::WebRuntimeFeatures
;
22 using WebKit::WebSettings
;
23 using WebKit::WebSize
;
24 using WebKit::WebString
;
26 using WebKit::WebView
;
28 namespace webkit_glue
{
30 // "Zyyy" is the ISO 15924 script code for undetermined script aka Common.
31 const char WebPreferences::kCommonScript
[] = "Zyyy";
33 WebPreferences::WebPreferences()
34 : default_font_size(16),
35 default_fixed_font_size(13),
37 minimum_logical_font_size(6),
38 default_encoding("ISO-8859-1"),
39 apply_default_device_scale_factor_in_compositor(false),
40 javascript_enabled(true),
41 web_security_enabled(true),
42 javascript_can_open_windows_automatically(true),
43 loads_images_automatically(true),
45 plugins_enabled(true),
46 dom_paste_enabled(false), // enables execCommand("paste")
47 developer_extras_enabled(false), // Requires extra work by embedder
48 site_specific_quirks_enabled(false),
49 shrinks_standalone_images_to_fit(true),
50 uses_universal_detector(false), // Disabled: page cycler regression
51 text_areas_are_resizable(true),
53 allow_scripts_to_close_windows(false),
54 uses_page_cache(false),
55 remote_fonts_enabled(true),
56 javascript_can_access_clipboard(false),
57 xss_auditor_enabled(true),
58 dns_prefetching_enabled(true),
59 local_storage_enabled(false),
60 databases_enabled(false),
61 application_cache_enabled(false),
63 caret_browsing_enabled(false),
64 hyperlink_auditing_enabled(true),
66 user_style_sheet_enabled(false),
67 author_and_user_styles_enabled(true),
68 frame_flattening_enabled(false),
69 allow_universal_access_from_file_urls(false),
70 allow_file_access_from_file_urls(false),
71 webaudio_enabled(false),
72 experimental_webgl_enabled(false),
73 flash_3d_enabled(true),
74 flash_stage3d_enabled(false),
75 gl_multisampling_enabled(true),
76 privileged_webgl_extensions_enabled(false),
77 webgl_errors_to_console_enabled(true),
78 show_composited_layer_borders(false),
79 show_composited_layer_tree(false),
80 show_fps_counter(false),
81 show_paint_rects(false),
82 asynchronous_spell_checking_enabled(true),
83 unified_textchecker_enabled(false),
84 accelerated_compositing_enabled(false),
85 force_compositing_mode(false),
86 fixed_position_compositing_enabled(false),
87 accelerated_layers_enabled(false),
88 accelerated_animation_enabled(false),
89 accelerated_video_enabled(false),
90 accelerated_2d_canvas_enabled(false),
91 deferred_2d_canvas_enabled(false),
92 accelerated_painting_enabled(false),
93 accelerated_filters_enabled(false),
94 accelerated_plugins_enabled(false),
95 memory_info_enabled(false),
96 fullscreen_enabled(false),
97 allow_displaying_insecure_content(true),
98 allow_running_insecure_content(false),
99 password_echo_enabled(false),
100 should_print_backgrounds(false),
101 enable_scroll_animator(false),
102 visual_word_movement_enabled(false),
103 css_regions_enabled(false),
104 css_shaders_enabled(false),
105 device_supports_touch(false),
106 device_supports_mouse(true),
107 #if !defined(WEBCOMPOSITOR_OWNS_SETTINGS)
108 threaded_animation_enabled(false),
109 per_tile_painting_enabled(false),
110 partial_swap_enabled(false),
112 default_tile_width(256),
113 default_tile_height(256),
114 max_untiled_layer_width(512),
115 max_untiled_layer_height(512),
116 fixed_position_creates_stacking_context(false),
117 sync_xhr_in_documents_enabled(true),
118 number_of_cpu_cores(1),
119 cookie_enabled(true) {
120 standard_font_family_map
[kCommonScript
] =
121 ASCIIToUTF16("Times New Roman");
122 fixed_font_family_map
[kCommonScript
] =
123 ASCIIToUTF16("Courier New");
124 serif_font_family_map
[kCommonScript
] =
125 ASCIIToUTF16("Times New Roman");
126 sans_serif_font_family_map
[kCommonScript
] =
127 ASCIIToUTF16("Arial");
128 cursive_font_family_map
[kCommonScript
] =
129 ASCIIToUTF16("Script");
130 fantasy_font_family_map
[kCommonScript
] =
131 ASCIIToUTF16("Impact");
134 WebPreferences::~WebPreferences() {
139 void setStandardFontFamilyWrapper(WebSettings
* settings
,
140 const string16
& font
,
141 UScriptCode script
) {
142 settings
->setStandardFontFamily(font
, script
);
145 void setFixedFontFamilyWrapper(WebSettings
* settings
,
146 const string16
& font
,
147 UScriptCode script
) {
148 settings
->setFixedFontFamily(font
, script
);
151 void setSerifFontFamilyWrapper(WebSettings
* settings
,
152 const string16
& font
,
153 UScriptCode script
) {
154 settings
->setSerifFontFamily(font
, script
);
157 void setSansSerifFontFamilyWrapper(WebSettings
* settings
,
158 const string16
& font
,
159 UScriptCode script
) {
160 settings
->setSansSerifFontFamily(font
, script
);
163 void setCursiveFontFamilyWrapper(WebSettings
* settings
,
164 const string16
& font
,
165 UScriptCode script
) {
166 settings
->setCursiveFontFamily(font
, script
);
169 void setFantasyFontFamilyWrapper(WebSettings
* settings
,
170 const string16
& font
,
171 UScriptCode script
) {
172 settings
->setFantasyFontFamily(font
, script
);
175 typedef void (*SetFontFamilyWrapper
)(
176 WebKit::WebSettings
*, const string16
&, UScriptCode
);
178 // If |scriptCode| is a member of a family of "similar" script codes, returns
179 // the script code in that family that is used by WebKit for font selection
180 // purposes. For example, USCRIPT_KATAKANA_OR_HIRAGANA and USCRIPT_JAPANESE are
181 // considered equivalent for the purposes of font selection. WebKit uses the
182 // script code USCRIPT_KATAKANA_OR_HIRAGANA. So, if |scriptCode| is
183 // USCRIPT_JAPANESE, the function returns USCRIPT_KATAKANA_OR_HIRAGANA. WebKit
184 // uses different scripts than the ones in Chrome pref names because the version
185 // of ICU included on certain ports does not have some of the newer scripts. If
186 // |scriptCode| is not a member of such a family, returns |scriptCode|.
187 UScriptCode
GetScriptForWebSettings(UScriptCode scriptCode
) {
188 switch (scriptCode
) {
189 case USCRIPT_HIRAGANA
:
190 case USCRIPT_KATAKANA
:
191 case USCRIPT_JAPANESE
:
192 return USCRIPT_KATAKANA_OR_HIRAGANA
;
194 return USCRIPT_HANGUL
;
200 void ApplyFontsFromMap(const WebPreferences::ScriptFontFamilyMap
& map
,
201 SetFontFamilyWrapper setter
,
202 WebSettings
* settings
) {
203 for (WebPreferences::ScriptFontFamilyMap::const_iterator it
= map
.begin();
204 it
!= map
.end(); ++it
) {
205 int32 script
= u_getPropertyValueEnum(UCHAR_SCRIPT
, (it
->first
).c_str());
206 if (script
>= 0 && script
< USCRIPT_CODE_LIMIT
) {
207 UScriptCode code
= static_cast<UScriptCode
>(script
);
208 (*setter
)(settings
, it
->second
, GetScriptForWebSettings(code
));
215 void WebPreferences::Apply(WebView
* web_view
) const {
216 WebSettings
* settings
= web_view
->settings();
217 ApplyFontsFromMap(standard_font_family_map
, setStandardFontFamilyWrapper
,
219 ApplyFontsFromMap(fixed_font_family_map
, setFixedFontFamilyWrapper
, settings
);
220 ApplyFontsFromMap(serif_font_family_map
, setSerifFontFamilyWrapper
, settings
);
221 ApplyFontsFromMap(sans_serif_font_family_map
, setSansSerifFontFamilyWrapper
,
223 ApplyFontsFromMap(cursive_font_family_map
, setCursiveFontFamilyWrapper
,
225 ApplyFontsFromMap(fantasy_font_family_map
, setFantasyFontFamilyWrapper
,
227 settings
->setDefaultFontSize(default_font_size
);
228 settings
->setDefaultFixedFontSize(default_fixed_font_size
);
229 settings
->setMinimumFontSize(minimum_font_size
);
230 settings
->setMinimumLogicalFontSize(minimum_logical_font_size
);
231 settings
->setDefaultTextEncodingName(ASCIIToUTF16(default_encoding
));
232 settings
->setApplyDefaultDeviceScaleFactorInCompositor(
233 apply_default_device_scale_factor_in_compositor
);
234 settings
->setJavaScriptEnabled(javascript_enabled
);
235 settings
->setWebSecurityEnabled(web_security_enabled
);
236 settings
->setJavaScriptCanOpenWindowsAutomatically(
237 javascript_can_open_windows_automatically
);
238 settings
->setLoadsImagesAutomatically(loads_images_automatically
);
239 settings
->setImagesEnabled(images_enabled
);
240 settings
->setPluginsEnabled(plugins_enabled
);
241 settings
->setDOMPasteAllowed(dom_paste_enabled
);
242 settings
->setDeveloperExtrasEnabled(developer_extras_enabled
);
243 settings
->setNeedsSiteSpecificQuirks(site_specific_quirks_enabled
);
244 settings
->setShrinksStandaloneImagesToFit(shrinks_standalone_images_to_fit
);
245 settings
->setUsesEncodingDetector(uses_universal_detector
);
246 settings
->setTextAreasAreResizable(text_areas_are_resizable
);
247 settings
->setAllowScriptsToCloseWindows(allow_scripts_to_close_windows
);
248 if (user_style_sheet_enabled
)
249 settings
->setUserStyleSheetLocation(user_style_sheet_location
);
251 settings
->setUserStyleSheetLocation(WebURL());
252 settings
->setAuthorAndUserStylesEnabled(author_and_user_styles_enabled
);
253 settings
->setUsesPageCache(uses_page_cache
);
254 settings
->setDownloadableBinaryFontsEnabled(remote_fonts_enabled
);
255 settings
->setJavaScriptCanAccessClipboard(javascript_can_access_clipboard
);
256 settings
->setXSSAuditorEnabled(xss_auditor_enabled
);
257 settings
->setDNSPrefetchingEnabled(dns_prefetching_enabled
);
258 settings
->setLocalStorageEnabled(local_storage_enabled
);
259 settings
->setSyncXHRInDocumentsEnabled(sync_xhr_in_documents_enabled
);
260 WebRuntimeFeatures::enableDatabase(databases_enabled
);
261 settings
->setOfflineWebApplicationCacheEnabled(application_cache_enabled
);
262 settings
->setCaretBrowsingEnabled(caret_browsing_enabled
);
263 settings
->setHyperlinkAuditingEnabled(hyperlink_auditing_enabled
);
264 settings
->setCookieEnabled(cookie_enabled
);
266 // This setting affects the behavior of links in an editable region:
267 // clicking the link should select it rather than navigate to it.
268 // Safari uses the same default. It is unlikley an embedder would want to
269 // change this, since it would break existing rich text editors.
270 settings
->setEditableLinkBehaviorNeverLive();
272 settings
->setFrameFlatteningEnabled(frame_flattening_enabled
);
274 settings
->setFontRenderingModeNormal();
275 settings
->setJavaEnabled(java_enabled
);
277 // By default, allow_universal_access_from_file_urls is set to false and thus
278 // we mitigate attacks from local HTML files by not granting file:// URLs
279 // universal access. Only test shell will enable this.
280 settings
->setAllowUniversalAccessFromFileURLs(
281 allow_universal_access_from_file_urls
);
282 settings
->setAllowFileAccessFromFileURLs(allow_file_access_from_file_urls
);
284 // We prevent WebKit from checking if it needs to add a "text direction"
285 // submenu to a context menu. it is not only because we don't need the result
286 // but also because it cause a possible crash in Editor::hasBidiSelection().
287 settings
->setTextDirectionSubmenuInclusionBehaviorNeverIncluded();
289 // Enable the web audio API if requested on the command line.
290 settings
->setWebAudioEnabled(webaudio_enabled
);
292 // Enable experimental WebGL support if requested on command line
293 // and support is compiled in.
294 settings
->setExperimentalWebGLEnabled(experimental_webgl_enabled
);
296 // Disable GL multisampling if requested on command line.
297 settings
->setOpenGLMultisamplingEnabled(gl_multisampling_enabled
);
299 // Enable privileged WebGL extensions for Chrome extensions or if requested
301 settings
->setPrivilegedWebGLExtensionsEnabled(
302 privileged_webgl_extensions_enabled
);
304 // Enable WebGL errors to the JS console if requested.
305 settings
->setWebGLErrorsToConsoleEnabled(webgl_errors_to_console_enabled
);
307 // Display colored borders around composited render layers if requested
309 settings
->setShowDebugBorders(show_composited_layer_borders
);
311 // Display an FPS indicator if requested on the command line.
312 settings
->setShowFPSCounter(show_fps_counter
);
314 // Display the current compositor tree as overlay if requested on
316 settings
->setShowPlatformLayerTree(show_composited_layer_tree
);
318 // Display visualization of what has changed on the screen using an
319 // overlay of rects, if requested on the command line.
320 settings
->setShowPaintRects(show_paint_rects
);
322 // Enable gpu-accelerated compositing if requested on the command line.
323 settings
->setAcceleratedCompositingEnabled(accelerated_compositing_enabled
);
325 // Always enter compositing if requested on the command line.
326 settings
->setForceCompositingMode(force_compositing_mode
);
328 // Enable compositing for fixed position elements if requested
329 // on the command line.
330 settings
->setAcceleratedCompositingForFixedPositionEnabled(
331 fixed_position_compositing_enabled
);
333 // Enable gpu-accelerated 2d canvas if requested on the command line.
334 settings
->setAccelerated2dCanvasEnabled(accelerated_2d_canvas_enabled
);
336 // Enable deferred 2d canvas if requested on the command line.
337 settings
->setDeferred2dCanvasEnabled(deferred_2d_canvas_enabled
);
339 // Enable gpu-accelerated painting if requested on the command line.
340 settings
->setAcceleratedPaintingEnabled(accelerated_painting_enabled
);
342 // Enable gpu-accelerated filters if requested on the command line.
343 settings
->setAcceleratedFiltersEnabled(accelerated_filters_enabled
);
345 // Enabling accelerated layers from the command line enabled accelerated
346 // 3D CSS, Video, and Animations.
347 settings
->setAcceleratedCompositingFor3DTransformsEnabled(
348 accelerated_layers_enabled
);
349 settings
->setAcceleratedCompositingForVideoEnabled(
350 accelerated_video_enabled
);
351 settings
->setAcceleratedCompositingForAnimationEnabled(
352 accelerated_animation_enabled
);
354 // Enabling accelerated plugins if specified from the command line.
355 settings
->setAcceleratedCompositingForPluginsEnabled(
356 accelerated_plugins_enabled
);
358 // WebGL and accelerated 2D canvas are always gpu composited.
359 settings
->setAcceleratedCompositingForCanvasEnabled(
360 experimental_webgl_enabled
|| accelerated_2d_canvas_enabled
);
362 // Enable memory info reporting to page if requested on the command line.
363 settings
->setMemoryInfoEnabled(memory_info_enabled
);
365 settings
->setAsynchronousSpellCheckingEnabled(
366 asynchronous_spell_checking_enabled
);
367 settings
->setUnifiedTextCheckerEnabled(unified_textchecker_enabled
);
369 for (WebInspectorPreferences::const_iterator it
= inspector_settings
.begin();
370 it
!= inspector_settings
.end(); ++it
)
371 web_view
->setInspectorSetting(WebString::fromUTF8(it
->first
),
372 WebString::fromUTF8(it
->second
));
374 // Tabs to link is not part of the settings. WebCore calls
375 // ChromeClient::tabsToLinks which is part of the glue code.
376 web_view
->setTabsToLinks(tabs_to_links
);
378 settings
->setInteractiveFormValidationEnabled(true);
380 settings
->setFullScreenEnabled(fullscreen_enabled
);
381 settings
->setAllowDisplayOfInsecureContent(allow_displaying_insecure_content
);
382 settings
->setAllowRunningOfInsecureContent(allow_running_insecure_content
);
383 settings
->setPasswordEchoEnabled(password_echo_enabled
);
384 settings
->setShouldPrintBackgrounds(should_print_backgrounds
);
385 settings
->setEnableScrollAnimator(enable_scroll_animator
);
386 settings
->setVisualWordMovementEnabled(visual_word_movement_enabled
);
388 settings
->setExperimentalCSSRegionsEnabled(css_regions_enabled
);
389 settings
->setExperimentalCSSCustomFilterEnabled(css_shaders_enabled
);
391 settings
->setDeviceSupportsTouch(device_supports_touch
);
392 settings
->setDeviceSupportsMouse(device_supports_mouse
);
394 #if !defined(WEBCOMPOSITOR_OWNS_SETTINGS)
395 settings
->setThreadedAnimationEnabled(threaded_animation_enabled
);
397 // Enable per-tile painting if requested on the command line.
398 settings
->setPerTilePaintingEnabled(per_tile_painting_enabled
);
400 // Enable partial swaps if specified form the command line.
401 settings
->setPartialSwapEnabled(partial_swap_enabled
);
404 settings
->setDefaultTileSize(
405 WebSize(default_tile_width
, default_tile_height
));
406 settings
->setMaxUntiledLayerSize(
407 WebSize(max_untiled_layer_width
, max_untiled_layer_height
));
409 settings
->setFixedPositionCreatesStackingContext(
410 fixed_position_creates_stacking_context
);
412 WebNetworkStateNotifier::setOnLine(is_online
);
415 } // namespace webkit_glue