Skip to content

refactor: follow-ups to emitting WebContents' forwarded events natively - #53981

Merged
MarshallOfSound merged 1 commit into
mainfrom
refactor/webcontents-events-followup
Sep 16, 2026
Merged

MarshallOfSound merged 1 commit into
mainfrom
refactor/webcontents-events-followup

Conversation

@MarshallOfSound

Copy link
Copy Markdown
Member

Description of Change

Follow-up to #53975 addressing @ckerr's review:

  • render-process-gone / login: the WebContents' own listeners now run off the wrapper captured before the app emit, so they still run if an app listener destroys the WebContents (as the remainder of an in-progress emit() did before), and the native object is not touched after the app emit.
  • select-bluetooth-device: the chooser and the WebContents are re-checked after calling listenerCount(), which an app can replace.
  • Removed the unused weak pointer in OnFirstNonEmptyLayout; const isolate locals; brace-initialised the callback scopes.
  • Specs for the ordering guarantees: app listeners before the webContents', one microtask/nextTick drain after both, and the destroyed-by-app-listener case.

On the microtask question: the paired emits share one outer node::CallbackScope, so the inner MakeCallback scopes are nested (depth > 1) and neither they nor gin's kRunMicrotasks scope (explicit microtask policy in the browser process) checkpoint; ticks and microtasks run once when the outer scope closes, after both emits — the new spec asserts exactly that and matches the pre-#53975 behaviour. One ordering difference does remain and is intentional: a webContents.prependListener('login' | 'render-process-gone', …) listener used to run before app listeners (it sat ahead of the internal forwarding listener) and now runs after them.

Checklist

Release Notes

Notes: none

- 'render-process-gone' and 'login': the WebContents' own listeners run off
  the wrapper saved before the app emit, so they still run when an app
  listener destroys the WebContents (as the rest of an emit in progress
  did), and nothing of the native object is used after the app emit
- select-bluetooth-device: re-check the chooser and the WebContents after
  calling the app-replaceable listenerCount()
- drop the now-unused weak pointer in OnFirstNonEmptyLayout, make the
  isolate locals const, brace-initialise the callback scopes
- specs for the app-before-webContents order, the shared microtask drain,
  and the destroyed-by-app-listener case
@MarshallOfSound MarshallOfSound added semver/none target/45-x-y PR should also be added to the "45-x-y" branch. labels Sep 16, 2026
@MarshallOfSound
MarshallOfSound merged commit 4d10ef9 into main Sep 16, 2026
90 checks passed
@release-clerk

release-clerk Bot commented Sep 16, 2026

Copy link
Copy Markdown

No Release Notes

@MarshallOfSound
MarshallOfSound deleted the refactor/webcontents-events-followup branch September 16, 2026 16:22
@trop

trop Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "45-x-y" cleanly;
you will need to perform this backport manually.

@trop trop Bot added needs-manual-bp/45-x-y and removed target/45-x-y PR should also be added to the "45-x-y" branch. labels Sep 16, 2026
MarshallOfSound added a commit that referenced this pull request Sep 16, 2026
…ly (#53981)

- 'render-process-gone' and 'login': the WebContents' own listeners run off
  the wrapper saved before the app emit, so they still run when an app
  listener destroys the WebContents (as the rest of an emit in progress
  did), and nothing of the native object is used after the app emit
- select-bluetooth-device: re-check the chooser and the WebContents after
  calling the app-replaceable listenerCount()
- drop the now-unused weak pointer in OnFirstNonEmptyLayout, make the
  isolate locals const, brace-initialise the callback scopes
- specs for the app-before-webContents order, the shared microtask drain,
  and the destroyed-by-app-listener case

(cherry picked from commit 4d10ef9)
MarshallOfSound added a commit that referenced this pull request Sep 16, 2026
* refactor: emit WebContents' forwarded events natively

WebContents' JS _init registered listeners on itself whose only job was to
re-emit with data C++ already had: '-console-message' -> 'console-message'
(plus the deprecated positional arguments), '-unresponsive' ->
'unresponsive' (when not ignored/hidden/uninitialised), 'login' and
'render-process-gone' -> app (and a debugging hint when logging is on),
'ready-to-show' -> the owner BrowserWindow, '-before-unload-fired' ->
preventDefault for non-interactive types, 'devtools-reload-page' ->
reload(), 'select-bluetooth-device' -> cancel when nobody listens, and
app 'web-contents-created' at the end of _init. Each is now done where the
event originates, in the order the JS listener (always the first one) gave:
app before the WebContents' own listeners for login/render-process-gone,
the window after the WebContents for ready-to-show, reload() before
'devtools-reload-page'. The 'console-message' deprecation warning is now
given when a listener with the old signature is added rather than on its
first message.

Co-authored-by: Sam Attard <[email protected]>

* fix: take the weak pointer before app listeners can destroy the WebContents

Co-authored-by: Sam Attard <[email protected]>

* fix: run paired emits under one callback scope so ticks and microtasks drain after both

Co-authored-by: Sam Attard <[email protected]>

* fix: read the login event's defaultPrevented after listeners' microtasks have run

Co-authored-by: Sam Attard <[email protected]>

* fix: notify the window that owned the WebContents before its ready-to-show listeners ran

Co-authored-by: Sam Attard <[email protected]>

* fix: hand app and webContents 'login' listeners the same authInfo object

Co-authored-by: Sam Attard <[email protected]>

* fix: count select-bluetooth-device listeners under the emit's callback scope

Co-authored-by: Sam Attard <[email protected]>

* refactor: follow-ups to emitting WebContents' forwarded events natively (#53981)

- 'render-process-gone' and 'login': the WebContents' own listeners run off
  the wrapper saved before the app emit, so they still run when an app
  listener destroys the WebContents (as the rest of an emit in progress
  did), and nothing of the native object is used after the app emit
- select-bluetooth-device: re-check the chooser and the WebContents after
  calling the app-replaceable listenerCount()
- drop the now-unused weak pointer in OnFirstNonEmptyLayout, make the
  isolate locals const, brace-initialise the callback scopes
- specs for the app-before-webContents order, the shared microtask drain,
  and the destroyed-by-app-listener case

(cherry picked from commit 4d10ef9)

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Sam Attard <[email protected]>
Co-authored-by: Samuel Attard <[email protected]>
@MarshallOfSound MarshallOfSound added merged/45-x-y PR was merged to the "45-x-y" branch. and removed needs-manual-bp/45-x-y labels Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

46-x-y merged/45-x-y PR was merged to the "45-x-y" branch. semver/none

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants