1 // Copyright (c) 2011 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 <unicode/uchar.h>
9 #include "base/string_util.h"
10 #include "base/utf_string_conversions.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNetworkStateNotifier.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRuntimeFeatures.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSettings.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
18 #include "webkit/glue/webkit_glue.h"
20 using WebKit::WebNetworkStateNotifier
;
21 using WebKit::WebRuntimeFeatures
;
22 using WebKit::WebSettings
;
23 using WebKit::WebString
;
25 using WebKit::WebView
;
27 WebPreferences::WebPreferences()
28 : standard_font_family(ASCIIToUTF16("Times New Roman")),
29 fixed_font_family(ASCIIToUTF16("Courier New")),
30 serif_font_family(ASCIIToUTF16("Times New Roman")),
31 sans_serif_font_family(ASCIIToUTF16("Arial")),
32 cursive_font_family(ASCIIToUTF16("Script")),
33 fantasy_font_family(), // Not sure what to use on Windows.
34 default_font_size(16),
35 default_fixed_font_size(13),
37 minimum_logical_font_size(6),
38 default_encoding("ISO-8859-1"),
39 javascript_enabled(true),
40 web_security_enabled(true),
41 javascript_can_open_windows_automatically(true),
42 loads_images_automatically(true),
43 plugins_enabled(true),
44 dom_paste_enabled(false), // enables execCommand("paste")
45 developer_extras_enabled(false), // Requires extra work by embedder
46 site_specific_quirks_enabled(false),
47 shrinks_standalone_images_to_fit(true),
48 uses_universal_detector(false), // Disabled: page cycler regression
49 text_areas_are_resizable(true),
51 allow_scripts_to_close_windows(false),
52 uses_page_cache(false),
53 remote_fonts_enabled(true),
54 javascript_can_access_clipboard(false),
55 xss_auditor_enabled(false),
56 dns_prefetching_enabled(true),
57 local_storage_enabled(false),
58 databases_enabled(false),
59 application_cache_enabled(false),
61 caret_browsing_enabled(false),
62 hyperlink_auditing_enabled(true),
64 user_style_sheet_enabled(false),
65 author_and_user_styles_enabled(true),
66 frame_flattening_enabled(false),
67 allow_universal_access_from_file_urls(false),
68 allow_file_access_from_file_urls(false),
69 webaudio_enabled(false),
70 experimental_webgl_enabled(false),
71 gl_multisampling_enabled(true),
72 show_composited_layer_borders(false),
73 show_composited_layer_tree(false),
74 show_fps_counter(false),
75 asynchronous_spell_checking_enabled(true),
76 accelerated_compositing_enabled(false),
77 threaded_compositing_enabled(false),
78 force_compositing_mode(false),
79 allow_webui_compositing(false),
80 composite_to_texture_enabled(false),
81 accelerated_layers_enabled(false),
82 accelerated_video_enabled(false),
83 accelerated_2d_canvas_enabled(false),
84 accelerated_drawing_enabled(false),
85 accelerated_plugins_enabled(false),
86 memory_info_enabled(false),
87 interactive_form_validation_enabled(true),
88 fullscreen_enabled(false),
89 allow_displaying_insecure_content(true),
90 allow_running_insecure_content(false),
91 should_print_backgrounds(false),
92 enable_scroll_animator(false),
93 hixie76_websocket_protocol_enabled(false) {
96 WebPreferences::~WebPreferences() {
101 void setStandardFontFamilyWrapper(WebSettings
* settings
,
102 const string16
& font
,
103 UScriptCode script
) {
104 settings
->setStandardFontFamily(font
, script
);
107 void setFixedFontFamilyWrapper(WebSettings
* settings
,
108 const string16
& font
,
109 UScriptCode script
) {
110 settings
->setFixedFontFamily(font
, script
);
113 void setSerifFontFamilyWrapper(WebSettings
* settings
,
114 const string16
& font
,
115 UScriptCode script
) {
116 settings
->setSerifFontFamily(font
, script
);
119 void setSansSerifFontFamilyWrapper(WebSettings
* settings
,
120 const string16
& font
,
121 UScriptCode script
) {
122 settings
->setSansSerifFontFamily(font
, script
);
125 void setCursiveFontFamilyWrapper(WebSettings
* settings
,
126 const string16
& font
,
127 UScriptCode script
) {
128 settings
->setCursiveFontFamily(font
, script
);
131 void setFantasyFontFamilyWrapper(WebSettings
* settings
,
132 const string16
& font
,
133 UScriptCode script
) {
134 settings
->setFantasyFontFamily(font
, script
);
137 typedef void (*SetFontFamilyWrapper
)(
138 WebKit::WebSettings
*, const string16
&, UScriptCode
);
140 void ApplyFontsFromMap(const WebPreferences::ScriptFontFamilyMap
& map
,
141 SetFontFamilyWrapper setter
,
142 WebSettings
* settings
) {
143 for (WebPreferences::ScriptFontFamilyMap::const_iterator it
= map
.begin();
144 it
!= map
.end(); ++it
) {
145 int32 script
= u_getPropertyValueEnum(UCHAR_SCRIPT
, (it
->first
).c_str());
146 if (script
>= 0 && script
< USCRIPT_CODE_LIMIT
)
147 (*setter
)(settings
, it
->second
, (UScriptCode
) script
);
153 void WebPreferences::Apply(WebView
* web_view
) const {
154 WebSettings
* settings
= web_view
->settings();
155 settings
->setStandardFontFamily(standard_font_family
);
156 settings
->setFixedFontFamily(fixed_font_family
);
157 settings
->setSerifFontFamily(serif_font_family
);
158 settings
->setSansSerifFontFamily(sans_serif_font_family
);
159 settings
->setCursiveFontFamily(cursive_font_family
);
160 settings
->setFantasyFontFamily(fantasy_font_family
);
161 ApplyFontsFromMap(standard_font_family_map
, setStandardFontFamilyWrapper
,
163 ApplyFontsFromMap(fixed_font_family_map
, setFixedFontFamilyWrapper
, settings
);
164 ApplyFontsFromMap(serif_font_family_map
, setSerifFontFamilyWrapper
, settings
);
165 ApplyFontsFromMap(sans_serif_font_family_map
, setSansSerifFontFamilyWrapper
,
167 ApplyFontsFromMap(cursive_font_family_map
, setCursiveFontFamilyWrapper
,
169 ApplyFontsFromMap(fantasy_font_family_map
, setFantasyFontFamilyWrapper
,
171 settings
->setDefaultFontSize(default_font_size
);
172 settings
->setDefaultFixedFontSize(default_fixed_font_size
);
173 settings
->setMinimumFontSize(minimum_font_size
);
174 settings
->setMinimumLogicalFontSize(minimum_logical_font_size
);
175 settings
->setDefaultTextEncodingName(ASCIIToUTF16(default_encoding
));
176 settings
->setJavaScriptEnabled(javascript_enabled
);
177 settings
->setWebSecurityEnabled(web_security_enabled
);
178 settings
->setJavaScriptCanOpenWindowsAutomatically(
179 javascript_can_open_windows_automatically
);
180 settings
->setLoadsImagesAutomatically(loads_images_automatically
);
181 settings
->setPluginsEnabled(plugins_enabled
);
182 settings
->setDOMPasteAllowed(dom_paste_enabled
);
183 settings
->setDeveloperExtrasEnabled(developer_extras_enabled
);
184 settings
->setNeedsSiteSpecificQuirks(site_specific_quirks_enabled
);
185 settings
->setShrinksStandaloneImagesToFit(shrinks_standalone_images_to_fit
);
186 settings
->setUsesEncodingDetector(uses_universal_detector
);
187 settings
->setTextAreasAreResizable(text_areas_are_resizable
);
188 settings
->setAllowScriptsToCloseWindows(allow_scripts_to_close_windows
);
189 if (user_style_sheet_enabled
)
190 settings
->setUserStyleSheetLocation(user_style_sheet_location
);
192 settings
->setUserStyleSheetLocation(WebURL());
193 settings
->setAuthorAndUserStylesEnabled(author_and_user_styles_enabled
);
194 settings
->setUsesPageCache(uses_page_cache
);
195 settings
->setDownloadableBinaryFontsEnabled(remote_fonts_enabled
);
196 settings
->setJavaScriptCanAccessClipboard(javascript_can_access_clipboard
);
197 settings
->setXSSAuditorEnabled(xss_auditor_enabled
);
198 settings
->setDNSPrefetchingEnabled(dns_prefetching_enabled
);
199 settings
->setLocalStorageEnabled(local_storage_enabled
);
200 WebRuntimeFeatures::enableDatabase(
201 WebRuntimeFeatures::isDatabaseEnabled() || databases_enabled
);
202 settings
->setOfflineWebApplicationCacheEnabled(application_cache_enabled
);
203 settings
->setCaretBrowsingEnabled(caret_browsing_enabled
);
204 settings
->setHyperlinkAuditingEnabled(hyperlink_auditing_enabled
);
206 // This setting affects the behavior of links in an editable region:
207 // clicking the link should select it rather than navigate to it.
208 // Safari uses the same default. It is unlikley an embedder would want to
209 // change this, since it would break existing rich text editors.
210 settings
->setEditableLinkBehaviorNeverLive();
212 settings
->setFrameFlatteningEnabled(frame_flattening_enabled
);
214 settings
->setFontRenderingModeNormal();
215 settings
->setJavaEnabled(java_enabled
);
217 // By default, allow_universal_access_from_file_urls is set to false and thus
218 // we mitigate attacks from local HTML files by not granting file:// URLs
219 // universal access. Only test shell will enable this.
220 settings
->setAllowUniversalAccessFromFileURLs(
221 allow_universal_access_from_file_urls
);
222 settings
->setAllowFileAccessFromFileURLs(allow_file_access_from_file_urls
);
224 // We prevent WebKit from checking if it needs to add a "text direction"
225 // submenu to a context menu. it is not only because we don't need the result
226 // but also because it cause a possible crash in Editor::hasBidiSelection().
227 settings
->setTextDirectionSubmenuInclusionBehaviorNeverIncluded();
229 // Enable the web audio API if requested on the command line.
230 settings
->setWebAudioEnabled(webaudio_enabled
);
232 // Enable experimental WebGL support if requested on command line
233 // and support is compiled in.
234 settings
->setExperimentalWebGLEnabled(experimental_webgl_enabled
);
236 // Disable GL multisampling if requested on command line.
237 settings
->setOpenGLMultisamplingEnabled(gl_multisampling_enabled
);
239 // Display colored borders around composited render layers if requested
241 settings
->setShowDebugBorders(show_composited_layer_borders
);
243 // Display an FPS indicator if requested on the command line.
244 settings
->setShowFPSCounter(show_fps_counter
);
246 // Display the current compositor tree as overlay if requested on
248 settings
->setShowPlatformLayerTree(show_composited_layer_tree
);
250 // Enable gpu-accelerated compositing if requested on the command line.
251 settings
->setAcceleratedCompositingEnabled(accelerated_compositing_enabled
);
253 settings
->setUseThreadedCompositor(threaded_compositing_enabled
);
255 // Always enter compositing if requested on the command line.
256 settings
->setForceCompositingMode(force_compositing_mode
);
258 // Enable composite to offscreen texture if requested on the command line.
259 settings
->setCompositeToTextureEnabled(composite_to_texture_enabled
);
261 // Enable gpu-accelerated 2d canvas if requested on the command line.
262 settings
->setAccelerated2dCanvasEnabled(accelerated_2d_canvas_enabled
);
264 // Enable gpu-accelerated drawing if requested on the command line.
265 settings
->setAcceleratedDrawingEnabled(accelerated_drawing_enabled
);
267 // Enabling accelerated layers from the command line enabled accelerated
268 // 3D CSS, Video, and Animations.
269 settings
->setAcceleratedCompositingFor3DTransformsEnabled(
270 accelerated_layers_enabled
);
271 settings
->setAcceleratedCompositingForVideoEnabled(
272 accelerated_video_enabled
);
273 settings
->setAcceleratedCompositingForAnimationEnabled(
274 accelerated_layers_enabled
);
276 // Enabling accelerated plugins if specified from the command line.
277 settings
->setAcceleratedCompositingForPluginsEnabled(
278 accelerated_plugins_enabled
);
280 // WebGL and accelerated 2D canvas are always gpu composited.
281 settings
->setAcceleratedCompositingForCanvasEnabled(
282 experimental_webgl_enabled
|| accelerated_2d_canvas_enabled
);
284 // Enable memory info reporting to page if requested on the command line.
285 settings
->setMemoryInfoEnabled(memory_info_enabled
);
287 settings
->setAsynchronousSpellCheckingEnabled(
288 asynchronous_spell_checking_enabled
);
290 for (WebInspectorPreferences::const_iterator it
= inspector_settings
.begin();
291 it
!= inspector_settings
.end(); ++it
)
292 web_view
->setInspectorSetting(WebString::fromUTF8(it
->first
),
293 WebString::fromUTF8(it
->second
));
295 // Tabs to link is not part of the settings. WebCore calls
296 // ChromeClient::tabsToLinks which is part of the glue code.
297 web_view
->setTabsToLinks(tabs_to_links
);
299 settings
->setInteractiveFormValidationEnabled(
300 interactive_form_validation_enabled
);
302 settings
->setFullScreenEnabled(fullscreen_enabled
);
303 settings
->setAllowDisplayOfInsecureContent(allow_displaying_insecure_content
);
304 settings
->setAllowRunningOfInsecureContent(allow_running_insecure_content
);
305 settings
->setShouldPrintBackgrounds(should_print_backgrounds
);
306 settings
->setEnableScrollAnimator(enable_scroll_animator
);
307 settings
->setHixie76WebSocketProtocolEnabled(
308 hixie76_websocket_protocol_enabled
);
310 WebNetworkStateNotifier::setOnLine(is_online
);