Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use nested run loop in clipboard.readImage #39487

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion shell/common/api/electron_api_clipboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "base/containers/contains.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "shell/browser/browser.h"
#include "shell/common/gin_converters/image_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
Expand Down Expand Up @@ -220,10 +221,18 @@ void Clipboard::WriteBookmark(const std::u16string& title,
}

gfx::Image Clipboard::ReadImage(gin_helper::Arguments* args) {
// The ReadPng uses thread pool which requires app ready.
if (IsBrowserProcess() && !Browser::Get()->is_ready()) {
args->ThrowError(
"clipboard.readImage is available only after app ready in the main "
"process");
return gfx::Image();
}

ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
absl::optional<gfx::Image> image;

base::RunLoop run_loop;
base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
base::RepeatingClosure callback = run_loop.QuitClosure();
clipboard->ReadPng(
GetClipboardBuffer(args),
Expand Down
5 changes: 5 additions & 0 deletions spec/api-clipboard-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ ifdescribe(process.platform !== 'win32' || process.arch !== 'arm64')('clipboard
const readImage = clipboard.readImage();
expect(readImage.toDataURL()).to.equal(i.toDataURL());
});

it('works for empty image', () => {
clipboard.writeText('Not an Image');
expect(clipboard.readImage().isEmpty()).to.be.true();
});
});

describe('clipboard.readText()', () => {
Expand Down