OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "components/html_viewer/blink_settings_impl.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "build/build_config.h" | |
11 #include "components/html_viewer/web_preferences.h" | |
12 #include "third_party/WebKit/public/platform/WebString.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 "ui/base/touch/touch_device.h" | |
20 #include "ui/base/ui_base_switches_util.h" | |
21 #include "ui/gfx/font_render_params.h" | |
22 | |
23 #if defined(OS_ANDROID) | |
24 #include "url/gurl.h" | |
25 #endif | |
26 | |
27 #if defined(OS_LINUX) | |
28 #include "third_party/WebKit/public/web/linux/WebFontRendering.h" | |
29 #endif | |
30 | |
31 namespace html_viewer { | |
32 | |
33 // TODO(rjkroege): Update this code once http://crbug.com/481108 has been | |
34 // completed and the WebSettings parameter space has been reduced. | |
35 // | |
36 // The code inside the anonymous namespace has been copied from content | |
37 // and is largely unchanged here. My assumption is that this is a | |
38 // temporary measure until 481108 is resolved. | |
39 namespace { | |
40 | |
41 // Command line switch string to disable experimental WebGL. | |
42 const char kDisableWebGLSwitch[] = "disable-webgl"; | |
43 | |
44 blink::WebConnectionType NetConnectionTypeToWebConnectionType( | |
45 net::NetworkChangeNotifier::ConnectionType net_type) { | |
46 switch (net_type) { | |
47 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN: | |
48 return blink::WebConnectionTypeUnknown; | |
49 case net::NetworkChangeNotifier::CONNECTION_ETHERNET: | |
50 return blink::WebConnectionTypeEthernet; | |
51 case net::NetworkChangeNotifier::CONNECTION_WIFI: | |
52 return blink::WebConnectionTypeWifi; | |
53 case net::NetworkChangeNotifier::CONNECTION_NONE: | |
54 return blink::WebConnectionTypeNone; | |
55 case net::NetworkChangeNotifier::CONNECTION_2G: | |
56 return blink::WebConnectionTypeCellular2G; | |
57 case net::NetworkChangeNotifier::CONNECTION_3G: | |
58 return blink::WebConnectionTypeCellular3G; | |
59 case net::NetworkChangeNotifier::CONNECTION_4G: | |
60 return blink::WebConnectionTypeCellular4G; | |
61 case net::NetworkChangeNotifier::CONNECTION_BLUETOOTH: | |
62 return blink::WebConnectionTypeBluetooth; | |
63 } | |
64 NOTREACHED(); | |
65 return blink::WebConnectionTypeNone; | |
66 } | |
67 | |
68 typedef void (*SetFontFamilyWrapper)(blink::WebSettings*, | |
69 const base::string16&, | |
70 UScriptCode); | |
71 | |
72 // If |scriptCode| is a member of a family of "similar" script codes, returns | |
73 // the script code in that family that is used by WebKit for font selection | |
74 // purposes. For example, USCRIPT_KATAKANA_OR_HIRAGANA and USCRIPT_JAPANESE are | |
75 // considered equivalent for the purposes of font selection. WebKit uses the | |
76 // script code USCRIPT_KATAKANA_OR_HIRAGANA. So, if |scriptCode| is | |
77 // USCRIPT_JAPANESE, the function returns USCRIPT_KATAKANA_OR_HIRAGANA. WebKit | |
78 // uses different scripts than the ones in Chrome pref names because the version | |
79 // of ICU included on certain ports does not have some of the newer scripts. If | |
80 // |scriptCode| is not a member of such a family, returns |scriptCode|. | |
81 UScriptCode GetScriptForWebSettings(UScriptCode scriptCode) { | |
82 switch (scriptCode) { | |
83 case USCRIPT_HIRAGANA: | |
84 case USCRIPT_KATAKANA: | |
85 case USCRIPT_JAPANESE: | |
86 return USCRIPT_KATAKANA_OR_HIRAGANA; | |
87 case USCRIPT_KOREAN: | |
88 return USCRIPT_HANGUL; | |
89 default: | |
90 return scriptCode; | |
91 } | |
92 } | |
93 | |
94 void SetStandardFontFamilyWrapper(blink::WebSettings* settings, | |
95 const base::string16& font, | |
96 UScriptCode script) { | |
97 settings->setStandardFontFamily(font, script); | |
98 } | |
99 | |
100 void SetFixedFontFamilyWrapper(blink::WebSettings* settings, | |
101 const base::string16& font, | |
102 UScriptCode script) { | |
103 settings->setFixedFontFamily(font, script); | |
104 } | |
105 | |
106 void SetSerifFontFamilyWrapper(blink::WebSettings* settings, | |
107 const base::string16& font, | |
108 UScriptCode script) { | |
109 settings->setSerifFontFamily(font, script); | |
110 } | |
111 | |
112 void SetSansSerifFontFamilyWrapper(blink::WebSettings* settings, | |
113 const base::string16& font, | |
114 UScriptCode script) { | |
115 settings->setSansSerifFontFamily(font, script); | |
116 } | |
117 | |
118 void SetCursiveFontFamilyWrapper(blink::WebSettings* settings, | |
119 const base::string16& font, | |
120 UScriptCode script) { | |
121 settings->setCursiveFontFamily(font, script); | |
122 } | |
123 | |
124 void SetFantasyFontFamilyWrapper(blink::WebSettings* settings, | |
125 const base::string16& font, | |
126 UScriptCode script) { | |
127 settings->setFantasyFontFamily(font, script); | |
128 } | |
129 | |
130 void SetPictographFontFamilyWrapper(blink::WebSettings* settings, | |
131 const base::string16& font, | |
132 UScriptCode script) { | |
133 settings->setPictographFontFamily(font, script); | |
134 } | |
135 | |
136 void ApplyFontsFromMap(const ScriptFontFamilyMap& map, | |
137 SetFontFamilyWrapper setter, | |
138 blink::WebSettings* settings) { | |
139 for (ScriptFontFamilyMap::const_iterator it = map.begin(); it != map.end(); | |
140 ++it) { | |
141 int32_t script = u_getPropertyValueEnum(UCHAR_SCRIPT, (it->first).c_str()); | |
142 if (script >= 0 && script < USCRIPT_CODE_LIMIT) { | |
143 UScriptCode code = static_cast<UScriptCode>(script); | |
144 (*setter)(settings, it->second, GetScriptForWebSettings(code)); | |
145 } | |
146 } | |
147 } | |
148 | |
149 } // namespace | |
150 | |
151 void BlinkSettingsImpl::ApplySettings(blink::WebView* web_view, | |
152 const WebPreferences& prefs) const { | |
153 blink::WebSettings* settings = web_view->settings(); | |
154 | |
155 ApplyFontsFromMap(prefs.standard_font_family_map, | |
156 SetStandardFontFamilyWrapper, settings); | |
157 ApplyFontsFromMap(prefs.fixed_font_family_map, SetFixedFontFamilyWrapper, | |
158 settings); | |
159 ApplyFontsFromMap(prefs.serif_font_family_map, SetSerifFontFamilyWrapper, | |
160 settings); | |
161 ApplyFontsFromMap(prefs.sans_serif_font_family_map, | |
162 SetSansSerifFontFamilyWrapper, settings); | |
163 ApplyFontsFromMap(prefs.cursive_font_family_map, SetCursiveFontFamilyWrapper, | |
164 settings); | |
165 ApplyFontsFromMap(prefs.fantasy_font_family_map, SetFantasyFontFamilyWrapper, | |
166 settings); | |
167 ApplyFontsFromMap(prefs.pictograph_font_family_map, | |
168 SetPictographFontFamilyWrapper, settings); | |
169 | |
170 settings->setDefaultFontSize(prefs.default_font_size); | |
171 settings->setDefaultFixedFontSize(prefs.default_fixed_font_size); | |
172 settings->setMinimumFontSize(prefs.minimum_font_size); | |
173 settings->setMinimumLogicalFontSize(prefs.minimum_logical_font_size); | |
174 | |
175 settings->setDefaultTextEncodingName( | |
176 base::ASCIIToUTF16(prefs.default_encoding)); | |
177 settings->setJavaScriptEnabled(prefs.javascript_enabled); | |
178 | |
179 settings->setWebSecurityEnabled(prefs.web_security_enabled); | |
180 settings->setJavaScriptCanOpenWindowsAutomatically( | |
181 prefs.javascript_can_open_windows_automatically); | |
182 settings->setLoadsImagesAutomatically(prefs.loads_images_automatically); | |
183 settings->setImagesEnabled(prefs.images_enabled); | |
184 settings->setPluginsEnabled(prefs.plugins_enabled); | |
185 settings->setDOMPasteAllowed(prefs.dom_paste_enabled); | |
186 settings->setUsesEncodingDetector(prefs.uses_universal_detector); | |
187 settings->setTextAreasAreResizable(prefs.text_areas_are_resizable); | |
188 settings->setAllowScriptsToCloseWindows(prefs.allow_scripts_to_close_windows); | |
189 settings->setDownloadableBinaryFontsEnabled(prefs.remote_fonts_enabled); | |
190 settings->setJavaScriptCanAccessClipboard( | |
191 prefs.javascript_can_access_clipboard); | |
192 blink::WebRuntimeFeatures::enableXSLT(prefs.xslt_enabled); | |
193 blink::WebRuntimeFeatures::enableSlimmingPaintV2( | |
194 prefs.slimming_paint_v2_enabled); | |
195 settings->setXSSAuditorEnabled(prefs.xss_auditor_enabled); | |
196 settings->setDNSPrefetchingEnabled(prefs.dns_prefetching_enabled); | |
197 settings->setDataSaverEnabled(prefs.data_saver_enabled); | |
198 settings->setLocalStorageEnabled(prefs.local_storage_enabled); | |
199 settings->setSyncXHRInDocumentsEnabled(prefs.sync_xhr_in_documents_enabled); | |
200 blink::WebRuntimeFeatures::enableDatabase(prefs.databases_enabled); | |
201 settings->setOfflineWebApplicationCacheEnabled( | |
202 prefs.application_cache_enabled); | |
203 settings->setCaretBrowsingEnabled(prefs.caret_browsing_enabled); | |
204 settings->setHyperlinkAuditingEnabled(prefs.hyperlink_auditing_enabled); | |
205 settings->setCookieEnabled(prefs.cookie_enabled); | |
206 settings->setNavigateOnDragDrop(prefs.navigate_on_drag_drop); | |
207 | |
208 // By default, allow_universal_access_from_file_urls is set to false and thus | |
209 // we mitigate attacks from local HTML files by not granting file:// URLs | |
210 // universal access. Only test shell will enable this. | |
211 settings->setAllowUniversalAccessFromFileURLs( | |
212 prefs.allow_universal_access_from_file_urls); | |
213 settings->setAllowFileAccessFromFileURLs( | |
214 prefs.allow_file_access_from_file_urls); | |
215 | |
216 // Enable the web audio API if requested on the command line. | |
217 settings->setWebAudioEnabled(prefs.webaudio_enabled); | |
218 | |
219 // Enable experimental WebGL support if requested on command line | |
220 // and support is compiled in. | |
221 settings->setExperimentalWebGLEnabled(experimental_webgl_enabled_); | |
222 | |
223 // Disable GL multisampling if requested on command line. | |
224 settings->setOpenGLMultisamplingEnabled(prefs.gl_multisampling_enabled); | |
225 | |
226 // Enable WebGL errors to the JS console if requested. | |
227 settings->setWebGLErrorsToConsoleEnabled( | |
228 prefs.webgl_errors_to_console_enabled); | |
229 | |
230 // Uses the mock theme engine for scrollbars. | |
231 settings->setMockScrollbarsEnabled(prefs.mock_scrollbars_enabled); | |
232 | |
233 // Enable gpu-accelerated 2d canvas if requested on the command line. | |
234 settings->setAccelerated2dCanvasEnabled(prefs.accelerated_2d_canvas_enabled); | |
235 | |
236 settings->setMinimumAccelerated2dCanvasSize( | |
237 prefs.minimum_accelerated_2d_canvas_size); | |
238 | |
239 // Disable antialiasing for 2d canvas if requested on the command line. | |
240 settings->setAntialiased2dCanvasEnabled( | |
241 !prefs.antialiased_2d_canvas_disabled); | |
242 | |
243 // Enabled antialiasing of clips for 2d canvas if requested on the command | |
244 // line. | |
245 settings->setAntialiasedClips2dCanvasEnabled( | |
246 prefs.antialiased_clips_2d_canvas_enabled); | |
247 | |
248 // Set MSAA sample count for 2d canvas if requested on the command line (or | |
249 // default value if not). | |
250 settings->setAccelerated2dCanvasMSAASampleCount( | |
251 prefs.accelerated_2d_canvas_msaa_sample_count); | |
252 | |
253 settings->setUnifiedTextCheckerEnabled(prefs.unified_textchecker_enabled); | |
254 | |
255 // Tabs to link is not part of the settings. WebCore calls | |
256 // ChromeClient::tabsToLinks which is part of the glue code. | |
257 web_view->setTabsToLinks(prefs.tabs_to_links); | |
258 | |
259 settings->setAllowDisplayOfInsecureContent( | |
260 prefs.allow_displaying_insecure_content); | |
261 settings->setAllowRunningOfInsecureContent( | |
262 prefs.allow_running_insecure_content); | |
263 settings->setDisableReadingFromCanvas(prefs.disable_reading_from_canvas); | |
264 settings->setStrictMixedContentChecking(prefs.strict_mixed_content_checking); | |
265 | |
266 settings->setStrictlyBlockBlockableMixedContent( | |
267 prefs.strictly_block_blockable_mixed_content); | |
268 | |
269 settings->setStrictMixedContentCheckingForPlugin( | |
270 prefs.block_mixed_plugin_content); | |
271 | |
272 settings->setStrictPowerfulFeatureRestrictions( | |
273 prefs.strict_powerful_feature_restrictions); | |
274 settings->setPasswordEchoEnabled(prefs.password_echo_enabled); | |
275 settings->setShouldPrintBackgrounds(prefs.should_print_backgrounds); | |
276 settings->setShouldClearDocumentBackground( | |
277 prefs.should_clear_document_background); | |
278 settings->setEnableScrollAnimator(prefs.enable_scroll_animator); | |
279 | |
280 blink::WebRuntimeFeatures::enableTouch(prefs.touch_enabled); | |
281 settings->setMaxTouchPoints(prefs.pointer_events_max_touch_points); | |
282 settings->setAvailablePointerTypes(prefs.available_pointer_types); | |
283 settings->setPrimaryPointerType( | |
284 static_cast<blink::PointerType>(prefs.primary_pointer_type)); | |
285 settings->setAvailableHoverTypes(prefs.available_hover_types); | |
286 settings->setPrimaryHoverType( | |
287 static_cast<blink::HoverType>(prefs.primary_hover_type)); | |
288 settings->setDeviceSupportsTouch(prefs.device_supports_touch); | |
289 settings->setDeviceSupportsMouse(prefs.device_supports_mouse); | |
290 settings->setEnableTouchAdjustment(prefs.touch_adjustment_enabled); | |
291 settings->setMultiTargetTapNotificationEnabled( | |
292 switches::IsLinkDisambiguationPopupEnabled()); | |
293 | |
294 blink::WebRuntimeFeatures::enableImageColorProfiles( | |
295 prefs.image_color_profiles_enabled); | |
296 settings->setShouldRespectImageOrientation( | |
297 prefs.should_respect_image_orientation); | |
298 | |
299 settings->setUnsafePluginPastingEnabled(false); | |
300 settings->setEditingBehavior( | |
301 static_cast<blink::WebSettings::EditingBehavior>(prefs.editing_behavior)); | |
302 | |
303 settings->setSupportsMultipleWindows(prefs.supports_multiple_windows); | |
304 | |
305 settings->setViewportEnabled(prefs.viewport_enabled); | |
306 settings->setLoadWithOverviewMode(prefs.initialize_at_minimum_page_scale); | |
307 settings->setViewportMetaEnabled(prefs.viewport_meta_enabled); | |
308 settings->setMainFrameResizesAreOrientationChanges( | |
309 prefs.main_frame_resizes_are_orientation_changes); | |
310 | |
311 settings->setSmartInsertDeleteEnabled(prefs.smart_insert_delete_enabled); | |
312 | |
313 settings->setSpatialNavigationEnabled(prefs.spatial_navigation_enabled); | |
314 | |
315 settings->setSelectionIncludesAltImageText(true); | |
316 | |
317 settings->setV8CacheOptions( | |
318 static_cast<blink::WebSettings::V8CacheOptions>(prefs.v8_cache_options)); | |
319 | |
320 settings->setImageAnimationPolicy( | |
321 static_cast<blink::WebSettings::ImageAnimationPolicy>( | |
322 prefs.animation_policy)); | |
323 | |
324 // Needs to happen before setIgnoreVIewportTagScaleLimits below. | |
325 web_view->setDefaultPageScaleLimits(prefs.default_minimum_page_scale_factor, | |
326 prefs.default_maximum_page_scale_factor); | |
327 | |
328 #if defined(OS_ANDROID) | |
329 settings->setAllowCustomScrollbarInMainFrame(false); | |
330 settings->setTextAutosizingEnabled(prefs.text_autosizing_enabled); | |
331 settings->setAccessibilityFontScaleFactor(prefs.font_scale_factor); | |
332 settings->setDeviceScaleAdjustment(prefs.device_scale_adjustment); | |
333 settings->setFullscreenSupported(prefs.fullscreen_supported); | |
334 web_view->setIgnoreViewportTagScaleLimits(prefs.force_enable_zoom); | |
335 settings->setAutoZoomFocusedNodeToLegibleScale(true); | |
336 settings->setDoubleTapToZoomEnabled(prefs.double_tap_to_zoom_enabled); | |
337 settings->setMediaControlsOverlayPlayButtonEnabled(true); | |
338 settings->setMediaPlaybackRequiresUserGesture( | |
339 prefs.user_gesture_required_for_media_playback); | |
340 settings->setDefaultVideoPosterURL( | |
341 base::ASCIIToUTF16(prefs.default_video_poster_url.spec())); | |
342 settings->setSupportDeprecatedTargetDensityDPI( | |
343 prefs.support_deprecated_target_density_dpi); | |
344 settings->setUseLegacyBackgroundSizeShorthandBehavior( | |
345 prefs.use_legacy_background_size_shorthand_behavior); | |
346 settings->setWideViewportQuirkEnabled(prefs.wide_viewport_quirk); | |
347 settings->setUseWideViewport(prefs.use_wide_viewport); | |
348 settings->setForceZeroLayoutHeight(prefs.force_zero_layout_height); | |
349 settings->setViewportMetaLayoutSizeQuirk( | |
350 prefs.viewport_meta_layout_size_quirk); | |
351 settings->setViewportMetaMergeContentQuirk( | |
352 prefs.viewport_meta_merge_content_quirk); | |
353 settings->setViewportMetaNonUserScalableQuirk( | |
354 prefs.viewport_meta_non_user_scalable_quirk); | |
355 settings->setViewportMetaZeroValuesQuirk( | |
356 prefs.viewport_meta_zero_values_quirk); | |
357 settings->setClobberUserAgentInitialScaleQuirk( | |
358 prefs.clobber_user_agent_initial_scale_quirk); | |
359 settings->setIgnoreMainFrameOverflowHiddenQuirk( | |
360 prefs.ignore_main_frame_overflow_hidden_quirk); | |
361 settings->setReportScreenSizeInPhysicalPixelsQuirk( | |
362 prefs.report_screen_size_in_physical_pixels_quirk); | |
363 settings->setPreferHiddenVolumeControls(true); | |
364 settings->setMainFrameClipsContent(!prefs.record_whole_document); | |
365 settings->setShrinksViewportContentToFit(true); | |
366 settings->setUseMobileViewportStyle(true); | |
367 settings->setAutoplayExperimentMode( | |
368 blink::WebString::fromUTF8(prefs.autoplay_experiment_mode)); | |
369 #endif | |
370 | |
371 blink::WebNetworkStateNotifier::setOnLine(prefs.is_online); | |
372 blink::WebNetworkStateNotifier::setWebConnection( | |
373 NetConnectionTypeToWebConnectionType(prefs.net_info_connection_type), | |
374 prefs.net_info_max_bandwidth_mbps); | |
375 | |
376 settings->setPinchOverlayScrollbarThickness( | |
377 prefs.pinch_overlay_scrollbar_thickness); | |
378 settings->setUseSolidColorScrollbars(prefs.use_solid_color_scrollbars); | |
379 | |
380 settings->setShowContextMenuOnMouseUp(prefs.context_menu_on_mouse_up); | |
381 | |
382 #if defined(OS_MACOSX) | |
383 settings->setDoubleTapToZoomEnabled(true); | |
384 web_view->setMaximumLegibleScale(prefs.default_maximum_page_scale_factor); | |
385 #endif | |
386 } | |
387 | |
388 #if defined(OS_LINUX) | |
389 // TODO(rjkroege): Some of this information needs to be plumbed out of | |
390 // mus into the html_viewer. Most of this information needs to be | |
391 // dynamically adjustable (e.g. if I move a mandoline PlatformWindow from | |
392 // a LCD panel to a CRT display on a multiple-monitor system, then mus | |
393 // should communicate this to the app as part of having the app rasterize | |
394 // itself. See http://crbug.com/540054 | |
395 void BlinkSettingsImpl::ApplyFontRendereringSettings() const { | |
396 blink::WebFontRendering::setHinting(SkPaint::kNormal_Hinting); | |
397 blink::WebFontRendering::setAutoHint(false); | |
398 blink::WebFontRendering::setUseBitmaps(false); | |
399 blink::WebFontRendering::setLCDOrder( | |
400 gfx::FontRenderParams::SubpixelRenderingToSkiaLCDOrder( | |
401 gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE)); | |
402 blink::WebFontRendering::setLCDOrientation( | |
403 gfx::FontRenderParams::SubpixelRenderingToSkiaLCDOrientation( | |
404 gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE)); | |
405 blink::WebFontRendering::setAntiAlias(true); | |
406 blink::WebFontRendering::setSubpixelRendering(false); | |
407 blink::WebFontRendering::setSubpixelPositioning(false); | |
408 blink::WebFontRendering::setDefaultFontSize(0); | |
409 } | |
410 #else | |
411 void BlinkSettingsImpl::ApplyFontRendereringSettings() const {} | |
412 #endif | |
413 | |
414 BlinkSettingsImpl::BlinkSettingsImpl() : experimental_webgl_enabled_(false) {} | |
415 | |
416 void BlinkSettingsImpl::Init() { | |
417 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
418 experimental_webgl_enabled_ = !command_line->HasSwitch(kDisableWebGLSwitch); | |
419 } | |
420 | |
421 void BlinkSettingsImpl::ApplySettingsToWebView(blink::WebView* view) const { | |
422 WebPreferences prefs; | |
423 ApplySettings(view, prefs); | |
424 ApplyFontRendereringSettings(); | |
425 } | |
426 | |
427 } // namespace html_viewer | |
OLD | NEW |