diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 1eaf9eafd9d9..a6ab707e6b63 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -939,8 +939,6 @@ WebContents::~WebContents() { } inspectable_web_contents_->GetView()->SetDelegate(nullptr); - if (guest_delegate_) - guest_delegate_->WillDestroy(); // This event is only for internal use, which is emitted when WebContents is // being destroyed. @@ -1957,6 +1955,10 @@ void WebContents::WebContentsDestroyed() { return; wrapper->SetAlignedPointerInInternalField(0, nullptr); + // Tell WebViewGuestDelegate that the WebContents has been destroyed. + if (guest_delegate_) + guest_delegate_->WillDestroy(); + Observe(nullptr); Emit("destroyed"); } diff --git a/spec-main/webview-spec.ts b/spec-main/webview-spec.ts index 9aa4e5f95be2..99a8d4e8d1b0 100644 --- a/spec-main/webview-spec.ts +++ b/spec-main/webview-spec.ts @@ -397,6 +397,23 @@ describe(' tag', function () { expect(webview.getZoomFactor()).to.equal(1.2); await w.loadURL(`${zoomScheme}://host1`); }); + + it('does not crash when changing zoom level after webview is destroyed', async () => { + const w = new BrowserWindow({ + show: false, + webPreferences: { + webviewTag: true, + nodeIntegration: true, + session: webviewSession, + contextIsolation: false + } + }); + const attachPromise = emittedOnce(w.webContents, 'did-attach-webview'); + await w.loadFile(path.join(fixtures, 'pages', 'webview-zoom-inherited.html')); + await attachPromise; + await w.webContents.executeJavaScript('view.remove()'); + w.webContents.setZoomLevel(0.5); + }); }); describe('requestFullscreen from webview', () => {