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 #include "content/browser/renderer_host/input/touch_event_queue.h"
7 #include "base/auto_reset.h"
8 #include "base/debug/trace_event.h"
9 #include "base/stl_util.h"
10 #include "content/browser/renderer_host/input/timeout_monitor.h"
11 #include "content/common/input/web_touch_event_traits.h"
12 #include "ui/gfx/geometry/point_f.h"
14 using blink::WebInputEvent;
15 using blink::WebTouchEvent;
16 using blink::WebTouchPoint;
17 using ui::LatencyInfo;
22 // Time interval at which touchmove events will be forwarded to the client while
23 // scrolling is active and possible.
24 const double kAsyncTouchMoveIntervalSec = .2;
26 // A slop region just larger than that used by many web applications. When
27 // touchmove's are being sent asynchronously, movement outside this region will
28 // trigger an immediate async touchmove to cancel potential tap-related logic.
29 const double kApplicationSlopRegionLengthDipsSqared = 15. * 15.;
31 // Using a small epsilon when comparing slop distances allows pixel perfect
32 // slop determination when using fractional DIP coordinates (assuming the slop
33 // region and DPI scale are reasonably proportioned).
34 const float kSlopEpsilon = .05f;
36 TouchEventWithLatencyInfo ObtainCancelEventForTouchEvent(
37 const TouchEventWithLatencyInfo& event_to_cancel) {
38 TouchEventWithLatencyInfo event = event_to_cancel;
39 WebTouchEventTraits::ResetTypeAndTouchStates(
40 WebInputEvent::TouchCancel,
41 // TODO(rbyers): Shouldn't we use a fresh timestamp?
42 event.event.timeStampSeconds,
47 bool ShouldTouchTriggerTimeout(const WebTouchEvent& event) {
48 return (event.type == WebInputEvent::TouchStart ||
49 event.type == WebInputEvent::TouchMove) &&
50 !WebInputEventTraits::IgnoresAckDisposition(event);
53 bool OutsideApplicationSlopRegion(const WebTouchEvent& event,
54 const gfx::PointF& anchor) {
55 return (gfx::PointF(event.touches[0].position) - anchor).LengthSquared() >
56 kApplicationSlopRegionLengthDipsSqared;
62 // Cancels a touch sequence if a touchstart or touchmove ack response is
63 // sufficiently delayed.
64 class TouchEventQueue::TouchTimeoutHandler {
66 TouchTimeoutHandler(TouchEventQueue* touch_queue,
67 base::TimeDelta timeout_delay)
68 : touch_queue_(touch_queue),
69 timeout_delay_(timeout_delay),
70 pending_ack_state_(PENDING_ACK_NONE),
71 timeout_monitor_(base::Bind(&TouchTimeoutHandler::OnTimeOut,
72 base::Unretained(this))),
74 enabled_for_current_sequence_(false) {
75 DCHECK(timeout_delay != base::TimeDelta());
78 ~TouchTimeoutHandler() {}
80 void StartIfNecessary(const TouchEventWithLatencyInfo& event) {
81 DCHECK_EQ(pending_ack_state_, PENDING_ACK_NONE);
85 if (!ShouldTouchTriggerTimeout(event.event))
88 if (WebTouchEventTraits::IsTouchSequenceStart(event.event))
89 enabled_for_current_sequence_ = true;
91 if (!enabled_for_current_sequence_)
94 timeout_event_ = event;
95 timeout_monitor_.Restart(timeout_delay_);
98 bool ConfirmTouchEvent(InputEventAckState ack_result) {
99 switch (pending_ack_state_) {
100 case PENDING_ACK_NONE:
101 if (ack_result == INPUT_EVENT_ACK_STATE_CONSUMED)
102 enabled_for_current_sequence_ = false;
103 timeout_monitor_.Stop();
105 case PENDING_ACK_ORIGINAL_EVENT:
106 if (AckedTimeoutEventRequiresCancel(ack_result)) {
107 SetPendingAckState(PENDING_ACK_CANCEL_EVENT);
108 TouchEventWithLatencyInfo cancel_event =
109 ObtainCancelEventForTouchEvent(timeout_event_);
110 touch_queue_->SendTouchEventImmediately(cancel_event);
112 SetPendingAckState(PENDING_ACK_NONE);
113 touch_queue_->UpdateTouchConsumerStates(timeout_event_.event,
117 case PENDING_ACK_CANCEL_EVENT:
118 SetPendingAckState(PENDING_ACK_NONE);
124 bool FilterEvent(const WebTouchEvent& event) {
125 return HasTimeoutEvent();
128 void SetEnabled(bool enabled) {
129 if (enabled_ == enabled)
137 enabled_for_current_sequence_ = false;
138 // Only reset the |timeout_handler_| if the timer is running and has not
139 // yet timed out. This ensures that an already timed out sequence is
140 // properly flushed by the handler.
141 if (IsTimeoutTimerRunning()) {
142 pending_ack_state_ = PENDING_ACK_NONE;
143 timeout_monitor_.Stop();
147 bool IsTimeoutTimerRunning() const { return timeout_monitor_.IsRunning(); }
149 bool enabled() const { return enabled_; }
152 enum PendingAckState {
154 PENDING_ACK_ORIGINAL_EVENT,
155 PENDING_ACK_CANCEL_EVENT,
159 SetPendingAckState(PENDING_ACK_ORIGINAL_EVENT);
160 touch_queue_->FlushQueue();
163 // Skip a cancel event if the timed-out event had no consumer and was the
164 // initial event in the gesture.
165 bool AckedTimeoutEventRequiresCancel(InputEventAckState ack_result) const {
166 DCHECK(HasTimeoutEvent());
167 if (ack_result != INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS)
169 return !WebTouchEventTraits::IsTouchSequenceStart(timeout_event_.event);
172 void SetPendingAckState(PendingAckState new_pending_ack_state) {
173 DCHECK_NE(pending_ack_state_, new_pending_ack_state);
174 switch (new_pending_ack_state) {
175 case PENDING_ACK_ORIGINAL_EVENT:
176 DCHECK_EQ(pending_ack_state_, PENDING_ACK_NONE);
177 TRACE_EVENT_ASYNC_BEGIN0("input", "TouchEventTimeout", this);
179 case PENDING_ACK_CANCEL_EVENT:
180 DCHECK_EQ(pending_ack_state_, PENDING_ACK_ORIGINAL_EVENT);
181 DCHECK(!timeout_monitor_.IsRunning());
182 DCHECK(touch_queue_->empty());
183 TRACE_EVENT_ASYNC_STEP_INTO0(
184 "input", "TouchEventTimeout", this, "CancelEvent");
186 case PENDING_ACK_NONE:
187 DCHECK(!timeout_monitor_.IsRunning());
188 DCHECK(touch_queue_->empty());
189 TRACE_EVENT_ASYNC_END0("input", "TouchEventTimeout", this);
192 pending_ack_state_ = new_pending_ack_state;
195 bool HasTimeoutEvent() const {
196 return pending_ack_state_ != PENDING_ACK_NONE;
200 TouchEventQueue* touch_queue_;
202 // How long to wait on a touch ack before cancelling the touch sequence.
203 base::TimeDelta timeout_delay_;
205 // The touch event source for which we expect the next ack.
206 PendingAckState pending_ack_state_;
208 // The event for which the ack timeout is triggered.
209 TouchEventWithLatencyInfo timeout_event_;
211 // Provides timeout-based callback behavior.
212 TimeoutMonitor timeout_monitor_;
215 bool enabled_for_current_sequence_;
218 // Provides touchmove slop suppression for a single touch that remains within
219 // a given slop region, unless the touchstart is preventDefault'ed.
220 // TODO(jdduke): Use a flag bundled with each TouchEvent declaring whether it
221 // has exceeded the slop region, removing duplicated slop determination logic.
222 class TouchEventQueue::TouchMoveSlopSuppressor {
224 TouchMoveSlopSuppressor(double slop_suppression_length_dips)
225 : slop_suppression_length_dips_squared_(0),
226 suppressing_touchmoves_(false) {
227 if (slop_suppression_length_dips) {
228 slop_suppression_length_dips += kSlopEpsilon;
229 slop_suppression_length_dips_squared_ =
230 slop_suppression_length_dips * slop_suppression_length_dips;
234 bool FilterEvent(const WebTouchEvent& event) {
235 if (WebTouchEventTraits::IsTouchSequenceStart(event)) {
236 touch_sequence_start_position_ =
237 gfx::PointF(event.touches[0].position);
238 suppressing_touchmoves_ = slop_suppression_length_dips_squared_ != 0;
241 if (event.type == WebInputEvent::TouchEnd ||
242 event.type == WebInputEvent::TouchCancel)
243 suppressing_touchmoves_ = false;
245 if (event.type != WebInputEvent::TouchMove)
248 if (suppressing_touchmoves_) {
249 // Movement with a secondary pointer should terminate suppression.
250 if (event.touchesLength > 1) {
251 suppressing_touchmoves_ = false;
252 } else if (event.touchesLength == 1) {
253 // Movement outside of the slop region should terminate suppression.
254 gfx::PointF position(event.touches[0].position);
255 if ((position - touch_sequence_start_position_).LengthSquared() >
256 slop_suppression_length_dips_squared_)
257 suppressing_touchmoves_ = false;
260 return suppressing_touchmoves_;
263 void ConfirmTouchEvent(InputEventAckState ack_result) {
264 if (ack_result == INPUT_EVENT_ACK_STATE_CONSUMED)
265 suppressing_touchmoves_ = false;
268 bool suppressing_touchmoves() const { return suppressing_touchmoves_; }
271 double slop_suppression_length_dips_squared_;
272 gfx::PointF touch_sequence_start_position_;
273 bool suppressing_touchmoves_;
275 DISALLOW_COPY_AND_ASSIGN(TouchMoveSlopSuppressor);
278 // This class represents a single coalesced touch event. However, it also keeps
279 // track of all the original touch-events that were coalesced into a single
280 // event. The coalesced event is forwarded to the renderer, while the original
281 // touch-events are sent to the Client (on ACK for the coalesced event) so that
282 // the Client receives the event with their original timestamp.
283 class CoalescedWebTouchEvent {
285 CoalescedWebTouchEvent(const TouchEventWithLatencyInfo& event,
286 bool suppress_client_ack)
287 : coalesced_event_(event), suppress_client_ack_(suppress_client_ack) {
288 TRACE_EVENT_ASYNC_BEGIN0("input", "TouchEventQueue::QueueEvent", this);
291 ~CoalescedWebTouchEvent() {
292 TRACE_EVENT_ASYNC_END0("input", "TouchEventQueue::QueueEvent", this);
295 // Coalesces the event with the existing event if possible. Returns whether
296 // the event was coalesced.
297 bool CoalesceEventIfPossible(
298 const TouchEventWithLatencyInfo& event_with_latency) {
299 if (suppress_client_ack_)
302 if (!coalesced_event_.CanCoalesceWith(event_with_latency))
305 // Addition of the first event to |uncoaleseced_events_to_ack_| is deferred
306 // until the first coalesced event, optimizing the (common) case where the
307 // event is not coalesced at all.
308 if (uncoaleseced_events_to_ack_.empty())
309 uncoaleseced_events_to_ack_.push_back(coalesced_event_);
311 TRACE_EVENT_INSTANT0(
312 "input", "TouchEventQueue::MoveCoalesced", TRACE_EVENT_SCOPE_THREAD);
313 coalesced_event_.CoalesceWith(event_with_latency);
314 uncoaleseced_events_to_ack_.push_back(event_with_latency);
315 DCHECK_GE(uncoaleseced_events_to_ack_.size(), 2U);
319 void DispatchAckToClient(InputEventAckState ack_result,
320 const ui::LatencyInfo* optional_latency_info,
321 TouchEventQueueClient* client) {
323 if (suppress_client_ack_)
326 if (uncoaleseced_events_to_ack_.empty()) {
327 if (optional_latency_info)
328 coalesced_event_.latency.AddNewLatencyFrom(*optional_latency_info);
329 client->OnTouchEventAck(coalesced_event_, ack_result);
333 DCHECK_GE(uncoaleseced_events_to_ack_.size(), 2U);
334 for (WebTouchEventWithLatencyList::iterator
335 iter = uncoaleseced_events_to_ack_.begin(),
336 end = uncoaleseced_events_to_ack_.end();
339 if (optional_latency_info)
340 iter->latency.AddNewLatencyFrom(*optional_latency_info);
341 client->OnTouchEventAck(*iter, ack_result);
345 const TouchEventWithLatencyInfo& coalesced_event() const {
346 return coalesced_event_;
350 // This is the event that is forwarded to the renderer.
351 TouchEventWithLatencyInfo coalesced_event_;
353 // This is the list of the original events that were coalesced, each requiring
354 // future ack dispatch to the client.
355 // Note that this will be empty if no coalescing has occurred.
356 typedef std::vector<TouchEventWithLatencyInfo> WebTouchEventWithLatencyList;
357 WebTouchEventWithLatencyList uncoaleseced_events_to_ack_;
359 bool suppress_client_ack_;
361 DISALLOW_COPY_AND_ASSIGN(CoalescedWebTouchEvent);
364 TouchEventQueue::Config::Config()
365 : touchmove_slop_suppression_length_dips(0),
366 touch_scrolling_mode(TOUCH_SCROLLING_MODE_DEFAULT),
367 touch_ack_timeout_delay(base::TimeDelta::FromMilliseconds(200)),
368 touch_ack_timeout_supported(false) {
371 TouchEventQueue::TouchEventQueue(TouchEventQueueClient* client,
372 const Config& config)
374 dispatching_touch_ack_(NULL),
375 dispatching_touch_(false),
377 drop_remaining_touches_in_sequence_(false),
378 touchmove_slop_suppressor_(new TouchMoveSlopSuppressor(
379 config.touchmove_slop_suppression_length_dips)),
380 send_touch_events_async_(false),
381 needs_async_touchmove_for_outer_slop_region_(false),
382 last_sent_touch_timestamp_sec_(0),
383 touch_scrolling_mode_(config.touch_scrolling_mode) {
385 if (config.touch_ack_timeout_supported) {
386 timeout_handler_.reset(
387 new TouchTimeoutHandler(this, config.touch_ack_timeout_delay));
391 TouchEventQueue::~TouchEventQueue() {
392 if (!touch_queue_.empty())
393 STLDeleteElements(&touch_queue_);
396 void TouchEventQueue::QueueEvent(const TouchEventWithLatencyInfo& event) {
397 TRACE_EVENT0("input", "TouchEventQueue::QueueEvent");
399 // If the queueing of |event| was triggered by an ack dispatch, defer
400 // processing the event until the dispatch has finished.
401 if (touch_queue_.empty() && !dispatching_touch_ack_) {
402 // Optimization of the case without touch handlers. Removing this path
403 // yields identical results, but this avoids unnecessary allocations.
404 PreFilterResult filter_result = FilterBeforeForwarding(event.event);
405 if (filter_result != FORWARD_TO_RENDERER) {
406 client_->OnTouchEventAck(event,
407 filter_result == ACK_WITH_NO_CONSUMER_EXISTS
408 ? INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS
409 : INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
413 // There is no touch event in the queue. Forward it to the renderer
415 touch_queue_.push_back(new CoalescedWebTouchEvent(event, false));
416 ForwardNextEventToRenderer();
420 // If the last queued touch-event was a touch-move, and the current event is
421 // also a touch-move, then the events can be coalesced into a single event.
422 if (touch_queue_.size() > 1) {
423 CoalescedWebTouchEvent* last_event = touch_queue_.back();
424 if (last_event->CoalesceEventIfPossible(event))
427 touch_queue_.push_back(new CoalescedWebTouchEvent(event, false));
430 void TouchEventQueue::ProcessTouchAck(InputEventAckState ack_result,
431 const LatencyInfo& latency_info) {
432 TRACE_EVENT0("input", "TouchEventQueue::ProcessTouchAck");
434 DCHECK(!dispatching_touch_ack_);
435 dispatching_touch_ = false;
437 if (timeout_handler_ && timeout_handler_->ConfirmTouchEvent(ack_result))
440 touchmove_slop_suppressor_->ConfirmTouchEvent(ack_result);
442 if (touch_queue_.empty())
445 PopTouchEventToClient(ack_result, latency_info);
446 TryForwardNextEventToRenderer();
449 void TouchEventQueue::TryForwardNextEventToRenderer() {
450 DCHECK(!dispatching_touch_ack_);
451 // If there are queued touch events, then try to forward them to the renderer
452 // immediately, or ACK the events back to the client if appropriate.
453 while (!touch_queue_.empty()) {
454 PreFilterResult filter_result =
455 FilterBeforeForwarding(touch_queue_.front()->coalesced_event().event);
456 switch (filter_result) {
457 case ACK_WITH_NO_CONSUMER_EXISTS:
458 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
460 case ACK_WITH_NOT_CONSUMED:
461 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
463 case FORWARD_TO_RENDERER:
464 ForwardNextEventToRenderer();
470 void TouchEventQueue::ForwardNextEventToRenderer() {
471 TRACE_EVENT0("input", "TouchEventQueue::ForwardNextEventToRenderer");
474 DCHECK(!dispatching_touch_);
475 TouchEventWithLatencyInfo touch = touch_queue_.front()->coalesced_event();
477 if (send_touch_events_async_ &&
478 touch.event.type == WebInputEvent::TouchMove) {
479 // Throttling touchmove's in a continuous touchmove stream while scrolling
480 // reduces the risk of jank. However, it's still important that the web
481 // application be sent touches at key points in the gesture stream,
482 // e.g., when the application slop region is exceeded or touchmove
483 // coalescing fails because of different modifiers.
484 const bool send_touchmove_now =
486 (touch.event.timeStampSeconds >=
487 last_sent_touch_timestamp_sec_ + kAsyncTouchMoveIntervalSec) ||
488 (needs_async_touchmove_for_outer_slop_region_ &&
489 OutsideApplicationSlopRegion(touch.event,
490 touch_sequence_start_position_)) ||
491 (pending_async_touchmove_ &&
492 !pending_async_touchmove_->CanCoalesceWith(touch));
494 if (!send_touchmove_now) {
495 if (!pending_async_touchmove_) {
496 pending_async_touchmove_.reset(new TouchEventWithLatencyInfo(touch));
498 DCHECK(pending_async_touchmove_->CanCoalesceWith(touch));
499 pending_async_touchmove_->CoalesceWith(touch);
501 DCHECK_EQ(1U, size());
502 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
503 // It's possible (though unlikely) that ack'ing the current touch will
504 // trigger the queueing of another touch event (e.g., a touchcancel). As
505 // forwarding of the queued event will be deferred while the ack is being
506 // dispatched (see |OnTouchEvent()|), try forwarding it now.
507 TryForwardNextEventToRenderer();
512 last_sent_touch_timestamp_sec_ = touch.event.timeStampSeconds;
514 // Flush any pending async touch move. If it can be combined with the current
515 // (touchmove) event, great, otherwise send it immediately but separately. Its
516 // ack will trigger forwarding of the original |touch| event.
517 if (pending_async_touchmove_) {
518 if (pending_async_touchmove_->CanCoalesceWith(touch)) {
519 pending_async_touchmove_->CoalesceWith(touch);
520 pending_async_touchmove_->event.cancelable = !send_touch_events_async_;
521 touch = *pending_async_touchmove_;
522 pending_async_touchmove_.reset();
524 scoped_ptr<TouchEventWithLatencyInfo> async_move =
525 pending_async_touchmove_.Pass();
526 async_move->event.cancelable = false;
527 touch_queue_.push_front(new CoalescedWebTouchEvent(*async_move, true));
528 SendTouchEventImmediately(*async_move);
533 // Note: Marking touchstart events as not-cancelable prevents them from
534 // blocking subsequent gestures, but it may not be the best long term solution
535 // for tracking touch point dispatch.
536 if (send_touch_events_async_)
537 touch.event.cancelable = false;
539 // A synchronous ack will reset |dispatching_touch_|, in which case
540 // the touch timeout should not be started.
541 base::AutoReset<bool> dispatching_touch(&dispatching_touch_, true);
542 SendTouchEventImmediately(touch);
543 if (dispatching_touch_ && timeout_handler_)
544 timeout_handler_->StartIfNecessary(touch);
547 void TouchEventQueue::OnGestureScrollEvent(
548 const GestureEventWithLatencyInfo& gesture_event) {
549 if (gesture_event.event.type == blink::WebInputEvent::GestureScrollBegin) {
550 if (!touch_consumer_states_.is_empty() &&
551 !drop_remaining_touches_in_sequence_) {
552 DCHECK(!touchmove_slop_suppressor_->suppressing_touchmoves())
553 << "A touch handler should be offered a touchmove before scrolling.";
555 if (touch_scrolling_mode_ == TOUCH_SCROLLING_MODE_ASYNC_TOUCHMOVE &&
556 !drop_remaining_touches_in_sequence_ &&
557 touch_consumer_states_.is_empty()) {
558 // If no touch points have a consumer, prevent all subsequent touch events
559 // received during the scroll from reaching the renderer. This ensures
560 // that the first touchstart the renderer sees in any given sequence can
561 // always be preventDefault'ed (cancelable == true).
562 // TODO(jdduke): Revisit if touchstarts during scroll are made cancelable.
563 drop_remaining_touches_in_sequence_ = true;
566 if (touch_scrolling_mode_ == TOUCH_SCROLLING_MODE_ASYNC_TOUCHMOVE) {
567 needs_async_touchmove_for_outer_slop_region_ = true;
568 pending_async_touchmove_.reset();
574 if (gesture_event.event.type != blink::WebInputEvent::GestureScrollUpdate)
577 if (touch_scrolling_mode_ == TOUCH_SCROLLING_MODE_ASYNC_TOUCHMOVE)
578 send_touch_events_async_ = true;
580 if (touch_scrolling_mode_ != TOUCH_SCROLLING_MODE_TOUCHCANCEL)
583 // We assume that scroll events are generated synchronously from
584 // dispatching a touch event ack. This allows us to generate a synthetic
585 // cancel event that has the same touch ids as the touch event that
586 // is being acked. Otherwise, we don't perform the touch-cancel optimization.
587 if (!dispatching_touch_ack_)
590 if (drop_remaining_touches_in_sequence_)
593 drop_remaining_touches_in_sequence_ = true;
595 // Fake a TouchCancel to cancel the touch points of the touch event
596 // that is currently being acked.
597 // Note: |dispatching_touch_ack_| is non-null when we reach here, meaning we
598 // are in the scope of PopTouchEventToClient() and that no touch event
599 // in the queue is waiting for ack from renderer. So we can just insert
600 // the touch cancel at the beginning of the queue.
601 touch_queue_.push_front(new CoalescedWebTouchEvent(
602 ObtainCancelEventForTouchEvent(
603 dispatching_touch_ack_->coalesced_event()), true));
606 void TouchEventQueue::OnGestureEventAck(
607 const GestureEventWithLatencyInfo& event,
608 InputEventAckState ack_result) {
609 if (touch_scrolling_mode_ != TOUCH_SCROLLING_MODE_ASYNC_TOUCHMOVE)
612 if (event.event.type != blink::WebInputEvent::GestureScrollUpdate)
615 // Throttle sending touchmove events as long as the scroll events are handled.
616 // Note that there's no guarantee that this ACK is for the most recent
617 // gesture event (or even part of the current sequence). Worst case, the
618 // delay in updating the absorption state will result in minor UI glitches.
619 // A valid |pending_async_touchmove_| will be flushed when the next event is
621 send_touch_events_async_ = (ack_result == INPUT_EVENT_ACK_STATE_CONSUMED);
622 if (!send_touch_events_async_)
623 needs_async_touchmove_for_outer_slop_region_ = false;
626 void TouchEventQueue::OnHasTouchEventHandlers(bool has_handlers) {
627 DCHECK(!dispatching_touch_ack_);
628 DCHECK(!dispatching_touch_);
629 has_handlers_ = has_handlers;
632 bool TouchEventQueue::IsPendingAckTouchStart() const {
633 DCHECK(!dispatching_touch_ack_);
634 if (touch_queue_.empty())
637 const blink::WebTouchEvent& event =
638 touch_queue_.front()->coalesced_event().event;
639 return (event.type == WebInputEvent::TouchStart);
642 void TouchEventQueue::SetAckTimeoutEnabled(bool enabled) {
643 if (timeout_handler_)
644 timeout_handler_->SetEnabled(enabled);
647 bool TouchEventQueue::IsAckTimeoutEnabled() const {
648 return timeout_handler_ && timeout_handler_->enabled();
651 bool TouchEventQueue::HasPendingAsyncTouchMoveForTesting() const {
652 return pending_async_touchmove_;
655 bool TouchEventQueue::IsTimeoutRunningForTesting() const {
656 return timeout_handler_ && timeout_handler_->IsTimeoutTimerRunning();
659 const TouchEventWithLatencyInfo&
660 TouchEventQueue::GetLatestEventForTesting() const {
661 return touch_queue_.back()->coalesced_event();
664 void TouchEventQueue::FlushQueue() {
665 DCHECK(!dispatching_touch_ack_);
666 DCHECK(!dispatching_touch_);
667 pending_async_touchmove_.reset();
668 drop_remaining_touches_in_sequence_ = true;
669 while (!touch_queue_.empty())
670 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
673 void TouchEventQueue::PopTouchEventToClient(InputEventAckState ack_result) {
674 AckTouchEventToClient(ack_result, PopTouchEvent(), NULL);
677 void TouchEventQueue::PopTouchEventToClient(
678 InputEventAckState ack_result,
679 const LatencyInfo& renderer_latency_info) {
680 AckTouchEventToClient(ack_result, PopTouchEvent(), &renderer_latency_info);
683 void TouchEventQueue::AckTouchEventToClient(
684 InputEventAckState ack_result,
685 scoped_ptr<CoalescedWebTouchEvent> acked_event,
686 const ui::LatencyInfo* optional_latency_info) {
688 DCHECK(!dispatching_touch_ack_);
689 UpdateTouchConsumerStates(acked_event->coalesced_event().event, ack_result);
691 // Note that acking the touch-event may result in multiple gestures being sent
692 // to the renderer, or touch-events being queued.
693 base::AutoReset<const CoalescedWebTouchEvent*> dispatching_touch_ack(
694 &dispatching_touch_ack_, acked_event.get());
695 acked_event->DispatchAckToClient(ack_result, optional_latency_info, client_);
698 scoped_ptr<CoalescedWebTouchEvent> TouchEventQueue::PopTouchEvent() {
699 DCHECK(!touch_queue_.empty());
700 scoped_ptr<CoalescedWebTouchEvent> event(touch_queue_.front());
701 touch_queue_.pop_front();
705 void TouchEventQueue::SendTouchEventImmediately(
706 const TouchEventWithLatencyInfo& touch) {
707 if (needs_async_touchmove_for_outer_slop_region_) {
708 // Any event other than a touchmove (e.g., touchcancel or secondary
709 // touchstart) after a scroll has started will interrupt the need to send a
710 // an outer slop-region exceeding touchmove.
711 if (touch.event.type != WebInputEvent::TouchMove ||
712 OutsideApplicationSlopRegion(touch.event,
713 touch_sequence_start_position_))
714 needs_async_touchmove_for_outer_slop_region_ = false;
717 client_->SendTouchEventImmediately(touch);
720 TouchEventQueue::PreFilterResult
721 TouchEventQueue::FilterBeforeForwarding(const WebTouchEvent& event) {
722 if (timeout_handler_ && timeout_handler_->FilterEvent(event))
723 return ACK_WITH_NO_CONSUMER_EXISTS;
725 if (touchmove_slop_suppressor_->FilterEvent(event))
726 return ACK_WITH_NOT_CONSUMED;
728 if (WebTouchEventTraits::IsTouchSequenceStart(event)) {
729 touch_consumer_states_.clear();
730 send_touch_events_async_ = false;
731 pending_async_touchmove_.reset();
732 touch_sequence_start_position_ = gfx::PointF(event.touches[0].position);
733 drop_remaining_touches_in_sequence_ = false;
734 if (!has_handlers_) {
735 drop_remaining_touches_in_sequence_ = true;
736 return ACK_WITH_NO_CONSUMER_EXISTS;
740 if (drop_remaining_touches_in_sequence_ &&
741 event.type != WebInputEvent::TouchCancel) {
742 return ACK_WITH_NO_CONSUMER_EXISTS;
745 if (event.type == WebInputEvent::TouchStart)
746 return has_handlers_ ? FORWARD_TO_RENDERER : ACK_WITH_NO_CONSUMER_EXISTS;
748 for (unsigned int i = 0; i < event.touchesLength; ++i) {
749 const WebTouchPoint& point = event.touches[i];
750 // If a point has been stationary, then don't take it into account.
751 if (point.state == WebTouchPoint::StateStationary)
754 if (touch_consumer_states_.has_bit(point.id))
755 return FORWARD_TO_RENDERER;
758 return ACK_WITH_NO_CONSUMER_EXISTS;
761 void TouchEventQueue::UpdateTouchConsumerStates(const WebTouchEvent& event,
762 InputEventAckState ack_result) {
763 // Update the ACK status for each touch point in the ACKed event.
764 if (event.type == WebInputEvent::TouchEnd ||
765 event.type == WebInputEvent::TouchCancel) {
766 // The points have been released. Erase the ACK states.
767 for (unsigned i = 0; i < event.touchesLength; ++i) {
768 const WebTouchPoint& point = event.touches[i];
769 if (point.state == WebTouchPoint::StateReleased ||
770 point.state == WebTouchPoint::StateCancelled)
771 touch_consumer_states_.clear_bit(point.id);
773 } else if (event.type == WebInputEvent::TouchStart) {
774 for (unsigned i = 0; i < event.touchesLength; ++i) {
775 const WebTouchPoint& point = event.touches[i];
776 if (point.state == WebTouchPoint::StatePressed) {
777 if (ack_result != INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS)
778 touch_consumer_states_.mark_bit(point.id);
780 touch_consumer_states_.clear_bit(point.id);
786 } // namespace content