Browse Source

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

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Cheng Zhao <[email protected]>
trop[bot] 1 year ago
parent
commit
ceb5230395
2 changed files with 15 additions and 1 deletions
  1. 10 1
      shell/common/api/electron_api_clipboard.cc
  2. 5 0
      spec/api-clipboard-spec.ts

+ 10 - 1
shell/common/api/electron_api_clipboard.cc

@@ -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"
@@ -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),

+ 5 - 0
spec/api-clipboard-spec.ts

@@ -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()', () => {