forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceDelegate.swift
More file actions
165 lines (145 loc) · 4.94 KB
/
ServiceDelegate.swift
File metadata and controls
165 lines (145 loc) · 4.94 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
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.")
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 {
Logger.communicationBridge.error(
"Failed to launch extension service app: \(error)"
)
} else {
Logger.communicationBridge.info(
"Finished launching extension service app."
)
}
self.application = app
self.isLaunching = false
}
}
}