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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_DISPOSITION_GESTURE_FILTER_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_DISPOSITION_GESTURE_FILTER_H_
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"
18 // Interface with which the |TouchDispositionGestureFilter| forwards gestures
19 // for a given touch event.
20 class CONTENT_EXPORT TouchDispositionGestureFilterClient {
22 virtual void ForwardGestureEvent(const blink::WebGestureEvent&) = 0;
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 {
29 explicit TouchDispositionGestureFilter(
30 TouchDispositionGestureFilterClient* client);
31 ~TouchDispositionGestureFilter();
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.
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.
44 PacketResult OnGestureEventPacket(const GestureEventPacket& packet);
46 // To be called upon receipt of *all* touch event acks.
47 void OnTouchEventAck(InputEventAckState ack_state);
49 // Whether there are any active gesture sequences still queued in the filter.
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 {
59 struct GestureHandlingState {
60 GestureHandlingState();
61 // True iff the sequence has had at least one touch acked.
63 // True iff the sequence has had any touch down event consumed.
65 // True iff the first ack received for this sequence reported that no
73 void Push(const GestureEventPacket& packet);
75 const GestureEventPacket& Front() const;
76 void UpdateState(GestureEventPacket::GestureSource gesture_source,
77 InputEventAckState ack_state);
79 const GestureHandlingState& state() const { return state_; };
82 std::queue<GestureEventPacket> packets_;
83 GestureHandlingState state_;
85 bool IsGesturePrevented(blink::WebInputEvent::Type type,
86 InputEventAckState current,
87 const GestureSequence::GestureHandlingState& state)
90 void UpdateAndDispatchPackets(GestureSequence* sequence,
91 InputEventAckState ack_result);
93 void FilterAndSendPacket(
94 const GestureEventPacket& packet,
95 const GestureSequence::GestureHandlingState& sequence_state,
96 InputEventAckState ack_state);
98 void SendGesture(const blink::WebGestureEvent& gesture);
99 void CancelTapIfNecessary();
100 void CancelFlingIfNecessary();
101 GestureSequence& Head();
102 GestureSequence& Tail();
104 TouchDispositionGestureFilterClient* client_;
105 std::queue<GestureSequence> sequences_;
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_;
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_;
118 DISALLOW_COPY_AND_ASSIGN(TouchDispositionGestureFilter);
121 } // namespace content
123 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_DISPOSITION_GESTURE_FILTER_H_