Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle closing webContents in BrowserViews #37420

Merged
merged 2 commits into from Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions shell/browser/api/electron_api_browser_view.cc
Expand Up @@ -126,6 +126,9 @@ BrowserView::~BrowserView() {
}

void BrowserView::WebContentsDestroyed() {
if (owner_window())
owner_window()->window()->RemoveDraggableRegionProvider(this);

api_web_contents_ = nullptr;
web_contents_.Reset();
Unpin();
Expand Down
8 changes: 6 additions & 2 deletions shell/browser/api/electron_api_browser_window.cc
Expand Up @@ -112,6 +112,7 @@ BrowserWindow::~BrowserWindow() {
api_web_contents_->RemoveObserver(this);
// Destroy the WebContents.
OnCloseContents();
api_web_contents_->Destroy();
}
}

Expand Down Expand Up @@ -139,7 +140,6 @@ void BrowserWindow::WebContentsDestroyed() {

void BrowserWindow::OnCloseContents() {
BaseWindow::ResetBrowserViews();
api_web_contents_->Destroy();
}

void BrowserWindow::OnRendererResponsive(content::RenderProcessHost*) {
Expand Down Expand Up @@ -198,7 +198,11 @@ void BrowserWindow::OnCloseButtonClicked(bool* prevent_default) {

// Trigger beforeunload events for associated BrowserViews.
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);

// Required to make beforeunload handler work.
Expand Down
4 changes: 1 addition & 3 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -1222,9 +1222,7 @@ void WebContents::CloseContents(content::WebContents* source) {
for (ExtendedWebContentsObserver& observer : observers_)
observer.OnCloseContents();

// If there are observers, OnCloseContents will call Destroy()
if (observers_.empty())
Destroy();
Destroy();
}

void WebContents::ActivateContents(content::WebContents* source) {
Expand Down
18 changes: 18 additions & 0 deletions spec/api-browser-view-spec.ts
Expand Up @@ -345,6 +345,24 @@ describe('BrowserView module', () => {
const [code] = await once(rc.process, 'exit');
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()', () => {
Expand Down