secretive/Sources/Secretive/Controllers/AgentCommunicationController.swift

38 lines
1.0 KiB
Swift
Raw Permalink Normal View History

2022-01-03 06:15:18 +00:00
import Foundation
import Combine
import AppKit
2022-01-03 06:52:53 +00:00
import OSLog
2022-01-03 06:15:18 +00:00
import SecretKit
import SecretAgentKitProtocol
protocol AgentCommunicationControllerProtocol: ObservableObject {
2022-01-03 06:52:53 +00:00
var agent: AgentProtocol? { get }
2022-01-03 06:15:18 +00:00
}
class AgentCommunicationController: ObservableObject, AgentCommunicationControllerProtocol {
2022-01-03 06:52:53 +00:00
private(set) var agent: AgentProtocol? = nil
private var connection: NSXPCConnection? = nil
2022-01-03 06:15:18 +00:00
private var running = false
init() {
}
func configure() {
guard !running else { return }
2022-01-03 06:52:53 +00:00
connection = NSXPCConnection(machServiceName: Bundle.main.agentBundleID)
connection?.remoteObjectInterface = NSXPCInterface(with: AgentProtocol.self)
connection?.invalidationHandler = {
Logger().warning("XPC connection invalidated")
}
connection?.resume()
agent = connection?.remoteObjectProxyWithErrorHandler({ error in
Logger().error("\(String(describing: error))")
}) as! AgentProtocol
2022-01-03 06:15:18 +00:00
running = true
}
}