Skip to content

Commit b6321cc

Browse files
authored
fix: crash when navigating from a page with webview that has inherited zoom level (electron#24757)
* fix: cleanup webview zoom level observers on navigation * add spec * webview should be on same partition * wait for webview to finish loading
1 parent 38fafe4 commit b6321cc

5 files changed

Lines changed: 41 additions & 0 deletions

File tree

shell/browser/api/electron_api_web_contents.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,11 @@ bool WebContents::OnMessageReceived(const IPC::Message& message) {
14131413
// we need to make sure the api::WebContents is also deleted.
14141414
// For #4, the WebContents will be destroyed by embedder.
14151415
void WebContents::WebContentsDestroyed() {
1416+
// Give chance for guest delegate to cleanup its observers
1417+
// since the native class is only destroyed in the next tick.
1418+
if (guest_delegate_)
1419+
guest_delegate_->WillDestroy();
1420+
14161421
// Cleanup relationships with other parts.
14171422
RemoveFromWeakMap();
14181423

shell/browser/web_view_guest_delegate.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ void WebViewGuestDelegate::AttachToIframe(
5757
api_web_contents_->Emit("did-attach");
5858
}
5959

60+
void WebViewGuestDelegate::WillDestroy() {
61+
ResetZoomController();
62+
}
63+
6064
void WebViewGuestDelegate::DidDetach() {
6165
ResetZoomController();
6266
}

shell/browser/web_view_guest_delegate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class WebViewGuestDelegate : public content::BrowserPluginGuestDelegate,
2424
// Attach to the iframe.
2525
void AttachToIframe(content::WebContents* embedder_web_contents,
2626
int embedder_frame_id);
27+
void WillDestroy();
2728

2829
protected:
2930
// content::BrowserPluginGuestDelegate:

spec-main/webview-spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,25 @@ describe('<webview> tag', function () {
315315
const [, zoomLevel] = await emittedOnce(ipcMain, 'webview-origin-zoom-level');
316316
expect(zoomLevel).to.equal(2.0);
317317
});
318+
319+
it('does not crash when navigating with zoom level inherited from parent', async () => {
320+
const w = new BrowserWindow({
321+
show: false,
322+
webPreferences: {
323+
webviewTag: true,
324+
nodeIntegration: true,
325+
zoomFactor: 1.2,
326+
session: webviewSession
327+
}
328+
});
329+
const attachPromise = emittedOnce(w.webContents, 'did-attach-webview');
330+
const readyPromise = emittedOnce(ipcMain, 'dom-ready');
331+
w.loadFile(path.join(fixtures, 'pages', 'webview-zoom-inherited.html'));
332+
const [, webview] = await attachPromise;
333+
await readyPromise;
334+
expect(webview.getZoomFactor()).to.equal(1.2);
335+
await w.loadURL(`${zoomScheme}://host1`);
336+
});
318337
});
319338

320339
describe('nativeWindowOpen option', () => {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
<body>
3+
<webview src="./a.html" id="view" partition="webview-temp"/>
4+
</body>
5+
<script>
6+
const {ipcRenderer} = require('electron')
7+
const view = document.getElementById('view')
8+
view.addEventListener('dom-ready', () => {
9+
ipcRenderer.send('dom-ready')
10+
})
11+
</script>
12+
</html>

0 commit comments

Comments
 (0)