Skip to content

[Bug]: window.close from inside a page in a BrowserView will close the entire window #28260

@pushkin-

Description

@pushkin-

Preflight Checklist

Electron Version

12.0.1

What operating system are you using?

Windows

Operating System Version

Windows 10 v1909

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

when executing window.close() in web page in a BrowserView, the window won't close.

Actual Behavior

The window is closed

Testcase Gist URL

No response

This is a continuation of this closed issue

  1. run main.js
  2. execute window.close() inside of web page's devtools
  3. observe that the window closes

main.js:

(async () => {
	const { app, BrowserWindow, BrowserView } = require("electron");
	app.on("window-all-closed", async () => {
		app.quit();
	});

	await app.whenReady();
	const mainWindow = new BrowserWindow();
	const view = new BrowserView();
	view.setBounds({x: 0, y: 0, width: 800, height: 600});
	mainWindow.setBrowserView(view);

	await view.webContents.loadURL("https://google.com");
	view.webContents.openDevTools();
})();

Some notes form the closed issue:

I think the issue is that if you have a BrowserWindow with a BrowserView attached to it, calling window.close from inside the view will close the parent window, which isn't what I would expect to happen. Since the close event on BrowserWindow doesn't provide a way to tell whether the window itself or just the view is closing, there's no way to prevent this behavior without also making it impossible to close the window.

I think the expected behavior would be that calling close() from inside a BrowserView doesn't do anything to the window, and the view would emit a close event to let you destroy the view (like how webviews work).

This PR was opened to address this but never landed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions