-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProxyXPCServiceHost.swift
More file actions
173 lines (150 loc) · 5.92 KB
/
ProxyXPCServiceHost.swift
File metadata and controls
173 lines (150 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import Foundation
import Logging
import NIOConcurrencyHelpers
private final class ProxyXPCClientRegistry {
private struct ClientEntry {
let connection: NSXPCConnection
var isRegistered: Bool
}
private let state = NIOLockedValueBox<[ObjectIdentifier: ClientEntry]>([:])
func add(_ connection: NSXPCConnection) {
state.withLockedValue { state in
state[ObjectIdentifier(connection)] = ClientEntry(connection: connection, isRegistered: false)
}
}
func remove(_ connection: NSXPCConnection) {
state.withLockedValue { state in
state.removeValue(forKey: ObjectIdentifier(connection))
}
}
func markRegistered(_ connection: NSXPCConnection, value: Bool) {
state.withLockedValue { state in
let key = ObjectIdentifier(connection)
guard var entry = state[key] else { return }
entry.isRegistered = value
state[key] = entry
}
}
func registeredConnections() -> [NSXPCConnection] {
state.withLockedValue { state in
state.values.compactMap { entry in
entry.isRegistered ? entry.connection : nil
}
}
}
}
public final class ProxyXPCServiceHost: NSObject, NSXPCListenerDelegate, ProxyXPCControlProtocol {
public typealias StatusProvider = () -> ProxyXPCStatusPayload
public typealias ShutdownHandler = () -> Void
private let listener = NSXPCListener(machServiceName: ProxyXPCServiceConfiguration.machServiceName)
private let registry = ProxyXPCClientRegistry()
private let statusProvider: StatusProvider
private let shutdownHandler: ShutdownHandler
private let encoder = JSONEncoder()
private let logger = ProxyLogging.make("xpc")
private let lastBroadcastPayload = NIOLockedValueBox<Data?>(nil)
private var isRunning = false
public init(statusProvider: @escaping StatusProvider, shutdownHandler: @escaping ShutdownHandler) {
self.statusProvider = statusProvider
self.shutdownHandler = shutdownHandler
super.init()
listener.delegate = self
}
public func start() {
guard !isRunning else { return }
isRunning = true
listener.resume()
logger.info("XPC listener started", metadata: ["service": "\(ProxyXPCServiceConfiguration.machServiceName)"])
}
public func stop() {
guard isRunning else { return }
isRunning = false
listener.invalidate()
logger.info("XPC listener stopped", metadata: ["service": "\(ProxyXPCServiceConfiguration.machServiceName)"])
}
public func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool {
newConnection.exportedInterface = NSXPCInterface(with: ProxyXPCControlProtocol.self)
newConnection.exportedObject = self
newConnection.remoteObjectInterface = NSXPCInterface(with: ProxyXPCClientProtocol.self)
newConnection.invalidationHandler = { [weak self, weak newConnection] in
guard let self, let newConnection else { return }
self.registry.remove(newConnection)
}
registry.add(newConnection)
newConnection.resume()
return true
}
public func ping(_ reply: @escaping (String) -> Void) {
reply("pong")
}
public func fetchStatus(_ reply: @escaping (Data) -> Void) {
reply(encodedStatus())
}
public func registerClient(_ reply: @escaping (Data) -> Void) {
guard let connection = NSXPCConnection.current() else {
reply(encodedStatus())
return
}
registry.markRegistered(connection, value: true)
let payload = encodedStatus()
lastBroadcastPayload.withLockedValue { $0 = payload }
if let client = connection.remoteObjectProxyWithErrorHandler({ [logger] (error: Error) in
logger.debug("XPC callback delivery failed", metadata: ["error": "\(error)"])
}) as? ProxyXPCClientProtocol {
client.statusDidUpdate(payload)
}
reply(payload)
}
public func unregisterClient(_ reply: @escaping () -> Void) {
if let connection = NSXPCConnection.current() {
registry.markRegistered(connection, value: false)
}
reply()
}
public func requestShutdown(_ reply: @escaping (Bool) -> Void) {
shutdownHandler()
reply(true)
}
public func pushStatusIfChanged() {
guard isRunning else { return }
let payload = encodedStatus()
let shouldBroadcast = lastBroadcastPayload.withLockedValue { last in
if last == payload {
return false
}
last = payload
return true
}
if shouldBroadcast {
broadcast(payload: payload)
}
}
private func broadcast(payload: Data) {
for connection in registry.registeredConnections() {
guard let client = connection.remoteObjectProxyWithErrorHandler({ [logger] (error: Error) in
logger.debug("XPC callback delivery failed", metadata: ["error": "\(error)"])
}) as? ProxyXPCClientProtocol else {
continue
}
client.statusDidUpdate(payload)
}
}
private func encodedStatus() -> Data {
do {
return try encoder.encode(statusProvider())
} catch {
logger.error("Failed to encode XPC status payload", metadata: ["error": "\(error)"])
let fallback = ProxyXPCStatusPayload(
endpointDisplay: "unavailable",
reachable: false,
version: "unknown",
xcodeHealth: "Unknown",
activeClientCount: 0,
activeCorrelatedRequestCount: 0,
clients: [],
fetchError: "Failed to encode status payload."
)
return (try? encoder.encode(fallback)) ?? Data()
}
}
}