Chromium Code Reviews| 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 <algorithm> | |
| 8 #include <memory> | |
| 7 #include <string> | 9 #include <string> |
| 10 #include <utility> | |
| 11 #include <vector> | |
| 8 | 12 |
| 9 #include "base/base64.h" | 13 #include "base/base64.h" |
| 10 #include "base/bind.h" | 14 #include "base/bind.h" |
| 11 #include "base/location.h" | 15 #include "base/location.h" |
| 12 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/ref_counted_memory.h" | 17 #include "base/memory/ref_counted_memory.h" |
| 14 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 15 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/task_scheduler/post_task.h" | 21 #include "base/task_scheduler/post_task.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 34 #include "content/public/browser/storage_partition.h" | 38 #include "content/public/browser/storage_partition.h" |
| 35 #include "content/public/browser/web_contents_delegate.h" | 39 #include "content/public/browser/web_contents_delegate.h" |
| 36 #include "content/public/common/referrer.h" | 40 #include "content/public/common/referrer.h" |
| 37 #include "third_party/skia/include/core/SkBitmap.h" | 41 #include "third_party/skia/include/core/SkBitmap.h" |
| 38 #include "ui/base/page_transition_types.h" | 42 #include "ui/base/page_transition_types.h" |
| 39 #include "ui/gfx/codec/jpeg_codec.h" | 43 #include "ui/gfx/codec/jpeg_codec.h" |
| 40 #include "ui/gfx/codec/png_codec.h" | 44 #include "ui/gfx/codec/png_codec.h" |
| 41 #include "ui/gfx/geometry/size_conversions.h" | 45 #include "ui/gfx/geometry/size_conversions.h" |
| 42 #include "ui/gfx/image/image.h" | 46 #include "ui/gfx/image/image.h" |
| 43 #include "ui/gfx/image/image_util.h" | 47 #include "ui/gfx/image/image_util.h" |
| 48 #include "ui/gfx/switches.h" | |
| 44 #include "ui/snapshot/snapshot.h" | 49 #include "ui/snapshot/snapshot.h" |
| 45 #include "url/gurl.h" | 50 #include "url/gurl.h" |
| 46 | 51 |
| 47 namespace content { | 52 namespace content { |
| 48 namespace protocol { | 53 namespace protocol { |
| 49 | 54 |
| 50 namespace { | 55 namespace { |
| 51 | 56 |
| 52 static const char kPng[] = "png"; | 57 static const char kPng[] = "png"; |
| 53 static const char kJpeg[] = "jpeg"; | 58 static const char kJpeg[] = "jpeg"; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 75 | 80 |
| 76 std::string base_64_data; | 81 std::string base_64_data; |
| 77 base::Base64Encode( | 82 base::Base64Encode( |
| 78 base::StringPiece(reinterpret_cast<const char*>(data->front()), | 83 base::StringPiece(reinterpret_cast<const char*>(data->front()), |
| 79 data->size()), | 84 data->size()), |
| 80 &base_64_data); | 85 &base_64_data); |
| 81 | 86 |
| 82 return base_64_data; | 87 return base_64_data; |
| 83 } | 88 } |
| 84 | 89 |
| 90 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!
| |
| 91 const std::string& format, | |
| 92 int quality) { | |
| 93 std::vector<unsigned char> data; | |
| 94 SkAutoLockPixels lock_image(bitmap); | |
| 95 bool encoded; | |
| 96 if (format == kPng) { | |
| 97 encoded = gfx::PNGCodec::Encode( | |
| 98 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
| 99 gfx::PNGCodec::FORMAT_SkBitmap, | |
| 100 gfx::Size(bitmap.width(), bitmap.height()), | |
| 101 bitmap.width() * bitmap.bytesPerPixel(), false, | |
| 102 std::vector<gfx::PNGCodec::Comment>(), &data); | |
| 103 } else if (format == kJpeg) { | |
| 104 encoded = gfx::JPEGCodec::Encode( | |
| 105 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
| 106 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), | |
| 107 bitmap.width() * bitmap.bytesPerPixel(), quality, &data); | |
| 108 } else { | |
| 109 encoded = false; | |
| 110 } | |
| 111 if (!encoded) | |
| 112 return std::string(); | |
| 113 std::string base_64_data; | |
| 114 base::Base64Encode( | |
| 115 base::StringPiece(reinterpret_cast<char*>(&data[0]), data.size()), | |
| 116 &base_64_data); | |
| 117 return base_64_data; | |
| 118 } | |
| 119 | |
| 85 } // namespace | 120 } // namespace |
| 86 | 121 |
| 87 PageHandler::PageHandler() | 122 PageHandler::PageHandler() |
| 88 : DevToolsDomainHandler(Page::Metainfo::domainName), | 123 : DevToolsDomainHandler(Page::Metainfo::domainName), |
| 89 enabled_(false), | 124 enabled_(false), |
| 90 screencast_enabled_(false), | 125 screencast_enabled_(false), |
| 91 screencast_quality_(kDefaultScreenshotQuality), | 126 screencast_quality_(kDefaultScreenshotQuality), |
| 92 screencast_max_width_(-1), | 127 screencast_max_width_(-1), |
| 93 screencast_max_height_(-1), | 128 screencast_max_height_(-1), |
| 94 capture_every_nth_frame_(1), | 129 capture_every_nth_frame_(1), |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 285 Maybe<std::string> format, | 320 Maybe<std::string> format, |
| 286 Maybe<int> quality, | 321 Maybe<int> quality, |
| 287 std::unique_ptr<CaptureScreenshotCallback> callback) { | 322 std::unique_ptr<CaptureScreenshotCallback> callback) { |
| 288 if (!host_ || !host_->GetRenderWidgetHost()) { | 323 if (!host_ || !host_->GetRenderWidgetHost()) { |
| 289 callback->sendFailure(Response::InternalError()); | 324 callback->sendFailure(Response::InternalError()); |
| 290 return; | 325 return; |
| 291 } | 326 } |
| 292 | 327 |
| 293 std::string screenshot_format = format.fromMaybe(kPng); | 328 std::string screenshot_format = format.fromMaybe(kPng); |
| 294 int screenshot_quality = quality.fromMaybe(kDefaultScreenshotQuality); | 329 int screenshot_quality = quality.fromMaybe(kDefaultScreenshotQuality); |
| 295 | 330 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kHeadless)) { |
| 331 host_->GetRenderWidgetHost()->GetBitmapSnapshotFromBrowser(base::Bind( | |
| 332 &PageHandler::BitmapScreenshotCaptured, weak_factory_.GetWeakPtr(), | |
| 333 base::Passed(std::move(callback)), screenshot_format, | |
| 334 screenshot_quality)); | |
| 335 return; | |
| 336 } | |
| 296 host_->GetRenderWidgetHost()->GetSnapshotFromBrowser( | 337 host_->GetRenderWidgetHost()->GetSnapshotFromBrowser( |
| 297 base::Bind(&PageHandler::ScreenshotCaptured, weak_factory_.GetWeakPtr(), | 338 base::Bind(&PageHandler::ScreenshotCaptured, weak_factory_.GetWeakPtr(), |
| 298 base::Passed(std::move(callback)), screenshot_format, | 339 base::Passed(std::move(callback)), screenshot_format, |
| 299 screenshot_quality)); | 340 screenshot_quality)); |
| 300 } | 341 } |
| 301 | 342 |
| 302 void PageHandler::PrintToPDF(std::unique_ptr<PrintToPDFCallback> callback) { | 343 void PageHandler::PrintToPDF(std::unique_ptr<PrintToPDFCallback> callback) { |
| 303 callback->sendFailure(Response::Error("PrintToPDF is not implemented")); | 344 callback->sendFailure(Response::Error("PrintToPDF is not implemented")); |
| 304 return; | 345 return; |
| 305 } | 346 } |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 574 int quality, | 615 int quality, |
| 575 const gfx::Image& image) { | 616 const gfx::Image& image) { |
| 576 if (image.IsEmpty()) { | 617 if (image.IsEmpty()) { |
| 577 callback->sendFailure(Response::Error("Unable to capture screenshot")); | 618 callback->sendFailure(Response::Error("Unable to capture screenshot")); |
| 578 return; | 619 return; |
| 579 } | 620 } |
| 580 | 621 |
| 581 callback->sendSuccess(EncodeImage(image, format, quality)); | 622 callback->sendSuccess(EncodeImage(image, format, quality)); |
| 582 } | 623 } |
| 583 | 624 |
| 625 void PageHandler::BitmapScreenshotCaptured( | |
| 626 std::unique_ptr<CaptureScreenshotCallback> callback, | |
| 627 const std::string& format, | |
| 628 int quality, | |
| 629 const SkBitmap& bitmap) { | |
| 630 if (bitmap.drawsNothing()) { | |
| 631 callback->sendFailure(Response::Error("Unable to capture screenshot")); | |
| 632 return; | |
| 633 } | |
| 634 | |
| 635 base::PostTaskAndReplyWithResult( | |
| 636 FROM_HERE, base::Bind(&EncodeBitmap, bitmap, format, quality), | |
| 637 base::Bind(&PageHandler::BitmapScreenshotEncoded, | |
| 638 weak_factory_.GetWeakPtr(), | |
| 639 base::Passed(std::move(callback)))); | |
| 640 } | |
| 641 | |
| 642 void PageHandler::BitmapScreenshotEncoded( | |
| 643 std::unique_ptr<CaptureScreenshotCallback> callback, | |
| 644 const std::string& data) { | |
| 645 callback->sendSuccess(data); | |
| 646 } | |
| 647 | |
| 584 void PageHandler::OnColorPicked(int r, int g, int b, int a) { | 648 void PageHandler::OnColorPicked(int r, int g, int b, int a) { |
| 585 frontend_->ColorPicked( | 649 frontend_->ColorPicked( |
| 586 DOM::RGBA::Create().SetR(r).SetG(g).SetB(b).SetA(a).Build()); | 650 DOM::RGBA::Create().SetR(r).SetG(g).SetB(b).SetA(a).Build()); |
| 587 } | 651 } |
| 588 | 652 |
| 589 Response PageHandler::StopLoading() { | 653 Response PageHandler::StopLoading() { |
| 590 WebContentsImpl* web_contents = GetWebContents(); | 654 WebContentsImpl* web_contents = GetWebContents(); |
| 591 if (!web_contents) | 655 if (!web_contents) |
| 592 return Response::InternalError(); | 656 return Response::InternalError(); |
| 593 web_contents->Stop(); | 657 web_contents->Stop(); |
| 594 return Response::OK(); | 658 return Response::OK(); |
| 595 } | 659 } |
| 596 | 660 |
| 597 } // namespace protocol | 661 } // namespace protocol |
| 598 } // namespace content | 662 } // namespace content |
| OLD | NEW |