Skip to content

armgit / cra

Public Imported from GitHub
0

The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.

Leaving a Google Meet call's tab opens its picture-in-picture window (#198)

Victordd8ae647224e
diff --git a/Sources/Cra/Services/BrowserManager.swift b/Sources/Cra/Services/BrowserManager.swift
index c987572..bc0ea26 100644
--- a/Sources/Cra/Services/BrowserManager.swift
+++ b/Sources/Cra/Services/BrowserManager.swift
@@ -15,6 +15,9 @@ final class TabRuntime {
/// auto Picture in Picture); coming back brings the video home.
var autoPictureInPicture = false
var pendingAutoPictureInPicture = false
+ /// When Cra last ran the page's own `enterpictureinpicture` handler on leaving the tab (a
+ /// Google Meet call opens its Document Picture in Picture window); coming back closes it.
+ var sessionPictureInPictureAt: Date?
/// The tab switcher's thumbnail, taken while the page was last on screen.
var snapshot: NSImage?
var snapshotWork: DispatchWorkItem?
@@ -667,10 +670,47 @@ final class BrowserManager: NSObject, @preconcurrency CraBrowserDelegate {
/// It goes through the protocol's user gesture like the player's button:
/// the key press that lends the page activation raced the page going
/// hidden, and PiP sometimes never opened.
- private func leaveTab(_ tabID: UUID) {
+ ///
+ /// A page that registered its own `enterpictureinpicture` media session handler is asked
+ /// first, the way Chrome's AutoPictureInPictureTabHelper asks it when such a tab is hidden.
+ /// That is how Google Meet's "Automatic picture-in-picture" works: its handler opens a
+ /// Document Picture in Picture window of the call, which no `<video>` request can (GitHub
+ /// #198). Chrome also wants the camera or microphone in use or sound playing, but CEF doesn't
+ /// report capture for Chrome-style pages (Chrome's own media UI takes the request), so the
+ /// handler, which a site registers to opt in, is the condition. The page is asked whether it
+ /// has one without a user gesture; only a page that does gets one, to run it. `video: false`
+ /// skips the video request (a silent page when the window goes out of sight).
+ private func leaveTab(_ tabID: UUID, video: Bool = true) {
guard store.settings.autoPictureInPicture, let rt = runtimes[tabID], !rt.browser.isInPictureInPicture else { return }
rt.pendingAutoPictureInPicture = true
- requestPictureInPicture(tabID, automatic: true)
+ if rt.browser is WebKitBrowser {
+ if video { requestPictureInPicture(tabID, automatic: true) }
+ return
+ }
+ let query = """
+ (() => {
+ if (window.documentPictureInPicture?.window || document.pictureInPictureElement) return 'open';
+ return window.__cra?.query().includes('enterpictureinpicture') ? 'handler' : 'none';
+ })()
+ """
+ rt.browser.evaluate(inIsolatedWorld: PageScript.world, params: Self.evaluateParams(query, userGesture: false)) { [weak self, weak rt] result, _ in
+ DispatchQueue.main.async {
+ // Back on the tab before the page answered: nothing to open.
+ guard let self, let rt, self.runtimes[tabID] === rt, rt.pendingAutoPictureInPicture else { return }
+ switch Self.stringResult(result) {
+ case "handler":
+ rt.pendingAutoPictureInPicture = false
+ rt.sessionPictureInPictureAt = self.clock.now
+ self.log(tabID, "asks the page for Picture in Picture")
+ let run = "window.__cra?.send('enterpictureinpicture')"
+ rt.browser.evaluate(inIsolatedWorld: PageScript.world, params: Self.evaluateParams(run, userGesture: true)) { _, _ in }
+ case "open":
+ rt.pendingAutoPictureInPicture = false
+ default:
+ if video { self.requestPictureInPicture(tabID, automatic: true) } else { rt.pendingAutoPictureInPicture = false }
+ }
+ }
+ }
}
/// A split pane that stands in for a pinned tab: the Today copy `createSplit` and
@@ -695,9 +735,10 @@ final class BrowserManager: NSObject, @preconcurrency CraBrowserDelegate {
/// Arc's presentPipForOccludedWindow: a window out of sight and not key leaves its video tabs
/// the way a tab switch does (a four-finger swipe to another desktop, another app on top), and
- /// the video comes back when the window is key again. Only pages playing sound are asked:
- /// the automatic request wants an unmuted video playing, and a page without one in its main
- /// frame gets the activation nudge, which an app switch shouldn't send to every page.
+ /// the video comes back when the window is key again. Only pages playing sound get the video
+ /// request: it wants an unmuted video playing, and a page without one in its main frame gets
+ /// the activation nudge, which an app switch shouldn't send to every page. Every page is asked,
+ /// without a gesture, for its own `enterpictureinpicture` handler (a Meet call, #198).
func windowVisibilityChanged(covered: Bool) {
guard covered != windowCovered else { return }
windowCovered = covered
@@ -707,8 +748,9 @@ final class BrowserManager: NSObject, @preconcurrency CraBrowserDelegate {
// A WebKit page's own fullscreen window covers this one: that is the video on
// screen, not out of sight. Asked then, WebKit's Picture in Picture got stuck in its
// transition and never opened for that video again.
- if rt.browser.isAudible, !PageEngines.isInOwnFullscreen(rt.browser) { leaveTab(id) }
- } else if rt.autoPictureInPicture || rt.pendingAutoPictureInPicture {
+ // A silent page is asked only for its own handler (a call: GitHub #198).
+ if !PageEngines.isInOwnFullscreen(rt.browser) { leaveTab(id, video: rt.browser.isAudible) }
+ } else if rt.autoPictureInPicture || rt.pendingAutoPictureInPicture || rt.sessionPictureInPictureAt != nil {
returnToTab(rt)
}
}
@@ -734,6 +776,28 @@ final class BrowserManager: NSObject, @preconcurrency CraBrowserDelegate {
private func returnToTab(_ rt: TabRuntime) {
rt.pendingAutoPictureInPicture = false
if rt.browser.isInPictureInPicture { rt.browser.exitPictureInPicture() }
+ if let asked = rt.sessionPictureInPictureAt {
+ rt.sessionPictureInPictureAt = nil
+ closeSessionPictureInPicture(rt, waiting: clock.now.timeIntervalSince(asked) < 2)
+ }
+ }
+
+ /// Chrome closes an automatic Picture in Picture window when its tab is in front again, and
+ /// the page takes its content back as the window goes (Meet's call returns to the tab). A
+ /// window the page is still opening (the user came straight back) is closed as it opens.
+ private func closeSessionPictureInPicture(_ rt: TabRuntime, waiting: Bool) {
+ let js = """
+ ((wait) => {
+ const d = window.documentPictureInPicture;
+ if (!d) return;
+ if (d.window) return d.window.close();
+ if (!wait) return;
+ const opened = e => e.window.close();
+ d.addEventListener('enter', opened, { once: true });
+ setTimeout(() => d.removeEventListener('enter', opened), 2000);
+ })(\(waiting))
+ """
+ rt.browser.sendDevToolsMethod("Runtime.evaluate", params: Self.evaluateParams(js, userGesture: false)) { _, _ in }
}
func browser(_ browser: CraBrowser, didChangePictureInPicture active: Bool) {
diff --git a/Sources/Cra/Services/PageScript.swift b/Sources/Cra/Services/PageScript.swift
index 258bba2..b1455b1 100644
--- a/Sources/Cra/Services/PageScript.swift
+++ b/Sources/Cra/Services/PageScript.swift
@@ -11,7 +11,8 @@ import Foundation
/// - `send(action)` / `handlers`: the page's media session handlers (YouTube, Spotify…), so the
/// player can skip tracks. `setActionHandler` can't be called from outside and its handlers
/// live in the page's world, so a small wrapper there keeps them in a closure and answers an
-/// event whose name is random per launch: nothing in the page's world names Cra.
+/// event whose name is random per launch: nothing in the page's world names Cra. Leaving a
+/// call's tab sends `enterpictureinpicture` with Chrome's details (Google Meet, GitHub #198).
/// - `drm`: the page asked for Widevine (navigator.requestMediaKeySystemAccess): the Site Control
/// Center then offers to play the site in Safari's engine (docs/protected-media.md). A wrapper in
/// the page's world tells this world through the same per-launch event name.
@@ -36,7 +37,9 @@ enum PageScript {
} }.setActionHandler });
addEventListener('\(channel)', e => {
if (e.detail === '?') return dispatchEvent(new CustomEvent('\(channel)r', { detail: Object.keys(handlers).join(',') }));
- try { handlers[e.detail]?.({ action: e.detail }); } catch {}
+ const details = e.detail === 'enterpictureinpicture'
+ ? { action: e.detail, enterPictureInPictureReason: 'contentoccluded' } : { action: e.detail };
+ try { handlers[e.detail]?.(details); } catch {}
});
})();
(() => {
diff --git a/Sources/CraCEF/CraCEF.mm b/Sources/CraCEF/CraCEF.mm
index 735a511..e99904e 100644
--- a/Sources/CraCEF/CraCEF.mm
+++ b/Sources/CraCEF/CraCEF.mm
@@ -1229,6 +1229,16 @@ public:
CEF_REQUIRE_UI_THREAD();
CraBrowser* o = owner_;
if (!o || !o.delegate) return true;
+ // Document Picture in Picture (documentPictureInPicture.requestWindow: Google Meet's call
+ // window): CEF always makes Chrome's own Picture in Picture window for it. It is not a
+ // tab, so it gets a client with no CraBrowser rather than a new tab to host it, which
+ // showed up in the sidebar, was selected and took the window off the tab the user picked
+ // (GitHub #198). The page that opened it keeps it, and Chrome closes it with that page.
+ if (target_disposition == CEF_WOD_NEW_PICTURE_IN_PICTURE) {
+ client = new CraClient(nil);
+ CraPageSettings(settings);
+ return false;
+ }
// A target=_blank link the user clicked (Shift-clicked: a new window) may open in a
// Peek; window.open with features (a sign-in popup) is CEF_WOD_NEW_POPUP and stays one.
if (user_gesture && (target_disposition == CEF_WOD_NEW_FOREGROUND_TAB || target_disposition == CEF_WOD_NEW_WINDOW)
diff --git a/Tests/BrowserManagerLifecycleTests.swift b/Tests/BrowserManagerLifecycleTests.swift
index 4265a76..4875054 100644
--- a/Tests/BrowserManagerLifecycleTests.swift
+++ b/Tests/BrowserManagerLifecycleTests.swift
@@ -98,6 +98,7 @@ enum BrowserManagerLifecycleTests {
await leaveVideoTabPictureInPictureCase()
await splitCopySleepsPinnedTabCase()
await coveredWindowPictureInPictureCase()
+ await callPictureInPictureCase()
pageColoursCase()
extensionPageRetryCase()
await sleepCase("malformed or missing replies cannot authorize sleeping", reply: nil) { _, _ in }
@@ -686,6 +687,9 @@ enum BrowserManagerLifecycleTests {
(nil, 1, "a page whose answer was lost")] as [(String?, Int, String)] {
let before = ra.browser.automaticPictureInPictureRequests
manager.show(b, focus: false)
+ expect(ra.browser.lastIsolatedParams.contains(#""userGesture":false"#), "the page is first asked for its own handler, without a gesture")
+ ra.browser.reply(to: "cra:Runtime.evaluate", with: #"{"result":{"type":"string","value":"none"}}"#)
+ await drainMainQueue()
expect(ra.browser.lastParams.contains(#""userGesture":true"#), "leaving a video tab asks as a user gesture")
expect(ra.browser.lastParams.contains("})(true)"), "leaving a video tab takes only an audible, playing video")
let reply = answer.map { #"{"result":{"type":"string","value":"\#($0)"}}"# }
@@ -697,7 +701,7 @@ enum BrowserManagerLifecycleTests {
}
store.settings.autoPictureInPicture = false
manager.show(b, focus: false)
- expect(!ra.browser.replies.contains { $0.method == "Runtime.evaluate" }, "with the setting off, leaving the tab asks nothing")
+ expect(!ra.browser.replies.contains { $0.method.hasSuffix("Runtime.evaluate") }, "with the setting off, leaving the tab asks nothing")
manager.closeAll()
}
@@ -733,11 +737,85 @@ enum BrowserManagerLifecycleTests {
let rv = manager.ensureRuntime(for: video)!
manager.show(video, focus: false)
manager.show(copy.id, focus: false)
- expect(rv.browser.replies.contains { $0.method == "Runtime.evaluate" }, "leaving another video tab still asks for Picture in Picture")
+ expect(rv.browser.replies.contains { $0.method.hasSuffix("Runtime.evaluate") }, "leaving another video tab still asks for Picture in Picture")
expect(!rv.browser.replies.contains { $0.method == "Cra.discard" }, "and doesn't put it to sleep")
manager.closeAll()
}
+ /// A call (Google Meet with "Automatic picture-in-picture" on) registers its own
+ /// `enterpictureinpicture` media session handler, which opens a Document Picture in Picture
+ /// window of the call. Leaving its tab, or its window going out of sight, runs that handler as a
+ /// user gesture, as Chrome does; coming back closes the window (GitHub #198). The page is asked
+ /// whether it has the handler without a gesture, so a page without one never gets activation
+ /// from an app switch, and a page without one gets the video request as before.
+ @MainActor
+ private static func callPictureInPictureCase() async {
+ let clock = ManualScheduler()
+ let store = SidebarStore()
+ let manager = BrowserManager(store: store, favicons: FaviconCache(), container: NSView(), clock: clock)
+ let a = store.createTab(url: "https://meet.example.test/abc").id, b = store.createTab(url: "https://example.test/other").id
+ let ra = manager.ensureRuntime(for: a)!
+ store.settings.autoPictureInPicture = true
+ manager.show(a, focus: false)
+ let answer = { (value: String) in #"{"result":{"type":"string","value":"\#(value)"}}"# }
+ let evaluations = { ra.browser.replies.filter { $0.method == "Runtime.evaluate" }.count }
+ let isolated = { ra.browser.replies.filter { $0.method == "cra:Runtime.evaluate" }.count }
+
+ manager.show(b, focus: false)
+ expect(ra.browser.lastIsolatedParams.contains("enterpictureinpicture") && ra.browser.lastIsolatedParams.contains(#""userGesture":false"#),
+ "leaving the tab asks the page for its own handler, without a gesture")
+ ra.browser.reply(to: "cra:Runtime.evaluate", with: answer("handler"))
+ await drainMainQueue()
+ expect(isolated() == 1 && ra.browser.lastIsolatedParams.contains("send('enterpictureinpicture')")
+ && ra.browser.lastIsolatedParams.contains(#""userGesture":true"#), "a page with the handler has it run, as a user gesture")
+ ra.browser.reply(to: "cra:Runtime.evaluate", with: nil)
+ expect(evaluations() == 0 && ra.browser.automaticPictureInPictureRequests == 0, "and gets no video request too")
+ clock.advance(by: 30)
+ manager.show(a, focus: false)
+ expect(evaluations() == 1 && ra.browser.lastParams.contains("documentPictureInPicture") && ra.browser.lastParams.contains("})(false)"),
+ "back on the call's tab, its window closes")
+ ra.browser.reply(to: "Runtime.evaluate", with: nil)
+ manager.show(b, focus: false)
+ manager.show(a, focus: false)
+ expect(evaluations() == 0, "coming back again closes nothing: the window it opened is gone")
+ ra.browser.reply(to: "cra:Runtime.evaluate", with: answer("handler"))
+ await drainMainQueue()
+ expect(isolated() == 0, "back before the page answered: the handler isn't run")
+
+ // Back right after the handler ran: a window still opening is closed as it opens.
+ manager.show(b, focus: false)
+ ra.browser.reply(to: "cra:Runtime.evaluate", with: answer("handler"))
+ await drainMainQueue()
+ ra.browser.reply(to: "cra:Runtime.evaluate", with: nil)
+ manager.show(a, focus: false)
+ expect(evaluations() == 1 && ra.browser.lastParams.contains("})(true)"), "a window still opening is closed as it opens")
+ ra.browser.reply(to: "Runtime.evaluate", with: nil)
+
+ // A window the user opened from the call stays when they come back.
+ manager.show(b, focus: false)
+ ra.browser.reply(to: "cra:Runtime.evaluate", with: answer("open"))
+ await drainMainQueue()
+ manager.show(a, focus: false)
+ expect(evaluations() == 0 && isolated() == 0 && ra.browser.automaticPictureInPictureRequests == 0, "a Picture in Picture already open is left alone")
+
+ // The window going out of sight: a silent page runs its handler, but never gets the video
+ // request, which would send it the activation key.
+ manager.windowVisibilityChanged(covered: true)
+ ra.browser.reply(to: "cra:Runtime.evaluate", with: answer("none"))
+ await drainMainQueue()
+ expect(evaluations() == 0, "a silent page without the handler gets no video request on an app switch")
+ manager.windowVisibilityChanged(covered: false)
+ manager.windowVisibilityChanged(covered: true)
+ ra.browser.reply(to: "cra:Runtime.evaluate", with: answer("handler"))
+ await drainMainQueue()
+ expect(ra.browser.lastIsolatedParams.contains(#""userGesture":true"#), "a call whose window goes out of sight runs its handler")
+ ra.browser.reply(to: "cra:Runtime.evaluate", with: nil)
+ manager.windowVisibilityChanged(covered: false)
+ expect(evaluations() == 1 && ra.browser.lastParams.contains("documentPictureInPicture"), "the window back in sight closes the call's window")
+ ra.browser.reply(to: "Runtime.evaluate", with: nil)
+ manager.closeAll()
+ }
+
/// A window out of sight and not key (another app or desktop in front) leaves its video tab
/// like a tab switch, as Arc's presentPipForOccludedWindow does, and the video comes back when
/// the window is key again (X report, 1 Nov 2026). Silent pages aren't asked, and a Picture in
@@ -752,11 +830,15 @@ enum BrowserManagerLifecycleTests {
manager.show(a, focus: false)
manager.windowVisibilityChanged(covered: true)
- expect(!ra.browser.replies.contains { $0.method == "Runtime.evaluate" }, "a covered window doesn't ask a silent page")
+ ra.browser.reply(to: "cra:Runtime.evaluate", with: #"{"result":{"type":"string","value":"none"}}"#)
+ await drainMainQueue()
+ expect(!ra.browser.replies.contains { $0.method == "Runtime.evaluate" }, "a covered window doesn't send a silent page the video request")
manager.windowVisibilityChanged(covered: false)
ra.browser.isAudible = true
manager.windowVisibilityChanged(covered: true)
+ ra.browser.reply(to: "cra:Runtime.evaluate", with: #"{"result":{"type":"string","value":"none"}}"#)
+ await drainMainQueue()
expect(ra.browser.lastParams.contains(#""userGesture":true"#) && ra.browser.lastParams.contains("})(true)"),
"a covered window asks its playing page for automatic Picture in Picture")
ra.browser.reply(to: "Runtime.evaluate", with: #"{"result":{"type":"string","value":"ok"}}"#)
@@ -771,7 +853,7 @@ enum BrowserManagerLifecycleTests {
ra.browser.isInPictureInPicture = true
manager.browser(ra.browser, didChangePictureInPicture: true)
manager.windowVisibilityChanged(covered: true)
- expect(!ra.browser.replies.contains { $0.method == "Runtime.evaluate" }, "a page already in Picture in Picture isn't asked again")
+ expect(!ra.browser.replies.contains { $0.method.hasSuffix("Runtime.evaluate") }, "a page already in Picture in Picture isn't asked again")
manager.windowVisibilityChanged(covered: false)
expect(ra.browser.isInPictureInPicture, "Picture in Picture the user opened stays when the window shows again")
PictureInPictureChrome.shared.actions?.close()
diff --git a/Tests/BrowserManagerSupport/CraCEF.swift b/Tests/BrowserManagerSupport/CraCEF.swift
index 5d627a4..066fb35 100644
--- a/Tests/BrowserManagerSupport/CraCEF.swift
+++ b/Tests/BrowserManagerSupport/CraCEF.swift
@@ -72,8 +72,10 @@ open class CraBrowser {
lastParams = params
replies.append((method, completion))
}
+ public private(set) var lastIsolatedParams = ""
/// Recorded as "<world>:Runtime.evaluate".
public func evaluate(inIsolatedWorld world: String, params: String, completion: @escaping (String?, String?) -> Void) {
+ lastIsolatedParams = params
replies.append(("\(world):Runtime.evaluate", completion))
}
public func reply(to method: String, with result: String?) {