| 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/page_handler.h" | 5 #include "content/browser/devtools/protocol/page_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" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 | 44 |
| 45 namespace { | 45 namespace { |
| 46 | 46 |
| 47 static const char kPng[] = "png"; | 47 static const char kPng[] = "png"; |
| 48 static const char kJpeg[] = "jpeg"; | 48 static const char kJpeg[] = "jpeg"; |
| 49 static int kDefaultScreenshotQuality = 80; | 49 static int kDefaultScreenshotQuality = 80; |
| 50 static int kFrameRetryDelayMs = 100; | 50 static int kFrameRetryDelayMs = 100; |
| 51 static int kCaptureRetryLimit = 2; | 51 static int kCaptureRetryLimit = 2; |
| 52 static int kMaxScreencastFramesInFlight = 2; | 52 static int kMaxScreencastFramesInFlight = 2; |
| 53 | 53 |
| 54 std::string EncodeScreencastFrame(const SkBitmap& bitmap, | 54 std::string EncodeBitmap(const SkBitmap& bitmap, |
| 55 const std::string& format, | 55 const std::string& format, |
| 56 int quality) { | 56 int quality) { |
| 57 std::vector<unsigned char> data; | 57 std::vector<unsigned char> data; |
| 58 SkAutoLockPixels lock_image(bitmap); | 58 SkAutoLockPixels lock_image(bitmap); |
| 59 bool encoded; | 59 bool encoded; |
| 60 if (format == kPng) { | 60 if (format == kPng) { |
| 61 encoded = gfx::PNGCodec::Encode( | 61 encoded = gfx::PNGCodec::Encode( |
| 62 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 62 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| 63 gfx::PNGCodec::FORMAT_SkBitmap, | 63 gfx::PNGCodec::FORMAT_SkBitmap, |
| 64 gfx::Size(bitmap.width(), bitmap.height()), | 64 gfx::Size(bitmap.width(), bitmap.height()), |
| 65 bitmap.width() * bitmap.bytesPerPixel(), | 65 bitmap.width() * bitmap.bytesPerPixel(), |
| 66 false, std::vector<gfx::PNGCodec::Comment>(), &data); | 66 false, std::vector<gfx::PNGCodec::Comment>(), &data); |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 if (controller.GetEntryAtIndex(i)->GetUniqueID() == entry_id) { | 270 if (controller.GetEntryAtIndex(i)->GetUniqueID() == entry_id) { |
| 271 controller.GoToIndex(i); | 271 controller.GoToIndex(i); |
| 272 return Response::OK(); | 272 return Response::OK(); |
| 273 } | 273 } |
| 274 } | 274 } |
| 275 | 275 |
| 276 return Response::InvalidParams("No entry with passed id"); | 276 return Response::InvalidParams("No entry with passed id"); |
| 277 } | 277 } |
| 278 | 278 |
| 279 void PageHandler::CaptureScreenshot( | 279 void PageHandler::CaptureScreenshot( |
| 280 Maybe<std::string> format, |
| 281 Maybe<int> quality, |
| 280 std::unique_ptr<CaptureScreenshotCallback> callback) { | 282 std::unique_ptr<CaptureScreenshotCallback> callback) { |
| 281 if (!host_ || !host_->GetRenderWidgetHost()) { | 283 if (!host_ || !host_->GetRenderWidgetHost()) { |
| 282 callback->sendFailure(Response::InternalError()); | 284 callback->sendFailure(Response::InternalError()); |
| 283 return; | 285 return; |
| 284 } | 286 } |
| 285 | 287 |
| 288 std::string screenshot_format = format.fromMaybe(kPng); |
| 289 int screenshot_quality = quality.fromMaybe(kDefaultScreenshotQuality); |
| 290 |
| 286 host_->GetRenderWidgetHost()->GetSnapshotFromBrowser( | 291 host_->GetRenderWidgetHost()->GetSnapshotFromBrowser( |
| 287 base::Bind(&PageHandler::ScreenshotCaptured, | 292 base::Bind(&PageHandler::ScreenshotCaptured, weak_factory_.GetWeakPtr(), |
| 288 weak_factory_.GetWeakPtr(), base::Passed(std::move(callback)))); | 293 base::Passed(std::move(callback)), screenshot_format, |
| 294 screenshot_quality)); |
| 289 } | 295 } |
| 290 | 296 |
| 291 Response PageHandler::StartScreencast(Maybe<std::string> format, | 297 Response PageHandler::StartScreencast(Maybe<std::string> format, |
| 292 Maybe<int> quality, | 298 Maybe<int> quality, |
| 293 Maybe<int> max_width, | 299 Maybe<int> max_width, |
| 294 Maybe<int> max_height, | 300 Maybe<int> max_height, |
| 295 Maybe<int> every_nth_frame) { | 301 Maybe<int> every_nth_frame) { |
| 296 RenderWidgetHostImpl* widget_host = | 302 RenderWidgetHostImpl* widget_host = |
| 297 host_ ? host_->GetRenderWidgetHost() : nullptr; | 303 host_ ? host_->GetRenderWidgetHost() : nullptr; |
| 298 if (!widget_host) | 304 if (!widget_host) |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 509 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 504 FROM_HERE, base::Bind(&PageHandler::InnerSwapCompositorFrame, | 510 FROM_HERE, base::Bind(&PageHandler::InnerSwapCompositorFrame, |
| 505 weak_factory_.GetWeakPtr()), | 511 weak_factory_.GetWeakPtr()), |
| 506 base::TimeDelta::FromMilliseconds(kFrameRetryDelayMs)); | 512 base::TimeDelta::FromMilliseconds(kFrameRetryDelayMs)); |
| 507 } | 513 } |
| 508 --frames_in_flight_; | 514 --frames_in_flight_; |
| 509 return; | 515 return; |
| 510 } | 516 } |
| 511 base::PostTaskAndReplyWithResult( | 517 base::PostTaskAndReplyWithResult( |
| 512 base::WorkerPool::GetTaskRunner(true).get(), FROM_HERE, | 518 base::WorkerPool::GetTaskRunner(true).get(), FROM_HERE, |
| 513 base::Bind(&EncodeScreencastFrame, bitmap, screencast_format_, | 519 base::Bind(&EncodeBitmap, bitmap, screencast_format_, |
| 514 screencast_quality_), | 520 screencast_quality_), |
| 515 base::Bind(&PageHandler::ScreencastFrameEncoded, | 521 base::Bind(&PageHandler::ScreencastFrameEncoded, |
| 516 weak_factory_.GetWeakPtr(), base::Passed(&metadata), | 522 weak_factory_.GetWeakPtr(), base::Passed(&metadata), |
| 517 base::Time::Now())); | 523 base::Time::Now())); |
| 518 } | 524 } |
| 519 | 525 |
| 520 void PageHandler::ScreencastFrameEncoded(cc::CompositorFrameMetadata metadata, | 526 void PageHandler::ScreencastFrameEncoded(cc::CompositorFrameMetadata metadata, |
| 521 const base::Time& timestamp, | 527 const base::Time& timestamp, |
| 522 const std::string& data) { | 528 const std::string& data) { |
| 523 // Consider metadata empty in case it has no device scale factor. | 529 // Consider metadata empty in case it has no device scale factor. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 545 .SetDeviceHeight(screen_size_dip.height()) | 551 .SetDeviceHeight(screen_size_dip.height()) |
| 546 .SetScrollOffsetX(metadata.root_scroll_offset.x()) | 552 .SetScrollOffsetX(metadata.root_scroll_offset.x()) |
| 547 .SetScrollOffsetY(metadata.root_scroll_offset.y()) | 553 .SetScrollOffsetY(metadata.root_scroll_offset.y()) |
| 548 .SetTimestamp(timestamp.ToDoubleT()) | 554 .SetTimestamp(timestamp.ToDoubleT()) |
| 549 .Build(); | 555 .Build(); |
| 550 frontend_->ScreencastFrame(data, std::move(param_metadata), session_id_); | 556 frontend_->ScreencastFrame(data, std::move(param_metadata), session_id_); |
| 551 } | 557 } |
| 552 | 558 |
| 553 void PageHandler::ScreenshotCaptured( | 559 void PageHandler::ScreenshotCaptured( |
| 554 std::unique_ptr<CaptureScreenshotCallback> callback, | 560 std::unique_ptr<CaptureScreenshotCallback> callback, |
| 555 const unsigned char* png_data, | 561 const std::string& format, |
| 556 size_t png_size) { | 562 int quality, |
| 557 if (!png_data || !png_size) { | 563 const SkBitmap& bitmap) { |
| 564 if (bitmap.drawsNothing()) { |
| 558 callback->sendFailure(Response::Error("Unable to capture screenshot")); | 565 callback->sendFailure(Response::Error("Unable to capture screenshot")); |
| 559 return; | 566 return; |
| 560 } | 567 } |
| 561 | 568 |
| 562 std::string base_64_data; | 569 base::PostTaskAndReplyWithResult( |
| 563 base::Base64Encode( | 570 base::WorkerPool::GetTaskRunner(true).get(), FROM_HERE, |
| 564 base::StringPiece(reinterpret_cast<const char*>(png_data), png_size), | 571 base::Bind(&EncodeBitmap, bitmap, format, quality), |
| 565 &base_64_data); | 572 base::Bind(&PageHandler::ScreenshotEncoded, weak_factory_.GetWeakPtr(), |
| 566 callback->sendSuccess(base_64_data); | 573 base::Passed(std::move(callback)))); |
| 574 } |
| 575 |
| 576 void PageHandler::ScreenshotEncoded( |
| 577 std::unique_ptr<CaptureScreenshotCallback> callback, |
| 578 const std::string& data) { |
| 579 callback->sendSuccess(data); |
| 567 } | 580 } |
| 568 | 581 |
| 569 void PageHandler::OnColorPicked(int r, int g, int b, int a) { | 582 void PageHandler::OnColorPicked(int r, int g, int b, int a) { |
| 570 frontend_->ColorPicked( | 583 frontend_->ColorPicked( |
| 571 DOM::RGBA::Create().SetR(r).SetG(g).SetB(b).SetA(a).Build()); | 584 DOM::RGBA::Create().SetR(r).SetG(g).SetB(b).SetA(a).Build()); |
| 572 } | 585 } |
| 573 | 586 |
| 574 Response PageHandler::StopLoading() { | 587 Response PageHandler::StopLoading() { |
| 575 WebContentsImpl* web_contents = GetWebContents(); | 588 WebContentsImpl* web_contents = GetWebContents(); |
| 576 if (!web_contents) | 589 if (!web_contents) |
| 577 return Response::InternalError(); | 590 return Response::InternalError(); |
| 578 web_contents->Stop(); | 591 web_contents->Stop(); |
| 579 return Response::OK(); | 592 return Response::OK(); |
| 580 } | 593 } |
| 581 | 594 |
| 582 } // namespace protocol | 595 } // namespace protocol |
| 583 } // namespace content | 596 } // namespace content |
| OLD | NEW |