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: webview crash when removing in close event #38996

Merged
merged 1 commit into from
Jul 6, 2023
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
4 changes: 3 additions & 1 deletion shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,9 @@ void WebContents::CloseContents(content::WebContents* source) {
for (ExtendedWebContentsObserver& observer : observers_)
observer.OnCloseContents();

Destroy();
// This is handled by the embedder frame.
if (!IsGuest())
Destroy();
}

void WebContents::ActivateContents(content::WebContents* source) {
Expand Down
32 changes: 32 additions & 0 deletions spec/fixtures/crash-cases/webview-remove-on-wc-close/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.webview {
border: 1px solid black;
}
</style>
</head>

<body>
<button class="close-btn">close webview</button>
<webview class="webview" src="./webview.html"></webview>
<script>
const close = document.querySelector('.close-btn')
const webview = document.querySelector('.webview')

webview.addEventListener('close', () => {
webview.parentNode.removeChild(webview)
})

close.addEventListener('click', () => {
webview.executeJavaScript('window.close()', true)
})
</script>
</body>

</html>
29 changes: 29 additions & 0 deletions spec/fixtures/crash-cases/webview-remove-on-wc-close/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { app, BrowserWindow } = require('electron');

app.whenReady().then(() => {
const win = new BrowserWindow({
webPreferences: {
webviewTag: true
}
});

win.loadFile('index.html');

win.webContents.on('did-attach-webview', (event, contents) => {
contents.on('render-process-gone', () => {
process.exit(1);
});

contents.on('destroyed', () => {
process.exit(0);
});

contents.on('did-finish-load', () => {
win.webContents.executeJavaScript('document.querySelector(\'.close-btn\').click()');
});

contents.on('will-prevent-unload', event => {
event.preventDefault();
});
});
});
19 changes: 19 additions & 0 deletions spec/fixtures/crash-cases/webview-remove-on-wc-close/webview.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<h1>webview page</h1>
<script>
window.addEventListener('beforeunload', event => {
event.returnValue = 'test'
})
</script>
</body>

</html>