xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / Installation / Connection.swift
Connection.swift186 lines · 5.5 KiBRaw
| 1 | // |
| 2 | // Connection.swift |
| 3 | // XKit |
| 4 | // |
| 5 | // Created by Kabir Oberai on 15/11/19. |
| 6 | // Copyright © 2019 Kabir Oberai. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | import Foundation |
| 10 | import SwiftyMobileDevice |
| 11 | |
| 12 | private struct Weak<T: AnyObject> { |
| 13 | weak var value: T? |
| 14 | init(_ value: T) { self.value = value } |
| 15 | } |
| 16 | extension Weak: Sendable where T: Sendable {} |
| 17 | |
| 18 | // there is guaranteed to be at most one Connection instance per (udid, preferences) |
| 19 | // at any moment, thanks to the object pool. Consequently there is at most one device |
| 20 | // per (udid, preferences) at any moment too. |
| 21 | public actor Connection { |
| 22 | |
| 23 | public enum LookupHandler: Hashable, Sendable { |
| 24 | case system(LookupMode) |
| 25 | case custom(any ConnectionLookupHandler) |
| 26 | |
| 27 | public static func == (lhs: LookupHandler, rhs: LookupHandler) -> Bool { |
| 28 | switch (lhs, rhs) { |
| 29 | case (.system(let l), .system(let r)): |
| 30 | l == r |
| 31 | case (.custom(let l), .custom(let r)): |
| 32 | AnyHashable(l) == AnyHashable(r) |
| 33 | default: |
| 34 | false |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public func hash(into hasher: inout Hasher) { |
| 39 | switch self { |
| 40 | case .system(let mode): |
| 41 | hasher.combine(ObjectIdentifier(LookupMode.self)) |
| 42 | hasher.combine(mode) |
| 43 | case .custom(let handler): |
| 44 | hasher.combine(ObjectIdentifier(type(of: handler))) |
| 45 | hasher.combine(handler) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public struct Preferences: Hashable, Sendable { |
| 51 | public var lookupHandler: LookupHandler |
| 52 | |
| 53 | public init(lookupMode: LookupMode) { |
| 54 | self.lookupHandler = .system(lookupMode) |
| 55 | } |
| 56 | |
| 57 | public init(customLookupHandler: any ConnectionLookupHandler) { |
| 58 | self.lookupHandler = .custom(customLookupHandler) |
| 59 | } |
| 60 | |
| 61 | public init(lookupHandler: LookupHandler) { |
| 62 | self.lookupHandler = lookupHandler |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private struct ConnectionDescriptor: Hashable { |
| 67 | let udid: String |
| 68 | let preferences: Preferences |
| 69 | } |
| 70 | |
| 71 | private static let label = "xtool" |
| 72 | private static let pool = WeakPool<ConnectionDescriptor, Connection, Error>() |
| 73 | |
| 74 | private var handle: AnyObject? |
| 75 | private let heartbeatHandler: HeartbeatHandler? |
| 76 | private let udid: String |
| 77 | public let device: Device |
| 78 | public let client: LockdownClient |
| 79 | public let preferences: Preferences |
| 80 | |
| 81 | private init( |
| 82 | udid: String, |
| 83 | preferences: Preferences, |
| 84 | progress: (Double) -> Void |
| 85 | ) async throws { |
| 86 | progress(0/4) |
| 87 | |
| 88 | self.preferences = preferences |
| 89 | self.udid = udid |
| 90 | |
| 91 | switch preferences.lookupHandler { |
| 92 | case .system(let lookupMode): |
| 93 | device = try Device(udid: udid, lookupMode: lookupMode) |
| 94 | case .custom(let lookupHandler): |
| 95 | handle = try lookupHandler.createHandle() |
| 96 | progress(1/4) |
| 97 | device = try Device(udid: udid) |
| 98 | } |
| 99 | |
| 100 | progress(2/4) |
| 101 | |
| 102 | client = try LockdownClient(device: device, label: Self.label, performHandshake: true) |
| 103 | |
| 104 | #if os(iOS) |
| 105 | progress(3/4) |
| 106 | heartbeatHandler = try await HeartbeatHandler(device: device, client: client) |
| 107 | #else |
| 108 | heartbeatHandler = nil |
| 109 | #endif |
| 110 | |
| 111 | progress(4/4) |
| 112 | } |
| 113 | |
| 114 | public static func connection( |
| 115 | forUDID udid: String, |
| 116 | preferences: Preferences, |
| 117 | progress: @escaping @Sendable (Double) -> Void |
| 118 | ) async throws -> Connection { |
| 119 | progress(0) |
| 120 | defer { progress(1) } |
| 121 | return try await Self.pool.value( |
| 122 | key: ConnectionDescriptor(udid: udid, preferences: preferences) |
| 123 | ) { |
| 124 | try await Connection(udid: udid, preferences: preferences, progress: progress) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | deinit { |
| 129 | heartbeatHandler?.stop() |
| 130 | // we could nil out connections[udid] here but that might lead to |
| 131 | // weird race conditions against Connection.connection that seem |
| 132 | // like a nightmare to diagnose, and storing an empty box for a |
| 133 | // udid isn't much memory anyway |
| 134 | } |
| 135 | |
| 136 | public func startClient<T: LockdownService>(_ type: T.Type = T.self, sendEscrowBag: Bool = false) throws -> T { |
| 137 | try .init(device: device, service: .init(client: client, type: type, sendEscrowBag: sendEscrowBag)) |
| 138 | } |
| 139 | |
| 140 | } |
| 141 | |
| 142 | #if compiler(<6.2) |
| 143 | private typealias SendableMetatype = Any |
| 144 | #endif |
| 145 | |
| 146 | private actor WeakPool<Key: Hashable & SendableMetatype, Value: AnyObject & Sendable, Failure: Error> { |
| 147 | init() {} |
| 148 | |
| 149 | private var pendingValues: [Key: Task<Result<Value, Failure>, Never>] = [:] |
| 150 | private var existingValues: [Key: Weak<Value>] = [:] |
| 151 | |
| 152 | func value( |
| 153 | key: Key, |
| 154 | create: @escaping @Sendable () async throws(Failure) -> Value |
| 155 | ) async throws(Failure) -> Value { |
| 156 | if let pending = pendingValues[key] { |
| 157 | return try await pending.value.get() |
| 158 | } |
| 159 | |
| 160 | if let existing = existingValues[key] { |
| 161 | if let existingValue = existing.value { |
| 162 | return existingValue |
| 163 | } else { |
| 164 | existingValues[key] = nil |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | let task = Task { () -> Result<Value, Failure> in |
| 169 | do throws(Failure) { |
| 170 | let connection = try await create() |
| 171 | existingValues[key] = Weak(connection) |
| 172 | pendingValues[key] = nil |
| 173 | return .success(connection) |
| 174 | } catch { |
| 175 | return .failure(error) |
| 176 | } |
| 177 | } |
| 178 | pendingValues[key] = task |
| 179 | |
| 180 | return try await task.value.get() |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | public protocol ConnectionLookupHandler: Hashable, Sendable { |
| 185 | func createHandle() throws -> AnyObject |
| 186 | } |