Home | History | Annotate | Download | only in renderer
      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 "content/renderer/net_info_helper.h"
      9 #include "third_party/WebKit/public/platform/WebConnectionType.h"
     10 #include "third_party/WebKit/public/platform/WebString.h"
     11 #include "third_party/WebKit/public/platform/WebURL.h"
     12 #include "third_party/WebKit/public/web/WebKit.h"
     13 #include "third_party/WebKit/public/web/WebNetworkStateNotifier.h"
     14 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
     15 #include "third_party/WebKit/public/web/WebSettings.h"
     16 #include "third_party/WebKit/public/web/WebView.h"
     17 #include "third_party/icu/source/common/unicode/uchar.h"
     18 #include "third_party/icu/source/common/unicode/uscript.h"
     19 #include "webkit/common/webpreferences.h"
     20 
     21 using blink::WebNetworkStateNotifier;
     22 using blink::WebRuntimeFeatures;
     23 using blink::WebSettings;
     24 using blink::WebString;
     25 using blink::WebURL;
     26 using blink::WebView;
     27 
     28 namespace content {
     29 
     30 namespace {
     31 
     32 typedef void (*SetFontFamilyWrapper)(blink::WebSettings*,
     33                                      const base::string16&,
     34                                      UScriptCode);
     35 
     36 void setStandardFontFamilyWrapper(WebSettings* settings,
     37                                   const base::string16& font,
     38                                   UScriptCode script) {
     39   settings->setStandardFontFamily(font, script);
     40 }
     41 
     42 void setFixedFontFamilyWrapper(WebSettings* settings,
     43                                const base::string16& font,
     44                                UScriptCode script) {
     45   settings->setFixedFontFamily(font, script);
     46 }
     47 
     48 void setSerifFontFamilyWrapper(WebSettings* settings,
     49                                const base::string16& font,
     50                                UScriptCode script) {
     51   settings->setSerifFontFamily(font, script);
     52 }
     53 
     54 void setSansSerifFontFamilyWrapper(WebSettings* settings,
     55                                    const base::string16& font,
     56                                    UScriptCode script) {
     57   settings->setSansSerifFontFamily(font, script);
     58 }
     59 
     60 void setCursiveFontFamilyWrapper(WebSettings* settings,
     61                                  const base::string16& font,
     62                                  UScriptCode script) {
     63   settings->setCursiveFontFamily(font, script);
     64 }
     65 
     66 void setFantasyFontFamilyWrapper(WebSettings* settings,
     67                                  const base::string16& font,
     68                                  UScriptCode script) {
     69   settings->setFantasyFontFamily(font, script);
     70 }
     71 
     72 void setPictographFontFamilyWrapper(WebSettings* settings,
     73                                     const base::string16& font,
     74                                     UScriptCode script) {
     75   settings->setPictographFontFamily(font, script);
     76 }
     77 
     78 // If |scriptCode| is a member of a family of "similar" script codes, returns
     79 // the script code in that family that is used by WebKit for font selection
     80 // purposes.  For example, USCRIPT_KATAKANA_OR_HIRAGANA and USCRIPT_JAPANESE are
     81 // considered equivalent for the purposes of font selection.  WebKit uses the
     82 // script code USCRIPT_KATAKANA_OR_HIRAGANA.  So, if |scriptCode| is
     83 // USCRIPT_JAPANESE, the function returns USCRIPT_KATAKANA_OR_HIRAGANA.  WebKit
     84 // uses different scripts than the ones in Chrome pref names because the version
     85 // of ICU included on certain ports does not have some of the newer scripts.  If
     86 // |scriptCode| is not a member of such a family, returns |scriptCode|.
     87 UScriptCode GetScriptForWebSettings(UScriptCode scriptCode) {
     88   switch (scriptCode) {
     89   case USCRIPT_HIRAGANA:
     90   case USCRIPT_KATAKANA:
     91   case USCRIPT_JAPANESE:
     92     return USCRIPT_KATAKANA_OR_HIRAGANA;
     93   case USCRIPT_KOREAN:
     94     return USCRIPT_HANGUL;
     95   default:
     96     return scriptCode;
     97   }
     98 }
     99 
    100 void ApplyFontsFromMap(const webkit_glue::ScriptFontFamilyMap& map,
    101                        SetFontFamilyWrapper setter,
    102                        WebSettings* settings) {
    103   for (webkit_glue::ScriptFontFamilyMap::const_iterator it = map.begin();
    104        it != map.end();
    105        ++it) {
    106     int32 script = u_getPropertyValueEnum(UCHAR_SCRIPT, (it->first).c_str());
    107     if (script >= 0 && script < USCRIPT_CODE_LIMIT) {
    108       UScriptCode code = static_cast<UScriptCode>(script);
    109       (*setter)(settings, it->second, GetScriptForWebSettings(code));
    110     }
    111   }
    112 }
    113 
    114 }  // namespace
    115 
    116 void ApplyWebPreferences(const WebPreferences& prefs, WebView* web_view) {
    117   WebSettings* settings = web_view->settings();
    118   ApplyFontsFromMap(prefs.standard_font_family_map,
    119                     setStandardFontFamilyWrapper, settings);
    120   ApplyFontsFromMap(prefs.fixed_font_family_map,
    121                     setFixedFontFamilyWrapper, settings);
    122   ApplyFontsFromMap(prefs.serif_font_family_map,
    123                     setSerifFontFamilyWrapper, settings);
    124   ApplyFontsFromMap(prefs.sans_serif_font_family_map,
    125                     setSansSerifFontFamilyWrapper, settings);
    126   ApplyFontsFromMap(prefs.cursive_font_family_map,
    127                     setCursiveFontFamilyWrapper, settings);
    128   ApplyFontsFromMap(prefs.fantasy_font_family_map,
    129                     setFantasyFontFamilyWrapper, settings);
    130   ApplyFontsFromMap(prefs.pictograph_font_family_map,
    131                     setPictographFontFamilyWrapper, settings);
    132   settings->setDefaultFontSize(prefs.default_font_size);
    133   settings->setDefaultFixedFontSize(prefs.default_fixed_font_size);
    134   settings->setMinimumFontSize(prefs.minimum_font_size);
    135   settings->setMinimumLogicalFontSize(prefs.minimum_logical_font_size);
    136   settings->setDefaultTextEncodingName(
    137       base::ASCIIToUTF16(prefs.default_encoding));
    138   settings->setJavaScriptEnabled(prefs.javascript_enabled);
    139   settings->setWebSecurityEnabled(prefs.web_security_enabled);
    140   settings->setJavaScriptCanOpenWindowsAutomatically(
    141       prefs.javascript_can_open_windows_automatically);
    142   settings->setLoadsImagesAutomatically(prefs.loads_images_automatically);
    143   settings->setImagesEnabled(prefs.images_enabled);
    144   settings->setPluginsEnabled(prefs.plugins_enabled);
    145   settings->setDOMPasteAllowed(prefs.dom_paste_enabled);
    146   settings->setNeedsSiteSpecificQuirks(prefs.site_specific_quirks_enabled);
    147   settings->setShrinksStandaloneImagesToFit(
    148       prefs.shrinks_standalone_images_to_fit);
    149   settings->setUsesEncodingDetector(prefs.uses_universal_detector);
    150   settings->setTextAreasAreResizable(prefs.text_areas_are_resizable);
    151   settings->setAllowScriptsToCloseWindows(prefs.allow_scripts_to_close_windows);
    152   settings->setDownloadableBinaryFontsEnabled(prefs.remote_fonts_enabled);
    153   settings->setJavaScriptCanAccessClipboard(
    154       prefs.javascript_can_access_clipboard);
    155   WebRuntimeFeatures::enableXSLT(prefs.xslt_enabled);
    156   settings->setXSSAuditorEnabled(prefs.xss_auditor_enabled);
    157   settings->setDNSPrefetchingEnabled(prefs.dns_prefetching_enabled);
    158   settings->setLocalStorageEnabled(prefs.local_storage_enabled);
    159   settings->setSyncXHRInDocumentsEnabled(prefs.sync_xhr_in_documents_enabled);
    160   WebRuntimeFeatures::enableDatabase(prefs.databases_enabled);
    161   settings->setOfflineWebApplicationCacheEnabled(
    162       prefs.application_cache_enabled);
    163   settings->setCaretBrowsingEnabled(prefs.caret_browsing_enabled);
    164   settings->setHyperlinkAuditingEnabled(prefs.hyperlink_auditing_enabled);
    165   settings->setCookieEnabled(prefs.cookie_enabled);
    166   settings->setNavigateOnDragDrop(prefs.navigate_on_drag_drop);
    167 
    168   settings->setJavaEnabled(prefs.java_enabled);
    169 
    170   // By default, allow_universal_access_from_file_urls is set to false and thus
    171   // we mitigate attacks from local HTML files by not granting file:// URLs
    172   // universal access. Only test shell will enable this.
    173   settings->setAllowUniversalAccessFromFileURLs(
    174       prefs.allow_universal_access_from_file_urls);
    175   settings->setAllowFileAccessFromFileURLs(
    176       prefs.allow_file_access_from_file_urls);
    177 
    178   // Enable the web audio API if requested on the command line.
    179   settings->setWebAudioEnabled(prefs.webaudio_enabled);
    180 
    181   // Enable experimental WebGL support if requested on command line
    182   // and support is compiled in.
    183   settings->setExperimentalWebGLEnabled(prefs.experimental_webgl_enabled);
    184 
    185   // Disable GL multisampling if requested on command line.
    186   settings->setOpenGLMultisamplingEnabled(prefs.gl_multisampling_enabled);
    187 
    188   // Enable WebGL errors to the JS console if requested.
    189   settings->setWebGLErrorsToConsoleEnabled(
    190       prefs.webgl_errors_to_console_enabled);
    191 
    192   // Uses the mock theme engine for scrollbars.
    193   settings->setMockScrollbarsEnabled(prefs.mock_scrollbars_enabled);
    194 
    195   settings->setLayerSquashingEnabled(prefs.layer_squashing_enabled);
    196 
    197   // Enable gpu-accelerated compositing always.
    198   settings->setAcceleratedCompositingEnabled(true);
    199 
    200   // Enable gpu-accelerated 2d canvas if requested on the command line.
    201   settings->setAccelerated2dCanvasEnabled(prefs.accelerated_2d_canvas_enabled);
    202 
    203   settings->setMinimumAccelerated2dCanvasSize(
    204       prefs.minimum_accelerated_2d_canvas_size);
    205 
    206   // Disable antialiasing for 2d canvas if requested on the command line.
    207   settings->setAntialiased2dCanvasEnabled(
    208       !prefs.antialiased_2d_canvas_disabled);
    209 
    210   // Set MSAA sample count for 2d canvas if requested on the command line (or
    211   // default value if not).
    212   settings->setAccelerated2dCanvasMSAASampleCount(
    213       prefs.accelerated_2d_canvas_msaa_sample_count);
    214 
    215   // Enable deferred filter rendering if requested on the command line.
    216   settings->setDeferredFiltersEnabled(prefs.deferred_filters_enabled);
    217 
    218   // Enable container culling if requested on the command line.
    219   settings->setContainerCullingEnabled(prefs.container_culling_enabled);
    220 
    221   // Enable gesture tap highlight if requested on the command line.
    222   settings->setGestureTapHighlightEnabled(prefs.gesture_tap_highlight_enabled);
    223 
    224   // Enabling accelerated layers from the command line enabled accelerated
    225   // Video.
    226   settings->setAcceleratedCompositingForVideoEnabled(
    227       prefs.accelerated_compositing_for_video_enabled);
    228 
    229   // WebGL and accelerated 2D canvas are always gpu composited.
    230   settings->setAcceleratedCompositingForCanvasEnabled(
    231       prefs.experimental_webgl_enabled || prefs.accelerated_2d_canvas_enabled);
    232 
    233   settings->setAsynchronousSpellCheckingEnabled(
    234       prefs.asynchronous_spell_checking_enabled);
    235   settings->setUnifiedTextCheckerEnabled(prefs.unified_textchecker_enabled);
    236 
    237   for (webkit_glue::WebInspectorPreferences::const_iterator it =
    238            prefs.inspector_settings.begin();
    239        it != prefs.inspector_settings.end();
    240        ++it) {
    241     web_view->setInspectorSetting(WebString::fromUTF8(it->first),
    242                                   WebString::fromUTF8(it->second));
    243   }
    244 
    245   // Tabs to link is not part of the settings. WebCore calls
    246   // ChromeClient::tabsToLinks which is part of the glue code.
    247   web_view->setTabsToLinks(prefs.tabs_to_links);
    248 
    249   settings->setAllowDisplayOfInsecureContent(
    250       prefs.allow_displaying_insecure_content);
    251   settings->setAllowRunningOfInsecureContent(
    252       prefs.allow_running_insecure_content);
    253   settings->setPasswordEchoEnabled(prefs.password_echo_enabled);
    254   settings->setShouldPrintBackgrounds(prefs.should_print_backgrounds);
    255   settings->setShouldClearDocumentBackground(
    256       prefs.should_clear_document_background);
    257   settings->setEnableScrollAnimator(prefs.enable_scroll_animator);
    258 
    259   settings->setRegionBasedColumnsEnabled(prefs.region_based_columns_enabled);
    260 
    261   WebRuntimeFeatures::enableTouch(prefs.touch_enabled);
    262   settings->setMaxTouchPoints(prefs.pointer_events_max_touch_points);
    263   settings->setDeviceSupportsTouch(prefs.device_supports_touch);
    264   settings->setDeviceSupportsMouse(prefs.device_supports_mouse);
    265   settings->setEnableTouchAdjustment(prefs.touch_adjustment_enabled);
    266 
    267   settings->setDeferredImageDecodingEnabled(
    268       prefs.deferred_image_decoding_enabled);
    269   settings->setShouldRespectImageOrientation(
    270       prefs.should_respect_image_orientation);
    271 
    272   settings->setUnsafePluginPastingEnabled(false);
    273   settings->setEditingBehavior(
    274       static_cast<WebSettings::EditingBehavior>(prefs.editing_behavior));
    275 
    276   settings->setSupportsMultipleWindows(prefs.supports_multiple_windows);
    277 
    278   settings->setViewportEnabled(prefs.viewport_enabled);
    279   settings->setLoadWithOverviewMode(prefs.initialize_at_minimum_page_scale);
    280   settings->setViewportMetaEnabled(prefs.viewport_meta_enabled);
    281   settings->setMainFrameResizesAreOrientationChanges(
    282       prefs.main_frame_resizes_are_orientation_changes);
    283 
    284   settings->setSmartInsertDeleteEnabled(prefs.smart_insert_delete_enabled);
    285 
    286   settings->setSpatialNavigationEnabled(prefs.spatial_navigation_enabled);
    287 
    288   settings->setSelectionIncludesAltImageText(true);
    289 
    290 #if defined(OS_ANDROID)
    291   settings->setAllowCustomScrollbarInMainFrame(false);
    292   settings->setTextAutosizingEnabled(prefs.text_autosizing_enabled);
    293   settings->setAccessibilityFontScaleFactor(prefs.font_scale_factor);
    294   settings->setDeviceScaleAdjustment(prefs.device_scale_adjustment);
    295   web_view->setIgnoreViewportTagScaleLimits(prefs.force_enable_zoom);
    296   settings->setAutoZoomFocusedNodeToLegibleScale(true);
    297   settings->setDisallowFullscreenForNonMediaElements(
    298       prefs.disallow_fullscreen_for_non_media_elements);
    299   settings->setFullscreenSupported(prefs.fullscreen_supported);
    300   settings->setDoubleTapToZoomEnabled(prefs.double_tap_to_zoom_enabled);
    301   settings->setMediaControlsOverlayPlayButtonEnabled(true);
    302   settings->setMediaPlaybackRequiresUserGesture(
    303       prefs.user_gesture_required_for_media_playback);
    304   settings->setDefaultVideoPosterURL(
    305         base::ASCIIToUTF16(prefs.default_video_poster_url.spec()));
    306   settings->setSupportDeprecatedTargetDensityDPI(
    307       prefs.support_deprecated_target_density_dpi);
    308   settings->setUseLegacyBackgroundSizeShorthandBehavior(
    309       prefs.use_legacy_background_size_shorthand_behavior);
    310   settings->setWideViewportQuirkEnabled(prefs.wide_viewport_quirk);
    311   settings->setUseWideViewport(prefs.use_wide_viewport);
    312   settings->setForceZeroLayoutHeight(prefs.force_zero_layout_height);
    313   settings->setViewportMetaLayoutSizeQuirk(
    314       prefs.viewport_meta_layout_size_quirk);
    315   settings->setViewportMetaMergeContentQuirk(
    316       prefs.viewport_meta_merge_content_quirk);
    317   settings->setViewportMetaNonUserScalableQuirk(
    318       prefs.viewport_meta_non_user_scalable_quirk);
    319   settings->setViewportMetaZeroValuesQuirk(
    320       prefs.viewport_meta_zero_values_quirk);
    321   settings->setClobberUserAgentInitialScaleQuirk(
    322       prefs.clobber_user_agent_initial_scale_quirk);
    323   settings->setIgnoreMainFrameOverflowHiddenQuirk(
    324       prefs.ignore_main_frame_overflow_hidden_quirk);
    325   settings->setReportScreenSizeInPhysicalPixelsQuirk(
    326       prefs.report_screen_size_in_physical_pixels_quirk);
    327   settings->setMainFrameClipsContent(false);
    328   settings->setShrinksStandaloneImagesToFit(false);
    329   settings->setShrinksViewportContentToFit(true);
    330 #endif
    331 
    332   WebNetworkStateNotifier::setOnLine(prefs.is_online);
    333   WebNetworkStateNotifier::setWebConnectionType(
    334       NetConnectionTypeToWebConnectionType(prefs.connection_type));
    335   settings->setPinchVirtualViewportEnabled(
    336       prefs.pinch_virtual_viewport_enabled);
    337 
    338   settings->setPinchOverlayScrollbarThickness(
    339       prefs.pinch_overlay_scrollbar_thickness);
    340   settings->setUseSolidColorScrollbars(prefs.use_solid_color_scrollbars);
    341   settings->setCompositorTouchHitTesting(prefs.compositor_touch_hit_testing);
    342 }
    343 
    344 }  // namespace content
    345