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_tap_gesture.h"
7 #include "base/logging.h"
8 #include "content/common/input/input_event.h"
9 #include "third_party/WebKit/public/web/WebInputEvent.h"
10 #include "ui/events/latency_info.h"
15 void DispatchEventToPlatform(SyntheticGestureTarget
* target
,
16 const blink::WebInputEvent
& event
) {
17 target
->DispatchInputEventToPlatform(
18 InputEvent(event
, ui::LatencyInfo(), false));
23 SyntheticTapGesture::SyntheticTapGesture(
24 const SyntheticTapGestureParams
& params
)
26 gesture_source_type_(SyntheticGestureParams::DEFAULT_INPUT
),
28 DCHECK_GE(params_
.duration_ms
, 0);
31 SyntheticTapGesture::~SyntheticTapGesture() {}
33 SyntheticGesture::Result
SyntheticTapGesture::ForwardInputEvents(
34 const base::TimeTicks
& timestamp
, SyntheticGestureTarget
* target
) {
35 if (state_
== SETUP
) {
36 gesture_source_type_
= params_
.gesture_source_type
;
37 if (gesture_source_type_
== SyntheticGestureParams::DEFAULT_INPUT
)
38 gesture_source_type_
= target
->GetDefaultSyntheticGestureSourceType();
40 if (!target
->SupportsSyntheticGestureSourceType(gesture_source_type_
))
41 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_SUPPORTED_BY_PLATFORM
;
46 DCHECK_NE(gesture_source_type_
, SyntheticGestureParams::DEFAULT_INPUT
);
47 if (gesture_source_type_
== SyntheticGestureParams::TOUCH_INPUT
||
48 gesture_source_type_
== SyntheticGestureParams::MOUSE_INPUT
)
49 ForwardTouchOrMouseInputEvents(timestamp
, target
);
51 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED
;
53 return (state_
== DONE
) ? SyntheticGesture::GESTURE_FINISHED
54 : SyntheticGesture::GESTURE_RUNNING
;
57 void SyntheticTapGesture::ForwardTouchOrMouseInputEvents(
58 const base::TimeTicks
& timestamp
, SyntheticGestureTarget
* target
) {
61 Press(target
, timestamp
);
62 // Release immediately if duration is 0.
63 if (params_
.duration_ms
== 0) {
64 Release(target
, timestamp
);
67 start_time_
= timestamp
;
68 state_
= WAITING_TO_RELEASE
;
71 case WAITING_TO_RELEASE
:
72 if (timestamp
- start_time_
>= GetDuration()) {
73 Release(target
, start_time_
+ GetDuration());
78 NOTREACHED() << "State SETUP invalid for synthetic tap gesture.";
80 NOTREACHED() << "State DONE invalid for synthetic tap gesture.";
84 void SyntheticTapGesture::Press(SyntheticGestureTarget
* target
,
85 const base::TimeTicks
& timestamp
) {
86 if (gesture_source_type_
== SyntheticGestureParams::TOUCH_INPUT
) {
87 touch_event_
.PressPoint(params_
.position
.x(), params_
.position
.y());
88 touch_event_
.timeStampSeconds
= ConvertTimestampToSeconds(timestamp
);
89 DispatchEventToPlatform(target
, touch_event_
);
90 } else if (gesture_source_type_
== SyntheticGestureParams::MOUSE_INPUT
) {
91 blink::WebMouseEvent mouse_event
=
92 SyntheticWebMouseEventBuilder::Build(blink::WebInputEvent::MouseDown
,
96 mouse_event
.clickCount
= 1;
97 mouse_event
.timeStampSeconds
= ConvertTimestampToSeconds(timestamp
);
98 DispatchEventToPlatform(target
, mouse_event
);
100 NOTREACHED() << "Invalid gesture source type for synthetic tap gesture.";
104 void SyntheticTapGesture::Release(SyntheticGestureTarget
* target
,
105 const base::TimeTicks
& timestamp
) {
106 if (gesture_source_type_
== SyntheticGestureParams::TOUCH_INPUT
) {
107 touch_event_
.ReleasePoint(0);
108 touch_event_
.timeStampSeconds
= ConvertTimestampToSeconds(timestamp
);
109 DispatchEventToPlatform(target
, touch_event_
);
110 } else if (gesture_source_type_
== SyntheticGestureParams::MOUSE_INPUT
) {
111 blink::WebMouseEvent mouse_event
=
112 SyntheticWebMouseEventBuilder::Build(blink::WebInputEvent::MouseUp
,
113 params_
.position
.x(),
114 params_
.position
.y(),
116 mouse_event
.clickCount
= 1;
117 mouse_event
.timeStampSeconds
= ConvertTimestampToSeconds(timestamp
);
118 DispatchEventToPlatform(target
, mouse_event
);
120 NOTREACHED() << "Invalid gesture source type for synthetic tap gesture.";
124 base::TimeDelta
SyntheticTapGesture::GetDuration() const {
125 return base::TimeDelta::FromMilliseconds(params_
.duration_ms
);
128 } // namespace content