import SwiftUI import VirtualCore import Combine import OSLog /// Controls active virtual machine sessions, managing the lifecycle of session windows, controllers, and opening VMs from files or URLs. @MainActor public final class VirtualMachineSessionUIManager: ObservableObject { private let logger = Logger(for: VirtualMachineSessionUIManager.self) public let focusedSessionChanged = PassthroughSubject?, Never>() public static let shared = VirtualMachineSessionUIManager() private let openWindow = OpenCocoaWindowAction.default @Published private(set) var sessions = [VBVirtualMachine.ID: VirtualMachineSessionUI]() private let terminationPresenter = TerminationPresenter() /// Decides whether the app may quit while virtual machines are running. See `prepareForTermination()`. private(set) lazy var terminationCoordinator = SessionTerminationCoordinator( presenter: terminationPresenter, holdTermination: { Self.holdTermination() } ) private init() { terminationPresenter.manager = self } private func createSession(for vm: VBVirtualMachine, library: VMLibraryController, options: VMSessionOptions?) -> VirtualMachineSessionUI { let ui = VirtualMachineSessionUI(with: vm, library: library, options: options) sessions[vm.id] = ui return ui } public func session(for vm: VBVirtualMachine) -> VirtualMachineSessionUI? { sessions[vm.id] } public func sessionForDebugging(_ id: UUID, library: VMLibraryController) async throws -> VirtualMachineSessionUI { let matches = library.virtualMachines.filter { $0.uuid == id } guard matches.count == 1, let vm = matches.first else { throw Failure(matches.isEmpty ? "The selected VM is missing from the library." : "Multiple VMs have this UUID. Duplicate one with VirtualBuddy and set up its scheme again.") } guard !vm.needsInstall, vm.configuration.systemType == .mac, vm.configuration.guestAdditionsEnabled, vm.guestAppSupport == .full else { throw Failure("Xcode debugging requires an installed macOS VM with full Guest App support enabled.") } if session(for: vm) == nil { launch(vm, library: library, options: .default) } guard let ui = session(for: vm) else { throw Failure("Could not open the VM.") } if ui.controller.canResume { try await ui.controller.resume() } else if ui.controller.canStart { await ui.startOrResume() } guard ui.controller.state.isRunning else { throw Failure("Start or resume the selected VM to debug.") } return ui } func sessionIfAvailable(withID id: VBVirtualMachine.ID) -> VirtualMachineSessionUI? { sessions[id] } public func launch(_ vm: VBVirtualMachine, library: VMLibraryController, options: VMSessionOptions?) { guard !vm.needsInstall else { recoverInstallation(for: vm, library: library) return } if let existingSession = session(for: vm) { existingSession.update(with: options) existingSession.bringToFront() } else { launchNewSession(for: vm, library: library, options: options) } } private func presentMACAddressConflictAlert(conflicts: [MACAddressConflict]) async -> MACAddressConflictResolution { let alert = NSAlert() alert.messageText = "Duplicate MAC Address" let formattedConflicts = conflicts .sorted { $0.macAddress < $1.macAddress } .map { "\($0.macAddress) used by \"\($0.vmName)\"" } .joined(separator: "\n") alert.informativeText = "A network device on this virtual machine shares a MAC address with another virtual machine that's already running:\n\n\(formattedConflicts)\n\nRunning multiple virtual machines with the same MAC address may cause network connectivity issues." alert.addButton(withTitle: "Randomize MAC address & Continue") alert.addButton(withTitle: "Continue Anyway") alert.addButton(withTitle: "Cancel") let response: NSApplication.ModalResponse if let window = NSApp?.keyWindow { response = await alert.beginSheetModal(for: window) } else { response = alert.runModal() } switch response { case .alertFirstButtonReturn: return .randomize case .alertSecondButtonReturn: return .continueAnyway default: return .cancel } } private func launchNewSession(for vm: VBVirtualMachine, library: VMLibraryController, options: VMSessionOptions?) { let vmID = vm.id let session = createSession(for: vm, library: library, options: options) session.controller.macAddressConflictHandler = { [weak self] conflicts in guard let self else { return .cancel } return await self.presentMACAddressConflictAlert(conflicts: conflicts) } openWindow(id: vmID) { VirtualMachineSessionView() .environmentObject(session) .environmentObject(session.controller) .environmentObject(library) .environmentObject(self) } onClose: { [weak self] in guard let self else { return } guard let session = sessions[vmID] else { return } session.closeLiveConfiguration() VBMemoryLeakDebugAssertions.vb_objectShouldBeReleasedSoon(session) VBMemoryLeakDebugAssertions.vb_objectShouldBeReleasedSoon(session.controller) sessions[vmID] = nil } } public func recoverInstallation(for vm: VBVirtualMachine, library: VMLibraryController) { let alert = NSAlert() alert.messageText = "Finish Installation" alert.informativeText = "In order to start this virtual machine, its operating system needs to be installed. Would you like to install it now?" alert.addButton(withTitle: "Install") let deleteButton = alert.addButton(withTitle: "Delete") deleteButton.hasDestructiveAction = true alert.addButton(withTitle: "Cancel") let choice = alert.runModal() switch choice { case .alertFirstButtonReturn: launchInstallWizard(restoringAt: vm.bundleURL, library: library) case .alertSecondButtonReturn: library.performMoveToTrash(for: vm) default: break } } public func launchInstallWizard(restoringAt restoreURL: URL? = nil, library: VMLibraryController) { openWindow(animationBehavior: .documentWindow) { VMInstallationWizard(library: library, restoringAt: restoreURL) .environmentObject(library) } } public func launchImportVirtualMachinePanel(library: VMLibraryController) { guard let url = NSOpenPanel.run(accepting: VMImporterRegistry.default.supportedFileTypes, directoryURL: nil, defaultDirectoryKey: "importVirtualMachine", prompt: "Import") else { return } open(fileURL: url, library: library) } private func importVirtualMachine(from path: FilePath, using importer: any VMImporter, library: VMLibraryController) async { do { guard await confirmImport(using: importer) else { throw CancellationError() } logger.debug("Import authorized from \(importer.appName) - \(path)") var model = try await importer.importVirtualMachine(from: path, into: library) model.metadata.importedFromAppName = importer.appName try model.saveMetadata() library.reload() open(fileURL: model.bundleURL, library: library) } catch is CancellationError { logger.notice("Import cancelled") } catch { NSApp.presentError(error) } } private var importTask: Task? private func beginImportVirtualMachine(from path: FilePath, using importer: any VMImporter, library: VMLibraryController) { importTask = Task { await importVirtualMachine(from: path, using: importer, library: library) } } func confirmImport(using importer: any VMImporter) async -> Bool { #if DEBUG guard !Self.testImportSkipConfirmation else { return true } #endif return await NSAlert .runConfirmationAlert( title: "Import From \(importer.appName)?", message: "VirtualBuddy will attempt to import your virtual machine from \(importer.appName). All data from this virtual machine in \(importer.appName) will be copied into your VirtualBuddy library. Would you like to proceed?", continueButtonTitle: "Import", cancelButtonTitle: "Cancel", continueButtonIsDefault: true ) } } // MARK: - File Opening @MainActor public extension VirtualMachineSessionUIManager { func open(fileURL: URL, library: VMLibraryController) { do { let values = try fileURL.resourceValues(forKeys: [.contentTypeKey]) switch values.contentType { case .none: return case .virtualBuddyVM: handleOpenVirtualMachineFile(fileURL, library: library, options: nil) case .virtualBuddySavedState: handleOpenSavedStateFile(fileURL, library: library) default: let path = FilePath(fileURL) if let importer = VMImporterRegistry.default.importer(for: path) { beginImportVirtualMachine(from: path, using: importer, library: library) } else { throw Failure("Unsupported file type \(values.contentType?.identifier ?? "?")") } } } catch { logger.error("Error loading virtual machine from file at \(fileURL.path, privacy: .public): \(error, privacy: .public)") } } } @MainActor private extension VirtualMachineSessionUIManager { func handleOpenVirtualMachineFile(_ url: URL, library: VMLibraryController, options: VMSessionOptions?) { if let loadedVM = library.virtualMachines.first(where: { $0.bundleURL.path == url.path }) { launch(loadedVM, library: library, options: options) } else { do { let vm = try VBVirtualMachine(bundleURL: url) launch(vm, library: library, options: options) } catch { let alert = NSAlert(error: error) alert.runModal() } } } /// Saved states created by earlier versions can't be resumed safely, so they're only ever shown, never opened. func handleOpenSavedStateFile(_ url: URL, library: VMLibraryController) { SavedSessionPrompts.presentLegacySavedState(at: url) } } #if DEBUG // MARK: - Import Testing (Debug) @MainActor extension VirtualMachineSessionUIManager { static let testImportSkipConfirmation = UserDefaults.standard.bool(forKey: "VBTestImportSkipConfirmation") static let testImportVMPath: FilePath? = UserDefaults.standard.string(forKey: "VBTestImport").flatMap { FilePath($0) } public func testImportVMIfEnabled(library: VMLibraryController) { guard let path = Self.testImportVMPath else { return } open(fileURL: path.url, library: library) } } #endif