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
;
77 // Whether to filter unnecessary GestureFlingCancel events. Filtering should
78 // be disabled if there may be content-targetting fling curves about which
79 // the renderer is unaware (e.g., with Android WebView).
81 bool enable_fling_cancel_filtering
;
84 // Both |client| and |touchpad_client| must outlive the GestureEventQueue.
85 GestureEventQueue(GestureEventQueueClient
* client
,
86 TouchpadTapSuppressionControllerClient
* touchpad_client
,
87 const Config
& config
);
90 // Adds a gesture to the queue if it passes the relevant filters. If
91 // there are no events currently queued, the event will be forwarded
93 void QueueEvent(const GestureEventWithLatencyInfo
&);
95 // Indicates that the caller has received an acknowledgement from the renderer
96 // with state |ack_result| and event |type|. May send events if the queue is
98 void ProcessGestureAck(InputEventAckState ack_result
,
99 blink::WebInputEvent::Type type
,
100 const ui::LatencyInfo
& latency
);
102 // Notify the queue that a gesture fling animation in the renderer has ended.
103 void DidStopFlinging();
105 // Returns the |TouchpadTapSuppressionController| instance.
106 TouchpadTapSuppressionController
* GetTouchpadTapSuppressionController();
108 void ForwardGestureEvent(const GestureEventWithLatencyInfo
& gesture_event
);
111 return coalesced_gesture_events_
.empty() &&
112 debouncing_deferral_queue_
.empty();
115 int active_fling_count() const { return active_fling_count_
; }
117 void set_debounce_interval_time_ms_for_testing(int interval_ms
) {
118 debounce_interval_
= base::TimeDelta::FromMilliseconds(interval_ms
);
122 friend class GestureEventQueueTest
;
123 friend class MockRenderWidgetHost
;
125 // TODO(mohsen): There are a bunch of ShouldForward.../ShouldDiscard...
126 // methods that are getting confusing. This should be somehow fixed. Maybe
127 // while refactoring GEQ: http://crbug.com/148443.
129 // Inovked on the expiration of the debounce interval to release
131 void SendScrollEndingEventsNow();
133 // Returns |true| if the given GestureFlingCancel should be discarded
135 bool ShouldDiscardFlingCancelEvent(
136 const GestureEventWithLatencyInfo
& gesture_event
) const;
138 // Sub-filter for removing bounces from in-progress scrolls.
139 bool ShouldForwardForBounceReduction(
140 const GestureEventWithLatencyInfo
& gesture_event
);
142 // Sub-filter for removing unnecessary GestureFlingCancels.
143 bool ShouldForwardForGFCFiltering(
144 const GestureEventWithLatencyInfo
& gesture_event
) const;
146 // Sub-filter for suppressing taps immediately after a GestureFlingCancel.
147 bool ShouldForwardForTapSuppression(
148 const GestureEventWithLatencyInfo
& gesture_event
);
150 // Puts the events in a queue to forward them one by one; i.e., forward them
151 // whenever ACK for previous event is received. This queue also tries to
152 // coalesce events as much as possible.
153 void QueueAndForwardIfNecessary(
154 const GestureEventWithLatencyInfo
& gesture_event
);
156 // Merge or append a GestureScrollUpdate or GesturePinchUpdate into
157 // the coalescing queue, forwarding immediately if appropriate.
158 void QueueScrollOrPinchAndForwardIfNecessary(
159 const GestureEventWithLatencyInfo
& gesture_event
);
161 // The number of sent events for which we're awaiting an ack. These events
162 // remain at the head of the queue until ack'ed.
163 size_t EventsInFlightCount() const;
165 // The receiver of all forwarded gesture events.
166 GestureEventQueueClient
* client_
;
168 // Whether to filter unnecessary GestureFlingCancel events.
169 bool enable_fling_cancel_filtering_
;
171 // Whether there are any active flings in the renderer. As the fling
172 // end notification is asynchronous, we use a count rather than a boolean
173 // to avoid races in bookkeeping when starting a new fling.
174 int active_fling_count_
;
176 // True if a GestureScrollUpdate sequence is in progress.
177 bool scrolling_in_progress_
;
179 // True if two related gesture events were sent before without waiting
180 // for an ACK, so the next gesture ACK should be ignored.
181 bool ignore_next_ack_
;
183 // An object tracking the state of touchpad on the delivery of mouse events to
184 // the renderer to filter mouse immediately after a touchpad fling canceling
186 // TODO(mohsen): Move touchpad tap suppression out of GestureEventQueue since
187 // GEQ is meant to only be used for touchscreen gesture events.
188 TouchpadTapSuppressionController touchpad_tap_suppression_controller_
;
190 // An object tracking the state of touchscreen on the delivery of gesture tap
191 // events to the renderer to filter taps immediately after a touchscreen fling
193 TouchscreenTapSuppressionController touchscreen_tap_suppression_controller_
;
195 typedef std::deque
<GestureEventWithLatencyInfo
> GestureQueue
;
197 // Queue of coalesced gesture events not yet sent to the renderer. If
198 // |ignore_next_ack_| is false, then the event at the front of the queue has
199 // been sent and is awaiting an ACK, and all other events have yet to be sent.
200 // If |ignore_next_ack_| is true, then the two events at the front of the
201 // queue have been sent, and the second is awaiting an ACK. All other events
202 // have yet to be sent.
203 GestureQueue coalesced_gesture_events_
;
205 // Timer to release a previously deferred gesture event.
206 base::OneShotTimer
<GestureEventQueue
> debounce_deferring_timer_
;
208 // Queue of events that have been deferred for debounce.
209 GestureQueue debouncing_deferral_queue_
;
211 // Time window in which to debounce scroll/fling ends. Note that an interval
212 // of zero effectively disables debouncing.
213 base::TimeDelta debounce_interval_
;
215 DISALLOW_COPY_AND_ASSIGN(GestureEventQueue
);
218 } // namespace content
220 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURE_EVENT_QUEUE_H_