Add ICU message format support
[chromium-blink-merge.git] / content / browser / renderer_host / input / touch_emulator.cc
blob90a6bf70e88a2a95d30631315beb3467af8fd824
1 // Copyright 2014 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/browser/renderer_host/input/touch_emulator.h"
7 #include "content/browser/renderer_host/input/motion_event_web.h"
8 #include "content/browser/renderer_host/input/web_input_event_util.h"
9 #include "content/common/input/web_touch_event_traits.h"
10 #include "content/grit/content_resources.h"
11 #include "content/public/common/content_client.h"
12 #include "content/public/common/content_switches.h"
13 #include "third_party/WebKit/public/platform/WebCursorInfo.h"
14 #include "ui/events/base_event_utils.h"
15 #include "ui/events/blink/blink_event_util.h"
16 #include "ui/events/gesture_detection/gesture_provider_config_helper.h"
17 #include "ui/gfx/image/image.h"
19 using blink::WebGestureEvent;
20 using blink::WebInputEvent;
21 using blink::WebKeyboardEvent;
22 using blink::WebMouseEvent;
23 using blink::WebMouseWheelEvent;
24 using blink::WebTouchEvent;
25 using blink::WebTouchPoint;
27 namespace content {
29 namespace {
31 ui::GestureProvider::Config GetEmulatorGestureProviderConfig(
32 ui::GestureProviderConfigType config_type) {
33 ui::GestureProvider::Config config =
34 ui::GetGestureProviderConfig(config_type);
35 config.gesture_begin_end_types_enabled = false;
36 config.gesture_detector_config.swipe_enabled = false;
37 config.gesture_detector_config.two_finger_tap_enabled = false;
38 return config;
41 // Time between two consecutive mouse moves, during which second mouse move
42 // is not converted to touch.
43 const double kMouseMoveDropIntervalSeconds = 5.f / 1000;
45 } // namespace
47 TouchEmulator::TouchEmulator(TouchEmulatorClient* client,
48 float device_scale_factor)
49 : client_(client),
50 gesture_provider_config_type_(
51 ui::GestureProviderConfigType::CURRENT_PLATFORM),
52 double_tap_enabled_(true),
53 emulated_stream_active_sequence_count_(0),
54 native_stream_active_sequence_count_(0) {
55 DCHECK(client_);
56 ResetState();
58 bool use_2x = device_scale_factor > 1.5f;
59 float cursor_scale_factor = use_2x ? 2.f : 1.f;
60 cursor_size_ = InitCursorFromResource(&touch_cursor_,
61 cursor_scale_factor,
62 use_2x ? IDR_DEVTOOLS_TOUCH_CURSOR_ICON_2X :
63 IDR_DEVTOOLS_TOUCH_CURSOR_ICON);
64 InitCursorFromResource(&pinch_cursor_,
65 cursor_scale_factor,
66 use_2x ? IDR_DEVTOOLS_PINCH_CURSOR_ICON_2X :
67 IDR_DEVTOOLS_PINCH_CURSOR_ICON);
69 WebCursor::CursorInfo cursor_info;
70 cursor_info.type = blink::WebCursorInfo::TypePointer;
71 pointer_cursor_.InitFromCursorInfo(cursor_info);
74 TouchEmulator::~TouchEmulator() {
75 // We cannot cleanup properly in destructor, as we need roundtrip to the
76 // renderer for ack. Instead, the owner should call Disable, and only
77 // destroy this object when renderer is dead.
80 void TouchEmulator::ResetState() {
81 last_mouse_event_was_move_ = false;
82 last_mouse_move_timestamp_ = 0;
83 mouse_pressed_ = false;
84 shift_pressed_ = false;
85 suppress_next_fling_cancel_ = false;
86 pinch_scale_ = 1.f;
87 pinch_gesture_active_ = false;
90 void TouchEmulator::Enable(ui::GestureProviderConfigType config_type) {
91 if (!gesture_provider_ || gesture_provider_config_type_ != config_type) {
92 gesture_provider_config_type_ = config_type;
93 gesture_provider_.reset(new ui::FilteredGestureProvider(
94 GetEmulatorGestureProviderConfig(config_type), this));
95 // TODO(dgozman): Use synthetic secondary touch to support multi-touch.
96 gesture_provider_->SetMultiTouchZoomSupportEnabled(false);
97 gesture_provider_->SetDoubleTapSupportForPageEnabled(double_tap_enabled_);
99 UpdateCursor();
102 void TouchEmulator::Disable() {
103 if (!enabled())
104 return;
106 CancelTouch();
107 gesture_provider_.reset();
108 UpdateCursor();
109 ResetState();
112 void TouchEmulator::SetDoubleTapSupportForPageEnabled(bool enabled) {
113 double_tap_enabled_ = enabled;
114 if (gesture_provider_)
115 gesture_provider_->SetDoubleTapSupportForPageEnabled(enabled);
118 gfx::SizeF TouchEmulator::InitCursorFromResource(
119 WebCursor* cursor, float scale, int resource_id) {
120 gfx::Image& cursor_image =
121 content::GetContentClient()->GetNativeImageNamed(resource_id);
122 WebCursor::CursorInfo cursor_info;
123 cursor_info.type = blink::WebCursorInfo::TypeCustom;
124 cursor_info.image_scale_factor = scale;
125 cursor_info.custom_image = cursor_image.AsBitmap();
126 cursor_info.hotspot =
127 gfx::Point(cursor_image.Width() / 2, cursor_image.Height() / 2);
128 #if defined(OS_WIN)
129 cursor_info.external_handle = 0;
130 #endif
132 cursor->InitFromCursorInfo(cursor_info);
133 return gfx::ScaleSize(cursor_image.Size(), 1.f / scale);
136 bool TouchEmulator::HandleMouseEvent(const WebMouseEvent& mouse_event) {
137 if (!enabled())
138 return false;
140 if (mouse_event.button == WebMouseEvent::ButtonRight &&
141 mouse_event.type == WebInputEvent::MouseDown) {
142 client_->ShowContextMenuAtPoint(gfx::Point(mouse_event.x, mouse_event.y));
145 if (mouse_event.button != WebMouseEvent::ButtonLeft)
146 return true;
148 if (mouse_event.type == WebInputEvent::MouseMove) {
149 if (last_mouse_event_was_move_ &&
150 mouse_event.timeStampSeconds < last_mouse_move_timestamp_ +
151 kMouseMoveDropIntervalSeconds)
152 return true;
154 last_mouse_event_was_move_ = true;
155 last_mouse_move_timestamp_ = mouse_event.timeStampSeconds;
156 } else {
157 last_mouse_event_was_move_ = false;
160 if (mouse_event.type == WebInputEvent::MouseDown)
161 mouse_pressed_ = true;
162 else if (mouse_event.type == WebInputEvent::MouseUp)
163 mouse_pressed_ = false;
165 UpdateShiftPressed((mouse_event.modifiers & WebInputEvent::ShiftKey) != 0);
167 if (mouse_event.type != WebInputEvent::MouseDown &&
168 mouse_event.type != WebInputEvent::MouseMove &&
169 mouse_event.type != WebInputEvent::MouseUp) {
170 return true;
173 FillTouchEventAndPoint(mouse_event);
174 HandleEmulatedTouchEvent(touch_event_);
176 // Do not pass mouse events to the renderer.
177 return true;
180 bool TouchEmulator::HandleMouseWheelEvent(const WebMouseWheelEvent& event) {
181 if (!enabled())
182 return false;
184 // Send mouse wheel for easy scrolling when there is no active touch.
185 return emulated_stream_active_sequence_count_ > 0;
188 bool TouchEmulator::HandleKeyboardEvent(const WebKeyboardEvent& event) {
189 if (!enabled())
190 return false;
192 if (!UpdateShiftPressed((event.modifiers & WebInputEvent::ShiftKey) != 0))
193 return false;
195 if (!mouse_pressed_)
196 return false;
198 // Note: The necessary pinch events will be lazily inserted by
199 // |OnGestureEvent| depending on the state of |shift_pressed_|, using the
200 // scroll stream as the event driver.
201 if (shift_pressed_) {
202 // TODO(dgozman): Add secondary touch point and set anchor.
203 } else {
204 // TODO(dgozman): Remove secondary touch point and anchor.
207 // Never block keyboard events.
208 return false;
211 bool TouchEmulator::HandleTouchEvent(const blink::WebTouchEvent& event) {
212 // Block native event when emulated touch stream is active.
213 if (emulated_stream_active_sequence_count_)
214 return true;
216 bool is_sequence_start = WebTouchEventTraits::IsTouchSequenceStart(event);
217 // Do not allow middle-sequence event to pass through, if start was blocked.
218 if (!native_stream_active_sequence_count_ && !is_sequence_start)
219 return true;
221 if (is_sequence_start)
222 native_stream_active_sequence_count_++;
223 return false;
226 void TouchEmulator::HandleEmulatedTouchEvent(blink::WebTouchEvent event) {
227 DCHECK(gesture_provider_);
228 event.uniqueTouchEventId = ui::GetNextTouchEventId();
229 auto result = gesture_provider_->OnTouchEvent(MotionEventWeb(event));
230 if (!result.succeeded)
231 return;
233 const bool event_consumed = true;
234 // Block emulated event when emulated native stream is active.
235 if (native_stream_active_sequence_count_) {
236 gesture_provider_->OnTouchEventAck(event.uniqueTouchEventId,
237 event_consumed);
238 return;
241 bool is_sequence_start = WebTouchEventTraits::IsTouchSequenceStart(event);
242 // Do not allow middle-sequence event to pass through, if start was blocked.
243 if (!emulated_stream_active_sequence_count_ && !is_sequence_start) {
244 gesture_provider_->OnTouchEventAck(event.uniqueTouchEventId,
245 event_consumed);
246 return;
249 if (is_sequence_start)
250 emulated_stream_active_sequence_count_++;
252 event.causesScrollingIfUncanceled = result.did_generate_scroll;
253 client_->ForwardEmulatedTouchEvent(event);
256 bool TouchEmulator::HandleTouchEventAck(
257 const blink::WebTouchEvent& event, InputEventAckState ack_result) {
258 bool is_sequence_end = WebTouchEventTraits::IsTouchSequenceEnd(event);
259 if (emulated_stream_active_sequence_count_) {
260 if (is_sequence_end)
261 emulated_stream_active_sequence_count_--;
263 const bool event_consumed = ack_result == INPUT_EVENT_ACK_STATE_CONSUMED;
264 if (gesture_provider_)
265 gesture_provider_->OnTouchEventAck(event.uniqueTouchEventId,
266 event_consumed);
267 return true;
270 // We may have not seen native touch sequence start (when created in the
271 // middle of a sequence), so don't decrement sequence count below zero.
272 if (is_sequence_end && native_stream_active_sequence_count_)
273 native_stream_active_sequence_count_--;
274 return false;
277 void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) {
278 WebGestureEvent gesture_event =
279 ui::CreateWebGestureEventFromGestureEventData(gesture);
281 switch (gesture_event.type) {
282 case WebInputEvent::Undefined:
283 NOTREACHED() << "Undefined WebInputEvent type";
284 // Bail without sending the junk event to the client.
285 return;
287 case WebInputEvent::GestureScrollBegin:
288 client_->ForwardGestureEvent(gesture_event);
289 // PinchBegin must always follow ScrollBegin.
290 if (InPinchGestureMode())
291 PinchBegin(gesture_event);
292 break;
294 case WebInputEvent::GestureScrollUpdate:
295 if (InPinchGestureMode()) {
296 // Convert scrolls to pinches while shift is pressed.
297 if (!pinch_gesture_active_)
298 PinchBegin(gesture_event);
299 else
300 PinchUpdate(gesture_event);
301 } else {
302 // Pass scroll update further. If shift was released, end the pinch.
303 if (pinch_gesture_active_)
304 PinchEnd(gesture_event);
305 client_->ForwardGestureEvent(gesture_event);
307 break;
309 case WebInputEvent::GestureScrollEnd:
310 // PinchEnd must precede ScrollEnd.
311 if (pinch_gesture_active_)
312 PinchEnd(gesture_event);
313 client_->ForwardGestureEvent(gesture_event);
314 break;
316 case WebInputEvent::GestureFlingStart:
317 // PinchEnd must precede FlingStart.
318 if (pinch_gesture_active_)
319 PinchEnd(gesture_event);
320 if (InPinchGestureMode()) {
321 // No fling in pinch mode. Forward scroll end instead of fling start.
322 suppress_next_fling_cancel_ = true;
323 ScrollEnd(gesture_event);
324 } else {
325 suppress_next_fling_cancel_ = false;
326 client_->ForwardGestureEvent(gesture_event);
328 break;
330 case WebInputEvent::GestureFlingCancel:
331 // If fling start was suppressed, we should not send fling cancel either.
332 if (!suppress_next_fling_cancel_)
333 client_->ForwardGestureEvent(gesture_event);
334 suppress_next_fling_cancel_ = false;
335 break;
337 default:
338 // Everything else goes through.
339 client_->ForwardGestureEvent(gesture_event);
343 void TouchEmulator::CancelTouch() {
344 if (!emulated_stream_active_sequence_count_ || !enabled())
345 return;
347 WebTouchEventTraits::ResetTypeAndTouchStates(
348 WebInputEvent::TouchCancel,
349 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(),
350 &touch_event_);
351 DCHECK(gesture_provider_);
352 if (gesture_provider_->GetCurrentDownEvent())
353 HandleEmulatedTouchEvent(touch_event_);
356 void TouchEmulator::UpdateCursor() {
357 if (!enabled())
358 client_->SetCursor(pointer_cursor_);
359 else
360 client_->SetCursor(InPinchGestureMode() ? pinch_cursor_ : touch_cursor_);
363 bool TouchEmulator::UpdateShiftPressed(bool shift_pressed) {
364 if (shift_pressed_ == shift_pressed)
365 return false;
366 shift_pressed_ = shift_pressed;
367 UpdateCursor();
368 return true;
371 void TouchEmulator::PinchBegin(const WebGestureEvent& event) {
372 DCHECK(InPinchGestureMode());
373 DCHECK(!pinch_gesture_active_);
374 pinch_gesture_active_ = true;
375 pinch_anchor_ = gfx::Point(event.x, event.y);
376 pinch_scale_ = 1.f;
377 FillPinchEvent(event);
378 pinch_event_.type = WebInputEvent::GesturePinchBegin;
379 client_->ForwardGestureEvent(pinch_event_);
382 void TouchEmulator::PinchUpdate(const WebGestureEvent& event) {
383 DCHECK(pinch_gesture_active_);
384 int dy = pinch_anchor_.y() - event.y;
385 float scale = exp(dy * 0.002f);
386 FillPinchEvent(event);
387 pinch_event_.type = WebInputEvent::GesturePinchUpdate;
388 pinch_event_.data.pinchUpdate.scale = scale / pinch_scale_;
389 client_->ForwardGestureEvent(pinch_event_);
390 pinch_scale_ = scale;
393 void TouchEmulator::PinchEnd(const WebGestureEvent& event) {
394 DCHECK(pinch_gesture_active_);
395 pinch_gesture_active_ = false;
396 FillPinchEvent(event);
397 pinch_event_.type = WebInputEvent::GesturePinchEnd;
398 client_->ForwardGestureEvent(pinch_event_);
401 void TouchEmulator::FillPinchEvent(const WebInputEvent& event) {
402 pinch_event_.timeStampSeconds = event.timeStampSeconds;
403 pinch_event_.modifiers = event.modifiers;
404 pinch_event_.sourceDevice = blink::WebGestureDeviceTouchscreen;
405 pinch_event_.x = pinch_anchor_.x();
406 pinch_event_.y = pinch_anchor_.y();
409 void TouchEmulator::ScrollEnd(const WebGestureEvent& event) {
410 WebGestureEvent scroll_event;
411 scroll_event.timeStampSeconds = event.timeStampSeconds;
412 scroll_event.modifiers = event.modifiers;
413 scroll_event.sourceDevice = blink::WebGestureDeviceTouchscreen;
414 scroll_event.type = WebInputEvent::GestureScrollEnd;
415 client_->ForwardGestureEvent(scroll_event);
418 void TouchEmulator::FillTouchEventAndPoint(const WebMouseEvent& mouse_event) {
419 WebInputEvent::Type eventType;
420 switch (mouse_event.type) {
421 case WebInputEvent::MouseDown:
422 eventType = WebInputEvent::TouchStart;
423 break;
424 case WebInputEvent::MouseMove:
425 eventType = WebInputEvent::TouchMove;
426 break;
427 case WebInputEvent::MouseUp:
428 eventType = WebInputEvent::TouchEnd;
429 break;
430 default:
431 eventType = WebInputEvent::Undefined;
432 NOTREACHED() << "Invalid event for touch emulation: " << mouse_event.type;
434 touch_event_.touchesLength = 1;
435 touch_event_.modifiers = mouse_event.modifiers;
436 WebTouchEventTraits::ResetTypeAndTouchStates(
437 eventType, mouse_event.timeStampSeconds, &touch_event_);
439 WebTouchPoint& point = touch_event_.touches[0];
440 point.id = 0;
441 point.radiusX = 0.5f * cursor_size_.width();
442 point.radiusY = 0.5f * cursor_size_.height();
443 point.force = 1.f;
444 point.rotationAngle = 0.f;
445 point.position.x = mouse_event.x;
446 point.screenPosition.x = mouse_event.globalX;
447 point.position.y = mouse_event.y;
448 point.screenPosition.y = mouse_event.globalY;
451 bool TouchEmulator::InPinchGestureMode() const {
452 return shift_pressed_;
455 } // namespace content