Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / apple / Passwords / PasswordsXPCService.swift
PasswordsXPCService.swift134 lines · 6.0 KiBRaw
| 1 | // EverySync Passwords — XPC envelope service. |
| 2 | // |
| 3 | // The autofill extension cannot assume direct store access, so the containing |
| 4 | // Passwords app exposes this XPC service with EXACTLY the three IPC ops from |
| 5 | // `../IPC_CONTRACT.md` (which map 1:1 to KMP `SyncSink`). The service does |
| 6 | // `NSFileCoordinator` internally; callers just call the three methods. |
| 7 | // |
| 8 | // Service name: `com.vayunmathur.everysync.envelope-ipc` (must match the |
| 9 | // extension's `NSXPCConnection` + the service's `Info.plist`). |
| 10 | // The extension NEVER touches the relay — only these three ops. |
| 11 | // |
| 12 | // MAC-CHECK (needs xcodebuild on a Mac): |
| 13 | // - XPC service target with the App-Group entitlement |
| 14 | // (`group.<team>.everysync.shared`). |
| 15 | // - `NSXPCInterface` signatures below match on both sides (exported object + |
| 16 | // remote object). `@objc` + `NSSecureCoding`-compatible row type required |
| 17 | // at the boundary — hence `XPCEnvelopeRow` (plain data, no logic). |
| 18 | |
| 19 | import Foundation |
| 20 | |
| 21 | /// XPC-safe envelope row. Same five columns as `EnvelopeRow` / KMP `SyncSink`: |
| 22 | /// `syncId / updatedAt / deleted / payloadJson / contentHash`. Opaque payload |
| 23 | /// — the extension forwards it to the XCFramework; it never parses it. |
| 24 | @objc(XPCEnvelopeRow) |
| 25 | public final class XPCEnvelopeRow: NSObject, NSSecureCoding { |
| 26 | public static var supportsSecureCoding: Bool { true } |
| 27 | |
| 28 | public let syncId: String |
| 29 | public let updatedAt: Int64 |
| 30 | public let deleted: Bool |
| 31 | public let payloadJson: String |
| 32 | public let contentHash: String |
| 33 | |
| 34 | public init(syncId: String, updatedAt: Int64, deleted: Bool, payloadJson: String, contentHash: String) { |
| 35 | self.syncId = syncId |
| 36 | self.updatedAt = updatedAt |
| 37 | self.deleted = deleted |
| 38 | self.payloadJson = payloadJson |
| 39 | self.contentHash = contentHash |
| 40 | } |
| 41 | |
| 42 | public func encode(with coder: NSCoder) { |
| 43 | coder.encode(syncId, forKey: "syncId") |
| 44 | coder.encode(updatedAt, forKey: "updatedAt") |
| 45 | coder.encode(deleted, forKey: "deleted") |
| 46 | coder.encode(payloadJson, forKey: "payloadJson") |
| 47 | coder.encode(contentHash, forKey: "contentHash") |
| 48 | } |
| 49 | |
| 50 | public required init?(coder: NSCoder) { |
| 51 | guard let syncId = coder.decodeObject(of: NSString.self, forKey: "syncId") as String?, |
| 52 | let payloadJson = coder.decodeObject(of: NSString.self, forKey: "payloadJson") as String?, |
| 53 | let contentHash = coder.decodeObject(of: NSString.self, forKey: "contentHash") as String? else { |
| 54 | return nil |
| 55 | } |
| 56 | self.syncId = syncId |
| 57 | self.updatedAt = coder.decodeInt64(forKey: "updatedAt") |
| 58 | self.deleted = coder.decodeBool(forKey: "deleted") |
| 59 | self.payloadJson = payloadJson |
| 60 | self.contentHash = contentHash |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /// The three IPC ops. Table names cross the boundary as strings |
| 65 | /// (`passwords` / `notes` / `todos` / `contacts` / `calendars`); the service |
| 66 | /// validates against the `EnvelopeTable` allowlist before touching SQLite. |
| 67 | @objc(EnvelopeIPCServiceProtocol) |
| 68 | public protocol EnvelopeIPCServiceProtocol { |
| 69 | /// `querySince` — rows with `updatedAt > since`, tombstones included. |
| 70 | /// Reply: rows, or error (unhealthy read — caller aborts, never empties). |
| 71 | func querySince(vaultId: String, table: String, since: Int64, reply: @escaping ([XPCEnvelopeRow]?, Error?) -> Void) |
| 72 | /// `upsertRaw` — idempotent upsert keyed on `syncId`. |
| 73 | func upsertRaw(vaultId: String, table: String, rows: [XPCEnvelopeRow], reply: @escaping (Error?) -> Void) |
| 74 | /// `deleteByVault` — hard-delete (passwords) or timestamped tombstone |
| 75 | /// (everything else). `now` is epoch ms, stamped so delta queries observe |
| 76 | /// the delete (see `EnvelopeIPC.deleteByVault`). |
| 77 | func deleteByVault(vaultId: String, table: String, syncId: String, now: Int64, reply: @escaping (Error?) -> Void) |
| 78 | } |
| 79 | |
| 80 | /// Service implementation. Thin wrapper over `EnvelopeIPC` (see |
| 81 | /// `../Shared/EnvelopeIPC.swift`) — no logic beyond allowlist validation. |
| 82 | /// |
| 83 | /// MAC-CHECK: `EnvelopeIPC`/`EnvelopeTable`/`EnvelopeRow` live in the Shared |
| 84 | /// sources — the XPC service target must compile them in (or import the |
| 85 | /// shared module) or this file won't resolve. Same for `EverysyncCore`. |
| 86 | @objc(EnvelopeIPCService) |
| 87 | public final class EnvelopeIPCService: NSObject, EnvelopeIPCServiceProtocol { |
| 88 | private let store = EnvelopeIPC() |
| 89 | |
| 90 | private func table(named raw: String) -> EnvelopeTable? { |
| 91 | EnvelopeTable(rawValue: raw) |
| 92 | } |
| 93 | |
| 94 | public func querySince(vaultId: String, table: String, since: Int64, reply: @escaping ([XPCEnvelopeRow]?, Error?) -> Void) { |
| 95 | guard let t = self.table(named: table) else { |
| 96 | reply(nil, EnvelopeIPCError.sqlite("unknown table: \(table)")) |
| 97 | return |
| 98 | } |
| 99 | do { |
| 100 | let rows = try store.querySince(vaultId: vaultId, table: t, since: since) |
| 101 | reply(rows.map { XPCEnvelopeRow(syncId: $0.syncId, updatedAt: $0.updatedAt, deleted: $0.deleted, payloadJson: $0.payloadJson, contentHash: $0.contentHash) }, nil) |
| 102 | } catch { |
| 103 | reply(nil, error) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public func upsertRaw(vaultId: String, table: String, rows: [XPCEnvelopeRow], reply: @escaping (Error?) -> Void) { |
| 108 | guard let t = self.table(named: table) else { |
| 109 | reply(EnvelopeIPCError.sqlite("unknown table: \(table)")) |
| 110 | return |
| 111 | } |
| 112 | do { |
| 113 | try store.upsertRaw(vaultId: vaultId, table: t, rows: rows.map { |
| 114 | EnvelopeRow(syncId: $0.syncId, updatedAt: $0.updatedAt, deleted: $0.deleted, payloadJson: $0.payloadJson, contentHash: $0.contentHash) |
| 115 | }) |
| 116 | reply(nil) |
| 117 | } catch { |
| 118 | reply(error) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | public func deleteByVault(vaultId: String, table: String, syncId: String, now: Int64, reply: @escaping (Error?) -> Void) { |
| 123 | guard let t = self.table(named: table) else { |
| 124 | reply(EnvelopeIPCError.sqlite("unknown table: \(table)")) |
| 125 | return |
| 126 | } |
| 127 | do { |
| 128 | try store.deleteByVault(vaultId: vaultId, table: t, syncId: syncId, now: now) |
| 129 | reply(nil) |
| 130 | } catch { |
| 131 | reply(error) |
| 132 | } |
| 133 | } |
| 134 | } |