Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(199)

Side by Side Diff: third_party/WebKit/Source/core/input/TouchEventManager.cpp

Issue 2860663006: Remove WebTouchEvent from TouchEventManager APIs (Closed)
Patch Set: Fix more tests Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/input/TouchEventManager.h" 5 #include "core/input/TouchEventManager.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include "core/dom/Document.h" 8 #include "core/dom/Document.h"
9 #include "core/events/TouchEvent.h" 9 #include "core/events/TouchEvent.h"
10 #include "core/frame/Deprecation.h" 10 #include "core/frame/Deprecation.h"
(...skipping 19 matching lines...) Expand all
30 return registry.HasEventHandlers( 30 return registry.HasEventHandlers(
31 EventHandlerRegistry::kTouchStartOrMoveEventBlocking) || 31 EventHandlerRegistry::kTouchStartOrMoveEventBlocking) ||
32 registry.HasEventHandlers( 32 registry.HasEventHandlers(
33 EventHandlerRegistry::kTouchStartOrMoveEventPassive) || 33 EventHandlerRegistry::kTouchStartOrMoveEventPassive) ||
34 registry.HasEventHandlers( 34 registry.HasEventHandlers(
35 EventHandlerRegistry::kTouchEndOrCancelEventBlocking) || 35 EventHandlerRegistry::kTouchEndOrCancelEventBlocking) ||
36 registry.HasEventHandlers( 36 registry.HasEventHandlers(
37 EventHandlerRegistry::kTouchEndOrCancelEventPassive); 37 EventHandlerRegistry::kTouchEndOrCancelEventPassive);
38 } 38 }
39 39
40 const AtomicString& TouchEventNameForTouchPointState( 40 const AtomicString& TouchEventNameForPointerAction(WebInputEvent::Type action) {
41 WebTouchPoint::State state) { 41 switch (action) {
42 switch (state) { 42 case WebInputEvent::kPointerUp:
43 case WebTouchPoint::kStateReleased:
44 return EventTypeNames::touchend; 43 return EventTypeNames::touchend;
45 case WebTouchPoint::kStateCancelled: 44 case WebInputEvent::kPointerCancel:
46 return EventTypeNames::touchcancel; 45 return EventTypeNames::touchcancel;
47 case WebTouchPoint::kStatePressed: 46 case WebInputEvent::kPointerDown:
48 return EventTypeNames::touchstart; 47 return EventTypeNames::touchstart;
49 case WebTouchPoint::kStateMoved: 48 case WebInputEvent::kPointerMove:
50 return EventTypeNames::touchmove; 49 return EventTypeNames::touchmove;
51 case WebTouchPoint::kStateStationary:
52 // Fall through to default
53 default: 50 default:
54 NOTREACHED(); 51 NOTREACHED();
55 return g_empty_atom; 52 return g_empty_atom;
56 } 53 }
57 } 54 }
58 55
59 enum TouchEventDispatchResultType { 56 enum TouchEventDispatchResultType {
60 kUnhandledTouches, // Unhandled touch events. 57 kUnhandledTouches, // Unhandled touch events.
61 kHandledTouches, // Handled touch events. 58 kHandledTouches, // Handled touch events.
62 kTouchEventDispatchResultTypeMax, 59 kTouchEventDispatchResultTypeMax,
63 }; 60 };
64 61
65 bool IsTouchSequenceStart(const WebTouchEvent& event) { 62 WebTouchPoint::State TouchPointStateFromPointerEventAction(
66 if (!event.touches_length) 63 WebInputEvent::Type type,
67 return false; 64 bool stale) {
68 if (event.GetType() != WebInputEvent::kTouchStart) 65 if (stale)
69 return false; 66 return WebTouchPoint::kStateStationary;
70 for (size_t i = 0; i < event.touches_length; ++i) { 67 switch (type) {
71 if (event.touches[i].state != blink::WebTouchPoint::kStatePressed) 68 case WebInputEvent::Type::kPointerUp:
72 return false; 69 return WebTouchPoint::kStateReleased;
70 case WebInputEvent::Type::kPointerCancel:
71 return WebTouchPoint::kStateCancelled;
72 case WebInputEvent::Type::kPointerDown:
73 return WebTouchPoint::kStatePressed;
74 case WebInputEvent::Type::kPointerMove:
75 return WebTouchPoint::kStateMoved;
76 default:
77 NOTREACHED();
78 return WebTouchPoint::kStateUndefined;
73 } 79 }
74 return true;
75 } 80 }
76 81
77 // Defining this class type local to dispatchTouchEvents() and annotating 82 // Defining this class type local to dispatchTouchEvents() and annotating
78 // it with STACK_ALLOCATED(), runs into MSVC(VS 2013)'s C4822 warning 83 // it with STACK_ALLOCATED(), runs into MSVC(VS 2013)'s C4822 warning
79 // that the local class doesn't provide a local definition for 'operator new'. 84 // that the local class doesn't provide a local definition for 'operator new'.
80 // Which it intentionally doesn't and shouldn't. 85 // Which it intentionally doesn't and shouldn't.
81 // 86 //
82 // Work around such toolchain bugginess by lifting out the type, thereby 87 // Work around such toolchain bugginess by lifting out the type, thereby
83 // taking it out of C4822's reach. 88 // taking it out of C4822's reach.
84 class ChangedTouches final { 89 class ChangedTouches final {
85 STACK_ALLOCATED(); 90 STACK_ALLOCATED();
86 91
87 public: 92 public:
88 // The touches corresponding to the particular change state this struct 93 // The touches corresponding to the particular change state this struct
89 // instance represents. 94 // instance represents.
90 Member<TouchList> touches_; 95 Member<TouchList> touches_;
91 96
92 using EventTargetSet = HeapHashSet<Member<EventTarget>>; 97 using EventTargetSet = HeapHashSet<Member<EventTarget>>;
93 // Set of targets involved in m_touches. 98 // Set of targets involved in m_touches.
94 EventTargetSet targets_; 99 EventTargetSet targets_;
95
96 WebPointerProperties::PointerType pointer_type_;
97 }; 100 };
98 101
99 } // namespace 102 } // namespace
100 103
101 TouchEventManager::TouchEventManager(LocalFrame& frame) : frame_(frame) { 104 TouchEventManager::TouchEventManager(LocalFrame& frame) : frame_(frame) {
102 Clear(); 105 Clear();
103 } 106 }
104 107
105 void TouchEventManager::Clear() { 108 void TouchEventManager::Clear() {
106 touch_sequence_document_.Clear(); 109 touch_sequence_document_.Clear();
107 target_for_touch_id_.clear(); 110 touch_points_attributes_.clear();
108 region_for_touch_id_.clear();
109 touch_pressed_ = false;
110 suppressing_touchmoves_within_slop_ = false; 111 suppressing_touchmoves_within_slop_ = false;
111 current_touch_action_ = TouchAction::kTouchActionAuto; 112 current_touch_action_ = TouchAction::kTouchActionAuto;
112 } 113 }
113 114
114 DEFINE_TRACE(TouchEventManager) { 115 DEFINE_TRACE(TouchEventManager) {
115 visitor->Trace(frame_); 116 visitor->Trace(frame_);
116 visitor->Trace(touch_sequence_document_); 117 visitor->Trace(touch_sequence_document_);
117 visitor->Trace(target_for_touch_id_); 118 visitor->Trace(touch_points_attributes_);
118 } 119 }
119 120
120 Touch* TouchEventManager::CreateDomTouch(const WebTouchPoint& point, 121 Touch* TouchEventManager::CreateDomTouch(
121 bool* known_target) { 122 const TouchEventManager::TouchPointAttributes* point_attr,
122 Node* touch_node = nullptr; 123 bool* known_target) {
123 String region_id; 124 Node* touch_node = point_attr->target_;
125 String region_id = point_attr->region_;
124 *known_target = false; 126 *known_target = false;
125 FloatPoint content_point; 127 FloatPoint content_point;
126 FloatSize adjusted_radius; 128 FloatSize adjusted_radius;
127 129
128 if (point.state == WebTouchPoint::kStateReleased ||
129 point.state == WebTouchPoint::kStateCancelled) {
130 // The target should be the original target for this touch, so get
131 // it from the hashmap. As it's a release or cancel we also remove
132 // it from the map.
133 touch_node = target_for_touch_id_.Take(point.id);
134 region_id = region_for_touch_id_.Take(point.id);
135 } else {
136 // No hittest is performed on move or stationary, since the target
137 // is not allowed to change anyway.
138 touch_node = target_for_touch_id_.at(point.id);
139 region_id = region_for_touch_id_.at(point.id);
140 }
141
142 LocalFrame* target_frame = nullptr; 130 LocalFrame* target_frame = nullptr;
143 if (touch_node) { 131 if (touch_node) {
144 Document& doc = touch_node->GetDocument(); 132 Document& doc = touch_node->GetDocument();
145 // If the target node has moved to a new document while it was being 133 // If the target node has moved to a new document while it was being
146 // touched, we can't send events to the new document because that could 134 // touched, we can't send events to the new document because that could
147 // leak nodes from one document to another. See http://crbug.com/394339. 135 // leak nodes from one document to another. See http://crbug.com/394339.
148 if (&doc == touch_sequence_document_.Get()) { 136 if (&doc == touch_sequence_document_.Get()) {
149 target_frame = doc.GetFrame(); 137 target_frame = doc.GetFrame();
150 *known_target = true; 138 *known_target = true;
151 } 139 }
(...skipping 10 matching lines...) Expand all
162 // Document so that there's some valid node here. Perhaps this 150 // Document so that there's some valid node here. Perhaps this
163 // should really be LocalDOMWindow, but in all other cases the target of 151 // should really be LocalDOMWindow, but in all other cases the target of
164 // a Touch is a Node so using the window could be a breaking change. 152 // a Touch is a Node so using the window could be a breaking change.
165 // Since we know there was no handler invoked, the specific target 153 // Since we know there was no handler invoked, the specific target
166 // should be completely irrelevant to the application. 154 // should be completely irrelevant to the application.
167 touch_node = touch_sequence_document_; 155 touch_node = touch_sequence_document_;
168 target_frame = touch_sequence_document_->GetFrame(); 156 target_frame = touch_sequence_document_->GetFrame();
169 } 157 }
170 DCHECK(target_frame); 158 DCHECK(target_frame);
171 159
160 WebPointerEvent transformed_event =
161 point_attr->event_.WebPointerEventInRootFrame();
172 // pagePoint should always be in the target element's document coordinates. 162 // pagePoint should always be in the target element's document coordinates.
173 FloatPoint page_point = 163 FloatPoint page_point = target_frame->View()->RootFrameToContents(
174 target_frame->View()->RootFrameToContents(point.PositionInWidget()); 164 transformed_event.PositionInWidget());
175 float scale_factor = 1.0f / target_frame->PageZoomFactor(); 165 float scale_factor = 1.0f / target_frame->PageZoomFactor();
176 166
177 content_point = page_point.ScaledBy(scale_factor); 167 content_point = page_point.ScaledBy(scale_factor);
178 adjusted_radius = 168 adjusted_radius = FloatSize(transformed_event.width, transformed_event.height)
mustaq 2017/06/08 20:02:52 Radius = (width/2, height/2);
Navid Zolghadr 2017/06/08 21:18:41 I can see what you mean. But this is not what we d
mustaq 2017/06/09 16:12:27 Yikes, perhaps the blame goes to me! Filed crbug.
179 FloatSize(point.radius_x, point.radius_y).ScaledBy(scale_factor); 169 .ScaledBy(scale_factor);
180 170
181 return Touch::Create(target_frame, touch_node, point.id, 171 return Touch::Create(target_frame, touch_node, point_attr->event_.id,
182 point.PositionInScreen(), content_point, adjusted_radius, 172 transformed_event.PositionInScreen(), content_point,
183 point.rotation_angle, point.force, region_id); 173 adjusted_radius, transformed_event.rotation_angle,
174 transformed_event.force, region_id);
184 } 175 }
185 176
186 WebInputEventResult TouchEventManager::DispatchTouchEvents( 177 WebCoalescedInputEvent TouchEventManager::GenerateWebCoalescedInputEvent() {
187 const WebTouchEvent& event, 178 DCHECK(!touch_points_attributes_.IsEmpty());
188 const Vector<WebTouchEvent>& coalesced_events, 179
189 bool all_touches_released) { 180 WebTouchEvent event;
181
182 const auto& first_touch_pointer_event =
183 touch_points_attributes_.begin()->value->event_;
184 event.dispatch_type = first_touch_pointer_event.dispatch_type;
185 event.touch_start_or_first_touch_move =
186 first_touch_pointer_event.touch_start_or_first_touch_move;
187 event.moved_beyond_slop_region =
188 first_touch_pointer_event.moved_beyond_slop_region;
189 event.dispatch_type = first_touch_pointer_event.dispatch_type;
190 event.dispatch_type = first_touch_pointer_event.dispatch_type;
191 event.SetFrameScale(first_touch_pointer_event.FrameScale());
192 event.SetFrameTranslate(first_touch_pointer_event.FrameTranslate());
193 event.SetTimeStampSeconds(first_touch_pointer_event.TimeStampSeconds());
194 event.SetModifiers(first_touch_pointer_event.GetModifiers());
195
196 WebInputEvent::Type touch_event_type = WebInputEvent::kTouchMove;
197 Vector<WebPointerEvent> all_coalesced_events;
198 Vector<int> available_ids;
199 for (const auto& id : touch_points_attributes_.Keys())
200 available_ids.push_back(id);
201 std::sort(available_ids.begin(), available_ids.end());
202 for (const int& touch_point_id : available_ids) {
203 const auto& touch_point_attribute =
204 touch_points_attributes_.at(touch_point_id);
205 const WebPointerEvent& touch_pointer_event = touch_point_attribute->event_;
206 WebTouchPoint web_touch_point(touch_pointer_event);
207 web_touch_point.state = TouchPointStateFromPointerEventAction(
208 touch_pointer_event.GetType(), touch_point_attribute->stale_);
209 web_touch_point.radius_x = touch_pointer_event.width;
210 web_touch_point.radius_y = touch_pointer_event.height;
211 web_touch_point.rotation_angle = touch_pointer_event.rotation_angle;
212 event.touches[event.touches_length++] = web_touch_point;
213
214 // Only change the touch event type from move. So if we have two pointers
215 // in up and down state we just set the touch event type to the first one
216 // we see.
217 if (touch_event_type == WebInputEvent::kTouchMove) {
218 if (touch_pointer_event.GetType() == WebInputEvent::kPointerDown)
219 touch_event_type = WebInputEvent::kTouchStart;
220 else if (touch_pointer_event.GetType() == WebInputEvent::kPointerCancel)
221 touch_event_type = WebInputEvent::kTouchCancel;
222 else if (touch_pointer_event.GetType() == WebInputEvent::kPointerUp)
223 touch_event_type = WebInputEvent::kTouchEnd;
224 }
225
226 event.SetType(touch_event_type);
227
228 for (const WebPointerEvent& coalesced_event :
229 touch_point_attribute->coalesced_events_)
230 all_coalesced_events.push_back(coalesced_event);
231 }
232
233 // Create all coalesced touch events based on pointerevents
234 struct {
235 bool operator()(const WebPointerEvent& a, const WebPointerEvent& b) {
236 return a.TimeStampSeconds() < b.TimeStampSeconds();
237 }
238 } timestamp_based_event_comparison;
239 std::sort(all_coalesced_events.begin(), all_coalesced_events.end(),
240 timestamp_based_event_comparison);
241 WebCoalescedInputEvent result(event);
242 // This assumes that we only get move events as coalesced events.
243 for (const auto& web_pointer_event : all_coalesced_events) {
244 for (unsigned i = 0; i < event.touches_length; ++i) {
245 if (event.touches[i].id == web_pointer_event.id &&
246 event.touches[i].state == blink::WebTouchPoint::kStateMoved &&
247 web_pointer_event.GetType() == WebInputEvent::kPointerMove) {
248 event.touches[i].movement_x = web_pointer_event.movement_x;
249 event.touches[i].movement_y = web_pointer_event.movement_y;
250 event.SetTimeStampSeconds(web_pointer_event.TimeStampSeconds());
251 result.AddCoalescedEvent(event);
252 break;
253 }
254 }
255 for (unsigned i = 0; i < event.touches_length; ++i) {
256 event.touches[i].state = blink::WebTouchPoint::kStateStationary;
257 event.touches[i].movement_x = 0;
258 event.touches[i].movement_y = 0;
259 }
260 }
261
262 return result;
263 }
264
265 WebInputEventResult TouchEventManager::DispatchTouchEvents() {
190 // Build up the lists to use for the |touches|, |targetTouches| and 266 // Build up the lists to use for the |touches|, |targetTouches| and
191 // |changedTouches| attributes in the JS event. See 267 // |changedTouches| attributes in the JS event. See
192 // http://www.w3.org/TR/touch-events/#touchevent-interface for how these 268 // http://www.w3.org/TR/touch-events/#touchevent-interface for how these
193 // lists fit together. 269 // lists fit together.
194 270
195 if (event.GetType() == WebInputEvent::kTouchEnd || 271 bool new_touch_point_since_last_raf = false;
196 event.GetType() == WebInputEvent::kTouchCancel || 272 bool any_touch_canceled_ended = false;
197 event.touches_length > 1) { 273 bool all_touch_points_pressed = true;
198 suppressing_touchmoves_within_slop_ = false; 274
275 for (const auto& attr : touch_points_attributes_.Values()) {
276 if (!attr->stale_)
277 new_touch_point_since_last_raf = true;
278 if (attr->event_.GetType() == WebInputEvent::kPointerUp ||
279 attr->event_.GetType() == WebInputEvent::kPointerCancel)
280 any_touch_canceled_ended = true;
281 if (attr->event_.GetType() != WebInputEvent::kPointerDown)
282 all_touch_points_pressed = false;
199 } 283 }
200 284
201 if (suppressing_touchmoves_within_slop_ && 285 if (!new_touch_point_since_last_raf)
202 event.GetType() == WebInputEvent::kTouchMove) { 286 return WebInputEventResult::kNotHandled;
203 if (!event.moved_beyond_slop_region) 287
204 return WebInputEventResult::kHandledSuppressed; 288 if (any_touch_canceled_ended || touch_points_attributes_.size() > 1)
205 suppressing_touchmoves_within_slop_ = false; 289 suppressing_touchmoves_within_slop_ = false;
290
291 if (suppressing_touchmoves_within_slop_) {
292 // There is exactly one touch point here otherwise
mustaq 2017/06/08 20:02:52 Please add a dcheck.
Navid Zolghadr 2017/06/08 21:18:40 Done.
293 // |suppressing_touchmoves_within_slop_| would have been false.
294 const auto& touch_point_attribute = touch_points_attributes_.begin()->value;
295 if (touch_point_attribute->event_.GetType() ==
296 WebInputEvent::kPointerMove) {
297 if (!touch_point_attribute->event_.moved_beyond_slop_region)
298 return WebInputEventResult::kHandledSuppressed;
299 suppressing_touchmoves_within_slop_ = false;
300 }
206 } 301 }
207 302
208 // Holds the complete set of touches on the screen. 303 // Holds the complete set of touches on the screen.
209 TouchList* touches = TouchList::Create(); 304 TouchList* touches = TouchList::Create();
210 305
211 // A different view on the 'touches' list above, filtered and grouped by 306 // A different view on the 'touches' list above, filtered and grouped by
212 // event target. Used for the |targetTouches| list in the JS event. 307 // event target. Used for the |targetTouches| list in the JS event.
213 using TargetTouchesHeapMap = HeapHashMap<EventTarget*, Member<TouchList>>; 308 using TargetTouchesHeapMap = HeapHashMap<EventTarget*, Member<TouchList>>;
214 TargetTouchesHeapMap touches_by_target; 309 TargetTouchesHeapMap touches_by_target;
215 310
216 // Array of touches per state, used to assemble the |changedTouches| list. 311 // Array of touches per state, used to assemble the |changedTouches| list.
217 ChangedTouches changed_touches[WebTouchPoint::kStateMax + 1]; 312 ChangedTouches changed_touches[WebInputEvent::kPointerActionLast -
313 WebInputEvent::kPointerActionFirst + 1];
218 314
219 for (unsigned touch_point_idx = 0; touch_point_idx < event.touches_length; 315 Vector<int> available_ids;
220 touch_point_idx++) { 316 for (const auto& id : touch_points_attributes_.Keys())
221 const WebTouchPoint& point = event.TouchPointInRootFrame(touch_point_idx); 317 available_ids.push_back(id);
222 WebTouchPoint::State point_state = point.state; 318 std::sort(available_ids.begin(), available_ids.end());
319 for (const int& touch_point_id : available_ids) {
320 const auto& touch_point_attribute =
321 touch_points_attributes_.at(touch_point_id);
322 WebInputEvent::Type pointer_action =
323 touch_point_attribute->event_.GetType();
324 size_t pointer_action_idx =
325 pointer_action - WebInputEvent::kPointerActionFirst;
223 bool known_target; 326 bool known_target;
224 327
225 Touch* touch = CreateDomTouch(point, &known_target); 328 Touch* touch = CreateDomTouch(touch_point_attribute, &known_target);
226 EventTarget* touch_target = touch->target(); 329 EventTarget* touch_target = touch->target();
227 330
228 // Ensure this target's touch list exists, even if it ends up empty, so 331 // Ensure this target's touch list exists, even if it ends up empty, so
229 // it can always be passed to TouchEvent::Create below. 332 // it can always be passed to TouchEvent::Create below.
230 TargetTouchesHeapMap::iterator target_touches_iterator = 333 TargetTouchesHeapMap::iterator target_touches_iterator =
231 touches_by_target.find(touch_target); 334 touches_by_target.find(touch_target);
232 if (target_touches_iterator == touches_by_target.end()) { 335 if (target_touches_iterator == touches_by_target.end()) {
233 touches_by_target.Set(touch_target, TouchList::Create()); 336 touches_by_target.Set(touch_target, TouchList::Create());
234 target_touches_iterator = touches_by_target.find(touch_target); 337 target_touches_iterator = touches_by_target.find(touch_target);
235 } 338 }
236 339
237 // |touches| and |targetTouches| should only contain information about 340 // |touches| and |targetTouches| should only contain information about
238 // touches still on the screen, so if this point is released or 341 // touches still on the screen, so if this point is released or
239 // cancelled it will only appear in the |changedTouches| list. 342 // cancelled it will only appear in the |changedTouches| list.
240 if (point_state != WebTouchPoint::kStateReleased && 343 if (pointer_action != WebInputEvent::kPointerUp &&
241 point_state != WebTouchPoint::kStateCancelled) { 344 pointer_action != WebInputEvent::kPointerCancel) {
242 touches->Append(touch); 345 touches->Append(touch);
243 target_touches_iterator->value->Append(touch); 346 target_touches_iterator->value->Append(touch);
244 } 347 }
245 348
246 // Now build up the correct list for |changedTouches|. 349 // Now build up the correct list for |changedTouches|.
247 // Note that any touches that are in the TouchStationary state (e.g. if 350 // Note that any touches that are in the TouchStationary state (e.g. if
248 // the user had several points touched but did not move them all) should 351 // the user had several points touched but did not move them all) should
249 // never be in the |changedTouches| list so we do not handle them 352 // never be in the |changedTouches| list so we do not handle them
250 // explicitly here. See https://bugs.webkit.org/show_bug.cgi?id=37609 353 // explicitly here. See https://bugs.webkit.org/show_bug.cgi?id=37609
251 // for further discussion about the TouchStationary state. 354 // for further discussion about the TouchStationary state.
252 if (point_state != WebTouchPoint::kStateStationary && known_target) { 355 if (!touch_point_attribute->stale_ && known_target) {
253 DCHECK_LE(point_state, WebTouchPoint::kStateMax); 356 if (!changed_touches[pointer_action_idx].touches_)
254 if (!changed_touches[point_state].touches_) 357 changed_touches[pointer_action_idx].touches_ = TouchList::Create();
255 changed_touches[point_state].touches_ = TouchList::Create(); 358 changed_touches[pointer_action_idx].touches_->Append(touch);
256 changed_touches[point_state].touches_->Append(touch); 359 changed_touches[pointer_action_idx].targets_.insert(touch_target);
257 changed_touches[point_state].targets_.insert(touch_target);
258 changed_touches[point_state].pointer_type_ = point.pointer_type;
259 } 360 }
260 } 361 }
261 362
262 if (all_touches_released) { 363 WebInputEventResult event_result = WebInputEventResult::kNotHandled;
263 touch_sequence_document_.Clear();
264 current_touch_action_ = TouchAction::kTouchActionAuto;
265 }
266 364
267 WebInputEventResult event_result = WebInputEventResult::kNotHandled;
268 // First we construct the webcoalescedinputevent contains all the coalesced 365 // First we construct the webcoalescedinputevent contains all the coalesced
269 // touch event. 366 // touch event.
270 std::vector<const WebInputEvent*> coalesced_touches; 367 WebCoalescedInputEvent coalesced_event = GenerateWebCoalescedInputEvent();
271 for (size_t i = 0; i < coalesced_events.size(); ++i) {
272 coalesced_touches.push_back(&coalesced_events[i]);
273 }
274 WebCoalescedInputEvent coalesced_event(event, coalesced_touches);
275 368
276 // Now iterate through the |changedTouches| list and |m_targets| within it, 369 // Now iterate through the |changedTouches| list and |m_targets| within it,
277 // sending TouchEvents to the targets as required. 370 // sending TouchEvents to the targets as required.
278 for (unsigned state = 0; state <= WebTouchPoint::kStateMax; ++state) { 371 for (unsigned action = WebInputEvent::kPointerActionFirst;
279 if (!changed_touches[state].touches_) 372 action <= WebInputEvent::kPointerActionLast; ++action) {
373 size_t action_idx = action - WebInputEvent::kPointerActionFirst;
374 if (!changed_touches[action_idx].touches_)
280 continue; 375 continue;
281 376
282 const AtomicString& event_name(TouchEventNameForTouchPointState( 377 const AtomicString& event_name(TouchEventNameForPointerAction(
283 static_cast<WebTouchPoint::State>(state))); 378 static_cast<WebInputEvent::Type>(action)));
284 for (const auto& event_target : changed_touches[state].targets_) { 379
380 for (const auto& event_target : changed_touches[action_idx].targets_) {
285 EventTarget* touch_event_target = event_target; 381 EventTarget* touch_event_target = event_target;
286 TouchEvent* touch_event = TouchEvent::Create( 382 TouchEvent* touch_event = TouchEvent::Create(
287 coalesced_event, touches, touches_by_target.at(touch_event_target), 383 coalesced_event, touches, touches_by_target.at(touch_event_target),
288 changed_touches[state].touches_.Get(), event_name, 384 changed_touches[action_idx].touches_.Get(), event_name,
289 touch_event_target->ToNode()->GetDocument().domWindow(), 385 touch_event_target->ToNode()->GetDocument().domWindow(),
290 current_touch_action_); 386 current_touch_action_);
291 387
292 DispatchEventResult dom_dispatch_result = 388 DispatchEventResult dom_dispatch_result =
293 touch_event_target->DispatchEvent(touch_event); 389 touch_event_target->DispatchEvent(touch_event);
294 390
295 // Only report for top level documents with a single touch on 391 // Only report for top level documents with a single touch on
296 // touch-start or the first touch-move. 392 // touch-start or the first touch-move.
297 if (event.touch_start_or_first_touch_move && event.touches_length == 1 && 393 if (touch_points_attributes_.size() == 1 && frame_->IsMainFrame()) {
298 frame_->IsMainFrame()) { 394 const auto& event = touch_points_attributes_.begin()->value->event_;
299 // Record the disposition and latency of touch starts and first touch 395 if (event.touch_start_or_first_touch_move) {
300 // moves before and after the page is fully loaded respectively. 396 // Record the disposition and latency of touch starts and first touch
301 int64_t latency_in_micros = 397 // moves before and after the page is fully loaded respectively.
302 (TimeTicks::Now() - 398 int64_t latency_in_micros =
303 TimeTicks::FromSeconds(event.TimeStampSeconds())) 399 (TimeTicks::Now() -
304 .InMicroseconds(); 400 TimeTicks::FromSeconds(event.TimeStampSeconds()))
305 if (event.IsCancelable()) { 401 .InMicroseconds();
306 if (frame_->GetDocument()->IsLoadCompleted()) { 402 if (event.IsCancelable()) {
403 if (frame_->GetDocument()->IsLoadCompleted()) {
404 DEFINE_STATIC_LOCAL(EnumerationHistogram,
405 touch_dispositions_after_page_load_histogram,
406 ("Event.Touch.TouchDispositionsAfterPageLoad",
407 kTouchEventDispatchResultTypeMax));
408 touch_dispositions_after_page_load_histogram.Count(
409 (dom_dispatch_result != DispatchEventResult::kNotCanceled)
410 ? kHandledTouches
411 : kUnhandledTouches);
412
413 DEFINE_STATIC_LOCAL(
414 CustomCountHistogram, event_latency_after_page_load_histogram,
415 ("Event.Touch.TouchLatencyAfterPageLoad", 1, 100000000, 50));
416 event_latency_after_page_load_histogram.Count(latency_in_micros);
417 } else {
418 DEFINE_STATIC_LOCAL(
419 EnumerationHistogram,
420 touch_dispositions_before_page_load_histogram,
421 ("Event.Touch.TouchDispositionsBeforePageLoad",
422 kTouchEventDispatchResultTypeMax));
423 touch_dispositions_before_page_load_histogram.Count(
424 (dom_dispatch_result != DispatchEventResult::kNotCanceled)
425 ? kHandledTouches
426 : kUnhandledTouches);
427
428 DEFINE_STATIC_LOCAL(
429 CustomCountHistogram,
430 event_latency_before_page_load_histogram,
431 ("Event.Touch.TouchLatencyBeforePageLoad", 1, 100000000, 50));
432 event_latency_before_page_load_histogram.Count(latency_in_micros);
433 }
434 // Report the touch disposition there is no active fling animation.
307 DEFINE_STATIC_LOCAL(EnumerationHistogram, 435 DEFINE_STATIC_LOCAL(EnumerationHistogram,
308 touch_dispositions_after_page_load_histogram, 436 touch_dispositions_outside_fling_histogram,
309 ("Event.Touch.TouchDispositionsAfterPageLoad", 437 ("Event.Touch.TouchDispositionsOutsideFling2",
310 kTouchEventDispatchResultTypeMax)); 438 kTouchEventDispatchResultTypeMax));
311 touch_dispositions_after_page_load_histogram.Count( 439 touch_dispositions_outside_fling_histogram.Count(
312 (dom_dispatch_result != DispatchEventResult::kNotCanceled) 440 (dom_dispatch_result != DispatchEventResult::kNotCanceled)
313 ? kHandledTouches 441 ? kHandledTouches
314 : kUnhandledTouches); 442 : kUnhandledTouches);
443 }
315 444
316 DEFINE_STATIC_LOCAL( 445 // Report the touch disposition when there is an active fling
317 CustomCountHistogram, event_latency_after_page_load_histogram, 446 // animation.
318 ("Event.Touch.TouchLatencyAfterPageLoad", 1, 100000000, 50)); 447 // if (event[0].first.dispatch_type ==
319 event_latency_after_page_load_histogram.Count(latency_in_micros); 448 if (event.dispatch_type ==
320 } else { 449 WebInputEvent::kListenersForcedNonBlockingDueToFling) {
321 DEFINE_STATIC_LOCAL(EnumerationHistogram, 450 DEFINE_STATIC_LOCAL(EnumerationHistogram,
322 touch_dispositions_before_page_load_histogram, 451 touch_dispositions_during_fling_histogram,
323 ("Event.Touch.TouchDispositionsBeforePageLoad", 452 ("Event.Touch.TouchDispositionsDuringFling2",
324 kTouchEventDispatchResultTypeMax)); 453 kTouchEventDispatchResultTypeMax));
325 touch_dispositions_before_page_load_histogram.Count( 454 touch_dispositions_during_fling_histogram.Count(
326 (dom_dispatch_result != DispatchEventResult::kNotCanceled) 455 touch_event->PreventDefaultCalledOnUncancelableEvent()
327 ? kHandledTouches 456 ? kHandledTouches
328 : kUnhandledTouches); 457 : kUnhandledTouches);
329
330 DEFINE_STATIC_LOCAL(
331 CustomCountHistogram, event_latency_before_page_load_histogram,
332 ("Event.Touch.TouchLatencyBeforePageLoad", 1, 100000000, 50));
333 event_latency_before_page_load_histogram.Count(latency_in_micros);
334 } 458 }
335 // Report the touch disposition there is no active fling animation.
336 DEFINE_STATIC_LOCAL(EnumerationHistogram,
337 touch_dispositions_outside_fling_histogram,
338 ("Event.Touch.TouchDispositionsOutsideFling2",
339 kTouchEventDispatchResultTypeMax));
340 touch_dispositions_outside_fling_histogram.Count(
341 (dom_dispatch_result != DispatchEventResult::kNotCanceled)
342 ? kHandledTouches
343 : kUnhandledTouches);
344 }
345
346 // Report the touch disposition when there is an active fling animation.
347 if (event.dispatch_type ==
348 WebInputEvent::kListenersForcedNonBlockingDueToFling) {
349 DEFINE_STATIC_LOCAL(EnumerationHistogram,
350 touch_dispositions_during_fling_histogram,
351 ("Event.Touch.TouchDispositionsDuringFling2",
352 kTouchEventDispatchResultTypeMax));
353 touch_dispositions_during_fling_histogram.Count(
354 touch_event->PreventDefaultCalledOnUncancelableEvent()
355 ? kHandledTouches
356 : kUnhandledTouches);
357 } 459 }
358 } 460 }
359 event_result = EventHandlingUtil::MergeEventResult( 461 event_result = EventHandlingUtil::MergeEventResult(
360 event_result, 462 event_result,
361 EventHandlingUtil::ToWebInputEventResult(dom_dispatch_result)); 463 EventHandlingUtil::ToWebInputEventResult(dom_dispatch_result));
362 } 464 }
363 } 465 }
364 466
365 // Do not suppress any touchmoves if the touchstart is consumed. 467 // Suppress following touchmoves within the slop region if the touchstart is
366 if (IsTouchSequenceStart(event) && 468 // not consumed.
469 if (all_touch_points_pressed &&
367 event_result == WebInputEventResult::kNotHandled) { 470 event_result == WebInputEventResult::kNotHandled) {
368 suppressing_touchmoves_within_slop_ = true; 471 suppressing_touchmoves_within_slop_ = true;
369 } 472 }
370 473
371 return event_result; 474 return event_result;
372 } 475 }
373 476
374 void TouchEventManager::UpdateTargetAndRegionMapsForTouchStart( 477 void TouchEventManager::UpdateTouchAttributeMapsForPointerDown(
375 const WebTouchPoint& touch_point, 478 const WebPointerEvent& event,
376 const EventHandlingUtil::PointerEventTarget& pointer_event_target) { 479 const EventHandlingUtil::PointerEventTarget& pointer_event_target) {
377 // Touch events implicitly capture to the touched node, and don't change 480 // Touch events implicitly capture to the touched node, and don't change
378 // active/hover states themselves (Gesture events do). So we only need 481 // active/hover states themselves (Gesture events do). So we only need
379 // to hit-test on touchstart and when the target could be different than 482 // to hit-test on touchstart and when the target could be different than
380 // the corresponding pointer event target. 483 // the corresponding pointer event target.
381 DCHECK(touch_point.state == WebTouchPoint::kStatePressed); 484 DCHECK(event.GetType() == WebInputEvent::kPointerDown);
485 // Ideally we'd DCHECK(!touch_points_attributes_.Contains(event.id))
486 // since we shouldn't get a touchstart for a touch that's already
487 // down. However EventSender allows this to be violated and there's
488 // some tests that take advantage of it. There may also be edge
489 // cases in the browser where this happens.
490 // See http://crbug.com/345372.
491 touch_points_attributes_.Set(event.id, new TouchPointAttributes(event));
492
382 Node* touch_node = pointer_event_target.target_node; 493 Node* touch_node = pointer_event_target.target_node;
383 String region = pointer_event_target.region; 494 String region = pointer_event_target.region;
384 495
385 HitTestRequest::HitTestRequestType hit_type = HitTestRequest::kTouchEvent | 496 HitTestRequest::HitTestRequestType hit_type = HitTestRequest::kTouchEvent |
386 HitTestRequest::kReadOnly | 497 HitTestRequest::kReadOnly |
387 HitTestRequest::kActive; 498 HitTestRequest::kActive;
388 HitTestResult result; 499 HitTestResult result;
389 // For the touchPressed points hit-testing is done in 500 // For the touchPressed points hit-testing is done in
390 // PointerEventManager. If it was the second touch there is a 501 // PointerEventManager. If it was the second touch there is a
391 // capturing documents for the touch and |m_touchSequenceDocument| 502 // capturing documents for the touch and |m_touchSequenceDocument|
392 // is not null. So if PointerEventManager should hit-test again 503 // is not null. So if PointerEventManager should hit-test again
393 // against |m_touchSequenceDocument| if the target set by 504 // against |m_touchSequenceDocument| if the target set by
394 // PointerEventManager was either null or not in 505 // PointerEventManager was either null or not in
395 // |m_touchSequenceDocument|. 506 // |m_touchSequenceDocument|.
396 if (touch_sequence_document_ && 507 if (touch_sequence_document_ &&
397 (!touch_node || &touch_node->GetDocument() != touch_sequence_document_)) { 508 (!touch_node || &touch_node->GetDocument() != touch_sequence_document_)) {
398 if (touch_sequence_document_->GetFrame()) { 509 if (touch_sequence_document_->GetFrame()) {
399 LayoutPoint frame_point = LayoutPoint( 510 LayoutPoint frame_point = LayoutPoint(
400 touch_sequence_document_->GetFrame()->View()->RootFrameToContents( 511 touch_sequence_document_->GetFrame()->View()->RootFrameToContents(
401 touch_point.PositionInWidget())); 512 event.PositionInWidget()));
402 result = EventHandlingUtil::HitTestResultInFrame( 513 result = EventHandlingUtil::HitTestResultInFrame(
403 touch_sequence_document_->GetFrame(), frame_point, hit_type); 514 touch_sequence_document_->GetFrame(), frame_point, hit_type);
404 Node* node = result.InnerNode(); 515 Node* node = result.InnerNode();
405 if (!node) 516 if (!node)
406 return; 517 return;
407 if (isHTMLCanvasElement(node)) { 518 if (isHTMLCanvasElement(node)) {
408 HitTestCanvasResult* hit_test_canvas_result = 519 HitTestCanvasResult* hit_test_canvas_result =
409 toHTMLCanvasElement(node)->GetControlAndIdIfHitRegionExists( 520 toHTMLCanvasElement(node)->GetControlAndIdIfHitRegionExists(
410 result.PointInInnerNodeFrame()); 521 result.PointInInnerNodeFrame());
411 if (hit_test_canvas_result->GetControl()) 522 if (hit_test_canvas_result->GetControl())
(...skipping 11 matching lines...) Expand all
423 if (!touch_node) 534 if (!touch_node)
424 return; 535 return;
425 if (!touch_sequence_document_) { 536 if (!touch_sequence_document_) {
426 // Keep track of which document should receive all touch events 537 // Keep track of which document should receive all touch events
427 // in the active sequence. This must be a single document to 538 // in the active sequence. This must be a single document to
428 // ensure we don't leak Nodes between documents. 539 // ensure we don't leak Nodes between documents.
429 touch_sequence_document_ = &(touch_node->GetDocument()); 540 touch_sequence_document_ = &(touch_node->GetDocument());
430 DCHECK(touch_sequence_document_->GetFrame()->View()); 541 DCHECK(touch_sequence_document_->GetFrame()->View());
431 } 542 }
432 543
433 // Ideally we'd DCHECK(!m_targetForTouchID.contains(point.id()) 544 TouchPointAttributes* attributes = touch_points_attributes_.at(event.id);
434 // since we shouldn't get a touchstart for a touch that's already 545 attributes->target_ = touch_node;
435 // down. However EventSender allows this to be violated and there's 546 attributes->region_ = region;
436 // some tests that take advantage of it. There may also be edge
437 // cases in the browser where this happens.
438 // See http://crbug.com/345372.
439 target_for_touch_id_.Set(touch_point.id, touch_node);
440
441 region_for_touch_id_.Set(touch_point.id, region);
442 547
443 TouchAction effective_touch_action = 548 TouchAction effective_touch_action =
444 TouchActionUtil::ComputeEffectiveTouchAction(*touch_node); 549 TouchActionUtil::ComputeEffectiveTouchAction(*touch_node);
445 if (effective_touch_action != TouchAction::kTouchActionAuto) { 550 if (effective_touch_action != TouchAction::kTouchActionAuto) {
446 frame_->GetPage()->GetChromeClient().SetTouchAction(frame_, 551 frame_->GetPage()->GetChromeClient().SetTouchAction(frame_,
447 effective_touch_action); 552 effective_touch_action);
448 553
449 // Combine the current touch action sequence with the touch action 554 // Combine the current touch action sequence with the touch action
450 // for the current finger press. 555 // for the current finger press.
451 current_touch_action_ &= effective_touch_action; 556 current_touch_action_ &= effective_touch_action;
452 } 557 }
453 } 558 }
454 559
455 bool TouchEventManager::HitTestTouchPointsIfNeeded( 560 void TouchEventManager::HandleTouchPoint(
456 const WebTouchEvent& event, 561 const WebPointerEvent& event,
457 const HeapVector<EventHandlingUtil::PointerEventTarget>& 562 const Vector<WebPointerEvent>& coalesced_events,
458 pointer_event_targets) { 563 const EventHandlingUtil::PointerEventTarget& pointer_event_target) {
459 bool new_touch_sequence = true; 564 DCHECK_GE(event.GetType(), WebInputEvent::kPointerActionFirst);
460 bool all_touches_released = true; 565 DCHECK_LE(event.GetType(), WebInputEvent::kPointerActionLast);
461 566
462 for (unsigned i = 0; i < event.touches_length; ++i) { 567 if (touch_points_attributes_.IsEmpty()) {
463 WebTouchPoint::State state = event.touches[i].state;
464 if (state != WebTouchPoint::kStatePressed)
465 new_touch_sequence = false;
466 if (state != WebTouchPoint::kStateReleased &&
467 state != WebTouchPoint::kStateCancelled)
468 all_touches_released = false;
469 }
470 if (new_touch_sequence) {
471 // Ideally we'd DCHECK(!m_touchSequenceDocument) here since we should 568 // Ideally we'd DCHECK(!m_touchSequenceDocument) here since we should
472 // have cleared the active document when we saw the last release. But we 569 // have cleared the active document when we saw the last release. But we
473 // have some tests that violate this, ClusterFuzz could trigger it, and 570 // have some tests that violate this, ClusterFuzz could trigger it, and
474 // there may be cases where the browser doesn't reliably release all 571 // there may be cases where the browser doesn't reliably release all
475 // touches. http://crbug.com/345372 tracks this. 572 // touches. http://crbug.com/345372 tracks this.
476 touch_sequence_document_.Clear(); 573 allTouchesReleasedCleanup();
477 } 574 }
478 575
479 DCHECK(frame_->View()); 576 DCHECK(frame_->View());
480 if (touch_sequence_document_ && 577 if (touch_sequence_document_ &&
481 (!touch_sequence_document_->GetFrame() || 578 (!touch_sequence_document_->GetFrame() ||
482 !touch_sequence_document_->GetFrame()->View())) { 579 !touch_sequence_document_->GetFrame()->View())) {
483 // If the active touch document has no frame or view, it's probably being 580 // If the active touch document has no frame or view, it's probably being
484 // destroyed so we can't dispatch events. 581 // destroyed so we can't dispatch events.
485 return false; 582 return;
486 } 583 }
487 584
488 for (unsigned i = 0; i < event.touches_length; ++i) { 585 // In touch event model only touch starts can set the target and after that
489 // In touch event model only touch starts can set the target and after that 586 // the touch event always goes to that target.
490 // the touch event always goes to that target. 587 if (event.GetType() == WebInputEvent::kPointerDown) {
491 if (event.touches[i].state == WebTouchPoint::kStatePressed) { 588 UpdateTouchAttributeMapsForPointerDown(event, pointer_event_target);
492 UpdateTargetAndRegionMapsForTouchStart(event.TouchPointInRootFrame(i), 589 }
493 pointer_event_targets[i]); 590
591 // We might not receive the down action for a touch point. In that case we
592 // would have never added them to |touch_points_attributes_| or hit-tested
593 // them. For those just keep them in the map with a null target. Later they
594 // will be targeted at the |touch_sequence_document_|.
595 if (!touch_points_attributes_.Contains(event.id)) {
596 touch_points_attributes_.insert(event.id, new TouchPointAttributes(event));
597 }
598
599 TouchPointAttributes* attributes = touch_points_attributes_.at(event.id);
600 attributes->event_ = event;
601 attributes->coalesced_events_ = coalesced_events;
602 attributes->stale_ = false;
603 }
604
605 WebInputEventResult TouchEventManager::HandleVSyncSignal() {
606 // If there's no document receiving touch events, or no handlers on the
607 // document set to receive the events, then we can skip all the rest of
608 // sending the event.
609 WebInputEventResult result = WebInputEventResult::kNotHandled;
610 if (touch_sequence_document_ && touch_sequence_document_->GetPage() &&
611 HasTouchHandlers(
612 touch_sequence_document_->GetPage()->GetEventHandlerRegistry()) &&
613 touch_sequence_document_->GetFrame() &&
614 touch_sequence_document_->GetFrame()->View()) {
615 result = DispatchTouchEvents();
616 }
617
618 // Cleanup the |touch_points_attributes_| map from released and canceled
619 // touch points.
620 Vector<int> releasedCanceledPoints;
621 for (auto& attributes : touch_points_attributes_.Values()) {
622 if (attributes->event_.GetType() == WebInputEvent::kPointerUp ||
623 attributes->event_.GetType() == WebInputEvent::kPointerCancel) {
624 releasedCanceledPoints.push_back(attributes->event_.id);
625 } else {
626 attributes->stale_ = true;
627 attributes->event_.movement_x = 0;
628 attributes->event_.movement_y = 0;
494 } 629 }
495 } 630 }
496 631 for (int id : releasedCanceledPoints) {
497 touch_pressed_ = !all_touches_released; 632 touch_points_attributes_.erase(id);
498
499 // If there's no document receiving touch events, or no handlers on the
500 // document set to receive the events, then we can skip all the rest of
501 // this work.
502 if (!touch_sequence_document_ || !touch_sequence_document_->GetPage() ||
503 !HasTouchHandlers(
504 touch_sequence_document_->GetPage()->GetEventHandlerRegistry()) ||
505 !touch_sequence_document_->GetFrame()) {
506 if (all_touches_released) {
507 touch_sequence_document_.Clear();
508 }
509 return false;
510 } 633 }
511 634
512 return true; 635 if (touch_points_attributes_.IsEmpty()) {
636 allTouchesReleasedCleanup();
637 }
638
639 return result;
513 } 640 }
514 641
515 WebInputEventResult TouchEventManager::HandleTouchEvent( 642 void TouchEventManager::allTouchesReleasedCleanup() {
516 const WebTouchEvent& event, 643 touch_sequence_document_.Clear();
517 const Vector<WebTouchEvent>& coalesced_events, 644 current_touch_action_ = TouchAction::kTouchActionAuto;
518 const HeapVector<EventHandlingUtil::PointerEventTarget>&
519 pointer_event_targets) {
520 DCHECK(event.touches_length == pointer_event_targets.size());
521
522 if (!HitTestTouchPointsIfNeeded(event, pointer_event_targets))
523 return WebInputEventResult::kNotHandled;
524
525 bool all_touches_released = true;
526 for (unsigned i = 0; i < event.touches_length; ++i) {
527 WebTouchPoint::State state = event.touches[i].state;
528 if (state != WebTouchPoint::kStateReleased &&
529 state != WebTouchPoint::kStateCancelled)
530 all_touches_released = false;
531 }
532
533 return DispatchTouchEvents(event, coalesced_events, all_touches_released);
534 } 645 }
535 646
536 bool TouchEventManager::IsAnyTouchActive() const { 647 bool TouchEventManager::IsAnyTouchActive() const {
537 return touch_pressed_; 648 return !touch_points_attributes_.IsEmpty();
538 } 649 }
539 650
540 } // namespace blink 651 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698