67 lines
3.2 KiB
Swift
67 lines
3.2 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import Foundation
|
|
|
|
/// VCC Plus owns one catalog and installation authority. Only the authenticated,
|
|
/// disk-only request vocabulary crosses this boundary; no machine is exposed.
|
|
final class VCCCompanionService: @unchecked Sendable {
|
|
private let server: CompanionTransport.Server
|
|
|
|
@MainActor
|
|
init(library: SoftwareLibraryModel, configuration: CompanionTransport.Configuration = .vccPlus) throws {
|
|
let authority = CompanionLibraryAuthority(model: library)
|
|
let capabilities = CompanionCapabilities(protocolVersion: CompanionTransport.protocolVersion,
|
|
productVersion: Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "",
|
|
diskEditor: true, diskLibrary: true)
|
|
server = try CompanionTransport.Server(configuration: configuration) { method, payload in
|
|
try await Self.handle(method: method, payload: payload, authority: authority, capabilities: capabilities)
|
|
}
|
|
}
|
|
nonisolated private static func handle(method: String, payload: Data,
|
|
authority: CompanionLibraryAuthority, capabilities: CompanionCapabilities) async throws -> Data {
|
|
try Task.checkCancellation()
|
|
switch method {
|
|
case "capabilities":
|
|
guard payload.isEmpty else { throw CompanionLibraryFailure(message: "Invalid companion capabilities request.") }
|
|
return try JSONEncoder().encode(capabilities)
|
|
case "library":
|
|
guard payload.count <= 32 * 1024 else { throw CompanionLibraryFailure(message: "The library request is too large.") }
|
|
let request = try JSONDecoder().decode(CompanionLibraryRequest.self, from: payload)
|
|
let response = try await authority.handle(request)
|
|
try Task.checkCancellation()
|
|
// A complete catalog snapshot can be several MiB. Its encoding must
|
|
// not occupy the native window's main actor.
|
|
return try await Task.detached(priority: .utility) { try JSONEncoder().encode(response) }.value
|
|
default:
|
|
throw CompanionLibraryFailure(message: "This VCC Plus version does not support that companion operation.")
|
|
}
|
|
}
|
|
func stop() { server.stop() }
|
|
deinit { server.stop() }
|
|
}
|
|
|
|
|
|
import AppKit
|
|
import SwiftUI
|
|
|
|
/// A background catalog request creates no Metal display, polling timer, input
|
|
/// owner or saved machine-window placement. The same window hosts MachineView
|
|
/// only after an explicit activation of VCC Plus.
|
|
struct VCCCompanionDeferredMachineView: NSViewRepresentable {
|
|
let onWindow: @MainActor (NSWindow) -> Void
|
|
func makeNSView(context: Context) -> Anchor {
|
|
let view = Anchor(); view.onWindow = onWindow; return view
|
|
}
|
|
func updateNSView(_ view: Anchor, context: Context) { view.onWindow = onWindow }
|
|
final class Anchor: NSView {
|
|
var onWindow: (@MainActor (NSWindow) -> Void)?
|
|
override func viewDidMoveToWindow() {
|
|
super.viewDidMoveToWindow()
|
|
guard let window else { return }
|
|
DispatchQueue.main.async { [weak self, weak window] in
|
|
guard let window else { return }
|
|
self?.onWindow?(window)
|
|
}
|
|
}
|
|
}
|
|
}
|