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_
11 #include "base/basictypes.h"
12 #include "content/common/content_export.h"
13 #include "content/port/browser/event_with_latency_info.h"
14 #include "content/port/common/input_event_ack_state.h"
15 #include "third_party/WebKit/public/web/WebInputEvent.h"
19 class CoalescedWebTouchEvent
;
21 // Interface with which TouchEventQueue can forward touch events, and dispatch
22 // touch event responses.
23 class CONTENT_EXPORT TouchEventQueueClient
{
25 virtual ~TouchEventQueueClient() {}
27 virtual void SendTouchEventImmediately(
28 const TouchEventWithLatencyInfo
& event
) = 0;
30 virtual void OnTouchEventAck(
31 const TouchEventWithLatencyInfo
& event
,
32 InputEventAckState ack_result
) = 0;
35 // A queue for throttling and coalescing touch-events.
36 class CONTENT_EXPORT TouchEventQueue
{
39 // The |client| must outlive the TouchEventQueue.
40 explicit TouchEventQueue(TouchEventQueueClient
* client
);
43 // Adds an event to the queue. The event may be coalesced with previously
44 // queued events (e.g. consecutive touch-move events can be coalesced into a
45 // single touch-move event). The event may also be immediately forwarded to
46 // the renderer (e.g. when there are no other queued touch event).
47 void QueueEvent(const TouchEventWithLatencyInfo
& event
);
49 // Notifies the queue that a touch-event has been processed by the renderer.
50 // At this point, the queue may send one or more gesture events and/or
51 // additional queued touch-events to the renderer.
52 void ProcessTouchAck(InputEventAckState ack_result
,
53 const ui::LatencyInfo
& latency_info
);
55 // When GestureScrollBegin is received, we send a touch cancel to renderer,
56 // route all the following touch events directly to client, and ignore the
57 // ack for the touch cancel. When Gesture{ScrollEnd,FlingStart} is received,
58 // resume the normal flow of sending touch events to the renderer.
59 void OnGestureScrollEvent(const GestureEventWithLatencyInfo
& gesture_event
);
61 // Notifies the queue whether the renderer has at least one touch handler.
62 void OnHasTouchEventHandlers(bool has_handlers
);
64 // Returns whether the currently pending touch event (waiting ACK) is for
65 // a touch start event.
66 bool IsPendingAckTouchStart() const;
68 // Sets whether a delayed touch ack will cancel and flush the current
70 void SetAckTimeoutEnabled(bool enabled
, size_t ack_timeout_delay_ms
);
72 bool empty() const WARN_UNUSED_RESULT
{
73 return touch_queue_
.empty();
77 return touch_queue_
.size();
80 bool ack_timeout_enabled() const {
81 return ack_timeout_enabled_
;
84 bool has_handlers() const {
89 class TouchTimeoutHandler
;
90 friend class TouchTimeoutHandler
;
91 friend class TouchEventQueueTest
;
93 bool HasTimeoutEvent() const;
94 bool IsTimeoutRunningForTesting() const;
95 const TouchEventWithLatencyInfo
& GetLatestEventForTesting() const;
97 // Empties the queue of touch events. This may result in any number of gesture
98 // events being sent to the renderer.
101 // Walks the queue, checking each event for |ShouldForwardToRenderer()|.
102 // If true, forwards the touch event and stops processing further events.
103 // If false, acks the event with |INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS|.
104 void TryForwardNextEventToRenderer();
106 // Pops the touch-event from the top of the queue and sends it to the
107 // TouchEventQueueClient. This reduces the size of the queue by one.
108 void PopTouchEventToClient(InputEventAckState ack_result
,
109 const ui::LatencyInfo
& renderer_latency_info
);
111 bool ShouldForwardToRenderer(const blink::WebTouchEvent
& event
) const;
112 void ForwardToRenderer(const TouchEventWithLatencyInfo
& event
);
113 void UpdateTouchAckStates(const blink::WebTouchEvent
& event
,
114 InputEventAckState ack_result
);
117 // Handles touch event forwarding and ack'ed event dispatch.
118 TouchEventQueueClient
* client_
;
120 typedef std::deque
<CoalescedWebTouchEvent
*> TouchQueue
;
121 TouchQueue touch_queue_
;
123 // Maintain the ACK status for each touch point.
124 typedef std::map
<int, InputEventAckState
> TouchPointAckStates
;
125 TouchPointAckStates touch_ack_states_
;
127 // Used to defer touch forwarding when ack dispatch triggers |QueueEvent()|.
128 // If not NULL, |dispatching_touch_ack_| is the touch event of which the ack
129 // is being dispatched.
130 CoalescedWebTouchEvent
* dispatching_touch_ack_
;
132 // Used to prevent touch timeout scheduling if we receive a synchronous
133 // ack after forwarding a touch event to the client.
134 bool dispatching_touch_
;
136 // Whether there are any registered touch handlers. Defaults to false.
139 // Don't send touch events to the renderer while scrolling.
140 bool scroll_in_progress_
;
142 // Whether an event in the current (multi)touch sequence was consumed by the
143 // renderer. The touch timeout will never be activated when this is true.
144 bool renderer_is_consuming_touch_gesture_
;
146 // Optional handler for timed-out touch event acks, disabled by default.
147 bool ack_timeout_enabled_
;
148 scoped_ptr
<TouchTimeoutHandler
> timeout_handler_
;
150 DISALLOW_COPY_AND_ASSIGN(TouchEventQueue
);
153 } // namespace content
155 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_EVENT_QUEUE_H_