- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / touch_event_queue.h
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 CONTENT_EXPORT 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 CONTENT_EXPORT 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   // 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 GestureScrollEnd/GestureFlingStart is
58   // received, we resume the normal flow of sending touch events to renderer.
59   void OnGestureScrollEvent(const GestureEventWithLatencyInfo& gesture_event);
60
61   // Empties the queue of touch events. This may result in any number of gesture
62   // events being sent to the renderer.
63   void FlushQueue();
64
65   // Returns whether the event-queue is empty.
66   bool empty() const WARN_UNUSED_RESULT {
67     return touch_queue_.empty();
68   }
69
70   bool no_touch_to_renderer() const {
71     return no_touch_to_renderer_;
72   }
73
74  private:
75   friend class TouchEventQueueTest;
76
77   size_t GetQueueSize() const;
78   const TouchEventWithLatencyInfo& GetLatestEvent() const;
79
80   // Walks the queue, checking each event for |ShouldForwardToRenderer()|.
81   // If true, forwards the touch event and stops processing further events.
82   // If false, acks the event with |INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS|.
83   void TryForwardNextEventToRenderer();
84
85   // Pops the touch-event from the top of the queue and sends it to the
86   // TouchEventQueueClient. This reduces the size of the queue by one.
87   void PopTouchEventToClient(InputEventAckState ack_result,
88                              const ui::LatencyInfo& renderer_latency_info);
89
90   bool ShouldForwardToRenderer(const WebKit::WebTouchEvent& event) const;
91
92   // Handles touch event forwarding and ack'ed event dispatch.
93   TouchEventQueueClient* client_;
94
95   typedef std::deque<CoalescedWebTouchEvent*> TouchQueue;
96   TouchQueue touch_queue_;
97
98   // Maintain the ACK status for each touch point.
99   typedef std::map<int, InputEventAckState> TouchPointAckStates;
100   TouchPointAckStates touch_ack_states_;
101
102   // Used to defer touch forwarding when ack dispatch triggers |QueueEvent()|.
103   // If not NULL, |dispatching_touch_ack_| is the touch event of which the ack
104   // is being dispatched.
105   CoalescedWebTouchEvent* dispatching_touch_ack_;
106
107   // Don't send touch events to renderer. This is enabled when the page
108   // is scrolling. This behaviour is currently enabled only on aura behind
109   // a flag.
110   bool no_touch_to_renderer_;
111
112   DISALLOW_COPY_AND_ASSIGN(TouchEventQueue);
113 };
114
115 }  // namespace content
116
117 #endif  // CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_EVENT_QUEUE_H_