Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check web_contents() for destroyed WebContents #27965

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -877,7 +877,7 @@ content::WebContents* WebContents::OpenURLFromTab(
return nullptr;
}

if (!weak_this)
if (!weak_this || !web_contents())
return nullptr;

return CommonWebContentsDelegate::OpenURLFromTab(source, params);
Expand Down Expand Up @@ -1139,7 +1139,7 @@ void WebContents::RenderProcessGone(base::TerminationStatus status) {
Emit("crashed", status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED);

// User might destroy WebContents in the crashed event.
if (!weak_this)
if (!weak_this || !web_contents())
return;

v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
Expand Down Expand Up @@ -1209,7 +1209,7 @@ void WebContents::DidFinishLoad(content::RenderFrameHost* render_frame_host,
// ⚠️WARNING!⚠️
// Emit() triggers JS which can call destroy() on |this|. It's not safe to
// assume that |this| points to valid memory at this point.
if (is_main_frame && weak_this)
if (is_main_frame && weak_this && web_contents())
Emit("did-finish-load");
}

Expand Down Expand Up @@ -1580,6 +1580,7 @@ void WebContents::WebContentsDestroyed() {
// also do not want any method to be used, so just mark as destroyed here.
MarkDestroyed();

Observe(nullptr); // this->web_contents() will return nullptr
Emit("destroyed");

// For guest view based on OOPIF, the WebContents is released by the embedder
Expand Down Expand Up @@ -1726,7 +1727,7 @@ void WebContents::LoadURL(const GURL& url,
// ⚠️WARNING!⚠️
// LoadURLWithParams() triggers JS events which can call destroy() on |this|.
// It's not safe to assume that |this| points to valid memory at this point.
if (!weak_this)
if (!weak_this || !web_contents())
return;
}

Expand Down
7 changes: 7 additions & 0 deletions spec-main/api-web-contents-spec.ts
Expand Up @@ -2007,6 +2007,13 @@ describe('webContents module', () => {
});
contents.loadURL('about:blank').then(() => contents.forcefullyCrashRenderer());
});

it('does not crash main process when quiting in it', async () => {
const appPath = path.join(mainFixturesPath, 'apps', 'quit', 'main.js');
const appProcess = ChildProcess.spawn(process.execPath, [appPath]);
const [code] = await emittedOnce(appProcess, 'close');
expect(code).to.equal(0);
});
});

it('emits a cancelable event before creating a child webcontents', async () => {
Expand Down
10 changes: 10 additions & 0 deletions spec-main/fixtures/apps/quit/main.js
@@ -0,0 +1,10 @@
const { app, BrowserWindow } = require('electron');

app.once('ready', () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
w.webContents.once('crashed', () => {
app.quit();
});
w.webContents.loadURL('about:blank');
w.webContents.executeJavaScript('process.crash()');
});