Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / renderer_host / input / touch_event_queue.h
bloba61b90d8eca8c1707d31b3e9bfbadabb175047a0
1 // Copyright 2013 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_TOUCH_EVENT_QUEUE_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_EVENT_QUEUE_H_
8 #include <deque>
9 #include <map>
11 #include "base/basictypes.h"
12 #include "base/time/time.h"
13 #include "content/browser/renderer_host/event_with_latency_info.h"
14 #include "content/common/content_export.h"
15 #include "content/common/input/input_event_ack_state.h"
16 #include "third_party/WebKit/public/web/WebInputEvent.h"
17 #include "ui/gfx/geometry/point_f.h"
19 namespace content {
21 class CoalescedWebTouchEvent;
23 // Interface with which TouchEventQueue can forward touch events, and dispatch
24 // touch event responses.
25 class CONTENT_EXPORT TouchEventQueueClient {
26 public:
27 virtual ~TouchEventQueueClient() {}
29 virtual void SendTouchEventImmediately(
30 const TouchEventWithLatencyInfo& event) = 0;
32 virtual void OnTouchEventAck(
33 const TouchEventWithLatencyInfo& event,
34 InputEventAckState ack_result) = 0;
37 // A queue for throttling and coalescing touch-events.
38 class CONTENT_EXPORT TouchEventQueue {
39 public:
40 struct CONTENT_EXPORT Config {
41 Config();
43 // Controls whether touch ack timeouts will trigger touch cancellation.
44 // Defaults to 200ms.
45 base::TimeDelta touch_ack_timeout_delay;
47 // Whether the platform supports touch ack timeout behavior.
48 // Defaults to false (disabled).
49 bool touch_ack_timeout_supported;
52 // The |client| must outlive the TouchEventQueue.
53 TouchEventQueue(TouchEventQueueClient* client, const Config& config);
55 ~TouchEventQueue();
57 // Adds an event to the queue. The event may be coalesced with previously
58 // queued events (e.g. consecutive touch-move events can be coalesced into a
59 // single touch-move event). The event may also be immediately forwarded to
60 // the renderer (e.g. when there are no other queued touch event).
61 void QueueEvent(const TouchEventWithLatencyInfo& event);
63 // Notifies the queue that a touch-event has been processed by the renderer.
64 // At this point, the queue may send one or more gesture events and/or
65 // additional queued touch-events to the renderer.
66 void ProcessTouchAck(InputEventAckState ack_result,
67 const ui::LatencyInfo& latency_info);
69 // When GestureScrollBegin is received, we send a touch cancel to renderer,
70 // route all the following touch events directly to client, and ignore the
71 // ack for the touch cancel. When Gesture{ScrollEnd,FlingStart} is received,
72 // resume the normal flow of sending touch events to the renderer.
73 void OnGestureScrollEvent(const GestureEventWithLatencyInfo& gesture_event);
75 void OnGestureEventAck(
76 const GestureEventWithLatencyInfo& event,
77 InputEventAckState ack_result);
79 // Notifies the queue whether the renderer has at least one touch handler.
80 void OnHasTouchEventHandlers(bool has_handlers);
82 // Returns whether the currently pending touch event (waiting ACK) is for
83 // a touch start event.
84 bool IsPendingAckTouchStart() const;
86 // Sets whether a delayed touch ack will cancel and flush the current
87 // touch sequence. Note that, if the timeout was previously disabled, enabling
88 // it will take effect only for the following touch sequence.
89 void SetAckTimeoutEnabled(bool enabled);
91 bool IsAckTimeoutEnabled() const;
93 bool IsForwardingTouches();
95 bool empty() const WARN_UNUSED_RESULT {
96 return touch_queue_.empty();
99 size_t size() const {
100 return touch_queue_.size();
103 bool has_handlers() const { return has_handlers_; }
105 private:
106 class TouchTimeoutHandler;
107 class TouchMoveSlopSuppressor;
108 friend class TouchTimeoutHandler;
109 friend class TouchEventQueueTest;
111 bool HasPendingAsyncTouchMoveForTesting() const;
112 bool IsTimeoutRunningForTesting() const;
113 const TouchEventWithLatencyInfo& GetLatestEventForTesting() const;
115 // Empties the queue of touch events. This may result in any number of gesture
116 // events being sent to the renderer.
117 void FlushQueue();
119 // Walks the queue, checking each event with |FilterBeforeForwarding()|.
120 // If allowed, forwards the touch event and stops processing further events.
121 // Otherwise, acks the event with |INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS|.
122 void TryForwardNextEventToRenderer();
124 // Forwards the event at the head of the queue to the renderer.
125 void ForwardNextEventToRenderer();
127 // Pops the touch-event from the head of the queue and acks it to the client.
128 void PopTouchEventToClient(InputEventAckState ack_result);
130 // Pops the touch-event from the top of the queue and acks it to the client,
131 // updating the event with |renderer_latency_info|.
132 void PopTouchEventToClient(InputEventAckState ack_result,
133 const ui::LatencyInfo& renderer_latency_info);
135 // Ack all coalesced events in |acked_event| to the client with |ack_result|,
136 // updating the acked events with |optional_latency_info| if it exists.
137 void AckTouchEventToClient(InputEventAckState ack_result,
138 scoped_ptr<CoalescedWebTouchEvent> acked_event,
139 const ui::LatencyInfo* optional_latency_info);
141 // Safely pop the head of the queue.
142 scoped_ptr<CoalescedWebTouchEvent> PopTouchEvent();
144 // Dispatch |touch| to the client. Before dispatching, updates pointer
145 // states in touchmove events for pointers that have not changed position.
146 void SendTouchEventImmediately(TouchEventWithLatencyInfo* touch);
148 enum PreFilterResult {
149 ACK_WITH_NO_CONSUMER_EXISTS,
150 ACK_WITH_NOT_CONSUMED,
151 FORWARD_TO_RENDERER,
153 // Filter touches prior to forwarding to the renderer, e.g., if the renderer
154 // has no touch handler.
155 PreFilterResult FilterBeforeForwarding(const blink::WebTouchEvent& event);
156 void ForwardToRenderer(const TouchEventWithLatencyInfo& event);
157 void UpdateTouchConsumerStates(const blink::WebTouchEvent& event,
158 InputEventAckState ack_result);
160 // Handles touch event forwarding and ack'ed event dispatch.
161 TouchEventQueueClient* client_;
163 typedef std::deque<CoalescedWebTouchEvent*> TouchQueue;
164 TouchQueue touch_queue_;
166 // Position of the first touch in the most recent sequence forwarded to the
167 // client.
168 gfx::PointF touch_sequence_start_position_;
170 // Used to defer touch forwarding when ack dispatch triggers |QueueEvent()|.
171 // True within the scope of |AckTouchEventToClient()|.
172 bool dispatching_touch_ack_;
174 // Used to prevent touch timeout scheduling if we receive a synchronous
175 // ack after forwarding a touch event to the client.
176 bool dispatching_touch_;
178 // Whether the renderer has at least one touch handler.
179 bool has_handlers_;
181 // Whether any pointer in the touch sequence reported having a consumer.
182 bool has_handler_for_current_sequence_;
184 // Whether to allow any remaining touches for the current sequence. Note that
185 // this is a stricter condition than an empty |touch_consumer_states_|, as it
186 // also prevents forwarding of touchstart events for new pointers in the
187 // current sequence. This is only used when the event is synthetically
188 // cancelled after a touch timeout.
189 bool drop_remaining_touches_in_sequence_;
191 // Optional handler for timed-out touch event acks.
192 scoped_ptr<TouchTimeoutHandler> timeout_handler_;
194 // Suppression of TouchMove's within a slop region when a sequence has not yet
195 // been preventDefaulted.
196 scoped_ptr<TouchMoveSlopSuppressor> touchmove_slop_suppressor_;
198 // Whether touch events should remain buffered and dispatched asynchronously
199 // while a scroll sequence is active. In this mode, touchmove's are throttled
200 // and ack'ed immediately, but remain buffered in |pending_async_touchmove_|
201 // until a sufficient time period has elapsed since the last sent touch event.
202 // For details see the design doc at http://goo.gl/lVyJAa.
203 bool send_touch_events_async_;
204 scoped_ptr<TouchEventWithLatencyInfo> pending_async_touchmove_;
205 double last_sent_touch_timestamp_sec_;
207 // Event is saved to compare pointer positions for new touchmove events.
208 scoped_ptr<blink::WebTouchEvent> last_sent_touchevent_;
210 DISALLOW_COPY_AND_ASSIGN(TouchEventQueue);
213 } // namespace content
215 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_EVENT_QUEUE_H_