feat: add webContents.setWindowOpenHandler API (#24517)

Co-authored-by: Jeremy Rose <jeremya@chromium.org>
This commit is contained in:
loc 2020-11-10 09:06:03 -08:00 committed by GitHub
parent 6b222a2d8a
commit 0b85fdf26c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
56 changed files with 2087 additions and 885 deletions

View file

@ -2175,20 +2175,27 @@ describe('BrowserWindow module', () => {
it('should open windows in same domain with cross-scripting enabled', async () => {
const w = new BrowserWindow({
show: false,
show: true,
webPreferences: {
sandbox: true,
preload
}
});
w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
options.webPreferences!.preload = preload;
});
w.webContents.setWindowOpenHandler(() => ({
action: 'allow',
overrideBrowserWindowOptions: {
webPreferences: {
preload
}
}
}));
const htmlPath = path.join(__dirname, 'fixtures', 'api', 'sandbox.html?window-open');
const pageUrl = 'file://' + htmlPath;
const answer = emittedOnce(ipcMain, 'answer');
w.loadURL(pageUrl);
const [, url, frameName, , options] = await emittedOnce(w.webContents, 'new-window');
const [, { url, frameName, options }] = await emittedOnce(w.webContents, 'did-create-window');
const expectedUrl = process.platform === 'win32'
? 'file:///' + htmlPath.replace(/\\/g, '/')
: pageUrl;
@ -2202,16 +2209,22 @@ describe('BrowserWindow module', () => {
it('should open windows in another domain with cross-scripting disabled', async () => {
const w = new BrowserWindow({
show: false,
show: true,
webPreferences: {
sandbox: true,
preload
}
});
w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
options.webPreferences!.preload = preload;
});
w.webContents.setWindowOpenHandler(() => ({
action: 'allow',
overrideBrowserWindowOptions: {
webPreferences: {
preload
}
}
}));
w.loadFile(
path.join(__dirname, 'fixtures', 'api', 'sandbox.html'),
{ search: 'window-open-external' }
@ -2267,12 +2280,10 @@ describe('BrowserWindow module', () => {
});
const preloadPath = path.join(fixtures, 'api', 'new-window-preload.js');
w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
options.webPreferences!.preload = preloadPath;
});
w.webContents.setWindowOpenHandler(() => ({ action: 'allow', overrideBrowserWindowOptions: { webPreferences: { preload: preloadPath } } }));
w.loadFile(path.join(fixtures, 'api', 'new-window.html'));
const [, args] = await emittedOnce(ipcMain, 'answer');
expect(args).to.include('--enable-sandbox');
const [, { argv }] = await emittedOnce(ipcMain, 'answer');
expect(argv).to.include('--enable-sandbox');
});
it('should open windows with the options configured via new-window event listeners', async () => {
@ -2284,11 +2295,7 @@ describe('BrowserWindow module', () => {
});
const preloadPath = path.join(fixtures, 'api', 'new-window-preload.js');
w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
options.webPreferences!.preload = preloadPath;
const prefs = options.webPreferences as any;
prefs.foo = 'bar';
});
w.webContents.setWindowOpenHandler(() => ({ action: 'allow', overrideBrowserWindowOptions: { webPreferences: { preload: preloadPath, foo: 'bar' } } }));
w.loadFile(path.join(fixtures, 'api', 'new-window.html'));
const [[, childWebContents]] = await Promise.all([
emittedOnce(app, 'web-contents-created'),
@ -2307,11 +2314,13 @@ describe('BrowserWindow module', () => {
}
});
let childWc: WebContents | null = null;
w.webContents.on('new-window', (event, url, frameName, disposition, options) => {
options.webPreferences!.preload = preload;
childWc = (options as any).webContents;
w.webContents.setWindowOpenHandler(() => ({ action: 'allow', overrideBrowserWindowOptions: { webPreferences: { preload } } }));
w.webContents.on('did-create-window', (win) => {
childWc = win.webContents;
expect(w.webContents).to.not.equal(childWc);
});
ipcMain.once('parent-ready', function (event) {
expect(event.sender).to.equal(w.webContents, 'sender should be the parent');
event.sender.send('verified');
@ -2438,9 +2447,7 @@ describe('BrowserWindow module', () => {
enableRemoteModule: true
}
});
w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
options.webPreferences!.preload = preload;
});
w.webContents.setWindowOpenHandler(() => ({ action: 'allow', overrideBrowserWindowOptions: { webPreferences: { preload } } }));
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'sandbox.html'), { search: 'reload-remote-child' });
@ -2602,20 +2609,30 @@ describe('BrowserWindow module', () => {
});
it('should inherit the nativeWindowOpen setting in opened windows', async () => {
const preloadPath = path.join(fixtures, 'api', 'new-window-preload.js');
w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
options.webPreferences!.preload = preloadPath;
});
w.webContents.setWindowOpenHandler(() => ({
action: 'allow',
overrideBrowserWindowOptions: {
webPreferences: {
preload: preloadPath
}
}
}));
w.loadFile(path.join(fixtures, 'api', 'new-window.html'));
const [, args] = await emittedOnce(ipcMain, 'answer');
expect(args).to.include('--native-window-open');
const [, { nativeWindowOpen }] = await emittedOnce(ipcMain, 'answer');
expect(nativeWindowOpen).to.be.true();
});
it('should open windows with the options configured via new-window event listeners', async () => {
const preloadPath = path.join(fixtures, 'api', 'new-window-preload.js');
w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
options.webPreferences!.preload = preloadPath;
const prefs = options.webPreferences! as any;
prefs.foo = 'bar';
});
w.webContents.setWindowOpenHandler(() => ({
action: 'allow',
overrideBrowserWindowOptions: {
webPreferences: {
preload: preloadPath,
foo: 'bar'
}
}
}));
w.loadFile(path.join(fixtures, 'api', 'new-window.html'));
const [[, childWebContents]] = await Promise.all([
emittedOnce(app, 'web-contents-created'),
@ -2655,13 +2672,19 @@ describe('BrowserWindow module', () => {
}
});
w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
options.webPreferences!.preload = path.join(fixtures, 'api', 'window-open-preload.js');
});
w.webContents.setWindowOpenHandler(() => ({
action: 'allow',
overrideBrowserWindowOptions: {
webPreferences: {
preload: path.join(fixtures, 'api', 'window-open-preload.js')
}
}
}));
w.loadFile(path.join(fixtures, 'api', 'window-open-location-open.html'));
const [, args, typeofProcess] = await emittedOnce(ipcMain, 'answer');
expect(args).not.to.include('--node-integration');
expect(args).to.include('--native-window-open');
const [, { nodeIntegration, nativeWindowOpen, typeofProcess }] = await emittedOnce(ipcMain, 'answer');
expect(nodeIntegration).to.be.false();
expect(nativeWindowOpen).to.be.true();
expect(typeofProcess).to.eql('undefined');
});
@ -2675,11 +2698,16 @@ describe('BrowserWindow module', () => {
}
});
w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
options.webPreferences!.preload = path.join(fixtures, 'api', 'window-open-preload.js');
});
w.webContents.setWindowOpenHandler(() => ({
action: 'allow',
overrideBrowserWindowOptions: {
webPreferences: {
preload: path.join(fixtures, 'api', 'window-open-preload.js')
}
}
}));
w.loadFile(path.join(fixtures, 'api', 'window-open-location-open.html'));
const [, , , windowOpenerIsNull] = await emittedOnce(ipcMain, 'answer');
const [, { windowOpenerIsNull }] = await emittedOnce(ipcMain, 'answer');
expect(windowOpenerIsNull).to.be.false('window.opener is null');
});
});