Repository navigation
Expand file tree
/
Copy pathSystemServiceSetup.swift
More file actions
423 lines (392 loc) · 19.1 KB
/
Copy pathSystemServiceSetup.swift
File metadata and controls
423 lines (392 loc) · 19.1 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import Foundation
import Security
import ServiceManagement
import CaffeineLogging
import CaffeineServiceProtocol
@MainActor
protocol AppServiceRegistering {
var status: SMAppService.Status { get }
func register() throws
func unregister() async throws
}
extension SMAppService: AppServiceRegistering {}
@MainActor
protocol SystemServiceManaging: AnyObject {
var availability: SystemServiceSetup.Availability { get }
var loginMessage: String? { get }
var loginActionTitle: String? { get }
/// Status only. It never registers a service or validates the installation.
var helperAwaitsApproval: Bool { get }
/// Status only, for the exported log.
var registrationSummary: String { get }
/// Status only, for the setup screen. Neither validates the installation.
var helperStep: SetupStep { get }
var loginStep: SetupStep { get }
/// Whether this installation can register anything at all.
var canSetUp: Bool { get }
/// The setup screen explained launch at login; the card need not repeat it.
func acknowledgeLoginExplanation()
func registerHelper() throws
func updateHelper() async throws
func openSettings()
func prepareServices()
func retrySetup()
func refreshLoginStatus()
func performLoginAction()
}
@MainActor
final class SystemServiceSetup: SystemServiceManaging {
enum Availability { case readyToConnect, setupRequired, approvalRequired, unavailable(String) }
private let defaults: UserDefaults
private let daemon: any AppServiceRegistering
private let loginItem: any AppServiceRegistering
private let installationCheck: (() -> String?)?
private let waitToRegisterAgain: (Duration) async throws -> Void
private let openLoginItems: () -> Void
private let log: EventLog
private var loggedAvailability: String?
private var loggedLoginStatus: String?
// Earlier versions persisted an attempt before they actually registered.
// These receipts record only accepted registration, so a skipped one is tried at launch.
private let enrollmentKey = "caffeine.loginRegistrationAccepted"
private let helperEnrollmentKey = "caffeine.helperRegistrationAccepted"
// Set while this app's own service update has removed the registration.
private let replacementKey = "caffeine.helperReplacementPending"
private let explanationKey = "caffeine.loginExplanationAcknowledged"
/// macOS can refuse a registration that immediately follows removal.
static let registrationRetryDelays: [Duration] = [.seconds(1), .seconds(2), .seconds(3)]
private var prepared = false
private var helperRegistrationError: String?
private var loginRegistrationFailed = false
private(set) var loginMessage: String?
private(set) var loginActionTitle: String?
init(defaults: UserDefaults = .standard,
daemon: any AppServiceRegistering = SMAppService.daemon(plistName: CaffeineServiceIdentity.daemonPlist),
loginItem: any AppServiceRegistering = SMAppService.mainApp,
installationCheck: (() -> String?)? = nil,
waitToRegisterAgain: @escaping (Duration) async throws -> Void = { try await SuspendingClock().sleep(for: $0) },
openLoginItems: @escaping () -> Void = { SMAppService.openSystemSettingsLoginItems() },
log: EventLog = .disabled) {
self.defaults = defaults
self.daemon = daemon
self.loginItem = loginItem
self.installationCheck = installationCheck
self.waitToRegisterAgain = waitToRegisterAgain
self.openLoginItems = openLoginItems
self.log = log
}
var helperAwaitsApproval: Bool { daemon.status == .requiresApproval }
var registrationSummary: String {
"background service \(Self.name(daemon.status)), launch at login \(Self.name(loginItem.status))"
}
var canSetUp: Bool { installationProblem == nil }
var helperStep: SetupStep {
switch daemon.status {
case .enabled: return .allowed
case .requiresApproval:
return SetupStep(state: .needsApproval, actionTitle: "Open System Settings", note: SetupStep.approvalNote)
case .notRegistered, .notFound:
if let helperRegistrationError {
return SetupStep(state: .failed, actionTitle: "Try Again", note: helperRegistrationError)
}
return SetupStep(state: .off, actionTitle: "Set Up…")
@unknown default:
return SetupStep(state: .failed, note: "Caffeine couldn’t read the background service status.")
}
}
/// The same cases and actions as the card's login message.
var loginStep: SetupStep {
if loginRegistrationFailed && (loginItem.status == .notRegistered || loginItem.status == .notFound) {
return SetupStep(state: .failed, actionTitle: "Try Again", note: "Caffeine couldn’t enable launch at login.")
}
switch loginItem.status {
case .enabled: return .allowed
case .requiresApproval:
return SetupStep(state: .needsApproval, actionTitle: "Open System Settings", note: SetupStep.approvalNote)
case .notRegistered:
return SetupStep(state: .off, actionTitle: "Open System Settings", note: "Launch at login is off.")
case .notFound:
return SetupStep(state: .off, actionTitle: "Set Up Login…")
@unknown default:
return SetupStep(state: .failed, actionTitle: "Open System Settings", note: "Caffeine couldn’t read its login status.")
}
}
func acknowledgeLoginExplanation() {
guard !defaults.bool(forKey: explanationKey) else { return }
defaults.set(true, forKey: explanationKey)
refreshLoginStatus()
}
var availability: Availability {
let result = currentAvailability
// Read on every refresh; only a change is worth a line.
let summary: String = switch result {
case .readyToConnect: "registered and enabled"
case .approvalRequired: "registered, waiting for approval in System Settings"
case .setupRequired: "not registered (\(Self.name(daemon.status)))"
case .unavailable(let message): "unavailable – \(message)"
}
if summary != loggedAvailability {
loggedAvailability = summary
if case .unavailable = result { log.error(.setup, "Background service: \(summary)") }
else { log.notice(.setup, "Background service: \(summary)") }
}
return result
}
private var currentAvailability: Availability {
if let problem = installationProblem { return .unavailable(problem) }
switch daemon.status {
case .enabled:
recordAcceptedHelperRegistration()
return .readyToConnect
case .requiresApproval:
recordAcceptedHelperRegistration()
return .approvalRequired
// macOS can return notFound for a missing registration record even when
// the signed helper and plist are present. Validate the bundle separately.
case .notRegistered, .notFound:
if let helperRegistrationError { return .unavailable(helperRegistrationError) }
return .setupRequired
@unknown default: return .unavailable("Caffeine couldn’t read the background service status.")
}
}
private static func name(_ status: SMAppService.Status) -> String {
switch status {
case .notRegistered: "not registered"
case .enabled: "enabled"
case .requiresApproval: "requires approval"
case .notFound: "not found"
@unknown default: "unknown (\(status.rawValue))"
}
}
private static func describe(_ error: Error) -> String {
let error = error as NSError
return "\(error.domain) \(error.code) – \(error.localizedDescription)"
}
/// Registration alone is not readiness; the app must still authenticate and query the helper.
func registerHelper() throws {
if let problem = installationProblem {
log.error(.setup, "Registration skipped: \(problem)")
throw SetupError(problem)
}
log.notice(.setup, "Registering the background service (status \(Self.name(daemon.status)))")
do { try daemon.register() }
catch {
// macOS reports a daemon awaiting administrator approval as an error,
// although it has recorded the registration.
switch daemon.status {
case .enabled, .requiresApproval:
log.notice(.setup, "macOS recorded the registration and reported \(Self.describe(error)); status \(Self.name(daemon.status))")
default:
log.error(.setup, "Registration failed: \(Self.describe(error)); status \(Self.name(daemon.status))")
throw error
}
}
log.notice(.setup, "Registration accepted; status \(Self.name(daemon.status))")
recordAcceptedHelperRegistration()
}
private func recordAcceptedHelperRegistration() {
defaults.set(true, forKey: helperEnrollmentKey)
defaults.removeObject(forKey: replacementKey)
helperRegistrationError = nil
}
/// The caller must freshly verify the old helper is idle before replacing it.
/// Await the asynchronous unregister completion; the synchronous overload
/// returns before the old process has exited and is unsafe for re-registration.
func updateHelper() async throws {
if let problem = installationProblem {
log.error(.setup, "Service update skipped: \(problem)")
throw SetupError(problem)
}
guard daemon.status == .enabled else {
log.error(.setup, "Service update skipped: status is \(Self.name(daemon.status))")
throw SetupError("The background service status changed. Check its setup before updating it.")
}
do {
// Survives a relaunch: a registration this app removed is not a user's choice.
defaults.set(true, forKey: replacementKey)
log.notice(.setup, "Service update: removing the registration of the older service")
do { try await daemon.unregister() }
catch {
defaults.removeObject(forKey: replacementKey)
log.error(.setup, "Service update: removal failed: \(Self.describe(error))")
throw error
}
log.notice(.setup, "Service update: the older registration is removed")
try await registerReplacement()
} catch {
let message = "Caffeine couldn’t update its background service. \(error.localizedDescription)"
helperRegistrationError = message
throw SetupError(message)
}
}
private func registerReplacement() async throws {
var delays = Self.registrationRetryDelays[...]
while true {
do {
// Recheck the signed installation after the asynchronous removal.
return try registerHelper()
} catch {
guard installationProblem == nil, let delay = delays.popFirst() else { throw error }
log.info(.setup, "Service update: registering again in \(delay.components.seconds) s")
try await waitToRegisterAgain(delay)
}
}
}
func openSettings() {
log.notice(.setup, "Opening System Settings, Login Items & Extensions")
openLoginItems()
}
/// One automatic attempt per eligible process launch, independent of card lifetime.
func prepareServices() {
guard !prepared else { return }
if let problem = installationProblem {
log.error(.setup, "Setup is unavailable: \(problem)")
return
}
prepared = true
log.info(.setup, "At launch: \(registrationSummary); accepted before: service \(defaults.bool(forKey: helperEnrollmentKey) ? "yes" : "no")"
+ ", login \(defaults.bool(forKey: enrollmentKey) ? "yes" : "no")"
+ (defaults.bool(forKey: replacementKey) ? "; an interrupted service update is pending" : ""))
enrollLoginIfEligible()
switch daemon.status {
case .enabled, .requiresApproval:
recordAcceptedHelperRegistration()
case .notFound:
// macOS discards the record with a removed bundle. A missing record holds no
// user choice, so an earlier receipt must not block a reinstalled app.
attemptHelperRegistration()
case .notRegistered:
if !defaults.bool(forKey: helperEnrollmentKey) || defaults.bool(forKey: replacementKey) {
attemptHelperRegistration()
}
@unknown default: break
}
}
/// Explicit retry after a failed registration; refresh/wake never retries it.
func retrySetup() {
guard installationProblem == nil else { return }
if helperRegistrationError != nil { attemptHelperRegistration() }
if loginRegistrationFailed { registerLogin() }
}
private func attemptHelperRegistration() {
do {
switch daemon.status {
case .notRegistered, .notFound: try registerHelper()
default: helperRegistrationError = nil
}
} catch {
helperRegistrationError = "Caffeine couldn’t register its background service. \(error.localizedDescription)"
}
}
private func enrollLoginIfEligible() {
switch loginItem.status {
case .enabled, .requiresApproval:
defaults.set(true, forKey: enrollmentKey)
case .notRegistered, .notFound:
if !defaults.bool(forKey: enrollmentKey) { registerLogin() }
@unknown default: break
}
refreshLoginStatus()
}
private func registerLogin() {
do {
if loginItem.status == .notRegistered || loginItem.status == .notFound {
log.notice(.setup, "Registering launch at login (status \(Self.name(loginItem.status)))")
try loginItem.register()
}
defaults.set(true, forKey: enrollmentKey)
loginRegistrationFailed = false
} catch {
loginRegistrationFailed = true
log.error(.setup, "Launch at login could not be registered: \(Self.describe(error))")
}
refreshLoginStatus()
}
func refreshLoginStatus() {
guard prepared else { return }
let status = Self.name(loginItem.status)
if status != loggedLoginStatus {
loggedLoginStatus = status
log.notice(.setup, "Launch at login: \(status)")
}
if loginRegistrationFailed && (loginItem.status == .notRegistered || loginItem.status == .notFound) {
loginMessage = "Caffeine couldn’t enable launch at login. Try again."
loginActionTitle = "Try Again"
return
}
switch loginItem.status {
case .enabled:
defaults.set(true, forKey: enrollmentKey)
loginRegistrationFailed = false
loginMessage = defaults.bool(forKey: explanationKey) ? nil : "Caffeine will launch in the menu bar when you log in, with Keep awake off. Manage this in System Settings."
loginActionTitle = loginMessage == nil ? nil : "Got It"
case .requiresApproval:
defaults.set(true, forKey: enrollmentKey)
loginRegistrationFailed = false
loginMessage = "Launch at login needs your approval in System Settings."
loginActionTitle = "Open System Settings"
case .notRegistered:
loginMessage = "Launch at login is off. You can enable it in System Settings."
loginActionTitle = "Open System Settings"
case .notFound:
loginMessage = "Launch at login hasn’t been set up."
loginActionTitle = "Set Up Login…"
@unknown default:
loginMessage = "Caffeine couldn’t read its login status."
loginActionTitle = "Open System Settings"
}
}
func performLoginAction() {
if loginActionTitle == "Got It" {
defaults.set(true, forKey: explanationKey)
refreshLoginStatus()
} else if loginActionTitle == "Set Up Login…" || loginRegistrationFailed {
guard installationProblem == nil else { return }
registerLogin()
} else { openSettings() }
}
private var installationProblem: String? {
if let installationCheck { return installationCheck() }
let expected = url(fileURLWithPath: "/Applications/Caffeine.app", isDirectory: true)
guard Bundle.main.bundleURL.resolvingSymlinksInPath() == expected else {
return "Install the signed Caffeine app in Applications to enable its background service and launch at login."
}
guard Bundle.main.bundleIdentifier == CaffeineServiceIdentity.appIdentifier,
hasExpectedSignature(at: Bundle.main.bundleURL, identifier: CaffeineServiceIdentity.appIdentifier) else {
return "This build isn’t signed for Caffeine’s background service. Install a signed release to continue."
}
let helper = Bundle.main.bundleURL.appendingPathComponent("Contents/MacOS/CaffeineHelper")
if let problem = Self.bundledServiceProblem(at: Bundle.main.bundleURL) { return problem }
guard hasExpectedSignature(at: helper, identifier: CaffeineServiceIdentity.helperIdentifier) else {
return "Caffeine’s background service has an invalid signature. Reinstall the complete signed app."
}
return nil
}
static func bundledServiceProblem(at bundleURL: URL) -> String? {
let helper = bundleURL.appendingPathComponent("Contents/MacOS/CaffeineHelper")
let plist = bundleURL.appendingPathComponent("Contents/Library/LaunchDaemons/\(CaffeineServiceIdentity.daemonPlist)")
guard FileManager.default.isExecutableFile(atPath: helper.path),
let data = try? Data(contentsOf: plist),
let value = try? PropertyListSerialization.propertyList(from: data, format: nil),
let daemon = value as? [String: Any],
daemon["Label"] as? String == CaffeineServiceIdentity.helperIdentifier,
daemon["BundleProgram"] as? String == "Contents/MacOS/CaffeineHelper",
let services = daemon["MachServices"] as? [String: Bool],
services[CaffeineServiceIdentity.helperIdentifier] == true else {
return "Caffeine’s background service is missing or damaged. Reinstall the complete signed app in Applications."
}
return nil
}
private func hasExpectedSignature(at url: URL, identifier: String) -> Bool {
var code: SecStaticCode?
var requirement: SecRequirement?
guard SecStaticCodeCreateWithPath(url as CFURL, [], &code) == errSecSuccess,
SecRequirementCreateWithString(CaffeineServiceIdentity.signingRequirement(identifier: identifier) as CFString, [], &requirement) == errSecSuccess,
let code, let requirement else { return false }
return SecStaticCodeCheckValidity(code, SecCSFlags(rawValue: kSecCSCheckAllArchitectures), requirement) == errSecSuccess
}
private struct SetupError: Error, LocalizedError {
let errorDescription: String?
init(_ message: String) { errorDescription = message }
}
}