Browse Source

fix: don't emit `did-fail-load` for MediaDocuments (#37824)

* fix: don't emit did-fail-load for MediaDocuments

* spec: add test
Shelley Vohr 2 years ago
parent
commit
251e567eff

+ 11 - 0
shell/browser/api/electron_api_web_contents.cc

@@ -64,6 +64,7 @@
 #include "gin/handle.h"
 #include "gin/object_template_builder.h"
 #include "gin/wrappable.h"
+#include "media/base/mime_util.h"
 #include "mojo/public/cpp/bindings/associated_remote.h"
 #include "mojo/public/cpp/bindings/pending_receiver.h"
 #include "mojo/public/cpp/bindings/remote.h"
@@ -1762,6 +1763,16 @@ void WebContents::DidFinishLoad(content::RenderFrameHost* render_frame_host,
 void WebContents::DidFailLoad(content::RenderFrameHost* render_frame_host,
                               const GURL& url,
                               int error_code) {
+  // See DocumentLoader::StartLoadingResponse() - when we navigate to a media
+  // resource the original request for the media resource, which resulted in a
+  // committed navigation, is simply discarded. The media element created
+  // inside the MediaDocument then makes *another new* request for the same
+  // media resource.
+  bool is_media_document =
+      media::IsSupportedMediaMimeType(web_contents()->GetContentsMimeType());
+  if (error_code == net::ERR_ABORTED && is_media_document)
+    return;
+
   bool is_main_frame = !render_frame_host->GetParent();
   int frame_process_id = render_frame_host->GetProcess()->GetID();
   int frame_routing_id = render_frame_host->GetRoutingID();

+ 9 - 0
spec/api-browser-window-spec.ts

@@ -377,6 +377,15 @@ describe('BrowserWindow module', () => {
       expect(code).to.equal(-300);
       expect(isMainFrame).to.equal(true);
     });
+    it('should not emit did-fail-load for a successfully loaded media file', async () => {
+      w.webContents.on('did-fail-load', () => {
+        expect.fail('did-fail-load should not emit on media file loads');
+      });
+
+      const mediaStarted = once(w.webContents, 'media-started-playing');
+      w.loadFile(path.join(fixtures, 'cat-spin.mp4'));
+      await mediaStarted;
+    });
     it('should set `mainFrame = false` on did-fail-load events in iframes', async () => {
       const didFailLoad = once(w.webContents, 'did-fail-load');
       w.loadFile(path.join(fixtures, 'api', 'did-fail-load-iframe.html'));

BIN
spec/fixtures/cat-spin.mp4