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/browser/renderer_host/input/synthetic_gesture_target_aura.h"
7 #include "content/browser/renderer_host/render_widget_host_impl.h"
8 #include "content/browser/renderer_host/render_widget_host_view_aura.h"
9 #include "content/browser/renderer_host/ui_events_helper.h"
10 #include "ui/aura/client/screen_position_client.h"
11 #include "ui/aura/root_window.h"
12 #include "ui/aura/window.h"
13 #include "ui/events/gestures/gesture_configuration.h"
15 using blink::WebTouchEvent;
16 using blink::WebMouseWheelEvent;
20 SyntheticGestureTargetAura::SyntheticGestureTargetAura(
21 RenderWidgetHostImpl* host)
22 : SyntheticGestureTargetBase(host) {
25 void SyntheticGestureTargetAura::DispatchWebTouchEventToPlatform(
26 const WebTouchEvent& web_touch,
27 const ui::LatencyInfo& latency_info) {
28 aura::Window* window = GetWindow();
29 aura::client::ScreenPositionClient* position_client =
30 GetScreenPositionClient();
32 TouchEventWithLatencyInfo touch_with_latency(web_touch, latency_info);
34 // SyntheticGesture may skip calculating screenPosition, so we will fill it
35 // in here. "screenPosition" is converted from "position".
36 const size_t num_touches = touch_with_latency.event.touchesLength;
37 for (size_t i = 0; i < num_touches; ++ i) {
38 blink::WebTouchPoint* point = &touch_with_latency.event.touches[i];
39 gfx::Point position(point->position.x, point->position.y);
40 position_client->ConvertPointToScreen(window, &position);
41 point->screenPosition.x = position.x();
42 point->screenPosition.y = position.y();
45 ScopedVector<ui::TouchEvent> events;
46 bool conversion_success = MakeUITouchEventsFromWebTouchEvents(
47 touch_with_latency, &events, SCREEN_COORDINATES);
48 DCHECK(conversion_success);
50 aura::WindowTreeHostDelegate* root_window_host_delegate =
51 GetWindowTreeHostDelegate();
52 for (ScopedVector<ui::TouchEvent>::iterator iter = events.begin(),
53 end = events.end(); iter != end; ++iter) {
54 root_window_host_delegate->OnHostTouchEvent(*iter);
58 void SyntheticGestureTargetAura::DispatchWebMouseWheelEventToPlatform(
59 const blink::WebMouseWheelEvent& web_wheel,
60 const ui::LatencyInfo&) {
61 aura::Window* window = GetWindow();
62 aura::client::ScreenPositionClient* position_client =
63 GetScreenPositionClient();
64 gfx::Point location(web_wheel.x, web_wheel.y);
65 position_client->ConvertPointToScreen(window, &location);
67 ui::MouseEvent mouse_event(
68 ui::ET_MOUSEWHEEL, location, location, ui::EF_NONE, ui::EF_NONE);
69 ui::MouseWheelEvent wheel_event(
70 mouse_event, web_wheel.deltaX, web_wheel.deltaY);
72 GetWindowTreeHostDelegate()->OnHostMouseEvent(&wheel_event);
78 WebMouseEventTypeToEventType(blink::WebInputEvent::Type web_type) {
80 case blink::WebInputEvent::MouseDown:
81 return ui::ET_MOUSE_PRESSED;
83 case blink::WebInputEvent::MouseUp:
84 return ui::ET_MOUSE_RELEASED;
86 case blink::WebInputEvent::MouseMove:
87 return ui::ET_MOUSE_MOVED;
89 case blink::WebInputEvent::MouseEnter:
90 return ui::ET_MOUSE_ENTERED;
92 case blink::WebInputEvent::MouseLeave:
93 return ui::ET_MOUSE_EXITED;
95 case blink::WebInputEvent::ContextMenu:
96 NOTREACHED() << "WebInputEvent::ContextMenu not supported by"
97 "SyntheticGestureTargetAura";
103 return ui::ET_UNKNOWN;
106 int WebMouseEventButtonToFlags(blink::WebMouseEvent::Button button) {
108 case blink::WebMouseEvent::ButtonLeft:
109 return ui::EF_LEFT_MOUSE_BUTTON;
111 case blink::WebMouseEvent::ButtonMiddle:
112 return ui::EF_MIDDLE_MOUSE_BUTTON;
114 case blink::WebMouseEvent::ButtonRight:
115 return ui::EF_RIGHT_MOUSE_BUTTON;
126 void SyntheticGestureTargetAura::DispatchWebMouseEventToPlatform(
127 const blink::WebMouseEvent& web_mouse,
128 const ui::LatencyInfo& latency_info) {
129 aura::Window* window = GetWindow();
130 aura::client::ScreenPositionClient* position_client =
131 GetScreenPositionClient();
132 gfx::Point location(web_mouse.x, web_mouse.y);
133 position_client->ConvertPointToScreen(window, &location);
135 ui::EventType event_type = WebMouseEventTypeToEventType(web_mouse.type);
136 int flags = WebMouseEventButtonToFlags(web_mouse.button);
137 ui::MouseEvent mouse_event(event_type, location, location, flags, flags);
139 GetWindowTreeHostDelegate()->OnHostMouseEvent(&mouse_event);
142 SyntheticGestureParams::GestureSourceType
143 SyntheticGestureTargetAura::GetDefaultSyntheticGestureSourceType() const {
144 return SyntheticGestureParams::MOUSE_INPUT;
147 bool SyntheticGestureTargetAura::SupportsSyntheticGestureSourceType(
148 SyntheticGestureParams::GestureSourceType gesture_source_type) const {
149 return gesture_source_type == SyntheticGestureParams::TOUCH_INPUT ||
150 gesture_source_type == SyntheticGestureParams::MOUSE_INPUT;
153 int SyntheticGestureTargetAura::GetTouchSlopInDips() const {
154 // - 1 because Aura considers a pointer to be moving if it has moved at least
155 // 'max_touch_move_in_pixels_for_click' pixels.
156 return ui::GestureConfiguration::max_touch_move_in_pixels_for_click() - 1;
159 aura::Window* SyntheticGestureTargetAura::GetWindow() const {
160 aura::Window* window = render_widget_host()->GetView()->GetNativeView();
165 aura::WindowTreeHostDelegate*
166 SyntheticGestureTargetAura::GetWindowTreeHostDelegate() const {
167 aura::Window* root_window = GetWindow()->GetRootWindow();
168 aura::WindowTreeHostDelegate* root_window_host_delegate =
169 root_window->GetDispatcher()->AsWindowTreeHostDelegate();
170 DCHECK(root_window_host_delegate);
171 return root_window_host_delegate;
174 aura::client::ScreenPositionClient*
175 SyntheticGestureTargetAura::GetScreenPositionClient() const {
176 aura::Window* root_window = GetWindow()->GetRootWindow();
177 aura::client::ScreenPositionClient* position_client =
178 aura::client::GetScreenPositionClient(root_window);
179 DCHECK(position_client);
180 return position_client;
183 } // namespace content