From 09a48a1bf84b1c50ddd7d79610ae7980eabf50ff Mon Sep 17 00:00:00 2001 From: "mazda@chromium.org" Date: Fri, 4 May 2012 00:27:34 +0000 Subject: [PATCH] Use the asynchronous version of CopyFromBackingStore in CaptureVisibleTabFunction. BUG=120003 TEST=Take Screenshot Extension with site permission modified (http://code.google.com/chrome/extensions/samples.html#e1697cacebad05218798bf3e8a0f724517f0e8c3) worked properly. Review URL: http://codereview.chromium.org/10294003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@135254 0039d316-1c4b-4281-b951-d872f2087c98 --- .../extensions/extension_tabs_module.cc | 44 +++++++++++++------ .../extensions/extension_tabs_module.h | 7 +++ 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/chrome/browser/extensions/extension_tabs_module.cc b/chrome/browser/extensions/extension_tabs_module.cc index 054c58e8c36..07af114755f 100644 --- a/chrome/browser/extensions/extension_tabs_module.cc +++ b/chrome/browser/extensions/extension_tabs_module.cc @@ -57,6 +57,7 @@ #include "content/public/browser/notification_source.h" #include "content/public/browser/render_view_host.h" #include "content/public/browser/render_view_host_delegate.h" +#include "content/public/browser/render_widget_host_view.h" #include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents_view.h" #include "content/public/common/url_constants.h" @@ -1662,28 +1663,45 @@ bool CaptureVisibleTabFunction::RunImpl() { return false; RenderViewHost* render_view_host = web_contents->GetRenderViewHost(); + content::RenderWidgetHostView* view = render_view_host->GetView(); + if (!view) { + error_ = keys::kInternalVisibleTabCaptureError; + return false; + } + skia::PlatformCanvas* temp_canvas = new skia::PlatformCanvas; + render_view_host->AsyncCopyFromBackingStore( + gfx::Rect(), + view->GetViewBounds().size(), + temp_canvas, + base::Bind(&CaptureVisibleTabFunction::CopyFromBackingStoreComplete, + this, + base::Owned(temp_canvas))); + return true; +} - // If a backing store is cached for the tab we want to capture, - // and it can be copied into a bitmap, then use it to generate the image. - // For example, some uncommon X11 visual modes are not supported by - // CopyFromBackingStore(). - skia::PlatformCanvas temp_canvas; - if (render_view_host->CopyFromBackingStore( - gfx::Rect(), gfx::Size(), &temp_canvas)) { +void CaptureVisibleTabFunction::CopyFromBackingStoreComplete( + skia::PlatformCanvas* canvas, + bool succeeded) { + if (succeeded) { VLOG(1) << "captureVisibleTab() got image from backing store."; - SendResultFromBitmap(skia::GetTopDevice(temp_canvas)->accessBitmap(false)); - return true; + SendResultFromBitmap(skia::GetTopDevice(*canvas)->accessBitmap(false)); + return; + } + + WebContents* web_contents = NULL; + TabContentsWrapper* wrapper = NULL; + if (!GetTabToCapture(&web_contents, &wrapper)) { + error_ = keys::kInternalVisibleTabCaptureError; + SendResponse(false); + return; } // Ask the renderer for a snapshot of the tab. - wrapper->snapshot_tab_helper()->CaptureSnapshot(); registrar_.Add(this, chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, - content::Source(wrapper->web_contents())); + content::Source(web_contents)); AddRef(); // Balanced in CaptureVisibleTabFunction::Observe(). wrapper->snapshot_tab_helper()->CaptureSnapshot(); - - return true; } // If a backing store was not available in CaptureVisibleTabFunction::RunImpl, diff --git a/chrome/browser/extensions/extension_tabs_module.h b/chrome/browser/extensions/extension_tabs_module.h index 7738d75be6f..085b7437733 100644 --- a/chrome/browser/extensions/extension_tabs_module.h +++ b/chrome/browser/extensions/extension_tabs_module.h @@ -27,6 +27,9 @@ class DictionaryValue; namespace content { class WebContents; } // namespace content +namespace skia { +class PlatformCanvas; +} // namespace skia // Windows class GetWindowFunction : public SyncExtensionFunction { @@ -181,6 +184,10 @@ class CaptureVisibleTabFunction : public AsyncExtensionFunction, const content::NotificationDetails& details) OVERRIDE; void SendResultFromBitmap(const SkBitmap& screen_capture); + private: + void CopyFromBackingStoreComplete(skia::PlatformCanvas* canvas, + bool succeeded); + content::NotificationRegistrar registrar_; // The format (JPEG vs PNG) of the resulting image. Set in RunImpl().