Skip to content

Commit 68a5e64

Browse files
authored
fix(apple): harden Device Hub discovery and sidebar recovery (#2733)
1 parent 9208b90 commit 68a5e64

8 files changed

Lines changed: 414 additions & 160 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import ApplicationServices
2+
3+
package enum DeviceHubWindowRead {
4+
case available([AXUIElement])
5+
case failed(Int32)
6+
}
7+
8+
package struct DeviceHubWindowCandidate {
9+
package let processID: Int32
10+
package let window: AXUIElement
11+
}
12+
13+
package enum DeviceHubRowLookup {
14+
case identified(AXUIElement, restoreSidebar: Bool)
15+
case sidebarUnavailable
16+
case unconfirmed
17+
case timedOut
18+
19+
package var diagnostic: String {
20+
switch self {
21+
case .identified: return "identified"
22+
case .sidebarUnavailable: return "sidebar-unavailable"
23+
case .unconfirmed: return "row-unconfirmed"
24+
case .timedOut: return "timed-out"
25+
}
26+
}
27+
}
28+
29+
package struct DeviceHubWindowInventory {
30+
package private(set) var candidates: [DeviceHubWindowCandidate] = []
31+
package private(set) var emptyProcessIDs: [Int32] = []
32+
package private(set) var failures: [Int32: Int32] = [:]
33+
package private(set) var candidateOutcomes: [String] = []
34+
package private(set) var budgetExpired = false
35+
36+
package init() {}
37+
38+
package mutating func record(processID: Int32, read: DeviceHubWindowRead) {
39+
switch read {
40+
case .available(let windows):
41+
if windows.isEmpty { emptyProcessIDs.append(processID) }
42+
candidates.append(contentsOf: windows.map { DeviceHubWindowCandidate(processID: processID, window: $0) })
43+
case .failed(let status):
44+
failures[processID] = status
45+
}
46+
}
47+
48+
package mutating func record(candidate: Int, outcome: DeviceHubRowLookup) {
49+
candidateOutcomes.append("\(candidates[candidate].processID):\(candidate):\(outcome.diagnostic)")
50+
}
51+
52+
package mutating func expireBudget() { budgetExpired = true }
53+
54+
package func processesToReopen(attempted: Set<Int32>) -> [Int32] {
55+
budgetExpired ? [] : emptyProcessIDs.filter { !attempted.contains($0) }
56+
}
57+
58+
package func failureReason(reopenFailures: [Int32: String] = [:]) -> String {
59+
if !failures.isEmpty { return "device-hub-window-read-failed" }
60+
if budgetExpired { return "device-hub-discovery-timeout" }
61+
if !candidates.isEmpty { return "device-hub-identity-unconfirmed" }
62+
if let reason = reopenFailures.sorted(by: { $0.key < $1.key }).first?.value { return reason }
63+
return "device-hub-window-missing"
64+
}
65+
}

‎apple/macos-helper/Sources/AgentDeviceMacOSHelper/DeviceHubPose.swift‎

Lines changed: 45 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import ApplicationServices
44
import Darwin
55
import Foundation
66

7-
/// The Xcode Device Hub application. Its device windows carry an action bar whose pose controls
8-
/// are the only public seam onto the private channel that folds a simulator.
9-
private let deviceHubBundleId = "com.apple.dt.Devices"
10-
11-
/// How long a reopen gets to restore a window, and a sidebar selection to switch the window's
12-
/// device, before the press is refused.
7+
/// How long sidebar selection and pose-control discovery may each wait before refusing a press.
138
private let deviceHubSettleDeadline: TimeInterval = 8
149
private let deviceHubPoll: TimeInterval = 0.25
1510

@@ -51,16 +46,15 @@ func handleDeviceHub(arguments: [String]) throws -> any Encodable {
5146
details: ["reason": "accessibility-permission", "permission": "accessibility"]
5247
)
5348
}
54-
guard let pid = deviceHubProcessIdentifier() else {
55-
throw HelperError.commandFailed(
56-
"Xcode Device Hub is not running",
57-
details: ["reason": "device-hub-not-running", "bundleId": deviceHubBundleId]
58-
)
49+
let discovery = try discoverDeviceHubWindow(udid: udid, deviceName: deviceName)
50+
let window = discovery.window
51+
defer {
52+
if discovery.revealedSidebar {
53+
_ = setDeviceHubSidebar(visible: false, window: window,
54+
deadline: ProcessInfo.processInfo.systemUptime + 0.5)
55+
}
5956
}
60-
61-
let appElement = AXUIElementCreateApplication(pid)
62-
let (window, reopened) = try deviceHubWindow(in: appElement, pid: pid, deviceName: deviceName)
63-
let selected = try selectDevice(udid: udid, deviceName: deviceName, in: window, appElement: appElement)
57+
let selected = try selectDevice(row: discovery.row, deviceName: deviceName, in: window)
6458
let windowTitle = stringAttribute(window, attribute: kAXTitleAttribute as String) ?? ""
6559
guard let button = awaitPoseButton(in: window, description: control) else {
6660
throw HelperError.commandFailed(
@@ -84,64 +78,12 @@ func handleDeviceHub(arguments: [String]) throws -> any Encodable {
8478
pose: pose.rawValue,
8579
control: control,
8680
windowTitle: windowTitle,
87-
reopened: reopened,
81+
reopened: discovery.reopened,
8882
selected: selected
8983
)
9084
)
9185
}
9286

93-
/// Device Hub is launched through a trampoline, so LaunchServices registers it with no process
94-
/// identifier (`NSRunningApplication.processIdentifier` is -1) and an accessibility element built
95-
/// from that identifier is invalid. The process table is the only place its real pid appears.
96-
private func deviceHubProcessIdentifier() -> pid_t? {
97-
let executableSuffix = deviceHubExecutableSuffix
98-
let byteCount = proc_listpids(UInt32(PROC_ALL_PIDS), 0, nil, 0)
99-
guard byteCount > 0 else { return nil }
100-
var pids = [pid_t](repeating: 0, count: Int(byteCount) / MemoryLayout<pid_t>.size + 64)
101-
let filled = proc_listpids(
102-
UInt32(PROC_ALL_PIDS), 0, &pids, Int32(pids.count * MemoryLayout<pid_t>.size)
103-
)
104-
guard filled > 0 else { return nil }
105-
var path = [CChar](repeating: 0, count: 4096)
106-
for pid in pids.prefix(Int(filled) / MemoryLayout<pid_t>.size) where pid > 0 {
107-
guard proc_pidpath(pid, &path, UInt32(path.count)) > 0 else { continue }
108-
if String(cString: path).hasSuffix(executableSuffix) {
109-
return pid
110-
}
111-
}
112-
return nil
113-
}
114-
115-
/// A device window to drive: the one already showing this device when there is one, otherwise
116-
/// any device window, since its sidebar can switch it to the device. A Device Hub with no window
117-
/// at all — a simulator booted headlessly leaves it that way — is asked to reopen one.
118-
private func deviceHubWindow(
119-
in appElement: AXUIElement,
120-
pid: pid_t,
121-
deviceName: String
122-
) throws -> (window: AXUIElement, reopened: Bool) {
123-
if let window = preferredWindow(in: appElement, deviceName: deviceName) {
124-
return (window, false)
125-
}
126-
try sendReopenEvent(to: pid)
127-
let deadline = Date().addingTimeInterval(deviceHubSettleDeadline)
128-
while Date() < deadline {
129-
Thread.sleep(forTimeInterval: deviceHubPoll)
130-
if let window = preferredWindow(in: appElement, deviceName: deviceName) {
131-
return (window, true)
132-
}
133-
}
134-
throw HelperError.commandFailed(
135-
"Device Hub shows no device window to drive",
136-
details: ["reason": "device-hub-window-missing", "deviceName": deviceName]
137-
)
138-
}
139-
140-
private func preferredWindow(in appElement: AXUIElement, deviceName: String) -> AXUIElement? {
141-
let candidates = windows(of: appElement)
142-
return candidates.first { windowShows(deviceName: deviceName, $0) } ?? candidates.first
143-
}
144-
14587
private func windowShows(deviceName: String, _ window: AXUIElement) -> Bool {
14688
return deviceHubWindowShows(
14789
deviceName: deviceName,
@@ -152,7 +94,7 @@ private func windowShows(deviceName: String, _ window: AXUIElement) -> Bool {
15294
/// `kAEReopenApplication` is what the Dock sends when an app with no open windows is clicked, and
15395
/// it is the one event Device Hub answers by restoring the device window. Sent to the process
15496
/// directly, because LaunchServices cannot address a trampolined app by bundle identifier.
155-
private func sendReopenEvent(to pid: pid_t) throws {
97+
func sendDeviceHubReopenEvent(to pid: pid_t, timeout: TimeInterval) throws {
15698
let target = NSAppleEventDescriptor(processIdentifier: pid)
15799
let event = NSAppleEventDescriptor(
158100
eventClass: AEEventClass(kCoreEventClass),
@@ -162,7 +104,7 @@ private func sendReopenEvent(to pid: pid_t) throws {
162104
transactionID: AETransactionID(kAnyTransactionID)
163105
)
164106
do {
165-
_ = try event.sendEvent(options: [.noReply], timeout: 5)
107+
_ = try event.sendEvent(options: [.noReply], timeout: min(5, max(0.001, timeout)))
166108
} catch {
167109
let code = (error as NSError).code
168110
throw HelperError.commandFailed(
@@ -178,66 +120,43 @@ private func sendReopenEvent(to pid: pid_t) throws {
178120
/// Switches the window to the device through its sidebar row, whose accessibility identifier is
179121
/// `TableRow.Device.<udid>` — the one place Device Hub exposes a device identity that two
180122
/// simulators sharing a name cannot confuse. A window already titled with the device still gets
181-
/// the selection when the row is visible, because the title alone cannot tell such twins apart.
123+
/// the selection, because the title alone cannot tell such twins apart.
182124
private func selectDevice(
183-
udid: String,
125+
row: AXUIElement,
184126
deviceName: String,
185-
in window: AXUIElement,
186-
appElement: AXUIElement
127+
in window: AXUIElement
187128
) throws -> Bool {
188-
var shownSidebar = false
189-
var row = deviceRow(udid: udid, in: window)
190-
if row == nil, showSidebar(appElement: appElement) {
191-
shownSidebar = true
192-
let deadline = Date().addingTimeInterval(deviceHubSettleDeadline)
193-
while row == nil, Date() < deadline {
194-
Thread.sleep(forTimeInterval: deviceHubPoll)
195-
row = deviceRow(udid: udid, in: window)
196-
}
197-
}
198-
defer {
199-
if shownSidebar { _ = pressMenuItem(appElement: appElement, menu: "View", item: "Hide Sidebar") }
200-
}
201-
guard let row else {
202-
if windowShows(deviceName: deviceName, window) {
203-
return false
204-
}
205-
throw HelperError.commandFailed(
206-
"Device Hub lists no device \(udid) in its sidebar",
207-
details: ["reason": "device-hub-device-missing", "udid": udid, "deviceName": deviceName]
208-
)
209-
}
129+
let deadline = ProcessInfo.processInfo.systemUptime + deviceHubSettleDeadline
130+
AXUIElementSetMessagingTimeout(row, 0.25)
131+
AXUIElementSetMessagingTimeout(window, 0.25)
210132
let status = AXUIElementSetAttributeValue(row, kAXSelectedAttribute as CFString, kCFBooleanTrue)
211-
guard status == .success else {
133+
guard status == .success || status == .cannotComplete else {
212134
throw HelperError.commandFailed(
213135
"Device Hub refused to select \(deviceName) in its sidebar",
214136
details: ["reason": "device-hub-select-failed", "status": "\(status.rawValue)"]
215137
)
216138
}
217-
let deadline = Date().addingTimeInterval(deviceHubSettleDeadline)
218-
while !windowShows(deviceName: deviceName, window), Date() < deadline {
139+
while ProcessInfo.processInfo.systemUptime < deadline {
140+
var selected: CFTypeRef?
141+
if AXUIElementCopyAttributeValue(row, kAXSelectedAttribute as CFString, &selected) == .success,
142+
(selected as? Bool) == true, windowShows(deviceName: deviceName, window) { return true }
219143
Thread.sleep(forTimeInterval: deviceHubPoll)
220144
}
221-
guard windowShows(deviceName: deviceName, window) else {
222-
throw HelperError.commandFailed(
223-
"Device Hub did not switch its window to \(deviceName)",
224-
details: [
225-
"reason": "device-hub-select-unconfirmed",
226-
"windowTitle": stringAttribute(window, attribute: kAXTitleAttribute as String) ?? "",
227-
]
228-
)
229-
}
230-
return true
145+
throw HelperError.commandFailed(
146+
"Device Hub did not confirm selection of \(deviceName)",
147+
details: ["reason": "device-hub-select-unconfirmed", "status": "\(status.rawValue)"]
148+
)
231149
}
232150

233-
private func deviceRow(udid: String, in window: AXUIElement) -> AXUIElement? {
234-
guard let label = findElement(root: window, depth: 0, where: {
151+
func deviceHubDeviceRow(udid: String, in window: AXUIElement, deadline: TimeInterval) -> AXUIElement? {
152+
guard let label = findDeviceHubElement(root: window, depth: 0, deadline: deadline, where: {
235153
stringAttribute($0, attribute: "AXIdentifier") == deviceHubDeviceRowIdentifier(udid: udid)
236154
}) else {
237155
return nil
238156
}
239157
var current: AXUIElement? = label
240-
while let element = current {
158+
while let element = current, ProcessInfo.processInfo.systemUptime < deadline {
159+
AXUIElementSetMessagingTimeout(element, Float(max(0.001, min(0.25, deadline - ProcessInfo.processInfo.systemUptime))))
241160
if stringAttribute(element, attribute: kAXRoleAttribute as String) == "AXRow" {
242161
return element
243162
}
@@ -246,66 +165,52 @@ private func deviceRow(udid: String, in window: AXUIElement) -> AXUIElement? {
246165
return nil
247166
}
248167

249-
private func showSidebar(appElement: AXUIElement) -> Bool {
250-
return pressMenuItem(appElement: appElement, menu: "View", item: "Show Sidebar")
251-
}
252-
253-
private func pressMenuItem(appElement: AXUIElement, menu: String, item: String) -> Bool {
254-
guard let menuBar = elementAttribute(appElement, attribute: kAXMenuBarAttribute as String) else {
255-
return false
256-
}
257-
for menuBarItem in children(of: menuBar)
258-
where stringAttribute(menuBarItem, attribute: kAXTitleAttribute as String) == menu {
259-
for submenu in children(of: menuBarItem) {
260-
for menuItem in children(of: submenu)
261-
where stringAttribute(menuItem, attribute: kAXTitleAttribute as String) == item {
262-
return AXUIElementPerformAction(menuItem, kAXPressAction as CFString) == .success
263-
}
264-
}
265-
}
266-
return false
267-
}
268-
269168
/// The action bar is rebuilt for the device the window shows, so right after a sidebar selection
270169
/// the window already carries the new title while the pose controls are still being laid out.
271170
/// The controls are therefore awaited, not looked up once.
272171
private func awaitPoseButton(in window: AXUIElement, description: String) -> AXUIElement? {
273-
let deadline = Date().addingTimeInterval(deviceHubSettleDeadline)
172+
let deadline = ProcessInfo.processInfo.systemUptime + deviceHubSettleDeadline
274173
while true {
275-
if let button = poseButton(in: window, description: description) {
174+
if let button = poseButton(in: window, description: description, deadline: deadline) {
276175
return button
277176
}
278-
guard Date() < deadline else { return nil }
177+
guard ProcessInfo.processInfo.systemUptime < deadline else { return nil }
279178
Thread.sleep(forTimeInterval: deviceHubPoll)
280179
}
281180
}
282181

283182
/// The pose controls sit in the window's action bar as `AXButton`s described by their preset
284183
/// name; the simulated screen inside the same window is an iOS content group whose own buttons
285184
/// carry app labels, never these three.
286-
private func poseButton(in window: AXUIElement, description: String) -> AXUIElement? {
287-
return findElement(root: window, depth: 0) {
185+
private func poseButton(in window: AXUIElement, description: String, deadline: TimeInterval) -> AXUIElement? {
186+
return findDeviceHubElement(root: window, depth: 0, deadline: deadline) {
288187
stringAttribute($0, attribute: kAXRoleAttribute as String) == "AXButton"
289188
&& stringAttribute($0, attribute: kAXDescriptionAttribute as String) == description
290189
}
291190
}
292191

293-
private func findElement(
192+
func findDeviceHubElement(
294193
root: AXUIElement,
295194
depth: Int,
195+
deadline: TimeInterval,
296196
where matches: (AXUIElement) -> Bool
297197
) -> AXUIElement? {
298-
if depth > 14 {
198+
let remaining = deadline - ProcessInfo.processInfo.systemUptime
199+
if depth > 14 || remaining <= 0 {
299200
return nil
300201
}
202+
AXUIElementSetMessagingTimeout(root, Float(min(0.25, remaining)))
301203
for child in children(of: root) {
204+
let remaining = deadline - ProcessInfo.processInfo.systemUptime
205+
guard remaining > 0 else { return nil }
206+
AXUIElementSetMessagingTimeout(child, Float(min(0.25, remaining)))
302207
if matches(child) {
303208
return child
304209
}
305210
if stringAttribute(child, attribute: kAXSubroleAttribute as String) == "iOSContentGroup" {
306211
continue
307212
}
308-
if let nested = findElement(root: child, depth: depth + 1, where: matches) {
213+
if let nested = findDeviceHubElement(root: child, depth: depth + 1, deadline: deadline, where: matches) {
309214
return nested
310215
}
311216
}

0 commit comments

Comments
 (0)