| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/renderer_overrides_handler.h" | 5 #include "content/browser/devtools/renderer_overrides_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "content/browser/child_process_security_policy_impl.h" | 15 #include "content/browser/child_process_security_policy_impl.h" |
| 16 #include "content/browser/devtools/devtools_protocol_constants.h" | 16 #include "content/browser/devtools/devtools_protocol_constants.h" |
| 17 #include "content/browser/devtools/devtools_tracing_handler.h" | 17 #include "content/browser/devtools/devtools_tracing_handler.h" |
| 18 #include "content/browser/renderer_host/dip_util.h" | 18 #include "content/browser/renderer_host/dip_util.h" |
| 19 #include "content/browser/renderer_host/render_view_host_delegate.h" | 19 #include "content/browser/renderer_host/render_view_host_delegate.h" |
| 20 #include "content/browser/renderer_host/render_view_host_impl.h" | 20 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 21 #include "content/common/view_messages.h" |
| 21 #include "content/port/browser/render_widget_host_view_port.h" | 22 #include "content/port/browser/render_widget_host_view_port.h" |
| 22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/devtools_agent_host.h" | 24 #include "content/public/browser/devtools_agent_host.h" |
| 24 #include "content/public/browser/javascript_dialog_manager.h" | 25 #include "content/public/browser/javascript_dialog_manager.h" |
| 25 #include "content/public/browser/navigation_controller.h" | 26 #include "content/public/browser/navigation_controller.h" |
| 26 #include "content/public/browser/render_process_host.h" | 27 #include "content/public/browser/render_process_host.h" |
| 27 #include "content/public/browser/render_view_host.h" | 28 #include "content/public/browser/render_view_host.h" |
| 28 #include "content/public/browser/render_widget_host_view.h" | 29 #include "content/public/browser/render_widget_host_view.h" |
| 29 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 30 #include "content/public/browser/web_contents_delegate.h" | 31 #include "content/public/browser/web_contents_delegate.h" |
| 31 #include "content/public/common/page_transition_types.h" | 32 #include "content/public/common/page_transition_types.h" |
| 32 #include "content/public/common/referrer.h" | 33 #include "content/public/common/referrer.h" |
| 34 #include "ipc/ipc_sender.h" |
| 33 #include "third_party/WebKit/public/web/WebInputEvent.h" | 35 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 34 #include "ui/gfx/codec/jpeg_codec.h" | 36 #include "ui/gfx/codec/jpeg_codec.h" |
| 35 #include "ui/gfx/codec/png_codec.h" | 37 #include "ui/gfx/codec/png_codec.h" |
| 36 #include "ui/gfx/size_conversions.h" | 38 #include "ui/gfx/size_conversions.h" |
| 37 #include "ui/snapshot/snapshot.h" | 39 #include "ui/snapshot/snapshot.h" |
| 38 #include "url/gurl.h" | 40 #include "url/gurl.h" |
| 39 | 41 |
| 40 using WebKit::WebGestureEvent; | 42 using WebKit::WebGestureEvent; |
| 41 using WebKit::WebInputEvent; | 43 using WebKit::WebInputEvent; |
| 42 using WebKit::WebMouseEvent; | 44 using WebKit::WebMouseEvent; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 &RendererOverridesHandler::InputDispatchGestureEvent, | 143 &RendererOverridesHandler::InputDispatchGestureEvent, |
| 142 base::Unretained(this))); | 144 base::Unretained(this))); |
| 143 } | 145 } |
| 144 | 146 |
| 145 RendererOverridesHandler::~RendererOverridesHandler() {} | 147 RendererOverridesHandler::~RendererOverridesHandler() {} |
| 146 | 148 |
| 147 void RendererOverridesHandler::OnClientDetached() { | 149 void RendererOverridesHandler::OnClientDetached() { |
| 148 screencast_command_ = NULL; | 150 screencast_command_ = NULL; |
| 149 } | 151 } |
| 150 | 152 |
| 151 void RendererOverridesHandler::OnSwapCompositorFrame() { | 153 void RendererOverridesHandler::OnSwapCompositorFrame( |
| 152 if (!screencast_command_) | 154 const IPC::Message& message) { |
| 155 ViewHostMsg_SwapCompositorFrame::Param param; |
| 156 if (!ViewHostMsg_SwapCompositorFrame::Read(&message, ¶m)) |
| 153 return; | 157 return; |
| 158 last_compositor_frame_metadata_ = param.b.metadata; |
| 154 | 159 |
| 160 if (screencast_command_) |
| 161 InnerSwapCompositorFrame(); |
| 162 } |
| 163 |
| 164 void RendererOverridesHandler::InnerSwapCompositorFrame() { |
| 155 std::string format; | 165 std::string format; |
| 156 int quality = kDefaultScreenshotQuality; | 166 int quality = kDefaultScreenshotQuality; |
| 157 double scale = 1; | 167 double scale = 1; |
| 158 ParseCaptureParameters(screencast_command_.get(), &format, &quality, &scale); | 168 ParseCaptureParameters(screencast_command_.get(), &format, &quality, &scale); |
| 159 | 169 |
| 160 RenderViewHost* host = agent_->GetRenderViewHost(); | 170 RenderViewHost* host = agent_->GetRenderViewHost(); |
| 161 RenderWidgetHostViewPort* view_port = | 171 RenderWidgetHostViewPort* view_port = |
| 162 RenderWidgetHostViewPort::FromRWHV(host->GetView()); | 172 RenderWidgetHostViewPort::FromRWHV(host->GetView()); |
| 163 | 173 |
| 164 gfx::Rect view_bounds = host->GetView()->GetViewBounds(); | 174 gfx::Rect view_bounds = host->GetView()->GetViewBounds(); |
| 165 gfx::Size snapshot_size = gfx::ToFlooredSize( | 175 gfx::Size snapshot_size = gfx::ToFlooredSize( |
| 166 gfx::ScaleSize(view_bounds.size(), scale)); | 176 gfx::ScaleSize(view_bounds.size(), scale)); |
| 167 | 177 |
| 168 view_port->CopyFromCompositingSurface( | 178 view_port->CopyFromCompositingSurface( |
| 169 view_bounds, snapshot_size, | 179 view_bounds, snapshot_size, |
| 170 base::Bind(&RendererOverridesHandler::ScreenshotCaptured, | 180 base::Bind(&RendererOverridesHandler::ScreenshotCaptured, |
| 171 weak_factory_.GetWeakPtr(), | 181 weak_factory_.GetWeakPtr(), |
| 172 scoped_refptr<DevToolsProtocol::Command>(), format, quality, | 182 scoped_refptr<DevToolsProtocol::Command>(), format, quality, |
| 173 scale)); | 183 last_compositor_frame_metadata_)); |
| 174 } | 184 } |
| 175 | 185 |
| 176 | 186 |
| 177 // DOM agent handlers -------------------------------------------------------- | 187 // DOM agent handlers -------------------------------------------------------- |
| 178 | 188 |
| 179 scoped_refptr<DevToolsProtocol::Response> | 189 scoped_refptr<DevToolsProtocol::Response> |
| 180 RendererOverridesHandler::GrantPermissionsForSetFileInputFiles( | 190 RendererOverridesHandler::GrantPermissionsForSetFileInputFiles( |
| 181 scoped_refptr<DevToolsProtocol::Command> command) { | 191 scoped_refptr<DevToolsProtocol::Command> command) { |
| 182 base::DictionaryValue* params = command->params(); | 192 base::DictionaryValue* params = command->params(); |
| 183 base::ListValue* file_list = NULL; | 193 base::ListValue* file_list = NULL; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 bool is_unscaled_png = scale == 1 && format == kPng; | 292 bool is_unscaled_png = scale == 1 && format == kPng; |
| 283 if (is_unscaled_png && ui::GrabViewSnapshot(host->GetView()->GetNativeView(), | 293 if (is_unscaled_png && ui::GrabViewSnapshot(host->GetView()->GetNativeView(), |
| 284 &png, view_bounds)) { | 294 &png, view_bounds)) { |
| 285 std::string base64_data; | 295 std::string base64_data; |
| 286 bool success = base::Base64Encode( | 296 bool success = base::Base64Encode( |
| 287 base::StringPiece(reinterpret_cast<char*>(&*png.begin()), png.size()), | 297 base::StringPiece(reinterpret_cast<char*>(&*png.begin()), png.size()), |
| 288 &base64_data); | 298 &base64_data); |
| 289 if (success) { | 299 if (success) { |
| 290 base::DictionaryValue* result = new base::DictionaryValue(); | 300 base::DictionaryValue* result = new base::DictionaryValue(); |
| 291 result->SetString( | 301 result->SetString( |
| 292 devtools::Page::captureScreenshot::kResponseData, base64_data); | 302 devtools::Page::kData, base64_data); |
| 293 return command->SuccessResponse(result); | 303 return command->SuccessResponse(result); |
| 294 } | 304 } |
| 295 return command->InternalErrorResponse("Unable to base64encode screenshot"); | 305 return command->InternalErrorResponse("Unable to base64encode screenshot"); |
| 296 } | 306 } |
| 297 | 307 |
| 298 // Fallback to copying from compositing surface. | 308 // Fallback to copying from compositing surface. |
| 299 RenderWidgetHostViewPort* view_port = | 309 RenderWidgetHostViewPort* view_port = |
| 300 RenderWidgetHostViewPort::FromRWHV(host->GetView()); | 310 RenderWidgetHostViewPort::FromRWHV(host->GetView()); |
| 301 | 311 |
| 302 gfx::Size snapshot_size = gfx::ToFlooredSize( | 312 gfx::Size snapshot_size = gfx::ToFlooredSize( |
| 303 gfx::ScaleSize(view_bounds.size(), scale)); | 313 gfx::ScaleSize(view_bounds.size(), scale)); |
| 304 view_port->CopyFromCompositingSurface( | 314 view_port->CopyFromCompositingSurface( |
| 305 view_bounds, snapshot_size, | 315 view_bounds, snapshot_size, |
| 306 base::Bind(&RendererOverridesHandler::ScreenshotCaptured, | 316 base::Bind(&RendererOverridesHandler::ScreenshotCaptured, |
| 307 weak_factory_.GetWeakPtr(), command, format, quality, scale)); | 317 weak_factory_.GetWeakPtr(), command, format, quality, |
| 318 last_compositor_frame_metadata_)); |
| 308 return command->AsyncResponsePromise(); | 319 return command->AsyncResponsePromise(); |
| 309 } | 320 } |
| 310 | 321 |
| 311 scoped_refptr<DevToolsProtocol::Response> | 322 scoped_refptr<DevToolsProtocol::Response> |
| 312 RendererOverridesHandler::PageStartScreencast( | 323 RendererOverridesHandler::PageStartScreencast( |
| 313 scoped_refptr<DevToolsProtocol::Command> command) { | 324 scoped_refptr<DevToolsProtocol::Command> command) { |
| 314 screencast_command_ = command; | 325 screencast_command_ = command; |
| 315 OnSwapCompositorFrame(); | 326 InnerSwapCompositorFrame(); |
| 316 return command->SuccessResponse(NULL); | 327 return command->SuccessResponse(NULL); |
| 317 } | 328 } |
| 318 | 329 |
| 319 scoped_refptr<DevToolsProtocol::Response> | 330 scoped_refptr<DevToolsProtocol::Response> |
| 320 RendererOverridesHandler::PageStopScreencast( | 331 RendererOverridesHandler::PageStopScreencast( |
| 321 scoped_refptr<DevToolsProtocol::Command> command) { | 332 scoped_refptr<DevToolsProtocol::Command> command) { |
| 322 screencast_command_ = NULL; | 333 screencast_command_ = NULL; |
| 323 return command->SuccessResponse(NULL); | 334 return command->SuccessResponse(NULL); |
| 324 } | 335 } |
| 325 | 336 |
| 326 void RendererOverridesHandler::ScreenshotCaptured( | 337 void RendererOverridesHandler::ScreenshotCaptured( |
| 327 scoped_refptr<DevToolsProtocol::Command> command, | 338 scoped_refptr<DevToolsProtocol::Command> command, |
| 328 const std::string& format, | 339 const std::string& format, |
| 329 int quality, | 340 int quality, |
| 330 double scale, | 341 const cc::CompositorFrameMetadata& metadata, |
| 331 bool success, | 342 bool success, |
| 332 const SkBitmap& bitmap) { | 343 const SkBitmap& bitmap) { |
| 333 if (!success) { | 344 if (!success) { |
| 334 if (command) { | 345 if (command) { |
| 335 SendAsyncResponse( | 346 SendAsyncResponse( |
| 336 command->InternalErrorResponse("Unable to capture screenshot")); | 347 command->InternalErrorResponse("Unable to capture screenshot")); |
| 337 } | 348 } |
| 338 return; | 349 return; |
| 339 } | 350 } |
| 340 | 351 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 data.size()), | 385 data.size()), |
| 375 &base_64_data)) { | 386 &base_64_data)) { |
| 376 if (command) { | 387 if (command) { |
| 377 SendAsyncResponse( | 388 SendAsyncResponse( |
| 378 command->InternalErrorResponse("Unable to base64 encode")); | 389 command->InternalErrorResponse("Unable to base64 encode")); |
| 379 } | 390 } |
| 380 return; | 391 return; |
| 381 } | 392 } |
| 382 | 393 |
| 383 base::DictionaryValue* response = new base::DictionaryValue(); | 394 base::DictionaryValue* response = new base::DictionaryValue(); |
| 395 response->SetString(devtools::Page::kData, base_64_data); |
| 396 |
| 397 // Consider metadata empty in case it has no device scale factor. |
| 398 if (metadata.device_scale_factor != 0) { |
| 399 response->SetDouble(devtools::Page::kParamDeviceScaleFactor, |
| 400 metadata.device_scale_factor); |
| 401 response->SetDouble(devtools::Page::kParamPageScaleFactor, |
| 402 metadata.page_scale_factor); |
| 403 |
| 404 base::DictionaryValue* viewport = new base::DictionaryValue(); |
| 405 viewport->SetDouble(devtools::kParamX, metadata.root_scroll_offset.x()); |
| 406 viewport->SetDouble(devtools::kParamY, metadata.root_scroll_offset.y()); |
| 407 viewport->SetDouble(devtools::kParamWidth, metadata.viewport_size.width()); |
| 408 viewport->SetDouble(devtools::kParamHeight, |
| 409 metadata.viewport_size.height()); |
| 410 response->Set(devtools::Page::kParamViewport, viewport); |
| 411 } |
| 412 |
| 384 if (command) { | 413 if (command) { |
| 385 response->SetString( | |
| 386 devtools::Page::captureScreenshot::kResponseData, base_64_data); | |
| 387 SendAsyncResponse(command->SuccessResponse(response)); | 414 SendAsyncResponse(command->SuccessResponse(response)); |
| 388 } else { | 415 } else { |
| 389 response->SetString( | |
| 390 devtools::Page::screencastFrame::kResponseData, base_64_data); | |
| 391 SendNotification(devtools::Page::screencastFrame::kName, response); | 416 SendNotification(devtools::Page::screencastFrame::kName, response); |
| 392 } | 417 } |
| 393 } | 418 } |
| 394 | 419 |
| 395 | 420 |
| 396 // Input agent handlers ------------------------------------------------------ | 421 // Input agent handlers ------------------------------------------------------ |
| 397 | 422 |
| 398 scoped_refptr<DevToolsProtocol::Response> | 423 scoped_refptr<DevToolsProtocol::Response> |
| 399 RendererOverridesHandler::InputDispatchMouseEvent( | 424 RendererOverridesHandler::InputDispatchMouseEvent( |
| 400 scoped_refptr<DevToolsProtocol::Command> command) { | 425 scoped_refptr<DevToolsProtocol::Command> command) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 423 else if (type == "mouseMoved") | 448 else if (type == "mouseMoved") |
| 424 mouse_event.type = WebInputEvent::MouseMove; | 449 mouse_event.type = WebInputEvent::MouseMove; |
| 425 else | 450 else |
| 426 return NULL; | 451 return NULL; |
| 427 } else { | 452 } else { |
| 428 return NULL; | 453 return NULL; |
| 429 } | 454 } |
| 430 | 455 |
| 431 int x; | 456 int x; |
| 432 int y; | 457 int y; |
| 433 if (!params->GetInteger(devtools::Input::kParamX, &x) || | 458 if (!params->GetInteger(devtools::kParamX, &x) || |
| 434 !params->GetInteger(devtools::Input::kParamY, &y)) { | 459 !params->GetInteger(devtools::kParamY, &y)) { |
| 435 return NULL; | 460 return NULL; |
| 436 } | 461 } |
| 437 | 462 |
| 438 float device_scale_factor = ui::GetScaleFactorScale( | 463 float device_scale_factor = ui::GetScaleFactorScale( |
| 439 GetScaleFactorForView(host->GetView())); | 464 GetScaleFactorForView(host->GetView())); |
| 440 mouse_event.x = floor(x / device_scale_factor); | 465 mouse_event.x = floor(x / device_scale_factor); |
| 441 mouse_event.y = floor(y / device_scale_factor); | 466 mouse_event.y = floor(y / device_scale_factor); |
| 442 mouse_event.windowX = mouse_event.x; | 467 mouse_event.windowX = mouse_event.x; |
| 443 mouse_event.windowY = mouse_event.y; | 468 mouse_event.windowY = mouse_event.y; |
| 444 mouse_event.globalX = mouse_event.x; | 469 mouse_event.globalX = mouse_event.x; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 return NULL; | 532 return NULL; |
| 508 } else { | 533 } else { |
| 509 return NULL; | 534 return NULL; |
| 510 } | 535 } |
| 511 | 536 |
| 512 float device_scale_factor = ui::GetScaleFactorScale( | 537 float device_scale_factor = ui::GetScaleFactorScale( |
| 513 GetScaleFactorForView(host->GetView())); | 538 GetScaleFactorForView(host->GetView())); |
| 514 | 539 |
| 515 int x; | 540 int x; |
| 516 int y; | 541 int y; |
| 517 if (!params->GetInteger(devtools::Input::kParamX, &x) || | 542 if (!params->GetInteger(devtools::kParamX, &x) || |
| 518 !params->GetInteger(devtools::Input::kParamY, &y)) { | 543 !params->GetInteger(devtools::kParamY, &y)) { |
| 519 return NULL; | 544 return NULL; |
| 520 } | 545 } |
| 521 event.x = floor(x / device_scale_factor); | 546 event.x = floor(x / device_scale_factor); |
| 522 event.y = floor(y / device_scale_factor); | 547 event.y = floor(y / device_scale_factor); |
| 523 event.globalX = event.x; | 548 event.globalX = event.x; |
| 524 event.globalY = event.y; | 549 event.globalY = event.y; |
| 525 | 550 |
| 526 if (type == "scrollUpdate") { | 551 if (type == "scrollUpdate") { |
| 527 int dx; | 552 int dx; |
| 528 int dy; | 553 int dy; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 544 return NULL; | 569 return NULL; |
| 545 } | 570 } |
| 546 event.data.pinchUpdate.scale = static_cast<float>(scale); | 571 event.data.pinchUpdate.scale = static_cast<float>(scale); |
| 547 } | 572 } |
| 548 | 573 |
| 549 host->ForwardGestureEvent(event); | 574 host->ForwardGestureEvent(event); |
| 550 return command->SuccessResponse(NULL); | 575 return command->SuccessResponse(NULL); |
| 551 } | 576 } |
| 552 | 577 |
| 553 } // namespace content | 578 } // namespace content |
| OLD | NEW |