Skip to content

Commit

Permalink
fix: use nested run loop in clipboard.readImage (#39487)
Browse files Browse the repository at this point in the history
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Cheng Zhao <zcbenz@gmail.com>
  • Loading branch information
trop[bot] and zcbenz committed Aug 14, 2023
1 parent c09ebea commit ceb5230
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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

0 comments on commit ceb5230

Please sign in to comment.