Merge pull request #8254 from electron/webcontents-will-navigate-crash

Don't load URL if web contents is destroyed
This commit is contained in:
Kevin Sawicki 2016-12-28 13:40:31 -08:00 committed by GitHub
commit bc39964ece
4 changed files with 30 additions and 0 deletions

View file

@ -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);
}

View file

@ -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_)

View file

@ -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

View file

@ -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')
})
})