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/logging.h" |
| 9 #include "ui/events/gesture_detection/gesture_config_helper.h" |
| 10 #include "ui/events/gesture_detection/gesture_event_params.h" |
| 11 #include "ui/events/gesture_detection/motion_event.h" |
| 12 |
| 13 using blink::WebGestureEvent; |
| 14 using blink::WebInputEvent; |
| 15 |
| 16 namespace content { |
| 17 namespace { |
| 18 |
| 19 WebGestureEvent CreateGesture(const ui::GestureEventParams& params, |
| 20 float scale) { |
| 21 WebGestureEvent gesture; |
| 22 gesture.x = params.x * scale; |
| 23 gesture.y = params.y * scale; |
| 24 gesture.timeStampSeconds = (params.time - base::TimeTicks()).InSecondsF(); |
| 25 gesture.sourceDevice = WebGestureEvent::Touchscreen; |
| 26 |
| 27 switch (params.type) { |
| 28 case ui::GESTURE_SHOW_PRESS: |
| 29 gesture.type = WebInputEvent::GestureShowPress; |
| 30 gesture.data.showPress.width = params.data.show_press.width * scale; |
| 31 gesture.data.showPress.width = params.data.show_press.width * scale; |
| 32 break; |
| 33 case ui::GESTURE_DOUBLE_TAP: |
| 34 gesture.type = WebInputEvent::GestureDoubleTap; |
| 35 break; |
| 36 case ui::GESTURE_SINGLE_TAP_CONFIRMED: |
| 37 gesture.type = WebInputEvent::GestureTap; |
| 38 gesture.data.tap.tapCount = params.data.tap.tap_count; |
| 39 gesture.data.tap.width = params.data.tap.width * scale; |
| 40 gesture.data.tap.height = params.data.tap.height * scale; |
| 41 break; |
| 42 case ui::GESTURE_SINGLE_TAP_UNCONFIRMED: |
| 43 gesture.type = WebInputEvent::GestureTapUnconfirmed; |
| 44 gesture.data.tap.tapCount = params.data.tap.tap_count; |
| 45 gesture.data.tap.width = params.data.tap.width * scale; |
| 46 gesture.data.tap.height = params.data.tap.height * scale; |
| 47 break; |
| 48 case ui::GESTURE_LONG_PRESS: |
| 49 gesture.type = WebInputEvent::GestureLongPress; |
| 50 gesture.data.longPress.width = params.data.long_press.width * scale; |
| 51 gesture.data.longPress.height = params.data.long_press.height * scale; |
| 52 break; |
| 53 case ui::GESTURE_LONG_TAP: |
| 54 gesture.type = WebInputEvent::GestureLongTap; |
| 55 gesture.data.longPress.width = params.data.long_press.width * scale; |
| 56 gesture.data.longPress.height = params.data.long_press.height * scale; |
| 57 break; |
| 58 case ui::GESTURE_SCROLL_BEGIN: |
| 59 gesture.type = WebInputEvent::GestureScrollBegin; |
| 60 gesture.data.scrollBegin.deltaXHint = |
| 61 params.data.scroll_begin.delta_x_hint * scale; |
| 62 gesture.data.scrollBegin.deltaYHint = |
| 63 params.data.scroll_begin.delta_y_hint * scale; |
| 64 break; |
| 65 case ui::GESTURE_SCROLL_UPDATE: |
| 66 gesture.type = WebInputEvent::GestureScrollUpdate; |
| 67 gesture.data.scrollUpdate.deltaX = |
| 68 params.data.scroll_update.delta_x * scale; |
| 69 gesture.data.scrollUpdate.deltaY = |
| 70 params.data.scroll_update.delta_y * scale; |
| 71 gesture.data.scrollUpdate.velocityX = |
| 72 params.data.scroll_update.velocity_x * scale; |
| 73 gesture.data.scrollUpdate.velocityY = |
| 74 params.data.scroll_update.velocity_y * scale; |
| 75 break; |
| 76 case ui::GESTURE_SCROLL_END: |
| 77 gesture.type = WebInputEvent::GestureScrollEnd; |
| 78 break; |
| 79 case ui::GESTURE_FLING_START: |
| 80 gesture.type = WebInputEvent::GestureFlingStart; |
| 81 gesture.data.flingStart.velocityX = |
| 82 params.data.fling_start.velocity_x * scale; |
| 83 gesture.data.flingStart.velocityY = |
| 84 params.data.fling_start.velocity_y * scale; |
| 85 break; |
| 86 case ui::GESTURE_FLING_CANCEL: |
| 87 gesture.type = WebInputEvent::GestureFlingCancel; |
| 88 break; |
| 89 case ui::GESTURE_PINCH_BEGIN: |
| 90 gesture.type = WebInputEvent::GesturePinchBegin; |
| 91 break; |
| 92 case ui::GESTURE_PINCH_UPDATE: |
| 93 gesture.type = WebInputEvent::GesturePinchUpdate; |
| 94 gesture.data.pinchUpdate.scale = params.data.pinch_update.scale; |
| 95 break; |
| 96 case ui::GESTURE_PINCH_END: |
| 97 gesture.type = WebInputEvent::GesturePinchEnd; |
| 98 break; |
| 99 case ui::GESTURE_TAP_CANCEL: |
| 100 gesture.type = WebInputEvent::GestureTapCancel; |
| 101 break; |
| 102 case ui::GESTURE_TAP_DOWN: |
| 103 gesture.type = WebInputEvent::GestureTapDown; |
| 104 gesture.data.tapDown.width = params.data.tap_down.width * scale; |
| 105 gesture.data.tapDown.height = params.data.tap_down.height * scale; |
| 106 break; |
| 107 } |
| 108 |
| 109 return gesture; |
| 110 } |
| 111 |
| 112 } // namespace |
| 113 |
| 114 ContentGestureProvider::ContentGestureProvider( |
| 115 ContentGestureProviderClient* client, |
| 116 float touch_to_gesture_scale) |
| 117 : client_(client), |
| 118 touch_to_gesture_scale_(touch_to_gesture_scale), |
| 119 gesture_provider_(ui::DefaultGestureProviderConfig(), this), |
| 120 gesture_filter_(this), |
| 121 handling_event_(false) {} |
| 122 |
| 123 bool ContentGestureProvider::OnTouchEvent(const ui::MotionEvent& event) { |
| 124 DCHECK(!handling_event_); |
| 125 base::AutoReset<bool> handling_event(&handling_event_, true); |
| 126 |
| 127 pending_gesture_packet_ = GestureEventPacket::FromTouch(event); |
| 128 |
| 129 if (!gesture_provider_.OnTouchEvent(event)) |
| 130 return false; |
| 131 |
| 132 TouchDispositionGestureFilter::PacketResult result = |
| 133 gesture_filter_.OnGestureEventPacket(pending_gesture_packet_); |
| 134 if (result != TouchDispositionGestureFilter::SUCCESS) { |
| 135 NOTREACHED() << "Invalid touch gesture sequence detected."; |
| 136 return false; |
| 137 } |
| 138 |
| 139 return true; |
| 140 } |
| 141 |
| 142 void ContentGestureProvider::OnTouchEventAck(InputEventAckState ack_state) { |
| 143 gesture_filter_.OnTouchEventAck(ack_state); |
| 144 } |
| 145 |
| 146 void ContentGestureProvider::ResetGestureDetectors() { |
| 147 gesture_provider_.ResetGestureDetectors(); |
| 148 } |
| 149 |
| 150 void ContentGestureProvider::CancelActiveTouchSequence() { |
| 151 gesture_provider_.CancelActiveTouchSequence(); |
| 152 } |
| 153 |
| 154 void ContentGestureProvider::UpdateMultiTouchSupport( |
| 155 bool support_multi_touch_zoom) { |
| 156 gesture_provider_.UpdateMultiTouchSupport(support_multi_touch_zoom); |
| 157 } |
| 158 |
| 159 void ContentGestureProvider::UpdateDoubleTapSupportForPlatform( |
| 160 bool support_double_tap) { |
| 161 gesture_provider_.UpdateDoubleTapSupportForPlatform(support_double_tap); |
| 162 } |
| 163 |
| 164 void ContentGestureProvider::UpdateDoubleTapSupportForPage( |
| 165 bool support_double_tap) { |
| 166 gesture_provider_.UpdateDoubleTapSupportForPage(support_double_tap); |
| 167 } |
| 168 |
| 169 void ContentGestureProvider::OnGestureEvent( |
| 170 const ui::GestureEventParams& params) { |
| 171 WebGestureEvent gesture(CreateGesture(params, touch_to_gesture_scale_)); |
| 172 if (handling_event_) { |
| 173 pending_gesture_packet_.Push(gesture); |
| 174 return; |
| 175 } |
| 176 |
| 177 gesture_filter_.OnGestureEventPacket( |
| 178 GestureEventPacket::FromTouchTimeout(gesture)); |
| 179 } |
| 180 |
| 181 void ContentGestureProvider::ForwardGestureEvent( |
| 182 const blink::WebGestureEvent& event) { |
| 183 client_->OnGestureEvent(event); |
| 184 } |
| 185 |
| 186 } // namespace content |
OLD | NEW |