Move capturePage to WebContents

This commit is contained in:
Kevin Sawicki 2016-07-05 15:43:57 -07:00
parent d42d10a47c
commit f7b72f0948
8 changed files with 67 additions and 79 deletions

View file

@ -65,6 +65,7 @@
#include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context.h"
#include "third_party/WebKit/public/web/WebInputEvent.h" #include "third_party/WebKit/public/web/WebInputEvent.h"
#include "third_party/WebKit/public/web/WebFindOptions.h" #include "third_party/WebKit/public/web/WebFindOptions.h"
#include "ui/gfx/screen.h"
#include "atom/common/node_includes.h" #include "atom/common/node_includes.h"
@ -1235,6 +1236,55 @@ void WebContents::StartDrag(const mate::Dictionary& item,
} }
} }
void WebContents::CapturePage(mate::Arguments* args) {
gfx::Rect rect;
base::Callback<void(const gfx::Image&)> callback;
if (!(args->Length() == 1 && args->GetNext(&callback)) &&
!(args->Length() == 2 && args->GetNext(&rect)
&& args->GetNext(&callback))) {
args->ThrowError();
return;
}
const auto view = web_contents()->GetRenderWidgetHostView();
const auto host = view ? view->GetRenderWidgetHost() : nullptr;
if (!view || !host) {
callback.Run(gfx::Image());
return;
}
// Capture full page if user doesn't specify a |rect|.
const gfx::Size view_size = rect.IsEmpty() ? view->GetViewBounds().size() :
rect.size();
// By default, the requested bitmap size is the view size in screen
// coordinates. However, if there's more pixel detail available on the
// current system, increase the requested bitmap size to capture it all.
gfx::Size bitmap_size = view_size;
const gfx::NativeView native_view = view->GetNativeView();
const float scale =
gfx::Screen::GetScreen()->GetDisplayNearestWindow(native_view)
.device_scale_factor();
if (scale > 1.0f)
bitmap_size = gfx::ScaleToCeiledSize(view_size, scale);
host->CopyFromBackingStore(
gfx::Rect(rect.origin(), view_size),
bitmap_size,
base::Bind(&WebContents::OnCapturePageDone, base::Unretained(this), callback),
kBGRA_8888_SkColorType);
}
void WebContents::OnCapturePageDone(
base::Callback<void(const gfx::Image&)> callback,
const SkBitmap& bitmap,
content::ReadbackResponse response) {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
callback.Run(gfx::Image::CreateFrom1xBitmap(bitmap));
}
void WebContents::OnCursorChange(const content::WebCursor& cursor) { void WebContents::OnCursorChange(const content::WebCursor& cursor) {
content::WebCursor::CursorInfo info; content::WebCursor::CursorInfo info;
cursor.GetCursorInfo(&info); cursor.GetCursorInfo(&info);
@ -1370,6 +1420,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
.SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace) .SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
.SetMethod("showDefinitionForSelection", .SetMethod("showDefinitionForSelection",
&WebContents::ShowDefinitionForSelection) &WebContents::ShowDefinitionForSelection)
.SetMethod("capturePage", &WebContents::CapturePage)
.SetProperty("id", &WebContents::ID) .SetProperty("id", &WebContents::ID)
.SetProperty("session", &WebContents::Session) .SetProperty("session", &WebContents::Session)
.SetProperty("hostWebContents", &WebContents::HostWebContents) .SetProperty("hostWebContents", &WebContents::HostWebContents)

View file

@ -54,6 +54,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
using PrintToPDFCallback = using PrintToPDFCallback =
base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>)>; base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>)>;
using CapturePageCallback = base::Callback<void(const SkBitmap& bitmap)>;
// Create from an existing WebContents. // Create from an existing WebContents.
static mate::Handle<WebContents> CreateFrom( static mate::Handle<WebContents> CreateFrom(
v8::Isolate* isolate, content::WebContents* web_contents); v8::Isolate* isolate, content::WebContents* web_contents);
@ -145,6 +147,10 @@ class WebContents : public mate::TrackableObject<WebContents>,
// Dragging native items. // Dragging native items.
void StartDrag(const mate::Dictionary& item, mate::Arguments* args); void StartDrag(const mate::Dictionary& item, mate::Arguments* args);
// Captures the page with |rect|, |callback| would be called when capturing is
// done.
void CapturePage(mate::Arguments* args);
// Methods for creating <webview>. // Methods for creating <webview>.
void SetSize(const SetSizeParams& params); void SetSize(const SetSizeParams& params);
bool IsGuest() const; bool IsGuest() const;
@ -159,6 +165,11 @@ class WebContents : public mate::TrackableObject<WebContents>,
const std::string& frame_name, const std::string& frame_name,
WindowOpenDisposition disposition); WindowOpenDisposition disposition);
// Called when CapturePage is done.
void OnCapturePageDone(base::Callback<void(const gfx::Image&)>,
const SkBitmap& bitmap,
content::ReadbackResponse response);
// Returns the web preferences of current WebContents. // Returns the web preferences of current WebContents.
v8::Local<v8::Value> GetWebPreferences(v8::Isolate* isolate); v8::Local<v8::Value> GetWebPreferences(v8::Isolate* isolate);

View file

@ -55,15 +55,6 @@ namespace api {
namespace { namespace {
void OnCapturePageDone(
v8::Isolate* isolate,
const base::Callback<void(const gfx::Image&)>& callback,
const SkBitmap& bitmap) {
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
callback.Run(gfx::Image::CreateFrom1xBitmap(bitmap));
}
// Converts binary data to Buffer. // Converts binary data to Buffer.
v8::Local<v8::Value> ToBuffer(v8::Isolate* isolate, void* val, int size) { v8::Local<v8::Value> ToBuffer(v8::Isolate* isolate, void* val, int size) {
auto buffer = node::Buffer::Copy(isolate, static_cast<char*>(val), size); auto buffer = node::Buffer::Copy(isolate, static_cast<char*>(val), size);
@ -581,21 +572,6 @@ void Window::SetFocusable(bool focusable) {
return window_->SetFocusable(focusable); return window_->SetFocusable(focusable);
} }
void Window::CapturePage(mate::Arguments* args) {
gfx::Rect rect;
base::Callback<void(const gfx::Image&)> callback;
if (!(args->Length() == 1 && args->GetNext(&callback)) &&
!(args->Length() == 2 && args->GetNext(&rect)
&& args->GetNext(&callback))) {
args->ThrowError();
return;
}
window_->CapturePage(
rect, base::Bind(&OnCapturePageDone, args->isolate(), callback));
}
void Window::SetProgressBar(double progress) { void Window::SetProgressBar(double progress) {
window_->SetProgressBar(progress); window_->SetProgressBar(progress);
} }
@ -843,7 +819,6 @@ void Window::BuildPrototype(v8::Isolate* isolate,
.SetMethod("focusOnWebView", &Window::FocusOnWebView) .SetMethod("focusOnWebView", &Window::FocusOnWebView)
.SetMethod("blurWebView", &Window::BlurWebView) .SetMethod("blurWebView", &Window::BlurWebView)
.SetMethod("isWebViewFocused", &Window::IsWebViewFocused) .SetMethod("isWebViewFocused", &Window::IsWebViewFocused)
.SetMethod("capturePage", &Window::CapturePage)
.SetMethod("setProgressBar", &Window::SetProgressBar) .SetMethod("setProgressBar", &Window::SetProgressBar)
.SetMethod("setOverlayIcon", &Window::SetOverlayIcon) .SetMethod("setOverlayIcon", &Window::SetOverlayIcon)
.SetMethod("setThumbarButtons", &Window::SetThumbarButtons) .SetMethod("setThumbarButtons", &Window::SetThumbarButtons)

View file

@ -155,7 +155,6 @@ class Window : public mate::TrackableObject<Window>,
void SetIgnoreMouseEvents(bool ignore); void SetIgnoreMouseEvents(bool ignore);
void SetContentProtection(bool enable); void SetContentProtection(bool enable);
void SetFocusable(bool focusable); void SetFocusable(bool focusable);
void CapturePage(mate::Arguments* args);
void SetProgressBar(double progress); void SetProgressBar(double progress);
void SetOverlayIcon(const gfx::Image& overlay, void SetOverlayIcon(const gfx::Image& overlay,
const std::string& description); const std::string& description);

View file

@ -37,7 +37,6 @@
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/size_conversions.h" #include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/screen.h"
#include "ui/gl/gpu_switching_manager.h" #include "ui/gl/gpu_switching_manager.h"
DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::NativeWindowRelay); DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::NativeWindowRelay);
@ -315,39 +314,6 @@ bool NativeWindow::IsWebViewFocused() {
return host_view && host_view->HasFocus(); return host_view && host_view->HasFocus();
} }
void NativeWindow::CapturePage(const gfx::Rect& rect,
const CapturePageCallback& callback) {
const auto view = web_contents()->GetRenderWidgetHostView();
const auto host = view ? view->GetRenderWidgetHost() : nullptr;
if (!view || !host) {
callback.Run(SkBitmap());
return;
}
// Capture full page if user doesn't specify a |rect|.
const gfx::Size view_size = rect.IsEmpty() ? view->GetViewBounds().size() :
rect.size();
// By default, the requested bitmap size is the view size in screen
// coordinates. However, if there's more pixel detail available on the
// current system, increase the requested bitmap size to capture it all.
gfx::Size bitmap_size = view_size;
const gfx::NativeView native_view = view->GetNativeView();
const float scale =
gfx::Screen::GetScreen()->GetDisplayNearestWindow(native_view)
.device_scale_factor();
if (scale > 1.0f)
bitmap_size = gfx::ScaleToCeiledSize(view_size, scale);
host->CopyFromBackingStore(
gfx::Rect(rect.origin(), view_size),
bitmap_size,
base::Bind(&NativeWindow::OnCapturePageDone,
weak_factory_.GetWeakPtr(),
callback),
kBGRA_8888_SkColorType);
}
void NativeWindow::SetAutoHideMenuBar(bool auto_hide) { void NativeWindow::SetAutoHideMenuBar(bool auto_hide) {
} }
@ -634,10 +600,4 @@ void NativeWindow::NotifyReadyToShow() {
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnReadyToShow()); FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnReadyToShow());
} }
void NativeWindow::OnCapturePageDone(const CapturePageCallback& callback,
const SkBitmap& bitmap,
content::ReadbackResponse response) {
callback.Run(bitmap);
}
} // namespace atom } // namespace atom

View file

@ -54,8 +54,6 @@ struct DraggableRegion;
class NativeWindow : public base::SupportsUserData, class NativeWindow : public base::SupportsUserData,
public content::WebContentsObserver { public content::WebContentsObserver {
public: public:
using CapturePageCallback = base::Callback<void(const SkBitmap& bitmap)>;
class DialogScope { class DialogScope {
public: public:
explicit DialogScope(NativeWindow* window) explicit DialogScope(NativeWindow* window)
@ -179,11 +177,6 @@ class NativeWindow : public base::SupportsUserData,
virtual void BlurWebView(); virtual void BlurWebView();
virtual bool IsWebViewFocused(); virtual bool IsWebViewFocused();
// Captures the page with |rect|, |callback| would be called when capturing is
// done.
virtual void CapturePage(const gfx::Rect& rect,
const CapturePageCallback& callback);
// Toggle the menu bar. // Toggle the menu bar.
virtual void SetAutoHideMenuBar(bool auto_hide); virtual void SetAutoHideMenuBar(bool auto_hide);
virtual bool IsMenuBarAutoHide(); virtual bool IsMenuBarAutoHide();
@ -296,11 +289,6 @@ class NativeWindow : public base::SupportsUserData,
// Dispatch ReadyToShow event to observers. // Dispatch ReadyToShow event to observers.
void NotifyReadyToShow(); void NotifyReadyToShow();
// Called when CapturePage has done.
void OnCapturePageDone(const CapturePageCallback& callback,
const SkBitmap& bitmap,
content::ReadbackResponse response);
// Whether window has standard frame. // Whether window has standard frame.
bool has_frame_; bool has_frame_;

View file

@ -153,6 +153,9 @@ Object.assign(BrowserWindow.prototype, {
}, },
showDefinitionForSelection () { showDefinitionForSelection () {
return this.webContents.showDefinitionForSelection() return this.webContents.showDefinitionForSelection()
},
capturePage (...args) {
return this.webContents.capturePage.apply(this.webContents, args)
} }
}) })

View file

@ -386,7 +386,8 @@ var registerWebViewElement = function () {
'inspectServiceWorker', 'inspectServiceWorker',
'print', 'print',
'printToPDF', 'printToPDF',
'showDefinitionForSelection' 'showDefinitionForSelection',
'capturePage'
] ]
nonblockMethods = [ nonblockMethods = [
'insertCSS', 'insertCSS',