From 00c34fec6e46eeccabc7d1a168aabffbfa7d5497 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 4 Jul 2023 22:34:15 +0200 Subject: [PATCH] fix: webview crash when removing in close event --- .../browser/api/electron_api_web_contents.cc | 4 ++- .../webview-remove-on-wc-close/index.html | 32 +++++++++++++++++++ .../webview-remove-on-wc-close/index.js | 29 +++++++++++++++++ .../webview-remove-on-wc-close/webview.html | 19 +++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/crash-cases/webview-remove-on-wc-close/index.html create mode 100644 spec/fixtures/crash-cases/webview-remove-on-wc-close/index.js create mode 100644 spec/fixtures/crash-cases/webview-remove-on-wc-close/webview.html diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index b84ac07250562..5f4d985f8b727 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -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) { diff --git a/spec/fixtures/crash-cases/webview-remove-on-wc-close/index.html b/spec/fixtures/crash-cases/webview-remove-on-wc-close/index.html new file mode 100644 index 0000000000000..cbaaf2339d1c7 --- /dev/null +++ b/spec/fixtures/crash-cases/webview-remove-on-wc-close/index.html @@ -0,0 +1,32 @@ + + + + + + + Document + + + + + + + + + + diff --git a/spec/fixtures/crash-cases/webview-remove-on-wc-close/index.js b/spec/fixtures/crash-cases/webview-remove-on-wc-close/index.js new file mode 100644 index 0000000000000..6aba8425967b5 --- /dev/null +++ b/spec/fixtures/crash-cases/webview-remove-on-wc-close/index.js @@ -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(); + }); + }); +}); diff --git a/spec/fixtures/crash-cases/webview-remove-on-wc-close/webview.html b/spec/fixtures/crash-cases/webview-remove-on-wc-close/webview.html new file mode 100644 index 0000000000000..1402bea99b196 --- /dev/null +++ b/spec/fixtures/crash-cases/webview-remove-on-wc-close/webview.html @@ -0,0 +1,19 @@ + + + + + + + Document + + + +

webview page

+ + + +