1 // Copyright 2013 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.
5 #include "content/browser/renderer_host/input/synthetic_gesture_target_base.h"
7 #include "content/browser/renderer_host/render_widget_host_impl.h"
8 #include "content/browser/renderer_host/render_widget_host_view_base.h"
9 #include "content/browser/renderer_host/ui_events_helper.h"
10 #include "content/common/input_messages.h"
11 #include "third_party/WebKit/public/web/WebInputEvent.h"
12 #include "ui/events/event.h"
13 #include "ui/events/latency_info.h"
15 using blink::WebInputEvent
;
16 using blink::WebTouchEvent
;
17 using blink::WebTouchPoint
;
18 using blink::WebMouseEvent
;
19 using blink::WebMouseWheelEvent
;
24 // This value was determined experimentally. It was sufficient to not cause a
25 // fling on Android and Aura.
26 const int kPointerAssumedStoppedTimeMs
= 100;
28 // SyntheticGestureTargetBase passes input events straight on to the renderer
29 // without going through a gesture recognition framework. There is thus no touch
31 const float kTouchSlopInDips
= 0.0f
;
35 SyntheticGestureTargetBase::SyntheticGestureTargetBase(
36 RenderWidgetHostImpl
* host
)
41 SyntheticGestureTargetBase::~SyntheticGestureTargetBase() {
44 void SyntheticGestureTargetBase::DispatchInputEventToPlatform(
45 const WebInputEvent
& event
) {
47 "SyntheticGestureTarget::DispatchInputEventToPlatform",
48 "type", WebInputEventTraits::GetName(event
.type
));
50 ui::LatencyInfo latency_info
;
51 latency_info
.AddLatencyNumber(ui::INPUT_EVENT_LATENCY_UI_COMPONENT
, 0, 0);
53 if (WebInputEvent::isTouchEventType(event
.type
)) {
54 const WebTouchEvent
& web_touch
=
55 static_cast<const WebTouchEvent
&>(event
);
57 // Check that all touch pointers are within the content bounds.
58 if (web_touch
.type
== WebInputEvent::TouchStart
) {
59 for (unsigned i
= 0; i
< web_touch
.touchesLength
; i
++)
60 CHECK(web_touch
.touches
[i
].state
!= WebTouchPoint::StatePressed
||
61 PointIsWithinContents(web_touch
.touches
[i
].position
.x
,
62 web_touch
.touches
[i
].position
.y
))
63 << "Touch coordinates are not within content bounds on TouchStart.";
66 DispatchWebTouchEventToPlatform(web_touch
, latency_info
);
67 } else if (event
.type
== WebInputEvent::MouseWheel
) {
68 const WebMouseWheelEvent
& web_wheel
=
69 static_cast<const WebMouseWheelEvent
&>(event
);
70 CHECK(PointIsWithinContents(web_wheel
.x
, web_wheel
.y
))
71 << "Mouse wheel position is not within content bounds.";
72 DispatchWebMouseWheelEventToPlatform(web_wheel
, latency_info
);
73 } else if (WebInputEvent::isMouseEventType(event
.type
)) {
74 const WebMouseEvent
& web_mouse
=
75 static_cast<const WebMouseEvent
&>(event
);
76 CHECK(event
.type
!= WebInputEvent::MouseDown
||
77 PointIsWithinContents(web_mouse
.x
, web_mouse
.y
))
78 << "Mouse pointer is not within content bounds on MouseDown.";
79 DispatchWebMouseEventToPlatform(web_mouse
, latency_info
);
85 void SyntheticGestureTargetBase::DispatchWebTouchEventToPlatform(
86 const blink::WebTouchEvent
& web_touch
,
87 const ui::LatencyInfo
& latency_info
) {
88 // We assume that platforms supporting touch have their own implementation of
89 // SyntheticGestureTarget to route the events through their respective input
91 CHECK(false) << "Touch events not supported for this browser.";
94 void SyntheticGestureTargetBase::DispatchWebMouseWheelEventToPlatform(
95 const blink::WebMouseWheelEvent
& web_wheel
,
96 const ui::LatencyInfo
& latency_info
) {
97 host_
->ForwardWheelEventWithLatencyInfo(web_wheel
, latency_info
);
100 void SyntheticGestureTargetBase::DispatchWebMouseEventToPlatform(
101 const blink::WebMouseEvent
& web_mouse
,
102 const ui::LatencyInfo
& latency_info
) {
103 host_
->ForwardMouseEventWithLatencyInfo(web_mouse
, latency_info
);
106 void SyntheticGestureTargetBase::SetNeedsFlush() {
107 host_
->SetNeedsFlush();
110 SyntheticGestureParams::GestureSourceType
111 SyntheticGestureTargetBase::GetDefaultSyntheticGestureSourceType() const {
112 return SyntheticGestureParams::MOUSE_INPUT
;
115 base::TimeDelta
SyntheticGestureTargetBase::PointerAssumedStoppedTime()
117 return base::TimeDelta::FromMilliseconds(kPointerAssumedStoppedTimeMs
);
120 float SyntheticGestureTargetBase::GetTouchSlopInDips() const {
121 return kTouchSlopInDips
;
124 float SyntheticGestureTargetBase::GetMinScalingSpanInDips() const {
125 // The minimum scaling distance is only relevant for touch gestures and the
126 // base target doesn't support touch.
131 bool SyntheticGestureTargetBase::PointIsWithinContents(int x
, int y
) const {
132 gfx::Rect bounds
= host_
->GetView()->GetViewBounds();
133 bounds
-= bounds
.OffsetFromOrigin(); // Translate the bounds to (0,0).
134 return bounds
.Contains(x
, y
);
137 } // namespace content