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