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" |
14 #include "public/platform/WebLayerTreeView.h" | 15 #include "public/platform/WebLayerTreeView.h" |
15 #include "web/WebInputEventConversion.h" | 16 #include "web/WebInputEventConversion.h" |
16 #include "web/WebLocalFrameImpl.h" | 17 #include "web/WebLocalFrameImpl.h" |
17 #include "web/WebSettingsImpl.h" | 18 #include "web/WebSettingsImpl.h" |
18 #include "web/WebViewImpl.h" | 19 #include "web/WebViewImpl.h" |
19 #include "wtf/PtrUtil.h" | 20 #include "wtf/PtrUtil.h" |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 static float calculateDeviceScaleAdjustment(int width, int height, float deviceS
caleFactor) | 24 static float calculateDeviceScaleAdjustment(int width, int height, float deviceS
caleFactor) |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 } | 213 } |
213 | 214 |
214 m_webViewImpl->page()->settings().setDeviceScaleAdjustment(calculateDeviceSc
aleAdjustment(params.viewSize.width, params.viewSize.height, params.deviceScaleF
actor)); | 215 m_webViewImpl->page()->settings().setDeviceScaleAdjustment(calculateDeviceSc
aleAdjustment(params.viewSize.width, params.viewSize.height, params.deviceScaleF
actor)); |
215 | 216 |
216 if (params.screenPosition == WebDeviceEmulationParams::Mobile) | 217 if (params.screenPosition == WebDeviceEmulationParams::Mobile) |
217 enableMobileEmulation(); | 218 enableMobileEmulation(); |
218 else | 219 else |
219 disableMobileEmulation(); | 220 disableMobileEmulation(); |
220 | 221 |
221 m_webViewImpl->setCompositorDeviceScaleFactorOverride(params.deviceScaleFact
or); | 222 m_webViewImpl->setCompositorDeviceScaleFactorOverride(params.deviceScaleFact
or); |
222 m_webViewImpl->setRootLayerTransform(WebSize(params.offset.x, params.offset.
y), params.scale); | 223 updateRootLayerTransform(); |
223 // TODO(dgozman): mainFrameImpl() is null when it's remote. Figure out how | 224 // TODO(dgozman): mainFrameImpl() is null when it's remote. Figure out how |
224 // we end up with enabling emulation in this case. | 225 // we end up with enabling emulation in this case. |
225 if (m_webViewImpl->mainFrameImpl()) { | 226 if (m_webViewImpl->mainFrameImpl()) { |
226 if (Document* document = m_webViewImpl->mainFrameImpl()->frame()->docume
nt()) | 227 if (Document* document = m_webViewImpl->mainFrameImpl()->frame()->docume
nt()) |
227 document->mediaQueryAffectingValueChanged(); | 228 document->mediaQueryAffectingValueChanged(); |
228 } | 229 } |
229 } | 230 } |
230 | 231 |
231 void DevToolsEmulator::disableDeviceEmulation() | 232 void DevToolsEmulator::disableDeviceEmulation() |
232 { | 233 { |
233 if (!m_deviceMetricsEnabled) | 234 if (!m_deviceMetricsEnabled) |
234 return; | 235 return; |
235 | 236 |
236 m_deviceMetricsEnabled = false; | 237 m_deviceMetricsEnabled = false; |
237 m_webViewImpl->setBackgroundColorOverride(Color::transparent); | 238 m_webViewImpl->setBackgroundColorOverride(Color::transparent); |
238 m_webViewImpl->page()->settings().setDeviceScaleAdjustment(m_embedderDeviceS
caleAdjustment); | 239 m_webViewImpl->page()->settings().setDeviceScaleAdjustment(m_embedderDeviceS
caleAdjustment); |
239 disableMobileEmulation(); | 240 disableMobileEmulation(); |
240 m_webViewImpl->setCompositorDeviceScaleFactorOverride(0.f); | 241 m_webViewImpl->setCompositorDeviceScaleFactorOverride(0.f); |
241 m_webViewImpl->setRootLayerTransform(WebSize(0.f, 0.f), 1.f); | |
242 m_webViewImpl->setPageScaleFactor(1.f); | 242 m_webViewImpl->setPageScaleFactor(1.f); |
| 243 updateRootLayerTransform(); |
243 // mainFrameImpl() could be null during cleanup or remote <-> local swap. | 244 // mainFrameImpl() could be null during cleanup or remote <-> local swap. |
244 if (m_webViewImpl->mainFrameImpl()) { | 245 if (m_webViewImpl->mainFrameImpl()) { |
245 if (Document* document = m_webViewImpl->mainFrameImpl()->frame()->docume
nt()) | 246 if (Document* document = m_webViewImpl->mainFrameImpl()->frame()->docume
nt()) |
246 document->mediaQueryAffectingValueChanged(); | 247 document->mediaQueryAffectingValueChanged(); |
247 } | 248 } |
248 } | 249 } |
249 | 250 |
250 bool DevToolsEmulator::resizeIsDeviceSizeChange() | 251 bool DevToolsEmulator::resizeIsDeviceSizeChange() |
251 { | 252 { |
252 return m_deviceMetricsEnabled && m_emulateMobileEnabled; | 253 return m_deviceMetricsEnabled && m_emulateMobileEnabled; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 m_webViewImpl->setZoomFactorOverride(0); | 313 m_webViewImpl->setZoomFactorOverride(0); |
313 m_emulateMobileEnabled = false; | 314 m_emulateMobileEnabled = false; |
314 m_webViewImpl->setDefaultPageScaleLimits( | 315 m_webViewImpl->setDefaultPageScaleLimits( |
315 m_originalDefaultMinimumPageScaleFactor, | 316 m_originalDefaultMinimumPageScaleFactor, |
316 m_originalDefaultMaximumPageScaleFactor); | 317 m_originalDefaultMaximumPageScaleFactor); |
317 // mainFrameImpl() could be null during cleanup or remote <-> local swap. | 318 // mainFrameImpl() could be null during cleanup or remote <-> local swap. |
318 if (m_webViewImpl->mainFrameImpl()) | 319 if (m_webViewImpl->mainFrameImpl()) |
319 m_webViewImpl->mainFrameImpl()->frameView()->layout(); | 320 m_webViewImpl->mainFrameImpl()->frameView()->layout(); |
320 } | 321 } |
321 | 322 |
| 323 void DevToolsEmulator::setVisualTransformOverride(const WebFloatRect& area, floa
t scale) |
| 324 { |
| 325 GraphicsLayer* containerLayer = m_webViewImpl->page()->frameHost().visualVie
wport().containerLayer(); |
| 326 if (!m_visualTransformOverride) { |
| 327 m_visualTransformOverride = VisualTransformOverride(); |
| 328 if (containerLayer) |
| 329 m_visualTransformOverride->originalVisualViewportMasking = container
Layer->masksToBounds(); |
| 330 } |
| 331 |
| 332 m_visualTransformOverride->area = area; |
| 333 m_visualTransformOverride->scale = scale; |
| 334 |
| 335 // Ensure that all content inside the area is painted. |
| 336 if (m_webViewImpl->mainFrameImpl()) |
| 337 m_webViewImpl->mainFrameImpl()->frameView()->setVisibleContentRectForPai
nting(enclosingIntRect(area)); |
| 338 |
| 339 // Disable clipping on the visual viewport layer, to ensure the whole area i
s painted. |
| 340 if (containerLayer) |
| 341 containerLayer->setMasksToBounds(false); |
| 342 |
| 343 // Move the correct (scaled) content area to show in the top left of the |
| 344 // CompositorFrame via the root transform. |
| 345 updateRootLayerTransform(); |
| 346 } |
| 347 |
| 348 void DevToolsEmulator::clearVisualTransformOverride() |
| 349 { |
| 350 if (!m_visualTransformOverride) |
| 351 return; |
| 352 |
| 353 bool originalMasking = m_visualTransformOverride->originalVisualViewportMask
ing; |
| 354 m_visualTransformOverride = WTF::nullopt; |
| 355 |
| 356 // Restore original state. |
| 357 if (m_webViewImpl->mainFrameImpl()) |
| 358 m_webViewImpl->mainFrameImpl()->frameView()->resetVisibleContentRectForP
ainting(); |
| 359 GraphicsLayer* containerLayer = m_webViewImpl->page()->frameHost().visualVie
wport().containerLayer(); |
| 360 if (containerLayer) |
| 361 containerLayer->setMasksToBounds(originalMasking); |
| 362 updateRootLayerTransform(); |
| 363 } |
| 364 |
| 365 void DevToolsEmulator::mainFrameScrollOrScaleChanged() |
| 366 { |
| 367 // Visual transform override has to take current page scale and scroll |
| 368 // offset into account. Update the transform if override is active. |
| 369 if (m_visualTransformOverride) |
| 370 updateRootLayerTransform(); |
| 371 } |
| 372 |
| 373 void DevToolsEmulator::applyDeviceEmulationTransform(TransformationMatrix* trans
form) |
| 374 { |
| 375 if (m_deviceMetricsEnabled) { |
| 376 WebSize offset(m_emulationParams.offset.x, m_emulationParams.offset.y); |
| 377 // Scale first, so that translation is unaffected. |
| 378 transform->translate(offset.width, offset.height); |
| 379 transform->scale(m_emulationParams.scale); |
| 380 if (m_webViewImpl->mainFrameImpl()) |
| 381 m_webViewImpl->mainFrameImpl()->setInputEventsTransformForEmulation(
offset, m_emulationParams.scale); |
| 382 } else { |
| 383 if (m_webViewImpl->mainFrameImpl()) |
| 384 m_webViewImpl->mainFrameImpl()->setInputEventsTransformForEmulation(
WebSize(0, 0), 1.0); |
| 385 } |
| 386 } |
| 387 |
| 388 void DevToolsEmulator::applyVisualTransformOverride(TransformationMatrix* transf
orm) |
| 389 { |
| 390 if (!m_visualTransformOverride) |
| 391 return; |
| 392 |
| 393 // Transform operations follow in reverse application. |
| 394 // Last, scale positioned area according to override. |
| 395 transform->scale(m_visualTransformOverride->scale); |
| 396 |
| 397 // Translate while taking into account current scroll offset. |
| 398 WebSize scrollOffset = m_webViewImpl->mainFrame()->scrollOffset(); |
| 399 WebFloatPoint visualOffset = m_webViewImpl->visualViewportOffset(); |
| 400 float scrollX = scrollOffset.width + visualOffset.x; |
| 401 float scrollY = scrollOffset.height + visualOffset.y; |
| 402 transform->translate( |
| 403 -m_visualTransformOverride->area.x + scrollX, |
| 404 -m_visualTransformOverride->area.y + scrollY); |
| 405 |
| 406 // First, reverse page scale, so we don't have to take it into account for |
| 407 // calculation of the translation. |
| 408 transform->scale(1. / m_webViewImpl->pageScaleFactor()); |
| 409 } |
| 410 |
| 411 void DevToolsEmulator::updateRootLayerTransform() |
| 412 { |
| 413 TransformationMatrix transform; |
| 414 |
| 415 // Apply device emulation transform first, so that it is affected by the |
| 416 // visual transform override. |
| 417 applyVisualTransformOverride(&transform); |
| 418 applyDeviceEmulationTransform(&transform); |
| 419 |
| 420 m_webViewImpl->setRootLayerTransform(transform); |
| 421 } |
| 422 |
322 void DevToolsEmulator::setTouchEventEmulationEnabled(bool enabled) | 423 void DevToolsEmulator::setTouchEventEmulationEnabled(bool enabled) |
323 { | 424 { |
324 if (m_touchEventEmulationEnabled == enabled) | 425 if (m_touchEventEmulationEnabled == enabled) |
325 return; | 426 return; |
326 if (!m_touchEventEmulationEnabled) { | 427 if (!m_touchEventEmulationEnabled) { |
327 m_originalTouchEnabled = RuntimeEnabledFeatures::touchEnabled(); | 428 m_originalTouchEnabled = RuntimeEnabledFeatures::touchEnabled(); |
328 m_originalDeviceSupportsMouse = m_webViewImpl->page()->settings().device
SupportsMouse(); | 429 m_originalDeviceSupportsMouse = m_webViewImpl->page()->settings().device
SupportsMouse(); |
329 m_originalDeviceSupportsTouch = m_webViewImpl->page()->settings().device
SupportsTouch(); | 430 m_originalDeviceSupportsTouch = m_webViewImpl->page()->settings().device
SupportsTouch(); |
330 m_originalMaxTouchPoints = m_webViewImpl->page()->settings().maxTouchPoi
nts(); | 431 m_originalMaxTouchPoints = m_webViewImpl->page()->settings().maxTouchPoi
nts(); |
331 } | 432 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 m_lastPinchAnchorCss.reset(); | 483 m_lastPinchAnchorCss.reset(); |
383 m_lastPinchAnchorDip.reset(); | 484 m_lastPinchAnchorDip.reset(); |
384 } | 485 } |
385 return true; | 486 return true; |
386 } | 487 } |
387 | 488 |
388 return false; | 489 return false; |
389 } | 490 } |
390 | 491 |
391 } // namespace blink | 492 } // namespace blink |
OLD | NEW |