From 52b50e6b33791df9f62237a1630fee4357c7e03c Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Sun, 17 May 2020 17:05:05 +0200 Subject: [PATCH] feat: add app render-process-gone event (#23560) --- docs/api/app.md | 24 ++++++++++++++++++++++++ lib/browser/api/web-contents.js | 4 ++++ spec-main/api-app-spec.ts | 19 +++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/docs/api/app.md b/docs/api/app.md index 268b8f7b1019..e234d5a6d5ab 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -369,6 +369,30 @@ Returns: Emitted when the renderer process of `webContents` crashes or is killed. +**Deprecated:** This event is superceded by the `render-process-gone` event +which contains more information about why the render process dissapeared. It +isn't always because it crashed. The `killed` boolean can be replaced by +checking `reason === 'killed'` when you switch to that event. + +#### Event: 'render-process-gone' + +Returns: + +* `event` Event +* `webContents` [WebContents](web-contents.md) +* `details` Object + * `reason` String - The reason the render process is gone. Possible values: + * `clean-exit` - Process exited with an exit code of zero + * `abnormal-exit` - Process exited with a non-zero exit code + * `killed` - Process was sent a SIGTERM or otherwise killed externally + * `crashed` - Process crashed + * `oom` - Process ran out of memory + * `launch-failure` - Process never successfully launched + * `integrity-failure` - Windows code integrity checks failed + +Emitted when the renderer process unexpectedly dissapears. This is normally +because it was crashed or killed. + ### Event: 'accessibility-support-changed' _macOS_ _Windows_ Returns: diff --git a/lib/browser/api/web-contents.js b/lib/browser/api/web-contents.js index 49eacd45ca18..884de6e65348 100644 --- a/lib/browser/api/web-contents.js +++ b/lib/browser/api/web-contents.js @@ -501,6 +501,10 @@ WebContents.prototype._init = function () { app.emit('renderer-process-crashed', event, this, ...args); }); + this.on('render-process-gone', (event, ...args) => { + app.emit('render-process-gone', event, this, ...args); + }); + // The devtools requests the webContents to reload. this.on('devtools-reload-page', function () { this.reload(); diff --git a/spec-main/api-app-spec.ts b/spec-main/api-app-spec.ts index 8f962807456f..803d66f8be2b 100644 --- a/spec-main/api-app-spec.ts +++ b/spec-main/api-app-spec.ts @@ -432,6 +432,25 @@ describe('app module', () => { expect(webContents).to.equal(w.webContents); }); + it('should emit render-process-gone event when renderer crashes', async function () { + // FIXME: re-enable this test on win32. + if (process.platform === 'win32') { return this.skip(); } + w = new BrowserWindow({ + show: false, + webPreferences: { + nodeIntegration: true + } + }); + await w.loadURL('about:blank'); + + const promise = emittedOnce(app, 'render-process-gone'); + w.webContents.executeJavaScript('process.crash()'); + + const [, webContents, details] = await promise; + expect(webContents).to.equal(w.webContents); + expect(details.reason).to.be.oneOf(['crashed', 'abnormal-exit']); + }); + ifdescribe(features.isDesktopCapturerEnabled())('desktopCapturer module filtering', () => { it('should emit desktop-capturer-get-sources event when desktopCapturer.getSources() is invoked', async () => { w = new BrowserWindow({