Chromium Code Reviews| Index: content/browser/devtools/protocol/page_handler.cc |
| diff --git a/content/browser/devtools/protocol/page_handler.cc b/content/browser/devtools/protocol/page_handler.cc |
| index c31a27ecd24d5e9ce7fe1173af498aea53808243..af076da262ae328aeaf280b765259f2b563d00ae 100644 |
| --- a/content/browser/devtools/protocol/page_handler.cc |
| +++ b/content/browser/devtools/protocol/page_handler.cc |
| @@ -4,7 +4,11 @@ |
| #include "content/browser/devtools/protocol/page_handler.h" |
| +#include <algorithm> |
| +#include <memory> |
| #include <string> |
| +#include <utility> |
| +#include <vector> |
| #include "base/base64.h" |
| #include "base/bind.h" |
| @@ -41,6 +45,7 @@ |
| #include "ui/gfx/geometry/size_conversions.h" |
| #include "ui/gfx/image/image.h" |
| #include "ui/gfx/image/image_util.h" |
| +#include "ui/gfx/switches.h" |
| #include "ui/snapshot/snapshot.h" |
| #include "url/gurl.h" |
| @@ -82,6 +87,36 @@ std::string EncodeImage(const gfx::Image& image, |
| return base_64_data; |
| } |
| +std::string EncodeBitmap(const SkBitmap& bitmap, |
|
Eric Seckler
2017/03/06 07:32:53
Don't think you need this specialized version of E
dvallet
2017/03/08 02:26:14
Done, thanks for the tip!
|
| + const std::string& format, |
| + int quality) { |
| + std::vector<unsigned char> data; |
| + SkAutoLockPixels lock_image(bitmap); |
| + bool encoded; |
| + if (format == kPng) { |
| + encoded = gfx::PNGCodec::Encode( |
| + reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| + gfx::PNGCodec::FORMAT_SkBitmap, |
| + gfx::Size(bitmap.width(), bitmap.height()), |
| + bitmap.width() * bitmap.bytesPerPixel(), false, |
| + std::vector<gfx::PNGCodec::Comment>(), &data); |
| + } else if (format == kJpeg) { |
| + encoded = gfx::JPEGCodec::Encode( |
| + reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| + gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), |
| + bitmap.width() * bitmap.bytesPerPixel(), quality, &data); |
| + } else { |
| + encoded = false; |
| + } |
| + if (!encoded) |
| + return std::string(); |
| + std::string base_64_data; |
| + base::Base64Encode( |
| + base::StringPiece(reinterpret_cast<char*>(&data[0]), data.size()), |
| + &base_64_data); |
| + return base_64_data; |
| +} |
| + |
| } // namespace |
| PageHandler::PageHandler() |
| @@ -292,7 +327,13 @@ void PageHandler::CaptureScreenshot( |
| std::string screenshot_format = format.fromMaybe(kPng); |
| int screenshot_quality = quality.fromMaybe(kDefaultScreenshotQuality); |
| - |
| + if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kHeadless)) { |
| + host_->GetRenderWidgetHost()->GetBitmapSnapshotFromBrowser(base::Bind( |
| + &PageHandler::BitmapScreenshotCaptured, weak_factory_.GetWeakPtr(), |
| + base::Passed(std::move(callback)), screenshot_format, |
| + screenshot_quality)); |
| + return; |
| + } |
| host_->GetRenderWidgetHost()->GetSnapshotFromBrowser( |
| base::Bind(&PageHandler::ScreenshotCaptured, weak_factory_.GetWeakPtr(), |
| base::Passed(std::move(callback)), screenshot_format, |
| @@ -581,6 +622,29 @@ void PageHandler::ScreenshotCaptured( |
| callback->sendSuccess(EncodeImage(image, format, quality)); |
| } |
| +void PageHandler::BitmapScreenshotCaptured( |
| + std::unique_ptr<CaptureScreenshotCallback> callback, |
| + const std::string& format, |
| + int quality, |
| + const SkBitmap& bitmap) { |
| + if (bitmap.drawsNothing()) { |
| + callback->sendFailure(Response::Error("Unable to capture screenshot")); |
| + return; |
| + } |
| + |
| + base::PostTaskAndReplyWithResult( |
| + FROM_HERE, base::Bind(&EncodeBitmap, bitmap, format, quality), |
| + base::Bind(&PageHandler::BitmapScreenshotEncoded, |
| + weak_factory_.GetWeakPtr(), |
| + base::Passed(std::move(callback)))); |
| +} |
| + |
| +void PageHandler::BitmapScreenshotEncoded( |
| + std::unique_ptr<CaptureScreenshotCallback> callback, |
| + const std::string& data) { |
| + callback->sendSuccess(data); |
| +} |
| + |
| void PageHandler::OnColorPicked(int r, int g, int b, int a) { |
| frontend_->ColorPicked( |
| DOM::RGBA::Create().SetR(r).SetG(g).SetB(b).SetA(a).Build()); |