Skip to content

refactor: extract WebContents::NativeLifecycle - #53755

Merged
deepak1556 merged 7 commits into
mainfrom
robo/cppgc_web_contents_lifecycle
Sep 21, 2026
Merged

deepak1556 merged 7 commits into
mainfrom
robo/cppgc_web_contents_lifecycle

Conversation

@deepak1556

Copy link
Copy Markdown
Member

Description of Change

Refs #47922

Move the native resources owned by api::WebContents and every native registration that routes callbacks back into it (WebContentsObserver, WebContentsDelegate, InputEventObserver, the InspectableWebContents delegates, BackgroundThrottlingSource and ExclusiveAccessContext) into a nested NativeLifecycle helper that forwards through a weak link.

Isolating the native teardown from the wrapper is a prerequisite for managing api::WebContents with cppgc, where the wrapper is reclaimed by the collector instead of being deleted at a well defined point. No behavior change, the wrapper is still a DeprecatedWrappable that is pinned and deleted explicitly.

Release Notes

Notes: none

@deepak1556
deepak1556 added this pull request to stack #53756 September 9, 2026 04:33
@deepak1556 deepak1556 added semver/patch backwards-compatible bug fixes no-backport labels Sep 9, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Sep 9, 2026
@deepak1556
deepak1556 force-pushed the robo/cppgc_web_contents_lifecycle branch from 637b7d1 to 808ad28 Compare September 9, 2026 11:24
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Sep 10, 2026

@MarshallOfSound MarshallOfSound left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One real problem inline: destroyed now fires while external GetWeakPtr() handles are still valid, so calling win.setBackgroundColor() from a destroyed listener crashes the main process. Reproduces on this branch and is still there at the top of the stack.

@deepak1556
deepak1556 force-pushed the robo/cppgc_web_contents_lifecycle branch from 808ad28 to e4b239c Compare September 10, 2026 13:43
@deepak1556
deepak1556 marked this pull request as ready for review September 10, 2026 14:38
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Sep 10, 2026
@MarshallOfSound

Copy link
Copy Markdown
Member

@claude review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beyond the inline finding, I also checked two related teardown/ordering questions this run: whether AttachToIframe's new gating of externally_owned_ on native_lifecycle_->guest_delegate_ being set could let a webview cloned via webContents.clone() skip the externally-owned marking it needs — ruled out; and whether DetachCallbacks()'s new unconditional RemoveUserData(kElectronApiWebContentsKey) call, now also reached via DetachNativeCallbacks() at the start of ~WebContents(), could double-remove or race with the base class's own cleanup — ruled out.

Extended reasoning...

This run's inline comments already surface a confirmed use-after-free-adjacent ordering bug in NativeLifecycle::DisposeNative() for externally-owned WebContents (OOPIF guests / extension background pages), where Observe(nullptr) runs before ~WebContents()'s explicit WebContentsDestroyed() call, leaving web_contents() null when WebFrameMain teardown expects it. That alone, plus an outstanding MarshallOfSound review thread whose inline comments were only resolved by the PR author (not independently verified as addressed) rather than by commits, means a human still needs to look at this PR — self-resolution of another reviewer's objections on one's own PR is not evidence they were fixed. I additionally spent time on two adjacent scenarios not covered by the inline findings — the AttachToIframe/webContents.clone() externally-owned marking gate, and the new unconditional RemoveUserData call now reachable during ~WebContents() via DetachNativeCallbacks() — and found both to check out, which is worth recording so the human reviewer doesn't need to re-tread that ground.

Additional findings (outside the current diff — GitHub can't attach inline comments there):

  • 🔴 shell/browser/api/electron_api_web_contents.cc — For externally-owned WebContents (attached OOPIF guest views, extension background pages), NativeLifecycle::DisposeNative() now calls Observe(nullptr) before ~WebContents() makes its explicit WebContentsDestroyed() call, so web_contents() is already null there and WebFrameMain::DestroyAllForWebContents(web_contents()) is silently skipped -- a regression from the base branch where Observe(nullptr) ran at the very end of WebContentsDestroyed(), after the DestroyAllForWebContents call. Fix: run the WebFrameMain disposal before Observe(nullptr) is invoked for every teardown path (capture the content::WebContents pointer before DisposeNative()/Observe(nullptr) run, or move the DestroyAllForWebContents call into DisposeNative() itself before it stops observing).

    Extended reasoning...

    Path: ~WebContents() -> DetachNativeCallbacks() -> Emit("will-destroy") -> BeginNativeTeardown() -> native_lifecycle_->DisposeNative() -> WebContentsDestroyed() -> native_lifecycle_.reset(). Inside DisposeNative(), for externally_owned_==true (set in AttachToIframe for OOPIF guests, and in InitWithExtensionView for background pages), inspectable_web_contents_->ReleaseWebContents() is called so inspectable_web_contents_.reset() does NOT destroy the underlying content::WebContents (no automatic WebContentsObserver::WebContentsDestroyed callback fires); DisposeNative() then calls Observe(nullptr) directly. Control returns to ~WebContents(), which calls WebContents::WebContentsDestroyed() explicitly; its if (web_contents()) WebFrameMain::DestroyAllForWebContents(web_contents()); now evaluates web_contents() as null (native_lifecycle_->web_contents() returns null since Observe(nullptr) already ran), so the call is skipped. On base, WebContentsDestroyed()'s own Observe(nullptr) happened at the end of that same function, after this check, so web_contents() was still valid and…

    Verification: normal (regression introduced by this change). For externally-owned WebContents (attached OOPIF guests set at electron_api_web_contents.cc:4890, extension background pages at :1823), the new teardown order in ~WebContents (:1898-1901) runs native_lifecycle_->DisposeNative() before WebContents::WebContentsDestroyed(). DisposeNative() (:1418-1440) does, for externally_owned_: ReleaseWebContents()…

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Sep 11, 2026
@deepak1556
deepak1556 force-pushed the robo/cppgc_web_contents_lifecycle branch from 2938205 to b961796 Compare September 18, 2026 07:58
@deepak1556
deepak1556 requested review from a team as code owners September 18, 2026 07:58
@deepak1556
deepak1556 removed this pull request from stack #53756 September 18, 2026 07:59
@deepak1556
deepak1556 changed the base branch from robo/refactor_cppgc_callback_holder to main September 18, 2026 08:00
@deepak1556
deepak1556 removed request for a team September 18, 2026 08:03
@deepak1556
deepak1556 added this pull request to stack #54079 September 18, 2026 08:03
@deepak1556
deepak1556 marked this pull request as draft September 18, 2026 12:29
@deepak1556
deepak1556 marked this pull request as ready for review September 18, 2026 22:18
@ckerr
ckerr force-pushed the robo/cppgc_web_contents_lifecycle branch from b961796 to dfdaa8c Compare September 18, 2026 23:01
Move the native resources owned by api::WebContents and every native
registration that routes callbacks back into it (WebContentsObserver,
WebContentsDelegate, InputEventObserver, the InspectableWebContents
delegates, BackgroundThrottlingSource and ExclusiveAccessContext) into a
nested NativeLifecycle helper that forwards through a weak link.

Isolating the native teardown from the wrapper is a prerequisite for
managing api::WebContents with cppgc, where the wrapper is reclaimed by
the collector instead of being deleted at a well defined point. No
behavior change, the wrapper is still a DeprecatedWrappable that is
pinned and deleted explicitly.
Invalidate externally exposed WebContents weak pointers before disposing its
native resources, preventing BrowserWindow and WebContentsView callbacks from
accessing a partially destroyed WebContents.

Keep NativeLifecycle teardown notifications working through its synchronous
owner backlink, and track teardown with an explicit lifecycle state so
destruction remains idempotent.
Remove the obsolete WebContents attached state and only transfer native
ownership when a guest delegate is available to perform the iframe attachment.
Move frame wrapper cleanup into WebContents::NativeLifecycle so it runs while
the native WebContents pointer is still available.

Handle native destruction through the observer callback and externally owned
teardown before releasing the native contents, avoiding duplicate cleanup.
@deepak1556
deepak1556 force-pushed the robo/cppgc_web_contents_lifecycle branch from e6c74b0 to 2d8098a Compare September 20, 2026 17:27
@deepak1556
deepak1556 merged commit 5a1acec into main Sep 21, 2026
71 checks passed
@deepak1556
deepak1556 deleted the robo/cppgc_web_contents_lifecycle branch September 21, 2026 09:59
@release-clerk

release-clerk Bot commented Sep 21, 2026

Copy link
Copy Markdown

No Release Notes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

46-x-y no-backport semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants