Skip to content

Commit 7e61eb9

Browse files
committed
review
1 parent 828d7c0 commit 7e61eb9

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

Coder-Desktop/Coder-Desktop/VPN/VPNService.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,12 @@ final class CoderVPNService: NSObject, VPNService {
8181
// systemExtnDelegate holds a reference to the SystemExtensionDelegate so that it doesn't get
8282
// garbage collected while the OSSystemExtensionRequest is in flight, since the OS framework
8383
// only stores a weak reference to the delegate.
84-
var systemExtnDelegate: SystemExtensionDelegate<CoderVPNService>!
84+
var systemExtnDelegate: SystemExtensionDelegate<CoderVPNService>?
8585

8686
var serverAddress: String?
8787

8888
override init() {
8989
super.init()
90-
systemExtnDelegate = SystemExtensionDelegate(asyncDelegate: self)
9190
}
9291

9392
func start() async {

Coder-Desktop/Coder-Desktop/VPN/VPNSystemExtension.swift

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,8 @@ extension CoderVPNService: SystemExtensionAsyncRecorder {
6666
}
6767

6868
func installSystemExtension() {
69-
logger.info("activating SystemExtension")
70-
guard let bundleID = extensionBundle.bundleIdentifier else {
71-
logger.error("Bundle has no identifier")
72-
return
73-
}
74-
let request = OSSystemExtensionRequest.activationRequest(
75-
forExtensionWithIdentifier: bundleID,
76-
queue: .main
77-
)
78-
request.delegate = systemExtnDelegate
79-
OSSystemExtensionManager.shared.submitRequest(request)
80-
logger.info("submitted SystemExtension request with bundleID: \(bundleID)")
69+
systemExtnDelegate = SystemExtensionDelegate(asyncDelegate: self)
70+
systemExtnDelegate!.installSystemExtension()
8171
}
8272
}
8373

@@ -91,14 +81,28 @@ class SystemExtensionDelegate<AsyncDelegate: SystemExtensionAsyncRecorder>:
9181
// The `didFinishWithResult` function is called for both activation,
9282
// deactivation, and replacement requests. The API provides no way to
9383
// differentiate them. https://developer.apple.com/forums/thread/684021
94-
private var state: SystemExtensionDelegateState = .installing
84+
// This tracks the last request type made, to handle them accordingly.
85+
private var action: SystemExtensionDelegateAction = .none
9586

9687
init(asyncDelegate: AsyncDelegate) {
9788
self.asyncDelegate = asyncDelegate
9889
super.init()
9990
logger.info("SystemExtensionDelegate initialized")
10091
}
10192

93+
func installSystemExtension() {
94+
logger.info("activating SystemExtension")
95+
let bundleID = extensionBundle.bundleIdentifier!
96+
let request = OSSystemExtensionRequest.activationRequest(
97+
forExtensionWithIdentifier: bundleID,
98+
queue: .main
99+
)
100+
request.delegate = self
101+
action = .installing
102+
OSSystemExtensionManager.shared.submitRequest(request)
103+
logger.info("submitted SystemExtension request with bundleID: \(bundleID)")
104+
}
105+
102106
func request(
103107
_: OSSystemExtensionRequest,
104108
didFinishWithResult result: OSSystemExtensionRequest.Result
@@ -111,23 +115,24 @@ class SystemExtensionDelegate<AsyncDelegate: SystemExtensionAsyncRecorder>:
111115
}
112116
return
113117
}
114-
switch state {
118+
switch action {
115119
case .installing:
116120
logger.info("SystemExtension installed")
117121
Task { [asyncDelegate] in
118-
await asyncDelegate.recordSystemExtensionState(SystemExtensionState.installed)
122+
await asyncDelegate.recordSystemExtensionState(.installed)
119123
}
124+
action = .none
120125
case .deleting:
121126
logger.info("SystemExtension deleted")
122127
Task { [asyncDelegate] in
123-
await asyncDelegate.recordSystemExtensionState(SystemExtensionState.uninstalled)
128+
await asyncDelegate.recordSystemExtensionState(.uninstalled)
124129
}
125130
let request = OSSystemExtensionRequest.activationRequest(
126131
forExtensionWithIdentifier: extensionBundle.bundleIdentifier!,
127132
queue: .main
128133
)
129134
request.delegate = self
130-
state = .installing
135+
action = .installing
131136
OSSystemExtensionManager.shared.submitRequest(request)
132137
case .replacing:
133138
logger.info("SystemExtension replaced")
@@ -138,23 +143,26 @@ class SystemExtensionDelegate<AsyncDelegate: SystemExtensionAsyncRecorder>:
138143
queue: .main
139144
)
140145
request.delegate = self
141-
state = .deleting
146+
action = .deleting
142147
OSSystemExtensionManager.shared.submitRequest(request)
148+
case .none:
149+
logger.warning("Received an unexpected request result")
150+
break
143151
}
144152
}
145153

146154
func request(_: OSSystemExtensionRequest, didFailWithError error: Error) {
147155
logger.error("System extension request failed: \(error.localizedDescription)")
148156
Task { [asyncDelegate] in
149157
await asyncDelegate.recordSystemExtensionState(
150-
SystemExtensionState.failed(error.localizedDescription))
158+
.failed(error.localizedDescription))
151159
}
152160
}
153161

154162
func requestNeedsUserApproval(_ request: OSSystemExtensionRequest) {
155163
logger.error("Extension \(request.identifier) requires user approval")
156164
Task { [asyncDelegate] in
157-
await asyncDelegate.recordSystemExtensionState(SystemExtensionState.needsUserApproval)
165+
await asyncDelegate.recordSystemExtensionState(.needsUserApproval)
158166
}
159167
}
160168

@@ -180,12 +188,13 @@ class SystemExtensionDelegate<AsyncDelegate: SystemExtensionAsyncRecorder>:
180188
// There's no way to modify the deactivate request to use a different
181189
// version string (i.e. `existing.bundleVersion`).
182190
logger.info("App upgrade detected, replacing and then reinstalling")
183-
state = .replacing
191+
action = .replacing
184192
return .replace
185193
}
186194
}
187195

188-
enum SystemExtensionDelegateState {
196+
enum SystemExtensionDelegateAction {
197+
case none
189198
case installing
190199
case replacing
191200
case deleting

0 commit comments

Comments
 (0)