-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathServiceDelegate.swift
More file actions
237 lines (209 loc) · 8.68 KB
/
ServiceDelegate.swift
File metadata and controls
237 lines (209 loc) · 8.68 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import AppKit
import Foundation
import Logger
import XPCShared
class ServiceDelegate: NSObject, NSXPCListenerDelegate {
func listener(
_: NSXPCListener,
shouldAcceptNewConnection newConnection: NSXPCConnection
) -> Bool {
newConnection.exportedInterface = NSXPCInterface(
with: CommunicationBridgeXPCServiceProtocol.self
)
let exportedObject = XPCService()
newConnection.exportedObject = exportedObject
newConnection.resume()
Logger.communicationBridge.info("Accepted new connection.")
return true
}
}
class XPCService: CommunicationBridgeXPCServiceProtocol {
static let eventHandler = EventHandler()
func launchExtensionServiceIfNeeded(
withReply reply: @escaping (NSXPCListenerEndpoint?) -> Void
) {
Task {
await Self.eventHandler.launchExtensionServiceIfNeeded(withReply: reply)
}
}
func quit(withReply reply: @escaping () -> Void) {
Task {
await Self.eventHandler.quit(withReply: reply)
}
}
func updateServiceEndpoint(
endpoint: NSXPCListenerEndpoint,
withReply reply: @escaping () -> Void
) {
Task {
await Self.eventHandler.updateServiceEndpoint(endpoint: endpoint, withReply: reply)
}
}
}
actor EventHandler {
var endpoint: NSXPCListenerEndpoint?
let launcher = ExtensionServiceLauncher()
var exitTask: Task<Void, Error>?
init() {
Task { await rescheduleExitTask() }
}
func launchExtensionServiceIfNeeded(
withReply reply: @escaping (NSXPCListenerEndpoint?) -> Void
) async {
rescheduleExitTask()
#if DEBUG
if let endpoint, !(await testXPCListenerEndpoint(endpoint)) {
self.endpoint = nil
}
reply(endpoint)
#else
if await launcher.isApplicationValid {
Logger.communicationBridge.info("Service app is still valid")
reply(endpoint)
} else {
endpoint = nil
await launcher.launch()
reply(nil)
}
#endif
}
func quit(withReply reply: () -> Void) {
Logger.communicationBridge.info("Exiting service.")
listener.invalidate()
exit(0)
}
func updateServiceEndpoint(endpoint: NSXPCListenerEndpoint, withReply reply: () -> Void) {
rescheduleExitTask()
self.endpoint = endpoint
reply()
}
/// The bridge will kill itself when it's not used for a period.
/// It's fine that the bridge is killed because it will be launched again when needed.
private func rescheduleExitTask() {
exitTask?.cancel()
exitTask = Task {
#if DEBUG
try await Task.sleep(nanoseconds: 60_000_000_000)
Logger.communicationBridge.info("Exit will be called in release build.")
#else
try await Task.sleep(nanoseconds: 1_800_000_000_000)
Logger.communicationBridge.info("Exiting service.")
listener.invalidate()
exit(0)
#endif
}
}
}
actor ExtensionServiceLauncher {
let appIdentifier = bundleIdentifierBase.appending(".ExtensionService")
let appURL = Bundle.main.bundleURL.appendingPathComponent(
"GitHub Copilot for Xcode Extension.app"
)
var isLaunching: Bool = false
var application: NSRunningApplication?
var isApplicationValid: Bool {
guard let application else { return false }
if application.isTerminated { return false }
let identifier = application.processIdentifier
if let application = NSWorkspace.shared.runningApplications.first(where: {
$0.processIdentifier == identifier
}) {
Logger.communicationBridge.info(
"Service app found: \(application.processIdentifier) \(String(describing: application.bundleIdentifier))"
)
return true
}
return false
}
func launch() {
guard !isLaunching else { return }
isLaunching = true
Logger.communicationBridge.info("Launching extension service app.")
// First check if the app is already running
if let runningApp = NSWorkspace.shared.runningApplications.first(where: {
$0.bundleIdentifier == appIdentifier
}) {
Logger.communicationBridge.info("Extension service app already running with PID: \(runningApp.processIdentifier)")
self.application = runningApp
self.isLaunching = false
return
}
// Implement a retry mechanism with exponential backoff
Task {
var retryCount = 0
let maxRetries = 3
var success = false
while !success && retryCount < maxRetries {
do {
// Add a delay between retries with exponential backoff
if retryCount > 0 {
let delaySeconds = pow(2.0, Double(retryCount - 1))
Logger.communicationBridge.info("Retrying launch after \(delaySeconds) seconds (attempt \(retryCount + 1) of \(maxRetries))")
try await Task.sleep(nanoseconds: UInt64(delaySeconds * 1_000_000_000))
}
// Use a task-based approach for launching with timeout
let launchTask = Task<NSRunningApplication?, Error> { () -> NSRunningApplication? in
return await withCheckedContinuation { continuation in
NSWorkspace.shared.openApplication(
at: appURL,
configuration: {
let configuration = NSWorkspace.OpenConfiguration()
configuration.createsNewApplicationInstance = false
configuration.addsToRecentItems = false
configuration.activates = false
return configuration
}()
) { app, error in
if let error = error {
continuation.resume(returning: nil)
} else {
continuation.resume(returning: app)
}
}
}
}
// Set a timeout for the launch operation
let timeoutTask = Task {
try await Task.sleep(nanoseconds: 10_000_000_000) // 10 seconds
return
}
// Wait for either the launch or the timeout
let app = try await withTaskCancellationHandler {
try await launchTask.value ?? nil
} onCancel: {
launchTask.cancel()
}
// Cancel the timeout task
timeoutTask.cancel()
if let app = app {
// Success!
self.application = app
success = true
break
} else {
// App is nil, retry
retryCount += 1
Logger.communicationBridge.info("Launch attempt \(retryCount) failed, app is nil")
}
} catch {
retryCount += 1
Logger.communicationBridge.error("Error during launch attempt \(retryCount): \(error.localizedDescription)")
}
}
// Double-check we have a valid application
if !success && self.application == nil {
// After all retries, check once more if the app is running (it might have launched but we missed the callback)
if let runningApp = NSWorkspace.shared.runningApplications.first(where: {
$0.bundleIdentifier == appIdentifier
}) {
Logger.communicationBridge.info("Found running extension service after retries: \(runningApp.processIdentifier)")
self.application = runningApp
success = true
} else {
Logger.communicationBridge.info("Failed to launch extension service after \(maxRetries) attempts")
}
}
self.isLaunching = false
}
}
}