Skip to content

Commit

Permalink
fix: crashed events deprecation (#40111)
Browse files Browse the repository at this point in the history
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Milan Burda <milan.burda@gmail.com>
  • Loading branch information
trop[bot] and miniak committed Oct 9, 2023
1 parent 8f370cd commit 7e20732
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
10 changes: 8 additions & 2 deletions lib/browser/api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,11 @@ for (const name of events) {
}

// Deprecation.
deprecate.event(app, 'gpu-process-crashed', 'child-process-gone');
deprecate.event(app, 'renderer-process-crashed', 'render-process-gone');
deprecate.event(app, 'gpu-process-crashed', 'child-process-gone', () => {
// the old event is still emitted by App::OnGpuProcessCrashed()
return undefined;
});

deprecate.event(app, 'renderer-process-crashed', 'render-process-gone', (event: Electron.Event, webContents: Electron.WebContents, details: Electron.RenderProcessGoneDetails) => {
return [event, webContents, details.reason === 'killed'];
});
4 changes: 2 additions & 2 deletions lib/browser/api/web-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,8 @@ WebContents.prototype._init = function () {
ipcMain.emit(channel, event, message);
});

this.on('crashed', (event, ...args) => {
app.emit('renderer-process-crashed', event, this, ...args);
deprecate.event(this, 'crashed', 'render-process-gone', (event: Electron.Event, details: Electron.RenderProcessGoneDetails) => {
return [event, details.reason === 'killed'];
});

this.on('render-process-gone', (event, details) => {
Expand Down
7 changes: 5 additions & 2 deletions lib/common/deprecate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,17 @@ export function renameFunction<T extends Function> (fn: T, newName: string): T {
}

// change the name of an event
export function event (emitter: NodeJS.EventEmitter, oldName: string, newName: string) {
export function event (emitter: NodeJS.EventEmitter, oldName: string, newName: string, transformer: (...args: any[]) => any[] | undefined = (...args) => args) {
const warn = newName.startsWith('-') /* internal event */
? warnOnce(`${oldName} event`)
: warnOnce(`${oldName} event`, `${newName} event`);
return emitter.on(newName, function (this: NodeJS.EventEmitter, ...args) {
if (this.listenerCount(oldName) !== 0) {
warn();
this.emit(oldName, ...args);
const transformedArgs = transformer(...args);
if (transformedArgs) {
this.emit(oldName, ...transformedArgs);
}
}
});
}
Expand Down
7 changes: 0 additions & 7 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1726,13 +1726,6 @@ void WebContents::RenderViewDeleted(content::RenderViewHost* render_view_host) {

void WebContents::PrimaryMainFrameRenderProcessGone(
base::TerminationStatus status) {
auto weak_this = GetWeakPtr();
Emit("crashed", status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED);

// User might destroy WebContents in the crashed event.
if (!weak_this || !web_contents())
return;

v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate);
gin_helper::Dictionary details = gin_helper::Dictionary::CreateEmpty(isolate);
Expand Down

0 comments on commit 7e20732

Please sign in to comment.