diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 98fd8e836272e..a9cbd5b52c491 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -2166,6 +2166,7 @@ void WebContents::UpdateDraggableRegions( void WebContents::DidStartNavigation( content::NavigationHandle* navigation_handle) { + navigation_handle->SetSilentlyIgnoreErrors(); EmitNavigationEvent("did-start-navigation", navigation_handle); } @@ -2201,17 +2202,18 @@ void WebContents::DidFinishNavigation( owner_window_->NotifyLayoutWindowControlsOverlay(); } - if (!navigation_handle->HasCommitted()) - return; bool is_main_frame = navigation_handle->IsInMainFrame(); - content::RenderFrameHost* frame_host = - navigation_handle->GetRenderFrameHost(); + content::RenderFrameHost* frame_host = nullptr; + + if (navigation_handle->HasCommitted()) { + frame_host = navigation_handle->GetRenderFrameHost(); + } int frame_process_id = -1, frame_routing_id = -1; if (frame_host) { frame_process_id = frame_host->GetProcess()->GetID(); frame_routing_id = frame_host->GetRoutingID(); } - if (!navigation_handle->IsErrorPage()) { + if (!navigation_handle->IsErrorPage() && navigation_handle->HasCommitted()) { // FIXME: All the Emit() calls below could potentially result in |this| // being destroyed (by JS listening for the event and calling // webContents.destroy()). diff --git a/spec/api-browser-window-spec.ts b/spec/api-browser-window-spec.ts index 64d55589fb1b8..f90f01829c0cb 100644 --- a/spec/api-browser-window-spec.ts +++ b/spec/api-browser-window-spec.ts @@ -3716,11 +3716,21 @@ describe('BrowserWindow module', () => { } }); + const server = http.createServer((req, res) => { + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end('Blank Page'); + }); + const serverUrl = (await listen(server)).url; + const preloadPath = path.join(mainFixtures, 'api', 'new-window-preload.js'); w.webContents.setWindowOpenHandler(() => ({ action: 'allow', overrideBrowserWindowOptions: { webPreferences: { preload: preloadPath } } })); - w.loadFile(path.join(fixtures, 'api', 'new-window.html')); + await w.loadFile(path.join(fixtures, 'api', 'new-window.html'), { + query: { url: serverUrl } + }); const [, { argv }] = await once(ipcMain, 'answer'); expect(argv).to.include('--enable-sandbox'); + + server.close(); }); it('should open windows with the options configured via setWindowOpenHandler handlers', async () => { @@ -3731,15 +3741,25 @@ describe('BrowserWindow module', () => { } }); + const server = http.createServer((req, res) => { + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end('Blank Page'); + }); + const serverUrl = (await listen(server)).url; + const preloadPath = path.join(mainFixtures, 'api', 'new-window-preload.js'); w.webContents.setWindowOpenHandler(() => ({ action: 'allow', overrideBrowserWindowOptions: { webPreferences: { preload: preloadPath, contextIsolation: false } } })); - w.loadFile(path.join(fixtures, 'api', 'new-window.html')); + await w.loadFile(path.join(fixtures, 'api', 'new-window.html'), { + query: { url: serverUrl } + }); const [[, childWebContents]] = await Promise.all([ once(app, 'web-contents-created') as Promise<[any, WebContents]>, once(ipcMain, 'answer') ]); const webPreferences = childWebContents.getLastWebPreferences(); expect(webPreferences!.contextIsolation).to.equal(false); + + server.close(); }); it('should set ipc event sender correctly', async () => { @@ -3909,10 +3929,20 @@ describe('BrowserWindow module', () => { expect(content).to.equal('Hello'); }); it('blocks accessing cross-origin frames', async () => { + const server = http.createServer((req, res) => { + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end('Blank Page'); + }); + const serverUrl = (await listen(server)).url; + const answer = once(ipcMain, 'answer'); - w.loadFile(path.join(fixtures, 'api', 'native-window-open-cross-origin.html')); + await w.loadFile(path.join(fixtures, 'api', 'native-window-open-cross-origin.html'), { + query: { url: serverUrl } + }); const [, content] = await answer; expect(content).to.equal('Failed to read a named property \'toString\' from \'Location\': Blocked a frame with origin "file://" from accessing a cross-origin frame.'); + + server.close(); }); it('opens window from