IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / renderer_host / input / synthetic_tap_gesture.cc
blobea7e98419c4d56ab82579854ea1378b52b87654a
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"
12 namespace content {
13 namespace {
15 void DispatchEventToPlatform(SyntheticGestureTarget* target,
16 const blink::WebInputEvent& event) {
17 target->DispatchInputEventToPlatform(
18 InputEvent(event, ui::LatencyInfo(), false));
21 } // namespace
23 SyntheticTapGesture::SyntheticTapGesture(
24 const SyntheticTapGestureParams& params)
25 : params_(params),
26 gesture_source_type_(SyntheticGestureParams::DEFAULT_INPUT),
27 state_(SETUP) {
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;
43 state_ = PRESS;
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);
50 else
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) {
59 switch (state_) {
60 case PRESS:
61 Press(target, timestamp);
62 // Release immediately if duration is 0.
63 if (params_.duration_ms == 0) {
64 Release(target, timestamp);
65 state_ = DONE;
66 } else {
67 start_time_ = timestamp;
68 state_ = WAITING_TO_RELEASE;
70 break;
71 case WAITING_TO_RELEASE:
72 if (timestamp - start_time_ >= GetDuration()) {
73 Release(target, start_time_ + GetDuration());
74 state_ = DONE;
76 break;
77 case SETUP:
78 NOTREACHED() << "State SETUP invalid for synthetic tap gesture.";
79 case DONE:
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,
93 params_.position.x(),
94 params_.position.y(),
95 0);
96 mouse_event.clickCount = 1;
97 mouse_event.timeStampSeconds = ConvertTimestampToSeconds(timestamp);
98 DispatchEventToPlatform(target, mouse_event);
99 } else {
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);
119 } else {
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