| 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" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
| 13 #include "cc/output/compositor_frame_metadata.h" | 13 #include "cc/output/compositor_frame_metadata.h" |
| 14 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 14 #include "content/browser/renderer_host/render_widget_host_impl.h" | 15 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 15 #include "content/common/input/synthetic_pinch_gesture_params.h" | 16 #include "content/common/input/synthetic_pinch_gesture_params.h" |
| 16 #include "content/common/input/synthetic_smooth_scroll_gesture_params.h" | 17 #include "content/common/input/synthetic_smooth_scroll_gesture_params.h" |
| 17 #include "content/common/input/synthetic_tap_gesture_params.h" | 18 #include "content/common/input/synthetic_tap_gesture_params.h" |
| 18 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 19 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
| 19 #include "ui/events/keycodes/dom/keycode_converter.h" | 20 #include "ui/events/keycodes/dom/keycode_converter.h" |
| 20 #include "ui/gfx/geometry/point.h" | 21 #include "ui/gfx/geometry/point.h" |
| 21 | 22 |
| 22 namespace content { | 23 namespace content { |
| 23 namespace devtools { | 24 namespace devtools { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 131 |
| 131 InputHandler::InputHandler() | 132 InputHandler::InputHandler() |
| 132 : host_(NULL), | 133 : host_(NULL), |
| 133 page_scale_factor_(1.0), | 134 page_scale_factor_(1.0), |
| 134 weak_factory_(this) { | 135 weak_factory_(this) { |
| 135 } | 136 } |
| 136 | 137 |
| 137 InputHandler::~InputHandler() { | 138 InputHandler::~InputHandler() { |
| 138 } | 139 } |
| 139 | 140 |
| 140 void InputHandler::SetRenderWidgetHost(RenderWidgetHostImpl* host) { | 141 void InputHandler::SetRenderFrameHost(RenderFrameHostImpl* host) { |
| 141 host_ = host; | 142 host_ = host; |
| 142 } | 143 } |
| 143 | 144 |
| 144 void InputHandler::SetClient(std::unique_ptr<Client> client) { | 145 void InputHandler::SetClient(std::unique_ptr<Client> client) { |
| 145 client_.swap(client); | 146 client_.swap(client); |
| 146 } | 147 } |
| 147 | 148 |
| 148 void InputHandler::OnSwapCompositorFrame( | 149 void InputHandler::OnSwapCompositorFrame( |
| 149 const cc::CompositorFrameMetadata& frame_metadata) { | 150 const cc::CompositorFrameMetadata& frame_metadata) { |
| 150 page_scale_factor_ = frame_metadata.page_scale_factor; | 151 page_scale_factor_ = frame_metadata.page_scale_factor; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 if (code) { | 203 if (code) { |
| 203 event.domCode = static_cast<int>( | 204 event.domCode = static_cast<int>( |
| 204 ui::KeycodeConverter::CodeStringToDomCode(*code)); | 205 ui::KeycodeConverter::CodeStringToDomCode(*code)); |
| 205 } | 206 } |
| 206 | 207 |
| 207 if (key) { | 208 if (key) { |
| 208 event.domKey = static_cast<int>( | 209 event.domKey = static_cast<int>( |
| 209 ui::KeycodeConverter::KeyStringToDomKey(*key)); | 210 ui::KeycodeConverter::KeyStringToDomKey(*key)); |
| 210 } | 211 } |
| 211 | 212 |
| 212 if (!host_) | 213 if (!host_ || !host_->GetRenderWidgetHost()) |
| 213 return Response::ServerError("Could not connect to view"); | 214 return Response::ServerError("Could not connect to view"); |
| 214 | 215 |
| 215 host_->Focus(); | 216 host_->GetRenderWidgetHost()->Focus(); |
| 216 host_->ForwardKeyboardEvent(event); | 217 host_->GetRenderWidgetHost()->ForwardKeyboardEvent(event); |
| 217 return Response::OK(); | 218 return Response::OK(); |
| 218 } | 219 } |
| 219 | 220 |
| 220 Response InputHandler::DispatchMouseEvent( | 221 Response InputHandler::DispatchMouseEvent( |
| 221 const std::string& type, | 222 const std::string& type, |
| 222 int x, | 223 int x, |
| 223 int y, | 224 int y, |
| 224 const int* modifiers, | 225 const int* modifiers, |
| 225 const double* timestamp, | 226 const double* timestamp, |
| 226 const std::string* button, | 227 const std::string* button, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 238 | 239 |
| 239 event.x = x * page_scale_factor_; | 240 event.x = x * page_scale_factor_; |
| 240 event.y = y * page_scale_factor_; | 241 event.y = y * page_scale_factor_; |
| 241 event.windowX = x * page_scale_factor_; | 242 event.windowX = x * page_scale_factor_; |
| 242 event.windowY = y * page_scale_factor_; | 243 event.windowY = y * page_scale_factor_; |
| 243 event.globalX = x * page_scale_factor_; | 244 event.globalX = x * page_scale_factor_; |
| 244 event.globalY = y * page_scale_factor_; | 245 event.globalY = y * page_scale_factor_; |
| 245 event.clickCount = click_count ? *click_count : 0; | 246 event.clickCount = click_count ? *click_count : 0; |
| 246 event.pointerType = blink::WebPointerProperties::PointerType::Mouse; | 247 event.pointerType = blink::WebPointerProperties::PointerType::Mouse; |
| 247 | 248 |
| 248 if (!host_) | 249 if (!host_ || !host_->GetRenderWidgetHost()) |
| 249 return Response::ServerError("Could not connect to view"); | 250 return Response::ServerError("Could not connect to view"); |
| 250 | 251 |
| 251 host_->Focus(); | 252 host_->GetRenderWidgetHost()->Focus(); |
| 252 host_->ForwardMouseEvent(event); | 253 host_->GetRenderWidgetHost()->ForwardMouseEvent(event); |
| 253 return Response::OK(); | 254 return Response::OK(); |
| 254 } | 255 } |
| 255 | 256 |
| 256 Response InputHandler::EmulateTouchFromMouseEvent(const std::string& type, | 257 Response InputHandler::EmulateTouchFromMouseEvent(const std::string& type, |
| 257 int x, | 258 int x, |
| 258 int y, | 259 int y, |
| 259 double timestamp, | 260 double timestamp, |
| 260 const std::string& button, | 261 const std::string& button, |
| 261 double* delta_x, | 262 double* delta_x, |
| 262 double* delta_y, | 263 double* delta_y, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 287 | 288 |
| 288 event->x = x; | 289 event->x = x; |
| 289 event->y = y; | 290 event->y = y; |
| 290 event->windowX = x; | 291 event->windowX = x; |
| 291 event->windowY = y; | 292 event->windowY = y; |
| 292 event->globalX = x; | 293 event->globalX = x; |
| 293 event->globalY = y; | 294 event->globalY = y; |
| 294 event->clickCount = click_count ? *click_count : 0; | 295 event->clickCount = click_count ? *click_count : 0; |
| 295 event->pointerType = blink::WebPointerProperties::PointerType::Touch; | 296 event->pointerType = blink::WebPointerProperties::PointerType::Touch; |
| 296 | 297 |
| 297 if (!host_) | 298 if (!host_ || !host_->GetRenderWidgetHost()) |
| 298 return Response::ServerError("Could not connect to view"); | 299 return Response::ServerError("Could not connect to view"); |
| 299 | 300 |
| 300 if (event->type == blink::WebInputEvent::MouseWheel) | 301 if (event->type == blink::WebInputEvent::MouseWheel) |
| 301 host_->ForwardWheelEvent(wheel_event); | 302 host_->GetRenderWidgetHost()->ForwardWheelEvent(wheel_event); |
| 302 else | 303 else |
| 303 host_->ForwardMouseEvent(mouse_event); | 304 host_->GetRenderWidgetHost()->ForwardMouseEvent(mouse_event); |
| 304 return Response::OK(); | 305 return Response::OK(); |
| 305 } | 306 } |
| 306 | 307 |
| 307 Response InputHandler::SynthesizePinchGesture( | 308 Response InputHandler::SynthesizePinchGesture( |
| 308 DevToolsCommandId command_id, | 309 DevToolsCommandId command_id, |
| 309 int x, | 310 int x, |
| 310 int y, | 311 int y, |
| 311 double scale_factor, | 312 double scale_factor, |
| 312 const int* relative_speed, | 313 const int* relative_speed, |
| 313 const std::string* gesture_source_type) { | 314 const std::string* gesture_source_type) { |
| 314 if (!host_) | 315 if (!host_ || !host_->GetRenderWidgetHost()) |
| 315 return Response::ServerError("Could not connect to view"); | 316 return Response::ServerError("Could not connect to view"); |
| 316 | 317 |
| 317 SyntheticPinchGestureParams gesture_params; | 318 SyntheticPinchGestureParams gesture_params; |
| 318 const int kDefaultRelativeSpeed = 800; | 319 const int kDefaultRelativeSpeed = 800; |
| 319 | 320 |
| 320 gesture_params.scale_factor = scale_factor; | 321 gesture_params.scale_factor = scale_factor; |
| 321 gesture_params.anchor = CssPixelsToPointF(x, y, page_scale_factor_); | 322 gesture_params.anchor = CssPixelsToPointF(x, y, page_scale_factor_); |
| 322 gesture_params.relative_pointer_speed_in_pixels_s = | 323 gesture_params.relative_pointer_speed_in_pixels_s = |
| 323 relative_speed ? *relative_speed : kDefaultRelativeSpeed; | 324 relative_speed ? *relative_speed : kDefaultRelativeSpeed; |
| 324 | 325 |
| 325 if (!StringToGestureSourceType( | 326 if (!StringToGestureSourceType( |
| 326 gesture_source_type ? *gesture_source_type : kGestureSourceTypeDefault, | 327 gesture_source_type ? *gesture_source_type : kGestureSourceTypeDefault, |
| 327 gesture_params.gesture_source_type)) { | 328 gesture_params.gesture_source_type)) { |
| 328 return Response::InvalidParams("gestureSourceType"); | 329 return Response::InvalidParams("gestureSourceType"); |
| 329 } | 330 } |
| 330 | 331 |
| 331 host_->QueueSyntheticGesture( | 332 host_->GetRenderWidgetHost()->QueueSyntheticGesture( |
| 332 SyntheticGesture::Create(gesture_params), | 333 SyntheticGesture::Create(gesture_params), |
| 333 base::Bind(&InputHandler::SendSynthesizePinchGestureResponse, | 334 base::Bind(&InputHandler::SendSynthesizePinchGestureResponse, |
| 334 weak_factory_.GetWeakPtr(), command_id)); | 335 weak_factory_.GetWeakPtr(), command_id)); |
| 335 | 336 |
| 336 return Response::OK(); | 337 return Response::OK(); |
| 337 } | 338 } |
| 338 | 339 |
| 339 Response InputHandler::SynthesizeScrollGesture( | 340 Response InputHandler::SynthesizeScrollGesture( |
| 340 DevToolsCommandId command_id, | 341 DevToolsCommandId command_id, |
| 341 int x, | 342 int x, |
| 342 int y, | 343 int y, |
| 343 const int* x_distance, | 344 const int* x_distance, |
| 344 const int* y_distance, | 345 const int* y_distance, |
| 345 const int* x_overscroll, | 346 const int* x_overscroll, |
| 346 const int* y_overscroll, | 347 const int* y_overscroll, |
| 347 const bool* prevent_fling, | 348 const bool* prevent_fling, |
| 348 const int* speed, | 349 const int* speed, |
| 349 const std::string* gesture_source_type, | 350 const std::string* gesture_source_type, |
| 350 const int* repeat_count, | 351 const int* repeat_count, |
| 351 const int* repeat_delay_ms, | 352 const int* repeat_delay_ms, |
| 352 const std::string* interaction_marker_name) { | 353 const std::string* interaction_marker_name) { |
| 353 if (!host_) | 354 if (!host_ || !host_->GetRenderWidgetHost()) |
| 354 return Response::ServerError("Could not connect to view"); | 355 return Response::ServerError("Could not connect to view"); |
| 355 | 356 |
| 356 SyntheticSmoothScrollGestureParams gesture_params; | 357 SyntheticSmoothScrollGestureParams gesture_params; |
| 357 const bool kDefaultPreventFling = true; | 358 const bool kDefaultPreventFling = true; |
| 358 const int kDefaultSpeed = 800; | 359 const int kDefaultSpeed = 800; |
| 359 | 360 |
| 360 gesture_params.anchor = CssPixelsToPointF(x, y, page_scale_factor_); | 361 gesture_params.anchor = CssPixelsToPointF(x, y, page_scale_factor_); |
| 361 gesture_params.prevent_fling = | 362 gesture_params.prevent_fling = |
| 362 prevent_fling ? *prevent_fling : kDefaultPreventFling; | 363 prevent_fling ? *prevent_fling : kDefaultPreventFling; |
| 363 gesture_params.speed_in_pixels_s = speed ? *speed : kDefaultSpeed; | 364 gesture_params.speed_in_pixels_s = speed ? *speed : kDefaultSpeed; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 394 int repeat_count, | 395 int repeat_count, |
| 395 base::TimeDelta repeat_delay, | 396 base::TimeDelta repeat_delay, |
| 396 std::string interaction_marker_name, | 397 std::string interaction_marker_name, |
| 397 DevToolsCommandId command_id) { | 398 DevToolsCommandId command_id) { |
| 398 if (!interaction_marker_name.empty()) { | 399 if (!interaction_marker_name.empty()) { |
| 399 // TODO(alexclarke): Can we move this elsewhere? It doesn't really fit here. | 400 // TODO(alexclarke): Can we move this elsewhere? It doesn't really fit here. |
| 400 TRACE_EVENT_COPY_ASYNC_BEGIN0("benchmark", interaction_marker_name.c_str(), | 401 TRACE_EVENT_COPY_ASYNC_BEGIN0("benchmark", interaction_marker_name.c_str(), |
| 401 command_id.call_id); | 402 command_id.call_id); |
| 402 } | 403 } |
| 403 | 404 |
| 404 host_->QueueSyntheticGesture( | 405 host_->GetRenderWidgetHost()->QueueSyntheticGesture( |
| 405 SyntheticGesture::Create(gesture_params), | 406 SyntheticGesture::Create(gesture_params), |
| 406 base::Bind(&InputHandler::OnScrollFinished, weak_factory_.GetWeakPtr(), | 407 base::Bind(&InputHandler::OnScrollFinished, weak_factory_.GetWeakPtr(), |
| 407 gesture_params, repeat_count, repeat_delay, | 408 gesture_params, repeat_count, repeat_delay, |
| 408 interaction_marker_name, command_id)); | 409 interaction_marker_name, command_id)); |
| 409 } | 410 } |
| 410 | 411 |
| 411 void InputHandler::OnScrollFinished( | 412 void InputHandler::OnScrollFinished( |
| 412 SyntheticSmoothScrollGestureParams gesture_params, | 413 SyntheticSmoothScrollGestureParams gesture_params, |
| 413 int repeat_count, | 414 int repeat_count, |
| 414 base::TimeDelta repeat_delay, | 415 base::TimeDelta repeat_delay, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 432 } | 433 } |
| 433 } | 434 } |
| 434 | 435 |
| 435 Response InputHandler::SynthesizeTapGesture( | 436 Response InputHandler::SynthesizeTapGesture( |
| 436 DevToolsCommandId command_id, | 437 DevToolsCommandId command_id, |
| 437 int x, | 438 int x, |
| 438 int y, | 439 int y, |
| 439 const int* duration, | 440 const int* duration, |
| 440 const int* tap_count, | 441 const int* tap_count, |
| 441 const std::string* gesture_source_type) { | 442 const std::string* gesture_source_type) { |
| 442 if (!host_) | 443 if (!host_ || !host_->GetRenderWidgetHost()) |
| 443 return Response::ServerError("Could not connect to view"); | 444 return Response::ServerError("Could not connect to view"); |
| 444 | 445 |
| 445 SyntheticTapGestureParams gesture_params; | 446 SyntheticTapGestureParams gesture_params; |
| 446 const int kDefaultDuration = 50; | 447 const int kDefaultDuration = 50; |
| 447 const int kDefaultTapCount = 1; | 448 const int kDefaultTapCount = 1; |
| 448 | 449 |
| 449 gesture_params.position = CssPixelsToPointF(x, y, page_scale_factor_); | 450 gesture_params.position = CssPixelsToPointF(x, y, page_scale_factor_); |
| 450 gesture_params.duration_ms = duration ? *duration : kDefaultDuration; | 451 gesture_params.duration_ms = duration ? *duration : kDefaultDuration; |
| 451 | 452 |
| 452 if (!StringToGestureSourceType( | 453 if (!StringToGestureSourceType( |
| 453 gesture_source_type ? *gesture_source_type : kGestureSourceTypeDefault, | 454 gesture_source_type ? *gesture_source_type : kGestureSourceTypeDefault, |
| 454 gesture_params.gesture_source_type)) { | 455 gesture_params.gesture_source_type)) { |
| 455 return Response::InvalidParams("gestureSourceType"); | 456 return Response::InvalidParams("gestureSourceType"); |
| 456 } | 457 } |
| 457 | 458 |
| 458 if (!tap_count) | 459 if (!tap_count) |
| 459 tap_count = &kDefaultTapCount; | 460 tap_count = &kDefaultTapCount; |
| 460 | 461 |
| 461 for (int i = 0; i < *tap_count; i++) { | 462 for (int i = 0; i < *tap_count; i++) { |
| 462 // If we're doing more than one tap, don't send the response to the client | 463 // If we're doing more than one tap, don't send the response to the client |
| 463 // until we've completed the last tap. | 464 // until we've completed the last tap. |
| 464 bool is_last_tap = i == *tap_count - 1; | 465 bool is_last_tap = i == *tap_count - 1; |
| 465 host_->QueueSyntheticGesture( | 466 host_->GetRenderWidgetHost()->QueueSyntheticGesture( |
| 466 SyntheticGesture::Create(gesture_params), | 467 SyntheticGesture::Create(gesture_params), |
| 467 base::Bind(&InputHandler::SendSynthesizeTapGestureResponse, | 468 base::Bind(&InputHandler::SendSynthesizeTapGestureResponse, |
| 468 weak_factory_.GetWeakPtr(), command_id, is_last_tap)); | 469 weak_factory_.GetWeakPtr(), command_id, is_last_tap)); |
| 469 } | 470 } |
| 470 | 471 |
| 471 return Response::OK(); | 472 return Response::OK(); |
| 472 } | 473 } |
| 473 | 474 |
| 474 Response InputHandler::DispatchTouchEvent( | 475 Response InputHandler::DispatchTouchEvent( |
| 475 const std::string& type, | 476 const std::string& type, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 } else { | 518 } else { |
| 518 client_->SendError(command_id, | 519 client_->SendError(command_id, |
| 519 Response::InternalError(base::StringPrintf( | 520 Response::InternalError(base::StringPrintf( |
| 520 "Synthetic tap failed, result was %d", result))); | 521 "Synthetic tap failed, result was %d", result))); |
| 521 } | 522 } |
| 522 } | 523 } |
| 523 | 524 |
| 524 } // namespace input | 525 } // namespace input |
| 525 } // namespace devtools | 526 } // namespace devtools |
| 526 } // namespace content | 527 } // namespace content |
| OLD | NEW |