fix: addChildView() crashes when adding a closed WebContentsView (#47340)

fix: addChildView() crashes when add a closed WebContentsView

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Sida Zhu <zhusida@bytedance.com>
This commit is contained in:
trop[bot] 2025-06-04 14:08:44 +02:00 committed by GitHub
parent 492589670b
commit 2336cd67b7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 0 deletions

View file

@ -216,6 +216,12 @@ void View::AddChildViewAt(gin::Handle<View> child,
if (!view_)
return;
if (!child->view()) {
gin_helper::ErrorThrower(isolate()).ThrowError(
"Can't add a destroyed child view to a parent view");
return;
}
// This will CHECK and crash in View::AddChildViewAtImpl if not handled here.
if (view_ == child->view()) {
gin_helper::ErrorThrower(isolate()).ThrowError(

View file

@ -55,6 +55,20 @@ describe('WebContentsView', () => {
})).to.throw('options.webContents is already attached to a window');
});
it('should throw an error when adding a destroyed child view to the parent view', async () => {
const browserWindow = new BrowserWindow();
const webContentsView = new WebContentsView();
webContentsView.webContents.loadURL('about:blank');
webContentsView.webContents.destroy();
const destroyed = once(webContentsView.webContents, 'destroyed');
await destroyed;
expect(() => browserWindow.contentView.addChildView(webContentsView)).to.throw(
'Can\'t add a destroyed child view to a parent view'
);
});
it('should throw error when created with already attached webContents to other WebContentsView', () => {
const browserWindow = new BrowserWindow();