Add ICU message format support
[chromium-blink-merge.git] / content / browser / renderer_host / input / touch_event_queue.h
blob3a4bbdf4355065faca7457c6fd6693810be26741
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 // Touch ack timeout delay for desktop sites. If zero, timeout behavior
44 // is disabled for such sites. Defaults to 200ms.
45 base::TimeDelta desktop_touch_ack_timeout_delay;
47 // Touch ack timeout delay for mobile sites. If zero, timeout behavior
48 // is disabled for such sites. Defaults to 1000ms.
49 base::TimeDelta mobile_touch_ack_timeout_delay;
51 // Whether the platform supports touch ack timeout behavior.
52 // Defaults to false (disabled).
53 bool touch_ack_timeout_supported;
56 // The |client| must outlive the TouchEventQueue.
57 TouchEventQueue(TouchEventQueueClient* client, const Config& config);
59 ~TouchEventQueue();
61 // Adds an event to the queue. The event may be coalesced with previously
62 // queued events (e.g. consecutive touch-move events can be coalesced into a
63 // single touch-move event). The event may also be immediately forwarded to
64 // the renderer (e.g. when there are no other queued touch event).
65 void QueueEvent(const TouchEventWithLatencyInfo& event);
67 // Notifies the queue that a touch-event has been processed by the renderer.
68 // At this point, if the ack is for async touchmove, remove the uncancelable
69 // touchmove from the front of the queue and decide if it should dispatch the
70 // next pending async touch move event, otherwise the queue may send one or
71 // more gesture events and/or additional queued touch-events to the renderer.
72 void ProcessTouchAck(InputEventAckState ack_result,
73 const ui::LatencyInfo& latency_info,
74 const uint32 unique_touch_event_id);
76 // When GestureScrollBegin is received, we send a touch cancel to renderer,
77 // route all the following touch events directly to client, and ignore the
78 // ack for the touch cancel. When Gesture{ScrollEnd,FlingStart} is received,
79 // resume the normal flow of sending touch events to the renderer.
80 void OnGestureScrollEvent(const GestureEventWithLatencyInfo& gesture_event);
82 void OnGestureEventAck(
83 const GestureEventWithLatencyInfo& event,
84 InputEventAckState ack_result);
86 // Notifies the queue whether the renderer has at least one touch handler.
87 void OnHasTouchEventHandlers(bool has_handlers);
89 // Returns whether the currently pending touch event (waiting ACK) is for
90 // a touch start event.
91 bool IsPendingAckTouchStart() const;
93 // Sets whether a delayed touch ack will cancel and flush the current
94 // touch sequence. Note that, if the timeout was previously disabled, enabling
95 // it will take effect only for the following touch sequence.
96 void SetAckTimeoutEnabled(bool enabled);
98 // Sets whether the current site has a mobile friendly viewport. This
99 // determines which ack timeout delay will be used for *future* touch events.
100 // The default assumption is that the site is *not* mobile-optimized.
101 void SetIsMobileOptimizedSite(bool mobile_optimized_site);
103 // Whether ack timeout behavior is supported and enabled for the current site.
104 bool IsAckTimeoutEnabled() const;
106 bool IsForwardingTouches();
108 bool empty() const WARN_UNUSED_RESULT {
109 return touch_queue_.empty();
112 size_t size() const {
113 return touch_queue_.size();
116 bool has_handlers() const { return has_handlers_; }
118 size_t uncancelable_touch_moves_pending_ack_count() const {
119 return ack_pending_async_touchmove_ids_.size();
122 private:
123 class TouchTimeoutHandler;
124 class TouchMoveSlopSuppressor;
125 friend class TouchTimeoutHandler;
126 friend class TouchEventQueueTest;
128 bool HasPendingAsyncTouchMoveForTesting() const;
129 bool IsTimeoutRunningForTesting() const;
130 const TouchEventWithLatencyInfo& GetLatestEventForTesting() const;
132 // Empties the queue of touch events. This may result in any number of gesture
133 // events being sent to the renderer.
134 void FlushQueue();
136 // Walks the queue, checking each event with |FilterBeforeForwarding()|.
137 // If allowed, forwards the touch event and stops processing further events.
138 // Otherwise, acks the event with |INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS|.
139 void TryForwardNextEventToRenderer();
141 // Forwards the event at the head of the queue to the renderer.
142 void ForwardNextEventToRenderer();
144 // Pops the touch-event from the head of the queue and acks it to the client.
145 void PopTouchEventToClient(InputEventAckState ack_result);
147 // Pops the touch-event from the top of the queue and acks it to the client,
148 // updating the event with |renderer_latency_info|.
149 void PopTouchEventToClient(InputEventAckState ack_result,
150 const ui::LatencyInfo& renderer_latency_info);
152 // Ack all coalesced events in |acked_event| to the client with |ack_result|,
153 // updating the acked events with |optional_latency_info| if it exists.
154 void AckTouchEventToClient(InputEventAckState ack_result,
155 scoped_ptr<CoalescedWebTouchEvent> acked_event,
156 const ui::LatencyInfo* optional_latency_info);
158 // Safely pop the head of the queue.
159 scoped_ptr<CoalescedWebTouchEvent> PopTouchEvent();
161 // Dispatch |touch| to the client. Before dispatching, updates pointer
162 // states in touchmove events for pointers that have not changed position.
163 void SendTouchEventImmediately(TouchEventWithLatencyInfo* touch);
165 enum PreFilterResult {
166 ACK_WITH_NO_CONSUMER_EXISTS,
167 ACK_WITH_NOT_CONSUMED,
168 FORWARD_TO_RENDERER,
170 // Filter touches prior to forwarding to the renderer, e.g., if the renderer
171 // has no touch handler.
172 PreFilterResult FilterBeforeForwarding(const blink::WebTouchEvent& event);
173 void ForwardToRenderer(const TouchEventWithLatencyInfo& event);
174 void UpdateTouchConsumerStates(const blink::WebTouchEvent& event,
175 InputEventAckState ack_result);
176 void FlushPendingAsyncTouchmove();
178 // Handles touch event forwarding and ack'ed event dispatch.
179 TouchEventQueueClient* client_;
181 typedef std::deque<CoalescedWebTouchEvent*> TouchQueue;
182 TouchQueue touch_queue_;
184 // Position of the first touch in the most recent sequence forwarded to the
185 // client.
186 gfx::PointF touch_sequence_start_position_;
188 // Used to defer touch forwarding when ack dispatch triggers |QueueEvent()|.
189 // True within the scope of |AckTouchEventToClient()|.
190 bool dispatching_touch_ack_;
192 // Used to prevent touch timeout scheduling and increase the count for async
193 // touchmove if we receive a synchronous ack after forwarding a touch event
194 // to the client.
195 bool dispatching_touch_;
197 // Whether the renderer has at least one touch handler.
198 bool has_handlers_;
200 // Whether any pointer in the touch sequence reported having a consumer.
201 bool has_handler_for_current_sequence_;
203 // Whether to allow any remaining touches for the current sequence. Note that
204 // this is a stricter condition than an empty |touch_consumer_states_|, as it
205 // also prevents forwarding of touchstart events for new pointers in the
206 // current sequence. This is only used when the event is synthetically
207 // cancelled after a touch timeout.
208 bool drop_remaining_touches_in_sequence_;
210 // Optional handler for timed-out touch event acks.
211 scoped_ptr<TouchTimeoutHandler> timeout_handler_;
213 // Suppression of TouchMove's within a slop region when a sequence has not yet
214 // been preventDefaulted.
215 scoped_ptr<TouchMoveSlopSuppressor> touchmove_slop_suppressor_;
217 // Whether touch events should remain buffered and dispatched asynchronously
218 // while a scroll sequence is active. In this mode, touchmove's are throttled
219 // and ack'ed immediately, but remain buffered in |pending_async_touchmove_|
220 // until a sufficient time period has elapsed since the last sent touch event.
221 // For details see the design doc at http://goo.gl/lVyJAa.
222 bool send_touch_events_async_;
223 scoped_ptr<TouchEventWithLatencyInfo> pending_async_touchmove_;
225 // For uncancelable touch moves, not only we send a fake ack, but also a real
226 // ack from render, which we use to decide when to send the next async
227 // touchmove. This can help avoid the touch event queue keep growing when
228 // render handles touchmove slow. We use a queue
229 // ack_pending_async_touchmove_ids to store the recent dispatched
230 // uncancelable touchmoves which are still waiting for their acks back from
231 // render. We do not put them back to the front the touch_event_queue any
232 // more.
233 std::deque<uint32> ack_pending_async_touchmove_ids_;
235 double last_sent_touch_timestamp_sec_;
237 // Event is saved to compare pointer positions for new touchmove events.
238 scoped_ptr<blink::WebTouchEvent> last_sent_touchevent_;
240 DISALLOW_COPY_AND_ASSIGN(TouchEventQueue);
243 } // namespace content
245 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_EVENT_QUEUE_H_