// How CottonDB for Mac hands a web address — a Google sign-in above all — to // the person's browser. // // ## Why not `openURL` alone // // On the Mac that serves, the Instagram browser source keeps a HEADLESS Google // Chrome running on its own profile (`cotton-session-host`'s browser broker, // `--headless=new`). That Chrome still checks in with LaunchServices as an // ordinary foreground `com.google.Chrome`, and when two Chromes run, // LaunchServices hands a URL to whichever it lists first — on one Mac, on // 11 Oct 2026, every Google sign-in went to the invisible one, three times, and // the person saw nothing open at all. // // So when the default browser is Chrome, the address goes as the argument of a // NEW Chrome instance instead. Chrome's own process singleton then passes it to // the Chrome running on the person's default profile (or that new instance // becomes it, if none runs) and exits — the broker's Chrome, on another // `--user-data-dir`, never sees it. Any other default browser has no broker // beside it, and is opened the ordinary way. import Foundation #if os(macOS) import AppKit #endif /// Which way an address reaches the browser. public enum BrowserOpening: Equatable { /// The system's ordinary open (SwiftUI `openURL`, `NSWorkspace.open`). case ordinary /// A new instance of the browser at `app`, the address its argument. case newInstance(app: URL) /// The bundle the browser broker runs headless. public static let chromeBundleIdentifier = "com.google.Chrome" /// The way for an address whose default browser is `app` (`bundleIdentifier`). public static func plan(defaultBrowser app: URL?, bundleIdentifier: String?) -> BrowserOpening { guard let app, bundleIdentifier == chromeBundleIdentifier else { return .ordinary } return .newInstance(app: app) } #if os(macOS) /// Open `url` in the person's own browser; `fallback` is the ordinary open. @MainActor public static func open(_ url: URL, fallback: (URL, @escaping (Bool) -> Void) -> Void, accepted: @escaping (Bool) -> Void) { let app = NSWorkspace.shared.urlForApplication(toOpen: url) switch plan(defaultBrowser: app, bundleIdentifier: app.flatMap { Bundle(url: $0)?.bundleIdentifier }) { case .ordinary: fallback(url, accepted) case .newInstance(let app): let configuration = NSWorkspace.OpenConfiguration() configuration.createsNewApplicationInstance = true configuration.arguments = [url.absoluteString] Task { @MainActor in do { _ = try await NSWorkspace.shared.openApplication(at: app, configuration: configuration) accepted(true) } catch { accepted(false) } } } } #endif }