- add sources.
[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 WebKit::WebNetworkStateNotifier;
20 using WebKit::WebRuntimeFeatures;
21 using WebKit::WebSettings;
22 using WebKit::WebString;
23 using WebKit::WebURL;
24 using WebKit::WebView;
25
26 namespace content {
27
28 namespace {
29
30 typedef void (*SetFontFamilyWrapper)(WebKit::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(ASCIIToUTF16(prefs.default_encoding));
135   settings->setJavaScriptEnabled(prefs.javascript_enabled);
136   settings->setWebSecurityEnabled(prefs.web_security_enabled);
137   settings->setJavaScriptCanOpenWindowsAutomatically(
138       prefs.javascript_can_open_windows_automatically);
139   settings->setLoadsImagesAutomatically(prefs.loads_images_automatically);
140   settings->setImagesEnabled(prefs.images_enabled);
141   settings->setPluginsEnabled(prefs.plugins_enabled);
142   settings->setDOMPasteAllowed(prefs.dom_paste_enabled);
143   settings->setNeedsSiteSpecificQuirks(prefs.site_specific_quirks_enabled);
144   settings->setShrinksStandaloneImagesToFit(
145       prefs.shrinks_standalone_images_to_fit);
146   settings->setUsesEncodingDetector(prefs.uses_universal_detector);
147   settings->setTextAreasAreResizable(prefs.text_areas_are_resizable);
148   settings->setAllowScriptsToCloseWindows(prefs.allow_scripts_to_close_windows);
149   if (prefs.user_style_sheet_enabled)
150     settings->setUserStyleSheetLocation(prefs.user_style_sheet_location);
151   else
152     settings->setUserStyleSheetLocation(WebURL());
153   settings->setAuthorAndUserStylesEnabled(prefs.author_and_user_styles_enabled);
154   settings->setDownloadableBinaryFontsEnabled(prefs.remote_fonts_enabled);
155   settings->setJavaScriptCanAccessClipboard(
156       prefs.javascript_can_access_clipboard);
157   WebRuntimeFeatures::enableXSLT(prefs.xslt_enabled);
158   settings->setXSSAuditorEnabled(prefs.xss_auditor_enabled);
159   settings->setDNSPrefetchingEnabled(prefs.dns_prefetching_enabled);
160   settings->setLocalStorageEnabled(prefs.local_storage_enabled);
161   settings->setSyncXHRInDocumentsEnabled(prefs.sync_xhr_in_documents_enabled);
162   WebRuntimeFeatures::enableDatabase(prefs.databases_enabled);
163   settings->setOfflineWebApplicationCacheEnabled(
164       prefs.application_cache_enabled);
165   settings->setCaretBrowsingEnabled(prefs.caret_browsing_enabled);
166   settings->setHyperlinkAuditingEnabled(prefs.hyperlink_auditing_enabled);
167   settings->setCookieEnabled(prefs.cookie_enabled);
168
169   // This setting affects the behavior of links in an editable region:
170   // clicking the link should select it rather than navigate to it.
171   // Safari uses the same default. It is unlikley an embedder would want to
172   // change this, since it would break existing rich text editors.
173   settings->setEditableLinkBehaviorNeverLive();
174
175   settings->setJavaEnabled(prefs.java_enabled);
176
177   // By default, allow_universal_access_from_file_urls is set to false and thus
178   // we mitigate attacks from local HTML files by not granting file:// URLs
179   // universal access. Only test shell will enable this.
180   settings->setAllowUniversalAccessFromFileURLs(
181       prefs.allow_universal_access_from_file_urls);
182   settings->setAllowFileAccessFromFileURLs(
183       prefs.allow_file_access_from_file_urls);
184
185   // Enable the web audio API if requested on the command line.
186   settings->setWebAudioEnabled(prefs.webaudio_enabled);
187
188   // Enable experimental WebGL support if requested on command line
189   // and support is compiled in.
190   settings->setExperimentalWebGLEnabled(prefs.experimental_webgl_enabled);
191
192   // Disable GL multisampling if requested on command line.
193   settings->setOpenGLMultisamplingEnabled(prefs.gl_multisampling_enabled);
194
195   // Enable privileged WebGL extensions for Chrome extensions or if requested
196   // on command line.
197   settings->setPrivilegedWebGLExtensionsEnabled(
198       prefs.privileged_webgl_extensions_enabled);
199
200   // Enable WebGL errors to the JS console if requested.
201   settings->setWebGLErrorsToConsoleEnabled(
202       prefs.webgl_errors_to_console_enabled);
203
204   // Enables accelerated compositing for overflow scroll.
205   settings->setAcceleratedCompositingForOverflowScrollEnabled(
206       prefs.accelerated_compositing_for_overflow_scroll_enabled);
207
208   // Enables accelerated compositing for scrollable frames if requested on
209   // command line.
210   settings->setAcceleratedCompositingForScrollableFramesEnabled(
211       prefs.accelerated_compositing_for_scrollable_frames_enabled);
212
213   // Enables composited scrolling for frames if requested on command line.
214   settings->setCompositedScrollingForFramesEnabled(
215       prefs.composited_scrolling_for_frames_enabled);
216
217   // Uses the mock theme engine for scrollbars.
218   settings->setMockScrollbarsEnabled(prefs.mock_scrollbars_enabled);
219
220   settings->setThreadedHTMLParser(prefs.threaded_html_parser);
221
222   // Display visualization of what has changed on the screen using an
223   // overlay of rects, if requested on the command line.
224   settings->setShowPaintRects(prefs.show_paint_rects);
225
226   // Enable gpu-accelerated compositing if requested on the command line.
227   settings->setAcceleratedCompositingEnabled(
228       prefs.accelerated_compositing_enabled);
229
230   // Enable gpu-accelerated 2d canvas if requested on the command line.
231   settings->setAccelerated2dCanvasEnabled(prefs.accelerated_2d_canvas_enabled);
232
233   settings->setMinimumAccelerated2dCanvasSize(
234       prefs.minimum_accelerated_2d_canvas_size);
235
236   // Disable antialiasing for 2d canvas if requested on the command line.
237   settings->setAntialiased2dCanvasEnabled(
238       !prefs.antialiased_2d_canvas_disabled);
239
240   // Set MSAA sample count for 2d canvas if requested on the command line (or
241   // default value if not).
242   settings->setAccelerated2dCanvasMSAASampleCount(
243       prefs.accelerated_2d_canvas_msaa_sample_count);
244
245   // Enable gpu-accelerated filters if requested on the command line.
246   settings->setAcceleratedFiltersEnabled(prefs.accelerated_filters_enabled);
247
248   // Enable gesture tap highlight if requested on the command line.
249   settings->setGestureTapHighlightEnabled(prefs.gesture_tap_highlight_enabled);
250
251   // Enabling accelerated layers from the command line enabled accelerated
252   // 3D CSS, Video, and Animations.
253   settings->setAcceleratedCompositingFor3DTransformsEnabled(
254       prefs.accelerated_compositing_for_3d_transforms_enabled);
255   settings->setAcceleratedCompositingForVideoEnabled(
256       prefs.accelerated_compositing_for_video_enabled);
257   settings->setAcceleratedCompositingForAnimationEnabled(
258       prefs.accelerated_compositing_for_animation_enabled);
259
260   // Enabling accelerated plugins if specified from the command line.
261   settings->setAcceleratedCompositingForPluginsEnabled(
262       prefs.accelerated_compositing_for_plugins_enabled);
263
264   // WebGL and accelerated 2D canvas are always gpu composited.
265   settings->setAcceleratedCompositingForCanvasEnabled(
266       prefs.experimental_webgl_enabled || prefs.accelerated_2d_canvas_enabled);
267
268   // Enable memory info reporting to page if requested on the command line.
269   settings->setMemoryInfoEnabled(prefs.memory_info_enabled);
270
271   settings->setAsynchronousSpellCheckingEnabled(
272       prefs.asynchronous_spell_checking_enabled);
273   settings->setUnifiedTextCheckerEnabled(prefs.unified_textchecker_enabled);
274
275   for (webkit_glue::WebInspectorPreferences::const_iterator it =
276            prefs.inspector_settings.begin();
277        it != prefs.inspector_settings.end();
278        ++it) {
279     web_view->setInspectorSetting(WebString::fromUTF8(it->first),
280                                   WebString::fromUTF8(it->second));
281   }
282
283   // Tabs to link is not part of the settings. WebCore calls
284   // ChromeClient::tabsToLinks which is part of the glue code.
285   web_view->setTabsToLinks(prefs.tabs_to_links);
286
287   settings->setFullScreenEnabled(prefs.fullscreen_enabled);
288   settings->setAllowDisplayOfInsecureContent(
289       prefs.allow_displaying_insecure_content);
290   settings->setAllowRunningOfInsecureContent(
291       prefs.allow_running_insecure_content);
292   settings->setPasswordEchoEnabled(prefs.password_echo_enabled);
293   settings->setShouldPrintBackgrounds(prefs.should_print_backgrounds);
294   settings->setEnableScrollAnimator(prefs.enable_scroll_animator);
295   settings->setVisualWordMovementEnabled(prefs.visual_word_movement_enabled);
296
297   settings->setRegionBasedColumnsEnabled(prefs.region_based_columns_enabled);
298
299   WebRuntimeFeatures::enableLazyLayout(prefs.lazy_layout_enabled);
300   WebRuntimeFeatures::enableTouch(prefs.touch_enabled);
301   settings->setMaxTouchPoints(prefs.pointer_events_max_touch_points);
302   settings->setDeviceSupportsTouch(prefs.device_supports_touch);
303   settings->setDeviceSupportsMouse(prefs.device_supports_mouse);
304   settings->setEnableTouchAdjustment(prefs.touch_adjustment_enabled);
305
306   settings->setFixedPositionCreatesStackingContext(
307       prefs.fixed_position_creates_stacking_context);
308
309   settings->setDeferredImageDecodingEnabled(
310       prefs.deferred_image_decoding_enabled);
311   settings->setShouldRespectImageOrientation(
312       prefs.should_respect_image_orientation);
313
314   settings->setUnsafePluginPastingEnabled(false);
315   settings->setEditingBehavior(
316       static_cast<WebSettings::EditingBehavior>(prefs.editing_behavior));
317
318   settings->setSupportsMultipleWindows(prefs.supports_multiple_windows);
319
320   settings->setViewportEnabled(prefs.viewport_enabled);
321   settings->setLoadWithOverviewMode(prefs.initialize_at_minimum_page_scale);
322
323   settings->setSmartInsertDeleteEnabled(prefs.smart_insert_delete_enabled);
324
325   settings->setSpatialNavigationEnabled(prefs.spatial_navigation_enabled);
326
327   settings->setSelectionIncludesAltImageText(true);
328
329   // Crosswalk shared settings:
330   settings->setLayoutFallbackWidth(0);
331   settings->setMainFrameClipsContent(false);
332
333 #if defined(OS_TIZEN)
334   // Scrollbars should not be stylable.
335   settings->setAllowCustomScrollbarInMainFrame(false);
336
337   // Don't auto play music on Tizen devices.
338   settings->setMediaPlaybackRequiresUserGesture(true);
339
340   // IME support
341   settings->setAutoZoomFocusedNodeToLegibleScale(true);
342
343   // Enable double tap to zoom when zoomable.
344   settings->setDoubleTapToZoomEnabled(true);
345 #endif
346
347 #if defined(OS_ANDROID)
348   settings->setAllowCustomScrollbarInMainFrame(false);
349   settings->setTextAutosizingEnabled(prefs.text_autosizing_enabled);
350   settings->setTextAutosizingFontScaleFactor(prefs.font_scale_factor);
351   settings->setDeviceScaleAdjustment(prefs.device_scale_adjustment);
352   web_view->setIgnoreViewportTagScaleLimits(prefs.force_enable_zoom);
353   settings->setAutoZoomFocusedNodeToLegibleScale(true);
354   settings->setDoubleTapToZoomEnabled(prefs.double_tap_to_zoom_enabled);
355   settings->setMediaPlaybackRequiresUserGesture(
356       prefs.user_gesture_required_for_media_playback);
357   settings->setMediaFullscreenRequiresUserGesture(
358       prefs.user_gesture_required_for_media_fullscreen);
359   settings->setDefaultVideoPosterURL(
360         ASCIIToUTF16(prefs.default_video_poster_url.spec()));
361   settings->setSupportDeprecatedTargetDensityDPI(
362       prefs.support_deprecated_target_density_dpi);
363   settings->setUseLegacyBackgroundSizeShorthandBehavior(
364       prefs.use_legacy_background_size_shorthand_behavior);
365   settings->setWideViewportQuirkEnabled(prefs.wide_viewport_quirk);
366   settings->setUseWideViewport(prefs.use_wide_viewport);
367   settings->setViewportMetaLayoutSizeQuirk(
368       prefs.viewport_meta_layout_size_quirk);
369   settings->setViewportMetaMergeContentQuirk(
370       prefs.viewport_meta_merge_content_quirk);
371   settings->setViewportMetaZeroValuesQuirk(
372       prefs.viewport_meta_zero_values_quirk);
373   settings->setIgnoreMainFrameOverflowHiddenQuirk(
374       prefs.ignore_main_frame_overflow_hidden_quirk);
375   settings->setReportScreenSizeInPhysicalPixelsQuirk(
376       prefs.report_screen_size_in_physical_pixels_quirk);
377   settings->setMainFrameClipsContent(false);
378   settings->setShrinksStandaloneImagesToFit(false);
379 #endif
380
381   WebNetworkStateNotifier::setOnLine(prefs.is_online);
382   settings->setExperimentalWebSocketEnabled(
383       prefs.experimental_websocket_enabled);
384   settings->setPinchVirtualViewportEnabled(
385       prefs.pinch_virtual_viewport_enabled);
386
387   settings->setPinchOverlayScrollbarThickness(
388       prefs.pinch_overlay_scrollbar_thickness);
389   settings->setUseSolidColorScrollbars(prefs.use_solid_color_scrollbars);
390   settings->setCompositorTouchHitTesting(prefs.compositor_touch_hit_testing);
391 }
392
393 }  // namespace content