Add ICU message format support
[chromium-blink-merge.git] / content / common / input / web_input_event_traits.cc
blob547290783f71d999e8f9d66c58443b1ceb37dfae
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 CanScroll: %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,
74 event.canScroll);
77 void ApppendEventDetails(const WebGestureEvent& event, std::string* result) {
78 StringAppendF(result,
79 "{\n Pos: (%d, %d)\n GlobalPos: (%d, %d)\n SourceDevice: %d\n"
80 " RawData: (%f, %f, %f, %f, %d)\n}",
81 event.x,
82 event.y,
83 event.globalX,
84 event.globalY,
85 event.sourceDevice,
86 event.data.scrollUpdate.deltaX,
87 event.data.scrollUpdate.deltaY,
88 event.data.scrollUpdate.velocityX,
89 event.data.scrollUpdate.velocityY,
90 event.data.scrollUpdate.previousUpdateInSequencePrevented);
93 void ApppendTouchPointDetails(const WebTouchPoint& point, std::string* result) {
94 StringAppendF(result,
95 " (ID: %d, State: %d, ScreenPos: (%f, %f), Pos: (%f, %f),"
96 " Radius: (%f, %f), Rot: %f, Force: %f),\n",
97 point.id,
98 point.state,
99 point.screenPosition.x,
100 point.screenPosition.y,
101 point.position.x,
102 point.position.y,
103 point.radiusX,
104 point.radiusY,
105 point.rotationAngle,
106 point.force);
109 void ApppendEventDetails(const WebTouchEvent& event, std::string* result) {
110 StringAppendF(result,
111 "{\n Touches: %u, Cancelable: %d, CausesScrolling: %d,"
112 " uniqueTouchEventId: %u\n[\n",
113 event.touchesLength, event.cancelable,
114 event.causesScrollingIfUncanceled, event.uniqueTouchEventId);
115 for (unsigned i = 0; i < event.touchesLength; ++i)
116 ApppendTouchPointDetails(event.touches[i], result);
117 result->append(" ]\n}");
120 bool CanCoalesce(const WebKeyboardEvent& event_to_coalesce,
121 const WebKeyboardEvent& event) {
122 return false;
125 void Coalesce(const WebKeyboardEvent& event_to_coalesce,
126 WebKeyboardEvent* event) {
127 DCHECK(CanCoalesce(event_to_coalesce, *event));
130 bool CanCoalesce(const WebMouseEvent& event_to_coalesce,
131 const WebMouseEvent& event) {
132 return event.type == event_to_coalesce.type &&
133 event.type == WebInputEvent::MouseMove;
136 void Coalesce(const WebMouseEvent& event_to_coalesce, WebMouseEvent* event) {
137 DCHECK(CanCoalesce(event_to_coalesce, *event));
138 // Accumulate movement deltas.
139 int x = event->movementX;
140 int y = event->movementY;
141 *event = event_to_coalesce;
142 event->movementX += x;
143 event->movementY += y;
146 bool CanCoalesce(const WebMouseWheelEvent& event_to_coalesce,
147 const WebMouseWheelEvent& event) {
148 return event.modifiers == event_to_coalesce.modifiers &&
149 event.scrollByPage == event_to_coalesce.scrollByPage &&
150 event.phase == event_to_coalesce.phase &&
151 event.momentumPhase == event_to_coalesce.momentumPhase &&
152 event.hasPreciseScrollingDeltas ==
153 event_to_coalesce.hasPreciseScrollingDeltas &&
154 event.canScroll == event_to_coalesce.canScroll;
157 float GetUnacceleratedDelta(float accelerated_delta, float acceleration_ratio) {
158 return accelerated_delta * acceleration_ratio;
161 float GetAccelerationRatio(float accelerated_delta, float unaccelerated_delta) {
162 if (unaccelerated_delta == 0.f || accelerated_delta == 0.f)
163 return 1.f;
164 return unaccelerated_delta / accelerated_delta;
167 void Coalesce(const WebMouseWheelEvent& event_to_coalesce,
168 WebMouseWheelEvent* event) {
169 DCHECK(CanCoalesce(event_to_coalesce, *event));
170 float unaccelerated_x =
171 GetUnacceleratedDelta(event->deltaX,
172 event->accelerationRatioX) +
173 GetUnacceleratedDelta(event_to_coalesce.deltaX,
174 event_to_coalesce.accelerationRatioX);
175 float unaccelerated_y =
176 GetUnacceleratedDelta(event->deltaY,
177 event->accelerationRatioY) +
178 GetUnacceleratedDelta(event_to_coalesce.deltaY,
179 event_to_coalesce.accelerationRatioY);
180 event->deltaX += event_to_coalesce.deltaX;
181 event->deltaY += event_to_coalesce.deltaY;
182 event->wheelTicksX += event_to_coalesce.wheelTicksX;
183 event->wheelTicksY += event_to_coalesce.wheelTicksY;
184 event->accelerationRatioX =
185 GetAccelerationRatio(event->deltaX, unaccelerated_x);
186 event->accelerationRatioY =
187 GetAccelerationRatio(event->deltaY, unaccelerated_y);
188 DCHECK_GE(event_to_coalesce.timeStampSeconds, event->timeStampSeconds);
189 event->timeStampSeconds = event_to_coalesce.timeStampSeconds;
192 // Returns |kInvalidTouchIndex| iff |event| lacks a touch with an ID of |id|.
193 int GetIndexOfTouchID(const WebTouchEvent& event, int id) {
194 for (unsigned i = 0; i < event.touchesLength; ++i) {
195 if (event.touches[i].id == id)
196 return i;
198 return kInvalidTouchIndex;
201 bool CanCoalesce(const WebTouchEvent& event_to_coalesce,
202 const WebTouchEvent& event) {
203 if (event.type != event_to_coalesce.type ||
204 event.type != WebInputEvent::TouchMove ||
205 event.modifiers != event_to_coalesce.modifiers ||
206 event.touchesLength != event_to_coalesce.touchesLength ||
207 event.touchesLength > WebTouchEvent::touchesLengthCap)
208 return false;
210 static_assert(WebTouchEvent::touchesLengthCap <= sizeof(int32_t) * 8U,
211 "suboptimal touchesLengthCap size");
212 // Ensure that we have a 1-to-1 mapping of pointer ids between touches.
213 std::bitset<WebTouchEvent::touchesLengthCap> unmatched_event_touches(
214 (1 << event.touchesLength) - 1);
215 for (unsigned i = 0; i < event_to_coalesce.touchesLength; ++i) {
216 int event_touch_index =
217 GetIndexOfTouchID(event, event_to_coalesce.touches[i].id);
218 if (event_touch_index == kInvalidTouchIndex)
219 return false;
220 if (!unmatched_event_touches[event_touch_index])
221 return false;
222 unmatched_event_touches[event_touch_index] = false;
224 return unmatched_event_touches.none();
227 void Coalesce(const WebTouchEvent& event_to_coalesce, WebTouchEvent* event) {
228 DCHECK(CanCoalesce(event_to_coalesce, *event));
229 // The WebTouchPoints include absolute position information. So it is
230 // sufficient to simply replace the previous event with the new event->
231 // However, it is necessary to make sure that all the points have the
232 // correct state, i.e. the touch-points that moved in the last event, but
233 // didn't change in the current event, will have Stationary state. It is
234 // necessary to change them back to Moved state.
235 WebTouchEvent old_event = *event;
236 *event = event_to_coalesce;
237 for (unsigned i = 0; i < event->touchesLength; ++i) {
238 int i_old = GetIndexOfTouchID(old_event, event->touches[i].id);
239 if (old_event.touches[i_old].state == blink::WebTouchPoint::StateMoved)
240 event->touches[i].state = blink::WebTouchPoint::StateMoved;
242 event->causesScrollingIfUncanceled |= old_event.causesScrollingIfUncanceled;
245 bool CanCoalesce(const WebGestureEvent& event_to_coalesce,
246 const WebGestureEvent& event) {
247 if (event.type != event_to_coalesce.type ||
248 event.sourceDevice != event_to_coalesce.sourceDevice ||
249 event.modifiers != event_to_coalesce.modifiers)
250 return false;
252 if (event.type == WebInputEvent::GestureScrollUpdate)
253 return true;
255 // GesturePinchUpdate scales can be combined only if they share a focal point,
256 // e.g., with double-tap drag zoom.
257 if (event.type == WebInputEvent::GesturePinchUpdate &&
258 event.x == event_to_coalesce.x &&
259 event.y == event_to_coalesce.y)
260 return true;
262 return false;
265 void Coalesce(const WebGestureEvent& event_to_coalesce,
266 WebGestureEvent* event) {
267 DCHECK(CanCoalesce(event_to_coalesce, *event));
268 if (event->type == WebInputEvent::GestureScrollUpdate) {
269 event->data.scrollUpdate.deltaX +=
270 event_to_coalesce.data.scrollUpdate.deltaX;
271 event->data.scrollUpdate.deltaY +=
272 event_to_coalesce.data.scrollUpdate.deltaY;
273 DCHECK_EQ(
274 event->data.scrollUpdate.previousUpdateInSequencePrevented,
275 event_to_coalesce.data.scrollUpdate.previousUpdateInSequencePrevented);
276 } else if (event->type == WebInputEvent::GesturePinchUpdate) {
277 event->data.pinchUpdate.scale *= event_to_coalesce.data.pinchUpdate.scale;
278 // Ensure the scale remains bounded above 0 and below Infinity so that
279 // we can reliably perform operations like log on the values.
280 if (event->data.pinchUpdate.scale < numeric_limits<float>::min())
281 event->data.pinchUpdate.scale = numeric_limits<float>::min();
282 else if (event->data.pinchUpdate.scale > numeric_limits<float>::max())
283 event->data.pinchUpdate.scale = numeric_limits<float>::max();
287 struct WebInputEventToString {
288 template <class EventType>
289 bool Execute(const WebInputEvent& event, std::string* result) const {
290 SStringPrintf(result, "%s (Time: %lf, Modifiers: %d)\n",
291 WebInputEventTraits::GetName(event.type),
292 event.timeStampSeconds,
293 event.modifiers);
294 const EventType& typed_event = static_cast<const EventType&>(event);
295 ApppendEventDetails(typed_event, result);
296 return true;
300 struct WebInputEventSize {
301 template <class EventType>
302 bool Execute(WebInputEvent::Type /* type */, size_t* type_size) const {
303 *type_size = sizeof(EventType);
304 return true;
308 struct WebInputEventClone {
309 template <class EventType>
310 bool Execute(const WebInputEvent& event,
311 ScopedWebInputEvent* scoped_event) const {
312 DCHECK_EQ(sizeof(EventType), event.size);
313 *scoped_event = ScopedWebInputEvent(
314 new EventType(static_cast<const EventType&>(event)));
315 return true;
319 struct WebInputEventDelete {
320 template <class EventType>
321 bool Execute(WebInputEvent* event, bool* /* dummy_var */) const {
322 if (!event)
323 return false;
324 DCHECK_EQ(sizeof(EventType), event->size);
325 delete static_cast<EventType*>(event);
326 return true;
330 struct WebInputEventCanCoalesce {
331 template <class EventType>
332 bool Execute(const WebInputEvent& event_to_coalesce,
333 const WebInputEvent* event) const {
334 if (event_to_coalesce.type != event->type)
335 return false;
336 DCHECK_EQ(sizeof(EventType), event->size);
337 DCHECK_EQ(sizeof(EventType), event_to_coalesce.size);
338 return CanCoalesce(static_cast<const EventType&>(event_to_coalesce),
339 *static_cast<const EventType*>(event));
343 struct WebInputEventCoalesce {
344 template <class EventType>
345 bool Execute(const WebInputEvent& event_to_coalesce,
346 WebInputEvent* event) const {
347 Coalesce(static_cast<const EventType&>(event_to_coalesce),
348 static_cast<EventType*>(event));
349 return true;
353 template <typename Operator, typename ArgIn, typename ArgOut>
354 bool Apply(Operator op,
355 WebInputEvent::Type type,
356 const ArgIn& arg_in,
357 ArgOut* arg_out) {
358 if (WebInputEvent::isMouseEventType(type))
359 return op.template Execute<WebMouseEvent>(arg_in, arg_out);
360 else if (type == WebInputEvent::MouseWheel)
361 return op.template Execute<WebMouseWheelEvent>(arg_in, arg_out);
362 else if (WebInputEvent::isKeyboardEventType(type))
363 return op.template Execute<WebKeyboardEvent>(arg_in, arg_out);
364 else if (WebInputEvent::isTouchEventType(type))
365 return op.template Execute<WebTouchEvent>(arg_in, arg_out);
366 else if (WebInputEvent::isGestureEventType(type))
367 return op.template Execute<WebGestureEvent>(arg_in, arg_out);
369 NOTREACHED() << "Unknown webkit event type " << type;
370 return false;
373 } // namespace
375 const char* WebInputEventTraits::GetName(WebInputEvent::Type type) {
376 #define CASE_TYPE(t) case WebInputEvent::t: return #t
377 switch(type) {
378 CASE_TYPE(Undefined);
379 CASE_TYPE(MouseDown);
380 CASE_TYPE(MouseUp);
381 CASE_TYPE(MouseMove);
382 CASE_TYPE(MouseEnter);
383 CASE_TYPE(MouseLeave);
384 CASE_TYPE(ContextMenu);
385 CASE_TYPE(MouseWheel);
386 CASE_TYPE(RawKeyDown);
387 CASE_TYPE(KeyDown);
388 CASE_TYPE(KeyUp);
389 CASE_TYPE(Char);
390 CASE_TYPE(GestureScrollBegin);
391 CASE_TYPE(GestureScrollEnd);
392 CASE_TYPE(GestureScrollUpdate);
393 CASE_TYPE(GestureFlingStart);
394 CASE_TYPE(GestureFlingCancel);
395 CASE_TYPE(GestureShowPress);
396 CASE_TYPE(GestureTap);
397 CASE_TYPE(GestureTapUnconfirmed);
398 CASE_TYPE(GestureTapDown);
399 CASE_TYPE(GestureTapCancel);
400 CASE_TYPE(GestureDoubleTap);
401 CASE_TYPE(GestureTwoFingerTap);
402 CASE_TYPE(GestureLongPress);
403 CASE_TYPE(GestureLongTap);
404 CASE_TYPE(GesturePinchBegin);
405 CASE_TYPE(GesturePinchEnd);
406 CASE_TYPE(GesturePinchUpdate);
407 CASE_TYPE(TouchStart);
408 CASE_TYPE(TouchMove);
409 CASE_TYPE(TouchEnd);
410 CASE_TYPE(TouchCancel);
411 default:
412 // Must include default to let blink::WebInputEvent add new event types
413 // before they're added here.
414 DLOG(WARNING) <<
415 "Unhandled WebInputEvent type in WebInputEventTraits::GetName.\n";
416 break;
418 #undef CASE_TYPE
419 return "";
422 std::string WebInputEventTraits::ToString(const WebInputEvent& event) {
423 std::string result;
424 Apply(WebInputEventToString(), event.type, event, &result);
425 return result;
428 size_t WebInputEventTraits::GetSize(WebInputEvent::Type type) {
429 size_t size = 0;
430 Apply(WebInputEventSize(), type, type, &size);
431 return size;
434 ScopedWebInputEvent WebInputEventTraits::Clone(const WebInputEvent& event) {
435 ScopedWebInputEvent scoped_event;
436 Apply(WebInputEventClone(), event.type, event, &scoped_event);
437 return scoped_event.Pass();
440 void WebInputEventTraits::Delete(WebInputEvent* event) {
441 if (!event)
442 return;
443 bool dummy_var = false;
444 Apply(WebInputEventDelete(), event->type, event, &dummy_var);
447 bool WebInputEventTraits::CanCoalesce(const WebInputEvent& event_to_coalesce,
448 const WebInputEvent& event) {
449 // Early out before casting.
450 if (event_to_coalesce.type != event.type)
451 return false;
452 return Apply(WebInputEventCanCoalesce(),
453 event.type,
454 event_to_coalesce,
455 &event);
458 void WebInputEventTraits::Coalesce(const WebInputEvent& event_to_coalesce,
459 WebInputEvent* event) {
460 DCHECK(event);
461 Apply(WebInputEventCoalesce(), event->type, event_to_coalesce, event);
464 bool WebInputEventTraits::WillReceiveAckFromRenderer(
465 const WebInputEvent& event) {
466 switch (event.type) {
467 case WebInputEvent::MouseDown:
468 case WebInputEvent::MouseUp:
469 case WebInputEvent::MouseEnter:
470 case WebInputEvent::MouseLeave:
471 case WebInputEvent::ContextMenu:
472 case WebInputEvent::GestureScrollBegin:
473 case WebInputEvent::GestureScrollEnd:
474 case WebInputEvent::GestureShowPress:
475 case WebInputEvent::GestureTapUnconfirmed:
476 case WebInputEvent::GestureTapDown:
477 case WebInputEvent::GestureTapCancel:
478 case WebInputEvent::GesturePinchBegin:
479 case WebInputEvent::GesturePinchEnd:
480 case WebInputEvent::TouchCancel:
481 return false;
482 case WebInputEvent::TouchStart:
483 case WebInputEvent::TouchEnd:
484 return static_cast<const WebTouchEvent&>(event).cancelable;
485 default:
486 return true;
490 uint32 WebInputEventTraits::GetUniqueTouchEventId(const WebInputEvent& event) {
491 if (WebInputEvent::isTouchEventType(event.type)) {
492 return static_cast<const WebTouchEvent&>(event).uniqueTouchEventId;
494 return 0U;
497 } // namespace content