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_tap_gesture.h"
6
7 #include "base/logging.h"
8 #include "third_party/WebKit/public/web/WebInputEvent.h"
9 #include "ui/events/latency_info.h"
10
11 namespace content {
12
SyntheticTapGesture(const SyntheticTapGestureParams & params)13 SyntheticTapGesture::SyntheticTapGesture(
14 const SyntheticTapGestureParams& params)
15 : params_(params),
16 gesture_source_type_(SyntheticGestureParams::DEFAULT_INPUT),
17 state_(SETUP) {
18 DCHECK_GE(params_.duration_ms, 0);
19 }
20
~SyntheticTapGesture()21 SyntheticTapGesture::~SyntheticTapGesture() {}
22
ForwardInputEvents(const base::TimeTicks & timestamp,SyntheticGestureTarget * target)23 SyntheticGesture::Result SyntheticTapGesture::ForwardInputEvents(
24 const base::TimeTicks& timestamp, SyntheticGestureTarget* target) {
25 if (state_ == SETUP) {
26 gesture_source_type_ = params_.gesture_source_type;
27 if (gesture_source_type_ == SyntheticGestureParams::DEFAULT_INPUT)
28 gesture_source_type_ = target->GetDefaultSyntheticGestureSourceType();
29
30 state_ = PRESS;
31 }
32
33 DCHECK_NE(gesture_source_type_, SyntheticGestureParams::DEFAULT_INPUT);
34 if (gesture_source_type_ == SyntheticGestureParams::TOUCH_INPUT ||
35 gesture_source_type_ == SyntheticGestureParams::MOUSE_INPUT)
36 ForwardTouchOrMouseInputEvents(timestamp, target);
37 else
38 return SyntheticGesture::GESTURE_SOURCE_TYPE_NOT_IMPLEMENTED;
39
40 return (state_ == DONE) ? SyntheticGesture::GESTURE_FINISHED
41 : SyntheticGesture::GESTURE_RUNNING;
42 }
43
ForwardTouchOrMouseInputEvents(const base::TimeTicks & timestamp,SyntheticGestureTarget * target)44 void SyntheticTapGesture::ForwardTouchOrMouseInputEvents(
45 const base::TimeTicks& timestamp, SyntheticGestureTarget* target) {
46 switch (state_) {
47 case PRESS:
48 Press(target, timestamp);
49 // Release immediately if duration is 0.
50 if (params_.duration_ms == 0) {
51 Release(target, timestamp);
52 state_ = DONE;
53 } else {
54 start_time_ = timestamp;
55 state_ = WAITING_TO_RELEASE;
56 }
57 break;
58 case WAITING_TO_RELEASE:
59 if (timestamp - start_time_ >= GetDuration()) {
60 Release(target, start_time_ + GetDuration());
61 state_ = DONE;
62 }
63 break;
64 case SETUP:
65 NOTREACHED() << "State SETUP invalid for synthetic tap gesture.";
66 case DONE:
67 NOTREACHED() << "State DONE invalid for synthetic tap gesture.";
68 }
69 }
70
Press(SyntheticGestureTarget * target,const base::TimeTicks & timestamp)71 void SyntheticTapGesture::Press(SyntheticGestureTarget* target,
72 const base::TimeTicks& timestamp) {
73 if (gesture_source_type_ == SyntheticGestureParams::TOUCH_INPUT) {
74 touch_event_.PressPoint(params_.position.x(), params_.position.y());
75 touch_event_.timeStampSeconds = ConvertTimestampToSeconds(timestamp);
76 target->DispatchInputEventToPlatform(touch_event_);
77 } else if (gesture_source_type_ == SyntheticGestureParams::MOUSE_INPUT) {
78 blink::WebMouseEvent mouse_event =
79 SyntheticWebMouseEventBuilder::Build(blink::WebInputEvent::MouseDown,
80 params_.position.x(),
81 params_.position.y(),
82 0);
83 mouse_event.clickCount = 1;
84 mouse_event.timeStampSeconds = ConvertTimestampToSeconds(timestamp);
85 target->DispatchInputEventToPlatform(mouse_event);
86 } else {
87 NOTREACHED() << "Invalid gesture source type for synthetic tap gesture.";
88 }
89 }
90
Release(SyntheticGestureTarget * target,const base::TimeTicks & timestamp)91 void SyntheticTapGesture::Release(SyntheticGestureTarget* target,
92 const base::TimeTicks& timestamp) {
93 if (gesture_source_type_ == SyntheticGestureParams::TOUCH_INPUT) {
94 touch_event_.ReleasePoint(0);
95 touch_event_.timeStampSeconds = ConvertTimestampToSeconds(timestamp);
96 target->DispatchInputEventToPlatform(touch_event_);
97 } else if (gesture_source_type_ == SyntheticGestureParams::MOUSE_INPUT) {
98 blink::WebMouseEvent mouse_event =
99 SyntheticWebMouseEventBuilder::Build(blink::WebInputEvent::MouseUp,
100 params_.position.x(),
101 params_.position.y(),
102 0);
103 mouse_event.clickCount = 1;
104 mouse_event.timeStampSeconds = ConvertTimestampToSeconds(timestamp);
105 target->DispatchInputEventToPlatform(mouse_event);
106 } else {
107 NOTREACHED() << "Invalid gesture source type for synthetic tap gesture.";
108 }
109 }
110
GetDuration() const111 base::TimeDelta SyntheticTapGesture::GetDuration() const {
112 return base::TimeDelta::FromMilliseconds(params_.duration_ms);
113 }
114
115 } // namespace content
116