Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / synthetic_gesture_target_aura.cc
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.
4
5 #include "content/browser/renderer_host/input/synthetic_gesture_target_aura.h"
6
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"
14
15 using blink::WebTouchEvent;
16 using blink::WebMouseWheelEvent;
17
18 namespace content {
19
20 SyntheticGestureTargetAura::SyntheticGestureTargetAura(
21     RenderWidgetHostImpl* host)
22     : SyntheticGestureTargetBase(host) {
23 }
24
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();
31
32   TouchEventWithLatencyInfo touch_with_latency(web_touch, latency_info);
33
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();
43   }
44
45   ScopedVector<ui::TouchEvent> events;
46   bool conversion_success = MakeUITouchEventsFromWebTouchEvents(
47       touch_with_latency, &events, SCREEN_COORDINATES);
48   DCHECK(conversion_success);
49
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);
55   }
56 }
57
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);
66
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);
71
72   GetWindowTreeHostDelegate()->OnHostMouseEvent(&wheel_event);
73 }
74
75 namespace {
76
77 ui::EventType
78 WebMouseEventTypeToEventType(blink::WebInputEvent::Type web_type) {
79   switch (web_type) {
80     case blink::WebInputEvent::MouseDown:
81       return ui::ET_MOUSE_PRESSED;
82
83     case blink::WebInputEvent::MouseUp:
84       return ui::ET_MOUSE_RELEASED;
85
86     case blink::WebInputEvent::MouseMove:
87       return ui::ET_MOUSE_MOVED;
88
89     case blink::WebInputEvent::MouseEnter:
90       return ui::ET_MOUSE_ENTERED;
91
92     case blink::WebInputEvent::MouseLeave:
93       return ui::ET_MOUSE_EXITED;
94
95     case blink::WebInputEvent::ContextMenu:
96       NOTREACHED() << "WebInputEvent::ContextMenu not supported by"
97           "SyntheticGestureTargetAura";
98
99     default:
100       NOTREACHED();
101   }
102
103   return ui::ET_UNKNOWN;
104 }
105
106 int WebMouseEventButtonToFlags(blink::WebMouseEvent::Button button) {
107   switch (button) {
108     case blink::WebMouseEvent::ButtonLeft:
109       return ui::EF_LEFT_MOUSE_BUTTON;
110
111     case blink::WebMouseEvent::ButtonMiddle:
112       return ui::EF_MIDDLE_MOUSE_BUTTON;
113
114     case blink::WebMouseEvent::ButtonRight:
115       return ui::EF_RIGHT_MOUSE_BUTTON;
116
117     default:
118       NOTREACHED();
119   }
120
121   return 0;
122 }
123
124 }  // namespace
125
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);
134
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);
138
139   GetWindowTreeHostDelegate()->OnHostMouseEvent(&mouse_event);
140 }
141
142 SyntheticGestureParams::GestureSourceType
143 SyntheticGestureTargetAura::GetDefaultSyntheticGestureSourceType() const {
144   return SyntheticGestureParams::MOUSE_INPUT;
145 }
146
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;
151 }
152
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;
157 }
158
159 aura::Window* SyntheticGestureTargetAura::GetWindow() const {
160   aura::Window* window = render_widget_host()->GetView()->GetNativeView();
161   DCHECK(window);
162   return window;
163 }
164
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;
172 }
173
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;
181 }
182
183 }  // namespace content