Change next_proto member type.
[chromium-blink-merge.git] / content / common / input / web_input_event_traits.cc
blob087cf61e0cf2e8869c73ba55f03c4af33b102489
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 WindowPos: (%d, %d)\n"
44 " GlobalPos: (%d, %d)\n Movement: (%d, %d)\n Clicks: %d\n}",
45 event.button,
46 event.x,
47 event.y,
48 event.windowX,
49 event.windowY,
50 event.globalX,
51 event.globalY,
52 event.movementX,
53 event.movementY,
54 event.clickCount);
57 void ApppendEventDetails(const WebMouseWheelEvent& event, std::string* result) {
58 StringAppendF(result,
59 "{\n Delta: (%f, %f)\n WheelTicks: (%f, %f)\n Accel: (%f, %f)\n"
60 " ScrollByPage: %d\n HasPreciseScrollingDeltas: %d\n"
61 " Phase: (%d, %d)\n CanRubberband: (%d, %d)\n}",
62 event.deltaX,
63 event.deltaY,
64 event.wheelTicksX,
65 event.wheelTicksY,
66 event.accelerationRatioX,
67 event.accelerationRatioY,
68 event.scrollByPage,
69 event.hasPreciseScrollingDeltas,
70 event.phase,
71 event.momentumPhase,
72 event.canRubberbandLeft,
73 event.canRubberbandRight);
76 void ApppendEventDetails(const WebGestureEvent& event, std::string* result) {
77 StringAppendF(result,
78 "{\n Pos: (%d, %d)\n GlobalPos: (%d, %d)\n SourceDevice: %d\n"
79 " RawData: (%f, %f, %f, %f, %d)\n}",
80 event.x,
81 event.y,
82 event.globalX,
83 event.globalY,
84 event.sourceDevice,
85 event.data.scrollUpdate.deltaX,
86 event.data.scrollUpdate.deltaY,
87 event.data.scrollUpdate.velocityX,
88 event.data.scrollUpdate.velocityY,
89 event.data.scrollUpdate.previousUpdateInSequencePrevented);
92 void ApppendTouchPointDetails(const WebTouchPoint& point, std::string* result) {
93 StringAppendF(result,
94 " (ID: %d, State: %d, ScreenPos: (%f, %f), Pos: (%f, %f),"
95 " Radius: (%f, %f), Rot: %f, Force: %f),\n",
96 point.id,
97 point.state,
98 point.screenPosition.x,
99 point.screenPosition.y,
100 point.position.x,
101 point.position.y,
102 point.radiusX,
103 point.radiusY,
104 point.rotationAngle,
105 point.force);
108 void ApppendEventDetails(const WebTouchEvent& event, std::string* result) {
109 StringAppendF(result,
110 "{\n Touches: %u, Cancelable: %d, CausesScrolling: %d\n[\n",
111 event.touchesLength,
112 event.cancelable,
113 event.causesScrollingIfUncanceled);
114 for (unsigned i = 0; i < event.touchesLength; ++i)
115 ApppendTouchPointDetails(event.touches[i], result);
116 result->append(" ]\n}");
119 bool CanCoalesce(const WebKeyboardEvent& event_to_coalesce,
120 const WebKeyboardEvent& event) {
121 return false;
124 void Coalesce(const WebKeyboardEvent& event_to_coalesce,
125 WebKeyboardEvent* event) {
126 DCHECK(CanCoalesce(event_to_coalesce, *event));
129 bool CanCoalesce(const WebMouseEvent& event_to_coalesce,
130 const WebMouseEvent& event) {
131 return event.type == event_to_coalesce.type &&
132 event.type == WebInputEvent::MouseMove;
135 void Coalesce(const WebMouseEvent& event_to_coalesce, WebMouseEvent* event) {
136 DCHECK(CanCoalesce(event_to_coalesce, *event));
137 // Accumulate movement deltas.
138 int x = event->movementX;
139 int y = event->movementY;
140 *event = event_to_coalesce;
141 event->movementX += x;
142 event->movementY += y;
145 bool CanCoalesce(const WebMouseWheelEvent& event_to_coalesce,
146 const WebMouseWheelEvent& event) {
147 return event.modifiers == event_to_coalesce.modifiers &&
148 event.scrollByPage == event_to_coalesce.scrollByPage &&
149 event.phase == event_to_coalesce.phase &&
150 event.momentumPhase == event_to_coalesce.momentumPhase &&
151 event.hasPreciseScrollingDeltas ==
152 event_to_coalesce.hasPreciseScrollingDeltas;
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 COMPILE_ASSERT(WebTouchEvent::touchesLengthCap <= sizeof(int32_t) * 8U,
209 suboptimal_touches_length_cap_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;
242 bool CanCoalesce(const WebGestureEvent& event_to_coalesce,
243 const WebGestureEvent& event) {
244 if (event.type != event_to_coalesce.type ||
245 event.sourceDevice != event_to_coalesce.sourceDevice ||
246 event.modifiers != event_to_coalesce.modifiers)
247 return false;
249 if (event.type == WebInputEvent::GestureScrollUpdate)
250 return true;
252 // GesturePinchUpdate scales can be combined only if they share a focal point,
253 // e.g., with double-tap drag zoom.
254 if (event.type == WebInputEvent::GesturePinchUpdate &&
255 event.x == event_to_coalesce.x &&
256 event.y == event_to_coalesce.y)
257 return true;
259 return false;
262 void Coalesce(const WebGestureEvent& event_to_coalesce,
263 WebGestureEvent* event) {
264 DCHECK(CanCoalesce(event_to_coalesce, *event));
265 if (event->type == WebInputEvent::GestureScrollUpdate) {
266 event->data.scrollUpdate.deltaX +=
267 event_to_coalesce.data.scrollUpdate.deltaX;
268 event->data.scrollUpdate.deltaY +=
269 event_to_coalesce.data.scrollUpdate.deltaY;
270 DCHECK_EQ(
271 event->data.scrollUpdate.previousUpdateInSequencePrevented,
272 event_to_coalesce.data.scrollUpdate.previousUpdateInSequencePrevented);
273 } else if (event->type == WebInputEvent::GesturePinchUpdate) {
274 event->data.pinchUpdate.scale *= event_to_coalesce.data.pinchUpdate.scale;
275 // Ensure the scale remains bounded above 0 and below Infinity so that
276 // we can reliably perform operations like log on the values.
277 if (event->data.pinchUpdate.scale < numeric_limits<float>::min())
278 event->data.pinchUpdate.scale = numeric_limits<float>::min();
279 else if (event->data.pinchUpdate.scale > numeric_limits<float>::max())
280 event->data.pinchUpdate.scale = numeric_limits<float>::max();
284 struct WebInputEventToString {
285 template <class EventType>
286 bool Execute(const WebInputEvent& event, std::string* result) const {
287 SStringPrintf(result, "%s (Time: %lf, Modifiers: %d)\n",
288 WebInputEventTraits::GetName(event.type),
289 event.timeStampSeconds,
290 event.modifiers);
291 const EventType& typed_event = static_cast<const EventType&>(event);
292 ApppendEventDetails(typed_event, result);
293 return true;
297 struct WebInputEventSize {
298 template <class EventType>
299 bool Execute(WebInputEvent::Type /* type */, size_t* type_size) const {
300 *type_size = sizeof(EventType);
301 return true;
305 struct WebInputEventClone {
306 template <class EventType>
307 bool Execute(const WebInputEvent& event,
308 ScopedWebInputEvent* scoped_event) const {
309 DCHECK_EQ(sizeof(EventType), event.size);
310 *scoped_event = ScopedWebInputEvent(
311 new EventType(static_cast<const EventType&>(event)));
312 return true;
316 struct WebInputEventDelete {
317 template <class EventType>
318 bool Execute(WebInputEvent* event, bool* /* dummy_var */) const {
319 if (!event)
320 return false;
321 DCHECK_EQ(sizeof(EventType), event->size);
322 delete static_cast<EventType*>(event);
323 return true;
327 struct WebInputEventCanCoalesce {
328 template <class EventType>
329 bool Execute(const WebInputEvent& event_to_coalesce,
330 const WebInputEvent* event) const {
331 if (event_to_coalesce.type != event->type)
332 return false;
333 DCHECK_EQ(sizeof(EventType), event->size);
334 DCHECK_EQ(sizeof(EventType), event_to_coalesce.size);
335 return CanCoalesce(static_cast<const EventType&>(event_to_coalesce),
336 *static_cast<const EventType*>(event));
340 struct WebInputEventCoalesce {
341 template <class EventType>
342 bool Execute(const WebInputEvent& event_to_coalesce,
343 WebInputEvent* event) const {
344 Coalesce(static_cast<const EventType&>(event_to_coalesce),
345 static_cast<EventType*>(event));
346 return true;
350 template <typename Operator, typename ArgIn, typename ArgOut>
351 bool Apply(Operator op,
352 WebInputEvent::Type type,
353 const ArgIn& arg_in,
354 ArgOut* arg_out) {
355 if (WebInputEvent::isMouseEventType(type))
356 return op.template Execute<WebMouseEvent>(arg_in, arg_out);
357 else if (type == WebInputEvent::MouseWheel)
358 return op.template Execute<WebMouseWheelEvent>(arg_in, arg_out);
359 else if (WebInputEvent::isKeyboardEventType(type))
360 return op.template Execute<WebKeyboardEvent>(arg_in, arg_out);
361 else if (WebInputEvent::isTouchEventType(type))
362 return op.template Execute<WebTouchEvent>(arg_in, arg_out);
363 else if (WebInputEvent::isGestureEventType(type))
364 return op.template Execute<WebGestureEvent>(arg_in, arg_out);
366 NOTREACHED() << "Unknown webkit event type " << type;
367 return false;
370 } // namespace
372 const char* WebInputEventTraits::GetName(WebInputEvent::Type type) {
373 #define CASE_TYPE(t) case WebInputEvent::t: return #t
374 switch(type) {
375 CASE_TYPE(Undefined);
376 CASE_TYPE(MouseDown);
377 CASE_TYPE(MouseUp);
378 CASE_TYPE(MouseMove);
379 CASE_TYPE(MouseEnter);
380 CASE_TYPE(MouseLeave);
381 CASE_TYPE(ContextMenu);
382 CASE_TYPE(MouseWheel);
383 CASE_TYPE(RawKeyDown);
384 CASE_TYPE(KeyDown);
385 CASE_TYPE(KeyUp);
386 CASE_TYPE(Char);
387 CASE_TYPE(GestureScrollBegin);
388 CASE_TYPE(GestureScrollEnd);
389 CASE_TYPE(GestureScrollUpdate);
390 CASE_TYPE(GestureFlingStart);
391 CASE_TYPE(GestureFlingCancel);
392 CASE_TYPE(GestureShowPress);
393 CASE_TYPE(GestureTap);
394 CASE_TYPE(GestureTapUnconfirmed);
395 CASE_TYPE(GestureTapDown);
396 CASE_TYPE(GestureTapCancel);
397 CASE_TYPE(GestureDoubleTap);
398 CASE_TYPE(GestureTwoFingerTap);
399 CASE_TYPE(GestureLongPress);
400 CASE_TYPE(GestureLongTap);
401 CASE_TYPE(GesturePinchBegin);
402 CASE_TYPE(GesturePinchEnd);
403 CASE_TYPE(GesturePinchUpdate);
404 CASE_TYPE(TouchStart);
405 CASE_TYPE(TouchMove);
406 CASE_TYPE(TouchEnd);
407 CASE_TYPE(TouchCancel);
408 default:
409 // Must include default to let blink::WebInputEvent add new event types
410 // before they're added here.
411 DLOG(WARNING) <<
412 "Unhandled WebInputEvent type in WebInputEventTraits::GetName.\n";
413 break;
415 #undef CASE_TYPE
416 return "";
419 std::string WebInputEventTraits::ToString(const WebInputEvent& event) {
420 std::string result;
421 Apply(WebInputEventToString(), event.type, event, &result);
422 return result;
425 size_t WebInputEventTraits::GetSize(WebInputEvent::Type type) {
426 size_t size = 0;
427 Apply(WebInputEventSize(), type, type, &size);
428 return size;
431 ScopedWebInputEvent WebInputEventTraits::Clone(const WebInputEvent& event) {
432 ScopedWebInputEvent scoped_event;
433 Apply(WebInputEventClone(), event.type, event, &scoped_event);
434 return scoped_event.Pass();
437 void WebInputEventTraits::Delete(WebInputEvent* event) {
438 if (!event)
439 return;
440 bool dummy_var = false;
441 Apply(WebInputEventDelete(), event->type, event, &dummy_var);
444 bool WebInputEventTraits::CanCoalesce(const WebInputEvent& event_to_coalesce,
445 const WebInputEvent& event) {
446 // Early out before casting.
447 if (event_to_coalesce.type != event.type)
448 return false;
449 return Apply(WebInputEventCanCoalesce(),
450 event.type,
451 event_to_coalesce,
452 &event);
455 void WebInputEventTraits::Coalesce(const WebInputEvent& event_to_coalesce,
456 WebInputEvent* event) {
457 DCHECK(event);
458 Apply(WebInputEventCoalesce(), event->type, event_to_coalesce, event);
461 bool WebInputEventTraits::IgnoresAckDisposition(const WebInputEvent& event) {
462 switch (event.type) {
463 case WebInputEvent::MouseDown:
464 case WebInputEvent::MouseUp:
465 case WebInputEvent::MouseEnter:
466 case WebInputEvent::MouseLeave:
467 case WebInputEvent::ContextMenu:
468 case WebInputEvent::GestureScrollBegin:
469 case WebInputEvent::GestureScrollEnd:
470 case WebInputEvent::GestureShowPress:
471 case WebInputEvent::GestureTapUnconfirmed:
472 case WebInputEvent::GestureTapDown:
473 case WebInputEvent::GestureTapCancel:
474 case WebInputEvent::GesturePinchBegin:
475 case WebInputEvent::GesturePinchEnd:
476 case WebInputEvent::TouchCancel:
477 return true;
478 case WebInputEvent::TouchStart:
479 case WebInputEvent::TouchMove:
480 case WebInputEvent::TouchEnd:
481 return !static_cast<const WebTouchEvent&>(event).cancelable;
482 default:
483 return false;
487 } // namespace content