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