From 1308f790ad25f8c2286a4e3a89e9843c8e4408ac Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 20 Dec 2016 16:51:03 -0800 Subject: [PATCH 1/2] Don't load URL if web contents is destroyed --- atom/browser/api/atom_api_web_contents.cc | 5 +++++ atom/browser/api/trackable_object.h | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 09c220c6e1c..a45e166c92d 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -439,6 +439,11 @@ content::WebContents* WebContents::OpenURLFromTab( if (Emit("will-navigate", params.url)) return nullptr; + // Don't load the URL if the web contents was marked as destroyed from a + // will-navigate event listener + if (IsDestroyed()) + return nullptr; + return CommonWebContentsDelegate::OpenURLFromTab(source, params); } diff --git a/atom/browser/api/trackable_object.h b/atom/browser/api/trackable_object.h index 3f04783bcd5..7ec54457886 100644 --- a/atom/browser/api/trackable_object.h +++ b/atom/browser/api/trackable_object.h @@ -65,6 +65,12 @@ class TrackableObject : public TrackableObjectBase, Wrappable::GetWrapper()->SetAlignedPointerInInternalField(0, nullptr); } + bool IsDestroyed() { + v8::Local wrapper = Wrappable::GetWrapper(); + return wrapper->InternalFieldCount() == 0 || + wrapper->GetAlignedPointerFromInternalField(0) == nullptr; + } + // Finds out the TrackableObject from its ID in weak map. static T* FromWeakMapID(v8::Isolate* isolate, int32_t id) { if (!weak_map_) From abea1196de4b3a898da8aa31c04344c9d507f532 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 21 Dec 2016 10:00:27 -0800 Subject: [PATCH 2/2] Add spec for closing from will-navigate listener --- spec/api-browser-window-spec.js | 10 ++++++++++ spec/static/main.js | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index b718a6aa493..eabd9209e8f 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -267,6 +267,16 @@ describe('BrowserWindow module', function () { }) }) + describe('will-navigate event', function () { + it('allows the window to be closed from the event listener', (done) => { + ipcRenderer.send('close-on-will-navigate', w.id) + ipcRenderer.once('closed-on-will-navigate', () => { + done() + }) + w.loadURL('file://' + fixtures + '/pages/will-navigate.html') + }) + }) + describe('BrowserWindow.show()', function () { if (isCI) { return diff --git a/spec/static/main.js b/spec/static/main.js index 19b07399301..259350f9598 100644 --- a/spec/static/main.js +++ b/spec/static/main.js @@ -221,3 +221,12 @@ ipcMain.on('set-client-certificate-option', function (event, skip) { }) event.returnValue = 'done' }) + +ipcMain.on('close-on-will-navigate', (event, id) => { + const contents = event.sender + const window = BrowserWindow.fromId(id) + window.webContents.once('will-navigate', (event, input) => { + window.close() + contents.send('closed-on-will-navigate') + }) +})