33882e388bd3d326b748f3b7121f663af07040f3
[platform/framework/web/crosswalk.git] / src / content / renderer / web_preferences.cc
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.
4
5 #include "content/public/renderer/web_preferences.h"
6
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"
18
19 using blink::WebNetworkStateNotifier;
20 using blink::WebRuntimeFeatures;
21 using blink::WebSettings;
22 using blink::WebString;
23 using blink::WebURL;
24 using blink::WebView;
25
26 namespace content {
27
28 namespace {
29
30 typedef void (*SetFontFamilyWrapper)(blink::WebSettings*,
31                                      const base::string16&,
32                                      UScriptCode);
33
34 void setStandardFontFamilyWrapper(WebSettings* settings,
35                                   const base::string16& font,
36                                   UScriptCode script) {
37   settings->setStandardFontFamily(font, script);
38 }
39
40 void setFixedFontFamilyWrapper(WebSettings* settings,
41                                const base::string16& font,
42                                UScriptCode script) {
43   settings->setFixedFontFamily(font, script);
44 }
45
46 void setSerifFontFamilyWrapper(WebSettings* settings,
47                                const base::string16& font,
48                                UScriptCode script) {
49   settings->setSerifFontFamily(font, script);
50 }
51
52 void setSansSerifFontFamilyWrapper(WebSettings* settings,
53                                    const base::string16& font,
54                                    UScriptCode script) {
55   settings->setSansSerifFontFamily(font, script);
56 }
57
58 void setCursiveFontFamilyWrapper(WebSettings* settings,
59                                  const base::string16& font,
60                                  UScriptCode script) {
61   settings->setCursiveFontFamily(font, script);
62 }
63
64 void setFantasyFontFamilyWrapper(WebSettings* settings,
65                                  const base::string16& font,
66                                  UScriptCode script) {
67   settings->setFantasyFontFamily(font, script);
68 }
69
70 void setPictographFontFamilyWrapper(WebSettings* settings,
71                                     const base::string16& font,
72                                     UScriptCode script) {
73   settings->setPictographFontFamily(font, script);
74 }
75
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) {
86   switch (scriptCode) {
87   case USCRIPT_HIRAGANA:
88   case USCRIPT_KATAKANA:
89   case USCRIPT_JAPANESE:
90     return USCRIPT_KATAKANA_OR_HIRAGANA;
91   case USCRIPT_KOREAN:
92     return USCRIPT_HANGUL;
93   default:
94     return scriptCode;
95   }
96 }
97
98 void ApplyFontsFromMap(const webkit_glue::ScriptFontFamilyMap& map,
99                        SetFontFamilyWrapper setter,
100                        WebSettings* settings) {
101   for (webkit_glue::ScriptFontFamilyMap::const_iterator it = map.begin();
102        it != map.end();
103        ++it) {
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));
108     }
109   }
110 }
111
112 }  // namespace
113
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);
165
166   // This setting affects the behavior of links in an editable region:
167   // clicking the link should select it rather than navigate to it.
168   // Safari uses the same default. It is unlikley an embedder would want to
169   // change this, since it would break existing rich text editors.
170   settings->setEditableLinkBehaviorNeverLive();
171
172   settings->setJavaEnabled(prefs.java_enabled);
173
174   // By default, allow_universal_access_from_file_urls is set to false and thus
175   // we mitigate attacks from local HTML files by not granting file:// URLs
176   // universal access. Only test shell will enable this.
177   settings->setAllowUniversalAccessFromFileURLs(
178       prefs.allow_universal_access_from_file_urls);
179   settings->setAllowFileAccessFromFileURLs(
180       prefs.allow_file_access_from_file_urls);
181
182   // Enable the web audio API if requested on the command line.
183   settings->setWebAudioEnabled(prefs.webaudio_enabled);
184
185   // Enable experimental WebGL support if requested on command line
186   // and support is compiled in.
187   settings->setExperimentalWebGLEnabled(prefs.experimental_webgl_enabled);
188
189   // Disable GL multisampling if requested on command line.
190   settings->setOpenGLMultisamplingEnabled(prefs.gl_multisampling_enabled);
191
192   // Enable privileged WebGL extensions for Chrome extensions or if requested
193   // on command line.
194   settings->setPrivilegedWebGLExtensionsEnabled(
195       prefs.privileged_webgl_extensions_enabled);
196
197   // Enable WebGL errors to the JS console if requested.
198   settings->setWebGLErrorsToConsoleEnabled(
199       prefs.webgl_errors_to_console_enabled);
200
201   // Uses the mock theme engine for scrollbars.
202   settings->setMockScrollbarsEnabled(prefs.mock_scrollbars_enabled);
203
204   settings->setLayerSquashingEnabled(prefs.layer_squashing_enabled);
205
206   settings->setThreadedHTMLParser(prefs.threaded_html_parser);
207
208   // Display visualization of what has changed on the screen using an
209   // overlay of rects, if requested on the command line.
210   settings->setShowPaintRects(prefs.show_paint_rects);
211
212   // Enable gpu-accelerated compositing if requested on the command line.
213   settings->setAcceleratedCompositingEnabled(
214       prefs.accelerated_compositing_enabled);
215
216   // Enable gpu-accelerated 2d canvas if requested on the command line.
217   settings->setAccelerated2dCanvasEnabled(prefs.accelerated_2d_canvas_enabled);
218
219   settings->setMinimumAccelerated2dCanvasSize(
220       prefs.minimum_accelerated_2d_canvas_size);
221
222   // Disable antialiasing for 2d canvas if requested on the command line.
223   settings->setAntialiased2dCanvasEnabled(
224       !prefs.antialiased_2d_canvas_disabled);
225
226   // Set MSAA sample count for 2d canvas if requested on the command line (or
227   // default value if not).
228   settings->setAccelerated2dCanvasMSAASampleCount(
229       prefs.accelerated_2d_canvas_msaa_sample_count);
230
231   // Enable deferred filter rendering if requested on the command line.
232   settings->setDeferredFiltersEnabled(prefs.deferred_filters_enabled);
233
234   // Enable gesture tap highlight if requested on the command line.
235   settings->setGestureTapHighlightEnabled(prefs.gesture_tap_highlight_enabled);
236
237   // Enabling accelerated layers from the command line enabled accelerated
238   // 3D CSS, Video, and Animations.
239   settings->setAcceleratedCompositingFor3DTransformsEnabled(
240       prefs.accelerated_compositing_for_3d_transforms_enabled);
241   settings->setAcceleratedCompositingForVideoEnabled(
242       prefs.accelerated_compositing_for_video_enabled);
243   settings->setAcceleratedCompositingForAnimationEnabled(
244       prefs.accelerated_compositing_for_animation_enabled);
245
246   // Enabling accelerated plugins if specified from the command line.
247   settings->setAcceleratedCompositingForPluginsEnabled(
248       prefs.accelerated_compositing_for_plugins_enabled);
249
250   // WebGL and accelerated 2D canvas are always gpu composited.
251   settings->setAcceleratedCompositingForCanvasEnabled(
252       prefs.experimental_webgl_enabled || prefs.accelerated_2d_canvas_enabled);
253
254   settings->setAsynchronousSpellCheckingEnabled(
255       prefs.asynchronous_spell_checking_enabled);
256   settings->setUnifiedTextCheckerEnabled(prefs.unified_textchecker_enabled);
257
258   for (webkit_glue::WebInspectorPreferences::const_iterator it =
259            prefs.inspector_settings.begin();
260        it != prefs.inspector_settings.end();
261        ++it) {
262     web_view->setInspectorSetting(WebString::fromUTF8(it->first),
263                                   WebString::fromUTF8(it->second));
264   }
265
266   // Tabs to link is not part of the settings. WebCore calls
267   // ChromeClient::tabsToLinks which is part of the glue code.
268   web_view->setTabsToLinks(prefs.tabs_to_links);
269
270   // TODO(scheib): crbug.com/344002 Remove FullScreenEnabled from Blink
271   settings->setFullScreenEnabled(true);
272   settings->setAllowDisplayOfInsecureContent(
273       prefs.allow_displaying_insecure_content);
274   settings->setAllowRunningOfInsecureContent(
275       prefs.allow_running_insecure_content);
276   settings->setPasswordEchoEnabled(prefs.password_echo_enabled);
277   settings->setShouldPrintBackgrounds(prefs.should_print_backgrounds);
278   settings->setShouldClearDocumentBackground(
279       prefs.should_clear_document_background);
280   settings->setEnableScrollAnimator(prefs.enable_scroll_animator);
281   settings->setVisualWordMovementEnabled(prefs.visual_word_movement_enabled);
282
283   settings->setRegionBasedColumnsEnabled(prefs.region_based_columns_enabled);
284
285   WebRuntimeFeatures::enableLazyLayout(prefs.lazy_layout_enabled);
286   WebRuntimeFeatures::enableTouch(prefs.touch_enabled);
287   settings->setMaxTouchPoints(prefs.pointer_events_max_touch_points);
288   settings->setDeviceSupportsTouch(prefs.device_supports_touch);
289   settings->setDeviceSupportsMouse(prefs.device_supports_mouse);
290   settings->setEnableTouchAdjustment(prefs.touch_adjustment_enabled);
291
292   settings->setFixedPositionCreatesStackingContext(
293       prefs.fixed_position_creates_stacking_context);
294
295   settings->setDeferredImageDecodingEnabled(
296       prefs.deferred_image_decoding_enabled);
297   settings->setShouldRespectImageOrientation(
298       prefs.should_respect_image_orientation);
299
300   settings->setUnsafePluginPastingEnabled(false);
301   settings->setEditingBehavior(
302       static_cast<WebSettings::EditingBehavior>(prefs.editing_behavior));
303
304   settings->setSupportsMultipleWindows(prefs.supports_multiple_windows);
305
306   settings->setViewportEnabled(prefs.viewport_enabled);
307   settings->setLoadWithOverviewMode(prefs.initialize_at_minimum_page_scale);
308   settings->setViewportMetaEnabled(prefs.viewport_meta_enabled);
309   settings->setMainFrameResizesAreOrientationChanges(
310       prefs.main_frame_resizes_are_orientation_changes);
311
312   settings->setSmartInsertDeleteEnabled(prefs.smart_insert_delete_enabled);
313
314   settings->setSpatialNavigationEnabled(prefs.spatial_navigation_enabled);
315
316   settings->setSelectionIncludesAltImageText(true);
317
318   // Crosswalk shared settings:
319   settings->setLayoutFallbackWidth(0);
320   settings->setMainFrameClipsContent(false);
321
322 #if defined(OS_TIZEN)
323   // Scrollbars should not be stylable.
324   settings->setAllowCustomScrollbarInMainFrame(false);
325
326   // IME support
327   settings->setAutoZoomFocusedNodeToLegibleScale(true);
328
329   // Enable double tap to zoom when zoomable.
330   settings->setDoubleTapToZoomEnabled(true);
331 #endif
332
333 #if defined(OS_ANDROID)
334   settings->setAllowCustomScrollbarInMainFrame(false);
335   settings->setTextAutosizingEnabled(prefs.text_autosizing_enabled);
336   settings->setAccessibilityFontScaleFactor(prefs.font_scale_factor);
337   settings->setDeviceScaleAdjustment(prefs.device_scale_adjustment);
338   web_view->setIgnoreViewportTagScaleLimits(prefs.force_enable_zoom);
339   settings->setAutoZoomFocusedNodeToLegibleScale(true);
340   settings->setDoubleTapToZoomEnabled(prefs.double_tap_to_zoom_enabled);
341   settings->setMediaPlaybackRequiresUserGesture(
342       prefs.user_gesture_required_for_media_playback);
343   settings->setMediaFullscreenRequiresUserGesture(
344       prefs.user_gesture_required_for_media_fullscreen);
345   settings->setDefaultVideoPosterURL(
346         base::ASCIIToUTF16(prefs.default_video_poster_url.spec()));
347   settings->setSupportDeprecatedTargetDensityDPI(
348       prefs.support_deprecated_target_density_dpi);
349   settings->setUseLegacyBackgroundSizeShorthandBehavior(
350       prefs.use_legacy_background_size_shorthand_behavior);
351   settings->setWideViewportQuirkEnabled(prefs.wide_viewport_quirk);
352   settings->setUseWideViewport(prefs.use_wide_viewport);
353   settings->setViewportMetaLayoutSizeQuirk(
354       prefs.viewport_meta_layout_size_quirk);
355   settings->setViewportMetaMergeContentQuirk(
356       prefs.viewport_meta_merge_content_quirk);
357   settings->setViewportMetaNonUserScalableQuirk(
358       prefs.viewport_meta_non_user_scalable_quirk);
359   settings->setViewportMetaZeroValuesQuirk(
360       prefs.viewport_meta_zero_values_quirk);
361   settings->setClobberUserAgentInitialScaleQuirk(
362       prefs.clobber_user_agent_initial_scale_quirk);
363   settings->setIgnoreMainFrameOverflowHiddenQuirk(
364       prefs.ignore_main_frame_overflow_hidden_quirk);
365   settings->setReportScreenSizeInPhysicalPixelsQuirk(
366       prefs.report_screen_size_in_physical_pixels_quirk);
367   settings->setMainFrameClipsContent(false);
368   settings->setShrinksStandaloneImagesToFit(false);
369 #endif
370
371   WebNetworkStateNotifier::setOnLine(prefs.is_online);
372   settings->setExperimentalWebSocketEnabled(
373       prefs.experimental_websocket_enabled);
374   settings->setPinchVirtualViewportEnabled(
375       prefs.pinch_virtual_viewport_enabled);
376
377   settings->setPinchOverlayScrollbarThickness(
378       prefs.pinch_overlay_scrollbar_thickness);
379   settings->setUseSolidColorScrollbars(prefs.use_solid_color_scrollbars);
380   settings->setCompositorTouchHitTesting(prefs.compositor_touch_hit_testing);
381 }
382
383 }  // namespace content