File: passthrough_touch_event_queue.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (290 lines) | stat: -rw-r--r-- 11,691 bytes parent folder | download | duplicates (6)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// 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.

#ifndef COMPONENTS_INPUT_PASSTHROUGH_TOUCH_EVENT_QUEUE_H_
#define COMPONENTS_INPUT_PASSTHROUGH_TOUCH_EVENT_QUEUE_H_

#include <set>
#include <string>

#include "base/component_export.h"
#include "base/feature_list.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/field_trial_params.h"
#include "base/time/time.h"
#include "components/input/dispatch_to_renderer_callback.h"
#include "components/input/event_with_latency_info.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/input/input_event_result.mojom-shared.h"
#include "ui/events/blink/blink_features.h"

namespace content {
class InputRouterImplTestBase;
} // namespace content

namespace input {

class TouchTimeoutHandler;

// Interface with which PassthroughTouchEventQueue can forward touch events, and
// dispatch touch event responses.
class COMPONENT_EXPORT(INPUT) PassthroughTouchEventQueueClient {
 public:
  virtual ~PassthroughTouchEventQueueClient() = default;

  virtual void SendTouchEventImmediately(
      const TouchEventWithLatencyInfo& event,
      DispatchToRendererCallback& dispatch_callback) = 0;

  virtual void OnTouchEventAck(
      const TouchEventWithLatencyInfo& event,
      blink::mojom::InputEventResultSource ack_source,
      blink::mojom::InputEventResultState ack_result) = 0;

  virtual void OnFilteringTouchEvent(
      const blink::WebTouchEvent& touch_event) = 0;

  virtual void FlushDeferredGestureQueue() = 0;

  virtual DispatchToRendererCallback GetDispatchToRendererCallback() = 0;
};

// A queue that processes a touch-event and forwards it on to the
// renderer process immediately. This class assumes that queueing will
// happen inside the renderer process. This class will hold onto the pending
// events so that it can re-order the acks so that they appear in a
// logical order to the rest of the browser process. Due to the threaded
// model of the renderer it is possible that an ack for a touchend can
// be sent before the corresponding ack for the touchstart. This class
// corrects that state.
//
// This class also performs filtering over the sequence of touch-events to, for
// example, avoid sending events to the renderer that would have no effect. By
// default, we always forward touchstart and touchend but, if there are no
// handlers, touchmoves are filtered out of the sequence. The filtering logic
// is implemented in |FilterBeforeForwarding|.
class COMPONENT_EXPORT(INPUT) PassthroughTouchEventQueue {
 public:
  struct COMPONENT_EXPORT(INPUT) Config {
    Config();
    ~Config();
    Config(const Config& other);

    // Touch ack timeout delay for desktop sites. If zero, timeout behavior
    // is disabled for such sites. Defaults to 200ms.
    base::TimeDelta desktop_touch_ack_timeout_delay = base::Milliseconds(200);

    // Touch ack timeout delay for mobile sites. If zero, timeout behavior
    // is disabled for such sites. Defaults to 1000ms.
    base::TimeDelta mobile_touch_ack_timeout_delay = base::Milliseconds(1000);

    // Whether the platform supports touch ack timeout behavior.
    // Defaults to false (disabled).
    bool touch_ack_timeout_supported = false;

    // Whether we should allow events to bypass normal queue filter rules.
    bool skip_touch_filter =
        base::FeatureList::IsEnabled(blink::features::kSkipTouchEventFilter);

    // What events types are allowed to bypass the filter.
    std::string events_to_always_forward = kSkipTouchEventFilterType.Get();

    scoped_refptr<base::SequencedTaskRunner> task_runner;
  };

  PassthroughTouchEventQueue(PassthroughTouchEventQueueClient* client,
                             const Config& config);

  PassthroughTouchEventQueue(const PassthroughTouchEventQueue&) = delete;
  PassthroughTouchEventQueue& operator=(const PassthroughTouchEventQueue&) =
      delete;

  ~PassthroughTouchEventQueue();

  void QueueEvent(const TouchEventWithLatencyInfo& event,
                  DispatchToRendererCallback& dispatch_callback);

  void PrependTouchScrollNotification();

  void ProcessTouchAck(blink::mojom::InputEventResultSource ack_source,
                       blink::mojom::InputEventResultState ack_result,
                       const ui::LatencyInfo& latency_info,
                       const uint32_t unique_touch_event_id,
                       bool should_stop_timeout_monitor);

  void OnGestureEventAck(const GestureEventWithLatencyInfo& event,
                         blink::mojom::InputEventResultState ack_result);

  void OnHasTouchEventHandlers(bool has_handlers);

  bool IsPendingAckTouchStart() const;

  void SetAckTimeoutEnabled(bool enabled);

  void SetIsMobileOptimizedSite(bool mobile_optimized_site);

  bool IsAckTimeoutEnabled() const;

  bool Empty() const;

  void StopTimeoutMonitor();

  // Empties the queue of touch events. This may result in any number of gesture
  // events being sent to the renderer.
  void FlushQueue();

 protected:
  void SendTouchCancelEventForTouchEvent(
      const TouchEventWithLatencyInfo& event_to_cancel);
  void UpdateTouchConsumerStates(
      const blink::WebTouchEvent& event,
      blink::mojom::InputEventResultState ack_result);

 private:
  friend class content::InputRouterImplTestBase;
  friend class PassthroughTouchEventQueueTest;
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           TouchScrollStartedUnfiltered);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           TouchStartWithoutPageHandlersUnfiltered);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           TouchStartWithPageHandlersUnfiltered);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           TouchMoveFilteredAfterTimeout);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           TouchMoveWithoutPageHandlersUnfiltered);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           StationaryTouchMoveFiltered);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           StationaryTouchMoveWithActualTouchMoveUnfiltered);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           NonTouchMoveUnfiltered);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           TouchMoveWithNonTouchMoveUnfiltered);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           TouchMoveWithoutSequenceHandlerUnfiltered);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           TouchMoveWithoutPageHandlersUnfilteredWithSkipFlag);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           TouchStartUnfilteredWithForwardDiscrete);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           TouchMoveFilteredWithForwardDiscrete);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           TouchStartUnfilteredWithForwardAll);
  FRIEND_TEST_ALL_PREFIXES(PassthroughTouchEventQueueTest,
                           TouchMoveUnfilteredWithForwardAll);

  friend class TouchTimeoutHandler;

  class TouchEventWithLatencyInfoAndAckState
      : public TouchEventWithLatencyInfo {
   public:
    TouchEventWithLatencyInfoAndAckState(
        const TouchEventWithLatencyInfo&);
    blink::mojom::InputEventResultState ack_state() const { return ack_state_; }
    blink::mojom::InputEventResultSource ack_source() const {
      return ack_source_;
    }
    void set_ack_info(blink::mojom::InputEventResultSource source,
                      blink::mojom::InputEventResultState state) {
      ack_source_ = source;
      ack_state_ = state;
    }

   private:
    blink::mojom::InputEventResultSource ack_source_;
    blink::mojom::InputEventResultState ack_state_;
  };

  struct TouchEventWithLatencyInfoAndAckStateComparator {
    using is_transparent = void;
    bool operator()(const TouchEventWithLatencyInfoAndAckState& lhs,
                    const TouchEventWithLatencyInfoAndAckState& rhs) const {
      return lhs.event.unique_touch_event_id < rhs.event.unique_touch_event_id;
    }
    bool operator()(const TouchEventWithLatencyInfoAndAckState& lhs,
                    const uint32_t rhs) const {
      return lhs.event.unique_touch_event_id < rhs;
    }
    bool operator()(const uint32_t lhs,
                    const TouchEventWithLatencyInfoAndAckState& rhs) const {
      return lhs < rhs.event.unique_touch_event_id;
    }
  };

  enum class PreFilterResult {
    kUnfiltered = 0,
    kFilteredNoPageHandlers = 1,
    kFilteredTimeout = 2,
    kFilteredNoNonstationaryPointers = 3,
    kFilteredNoHandlerForSequence = 4,
    kMaxValue = kFilteredNoHandlerForSequence,
  };

  // Filter touches prior to forwarding to the renderer, e.g., if the renderer
  // has no touch handler.
  PreFilterResult FilterBeforeForwarding(const blink::WebTouchEvent& event);
  PreFilterResult FilterBeforeForwardingImpl(const blink::WebTouchEvent& event);
  bool ShouldFilterForEvent(const blink::WebTouchEvent& event);

  void AckTouchEventToClient(
      const TouchEventWithLatencyInfo& acked_event,
      blink::mojom::InputEventResultSource ack_source,
      blink::mojom::InputEventResultState ack_result);

  void SendTouchEventImmediately(TouchEventWithLatencyInfo* touch,
                                 bool wait_for_ack,
                                 DispatchToRendererCallback& dispatch_callback);

  void AckCompletedEvents();

  bool IsTimeoutRunningForTesting() const;
  size_t SizeForTesting() const;

  // Handles touch event forwarding and ack'ed event dispatch.
  raw_ptr<PassthroughTouchEventQueueClient> client_;

  // Whether the renderer has at least one consumer of touch events, e.g. a JS
  // event handler or hit-testable scrollbars
  bool has_handlers_;

  // Whether any pointer in the touch sequence may have having a consumer.
  bool maybe_has_handler_for_current_sequence_;

  // Whether to allow any remaining touches for the current sequence. Note that
  // this is a stricter condition than an empty |touch_consumer_states_|, as it
  // also prevents forwarding of touchstart events for new pointers in the
  // current sequence. This is only used when the event is synthetically
  // cancelled after a touch timeout or before a portal activation.
  bool drop_remaining_touches_in_sequence_;

  // Optional handler for timed-out touch event acks.
  std::unique_ptr<TouchTimeoutHandler> timeout_handler_;

  // Whether touch events should be sent as uncancelable or not.
  bool send_touch_events_async_;

  bool processing_acks_;

  // Event is saved to compare pointer positions for new touchmove events.
  std::unique_ptr<blink::WebTouchEvent> last_sent_touchevent_;

  // Stores outstanding touches that have been sent to the renderer but have
  // not yet been ack'd by the renderer. The set is explicitly ordered based
  // on the unique touch event id.
  std::set<TouchEventWithLatencyInfoAndAckState,
           TouchEventWithLatencyInfoAndAckStateComparator>
      outstanding_touches_;

  // Whether we should allow events to bypass normal queue filter rules.
  const bool skip_touch_filter_;
  // What events types are allowed to bypass the filter.
  const std::string events_to_always_forward_;
  static const base::FeatureParam<std::string> kSkipTouchEventFilterType;
};

}  // namespace input

#endif  // COMPONENTS_INPUT_PASSTHROUGH_TOUCH_EVENT_QUEUE_H_