summaryrefslogtreecommitdiff
path: root/chromium/third_party/WebKit/Source/core/events/PointerEventFactory.cpp
blob: 48d8ec26f850172ecb06852ff79eaee5132ff584 (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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// 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.

#include "core/events/PointerEventFactory.h"

#include "core/frame/LocalFrameView.h"
#include "platform/geometry/FloatSize.h"

namespace blink {

namespace {

inline int ToInt(WebPointerProperties::PointerType t) {
  return static_cast<int>(t);
}

const char* PointerTypeNameForWebPointPointerType(
    WebPointerProperties::PointerType type) {
  // TODO(mustaq): Fix when the spec starts supporting hovering erasers.
  switch (type) {
    case WebPointerProperties::PointerType::kUnknown:
      return "";
    case WebPointerProperties::PointerType::kTouch:
      return "touch";
    case WebPointerProperties::PointerType::kPen:
      return "pen";
    case WebPointerProperties::PointerType::kMouse:
      return "mouse";
    default:
      NOTREACHED();
      return "";
  }
}

const AtomicString& PointerEventNameForMouseEventName(
    const AtomicString& mouse_event_name) {
#define RETURN_CORRESPONDING_PE_NAME(eventSuffix)               \
  if (mouse_event_name == EventTypeNames::mouse##eventSuffix) { \
    return EventTypeNames::pointer##eventSuffix;                \
  }

  RETURN_CORRESPONDING_PE_NAME(down);
  RETURN_CORRESPONDING_PE_NAME(enter);
  RETURN_CORRESPONDING_PE_NAME(leave);
  RETURN_CORRESPONDING_PE_NAME(move);
  RETURN_CORRESPONDING_PE_NAME(out);
  RETURN_CORRESPONDING_PE_NAME(over);
  RETURN_CORRESPONDING_PE_NAME(up);

#undef RETURN_CORRESPONDING_PE_NAME

  NOTREACHED();
  return g_empty_atom;
}

unsigned short ButtonToButtonsBitfield(WebPointerProperties::Button button) {
#define CASE_BUTTON_TO_BUTTONS(enumLabel)       \
  case WebPointerProperties::Button::enumLabel: \
    return static_cast<unsigned short>(WebPointerProperties::Buttons::enumLabel)

  switch (button) {
    CASE_BUTTON_TO_BUTTONS(kNoButton);
    CASE_BUTTON_TO_BUTTONS(kLeft);
    CASE_BUTTON_TO_BUTTONS(kRight);
    CASE_BUTTON_TO_BUTTONS(kMiddle);
    CASE_BUTTON_TO_BUTTONS(kBack);
    CASE_BUTTON_TO_BUTTONS(kForward);
    CASE_BUTTON_TO_BUTTONS(kEraser);
  }

#undef CASE_BUTTON_TO_BUTTONS

  NOTREACHED();
  return 0;
}

const AtomicString& PointerEventNameForEventType(WebInputEvent::Type type) {
  switch (type) {
    case WebInputEvent::kPointerDown:
      return EventTypeNames::pointerdown;
    case WebInputEvent::kPointerUp:
      return EventTypeNames::pointerup;
    case WebInputEvent::kPointerMove:
      return EventTypeNames::pointermove;
    case WebInputEvent::kPointerCancel:
      return EventTypeNames::pointercancel;
    default:
      NOTREACHED();
      return g_empty_atom;
  }
}

float GetPointerEventPressure(float force, int buttons) {
  if (!buttons)
    return 0;
  if (std::isnan(force))
    return 0.5;
  return force;
}

void UpdateTouchPointerEventInit(const WebPointerEvent& web_pointer_event,
                                 LocalDOMWindow* dom_window,
                                 PointerEventInit* pointer_event_init) {
  // This function should not update attributes like pointerId, isPrimary,
  // and pointerType which is the same among the coalesced events and the
  // dispatched event.

  if (dom_window && dom_window->GetFrame() && dom_window->GetFrame()->View()) {
    LocalFrame* frame = dom_window->GetFrame();
    FloatPoint page_point = frame->View()->RootFrameToContents(
        web_pointer_event.PositionInWidget());
    float scale_factor = 1.0f / frame->PageZoomFactor();
    FloatPoint scroll_position(frame->View()->GetScrollOffset());
    FloatPoint client_point = page_point.ScaledBy(scale_factor);
    client_point.MoveBy(scroll_position.ScaledBy(-scale_factor));

    pointer_event_init->setClientX(client_point.X());
    pointer_event_init->setClientY(client_point.Y());

    if (web_pointer_event.GetType() == WebInputEvent::kPointerMove) {
      pointer_event_init->setMovementX(web_pointer_event.movement_x);
      pointer_event_init->setMovementY(web_pointer_event.movement_y);
    }

    FloatSize point_shape =
        FloatSize(web_pointer_event.width, web_pointer_event.height)
            .ScaledBy(scale_factor);
    pointer_event_init->setWidth(point_shape.Width());
    pointer_event_init->setHeight(point_shape.Height());
  }

  pointer_event_init->setScreenX(web_pointer_event.PositionInScreen().x);
  pointer_event_init->setScreenY(web_pointer_event.PositionInScreen().y);
  pointer_event_init->setPressure(GetPointerEventPressure(
      web_pointer_event.force, pointer_event_init->buttons()));
  pointer_event_init->setTiltX(web_pointer_event.tilt_x);
  pointer_event_init->setTiltY(web_pointer_event.tilt_y);
  pointer_event_init->setTangentialPressure(
      web_pointer_event.tangential_pressure);
  pointer_event_init->setTwist(web_pointer_event.twist);
}

void UpdateMousePointerEventInit(const WebMouseEvent& mouse_event,
                                 LocalDOMWindow* dom_window,
                                 PointerEventInit* pointer_event_init) {
  // This function should not update attributes like pointerId, isPrimary,
  // and pointerType which is the same among the coalesced events and the
  // dispatched event.

  pointer_event_init->setScreenX(mouse_event.PositionInScreen().x);
  pointer_event_init->setScreenY(mouse_event.PositionInScreen().y);

  FloatPoint location_in_frame_zoomed;
  if (dom_window && dom_window->GetFrame() && dom_window->GetFrame()->View()) {
    LocalFrame* frame = dom_window->GetFrame();
    LocalFrameView* frame_view = frame->View();
    FloatPoint location_in_contents =
        frame_view->RootFrameToContents(mouse_event.PositionInRootFrame());
    location_in_frame_zoomed =
        frame_view->ContentsToFrame(location_in_contents);
    float scale_factor = 1 / frame->PageZoomFactor();
    location_in_frame_zoomed.Scale(scale_factor, scale_factor);
  }

  pointer_event_init->setClientX(location_in_frame_zoomed.X());
  pointer_event_init->setClientY(location_in_frame_zoomed.Y());

  pointer_event_init->setPressure(GetPointerEventPressure(
      mouse_event.force, pointer_event_init->buttons()));
  pointer_event_init->setTiltX(mouse_event.tilt_x);
  pointer_event_init->setTiltY(mouse_event.tilt_y);
  pointer_event_init->setTangentialPressure(mouse_event.tangential_pressure);
  pointer_event_init->setTwist(mouse_event.twist);

  IntPoint movement = FlooredIntPoint(mouse_event.MovementInRootFrame());
  pointer_event_init->setMovementX(movement.X());
  pointer_event_init->setMovementY(movement.Y());
}

}  // namespace

const int PointerEventFactory::kInvalidId = 0;

// Mouse id is 1 to behave the same as MS Edge for compatibility reasons.
const int PointerEventFactory::kMouseId = 1;

void PointerEventFactory::SetIdTypeButtons(
    PointerEventInit& pointer_event_init,
    const WebPointerProperties& pointer_properties,
    unsigned buttons,
    bool can_scroll) {
  WebPointerProperties::PointerType pointer_type =
      pointer_properties.pointer_type;
  // Tweak the |buttons| to reflect pen eraser mode only if the pen is in
  // active buttons state w/o even considering the eraser button.
  // TODO(mustaq): Fix when the spec starts supporting hovering erasers.
  if (pointer_type == WebPointerProperties::PointerType::kEraser) {
    if (buttons != 0) {
      buttons |= static_cast<unsigned>(WebPointerProperties::Buttons::kEraser);
      buttons &= ~static_cast<unsigned>(WebPointerProperties::Buttons::kLeft);
    }
    pointer_type = WebPointerProperties::PointerType::kPen;
  }
  pointer_event_init.setButtons(buttons);

  const IncomingId incoming_id(pointer_type, pointer_properties.id);
  int pointer_id = AddIdAndActiveButtons(incoming_id, buttons != 0, can_scroll);
  pointer_event_init.setPointerId(pointer_id);
  pointer_event_init.setPointerType(
      PointerTypeNameForWebPointPointerType(pointer_type));
  pointer_event_init.setIsPrimary(IsPrimary(pointer_id));
}

void PointerEventFactory::SetEventSpecificFields(
    PointerEventInit& pointer_event_init,
    const AtomicString& type) {
  pointer_event_init.setBubbles(type != EventTypeNames::pointerenter &&
                                type != EventTypeNames::pointerleave);
  pointer_event_init.setCancelable(type != EventTypeNames::pointerenter &&
                                   type != EventTypeNames::pointerleave &&
                                   type != EventTypeNames::pointercancel &&
                                   type != EventTypeNames::gotpointercapture &&
                                   type != EventTypeNames::lostpointercapture);

  pointer_event_init.setComposed(true);
  pointer_event_init.setDetail(0);
}

PointerEvent* PointerEventFactory::Create(
    const AtomicString& mouse_event_name,
    const WebMouseEvent& mouse_event,
    const Vector<WebMouseEvent>& coalesced_mouse_events,
    LocalDOMWindow* view) {
  DCHECK(mouse_event_name == EventTypeNames::mousemove ||
         mouse_event_name == EventTypeNames::mousedown ||
         mouse_event_name == EventTypeNames::mouseup);

  AtomicString pointer_event_name =
      PointerEventNameForMouseEventName(mouse_event_name);

  unsigned buttons = MouseEvent::WebInputEventModifiersToButtons(
      static_cast<WebInputEvent::Modifiers>(mouse_event.GetModifiers()));
  PointerEventInit pointer_event_init;

  SetIdTypeButtons(pointer_event_init, mouse_event, buttons, false);
  SetEventSpecificFields(pointer_event_init, pointer_event_name);

  if (pointer_event_name == EventTypeNames::pointerdown ||
      pointer_event_name == EventTypeNames::pointerup) {
    WebPointerProperties::Button button = mouse_event.button;
    // TODO(mustaq): Fix when the spec starts supporting hovering erasers.
    if (mouse_event.pointer_type ==
            WebPointerProperties::PointerType::kEraser &&
        button == WebPointerProperties::Button::kLeft)
      button = WebPointerProperties::Button::kEraser;
    pointer_event_init.setButton(static_cast<int>(button));
  } else {
    DCHECK(pointer_event_name == EventTypeNames::pointermove);
    pointer_event_init.setButton(
        static_cast<int>(WebPointerProperties::Button::kNoButton));
  }

  UIEventWithKeyState::SetFromWebInputEventModifiers(
      pointer_event_init,
      static_cast<WebInputEvent::Modifiers>(mouse_event.GetModifiers()));

  // Make sure chorded buttons fire pointermove instead of pointerup/down.
  if ((pointer_event_name == EventTypeNames::pointerdown &&
       (buttons & ~ButtonToButtonsBitfield(mouse_event.button)) != 0) ||
      (pointer_event_name == EventTypeNames::pointerup && buttons != 0))
    pointer_event_name = EventTypeNames::pointermove;

  pointer_event_init.setView(view);

  UpdateMousePointerEventInit(mouse_event, view, &pointer_event_init);

  // Create coalesced events init structure only for pointermove.
  if (pointer_event_name == EventTypeNames::pointermove) {
    HeapVector<Member<PointerEvent>> coalesced_pointer_events;
    for (const auto& coalesced_mouse_event : coalesced_mouse_events) {
      // TODO(crbug.com/733774): Disabled the DCHECK on the id of mouse events
      // because it failed on some versions of Mac OS.
      // DCHECK_EQ(mouse_event.id, coalesced_mouse_event.id);

      DCHECK_EQ(mouse_event.pointer_type, coalesced_mouse_event.pointer_type);
      PointerEventInit coalesced_event_init = pointer_event_init;
      coalesced_event_init.setCancelable(false);
      coalesced_event_init.setBubbles(false);
      UpdateMousePointerEventInit(coalesced_mouse_event, view,
                                  &coalesced_event_init);
      PointerEvent* event = PointerEvent::Create(
          pointer_event_name, coalesced_event_init,
          TimeTicks::FromSeconds(coalesced_mouse_event.TimeStampSeconds()));
      // Set the trusted flag for the coalesced events at the creation time
      // as oppose to the normal events which is done at the dispatch time. This
      // is because we don't want to go over all the coalesced events at every
      // dispatch and add the implementation complexity while it has no sensible
      // usecase at this time.
      event->SetTrusted(true);
      coalesced_pointer_events.push_back(event);
    }
    pointer_event_init.setCoalescedEvents(coalesced_pointer_events);
  }

  return PointerEvent::Create(
      pointer_event_name, pointer_event_init,
      TimeTicks::FromSeconds(mouse_event.TimeStampSeconds()));
}

PointerEvent* PointerEventFactory::Create(
    const WebPointerEvent& web_pointer_event,
    const Vector<WebPointerEvent>& coalesced_events,
    LocalDOMWindow* view) {
  const WebInputEvent::Type event_type = web_pointer_event.GetType();
  const AtomicString& type = PointerEventNameForEventType(event_type);

  bool pointer_released_or_cancelled =
      event_type == WebInputEvent::kPointerUp ||
      event_type == WebInputEvent::kPointerCancel;
  bool pointer_pressed_or_released =
      event_type == WebInputEvent::kPointerDown ||
      event_type == WebInputEvent::kPointerUp;

  PointerEventInit pointer_event_init;

  SetIdTypeButtons(pointer_event_init, web_pointer_event,
                   pointer_released_or_cancelled ? 0 : 1, true);
  pointer_event_init.setButton(static_cast<int>(
      pointer_pressed_or_released ? WebPointerProperties::Button::kLeft
                                  : WebPointerProperties::Button::kNoButton));

  pointer_event_init.setView(view);
  UpdateTouchPointerEventInit(web_pointer_event, view, &pointer_event_init);

  UIEventWithKeyState::SetFromWebInputEventModifiers(
      pointer_event_init,
      static_cast<WebInputEvent::Modifiers>(web_pointer_event.GetModifiers()));

  SetEventSpecificFields(pointer_event_init, type);

  if (type == EventTypeNames::pointermove) {
    HeapVector<Member<PointerEvent>> coalesced_pointer_events;
    for (const auto& coalesced_event : coalesced_events) {
      DCHECK_EQ(web_pointer_event.GetType(), coalesced_event.GetType());
      DCHECK_EQ(web_pointer_event.id, coalesced_event.id);
      DCHECK_EQ(web_pointer_event.pointer_type, coalesced_event.pointer_type);
      PointerEventInit coalesced_event_init = pointer_event_init;
      coalesced_event_init.setCancelable(false);
      coalesced_event_init.setBubbles(false);
      UpdateTouchPointerEventInit(coalesced_event, view, &coalesced_event_init);
      PointerEvent* event = PointerEvent::Create(
          type, coalesced_event_init,
          TimeTicks::FromSeconds(coalesced_event.TimeStampSeconds()));
      // Set the trusted flag for the coalesced events at the creation time
      // as oppose to the normal events which is done at the dispatch time. This
      // is because we don't want to go over all the coalesced events at every
      // dispatch and add the implementation complexity while it has no sensible
      // usecase at this time.
      event->SetTrusted(true);
      coalesced_pointer_events.push_back(event);
    }
    pointer_event_init.setCoalescedEvents(coalesced_pointer_events);
  }

  return PointerEvent::Create(
      type, pointer_event_init,
      TimeTicks::FromSeconds(web_pointer_event.TimeStampSeconds()));
}

PointerEvent* PointerEventFactory::CreatePointerCancelEvent(
    const int pointer_id,
    TimeTicks platfrom_time_stamp) {
  DCHECK(pointer_id_mapping_.Contains(pointer_id));
  pointer_id_mapping_.Set(
      pointer_id,
      PointerAttributes(pointer_id_mapping_.at(pointer_id).incoming_id, false,
                        false));

  PointerEventInit pointer_event_init;

  pointer_event_init.setPointerId(pointer_id);
  pointer_event_init.setPointerType(PointerTypeNameForWebPointPointerType(
      pointer_id_mapping_.at(pointer_id).incoming_id.GetPointerType()));
  pointer_event_init.setIsPrimary(IsPrimary(pointer_id));

  SetEventSpecificFields(pointer_event_init, EventTypeNames::pointercancel);

  return PointerEvent::Create(EventTypeNames::pointercancel, pointer_event_init,
                              platfrom_time_stamp);
}

PointerEvent* PointerEventFactory::CreatePointerEventFrom(
    PointerEvent* pointer_event,
    const AtomicString& type,
    EventTarget* related_target) {
  PointerEventInit pointer_event_init;

  pointer_event_init.setPointerId(pointer_event->pointerId());
  pointer_event_init.setPointerType(pointer_event->pointerType());
  pointer_event_init.setIsPrimary(pointer_event->isPrimary());
  pointer_event_init.setWidth(pointer_event->width());
  pointer_event_init.setHeight(pointer_event->height());
  pointer_event_init.setScreenX(pointer_event->screenX());
  pointer_event_init.setScreenY(pointer_event->screenY());
  pointer_event_init.setClientX(pointer_event->clientX());
  pointer_event_init.setClientY(pointer_event->clientY());
  pointer_event_init.setButton(pointer_event->button());
  pointer_event_init.setButtons(pointer_event->buttons());
  pointer_event_init.setPressure(pointer_event->pressure());
  pointer_event_init.setTiltX(pointer_event->tiltX());
  pointer_event_init.setTiltY(pointer_event->tiltY());
  pointer_event_init.setTangentialPressure(pointer_event->tangentialPressure());
  pointer_event_init.setTwist(pointer_event->twist());
  pointer_event_init.setView(pointer_event->view());

  SetEventSpecificFields(pointer_event_init, type);

  if (related_target)
    pointer_event_init.setRelatedTarget(related_target);

  return PointerEvent::Create(type, pointer_event_init,
                              pointer_event->PlatformTimeStamp());
}

PointerEvent* PointerEventFactory::CreatePointerCaptureEvent(
    PointerEvent* pointer_event,
    const AtomicString& type) {
  DCHECK(type == EventTypeNames::gotpointercapture ||
         type == EventTypeNames::lostpointercapture);

  return CreatePointerEventFrom(pointer_event, type,
                                pointer_event->relatedTarget());
}

PointerEvent* PointerEventFactory::CreatePointerBoundaryEvent(
    PointerEvent* pointer_event,
    const AtomicString& type,
    EventTarget* related_target) {
  DCHECK(type == EventTypeNames::pointerout ||
         type == EventTypeNames::pointerleave ||
         type == EventTypeNames::pointerover ||
         type == EventTypeNames::pointerenter);

  return CreatePointerEventFrom(pointer_event, type, related_target);
}

PointerEventFactory::PointerEventFactory() {
  Clear();
}

PointerEventFactory::~PointerEventFactory() {
  Clear();
}

void PointerEventFactory::Clear() {
  for (int type = 0;
       type <= ToInt(WebPointerProperties::PointerType::kLastEntry); type++) {
    primary_id_[type] = PointerEventFactory::kInvalidId;
    id_count_[type] = 0;
  }
  pointer_incoming_id_mapping_.clear();
  pointer_id_mapping_.clear();

  // Always add mouse pointer in initialization and never remove it.
  // No need to add it to m_pointerIncomingIdMapping as it is not going to be
  // used with the existing APIs
  primary_id_[ToInt(WebPointerProperties::PointerType::kMouse)] = kMouseId;
  pointer_id_mapping_.insert(
      kMouseId, PointerAttributes(
                    IncomingId(WebPointerProperties::PointerType::kMouse, 0),
                    false, false));

  current_id_ = PointerEventFactory::kMouseId + 1;
}

int PointerEventFactory::AddIdAndActiveButtons(const IncomingId p,
                                               bool is_active_buttons,
                                               bool can_scroll) {
  // Do not add extra mouse pointer as it was added in initialization
  if (p.GetPointerType() == WebPointerProperties::PointerType::kMouse) {
    pointer_id_mapping_.Set(kMouseId,
                            PointerAttributes(p, is_active_buttons, false));
    return kMouseId;
  }

  if (pointer_incoming_id_mapping_.Contains(p)) {
    int mapped_id = pointer_incoming_id_mapping_.at(p);
    pointer_id_mapping_.Set(
        mapped_id, PointerAttributes(p, is_active_buttons, can_scroll));
    return mapped_id;
  }
  int type_int = p.PointerTypeInt();
  // We do not handle the overflow of m_currentId as it should be very rare
  int mapped_id = current_id_++;
  if (!id_count_[type_int])
    primary_id_[type_int] = mapped_id;
  id_count_[type_int]++;
  pointer_incoming_id_mapping_.insert(p, mapped_id);
  pointer_id_mapping_.insert(
      mapped_id, PointerAttributes(p, is_active_buttons, can_scroll));
  return mapped_id;
}

bool PointerEventFactory::Remove(const int mapped_id) {
  // Do not remove mouse pointer id as it should always be there
  if (mapped_id == kMouseId || !pointer_id_mapping_.Contains(mapped_id))
    return false;

  IncomingId p = pointer_id_mapping_.at(mapped_id).incoming_id;
  int type_int = p.PointerTypeInt();
  pointer_id_mapping_.erase(mapped_id);
  pointer_incoming_id_mapping_.erase(p);
  if (primary_id_[type_int] == mapped_id)
    primary_id_[type_int] = PointerEventFactory::kInvalidId;
  id_count_[type_int]--;
  return true;
}

Vector<int> PointerEventFactory::GetPointerIdsOfScrollCapablePointers() const {
  Vector<int> mapped_ids;

  for (auto iter = pointer_id_mapping_.begin();
       iter != pointer_id_mapping_.end(); ++iter) {
    int mapped_id = iter->key;
    if (iter->value.can_scroll)
      mapped_ids.push_back(mapped_id);
  }

  // Sorting for a predictable ordering.
  std::sort(mapped_ids.begin(), mapped_ids.end());
  return mapped_ids;
}

bool PointerEventFactory::IsPrimary(int mapped_id) const {
  if (!pointer_id_mapping_.Contains(mapped_id))
    return false;

  IncomingId p = pointer_id_mapping_.at(mapped_id).incoming_id;
  return primary_id_[p.PointerTypeInt()] == mapped_id;
}

bool PointerEventFactory::IsActive(const int pointer_id) const {
  return pointer_id_mapping_.Contains(pointer_id);
}

bool PointerEventFactory::IsActiveButtonsState(const int pointer_id) const {
  return pointer_id_mapping_.Contains(pointer_id) &&
         pointer_id_mapping_.at(pointer_id).is_active_buttons;
}

WebPointerProperties::PointerType PointerEventFactory::GetPointerType(
    int pointer_id) const {
  if (!IsActive(pointer_id))
    return WebPointerProperties::PointerType::kUnknown;
  return pointer_id_mapping_.at(pointer_id).incoming_id.GetPointerType();
}

int PointerEventFactory::GetPointerEventId(
    const WebPointerProperties& properties) const {
  if (properties.pointer_type == WebPointerProperties::PointerType::kMouse)
    return PointerEventFactory::kMouseId;
  IncomingId id(properties.pointer_type, properties.id);
  if (pointer_incoming_id_mapping_.Contains(id))
    return pointer_incoming_id_mapping_.at(id);
  return PointerEventFactory::kInvalidId;
}

}  // namespace blink