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. 4 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_EVENT_QUEUE_H_ 6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_EVENT_QUEUE_H_ 7 8 #include <deque> 9 #include <map> 10 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" 16 17 namespace content { 18 19 class CoalescedWebTouchEvent; 20 21 // Interface with which TouchEventQueue can forward touch events, and dispatch 22 // touch event responses. 23 class TouchEventQueueClient { 24 public: 25 virtual ~TouchEventQueueClient() {} 26 27 virtual void SendTouchEventImmediately( 28 const TouchEventWithLatencyInfo& event) = 0; 29 30 virtual void OnTouchEventAck( 31 const TouchEventWithLatencyInfo& event, 32 InputEventAckState ack_result) = 0; 33 }; 34 35 // A queue for throttling and coalescing touch-events. 36 class TouchEventQueue { 37 public: 38 39 // The |client| must outlive the TouchEventQueue. 40 explicit TouchEventQueue(TouchEventQueueClient* client); 41 virtual ~TouchEventQueue(); 42 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); 48 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); 54 55 // Empties the queue of touch events. This may result in any number of gesture 56 // events being sent to the renderer. 57 void FlushQueue(); 58 59 // Returns whether the event-queue is empty. 60 bool empty() const WARN_UNUSED_RESULT { 61 return touch_queue_.empty(); 62 } 63 64 private: 65 friend class MockRenderWidgetHost; 66 friend class ImmediateInputRouterTest; 67 68 CONTENT_EXPORT size_t GetQueueSize() const; 69 CONTENT_EXPORT const TouchEventWithLatencyInfo& GetLatestEvent() const; 70 71 // Pops the touch-event from the top of the queue and sends it to the 72 // TouchEventQueueClient. This reduces the size of the queue by one. 73 void PopTouchEventToClient(InputEventAckState ack_result, 74 const ui::LatencyInfo& renderer_latency_info); 75 76 bool ShouldForwardToRenderer(const WebKit::WebTouchEvent& event) const; 77 78 // Handles touch event forwarding and ack'ed event dispatch. 79 TouchEventQueueClient* client_; 80 81 typedef std::deque<CoalescedWebTouchEvent*> TouchQueue; 82 TouchQueue touch_queue_; 83 84 // Maintain the ACK status for each touch point. 85 typedef std::map<int, InputEventAckState> TouchPointAckStates; 86 TouchPointAckStates touch_ack_states_; 87 88 // Used to defer touch forwarding when ack dispatch triggers |QueueEvent()|. 89 bool dispatching_touch_ack_; 90 91 DISALLOW_COPY_AND_ASSIGN(TouchEventQueue); 92 }; 93 94 } // namespace content 95 96 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_EVENT_QUEUE_H_ 97