fix: crash calling BrowserWindow.removeBrowserView() with destroyed webContents (#38884)

fix: crash calling removeBrowserView() with destroyed webContents

https://github.com/electron/electron/issues/37642

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot]
2023-06-22 11:22:30 +02:00
committed by GitHub
parent e47bdb67f8
commit 192295697e
3 changed files with 21 additions and 0 deletions

View File

@@ -134,6 +134,10 @@ void BrowserView::WebContentsDestroyed() {
Unpin();
}
void BrowserView::OnCloseContents() {
api_web_contents_ = nullptr;
}
// static
gin::Handle<BrowserView> BrowserView::New(gin_helper::ErrorThrower thrower,
gin::Arguments* args) {

View File

@@ -71,6 +71,9 @@ class BrowserView : public gin::Wrappable<BrowserView>,
// content::WebContentsObserver:
void WebContentsDestroyed() override;
// ExtendedWebContentsObserver:
void OnCloseContents() override;
private:
void SetAutoResize(AutoResizeFlags flags);
void SetBounds(const gfx::Rect& bounds);

View File

@@ -257,6 +257,20 @@ describe('BrowserView module', () => {
w.removeBrowserView(view);
}).to.not.throw();
});
it('can be called on a BrowserView with a destroyed webContents', (done) => {
view = new BrowserView();
w.addBrowserView(view);
view.webContents.on('destroyed', () => {
w.removeBrowserView(view);
done();
});
view.webContents.loadURL('data:text/html,hello there').then(() => {
view.webContents.close();
});
});
});
describe('BrowserWindow.getBrowserViews()', () => {