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.
4
5#include "webkit/glue/webpreferences.h"
6
7#include "base/string_util.h"
8#include "base/utf_string_conversions.h"
9#include "third_party/WebKit/Source/WebKit/chromium/public/WebRuntimeFeatures.h"
10#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
11#include "third_party/WebKit/Source/WebKit/chromium/public/WebSettings.h"
12#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
13#include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h"
14#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
15#include "webkit/glue/webkit_glue.h"
16
17using WebKit::WebRuntimeFeatures;
18using WebKit::WebSettings;
19using WebKit::WebString;
20using WebKit::WebURL;
21using WebKit::WebView;
22
23WebPreferences::WebPreferences()
24    : standard_font_family(ASCIIToUTF16("Times New Roman")),
25      fixed_font_family(ASCIIToUTF16("Courier New")),
26      serif_font_family(ASCIIToUTF16("Times New Roman")),
27      sans_serif_font_family(ASCIIToUTF16("Arial")),
28      cursive_font_family(ASCIIToUTF16("Script")),
29      fantasy_font_family(),  // Not sure what to use on Windows.
30      default_font_size(16),
31      default_fixed_font_size(13),
32      minimum_font_size(0),
33      minimum_logical_font_size(6),
34      default_encoding("ISO-8859-1"),
35      javascript_enabled(true),
36      web_security_enabled(true),
37      javascript_can_open_windows_automatically(true),
38      loads_images_automatically(true),
39      plugins_enabled(true),
40      dom_paste_enabled(false),  // enables execCommand("paste")
41      developer_extras_enabled(false),  // Requires extra work by embedder
42      site_specific_quirks_enabled(false),
43      shrinks_standalone_images_to_fit(true),
44      uses_universal_detector(false),  // Disabled: page cycler regression
45      text_areas_are_resizable(true),
46      java_enabled(true),
47      allow_scripts_to_close_windows(false),
48      uses_page_cache(false),
49      remote_fonts_enabled(true),
50      javascript_can_access_clipboard(false),
51      xss_auditor_enabled(false),
52      local_storage_enabled(false),
53      databases_enabled(false),
54      application_cache_enabled(false),
55      tabs_to_links(true),
56      caret_browsing_enabled(false),
57      hyperlink_auditing_enabled(true),
58      user_style_sheet_enabled(false),
59      author_and_user_styles_enabled(true),
60      frame_flattening_enabled(false),
61      allow_universal_access_from_file_urls(false),
62      allow_file_access_from_file_urls(false),
63      webaudio_enabled(false),
64      experimental_webgl_enabled(false),
65      gl_multisampling_enabled(true),
66      show_composited_layer_borders(false),
67      show_composited_layer_tree(false),
68      show_fps_counter(false),
69      asynchronous_spell_checking_enabled(true),
70      accelerated_compositing_enabled(false),
71      force_compositing_mode(false),
72      composite_to_texture_enabled(false),
73      accelerated_layers_enabled(false),
74      accelerated_video_enabled(false),
75      accelerated_2d_canvas_enabled(false),
76      accelerated_drawing_enabled(false),
77      accelerated_plugins_enabled(false),
78      memory_info_enabled(false),
79      interactive_form_validation_enabled(true),
80      fullscreen_enabled(false) {
81}
82
83WebPreferences::~WebPreferences() {
84}
85
86void WebPreferences::Apply(WebView* web_view) const {
87  WebSettings* settings = web_view->settings();
88  settings->setStandardFontFamily(standard_font_family);
89  settings->setFixedFontFamily(fixed_font_family);
90  settings->setSerifFontFamily(serif_font_family);
91  settings->setSansSerifFontFamily(sans_serif_font_family);
92  settings->setCursiveFontFamily(cursive_font_family);
93  settings->setFantasyFontFamily(fantasy_font_family);
94  settings->setDefaultFontSize(default_font_size);
95  settings->setDefaultFixedFontSize(default_fixed_font_size);
96  settings->setMinimumFontSize(minimum_font_size);
97  settings->setMinimumLogicalFontSize(minimum_logical_font_size);
98  settings->setDefaultTextEncodingName(ASCIIToUTF16(default_encoding));
99  settings->setJavaScriptEnabled(javascript_enabled);
100  settings->setWebSecurityEnabled(web_security_enabled);
101  settings->setJavaScriptCanOpenWindowsAutomatically(
102      javascript_can_open_windows_automatically);
103  settings->setLoadsImagesAutomatically(loads_images_automatically);
104  settings->setPluginsEnabled(plugins_enabled);
105  settings->setDOMPasteAllowed(dom_paste_enabled);
106  settings->setDeveloperExtrasEnabled(developer_extras_enabled);
107  settings->setNeedsSiteSpecificQuirks(site_specific_quirks_enabled);
108  settings->setShrinksStandaloneImagesToFit(shrinks_standalone_images_to_fit);
109  settings->setUsesEncodingDetector(uses_universal_detector);
110  settings->setTextAreasAreResizable(text_areas_are_resizable);
111  settings->setAllowScriptsToCloseWindows(allow_scripts_to_close_windows);
112  if (user_style_sheet_enabled)
113    settings->setUserStyleSheetLocation(user_style_sheet_location);
114  else
115    settings->setUserStyleSheetLocation(WebURL());
116  settings->setAuthorAndUserStylesEnabled(author_and_user_styles_enabled);
117  settings->setUsesPageCache(uses_page_cache);
118  settings->setDownloadableBinaryFontsEnabled(remote_fonts_enabled);
119  settings->setJavaScriptCanAccessClipboard(javascript_can_access_clipboard);
120  settings->setXSSAuditorEnabled(xss_auditor_enabled);
121  settings->setLocalStorageEnabled(local_storage_enabled);
122  WebRuntimeFeatures::enableDatabase(
123      WebRuntimeFeatures::isDatabaseEnabled() || databases_enabled);
124  settings->setOfflineWebApplicationCacheEnabled(application_cache_enabled);
125  settings->setCaretBrowsingEnabled(caret_browsing_enabled);
126  settings->setHyperlinkAuditingEnabled(hyperlink_auditing_enabled);
127
128  // This setting affects the behavior of links in an editable region:
129  // clicking the link should select it rather than navigate to it.
130  // Safari uses the same default. It is unlikley an embedder would want to
131  // change this, since it would break existing rich text editors.
132  settings->setEditableLinkBehaviorNeverLive();
133
134  settings->setFrameFlatteningEnabled(frame_flattening_enabled);
135
136  settings->setFontRenderingModeNormal();
137  settings->setJavaEnabled(java_enabled);
138
139  // Turn this on to cause WebCore to paint the resize corner for us.
140  settings->setShouldPaintCustomScrollbars(true);
141
142  // By default, allow_universal_access_from_file_urls is set to false and thus
143  // we mitigate attacks from local HTML files by not granting file:// URLs
144  // universal access. Only test shell will enable this.
145  settings->setAllowUniversalAccessFromFileURLs(
146      allow_universal_access_from_file_urls);
147  settings->setAllowFileAccessFromFileURLs(allow_file_access_from_file_urls);
148
149  // We prevent WebKit from checking if it needs to add a "text direction"
150  // submenu to a context menu. it is not only because we don't need the result
151  // but also because it cause a possible crash in Editor::hasBidiSelection().
152  settings->setTextDirectionSubmenuInclusionBehaviorNeverIncluded();
153
154  // Enable the web audio API if requested on the command line.
155  settings->setWebAudioEnabled(webaudio_enabled);
156
157  // Enable experimental WebGL support if requested on command line
158  // and support is compiled in.
159  settings->setExperimentalWebGLEnabled(experimental_webgl_enabled);
160
161  // Disable GL multisampling if requested on command line.
162  settings->setOpenGLMultisamplingEnabled(gl_multisampling_enabled);
163
164  // Display colored borders around composited render layers if requested
165  // on command line.
166  settings->setShowDebugBorders(show_composited_layer_borders);
167
168  // Display an FPS indicator if requested on the command line.
169  settings->setShowFPSCounter(show_fps_counter);
170
171  // Display the current compositor tree as overlay if requested on
172  // the command line
173  settings->setShowPlatformLayerTree(show_composited_layer_tree);
174
175  // Enable gpu-accelerated compositing if requested on the command line.
176  settings->setAcceleratedCompositingEnabled(accelerated_compositing_enabled);
177
178  // Always enter compositing if requested on the command line.
179  settings->setForceCompositingMode(force_compositing_mode);
180
181  // Enable composite to offscreen texture if requested on the command line.
182  settings->setCompositeToTextureEnabled(composite_to_texture_enabled);
183
184  // Enable gpu-accelerated 2d canvas if requested on the command line.
185  settings->setAccelerated2dCanvasEnabled(accelerated_2d_canvas_enabled);
186
187  // Enable gpu-accelerated drawing if requested on the command line.
188  settings->setAcceleratedDrawingEnabled(accelerated_drawing_enabled);
189
190  // Enabling accelerated layers from the command line enabled accelerated
191  // 3D CSS, Video, and Animations.
192  settings->setAcceleratedCompositingFor3DTransformsEnabled(
193      accelerated_layers_enabled);
194  settings->setAcceleratedCompositingForVideoEnabled(
195      accelerated_video_enabled);
196  settings->setAcceleratedCompositingForAnimationEnabled(
197      accelerated_layers_enabled);
198
199  // Enabling accelerated plugins if specified from the command line.
200  settings->setAcceleratedCompositingForPluginsEnabled(
201      accelerated_plugins_enabled);
202
203  // WebGL and accelerated 2D canvas are always gpu composited.
204  settings->setAcceleratedCompositingForCanvasEnabled(
205      experimental_webgl_enabled || accelerated_2d_canvas_enabled);
206
207  // Enable memory info reporting to page if requested on the command line.
208  settings->setMemoryInfoEnabled(memory_info_enabled);
209
210  settings->setAsynchronousSpellCheckingEnabled(
211      asynchronous_spell_checking_enabled);
212
213  for (WebInspectorPreferences::const_iterator it = inspector_settings.begin();
214       it != inspector_settings.end(); ++it)
215    web_view->setInspectorSetting(WebString::fromUTF8(it->first),
216                                  WebString::fromUTF8(it->second));
217
218  // Tabs to link is not part of the settings. WebCore calls
219  // ChromeClient::tabsToLinks which is part of the glue code.
220  web_view->setTabsToLinks(tabs_to_links);
221
222  settings->setInteractiveFormValidationEnabled(
223      interactive_form_validation_enabled);
224
225  settings->setFullScreenEnabled(fullscreen_enabled);
226}
227