Fix capturePage API, closes #847
This commit is contained in:
parent
a0034521da
commit
a9b1b567fb
1 changed files with 29 additions and 12 deletions
|
@ -50,11 +50,15 @@
|
||||||
#include "ipc/ipc_message_macros.h"
|
#include "ipc/ipc_message_macros.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
#include "ui/gfx/codec/png_codec.h"
|
#include "ui/gfx/codec/png_codec.h"
|
||||||
|
#include "ui/gfx/geometry/size_conversions.h"
|
||||||
#include "ui/gfx/point.h"
|
#include "ui/gfx/point.h"
|
||||||
#include "ui/gfx/rect.h"
|
#include "ui/gfx/rect.h"
|
||||||
|
#include "ui/gfx/screen.h"
|
||||||
#include "ui/gfx/size.h"
|
#include "ui/gfx/size.h"
|
||||||
|
|
||||||
using content::NavigationEntry;
|
using content::NavigationEntry;
|
||||||
|
using content::RenderWidgetHostView;
|
||||||
|
using content::RenderWidgetHost;
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
@ -289,31 +293,43 @@ void NativeWindow::BlurWebView() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NativeWindow::IsWebViewFocused() {
|
bool NativeWindow::IsWebViewFocused() {
|
||||||
content::RenderWidgetHostView* host_view =
|
RenderWidgetHostView* host_view =
|
||||||
GetWebContents()->GetRenderViewHost()->GetView();
|
GetWebContents()->GetRenderViewHost()->GetView();
|
||||||
return host_view && host_view->HasFocus();
|
return host_view && host_view->HasFocus();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeWindow::CapturePage(const gfx::Rect& rect,
|
void NativeWindow::CapturePage(const gfx::Rect& rect,
|
||||||
const CapturePageCallback& callback) {
|
const CapturePageCallback& callback) {
|
||||||
content::RenderViewHost* render_view_host =
|
content::WebContents* contents = GetWebContents();
|
||||||
GetWebContents()->GetRenderViewHost();
|
RenderWidgetHostView* const view = contents->GetRenderWidgetHostView();
|
||||||
content::RenderWidgetHostView* render_widget_host_view =
|
RenderWidgetHost* const host = view ? view->GetRenderWidgetHost() : nullptr;
|
||||||
render_view_host->GetView();
|
if (!view || !host) {
|
||||||
|
|
||||||
if (!render_widget_host_view) {
|
|
||||||
callback.Run(std::vector<unsigned char>());
|
callback.Run(std::vector<unsigned char>());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
GetWebContents()->GetRenderViewHost()->CopyFromBackingStore(
|
// Capture full page if user doesn't specify a |rect|.
|
||||||
rect,
|
const gfx::Size view_size = rect.IsEmpty() ? view->GetViewBounds().size() :
|
||||||
rect.IsEmpty() ? render_widget_host_view->GetViewBounds().size() :
|
rect.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();
|
||||||
|
gfx::Screen* const screen = gfx::Screen::GetScreenFor(native_view);
|
||||||
|
const float scale =
|
||||||
|
screen->GetDisplayNearestWindow(native_view).device_scale_factor();
|
||||||
|
if (scale > 1.0f)
|
||||||
|
bitmap_size = gfx::ToCeiledSize(gfx::ScaleSize(view_size, scale));
|
||||||
|
|
||||||
|
host->CopyFromBackingStore(
|
||||||
|
rect.IsEmpty() ? gfx::Rect(view_size) : rect,
|
||||||
|
bitmap_size,
|
||||||
base::Bind(&NativeWindow::OnCapturePageDone,
|
base::Bind(&NativeWindow::OnCapturePageDone,
|
||||||
weak_factory_.GetWeakPtr(),
|
weak_factory_.GetWeakPtr(),
|
||||||
callback),
|
callback),
|
||||||
kAlpha_8_SkColorType);
|
kBGRA_8888_SkColorType);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeWindow::DestroyWebContents() {
|
void NativeWindow::DestroyWebContents() {
|
||||||
|
@ -698,6 +714,7 @@ void NativeWindow::NotifyWindowUnresponsive() {
|
||||||
void NativeWindow::OnCapturePageDone(const CapturePageCallback& callback,
|
void NativeWindow::OnCapturePageDone(const CapturePageCallback& callback,
|
||||||
bool succeed,
|
bool succeed,
|
||||||
const SkBitmap& bitmap) {
|
const SkBitmap& bitmap) {
|
||||||
|
SkAutoLockPixels screen_capture_lock(bitmap);
|
||||||
std::vector<unsigned char> data;
|
std::vector<unsigned char> data;
|
||||||
if (succeed)
|
if (succeed)
|
||||||
gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &data);
|
gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &data);
|
||||||
|
|
Loading…
Reference in a new issue