1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
content / browser / renderer_host / input / synthetic_gesture_target_aura.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/input/synthetic_gesture_target_aura.h"
#include <stddef.h>
#include <memory>
#include <vector>
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_view_aura.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/compositor/compositor.h"
#include "ui/events/blink/blink_event_util.h"
#include "ui/events/event_sink.h"
#include "ui/events/event_utils.h"
#include "ui/events/gesture_detection/gesture_configuration.h"
using blink::WebTouchEvent;
using blink::WebMouseWheelEvent;
namespace content {
namespace {
int WebEventButtonToUIEventButtonFlags(blink::WebMouseEvent::Button button) {
if (button == blink::WebMouseEvent::Button::kLeft)
return ui::EF_LEFT_MOUSE_BUTTON;
if (button == blink::WebMouseEvent::Button::kMiddle)
return ui::EF_MIDDLE_MOUSE_BUTTON;
if (button == blink::WebMouseEvent::Button::kRight)
return ui::EF_RIGHT_MOUSE_BUTTON;
if (button == blink::WebMouseEvent::Button::kBack)
return ui::EF_BACK_MOUSE_BUTTON;
if (button == blink::WebMouseEvent::Button::kForward)
return ui::EF_FORWARD_MOUSE_BUTTON;
return 0;
}
enum class TouchEventCoordinateSystem { SCREEN_COORDINATES, LOCAL_COORDINATES };
ui::EventType WebTouchPointStateToEventType(blink::WebTouchPoint::State state) {
switch (state) {
case blink::WebTouchPoint::State::kStateReleased:
return ui::EventType::kTouchReleased;
case blink::WebTouchPoint::State::kStatePressed:
return ui::EventType::kTouchPressed;
case blink::WebTouchPoint::State::kStateMoved:
return ui::EventType::kTouchMoved;
case blink::WebTouchPoint::State::kStateCancelled:
return ui::EventType::kTouchCancelled;
case blink::WebTouchPoint::State::kStateStationary:
case blink::WebTouchPoint::State::kStateUndefined:
return ui::EventType::kUnknown;
}
}
// Creates a list of ui::TouchEvents out of a single WebTouchEvent.
// A WebTouchEvent can contain information about a number of WebTouchPoints,
// whereas a ui::TouchEvent contains information about a single touch-point. So
// it is possible to create more than one ui::TouchEvents out of a single
// WebTouchEvent. All the ui::TouchEvent in the list will carry the same
// LatencyInfo the WebTouchEvent carries.
// |coordinate_system| specifies which fields to use for the co-ordinates,
// WebTouchPoint.position or WebTouchPoint.screenPosition. Is's up to the
// caller to do any co-ordinate system mapping (typically to get them into
// the Aura EventDispatcher co-ordinate system).
bool MakeUITouchEventsFromWebTouchEvents(
const input::TouchEventWithLatencyInfo& touch_with_latency,
std::vector<std::unique_ptr<ui::TouchEvent>>* list,
TouchEventCoordinateSystem coordinate_system) {
const blink::WebTouchEvent& touch = touch_with_latency.event;
ui::EventType type = ui::EventType::kUnknown;
switch (touch.GetType()) {
case blink::WebInputEvent::Type::kTouchStart:
type = ui::EventType::kTouchPressed;
break;
case blink::WebInputEvent::Type::kTouchEnd:
type = ui::EventType::kTouchReleased;
break;
case blink::WebInputEvent::Type::kTouchMove:
type = ui::EventType::kTouchMoved;
break;
case blink::WebInputEvent::Type::kTouchCancel:
type = ui::EventType::kTouchCancelled;
break;
default:
NOTREACHED_IN_MIGRATION();
return false;
}
int flags = ui::WebEventModifiersToEventFlags(touch.GetModifiers());
base::TimeTicks timestamp = touch.TimeStamp();
for (unsigned i = 0; i < touch.touches_length; ++i) {
const blink::WebTouchPoint& point = touch.touches[i];
if (WebTouchPointStateToEventType(point.state) != type) {
continue;
}
// ui events start in the co-ordinate space of the EventDispatcher.
gfx::PointF location;
if (coordinate_system == TouchEventCoordinateSystem::LOCAL_COORDINATES) {
location = point.PositionInWidget();
} else {
location = point.PositionInScreen();
}
auto uievent = std::make_unique<ui::TouchEvent>(
type, gfx::Point(), timestamp,
ui::PointerDetails(ui::EventPointerType::kTouch, point.id,
point.radius_x, point.radius_y, point.force,
point.rotation_angle, point.tilt_x, point.tilt_y,
point.tangential_pressure),
flags);
uievent->set_location_f(location);
uievent->set_root_location_f(location);
uievent->set_latency(touch_with_latency.latency);
list->push_back(std::move(uievent));
}
return true;
}
} // namespace
SyntheticGestureTargetAura::SyntheticGestureTargetAura(
RenderWidgetHostImpl* host)
: SyntheticGestureTargetBase(host) {
device_scale_factor_ = host->GetDeviceScaleFactor();
observed_compositor_ = GetView()->GetCompositor();
if (observed_compositor_) {
observed_compositor_->AddSimpleBeginFrameObserver(this);
}
}
SyntheticGestureTargetAura::~SyntheticGestureTargetAura() {
if (observed_compositor_) {
observed_compositor_->RemoveSimpleBeginFrameObserver(this);
observed_compositor_ = nullptr;
}
}
void SyntheticGestureTargetAura::DispatchWebTouchEventToPlatform(
const WebTouchEvent& web_touch,
const ui::LatencyInfo& latency_info) {
input::TouchEventWithLatencyInfo touch_with_latency(web_touch, latency_info);
for (size_t i = 0; i < touch_with_latency.event.touches_length; i++) {
touch_with_latency.event.touches[i].radius_x *= device_scale_factor_;
touch_with_latency.event.touches[i].radius_y *= device_scale_factor_;
}
std::vector<std::unique_ptr<ui::TouchEvent>> events;
bool conversion_success = MakeUITouchEventsFromWebTouchEvents(
touch_with_latency, &events,
TouchEventCoordinateSystem::LOCAL_COORDINATES);
DCHECK(conversion_success);
aura::Window* window = GetWindow();
aura::WindowTreeHost* host = window->GetHost();
for (auto& event : events) {
// Synthetic events from devtools debugger need to be dispatched explicitly
// to the target window. Otherwise they will end up in the active tab
// which might be different from the target.
if (web_touch.GetModifiers() & blink::WebInputEvent::kFromDebugger) {
window->delegate()->OnEvent(event.get());
} else {
event->ConvertLocationToTarget(window, host->window());
ui::EventDispatchDetails details =
event_injector_.Inject(host, event.get());
if (details.dispatcher_destroyed)
break;
}
}
}
void SyntheticGestureTargetAura::DispatchWebMouseWheelEventToPlatform(
const blink::WebMouseWheelEvent& web_wheel,
const ui::LatencyInfo&) {
if (web_wheel.phase == blink::WebMouseWheelEvent::kPhaseEnded) {
// Send the pending wheel end event immediately.
GetView()->GetMouseWheelPhaseHandler()->DispatchPendingWheelEndEvent();
return;
}
base::TimeTicks timestamp = web_wheel.TimeStamp();
int modifiers = ui::WebEventModifiersToEventFlags(web_wheel.GetModifiers());
if (web_wheel.delta_units == ui::ScrollGranularity::kScrollByPrecisePixel) {
modifiers |= ui::EF_PRECISION_SCROLLING_DELTA;
} else if (web_wheel.delta_units == ui::ScrollGranularity::kScrollByPage) {
modifiers |= ui::EF_SCROLL_BY_PAGE;
}
float delta_x = web_wheel.delta_x + wheel_precision_x_;
float delta_y = web_wheel.delta_y + wheel_precision_y_;
ui::MouseWheelEvent wheel_event(
gfx::Vector2d(delta_x, delta_y), web_wheel.PositionInWidget(),
web_wheel.PositionInWidget(), timestamp, modifiers, ui::EF_NONE);
wheel_precision_x_ = delta_x - wheel_event.x_offset();
wheel_precision_y_ = delta_y - wheel_event.y_offset();
aura::Window* window = GetWindow();
// Synthetic events from devtools debugger need to be dispatched explicitly
// to the target window. Otherwise they will end up in the active tab
// which might be different from the target.
if (web_wheel.GetModifiers() & blink::WebInputEvent::kFromDebugger) {
window->delegate()->OnEvent(&wheel_event);
} else {
wheel_event.ConvertLocationToTarget(window, window->GetRootWindow());
ui::EventDispatchDetails details =
event_injector_.Inject(window->GetHost(), &wheel_event);
if (details.dispatcher_destroyed)
return;
}
}
void SyntheticGestureTargetAura::DispatchWebGestureEventToPlatform(
const blink::WebGestureEvent& web_gesture,
const ui::LatencyInfo& latency_info) {
DCHECK(blink::WebInputEvent::IsPinchGestureEventType(web_gesture.GetType()) ||
blink::WebInputEvent::IsFlingGestureEventType(web_gesture.GetType()));
ui::EventType event_type = web_gesture.GetTypeAsUiEventType();
int flags = ui::WebEventModifiersToEventFlags(web_gesture.GetModifiers());
aura::Window* window = GetWindow();
if (blink::WebInputEvent::IsPinchGestureEventType(web_gesture.GetType())) {
ui::GestureEventDetails pinch_details(event_type);
pinch_details.set_device_type(ui::GestureDeviceType::DEVICE_TOUCHPAD);
if (event_type == ui::EventType::kGesturePinchUpdate) {
pinch_details.set_scale(web_gesture.data.pinch_update.scale);
}
ui::GestureEvent pinch_event(web_gesture.PositionInWidget().x(),
web_gesture.PositionInWidget().y(), flags,
web_gesture.TimeStamp(), pinch_details);
// Synthetic events from devtools debugger need to be dispatched explicitly
// to the target window. Otherwise they will end up in the active tab
// which might be different from the target.
if (web_gesture.GetModifiers() & blink::WebInputEvent::kFromDebugger) {
window->delegate()->OnEvent(&pinch_event);
} else {
pinch_event.ConvertLocationToTarget(window, window->GetRootWindow());
event_injector_.Inject(window->GetHost(), &pinch_event);
}
return;
}
ui::EventMomentumPhase momentum_phase =
web_gesture.GetType() == blink::WebInputEvent::Type::kGestureFlingStart
? ui::EventMomentumPhase::BEGAN
: ui::EventMomentumPhase::END;
ui::ScrollEvent scroll_event(event_type, web_gesture.PositionInWidget(),
web_gesture.PositionInWidget(),
web_gesture.TimeStamp(), flags,
web_gesture.data.fling_start.velocity_x,
web_gesture.data.fling_start.velocity_y, 0, 0, 2,
momentum_phase, ui::ScrollEventPhase::kNone);
// Synthetic events from devtools debugger need to be dispatched explicitly
// to the target window. Otherwise they will end up in the active tab
// which might be different from the target.
if (web_gesture.GetModifiers() & blink::WebInputEvent::kFromDebugger) {
window->delegate()->OnEvent(&scroll_event);
} else {
scroll_event.ConvertLocationToTarget(window, window->GetRootWindow());
event_injector_.Inject(window->GetHost(), &scroll_event);
}
}
void SyntheticGestureTargetAura::DispatchWebMouseEventToPlatform(
const blink::WebMouseEvent& web_mouse_event,
const ui::LatencyInfo& latency_info) {
ui::EventType event_type = web_mouse_event.GetTypeAsUiEventType();
int flags = ui::WebEventModifiersToEventFlags(web_mouse_event.GetModifiers());
ui::PointerDetails pointer_details(
ui::WebPointerTypeToEventPointerType(web_mouse_event.pointer_type),
web_mouse_event.id, 0, 0, web_mouse_event.force, web_mouse_event.twist,
web_mouse_event.tilt_x, web_mouse_event.tilt_y,
web_mouse_event.tangential_pressure);
int changed_button_flags = 0;
if (event_type == ui::EventType::kMousePressed ||
event_type == ui::EventType::kMouseReleased) {
changed_button_flags =
WebEventButtonToUIEventButtonFlags(web_mouse_event.button);
}
ui::MouseEvent mouse_event(event_type, web_mouse_event.PositionInWidget(),
web_mouse_event.PositionInWidget(),
web_mouse_event.TimeStamp(), flags,
changed_button_flags, pointer_details);
aura::Window* window = GetWindow();
mouse_event.SetClickCount(web_mouse_event.click_count);
// Synthetic events from devtools debugger need to be dispatched explicitly
// to the target window. Otherwise they will end up in the active tab
// which might be different from the target.
if (web_mouse_event.GetModifiers() & blink::WebInputEvent::kFromDebugger) {
window->delegate()->OnEvent(&mouse_event);
} else {
mouse_event.ConvertLocationToTarget(window, window->GetRootWindow());
ui::EventDispatchDetails details =
event_injector_.Inject(window->GetHost(), &mouse_event);
if (details.dispatcher_destroyed)
return;
}
}
content::mojom::GestureSourceType
SyntheticGestureTargetAura::GetDefaultSyntheticGestureSourceType() const {
return content::mojom::GestureSourceType::kMouseInput;
}
void SyntheticGestureTargetAura::GetVSyncParameters(
base::TimeTicks& timebase,
base::TimeDelta& interval) const {
timebase = vsync_timebase_;
interval = vsync_interval_;
}
void SyntheticGestureTargetAura::OnBeginFrame(base::TimeTicks frame_begin_time,
base::TimeDelta frame_interval) {
vsync_timebase_ = frame_begin_time;
vsync_interval_ = frame_interval;
}
void SyntheticGestureTargetAura::OnBeginFrameSourceShuttingDown() {
if (observed_compositor_) {
observed_compositor_->RemoveSimpleBeginFrameObserver(this);
observed_compositor_ = nullptr;
}
}
float SyntheticGestureTargetAura::GetTouchSlopInDips() const {
return ui::GestureConfiguration::GetInstance()
->max_touch_move_in_pixels_for_click();
}
float SyntheticGestureTargetAura::GetSpanSlopInDips() const {
return ui::GestureConfiguration::GetInstance()->span_slop();
}
float SyntheticGestureTargetAura::GetMinScalingSpanInDips() const {
return ui::GestureConfiguration::GetInstance()->min_scaling_span_in_pixels();
}
RenderWidgetHostViewAura* SyntheticGestureTargetAura::GetView() const {
auto* view =
static_cast<RenderWidgetHostViewAura*>(render_widget_host()->GetView());
DCHECK(view);
return view;
}
aura::Window* SyntheticGestureTargetAura::GetWindow() const {
aura::Window* window = GetView()->GetNativeView();
DCHECK(window);
return window;
}
} // namespace content