Repository navigation
Reapply media-playback hibernation fix (issue 5409) #5441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import Foundation | ||
| import WebKit | ||
|
|
||
| /// Receives `{ frameID, playing }` from the injected media-playback hook and | ||
| /// forwards it to the owning ``BrowserPanel`` on the main actor. | ||
| /// | ||
| /// Mirrors ``ReactGrabMessageHandler``: a thin `NSObject` adapter so the panel | ||
| /// itself never has to conform to `WKScriptMessageHandler`. | ||
| final class BrowserMediaPlaybackMessageHandler: NSObject, WKScriptMessageHandler { | ||
| private let onReport: @MainActor (BrowserMediaPlaybackReport) -> Void | ||
|
|
||
| init(onReport: @escaping @MainActor (BrowserMediaPlaybackReport) -> Void) { | ||
| self.onReport = onReport | ||
| } | ||
|
|
||
| func userContentController( | ||
| _ userContentController: WKUserContentController, | ||
| didReceive message: WKScriptMessage | ||
| ) { | ||
| guard let body = message.body as? [String: Any], | ||
| let frameID = body["frameID"] as? String, | ||
| let playing = body["playing"] as? Bool else { return } | ||
| let report = BrowserMediaPlaybackReport(frameID: frameID, isPlaying: playing) | ||
| // WebKit delivers script messages on the main thread. Apply the report | ||
| // synchronously instead of hopping through a `Task` so it lands in | ||
| // WebKit's delivery order relative to navigation callbacks: a report | ||
| // emitted by a document before it navigates away is applied before the | ||
| // matching `didCommit` reset, so a stale `playing: true` cannot re-add a | ||
| // dead frame id after the reset and pin the pane against discard. | ||
| MainActor.assumeIsolated { | ||
| onReport(report) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import Foundation | ||
|
|
||
| /// A per-frame media-playback report from the injected media-playback hook. | ||
| struct BrowserMediaPlaybackReport: Sendable { | ||
| /// Stable id for the reporting frame's document, so the native side can | ||
| /// aggregate playback across the main frame and any (cross-origin) iframes. | ||
| let frameID: String | ||
| /// Whether that frame currently has any actively-playing media. | ||
| let isPlaying: Bool | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| import Foundation | ||
| import WebKit | ||
|
|
||
| /// Name of the `WKScriptMessageHandler` the injected media-playback hook posts to. | ||
| private let mediaPlaybackMessageHandlerName = "cmuxMediaPlayback" | ||
|
|
||
| extension BrowserPanel { | ||
| /// Isolated content world for the media-playback hook. | ||
| /// | ||
| /// Both the injected script and the message handler live here, not the page | ||
| /// world, so arbitrary page JavaScript cannot reach | ||
| /// the private media-playback message handler and post a fake `{ playing: | ||
| /// true }` report that would pin the pane alive forever and defeat | ||
| /// hidden-webview discard. A content world shares the DOM, so the script's | ||
| /// media event listeners and `MutationObserver` still observe page playback. | ||
| static let mediaPlaybackContentWorld = WKContentWorld.world(name: mediaPlaybackMessageHandlerName) | ||
|
|
||
| /// Injected document-start hook that reports whether the current frame has | ||
| /// any actively-playing `<video>`/`<audio>` element. | ||
| /// | ||
| /// Runs in every frame (main frame and cross-origin iframes) so an embedded | ||
| /// player (a news site embedding a YouTube/Vimeo/Twitch iframe, etc.) keeps | ||
| /// its hidden pane alive too. Each frame tags its report with a stable | ||
| /// per-document id; the native side keeps a pane alive while any frame is | ||
| /// playing and releases it once every frame has stopped | ||
| /// (https://github.com/manaflow-ai/cmux/issues/5409). | ||
| /// | ||
| /// Reports only on change (debounced via `lastReported`) and on `pagehide`. | ||
| /// Uses `paused`/`ended`, so playback that the user has muted still counts as | ||
| /// "playing media" (matching Chrome's keep-alive). Uses only public DOM APIs, | ||
| /// so it is stable across WebKit/macOS versions, unlike the private | ||
| /// `_isPlayingAudio` KVO property (which would also drop user-muted video). | ||
| /// | ||
| /// The script is purely passive (capture-phase listeners only; no console, | ||
| /// prototype, or enumerable-global tampering) so it does not trip the | ||
| /// fingerprinting checks of CAPTCHA providers that live in cross-origin | ||
| /// iframes. `WKUserScript` injects it once per document, so no install guard | ||
| /// is needed. Known limitation: media produced via the Web Audio API with no | ||
| /// `<video>`/`<audio>` element (some web games) is not detected, since a | ||
| /// running `AudioContext` is not a reliable "audible" signal and would | ||
| /// over-retain idle panes. | ||
| static let mediaPlaybackTrackingBootstrapScriptSource = """ | ||
| (() => { | ||
| try { | ||
| const frameID = (() => { | ||
| try { | ||
| if (window.crypto && typeof window.crypto.randomUUID === "function") { | ||
| return window.crypto.randomUUID(); | ||
| } | ||
| } catch (_) {} | ||
| return Date.now().toString(36) + "-" + Math.random().toString(36).slice(2); | ||
| })(); | ||
|
|
||
| let lastReported = null; | ||
| let mediaObserver = null; | ||
|
|
||
| const isElementPlaying = (el) => { | ||
| try { | ||
| return !!el && !el.paused && !el.ended; | ||
| } catch (_) { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| const anyPlaying = () => { | ||
| try { | ||
| const media = document.querySelectorAll("video, audio"); | ||
| for (let i = 0; i < media.length; i++) { | ||
| if (isElementPlaying(media[i])) return true; | ||
| } | ||
| } catch (_) {} | ||
| return false; | ||
| }; | ||
|
|
||
| const post = (playing) => { | ||
| try { | ||
| window.webkit.messageHandlers["\(mediaPlaybackMessageHandlerName)"].postMessage({ | ||
| frameID: frameID, | ||
| playing: playing | ||
| }); | ||
| } catch (_) {} | ||
| }; | ||
|
|
||
| const disconnectObserver = () => { | ||
| if (!mediaObserver) return; | ||
| try { mediaObserver.disconnect(); } catch (_) {} | ||
| mediaObserver = null; | ||
| }; | ||
|
|
||
| const removedMedia = (node) => { | ||
| try { | ||
| if (!node || node.nodeType !== 1) return false; | ||
| if (node.matches && node.matches("video, audio")) return true; | ||
| return !!(node.querySelector && node.querySelector("video, audio")); | ||
| } catch (_) { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| // Removing a playing element from the DOM fires no media event, so while | ||
| // anything is playing watch for DOM mutations and recheck. This is the | ||
| // only signal that survives while the page is hidden (timers are | ||
| // throttled, MutationObserver is not), so a player that tears down its | ||
| // <video> still clears the blocker and lets the pane be discarded. | ||
| // | ||
| // Only recheck when a mutation actually removes a media element; added | ||
| // media already fires a `play` event. This avoids a full-document | ||
| // querySelectorAll on every DOM change of a mutation-heavy SPA (live | ||
| // chat, infinite scroll) while a video plays. | ||
| function syncObserver(playing) { | ||
| if (playing) { | ||
| if (mediaObserver) return; | ||
| try { | ||
| mediaObserver = new MutationObserver((records) => { | ||
| for (let i = 0; i < records.length; i++) { | ||
| const removed = records[i].removedNodes; | ||
| for (let j = 0; j < removed.length; j++) { | ||
| if (removedMedia(removed[j])) { | ||
| report(); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| mediaObserver.observe(document.documentElement || document, { | ||
| childList: true, | ||
| subtree: true | ||
| }); | ||
| } catch (_) {} | ||
| } else { | ||
| disconnectObserver(); | ||
| } | ||
| } | ||
|
|
||
| function report() { | ||
| const playing = anyPlaying(); | ||
| syncObserver(playing); | ||
| if (playing === lastReported) return; | ||
| lastReported = playing; | ||
| post(playing); | ||
| } | ||
|
|
||
| // Media events do not bubble, but capture-phase listeners on `document` | ||
| // still observe them as the event travels down to the target element. | ||
| const events = [ | ||
| "play", "playing", "pause", "ended", "emptied", | ||
| "waiting", "stalled", "suspend", "abort", "loadeddata" | ||
| ]; | ||
| for (let i = 0; i < events.length; i++) { | ||
| document.addEventListener(events[i], report, true); | ||
| } | ||
|
|
||
| window.addEventListener("pagehide", () => { | ||
| disconnectObserver(); | ||
| if (lastReported === false) return; | ||
| lastReported = false; | ||
| post(false); | ||
| }, true); | ||
|
|
||
| document.addEventListener("DOMContentLoaded", report, true); | ||
| report(); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } catch (_) {} | ||
| return true; | ||
| })(); | ||
| """ | ||
|
|
||
| /// Installs the media-playback message handler on `webView`. | ||
| /// | ||
| /// Each `BrowserPanel` webview is created with a fresh `WKWebViewConfiguration` | ||
| /// (`makeWebView`), so the handler name is never registered twice on one | ||
| /// content controller. Reset `isPlayingMedia` for the freshly bound webview. | ||
| func setupMediaPlaybackMessageHandler(for webView: WKWebView) { | ||
| resetMediaPlaybackTracking() | ||
| // Bind the handler to this webview generation. The handler stays alive on | ||
| // the old content controller until the old webview deallocates, so a late | ||
| // report from a replaced document must be ignored or it would repopulate | ||
| // playingMediaFrameIDs for a page that is gone and block discard forever. | ||
| let boundWebViewInstanceID = webViewInstanceID | ||
| let handler = BrowserMediaPlaybackMessageHandler { [weak self] report in | ||
| self?.handleMediaPlaybackReport(report, fromWebViewInstanceID: boundWebViewInstanceID) | ||
| } | ||
| mediaPlaybackMessageHandler = handler | ||
| webView.configuration.userContentController.add( | ||
| handler, | ||
| contentWorld: Self.mediaPlaybackContentWorld, | ||
| name: mediaPlaybackMessageHandlerName | ||
| ) | ||
| } | ||
|
|
||
| /// Applies a per-frame playback report from the injected hook, aggregating | ||
| /// across the main frame and any iframes. Reports from a superseded webview | ||
| /// generation are dropped. | ||
| func handleMediaPlaybackReport( | ||
| _ report: BrowserMediaPlaybackReport, | ||
| fromWebViewInstanceID instanceID: UUID | ||
| ) { | ||
| guard instanceID == webViewInstanceID else { return } | ||
| applyMediaPlaybackReport(frameID: report.frameID, isPlaying: report.isPlaying) | ||
| #if DEBUG | ||
| cmuxDebugLog( | ||
| "browser.media.playback panel=\(id.uuidString.prefix(5)) " + | ||
| "frame=\(report.frameID.prefix(5)) playing=\(report.isPlaying ? 1 : 0) " + | ||
| "anyPlaying=\(isPlayingMedia ? 1 : 0)" | ||
| ) | ||
| #endif | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.