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

Unified Diff: extensions/browser/api/web_contents_capture_client.cc

Issue 1582053002: Implement webview.captureVisibleRegion() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix test so it waits for the first frame to be generated. Created 4 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: extensions/browser/api/web_contents_capture_client.cc
diff --git a/extensions/browser/api/capture_web_contents_function.cc b/extensions/browser/api/web_contents_capture_client.cc
similarity index 70%
rename from extensions/browser/api/capture_web_contents_function.cc
rename to extensions/browser/api/web_contents_capture_client.cc
index e10842d8aee0aa103031261e8dec0ca9045f5479..e99eb600599a744ffe1b65d90088b8b66640bcb8 100644
--- a/extensions/browser/api/capture_web_contents_function.cc
+++ b/extensions/browser/api/web_contents_capture_client.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "extensions/browser/api/capture_web_contents_function.h"
+#include "extensions/browser/api/web_contents_capture_client.h"
#include "base/base64.h"
#include "base/strings/stringprintf.h"
@@ -25,28 +25,14 @@ namespace extensions {
using api::extension_types::ImageDetails;
-bool CaptureWebContentsFunction::HasPermission() {
- return true;
-}
-
-bool CaptureWebContentsFunction::RunAsync() {
- EXTENSION_FUNCTION_VALIDATE(args_);
-
- context_id_ = extension_misc::kCurrentWindowId;
- args_->GetInteger(0, &context_id_);
-
- scoped_ptr<ImageDetails> image_details;
- if (args_->GetSize() > 1) {
- base::Value* spec = NULL;
- EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &spec) && spec);
- image_details = ImageDetails::FromValue(*spec);
- }
-
- if (!IsScreenshotEnabled())
+bool WebContentsCaptureClient::CaptureAsync(
+ WebContents* web_contents,
+ const ImageDetails* image_details,
+ const content::ReadbackRequestCallback callback) {
+ if (!web_contents)
return false;
- WebContents* contents = GetWebContentsForID(context_id_);
- if (!contents)
+ if (!IsScreenshotEnabled())
return false;
// The default format and quality setting used when encoding jpegs.
@@ -65,7 +51,7 @@ bool CaptureWebContentsFunction::RunAsync() {
}
// TODO(miu): Account for fullscreen render widget? http://crbug.com/419878
- RenderWidgetHostView* const view = contents->GetRenderWidgetHostView();
+ RenderWidgetHostView* const view = web_contents->GetRenderWidgetHostView();
RenderWidgetHost* const host = view ? view->GetRenderWidgetHost() : nullptr;
if (!view || !host) {
OnCaptureFailure(FAILURE_REASON_VIEW_INVISIBLE);
@@ -84,26 +70,41 @@ bool CaptureWebContentsFunction::RunAsync() {
if (scale > 1.0f)
bitmap_size = gfx::ScaleToCeiledSize(view_size, scale);
- host->CopyFromBackingStore(
- gfx::Rect(view_size),
- bitmap_size,
- base::Bind(&CaptureWebContentsFunction::CopyFromBackingStoreComplete,
- this),
- kN32_SkColorType);
+ host->CopyFromBackingStore(gfx::Rect(view_size), bitmap_size, callback,
+ kN32_SkColorType);
return true;
}
-void CaptureWebContentsFunction::CopyFromBackingStoreComplete(
+void WebContentsCaptureClient::CopyFromBackingStoreComplete(
const SkBitmap& bitmap,
content::ReadbackResponse response) {
if (response == content::READBACK_SUCCESS) {
OnCaptureSuccess(bitmap);
return;
}
+ // TODO(wjmaclean): Improve error reporting. Why aren't we passing more
Fady Samuel 2016/01/25 16:20:39 Remove this now?
+ // information here?
+ std::string reason;
+ switch (response) {
+ case content::READBACK_FAILED:
+ reason = "READBACK_FAILED";
+ break;
+ case content::READBACK_SURFACE_UNAVAILABLE:
+ reason = "READBACK_SURFACE_UNAVAILABLE";
+ break;
+ case content::READBACK_BITMAP_ALLOCATION_FAILURE:
+ reason = "READBACK_BITMAP_ALLOCATION_FAILURE";
+ break;
+ default:
+ reason = "<unknown>";
+ }
OnCaptureFailure(FAILURE_REASON_UNKNOWN);
}
-void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) {
+// TODO(wjmaclean) can this be static?
+bool WebContentsCaptureClient::EncodeBitmap(const SkBitmap& bitmap,
+ std::string* base64_result) {
+ DCHECK(base64_result);
std::vector<unsigned char> data;
SkAutoLockPixels screen_capture_lock(bitmap);
bool encoded = false;
@@ -112,12 +113,8 @@ void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) {
case api::extension_types::IMAGE_FORMAT_JPEG:
encoded = gfx::JPEGCodec::Encode(
reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
- gfx::JPEGCodec::FORMAT_SkBitmap,
- bitmap.width(),
- bitmap.height(),
- static_cast<int>(bitmap.rowBytes()),
- image_quality_,
- &data);
+ gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(),
+ static_cast<int>(bitmap.rowBytes()), image_quality_, &data);
mime_type = kMimeTypeJpeg;
break;
case api::extension_types::IMAGE_FORMAT_PNG:
@@ -131,20 +128,17 @@ void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) {
NOTREACHED() << "Invalid image format.";
}
- if (!encoded) {
- OnCaptureFailure(FAILURE_REASON_ENCODING_FAILED);
- return;
- }
+ if (!encoded)
+ return false;
- std::string base64_result;
base::StringPiece stream_as_string(reinterpret_cast<const char*>(data.data()),
data.size());
- base::Base64Encode(stream_as_string, &base64_result);
- base64_result.insert(
+ base::Base64Encode(stream_as_string, base64_result);
+ base64_result->insert(
0, base::StringPrintf("data:%s;base64,", mime_type.c_str()));
- SetResult(new base::StringValue(base64_result));
- SendResponse(true);
+
+ return true;
}
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698