Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / touch_disposition_gesture_filter.h
1 // Copyright 2014 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_DISPOSITION_GESTURE_FILTER_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_DISPOSITION_GESTURE_FILTER_H_
7
8 #include <queue>
9 #include <set>
10
11 #include "content/browser/renderer_host/input/gesture_event_packet.h"
12 #include "content/common/content_export.h"
13 #include "content/port/common/input_event_ack_state.h"
14 #include "third_party/WebKit/public/web/WebInputEvent.h"
15
16 namespace content {
17
18 // Interface with which the |TouchDispositionGestureFilter| forwards gestures
19 // for a given touch event.
20 class CONTENT_EXPORT TouchDispositionGestureFilterClient {
21  public:
22   virtual void ForwardGestureEvent(const blink::WebGestureEvent&) = 0;
23 };
24
25 // Given a stream of touch-derived gesture packets, produces a refined gesture
26 // sequence based on the ack dispositions of the generating touch events.
27 class CONTENT_EXPORT TouchDispositionGestureFilter {
28  public:
29   explicit TouchDispositionGestureFilter(
30       TouchDispositionGestureFilterClient* client);
31   ~TouchDispositionGestureFilter();
32
33   // To be called upon production of touch-derived gestures by the platform,
34   // *prior* to the generating touch being forward to the renderer.  In
35   // particular, |packet| contains [0, n] gestures that correspond to a given
36   // touch event. It is imperative that a single packet is received for
37   // *each* touch event, even those that did not produce a gesture.
38   enum PacketResult {
39     SUCCESS,              // Packet successfully queued.
40     INVALID_PACKET_ORDER, // Packets were received in the wrong order, i.e.,
41                           // TOUCH_BEGIN should always precede other packets.
42     INVALID_PACKET_TYPE,  // Packet had an invalid type.
43   };
44   PacketResult OnGestureEventPacket(const GestureEventPacket& packet);
45
46   // To be called upon receipt of *all* touch event acks.
47   void OnTouchEventAck(InputEventAckState ack_state);
48
49   // Whether there are any active gesture sequences still queued in the filter.
50   bool IsEmpty() const;
51
52  private:
53   // Utility class for tracking gesture events and dispositions for a single
54   // gesture sequence. A single sequence corresponds to all gestures created
55   // between the first finger down and the last finger up, including gestures
56   // generated by timeouts from a statinoary finger.
57   class GestureSequence {
58    public:
59     struct GestureHandlingState {
60       GestureHandlingState();
61       // True iff the sequence has had at least one touch acked.
62       bool seen_ack;
63       // True iff the sequence has had any touch down event consumed.
64       bool start_consumed;
65       // True iff the first ack received for this sequence reported that no
66       // consumer exists.
67       bool no_consumer;
68     };
69
70     GestureSequence();
71     ~GestureSequence();
72
73     void Push(const GestureEventPacket& packet);
74     void Pop();
75     const GestureEventPacket& Front() const;
76     void UpdateState(GestureEventPacket::GestureSource gesture_source,
77                      InputEventAckState ack_state);
78     bool IsEmpty() const;
79     const GestureHandlingState& state() const { return state_; };
80
81    private:
82     std::queue<GestureEventPacket> packets_;
83     GestureHandlingState state_;
84   };
85   bool IsGesturePrevented(blink::WebInputEvent::Type type,
86                           InputEventAckState current,
87                           const GestureSequence::GestureHandlingState& state)
88       const;
89
90   void UpdateAndDispatchPackets(GestureSequence* sequence,
91                                 InputEventAckState ack_result);
92
93   void FilterAndSendPacket(
94       const GestureEventPacket& packet,
95       const GestureSequence::GestureHandlingState& sequence_state,
96       InputEventAckState ack_state);
97
98   void SendGesture(const blink::WebGestureEvent& gesture);
99   void CancelTapIfNecessary();
100   void CancelFlingIfNecessary();
101   GestureSequence& Head();
102   GestureSequence& Tail();
103
104   TouchDispositionGestureFilterClient* client_;
105   std::queue<GestureSequence> sequences_;
106
107   // If the previous gesture of a given type was dropped instead of being
108   // dispatched, its type will occur in this set. Cleared when a new touch
109   // sequence begins to be acked.
110   std::set<blink::WebInputEvent::Type> last_event_of_type_dropped_;
111
112   // Bookkeeping for inserting synthetic Gesture{Tap,Fling}Cancel events
113   // when necessary, e.g., GestureTapCancel when scrolling begins, or
114   // GestureFlingCancel when a user taps following a GestureFlingStart.
115   bool needs_tap_ending_event_;
116   bool needs_fling_ending_event_;
117
118   DISALLOW_COPY_AND_ASSIGN(TouchDispositionGestureFilter);
119 };
120
121 }  // namespace content
122
123 #endif  // CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_DISPOSITION_GESTURE_FILTER_H_