| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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/content_gesture_provider.h" | |
| 6 | |
| 7 #include "base/auto_reset.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "content/public/common/content_switches.h" | |
| 11 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
| 12 #include "ui/events/gesture_detection/gesture_config_helper.h" | |
| 13 #include "ui/events/gesture_detection/motion_event.h" | |
| 14 | |
| 15 using blink::WebGestureEvent; | |
| 16 using blink::WebInputEvent; | |
| 17 | |
| 18 namespace content { | |
| 19 namespace { | |
| 20 | |
| 21 WebGestureEvent CreateGesture(const ui::GestureEventData& data, | |
| 22 float scale) { | |
| 23 WebGestureEvent gesture; | |
| 24 gesture.x = data.x * scale; | |
| 25 gesture.y = data.y * scale; | |
| 26 gesture.timeStampSeconds = (data.time - base::TimeTicks()).InSecondsF(); | |
| 27 gesture.sourceDevice = WebGestureEvent::Touchscreen; | |
| 28 | |
| 29 switch (data.type) { | |
| 30 case ui::ET_GESTURE_SHOW_PRESS: | |
| 31 gesture.type = WebInputEvent::GestureShowPress; | |
| 32 gesture.data.showPress.width = data.details.show_press.width * scale; | |
| 33 gesture.data.showPress.width = data.details.show_press.width * scale; | |
| 34 break; | |
| 35 case ui::ET_GESTURE_DOUBLE_TAP: | |
| 36 gesture.type = WebInputEvent::GestureDoubleTap; | |
| 37 break; | |
| 38 case ui::ET_GESTURE_TAP: | |
| 39 gesture.type = WebInputEvent::GestureTap; | |
| 40 gesture.data.tap.tapCount = data.details.tap.tap_count; | |
| 41 gesture.data.tap.width = data.details.tap.width * scale; | |
| 42 gesture.data.tap.height = data.details.tap.height * scale; | |
| 43 break; | |
| 44 case ui::ET_GESTURE_TAP_UNCONFIRMED: | |
| 45 gesture.type = WebInputEvent::GestureTapUnconfirmed; | |
| 46 gesture.data.tap.tapCount = data.details.tap.tap_count; | |
| 47 gesture.data.tap.width = data.details.tap.width * scale; | |
| 48 gesture.data.tap.height = data.details.tap.height * scale; | |
| 49 break; | |
| 50 case ui::ET_GESTURE_LONG_PRESS: | |
| 51 gesture.type = WebInputEvent::GestureLongPress; | |
| 52 gesture.data.longPress.width = data.details.long_press.width * scale; | |
| 53 gesture.data.longPress.height = data.details.long_press.height * scale; | |
| 54 break; | |
| 55 case ui::ET_GESTURE_LONG_TAP: | |
| 56 gesture.type = WebInputEvent::GestureLongTap; | |
| 57 gesture.data.longPress.width = data.details.long_press.width * scale; | |
| 58 gesture.data.longPress.height = data.details.long_press.height * scale; | |
| 59 break; | |
| 60 case ui::ET_GESTURE_SCROLL_BEGIN: | |
| 61 gesture.type = WebInputEvent::GestureScrollBegin; | |
| 62 gesture.data.scrollBegin.deltaXHint = | |
| 63 data.details.scroll_begin.delta_x_hint * scale; | |
| 64 gesture.data.scrollBegin.deltaYHint = | |
| 65 data.details.scroll_begin.delta_y_hint * scale; | |
| 66 break; | |
| 67 case ui::ET_GESTURE_SCROLL_UPDATE: | |
| 68 gesture.type = WebInputEvent::GestureScrollUpdate; | |
| 69 gesture.data.scrollUpdate.deltaX = | |
| 70 data.details.scroll_update.delta_x * scale; | |
| 71 gesture.data.scrollUpdate.deltaY = | |
| 72 data.details.scroll_update.delta_y * scale; | |
| 73 gesture.data.scrollUpdate.velocityX = | |
| 74 data.details.scroll_update.velocity_x * scale; | |
| 75 gesture.data.scrollUpdate.velocityY = | |
| 76 data.details.scroll_update.velocity_y * scale; | |
| 77 break; | |
| 78 case ui::ET_GESTURE_SCROLL_END: | |
| 79 gesture.type = WebInputEvent::GestureScrollEnd; | |
| 80 break; | |
| 81 case ui::ET_SCROLL_FLING_START: | |
| 82 gesture.type = WebInputEvent::GestureFlingStart; | |
| 83 gesture.data.flingStart.velocityX = | |
| 84 data.details.fling_start.velocity_x * scale; | |
| 85 gesture.data.flingStart.velocityY = | |
| 86 data.details.fling_start.velocity_y * scale; | |
| 87 break; | |
| 88 case ui::ET_SCROLL_FLING_CANCEL: | |
| 89 gesture.type = WebInputEvent::GestureFlingCancel; | |
| 90 break; | |
| 91 case ui::ET_GESTURE_PINCH_BEGIN: | |
| 92 gesture.type = WebInputEvent::GesturePinchBegin; | |
| 93 break; | |
| 94 case ui::ET_GESTURE_PINCH_UPDATE: | |
| 95 gesture.type = WebInputEvent::GesturePinchUpdate; | |
| 96 gesture.data.pinchUpdate.scale = data.details.pinch_update.scale; | |
| 97 break; | |
| 98 case ui::ET_GESTURE_PINCH_END: | |
| 99 gesture.type = WebInputEvent::GesturePinchEnd; | |
| 100 break; | |
| 101 case ui::ET_GESTURE_TAP_CANCEL: | |
| 102 gesture.type = WebInputEvent::GestureTapCancel; | |
| 103 break; | |
| 104 case ui::ET_GESTURE_TAP_DOWN: | |
| 105 gesture.type = WebInputEvent::GestureTapDown; | |
| 106 gesture.data.tapDown.width = data.details.tap_down.width * scale; | |
| 107 gesture.data.tapDown.height = data.details.tap_down.height * scale; | |
| 108 break; | |
| 109 default: | |
| 110 NOTREACHED() << "ui::EventType provided wasn't a gesture event."; | |
| 111 break; | |
| 112 } | |
| 113 | |
| 114 return gesture; | |
| 115 } | |
| 116 | |
| 117 ui::TouchDispositionGestureFilter::TouchEventAck | |
| 118 ToTouchDispositionGestureFilterAck(InputEventAckState ack_state) { | |
| 119 switch (ack_state) { | |
| 120 case INPUT_EVENT_ACK_STATE_CONSUMED: | |
| 121 return ui::TouchDispositionGestureFilter::CONSUMED; | |
| 122 case INPUT_EVENT_ACK_STATE_NOT_CONSUMED: | |
| 123 return ui::TouchDispositionGestureFilter::NOT_CONSUMED; | |
| 124 case INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS: | |
| 125 return ui::TouchDispositionGestureFilter::NO_CONSUMER_EXISTS; | |
| 126 case INPUT_EVENT_ACK_STATE_IGNORED: | |
| 127 return ui::TouchDispositionGestureFilter::CONSUMED; | |
| 128 case INPUT_EVENT_ACK_STATE_UNKNOWN: | |
| 129 break; | |
| 130 }; | |
| 131 NOTREACHED() << "Invalid ack state: " << ack_state; | |
| 132 return ui::TouchDispositionGestureFilter::NO_CONSUMER_EXISTS; | |
| 133 } | |
| 134 | |
| 135 ui::GestureProvider::Config GetGestureProviderConfig() { | |
| 136 ui::GestureProvider::Config config = ui::DefaultGestureProviderConfig(); | |
| 137 #if defined(OS_ANDROID) | |
| 138 config.disable_click_delay = | |
| 139 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableClickDelay); | |
| 140 #endif | |
| 141 return config; | |
| 142 } | |
| 143 | |
| 144 } // namespace | |
| 145 | |
| 146 ContentGestureProvider::ContentGestureProvider( | |
| 147 ContentGestureProviderClient* client, | |
| 148 float touch_to_gesture_scale) | |
| 149 : client_(client), | |
| 150 touch_to_gesture_scale_(touch_to_gesture_scale), | |
| 151 gesture_provider_(GetGestureProviderConfig(), this), | |
| 152 gesture_filter_(this), | |
| 153 handling_event_(false) {} | |
| 154 | |
| 155 bool ContentGestureProvider::OnTouchEvent(const ui::MotionEvent& event) { | |
| 156 DCHECK(!handling_event_); | |
| 157 base::AutoReset<bool> handling_event(&handling_event_, true); | |
| 158 | |
| 159 pending_gesture_packet_ = ui::GestureEventDataPacket::FromTouch(event); | |
| 160 | |
| 161 if (!gesture_provider_.OnTouchEvent(event)) | |
| 162 return false; | |
| 163 | |
| 164 ui::TouchDispositionGestureFilter::PacketResult result = | |
| 165 gesture_filter_.OnGesturePacket(pending_gesture_packet_); | |
| 166 if (result != ui::TouchDispositionGestureFilter::SUCCESS) { | |
| 167 NOTREACHED() << "Invalid touch gesture sequence detected."; | |
| 168 return false; | |
| 169 } | |
| 170 | |
| 171 return true; | |
| 172 } | |
| 173 | |
| 174 void ContentGestureProvider::OnTouchEventAck(InputEventAckState ack_state) { | |
| 175 DCHECK_NE(INPUT_EVENT_ACK_STATE_UNKNOWN, ack_state); | |
| 176 gesture_filter_.OnTouchEventAck( | |
| 177 ToTouchDispositionGestureFilterAck(ack_state)); | |
| 178 } | |
| 179 | |
| 180 void ContentGestureProvider::ResetGestureDetectors() { | |
| 181 gesture_provider_.ResetGestureDetectors(); | |
| 182 } | |
| 183 | |
| 184 void ContentGestureProvider::SetMultiTouchSupportEnabled(bool enabled) { | |
| 185 gesture_provider_.SetMultiTouchSupportEnabled(enabled); | |
| 186 } | |
| 187 | |
| 188 void ContentGestureProvider::SetDoubleTapSupportForPlatformEnabled( | |
| 189 bool enabled) { | |
| 190 gesture_provider_.SetDoubleTapSupportForPlatformEnabled(enabled); | |
| 191 } | |
| 192 | |
| 193 void ContentGestureProvider::SetDoubleTapSupportForPageEnabled(bool enabled) { | |
| 194 gesture_provider_.SetDoubleTapSupportForPageEnabled(enabled); | |
| 195 } | |
| 196 | |
| 197 const ui::MotionEvent* ContentGestureProvider::GetCurrentDownEvent() const { | |
| 198 return gesture_provider_.current_down_event(); | |
| 199 } | |
| 200 | |
| 201 void ContentGestureProvider::OnGestureEvent( | |
| 202 const ui::GestureEventData& event) { | |
| 203 if (handling_event_) { | |
| 204 pending_gesture_packet_.Push(event); | |
| 205 return; | |
| 206 } | |
| 207 | |
| 208 gesture_filter_.OnGesturePacket( | |
| 209 ui::GestureEventDataPacket::FromTouchTimeout(event)); | |
| 210 } | |
| 211 | |
| 212 void ContentGestureProvider::ForwardGestureEvent( | |
| 213 const ui::GestureEventData& event) { | |
| 214 client_->OnGestureEvent(CreateGesture(event, touch_to_gesture_scale_)); | |
| 215 } | |
| 216 | |
| 217 } // namespace content | |
| OLD | NEW |