diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index f66c6d2fefe9..a57e3042d5b2 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -3300,6 +3300,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(); } @@ -3728,7 +3734,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 188763e0ca49..e182e83a798c 100644 --- a/spec/api-web-contents-spec.ts +++ b/spec/api-web-contents-spec.ts @@ -1077,6 +1077,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) => {