Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / common / input / web_input_event_traits.cc
blobf580ecdc4dd7c44c1150bc38080d4c855b6f5532
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/common/input/web_input_event_traits.h"
7 #include <bitset>
8 #include <limits>
10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h"
13 using base::StringAppendF;
14 using base::SStringPrintf;
15 using blink::WebGestureEvent;
16 using blink::WebInputEvent;
17 using blink::WebKeyboardEvent;
18 using blink::WebMouseEvent;
19 using blink::WebMouseWheelEvent;
20 using blink::WebTouchEvent;
21 using blink::WebTouchPoint;
22 using std::numeric_limits;
24 namespace content {
25 namespace {
27 const int kInvalidTouchIndex = -1;
29 void ApppendEventDetails(const WebKeyboardEvent& event, std::string* result) {
30 StringAppendF(result,
31 "{\n WinCode: %d\n NativeCode: %d\n IsSystem: %d\n"
32 " Text: %s\n UnmodifiedText: %s\n KeyIdentifier: %s\n}",
33 event.windowsKeyCode,
34 event.nativeKeyCode,
35 event.isSystemKey,
36 reinterpret_cast<const char*>(event.text),
37 reinterpret_cast<const char*>(event.unmodifiedText),
38 reinterpret_cast<const char*>(event.keyIdentifier));
41 void ApppendEventDetails(const WebMouseEvent& event, std::string* result) {
42 StringAppendF(result,
43 "{\n Button: %d\n Pos: (%d, %d)\n GlobalPos: (%d, %d)\n"
44 " Movement: (%d, %d)\n Clicks: %d\n}",
45 event.button,
46 event.x,
47 event.y,
48 event.globalX,
49 event.globalY,
50 event.movementX,
51 event.movementY,
52 event.clickCount);
55 void ApppendEventDetails(const WebMouseWheelEvent& event, std::string* result) {
56 StringAppendF(result,
57 "{\n Delta: (%f, %f)\n WheelTicks: (%f, %f)\n Accel: (%f, %f)\n"
58 " ScrollByPage: %d\n HasPreciseScrollingDeltas: %d\n"
59 " Phase: (%d, %d)\n CanRubberband: (%d, %d)\n CanScroll: %d\n}",
60 event.deltaX,
61 event.deltaY,
62 event.wheelTicksX,
63 event.wheelTicksY,
64 event.accelerationRatioX,
65 event.accelerationRatioY,
66 event.scrollByPage,
67 event.hasPreciseScrollingDeltas,
68 event.phase,
69 event.momentumPhase,
70 event.canRubberbandLeft,
71 event.canRubberbandRight,
72 event.canScroll);
75 void ApppendEventDetails(const WebGestureEvent& event, std::string* result) {
76 StringAppendF(result,
77 "{\n Pos: (%d, %d)\n GlobalPos: (%d, %d)\n SourceDevice: %d\n"
78 " RawData: (%f, %f, %f, %f, %d)\n}",
79 event.x,
80 event.y,
81 event.globalX,
82 event.globalY,
83 event.sourceDevice,
84 event.data.scrollUpdate.deltaX,
85 event.data.scrollUpdate.deltaY,
86 event.data.scrollUpdate.velocityX,
87 event.data.scrollUpdate.velocityY,
88 event.data.scrollUpdate.previousUpdateInSequencePrevented);
91 void ApppendTouchPointDetails(const WebTouchPoint& point, std::string* result) {
92 StringAppendF(result,
93 " (ID: %d, State: %d, ScreenPos: (%f, %f), Pos: (%f, %f),"
94 " Radius: (%f, %f), Rot: %f, Force: %f),\n",
95 point.id,
96 point.state,
97 point.screenPosition.x,
98 point.screenPosition.y,
99 point.position.x,
100 point.position.y,
101 point.radiusX,
102 point.radiusY,
103 point.rotationAngle,
104 point.force);
107 void ApppendEventDetails(const WebTouchEvent& event, std::string* result) {
108 StringAppendF(result,
109 "{\n Touches: %u, Cancelable: %d, CausesScrolling: %d,"
110 " uniqueTouchEventId: %u\n[\n",
111 event.touchesLength, event.cancelable,
112 event.causesScrollingIfUncanceled, event.uniqueTouchEventId);
113 for (unsigned i = 0; i < event.touchesLength; ++i)
114 ApppendTouchPointDetails(event.touches[i], result);
115 result->append(" ]\n}");
118 bool CanCoalesce(const WebKeyboardEvent& event_to_coalesce,
119 const WebKeyboardEvent& event) {
120 return false;
123 void Coalesce(const WebKeyboardEvent& event_to_coalesce,
124 WebKeyboardEvent* event) {
125 DCHECK(CanCoalesce(event_to_coalesce, *event));
128 bool CanCoalesce(const WebMouseEvent& event_to_coalesce,
129 const WebMouseEvent& event) {
130 return event.type == event_to_coalesce.type &&
131 event.type == WebInputEvent::MouseMove;
134 void Coalesce(const WebMouseEvent& event_to_coalesce, WebMouseEvent* event) {
135 DCHECK(CanCoalesce(event_to_coalesce, *event));
136 // Accumulate movement deltas.
137 int x = event->movementX;
138 int y = event->movementY;
139 *event = event_to_coalesce;
140 event->movementX += x;
141 event->movementY += y;
144 bool CanCoalesce(const WebMouseWheelEvent& event_to_coalesce,
145 const WebMouseWheelEvent& event) {
146 return event.modifiers == event_to_coalesce.modifiers &&
147 event.scrollByPage == event_to_coalesce.scrollByPage &&
148 event.phase == event_to_coalesce.phase &&
149 event.momentumPhase == event_to_coalesce.momentumPhase &&
150 event.hasPreciseScrollingDeltas ==
151 event_to_coalesce.hasPreciseScrollingDeltas &&
152 event.canScroll == event_to_coalesce.canScroll;
155 float GetUnacceleratedDelta(float accelerated_delta, float acceleration_ratio) {
156 return accelerated_delta * acceleration_ratio;
159 float GetAccelerationRatio(float accelerated_delta, float unaccelerated_delta) {
160 if (unaccelerated_delta == 0.f || accelerated_delta == 0.f)
161 return 1.f;
162 return unaccelerated_delta / accelerated_delta;
165 void Coalesce(const WebMouseWheelEvent& event_to_coalesce,
166 WebMouseWheelEvent* event) {
167 DCHECK(CanCoalesce(event_to_coalesce, *event));
168 float unaccelerated_x =
169 GetUnacceleratedDelta(event->deltaX,
170 event->accelerationRatioX) +
171 GetUnacceleratedDelta(event_to_coalesce.deltaX,
172 event_to_coalesce.accelerationRatioX);
173 float unaccelerated_y =
174 GetUnacceleratedDelta(event->deltaY,
175 event->accelerationRatioY) +
176 GetUnacceleratedDelta(event_to_coalesce.deltaY,
177 event_to_coalesce.accelerationRatioY);
178 event->deltaX += event_to_coalesce.deltaX;
179 event->deltaY += event_to_coalesce.deltaY;
180 event->wheelTicksX += event_to_coalesce.wheelTicksX;
181 event->wheelTicksY += event_to_coalesce.wheelTicksY;
182 event->accelerationRatioX =
183 GetAccelerationRatio(event->deltaX, unaccelerated_x);
184 event->accelerationRatioY =
185 GetAccelerationRatio(event->deltaY, unaccelerated_y);
186 DCHECK_GE(event_to_coalesce.timeStampSeconds, event->timeStampSeconds);
187 event->timeStampSeconds = event_to_coalesce.timeStampSeconds;
190 // Returns |kInvalidTouchIndex| iff |event| lacks a touch with an ID of |id|.
191 int GetIndexOfTouchID(const WebTouchEvent& event, int id) {
192 for (unsigned i = 0; i < event.touchesLength; ++i) {
193 if (event.touches[i].id == id)
194 return i;
196 return kInvalidTouchIndex;
199 bool CanCoalesce(const WebTouchEvent& event_to_coalesce,
200 const WebTouchEvent& event) {
201 if (event.type != event_to_coalesce.type ||
202 event.type != WebInputEvent::TouchMove ||
203 event.modifiers != event_to_coalesce.modifiers ||
204 event.touchesLength != event_to_coalesce.touchesLength ||
205 event.touchesLength > WebTouchEvent::touchesLengthCap)
206 return false;
208 static_assert(WebTouchEvent::touchesLengthCap <= sizeof(int32_t) * 8U,
209 "suboptimal touchesLengthCap size");
210 // Ensure that we have a 1-to-1 mapping of pointer ids between touches.
211 std::bitset<WebTouchEvent::touchesLengthCap> unmatched_event_touches(
212 (1 << event.touchesLength) - 1);
213 for (unsigned i = 0; i < event_to_coalesce.touchesLength; ++i) {
214 int event_touch_index =
215 GetIndexOfTouchID(event, event_to_coalesce.touches[i].id);
216 if (event_touch_index == kInvalidTouchIndex)
217 return false;
218 if (!unmatched_event_touches[event_touch_index])
219 return false;
220 unmatched_event_touches[event_touch_index] = false;
222 return unmatched_event_touches.none();
225 void Coalesce(const WebTouchEvent& event_to_coalesce, WebTouchEvent* event) {
226 DCHECK(CanCoalesce(event_to_coalesce, *event));
227 // The WebTouchPoints include absolute position information. So it is
228 // sufficient to simply replace the previous event with the new event->
229 // However, it is necessary to make sure that all the points have the
230 // correct state, i.e. the touch-points that moved in the last event, but
231 // didn't change in the current event, will have Stationary state. It is
232 // necessary to change them back to Moved state.
233 WebTouchEvent old_event = *event;
234 *event = event_to_coalesce;
235 for (unsigned i = 0; i < event->touchesLength; ++i) {
236 int i_old = GetIndexOfTouchID(old_event, event->touches[i].id);
237 if (old_event.touches[i_old].state == blink::WebTouchPoint::StateMoved)
238 event->touches[i].state = blink::WebTouchPoint::StateMoved;
240 event->causesScrollingIfUncanceled |= old_event.causesScrollingIfUncanceled;
243 bool CanCoalesce(const WebGestureEvent& event_to_coalesce,
244 const WebGestureEvent& event) {
245 if (event.type != event_to_coalesce.type ||
246 event.sourceDevice != event_to_coalesce.sourceDevice ||
247 event.modifiers != event_to_coalesce.modifiers)
248 return false;
250 if (event.type == WebInputEvent::GestureScrollUpdate)
251 return true;
253 // GesturePinchUpdate scales can be combined only if they share a focal point,
254 // e.g., with double-tap drag zoom.
255 if (event.type == WebInputEvent::GesturePinchUpdate &&
256 event.x == event_to_coalesce.x &&
257 event.y == event_to_coalesce.y)
258 return true;
260 return false;
263 void Coalesce(const WebGestureEvent& event_to_coalesce,
264 WebGestureEvent* event) {
265 DCHECK(CanCoalesce(event_to_coalesce, *event));
266 if (event->type == WebInputEvent::GestureScrollUpdate) {
267 event->data.scrollUpdate.deltaX +=
268 event_to_coalesce.data.scrollUpdate.deltaX;
269 event->data.scrollUpdate.deltaY +=
270 event_to_coalesce.data.scrollUpdate.deltaY;
271 DCHECK_EQ(
272 event->data.scrollUpdate.previousUpdateInSequencePrevented,
273 event_to_coalesce.data.scrollUpdate.previousUpdateInSequencePrevented);
274 } else if (event->type == WebInputEvent::GesturePinchUpdate) {
275 event->data.pinchUpdate.scale *= event_to_coalesce.data.pinchUpdate.scale;
276 // Ensure the scale remains bounded above 0 and below Infinity so that
277 // we can reliably perform operations like log on the values.
278 if (event->data.pinchUpdate.scale < numeric_limits<float>::min())
279 event->data.pinchUpdate.scale = numeric_limits<float>::min();
280 else if (event->data.pinchUpdate.scale > numeric_limits<float>::max())
281 event->data.pinchUpdate.scale = numeric_limits<float>::max();
285 struct WebInputEventToString {
286 template <class EventType>
287 bool Execute(const WebInputEvent& event, std::string* result) const {
288 SStringPrintf(result, "%s (Time: %lf, Modifiers: %d)\n",
289 WebInputEventTraits::GetName(event.type),
290 event.timeStampSeconds,
291 event.modifiers);
292 const EventType& typed_event = static_cast<const EventType&>(event);
293 ApppendEventDetails(typed_event, result);
294 return true;
298 struct WebInputEventSize {
299 template <class EventType>
300 bool Execute(WebInputEvent::Type /* type */, size_t* type_size) const {
301 *type_size = sizeof(EventType);
302 return true;
306 struct WebInputEventClone {
307 template <class EventType>
308 bool Execute(const WebInputEvent& event,
309 ScopedWebInputEvent* scoped_event) const {
310 DCHECK_EQ(sizeof(EventType), event.size);
311 *scoped_event = ScopedWebInputEvent(
312 new EventType(static_cast<const EventType&>(event)));
313 return true;
317 struct WebInputEventDelete {
318 template <class EventType>
319 bool Execute(WebInputEvent* event, bool* /* dummy_var */) const {
320 if (!event)
321 return false;
322 DCHECK_EQ(sizeof(EventType), event->size);
323 delete static_cast<EventType*>(event);
324 return true;
328 struct WebInputEventCanCoalesce {
329 template <class EventType>
330 bool Execute(const WebInputEvent& event_to_coalesce,
331 const WebInputEvent* event) const {
332 if (event_to_coalesce.type != event->type)
333 return false;
334 DCHECK_EQ(sizeof(EventType), event->size);
335 DCHECK_EQ(sizeof(EventType), event_to_coalesce.size);
336 return CanCoalesce(static_cast<const EventType&>(event_to_coalesce),
337 *static_cast<const EventType*>(event));
341 struct WebInputEventCoalesce {
342 template <class EventType>
343 bool Execute(const WebInputEvent& event_to_coalesce,
344 WebInputEvent* event) const {
345 Coalesce(static_cast<const EventType&>(event_to_coalesce),
346 static_cast<EventType*>(event));
347 return true;
351 template <typename Operator, typename ArgIn, typename ArgOut>
352 bool Apply(Operator op,
353 WebInputEvent::Type type,
354 const ArgIn& arg_in,
355 ArgOut* arg_out) {
356 if (WebInputEvent::isMouseEventType(type))
357 return op.template Execute<WebMouseEvent>(arg_in, arg_out);
358 else if (type == WebInputEvent::MouseWheel)
359 return op.template Execute<WebMouseWheelEvent>(arg_in, arg_out);
360 else if (WebInputEvent::isKeyboardEventType(type))
361 return op.template Execute<WebKeyboardEvent>(arg_in, arg_out);
362 else if (WebInputEvent::isTouchEventType(type))
363 return op.template Execute<WebTouchEvent>(arg_in, arg_out);
364 else if (WebInputEvent::isGestureEventType(type))
365 return op.template Execute<WebGestureEvent>(arg_in, arg_out);
367 NOTREACHED() << "Unknown webkit event type " << type;
368 return false;
371 } // namespace
373 const char* WebInputEventTraits::GetName(WebInputEvent::Type type) {
374 #define CASE_TYPE(t) case WebInputEvent::t: return #t
375 switch(type) {
376 CASE_TYPE(Undefined);
377 CASE_TYPE(MouseDown);
378 CASE_TYPE(MouseUp);
379 CASE_TYPE(MouseMove);
380 CASE_TYPE(MouseEnter);
381 CASE_TYPE(MouseLeave);
382 CASE_TYPE(ContextMenu);
383 CASE_TYPE(MouseWheel);
384 CASE_TYPE(RawKeyDown);
385 CASE_TYPE(KeyDown);
386 CASE_TYPE(KeyUp);
387 CASE_TYPE(Char);
388 CASE_TYPE(GestureScrollBegin);
389 CASE_TYPE(GestureScrollEnd);
390 CASE_TYPE(GestureScrollUpdate);
391 CASE_TYPE(GestureFlingStart);
392 CASE_TYPE(GestureFlingCancel);
393 CASE_TYPE(GestureShowPress);
394 CASE_TYPE(GestureTap);
395 CASE_TYPE(GestureTapUnconfirmed);
396 CASE_TYPE(GestureTapDown);
397 CASE_TYPE(GestureTapCancel);
398 CASE_TYPE(GestureDoubleTap);
399 CASE_TYPE(GestureTwoFingerTap);
400 CASE_TYPE(GestureLongPress);
401 CASE_TYPE(GestureLongTap);
402 CASE_TYPE(GesturePinchBegin);
403 CASE_TYPE(GesturePinchEnd);
404 CASE_TYPE(GesturePinchUpdate);
405 CASE_TYPE(TouchStart);
406 CASE_TYPE(TouchMove);
407 CASE_TYPE(TouchEnd);
408 CASE_TYPE(TouchCancel);
409 default:
410 // Must include default to let blink::WebInputEvent add new event types
411 // before they're added here.
412 DLOG(WARNING) <<
413 "Unhandled WebInputEvent type in WebInputEventTraits::GetName.\n";
414 break;
416 #undef CASE_TYPE
417 return "";
420 std::string WebInputEventTraits::ToString(const WebInputEvent& event) {
421 std::string result;
422 Apply(WebInputEventToString(), event.type, event, &result);
423 return result;
426 size_t WebInputEventTraits::GetSize(WebInputEvent::Type type) {
427 size_t size = 0;
428 Apply(WebInputEventSize(), type, type, &size);
429 return size;
432 ScopedWebInputEvent WebInputEventTraits::Clone(const WebInputEvent& event) {
433 ScopedWebInputEvent scoped_event;
434 Apply(WebInputEventClone(), event.type, event, &scoped_event);
435 return scoped_event.Pass();
438 void WebInputEventTraits::Delete(WebInputEvent* event) {
439 if (!event)
440 return;
441 bool dummy_var = false;
442 Apply(WebInputEventDelete(), event->type, event, &dummy_var);
445 bool WebInputEventTraits::CanCoalesce(const WebInputEvent& event_to_coalesce,
446 const WebInputEvent& event) {
447 // Early out before casting.
448 if (event_to_coalesce.type != event.type)
449 return false;
450 return Apply(WebInputEventCanCoalesce(),
451 event.type,
452 event_to_coalesce,
453 &event);
456 void WebInputEventTraits::Coalesce(const WebInputEvent& event_to_coalesce,
457 WebInputEvent* event) {
458 DCHECK(event);
459 Apply(WebInputEventCoalesce(), event->type, event_to_coalesce, event);
462 bool WebInputEventTraits::WillReceiveAckFromRenderer(
463 const WebInputEvent& event) {
464 switch (event.type) {
465 case WebInputEvent::MouseDown:
466 case WebInputEvent::MouseUp:
467 case WebInputEvent::MouseEnter:
468 case WebInputEvent::MouseLeave:
469 case WebInputEvent::ContextMenu:
470 case WebInputEvent::GestureScrollBegin:
471 case WebInputEvent::GestureScrollEnd:
472 case WebInputEvent::GestureShowPress:
473 case WebInputEvent::GestureTapUnconfirmed:
474 case WebInputEvent::GestureTapDown:
475 case WebInputEvent::GestureTapCancel:
476 case WebInputEvent::GesturePinchBegin:
477 case WebInputEvent::GesturePinchEnd:
478 case WebInputEvent::TouchCancel:
479 return false;
480 case WebInputEvent::TouchStart:
481 case WebInputEvent::TouchEnd:
482 return static_cast<const WebTouchEvent&>(event).cancelable;
483 default:
484 return true;
488 uint32 WebInputEventTraits::GetUniqueTouchEventId(const WebInputEvent& event) {
489 if (WebInputEvent::isTouchEventType(event.type)) {
490 return static_cast<const WebTouchEvent&>(event).uniqueTouchEventId;
492 return 0U;
495 } // namespace content