From ff94658d4683235ffc734c67e26a728f14092cd8 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Wed, 25 Sep 2024 13:17:37 +0200 Subject: [PATCH] fix: crash when focusing `WebView` `webContents` (#43934) fix: crash when focusing WebView Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr --- shell/browser/api/electron_api_web_contents.cc | 9 ++++++++- spec/api-web-contents-spec.ts | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 1f17229b9f11..ac276102d2ed 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -3305,6 +3305,12 @@ void WebContents::Focus() { if (owner_window()) owner_window()->Focus(true); #endif + + // WebView uses WebContentsViewChildFrame, which doesn't have a Focus impl + // and triggers a fatal NOTREACHED. + if (is_guest()) + return; + web_contents()->Focus(); } @@ -3733,7 +3739,8 @@ void WebContents::SetBackgroundColor(std::optional maybe_color) { content::RenderWidgetHostView* rwhv = rfh->GetView(); if (rwhv) { - // RenderWidgetHostView doesn't allow setting an alpha that's not 0 or 255. + // RenderWidgetHostView doesn't allow setting an alpha that's not 0 or + // 255. rwhv->SetBackgroundColor(is_opaque ? color : SK_ColorTRANSPARENT); static_cast(rwhv) ->SetContentBackgroundColor(color); diff --git a/spec/api-web-contents-spec.ts b/spec/api-web-contents-spec.ts index 9528b4552196..484db8657595 100644 --- a/spec/api-web-contents-spec.ts +++ b/spec/api-web-contents-spec.ts @@ -1155,6 +1155,22 @@ describe('webContents module', () => { expect(currentFocused).to.be.true(); expect(childFocused).to.be.false(); }); + + it('does not crash when focusing a WebView webContents', async () => { + const w = new BrowserWindow({ + show: false, + webPreferences: { + nodeIntegration: true, + webviewTag: true + } + }); + + w.show(); + await w.loadURL('data:text/html,'); + + const wc = webContents.getAllWebContents().find((wc) => wc.getType() === 'webview')!; + expect(() => wc.focus()).to.not.throw(); + }); }); const moveFocusToDevTools = async (win: BrowserWindow) => {