File: touch_timeout_handler.cc

package info (click to toggle)
chromium 146.0.7680.153-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,057,156 kB
  • sloc: cpp: 36,426,539; ansic: 7,626,206; javascript: 3,599,825; python: 1,658,592; xml: 842,302; asm: 722,011; pascal: 186,153; sh: 88,976; perl: 88,684; objc: 79,984; sql: 60,492; cs: 42,470; fortran: 24,101; makefile: 21,141; tcl: 15,277; php: 14,022; yacc: 9,154; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (202 lines) | stat: -rw-r--r-- 6,468 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/input/touch_timeout_handler.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/task/sequenced_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/typed_macros.h"
#include "components/input/passthrough_touch_event_queue.h"
#include "third_party/perfetto/include/perfetto/tracing/track.h"
#include "ui/events/base_event_utils.h"
#include "ui/gfx/geometry/point_f.h"

using blink::WebInputEvent;
using blink::WebTouchEvent;
using blink::WebTouchPoint;
using ui::LatencyInfo;

namespace input {
namespace {

bool ShouldTouchTriggerTimeout(const WebTouchEvent& event) {
  return (event.GetType() == WebInputEvent::Type::kTouchStart ||
          event.GetType() == WebInputEvent::Type::kTouchMove) &&
         event.dispatch_type == WebInputEvent::DispatchType::kBlocking;
}

}  // namespace

TouchTimeoutHandler::TouchTimeoutHandler(
    PassthroughTouchEventQueue* touch_queue,
    base::TimeDelta desktop_timeout_delay,
    base::TimeDelta mobile_timeout_delay,
    scoped_refptr<base::SequencedTaskRunner> task_runner)
    : touch_queue_(touch_queue),
      desktop_timeout_delay_(desktop_timeout_delay),
      mobile_timeout_delay_(mobile_timeout_delay),
      use_mobile_timeout_(false),
      pending_ack_state_(PENDING_ACK_NONE),
      timeout_monitor_(base::BindRepeating(&TouchTimeoutHandler::OnTimeOut,
                                           base::Unretained(this)),
                       task_runner),
      enabled_(true),
      enabled_for_current_sequence_(false) {
  SetUseMobileTimeout(false);
}

TouchTimeoutHandler::~TouchTimeoutHandler() = default;

void TouchTimeoutHandler::StartIfNecessary(
    const TouchEventWithLatencyInfo& event) {
  if (pending_ack_state_ != PENDING_ACK_NONE)
    return;

  if (!enabled_)
    return;

  const base::TimeDelta timeout_delay = GetTimeoutDelay();
  if (timeout_delay.is_zero())
    return;

  if (!ShouldTouchTriggerTimeout(event.event))
    return;

  if (event.event.IsTouchSequenceStart()) {
    enabled_for_current_sequence_ = true;
  }

  if (!enabled_for_current_sequence_)
    return;

  timeout_event_ = event;
  timeout_monitor_.Restart(timeout_delay);
}

bool TouchTimeoutHandler::ConfirmTouchEvent(
    uint32_t unique_touch_event_id,
    blink::mojom::InputEventResultState ack_result,
    bool should_stop_timeout_monitor) {
  if (timeout_event_.event.unique_touch_event_id != unique_touch_event_id)
    return false;

  switch (pending_ack_state_) {
    case PENDING_ACK_NONE:
      if (ack_result == blink::mojom::InputEventResultState::kConsumed)
        enabled_for_current_sequence_ = false;
      if (should_stop_timeout_monitor)
        timeout_monitor_.Stop();
      return false;
    case PENDING_ACK_ORIGINAL_EVENT:
      if (AckedTimeoutEventRequiresCancel(ack_result)) {
        TRACE_EVENT_INSTANT("input", "PendingAckOriginalEvent-RequiresCancel");
        SetPendingAckState(PENDING_ACK_CANCEL_EVENT);
        touch_queue_->SendTouchCancelEventForTouchEvent(timeout_event_);
      } else {
        TRACE_EVENT_INSTANT("input", "PendingAckOriginalEvent");
        SetPendingAckState(PENDING_ACK_NONE);
        touch_queue_->UpdateTouchConsumerStates(timeout_event_.event,
                                                ack_result);
      }
      return true;
    case PENDING_ACK_CANCEL_EVENT:
      TRACE_EVENT_INSTANT("input", "PendingAckCancelEvent");
      SetPendingAckState(PENDING_ACK_NONE);
      return true;
  }
  return false;
}

bool TouchTimeoutHandler::FilterEvent(const WebTouchEvent& event) {
  if (!HasTimeoutEvent())
    return false;

  if (event.IsTouchSequenceStart()) {
    // If a new sequence is observed while we're still waiting on the
    // timed-out sequence response, also count the new sequence as timed-out.
  }

  return true;
}

void TouchTimeoutHandler::StopTimeoutMonitor() {
  timeout_monitor_.Stop();
}

void TouchTimeoutHandler::SetEnabled(bool enabled) {
  if (enabled_ == enabled)
    return;

  enabled_ = enabled;

  if (enabled_)
    return;

  enabled_for_current_sequence_ = false;
  // Only reset the |timeout_handler_| if the timer is running and has not
  // yet timed out. This ensures that an already timed out sequence is
  // properly flushed by the handler.
  if (IsTimeoutTimerRunning()) {
    pending_ack_state_ = PENDING_ACK_NONE;
    timeout_monitor_.Stop();
  }
}

void TouchTimeoutHandler::SetUseMobileTimeout(bool use_mobile_timeout) {
  use_mobile_timeout_ = use_mobile_timeout;
}

void TouchTimeoutHandler::OnTimeOut() {
  SetPendingAckState(PENDING_ACK_ORIGINAL_EVENT);
  touch_queue_->FlushQueue();
}

// Skip a cancel event if the timed-out event had no consumer and was the
// initial event in the gesture.
bool TouchTimeoutHandler::AckedTimeoutEventRequiresCancel(
    blink::mojom::InputEventResultState ack_result) const {
  DCHECK(HasTimeoutEvent());
  if (ack_result != blink::mojom::InputEventResultState::kNoConsumerExists)
    return true;
  return !timeout_event_.event.IsTouchSequenceStart();
}

void TouchTimeoutHandler::SetPendingAckState(
    PendingAckState new_pending_ack_state) {
  DCHECK_NE(pending_ack_state_, new_pending_ack_state);
  switch (new_pending_ack_state) {
    case PENDING_ACK_ORIGINAL_EVENT:
      DCHECK_EQ(pending_ack_state_, PENDING_ACK_NONE);
      TRACE_EVENT_BEGIN("input", "TouchEventTimeout",
                        perfetto::Track::FromPointer(this));
      break;
    case PENDING_ACK_CANCEL_EVENT:
      DCHECK_EQ(pending_ack_state_, PENDING_ACK_ORIGINAL_EVENT);
      DCHECK(!timeout_monitor_.IsRunning());
      DCHECK(touch_queue_->Empty());
      TRACE_EVENT_END("input", perfetto::Track::FromPointer(this));
      TRACE_EVENT_BEGIN("input", "CancelEvent",
                        perfetto::Track::FromPointer(this));
      break;
    case PENDING_ACK_NONE:
      DCHECK(!timeout_monitor_.IsRunning());
      DCHECK(touch_queue_->Empty());
      TRACE_EVENT_END("input", perfetto::Track::FromPointer(this));
      break;
  }
  pending_ack_state_ = new_pending_ack_state;
}

base::TimeDelta TouchTimeoutHandler::GetTimeoutDelay() const {
  return use_mobile_timeout_ ? mobile_timeout_delay_ : desktop_timeout_delay_;
}

bool TouchTimeoutHandler::HasTimeoutEvent() const {
  return pending_ack_state_ != PENDING_ACK_NONE;
}

}  // namespace input