feat: promisify win.capturePage() (#15743)

* feat: promisify win.capturePage

* mark optional arg correctly

* Add to breaking changes doc

* properly deprecate win.capturePage

* remove change from api-contract

* document both callback and promise versions

* address docs feedback

* update promisification progress doc
This commit is contained in:
Shelley Vohr 2018-11-27 23:50:53 -05:00 committed by GitHub
parent 73fbb69c50
commit 41c2685204
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 74 additions and 38 deletions

View file

@ -255,12 +255,12 @@ content::ServiceWorkerContext* GetServiceWorkerContext(
}
// Called when CapturePage is done.
void OnCapturePageDone(const base::Callback<void(const gfx::Image&)>& callback,
void OnCapturePageDone(scoped_refptr<util::Promise> promise,
const SkBitmap& bitmap) {
// Hack to enable transparency in captured image
// TODO(nitsakh) Remove hack once fixed in chromium
const_cast<SkBitmap&>(bitmap).setAlphaType(kPremul_SkAlphaType);
callback.Run(gfx::Image::CreateFrom1xBitmap(bitmap));
promise->Resolve(gfx::Image::CreateFrom1xBitmap(bitmap));
}
} // namespace
@ -1756,21 +1756,17 @@ void WebContents::StartDrag(const mate::Dictionary& item,
}
}
void WebContents::CapturePage(mate::Arguments* args) {
v8::Local<v8::Promise> WebContents::CapturePage(mate::Arguments* args) {
gfx::Rect rect;
base::Callback<void(const gfx::Image&)> callback;
scoped_refptr<util::Promise> promise = new util::Promise(isolate());
if (!(args->Length() == 1 && args->GetNext(&callback)) &&
!(args->Length() == 2 && args->GetNext(&rect) &&
args->GetNext(&callback))) {
args->ThrowError();
return;
}
// get rect arguments if they exist
args->GetNext(&rect);
auto* const view = web_contents()->GetRenderWidgetHostView();
if (!view) {
callback.Run(gfx::Image());
return;
promise->Resolve(gfx::Image());
return promise->GetHandle();
}
// Capture full page if user doesn't specify a |rect|.
@ -1789,7 +1785,8 @@ void WebContents::CapturePage(mate::Arguments* args) {
bitmap_size = gfx::ScaleToCeiledSize(view_size, scale);
view->CopyFromSurface(gfx::Rect(rect.origin(), view_size), bitmap_size,
base::BindOnce(&OnCapturePageDone, callback));
base::BindOnce(&OnCapturePageDone, promise));
return promise->GetHandle();
}
void WebContents::OnCursorChange(const content::WebCursor& cursor) {