// The doors to the person's own stores on this device — their address book // today — and the rule that only the APP opens one. // // **Why the doors are installed, not built.** On a Mac, `swift test` runs the // package's tests in a process that is signed in to the owner's iCloud: a test // that built a `CNContactStore` reached the owner's real address book over XPC // (run 5653, 11 Oct 2026 — every write was refused and the run hung, but it // was one refusal away from writing invented cards into his contacts). So no // package source builds a store. The live implementations live in the phone // app target (`App/Cotton`), which installs them at launch; the Mac app reads // no address book and installs none. A test // bundle links the package and never the app, so it cannot name them — the // compile fails — and in a test process every door here is shut. // `LiveStoresSweepTests` holds both halves: no package or test source names a // live store (Contacts, Photos, Calendar, Mail), and nothing but an app // installs one. // // The CAMERA ROLL is the second door, write-only: the Camera tool saves a shot // there too while the person leaves that on. Calendar and Mail have no bridge // on this side today; the sweep is what makes the first one come through here. import Foundation import os /// How much of each card a read asks for. public enum AddressCardDepth: Sendable { /// Names and phone numbers (Comms' "Find people I know"). case namesAndNumbers /// Everything People brings in: names, the platform's composed name, work, numbers, mail, cities, birthday. case whole } /// One card of the address book, as values. The package names no Contacts type: only the app's own book does /// (`App/Cotton/AddressBook+Contacts.swift`), so an app built without it carries no reference to the Contacts API /// and App Store processing asks it for no purpose string (ITMS-90683, 22 Nov 2026). public struct AddressCard: Sendable, Equatable { public var id: String /// The platform's own composed full name (prefix, middle name, suffix, the locale's order), when it has one. public var composedName: String? public var givenName: String public var familyName: String public var organization: String public var jobTitle: String public var phones: [String] public var emails: [String] public var cities: [String] public var birthday: DateComponents? public init( id: String, composedName: String? = nil, givenName: String = "", familyName: String = "", organization: String = "", jobTitle: String = "", phones: [String] = [], emails: [String] = [], cities: [String] = [], birthday: DateComponents? = nil ) { self.id = id self.composedName = composedName self.givenName = givenName self.familyName = familyName self.organization = organization self.jobTitle = jobTitle self.phones = phones self.emails = emails self.cities = cities self.birthday = birthday } } /// A device's address book, as the app opens it. Every call may reach the /// Contacts daemon, so callers make them off the main actor. public protocol AddressBook: Sendable { /// What the OS says about contacts now; `limited` is a yes. func access() -> CapabilityAccess /// The one prompt; true when the person said yes. func askAccess() async -> Bool /// Every card, one at a time, by given name. func enumerate(_ depth: AddressCardDepth, _ each: (AddressCard) -> Void) throws /// One card's picture, the full image before the thumbnail. func picture(_ identifier: String) throws -> Data? } /// A device's camera roll, as the app opens it: a shot's file added, never a /// read. The call reaches the Photos daemon, so it is made off the main actor. public protocol CameraRoll: Sendable { func save(file: URL, kind: CaptureMediaKind) async throws } /// Why a store could not be reached: this process is not the app. public struct LiveStoreUnreachable: Error, LocalizedError, Equatable { public let store: String public init(store: String) { self.store = store } public var errorDescription: String? { "This process has no \(store): only the app opens one" } } /// The stores this process may reach — none until the app installs them. public enum LiveStores { private static let held = OSAllocatedUnfairLock<(any AddressBook)?>(initialState: nil) /// The app's address book, or nil — always nil in a test process. public static var addressBook: (any AddressBook)? { held.withLock { $0 } } /// Whether the app has installed its stores. A permission prompt for one of /// them is raised only once it has: a test process is never asked about /// the person's contacts or photos. public static var installed: Bool { addressBook != nil } /// The address book, or the reason there is none. public static func reachAddressBook() throws -> any AddressBook { guard let book = addressBook else { throw LiveStoreUnreachable(store: "address book") } return book } /// Called by the app at launch, and by nothing else. public static func install(addressBook: any AddressBook) { held.withLock { $0 = addressBook } } private static let roll = OSAllocatedUnfairLock<(any CameraRoll)?>(initialState: nil) /// The camera roll the app installed; nil in every other process. public static var cameraRoll: (any CameraRoll)? { roll.withLock { $0 } } public static func install(cameraRoll: any CameraRoll) { roll.withLock { $0 = cameraRoll } } } /// The OS's answer for a capability whose API only an app links: the camera and the photo library. An app whose /// plist does not say why it would use one is built without its code (`COTTON_NO_CAMERA`, `COTTON_NO_PHOTOS`), so App /// Store processing finds no reference to ask a purpose string for (ITMS-90683, 22 Nov 2026), and here it is /// `unavailable`. public protocol AccessAsking: Sendable { func status() async -> CapabilityAccess func ask() async -> CapabilityAccess } extension LiveStores { private static let asking = OSAllocatedUnfairLock<[DeviceCapability: any AccessAsking]>(initialState: [:]) /// What the app installed for this capability; nil when it was built without it, and in every test process. public static func access(for capability: DeviceCapability) -> (any AccessAsking)? { asking.withLock { $0[capability] } } public static func install(access: any AccessAsking, for capability: DeviceCapability) { asking.withLock { $0[capability] = access } } }