Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: content/browser/renderer_host/input/touch_emulator.cc

Issue 138163016: [DevTools] Touch emulation in content. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "content/browser/renderer_host/input/touch_emulator.h"
6
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/public/common/content_client.h"
10 #include "content/public/common/content_switches.h"
11 #include "grit/content_resources.h"
12 #include "third_party/WebKit/public/platform/WebCursorInfo.h"
13 #include "ui/events/gesture_detection/gesture_config_helper.h"
14 #include "ui/gfx/image/image.h"
15
16 using blink::WebGestureEvent;
17 using blink::WebInputEvent;
18 using blink::WebKeyboardEvent;
19 using blink::WebMouseEvent;
20 using blink::WebMouseWheelEvent;
21 using blink::WebTouchEvent;
22 using blink::WebTouchPoint;
23
24 namespace content {
25
26 namespace {
27
28 ui::GestureProvider::Config GetGestureProviderConfig() {
29 // TODO(dgozman): Use different configs to emulate mobile/desktop as
30 // requested by renderer.
31 ui::GestureProvider::Config config = ui::DefaultGestureProviderConfig();
32 config.gesture_begin_end_types_enabled = false;
33 return config;
34 }
35
36 } // namespace
37
38 TouchEmulator::TouchEmulator(TouchEmulatorClient* client)
39 : client_(client),
40 gesture_provider_(GetGestureProviderConfig(), this) {
41 Init();
42 }
43
44 TouchEmulator::TouchEmulator(TouchEmulatorClient* client,
45 ui::GestureProvider::Config gesture_config)
46 : client_(client),
47 gesture_provider_(gesture_config, this) {
48 Init();
49 }
50
51 void TouchEmulator::Init() {
52 DCHECK(client_);
53 enabled_ = false;
54 mouse_pressed_ = false;
55 shift_pressed_ = false;
56 touch_active_ = false;
57 scroll_gesture_active_ = false;
58 fling_start_suppressed_ = false;
59 pinch_scale_ = 1.f;
60 pinch_gesture_active_ = false;
61
62 InitCursorFromResource(&touch_cursor_, IDR_DEVTOOLS_TOUCH_CURSOR_ICON);
63 InitCursorFromResource(&pinch_cursor_, IDR_DEVTOOLS_PINCH_CURSOR_ICON);
64
65 WebCursor::CursorInfo cursor_info;
66 cursor_info.type = blink::WebCursorInfo::TypePointer;
67 pointer_cursor_.InitFromCursorInfo(cursor_info);
68
69 // TODO(dgozman): Use synthetic secondary touch to support multi-touch.
70 gesture_provider_.SetMultiTouchSupportEnabled(false);
71 // TODO(dgozman): Enable double tap if requested by the renderer.
72 // TODO(dgozman): Don't break double-tap-based pinch with shift handling.
73 gesture_provider_.SetDoubleTapSupportForPlatformEnabled(false);
74 }
75
76 TouchEmulator::~TouchEmulator() {
77 if (enabled_) {
jdduke (slow) 2014/04/08 19:14:11 Actually, the safest thing here would be to do not
dgozman 2014/04/09 17:00:05 Sounds reasonable. Done.
78 // Try to cleanup gestures at least.
79 if (pinch_gesture_active_) {
80 pinch_event_.timeStampSeconds = base::Time::Now().ToDoubleT();
81 PinchEnd(pinch_event_);
82 }
83 if (scroll_gesture_active_) {
84 pinch_event_.timeStampSeconds = base::Time::Now().ToDoubleT();
85 ScrollEnd(pinch_event_);
86 }
87 }
88 }
89
90 void TouchEmulator::Enable() {
91 if (enabled_)
92 return;
93
94 enabled_ = true;
95 shift_pressed_ = false;
96 mouse_pressed_ = false;
97 touch_active_ = false;
98 scroll_gesture_active_ = false;
99 fling_start_suppressed_ = false;
100 pinch_gesture_active_ = false;
101 UpdateCursor();
102 }
103
104 void TouchEmulator::Disable() {
105 if (!enabled_)
106 return;
107
108 enabled_ = false;
109 UpdateCursor();
110 CancelTouch();
111 }
112
113 void TouchEmulator::InitCursorFromResource(WebCursor* cursor, int resource_id) {
114 gfx::Image& cursor_image =
115 content::GetContentClient()->GetNativeImageNamed(resource_id);
116 WebCursor::CursorInfo cursor_info;
117 cursor_info.type = blink::WebCursorInfo::TypeCustom;
118 // TODO(dgozman): Add HiDPI cursors.
119 cursor_info.image_scale_factor = 1.f;
120 cursor_info.custom_image = cursor_image.AsBitmap();
121 cursor_info.hotspot =
122 gfx::Point(cursor_image.Width() / 2, cursor_image.Height() / 2);
123 #if defined(OS_WIN)
124 cursor_info.external_handle = 0;
125 #endif
126
127 cursor->InitFromCursorInfo(cursor_info);
128 }
129
130 bool TouchEmulator::HandleMouseEvent(const WebMouseEvent& mouse_event) {
131 if (!enabled_)
132 return false;
133
134 if (mouse_event.button != WebMouseEvent::ButtonLeft)
135 return true;
136
137 if (mouse_event.type == WebInputEvent::MouseDown)
138 mouse_pressed_ = true;
139 if (mouse_event.type == WebInputEvent::MouseUp)
jdduke (slow) 2014/04/08 19:14:11 Nit: else if
dgozman 2014/04/09 17:00:05 Done.
140 mouse_pressed_ = false;
141
142 UpdateShiftPressed((mouse_event.modifiers & WebInputEvent::ShiftKey) != 0);
143
144 if (FillTouchEventAndPoint(mouse_event) &&
145 gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) {
146 client_->ForwardTouchEvent(touch_event_);
147 }
148
149 // Do not pass mouse events to the renderer.
150 return true;
151 }
152
153 bool TouchEmulator::HandleMouseWheelEvent(const WebMouseWheelEvent& event) {
154 if (!enabled_)
155 return false;
156
157 // No mouse wheel events for the renderer.
158 return true;
159 }
160
161 bool TouchEmulator::HandleKeyboardEvent(const WebKeyboardEvent& event) {
162 if (!enabled_)
163 return false;
164
165 if (!UpdateShiftPressed((event.modifiers & WebInputEvent::ShiftKey) != 0))
166 return false;
167
168 if (!mouse_pressed_)
169 return false;
170
171 // Note: The necessary pinch events will be lazily inserted by
172 // |OnGestureEvent| depending on the state of |shift_pressed_|, using the
173 // scroll stream as the event driver.
174 if (shift_pressed_) {
175 // TODO(dgozman): Add secondary touch point and set anchor.
176 } else {
177 // TODO(dgozman): Remove secondary touch point and anchor.
178 }
179
180 // Never block keyboard events.
181 return false;
182 }
183
184 bool TouchEmulator::HandleTouchEventAck(InputEventAckState ack_result) {
185 const bool event_consumed = ack_result == INPUT_EVENT_ACK_STATE_CONSUMED;
186 gesture_provider_.OnTouchEventAck(event_consumed);
187 // TODO(dgozman): Disable emulation when real touch events are available.
188 return true;
189 }
190
191 void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) {
192 WebGestureEvent gesture_event =
193 CreateWebGestureEventFromGestureEventData(gesture, 1);
194
195 // Convert scrolls to pinches while shift is pressed.
jdduke (slow) 2014/04/08 19:14:11 I wonder if this method would be easier to follow
dgozman 2014/04/09 17:00:05 Switch looks better. However, we still need differ
196 if (gesture_event.type == WebInputEvent::GestureScrollUpdate) {
197 if (InPinchGestureMode()) {
198 if (!pinch_gesture_active_)
199 PinchBegin(gesture_event);
200 else
201 PinchUpdate(gesture_event);
202 return;
203 } else {
204 if (pinch_gesture_active_)
205 PinchEnd(gesture_event);
206 }
207 }
208
209 if ((gesture_event.type == WebInputEvent::GestureScrollEnd ||
210 gesture_event.type == WebInputEvent::GestureFlingStart)) {
211 scroll_gesture_active_ = false;
212 // PinchEnd must always precede ScrollEnd/FlingStart.
213 if (pinch_gesture_active_)
214 PinchEnd(gesture_event);
215 }
216
217 if (gesture_event.type == WebInputEvent::GestureFlingStart &&
218 InPinchGestureMode()) {
219 // No fling in pinch mode. Forward scroll end instead of fling start.
220 fling_start_suppressed_ = true;
221 ScrollEnd(gesture_event);
222 } else {
223 bool suppress_fling_cancel =
224 gesture_event.type == WebInputEvent::GestureFlingCancel &&
225 fling_start_suppressed_;
226 if (!suppress_fling_cancel)
227 client_->ForwardGestureEvent(gesture_event);
228 if (gesture_event.type == WebInputEvent::GestureFlingStart ||
229 gesture_event.type == WebInputEvent::GestureFlingCancel)
230 fling_start_suppressed_ = false;
231 // TODO(dgozman): sometimes we hit a race, where keyboard event cancels
232 // fling in browser process, but does not in renderer process.
233 }
234
235 if (gesture_event.type == WebInputEvent::GestureScrollBegin) {
236 scroll_gesture_active_ = true;
237 // PinchBegin must always follow ScrollBegin.
238 if (InPinchGestureMode())
239 PinchBegin(gesture_event);
240 }
241 }
242
243 void TouchEmulator::CancelTouch() {
244 if (!touch_active_)
245 return;
246
247 touch_event_.timeStampSeconds = base::Time::Now().ToDoubleT();
248 touch_event_.type = WebInputEvent::TouchCancel;
249 touch_event_.touches[0].state = WebTouchPoint::StateCancelled;
250 touch_active_ = false;
251 if (gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_)))
252 client_->ForwardTouchEvent(touch_event_);
253 }
254
255 void TouchEmulator::UpdateCursor() {
256 if (!enabled_)
257 client_->SetCursor(pointer_cursor_);
258 else
259 client_->SetCursor(InPinchGestureMode() ? pinch_cursor_ : touch_cursor_);
260 }
261
262 bool TouchEmulator::UpdateShiftPressed(bool shift_pressed) {
263 if (shift_pressed_ == shift_pressed)
264 return false;
265 shift_pressed_ = shift_pressed;
266 UpdateCursor();
267 return true;
268 }
269
270 void TouchEmulator::PinchBegin(const WebGestureEvent& event) {
271 DCHECK(InPinchGestureMode());
272 DCHECK(!pinch_gesture_active_);
273 pinch_gesture_active_ = true;
274 pinch_anchor_ = gfx::Point(event.x, event.y);
275 pinch_scale_ = 1.f;
276 FillPinchEvent(event);
277 pinch_event_.type = WebInputEvent::GesturePinchBegin;
278 client_->ForwardGestureEvent(pinch_event_);
279 }
280
281 void TouchEmulator::PinchUpdate(const WebGestureEvent& event) {
282 DCHECK(pinch_gesture_active_);
283 int dy = pinch_anchor_.y() - event.y;
284 float scale = exp(dy * 0.002f);
285 FillPinchEvent(event);
286 pinch_event_.type = WebInputEvent::GesturePinchUpdate;
287 pinch_event_.data.pinchUpdate.scale = scale / pinch_scale_;
288 client_->ForwardGestureEvent(pinch_event_);
289 pinch_scale_ = scale;
290 }
291
292 void TouchEmulator::PinchEnd(const WebGestureEvent& event) {
293 DCHECK(pinch_gesture_active_);
294 pinch_gesture_active_ = false;
295 FillPinchEvent(event);
296 pinch_event_.type = WebInputEvent::GesturePinchEnd;
297 client_->ForwardGestureEvent(pinch_event_);
298 }
299
300 void TouchEmulator::FillPinchEvent(const WebInputEvent& event) {
301 pinch_event_.timeStampSeconds = event.timeStampSeconds;
302 pinch_event_.modifiers = event.modifiers;
303 pinch_event_.sourceDevice = blink::WebGestureEvent::Touchscreen;
304 pinch_event_.x = pinch_anchor_.x();
305 pinch_event_.y = pinch_anchor_.y();
306 }
307
308 void TouchEmulator::ScrollEnd(const WebGestureEvent& event) {
309 WebGestureEvent scroll_event;
310 scroll_event.timeStampSeconds = event.timeStampSeconds;
311 scroll_event.modifiers = event.modifiers;
312 scroll_event.sourceDevice = blink::WebGestureEvent::Touchscreen;
313 scroll_event.type = WebInputEvent::GestureScrollEnd;
314 client_->ForwardGestureEvent(scroll_event);
315 }
316
317 bool TouchEmulator::FillTouchEventAndPoint(const WebMouseEvent& mouse_event) {
318 if (mouse_event.type != WebInputEvent::MouseDown &&
319 mouse_event.type != WebInputEvent::MouseMove &&
320 mouse_event.type != WebInputEvent::MouseUp) {
321 return false;
322 }
323
324 touch_event_.touchesLength = 1;
325 touch_event_.timeStampSeconds = mouse_event.timeStampSeconds;
326 touch_event_.modifiers = mouse_event.modifiers;
327
328 WebTouchPoint& point = touch_event_.touches[0];
329 point.id = 0;
330 point.radiusX = point.radiusY = 1.f;
331 point.force = 1.f;
332 point.rotationAngle = 0.f;
333 point.position.x = mouse_event.x;
334 point.screenPosition.x = mouse_event.globalX;
335 point.position.y = mouse_event.y;
336 point.screenPosition.y = mouse_event.globalY;
337
338 switch (mouse_event.type) {
339 case WebInputEvent::MouseDown:
340 touch_event_.type = WebInputEvent::TouchStart;
341 touch_active_ = true;
342 point.state = WebTouchPoint::StatePressed;
343 break;
344 case WebInputEvent::MouseMove:
345 touch_event_.type = WebInputEvent::TouchMove;
346 point.state = WebTouchPoint::StateMoved;
347 break;
348 case WebInputEvent::MouseUp:
349 touch_event_.type = WebInputEvent::TouchEnd;
350 touch_active_ = false;
351 point.state = WebTouchPoint::StateReleased;
352 break;
353 default:
354 NOTREACHED();
355 }
356 return true;
357 }
358
359 bool TouchEmulator::InPinchGestureMode() const {
360 return shift_pressed_;
361 }
362
363 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698