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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURE_EVENT_QUEUE_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURE_EVENT_QUEUE_H_
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/timer/timer.h"
13 #include "content/browser/renderer_host/event_with_latency_info.h"
14 #include "content/browser/renderer_host/input/tap_suppression_controller.h"
15 #include "content/browser/renderer_host/input/touchpad_tap_suppression_controller.h"
16 #include "content/browser/renderer_host/input/touchscreen_tap_suppression_controller.h"
17 #include "content/common/content_export.h"
18 #include "content/common/input/input_event_ack_state.h"
19 #include "third_party/WebKit/public/web/WebInputEvent.h"
20 #include "ui/gfx/transform.h"
23 class GestureEventQueueTest
;
25 class MockRenderWidgetHost
;
27 // Interface with which the GestureEventQueue can forward gesture events, and
28 // dispatch gesture event responses.
29 class CONTENT_EXPORT GestureEventQueueClient
{
31 virtual ~GestureEventQueueClient() {}
33 virtual void SendGestureEventImmediately(
34 const GestureEventWithLatencyInfo
& event
) = 0;
36 virtual void OnGestureEventAck(
37 const GestureEventWithLatencyInfo
& event
,
38 InputEventAckState ack_result
) = 0;
41 // Maintains WebGestureEvents in a queue before forwarding them to the renderer
42 // to apply a sequence of filters on them:
43 // 1. The sequence is filtered for bounces. A bounce is when the finger lifts
44 // from the screen briefly during an in-progress scroll. Ifco this happens,
45 // non-GestureScrollUpdate events are queued until the de-bounce interval
46 // passes or another GestureScrollUpdate event occurs.
47 // 2. Unnecessary GestureFlingCancel events are filtered. These are
48 // GestureFlingCancels that have no corresponding GestureFlingStart in the
50 // 3. Taps immediately after a GestureFlingCancel (caused by the same tap) are
52 // 4. Whenever possible, events in the queue are coalesced to have as few events
53 // as possible and therefore maximize the chance that the event stream can be
54 // handled entirely by the compositor thread.
55 // Events in the queue are forwarded to the renderer one by one; i.e., each
56 // event is sent after receiving the ACK for previous one. The only exception is
57 // that if a GestureScrollUpdate is followed by a GesturePinchUpdate, they are
59 // TODO(rjkroege): Possibly refactor into a filter chain:
60 // http://crbug.com/148443.
61 class CONTENT_EXPORT GestureEventQueue
{
63 struct CONTENT_EXPORT Config
{
66 // Controls touchpad-related tap suppression, disabled by default.
67 TapSuppressionController::Config touchpad_tap_suppression_config
;
69 // Controls touchscreen-related tap suppression, disabled by default.
70 TapSuppressionController::Config touchscreen_tap_suppression_config
;
72 // Determines whether non-scroll gesture events are "debounced" during an
73 // active scroll sequence, suppressing brief scroll interruptions.
74 // Zero by default (disabled).
75 base::TimeDelta debounce_interval
;
78 // Both |client| and |touchpad_client| must outlive the GestureEventQueue.
79 GestureEventQueue(GestureEventQueueClient
* client
,
80 TouchpadTapSuppressionControllerClient
* touchpad_client
,
81 const Config
& config
);
84 // Adds a gesture to the queue if it passes the relevant filters. If
85 // there are no events currently queued, the event will be forwarded
87 void QueueEvent(const GestureEventWithLatencyInfo
&);
89 // Indicates that the caller has received an acknowledgement from the renderer
90 // with state |ack_result| and event |type|. May send events if the queue is
92 void ProcessGestureAck(InputEventAckState ack_result
,
93 blink::WebInputEvent::Type type
,
94 const ui::LatencyInfo
& latency
);
96 // Sets the state of the |fling_in_progress_| field to indicate that a fling
97 // is definitely not in progress.
98 void FlingHasBeenHalted();
100 // Returns the |TouchpadTapSuppressionController| instance.
101 TouchpadTapSuppressionController
* GetTouchpadTapSuppressionController();
103 void ForwardGestureEvent(const GestureEventWithLatencyInfo
& gesture_event
);
106 return coalesced_gesture_events_
.empty() &&
107 debouncing_deferral_queue_
.empty();
110 void set_debounce_interval_time_ms_for_testing(int interval_ms
) {
111 debounce_interval_
= base::TimeDelta::FromMilliseconds(interval_ms
);
115 friend class GestureEventQueueTest
;
116 friend class MockRenderWidgetHost
;
118 // TODO(mohsen): There are a bunch of ShouldForward.../ShouldDiscard...
119 // methods that are getting confusing. This should be somehow fixed. Maybe
120 // while refactoring GEQ: http://crbug.com/148443.
122 // Inovked on the expiration of the debounce interval to release
124 void SendScrollEndingEventsNow();
126 // Returns |true| if the given GestureFlingCancel should be discarded
128 bool ShouldDiscardFlingCancelEvent(
129 const GestureEventWithLatencyInfo
& gesture_event
) const;
131 // Sub-filter for removing bounces from in-progress scrolls.
132 bool ShouldForwardForBounceReduction(
133 const GestureEventWithLatencyInfo
& gesture_event
);
135 // Sub-filter for removing unnecessary GestureFlingCancels.
136 bool ShouldForwardForGFCFiltering(
137 const GestureEventWithLatencyInfo
& gesture_event
) const;
139 // Sub-filter for suppressing taps immediately after a GestureFlingCancel.
140 bool ShouldForwardForTapSuppression(
141 const GestureEventWithLatencyInfo
& gesture_event
);
143 // Puts the events in a queue to forward them one by one; i.e., forward them
144 // whenever ACK for previous event is received. This queue also tries to
145 // coalesce events as much as possible.
146 void QueueAndForwardIfNecessary(
147 const GestureEventWithLatencyInfo
& gesture_event
);
149 // Merge or append a GestureScrollUpdate or GesturePinchUpdate into
150 // the coalescing queue, forwarding immediately if appropriate.
151 void QueueScrollOrPinchAndForwardIfNecessary(
152 const GestureEventWithLatencyInfo
& gesture_event
);
154 // The number of sent events for which we're awaiting an ack. These events
155 // remain at the head of the queue until ack'ed.
156 size_t EventsInFlightCount() const;
158 // The receiver of all forwarded gesture events.
159 GestureEventQueueClient
* client_
;
161 // True if a GestureFlingStart is in progress on the renderer or
162 // queued without a subsequent queued GestureFlingCancel event.
163 bool fling_in_progress_
;
165 // True if a GestureScrollUpdate sequence is in progress.
166 bool scrolling_in_progress_
;
168 // True if two related gesture events were sent before without waiting
169 // for an ACK, so the next gesture ACK should be ignored.
170 bool ignore_next_ack_
;
172 // An object tracking the state of touchpad on the delivery of mouse events to
173 // the renderer to filter mouse immediately after a touchpad fling canceling
175 // TODO(mohsen): Move touchpad tap suppression out of GestureEventQueue since
176 // GEQ is meant to only be used for touchscreen gesture events.
177 TouchpadTapSuppressionController touchpad_tap_suppression_controller_
;
179 // An object tracking the state of touchscreen on the delivery of gesture tap
180 // events to the renderer to filter taps immediately after a touchscreen fling
182 TouchscreenTapSuppressionController touchscreen_tap_suppression_controller_
;
184 typedef std::deque
<GestureEventWithLatencyInfo
> GestureQueue
;
186 // Queue of coalesced gesture events not yet sent to the renderer. If
187 // |ignore_next_ack_| is false, then the event at the front of the queue has
188 // been sent and is awaiting an ACK, and all other events have yet to be sent.
189 // If |ignore_next_ack_| is true, then the two events at the front of the
190 // queue have been sent, and the second is awaiting an ACK. All other events
191 // have yet to be sent.
192 GestureQueue coalesced_gesture_events_
;
194 // Timer to release a previously deferred gesture event.
195 base::OneShotTimer
<GestureEventQueue
> debounce_deferring_timer_
;
197 // Queue of events that have been deferred for debounce.
198 GestureQueue debouncing_deferral_queue_
;
200 // Time window in which to debounce scroll/fling ends. Note that an interval
201 // of zero effectively disables debouncing.
202 base::TimeDelta debounce_interval_
;
204 DISALLOW_COPY_AND_ASSIGN(GestureEventQueue
);
207 } // namespace content
209 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURE_EVENT_QUEUE_H_