summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/input/touch_event_manager.h
blob: f55565f273cf27e93f1b8fc951e8fd765fd1a34f (plain)
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_INPUT_TOUCH_EVENT_MANAGER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_INPUT_TOUCH_EVENT_MANAGER_H_

#include "base/macros.h"
#include "third_party/blink/public/platform/web_coalesced_input_event.h"
#include "third_party/blink/public/platform/web_input_event_result.h"
#include "third_party/blink/public/platform/web_pointer_event.h"
#include "third_party/blink/public/platform/web_touch_event.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/events/pointer_event_factory.h"
#include "third_party/blink/renderer/core/input/event_handling_util.h"
#include "third_party/blink/renderer/platform/graphics/touch_action.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace blink {

class LocalFrame;
class Document;
class Touch;

// This class takes care of dispatching all touch events and
// maintaining related states.
class CORE_EXPORT TouchEventManager final
    : public GarbageCollected<TouchEventManager> {
 public:

  explicit TouchEventManager(LocalFrame&);
  void Trace(blink::Visitor*);

  void HandleTouchPoint(const WebPointerEvent&,
                        const Vector<WebPointerEvent>&,
                        const event_handling_util::PointerEventTarget&);

  WebInputEventResult FlushEvents();

  // Resets the internal state of this object.
  void Clear();

  // Returns whether there is any touch on the screen.
  bool IsAnyTouchActive() const;

 private:
  // Class represending one touch point event with its coalesced events and
  // related attributes.
  class TouchPointAttributes final
      : public GarbageCollected<TouchPointAttributes> {
   public:
    void Trace(blink::Visitor* visitor) { visitor->Trace(target_); }

    TouchPointAttributes() = default;
    explicit TouchPointAttributes(WebPointerEvent event)
        : event_(event), stale_(false) {}

    // Last state of the touch point.
    WebPointerEvent event_;
    // The list of coalesced events of the touch point represented by this class
    // if there is any. Note that at the end of each frame this list gets
    // cleared and the touch point |stale_| flag will be true for the next frame
    // unless more new events arrives for this touch point.
    Vector<WebPointerEvent> coalesced_events_;
    Member<Node> target_;  // The target of each active touch point.
    String region_;        //  // The region of each active touch point.
    bool stale_;
  };

  WebCoalescedInputEvent GenerateWebCoalescedInputEvent();
  Touch* CreateDomTouch(const TouchPointAttributes*, bool* known_target);
  void AllTouchesReleasedCleanup();

  // Keeps track of attributes of the touch point in the
  // |touch_points_attributes_| map and does the hit-testing if the original hit
  // test result was not inside capturing frame |touch_sequence_document_| for
  // touch events.
  void UpdateTouchAttributeMapsForPointerDown(
      const WebPointerEvent&,
      const event_handling_util::PointerEventTarget&);

  // This is triggered either by VSync signal to send one touch event per frame
  // accumulating all move events or by discrete events pointerdown/up/cancel.
  WebInputEventResult DispatchTouchEventFromAccumulatdTouchPoints();

  // Used only if |should_enforce_vertical_scroll_| is set.
  WebInputEventResult EnsureVerticalScrollIsPossible(WebInputEventResult);

  // NOTE: If adding a new field to this class please ensure that it is
  // cleared in |TouchEventManager::clear()|.

  const Member<LocalFrame> frame_;

  // The attributes of each active touch point indexed by the touch ID.
  using TouchAttributeMap =
      HeapHashMap<int,
                  Member<TouchPointAttributes>,
                  WTF::IntHash<int>,
                  WTF::UnsignedWithZeroKeyHashTraits<int>>;
  TouchAttributeMap touch_attribute_map_;

  // If set, the document of the active touch sequence. Unset if no touch
  // sequence active.
  Member<Document> touch_sequence_document_;

  bool suppressing_touchmoves_within_slop_;

  // This is used to created a consistent sequence of coalesced events compared
  // to the last frame.
  WebTouchEvent last_coalesced_touch_event_;

  // The current touch action, computed on each touch start and is
  // a union of all touches. Reset when all touches are released.
  TouchAction current_touch_action_;

  // TODO(ekaramad): Send the update after 'touchmove' to make sure we are only
  // enforcing vertical scroll.
  // When true, the current touch sequence should be handled such that vertical
  // scrolling is always possible. To this end, the output of event handlers for
  // 'touchstart' and 'touchmove' is overwritten to not handled so that
  // scrolling cannot be blocked. However, to ensure only vertical scrolling is
  // possible, the update for effective 'touch-action' is postponed to after
  // handling 'touchstart' handlers and potentially overwritten to 'pan-y' so
  // that only horizontal scrolling is blocked.
  bool should_enforce_vertical_scroll_ = false;
  // When set to a value, the effective touch-action is sent to the browser
  // after all 'touchstart' handlers have been invoked. This is used by feature
  // policy to enforce specific directions of scroll in spite of scroll-blocking
  // events being prevent defaulted. When multiple pointer down events occur
  // during the same touch sequence, the value of effective touch action which
  // is sent to the browser after handling each dispatched 'touchstart' is the
  // intersection of all the previously calculated effective touch action values
  // during the sequence.
  base::Optional<TouchAction> delayed_effective_touch_action_;

  DISALLOW_COPY_AND_ASSIGN(TouchEventManager);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_INPUT_TOUCH_EVENT_MANAGER_H_