Browse Source

fix: DesktopCapturer gc'd prior to capture completion (#28279)

desktopCapture.getSources() returns a promise which should resolve
when capturing finishes. Internally it creates an instance of
DesktopCapturer which is responsible for resolving or rejecting
the promise.

Between the time DesktopCapturer starts capturing frames and when
it finishes, it's possible for its handle to be GC'd leading to
it never resolving.

These changes pin the instance of DesktopCapturer until it either
finishes or errors.

fixes #25595

Co-authored-by: samuelmaddock <[email protected]>
trop[bot] 4 years ago
parent
commit
51f9712c8f

+ 11 - 1
shell/browser/api/electron_api_desktop_capturer.cc

@@ -161,6 +161,9 @@ void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) {
         v8::Locker locker(isolate);
         v8::HandleScope scope(isolate);
         gin_helper::CallMethod(this, "_onerror", "Failed to get sources.");
+
+        Unpin();
+
         return;
       }
 
@@ -194,12 +197,19 @@ void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) {
     v8::Locker locker(isolate);
     v8::HandleScope scope(isolate);
     gin_helper::CallMethod(this, "_onfinished", captured_sources_);
+
+    Unpin();
   }
 }
 
 // static
 gin::Handle<DesktopCapturer> DesktopCapturer::Create(v8::Isolate* isolate) {
-  return gin::CreateHandle(isolate, new DesktopCapturer(isolate));
+  auto handle = gin::CreateHandle(isolate, new DesktopCapturer(isolate));
+
+  // Keep reference alive until capturing has finished.
+  handle->Pin(isolate);
+
+  return handle;
 }
 
 gin::ObjectTemplateBuilder DesktopCapturer::GetObjectTemplateBuilder(

+ 2 - 0
shell/browser/api/electron_api_desktop_capturer.h

@@ -13,12 +13,14 @@
 #include "chrome/browser/media/webrtc/native_desktop_media_list.h"
 #include "gin/handle.h"
 #include "gin/wrappable.h"
+#include "shell/common/gin_helper/pinnable.h"
 
 namespace electron {
 
 namespace api {
 
 class DesktopCapturer : public gin::Wrappable<DesktopCapturer>,
+                        public gin_helper::Pinnable<DesktopCapturer>,
                         public DesktopMediaListObserver {
  public:
   struct Source {