fix: crash when focusing WebView webContents (#43921)
		
	fix: crash when focusing WebView
This commit is contained in:
		
					parent
					
						
							
								500d4f0d05
							
						
					
				
			
			
				commit
				
					
						bcf4095ace
					
				
			
		
					 2 changed files with 24 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -3339,6 +3339,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();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -3782,7 +3788,8 @@ void WebContents::SetBackgroundColor(std::optional<SkColor> 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<content::RenderWidgetHostViewBase*>(rwhv)
 | 
			
		||||
        ->SetContentBackgroundColor(color);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1207,6 +1207,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,<webview src="data:text/html,hi"></webview>');
 | 
			
		||||
 | 
			
		||||
        const wc = webContents.getAllWebContents().find((wc) => wc.getType() === 'webview')!;
 | 
			
		||||
        expect(() => wc.focus()).to.not.throw();
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    const moveFocusToDevTools = async (win: BrowserWindow) => {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue