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.
4
5 #include "content/browser/renderer_host/input/synthetic_gesture_target_base.h"
6
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/input_event.h"
11 #include "content/common/input_messages.h"
12 #include "third_party/WebKit/public/web/WebInputEvent.h"
13 #include "ui/events/event.h"
14 #include "ui/events/latency_info.h"
15
16 using blink::WebInputEvent;
17 using blink::WebTouchEvent;
18 using blink::WebMouseEvent;
19 using blink::WebMouseWheelEvent;
20
21 namespace content {
22 namespace {
23
24 // This value was determined experimentally. It was sufficient to not cause a
25 // fling on Android.
26 const int kPointerAssumedStoppedTimeMs = 50;
27
28 // SyntheticGestureTargetBase passes input events straight on to the renderer
29 // without going through a gesture recognition framework. There is thus no touch
30 // slop.
31 const int kTouchSlopInDips = 0;
32
33 } // namespace
34
SyntheticGestureTargetBase(RenderWidgetHostImpl * host)35 SyntheticGestureTargetBase::SyntheticGestureTargetBase(
36 RenderWidgetHostImpl* host)
37 : host_(host) {
38 DCHECK(host);
39 }
40
~SyntheticGestureTargetBase()41 SyntheticGestureTargetBase::~SyntheticGestureTargetBase() {
42 }
43
DispatchInputEventToPlatform(const InputEvent & event)44 void SyntheticGestureTargetBase::DispatchInputEventToPlatform(
45 const InputEvent& event) {
46 const WebInputEvent* web_event = event.web_event.get();
47 if (WebInputEvent::isTouchEventType(web_event->type)) {
48 DCHECK(SupportsSyntheticGestureSourceType(
49 SyntheticGestureParams::TOUCH_INPUT));
50
51 const WebTouchEvent* web_touch =
52 static_cast<const WebTouchEvent*>(web_event);
53 DispatchWebTouchEventToPlatform(*web_touch, event.latency_info);
54 } else if (web_event->type == WebInputEvent::MouseWheel) {
55 DCHECK(SupportsSyntheticGestureSourceType(
56 SyntheticGestureParams::MOUSE_INPUT));
57
58 const WebMouseWheelEvent* web_wheel =
59 static_cast<const WebMouseWheelEvent*>(web_event);
60 DispatchWebMouseWheelEventToPlatform(*web_wheel, event.latency_info);
61 } else if (WebInputEvent::isMouseEventType(web_event->type)) {
62 DCHECK(SupportsSyntheticGestureSourceType(
63 SyntheticGestureParams::MOUSE_INPUT));
64
65 const WebMouseEvent* web_mouse =
66 static_cast<const WebMouseEvent*>(web_event);
67 DispatchWebMouseEventToPlatform(*web_mouse, event.latency_info);
68 } else {
69 NOTREACHED();
70 }
71 }
72
DispatchWebTouchEventToPlatform(const blink::WebTouchEvent & web_touch,const ui::LatencyInfo & latency_info)73 void SyntheticGestureTargetBase::DispatchWebTouchEventToPlatform(
74 const blink::WebTouchEvent& web_touch,
75 const ui::LatencyInfo& latency_info) {
76 host_->ForwardTouchEventWithLatencyInfo(web_touch, latency_info);
77 }
78
DispatchWebMouseWheelEventToPlatform(const blink::WebMouseWheelEvent & web_wheel,const ui::LatencyInfo & latency_info)79 void SyntheticGestureTargetBase::DispatchWebMouseWheelEventToPlatform(
80 const blink::WebMouseWheelEvent& web_wheel,
81 const ui::LatencyInfo& latency_info) {
82 host_->ForwardWheelEventWithLatencyInfo(
83 MouseWheelEventWithLatencyInfo(web_wheel, latency_info));
84 }
85
DispatchWebMouseEventToPlatform(const blink::WebMouseEvent & web_mouse,const ui::LatencyInfo & latency_info)86 void SyntheticGestureTargetBase::DispatchWebMouseEventToPlatform(
87 const blink::WebMouseEvent& web_mouse,
88 const ui::LatencyInfo& latency_info) {
89 host_->ForwardMouseEventWithLatencyInfo(
90 MouseEventWithLatencyInfo(web_mouse, latency_info));
91 }
92
OnSyntheticGestureCompleted(SyntheticGesture::Result result)93 void SyntheticGestureTargetBase::OnSyntheticGestureCompleted(
94 SyntheticGesture::Result result) {
95 host_->Send(new InputMsg_SyntheticGestureCompleted(host_->GetRoutingID()));
96 }
97
SetNeedsFlush()98 void SyntheticGestureTargetBase::SetNeedsFlush() {
99 host_->SetNeedsFlush();
100 }
101
102 SyntheticGestureParams::GestureSourceType
GetDefaultSyntheticGestureSourceType() const103 SyntheticGestureTargetBase::GetDefaultSyntheticGestureSourceType() const {
104 return SyntheticGestureParams::MOUSE_INPUT;
105 }
106
SupportsSyntheticGestureSourceType(SyntheticGestureParams::GestureSourceType gesture_source_type) const107 bool SyntheticGestureTargetBase::SupportsSyntheticGestureSourceType(
108 SyntheticGestureParams::GestureSourceType gesture_source_type) const {
109 return gesture_source_type == SyntheticGestureParams::MOUSE_INPUT ||
110 gesture_source_type == SyntheticGestureParams::TOUCH_INPUT;
111 }
112
PointerAssumedStoppedTime() const113 base::TimeDelta SyntheticGestureTargetBase::PointerAssumedStoppedTime()
114 const {
115 return base::TimeDelta::FromMilliseconds(kPointerAssumedStoppedTimeMs);
116 }
117
GetTouchSlopInDips() const118 int SyntheticGestureTargetBase::GetTouchSlopInDips() const {
119 return kTouchSlopInDips;
120 }
121
122 } // namespace content
123