33 lines
1.3 KiB
Swift
33 lines
1.3 KiB
Swift
import Foundation
|
|
|
|
final class SMARTService: NSObject, SMARTServiceProtocol {
|
|
func readSMART(withReply reply: @escaping (NSNumber?, NSNumber?) -> Void) {
|
|
Task {
|
|
let result = await DiskSMARTReader.readReport()
|
|
reply(result?.remainingLife.map(NSNumber.init(value:)), result?.powerOnHours.map(NSNumber.init(value:)))
|
|
}
|
|
}
|
|
}
|
|
|
|
final class ServiceDelegate: NSObject, NSXPCListenerDelegate {
|
|
// Bind the peer to the containing app's actual designated signature, including ad-hoc builds.
|
|
private let requirement: String? = {
|
|
let host = Bundle.main.bundleURL.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
|
|
return CodeIdentity.requirement(at: host)
|
|
}()
|
|
|
|
func listener(_ listener: NSXPCListener, shouldAcceptNewConnection connection: NSXPCConnection) -> Bool {
|
|
guard connection.effectiveUserIdentifier == getuid(), let requirement else { return false }
|
|
connection.setCodeSigningRequirement(requirement)
|
|
connection.exportedInterface = NSXPCInterface(with: SMARTServiceProtocol.self)
|
|
connection.exportedObject = SMARTService()
|
|
connection.resume()
|
|
return true
|
|
}
|
|
}
|
|
|
|
let delegate = ServiceDelegate()
|
|
let listener = NSXPCListener.service()
|
|
listener.delegate = delegate
|
|
listener.resume()
|