OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #include "content/browser/renderer_host/input/touch_timeout_handler.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/auto_reset.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/trace_event/trace_event.h" |
| 14 #include "content/browser/renderer_host/input/touch_event_queue.h" |
| 15 #include "content/common/input/web_touch_event_traits.h" |
| 16 #include "ui/events/base_event_utils.h" |
| 17 #include "ui/gfx/geometry/point_f.h" |
| 18 |
| 19 using blink::WebInputEvent; |
| 20 using blink::WebTouchEvent; |
| 21 using blink::WebTouchPoint; |
| 22 using ui::LatencyInfo; |
| 23 |
| 24 namespace content { |
| 25 namespace { |
| 26 |
| 27 bool ShouldTouchTriggerTimeout(const WebTouchEvent& event) { |
| 28 return (event.type() == WebInputEvent::TouchStart || |
| 29 event.type() == WebInputEvent::TouchMove) && |
| 30 event.dispatchType == WebInputEvent::Blocking; |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 TouchTimeoutHandler::TouchTimeoutHandler(TouchEventQueue* touch_queue, |
| 36 base::TimeDelta desktop_timeout_delay, |
| 37 base::TimeDelta mobile_timeout_delay) |
| 38 : touch_queue_(touch_queue), |
| 39 desktop_timeout_delay_(desktop_timeout_delay), |
| 40 mobile_timeout_delay_(mobile_timeout_delay), |
| 41 use_mobile_timeout_(false), |
| 42 pending_ack_state_(PENDING_ACK_NONE), |
| 43 timeout_monitor_( |
| 44 base::Bind(&TouchTimeoutHandler::OnTimeOut, base::Unretained(this))), |
| 45 enabled_(true), |
| 46 enabled_for_current_sequence_(false), |
| 47 sequence_awaiting_uma_update_(false), |
| 48 sequence_using_mobile_timeout_(false) { |
| 49 SetUseMobileTimeout(false); |
| 50 } |
| 51 |
| 52 TouchTimeoutHandler::~TouchTimeoutHandler() { |
| 53 LogSequenceEndForUMAIfNecessary(false); |
| 54 } |
| 55 |
| 56 void TouchTimeoutHandler::StartIfNecessary( |
| 57 const TouchEventWithLatencyInfo& event) { |
| 58 if (pending_ack_state_ != PENDING_ACK_NONE) |
| 59 return; |
| 60 |
| 61 if (!enabled_) |
| 62 return; |
| 63 |
| 64 const base::TimeDelta timeout_delay = GetTimeoutDelay(); |
| 65 if (timeout_delay.is_zero()) |
| 66 return; |
| 67 |
| 68 if (!ShouldTouchTriggerTimeout(event.event)) |
| 69 return; |
| 70 |
| 71 if (WebTouchEventTraits::IsTouchSequenceStart(event.event)) { |
| 72 LogSequenceStartForUMA(); |
| 73 enabled_for_current_sequence_ = true; |
| 74 } |
| 75 |
| 76 if (!enabled_for_current_sequence_) |
| 77 return; |
| 78 |
| 79 timeout_event_ = event; |
| 80 timeout_monitor_.Restart(timeout_delay); |
| 81 } |
| 82 |
| 83 bool TouchTimeoutHandler::ConfirmTouchEvent(uint32_t unique_touch_event_id, |
| 84 InputEventAckState ack_result) { |
| 85 if (timeout_event_.event.uniqueTouchEventId != unique_touch_event_id) |
| 86 return false; |
| 87 |
| 88 switch (pending_ack_state_) { |
| 89 case PENDING_ACK_NONE: |
| 90 if (ack_result == INPUT_EVENT_ACK_STATE_CONSUMED) |
| 91 enabled_for_current_sequence_ = false; |
| 92 timeout_monitor_.Stop(); |
| 93 return false; |
| 94 case PENDING_ACK_ORIGINAL_EVENT: |
| 95 if (AckedTimeoutEventRequiresCancel(ack_result)) { |
| 96 SetPendingAckState(PENDING_ACK_CANCEL_EVENT); |
| 97 touch_queue_->SendTouchCancelEventForTouchEvent(timeout_event_); |
| 98 } else { |
| 99 SetPendingAckState(PENDING_ACK_NONE); |
| 100 touch_queue_->UpdateTouchConsumerStates(timeout_event_.event, |
| 101 ack_result); |
| 102 } |
| 103 return true; |
| 104 case PENDING_ACK_CANCEL_EVENT: |
| 105 SetPendingAckState(PENDING_ACK_NONE); |
| 106 return true; |
| 107 } |
| 108 return false; |
| 109 } |
| 110 |
| 111 bool TouchTimeoutHandler::FilterEvent(const WebTouchEvent& event) { |
| 112 if (!HasTimeoutEvent()) |
| 113 return false; |
| 114 |
| 115 if (WebTouchEventTraits::IsTouchSequenceStart(event)) { |
| 116 // If a new sequence is observed while we're still waiting on the |
| 117 // timed-out sequence response, also count the new sequence as timed-out. |
| 118 LogSequenceStartForUMA(); |
| 119 LogSequenceEndForUMAIfNecessary(true); |
| 120 } |
| 121 |
| 122 return true; |
| 123 } |
| 124 |
| 125 void TouchTimeoutHandler::SetEnabled(bool enabled) { |
| 126 if (enabled_ == enabled) |
| 127 return; |
| 128 |
| 129 enabled_ = enabled; |
| 130 |
| 131 if (enabled_) |
| 132 return; |
| 133 |
| 134 enabled_for_current_sequence_ = false; |
| 135 // Only reset the |timeout_handler_| if the timer is running and has not |
| 136 // yet timed out. This ensures that an already timed out sequence is |
| 137 // properly flushed by the handler. |
| 138 if (IsTimeoutTimerRunning()) { |
| 139 pending_ack_state_ = PENDING_ACK_NONE; |
| 140 timeout_monitor_.Stop(); |
| 141 } |
| 142 } |
| 143 |
| 144 void TouchTimeoutHandler::SetUseMobileTimeout(bool use_mobile_timeout) { |
| 145 use_mobile_timeout_ = use_mobile_timeout; |
| 146 } |
| 147 |
| 148 void TouchTimeoutHandler::OnTimeOut() { |
| 149 LogSequenceEndForUMAIfNecessary(true); |
| 150 SetPendingAckState(PENDING_ACK_ORIGINAL_EVENT); |
| 151 touch_queue_->FlushQueue(); |
| 152 } |
| 153 |
| 154 // Skip a cancel event if the timed-out event had no consumer and was the |
| 155 // initial event in the gesture. |
| 156 bool TouchTimeoutHandler::AckedTimeoutEventRequiresCancel( |
| 157 InputEventAckState ack_result) const { |
| 158 DCHECK(HasTimeoutEvent()); |
| 159 if (ack_result != INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS) |
| 160 return true; |
| 161 return !WebTouchEventTraits::IsTouchSequenceStart(timeout_event_.event); |
| 162 } |
| 163 |
| 164 void TouchTimeoutHandler::SetPendingAckState( |
| 165 PendingAckState new_pending_ack_state) { |
| 166 DCHECK_NE(pending_ack_state_, new_pending_ack_state); |
| 167 switch (new_pending_ack_state) { |
| 168 case PENDING_ACK_ORIGINAL_EVENT: |
| 169 DCHECK_EQ(pending_ack_state_, PENDING_ACK_NONE); |
| 170 TRACE_EVENT_ASYNC_BEGIN0("input", "TouchEventTimeout", this); |
| 171 break; |
| 172 case PENDING_ACK_CANCEL_EVENT: |
| 173 DCHECK_EQ(pending_ack_state_, PENDING_ACK_ORIGINAL_EVENT); |
| 174 DCHECK(!timeout_monitor_.IsRunning()); |
| 175 DCHECK(touch_queue_->Empty()); |
| 176 TRACE_EVENT_ASYNC_STEP_INTO0("input", "TouchEventTimeout", this, |
| 177 "CancelEvent"); |
| 178 break; |
| 179 case PENDING_ACK_NONE: |
| 180 DCHECK(!timeout_monitor_.IsRunning()); |
| 181 DCHECK(touch_queue_->Empty()); |
| 182 TRACE_EVENT_ASYNC_END0("input", "TouchEventTimeout", this); |
| 183 break; |
| 184 } |
| 185 pending_ack_state_ = new_pending_ack_state; |
| 186 } |
| 187 |
| 188 void TouchTimeoutHandler::LogSequenceStartForUMA() { |
| 189 // Always flush any unlogged entries before starting a new one. |
| 190 LogSequenceEndForUMAIfNecessary(false); |
| 191 sequence_awaiting_uma_update_ = true; |
| 192 sequence_using_mobile_timeout_ = use_mobile_timeout_; |
| 193 } |
| 194 |
| 195 void TouchTimeoutHandler::LogSequenceEndForUMAIfNecessary(bool timed_out) { |
| 196 if (!sequence_awaiting_uma_update_) |
| 197 return; |
| 198 |
| 199 sequence_awaiting_uma_update_ = false; |
| 200 |
| 201 if (sequence_using_mobile_timeout_) { |
| 202 UMA_HISTOGRAM_BOOLEAN("Event.Touch.TimedOutOnMobileSite", timed_out); |
| 203 } else { |
| 204 UMA_HISTOGRAM_BOOLEAN("Event.Touch.TimedOutOnDesktopSite", timed_out); |
| 205 } |
| 206 } |
| 207 |
| 208 base::TimeDelta TouchTimeoutHandler::GetTimeoutDelay() const { |
| 209 return use_mobile_timeout_ ? mobile_timeout_delay_ : desktop_timeout_delay_; |
| 210 } |
| 211 |
| 212 bool TouchTimeoutHandler::HasTimeoutEvent() const { |
| 213 return pending_ack_state_ != PENDING_ACK_NONE; |
| 214 } |
| 215 |
| 216 } // namespace content |
OLD | NEW |