secretive/Sources/Secretive/Controllers/UpdaterCommunicationController.swift

42 lines
1.2 KiB
Swift
Raw Permalink Normal View History

2022-02-12 06:52:45 +00:00
import Foundation
import Combine
import AppKit
import OSLog
import SecretKit
2022-02-12 07:31:03 +00:00
//import SecretiveUpdater
import ServiceManagement
2022-02-12 06:52:45 +00:00
class UpdaterCommunicationController: ObservableObject {
private(set) var updater: UpdaterProtocol? = nil
private var connection: NSXPCConnection? = nil
private var running = false
init() {
}
2022-02-17 06:02:57 +00:00
func installUpdate(url: URL) {
2022-02-12 06:52:45 +00:00
guard !running else { return }
2022-02-17 06:02:57 +00:00
_ = SMLoginItemSetEnabled(Bundle.main.updaterBundleID as CFString, false)
SMLoginItemSetEnabled(Bundle.main.updaterBundleID as CFString, true)
connection = NSXPCConnection(machServiceName: Bundle.main.updaterBundleID)
2022-02-12 06:52:45 +00:00
connection?.remoteObjectInterface = NSXPCInterface(with: UpdaterProtocol.self)
connection?.invalidationHandler = {
Logger().warning("XPC connection invalidated")
}
connection?.resume()
updater = connection?.remoteObjectProxyWithErrorHandler({ error in
Logger().error("\(String(describing: error))")
}) as? UpdaterProtocol
2022-02-17 06:02:57 +00:00
running = true
let existingURL = Bundle.main.bundleURL
2022-02-12 07:31:03 +00:00
Task {
2022-02-17 06:02:57 +00:00
let result = try await updater?.installUpdate(url: url, to: existingURL)
print(result)
2022-02-12 07:31:03 +00:00
}
2022-02-12 06:52:45 +00:00
}
}