fix(computer-use): settle macOS permission checks (#13025)
This commit is contained in:
+116
@@ -0,0 +1,116 @@
|
||||
import Dispatch
|
||||
import Foundation
|
||||
|
||||
public struct PermissionStatusSnapshot: Equatable, Sendable {
|
||||
public let accessibilityGranted: Bool
|
||||
public let screenshotsGranted: Bool
|
||||
|
||||
public init(accessibilityGranted: Bool, screenshotsGranted: Bool) {
|
||||
self.accessibilityGranted = accessibilityGranted
|
||||
self.screenshotsGranted = screenshotsGranted
|
||||
}
|
||||
}
|
||||
|
||||
public enum PermissionStatusSnapshotProbe {
|
||||
public typealias Probe = @Sendable () -> Bool
|
||||
|
||||
private static let probeQueue = DispatchQueue(
|
||||
label: "com.stablyai.orca.computer-use-permission-status",
|
||||
attributes: .concurrent
|
||||
)
|
||||
|
||||
public static func capture(
|
||||
accessibilityProbe: @escaping Probe,
|
||||
screenshotsProbe: @escaping Probe
|
||||
) -> PermissionStatusSnapshot {
|
||||
let result = PermissionStatusSnapshotResult()
|
||||
let group = DispatchGroup()
|
||||
|
||||
group.enter()
|
||||
probeQueue.async {
|
||||
result.recordAccessibility(accessibilityProbe())
|
||||
group.leave()
|
||||
}
|
||||
group.enter()
|
||||
probeQueue.async {
|
||||
result.recordScreenshots(screenshotsProbe())
|
||||
group.leave()
|
||||
}
|
||||
group.wait()
|
||||
return result.snapshot
|
||||
}
|
||||
}
|
||||
|
||||
public final class PermissionStatusRefreshCoordinator: @unchecked Sendable {
|
||||
public typealias SnapshotProbe = @Sendable () -> PermissionStatusSnapshot
|
||||
public typealias SnapshotHandler = @Sendable (PermissionStatusSnapshot) -> Void
|
||||
|
||||
private let callbackQueue: DispatchQueue
|
||||
private let handler: SnapshotHandler
|
||||
private let probe: SnapshotProbe
|
||||
private let stateLock = NSLock()
|
||||
private let workerQueue: DispatchQueue
|
||||
private var refreshInFlight = false
|
||||
|
||||
public init(
|
||||
workerQueue: DispatchQueue = DispatchQueue(
|
||||
label: "com.stablyai.orca.computer-use-permission-refresh",
|
||||
qos: .userInitiated
|
||||
),
|
||||
callbackQueue: DispatchQueue = .main,
|
||||
probe: @escaping SnapshotProbe,
|
||||
handler: @escaping SnapshotHandler
|
||||
) {
|
||||
self.workerQueue = workerQueue
|
||||
self.callbackQueue = callbackQueue
|
||||
self.probe = probe
|
||||
self.handler = handler
|
||||
}
|
||||
|
||||
public func refresh() {
|
||||
stateLock.lock()
|
||||
guard !refreshInFlight else {
|
||||
stateLock.unlock()
|
||||
return
|
||||
}
|
||||
refreshInFlight = true
|
||||
stateLock.unlock()
|
||||
|
||||
workerQueue.async { [self] in
|
||||
let snapshot = probe()
|
||||
callbackQueue.async { [self] in
|
||||
stateLock.lock()
|
||||
refreshInFlight = false
|
||||
stateLock.unlock()
|
||||
handler(snapshot)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final class PermissionStatusSnapshotResult: @unchecked Sendable {
|
||||
private let lock = NSLock()
|
||||
private var accessibilityGranted = false
|
||||
private var screenshotsGranted = false
|
||||
|
||||
var snapshot: PermissionStatusSnapshot {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return PermissionStatusSnapshot(
|
||||
accessibilityGranted: accessibilityGranted,
|
||||
screenshotsGranted: screenshotsGranted
|
||||
)
|
||||
}
|
||||
|
||||
func recordAccessibility(_ granted: Bool) {
|
||||
lock.lock()
|
||||
accessibilityGranted = granted
|
||||
lock.unlock()
|
||||
}
|
||||
|
||||
func recordScreenshots(_ granted: Bool) {
|
||||
lock.lock()
|
||||
screenshotsGranted = granted
|
||||
lock.unlock()
|
||||
}
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
import Foundation
|
||||
|
||||
public enum PermissionTrustSettling {
|
||||
public struct Outcome: Equatable {
|
||||
public let settled: Bool
|
||||
public let attempts: Int
|
||||
public let waitedMs: Int
|
||||
|
||||
public init(settled: Bool, attempts: Int, waitedMs: Int) {
|
||||
self.settled = settled
|
||||
self.attempts = attempts
|
||||
self.waitedMs = waitedMs
|
||||
}
|
||||
}
|
||||
|
||||
public static let defaultTimeoutMs = 1_500
|
||||
public static let defaultIntervalMs = 100
|
||||
|
||||
public static func settle(
|
||||
timeoutMs: Int = defaultTimeoutMs,
|
||||
intervalMs: Int = defaultIntervalMs,
|
||||
sleepMs: (Int) -> Void = { interval in
|
||||
Thread.sleep(forTimeInterval: TimeInterval(interval) / 1_000)
|
||||
},
|
||||
probe: () -> Bool
|
||||
) -> Outcome {
|
||||
precondition(timeoutMs >= 0, "timeoutMs must not be negative")
|
||||
precondition(intervalMs > 0, "intervalMs must be positive")
|
||||
var attempts = 0
|
||||
var waitedMs = 0
|
||||
while true {
|
||||
attempts += 1
|
||||
if probe() {
|
||||
return Outcome(settled: true, attempts: attempts, waitedMs: waitedMs)
|
||||
}
|
||||
if waitedMs >= timeoutMs {
|
||||
return Outcome(settled: false, attempts: attempts, waitedMs: waitedMs)
|
||||
}
|
||||
let interval = min(intervalMs, timeoutMs - waitedMs)
|
||||
sleepMs(interval)
|
||||
waitedMs += interval
|
||||
}
|
||||
}
|
||||
|
||||
public static func settleWithFallback(
|
||||
initialTimeoutMs: Int = defaultTimeoutMs,
|
||||
finalTimeoutMs: Int,
|
||||
intervalMs: Int = defaultIntervalMs,
|
||||
sleepMs: (Int) -> Void = { interval in
|
||||
Thread.sleep(forTimeInterval: TimeInterval(interval) / 1_000)
|
||||
},
|
||||
probe: () -> Bool,
|
||||
fallbackProbe: () -> Bool
|
||||
) -> Bool {
|
||||
if settle(
|
||||
timeoutMs: initialTimeoutMs,
|
||||
intervalMs: intervalMs,
|
||||
sleepMs: sleepMs,
|
||||
probe: probe
|
||||
).settled {
|
||||
return true
|
||||
}
|
||||
if fallbackProbe() {
|
||||
return true
|
||||
}
|
||||
return settle(
|
||||
timeoutMs: finalTimeoutMs,
|
||||
intervalMs: intervalMs,
|
||||
sleepMs: sleepMs,
|
||||
probe: probe
|
||||
).settled
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
public struct ScreenCaptureProbeWindow: Equatable {
|
||||
public let layer: Int
|
||||
public let ownerPid: Int32
|
||||
public let windowId: UInt32
|
||||
|
||||
public init(layer: Int, ownerPid: Int32, windowId: UInt32) {
|
||||
self.layer = layer
|
||||
self.ownerPid = ownerPid
|
||||
self.windowId = windowId
|
||||
}
|
||||
}
|
||||
|
||||
public enum ScreenCaptureProbeWindowSelection {
|
||||
public static func firstCrossProcessNormalWindow(
|
||||
ownPid: Int32,
|
||||
windows: [ScreenCaptureProbeWindow]
|
||||
) -> UInt32? {
|
||||
windows.first { window in
|
||||
window.layer == 0 && window.ownerPid != ownPid
|
||||
}?.windowId
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user