From c5da330cdfbf0eb0dedead91953ed68dd8076a2a Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 10 Jan 2017 15:34:29 -0800 Subject: [PATCH 1/4] Add failing spec for sandboxed window.open --- spec/api-browser-window-spec.js | 19 +++++++++++++++++++ spec/static/main.js | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index f19ff06b1110..9f45492dcd18 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1012,6 +1012,25 @@ describe('BrowserWindow module', function () { }) }) }) + + it('supports calling preventDefault on new-window events', (done) => { + w.destroy() + w = new BrowserWindow({ + show: false, + webPreferences: { + sandbox: true, + } + }) + const initialWebContents = webContents.getAllWebContents() + ipcRenderer.send('prevent-next-new-window', w.webContents.id) + w.webContents.once('new-window', () => { + process.nextTick(() => { + assert.deepEqual(webContents.getAllWebContents().length, initialWebContents.length) + done() + }) + }) + w.loadURL('file://' + path.join(fixtures, 'pages', 'window-open.html')) + }) }) }) diff --git a/spec/static/main.js b/spec/static/main.js index b9d7d9473c31..2b625fd09528 100644 --- a/spec/static/main.js +++ b/spec/static/main.js @@ -245,3 +245,8 @@ ipcMain.on('create-window-with-options-cycle', (event) => { const window = new BrowserWindow({show: false, foo: foo}) event.returnValue = window.id }) + + +ipcMain.on('prevent-next-new-window', (event, id) => { + webContents.fromId(id).once('new-window', event => event.preventDefault()) +}) From 8f63fdb278ccefa4847748637ec69ee04c887ba7 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 10 Jan 2017 15:40:07 -0800 Subject: [PATCH 2/4] Destroy web contents when preventing default --- atom/browser/api/atom_api_web_contents.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index a3753a6d8f05..af64d9017c4a 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -419,9 +419,11 @@ void WebContents::AddNewContents(content::WebContents* source, v8::Locker locker(isolate()); v8::HandleScope handle_scope(isolate()); auto api_web_contents = CreateFrom(isolate(), new_contents); - Emit("-add-new-contents", api_web_contents, disposition, user_gesture, + if (Emit("-add-new-contents", api_web_contents, disposition, user_gesture, initial_rect.x(), initial_rect.y(), initial_rect.width(), - initial_rect.height()); + initial_rect.height())) { + api_web_contents->DestroyWebContents(); + } } content::WebContents* WebContents::OpenURLFromTab( From 8098ca3b6679760cede9facb8109c78b19a7dc9f Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 10 Jan 2017 15:46:58 -0800 Subject: [PATCH 3/4] Remove unneeded nextTick and assert array --- spec/api-browser-window-spec.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 9f45492dcd18..aa98bc0f7377 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1024,10 +1024,8 @@ describe('BrowserWindow module', function () { const initialWebContents = webContents.getAllWebContents() ipcRenderer.send('prevent-next-new-window', w.webContents.id) w.webContents.once('new-window', () => { - process.nextTick(() => { - assert.deepEqual(webContents.getAllWebContents().length, initialWebContents.length) - done() - }) + assert.deepEqual(webContents.getAllWebContents(), initialWebContents) + done() }) w.loadURL('file://' + path.join(fixtures, 'pages', 'window-open.html')) }) From 97dd680b3241288bc5be9c9529ff8d770b922608 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 12 Jan 2017 10:28:02 -0800 Subject: [PATCH 4/4] Remove linter errors --- spec/api-browser-window-spec.js | 2 +- spec/static/main.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index aa98bc0f7377..ab5d6d2d0d0b 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1018,7 +1018,7 @@ describe('BrowserWindow module', function () { w = new BrowserWindow({ show: false, webPreferences: { - sandbox: true, + sandbox: true } }) const initialWebContents = webContents.getAllWebContents() diff --git a/spec/static/main.js b/spec/static/main.js index 2b625fd09528..9612ed25cf05 100644 --- a/spec/static/main.js +++ b/spec/static/main.js @@ -246,7 +246,6 @@ ipcMain.on('create-window-with-options-cycle', (event) => { event.returnValue = window.id }) - ipcMain.on('prevent-next-new-window', (event, id) => { webContents.fromId(id).once('new-window', event => event.preventDefault()) })