Refactors gesture conversion functions to ui/events/blink
[chromium-blink-merge.git] / content / browser / renderer_host / input / gesture_event_queue.h
blob693d25d6aa02311d89703ad8fe83acf7efa6f07b
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_
8 #include <deque>
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"
22 namespace content {
23 class GestureEventQueueTest;
24 class InputRouter;
25 class MockRenderWidgetHost;
27 // Interface with which the GestureEventQueue can forward gesture events, and
28 // dispatch gesture event responses.
29 class CONTENT_EXPORT GestureEventQueueClient {
30 public:
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
49 // queue.
50 // 3. Taps immediately after a GestureFlingCancel (caused by the same tap) are
51 // filtered.
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
58 // sent together.
59 // TODO(rjkroege): Possibly refactor into a filter chain:
60 // http://crbug.com/148443.
61 class CONTENT_EXPORT GestureEventQueue {
62 public:
63 struct CONTENT_EXPORT Config {
64 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);
82 ~GestureEventQueue();
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
86 // immediately.
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
91 // not empty.
92 void ProcessGestureAck(InputEventAckState ack_result,
93 blink::WebInputEvent::Type type,
94 const ui::LatencyInfo& latency);
96 // Notify the queue that a gesture fling animation in the renderer has ended.
97 void DidStopFlinging();
99 // Returns the |TouchpadTapSuppressionController| instance.
100 TouchpadTapSuppressionController* GetTouchpadTapSuppressionController();
102 void ForwardGestureEvent(const GestureEventWithLatencyInfo& gesture_event);
104 bool empty() const {
105 return coalesced_gesture_events_.empty() &&
106 debouncing_deferral_queue_.empty();
109 int active_fling_count() const { return active_fling_count_; }
111 void set_debounce_interval_time_ms_for_testing(int interval_ms) {
112 debounce_interval_ = base::TimeDelta::FromMilliseconds(interval_ms);
115 private:
116 friend class GestureEventQueueTest;
117 friend class MockRenderWidgetHost;
119 // TODO(mohsen): There are a bunch of ShouldForward.../ShouldDiscard...
120 // methods that are getting confusing. This should be somehow fixed. Maybe
121 // while refactoring GEQ: http://crbug.com/148443.
123 // Inovked on the expiration of the debounce interval to release
124 // deferred events.
125 void SendScrollEndingEventsNow();
127 // Returns |true| if the given GestureFlingCancel should be discarded
128 // as unnecessary.
129 bool ShouldDiscardFlingCancelEvent(
130 const GestureEventWithLatencyInfo& gesture_event) const;
132 // Sub-filter for removing bounces from in-progress scrolls.
133 bool ShouldForwardForBounceReduction(
134 const GestureEventWithLatencyInfo& gesture_event);
136 // Sub-filter for removing unnecessary GestureFlingCancels.
137 bool ShouldForwardForGFCFiltering(
138 const GestureEventWithLatencyInfo& gesture_event) const;
140 // Sub-filter for suppressing taps immediately after a GestureFlingCancel.
141 bool ShouldForwardForTapSuppression(
142 const GestureEventWithLatencyInfo& gesture_event);
144 // Puts the events in a queue to forward them one by one; i.e., forward them
145 // whenever ACK for previous event is received. This queue also tries to
146 // coalesce events as much as possible.
147 void QueueAndForwardIfNecessary(
148 const GestureEventWithLatencyInfo& gesture_event);
150 // Merge or append a GestureScrollUpdate or GesturePinchUpdate into
151 // the coalescing queue, forwarding immediately if appropriate.
152 void QueueScrollOrPinchAndForwardIfNecessary(
153 const GestureEventWithLatencyInfo& gesture_event);
155 // The number of sent events for which we're awaiting an ack. These events
156 // remain at the head of the queue until ack'ed.
157 size_t EventsInFlightCount() const;
159 // The receiver of all forwarded gesture events.
160 GestureEventQueueClient* client_;
162 // Whether there are any active flings in the renderer. As the fling
163 // end notification is asynchronous, we use a count rather than a boolean
164 // to avoid races in bookkeeping when starting a new fling.
165 int active_fling_count_;
167 // True if a GestureScrollUpdate sequence is in progress.
168 bool scrolling_in_progress_;
170 // True if two related gesture events were sent before without waiting
171 // for an ACK, so the next gesture ACK should be ignored.
172 bool ignore_next_ack_;
174 // An object tracking the state of touchpad on the delivery of mouse events to
175 // the renderer to filter mouse immediately after a touchpad fling canceling
176 // tap.
177 // TODO(mohsen): Move touchpad tap suppression out of GestureEventQueue since
178 // GEQ is meant to only be used for touchscreen gesture events.
179 TouchpadTapSuppressionController touchpad_tap_suppression_controller_;
181 // An object tracking the state of touchscreen on the delivery of gesture tap
182 // events to the renderer to filter taps immediately after a touchscreen fling
183 // canceling tap.
184 TouchscreenTapSuppressionController touchscreen_tap_suppression_controller_;
186 typedef std::deque<GestureEventWithLatencyInfo> GestureQueue;
188 // Queue of coalesced gesture events not yet sent to the renderer. If
189 // |ignore_next_ack_| is false, then the event at the front of the queue has
190 // been sent and is awaiting an ACK, and all other events have yet to be sent.
191 // If |ignore_next_ack_| is true, then the two events at the front of the
192 // queue have been sent, and the second is awaiting an ACK. All other events
193 // have yet to be sent.
194 GestureQueue coalesced_gesture_events_;
196 // Timer to release a previously deferred gesture event.
197 base::OneShotTimer<GestureEventQueue> debounce_deferring_timer_;
199 // Queue of events that have been deferred for debounce.
200 GestureQueue debouncing_deferral_queue_;
202 // Time window in which to debounce scroll/fling ends. Note that an interval
203 // of zero effectively disables debouncing.
204 base::TimeDelta debounce_interval_;
206 DISALLOW_COPY_AND_ASSIGN(GestureEventQueue);
209 } // namespace content
211 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURE_EVENT_QUEUE_H_