From 0eb9c4b16d5dcd6be4b5722e0239c1b319c99b11 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 11 Oct 2021 09:33:35 +0900 Subject: [PATCH] fix: correctly notify WebViewGuestDelegate when webview is detached (#31350) --- shell/browser/api/electron_api_web_contents.cc | 6 ++++-- spec-main/webview-spec.ts | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) 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', () => {