Repository navigation
refactor: extract WebContents::NativeLifecycle - #53755
Conversation
637b7d1 to
808ad28
Compare
MarshallOfSound
left a comment
There was a problem hiding this comment.
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.
808ad28 to
e4b239c
Compare
|
@claude review |
There was a problem hiding this comment.
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()…
2938205 to
b961796
Compare
b961796 to
dfdaa8c
Compare
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.
e6c74b0 to
2d8098a
Compare
|
No Release Notes |
Description of Change
Refs #47922
Move the native resources owned by
api::WebContentsand every native registration that routes callbacks back into it (WebContentsObserver, WebContentsDelegate, InputEventObserver, the InspectableWebContents delegates, BackgroundThrottlingSource and ExclusiveAccessContext) into a nestedNativeLifecyclehelper that forwards through a weak link.Isolating the native teardown from the wrapper is a prerequisite for managing
api::WebContentswith 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 aDeprecatedWrappablethat is pinned and deleted explicitly.Release Notes
Notes: none