From 8d6764e0a07de877926a55db8c88733aed87ebe0 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 21 Nov 2013 22:03:29 +0800 Subject: [PATCH] Return screenshot's data buffer instead of directly saving to file. --- browser/native_window.cc | 24 ++++-------------------- browser/native_window.h | 12 ++++++------ 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/browser/native_window.cc b/browser/native_window.cc index 182edc3..b3bb5ef 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -200,14 +200,12 @@ bool NativeWindow::SetIcon(const std::string& str_path) { } void NativeWindow::CapturePage(const gfx::Rect& rect, - const base::FilePath& path, const CapturePageCallback& callback) { GetWebContents()->GetRenderViewHost()->CopyFromBackingStore( rect, gfx::Size(), base::Bind(&NativeWindow::OnCapturePageDone, base::Unretained(this), - path, callback)); } @@ -387,27 +385,13 @@ void NativeWindow::Observe(int type, } } -void NativeWindow::OnCapturePageDone(const base::FilePath& filename, - const CapturePageCallback& callback, +void NativeWindow::OnCapturePageDone(const CapturePageCallback& callback, bool succeed, const SkBitmap& bitmap) { - if (!succeed) { - callback.Run(false); - return; - } - std::vector data; - bool encoded = gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &data); - if (!encoded) { - callback.Run(false); - return; - } - - int written = file_util::WriteFile( - filename, - reinterpret_cast(&data[0]), - data.size()); - callback.Run(written > 0); + if (succeed) + gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &data); + callback.Run(data); } void NativeWindow::OnRendererMessage(const string16& channel, diff --git a/browser/native_window.h b/browser/native_window.h index 07e839e..2506afb 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -49,6 +49,9 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, public content::WebContentsObserver, public content::NotificationObserver { public: + typedef base::Callback& buffer)> + CapturePageCallback; + virtual ~NativeWindow(); // Create window with existing WebContents. @@ -108,11 +111,9 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, virtual void RestartHangMonitorTimeout(); virtual bool SetIcon(const std::string& path); - // Captures the page with |rect| and saves the image to |path|, |callback| - // would be called when capturing is done. - typedef base::Callback CapturePageCallback; + // Captures the page with |rect|, |callback| would be called when capturing is + // done. virtual void CapturePage(const gfx::Rect& rect, - const base::FilePath& path, const CapturePageCallback& callback); // The same with closing a tab in a real browser. @@ -192,8 +193,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, void RendererUnresponsiveDelayed(); // Called when CapturePage has done. - void OnCapturePageDone(const base::FilePath& filename, - const CapturePageCallback& callback, + void OnCapturePageDone(const CapturePageCallback& callback, bool succeed, const SkBitmap& bitmap); -- 2.7.4