fix: handle closing webContents in BrowserViews (#37420)

* fix: handle closing webContents in BrowserViews

* test: add window.close() test
This commit is contained in:
Shelley Vohr 2023-03-01 11:35:06 +01:00 committed by GitHub
parent 8fb0f43030
commit 5e25d23794
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 5 deletions

View file

@ -126,6 +126,9 @@ BrowserView::~BrowserView() {
} }
void BrowserView::WebContentsDestroyed() { void BrowserView::WebContentsDestroyed() {
if (owner_window())
owner_window()->window()->RemoveDraggableRegionProvider(this);
api_web_contents_ = nullptr; api_web_contents_ = nullptr;
web_contents_.Reset(); web_contents_.Reset();
Unpin(); Unpin();

View file

@ -112,6 +112,7 @@ BrowserWindow::~BrowserWindow() {
api_web_contents_->RemoveObserver(this); api_web_contents_->RemoveObserver(this);
// Destroy the WebContents. // Destroy the WebContents.
OnCloseContents(); OnCloseContents();
api_web_contents_->Destroy();
} }
} }
@ -139,7 +140,6 @@ void BrowserWindow::WebContentsDestroyed() {
void BrowserWindow::OnCloseContents() { void BrowserWindow::OnCloseContents() {
BaseWindow::ResetBrowserViews(); BaseWindow::ResetBrowserViews();
api_web_contents_->Destroy();
} }
void BrowserWindow::OnRendererResponsive(content::RenderProcessHost*) { void BrowserWindow::OnRendererResponsive(content::RenderProcessHost*) {
@ -198,7 +198,11 @@ void BrowserWindow::OnCloseButtonClicked(bool* prevent_default) {
// Trigger beforeunload events for associated BrowserViews. // Trigger beforeunload events for associated BrowserViews.
for (NativeBrowserView* view : window_->browser_views()) { for (NativeBrowserView* view : window_->browser_views()) {
auto* vwc = view->GetInspectableWebContents()->GetWebContents(); auto* iwc = view->GetInspectableWebContents();
if (!iwc)
continue;
auto* vwc = iwc->GetWebContents();
auto* api_web_contents = api::WebContents::From(vwc); auto* api_web_contents = api::WebContents::From(vwc);
// Required to make beforeunload handler work. // Required to make beforeunload handler work.

View file

@ -1223,9 +1223,7 @@ void WebContents::CloseContents(content::WebContents* source) {
for (ExtendedWebContentsObserver& observer : observers_) for (ExtendedWebContentsObserver& observer : observers_)
observer.OnCloseContents(); observer.OnCloseContents();
// If there are observers, OnCloseContents will call Destroy() Destroy();
if (observers_.empty())
Destroy();
} }
void WebContents::ActivateContents(content::WebContents* source) { void WebContents::ActivateContents(content::WebContents* source) {

View file

@ -345,6 +345,24 @@ describe('BrowserView module', () => {
const [code] = await once(rc.process, 'exit'); const [code] = await once(rc.process, 'exit');
expect(code).to.equal(0); expect(code).to.equal(0);
}); });
it('emits the destroyed event when webContents.close() is called', async () => {
view = new BrowserView();
w.setBrowserView(view);
await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
view.webContents.close();
await once(view.webContents, 'destroyed');
});
it('emits the destroyed event when window.close() is called', async () => {
view = new BrowserView();
w.setBrowserView(view);
await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
view.webContents.executeJavaScript('window.close()');
await once(view.webContents, 'destroyed');
});
}); });
describe('window.open()', () => { describe('window.open()', () => {