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 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 } | 216 } |
213 | 217 |
214 m_webViewImpl->page()->settings().setDeviceScaleAdjustment(calculateDeviceSc aleAdjustment(params.viewSize.width, params.viewSize.height, params.deviceScaleF actor)); | 218 m_webViewImpl->page()->settings().setDeviceScaleAdjustment(calculateDeviceSc aleAdjustment(params.viewSize.width, params.viewSize.height, params.deviceScaleF actor)); |
215 | 219 |
216 if (params.screenPosition == WebDeviceEmulationParams::Mobile) | 220 if (params.screenPosition == WebDeviceEmulationParams::Mobile) |
217 enableMobileEmulation(); | 221 enableMobileEmulation(); |
218 else | 222 else |
219 disableMobileEmulation(); | 223 disableMobileEmulation(); |
220 | 224 |
221 m_webViewImpl->setCompositorDeviceScaleFactorOverride(params.deviceScaleFact or); | 225 m_webViewImpl->setCompositorDeviceScaleFactorOverride(params.deviceScaleFact or); |
222 m_webViewImpl->setRootLayerTransform(WebSize(params.offset.x, params.offset. y), params.scale); | 226 updateRootLayerTransform(); |
223 // TODO(dgozman): mainFrameImpl() is null when it's remote. Figure out how | 227 // TODO(dgozman): mainFrameImpl() is null when it's remote. Figure out how |
224 // we end up with enabling emulation in this case. | 228 // we end up with enabling emulation in this case. |
225 if (m_webViewImpl->mainFrameImpl()) { | 229 if (m_webViewImpl->mainFrameImpl()) { |
226 if (Document* document = m_webViewImpl->mainFrameImpl()->frame()->docume nt()) | 230 if (Document* document = m_webViewImpl->mainFrameImpl()->frame()->docume nt()) |
227 document->mediaQueryAffectingValueChanged(); | 231 document->mediaQueryAffectingValueChanged(); |
228 } | 232 } |
229 } | 233 } |
230 | 234 |
231 void DevToolsEmulator::disableDeviceEmulation() | 235 void DevToolsEmulator::disableDeviceEmulation() |
232 { | 236 { |
233 if (!m_deviceMetricsEnabled) | 237 if (!m_deviceMetricsEnabled) |
234 return; | 238 return; |
235 | 239 |
236 m_deviceMetricsEnabled = false; | 240 m_deviceMetricsEnabled = false; |
237 m_webViewImpl->setBackgroundColorOverride(Color::transparent); | 241 m_webViewImpl->setBackgroundColorOverride(Color::transparent); |
238 m_webViewImpl->page()->settings().setDeviceScaleAdjustment(m_embedderDeviceS caleAdjustment); | 242 m_webViewImpl->page()->settings().setDeviceScaleAdjustment(m_embedderDeviceS caleAdjustment); |
239 disableMobileEmulation(); | 243 disableMobileEmulation(); |
240 m_webViewImpl->setCompositorDeviceScaleFactorOverride(0.f); | 244 m_webViewImpl->setCompositorDeviceScaleFactorOverride(0.f); |
241 m_webViewImpl->setRootLayerTransform(WebSize(0.f, 0.f), 1.f); | |
242 m_webViewImpl->setPageScaleFactor(1.f); | 245 m_webViewImpl->setPageScaleFactor(1.f); |
246 updateRootLayerTransform(); | |
243 // mainFrameImpl() could be null during cleanup or remote <-> local swap. | 247 // mainFrameImpl() could be null during cleanup or remote <-> local swap. |
244 if (m_webViewImpl->mainFrameImpl()) { | 248 if (m_webViewImpl->mainFrameImpl()) { |
245 if (Document* document = m_webViewImpl->mainFrameImpl()->frame()->docume nt()) | 249 if (Document* document = m_webViewImpl->mainFrameImpl()->frame()->docume nt()) |
246 document->mediaQueryAffectingValueChanged(); | 250 document->mediaQueryAffectingValueChanged(); |
247 } | 251 } |
248 } | 252 } |
249 | 253 |
250 bool DevToolsEmulator::resizeIsDeviceSizeChange() | 254 bool DevToolsEmulator::resizeIsDeviceSizeChange() |
251 { | 255 { |
252 return m_deviceMetricsEnabled && m_emulateMobileEnabled; | 256 return m_deviceMetricsEnabled && m_emulateMobileEnabled; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 m_webViewImpl->setZoomFactorOverride(0); | 316 m_webViewImpl->setZoomFactorOverride(0); |
313 m_emulateMobileEnabled = false; | 317 m_emulateMobileEnabled = false; |
314 m_webViewImpl->setDefaultPageScaleLimits( | 318 m_webViewImpl->setDefaultPageScaleLimits( |
315 m_originalDefaultMinimumPageScaleFactor, | 319 m_originalDefaultMinimumPageScaleFactor, |
316 m_originalDefaultMaximumPageScaleFactor); | 320 m_originalDefaultMaximumPageScaleFactor); |
317 // mainFrameImpl() could be null during cleanup or remote <-> local swap. | 321 // mainFrameImpl() could be null during cleanup or remote <-> local swap. |
318 if (m_webViewImpl->mainFrameImpl()) | 322 if (m_webViewImpl->mainFrameImpl()) |
319 m_webViewImpl->mainFrameImpl()->frameView()->layout(); | 323 m_webViewImpl->mainFrameImpl()->frameView()->layout(); |
320 } | 324 } |
321 | 325 |
326 void DevToolsEmulator::forceViewport(const WebFloatPoint& position, float scale) | |
327 { | |
328 GraphicsLayer* containerLayer = m_webViewImpl->page()->frameHost().visualVie wport().containerLayer(); | |
329 if (!m_viewportOverride) { | |
330 m_viewportOverride = ViewportOverride(); | |
331 if (containerLayer) | |
332 m_viewportOverride->originalVisualViewportMasking = containerLayer-> masksToBounds(); | |
333 } | |
334 | |
335 m_viewportOverride->position = position; | |
336 m_viewportOverride->scale = scale; | |
337 | |
338 // Disable clipping on the visual viewport layer, to ensure the whole area i s painted. | |
339 if (containerLayer) | |
340 containerLayer->setMasksToBounds(false); | |
dgozman
2016/09/17 01:22:28
This line should be next to |originalVisualViewpor
Eric Seckler
2016/09/22 12:43:00
Done.
| |
341 | |
342 // Move the correct (scaled) content area to show in the top left of the | |
343 // CompositorFrame via the root transform. | |
344 updateRootLayerTransform(); | |
345 } | |
346 | |
347 void DevToolsEmulator::resetViewport() | |
348 { | |
349 if (!m_viewportOverride) | |
350 return; | |
351 | |
352 bool originalMasking = m_viewportOverride->originalVisualViewportMasking; | |
353 m_viewportOverride = WTF::nullopt; | |
354 | |
355 // Restore original state. | |
dgozman
2016/09/17 01:22:28
nit: I don't think this comment brings anything.
Eric Seckler
2016/09/22 12:43:00
Done.
| |
356 GraphicsLayer* containerLayer = m_webViewImpl->page()->frameHost().visualVie wport().containerLayer(); | |
357 if (containerLayer) | |
358 containerLayer->setMasksToBounds(originalMasking); | |
359 updateRootLayerTransform(); | |
360 } | |
361 | |
362 void DevToolsEmulator::mainFrameScrollOrScaleChanged() | |
363 { | |
364 // Viewport override has to take current page scale and scroll offset into | |
365 // account. Update the transform if override is active. | |
366 if (m_viewportOverride) | |
367 updateRootLayerTransform(); | |
368 } | |
369 | |
370 void DevToolsEmulator::applyDeviceEmulationTransform(TransformationMatrix* trans form) | |
371 { | |
372 if (m_deviceMetricsEnabled) { | |
373 WebSize offset(m_emulationParams.offset.x, m_emulationParams.offset.y); | |
374 // Scale first, so that translation is unaffected. | |
375 transform->translate(offset.width, offset.height); | |
376 transform->scale(m_emulationParams.scale); | |
377 if (m_webViewImpl->mainFrameImpl()) | |
378 m_webViewImpl->mainFrameImpl()->setInputEventsTransformForEmulation( offset, m_emulationParams.scale); | |
379 } else { | |
380 if (m_webViewImpl->mainFrameImpl()) | |
381 m_webViewImpl->mainFrameImpl()->setInputEventsTransformForEmulation( WebSize(0, 0), 1.0); | |
382 } | |
383 } | |
384 | |
385 void DevToolsEmulator::applyViewportOverride(TransformationMatrix* transform) | |
386 { | |
387 if (!m_viewportOverride) | |
388 return; | |
389 | |
390 // Transform operations follow in reverse application. | |
391 // Last, scale positioned area according to override. | |
392 transform->scale(m_viewportOverride->scale); | |
393 | |
394 // Translate while taking into account current scroll offset. | |
395 WebSize scrollOffset = m_webViewImpl->mainFrame()->scrollOffset(); | |
396 WebFloatPoint visualOffset = m_webViewImpl->visualViewportOffset(); | |
397 float scrollX = scrollOffset.width + visualOffset.x; | |
398 float scrollY = scrollOffset.height + visualOffset.y; | |
399 transform->translate( | |
400 -m_viewportOverride->position.x + scrollX, | |
401 -m_viewportOverride->position.y + scrollY); | |
402 | |
403 // First, reverse page scale, so we don't have to take it into account for | |
404 // calculation of the translation. | |
405 transform->scale(1. / m_webViewImpl->pageScaleFactor()); | |
406 } | |
407 | |
408 void DevToolsEmulator::updateRootLayerTransform() | |
409 { | |
410 TransformationMatrix transform; | |
411 | |
412 // Apply device emulation transform first, so that it is affected by the | |
413 // viewport override. | |
414 applyViewportOverride(&transform); | |
415 applyDeviceEmulationTransform(&transform); | |
416 m_webViewImpl->setRootLayerTransform(transform); | |
417 } | |
418 | |
419 WTF::Optional<IntRect> DevToolsEmulator::visibleContentRectForPainting() const | |
420 { | |
421 if (!m_viewportOverride) | |
422 return WTF::nullopt; | |
423 FloatSize viewportSize(m_webViewImpl->layerTreeView()->getViewportSize()); | |
424 viewportSize.scale(1. / m_webViewImpl->compositorDeviceScaleFactor()); | |
425 viewportSize.scale(1. / m_viewportOverride->scale); | |
426 return enclosingIntRect(FloatRect( | |
427 m_viewportOverride->position.x, | |
428 m_viewportOverride->position.y, | |
429 viewportSize.width(), | |
430 viewportSize.height())); | |
431 } | |
432 | |
322 void DevToolsEmulator::setTouchEventEmulationEnabled(bool enabled) | 433 void DevToolsEmulator::setTouchEventEmulationEnabled(bool enabled) |
323 { | 434 { |
324 if (m_touchEventEmulationEnabled == enabled) | 435 if (m_touchEventEmulationEnabled == enabled) |
325 return; | 436 return; |
326 if (!m_touchEventEmulationEnabled) { | 437 if (!m_touchEventEmulationEnabled) { |
327 m_originalTouchEnabled = RuntimeEnabledFeatures::touchEnabled(); | 438 m_originalTouchEnabled = RuntimeEnabledFeatures::touchEnabled(); |
328 m_originalDeviceSupportsMouse = m_webViewImpl->page()->settings().device SupportsMouse(); | 439 m_originalDeviceSupportsMouse = m_webViewImpl->page()->settings().device SupportsMouse(); |
329 m_originalDeviceSupportsTouch = m_webViewImpl->page()->settings().device SupportsTouch(); | 440 m_originalDeviceSupportsTouch = m_webViewImpl->page()->settings().device SupportsTouch(); |
330 m_originalMaxTouchPoints = m_webViewImpl->page()->settings().maxTouchPoi nts(); | 441 m_originalMaxTouchPoints = m_webViewImpl->page()->settings().maxTouchPoi nts(); |
331 } | 442 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
382 m_lastPinchAnchorCss.reset(); | 493 m_lastPinchAnchorCss.reset(); |
383 m_lastPinchAnchorDip.reset(); | 494 m_lastPinchAnchorDip.reset(); |
384 } | 495 } |
385 return true; | 496 return true; |
386 } | 497 } |
387 | 498 |
388 return false; | 499 return false; |
389 } | 500 } |
390 | 501 |
391 } // namespace blink | 502 } // namespace blink |
OLD | NEW |