OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/renderer/render_widget.h" | 5 #include "content/renderer/render_widget.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 #include "third_party/WebKit/public/platform/WebString.h" | 46 #include "third_party/WebKit/public/platform/WebString.h" |
47 #include "third_party/WebKit/public/web/WebCursorInfo.h" | 47 #include "third_party/WebKit/public/web/WebCursorInfo.h" |
48 #include "third_party/WebKit/public/web/WebHelperPlugin.h" | 48 #include "third_party/WebKit/public/web/WebHelperPlugin.h" |
49 #include "third_party/WebKit/public/web/WebPagePopup.h" | 49 #include "third_party/WebKit/public/web/WebPagePopup.h" |
50 #include "third_party/WebKit/public/web/WebPopupMenu.h" | 50 #include "third_party/WebKit/public/web/WebPopupMenu.h" |
51 #include "third_party/WebKit/public/web/WebPopupMenuInfo.h" | 51 #include "third_party/WebKit/public/web/WebPopupMenuInfo.h" |
52 #include "third_party/WebKit/public/web/WebRange.h" | 52 #include "third_party/WebKit/public/web/WebRange.h" |
53 #include "third_party/WebKit/public/web/WebScreenInfo.h" | 53 #include "third_party/WebKit/public/web/WebScreenInfo.h" |
54 #include "third_party/skia/include/core/SkShader.h" | 54 #include "third_party/skia/include/core/SkShader.h" |
55 #include "ui/base/ui_base_switches.h" | 55 #include "ui/base/ui_base_switches.h" |
56 #include "ui/gfx/point.h" | 56 #include "ui/gfx/point_conversions.h" |
57 #include "ui/gfx/rect_conversions.h" | 57 #include "ui/gfx/rect_conversions.h" |
58 #include "ui/gfx/size_conversions.h" | 58 #include "ui/gfx/size_conversions.h" |
59 #include "ui/gfx/skia_util.h" | 59 #include "ui/gfx/skia_util.h" |
| 60 #include "ui/gfx/vector2d_conversions.h" |
60 #include "ui/gl/gl_switches.h" | 61 #include "ui/gl/gl_switches.h" |
61 #include "ui/surface/transport_dib.h" | 62 #include "ui/surface/transport_dib.h" |
62 #include "webkit/renderer/compositor_bindings/web_rendering_stats_impl.h" | 63 #include "webkit/renderer/compositor_bindings/web_rendering_stats_impl.h" |
63 | 64 |
64 #if defined(OS_ANDROID) | 65 #if defined(OS_ANDROID) |
65 #include "content/renderer/android/synchronous_compositor_factory.h" | 66 #include "content/renderer/android/synchronous_compositor_factory.h" |
66 #endif | 67 #endif |
67 | 68 |
68 #if defined(OS_POSIX) | 69 #if defined(OS_POSIX) |
69 #include "ipc/ipc_channel_posix.h" | 70 #include "ipc/ipc_channel_posix.h" |
70 #include "third_party/skia/include/core/SkMallocPixelRef.h" | 71 #include "third_party/skia/include/core/SkMallocPixelRef.h" |
71 #include "third_party/skia/include/core/SkPixelRef.h" | 72 #include "third_party/skia/include/core/SkPixelRef.h" |
72 #endif // defined(OS_POSIX) | 73 #endif // defined(OS_POSIX) |
73 | 74 |
74 #include "third_party/WebKit/public/web/WebWidget.h" | 75 #include "third_party/WebKit/public/web/WebWidget.h" |
75 | 76 |
76 using WebKit::WebCompositionUnderline; | 77 using WebKit::WebCompositionUnderline; |
77 using WebKit::WebCursorInfo; | 78 using WebKit::WebCursorInfo; |
78 using WebKit::WebGestureEvent; | 79 using WebKit::WebGestureEvent; |
79 using WebKit::WebInputEvent; | 80 using WebKit::WebInputEvent; |
80 using WebKit::WebKeyboardEvent; | 81 using WebKit::WebKeyboardEvent; |
81 using WebKit::WebMouseEvent; | 82 using WebKit::WebMouseEvent; |
| 83 using WebKit::WebMouseWheelEvent; |
82 using WebKit::WebNavigationPolicy; | 84 using WebKit::WebNavigationPolicy; |
83 using WebKit::WebPagePopup; | 85 using WebKit::WebPagePopup; |
84 using WebKit::WebPoint; | 86 using WebKit::WebPoint; |
85 using WebKit::WebPopupMenu; | 87 using WebKit::WebPopupMenu; |
86 using WebKit::WebPopupMenuInfo; | 88 using WebKit::WebPopupMenuInfo; |
87 using WebKit::WebPopupType; | 89 using WebKit::WebPopupType; |
88 using WebKit::WebRange; | 90 using WebKit::WebRange; |
89 using WebKit::WebRect; | 91 using WebKit::WebRect; |
90 using WebKit::WebScreenInfo; | 92 using WebKit::WebScreenInfo; |
91 using WebKit::WebSize; | 93 using WebKit::WebSize; |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose) | 381 IPC_MESSAGE_HANDLER(ViewMsg_Close, OnClose) |
380 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck) | 382 IPC_MESSAGE_HANDLER(ViewMsg_CreatingNew_ACK, OnCreatingNewAck) |
381 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize) | 383 IPC_MESSAGE_HANDLER(ViewMsg_Resize, OnResize) |
382 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect) | 384 IPC_MESSAGE_HANDLER(ViewMsg_ChangeResizeRect, OnChangeResizeRect) |
383 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden) | 385 IPC_MESSAGE_HANDLER(ViewMsg_WasHidden, OnWasHidden) |
384 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown) | 386 IPC_MESSAGE_HANDLER(ViewMsg_WasShown, OnWasShown) |
385 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut) | 387 IPC_MESSAGE_HANDLER(ViewMsg_WasSwappedOut, OnWasSwappedOut) |
386 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck) | 388 IPC_MESSAGE_HANDLER(ViewMsg_UpdateRect_ACK, OnUpdateRectAck) |
387 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK, | 389 IPC_MESSAGE_HANDLER(ViewMsg_SwapBuffers_ACK, |
388 OnViewContextSwapBuffersComplete) | 390 OnViewContextSwapBuffersComplete) |
| 391 IPC_MESSAGE_HANDLER(ViewMsg_EmulateDevice_ACK, OnEmulateDeviceAck) |
389 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive) | 392 IPC_MESSAGE_HANDLER(ViewMsg_SetInputMethodActive, OnSetInputMethodActive) |
390 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition) | 393 IPC_MESSAGE_HANDLER(ViewMsg_ImeSetComposition, OnImeSetComposition) |
391 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition) | 394 IPC_MESSAGE_HANDLER(ViewMsg_ImeConfirmComposition, OnImeConfirmComposition) |
392 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnPaintAtSize) | 395 IPC_MESSAGE_HANDLER(ViewMsg_PaintAtSize, OnPaintAtSize) |
393 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnRepaint) | 396 IPC_MESSAGE_HANDLER(ViewMsg_Repaint, OnRepaint) |
394 IPC_MESSAGE_HANDLER(ViewMsg_SyntheticGestureCompleted, | 397 IPC_MESSAGE_HANDLER(ViewMsg_SyntheticGestureCompleted, |
395 OnSyntheticGestureCompleted) | 398 OnSyntheticGestureCompleted) |
396 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection) | 399 IPC_MESSAGE_HANDLER(ViewMsg_SetTextDirection, OnSetTextDirection) |
397 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck) | 400 IPC_MESSAGE_HANDLER(ViewMsg_Move_ACK, OnRequestMoveAck) |
398 IPC_MESSAGE_HANDLER(ViewMsg_UpdateScreenRects, OnUpdateScreenRects) | 401 IPC_MESSAGE_HANDLER(ViewMsg_UpdateScreenRects, OnUpdateScreenRects) |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 // If there is a Send call on the stack, then it could be dangerous to close | 518 // If there is a Send call on the stack, then it could be dangerous to close |
516 // now. Post a task that only gets invoked when there are no nested message | 519 // now. Post a task that only gets invoked when there are no nested message |
517 // loops. | 520 // loops. |
518 base::MessageLoop::current()->PostNonNestableTask( | 521 base::MessageLoop::current()->PostNonNestableTask( |
519 FROM_HERE, base::Bind(&RenderWidget::Close, this)); | 522 FROM_HERE, base::Bind(&RenderWidget::Close, this)); |
520 | 523 |
521 // Balances the AddRef taken when we called AddRoute. | 524 // Balances the AddRef taken when we called AddRoute. |
522 Release(); | 525 Release(); |
523 } | 526 } |
524 | 527 |
| 528 void RenderWidget::EmulateDevice( |
| 529 bool enabled, |
| 530 const gfx::Size& device_size, |
| 531 const gfx::Rect& widget_rect, |
| 532 float device_scale_factor, |
| 533 bool fit_to_view) { |
| 534 pending_device_emulation_enabled_ = enabled; |
| 535 if (enabled) { |
| 536 pending_device_emulation_helper_.reset(new DeviceEmulationHelper( |
| 537 device_size, |
| 538 widget_rect, |
| 539 device_scale_factor, |
| 540 fit_to_view)); |
| 541 } else { |
| 542 pending_device_emulation_helper_.reset(); |
| 543 } |
| 544 Send(new ViewHostMsg_EmulateDevice(routing_id_, enabled)); |
| 545 } |
| 546 |
| 547 void RenderWidget::OnEmulateDeviceAck(bool enabled) { |
| 548 // We may have got another emulate device request while sending message |
| 549 // to the host. If the host idea of whether device emulation is enabled |
| 550 // differs, wait for another ack with the right |enabled| value. |
| 551 if (enabled != pending_device_emulation_enabled_) |
| 552 return; |
| 553 |
| 554 if (pending_device_emulation_enabled_) { |
| 555 if (!device_emulation_helper_) { |
| 556 device_emulation_helper_ = pending_device_emulation_helper_.Pass(); |
| 557 device_emulation_helper_->BeginEmulation(this); |
| 558 } else { |
| 559 device_emulation_helper_->ChangeEmulationParams( |
| 560 this, pending_device_emulation_helper_.get()); |
| 561 pending_device_emulation_helper_.reset(); |
| 562 } |
| 563 } else { |
| 564 if (device_emulation_helper_) |
| 565 device_emulation_helper_->EndEmulation(this); |
| 566 device_emulation_helper_.reset(); |
| 567 } |
| 568 } |
| 569 |
| 570 void RenderWidget::SetDeviceEmulationScale(float scale) { |
| 571 // This is only supported in RenderView. |
| 572 NOTREACHED(); |
| 573 } |
| 574 |
525 // Got a response from the browser after the renderer decided to create a new | 575 // Got a response from the browser after the renderer decided to create a new |
526 // view. | 576 // view. |
527 void RenderWidget::OnCreatingNewAck() { | 577 void RenderWidget::OnCreatingNewAck() { |
528 DCHECK(routing_id_ != MSG_ROUTING_NONE); | 578 DCHECK(routing_id_ != MSG_ROUTING_NONE); |
529 | 579 |
530 CompleteInit(); | 580 CompleteInit(); |
531 } | 581 } |
532 | 582 |
533 void RenderWidget::OnResize(const ViewMsg_Resize_Params& params) { | 583 void RenderWidget::OnResize(const ViewMsg_Resize_Params& params) { |
| 584 if (device_emulation_helper_) { |
| 585 device_emulation_helper_->OnResizeMessage(this, params); |
| 586 return; |
| 587 } |
| 588 |
534 screen_info_ = params.screen_info; | 589 screen_info_ = params.screen_info; |
535 SetDeviceScaleFactor(screen_info_.deviceScaleFactor); | 590 SetDeviceScaleFactor(screen_info_.deviceScaleFactor); |
536 Resize(params.new_size, params.physical_backing_size, | 591 Resize(params.new_size, params.physical_backing_size, |
537 params.overdraw_bottom_height, params.resizer_rect, | 592 params.overdraw_bottom_height, params.resizer_rect, |
538 params.is_fullscreen, SEND_RESIZE_ACK); | 593 params.is_fullscreen, SEND_RESIZE_ACK); |
539 } | 594 } |
540 | 595 |
541 void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) { | 596 void RenderWidget::OnChangeResizeRect(const gfx::Rect& resizer_rect) { |
542 if (resizer_rect_ != resizer_rect) { | 597 if (resizer_rect_ != resizer_rect) { |
543 gfx::Rect view_rect(size_); | 598 gfx::Rect view_rect(size_); |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
806 } | 861 } |
807 | 862 |
808 // Continue painting if necessary... | 863 // Continue painting if necessary... |
809 DoDeferredUpdateAndSendInputAck(); | 864 DoDeferredUpdateAndSendInputAck(); |
810 } | 865 } |
811 | 866 |
812 void RenderWidget::OnHandleInputEvent(const WebKit::WebInputEvent* input_event, | 867 void RenderWidget::OnHandleInputEvent(const WebKit::WebInputEvent* input_event, |
813 const ui::LatencyInfo& latency_info, | 868 const ui::LatencyInfo& latency_info, |
814 bool is_keyboard_shortcut) { | 869 bool is_keyboard_shortcut) { |
815 handling_input_event_ = true; | 870 handling_input_event_ = true; |
| 871 |
| 872 scoped_ptr<const WebKit::WebInputEvent> owner; |
| 873 if (device_emulation_helper_) { |
| 874 input_event = device_emulation_helper_->ConvertInputEventToEmulated( |
| 875 this, input_event); |
| 876 if (input_event) |
| 877 owner.reset(input_event); |
| 878 } |
| 879 if (popup_device_emulation_helper_) { |
| 880 // input_event = popup_device_emulation_helper_->ConvertInputEventToEmulated( |
| 881 // this, input_event); |
| 882 // if (input_event) |
| 883 // owner.reset(input_event); |
| 884 } |
| 885 |
816 if (!input_event) { | 886 if (!input_event) { |
817 handling_input_event_ = false; | 887 handling_input_event_ = false; |
818 return; | 888 return; |
819 } | 889 } |
820 | 890 |
821 const char* const event_name = GetEventName(input_event->type); | 891 const char* const event_name = GetEventName(input_event->type); |
822 TRACE_EVENT1("renderer", "RenderWidget::OnHandleInputEvent", | 892 TRACE_EVENT1("renderer", "RenderWidget::OnHandleInputEvent", |
823 "event", event_name); | 893 "event", event_name); |
824 | 894 |
825 if (compositor_) | 895 if (compositor_) |
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1418 compositor_->Composite(frame_begin_time); | 1488 compositor_->Composite(frame_begin_time); |
1419 } | 1489 } |
1420 | 1490 |
1421 /////////////////////////////////////////////////////////////////////////////// | 1491 /////////////////////////////////////////////////////////////////////////////// |
1422 // WebWidgetClient | 1492 // WebWidgetClient |
1423 | 1493 |
1424 void RenderWidget::didInvalidateRect(const WebRect& rect) { | 1494 void RenderWidget::didInvalidateRect(const WebRect& rect) { |
1425 // The invalidated rect might be outside the bounds of the view. | 1495 // The invalidated rect might be outside the bounds of the view. |
1426 gfx::Rect view_rect(size_); | 1496 gfx::Rect view_rect(size_); |
1427 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, rect); | 1497 gfx::Rect damaged_rect = gfx::IntersectRects(view_rect, rect); |
| 1498 if (popup_device_emulation_helper_) |
| 1499 damaged_rect = view_rect; |
1428 if (damaged_rect.IsEmpty()) | 1500 if (damaged_rect.IsEmpty()) |
1429 return; | 1501 return; |
1430 | 1502 |
1431 paint_aggregator_.InvalidateRect(damaged_rect); | 1503 paint_aggregator_.InvalidateRect(damaged_rect); |
1432 | 1504 |
1433 // We may not need to schedule another call to DoDeferredUpdate. | 1505 // We may not need to schedule another call to DoDeferredUpdate. |
1434 if (invalidation_task_posted_) | 1506 if (invalidation_task_posted_) |
1435 return; | 1507 return; |
1436 if (!paint_aggregator_.HasPendingUpdate()) | 1508 if (!paint_aggregator_.HasPendingUpdate()) |
1437 return; | 1509 return; |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1769 return pending_window_rect_; | 1841 return pending_window_rect_; |
1770 | 1842 |
1771 return view_screen_rect_; | 1843 return view_screen_rect_; |
1772 } | 1844 } |
1773 | 1845 |
1774 void RenderWidget::setToolTipText(const WebKit::WebString& text, | 1846 void RenderWidget::setToolTipText(const WebKit::WebString& text, |
1775 WebTextDirection hint) { | 1847 WebTextDirection hint) { |
1776 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint)); | 1848 Send(new ViewHostMsg_SetTooltipText(routing_id_, text, hint)); |
1777 } | 1849 } |
1778 | 1850 |
1779 void RenderWidget::setWindowRect(const WebRect& pos) { | 1851 void RenderWidget::setWindowRect(const WebRect& rect) { |
| 1852 WebRect pos = rect; |
| 1853 if (popup_device_emulation_helper_) { |
| 1854 pos = popup_device_emulation_helper_->ConvertPopupScreenRectFromEmulated( |
| 1855 this, rect); |
| 1856 } |
| 1857 |
1780 if (did_show_) { | 1858 if (did_show_) { |
1781 if (!RenderThreadImpl::current()->layout_test_mode()) { | 1859 if (!RenderThreadImpl::current()->layout_test_mode()) { |
1782 Send(new ViewHostMsg_RequestMove(routing_id_, pos)); | 1860 Send(new ViewHostMsg_RequestMove(routing_id_, pos)); |
1783 SetPendingWindowRect(pos); | 1861 SetPendingWindowRect(pos); |
1784 } else { | 1862 } else { |
1785 WebSize new_size(pos.width, pos.height); | 1863 WebSize new_size(pos.width, pos.height); |
1786 Resize(new_size, new_size, overdraw_bottom_height_, | 1864 Resize(new_size, new_size, overdraw_bottom_height_, |
1787 WebRect(), is_fullscreen_, NO_RESIZE_ACK); | 1865 WebRect(), is_fullscreen_, NO_RESIZE_ACK); |
1788 view_screen_rect_ = pos; | 1866 view_screen_rect_ = pos; |
1789 window_screen_rect_ = pos; | 1867 window_screen_rect_ = pos; |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2010 } | 2088 } |
2011 | 2089 |
2012 void RenderWidget::OnSetTextDirection(WebTextDirection direction) { | 2090 void RenderWidget::OnSetTextDirection(WebTextDirection direction) { |
2013 if (!webwidget_) | 2091 if (!webwidget_) |
2014 return; | 2092 return; |
2015 webwidget_->setTextDirection(direction); | 2093 webwidget_->setTextDirection(direction); |
2016 } | 2094 } |
2017 | 2095 |
2018 void RenderWidget::OnUpdateScreenRects(const gfx::Rect& view_screen_rect, | 2096 void RenderWidget::OnUpdateScreenRects(const gfx::Rect& view_screen_rect, |
2019 const gfx::Rect& window_screen_rect) { | 2097 const gfx::Rect& window_screen_rect) { |
| 2098 if (device_emulation_helper_) { |
| 2099 device_emulation_helper_->OnUpdateScreenRectsMessage( |
| 2100 this, view_screen_rect, window_screen_rect); |
| 2101 return; |
| 2102 } |
2020 view_screen_rect_ = view_screen_rect; | 2103 view_screen_rect_ = view_screen_rect; |
2021 window_screen_rect_ = window_screen_rect; | 2104 window_screen_rect_ = window_screen_rect; |
2022 Send(new ViewHostMsg_UpdateScreenRects_ACK(routing_id())); | 2105 Send(new ViewHostMsg_UpdateScreenRects_ACK(routing_id())); |
2023 } | 2106 } |
2024 | 2107 |
2025 #if defined(OS_ANDROID) | 2108 #if defined(OS_ANDROID) |
2026 void RenderWidget::OnImeBatchStateChanged(bool is_begin) { | 2109 void RenderWidget::OnImeBatchStateChanged(bool is_begin) { |
2027 Send(new ViewHostMsg_ImeBatchStateChanged_ACK(routing_id(), is_begin)); | 2110 Send(new ViewHostMsg_ImeBatchStateChanged_ACK(routing_id(), is_begin)); |
2028 } | 2111 } |
2029 | 2112 |
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2524 weak_ptr_factory_.GetWeakPtr())); | 2607 weak_ptr_factory_.GetWeakPtr())); |
2525 | 2608 |
2526 if (!context->InitializeWithDefaultBufferSizes( | 2609 if (!context->InitializeWithDefaultBufferSizes( |
2527 attributes, | 2610 attributes, |
2528 false /* bind generates resources */, | 2611 false /* bind generates resources */, |
2529 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE)
) | 2612 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE)
) |
2530 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>(); | 2613 return scoped_ptr<WebGraphicsContext3DCommandBufferImpl>(); |
2531 return context.Pass(); | 2614 return context.Pass(); |
2532 } | 2615 } |
2533 | 2616 |
| 2617 // This class scales input event's coordinates, and then sets screen coordinates |
| 2618 // to emulate specific view position on the screen. |
| 2619 class DeviceEmulationEventRewriter : public InputEventRewriter { |
| 2620 public: |
| 2621 DeviceEmulationEventRewriter() {} |
| 2622 virtual ~DeviceEmulationEventRewriter() {} |
| 2623 |
| 2624 void SetParams(float scale, const gfx::Point& origin); |
| 2625 |
| 2626 // InputEventRewriter implementation. |
| 2627 virtual WebKit::WebInputEvent* RewriteInputEvent( |
| 2628 const WebKit::WebInputEvent* event); |
| 2629 |
| 2630 private: |
| 2631 void ConvertMouseEventToEmulated(WebKit::WebMouseEvent* event); |
| 2632 void ConvertTouchPointToEmulated(WebKit::WebTouchPoint* point); |
| 2633 |
| 2634 // Event coordinates are scaled by this value. |
| 2635 float scale_; |
| 2636 |
| 2637 // Widget screen position, used to set proper event screen coordinates. |
| 2638 gfx::Point origin_; |
| 2639 }; |
| 2640 |
| 2641 void DeviceEmulationEventRewriter::SetParams( |
| 2642 float scale, const gfx::Point& origin) { |
| 2643 scale_ = scale; |
| 2644 origin_ = origin; |
| 2645 } |
| 2646 |
| 2647 WebKit::WebInputEvent* DeviceEmulationEventRewriter::RewriteInputEvent( |
| 2648 const WebKit::WebInputEvent* event) { |
| 2649 if (!event) |
| 2650 return NULL; |
| 2651 |
| 2652 if (WebInputEvent::isKeyboardEventType(event->type)) { |
| 2653 const WebKeyboardEvent& keyboard_event = |
| 2654 *static_cast<const WebKeyboardEvent*>(event); |
| 2655 WebKeyboardEvent* result = new WebKeyboardEvent(keyboard_event); |
| 2656 return result; |
| 2657 } |
| 2658 |
| 2659 if (WebInputEvent::isMouseEventType(event->type)) { |
| 2660 const WebMouseEvent& mouse_event = |
| 2661 *static_cast<const WebMouseEvent*>(event); |
| 2662 WebMouseEvent* result = new WebMouseEvent(mouse_event); |
| 2663 ConvertMouseEventToEmulated(result); |
| 2664 return result; |
| 2665 } |
| 2666 |
| 2667 if (WebInputEvent::MouseWheel == event->type) { |
| 2668 const WebMouseWheelEvent& mouse_wheel_event = |
| 2669 *static_cast<const WebMouseWheelEvent*>(event); |
| 2670 WebMouseWheelEvent* result = new WebMouseWheelEvent(mouse_wheel_event); |
| 2671 ConvertMouseEventToEmulated(result); |
| 2672 return result; |
| 2673 } |
| 2674 |
| 2675 if (WebInputEvent::isGestureEventType(event->type)) { |
| 2676 const WebGestureEvent& gesture_event = |
| 2677 *static_cast<const WebGestureEvent*>(event); |
| 2678 WebGestureEvent* result = new WebGestureEvent(gesture_event); |
| 2679 result->x = 1.f / scale_ * result->x; |
| 2680 result->y = 1.f / scale_ * result->y; |
| 2681 result->globalX = origin_.x() + result->x; |
| 2682 result->globalY = origin_.y() + result->y; |
| 2683 return result; |
| 2684 } |
| 2685 |
| 2686 if (WebInputEvent::isTouchEventType(event->type)) { |
| 2687 const WebTouchEvent& touch_event = |
| 2688 *static_cast<const WebTouchEvent*>(event); |
| 2689 WebTouchEvent* result = new WebTouchEvent(touch_event); |
| 2690 for (size_t index = 0; index < result->touchesLength; ++index) { |
| 2691 ConvertTouchPointToEmulated(&result->touches[index]); |
| 2692 } |
| 2693 for (size_t index = 0; index < result->changedTouchesLength; ++index) { |
| 2694 ConvertTouchPointToEmulated(&result->changedTouches[index]); |
| 2695 } |
| 2696 for (size_t index = 0; index < result->targetTouchesLength; ++index) { |
| 2697 ConvertTouchPointToEmulated(&result->targetTouches[index]); |
| 2698 } |
| 2699 return result; |
| 2700 } |
| 2701 |
| 2702 return NULL; |
| 2703 } |
| 2704 |
| 2705 void DeviceEmulationEventRewriter::ConvertMouseEventToEmulated( |
| 2706 WebKit::WebMouseEvent* event) { |
| 2707 event->x = 1.f / scale_ * event->x; |
| 2708 event->y = 1.f / scale_ * event->y; |
| 2709 event->movementX = 1.f / scale_ * event->movementX; |
| 2710 event->movementY = 1.f / scale_ * event->movementY; |
| 2711 event->windowX = origin_.x() + event->x; |
| 2712 event->windowY = origin_.y() + event->y; |
| 2713 event->globalX = event->windowX; |
| 2714 event->globalY = event->windowY; |
| 2715 } |
| 2716 |
| 2717 void DeviceEmulationEventRewriter::ConvertTouchPointToEmulated( |
| 2718 WebKit::WebTouchPoint* point) { |
| 2719 point->position.x = 1.f / scale_ * point->position.x; |
| 2720 point->position.y = 1.f / scale_ * point->position.y; |
| 2721 point->screenPosition.x = origin_.x() + point->position.x; |
| 2722 point->screenPosition.y = origin_.y() + point->position.y; |
| 2723 } |
| 2724 |
| 2725 |
| 2726 RenderWidget::DeviceEmulationHelper::DeviceEmulationHelper( |
| 2727 const gfx::Size& device_size, |
| 2728 const gfx::Rect& widget_rect, |
| 2729 float device_scale_factor, |
| 2730 bool fit_to_view) |
| 2731 : device_size_(device_size), |
| 2732 widget_rect_(widget_rect), |
| 2733 device_scale_factor_(device_scale_factor), |
| 2734 fit_to_view_(fit_to_view), |
| 2735 weak_ptr_factory_(this) { |
| 2736 } |
| 2737 |
| 2738 RenderWidget::DeviceEmulationHelper::~DeviceEmulationHelper() { |
| 2739 } |
| 2740 |
| 2741 void RenderWidget::DeviceEmulationHelper::BeginEmulation(RenderWidget* widget) { |
| 2742 original_size_ = widget->size_; |
| 2743 original_physical_backing_size_ = widget->physical_backing_size_; |
| 2744 original_screen_info_ = widget->screen_info_; |
| 2745 original_view_screen_rect_ = widget->view_screen_rect_; |
| 2746 original_window_screen_rect_ = widget->window_screen_rect_; |
| 2747 input_event_rewriter_.reset(new DeviceEmulationEventRewriter); |
| 2748 Apply(widget, widget->overdraw_bottom_height_, widget->resizer_rect_, |
| 2749 widget->is_fullscreen_); |
| 2750 } |
| 2751 |
| 2752 void RenderWidget::DeviceEmulationHelper::ChangeEmulationParams( |
| 2753 RenderWidget* widget, DeviceEmulationHelper* params) { |
| 2754 device_size_ = params->device_size_; |
| 2755 widget_rect_ = params->widget_rect_; |
| 2756 device_scale_factor_ = params->device_scale_factor_; |
| 2757 fit_to_view_ = params->fit_to_view_; |
| 2758 Apply(widget, widget->overdraw_bottom_height_, widget->resizer_rect_, |
| 2759 widget->is_fullscreen_); |
| 2760 } |
| 2761 |
| 2762 void RenderWidget::DeviceEmulationHelper::EndEmulation(RenderWidget* widget) { |
| 2763 widget->screen_info_ = original_screen_info_; |
| 2764 if (widget->compositor_) { |
| 2765 // Passing zero cancels the override. |
| 2766 widget->compositor_->OverrideDeviceScaleFactor(0.f); |
| 2767 } |
| 2768 widget->SetDeviceScaleFactor(original_screen_info_.deviceScaleFactor); |
| 2769 widget->SetDeviceEmulationScale(1.f); |
| 2770 widget->view_screen_rect_ = original_view_screen_rect_; |
| 2771 widget->window_screen_rect_ = original_window_screen_rect_; |
| 2772 widget->Resize(original_size_, |
| 2773 original_physical_backing_size_, |
| 2774 widget->overdraw_bottom_height_, widget->resizer_rect_, |
| 2775 widget->is_fullscreen_, NO_RESIZE_ACK); |
| 2776 } |
| 2777 |
| 2778 void RenderWidget::DeviceEmulationHelper::Apply(RenderWidget* widget, |
| 2779 float overdraw_bottom_height, gfx::Rect resizer_rect, bool is_fullscreen) { |
| 2780 if (fit_to_view_) { |
| 2781 float width_ratio = !original_size_.width() ? 1.f : |
| 2782 static_cast<float>(widget_rect_.width()) / original_size_.width(); |
| 2783 float height_ratio = !original_size_.height() ? 1.f : |
| 2784 static_cast<float>(widget_rect_.height()) / original_size_.height(); |
| 2785 float ratio = std::max(1.0f, std::max(width_ratio, height_ratio)); |
| 2786 scale_ = 1.f / ratio; |
| 2787 } else { |
| 2788 scale_ = 1.f; |
| 2789 } |
| 2790 |
| 2791 input_event_rewriter_->SetParams(scale_, widget_rect_.origin()); |
| 2792 InputHandlerManager* input_handler_manager = |
| 2793 RenderThreadImpl::current()->input_handler_manager(); |
| 2794 if (input_handler_manager) { |
| 2795 // Make a copy to be owned by InputHandlerManager. |
| 2796 scoped_ptr<DeviceEmulationEventRewriter> copy( |
| 2797 new DeviceEmulationEventRewriter(*input_event_rewriter_)); |
| 2798 input_handler_manager->SetInputEventRewriter( |
| 2799 widget->routing_id_, copy.PassAs<InputEventRewriter>()); |
| 2800 } |
| 2801 |
| 2802 widget->screen_info_.rect = gfx::Rect(device_size_); |
| 2803 widget->screen_info_.availableRect = gfx::Rect(device_size_); |
| 2804 widget->screen_info_.deviceScaleFactor = device_scale_factor_; |
| 2805 |
| 2806 if (widget->compositor_) { |
| 2807 // We keep the real device scale factor in compositor to prevent unnecessary |
| 2808 // scaling on browser side. This override ensures that compositor |
| 2809 // will not change scale factor due to WebLayerTreeView's call. |
| 2810 widget->compositor_->OverrideDeviceScaleFactor( |
| 2811 original_screen_info_.deviceScaleFactor); |
| 2812 } |
| 2813 |
| 2814 // In order to fit into view, WebView applies scaling transform to the |
| 2815 // root graphics layer. |
| 2816 widget->SetDeviceEmulationScale(scale_); |
| 2817 |
| 2818 widget->SetDeviceScaleFactor(device_scale_factor_); |
| 2819 widget->view_screen_rect_ = widget_rect_; |
| 2820 widget->window_screen_rect_ = widget->screen_info_.availableRect; |
| 2821 |
| 2822 gfx::Size physical_backing_size = gfx::ToCeiledSize(gfx::ScaleSize( |
| 2823 widget_rect_.size(), original_screen_info_.deviceScaleFactor * scale_)); |
| 2824 widget->Resize(widget_rect_.size(), physical_backing_size, |
| 2825 overdraw_bottom_height, resizer_rect, |
| 2826 is_fullscreen, NO_RESIZE_ACK); |
| 2827 } |
| 2828 |
| 2829 void RenderWidget::DeviceEmulationHelper::OnResizeMessage( |
| 2830 RenderWidget* widget, const ViewMsg_Resize_Params& params) { |
| 2831 bool need_ack = params.new_size != original_size_; |
| 2832 original_size_ = params.new_size; |
| 2833 original_physical_backing_size_ = params.physical_backing_size; |
| 2834 original_screen_info_ = params.screen_info; |
| 2835 Apply(widget, params.overdraw_bottom_height, params.resizer_rect, |
| 2836 params.is_fullscreen); |
| 2837 |
| 2838 if (need_ack) { |
| 2839 widget->set_next_paint_is_resize_ack(); |
| 2840 if (widget->is_accelerated_compositing_active_ && widget->compositor_) |
| 2841 widget->compositor_->SetNeedsRedrawRect(gfx::Rect(widget->size_)); |
| 2842 else |
| 2843 widget->didInvalidateRect(gfx::Rect(widget->size_)); |
| 2844 } |
| 2845 } |
| 2846 |
| 2847 void RenderWidget::DeviceEmulationHelper::OnUpdateScreenRectsMessage( |
| 2848 RenderWidget* widget, |
| 2849 const gfx::Rect view_screen_rect, |
| 2850 const gfx::Rect window_screen_rect) { |
| 2851 original_view_screen_rect_ = view_screen_rect; |
| 2852 original_window_screen_rect_ = window_screen_rect; |
| 2853 widget->Send(new ViewHostMsg_UpdateScreenRects_ACK(widget->routing_id())); |
| 2854 } |
| 2855 |
| 2856 void RenderWidget::DeviceEmulationHelper::OnShowContextMenu( |
| 2857 RenderWidget* widget, ContextMenuParams* params) { |
| 2858 params->x *= scale_; |
| 2859 params->y *= scale_; |
| 2860 } |
| 2861 |
| 2862 WebKit::WebInputEvent* RenderWidget::DeviceEmulationHelper::ConvertInputEventToE
mulated( |
| 2863 RenderWidget* widget, const WebKit::WebInputEvent* event) { |
| 2864 return input_event_rewriter_->RewriteInputEvent(event); |
| 2865 } |
| 2866 |
| 2867 void RenderWidget::DeviceEmulationHelper::PopupCreated( |
| 2868 RenderWidget* widget, RenderWidget* popup) { |
| 2869 popup->popup_device_emulation_helper_ = weak_ptr_factory_.GetWeakPtr(); |
| 2870 } |
| 2871 |
| 2872 WebKit::WebRect RenderWidget::DeviceEmulationHelper::ConvertPopupScreenRectFromE
mulated( |
| 2873 RenderWidget* popup, const WebKit::WebRect& rect) { |
| 2874 WebKit::WebRect result = rect; |
| 2875 result.x = original_view_screen_rect_.x() + |
| 2876 (result.x - widget_rect_.x()) * scale_; |
| 2877 result.y = original_view_screen_rect_.y() + |
| 2878 (result.y - widget_rect_.y()) * scale_; |
| 2879 return result; |
| 2880 } |
| 2881 |
2534 } // namespace content | 2882 } // namespace content |
OLD | NEW |