fix: webview crash when removing in close event (#38996)

This commit is contained in:
Shelley Vohr 2023-07-06 10:20:34 +02:00 committed by GitHub
parent 5a77c75753
commit c7a64ab994
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 1 deletions

View file

@ -1297,6 +1297,8 @@ void WebContents::CloseContents(content::WebContents* source) {
for (ExtendedWebContentsObserver& observer : observers_)
observer.OnCloseContents();
// This is handled by the embedder frame.
if (!IsGuest())
Destroy();
}

View file

@ -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>

View file

@ -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();
});
});
});

View file

@ -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>