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

Don't load URL if web contents is destroyed #8254

Merged
merged 2 commits into from Dec 28, 2016
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
5 changes: 5 additions & 0 deletions atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -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);
}

Expand Down
6 changes: 6 additions & 0 deletions atom/browser/api/trackable_object.h
Expand Up @@ -65,6 +65,12 @@ class TrackableObject : public TrackableObjectBase,
Wrappable<T>::GetWrapper()->SetAlignedPointerInInternalField(0, nullptr);
}

bool IsDestroyed() {
v8::Local<v8::Object> wrapper = Wrappable<T>::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_)
Expand Down
10 changes: 10 additions & 0 deletions spec/api-browser-window-spec.js
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/static/main.js
Expand Up @@ -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')
})
})