|
| 1 | +//! System clipboard integration. |
| 2 | +//! |
| 3 | +//! Provides thread-safe clipboard access using a process-global singleton. |
| 4 | +//! On Windows, `arboard::Clipboard::new()` initializes COM. When multiple |
| 5 | +//! threads call `Clipboard::new()` concurrently (e.g., during parallel test |
| 6 | +//! execution), the concurrent COM initialization can corrupt the heap, |
| 7 | +//! causing `STATUS_HEAP_CORRUPTION` (0xc0000374). |
| 8 | +//! |
| 9 | +//! This module solves the problem by creating exactly one `Clipboard` |
| 10 | +//! instance for the entire process, serializing all access through a `Mutex`. |
| 11 | +//! Clipboard operations are infrequent (only on user copy/cut/paste), so |
| 12 | +//! the serialization has no practical performance impact. |
| 13 | +
|
| 14 | +use std::sync::{Mutex, OnceLock}; |
| 15 | + |
| 16 | +/// Process-global clipboard singleton. |
| 17 | +/// |
| 18 | +/// `OnceLock` ensures `Clipboard::new()` is called exactly once. |
| 19 | +/// `Mutex` serializes all subsequent access. |
| 20 | +static CLIPBOARD: OnceLock<Mutex<Option<arboard::Clipboard>>> = OnceLock::new(); |
| 21 | + |
| 22 | +/// Runs a closure with access to the global clipboard context. |
| 23 | +/// |
| 24 | +/// Returns `None` if the clipboard is unavailable (headless environments, |
| 25 | +/// CI, SSH sessions without a clipboard provider) or if the mutex is poisoned. |
| 26 | +fn with_clipboard<F, R>(f: F) -> Option<R> |
| 27 | +where |
| 28 | + F: FnOnce(&mut arboard::Clipboard) -> R, |
| 29 | +{ |
| 30 | + let mutex = CLIPBOARD.get_or_init(|| Mutex::new(arboard::Clipboard::new().ok())); |
| 31 | + let mut guard = mutex.lock().ok()?; |
| 32 | + guard.as_mut().map(f) |
| 33 | +} |
| 34 | + |
| 35 | +/// Attempt to write text to the system clipboard. |
| 36 | +/// |
| 37 | +/// Errors are silently ignored — this is best-effort. Falls back gracefully |
| 38 | +/// in headless environments (CI, SSH) where no clipboard provider exists. |
| 39 | +pub(crate) fn system_clipboard_set(text: &str) { |
| 40 | + with_clipboard(|cb| { |
| 41 | + let _ = cb.set_text(text); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +/// Attempt to read text from the system clipboard. |
| 46 | +/// |
| 47 | +/// Returns `None` if the clipboard is unavailable or doesn't contain text. |
| 48 | +pub(crate) fn system_clipboard_get() -> Option<String> { |
| 49 | + with_clipboard(|cb| cb.get_text().ok()) |
| 50 | + .flatten() |
| 51 | + .filter(|s| !s.is_empty()) |
| 52 | +} |
| 53 | + |
| 54 | +#[cfg(test)] |
| 55 | +mod tests { |
| 56 | + use super::*; |
| 57 | + |
| 58 | + #[test] |
| 59 | + fn test_clipboard_set_does_not_panic() { |
| 60 | + // Should not panic even in headless CI environments |
| 61 | + system_clipboard_set("test"); |
| 62 | + } |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn test_clipboard_get_does_not_panic() { |
| 66 | + // Should return None in headless CI, Some in desktop environments |
| 67 | + let _ = system_clipboard_get(); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn test_clipboard_roundtrip() { |
| 72 | + // Set and get — may return None in headless environments. |
| 73 | + // Cannot assert exact content because the system clipboard is |
| 74 | + // global and other parallel tests may write to it. |
| 75 | + system_clipboard_set("roundtrip_test"); |
| 76 | + let _ = system_clipboard_get(); |
| 77 | + } |
| 78 | + |
| 79 | + #[test] |
| 80 | + fn test_with_clipboard_returns_none_gracefully() { |
| 81 | + // Verify the with_clipboard wrapper handles unavailable clipboard |
| 82 | + let result = with_clipboard(|cb| cb.get_text().ok()); |
| 83 | + // Result is either Some(Some/None) or None — both are fine |
| 84 | + let _ = result; |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn test_repeated_access_same_thread() { |
| 89 | + // Verify that repeated access reuses the singleton without issues. |
| 90 | + for _ in 0..100 { |
| 91 | + system_clipboard_set("repeated"); |
| 92 | + let _ = system_clipboard_get(); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments