diff --git a/packages/gui-extensions/src/browser/chromium.ts b/packages/gui-extensions/src/browser/chromium.ts index 6206c7e9f4b..5509c3ba50f 100644 --- a/packages/gui-extensions/src/browser/chromium.ts +++ b/packages/gui-extensions/src/browser/chromium.ts @@ -12,6 +12,7 @@ import { createProfiling, type Recording } from "./profiling" import type { BrowserNetwork } from "./network" import { allowedDestination, + committedLoad, destinationOrigin, fileURLWithin, localFileURL, @@ -539,12 +540,29 @@ export function createBrowserPage( // whether there is a document worth exposing over the themed background. const updateVisibility = () => embed.show(content) + const load = (url: string) => { + let committed = false + + const commit = () => { + committed = true + } + + contents.once("did-navigate", commit) + + return contents + .loadURL(url) + .catch((error: Error) => { + if (!committedLoad(error, committed)) throw error + }) + .finally(() => contents.off("did-navigate", commit)) + } + const ready = Promise.all([ files.ready, ...(options.initialize === false ? [] : [ - contents.loadURL(normalizeURL(options.restore?.url || "about:blank", policy)).catch((error: Error) => { + load(normalizeURL(options.restore?.url || "about:blank", policy)).catch((error: Error) => { if (!options.restore) throw error // A dev server may have stopped while this page was unloaded. Keep its tab available to retry. options.publish(error.message) @@ -726,7 +744,7 @@ export function createBrowserPage( signal.addEventListener("abort", cancel, { once: true }) try { - await contents.loadURL(url) + await load(url) } finally { signal.removeEventListener("abort", cancel) } diff --git a/packages/gui-extensions/src/browser/policy.test.ts b/packages/gui-extensions/src/browser/policy.test.ts index d6467ca731b..3af215efbf4 100644 --- a/packages/gui-extensions/src/browser/policy.test.ts +++ b/packages/gui-extensions/src/browser/policy.test.ts @@ -1,5 +1,13 @@ import { expect, test } from "bun:test" -import { allowedDestination, destinationOrigin, fileURLWithin, localFileURL, normalizeURL, refusal } from "./policy" +import { + allowedDestination, + committedLoad, + destinationOrigin, + fileURLWithin, + localFileURL, + normalizeURL, + refusal, +} from "./policy" test("allows cross-origin HTTP navigation but rejects unsafe destinations and embedded credentials", () => { expect(destinationOrigin("https://other.example/path")).toBe("https://other.example") @@ -80,3 +88,21 @@ test("windows workspace roots match drive-letter file URLs", () => { expect(fileURLWithin("file:///C:/Users/me/repo2/x.html", roots)).toBe(false) expect(fileURLWithin("file:///D:/Users/me/repo/x.html", roots)).toBe(false) }) + +test("a media URL whose viewer committed has loaded, while a download or network failure still fails", () => { + // The shape Electron's loadURL rejects with when loading stops before did-finish-load. + const stopped = Object.assign(new Error("ERR_FAILED (-2) loading 'http://127.0.0.1/clip.mp4'"), { + errno: -2, + code: "ERR_FAILED", + }) + + const refused = Object.assign(new Error("ERR_CONNECTION_REFUSED (-102) loading 'http://127.0.0.1/'"), { + errno: -102, + code: "ERR_CONNECTION_REFUSED", + }) + + expect(committedLoad(stopped, true)).toBe(true) + expect(committedLoad(stopped, false)).toBe(false) + expect(committedLoad(refused, true)).toBe(false) + expect(committedLoad(new Error("ERR_FAILED"), true)).toBe(false) +}) diff --git a/packages/gui-extensions/src/browser/policy.ts b/packages/gui-extensions/src/browser/policy.ts index 284e5d3cc6a..cf0e3615883 100644 --- a/packages/gui-extensions/src/browser/policy.ts +++ b/packages/gui-extensions/src/browser/policy.ts @@ -93,3 +93,12 @@ function withScheme(input: string) { return `${local ? "http" : "https"}://${value}` } + +/** + * Electron rejects `loadURL` with ERR_FAILED (-2) whenever loading stops before `did-finish-load`. Chromium's media + * viewer for a video or audio URL commits its document and stops that way, so a committed navigation has loaded. A + * download never commits, so it still fails. + */ +export function committedLoad(error: Error, committed: boolean) { + return committed && "errno" in error && error.errno === -2 +}