// Cotton Keys' side of its credential provider: when the drain runs (its// passkeys and its passwords, one pass: `PasskeyArrivals`).//// Only Cotton Keys (`flavour: keys`) carries a provider, so only it drains// the passkey mailbox (`CottonKit/PasskeyArrivals.swift`): once the file is// open, at every foreground, when the provider knocks (a Darwin notification// while Keys is running), and after each sync pass (a row made on another// device is mirrored into this phone's keychain and told to iOS). One pass// at a time; a pass asked while one runs runs once more after it.importCottonKitimportCottonMailboximportCottonValuesimportFoundation@MainActorfinalclassKeysPasskeys {/// Whether this app is the passkey app.staticvar applies: Bool { FlavourManifest.current?.flavour == "keys" }privatelet arrivals: PasskeyArrivalsprivatelet say: @MainActor (String) -> Voidprivatevar running = falseprivatevar again: Bool?init(core: any CottonCalling, say: @escaping @MainActor (String) -> Void) { arrivals = PasskeyArrivals(core: core)self.say = sayKnockListener.shared.listen { [weakself] inTask { @MainActor inself?.run(presence: true) } } }/// One pass; `presence` when the person is looking at the app, so a/// waiting registration may ask for Face ID once.func run(presence: Bool) {guard !running else { again = (again ?? false) || presencereturn } running = trueTask { @MainActor inlet pass = await arrivals.run(presence: presence)if presence, pass.waiting > 0 { say(ShellLines.line("passwords", pass.waiting == 1 ? "passkeys.waiting_one" : "passkeys.waiting_many", ["n": String(pass.waiting)])) }if presence, pass.passwordsWaiting > 0 {let key = pass.passwordsWaiting == 1 ? "passkeys.saved_waiting_one" : "passkeys.saved_waiting_many" say(ShellLines.line("passwords", key, ["n": String(pass.passwordsWaiting)])) } running = falseiflet presence = again { again = nil run(presence: presence) } } }}/// The provider's knock, heard while Keys runs.privatefinalclassKnockListener: @unchecked Sendable {staticlet shared = KnockListener()privatevar heard: (@Sendable () -> Void)?privatevar listening = falsefunc listen(_ heard: @escaping @Sendable () -> Void) {self.heard = heardguard !listening else { return } listening = truelet center = CFNotificationCenterGetDarwinNotifyCenter()let name = PasskeyChannel.knockName() as CFString CFNotificationCenterAddObserver(center, Unmanaged.passUnretained(self).toOpaque(), { _, observer, _, _, _ inguardlet observer else { return }Unmanaged<KnockListener>.fromOpaque(observer).takeUnretainedValue().heard?() }, name, nil, .deliverImmediately) }}