]> khamidou — GitHub mirrors - electron.git/commitdiff
Add API to capture a page into file.
authorCheng Zhao <zcbenz@gmail.com>
Thu, 21 Nov 2013 13:50:06 +0000 (21:50 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 21 Nov 2013 13:50:06 +0000 (21:50 +0800)
browser/native_window.cc
browser/native_window.h

index db671f9a77a68ddfcefe2f227bd3fe64b2d5e859..182edc351929e59b1502fd03a84966c99f676130 100644 (file)
@@ -29,6 +29,7 @@
 #include "common/api/api_messages.h"
 #include "common/options_switches.h"
 #include "ipc/ipc_message_macros.h"
+#include "ui/gfx/codec/png_codec.h"
 #include "ui/gfx/point.h"
 #include "ui/gfx/rect.h"
 #include "ui/gfx/size.h"
@@ -198,6 +199,18 @@ bool NativeWindow::SetIcon(const std::string& str_path) {
   return true;
 }
 
+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));
+}
+
 void NativeWindow::CloseWebContents() {
   bool prevent_default = false;
   FOR_EACH_OBSERVER(NativeWindowObserver,
@@ -374,6 +387,29 @@ void NativeWindow::Observe(int type,
   }
 }
 
+void NativeWindow::OnCapturePageDone(const base::FilePath& filename,
+                                     const CapturePageCallback& callback,
+                                     bool succeed,
+                                     const SkBitmap& bitmap) {
+  if (!succeed) {
+    callback.Run(false);
+    return;
+  }
+
+  std::vector<unsigned char> data;
+  bool encoded = gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &data);
+  if (!encoded) {
+    callback.Run(false);
+    return;
+  }
+
+  int written = file_util::WriteFile(
+      filename,
+      reinterpret_cast<const char*>(&data[0]),
+      data.size());
+  callback.Run(written > 0);
+}
+
 void NativeWindow::OnRendererMessage(const string16& channel,
                                      const base::ListValue& args) {
   AtomBrowserMainParts::Get()->atom_bindings()->OnRendererMessage(
index e10c0f7d88dfad6c27d7dd48c762c29d5aa5d4be..07e839e839b9f1581311dcd4157d3ed0d7c4c74e 100644 (file)
@@ -108,6 +108,13 @@ 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<void(bool succeed)> CapturePageCallback;
+  virtual void CapturePage(const gfx::Rect& rect,
+                           const base::FilePath& path,
+                           const CapturePageCallback& callback);
+
   // The same with closing a tab in a real browser.
   //
   // Should be called by platform code when user want to close the window.
@@ -184,6 +191,12 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
  private:
   void RendererUnresponsiveDelayed();
 
+  // Called when CapturePage has done.
+  void OnCapturePageDone(const base::FilePath& filename,
+                         const CapturePageCallback& callback,
+                         bool succeed,
+                         const SkBitmap& bitmap);
+
   void OnRendererMessage(const string16& channel,
                          const base::ListValue& args);