fix: clipboard.readImage() should be synchronous (#39069)

* feat: clipboard.readImage returns a Promise

* chore: update breaking changes doc

* fix: make function synchronous

* Update docs/api/native-image.md

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

---------

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
This commit is contained in:
Shelley Vohr 2023-07-13 22:59:14 +02:00 committed by GitHub
parent 34e7c3696a
commit f61425efdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,7 @@
#include <map> #include <map>
#include "base/containers/contains.h" #include "base/containers/contains.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "shell/common/gin_converters/image_converter.h" #include "shell/common/gin_converters/image_converter.h"
#include "shell/common/gin_helper/dictionary.h" #include "shell/common/gin_helper/dictionary.h"
@ -221,17 +222,23 @@ void Clipboard::WriteBookmark(const std::u16string& title,
gfx::Image Clipboard::ReadImage(gin_helper::Arguments* args) { gfx::Image Clipboard::ReadImage(gin_helper::Arguments* args) {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
absl::optional<gfx::Image> image; absl::optional<gfx::Image> image;
base::RunLoop run_loop;
base::RepeatingClosure callback = run_loop.QuitClosure();
clipboard->ReadPng( clipboard->ReadPng(
GetClipboardBuffer(args), GetClipboardBuffer(args),
/* data_dst = */ nullptr, /* data_dst = */ nullptr,
base::BindOnce( base::BindOnce(
[](absl::optional<gfx::Image>* image, [](absl::optional<gfx::Image>* image, base::RepeatingClosure cb,
const std::vector<uint8_t>& result) { const std::vector<uint8_t>& result) {
SkBitmap bitmap; SkBitmap bitmap;
gfx::PNGCodec::Decode(result.data(), result.size(), &bitmap); gfx::PNGCodec::Decode(result.data(), result.size(), &bitmap);
image->emplace(gfx::Image::CreateFrom1xBitmap(bitmap)); image->emplace(gfx::Image::CreateFrom1xBitmap(bitmap));
std::move(cb).Run();
}, },
&image)); &image, std::move(callback)));
run_loop.Run();
DCHECK(image.has_value()); DCHECK(image.has_value());
return image.value(); return image.value();
} }