]> khamidou — GitHub mirrors - electron.git/commitdiff
Add BrowserWindow.capturePage API.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 22 Nov 2013 06:23:19 +0000 (14:23 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 22 Nov 2013 06:23:19 +0000 (14:23 +0800)
browser/api/atom_api_window.cc
browser/api/atom_api_window.h
vendor/node

index 42404a4c185332e5a73ecc721795dd116f603f41..0dda0617bd239ad508acf8cb233d3a90b25f7940 100644 (file)
@@ -14,6 +14,7 @@
 #include "ui/gfx/point.h"
 #include "ui/gfx/rect.h"
 #include "ui/gfx/size.h"
+#include "vendor/node/src/node_buffer.h"
 
 using content::V8ValueConverter;
 using content::NavigationController;
@@ -82,6 +83,20 @@ void Window::OnRendererCrashed() {
   Emit("crashed");
 }
 
+void Window::OnCapturePageDone(v8::Persistent<v8::Function> callback,
+                               const std::vector<unsigned char>& data) {
+  v8::HandleScope scope;
+
+  // TODO Use new Buffer API when we updated to node v0.12.x.
+  node::Buffer* buffer = node::Buffer::New(
+      reinterpret_cast<const char*>(data.data()),
+      data.size());
+
+  v8::Handle<v8::Value> arg = buffer->handle_;
+  callback->Call(v8::Context::GetCurrent()->Global(), 1, &arg);
+  callback.Dispose(v8::Isolate::GetCurrent());
+}
+
 // static
 v8::Handle<v8::Value> Window::New(const v8::Arguments &args) {
   v8::HandleScope scope;
@@ -480,6 +495,22 @@ v8::Handle<v8::Value> Window::RestartHangMonitorTimeout(
   return v8::Undefined();
 }
 
+// static
+v8::Handle<v8::Value> Window::CapturePage(const v8::Arguments& args) {
+  UNWRAP_WINDOW_AND_CHECK;
+
+  gfx::Rect rect;
+  v8::Persistent<v8::Function> callback;
+  if (!FromV8Arguments(args, &rect, &callback))
+    return node::ThrowTypeError("Bad argument");
+
+  self->window_->CapturePage(rect, base::Bind(&Window::OnCapturePageDone,
+                                              base::Unretained(self),
+                                              callback));
+
+  return v8::Undefined();
+}
+
 // static
 v8::Handle<v8::Value> Window::GetPageTitle(const v8::Arguments &args) {
   UNWRAP_WINDOW_AND_CHECK;
@@ -721,6 +752,7 @@ void Window::Initialize(v8::Handle<v8::Object> target) {
   NODE_SET_PROTOTYPE_METHOD(t,
                             "restartHangMonitorTimeout",
                             RestartHangMonitorTimeout);
+  NODE_SET_PROTOTYPE_METHOD(t, "capturePage", CapturePage);
 
   NODE_SET_PROTOTYPE_METHOD(t, "getPageTitle", GetPageTitle);
   NODE_SET_PROTOTYPE_METHOD(t, "isLoading", IsLoading);
index 14a707e28bea73ed1e0f0b31bae464bafc353349..03709dc22a10d1cb59b6df34158d1961fecb0739 100644 (file)
@@ -5,6 +5,8 @@
 #ifndef ATOM_BROWSER_API_ATOM_API_WINDOW_H_
 #define ATOM_BROWSER_API_ATOM_API_WINDOW_H_
 
+#include <vector>
+
 #include "base/memory/scoped_ptr.h"
 #include "browser/api/atom_api_event_emitter.h"
 #include "browser/native_window_observer.h"
@@ -87,6 +89,7 @@ class Window : public EventEmitter,
   static v8::Handle<v8::Value> IsWebViewFocused(const v8::Arguments& args);
   static v8::Handle<v8::Value> RestartHangMonitorTimeout(
       const v8::Arguments &args);
+  static v8::Handle<v8::Value> CapturePage(const v8::Arguments& args);
 
   // APIs for WebContents.
   static v8::Handle<v8::Value> GetPageTitle(const v8::Arguments &args);
@@ -110,6 +113,10 @@ class Window : public EventEmitter,
   static v8::Handle<v8::Value> Reload(const v8::Arguments &args);
   static v8::Handle<v8::Value> ReloadIgnoringCache(const v8::Arguments &args);
 
+  // Called when capturePage is done.
+  void OnCapturePageDone(v8::Persistent<v8::Function> callback,
+                         const std::vector<unsigned char>& data);
+
   scoped_ptr<NativeWindow> window_;
 
   DISALLOW_COPY_AND_ASSIGN(Window);
index 7f44fac0fd64af7ec172c9606af1f3cec684a9ce..24c11bdc794c2b7b79163cb4737046f8de42446d 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 7f44fac0fd64af7ec172c9606af1f3cec684a9ce
+Subproject commit 24c11bdc794c2b7b79163cb4737046f8de42446d