OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/devtools/protocol/input_handler.h" | 5 #include "content/browser/devtools/protocol/input_handler.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 return false; | 48 return false; |
49 } | 49 } |
50 } | 50 } |
51 | 51 |
52 } | 52 } |
53 | 53 |
54 typedef DevToolsProtocolClient::Response Response; | 54 typedef DevToolsProtocolClient::Response Response; |
55 | 55 |
56 namespace { | 56 namespace { |
57 | 57 |
58 void SetEventModifiers(blink::WebInputEvent* event, const int* modifiers) { | 58 int GetEventModifiers(const int* modifiers, |
59 if (!modifiers) | 59 const bool* auto_repeat, |
60 return; | 60 const bool* is_keypad) { |
61 if (*modifiers & 1) | 61 int result = 0; |
62 event->modifiers |= blink::WebInputEvent::AltKey; | 62 if (auto_repeat && *auto_repeat) |
63 if (*modifiers & 2) | 63 result |= blink::WebInputEvent::IsAutoRepeat; |
64 event->modifiers |= blink::WebInputEvent::ControlKey; | 64 if (is_keypad && *is_keypad) |
65 if (*modifiers & 4) | 65 result |= blink::WebInputEvent::IsKeyPad; |
66 event->modifiers |= blink::WebInputEvent::MetaKey; | 66 |
67 if (*modifiers & 8) | 67 if (modifiers) { |
68 event->modifiers |= blink::WebInputEvent::ShiftKey; | 68 if (*modifiers & 1) |
| 69 result |= blink::WebInputEvent::AltKey; |
| 70 if (*modifiers & 2) |
| 71 result |= blink::WebInputEvent::ControlKey; |
| 72 if (*modifiers & 4) |
| 73 result |= blink::WebInputEvent::MetaKey; |
| 74 if (*modifiers & 8) |
| 75 result |= blink::WebInputEvent::ShiftKey; |
| 76 } |
| 77 return result; |
69 } | 78 } |
70 | 79 |
71 void SetEventTimestamp(blink::WebInputEvent* event, const double* timestamp) { | 80 base::TimeTicks GetEventTimeTicks(const double* timestamp) { |
72 // Convert timestamp, in seconds since unix epoch, to an event timestamp | 81 // Convert timestamp, in seconds since unix epoch, to an event timestamp |
73 // which is time ticks since platform start time. | 82 // which is time ticks since platform start time. |
74 base::TimeTicks ticks = timestamp | 83 return timestamp |
75 ? base::TimeDelta::FromSecondsD(*timestamp) + | 84 ? base::TimeDelta::FromSecondsD(*timestamp) + |
76 base::TimeTicks::UnixEpoch() | 85 base::TimeTicks::UnixEpoch() |
77 : base::TimeTicks::Now(); | 86 : base::TimeTicks::Now(); |
78 event->timeStampSeconds = (ticks - base::TimeTicks()).InSecondsF(); | 87 } |
| 88 |
| 89 double GetEventTimestamp(const double* timestamp) { |
| 90 return (GetEventTimeTicks(timestamp) - base::TimeTicks()).InSecondsF(); |
79 } | 91 } |
80 | 92 |
81 bool SetKeyboardEventText(blink::WebUChar* to, const std::string* from) { | 93 bool SetKeyboardEventText(blink::WebUChar* to, const std::string* from) { |
82 if (!from) | 94 if (!from) |
83 return true; | 95 return true; |
84 | 96 |
85 base::string16 text16 = base::UTF8ToUTF16(*from); | 97 base::string16 text16 = base::UTF8ToUTF16(*from); |
86 if (text16.size() > blink::WebKeyboardEvent::textLengthCap) | 98 if (text16.size() > blink::WebKeyboardEvent::textLengthCap) |
87 return false; | 99 return false; |
88 | 100 |
89 for (size_t i = 0; i < text16.size(); ++i) | 101 for (size_t i = 0; i < text16.size(); ++i) |
90 to[i] = text16[i]; | 102 to[i] = text16[i]; |
91 return true; | 103 return true; |
92 } | 104 } |
93 | 105 |
94 bool SetMouseEventButton(blink::WebMouseEvent* event, | 106 bool GetMouseEventButton(const std::string* button, |
95 const std::string* button) { | 107 blink::WebPointerProperties::Button* event_button, |
| 108 int* event_modifiers) { |
96 if (!button) | 109 if (!button) |
97 return true; | 110 return true; |
98 | 111 |
99 if (*button == dispatch_mouse_event::kButtonNone) { | 112 if (*button == dispatch_mouse_event::kButtonNone) { |
100 event->button = blink::WebMouseEvent::Button::NoButton; | 113 *event_button = blink::WebMouseEvent::Button::NoButton; |
101 } else if (*button == dispatch_mouse_event::kButtonLeft) { | 114 } else if (*button == dispatch_mouse_event::kButtonLeft) { |
102 event->button = blink::WebMouseEvent::Button::Left; | 115 *event_button = blink::WebMouseEvent::Button::Left; |
103 event->modifiers |= blink::WebInputEvent::LeftButtonDown; | 116 *event_modifiers = blink::WebInputEvent::LeftButtonDown; |
104 } else if (*button == dispatch_mouse_event::kButtonMiddle) { | 117 } else if (*button == dispatch_mouse_event::kButtonMiddle) { |
105 event->button = blink::WebMouseEvent::Button::Middle; | 118 *event_button = blink::WebMouseEvent::Button::Middle; |
106 event->modifiers |= blink::WebInputEvent::MiddleButtonDown; | 119 *event_modifiers = blink::WebInputEvent::MiddleButtonDown; |
107 } else if (*button == dispatch_mouse_event::kButtonRight) { | 120 } else if (*button == dispatch_mouse_event::kButtonRight) { |
108 event->button = blink::WebMouseEvent::Button::Right; | 121 *event_button = blink::WebMouseEvent::Button::Right; |
109 event->modifiers |= blink::WebInputEvent::RightButtonDown; | 122 *event_modifiers = blink::WebInputEvent::RightButtonDown; |
110 } else { | 123 } else { |
111 return false; | 124 return false; |
112 } | 125 } |
113 return true; | 126 return true; |
114 } | 127 } |
115 | 128 |
116 bool SetMouseEventType(blink::WebMouseEvent* event, const std::string& type) { | 129 blink::WebInputEvent::Type GetMouseEventType(const std::string& type) { |
117 if (type == dispatch_mouse_event::kTypeMousePressed) { | 130 if (type == dispatch_mouse_event::kTypeMousePressed) |
118 event->type = blink::WebInputEvent::MouseDown; | 131 return blink::WebInputEvent::MouseDown; |
119 } else if (type == dispatch_mouse_event::kTypeMouseReleased) { | 132 if (type == dispatch_mouse_event::kTypeMouseReleased) |
120 event->type = blink::WebInputEvent::MouseUp; | 133 return blink::WebInputEvent::MouseUp; |
121 } else if (type == dispatch_mouse_event::kTypeMouseMoved) { | 134 if (type == dispatch_mouse_event::kTypeMouseMoved) |
122 event->type = blink::WebInputEvent::MouseMove; | 135 return blink::WebInputEvent::MouseMove; |
123 } else { | 136 return blink::WebInputEvent::Undefined; |
124 return false; | |
125 } | |
126 return true; | |
127 } | 137 } |
128 | 138 |
129 } // namespace | 139 } // namespace |
130 | 140 |
131 InputHandler::InputHandler() | 141 InputHandler::InputHandler() |
132 : host_(NULL), | 142 : host_(NULL), |
133 page_scale_factor_(1.0), | 143 page_scale_factor_(1.0), |
134 weak_factory_(this) { | 144 weak_factory_(this) { |
135 } | 145 } |
136 | 146 |
(...skipping 21 matching lines...) Expand all Loading... |
158 const std::string* text, | 168 const std::string* text, |
159 const std::string* unmodified_text, | 169 const std::string* unmodified_text, |
160 const std::string* key_identifier, | 170 const std::string* key_identifier, |
161 const std::string* code, | 171 const std::string* code, |
162 const std::string* key, | 172 const std::string* key, |
163 const int* windows_virtual_key_code, | 173 const int* windows_virtual_key_code, |
164 const int* native_virtual_key_code, | 174 const int* native_virtual_key_code, |
165 const bool* auto_repeat, | 175 const bool* auto_repeat, |
166 const bool* is_keypad, | 176 const bool* is_keypad, |
167 const bool* is_system_key) { | 177 const bool* is_system_key) { |
168 NativeWebKeyboardEvent event; | 178 blink::WebInputEvent::Type web_event_type = blink::WebInputEvent::Undefined; |
169 event.skip_in_browser = true; | |
170 | |
171 if (type == dispatch_key_event::kTypeKeyDown) { | 179 if (type == dispatch_key_event::kTypeKeyDown) { |
172 event.type = blink::WebInputEvent::KeyDown; | 180 web_event_type = blink::WebInputEvent::KeyDown; |
173 } else if (type == dispatch_key_event::kTypeKeyUp) { | 181 } else if (type == dispatch_key_event::kTypeKeyUp) { |
174 event.type = blink::WebInputEvent::KeyUp; | 182 web_event_type = blink::WebInputEvent::KeyUp; |
175 } else if (type == dispatch_key_event::kTypeChar) { | 183 } else if (type == dispatch_key_event::kTypeChar) { |
176 event.type = blink::WebInputEvent::Char; | 184 web_event_type = blink::WebInputEvent::Char; |
177 } else if (type == dispatch_key_event::kTypeRawKeyDown) { | 185 } else if (type == dispatch_key_event::kTypeRawKeyDown) { |
178 event.type = blink::WebInputEvent::RawKeyDown; | 186 web_event_type = blink::WebInputEvent::RawKeyDown; |
179 } else { | 187 } else { |
180 return Response::InvalidParams( | 188 return Response::InvalidParams( |
181 base::StringPrintf("Unexpected event type '%s'", type.c_str())); | 189 base::StringPrintf("Unexpected event type '%s'", type.c_str())); |
182 } | 190 } |
183 | 191 |
184 SetEventModifiers(&event, modifiers); | 192 NativeWebKeyboardEvent event( |
185 SetEventTimestamp(&event, timestamp); | 193 web_event_type, GetEventModifiers(modifiers, auto_repeat, is_keypad), |
| 194 GetEventTimeTicks(timestamp)); |
| 195 event.skip_in_browser = true; |
186 if (!SetKeyboardEventText(event.text, text)) | 196 if (!SetKeyboardEventText(event.text, text)) |
187 return Response::InvalidParams("Invalid 'text' parameter"); | 197 return Response::InvalidParams("Invalid 'text' parameter"); |
188 if (!SetKeyboardEventText(event.unmodifiedText, unmodified_text)) | 198 if (!SetKeyboardEventText(event.unmodifiedText, unmodified_text)) |
189 return Response::InvalidParams("Invalid 'unmodifiedText' parameter"); | 199 return Response::InvalidParams("Invalid 'unmodifiedText' parameter"); |
190 | 200 |
191 if (windows_virtual_key_code) | 201 if (windows_virtual_key_code) |
192 event.windowsKeyCode = *windows_virtual_key_code; | 202 event.windowsKeyCode = *windows_virtual_key_code; |
193 if (native_virtual_key_code) | 203 if (native_virtual_key_code) |
194 event.nativeKeyCode = *native_virtual_key_code; | 204 event.nativeKeyCode = *native_virtual_key_code; |
195 if (auto_repeat && *auto_repeat) | |
196 event.modifiers |= blink::WebInputEvent::IsAutoRepeat; | |
197 if (is_keypad && *is_keypad) | |
198 event.modifiers |= blink::WebInputEvent::IsKeyPad; | |
199 if (is_system_key) | 205 if (is_system_key) |
200 event.isSystemKey = *is_system_key; | 206 event.isSystemKey = *is_system_key; |
201 | 207 |
202 if (code) { | 208 if (code) { |
203 event.domCode = static_cast<int>( | 209 event.domCode = static_cast<int>( |
204 ui::KeycodeConverter::CodeStringToDomCode(*code)); | 210 ui::KeycodeConverter::CodeStringToDomCode(*code)); |
205 } | 211 } |
206 | 212 |
207 if (key) { | 213 if (key) { |
208 event.domKey = static_cast<int>( | 214 event.domKey = static_cast<int>( |
209 ui::KeycodeConverter::KeyStringToDomKey(*key)); | 215 ui::KeycodeConverter::KeyStringToDomKey(*key)); |
210 } | 216 } |
211 | 217 |
212 if (!host_) | 218 if (!host_) |
213 return Response::ServerError("Could not connect to view"); | 219 return Response::ServerError("Could not connect to view"); |
214 | 220 |
215 host_->Focus(); | 221 host_->Focus(); |
216 host_->ForwardKeyboardEvent(event); | 222 host_->ForwardKeyboardEvent(event); |
217 return Response::OK(); | 223 return Response::OK(); |
218 } | 224 } |
219 | 225 |
220 Response InputHandler::DispatchMouseEvent( | 226 Response InputHandler::DispatchMouseEvent( |
221 const std::string& type, | 227 const std::string& type, |
222 int x, | 228 int x, |
223 int y, | 229 int y, |
224 const int* modifiers, | 230 const int* modifiers, |
225 const double* timestamp, | 231 const double* timestamp, |
226 const std::string* button, | 232 const std::string* button, |
227 const int* click_count) { | 233 const int* click_count) { |
228 blink::WebMouseEvent event; | 234 blink::WebInputEvent::Type event_type = GetMouseEventType(type); |
229 | 235 if (event_type == blink::WebInputEvent::Undefined) { |
230 if (!SetMouseEventType(&event, type)) { | |
231 return Response::InvalidParams( | 236 return Response::InvalidParams( |
232 base::StringPrintf("Unexpected event type '%s'", type.c_str())); | 237 base::StringPrintf("Unexpected event type '%s'", type.c_str())); |
233 } | 238 } |
234 SetEventModifiers(&event, modifiers); | 239 blink::WebPointerProperties::Button event_button = |
235 SetEventTimestamp(&event, timestamp); | 240 blink::WebPointerProperties::Button::NoButton; |
236 if (!SetMouseEventButton(&event, button)) | 241 int button_modifiers = 0; |
| 242 if (!GetMouseEventButton(button, &event_button, &button_modifiers)) |
237 return Response::InvalidParams("Invalid mouse button"); | 243 return Response::InvalidParams("Invalid mouse button"); |
238 | 244 |
| 245 blink::WebMouseEvent event( |
| 246 event_type, |
| 247 GetEventModifiers(modifiers, nullptr, nullptr) | button_modifiers, |
| 248 GetEventTimestamp(timestamp)); |
| 249 |
| 250 event.button = event_button; |
239 event.x = x * page_scale_factor_; | 251 event.x = x * page_scale_factor_; |
240 event.y = y * page_scale_factor_; | 252 event.y = y * page_scale_factor_; |
241 event.windowX = x * page_scale_factor_; | 253 event.windowX = x * page_scale_factor_; |
242 event.windowY = y * page_scale_factor_; | 254 event.windowY = y * page_scale_factor_; |
243 event.globalX = x * page_scale_factor_; | 255 event.globalX = x * page_scale_factor_; |
244 event.globalY = y * page_scale_factor_; | 256 event.globalY = y * page_scale_factor_; |
245 event.clickCount = click_count ? *click_count : 0; | 257 event.clickCount = click_count ? *click_count : 0; |
246 event.pointerType = blink::WebPointerProperties::PointerType::Mouse; | 258 event.pointerType = blink::WebPointerProperties::PointerType::Mouse; |
247 | 259 |
248 if (!host_) | 260 if (!host_) |
249 return Response::ServerError("Could not connect to view"); | 261 return Response::ServerError("Could not connect to view"); |
250 | 262 |
251 host_->Focus(); | 263 host_->Focus(); |
252 host_->ForwardMouseEvent(event); | 264 host_->ForwardMouseEvent(event); |
253 return Response::OK(); | 265 return Response::OK(); |
254 } | 266 } |
255 | 267 |
256 Response InputHandler::EmulateTouchFromMouseEvent(const std::string& type, | 268 Response InputHandler::EmulateTouchFromMouseEvent(const std::string& type, |
257 int x, | 269 int x, |
258 int y, | 270 int y, |
259 double timestamp, | 271 double timestamp, |
260 const std::string& button, | 272 const std::string& button, |
261 double* delta_x, | 273 double* delta_x, |
262 double* delta_y, | 274 double* delta_y, |
263 int* modifiers, | 275 int* modifiers, |
264 int* click_count) { | 276 int* click_count) { |
265 blink::WebMouseWheelEvent wheel_event; | 277 blink::WebInputEvent::Type event_type; |
266 blink::WebMouseEvent mouse_event; | |
267 blink::WebMouseEvent* event = &mouse_event; | |
268 | |
269 if (type == emulate_touch_from_mouse_event::kTypeMouseWheel) { | 278 if (type == emulate_touch_from_mouse_event::kTypeMouseWheel) { |
| 279 event_type = blink::WebInputEvent::MouseWheel; |
270 if (!delta_x || !delta_y) { | 280 if (!delta_x || !delta_y) { |
271 return Response::InvalidParams( | 281 return Response::InvalidParams( |
272 "'deltaX' and 'deltaY' are expected for mouseWheel event"); | 282 "'deltaX' and 'deltaY' are expected for mouseWheel event"); |
273 } | 283 } |
274 wheel_event.deltaX = static_cast<float>(*delta_x); | 284 } else { |
275 wheel_event.deltaY = static_cast<float>(*delta_y); | 285 event_type = GetMouseEventType(type); |
276 event = &wheel_event; | 286 if (event_type == blink::WebInputEvent::Undefined) { |
277 event->type = blink::WebInputEvent::MouseWheel; | 287 return Response::InvalidParams( |
278 } else if (!SetMouseEventType(event, type)) { | 288 base::StringPrintf("Unexpected event type '%s'", type.c_str())); |
279 return Response::InvalidParams( | 289 } |
280 base::StringPrintf("Unexpected event type '%s'", type.c_str())); | |
281 } | 290 } |
282 | 291 |
283 SetEventModifiers(event, modifiers); | 292 blink::WebPointerProperties::Button event_button = |
284 SetEventTimestamp(event, ×tamp); | 293 blink::WebPointerProperties::Button::NoButton; |
285 if (!SetMouseEventButton(event, &button)) | 294 int button_modifiers = 0; |
| 295 if (!GetMouseEventButton(&button, &event_button, &button_modifiers)) |
286 return Response::InvalidParams("Invalid mouse button"); | 296 return Response::InvalidParams("Invalid mouse button"); |
287 | 297 |
288 event->x = x; | 298 ui::ScopedWebInputEvent event; |
289 event->y = y; | 299 blink::WebMouseWheelEvent* wheel_event = nullptr; |
290 event->windowX = x; | 300 blink::WebMouseEvent* mouse_event = nullptr; |
291 event->windowY = y; | 301 if (type == emulate_touch_from_mouse_event::kTypeMouseWheel) { |
292 event->globalX = x; | 302 wheel_event = new blink::WebMouseWheelEvent( |
293 event->globalY = y; | 303 event_type, |
294 event->clickCount = click_count ? *click_count : 0; | 304 GetEventModifiers(modifiers, nullptr, nullptr) | button_modifiers, |
295 event->pointerType = blink::WebPointerProperties::PointerType::Touch; | 305 GetEventTimestamp(×tamp)); |
| 306 mouse_event = wheel_event; |
| 307 event.reset(wheel_event); |
| 308 wheel_event->deltaX = static_cast<float>(*delta_x); |
| 309 wheel_event->deltaY = static_cast<float>(*delta_y); |
| 310 } else { |
| 311 mouse_event = new blink::WebMouseEvent( |
| 312 event_type, |
| 313 GetEventModifiers(modifiers, nullptr, nullptr) | button_modifiers, |
| 314 GetEventTimestamp(×tamp)); |
| 315 event.reset(mouse_event); |
| 316 } |
| 317 |
| 318 mouse_event->x = x; |
| 319 mouse_event->y = y; |
| 320 mouse_event->button = event_button; |
| 321 mouse_event->windowX = x; |
| 322 mouse_event->windowY = y; |
| 323 mouse_event->globalX = x; |
| 324 mouse_event->globalY = y; |
| 325 mouse_event->clickCount = click_count ? *click_count : 0; |
| 326 mouse_event->pointerType = blink::WebPointerProperties::PointerType::Touch; |
296 | 327 |
297 if (!host_) | 328 if (!host_) |
298 return Response::ServerError("Could not connect to view"); | 329 return Response::ServerError("Could not connect to view"); |
299 | 330 |
300 if (event->type == blink::WebInputEvent::MouseWheel) | 331 if (wheel_event) |
301 host_->ForwardWheelEvent(wheel_event); | 332 host_->ForwardWheelEvent(*wheel_event); |
302 else | 333 else |
303 host_->ForwardMouseEvent(mouse_event); | 334 host_->ForwardMouseEvent(*mouse_event); |
304 return Response::OK(); | 335 return Response::OK(); |
305 } | 336 } |
306 | 337 |
307 Response InputHandler::SynthesizePinchGesture( | 338 Response InputHandler::SynthesizePinchGesture( |
308 DevToolsCommandId command_id, | 339 DevToolsCommandId command_id, |
309 int x, | 340 int x, |
310 int y, | 341 int y, |
311 double scale_factor, | 342 double scale_factor, |
312 const int* relative_speed, | 343 const int* relative_speed, |
313 const std::string* gesture_source_type) { | 344 const std::string* gesture_source_type) { |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 } else { | 548 } else { |
518 client_->SendError(command_id, | 549 client_->SendError(command_id, |
519 Response::InternalError(base::StringPrintf( | 550 Response::InternalError(base::StringPrintf( |
520 "Synthetic tap failed, result was %d", result))); | 551 "Synthetic tap failed, result was %d", result))); |
521 } | 552 } |
522 } | 553 } |
523 | 554 |
524 } // namespace input | 555 } // namespace input |
525 } // namespace devtools | 556 } // namespace devtools |
526 } // namespace content | 557 } // namespace content |
OLD | NEW |