Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: content/browser/devtools/protocol/page_handler.cc

Issue 2732923002: Add fromSurface optional parameter to devtools Page.CaptureScreenshot (Closed)
Patch Set: Remove switches include Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 return Response::OK(); 282 return Response::OK();
278 } 283 }
279 } 284 }
280 285
281 return Response::InvalidParams("No entry with passed id"); 286 return Response::InvalidParams("No entry with passed id");
282 } 287 }
283 288
284 void PageHandler::CaptureScreenshot( 289 void PageHandler::CaptureScreenshot(
285 Maybe<std::string> format, 290 Maybe<std::string> format,
286 Maybe<int> quality, 291 Maybe<int> quality,
292 Maybe<bool> from_surface,
287 std::unique_ptr<CaptureScreenshotCallback> callback) { 293 std::unique_ptr<CaptureScreenshotCallback> callback) {
288 if (!host_ || !host_->GetRenderWidgetHost()) { 294 if (!host_ || !host_->GetRenderWidgetHost()) {
289 callback->sendFailure(Response::InternalError()); 295 callback->sendFailure(Response::InternalError());
290 return; 296 return;
291 } 297 }
292 298
293 std::string screenshot_format = format.fromMaybe(kPng); 299 std::string screenshot_format = format.fromMaybe(kPng);
294 int screenshot_quality = quality.fromMaybe(kDefaultScreenshotQuality); 300 int screenshot_quality = quality.fromMaybe(kDefaultScreenshotQuality);
301 bool screenshot_from_surface = from_surface.fromMaybe(false);
295 302
303 if (screenshot_from_surface) {
304 host_->GetRenderWidgetHost()->GetSurfaceSnapshotFromBrowser(base::Bind(
305 &PageHandler::ScreenshotFromSurfaceCaptured, weak_factory_.GetWeakPtr(),
306 base::Passed(std::move(callback)), screenshot_format,
307 screenshot_quality));
308 return;
309 }
296 host_->GetRenderWidgetHost()->GetSnapshotFromBrowser( 310 host_->GetRenderWidgetHost()->GetSnapshotFromBrowser(
297 base::Bind(&PageHandler::ScreenshotCaptured, weak_factory_.GetWeakPtr(), 311 base::Bind(&PageHandler::ScreenshotCaptured, weak_factory_.GetWeakPtr(),
298 base::Passed(std::move(callback)), screenshot_format, 312 base::Passed(std::move(callback)), screenshot_format,
299 screenshot_quality)); 313 screenshot_quality));
300 } 314 }
301 315
302 void PageHandler::PrintToPDF(std::unique_ptr<PrintToPDFCallback> callback) { 316 void PageHandler::PrintToPDF(std::unique_ptr<PrintToPDFCallback> callback) {
303 callback->sendFailure(Response::Error("PrintToPDF is not implemented")); 317 callback->sendFailure(Response::Error("PrintToPDF is not implemented"));
304 return; 318 return;
305 } 319 }
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 int quality, 588 int quality,
575 const gfx::Image& image) { 589 const gfx::Image& image) {
576 if (image.IsEmpty()) { 590 if (image.IsEmpty()) {
577 callback->sendFailure(Response::Error("Unable to capture screenshot")); 591 callback->sendFailure(Response::Error("Unable to capture screenshot"));
578 return; 592 return;
579 } 593 }
580 594
581 callback->sendSuccess(EncodeImage(image, format, quality)); 595 callback->sendSuccess(EncodeImage(image, format, quality));
582 } 596 }
583 597
598 void PageHandler::ScreenshotFromSurfaceCaptured(
599 std::unique_ptr<CaptureScreenshotCallback> callback,
600 const std::string& format,
601 int quality,
602 const SkBitmap& bitmap) {
603 if (bitmap.drawsNothing()) {
604 callback->sendFailure(Response::Error("Unable to capture screenshot"));
605 return;
606 }
607
608 base::PostTaskAndReplyWithResult(
609 FROM_HERE,
610 base::Bind(&EncodeImage, gfx::Image::CreateFrom1xBitmap(bitmap), format,
611 quality),
612 base::Bind(&PageHandler::ScreenshotFromSurfaceEncoded,
dgozman 2017/03/08 19:21:36 You can bind directly to callback: base::Bind(&Ca
dvallet 2017/03/09 06:19:47 Done.
613 weak_factory_.GetWeakPtr(),
614 base::Passed(std::move(callback))));
615 }
616
617 void PageHandler::ScreenshotFromSurfaceEncoded(
618 std::unique_ptr<CaptureScreenshotCallback> callback,
619 const std::string& data) {
620 callback->sendSuccess(data);
621 }
622
584 void PageHandler::OnColorPicked(int r, int g, int b, int a) { 623 void PageHandler::OnColorPicked(int r, int g, int b, int a) {
585 frontend_->ColorPicked( 624 frontend_->ColorPicked(
586 DOM::RGBA::Create().SetR(r).SetG(g).SetB(b).SetA(a).Build()); 625 DOM::RGBA::Create().SetR(r).SetG(g).SetB(b).SetA(a).Build());
587 } 626 }
588 627
589 Response PageHandler::StopLoading() { 628 Response PageHandler::StopLoading() {
590 WebContentsImpl* web_contents = GetWebContents(); 629 WebContentsImpl* web_contents = GetWebContents();
591 if (!web_contents) 630 if (!web_contents)
592 return Response::InternalError(); 631 return Response::InternalError();
593 web_contents->Stop(); 632 web_contents->Stop();
594 return Response::OK(); 633 return Response::OK();
595 } 634 }
596 635
597 } // namespace protocol 636 } // namespace protocol
598 } // namespace content 637 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698