OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "web/DevToolsEmulator.h" | 5 #include "web/DevToolsEmulator.h" |
6 | 6 |
7 #include "core/frame/FrameHost.h" | 7 #include "core/frame/FrameHost.h" |
8 #include "core/frame/FrameView.h" | 8 #include "core/frame/FrameView.h" |
9 #include "core/frame/Settings.h" | 9 #include "core/frame/Settings.h" |
10 #include "core/frame/VisualViewport.h" | 10 #include "core/frame/VisualViewport.h" |
11 #include "core/page/Page.h" | 11 #include "core/page/Page.h" |
12 #include "core/style/ComputedStyle.h" | 12 #include "core/style/ComputedStyle.h" |
13 #include "platform/RuntimeEnabledFeatures.h" | 13 #include "platform/RuntimeEnabledFeatures.h" |
| 14 #include "platform/geometry/FloatRect.h" |
| 15 #include "platform/geometry/FloatSize.h" |
| 16 #include "platform/geometry/IntRect.h" |
| 17 #include "platform/geometry/IntSize.h" |
14 #include "public/platform/WebLayerTreeView.h" | 18 #include "public/platform/WebLayerTreeView.h" |
15 #include "web/WebInputEventConversion.h" | 19 #include "web/WebInputEventConversion.h" |
16 #include "web/WebLocalFrameImpl.h" | 20 #include "web/WebLocalFrameImpl.h" |
17 #include "web/WebSettingsImpl.h" | 21 #include "web/WebSettingsImpl.h" |
18 #include "web/WebViewImpl.h" | 22 #include "web/WebViewImpl.h" |
19 #include "wtf/PtrUtil.h" | 23 #include "wtf/PtrUtil.h" |
20 | 24 |
21 namespace { | 25 namespace { |
22 | 26 |
23 static float calculateDeviceScaleAdjustment(int width, int height, float deviceS
caleFactor) | 27 static float calculateDeviceScaleAdjustment(int width, int height, float deviceS
caleFactor) |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 bool emulateMobileEnabled = m_deviceMetricsEnabled && m_emulateMobileEnabled
; | 194 bool emulateMobileEnabled = m_deviceMetricsEnabled && m_emulateMobileEnabled
; |
191 if (!emulateMobileEnabled) | 195 if (!emulateMobileEnabled) |
192 m_webViewImpl->page()->settings().setPrimaryHoverType(hoverType); | 196 m_webViewImpl->page()->settings().setPrimaryHoverType(hoverType); |
193 } | 197 } |
194 | 198 |
195 void DevToolsEmulator::enableDeviceEmulation(const WebDeviceEmulationParams& par
ams) | 199 void DevToolsEmulator::enableDeviceEmulation(const WebDeviceEmulationParams& par
ams) |
196 { | 200 { |
197 if (m_deviceMetricsEnabled | 201 if (m_deviceMetricsEnabled |
198 && m_emulationParams.viewSize == params.viewSize | 202 && m_emulationParams.viewSize == params.viewSize |
199 && m_emulationParams.screenPosition == params.screenPosition | 203 && m_emulationParams.screenPosition == params.screenPosition |
200 && m_emulationParams.deviceScaleFactor == params.deviceScaleFactor | 204 && m_emulationParams.deviceScaleFactor == params.deviceScaleFactor) { |
201 && m_emulationParams.offset == params.offset | |
202 && m_emulationParams.scale == params.scale) { | |
203 return; | 205 return; |
204 } | 206 } |
205 | 207 |
206 m_emulationParams = params; | 208 m_emulationParams = params; |
207 | 209 |
208 if (!m_deviceMetricsEnabled) { | 210 if (!m_deviceMetricsEnabled) { |
209 m_deviceMetricsEnabled = true; | 211 m_deviceMetricsEnabled = true; |
210 if (params.viewSize.width || params.viewSize.height) | 212 if (params.viewSize.width || params.viewSize.height) |
211 m_webViewImpl->setBackgroundColorOverride(Color::darkGray); | 213 m_webViewImpl->setBackgroundColorOverride(Color::darkGray); |
212 } | 214 } |
213 | 215 |
214 m_webViewImpl->page()->settings().setDeviceScaleAdjustment(calculateDeviceSc
aleAdjustment(params.viewSize.width, params.viewSize.height, params.deviceScaleF
actor)); | 216 m_webViewImpl->page()->settings().setDeviceScaleAdjustment(calculateDeviceSc
aleAdjustment(params.viewSize.width, params.viewSize.height, params.deviceScaleF
actor)); |
215 | 217 |
216 if (params.screenPosition == WebDeviceEmulationParams::Mobile) | 218 if (params.screenPosition == WebDeviceEmulationParams::Mobile) |
217 enableMobileEmulation(); | 219 enableMobileEmulation(); |
218 else | 220 else |
219 disableMobileEmulation(); | 221 disableMobileEmulation(); |
220 | 222 |
221 m_webViewImpl->setCompositorDeviceScaleFactorOverride(params.deviceScaleFact
or); | 223 m_webViewImpl->setCompositorDeviceScaleFactorOverride(params.deviceScaleFact
or); |
222 m_webViewImpl->setRootLayerTransform(WebSize(params.offset.x, params.offset.
y), params.scale); | 224 updateRootLayerTransform(); |
223 // TODO(dgozman): mainFrameImpl() is null when it's remote. Figure out how | 225 // TODO(dgozman): mainFrameImpl() is null when it's remote. Figure out how |
224 // we end up with enabling emulation in this case. | 226 // we end up with enabling emulation in this case. |
225 if (m_webViewImpl->mainFrameImpl()) { | 227 if (m_webViewImpl->mainFrameImpl()) { |
226 if (Document* document = m_webViewImpl->mainFrameImpl()->frame()->docume
nt()) | 228 if (Document* document = m_webViewImpl->mainFrameImpl()->frame()->docume
nt()) |
227 document->mediaQueryAffectingValueChanged(); | 229 document->mediaQueryAffectingValueChanged(); |
228 } | 230 } |
229 } | 231 } |
230 | 232 |
231 void DevToolsEmulator::disableDeviceEmulation() | 233 void DevToolsEmulator::disableDeviceEmulation() |
232 { | 234 { |
233 if (!m_deviceMetricsEnabled) | 235 if (!m_deviceMetricsEnabled) |
234 return; | 236 return; |
235 | 237 |
236 m_deviceMetricsEnabled = false; | 238 m_deviceMetricsEnabled = false; |
237 m_webViewImpl->setBackgroundColorOverride(Color::transparent); | 239 m_webViewImpl->setBackgroundColorOverride(Color::transparent); |
238 m_webViewImpl->page()->settings().setDeviceScaleAdjustment(m_embedderDeviceS
caleAdjustment); | 240 m_webViewImpl->page()->settings().setDeviceScaleAdjustment(m_embedderDeviceS
caleAdjustment); |
239 disableMobileEmulation(); | 241 disableMobileEmulation(); |
240 m_webViewImpl->setCompositorDeviceScaleFactorOverride(0.f); | 242 m_webViewImpl->setCompositorDeviceScaleFactorOverride(0.f); |
241 m_webViewImpl->setRootLayerTransform(WebSize(0.f, 0.f), 1.f); | |
242 m_webViewImpl->setPageScaleFactor(1.f); | 243 m_webViewImpl->setPageScaleFactor(1.f); |
| 244 updateRootLayerTransform(); |
243 // mainFrameImpl() could be null during cleanup or remote <-> local swap. | 245 // mainFrameImpl() could be null during cleanup or remote <-> local swap. |
244 if (m_webViewImpl->mainFrameImpl()) { | 246 if (m_webViewImpl->mainFrameImpl()) { |
245 if (Document* document = m_webViewImpl->mainFrameImpl()->frame()->docume
nt()) | 247 if (Document* document = m_webViewImpl->mainFrameImpl()->frame()->docume
nt()) |
246 document->mediaQueryAffectingValueChanged(); | 248 document->mediaQueryAffectingValueChanged(); |
247 } | 249 } |
248 } | 250 } |
249 | 251 |
250 bool DevToolsEmulator::resizeIsDeviceSizeChange() | 252 bool DevToolsEmulator::resizeIsDeviceSizeChange() |
251 { | 253 { |
252 return m_deviceMetricsEnabled && m_emulateMobileEnabled; | 254 return m_deviceMetricsEnabled && m_emulateMobileEnabled; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 m_webViewImpl->setZoomFactorOverride(0); | 314 m_webViewImpl->setZoomFactorOverride(0); |
313 m_emulateMobileEnabled = false; | 315 m_emulateMobileEnabled = false; |
314 m_webViewImpl->setDefaultPageScaleLimits( | 316 m_webViewImpl->setDefaultPageScaleLimits( |
315 m_originalDefaultMinimumPageScaleFactor, | 317 m_originalDefaultMinimumPageScaleFactor, |
316 m_originalDefaultMaximumPageScaleFactor); | 318 m_originalDefaultMaximumPageScaleFactor); |
317 // mainFrameImpl() could be null during cleanup or remote <-> local swap. | 319 // mainFrameImpl() could be null during cleanup or remote <-> local swap. |
318 if (m_webViewImpl->mainFrameImpl()) | 320 if (m_webViewImpl->mainFrameImpl()) |
319 m_webViewImpl->mainFrameImpl()->frameView()->layout(); | 321 m_webViewImpl->mainFrameImpl()->frameView()->layout(); |
320 } | 322 } |
321 | 323 |
| 324 void DevToolsEmulator::forceViewport(const WebFloatPoint& position, float scale) |
| 325 { |
| 326 GraphicsLayer* containerLayer = m_webViewImpl->page()->frameHost().visualVie
wport().containerLayer(); |
| 327 if (!m_viewportOverride) { |
| 328 m_viewportOverride = ViewportOverride(); |
| 329 if (containerLayer) |
| 330 m_viewportOverride->originalVisualViewportMasking = containerLayer->
masksToBounds(); |
| 331 } |
| 332 |
| 333 m_viewportOverride->position = position; |
| 334 m_viewportOverride->scale = scale; |
| 335 |
| 336 // Disable clipping on the visual viewport layer, to ensure the whole area i
s painted. |
| 337 if (containerLayer) |
| 338 containerLayer->setMasksToBounds(false); |
| 339 |
| 340 // Move the correct (scaled) content area to show in the top left of the |
| 341 // CompositorFrame via the root transform. |
| 342 updateRootLayerTransform(); |
| 343 } |
| 344 |
| 345 void DevToolsEmulator::resetViewport() |
| 346 { |
| 347 if (!m_viewportOverride) |
| 348 return; |
| 349 |
| 350 bool originalMasking = m_viewportOverride->originalVisualViewportMasking; |
| 351 m_viewportOverride = WTF::nullopt; |
| 352 |
| 353 // Restore original state. |
| 354 GraphicsLayer* containerLayer = m_webViewImpl->page()->frameHost().visualVie
wport().containerLayer(); |
| 355 if (containerLayer) |
| 356 containerLayer->setMasksToBounds(originalMasking); |
| 357 updateRootLayerTransform(); |
| 358 } |
| 359 |
| 360 void DevToolsEmulator::mainFrameScrollOrScaleChanged() |
| 361 { |
| 362 // Viewport override has to take current page scale and scroll offset into |
| 363 // account. Update the transform if override is active. |
| 364 if (m_viewportOverride) |
| 365 updateRootLayerTransform(); |
| 366 } |
| 367 |
| 368 void DevToolsEmulator::applyDeviceEmulationTransform(TransformationMatrix* trans
form) |
| 369 { |
| 370 if (m_deviceMetricsEnabled) { |
| 371 WebSize offset(m_emulationParams.offset.x, m_emulationParams.offset.y); |
| 372 // Scale first, so that translation is unaffected. |
| 373 transform->translate(offset.width, offset.height); |
| 374 transform->scale(m_emulationParams.scale); |
| 375 if (m_webViewImpl->mainFrameImpl()) |
| 376 m_webViewImpl->mainFrameImpl()->setInputEventsTransformForEmulation(
offset, m_emulationParams.scale); |
| 377 } else { |
| 378 if (m_webViewImpl->mainFrameImpl()) |
| 379 m_webViewImpl->mainFrameImpl()->setInputEventsTransformForEmulation(
WebSize(0, 0), 1.0); |
| 380 } |
| 381 } |
| 382 |
| 383 void DevToolsEmulator::applyViewportOverride(TransformationMatrix* transform) |
| 384 { |
| 385 if (!m_viewportOverride) |
| 386 return; |
| 387 |
| 388 // Transform operations follow in reverse application. |
| 389 // Last, scale positioned area according to override. |
| 390 transform->scale(m_viewportOverride->scale); |
| 391 |
| 392 // Translate while taking into account current scroll offset. |
| 393 WebSize scrollOffset = m_webViewImpl->mainFrame()->scrollOffset(); |
| 394 WebFloatPoint visualOffset = m_webViewImpl->visualViewportOffset(); |
| 395 float scrollX = scrollOffset.width + visualOffset.x; |
| 396 float scrollY = scrollOffset.height + visualOffset.y; |
| 397 transform->translate( |
| 398 -m_viewportOverride->position.x + scrollX, |
| 399 -m_viewportOverride->position.y + scrollY); |
| 400 |
| 401 // First, reverse page scale, so we don't have to take it into account for |
| 402 // calculation of the translation. |
| 403 transform->scale(1. / m_webViewImpl->pageScaleFactor()); |
| 404 } |
| 405 |
| 406 void DevToolsEmulator::updateRootLayerTransform() |
| 407 { |
| 408 TransformationMatrix transform; |
| 409 |
| 410 // Apply device emulation transform first, so that it is affected by the |
| 411 // viewport override. |
| 412 applyViewportOverride(&transform); |
| 413 applyDeviceEmulationTransform(&transform); |
| 414 m_webViewImpl->setRootLayerTransform(transform); |
| 415 } |
| 416 |
| 417 WTF::Optional<IntRect> DevToolsEmulator::visibleContentRectForPainting() const |
| 418 { |
| 419 if (!m_viewportOverride) |
| 420 return WTF::nullopt; |
| 421 FloatSize viewportSize(m_webViewImpl->layerTreeView()->getViewportSize()); |
| 422 viewportSize.scale(1. / m_webViewImpl->layerTreeView()->getDeviceScaleFactor
()); |
| 423 viewportSize.scale(1. / m_viewportOverride->scale); |
| 424 return enclosingIntRect(FloatRect( |
| 425 m_viewportOverride->position.x, |
| 426 m_viewportOverride->position.y, |
| 427 viewportSize.width(), |
| 428 viewportSize.height())); |
| 429 } |
| 430 |
322 void DevToolsEmulator::setTouchEventEmulationEnabled(bool enabled) | 431 void DevToolsEmulator::setTouchEventEmulationEnabled(bool enabled) |
323 { | 432 { |
324 if (m_touchEventEmulationEnabled == enabled) | 433 if (m_touchEventEmulationEnabled == enabled) |
325 return; | 434 return; |
326 if (!m_touchEventEmulationEnabled) { | 435 if (!m_touchEventEmulationEnabled) { |
327 m_originalTouchEnabled = RuntimeEnabledFeatures::touchEnabled(); | 436 m_originalTouchEnabled = RuntimeEnabledFeatures::touchEnabled(); |
328 m_originalDeviceSupportsMouse = m_webViewImpl->page()->settings().device
SupportsMouse(); | 437 m_originalDeviceSupportsMouse = m_webViewImpl->page()->settings().device
SupportsMouse(); |
329 m_originalDeviceSupportsTouch = m_webViewImpl->page()->settings().device
SupportsTouch(); | 438 m_originalDeviceSupportsTouch = m_webViewImpl->page()->settings().device
SupportsTouch(); |
330 m_originalMaxTouchPoints = m_webViewImpl->page()->settings().maxTouchPoi
nts(); | 439 m_originalMaxTouchPoints = m_webViewImpl->page()->settings().maxTouchPoi
nts(); |
331 } | 440 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 m_lastPinchAnchorCss.reset(); | 491 m_lastPinchAnchorCss.reset(); |
383 m_lastPinchAnchorDip.reset(); | 492 m_lastPinchAnchorDip.reset(); |
384 } | 493 } |
385 return true; | 494 return true; |
386 } | 495 } |
387 | 496 |
388 return false; | 497 return false; |
389 } | 498 } |
390 | 499 |
391 } // namespace blink | 500 } // namespace blink |
OLD | NEW |