Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

feat(remote-desktop): 增加防窥屏、被控端静音和双向剪贴板同步 (#4369)

Dash397e0dce8d3b
diff --git a/apps/desktop/forge.config.ts b/apps/desktop/forge.config.ts
index eac09f28f7..fe7ace53cd 100644
--- a/apps/desktop/forge.config.ts
+++ b/apps/desktop/forge.config.ts
@@ -1107,7 +1107,8 @@ function buildRemoteDesktopInput(platform: ForgePlatform, arch: ForgeArch): void
const result = spawnSync('xcrun', ['clang', path.join(__dirname, 'native', 'remote-desktop', 'macos-capture.m'),
...captureArch, '-mmacosx-version-min=10.15', '-fobjc-arc', '-fblocks', '-O2',
'-framework', 'Foundation', '-framework', 'AppKit', '-framework', 'CoreGraphics', '-framework', 'CoreImage',
- '-framework', 'IOSurface', '-framework', 'ImageIO', '-framework', 'IOKit', '-o', capture], { stdio: 'inherit' });
+ '-framework', 'IOSurface', '-framework', 'ImageIO', '-framework', 'IOKit',
+ '-weak_framework', 'ScreenCaptureKit', '-framework', 'CoreMedia', '-framework', 'CoreVideo', '-o', capture], { stdio: 'inherit' });
if (result.error || result.status !== 0) throw new Error('Remote desktop capture build failed');
fs.chmodSync(capture, 0o755);
} else if (process.platform === 'win32' && platform === 'win32') {
diff --git a/apps/desktop/native/remote-desktop/macos-capture.m b/apps/desktop/native/remote-desktop/macos-capture.m
index 5eb6924e8f..bf37b2c60a 100644
--- a/apps/desktop/native/remote-desktop/macos-capture.m
+++ b/apps/desktop/native/remote-desktop/macos-capture.m
@@ -4,6 +4,9 @@
#import <CoreImage/CoreImage.h>
#import <IOSurface/IOSurface.h>
#import <IOKit/pwr_mgt/IOPMLib.h>
+#import <ScreenCaptureKit/ScreenCaptureKit.h>
+#import <CoreMedia/CoreMedia.h>
+#import <CoreVideo/CoreVideo.h>
#import <dlfcn.h>
#import <signal.h>
#import <unistd.h>
@@ -21,6 +24,19 @@ typedef CFTypeRef (*CreateStream)(CGDirectDisplayID, size_t, size_t, int32_t,
CFDictionaryRef, dispatch_queue_t, FrameHandler);
typedef CGError (*StartStream)(CFTypeRef);
+@interface PrivacyCaptureOutput : NSObject <SCStreamOutput, SCStreamDelegate>
+@property(copy) FrameHandler handler;
+@property(strong) SCStream *stream;
+@end
+@implementation PrivacyCaptureOutput
+- (void)stream:(SCStream *)stream didOutputSampleBuffer:(CMSampleBufferRef)sample ofType:(SCStreamOutputType)type {
+ if (type != SCStreamOutputTypeScreen || !CMSampleBufferIsValid(sample)) return;
+ CVPixelBufferRef buffer = CMSampleBufferGetImageBuffer(sample);
+ if (buffer) self.handler(0, 0, CVPixelBufferGetIOSurface(buffer), NULL);
+}
+- (void)stream:(SCStream *)stream didStopWithError:(NSError *)error { _exit(5); }
+@end
+
static NSDictionary *readCursor(CGDirectDisplayID display) {
// Read on the main queue. currentSystemCursor returns the actual global shape,
// including cursors supplied by other applications (not this helper's cursor).
@@ -57,8 +73,10 @@ static NSDictionary *readCursor(CGDirectDisplayID display) {
int main(int argc, const char *argv[]) {
@autoreleasepool {
- BOOL overlay = argc == 5 && strcmp(argv[2], "cursor-overlay") == 0;
- if (argc != 2 && !overlay) return 2;
+ BOOL overlay = (argc == 5 || argc == 6) && strcmp(argv[2], "cursor-overlay") == 0;
+ if (argc != 2 && argc != 3 && !overlay) return 2;
+ NSString *excluded = (overlay && argc == 6) || (!overlay && argc == 3)
+ ? [NSString stringWithUTF8String:argv[argc - 1]] : @"";
int fps = overlay && strcmp(argv[3], "60") == 0 ? 60 : (overlay ? 30 : 15);
double quality = overlay ? atof(argv[4]) : 0.55;
if (quality < 0.1 || quality > 1) return 2;
@@ -75,7 +93,7 @@ int main(int argc, const char *argv[]) {
StartStream start = (StartStream)dlsym(RTLD_DEFAULT, "CGDisplayStreamStart");
const CFStringRef *intervalKey = dlsym(RTLD_DEFAULT, "kCGDisplayStreamMinimumFrameTime");
const CFStringRef *cursorKey = dlsym(RTLD_DEFAULT, "kCGDisplayStreamShowCursor");
- if (!create || !start || !intervalKey || !cursorKey) return 4;
+ if (!excluded.length && (!create || !start || !intervalKey || !cursorKey)) return 4;
CGDirectDisplayID display = (uint32_t)value;
size_t width = CGDisplayPixelsWide(display), height = CGDisplayPixelsHigh(display);
if (!width || !height) return 2;
@@ -116,9 +134,7 @@ int main(int argc, const char *argv[]) {
bytes += count; remaining -= count;
}
};
- CFTypeRef stream = create(display, width, height, 'BGRA',
- (__bridge CFDictionaryRef)@{(__bridge NSString *)*intervalKey: @(1.0/fps), (__bridge NSString *)*cursorKey: separateCursor ? @NO : @YES}, queue,
- ^(int32_t status, uint64_t timestamp, IOSurfaceRef surface, const void *update) {
+ FrameHandler receiveFrame = ^(int32_t status, uint64_t timestamp, IOSurfaceRef surface, const void *update) {
@autoreleasepool {
if (status == 3) _exit(5); // stopped; never serve the previous session's frame
if (status == 2) { latest = nil; return; } // blank display
@@ -143,8 +159,45 @@ int main(int argc, const char *argv[]) {
latest = jpeg.length <= limit ? jpeg : nil;
reply();
}
- });
- if (!stream || start(stream) != kCGErrorSuccess) return 5;
+ };
+ if (excluded.length) {
+ NSArray<NSString *> *ids = [excluded componentsSeparatedByString:@","];
+ // Explicit exclusion is mandatory: NSWindowSharingNone alone is ignored
+ // by modern ScreenCaptureKit. Never fall back to an unfiltered stream.
+ [SCShareableContent getShareableContentExcludingDesktopWindows:NO onScreenWindowsOnly:NO
+ completionHandler:^(SCShareableContent *content, NSError *error) {
+ if (error) _exit(5);
+ SCDisplay *selected = nil;
+ for (SCDisplay *candidate in content.displays) if (candidate.displayID == display) selected = candidate;
+ NSMutableArray<SCWindow *> *windows = [NSMutableArray array];
+ for (SCWindow *window in content.windows)
+ if ([ids containsObject:[NSString stringWithFormat:@"%u", window.windowID]]) [windows addObject:window];
+ // ScreenCaptureKit may omit windows belonging to another display from
+ // this display's shareable-content snapshot. A partial match is
+ // still valid; an empty match is fail-closed.
+ if (!selected || windows.count == 0) _exit(5);
+ SCContentFilter *filter = [[SCContentFilter alloc] initWithDisplay:selected excludingWindows:windows];
+ SCStreamConfiguration *config = [SCStreamConfiguration new];
+ config.width = width; config.height = height;
+ config.minimumFrameInterval = CMTimeMake(1, fps);
+ config.showsCursor = !separateCursor;
+ config.pixelFormat = kCVPixelFormatType_32BGRA;
+ config.queueDepth = 3;
+ PrivacyCaptureOutput *output = [PrivacyCaptureOutput new];
+ output.handler = receiveFrame;
+ output.stream = [[SCStream alloc] initWithFilter:filter configuration:config delegate:output];
+ NSError *failure = nil;
+ if (![output.stream addStreamOutput:output type:SCStreamOutputTypeScreen sampleHandlerQueue:queue error:&failure]) _exit(5);
+ // Intentional process-lifetime ownership. Parent EOF/watchdog exits
+ // the helper and releases every capture resource together.
+ CFRetain((__bridge CFTypeRef)output);
+ [output.stream startCaptureWithCompletionHandler:^(NSError *failure) { if (failure) _exit(5); }];
+ }];
+ } else {
+ CFTypeRef stream = create(display, width, height, 'BGRA',
+ (__bridge CFDictionaryRef)@{(__bridge NSString *)*intervalKey: @(1.0/fps), (__bridge NSString *)*cursorKey: separateCursor ? @NO : @YES}, queue, receiveFrame);
+ if (!stream || start(stream) != kCGErrorSuccess) return 5;
+ }
signal(SIGPIPE, SIG_IGN);
// stdin is a private parent pipe: one byte asks for one latest frame. EOF
// and a stalled parent both terminate capture without retaining any pixels.
diff --git a/apps/desktop/native/remote-desktop/macos-input.swift b/apps/desktop/native/remote-desktop/macos-input.swift
index 035c02c2e4..8d153f0666 100644
--- a/apps/desktop/native/remote-desktop/macos-input.swift
+++ b/apps/desktop/native/remote-desktop/macos-input.swift
@@ -185,6 +185,37 @@ func apply(_ event: [String: Any]) {
}
}
}
+
+// Armed -> draining -> confirming. Draining lets the input helper release its
+// pressed keys/buttons before Main opens a dialog; confirming rejects injection.
+// Held initiating keys/buttons are consumed through release (including repeats).
+struct PrivacyInputGate {
+ var phase = 0
+ var swallowed = Set<Int64>()
+ mutating func consume(physical: Bool, press: Int64?, down: Bool, trigger: Bool, scroll: Bool = false) -> (Bool, Bool) {
+ if !physical { return (phase == 2, false) }
+ if scroll {
+ let notify = phase == 0
+ if notify { phase = 1 }
+ return (true, notify)
+ }
+ if let press = press, swallowed.contains(press) {
+ if !down { swallowed.remove(press) }
+ return (true, false)
+ }
+ if phase == 0 && trigger {
+ phase = 1
+ if let press = press, down { swallowed.insert(press) }
+ return (true, true)
+ }
+ if phase == 1 {
+ if let press = press, down { swallowed.insert(press) }
+ return (true, false)
+ }
+ return (false, false)
+ }
+}
+
#if !DESKTOP_INPUT_TEST
if CommandLine.arguments.contains("--check") {
print(AXIsProcessTrusted() ? "ready" : "permission"); exit(0)
@@ -221,6 +252,69 @@ if CommandLine.arguments.count == 2 && CommandLine.arguments[1] == "--lock-scree
exit(2)
}
guard AXIsProcessTrusted() else { print("permission"); fflush(stdout); exit(2) }
+if CommandLine.arguments == [CommandLine.arguments[0], "--privacy-input"] {
+ var heartbeat = Date()
+ var previousApp: NSRunningApplication?
+ let events: [CGEventType] = [.keyDown, .keyUp, .flagsChanged, .leftMouseDown, .leftMouseUp,
+ .rightMouseDown, .rightMouseUp, .otherMouseDown, .otherMouseUp, .mouseMoved,
+ .leftMouseDragged, .rightMouseDragged, .otherMouseDragged, .scrollWheel]
+ let mask = events.reduce(CGEventMask(0)) { $0 | (CGEventMask(1) << $1.rawValue) }
+ // Context remains alive on this run loop until process exit; callbacks never
+ // wait on stdin, Main or network and never print event contents.
+ let context = UnsafeMutablePointer<PrivacyInputGate>.allocate(capacity: 1)
+ context.initialize(to: PrivacyInputGate())
+ guard let tap = CGEvent.tapCreate(tap: .cgSessionEventTap, place: .headInsertEventTap,
+ options: .defaultTap, eventsOfInterest: mask, callback: { _, type, event, info in
+ if type == .tapDisabledByTimeout || type == .tapDisabledByUserInput {
+ print("error"); fflush(stdout); exit(2)
+ }
+ let state = info!.assumingMemoryBound(to: PrivacyInputGate.self)
+ let physical = event.getIntegerValueField(.eventSourceUnixProcessID) == 0
+ && event.getIntegerValueField(.eventSourceStateID) == Int64(CGEventSourceStateID.hidSystemState.rawValue)
+ let key = type == .keyDown || type == .keyUp
+ let mouseDown = [.leftMouseDown, .rightMouseDown, .otherMouseDown].contains(type)
+ let mouseUp = [.leftMouseUp, .rightMouseUp, .otherMouseUp].contains(type)
+ let press: Int64? = key ? event.getIntegerValueField(.keyboardEventKeycode)
+ : (mouseDown || mouseUp ? 100000 + event.getIntegerValueField(.mouseEventButtonNumber) : nil)
+ let (consume, notify) = state.pointee.consume(physical: physical, press: press,
+ down: type == .keyDown || mouseDown, trigger: type == .keyDown || mouseDown || type == .flagsChanged, scroll: type == .scrollWheel)
+ if notify { print("local-input"); fflush(stdout) }
+ return consume ? nil : Unmanaged.passUnretained(event)
+ }, userInfo: context), let runSource = CFMachPortCreateRunLoopSource(nil, tap, 0) else {
+ print("error"); fflush(stdout); exit(2)
+ }
+ CFRunLoopAddSource(CFRunLoopGetMain(), runSource, .commonModes)
+ CGEvent.tapEnable(tap: tap, enable: true)
+ print("ready"); fflush(stdout)
+ DispatchQueue.global().async {
+ while let command = readLine() {
+ guard command.utf8.count <= 16 else { exit(2) }
+ DispatchQueue.main.async {
+ heartbeat = Date()
+ switch command {
+ case "ping": break
+ case "confirm":
+ previousApp = NSWorkspace.shared.frontmostApplication
+ context.pointee.phase = 2; print("confirmed"); fflush(stdout)
+ case "resume":
+ _ = previousApp?.activate(options: [.activateIgnoringOtherApps])
+ previousApp = nil
+ context.pointee.phase = 0; print("ready"); fflush(stdout)
+ default: exit(2)
+ }
+ }
+ }
+ exit(0) // Process exit removes the tap, even if Main died with a dialog up.
+ }
+ let timer = DispatchSource.makeTimerSource(queue: .main)
+ timer.schedule(deadline: .now() + 1, repeating: 1)
+ timer.setEventHandler {
+ if Date().timeIntervalSince(heartbeat) > 5 || inputCaller.code() == nil || !AXIsProcessTrusted() { exit(2) }
+ }
+ timer.resume()
+ CFRunLoopRun()
+ exit(0)
+}
print("ready"); fflush(stdout)
let watchdog = DispatchSource.makeTimerSource(queue: .global())
watchdog.schedule(deadline: .now() + 1, repeating: 1)
diff --git a/apps/desktop/native/remote-desktop/macos-input.test.swift b/apps/desktop/native/remote-desktop/macos-input.test.swift
index 1bf745ebf4..e4f280e05c 100644
--- a/apps/desktop/native/remote-desktop/macos-input.test.swift
+++ b/apps/desktop/native/remote-desktop/macos-input.test.swift
@@ -27,3 +27,25 @@ for down in [true, false] {
precondition(!control.flags.contains(.maskSecondaryFn), "Control must not inherit F11's Fn state")
}
print("native keyboard flags passed")
+
+for press: Int64 in [36, 100000] {
+ var gate = PrivacyInputGate()
+ precondition(gate.consume(physical: false, press: press, down: true, trigger: true) == (false, false))
+ precondition(gate.consume(physical: true, press: press, down: true, trigger: true) == (true, true))
+ precondition(gate.consume(physical: false, press: press, down: false, trigger: false) == (false, false))
+ gate.phase = 2
+ precondition(gate.consume(physical: false, press: press, down: true, trigger: true) == (true, false))
+ precondition(gate.consume(physical: true, press: press, down: true, trigger: true) == (true, false))
+ precondition(gate.consume(physical: true, press: press, down: false, trigger: false) == (true, false))
+ precondition(gate.consume(physical: true, press: press, down: true, trigger: true) == (false, false))
+ gate.phase = 0
+ precondition(gate.consume(physical: false, press: press, down: true, trigger: true) == (false, false))
+}
+
+var wheelGate = PrivacyInputGate()
+for phase in [0, 1, 2, 0] {
+ wheelGate.phase = phase
+ precondition(wheelGate.consume(physical: false, press: nil, down: false, trigger: false, scroll: true) == (phase == 2, false))
+ precondition(wheelGate.consume(physical: true, press: nil, down: false, trigger: false, scroll: true) == (true, phase == 0))
+ precondition(wheelGate.swallowed.isEmpty)
+}
diff --git a/apps/desktop/native/remote-desktop/windows-input/src/main.rs b/apps/desktop/native/remote-desktop/windows-input/src/main.rs
index 4bdf28bb19..cd5b9091da 100644
--- a/apps/desktop/native/remote-desktop/windows-input/src/main.rs
+++ b/apps/desktop/native/remote-desktop/windows-input/src/main.rs
@@ -9,6 +9,7 @@ use windows_sys::Win32::Foundation::GetLastError;
use windows_sys::Win32::UI::{HiDpi::*, Input::KeyboardAndMouse::*, WindowsAndMessaging::*};
mod desktop;
mod selection;
+mod privacy;
static FAILED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
fn send(input: INPUT) -> bool {
@@ -136,6 +137,10 @@ fn release(keys: &mut HashSet<u16>, buttons: &mut HashSet<u64>) {
}
}
fn main() {
+ if std::env::args().nth(1).as_deref() == Some("--privacy-input") {
+ privacy::run();
+ return;
+ }
if matches!(std::env::args().nth(1).as_deref(), Some("--clipboard-selection" | "--clipboard-content-selection")) {
match selection::read(std::env::args().nth(1).as_deref() == Some("--clipboard-content-selection")) {
Ok(text) => println!("{}", serde_json::json!({"text":text})),
diff --git a/apps/desktop/native/remote-desktop/windows-input/src/privacy.rs b/apps/desktop/native/remote-desktop/windows-input/src/privacy.rs
new file mode 100644
index 0000000000..03fbdf41bf
--- /dev/null
+++ b/apps/desktop/native/remote-desktop/windows-input/src/privacy.rs
@@ -0,0 +1,220 @@
+//! Ephemeral physical-input gate, on the user's desktop, never the service's.
+//! No event contents leave this process. Closing stdin or losing its heartbeat
+//! exits the message loop and removes both hooks.
+use std::{
+ cell::RefCell,
+ collections::HashSet,
+ io::{self, BufRead, Read, Write},
+ time::{Duration, Instant},
+};
+use windows_sys::Win32::{
+ Foundation::*, System::Threading::GetCurrentThreadId, UI::WindowsAndMessaging::*,
+};
+
+#[derive(Default)]
+struct Gate {
+ phase: u8, // 0 armed, 1 draining input helper, 2 local dialog
+ swallowed: HashSet<u32>,
+}
+impl Gate {
+ fn consume(
+ &mut self,
+ physical: bool,
+ press: Option<u32>,
+ down: bool,
+ scroll: bool,
+ ) -> (bool, bool) {
+ if !physical {
+ return (self.phase == 2, false);
+ }
+ if scroll {
+ let notify = self.phase == 0;
+ if notify {
+ self.phase = 1;
+ }
+ return (true, notify);
+ }
+ if let Some(press) = press {
+ if self.swallowed.contains(&press) {
+ if !down {
+ self.swallowed.remove(&press);
+ }
+ return (true, false);
+ }
+ }
+ if self.phase == 0 && down {
+ self.phase = 1;
+ if let Some(press) = press {
+ self.swallowed.insert(press);
+ }
+ return (true, true);
+ }
+ if self.phase == 1 {
+ if down {
+ if let Some(press) = press {
+ self.swallowed.insert(press);
+ }
+ }
+ return (true, false);
+ }
+ (false, false)
+ }
+}
+thread_local! { static GATE: RefCell<Gate> = RefCell::new(Gate::default()); }
+fn consume(physical: bool, press: Option<u32>, down: bool, scroll: bool) -> bool {
+ let (consume, notify) =
+ GATE.with(|gate| gate.borrow_mut().consume(physical, press, down, scroll));
+ if notify {
+ println!("local-input");
+ io::stdout().flush().ok();
+ }
+ consume
+}
+unsafe extern "system" fn keyboard(code: i32, w: WPARAM, l: LPARAM) -> LRESULT {
+ if code >= 0 {
+ let event = &*(l as *const KBDLLHOOKSTRUCT);
+ let down = w as u32 == WM_KEYDOWN || w as u32 == WM_SYSKEYDOWN;
+ if consume(
+ event.flags & LLKHF_INJECTED == 0,
+ Some(event.vkCode),
+ down,
+ false,
+ ) {
+ return 1;
+ }
+ }
+ CallNextHookEx(std::ptr::null_mut(), code, w, l)
+}
+unsafe extern "system" fn mouse(code: i32, w: WPARAM, l: LPARAM) -> LRESULT {
+ if code >= 0 {
+ let event = &*(l as *const MSLLHOOKSTRUCT);
+ let (press, down) = match w as u32 {
+ WM_LBUTTONDOWN => (Some(0x10000), true),
+ WM_LBUTTONUP => (Some(0x10000), false),
+ WM_RBUTTONDOWN => (Some(0x10001), true),
+ WM_RBUTTONUP => (Some(0x10001), false),
+ WM_MBUTTONDOWN => (Some(0x10002), true),
+ WM_MBUTTONUP => (Some(0x10002), false),
+ WM_XBUTTONDOWN => (Some(0x10003 + (event.mouseData >> 16)), true),
+ WM_XBUTTONUP => (Some(0x10003 + (event.mouseData >> 16)), false),
+ _ => (None, false),
+ };
+ if consume(
+ event.flags & LLMHF_INJECTED == 0,
+ press,
+ down,
+ matches!(w as u32, WM_MOUSEWHEEL | WM_MOUSEHWHEEL),
+ ) {
+ return 1;
+ }
+ }
+ CallNextHookEx(std::ptr::null_mut(), code, w, l)
+}
+pub fn run() {
+ unsafe {
+ let mut message: MSG = std::mem::zeroed();
+ // Create the queue before the stdin reader posts commands to it.
+ PeekMessageW(&mut message, std::ptr::null_mut(), 0, 0, PM_NOREMOVE);
+ let thread = GetCurrentThreadId();
+ let keyboard_hook =
+ SetWindowsHookExW(WH_KEYBOARD_LL, Some(keyboard), std::ptr::null_mut(), 0);
+ let mouse_hook = SetWindowsHookExW(WH_MOUSE_LL, Some(mouse), std::ptr::null_mut(), 0);
+ if keyboard_hook.is_null() || mouse_hook.is_null() {
+ std::process::exit(2);
+ }
+ const COMMAND: u32 = WM_APP + 1;
+ std::thread::spawn(move || {
+ let mut stdin = io::stdin().lock();
+ loop {
+ let mut bytes = Vec::new();
+ match stdin.by_ref().take(17).read_until(b'\n', &mut bytes) {
+ Ok(0) | Err(_) => break,
+ Ok(_) => {
+ let command = match bytes.as_slice() {
+ b"ping\n" => 0,
+ b"confirm\n" => 1,
+ b"resume\n" => 2,
+ _ => break,
+ };
+ if PostThreadMessageW(thread, COMMAND, command, 0) == 0 {
+ break;
+ }
+ }
+ }
+ }
+ PostThreadMessageW(thread, WM_QUIT, 0, 0);
+ });
+ if SetTimer(std::ptr::null_mut(), 1, 1000, None) == 0 {
+ std::process::exit(2);
+ }
+ let mut last_seen = Instant::now();
+ let mut previous_window = std::ptr::null_mut();
+ println!("ready");
+ io::stdout().flush().ok();
+ while GetMessageW(&mut message, std::ptr::null_mut(), 0, 0) > 0 {
+ if message.message == COMMAND {
+ last_seen = Instant::now();
+ match message.wParam {
+ 1 => {
+ previous_window = GetForegroundWindow();
+ GATE.with(|gate| gate.borrow_mut().phase = 2);
+ println!("confirmed");
+ }
+ 2 => {
+ if !previous_window.is_null() {
+ SetForegroundWindow(previous_window);
+ }
+ GATE.with(|gate| gate.borrow_mut().phase = 0);
+ println!("ready");
+ }
+ _ => (),
+ }
+ io::stdout().flush().ok();
+ } else if message.message == WM_TIMER && last_seen.elapsed() > Duration::from_secs(5) {
+ break;
+ }
+ TranslateMessage(&message);
+ DispatchMessageW(&message);
+ }
+ UnhookWindowsHookEx(keyboard_hook);
+ UnhookWindowsHookEx(mouse_hook);
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::Gate;
+ #[test]
+ fn injected_input_passes_until_dialog_and_held_local_key_cannot_confirm() {
+ let mut gate = Gate::default();
+ assert_eq!(gate.consume(false, Some(13), true, false), (false, false));
+ assert_eq!(gate.consume(true, Some(13), true, false), (true, true));
+ assert_eq!(gate.consume(false, Some(13), false, false), (false, false)); // drain releases
+ gate.phase = 2;
+ assert_eq!(gate.consume(false, Some(13), true, false), (true, false));
+ assert_eq!(gate.consume(true, Some(13), true, false), (true, false)); // held Enter
+ assert_eq!(gate.consume(true, Some(13), false, false), (true, false));
+ assert_eq!(gate.consume(true, Some(13), true, false), (false, false)); // fresh local press
+ gate.phase = 0;
+ assert_eq!(gate.consume(false, Some(13), true, false), (false, false));
+ }
+}
+
+#[cfg(test)]
+mod wheel_tests {
+ use super::Gate;
+ #[test]
+ fn physical_wheel_triggers_once_and_never_reaches_the_covered_application() {
+ let mut gate = Gate::default();
+ assert_eq!(gate.consume(false, None, false, true), (false, false));
+ assert_eq!(gate.consume(true, None, false, true), (true, true));
+ assert_eq!(gate.consume(true, None, false, true), (true, false));
+ assert_eq!(gate.consume(false, None, false, true), (false, false));
+ gate.phase = 2;
+ assert_eq!(gate.consume(true, None, false, true), (true, false));
+ assert_eq!(gate.consume(false, None, false, true), (true, false));
+ gate.phase = 0;
+ assert_eq!(gate.consume(true, None, false, true), (true, true));
+ assert!(gate.swallowed.is_empty());
+ }
+}
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts
index 7cc87e2708..03ec766a2a 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts
@@ -175,6 +175,7 @@ vi.mock('../../utils/ipcValidate', () => ({
},
}));
import { registerRemoteDesktopIpc } from '../index';
+import { PrivacyScreen } from '../privacyScreen';
const event = (owner = h.owner) => ({ sender: owner, senderFrame: owner.mainFrame });
const flush = async () => {
for (let i = 0; i < 12; i++) await Promise.resolve();
@@ -203,12 +204,19 @@ beforeEach(() => {
registerRemoteDesktopIpc();
});
afterEach(() => {
+ vi.restoreAllMocks();
h.deps.stopVideo();
vi.clearAllTimers();
vi.useRealTimers();
vi.unstubAllGlobals();
});
+it.each([false, true])('only stops on added displays with privacy masks active=%s', (active) => {
+ vi.spyOn(PrivacyScreen.prototype, 'active', 'get').mockReturnValue(active);
+ h.screenHandlers.get('display-added')({}, { id: 2 });
+ expect(h.stop).toHaveBeenCalledTimes(active ? 1 : 0);
+});
+
it.each(['resolution', 'restoreResolution'])(
'waits for native completion and projection on %s',
async (method) => {
@@ -250,19 +258,18 @@ it.each(['resolution', 'restoreResolution'])(
},
);
-it.each([
- ['scaleFactor'],
- ['bounds', 'scaleFactor'],
- ['bounds', 'workArea', 'scaleFactor'],
-])('keeps managed geometry after late display metrics %j', (...metrics) => {
- h.screenHandlers.get('display-metrics-changed')(
- {},
- { id: 1, size: { width: 1920, height: 1080 } },
- metrics,
- );
- expect(h.geometryMatches).toHaveBeenCalledWith('1', 1920, 1080);
- expect(h.stop).not.toHaveBeenCalled();
-});
+it.each([['scaleFactor'], ['bounds', 'scaleFactor'], ['bounds', 'workArea', 'scaleFactor']])(
+ 'keeps managed geometry after late display metrics %j',
+ (...metrics) => {
+ h.screenHandlers.get('display-metrics-changed')(
+ {},
+ { id: 1, size: { width: 1920, height: 1080 } },
+ metrics,
+ );
+ expect(h.geometryMatches).toHaveBeenCalledWith('1', 1920, 1080);
+ expect(h.stop).not.toHaveBeenCalled();
+ },
+);
it.each([
[true, ['rotation']],
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/clipboard.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/clipboard.test.ts
index aececed79f..64b066142b 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/clipboard.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/clipboard.test.ts
@@ -2,14 +2,24 @@ import { beforeEach, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => ({
clipboard: {
- availableFormats: vi.fn(), readImage: vi.fn(), readText: vi.fn(),
- readHTML: vi.fn(), readRTF: vi.fn(),
+ availableFormats: vi.fn(),
+ readImage: vi.fn(),
+ readText: vi.fn(),
+ readHTML: vi.fn(),
+ readRTF: vi.fn(),
+ write: vi.fn(),
},
- version: vi.fn(), selection: vi.fn(),
+ version: vi.fn(),
+ selection: vi.fn(),
+ decode: vi.fn(),
+}));
+vi.mock('electron', () => ({
+ clipboard: mocks.clipboard,
+ nativeImage: { createFromBuffer: mocks.decode },
}));
-vi.mock('electron', () => ({ clipboard: mocks.clipboard, nativeImage: {} }));
vi.mock('../inputHost', () => ({
- readDesktopClipboardVersion: mocks.version, readDesktopSelection: mocks.selection,
+ readDesktopClipboardVersion: mocks.version,
+ readDesktopSelection: mocks.selection,
}));
import { transferDesktopClipboardContent } from '../clipboard';
@@ -26,15 +36,18 @@ beforeEach(() => {
mocks.clipboard.readHTML.mockReturnValue('<p>caption</p>');
mocks.clipboard.readRTF.mockReturnValue('rtf caption');
});
-it.each(['CF_HDROP', 'public.file-url', 'text/uri-list'])('copies inline image without %s metadata or paths', async (format) => {
- mocks.clipboard.availableFormats.mockReturnValue([format, 'image/png', 'text/plain']);
- mocks.clipboard.readText.mockReturnValue('/private/local-image.png');
- await expect(copy()).resolves.toEqual({ png: png.toString('base64') });
- expect(mocks.clipboard.readImage).toHaveBeenCalledTimes(1);
- expect(mocks.clipboard.readText).not.toHaveBeenCalled();
- expect(mocks.clipboard.readHTML).not.toHaveBeenCalled();
- expect(mocks.clipboard.readRTF).not.toHaveBeenCalled();
-});
+it.each(['CF_HDROP', 'public.file-url', 'text/uri-list'])(
+ 'copies inline image without %s metadata or paths',
+ async (format) => {
+ mocks.clipboard.availableFormats.mockReturnValue([format, 'image/png', 'text/plain']);
+ mocks.clipboard.readText.mockReturnValue('/private/local-image.png');
+ await expect(copy()).resolves.toEqual({ png: png.toString('base64') });
+ expect(mocks.clipboard.readImage).toHaveBeenCalledTimes(1);
+ expect(mocks.clipboard.readText).not.toHaveBeenCalled();
+ expect(mocks.clipboard.readHTML).not.toHaveBeenCalled();
+ expect(mocks.clipboard.readRTF).not.toHaveBeenCalled();
+ },
+);
it('still refuses file-only clipboard content', async () => {
mocks.clipboard.availableFormats.mockReturnValue(['CF_HDROP', 'text/plain']);
mocks.clipboard.readImage.mockReturnValue({ isEmpty: () => true });
@@ -42,7 +55,12 @@ it('still refuses file-only clipboard content', async () => {
expect(mocks.clipboard.readText).not.toHaveBeenCalled();
});
it('retains ordinary portable alternatives without file formats', async () => {
- await expect(copy()).resolves.toEqual({ text: 'caption', html: '<p>caption</p>', rtf: 'rtf caption', png: png.toString('base64') });
+ await expect(copy()).resolves.toEqual({
+ text: 'caption',
+ html: '<p>caption</p>',
+ rtf: 'rtf caption',
+ png: png.toString('base64'),
+ });
});
it('rejects an image if the clipboard changes during capture', async () => {
mocks.clipboard.availableFormats.mockReturnValue(['CF_HDROP', 'image/png']);
@@ -51,6 +69,77 @@ it('rejects an image if the clipboard changes during capture', async () => {
});
it('retains pixel bounds on file-backed images', async () => {
mocks.clipboard.availableFormats.mockReturnValue(['CF_HDROP', 'image/png']);
- mocks.clipboard.readImage.mockReturnValue({ ...image, getSize: () => ({ width: 10000, height: 10000 }) });
+ mocks.clipboard.readImage.mockReturnValue({
+ ...image,
+ getSize: () => ({ width: 10000, height: 10000 }),
+ });
await expect(copy()).rejects.toThrow('CLIPBOARD_TOO_LONG');
});
+
+const syncCopy = () =>
+ transferDesktopClipboardContent('copy', undefined, () => true, vi.fn(), { sync: true });
+it.each([copy, syncCopy])(
+ 'rejects oversized native images before encoding in either copy mode',
+ async (read) => {
+ const encode = vi.fn();
+ mocks.clipboard.readImage.mockReturnValue({
+ ...image,
+ getSize: () => ({ width: 2001, height: 2000 }),
+ toPNG: encode,
+ });
+ await expect(read()).rejects.toThrow('CLIPBOARD_TOO_LONG');
+ expect(encode).not.toHaveBeenCalled();
+ },
+);
+it('rejects an oversized encoded buffer before making a Base64 copy', async () => {
+ const bytes = Buffer.alloc(8 * 1024 * 1024 + 1);
+ const stringify = vi.spyOn(bytes, 'toString');
+ mocks.clipboard.readImage.mockReturnValue({ ...image, toPNG: () => bytes });
+ await expect(syncCopy()).rejects.toThrow('CLIPBOARD_TOO_LONG');
+ expect(stringify).not.toHaveBeenCalled();
+});
+it('accepts the exact pixel boundary and explicitly encodes at scale one', async () => {
+ const encode = vi.fn(() => png);
+ const size = vi.fn(() => ({ width: 2000, height: 2000 }));
+ mocks.clipboard.readImage.mockReturnValue({
+ ...image,
+ getSize: size,
+ toPNG: encode,
+ });
+ await syncCopy();
+ expect(size).toHaveBeenCalledWith(1);
+ expect(encode).toHaveBeenCalledWith({ scaleFactor: 1 });
+});
+it.each([0, 2001, 0xffffffff])(
+ 'refuses incoming invalid/oversized dimensions before native decode: %s',
+ async (width) => {
+ const bytes = Buffer.alloc(33);
+ Buffer.from([137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82]).copy(bytes);
+ bytes.writeUInt32BE(width, 16);
+ bytes.writeUInt32BE(2000, 20);
+ await expect(
+ transferDesktopClipboardContent(
+ 'paste',
+ { png: bytes.toString('base64') },
+ () => true,
+ vi.fn(),
+ ),
+ ).rejects.toThrow('CLIPBOARD_TOO_LONG');
+ expect(mocks.decode).not.toHaveBeenCalled();
+ expect(mocks.clipboard.write).not.toHaveBeenCalled();
+ },
+);
+it('also bounds image encoding during post-write synchronization verification', async () => {
+ const encode = vi.fn();
+ mocks.clipboard.readImage.mockReturnValue({
+ ...image,
+ getSize: () => ({ width: 8000, height: 8000 }),
+ toPNG: encode,
+ });
+ await expect(
+ transferDesktopClipboardContent('paste', { text: 'caption' }, () => true, vi.fn(), {
+ sync: true,
+ }),
+ ).rejects.toThrow('CLIPBOARD_TOO_LONG');
+ expect(encode).not.toHaveBeenCalled();
+});
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/clipboardTransfer.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/clipboardTransfer.test.ts
index 835a0fd780..c2723489c0 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/clipboardTransfer.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/clipboardTransfer.test.ts
@@ -6,64 +6,173 @@ beforeEach(() => vi.useFakeTimers());
afterEach(() => vi.useRealTimers());
describe('clipboard transfer idle expiry', () => {
- it.each(['copy', 'paste'] as const)('keeps %s progressing beyond one minute and settles only once', async (direction) => {
- const content = { text: 'x'.repeat(CLIPBOARD_CHUNK_CHARS * 2) };
- const data = JSON.stringify(content);
- const transfer = vi.fn(async (_action: 'copy' | 'paste', _content?: RemoteClipboardContent) => content);
+ it('returns small copies inline only when requested, and keeps large copies chunked', async () => {
+ const buffer = new ClipboardTransfer();
const current = () => true;
+ const request = {
+ op: 'clipboardContent' as const,
+ lease: 'lease',
+ action: 'copy' as const,
+ sync: true,
+ inline: true,
+ version: '1',
+ };
+ const result = await buffer.handle(request, current, async () => ({ text: 'hello' }));
+ expect(result).toEqual({ data: '{"text":"hello"}', length: 16 });
+ expect(vi.getTimerCount()).toBe(0);
+ const large = (await buffer.handle(request, current, async () => ({
+ text: 'x'.repeat(CLIPBOARD_CHUNK_CHARS),
+ }))) as { id: string };
+ expect(large.id).toBeTruthy();
+ buffer.reset();
+ const legacy = (await buffer.handle({ ...request, inline: undefined }, current, async () => ({
+ text: 'hello',
+ }))) as { id: string };
+ expect(legacy.id).toBeTruthy();
+ buffer.reset();
+ });
+ it('passes the expected version through inline writes and rejects stale completion', async () => {
const buffer = new ClipboardTransfer();
- const common = { op: 'clipboardContent' as const, lease: 'lease' };
- const { id } = await buffer.handle(
- direction === 'copy' ? { ...common, action: 'copy' } : { ...common, action: 'begin', length: data.length },
- current, transfer,
- ) as { id: string };
- let received = '';
- for (let offset = 0; offset < data.length; offset += CLIPBOARD_CHUNK_CHARS) {
- await vi.advanceTimersByTimeAsync(45_000);
- const chunk = data.slice(offset, offset + CLIPBOARD_CHUNK_CHARS);
+ let current = true;
+ const check = () => current;
+ const request = {
+ op: 'clipboardContent' as const,
+ lease: 'lease',
+ action: 'paste' as const,
+ sync: true,
+ version: '1',
+ data: '{"text":"hello"}',
+ };
+ const transfer = vi.fn(async () => ({ version: '2' }));
+ expect(await buffer.handle(request, check, transfer)).toEqual({ version: '2' });
+ expect(transfer).toHaveBeenCalledWith('paste', { text: 'hello' }, check, request);
+ await expect(
+ buffer.handle(request, check, async () => {
+ current = false;
+ return { version: '3' };
+ }),
+ ).rejects.toThrow('DESKTOP_LEASE_EXPIRED');
+ });
+ it.each(['copy', 'paste'] as const)(
+ 'keeps %s progressing beyond one minute and settles only once',
+ async (direction) => {
+ const content = { text: 'x'.repeat(CLIPBOARD_CHUNK_CHARS * 2) };
+ const data = JSON.stringify(content);
+ const transfer = vi.fn(
+ async (_action: 'copy' | 'paste', _content?: RemoteClipboardContent) => content,
+ );
+ const current = () => true;
+ const buffer = new ClipboardTransfer();
+ const common = { op: 'clipboardContent' as const, lease: 'lease' };
+ const { id } = (await buffer.handle(
+ direction === 'copy'
+ ? { ...common, action: 'copy' }
+ : { ...common, action: 'begin', length: data.length },
+ current,
+ transfer,
+ )) as { id: string };
+ let received = '';
+ for (let offset = 0; offset < data.length; offset += CLIPBOARD_CHUNK_CHARS) {
+ await vi.advanceTimersByTimeAsync(45_000);
+ const chunk = data.slice(offset, offset + CLIPBOARD_CHUNK_CHARS);
+ if (direction === 'copy') {
+ const reply = (await buffer.handle(
+ { ...common, action: 'read', id, offset },
+ current,
+ transfer,
+ )) as { data: string };
+ received += reply.data;
+ } else {
+ await buffer.handle(
+ { ...common, action: 'write', id, offset, data: chunk },
+ current,
+ transfer,
+ );
+ }
+ }
if (direction === 'copy') {
- const reply = await buffer.handle({ ...common, action: 'read', id, offset }, current, transfer) as { data: string };
- received += reply.data;
+ expect(received).toBe(data);
+ await buffer.handle({ ...common, action: 'cancel', id }, current, transfer);
} else {
- await buffer.handle({ ...common, action: 'write', id, offset, data: chunk }, current, transfer);
+ await buffer.handle({ ...common, action: 'commit', id }, current, transfer);
+ expect(transfer).toHaveBeenCalledWith('paste', content, current);
+ await expect(
+ buffer.handle({ ...common, action: 'commit', id }, current, transfer),
+ ).rejects.toThrow('CLIPBOARD_EXPIRED');
}
- }
- if (direction === 'copy') {
- expect(received).toBe(data);
- await buffer.handle({ ...common, action: 'cancel', id }, current, transfer);
- } else {
- await buffer.handle({ ...common, action: 'commit', id }, current, transfer);
- expect(transfer).toHaveBeenCalledWith('paste', content, current);
- await expect(buffer.handle({ ...common, action: 'commit', id }, current, transfer)).rejects.toThrow('CLIPBOARD_EXPIRED');
- }
- expect(transfer).toHaveBeenCalledTimes(1);
- expect(vi.getTimerCount()).toBe(0);
- });
+ expect(transfer).toHaveBeenCalledTimes(1);
+ expect(vi.getTimerCount()).toBe(0);
+ },
+ );
- it.each(['copy', 'paste'] as const)('expires idle %s even after invalid or unauthorized requests', async (direction) => {
- const buffer = new ClipboardTransfer();
- const transfer = vi.fn(async () => ({ text: 'content' }));
- const common = { op: 'clipboardContent' as const, lease: 'lease' };
- const { id } = await buffer.handle(
- direction === 'copy' ? { ...common, action: 'copy' } : { ...common, action: 'begin', length: 100 },
- () => true, transfer,
- ) as { id: string };
- const chunk = direction === 'copy'
- ? { ...common, action: 'read' as const, id, offset: 0 }
- : { ...common, action: 'write' as const, id, offset: 0, data: 'x' };
- await vi.advanceTimersByTimeAsync(30_000);
- await buffer.handle(chunk, () => true, transfer);
- await vi.advanceTimersByTimeAsync(59_999);
- await expect(buffer.handle({ ...chunk, id: 'wrong' }, () => true, transfer)).rejects.toThrow('CLIPBOARD_EXPIRED');
- await expect(buffer.handle({ ...chunk, lease: 'other' }, () => true, transfer)).rejects.toThrow('CLIPBOARD_EXPIRED');
- await expect(buffer.handle({ ...chunk, offset: 1000 }, () => true, transfer)).rejects.toThrow('INVALID_REQUEST');
- const wrongDirection = direction === 'copy'
- ? { ...common, action: 'write' as const, id, offset: 0, data: 'x' }
- : { ...common, action: 'read' as const, id, offset: 0 };
- await expect(buffer.handle(wrongDirection, () => true, transfer)).rejects.toThrow('INVALID_REQUEST');
- await expect(buffer.handle(chunk, () => false, transfer)).rejects.toThrow('DESKTOP_LEASE_EXPIRED');
- await vi.advanceTimersByTimeAsync(1);
- await expect(buffer.handle(chunk, () => true, transfer)).rejects.toThrow('CLIPBOARD_EXPIRED');
- expect(vi.getTimerCount()).toBe(0);
- });
+ it.each(['copy', 'paste'] as const)(
+ 'expires idle %s even after invalid or unauthorized requests',
+ async (direction) => {
+ const buffer = new ClipboardTransfer();
+ const transfer = vi.fn(async () => ({ text: 'content' }));
+ const common = { op: 'clipboardContent' as const, lease: 'lease' };
+ const { id } = (await buffer.handle(
+ direction === 'copy'
+ ? { ...common, action: 'copy' }
+ : { ...common, action: 'begin', length: 100 },
+ () => true,
+ transfer,
+ )) as { id: string };
+ const chunk =
+ direction === 'copy'
+ ? { ...common, action: 'read' as const, id, offset: 0 }
+ : { ...common, action: 'write' as const, id, offset: 0, data: 'x' };
+ await vi.advanceTimersByTimeAsync(30_000);
+ await buffer.handle(chunk, () => true, transfer);
+ await vi.advanceTimersByTimeAsync(59_999);
+ await expect(buffer.handle({ ...chunk, id: 'wrong' }, () => true, transfer)).rejects.toThrow(
+ 'CLIPBOARD_EXPIRED',
+ );
+ await expect(
+ buffer.handle({ ...chunk, lease: 'other' }, () => true, transfer),
+ ).rejects.toThrow('CLIPBOARD_EXPIRED');
+ await expect(buffer.handle({ ...chunk, offset: 1000 }, () => true, transfer)).rejects.toThrow(
+ 'INVALID_REQUEST',
+ );
+ const wrongDirection =
+ direction === 'copy'
+ ? { ...common, action: 'write' as const, id, offset: 0, data: 'x' }
+ : { ...common, action: 'read' as const, id, offset: 0 };
+ await expect(buffer.handle(wrongDirection, () => true, transfer)).rejects.toThrow(
+ 'INVALID_REQUEST',
+ );
+ await expect(buffer.handle(chunk, () => false, transfer)).rejects.toThrow(
+ 'DESKTOP_LEASE_EXPIRED',
+ );
+ await vi.advanceTimersByTimeAsync(1);
+ await expect(buffer.handle(chunk, () => true, transfer)).rejects.toThrow('CLIPBOARD_EXPIRED');
+ expect(vi.getTimerCount()).toBe(0);
+ },
+ );
+});
+
+it.each([true, false])('does not replace an active transfer with sync=%s', async (sync) => {
+ const buffer = new ClipboardTransfer();
+ const common = { op: 'clipboardContent' as const, lease: 'lease' };
+ const transfer = vi.fn(async () => ({ text: 'hello' }));
+ const first = (await buffer.handle(
+ { ...common, action: 'copy', sync: !sync },
+ () => true,
+ transfer,
+ )) as { id: string };
+ await expect(
+ buffer.handle({ ...common, action: 'copy', sync }, () => true, transfer),
+ ).rejects.toThrow('DESKTOP_CLIPBOARD_BUSY');
+ if (!sync) buffer.resetSync();
+ else {
+ buffer.resetSync();
+ expect(
+ await buffer.handle(
+ { ...common, action: 'read', id: first.id, offset: 0 },
+ () => true,
+ transfer,
+ ),
+ ).toEqual({ data: '{"text":"hello"}' });
+ }
+ buffer.reset();
});
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts
index b6c65e0b0e..315dfa854d 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from 'vitest';
+import { readFileSync } from 'node:fs';
import { RemoteDesktopController, type DesktopControllerDeps } from '../controller';
import { enumerateDesktopSources } from '../captureSource';
import type { RemoteDesktopIceReply, RemoteDesktopLease } from '@cindy/device-link';
@@ -39,6 +40,95 @@ function harness() {
};
}
describe('remote desktop authority and lifecycle', () => {
+ it('registers the returned stop promise with the awaited quit phase', () => {
+ const source = readFileSync(new URL('../index.ts', import.meta.url), 'utf8');
+ const registration = source.slice(
+ source.indexOf('onQuit('),
+ source.indexOf(" screen.on('display-removed'"),
+ );
+ const register = vi.fn();
+ const pending = new Promise<void>(() => {});
+ const stop = vi.fn();
+ const stopAndRestore = vi.fn(() => pending);
+ const dismiss = vi.fn();
+ const clear = vi.fn();
+ new Function('onQuit', 'clearInterval', 'timer', 'permissions', 'remoteDesktop', registration)(
+ register,
+ clear,
+ 1,
+ { dismiss },
+ { stop, stopAndRestore },
+ );
+ expect(register).toHaveBeenCalledWith('remote-desktop-restore', expect.any(Function), 'async');
+ register.mock.calls[0][1]();
+ expect(stop).toHaveBeenCalledOnce();
+ expect(register.mock.calls[1][1]()).toBe(pending);
+ expect(clear).toHaveBeenCalledWith(1);
+ expect(dismiss).toHaveBeenCalledOnce();
+ });
+ it.each(['stop', 'stopAndRestore'] as const)(
+ 'keeps %s pending until audio restoration finishes',
+ async (method) => {
+ const h = harness();
+ await h.start();
+ let finish!: () => void;
+ h.deps.stopHostMute = () =>
+ new Promise<void>((resolve) => {
+ finish = resolve;
+ });
+ let settled = false;
+ const stopped = h.controller[method]().then(() => {
+ settled = true;
+ });
+ expect(h.controller.state).toBeNull();
+ expect(h.deps.stopInput).toHaveBeenCalledOnce();
+ await Promise.resolve();
+ expect(settled).toBe(false);
+ finish();
+ await stopped;
+ expect(settled).toBe(true);
+ },
+ );
+ it.each([false, true])(
+ 'clears safety before a temporary resolution change, interrupted=%s',
+ async (interrupted) => {
+ const h = harness();
+ h.deps.displayModes = vi.fn(async () => [
+ { id: '1', width: 1920, height: 1080, current: true },
+ { id: '2', width: 3840, height: 2160, current: false },
+ ]);
+ h.deps.resolution = vi.fn(async (_display, _mode, before) => before());
+ h.deps.stopPrivacyScreen = vi.fn();
+ let finish!: () => void;
+ const safety = new Promise<void>((resolve) => {
+ finish = resolve;
+ });
+ h.deps.stopHostMute = vi.fn(() => safety);
+ const { lease } = await h.start();
+ await h.controller.request('phone', { op: 'control', lease, enabled: true });
+ const pending = h.controller.request('phone', {
+ op: 'resolution',
+ lease,
+ modeId: '2',
+ temporary: true,
+ });
+ const rejected = interrupted
+ ? expect(pending).rejects.toThrow('DESKTOP_LEASE_EXPIRED')
+ : undefined;
+ expect(h.deps.stopInput).toHaveBeenCalled();
+ expect(h.deps.stopPrivacyScreen).toHaveBeenCalledOnce();
+ expect(h.deps.displayModes).not.toHaveBeenCalled();
+ if (interrupted) void h.controller.stop();
+ finish();
+ if (interrupted) {
+ await rejected;
+ expect(h.deps.resolution).not.toHaveBeenCalled();
+ } else {
+ await pending;
+ expect(h.deps.resolution).toHaveBeenCalledOnce();
+ }
+ },
+ );
it('does not publish selected geometry or resume control before it is observed', async () => {
const h = harness();
h.deps.displayModes = async () => [
@@ -280,6 +370,96 @@ describe('remote desktop authority and lifecycle', () => {
expect(h.deps.stopVideo).not.toHaveBeenCalled();
});
+ it.each([true, false])('uses the current privacy exit lock preference (%s)', async (enabled) => {
+ const h = harness();
+ h.deps.privacyScreen = vi.fn(async () => {});
+ h.deps.lockScreen = vi.fn(async (current) => {
+ expect(current()).toBe(true);
+ });
+ const { lease } = await h.start();
+ await h.controller.request('phone', { op: 'control', lease, enabled: true });
+ await h.controller.request('phone', {
+ op: 'privacyScreen',
+ lease,
+ enabled: true,
+ lockOnExit: true,
+ });
+ await h.controller.request('phone', {
+ op: 'privacyScreen',
+ lease,
+ enabled: true,
+ lockOnExit: enabled,
+ });
+ await h.controller.stopPrivacyByUser();
+ expect(h.deps.lockScreen).toHaveBeenCalledTimes(enabled ? 1 : 0);
+ expect(h.controller.state).toBeNull();
+ await expect(h.controller.request('phone', { op: 'heartbeat', lease })).rejects.toThrow(
+ 'DESKTOP_STOPPED',
+ );
+ });
+ it('ends privacy control even when locking fails', async () => {
+ const h = harness();
+ h.deps.privacyScreen = vi.fn(async () => {});
+ h.deps.lockScreen = vi.fn(async () => {
+ throw new Error('LOCK_FAILED');
+ });
+ const { lease } = await h.start();
+ await h.controller.request('phone', { op: 'control', lease, enabled: true });
+ await h.controller.request('phone', {
+ op: 'privacyScreen',
+ lease,
+ enabled: true,
+ lockOnExit: true,
+ });
+ await expect(h.controller.stopPrivacyByUser()).rejects.toThrow('LOCK_FAILED');
+ expect(h.controller.state).toBeNull();
+ });
+ it('requires owner control and opt-in for synchronization and invalidates a delayed read on disable', async () => {
+ const h = harness(),
+ { lease } = await h.start();
+ let finish!: (version: string) => void;
+ h.deps.clipboardVersion = () =>
+ new Promise((resolve) => {
+ finish = resolve;
+ });
+ await expect(
+ h.controller.request('phone', { op: 'clipboardSync', lease, enabled: true }),
+ ).rejects.toThrow('DESKTOP_CLIPBOARD_UNAVAILABLE');
+ await h.controller.request('phone', { op: 'control', lease, enabled: true });
+ await h.controller.request('phone', { op: 'clipboardSync', lease, enabled: true });
+ await expect(h.controller.request('other', { op: 'clipboardVersion', lease })).rejects.toThrow(
+ 'DESKTOP_LEASE_EXPIRED',
+ );
+ const read = h.controller.request('phone', { op: 'clipboardVersion', lease });
+ await h.controller.request('phone', { op: 'clipboardSync', lease, enabled: false });
+ finish('12');
+ await expect(read).rejects.toThrow('DESKTOP_LEASE_EXPIRED');
+ h.controller.stop();
+ });
+ it('restores privacy on lease expiry and invalidates an older enable after disable', async () => {
+ const h = harness(),
+ { lease } = await h.start();
+ let finish!: () => void;
+ let oldCurrent!: () => boolean;
+ h.deps.stopPrivacyScreen = vi.fn();
+ h.deps.privacyScreen = async (enabled, current) => {
+ if (enabled) {
+ oldCurrent = current;
+ await new Promise<void>((resolve) => {
+ finish = resolve;
+ });
+ }
+ };
+ await h.controller.request('phone', { op: 'control', lease, enabled: true });
+ const enable = h.controller.request('phone', { op: 'privacyScreen', lease, enabled: true });
+ await h.controller.request('phone', { op: 'privacyScreen', lease, enabled: false });
+ expect(oldCurrent()).toBe(false);
+ finish();
+ await expect(enable).rejects.toThrow('DESKTOP_LEASE_EXPIRED');
+ h.advance(60000);
+ h.controller.tick();
+ expect(h.deps.stopPrivacyScreen).toHaveBeenCalled();
+ });
it('locks only on explicit owner exit and blocks takeover until lock completes', async () => {
const h = harness(),
first = await h.start();
@@ -975,3 +1155,97 @@ describe('remote desktop authority and lifecycle', () => {
expect(h.controller.hasLease(lease.lease)).toBe(true);
});
});
+it.each(['control', 'presentation', 'failure', 'stop'] as const)(
+ 'releases input before failing safety restoration on %s, without stopping replacement input',
+ async (kind) => {
+ const h = harness(),
+ { lease } = await h.start();
+ await h.controller.request('phone', { op: 'control', lease, enabled: true });
+ let rejectRestore!: (error: Error) => void;
+ h.deps.stopHostMute = vi.fn(
+ () =>
+ new Promise<void>((_, reject) => {
+ rejectRestore = reject;
+ }),
+ );
+ h.deps.stopPrivacyScreen = vi.fn();
+ let result: Promise<unknown> | undefined;
+ if (kind === 'failure') h.controller.releaseControl();
+ else if (kind === 'stop') h.controller.stop();
+ else
+ result = h.controller.request('phone', { op: kind, lease, enabled: kind === 'presentation' });
+ // Critical cleanup cannot wait for slow/failing operating-system restoration.
+ expect(h.deps.stopInput).toHaveBeenCalledOnce();
+ expect(h.deps.stopPrivacyScreen).toHaveBeenCalledOnce();
+ expect(h.controller.state?.controlling ?? false).toBe(false);
+ const next = kind === 'stop' ? (await h.start()).lease : lease;
+ await h.controller.request('phone', { op: 'control', lease: next, enabled: true });
+ const rejection = result ? expect(result).rejects.toThrow('restore failed') : Promise.resolve();
+ rejectRestore(new Error('restore failed'));
+ await rejection;
+ await Promise.resolve();
+ expect(h.deps.stopInput).toHaveBeenCalledOnce();
+ expect(h.controller.state?.controlling).toBe(true);
+ },
+);
+it.each(['control', 'presentation', 'failure'] as const)(
+ 'revokes safety effects on %s control loss',
+ async (kind) => {
+ const h = harness(),
+ { lease } = await h.start();
+ h.deps.privacyScreen = vi.fn(async () => {});
+ h.deps.hostMute = vi.fn(async () => {});
+ h.deps.stopPrivacyScreen = vi.fn();
+ h.deps.stopHostMute = vi.fn();
+ for (const op of ['privacyScreen', 'hostMute'] as const)
+ await expect(h.controller.request('phone', { op, lease, enabled: true })).rejects.toThrow(
+ 'DESKTOP_VIEW_ONLY',
+ );
+ expect(h.deps.privacyScreen).not.toHaveBeenCalled();
+ expect(h.deps.hostMute).not.toHaveBeenCalled();
+ await h.controller.request('phone', { op: 'control', lease, enabled: true });
+ await h.controller.request('phone', { op: 'privacyScreen', lease, enabled: true });
+ await h.controller.request('phone', { op: 'hostMute', lease, enabled: true });
+ if (kind === 'failure') h.controller.releaseControl();
+ else await h.controller.request('phone', { op: kind, lease, enabled: kind === 'presentation' });
+ expect(h.deps.stopPrivacyScreen).toHaveBeenCalledOnce();
+ expect(h.deps.stopHostMute).toHaveBeenCalledOnce();
+ expect(h.deps.stopVideo).not.toHaveBeenCalled();
+ },
+);
+it('joins duplicate privacy initialization and invalidates it on control loss', async () => {
+ const h = harness(),
+ { lease } = await h.start();
+ await h.controller.request('phone', { op: 'control', lease, enabled: true });
+ let ready!: () => void;
+ h.deps.privacyScreen = vi.fn(
+ () =>
+ new Promise<void>((resolve) => {
+ ready = resolve;
+ }),
+ );
+ const first = h.controller.request('phone', { op: 'privacyScreen', lease, enabled: true });
+ const second = h.controller.request('phone', {
+ op: 'privacyScreen',
+ lease,
+ enabled: true,
+ lockOnExit: true,
+ });
+ let settled = false;
+ void second.then(
+ () => {
+ settled = true;
+ },
+ () => {},
+ );
+ await Promise.resolve();
+ expect(settled).toBe(false);
+ expect(h.deps.privacyScreen).toHaveBeenCalledOnce();
+ h.controller.releaseControl();
+ const errors = Promise.all([
+ expect(first).rejects.toThrow('DESKTOP_LEASE_EXPIRED'),
+ expect(second).rejects.toThrow('DESKTOP_LEASE_EXPIRED'),
+ ]);
+ ready();
+ await errors;
+});
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/inputHost.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/inputHost.test.ts
index f9b20bd391..bc37d82231 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/inputHost.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/inputHost.test.ts
@@ -205,6 +205,99 @@ describe('native input lifecycle', () => {
await flush();
}
});
+ it.each(['ready', 'failure', 'stop'] as const)(
+ 'keeps resumed input paused through preparation and handshake: %s',
+ async (outcome) => {
+ const first = childProcess();
+ const replacement = childProcess();
+ const failure = vi.fn();
+ let prepared!: (binary: string) => void;
+ const resolveBinary = vi
+ .fn()
+ .mockResolvedValueOnce('/test/helper')
+ .mockImplementationOnce(
+ () =>
+ new Promise<string>((resolve) => {
+ prepared = resolve;
+ }),
+ );
+ const spawn = vi
+ .fn()
+ .mockImplementationOnce(() => {
+ queueMicrotask(() => first.child.stdout.emit('data', Buffer.from('ready\n')));
+ return first.typed;
+ })
+ .mockReturnValueOnce(replacement.typed);
+ const host = new DesktopInputHost(failure, { platform: 'darwin', resolveBinary, spawn });
+ const events = [{ kind: 'key' as const, code: 'Enter', down: true }];
+ await host.start('1');
+ const pausing = host.pauseForPrivacy();
+ first.exit();
+ const resume = await pausing;
+ const resuming = resume();
+ expect(() => host.input(events)).not.toThrow();
+ await Promise.resolve();
+ expect(() => host.input(events)).not.toThrow();
+ prepared('/test/helper');
+ await Promise.resolve();
+ expect(spawn).toHaveBeenCalledTimes(2);
+ expect(() => host.input(events)).not.toThrow();
+ expect(replacement.child.stdin.write).not.toHaveBeenCalled();
+ if (outcome === 'stop') host.stop();
+ replacement.child.stdout.emit(
+ 'data',
+ Buffer.from(outcome === 'failure' ? 'permission\n' : 'ready\n'),
+ );
+ await resuming;
+ if (outcome === 'ready') {
+ host.input(events);
+ await flush();
+ expect(replacement.child.stdin.write).toHaveBeenCalledOnce();
+ } else {
+ expect(() => host.input(events)).toThrow('DESKTOP_INPUT_UNAVAILABLE');
+ expect(replacement.child.stdin.write).not.toHaveBeenCalled();
+ }
+ expect(failure).toHaveBeenCalledTimes(outcome === 'failure' ? 1 : 0);
+ host.stop();
+ replacement.exit();
+ await expect(withAgentDesktopInput(async () => {})).resolves.toBeUndefined();
+ },
+ );
+ it.each([false, true])(
+ 'pauses remote input while retaining ownership; disconnected=%s',
+ async (disconnected) => {
+ const children = [childProcess(), childProcess()];
+ let index = 0;
+ const spawn = vi.fn(() => {
+ const c = children[index++];
+ queueMicrotask(() => c.child.stdout.emit('data', Buffer.from('ready\n')));
+ return c.typed;
+ });
+ const host = new DesktopInputHost(vi.fn(), {
+ platform: 'darwin',
+ resolveBinary: async () => '/test/helper',
+ spawn,
+ });
+ await host.start('1');
+ const pausing = host.pauseForPrivacy();
+ host.input([{ kind: 'key', code: 'Enter', down: true }]);
+ expect(children[0].child.stdin.write).not.toHaveBeenCalled();
+ children[0].exit();
+ const resume = await pausing;
+ await expect(withAgentDesktopInput(async () => {})).rejects.toThrow('input is active');
+ if (disconnected) host.stop();
+ await resume();
+ expect(spawn).toHaveBeenCalledTimes(disconnected ? 1 : 2);
+ if (!disconnected) {
+ host.input([{ kind: 'key', code: 'Enter', down: true }]);
+ await flush();
+ expect(children[1].child.stdin.write).toHaveBeenCalled();
+ }
+ host.stop();
+ children[1].exit();
+ await expect(withAgentDesktopInput(async () => {})).resolves.toBeUndefined();
+ },
+ );
it('keeps Agent input excluded until the old helper has actually exited', async () => {
const c = childProcess();
const host = new DesktopInputHost(vi.fn(), {
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/macosInput.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/macosInput.test.ts
index 4e179f39ea..38579aee3b 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/macosInput.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/macosInput.test.ts
@@ -6,8 +6,10 @@ import { promisify } from 'node:util';
import { expect, it } from 'vitest';
const exec = promisify(execFile);
-const callerPath = path.resolve(import.meta.dirname,
- '../../../../../../packages/remote-credentials-native/Sources/DesktopNativeCaller/DesktopNativeCaller.swift');
+const callerPath = path.resolve(
+ import.meta.dirname,
+ '../../../../../../packages/remote-credentials-native/Sources/DesktopNativeCaller/DesktopNativeCaller.swift',
+);
it.skipIf(process.platform !== 'darwin')(
'preserves native system shortcut flags without posting input',
@@ -46,6 +48,7 @@ it.skipIf(process.platform !== 'darwin')(
for (const args of [
[],
['--check'],
+ ['--privacy-input'],
['--lock-screen'],
['--request-permission'],
['--clipboard-version'],
@@ -68,11 +71,10 @@ it.skipIf(process.platform !== 'darwin')(
// Exercise the real audit-token/SecCode APIs with a harmless probe. It
// contains only authentication, never AX reads, input or permission UI.
- const prefix = (await readFile(callerPath, 'utf8'))
- .replace(
- '"DESKTOP_INPUT_DEVELOPMENT_EXECUTABLE"',
- JSON.stringify(Buffer.from(process.execPath).toString('base64')),
- );
+ const prefix = (await readFile(callerPath, 'utf8')).replace(
+ '"DESKTOP_INPUT_DEVELOPMENT_EXECUTABLE"',
+ JSON.stringify(Buffer.from(process.execPath).toString('base64')),
+ );
const main = path.join(directory, 'main.swift');
const probe = path.join(directory, 'probe');
await writeFile(
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/privacyInput.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/privacyInput.test.ts
new file mode 100644
index 0000000000..bfcdd41c54
--- /dev/null
+++ b/apps/desktop/src/main/remote-desktop/__tests__/privacyInput.test.ts
@@ -0,0 +1,98 @@
+import { EventEmitter } from 'node:events';
+import type { ChildProcessWithoutNullStreams } from 'node:child_process';
+import { afterEach, expect, it, vi } from 'vitest';
+import { watchPrivacyInput } from '../privacyInput';
+
+vi.mock('../inputHost', () => ({ resolveDesktopInputBinary: vi.fn() }));
+afterEach(() => vi.useRealTimers());
+function fixture() {
+ const child = Object.assign(new EventEmitter(), {
+ stdout: new EventEmitter(),
+ stderr: { resume() {} },
+ stdin: Object.assign(new EventEmitter(), { write: vi.fn(), end: vi.fn(), writableLength: 0 }),
+ kill: vi.fn(),
+ });
+ const local = vi.fn();
+ const failed = vi.fn();
+ const runtime = {
+ resolveBinary: async () => '/helper',
+ spawn: () => child as unknown as ChildProcessWithoutNullStreams,
+ };
+ const line = (text: string) => child.stdout.emit('data', Buffer.from(text));
+ return { child, local, failed, runtime, line };
+}
+
+it('requires native acknowledgement and parses split status words without exposing event data', async () => {
+ const f = fixture();
+ const starting = watchPrivacyInput(f.local, f.failed, f.runtime);
+ await Promise.resolve();
+ f.line('rea');
+ f.line('dy\n');
+ const monitor = await starting;
+ f.line('local-in');
+ f.line('put\n');
+ expect(f.local).toHaveBeenCalledExactlyOnceWith();
+ let confirmed = false;
+ const confirming = monitor.confirm().then(() => {
+ confirmed = true;
+ });
+ await Promise.resolve();
+ expect(confirmed).toBe(false);
+ expect(f.child.stdin.write).toHaveBeenLastCalledWith('confirm\n');
+ f.line('confirmed\n');
+ await confirming;
+ const resuming = monitor.resume();
+ f.line('ready\n');
+ await resuming;
+ monitor.stop();
+ f.child.emit('exit');
+ f.line('local-input\n');
+ expect(f.local).toHaveBeenCalledOnce();
+ expect(f.failed).not.toHaveBeenCalled();
+});
+
+it.each(['exit', 'timeout', 'oversized'])(
+ 'fails closed on %s during confirmation',
+ async (mode) => {
+ vi.useFakeTimers();
+ const f = fixture();
+ const starting = watchPrivacyInput(f.local, f.failed, f.runtime);
+ await Promise.resolve();
+ f.line('ready\n');
+ const monitor = await starting;
+ const checking = expect(monitor.confirm()).rejects.toThrow('DESKTOP_PRIVACY_UNAVAILABLE');
+ if (mode === 'exit') f.child.emit('exit');
+ else if (mode === 'oversized') f.line('a'.repeat(1025));
+ else await vi.advanceTimersByTimeAsync(5000);
+ await checking;
+ expect(f.failed).toHaveBeenCalledOnce();
+ expect(f.child.stdin.end).toHaveBeenCalledOnce();
+ monitor.stop();
+ },
+);
+
+it('waits for actual process close after EOF or fallback kill, including repeated stop', async () => {
+ vi.useFakeTimers();
+ const f = fixture();
+ const starting = watchPrivacyInput(f.local, f.failed, f.runtime);
+ await Promise.resolve();
+ f.line('ready\n');
+ const monitor = await starting;
+ let closed = false;
+ const stopping = monitor.stop();
+ expect(monitor.stop()).toBe(stopping);
+ void stopping.then(() => {
+ closed = true;
+ });
+ expect(f.child.stdin.end).toHaveBeenCalledOnce();
+ await vi.advanceTimersByTimeAsync(1000);
+ expect(f.child.kill).toHaveBeenCalledOnce();
+ expect(closed).toBe(false);
+ f.child.emit('exit');
+ await Promise.resolve();
+ expect(closed).toBe(false);
+ f.child.emit('close');
+ await stopping;
+ expect(closed).toBe(true);
+ expect(f.failed).not.toHaveBeenCalled();
+});
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/privacyScreen.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/privacyScreen.test.ts
new file mode 100644
index 0000000000..f833ba6f0a
--- /dev/null
+++ b/apps/desktop/src/main/remote-desktop/__tests__/privacyScreen.test.ts
@@ -0,0 +1,350 @@
+import { EventEmitter } from 'node:events';
+import { beforeEach, expect, it, vi } from 'vitest';
+
+const state = vi.hoisted(() => ({ windows: [] as any[], displays: [] as any[], confirm: vi.fn() }));
+vi.mock('../../i18n', () => ({ t: (key: string) => key }));
+vi.mock('../../logger', () => ({ createLogger: () => ({ debug: vi.fn(), warn: vi.fn() }) }));
+vi.mock('electron', () => ({
+ app: { focus: vi.fn() },
+ dialog: { showMessageBox: state.confirm },
+ BrowserWindow: class extends EventEmitter {
+ id = state.windows.length + 1;
+ destroyed = false;
+ options: any;
+ bounds = { x: 0, y: 25, width: 1440, height: 875 };
+ webContents = Object.assign(new EventEmitter(), {
+ setWindowOpenHandler: vi.fn(),
+ focus: vi.fn(),
+ });
+ constructor(options: any) {
+ super();
+ this.options = options;
+ state.windows.push(this);
+ }
+ setMenuBarVisibility() {}
+ async loadURL() {}
+ setContentProtection() {}
+ setIgnoreMouseEvents = vi.fn();
+ setFocusable = vi.fn();
+ setVisibleOnAllWorkspaces() {}
+ setAlwaysOnTop() {}
+ getMediaSourceId() {
+ return `window:${this.id}:0`;
+ }
+ getBounds() {
+ return this.bounds;
+ }
+ setBounds(bounds: typeof this.bounds) {
+ this.bounds = bounds;
+ }
+ showInactive() {}
+ focus = vi.fn();
+ isVisible() {
+ return true;
+ }
+ isFocused() {
+ return true;
+ }
+ isAlwaysOnTop() {
+ return true;
+ }
+ isDestroyed() {
+ return this.destroyed;
+ }
+ destroy() {
+ this.destroyed = true;
+ this.emit('closed');
+ }
+ },
+ screen: Object.assign(new EventEmitter(), {
+ getAllDisplays: () => state.displays,
+ getDisplayMatching: () => ({ bounds: { x: 0, y: 0, width: 1440, height: 900 } }),
+ }),
+ globalShortcut: { register: vi.fn(() => true), unregister: vi.fn() },
+ session: {
+ fromPartition: () => ({
+ setPermissionCheckHandler() {},
+ setPermissionRequestHandler() {},
+ webRequest: { onBeforeRequest() {} },
+ }),
+ },
+}));
+
+import { PrivacyScreen } from '../privacyScreen';
+import { screen } from 'electron';
+beforeEach(() => {
+ screen.removeAllListeners();
+ state.displays = [{ id: 1, bounds: { x: 0, y: 0, width: 1440, height: 900 } }];
+ state.windows.length = 0;
+ state.confirm.mockReset().mockResolvedValue({ response: 1 });
+});
+
+it('relayouts the non-captured display and releases its listeners on stop', async () => {
+ state.displays.push({ id: 2, bounds: { x: 1440, y: 0, width: 900, height: 1200 } });
+ const h = fixture();
+ await h.masks.set(true, () => true);
+ const second = state.windows[1];
+ const bounds = { x: -2000, y: -100, width: 2000, height: 1400 };
+ screen.emit('display-metrics-changed', {}, { id: 2, bounds }, [
+ 'bounds',
+ 'rotation',
+ 'scaleFactor',
+ ]);
+ expect(second.getBounds()).toEqual(bounds);
+ expect(state.windows[0].getBounds()).toEqual(state.displays[0].bounds);
+ expect(h.stopped).not.toHaveBeenCalled();
+ h.stopped.mockReturnValue(new Promise(() => {}));
+ h.failed();
+ const lockingBounds = { ...bounds, x: 2000 };
+ screen.emit('display-metrics-changed', {}, { id: 2, bounds: lockingBounds }, ['bounds']);
+ expect(second.getBounds()).toEqual(lockingBounds);
+ h.masks.stop();
+ expect(screen.listenerCount('display-metrics-changed')).toBe(0);
+});
+
+function fixture() {
+ const stopped = vi.fn();
+ const excluded = vi.fn();
+ const resume = vi.fn(async () => {});
+ const suspend = vi.fn(async () => resume);
+ const monitor = {
+ confirm: vi.fn(async () => {}),
+ resume: vi.fn(async () => {}),
+ stop: vi.fn(async () => {}),
+ };
+ let local!: () => void;
+ let failed!: () => void;
+ const masks = new PrivacyScreen(excluded, stopped, suspend, async (onLocal, onFailed) => {
+ local = onLocal;
+ failed = onFailed;
+ return monitor;
+ });
+ return {
+ masks,
+ stopped,
+ excluded,
+ suspend,
+ resume,
+ monitor,
+ local: () => local(),
+ failed: () => failed(),
+ };
+}
+const settle = async () => {
+ for (let i = 0; i < 12; i++) await Promise.resolve();
+};
+
+it('keeps the full-display mask passive so injected input reaches underlying apps', async () => {
+ const f = fixture();
+ expect(f.masks.active).toBe(false);
+ const preparing = f.masks.set(true, () => true);
+ expect(f.masks.active).toBe(true);
+ await preparing;
+ expect(f.masks.active).toBe(true);
+ const window = state.windows[0];
+ expect(window.options).toMatchObject({
+ enableLargerThanScreen: true,
+ roundedCorners: false,
+ focusable: false,
+ });
+ expect(window.getBounds()).toEqual({ x: 0, y: 0, width: 1440, height: 900 });
+ expect(window.setIgnoreMouseEvents).toHaveBeenLastCalledWith(true);
+ expect(window.focus).not.toHaveBeenCalled();
+ window.webContents.emit('before-mouse-event', {}, { type: 'mouseDown' });
+ window.webContents.emit('before-input-event', {}, { type: 'keyDown' });
+ expect(state.confirm).not.toHaveBeenCalled();
+ f.masks.stop();
+ expect(f.masks.active).toBe(false);
+ expect(f.monitor.stop).toHaveBeenCalledOnce();
+});
+
+it('drains remote input and fences late injection before showing one local confirmation', async () => {
+ const f = fixture();
+ let drained!: () => void;
+ f.suspend.mockImplementation(
+ () =>
+ new Promise((resolve) => {
+ drained = () => resolve(f.resume);
+ }),
+ );
+ await f.masks.set(true, () => true);
+ f.local();
+ f.local();
+ expect(f.suspend).toHaveBeenCalledOnce();
+ expect(state.confirm).not.toHaveBeenCalled();
+ drained();
+ await settle();
+ expect(f.monitor.confirm.mock.invocationCallOrder[0]).toBeLessThan(
+ state.confirm.mock.invocationCallOrder[0],
+ );
+ expect(state.confirm).toHaveBeenCalledOnce();
+ expect(f.stopped).toHaveBeenCalledOnce();
+ expect(state.windows[0].destroyed).toBe(true);
+ expect(f.excluded).toHaveBeenLastCalledWith([]);
+});
+
+it('restores passive masking and input on cancel, but ignores an old dialog after replacement', async () => {
+ const f = fixture();
+ await f.masks.set(true, () => true);
+ state.confirm.mockResolvedValueOnce({ response: 0 });
+ f.local();
+ await settle();
+ expect(f.stopped).not.toHaveBeenCalled();
+ expect(state.windows[0].setFocusable).toHaveBeenLastCalledWith(false);
+ expect(state.windows[0].setIgnoreMouseEvents).toHaveBeenLastCalledWith(true);
+ expect(f.monitor.resume).toHaveBeenCalledOnce();
+ expect(f.resume).toHaveBeenCalledOnce();
+ let finish!: (result: { response: number }) => void;
+ state.confirm.mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ finish = resolve;
+ }),
+ );
+ f.local();
+ await settle();
+ f.masks.stop();
+ await f.masks.set(true, () => true);
+ finish({ response: 1 });
+ await settle();
+ expect(f.stopped).not.toHaveBeenCalled();
+ expect(state.windows[1].destroyed).toBe(false);
+ expect(f.monitor.resume).toHaveBeenCalledOnce();
+ f.masks.stop();
+});
+
+it('does not open a stale dialog if disconnected while input is draining', async () => {
+ const f = fixture();
+ let drained!: () => void;
+ f.suspend.mockImplementation(
+ () =>
+ new Promise((resolve) => {
+ drained = () => resolve(f.resume);
+ }),
+ );
+ await f.masks.set(true, () => true);
+ f.local();
+ f.masks.stop();
+ drained();
+ await settle();
+ expect(state.confirm).not.toHaveBeenCalled();
+ expect(f.monitor.confirm).not.toHaveBeenCalled();
+});
+
+it('ends the lease instead of leaving an inescapable mask when its native watcher fails', async () => {
+ const f = fixture();
+ await f.masks.set(true, () => true);
+ f.failed();
+ await settle();
+ expect(f.stopped).toHaveBeenCalledOnce();
+ expect(state.windows[0].destroyed).toBe(true);
+});
+
+it.each(['confirmed', 'watcher', 'renderer'] as const)(
+ 'retains masks and capture exclusions while %s disconnect waits for locking',
+ async (reason) => {
+ const f = fixture();
+ let finish!: () => void;
+ f.stopped.mockImplementation(
+ () =>
+ new Promise<void>((resolve) => {
+ finish = resolve;
+ }),
+ );
+ await f.masks.set(true, () => true);
+ const window = state.windows[0];
+ if (reason === 'confirmed') f.local();
+ else if (reason === 'watcher') f.failed();
+ else window.webContents.emit('render-process-gone');
+ await settle();
+ expect(f.stopped).toHaveBeenCalledOnce();
+ expect(window.destroyed).toBe(false);
+ expect(f.excluded).toHaveBeenLastCalledWith([window.id]);
+ expect(f.resume).not.toHaveBeenCalled();
+ f.failed();
+ expect(f.stopped).toHaveBeenCalledOnce();
+ finish();
+ await settle();
+ expect(window.destroyed).toBe(true);
+ expect(f.excluded).toHaveBeenLastCalledWith([]);
+ expect(f.resume).not.toHaveBeenCalled();
+ },
+);
+
+it('does not clear replacement masks when an old disconnect rejects late', async () => {
+ const f = fixture();
+ let reject!: (error: Error) => void;
+ f.stopped.mockImplementation(
+ () =>
+ new Promise<void>((_, fail) => {
+ reject = fail;
+ }),
+ );
+ await f.masks.set(true, () => true);
+ f.local();
+ await settle();
+ f.masks.stop();
+ await f.masks.set(true, () => true);
+ const replacement = state.windows[1];
+ reject(new Error('lock failed'));
+ await settle();
+ expect(replacement.destroyed).toBe(false);
+ expect(f.excluded).toHaveBeenLastCalledWith([replacement.id]);
+ f.masks.stop();
+});
+
+it('does not expose the confirmation if the native injection fence fails', async () => {
+ const f = fixture();
+ await f.masks.set(true, () => true);
+ f.monitor.confirm.mockRejectedValueOnce(new Error('unavailable'));
+ f.local();
+ await settle();
+ expect(state.confirm).not.toHaveBeenCalled();
+ expect(f.stopped).toHaveBeenCalledOnce();
+});
+
+it.each(['confirmed', 'watcher', 'renderer'] as const)(
+ 'waits for hook exit before locking, with masks retained: %s',
+ async (reason) => {
+ const f = fixture();
+ let unhook!: () => void;
+ f.monitor.stop.mockReturnValue(
+ new Promise<void>((resolve) => {
+ unhook = resolve;
+ }),
+ );
+ await f.masks.set(true, () => true);
+ const window = state.windows[0];
+ if (reason === 'confirmed') f.local();
+ else if (reason === 'watcher') f.failed();
+ else window.webContents.emit('render-process-gone');
+ await settle();
+ expect(f.monitor.stop).toHaveBeenCalledOnce();
+ expect(f.stopped).not.toHaveBeenCalled();
+ expect(window.destroyed).toBe(false);
+ expect(f.excluded).toHaveBeenLastCalledWith([window.id]);
+ unhook();
+ await settle();
+ expect(f.stopped).toHaveBeenCalledOnce();
+ expect(window.destroyed).toBe(true);
+ },
+);
+
+it('does not lock a replacement lease after delayed old hook teardown', async () => {
+ const f = fixture();
+ let unhook!: () => void;
+ f.monitor.stop.mockReturnValueOnce(
+ new Promise<void>((resolve) => {
+ unhook = resolve;
+ }),
+ );
+ await f.masks.set(true, () => true);
+ f.failed();
+ f.masks.stop();
+ await f.masks.set(true, () => true);
+ unhook();
+ await settle();
+ expect(f.stopped).not.toHaveBeenCalled();
+ expect(state.windows[1].destroyed).toBe(false);
+ f.masks.stop();
+});
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplay.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplay.test.ts
index 50d1f3329a..b636d1f38b 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplay.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/viewerDisplay.test.ts
@@ -41,6 +41,63 @@ function fixture() {
}
describe('viewer-sized desktop ownership', () => {
+ it.each(['viewerDisplay', 'restoreViewerDisplay'] as const)(
+ 'revokes old safety effects and pending privacy initialization during %s',
+ async (op) => {
+ const f = fixture();
+ const { lease } = await f.start();
+ if (op === 'restoreViewerDisplay') {
+ await f.host.request('phone', { op: 'viewerDisplay', lease, width: 900, height: 1600 });
+ await f.host.request('phone', { op: 'control', lease, enabled: true });
+ }
+ let ready!: () => void;
+ f.deps.privacyScreen = vi.fn(
+ () =>
+ new Promise<void>((resolve) => {
+ ready = resolve;
+ }),
+ );
+ f.deps.stopPrivacyScreen = vi.fn();
+ f.deps.hostMute = vi.fn(async () => {});
+ f.deps.stopHostMute = vi.fn(async () => {});
+ f.deps.clipboardVersion = vi.fn(async () => 'old');
+ await f.host.request('phone', { op: 'hostMute', lease, enabled: true });
+ await f.host.request('phone', { op: 'clipboardSync', lease, enabled: true });
+ const privacy = f.host.request('phone', { op: 'privacyScreen', lease, enabled: true });
+ const rejected = expect(privacy).rejects.toThrow('DESKTOP_LEASE_EXPIRED');
+ await f.host.request('phone', { op, lease, width: 900, height: 1600 });
+ expect(f.deps.stopPrivacyScreen).toHaveBeenCalledOnce();
+ expect(f.deps.stopHostMute).toHaveBeenCalledOnce();
+ ready();
+ await rejected;
+ await f.host.request('phone', { op: 'control', lease, enabled: true });
+ await expect(f.host.request('phone', { op: 'clipboardVersion', lease })).rejects.toThrow(
+ 'DESKTOP_CLIPBOARD_UNAVAILABLE',
+ );
+ },
+ );
+ it('does not release input or create a display after disconnect during safety restoration', async () => {
+ const f = fixture();
+ const { lease } = await f.start();
+ let restore!: () => void;
+ const restoration = new Promise<void>((resolve) => {
+ restore = resolve;
+ });
+ f.deps.stopHostMute = vi.fn(() => restoration);
+ f.deps.releaseInput = vi.fn(async () => {});
+ const pending = f.host.request('phone', {
+ op: 'viewerDisplay',
+ lease,
+ width: 900,
+ height: 1600,
+ });
+ const rejected = expect(pending).rejects.toThrow('DESKTOP_LEASE_EXPIRED');
+ f.host.stop('phone');
+ restore();
+ await rejected;
+ expect(f.deps.releaseInput).not.toHaveBeenCalled();
+ expect(f.deps.createViewerDisplay).not.toHaveBeenCalled();
+ });
it.each(['stop', 'restoreViewerDisplay'] as const)(
'restores the original system mode after switching from a system mode to fit on %s',
async (ending) => {
@@ -273,6 +330,7 @@ describe('viewer-sized desktop ownership', () => {
height: 1600,
});
expect(f.deps.createViewerDisplay).not.toHaveBeenCalled();
+ await vi.waitFor(() => expect(release).toBeTypeOf('function'));
f.host.stop('phone');
release();
await expect(pending).rejects.toThrow('DESKTOP_LEASE_EXPIRED');
@@ -372,6 +430,7 @@ describe('viewer-sized desktop ownership', () => {
width: 900,
height: 1600,
});
+ await vi.waitFor(() => expect(resolve).toBeTypeOf('function'));
f.host.stop('phone');
resolve(f.handle);
await expect(pending).rejects.toThrow('DESKTOP_LEASE_EXPIRED');
diff --git a/apps/desktop/src/main/remote-desktop/clipboard.ts b/apps/desktop/src/main/remote-desktop/clipboard.ts
index 4c957e5b99..92d7bdd92d 100644
--- a/apps/desktop/src/main/remote-desktop/clipboard.ts
+++ b/apps/desktop/src/main/remote-desktop/clipboard.ts
@@ -1,7 +1,53 @@
import { clipboard, nativeImage } from 'electron';
-import { REMOTE_DESKTOP_MAX_CLIPBOARD_CHARS, type DesktopInput, type RemoteClipboardContent, parseClipboardContent, CLIPBOARD_MAX_CHARS } from '@cindy/device-link';
+import { createHash } from 'node:crypto';
+import {
+ REMOTE_DESKTOP_MAX_CLIPBOARD_CHARS,
+ type DesktopInput,
+ type RemoteClipboardContent,
+ parseClipboardContent,
+ CLIPBOARD_MAX_CHARS,
+} from '@cindy/device-link';
import { readDesktopClipboardVersion, readDesktopSelection } from './inputHost';
+const IMAGE_BYTES = 8 * 1024 * 1024;
+const IMAGE_PIXELS = 4_000_000;
+
+function checkImageSize(width: number, height: number): void {
+ if (
+ !Number.isSafeInteger(width) ||
+ !Number.isSafeInteger(height) ||
+ width <= 0 ||
+ height <= 0 ||
+ width > IMAGE_PIXELS / height
+ )
+ throw new Error('CLIPBOARD_TOO_LONG');
+}
+
+/** Bound native encoding before PNG allocation, and Base64/hash copies afterwards. */
+function clipboardPng(image: Electron.NativeImage): Buffer {
+ if (image.isEmpty()) return Buffer.alloc(0);
+ const size = image.getSize(1);
+ checkImageSize(size.width, size.height);
+ const png = image.toPNG({ scaleFactor: 1 });
+ if (png.length > IMAGE_BYTES) throw new Error('CLIPBOARD_TOO_LONG');
+ return png;
+}
+
+function incomingPng(encoded: string): Buffer {
+ if (encoded.length > Math.ceil(IMAGE_BYTES / 3) * 4) throw new Error('CLIPBOARD_TOO_LONG');
+ const bytes = Buffer.from(encoded, 'base64');
+ if (bytes.length > IMAGE_BYTES) throw new Error('CLIPBOARD_TOO_LONG');
+ if (
+ bytes.length < 33 ||
+ !bytes
+ .subarray(0, 16)
+ .equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82]))
+ )
+ throw new Error('CLIPBOARD_UNSUPPORTED');
+ checkImageSize(bytes.readUInt32BE(16), bytes.readUInt32BE(20));
+ return bytes;
+}
+
/** Explicit selection/text transfer. No background sync, logging or persistence. */
export async function transferDesktopClipboard(
action: 'copy' | 'paste',
@@ -56,23 +102,29 @@ export async function transferDesktopClipboardContent(
content: RemoteClipboardContent | undefined,
isCurrent: () => boolean,
input: (events: DesktopInput[]) => void,
-): Promise<RemoteClipboardContent | void> {
- const check = () => { if (!isCurrent()) throw new Error('DESKTOP_LEASE_EXPIRED'); };
+ options?: { sync?: boolean; version?: string },
+): Promise<RemoteClipboardContent | { version: string } | void> {
+ const check = () => {
+ if (!isCurrent()) throw new Error('DESKTOP_LEASE_EXPIRED');
+ };
check();
- input([{ kind: 'release' }]);
+ if (!options?.sync) input([{ kind: 'release' }]);
if (action === 'copy') {
- const selected = await readDesktopSelection(true);
+ const selected = options?.sync ? null : await readDesktopSelection(true);
check();
if (selected) return { text: selected };
const before = await readDesktopClipboardVersion(true);
check();
+ if (options?.version && before !== options.version)
+ throw new Error('DESKTOP_CLIPBOARD_CHANGED');
const formats = clipboard.availableFormats();
// A file-backed image may also expose a portable bitmap. Never read the
// file flavor or dereference its path; a file alone remains unsupported.
- const fileBacked = formats.some((format) => /file-url|filenames|hdrop|filecontents|filegroupdescriptor|uri-list/i.test(format));
+ const fileBacked = formats.some((format) =>
+ /file-url|filenames|hdrop|filecontents|filegroupdescriptor|uri-list/i.test(format),
+ );
const image = clipboard.readImage();
- if (fileBacked && image.isEmpty())
- throw new Error('CLIPBOARD_UNSUPPORTED');
+ if (fileBacked && image.isEmpty()) throw new Error('CLIPBOARD_UNSUPPORTED');
const snapshot: RemoteClipboardContent = {};
// File-manager text/HTML can be a local path rather than image content.
const text = fileBacked ? '' : clipboard.readText();
@@ -82,41 +134,59 @@ export async function transferDesktopClipboardContent(
if (html) snapshot.html = html;
if (rtf) snapshot.rtf = rtf;
if (!image.isEmpty()) {
- const size = image.getSize();
- if (size.width * size.height > 64_000_000) throw new Error('CLIPBOARD_TOO_LONG');
- snapshot.png = image.toPNG().toString('base64');
+ snapshot.png = clipboardPng(image).toString('base64');
}
const after = await readDesktopClipboardVersion(true);
check();
if (before !== after) throw new Error('DESKTOP_CLIPBOARD_CHANGED');
- if (!Object.keys(snapshot).length) throw new Error(formats.length ? 'CLIPBOARD_UNSUPPORTED' : 'CLIPBOARD_EMPTY');
+ if (!Object.keys(snapshot).length)
+ throw new Error(formats.length ? 'CLIPBOARD_UNSUPPORTED' : 'CLIPBOARD_EMPTY');
const json = JSON.stringify(snapshot);
if (json.length > CLIPBOARD_MAX_CHARS) throw new Error('CLIPBOARD_TOO_LONG');
return parseClipboardContent(json);
}
if (!content) throw new Error('CLIPBOARD_EMPTY');
+ const pngBytes = content.png ? incomingPng(content.png) : undefined;
const value = parseClipboardContent(JSON.stringify(content));
let image: Electron.NativeImage | undefined;
if (value.png) {
- const bytes = Buffer.from(value.png, 'base64');
- // Bound decompression before passing untrusted image bytes to nativeImage.
- if (bytes.length < 24 || bytes.readUInt32BE(16) * bytes.readUInt32BE(20) > 64_000_000)
- throw new Error('CLIPBOARD_TOO_LONG');
- image = nativeImage.createFromBuffer(bytes);
+ image = nativeImage.createFromBuffer(pngBytes!);
if (image.isEmpty()) throw new Error('CLIPBOARD_UNSUPPORTED');
}
- await readDesktopClipboardVersion();
+ const version = await readDesktopClipboardVersion();
check();
+ if (options?.version && version !== options.version) throw new Error('DESKTOP_CLIPBOARD_CHANGED');
clipboard.write({
...(value.text || value.url ? { text: value.text || value.url } : {}),
...(value.html ? { html: value.html } : {}),
...(value.rtf ? { rtf: value.rtf } : {}),
...(image ? { image } : {}),
});
- if ((image && clipboard.readImage().isEmpty()) ||
- (value.text && clipboard.readText() !== value.text))
+ if (
+ (image && clipboard.readImage().isEmpty()) ||
+ (value.text && clipboard.readText() !== value.text)
+ )
throw new Error('DESKTOP_CLIPBOARD_WRITE_FAILED');
check();
+ if (options?.sync) {
+ const fingerprint = () =>
+ createHash('sha256')
+ .update(
+ JSON.stringify([
+ clipboard.availableFormats(),
+ clipboard.readText(),
+ clipboard.readHTML(),
+ clipboard.readRTF(),
+ ]),
+ )
+ .update(clipboardPng(clipboard.readImage()))
+ .digest('hex');
+ const written = fingerprint();
+ const version = await readDesktopClipboardVersion(true);
+ check();
+ if (fingerprint() !== written) throw new Error('DESKTOP_CLIPBOARD_CHANGED');
+ return { version };
+ }
const modifier = process.platform === 'darwin' ? 'MetaLeft' : 'ControlLeft';
input([
{ kind: 'release' },
diff --git a/apps/desktop/src/main/remote-desktop/clipboardTransfer.ts b/apps/desktop/src/main/remote-desktop/clipboardTransfer.ts
index bbd2d31d3a..b706043bf7 100644
--- a/apps/desktop/src/main/remote-desktop/clipboardTransfer.ts
+++ b/apps/desktop/src/main/remote-desktop/clipboardTransfer.ts
@@ -1,47 +1,111 @@
import { randomUUID } from 'node:crypto';
import {
- CLIPBOARD_CHUNK_CHARS, CLIPBOARD_MAX_CHARS, parseClipboardContent,
- type ClipboardContentRequest, type RemoteClipboardContent,
+ CLIPBOARD_CHUNK_CHARS,
+ CLIPBOARD_MAX_CHARS,
+ parseClipboardContent,
+ type ClipboardContentRequest,
+ type RemoteClipboardContent,
} from '@cindy/device-link';
/** One bounded transfer per controller, released after 60s idle. Payloads never reach disk/logs. */
export class ClipboardTransfer {
- private value: { id: string; lease: string; length: number; data: string; direction: 'copy' | 'paste' } | null = null;
+ private value: {
+ id: string;
+ lease: string;
+ length: number;
+ data: string;
+ direction: 'copy' | 'paste';
+ sync?: boolean;
+ version?: string;
+ } | null = null;
private timer: ReturnType<typeof setTimeout> | undefined;
reset(): void {
clearTimeout(this.timer);
this.timer = undefined;
this.value = null;
}
+ resetSync(): void {
+ if (this.value?.sync) this.reset();
+ }
async handle(
request: ClipboardContentRequest,
isCurrent: () => boolean,
- transfer: (action: 'copy' | 'paste', content: RemoteClipboardContent | undefined, isCurrent: () => boolean) => Promise<RemoteClipboardContent | void>,
+ transfer: (
+ action: 'copy' | 'paste',
+ content: RemoteClipboardContent | undefined,
+ isCurrent: () => boolean,
+ options?: { sync?: boolean; version?: string },
+ ) => Promise<RemoteClipboardContent | { version: string } | void>,
): Promise<unknown> {
if (!isCurrent()) throw new Error('DESKTOP_LEASE_EXPIRED');
+ if (this.value && ['copy', 'begin', 'paste'].includes(request.action))
+ throw new Error('DESKTOP_CLIPBOARD_BUSY');
+ if (request.action === 'paste') {
+ if (!request.sync || !request.version || request.data.length > CLIPBOARD_CHUNK_CHARS)
+ throw new Error('INVALID_REQUEST');
+ this.reset();
+ const receipt = await transfer(
+ 'paste',
+ parseClipboardContent(request.data),
+ isCurrent,
+ request,
+ );
+ if (!isCurrent()) throw new Error('DESKTOP_LEASE_EXPIRED');
+ return receipt;
+ }
if (request.action === 'copy' || request.action === 'begin') {
this.reset();
- const data = request.action === 'copy' ? JSON.stringify(await transfer('copy', undefined, isCurrent)) : '';
+ const data =
+ request.action === 'copy'
+ ? JSON.stringify(
+ await (request.sync
+ ? transfer('copy', undefined, isCurrent, request)
+ : transfer('copy', undefined, isCurrent)),
+ )
+ : '';
if (!isCurrent()) throw new Error('DESKTOP_LEASE_EXPIRED');
if (typeof data !== 'string') throw new Error('CLIPBOARD_EMPTY');
const length = request.action === 'begin' ? request.length : data.length;
if (length > CLIPBOARD_MAX_CHARS) throw new Error('CLIPBOARD_TOO_LONG');
+ if (
+ request.action === 'copy' &&
+ request.sync &&
+ request.inline &&
+ length <= CLIPBOARD_CHUNK_CHARS
+ )
+ return { data, length };
const id = randomUUID();
- this.value = { id, lease: request.lease, length, data, direction: request.action === 'copy' ? 'copy' : 'paste' };
+ this.value = {
+ id,
+ lease: request.lease,
+ length,
+ data,
+ direction: request.action === 'copy' ? 'copy' : 'paste',
+ sync: request.sync,
+ version: request.version,
+ };
this.timer = setTimeout(() => this.reset(), 60_000);
this.timer.unref();
return { id, length };
}
const value = this.value;
- if (!value || value.id !== request.id || value.lease !== request.lease) throw new Error('CLIPBOARD_EXPIRED');
- if (request.action === 'cancel') { this.reset(); return { ok: true }; }
+ if (!value || value.id !== request.id || value.lease !== request.lease)
+ throw new Error('CLIPBOARD_EXPIRED');
+ if (Boolean(value.sync) !== Boolean(request.sync)) throw new Error('CLIPBOARD_EXPIRED');
+ if (request.action === 'cancel') {
+ this.reset();
+ return { ok: true };
+ }
if (request.action === 'read' && value.direction === 'copy') {
if (request.offset >= value.length) throw new Error('INVALID_REQUEST');
this.timer?.refresh(); // Slow links may need more than a minute for a complete item.
return { data: value.data.slice(request.offset, request.offset + CLIPBOARD_CHUNK_CHARS) };
}
if (request.action === 'write' && value.direction === 'paste') {
- if (request.offset !== value.data.length || value.data.length + request.data.length > value.length)
+ if (
+ request.offset !== value.data.length ||
+ value.data.length + request.data.length > value.length
+ )
throw new Error('INVALID_REQUEST');
value.data += request.data;
this.timer?.refresh();
@@ -50,8 +114,14 @@ export class ClipboardTransfer {
if (request.action === 'commit' && value.direction === 'paste') {
this.reset(); // Consume before any side effect: a repeated commit cannot paste twice.
if (value.data.length !== value.length) throw new Error('INVALID_REQUEST');
- await transfer('paste', parseClipboardContent(value.data), isCurrent);
- return { ok: true };
+ const content = parseClipboardContent(value.data);
+ const receipt = await (value.sync
+ ? transfer('paste', content, isCurrent, value)
+ : transfer('paste', content, isCurrent));
+ return {
+ ok: true,
+ ...(value.sync && receipt && 'version' in receipt ? { version: receipt.version } : {}),
+ };
}
throw new Error('INVALID_REQUEST');
}
diff --git a/apps/desktop/src/main/remote-desktop/controller.ts b/apps/desktop/src/main/remote-desktop/controller.ts
index 6d0be8433f..89411ef368 100644
--- a/apps/desktop/src/main/remote-desktop/controller.ts
+++ b/apps/desktop/src/main/remote-desktop/controller.ts
@@ -1,5 +1,6 @@
import { ClipboardTransfer } from './clipboardTransfer';
import { randomUUID } from 'node:crypto';
+import { createLogger } from '../logger';
import type { ViewerDisplayHandle } from './viewerDisplay';
import {
parseRemoteDesktopRequest,
@@ -18,6 +19,8 @@ import {
desktopPermissionReady,
} from '@cindy/device-link';
+const log = createLogger('remote-desktop:controller');
+
export interface DesktopControllerDeps {
authorized(peer: string): boolean;
/**
@@ -77,7 +80,13 @@ export interface DesktopControllerDeps {
action: 'copy' | 'paste',
content: RemoteClipboardContent | undefined,
isCurrent: () => boolean,
- ): Promise<RemoteClipboardContent | void>;
+ options?: { sync?: boolean; version?: string },
+ ): Promise<RemoteClipboardContent | { version: string } | void>;
+ clipboardVersion?(): Promise<string>;
+ privacyScreen?(enabled: boolean, isCurrent: () => boolean): Promise<void>;
+ stopPrivacyScreen?(): void;
+ hostMute?(enabled: boolean): Promise<void>;
+ stopHostMute?(): Promise<void>;
changed(): void;
now?: () => number;
}
@@ -92,6 +101,9 @@ export class RemoteDesktopController {
sequence: number;
authenticationSession: string | undefined;
backgroundViewing?: boolean;
+ clipboardSync?: boolean;
+ hostMute?: boolean;
+ privacyLockOnExit?: boolean;
})
| null = null;
private starting = false;
@@ -111,6 +123,33 @@ export class RemoteDesktopController {
private startingPeer: string | null = null;
private framePending = false;
private clipboardPending = false;
+ private syncGeneration = 0;
+ private privacyGeneration = 0;
+ private privacyOperation: { enabled: boolean; promise: Promise<{ enabled: boolean }> } | null =
+ null;
+ private async clearSafety(): Promise<void> {
+ this.privacyGeneration++;
+ this.privacyOperation = null;
+ this.syncGeneration++;
+ if (this.active) this.active.clipboardSync = false;
+ try {
+ this.deps.stopPrivacyScreen?.();
+ } finally {
+ await this.deps.stopHostMute?.();
+ }
+ }
+ /** Revoke input synchronously; optional OS restoration must never retain it
+ * or stop a replacement helper after an asynchronous restore settles.
+ */
+ private revokeControl(): Promise<void> {
+ if (this.active) this.active.controlling = false;
+ this.controlGeneration++;
+ this.clipboardTransfer.reset();
+ this.deps.stopInput();
+ const restoring = this.clearSafety();
+ this.deps.changed();
+ return restoring;
+ }
private clipboardTransfer = new ClipboardTransfer();
private lastFrame = -Infinity;
private controlGeneration = 0;
@@ -172,16 +211,19 @@ export class RemoteDesktopController {
)
this.stop();
}
- stop(peer?: string): void {
+ stop(peer?: string): Promise<void> {
if (peer && this.active?.peer !== peer) {
// Cancelling a takeover candidate must not revoke the current owner's work.
if (this.startingPeer === peer) this.startingPeer = null;
- return;
+ return Promise.resolve();
}
this.lockAbort?.abort();
if (this.active) this.lastEnded = { peer: this.active.peer, lease: this.active.lease };
this.clipboardTransfer.reset();
this.controlGeneration++;
+ const restoring = this.clearSafety().catch(() =>
+ log.warn('Safety restoration failed after stop'),
+ );
this.active = null;
this.deps.stopInput();
this.deps.stopVideo();
@@ -193,6 +235,7 @@ export class RemoteDesktopController {
void this.restoreStoppedDisplay().catch(() => {});
this.viewerGeometryManaged = false;
this.deps.changed();
+ return restoring;
}
private async temporaryResolution(
peer: string,
@@ -216,6 +259,8 @@ export class RemoteDesktopController {
try {
this.deps.stopInput();
this.deps.stopVideo();
+ await this.clearSafety();
+ requireCurrent();
await this.deps.releaseInput?.();
requireCurrent();
// Remove the temporary mirror before enumerating the physical monitor.
@@ -297,8 +342,8 @@ export class RemoteDesktopController {
}
/** Join the same restoration used by disconnects before the process exits. */
async stopAndRestore(): Promise<void> {
- this.stop();
- await this.restoreStoppedDisplay();
+ const safety = this.stop();
+ await Promise.all([safety, this.restoreStoppedDisplay()]);
}
/**
* Input injection failed while the lease is still valid. Input belongs to the
@@ -311,11 +356,7 @@ export class RemoteDesktopController {
releaseControl(): void {
const active = this.active;
if (!active || (!active.controlling && !this.inputStarting)) return;
- active.controlling = false;
- this.controlGeneration++;
- this.clipboardTransfer.reset();
- this.deps.stopInput();
- this.deps.changed();
+ void this.revokeControl().catch(() => log.warn('Safety restoration failed after control loss'));
}
/** Explicit local disconnect must not be undone by the phone's recovery. */
stopByUser(): void {
@@ -323,6 +364,22 @@ export class RemoteDesktopController {
if (target) this.userStopped.set(target.peer, target.lease);
this.stop();
}
+ async stopPrivacyByUser(): Promise<void> {
+ const active = this.active;
+ if (!active?.privacyLockOnExit) {
+ this.stopByUser();
+ return;
+ }
+ // Enter the existing lock/stop path before marking the lease stopped:
+ // subsequent phone input and recovery must be rejected while locking.
+ const ending = this.request(active.peer, { op: 'stop', lease: active.lease, lockScreen: true });
+ this.userStopped.set(active.peer, active.lease);
+ try {
+ await ending;
+ } finally {
+ if (this.active === active) this.stop(active.peer);
+ }
+ }
private require(peer: string, lease: string) {
this.tick();
if (this.userStopped.get(peer) === lease) throw new Error('DESKTOP_STOPPED');
@@ -466,6 +523,60 @@ export class RemoteDesktopController {
if (this.displayChanging && !['stop', 'heartbeat', 'frame', 'input'].includes(request.op))
throw new Error('DESKTOP_DISPLAY_BUSY');
switch (request.op) {
+ case 'privacyScreen': {
+ if (!active.controlling) throw new Error('DESKTOP_VIEW_ONLY');
+ if (!this.deps.privacyScreen) throw new Error('DESKTOP_PRIVACY_UNAVAILABLE');
+ active.privacyLockOnExit = request.lockOnExit === true;
+ // Lock preference changes join the same mask/capture initialization.
+ if (this.privacyOperation?.enabled === request.enabled)
+ return this.privacyOperation.promise;
+ const generation = ++this.privacyGeneration;
+ const current = () => {
+ this.tick();
+ return (
+ this.active === active && active.controlling && generation === this.privacyGeneration
+ );
+ };
+ const promise = this.deps
+ .privacyScreen(request.enabled, current)
+ .then(() => {
+ if (!current()) throw new Error('DESKTOP_LEASE_EXPIRED');
+ return { enabled: request.enabled };
+ })
+ .catch((error) => {
+ if (generation === this.privacyGeneration) this.privacyOperation = null;
+ throw error;
+ });
+ this.privacyOperation = { enabled: request.enabled, promise };
+ return promise;
+ }
+ case 'clipboardSync': {
+ if (request.enabled && (!active.controlling || !this.deps.clipboardVersion))
+ throw new Error('DESKTOP_CLIPBOARD_UNAVAILABLE');
+ if (active.clipboardSync !== request.enabled) {
+ this.syncGeneration++;
+ this.clipboardTransfer.resetSync();
+ }
+ active.clipboardSync = request.enabled;
+ return { enabled: request.enabled };
+ }
+ case 'hostMute': {
+ if (!active.controlling) throw new Error('DESKTOP_VIEW_ONLY');
+ if (!this.deps.hostMute) throw new Error('DESKTOP_HOST_MUTE_UNAVAILABLE');
+ active.hostMute = request.enabled;
+ await this.deps.hostMute(request.enabled);
+ return { enabled: request.enabled };
+ }
+ case 'clipboardVersion': {
+ if (!active.controlling || !active.clipboardSync || !this.deps.clipboardVersion)
+ throw new Error('DESKTOP_CLIPBOARD_UNAVAILABLE');
+ const generation = this.syncGeneration;
+ const version = await this.deps.clipboardVersion();
+ this.require(peer, request.lease);
+ if (!active.controlling || !active.clipboardSync || generation !== this.syncGeneration)
+ throw new Error('DESKTOP_LEASE_EXPIRED');
+ return { version };
+ }
case 'restoreViewerDisplay':
case 'viewerDisplay': {
if (!active.controlling) throw new Error('DESKTOP_VIEW_ONLY');
@@ -484,6 +595,10 @@ export class RemoteDesktopController {
// Wait for held keys/buttons to be released in the old coordinate space.
this.deps.stopInput();
this.deps.stopVideo();
+ // Safety effects belong to the old capture/control session, even though
+ // resizing retains its lease. Invalidate pending enables before waiting.
+ await this.clearSafety();
+ if (!current()) throw new Error('DESKTOP_LEASE_EXPIRED');
if (this.deps.releaseInput) await this.deps.releaseInput();
if (!current()) throw new Error('DESKTOP_LEASE_EXPIRED');
if (request.op === 'restoreViewerDisplay' && !this.viewerDisplay?.restore)
@@ -572,24 +687,23 @@ export class RemoteDesktopController {
case 'presentation': {
active.backgroundViewing = request.enabled;
if (request.enabled) {
- active.controlling = false;
- this.clipboardTransfer.reset();
- this.controlGeneration++;
- this.deps.stopInput();
+ await this.revokeControl();
+ } else {
+ this.deps.changed();
}
- this.deps.changed();
return { controlling: active.controlling };
}
case 'control': {
if (this.locking) throw new Error('DESKTOP_BUSY');
if (request.enabled) active.backgroundViewing = false;
if (request.enabled && this.inputStarting) throw new Error('DESKTOP_INPUT_BUSY');
+ if (!request.enabled) {
+ await this.revokeControl();
+ return { controlling: active.controlling };
+ }
this.clipboardTransfer.reset();
const generation = ++this.controlGeneration;
- if (!request.enabled) {
- active.controlling = false;
- this.deps.stopInput();
- } else if (!active.controlling) {
+ if (!active.controlling) {
this.inputStarting = true;
try {
await this.deps.startInput(active.display.id);
@@ -617,14 +731,24 @@ export class RemoteDesktopController {
case 'clipboardContent':
case 'clipboard': {
if (!active.controlling) throw new Error('DESKTOP_VIEW_ONLY');
+ if (request.op === 'clipboardContent' && request.sync && !active.clipboardSync)
+ throw new Error('DESKTOP_CLIPBOARD_UNAVAILABLE');
if (request.op === 'clipboard' ? !this.deps.clipboard : !this.deps.clipboardContent)
throw new Error('DESKTOP_CLIPBOARD_UNAVAILABLE');
if (this.clipboardPending) throw new Error('DESKTOP_CLIPBOARD_BUSY');
const generation = this.controlGeneration;
+ const syncGeneration = this.syncGeneration;
const isCurrent = () => {
this.tick();
return (
- this.active === active && active.controlling && generation === this.controlGeneration
+ this.active === active &&
+ active.controlling &&
+ generation === this.controlGeneration &&
+ !(
+ request.op === 'clipboardContent' &&
+ request.sync &&
+ (!active.clipboardSync || syncGeneration !== this.syncGeneration)
+ )
);
};
this.clipboardPending = true;
diff --git a/apps/desktop/src/main/remote-desktop/index.ts b/apps/desktop/src/main/remote-desktop/index.ts
index b250706302..45aca3ba99 100644
--- a/apps/desktop/src/main/remote-desktop/index.ts
+++ b/apps/desktop/src/main/remote-desktop/index.ts
@@ -12,6 +12,7 @@ import {
type DesktopCapturerSource,
} from 'electron';
import { randomUUID } from 'node:crypto';
+import { release as osRelease } from 'node:os';
import { loadDesktopIceServers } from './iceConfig';
import { remoteCredentialHost } from './credentialHost';
import {
@@ -44,12 +45,15 @@ import { desktopCaptureSource, enumerateDesktopSources } from './captureSource';
import { encodeDesktopFrame, encodeNativeRelayFrame } from './frame';
import { transferDesktopClipboard, transferDesktopClipboardContent } from './clipboard';
import { NativeDesktopCapture } from './nativeCapture';
+import { PrivacyScreen } from './privacyScreen';
+import { systemAudioMuteGuard } from '../voice-input/SystemAudioMuteGuard.js';
import { readWindowsDesktopSupport, configureWindowsDesktopSupport } from './windowsHost';
import {
DesktopInputHost,
readDesktopDisplayModes,
setDesktopDisplayMode,
readDesktopInputPermission,
+ readDesktopClipboardVersion,
readDesktopLockState,
lockDesktopScreen,
requestDesktopInputPermission,
@@ -99,6 +103,20 @@ const permissions = new RemoteDesktopPermissionsService({
let host: WebContents | null = null;
const captureWindow = new DesktopCaptureWindow(() => remoteDesktop.stop());
const nativeCapture = new NativeDesktopCapture();
+const privacyScreen = new PrivacyScreen(
+ (ids) => nativeCapture.setExcludedWindows(ids),
+ () => {
+ return remoteDesktop
+ .stopPrivacyByUser()
+ .catch((error) => console.error('[remote-desktop] privacy exit lock failed', error));
+ },
+ () => input.pauseForPrivacy(),
+);
+const supportsPrivacyScreen =
+ (process.platform === 'darwin' &&
+ typeof process.getSystemVersion === 'function' &&
+ Number(process.getSystemVersion().split('.')[0]) >= 14) ||
+ (process.platform === 'win32' && Number(osRelease().split('.')[2]) >= 19041);
let displayAwake: number | null = null;
let nativeDisplay: string | null = null;
let windowsAvailable = false;
@@ -349,14 +367,18 @@ export const remoteDesktop = new RemoteDesktopController({
cursorOverlay: process.platform === 'darwin' || (process.platform === 'win32' && windowsAvailable),
lockOnExit: process.platform === 'darwin',
clipboardContent: process.platform === 'darwin' || process.platform === 'win32',
+ clipboardSync: process.platform === 'darwin' || process.platform === 'win32',
+ clipboardInline: process.platform === 'darwin' || process.platform === 'win32',
+ privacyScreen: supportsPrivacyScreen,
+ hostMute: process.platform === 'darwin' || process.platform === 'win32',
clipboardText: process.platform === 'darwin' || process.platform === 'win32',
videoSettings: true,
trickleIce: true,
backgroundViewing: true,
systemAudio: supportsSystemAudio,
- displayModes: process.platform === 'darwin',
viewerDisplay,
viewerDisplayRestore: viewerDisplay,
+ displayModes: process.platform === 'darwin',
enabled,
canControl: process.platform === 'darwin' || process.platform === 'win32',
platform: process.platform,
@@ -399,8 +421,49 @@ export const remoteDesktop = new RemoteDesktopController({
},
clipboard: (action, text, isCurrent) =>
transferDesktopClipboard(action, text, isCurrent, (events) => input.input(events)),
- clipboardContent: (action, content, isCurrent) =>
- transferDesktopClipboardContent(action, content, isCurrent, (events) => input.input(events)),
+ clipboardContent: (action, content, isCurrent, options) =>
+ transferDesktopClipboardContent(
+ action,
+ content,
+ isCurrent,
+ (events) => input.input(events),
+ options,
+ ),
+ // Poll counters even for nonportable items; the content read owns format validation.
+ clipboardVersion: () => readDesktopClipboardVersion(),
+ privacyScreen: async (enabled, current) => {
+ if (!supportsPrivacyScreen) throw new Error('DESKTOP_PRIVACY_UNAVAILABLE');
+ if (enabled && process.platform === 'darwin' && videoLease && nativeDisplay) {
+ nativeOverlay = true;
+ await privacyScreen.set(true, current);
+ try {
+ await nativeCapture.preparePrivacy(nativeDisplay, nativeSettings);
+ } catch (error) {
+ if (current()) privacyScreen.stop();
+ throw error;
+ }
+ if (!current()) {
+ throw new Error('DESKTOP_LEASE_EXPIRED');
+ }
+ return;
+ }
+ await privacyScreen.set(enabled, current);
+ if (!enabled || process.platform === 'win32') return;
+ const deadline = Date.now() + 4500;
+ while (current() && !nativeCapture.privacyReady && Date.now() < deadline)
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ if (!current()) throw new Error('DESKTOP_LEASE_EXPIRED');
+ if (!nativeCapture.privacyReady) {
+ privacyScreen.stop();
+ throw new Error('DESKTOP_PRIVACY_UNAVAILABLE');
+ }
+ },
+ stopPrivacyScreen: () => privacyScreen.stop(),
+ hostMute: async (enabled) => {
+ if (enabled) await systemAudioMuteGuard.mute('remote-desktop');
+ else await systemAudioMuteGuard.restore('remote-desktop');
+ },
+ stopHostMute: () => systemAudioMuteGuard.restore('remote-desktop'),
displayModes: readDesktopDisplayModes,
displayPresent: (displayId) =>
screen.getAllDisplays().some((display) => String(display.id) === displayId),
@@ -472,6 +535,11 @@ export function registerRemoteDesktopIpc(
if (remoteDesktop.changingDisplay) return;
if (String(display.id) === remoteDesktop.displayId) remoteDesktop.stop();
});
+ screen.on('display-added', () => {
+ // A new screen invalidates mask coverage, not the selected capture geometry.
+ // Include masks still being prepared, before the controller reports enabled.
+ if (privacyScreen.active && !remoteDesktop.changingDisplay) remoteDesktop.stop();
+ });
screen.on('display-metrics-changed', (_event, display, metrics) => {
if (remoteDesktop.changingDisplay) return;
// Managed resolution changes can deliver scaleFactor after preparation
diff --git a/apps/desktop/src/main/remote-desktop/inputHost.ts b/apps/desktop/src/main/remote-desktop/inputHost.ts
index 9b060a5de5..1df71ac8cf 100644
--- a/apps/desktop/src/main/remote-desktop/inputHost.ts
+++ b/apps/desktop/src/main/remote-desktop/inputHost.ts
@@ -48,6 +48,7 @@ async function resolveBinary(): Promise<string> {
if (process.platform === 'darwin') digest.update(process.execPath).update('dev-caller-v1');
if (process.platform === 'win32') {
digest.update(await fs.readFile(path.join(sourceRoot, 'windows-input', 'src', 'desktop.rs')));
+ digest.update(await fs.readFile(path.join(sourceRoot, 'windows-input', 'src', 'privacy.rs')));
digest.update(
await fs.readFile(path.join(sourceRoot, 'windows-input', 'src', 'selection.rs')),
);
@@ -108,6 +109,8 @@ async function resolveBinary(): Promise<string> {
return build;
}
+export { resolveBinary as resolveDesktopInputBinary };
+
/** Probe the actual input process, never CuaDriver's or Electron's AX grant. */
export async function readDesktopLockState(): Promise<'locked' | 'unlocked' | 'unavailable'> {
if (process.platform !== 'darwin') return 'unavailable';
@@ -193,6 +196,7 @@ export class DesktopInputHost {
private generation = 0;
private heartbeat: ReturnType<typeof setInterval> | null = null;
private stopping: Promise<void> = Promise.resolve();
+ private privacyPaused = false;
constructor(
private readonly onFailure: () => void,
private readonly runtime: {
@@ -208,6 +212,10 @@ export class DesktopInputHost {
this.stop();
const platform = this.runtime.platform ?? process.platform;
if (platform !== 'darwin' && platform !== 'win32') throw new Error('DESKTOP_INPUT_UNSUPPORTED');
+ // A replacement helper is not usable until its ready handshake completes.
+ // In particular, canceling a privacy confirmation must keep dropping input
+ // throughout teardown, binary preparation and startup, without losing control.
+ this.privacyPaused = true;
const generation = this.generation;
await this.stopping;
if (generation !== this.generation) throw new Error('DESKTOP_LEASE_EXPIRED');
@@ -227,6 +235,7 @@ export class DesktopInputHost {
this.windows = connection;
this.displayId = displayId;
this.heartbeat = setInterval(() => this.write([]), 2000);
+ this.privacyPaused = false;
return;
}
const binary = await this.runtime.resolveBinary();
@@ -277,12 +286,15 @@ export class DesktopInputHost {
}
});
this.heartbeat = setInterval(() => this.write([]), 2000);
+ this.privacyPaused = false;
} catch (error) {
- if (generation === this.generation) this.stop();
+ if (generation !== this.generation) throw new Error('DESKTOP_LEASE_EXPIRED');
+ this.stop();
throw error;
}
}
input(events: DesktopInput[]): void {
+ if (this.privacyPaused) return;
const display = screen.getAllDisplays().find((item) => String(item.id) === this.displayId);
if (!display || (!this.child && !this.windows)) throw new Error('DESKTOP_INPUT_UNAVAILABLE');
if (events.length === 0) return;
@@ -351,8 +363,33 @@ export class DesktopInputHost {
this.stop();
await this.stopping;
}
+ /** Drain/release native input before a local-only confirmation gains focus.
+ * Restart only this input helper on cancel, never the media session or lease.
+ */
+ async pauseForPrivacy(): Promise<() => Promise<void>> {
+ if (!this.child && !this.windows) throw new Error('DESKTOP_INPUT_UNAVAILABLE');
+ const displayId = this.displayId;
+ this.stop();
+ // The old activity stays owned until native exit. Hold a separate existing
+ // activity reservation for the local confirmation, released by stop/start.
+ this.activity = new HumanDesktopInput();
+ this.activity.holdUntilExit();
+ this.privacyPaused = true;
+ const generation = this.generation;
+ await this.stopping;
+ return async () => {
+ if (generation !== this.generation) return;
+ try {
+ await this.start(displayId);
+ } catch (error) {
+ if (!(error instanceof Error && error.message === 'DESKTOP_LEASE_EXPIRED'))
+ this.onFailure();
+ }
+ };
+ }
stop(): void {
this.generation++;
+ this.privacyPaused = false;
this.acknowledge?.(new Error('DESKTOP_LEASE_EXPIRED'));
const windows = this.windows;
const writing = this.writing;
diff --git a/apps/desktop/src/main/remote-desktop/nativeCapture.ts b/apps/desktop/src/main/remote-desktop/nativeCapture.ts
index 7c616ff7ec..7f69ce181d 100644
--- a/apps/desktop/src/main/remote-desktop/nativeCapture.ts
+++ b/apps/desktop/src/main/remote-desktop/nativeCapture.ts
@@ -1,4 +1,8 @@
-import { isRemoteDesktopCursor, type RemoteDesktopCursorFrame, type RemoteDesktopVideoSettings } from '@cindy/device-link';
+import {
+ isRemoteDesktopCursor,
+ type RemoteDesktopCursorFrame,
+ type RemoteDesktopVideoSettings,
+} from '@cindy/device-link';
import { app, nativeImage, screen } from 'electron';
import { execFile, spawn, type ChildProcessWithoutNullStreams } from 'node:child_process';
import { createHash } from 'node:crypto';
@@ -53,6 +57,12 @@ async function binary(): Promise<string> {
'ImageIO',
'-framework',
'IOKit',
+ '-weak_framework',
+ 'ScreenCaptureKit',
+ '-framework',
+ 'CoreMedia',
+ '-framework',
+ 'CoreVideo',
'-o',
temporary,
],
@@ -74,6 +84,23 @@ async function binary(): Promise<string> {
* Neither adapter keeps frame history on disk.
*/
export class NativeDesktopCapture {
+ private excludedWindows = '';
+ private confirmedExclusion = '';
+ setExcludedWindows(ids: number[]): void {
+ const next = ids.join(',');
+ if (next === this.excludedWindows) return;
+ this.stop();
+ this.excludedWindows = next;
+ }
+ get privacyReady(): boolean {
+ return !!this.excludedWindows && this.confirmedExclusion === this.excludedWindows;
+ }
+ async preparePrivacy(display: string, settings?: RemoteDesktopVideoSettings): Promise<void> {
+ if (process.platform !== 'darwin') return;
+ this.stop();
+ const frame = await this.frame(display, true, settings);
+ if (!frame || !this.privacyReady) throw new Error('DESKTOP_PRIVACY_UNAVAILABLE');
+ }
private windows: WindowsDesktopConnection | null = null;
private child: ChildProcessWithoutNullStreams | null = null;
private display = '';
@@ -82,7 +109,11 @@ export class NativeDesktopCapture {
private busy = false;
private cancel: ((error?: Error) => void) | null = null;
- async frame(display: string, overlay = false, settings?: RemoteDesktopVideoSettings): Promise<string | RemoteDesktopCursorFrame | null> {
+ async frame(
+ display: string,
+ overlay = false,
+ settings?: RemoteDesktopVideoSettings,
+ ): Promise<string | RemoteDesktopCursorFrame | null> {
if (process.platform === 'win32') {
if (this.busy) return null;
const config = overlay ? 'overlay:' + (settings?.bitrate ?? 0) : '';
@@ -136,8 +167,13 @@ export class NativeDesktopCapture {
if (!this.child) {
const executable = await binary();
if (generation !== this.generation) return null;
- const quality = settings?.bitrate === 20_000_000 ? 0.95 : settings?.bitrate === 8_000_000 ? 0.8 : 0.65;
- const child = spawn(executable, overlay ? [display, 'cursor-overlay', String(settings?.fps ?? 30), String(quality)] : [display], { stdio: 'pipe' });
+ const quality =
+ settings?.bitrate === 20_000_000 ? 0.95 : settings?.bitrate === 8_000_000 ? 0.8 : 0.65;
+ const args = overlay
+ ? [display, 'cursor-overlay', String(settings?.fps ?? 30), String(quality)]
+ : [display];
+ if (this.excludedWindows) args.push(this.excludedWindows);
+ const child = spawn(executable, args, { stdio: 'pipe' });
this.child = child;
this.display = display;
this.config = config;
@@ -150,9 +186,11 @@ export class NativeDesktopCapture {
});
child.on('exit', (code) => {
if (this.child !== child) return;
- this.cancel?.(new Error(code === 3
- ? 'DESKTOP_SCREEN_PERMISSION_REQUIRED'
- : 'DESKTOP_VIDEO_UNAVAILABLE'));
+ this.cancel?.(
+ new Error(
+ code === 3 ? 'DESKTOP_SCREEN_PERMISSION_REQUIRED' : 'DESKTOP_VIDEO_UNAVAILABLE',
+ ),
+ );
this.stop();
});
}
@@ -160,6 +198,8 @@ export class NativeDesktopCapture {
return await new Promise<string | RemoteDesktopCursorFrame | null>((resolve, reject) => {
let text = '';
const finish = (frame: string | RemoteDesktopCursorFrame | null, error?: Error) => {
+ if (frame && generation === this.generation)
+ this.confirmedExclusion = this.excludedWindows;
clearTimeout(timer);
child.stdout.off('data', receive);
this.cancel = null;
@@ -176,11 +216,17 @@ export class NativeDesktopCapture {
if (overlay) {
try {
const value = JSON.parse(text) as RemoteDesktopCursorFrame;
- if (typeof value.jpeg !== 'string' || value.jpeg.length > 1_333_336 ||
- !/^[A-Za-z0-9+/]+={0,2}$/.test(value.jpeg) || (value.cursor !== null && !isRemoteDesktopCursor(value.cursor)))
+ if (
+ typeof value.jpeg !== 'string' ||
+ value.jpeg.length > 1_333_336 ||
+ !/^[A-Za-z0-9+/]+={0,2}$/.test(value.jpeg) ||
+ (value.cursor !== null && !isRemoteDesktopCursor(value.cursor))
+ )
throw new Error('INVALID_CURSOR_FRAME');
finish(generation === this.generation ? value : null);
- } catch { this.stop(); }
+ } catch {
+ this.stop();
+ }
return;
}
const jpeg = text.slice(0, -1);
@@ -202,6 +248,7 @@ export class NativeDesktopCapture {
}
stop(): void {
+ this.confirmedExclusion = '';
this.generation++;
this.windows?.close();
this.windows = null;
diff --git a/apps/desktop/src/main/remote-desktop/privacyInput.ts b/apps/desktop/src/main/remote-desktop/privacyInput.ts
new file mode 100644
index 0000000000..6c0777cd42
--- /dev/null
+++ b/apps/desktop/src/main/remote-desktop/privacyInput.ts
@@ -0,0 +1,94 @@
+import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process';
+import { resolveDesktopInputBinary } from './inputHost';
+
+export interface PrivacyInputMonitor {
+ confirm(): Promise<void>;
+ resume(): Promise<void>;
+ stop(): Promise<void>;
+}
+
+/** The existing signed helper runs in a separate, mask-owned observation mode.
+ * Only fixed status words cross stdout; no key, text or pointer data leaves it.
+ * Native hooks consume the initiating physical input, then fence injected input
+ * before the local dialog opens. EOF/watchdog always unhooks.
+ */
+export async function watchPrivacyInput(
+ localInput: () => void,
+ failed: () => void,
+ runtime = {
+ resolveBinary: resolveDesktopInputBinary,
+ spawn: (binary: string): ChildProcessWithoutNullStreams =>
+ spawn(binary, ['--privacy-input'], { stdio: 'pipe', windowsHide: true }),
+ },
+): Promise<PrivacyInputMonitor> {
+ const child = runtime.spawn(await runtime.resolveBinary());
+ // Process close proves the OS has removed its hooks, unlike merely sending EOF.
+ const closed = new Promise<void>((resolve) => child.once('close', () => resolve()));
+ let stopped = false;
+ let buffer = '';
+ let pending: { expected: string; done(error?: Error): void } | undefined;
+ let heartbeat: ReturnType<typeof setInterval> | undefined;
+ const stop = () => {
+ if (stopped) return closed;
+ stopped = true;
+ clearInterval(heartbeat);
+ pending?.done(new Error('DESKTOP_PRIVACY_UNAVAILABLE'));
+ child.stdin.end();
+ const timer = setTimeout(() => child.kill(), 1000);
+ timer.unref();
+ child.once('exit', () => clearTimeout(timer));
+ return closed;
+ };
+ const fail = () => {
+ if (stopped) return;
+ stop();
+ failed();
+ };
+ const wait = (expected: string, command?: string): Promise<void> =>
+ new Promise((resolve, reject) => {
+ if (stopped || pending) return reject(new Error('DESKTOP_PRIVACY_UNAVAILABLE'));
+ const timer = setTimeout(fail, 5000);
+ pending = {
+ expected,
+ done(error) {
+ clearTimeout(timer);
+ pending = undefined;
+ error ? reject(error) : resolve();
+ },
+ };
+ if (command) child.stdin.write(`${command}\n`);
+ });
+ const ready = wait('ready');
+ child.stderr.resume();
+ child.on('error', fail);
+ child.on('exit', fail);
+ child.stdin.on('error', fail);
+ child.stdout.on('data', (data: Buffer) => {
+ if (stopped) return;
+ buffer += data.toString();
+ if (buffer.length > 1024) return fail();
+ let end: number;
+ while ((end = buffer.indexOf('\n')) >= 0) {
+ const line = buffer.slice(0, end).trim();
+ buffer = buffer.slice(end + 1);
+ if (line === pending?.expected) pending.done();
+ else if (line === 'local-input') localInput();
+ else return fail();
+ }
+ });
+ heartbeat = setInterval(() => {
+ if (child.stdin.writableLength > 1024) fail();
+ else child.stdin.write('ping\n');
+ }, 1000);
+ try {
+ await ready;
+ return {
+ confirm: () => wait('confirmed', 'confirm'),
+ resume: () => wait('ready', 'resume'),
+ stop,
+ };
+ } catch (error) {
+ stop();
+ throw error;
+ }
+}
diff --git a/apps/desktop/src/main/remote-desktop/privacyScreen.ts b/apps/desktop/src/main/remote-desktop/privacyScreen.ts
new file mode 100644
index 0000000000..bb40c73c01
--- /dev/null
+++ b/apps/desktop/src/main/remote-desktop/privacyScreen.ts
@@ -0,0 +1,244 @@
+import { app, BrowserWindow, dialog, screen, session } from 'electron';
+import { createLogger } from '../logger';
+import { t } from '../i18n';
+import { privacyScreenHtml } from './privacyScreenHtml';
+import { watchPrivacyInput, type PrivacyInputMonitor } from './privacyInput';
+
+const log = createLogger('remote-desktop:privacy');
+
+/** Ephemeral physical-screen masks, not reusable tool windows.
+ * No preload or persistent state. Destroy on
+ * every lease end; native physical input opens the local disconnect confirmation.
+ */
+export class PrivacyScreen {
+ private windows: BrowserWindow[] = [];
+ private generation = 0;
+ private partitionConfigured = false;
+ private confirming = false;
+ private monitor: PrivacyInputMonitor | null = null;
+ get active(): boolean {
+ return this.windows.length > 0;
+ }
+ constructor(
+ private readonly excluded: (ids: number[]) => void,
+ private readonly stopped: () => void | Promise<void>,
+ private readonly suspendInput: () => Promise<() => Promise<void>>,
+ private readonly watchInput = watchPrivacyInput,
+ ) {}
+ private async disconnect(generation: number): Promise<void> {
+ if (generation !== this.generation) return;
+ // Invalidate callbacks immediately, but retain masks and capture exclusions
+ // until the controller has finished locking and ended the viewing lease.
+ const ending = ++this.generation;
+ try {
+ // The confirming hook rejects injected input, including our lock shortcut.
+ // Keep the masks, but wait for hook teardown before asking the OS to lock.
+ await this.monitor?.stop();
+ if (ending !== this.generation) return;
+ await this.stopped();
+ } catch {
+ log.warn('privacy disconnect failed');
+ } finally {
+ if (ending === this.generation) this.stop();
+ }
+ }
+ private async confirmExit(window: BrowserWindow, generation: number): Promise<void> {
+ if (this.confirming || generation !== this.generation || window.isDestroyed()) return;
+ this.confirming = true;
+ let resumeInput: (() => Promise<void>) | undefined;
+ try {
+ // No local dialog exists while the old input helper drains/releases.
+ // Then the native hook blocks even late injected events before focus moves.
+ resumeInput = await this.suspendInput();
+ if (generation !== this.generation || !this.monitor) return;
+ await this.monitor.confirm();
+ if (generation !== this.generation) return;
+ window.setFocusable(true);
+ window.setIgnoreMouseEvents(false);
+ app.focus({ steal: true });
+ window.focus();
+ const { response } = await dialog.showMessageBox(window, {
+ type: 'question',
+ message: t('privacyExit.title'),
+ detail: t('privacyExit.detail'),
+ buttons: [t('privacyExit.cancel'), t('privacyExit.disconnect')],
+ defaultId: 0,
+ cancelId: 0,
+ noLink: true,
+ });
+ if (response === 1 && generation === this.generation) {
+ resumeInput = undefined;
+ await this.disconnect(generation);
+ }
+ } catch (error) {
+ log.warn('privacy exit confirmation failed', error);
+ if (generation === this.generation) {
+ resumeInput = undefined;
+ await this.disconnect(generation);
+ }
+ } finally {
+ if (generation === this.generation) {
+ window.setIgnoreMouseEvents(true);
+ window.setFocusable(false);
+ try {
+ // Restore input while the native fence is still up. Only then re-arm
+ // local exit; a fresh physical click cannot be lost during resume.
+ await resumeInput?.();
+ resumeInput = undefined;
+ if (generation === this.generation) {
+ this.confirming = false;
+ await this.monitor?.resume();
+ }
+ } catch {
+ if (generation === this.generation) {
+ resumeInput = undefined;
+ await this.disconnect(generation);
+ }
+ }
+ }
+ await resumeInput?.();
+ }
+ }
+ async set(enabled: boolean, current: () => boolean): Promise<void> {
+ if (!enabled) {
+ this.stop();
+ return;
+ }
+ if (!current()) throw new Error('DESKTOP_LEASE_EXPIRED');
+ if (this.windows.length) return;
+ const generation = ++this.generation;
+ const created: BrowserWindow[] = [];
+ const partition = session.fromPartition('cindy-desktop-privacy', { cache: false });
+ if (!this.partitionConfigured) {
+ partition.setPermissionCheckHandler(() => false);
+ partition.setPermissionRequestHandler((_webContents, _permission, callback) =>
+ callback(false),
+ );
+ partition.webRequest.onBeforeRequest((details, callback) =>
+ callback({ cancel: !details.url.startsWith('data:') }),
+ );
+ this.partitionConfigured = true;
+ }
+ const displays = screen.getAllDisplays();
+ try {
+ for (const display of displays) {
+ const window = new BrowserWindow({
+ ...display.bounds,
+ show: false,
+ frame: false,
+ // A frameless macOS window still gets the platform's rounded mask by
+ // default. Disable it so the physical display corners cannot reveal
+ // the captured desktop underneath.
+ roundedCorners: false,
+ enableLargerThanScreen: true,
+ acceptFirstMouse: true,
+ focusable: false,
+ resizable: false,
+ movable: false,
+ minimizable: false,
+ maximizable: false,
+ fullscreenable: false,
+ skipTaskbar: true,
+ hasShadow: false,
+ // Functional blackout pixels, independent of the application's theme.
+ backgroundColor: '#000000',
+ webPreferences: {
+ session: partition,
+ sandbox: true,
+ contextIsolation: true,
+ nodeIntegration: false,
+ nodeIntegrationInSubFrames: false,
+ nodeIntegrationInWorker: false,
+ webSecurity: true,
+ allowRunningInsecureContent: false,
+ experimentalFeatures: false,
+ plugins: false,
+ navigateOnDragDrop: false,
+ devTools: false,
+ },
+ });
+ created.push(window);
+ if (generation === this.generation) this.windows.push(window);
+ // Each mask follows its physical display, including non-captured screens.
+ const relayout = (_event: unknown, changed: Electron.Display) => {
+ if (window.isDestroyed() || changed.id !== display.id) return;
+ try {
+ window.setBounds(changed.bounds, false);
+ } catch {
+ void this.disconnect(generation);
+ }
+ };
+ screen.on('display-metrics-changed', relayout);
+ window.on('closed', () => {
+ screen.removeListener('display-metrics-changed', relayout);
+ void this.disconnect(generation);
+ });
+ window.setMenuBarVisibility(false);
+ await window.loadURL(
+ `data:text/html;charset=utf-8,${encodeURIComponent(privacyScreenHtml(t('privacyExit.status'), t('privacyExit.hint')))}`,
+ );
+ window.webContents.setWindowOpenHandler(() => ({ action: 'deny' }));
+ window.webContents.on('will-navigate', (event) => event.preventDefault());
+ window.setContentProtection(true);
+ // Remote system injection must reach the app behind this passive mask.
+ // Physical input is identified and consumed by the native hook instead.
+ window.setIgnoreMouseEvents(true);
+ window.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
+ window.setAlwaysOnTop(true, 'screen-saver', 1);
+ window.webContents.on('render-process-gone', () => {
+ void this.disconnect(generation);
+ });
+ }
+ if (!current() || generation !== this.generation) throw new Error('DESKTOP_LEASE_EXPIRED');
+ const ids = this.windows.map((window) => Number(window.getMediaSourceId().split(':')[1]));
+ if (!ids.length || ids.some((id) => !Number.isSafeInteger(id) || id <= 0))
+ throw new Error('DESKTOP_PRIVACY_UNAVAILABLE');
+ this.excluded(ids);
+ const monitor = await this.watchInput(
+ () => {
+ if (generation === this.generation) void this.confirmExit(this.windows[0], generation);
+ },
+ () => {
+ void this.disconnect(generation);
+ },
+ );
+ if (!current() || generation !== this.generation) {
+ monitor.stop();
+ throw new Error('DESKTOP_LEASE_EXPIRED');
+ }
+ this.monitor = monitor;
+ for (const [index, window] of this.windows.entries()) {
+ const target = screen
+ .getAllDisplays()
+ .find((display) => display.id === displays[index].id)?.bounds;
+ if (!target) throw new Error('DESKTOP_PRIVACY_UNAVAILABLE');
+ window.showInactive();
+ window.setBounds(target, false);
+ log.debug('mask shown', {
+ windowId: window.id,
+ target,
+ actual: window.getBounds(),
+ visible: window.isVisible(),
+ focused: window.isFocused(),
+ alwaysOnTop: window.isAlwaysOnTop(),
+ });
+ }
+ } catch (error) {
+ if (generation === this.generation) this.stop();
+ else {
+ for (const window of created) if (!window.isDestroyed()) window.destroy();
+ }
+ throw error;
+ }
+ }
+ stop(): void {
+ this.generation++;
+ this.confirming = false;
+ this.monitor?.stop();
+ this.monitor = null;
+ const windows = this.windows;
+ this.windows = [];
+ for (const window of windows) if (!window.isDestroyed()) window.destroy();
+ this.excluded([]);
+ }
+}
diff --git a/apps/desktop/src/main/remote-desktop/privacyScreenHtml.ts b/apps/desktop/src/main/remote-desktop/privacyScreenHtml.ts
new file mode 100644
index 0000000000..208d42c2c7
--- /dev/null
+++ b/apps/desktop/src/main/remote-desktop/privacyScreenHtml.ts
@@ -0,0 +1,35 @@
+import illustration from '../../renderer/assets/splash/illustration.webp?inline';
+import wordmarkDark from '../../renderer/assets/splash/wordmark.png?inline';
+import wordmarkLight from '../../renderer/assets/splash/wordmark-light.png?inline';
+
+const escape = (value: string) =>
+ value.replace(
+ /[&<>"']/g,
+ (char) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[char]!,
+ );
+
+/** Bundled artwork only: the privacy partition never needs file or network access. */
+export function privacyScreenHtml(status: string, hint: string): string {
+ return `<!doctype html><html><head><meta charset="utf-8">
+<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src data:; style-src 'unsafe-inline'">
+<style>
+/* Isolated surface: mirrors the built-in surface/text semantic tokens. */
+:root{color-scheme:light dark;--surface:#f8f8f6;--text-primary:#262626;--text-secondary:#737373}
+@media(prefers-color-scheme:dark){:root{--surface:#1f1f1e;--text-primary:#d4d4d4;--text-secondary:#a3a3a3}}
+*{box-sizing:border-box}html,body{margin:0;width:100%;height:100%;overflow:hidden;background:var(--surface);color:var(--text-primary)}
+body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;user-select:none;display:grid;place-items:center;padding:40px}
+main{width:min(1100px,92vw);display:flex;align-items:center;justify-content:center;gap:clamp(24px,4vw,72px)}
+.hero{width:min(48vw,62vh,600px);height:auto;flex-shrink:0;animation:arrive 650ms ease-out both}
+.copy{max-width:460px;animation:arrive 650ms 100ms ease-out both}
+.wordmark{display:block;width:clamp(160px,18vw,230px);height:auto;margin-bottom:32px}
+h1{font-size:clamp(21px,2.2vw,32px);font-weight:500;line-height:1.5;letter-spacing:-.02em;margin:0;text-wrap:balance}
+p{font-size:14px;line-height:1.8;color:var(--text-secondary);margin:20px 0 0;text-wrap:balance}
+@keyframes arrive{from{opacity:0;transform:translateY(12px)}to{opacity:1;transform:none}}
+@media(prefers-reduced-motion:reduce){.hero,.copy{animation:none}}
+@media(max-aspect-ratio:1/1){main{flex-direction:column;gap:16px;text-align:center}.hero{width:min(68vw,46vh)}.wordmark{margin:0 auto 20px}.copy{max-width:85vw}h1{font-size:22px}}
+</style></head><body><main>
+<img class="hero" src="${illustration}" alt="">
+<section class="copy"><picture><source media="(prefers-color-scheme:dark)" srcset="${wordmarkDark}"><img class="wordmark" src="${wordmarkLight}" alt="Cindy"></picture>
+<h1>${escape(status)}</h1><p>${escape(hint)}</p></section>
+</main></body></html>`;
+}
diff --git a/apps/desktop/src/main/voice-input/SystemAudioMuteGuard.ts b/apps/desktop/src/main/voice-input/SystemAudioMuteGuard.ts
index 179b4d7252..569741b173 100644
--- a/apps/desktop/src/main/voice-input/SystemAudioMuteGuard.ts
+++ b/apps/desktop/src/main/voice-input/SystemAudioMuteGuard.ts
@@ -4,6 +4,9 @@ import { createLogger } from '../logger.js';
const log = createLogger('voice-input:system-audio');
+// WebContents owners are numeric; the remote session has a separate identity.
+type AudioMuteOwner = number | 'remote-desktop';
+
type AudioSnapshot = {
outputMuted: boolean;
};
@@ -28,9 +31,10 @@ async function loadLoudness(): Promise<LoudnessModule | null> {
if (loudnessCache !== undefined) return loudnessCache;
try {
const mod = await import('loudness');
- loudnessCache = ((mod as { default?: LoudnessModule }).default ?? (mod as unknown as LoudnessModule));
+ loudnessCache =
+ (mod as { default?: LoudnessModule }).default ?? (mod as unknown as LoudnessModule);
} catch (err) {
- log.warn('failed to load loudness on Windows; voice input mute will be no-op', {
+ log.warn('failed to load loudness on Windows', {
error: err instanceof Error ? err.message : String(err),
});
loudnessCache = null;
@@ -52,28 +56,34 @@ const SUPPORTS_MUTE = process.platform === 'darwin' || process.platform === 'win
* - other: no-op (graceful degradation).
*/
export class SystemAudioMuteGuard {
- private readonly owners = new Set<number>();
+ private readonly owners = new Set<AudioMuteOwner>();
private snapshot: AudioSnapshot | null = null;
private tail: Promise<void> = Promise.resolve();
- async mute(ownerId: number): Promise<void> {
+ async mute(ownerId: AudioMuteOwner): Promise<void> {
if (!SUPPORTS_MUTE) return;
await this.enqueue(async () => {
if (this.owners.has(ownerId)) return;
- if (this.owners.size === 0) {
+ if (this.snapshot === null) {
this.snapshot = await muteOutputAndReadSnapshot();
log.info('muted for voice input', { ownerId, wasMuted: this.snapshot.outputMuted });
+ } else if (this.owners.size === 0) {
+ // A rejected restore may still have reached the OS. Reassert mute for
+ // the new owner without replacing the outstanding original snapshot.
+ await setOutputMuted(true);
}
this.owners.add(ownerId);
});
}
- async restore(ownerId: number): Promise<void> {
+ async restore(ownerId: AudioMuteOwner): Promise<void> {
if (!SUPPORTS_MUTE) return;
await this.enqueue(async () => {
- if (!this.owners.delete(ownerId)) return;
+ this.owners.delete(ownerId);
if (this.owners.size > 0) return;
+ // A failed OS restore leaves the snapshot pending, even after its owner
+ // has ended. A later restore or mute cycle must retain that original state.
const snapshot = this.snapshot;
if (!snapshot) return;
await setOutputMuted(snapshot.outputMuted);
@@ -97,7 +107,9 @@ export class SystemAudioMuteGuard {
private enqueue(job: () => Promise<void>): Promise<void> {
const next = this.tail.then(job, job);
this.tail = next.catch((error) => {
- log.warn('system audio mute job failed', { error: error instanceof Error ? error.message : String(error) });
+ log.warn('system audio mute job failed', {
+ error: error instanceof Error ? error.message : String(error),
+ });
});
return next;
}
@@ -129,7 +141,7 @@ async function readOutputMuted(): Promise<boolean> {
}
if (process.platform === 'win32') {
const l = await loadLoudness();
- if (!l) return false;
+ if (!l) throw new Error('SYSTEM_AUDIO_UNAVAILABLE');
return l.getMuted();
}
return false;
@@ -137,12 +149,14 @@ async function readOutputMuted(): Promise<boolean> {
async function setOutputMuted(muted: boolean): Promise<void> {
if (process.platform === 'darwin') {
- await runOsascript([muted ? 'set volume with output muted' : 'set volume without output muted']);
+ await runOsascript([
+ muted ? 'set volume with output muted' : 'set volume without output muted',
+ ]);
return;
}
if (process.platform === 'win32') {
const l = await loadLoudness();
- if (!l) return;
+ if (!l) throw new Error('SYSTEM_AUDIO_UNAVAILABLE');
await l.setMuted(muted);
return;
}
diff --git a/apps/desktop/src/main/voice-input/__tests__/SystemAudioMuteGuard.test.ts b/apps/desktop/src/main/voice-input/__tests__/SystemAudioMuteGuard.test.ts
new file mode 100644
index 0000000000..91bc1df2e4
--- /dev/null
+++ b/apps/desktop/src/main/voice-input/__tests__/SystemAudioMuteGuard.test.ts
@@ -0,0 +1,75 @@
+import { beforeEach, afterEach, expect, it, vi } from 'vitest';
+
+const audio = vi.hoisted(() => ({ getMuted: vi.fn(), setMuted: vi.fn() }));
+vi.mock('loudness', () => ({ default: audio }));
+vi.mock('../../logger.js', () => ({ createLogger: () => ({ info: vi.fn(), warn: vi.fn() }) }));
+
+beforeEach(() => {
+ vi.resetModules();
+ vi.doMock('loudness', () => ({ default: audio }));
+ vi.spyOn(process, 'platform', 'get').mockReturnValue('win32');
+ audio.getMuted.mockReset().mockResolvedValue(false);
+ audio.setMuted.mockReset().mockResolvedValue(undefined);
+});
+afterEach(() => vi.restoreAllMocks());
+
+it.each(['restore', 'restoreAll'] as const)(
+ 'retains original audio state after failed %s through a new owner',
+ async (method) => {
+ const { SystemAudioMuteGuard } = await import('../SystemAudioMuteGuard');
+ const guard = new SystemAudioMuteGuard();
+ await guard.mute(1);
+ audio.setMuted.mockRejectedValueOnce(new Error('OS unavailable'));
+ await expect(method === 'restore' ? guard.restore(1) : guard.restoreAll()).rejects.toThrow(
+ 'OS unavailable',
+ );
+ await guard.mute(2);
+ await guard.restore(1);
+ expect(audio.setMuted.mock.calls).toEqual([[true], [false], [true]]);
+ await guard.restore(2);
+ expect(audio.getMuted).toHaveBeenCalledTimes(1);
+ expect(audio.setMuted.mock.calls).toEqual([[true], [false], [true], [false]]);
+ },
+);
+
+it('allows a failed last-owner restore to be retried without another mute', async () => {
+ const { SystemAudioMuteGuard } = await import('../SystemAudioMuteGuard');
+ const guard = new SystemAudioMuteGuard();
+ await guard.mute(1);
+ audio.setMuted.mockRejectedValueOnce(new Error('OS unavailable'));
+ await expect(guard.restore(1)).rejects.toThrow();
+ await guard.restore(1);
+ expect(audio.setMuted.mock.calls).toEqual([[true], [false], [false]]);
+});
+
+it('rejects mute when the Windows backend cannot load, including cached failures', async () => {
+ vi.doMock('loudness', () => {
+ throw new Error('module missing');
+ });
+ const { SystemAudioMuteGuard } = await import('../SystemAudioMuteGuard');
+ const guard = new SystemAudioMuteGuard();
+ await expect(guard.mute(1)).rejects.toThrow('SYSTEM_AUDIO_UNAVAILABLE');
+ await expect(guard.mute(2)).rejects.toThrow('SYSTEM_AUDIO_UNAVAILABLE');
+ expect(audio.getMuted).not.toHaveBeenCalled();
+ expect(audio.setMuted).not.toHaveBeenCalled();
+});
+
+it.each([true, false])(
+ 'keeps remote and WebContents owners independent, remote first=%s',
+ async (remoteFirst) => {
+ const { SystemAudioMuteGuard } = await import('../SystemAudioMuteGuard');
+ const guard = new SystemAudioMuteGuard();
+ const owners = remoteFirst
+ ? ['remote-desktop' as const, 0xc1d0]
+ : [0xc1d0, 'remote-desktop' as const];
+ await guard.mute(owners[0]);
+ await guard.mute(owners[1]);
+ await guard.mute(owners[0]);
+ await guard.restore(owners[0]);
+ await guard.restore(owners[0]);
+ expect(audio.getMuted).toHaveBeenCalledTimes(1);
+ expect(audio.setMuted.mock.calls).toEqual([[true]]);
+ await guard.restore(owners[1]);
+ expect(audio.setMuted.mock.calls).toEqual([[true], [false]]);
+ },
+);
diff --git a/apps/desktop/src/renderer/i18n/locales/en/common.json b/apps/desktop/src/renderer/i18n/locales/en/common.json
index 22d7d84701..2249950fec 100644
--- a/apps/desktop/src/renderer/i18n/locales/en/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/en/common.json
@@ -1,4 +1,12 @@
{
+ "privacyExit": {
+ "status": "This computer is being remotely controlled through Cindy",
+ "hint": "Click or press any key to disconnect…",
+ "title": "Disconnect remote desktop?",
+ "detail": "This will end remote control and restore this computer’s screen. The computer will lock if lock on exit is enabled.",
+ "cancel": "Keep connected",
+ "disconnect": "Disconnect"
+ },
"appBadge": {
"attentionCount": "Tasks needing attention: {{count}}"
},
diff --git a/apps/desktop/src/renderer/i18n/locales/ja/common.json b/apps/desktop/src/renderer/i18n/locales/ja/common.json
index 703de7ce1e..0c057aefc0 100644
--- a/apps/desktop/src/renderer/i18n/locales/ja/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ja/common.json
@@ -1,4 +1,12 @@
{
+ "privacyExit": {
+ "status": "Cindy でリモート操作されています",
+ "hint": "クリックまたは任意のキーを押すと、切断を確認できます",
+ "title": "リモートデスクトップを切断しますか?",
+ "detail": "リモート操作を終了し、パソコンの画面を元に戻します。終了時のロックが有効な場合は、パソコンもロックされます。",
+ "cancel": "接続を維持",
+ "disconnect": "切断"
+ },
"appBadge": {
"attentionCount": "対応が必要なセッション:{{count}}"
},
diff --git a/apps/desktop/src/renderer/i18n/locales/ko/common.json b/apps/desktop/src/renderer/i18n/locales/ko/common.json
index 7ff3b79535..4de16c40d2 100644
--- a/apps/desktop/src/renderer/i18n/locales/ko/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ko/common.json
@@ -1,4 +1,12 @@
{
+ "privacyExit": {
+ "status": "Cindy를 통해 원격 제어 중입니다",
+ "hint": "화면을 클릭하거나 아무 키나 누르면 연결 끊기를 확인합니다",
+ "title": "원격 데스크톱 연결을 끊을까요?",
+ "detail": "원격 제어를 종료하고 컴퓨터 화면을 복원합니다. 종료 시 잠금이 켜져 있으면 컴퓨터도 잠깁니다.",
+ "cancel": "연결 유지",
+ "disconnect": "연결 끊기"
+ },
"appBadge": {
"attentionCount": "확인이 필요한 세션: {{count}}"
},
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
index 0d67fb92a4..ce8ff755d9 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
@@ -1,4 +1,12 @@
{
+ "privacyExit": {
+ "status": "正在通过 Cindy 被远程控制",
+ "hint": "点击屏幕或按任意键,确认后断开连接",
+ "title": "断开远程桌面连接?",
+ "detail": "这将结束远程控制并恢复电脑屏幕。如果已开启退出时锁屏,电脑也会锁屏。",
+ "cancel": "保持连接",
+ "disconnect": "断开连接"
+ },
"appBadge": {
"attentionCount": "需要关注的任务:{{count}}"
},
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
index 99329d5681..6de1dff447 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-TW/common.json
@@ -1,4 +1,12 @@
{
+ "privacyExit": {
+ "status": "正在透過 Cindy 被遠端控制",
+ "hint": "點擊螢幕或按任意鍵,確認後中斷連線",
+ "title": "中斷遠端桌面連線?",
+ "detail": "這將結束遠端控制並恢復電腦螢幕。如果已開啟結束時鎖定螢幕,電腦也會鎖定。",
+ "cancel": "保持連線",
+ "disconnect": "中斷連線"
+ },
"appBadge": {
"attentionCount": "需要關注的任務:{{count}}"
},
diff --git a/apps/mobile/modules/cindy-remote-presentation/android/build.gradle b/apps/mobile/modules/cindy-remote-presentation/android/build.gradle
new file mode 100644
index 0000000000..726e0d93c0
--- /dev/null
+++ b/apps/mobile/modules/cindy-remote-presentation/android/build.gradle
@@ -0,0 +1,15 @@
+plugins {
+ id 'com.android.library'
+ id 'expo-module-gradle-plugin'
+}
+group = 'com.cindy.remotepresentation'
+version = '0.1.0'
+android {
+ namespace 'com.cindy.remotepresentation'
+ defaultConfig {
+ minSdkVersion 24
+ versionCode 1
+ versionName '0.1.0'
+ }
+}
+dependencies { implementation 'androidx.core:core:1.15.0' }
diff --git a/apps/mobile/modules/cindy-remote-presentation/android/src/main/AndroidManifest.xml b/apps/mobile/modules/cindy-remote-presentation/android/src/main/AndroidManifest.xml
new file mode 100644
index 0000000000..fbc1b5db07
--- /dev/null
+++ b/apps/mobile/modules/cindy-remote-presentation/android/src/main/AndroidManifest.xml
@@ -0,0 +1,9 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+ <application>
+ <provider android:name="com.cindy.remotepresentation.RemoteClipboardProvider"
+ android:authorities="${applicationId}.remoteclipboard" android:exported="false"
+ android:grantUriPermissions="true">
+ <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/cindy_remote_clipboard_paths" />
+ </provider>
+ </application>
+</manifest>
diff --git a/apps/mobile/modules/cindy-remote-presentation/android/src/main/java/com/cindy/remotepresentation/CindyRemotePresentationModule.kt b/apps/mobile/modules/cindy-remote-presentation/android/src/main/java/com/cindy/remotepresentation/CindyRemotePresentationModule.kt
new file mode 100644
index 0000000000..9118d9ab0a
--- /dev/null
+++ b/apps/mobile/modules/cindy-remote-presentation/android/src/main/java/com/cindy/remotepresentation/CindyRemotePresentationModule.kt
@@ -0,0 +1,235 @@
+package com.cindy.remotepresentation
+
+import android.content.ClipData
+import android.content.ClipboardManager
+import android.content.Context
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
+import android.net.Uri
+import android.util.Base64
+import expo.modules.kotlin.modules.Module
+import expo.modules.kotlin.modules.ModuleDefinition
+import expo.modules.kotlin.functions.Queues
+import expo.modules.kotlin.functions.Coroutine
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+import android.os.Build
+import android.os.Handler
+import android.os.Looper
+import java.util.concurrent.atomic.AtomicLong
+import android.text.Html
+import androidx.core.content.FileProvider
+import java.io.File
+import java.util.UUID
+import org.json.JSONObject
+import java.io.ByteArrayOutputStream
+
+// Bound encoded buffers before Bitmap/JSON/Base64 can multiply their footprint.
+private const val IMAGE_BYTES = 8 * 1024 * 1024
+private const val IMAGE_PIXELS = 4_000_000L
+private fun decodeClipboardPng(encoded: String): ByteArray {
+ if (encoded.length > ((IMAGE_BYTES + 2) / 3) * 4) throw Exception("CLIPBOARD_TOO_LONG")
+ val bytes = Base64.decode(encoded, Base64.DEFAULT)
+ if (bytes.size > IMAGE_BYTES) throw Exception("CLIPBOARD_TOO_LONG")
+ if (bytes.size < 33 || !bytes.copyOfRange(0, 16).contentEquals(
+ byteArrayOf(-119,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82)))
+ throw Exception("CLIPBOARD_UNSUPPORTED")
+ fun dimension(offset: Int): Long = (offset until offset + 4).fold(0L) { value, index ->
+ (value shl 8) or (bytes[index].toLong() and 255L)
+ }
+ val width = dimension(16)
+ val height = dimension(20)
+ if (width <= 0 || height <= 0 || width > IMAGE_PIXELS / height) throw Exception("CLIPBOARD_TOO_LONG")
+ return bytes
+}
+private class ClipboardImageOutput : ByteArrayOutputStream(8192) {
+ override fun write(value: Int) {
+ if (count >= IMAGE_BYTES) throw Exception("CLIPBOARD_TOO_LONG")
+ super.write(value)
+ }
+ override fun write(bytes: ByteArray, offset: Int, length: Int) {
+ if (length > IMAGE_BYTES - count) throw Exception("CLIPBOARD_TOO_LONG")
+ super.write(bytes, offset, length)
+ }
+ fun base64(): String = Base64.encodeToString(buf, 0, count, Base64.NO_WRAP)
+}
+
+class CindyRemotePresentationModule : Module() {
+ private val clipboard: ClipboardManager
+ get() = appContext.reactContext!!.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
+
+ // No payload is retained: notifications and foreground transitions only
+ // invalidate the version. Atomic because Expo lifecycle callbacks may run
+ // outside the MAIN queue used by clipboard operations.
+ private val clipboardGeneration = AtomicLong(0)
+ private val clipboardEpoch = UUID.randomUUID().toString()
+ private var observedClipboard: ClipboardManager? = null
+ private val clipboardListener = ClipboardManager.OnPrimaryClipChangedListener {
+ clipboardGeneration.incrementAndGet()
+ }
+
+ override fun definition() = ModuleDefinition {
+ Name("CindyRemotePresentation")
+ OnActivityEntersForeground { clipboardGeneration.incrementAndGet() }
+ OnDestroy {
+ Handler(Looper.getMainLooper()).post {
+ observedClipboard?.removePrimaryClipChangedListener(clipboardListener)
+ observedClipboard = null
+ }
+ }
+ // Android WebView owns playback; only iOS needs an AVAudioSession override.
+ AsyncFunction("playback") { _: Boolean -> Unit }
+ AsyncFunction("clipboardVersion") { foreground(); clipboardVersion() }.runOnQueue(Queues.MAIN)
+ AsyncFunction("readClipboard") Coroutine { ->
+ val clip = withContext(Dispatchers.Main.immediate) {
+ foreground()
+ clipboard.primaryClip ?: throw Exception("CLIPBOARD_EMPTY")
+ }
+ val result = withContext(Dispatchers.IO) { readClipboard(clip) }
+ withContext(Dispatchers.Main.immediate) { foreground() }
+ result
+ }
+ AsyncFunction("syncClipboard") Coroutine { json: String, version: String -> writeClipboard(json, version) }
+ AsyncFunction("writeClipboard") Coroutine { json: String -> writeClipboard(json, null); Unit }
+ }
+
+ private fun foreground() {
+ if (appContext.currentActivity?.hasWindowFocus() != true) throw Exception("CLIPBOARD_NOT_ALLOWED")
+ }
+
+ // Poll only a change token, never primaryClip or item text/HTML/URI. The
+ // description timestamp also detects a change whose listener callback is
+ // still queued; API 24/25 use the listener and foreground invalidation.
+ private fun clipboardVersion(): String {
+ val manager = clipboard
+ if (observedClipboard == null) {
+ manager.addPrimaryClipChangedListener(clipboardListener)
+ observedClipboard = manager
+ }
+ val stamp = if (Build.VERSION.SDK_INT >= 26) manager.primaryClipDescription?.timestamp else null
+ return "$clipboardEpoch:${clipboardGeneration.get()}:$stamp"
+ }
+
+ private fun readClipboard(clip: ClipData): String {
+ if (clip.itemCount != 1) throw Exception("CLIPBOARD_UNSUPPORTED")
+ val item = clip.getItemAt(0)
+ val result = JSONObject()
+ item.text?.toString()?.takeIf { it.isNotEmpty() }?.let { result.put("text", it) }
+ item.htmlText?.takeIf { it.isNotEmpty() }?.let { result.put("html", it) }
+ item.uri?.let { uri -> if (uri.scheme == "http" || uri.scheme == "https") result.put("url", uri.toString()) }
+ item.uri?.takeIf { it.scheme == "content" }?.let { uri ->
+ val resolver = appContext.reactContext!!.contentResolver
+ if (resolver.getType(uri)?.startsWith("image/") == true) {
+ val bytes = resolver.openInputStream(uri)?.use { input ->
+ val output = ClipboardImageOutput()
+ val buffer = ByteArray(8192)
+ while (true) {
+ val count = input.read(buffer)
+ if (count < 0) break
+ output.write(buffer, 0, count)
+ }
+ output.toByteArray()
+ } ?: throw Exception("CLIPBOARD_UNSUPPORTED")
+ val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
+ BitmapFactory.decodeByteArray(bytes, 0, bytes.size, bounds)
+ if (bounds.outWidth <= 0 || bounds.outHeight <= 0 || bounds.outWidth.toLong() * bounds.outHeight > IMAGE_PIXELS)
+ throw Exception("CLIPBOARD_TOO_LONG")
+ val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) ?: throw Exception("CLIPBOARD_UNSUPPORTED")
+ try {
+ val output = ClipboardImageOutput()
+ if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, output)) throw Exception("CLIPBOARD_TOO_LONG")
+ bitmap.recycle()
+ result.put("png", output.base64())
+ } finally { bitmap.recycle() }
+ }
+ }
+ if (result.length() == 0) throw Exception("CLIPBOARD_UNSUPPORTED")
+ return result.toString().also { if (it.length > 32 * 1024 * 1024) throw Exception("CLIPBOARD_TOO_LONG") }
+ }
+
+ private suspend fun writeClipboard(json: String, expectedVersion: String?): String = withContext(Dispatchers.IO) {
+ withContext(Dispatchers.Main.immediate) {
+ foreground()
+ if (expectedVersion != null && clipboardVersion() != expectedVersion) throw Exception("CLIPBOARD_CHANGED")
+ }
+ if (json.length > 32 * 1024 * 1024) throw Exception("CLIPBOARD_TOO_LONG")
+ val content = JSONObject(json)
+ val html = content.optString("html", null)
+ val text = content.optString("text", null) ?: html?.let { Html.fromHtml(it, Html.FROM_HTML_MODE_LEGACY).toString() }
+ val uri = content.optString("url", null)?.let { Uri.parse(it) }
+ val png = content.optString("png", null)
+ // Process incarnation, shared by all module instances without an ownership registry.
+ val pendingPrefix = "${android.os.Process.myPid()}-${android.os.Process.getStartElapsedRealtime()}-"
+ var imageFile: File? = null
+ var committed = false
+ try {
+ val clip = when {
+ png != null -> {
+ val bytes = decodeClipboardPng(png)
+ val context = appContext.reactContext!!
+ val directory = File(context.cacheDir, "remote-clipboard").apply { mkdirs() }
+ // Other writes must not reclaim an image that is still being prepared.
+ imageFile = File(directory, "$pendingPrefix${UUID.randomUUID()}.png.pending")
+ imageFile!!.writeBytes(bytes)
+ val publishedFile = File(directory, imageFile!!.name.removeSuffix(".pending"))
+ val imageUri = FileProvider.getUriForFile(context, "${context.packageName}.remoteclipboard", publishedFile)
+ val mimeTypes = listOfNotNull(
+ "image/png",
+ text?.let { "text/plain" },
+ html?.let { "text/html" },
+ ).toTypedArray()
+ ClipData("Cindy", mimeTypes, ClipData.Item(text, html, null, imageUri))
+ }
+ text != null && html != null -> ClipData.newHtmlText("Cindy", text, html)
+ text != null -> ClipData.newPlainText("Cindy", text)
+ uri != null -> ClipData.newRawUri("Cindy", uri)
+ else -> throw Exception("CLIPBOARD_UNSUPPORTED")
+ }
+ withContext(Dispatchers.Main.immediate) {
+ foreground()
+ if (expectedVersion != null && clipboardVersion() != expectedVersion) throw Exception("CLIPBOARD_CHANGED")
+ imageFile?.let { pending ->
+ val published = File(pending.parentFile, pending.name.removeSuffix(".pending"))
+ if (!pending.renameTo(published)) throw Exception("CLIPBOARD_UNAVAILABLE")
+ imageFile = published
+ }
+ clipboard.setPrimaryClip(clip)
+ committed = true
+ // Change the token before returning even if the notification is delayed.
+ clipboardGeneration.incrementAndGet()
+ // Every replacement (including text/URL) reclaims published images.
+ // Inspect metadata in this non-suspending publication block so concurrent
+ // writes cannot escape the cap or lose their pending file. No image IO.
+ // Failure to inspect the current clipboard means retaining every file.
+ runCatching {
+ val current = clipboard.primaryClip ?: return@runCatching
+ val retained = (0 until current.itemCount).mapNotNull { current.getItemAt(it).uri }.toSet()
+ val context = appContext.reactContext!!
+ val images = File(context.cacheDir, "remote-clipboard").listFiles()
+ ?.filter { it.extension == "png" || it.name.endsWith(".png.pending") }
+ ?.sortedByDescending { it.lastModified() } ?: emptyList()
+ var graceFiles = 0
+ for (file in images) {
+ val uri = FileProvider.getUriForFile(context, "${context.packageName}.remoteclipboard", file)
+ if (uri in retained) continue
+ if (file.name.endsWith(".png.pending")) {
+ // Only stale files from previous processes (including legacy names).
+ // Never age out this process's work, even across module recreation
+ // or a long suspension. Pending files were never published as URIs.
+ if (!file.name.startsWith(pendingPrefix) && System.currentTimeMillis() - file.lastModified() > 3_600_000) file.delete()
+ continue
+ }
+ // At most three previous images (24 MiB) get a read grace period.
+ if (System.currentTimeMillis() - file.lastModified() <= 3_600_000 && graceFiles < 3) {
+ graceFiles++
+ } else file.delete()
+ }
+ }
+ clipboardVersion()
+ }
+ } finally {
+ // Cancellation after publishing must not delete the image Android now owns.
+ if (!committed) imageFile?.delete()
+ }
+ }
+}
diff --git a/apps/mobile/modules/cindy-remote-presentation/android/src/main/java/com/cindy/remotepresentation/RemoteClipboardProvider.kt b/apps/mobile/modules/cindy-remote-presentation/android/src/main/java/com/cindy/remotepresentation/RemoteClipboardProvider.kt
new file mode 100644
index 0000000000..d20335da12
--- /dev/null
+++ b/apps/mobile/modules/cindy-remote-presentation/android/src/main/java/com/cindy/remotepresentation/RemoteClipboardProvider.kt
@@ -0,0 +1,5 @@
+package com.cindy.remotepresentation
+
+import androidx.core.content.FileProvider
+
+class RemoteClipboardProvider : FileProvider()
diff --git a/apps/mobile/modules/cindy-remote-presentation/android/src/main/res/xml/cindy_remote_clipboard_paths.xml b/apps/mobile/modules/cindy-remote-presentation/android/src/main/res/xml/cindy_remote_clipboard_paths.xml
new file mode 100644
index 0000000000..7f6acd8dc6
--- /dev/null
+++ b/apps/mobile/modules/cindy-remote-presentation/android/src/main/res/xml/cindy_remote_clipboard_paths.xml
@@ -0,0 +1,3 @@
+<paths xmlns:android="http://schemas.android.com/apk/res/android">
+ <cache-path name="clipboard" path="remote-clipboard/" />
+</paths>
diff --git a/apps/mobile/modules/cindy-remote-presentation/expo-module.config.json b/apps/mobile/modules/cindy-remote-presentation/expo-module.config.json
index 98dfc33574..2807681d87 100644
--- a/apps/mobile/modules/cindy-remote-presentation/expo-module.config.json
+++ b/apps/mobile/modules/cindy-remote-presentation/expo-module.config.json
@@ -1,11 +1,10 @@
{
- "platforms": [
- "apple"
- ],
+ "platforms": ["apple", "android"],
"apple": {
"podspecPath": "./ios/CindyRemotePresentation.podspec",
- "modules": [
- "CindyRemotePresentationModule"
- ]
+ "modules": ["CindyRemotePresentationModule"]
+ },
+ "android": {
+ "modules": ["com.cindy.remotepresentation.CindyRemotePresentationModule"]
}
}
diff --git a/apps/mobile/modules/cindy-remote-presentation/ios/CindyRemotePresentationModule.swift b/apps/mobile/modules/cindy-remote-presentation/ios/CindyRemotePresentationModule.swift
index 637d4307de..74a99d8db6 100644
--- a/apps/mobile/modules/cindy-remote-presentation/ios/CindyRemotePresentationModule.swift
+++ b/apps/mobile/modules/cindy-remote-presentation/ios/CindyRemotePresentationModule.swift
@@ -7,6 +7,18 @@ public class CindyRemotePresentationModule: Module {
private var previousAudio: (AVAudioSession.Category, AVAudioSession.Mode, AVAudioSession.CategoryOptions)?
public func definition() -> ModuleDefinition {
Name("CindyRemotePresentation")
+ AsyncFunction("clipboardVersion") { () -> String in
+ try RemoteClipboard.foreground()
+ return String(UIPasteboard.general.changeCount)
+ }.runOnQueue(.main)
+ AsyncFunction("syncClipboard") { (json: String, version: String) -> String in
+ try RemoteClipboard.foreground()
+ guard String(UIPasteboard.general.changeCount) == version else {
+ throw RemoteClipboard.failure("CLIPBOARD_CHANGED")
+ }
+ try RemoteClipboard.write(json, expectedVersion: version)
+ return String(UIPasteboard.general.changeCount)
+ }.runOnQueue(.main)
AsyncFunction("readClipboard") { () -> String in
try RemoteClipboard.read()
}.runOnQueue(.main)
@@ -104,11 +116,17 @@ enum RemoteClipboard {
}
if let image = image {
guard (image.images?.count ?? 1) <= 1 else { throw failure("CLIPBOARD_UNSUPPORTED") }
- guard image.size.width * image.scale * image.size.height * image.scale <= 64_000_000 else {
+ guard RemoteClipboardSize.acceptsImage(
+ width: Double(image.size.width * image.scale), height: Double(image.size.height * image.scale)
+ ) else {
+ throw failure("CLIPBOARD_TOO_LONG")
+ }
+ if let bitmap = image.cgImage,
+ !RemoteClipboardSize.acceptsImage(width: Double(bitmap.width), height: Double(bitmap.height)) {
throw failure("CLIPBOARD_TOO_LONG")
}
guard let png = image.pngData() else { throw failure("CLIPBOARD_UNSUPPORTED") }
- guard png.count <= limit * 3 / 4 else { throw failure("CLIPBOARD_TOO_LONG") }
+ guard png.count <= RemoteClipboardSize.imageBytes else { throw failure("CLIPBOARD_TOO_LONG") }
result["png"] = png.base64EncodedString()
}
guard !result.isEmpty else { throw failure("CLIPBOARD_UNSUPPORTED") }
@@ -120,7 +138,7 @@ enum RemoteClipboard {
return json
}
- static func write(_ json: String) throws {
+ static func write(_ json: String, expectedVersion: String? = nil) throws {
try foreground()
guard RemoteClipboardSize.accepts(json) else { throw failure("CLIPBOARD_TOO_LONG") }
guard let data = json.data(using: .utf8),
@@ -139,15 +157,24 @@ enum RemoteClipboard {
item["public.url"] = url
}
if let encoded = content["png"] {
+ guard encoded.utf8.count <= ((RemoteClipboardSize.imageBytes + 2) / 3) * 4 else {
+ throw failure("CLIPBOARD_TOO_LONG")
+ }
guard let png = Data(base64Encoded: encoded), png.count >= 24,
Array(png.prefix(8)) == [137, 80, 78, 71, 13, 10, 26, 10] else { throw failure("CLIPBOARD_UNSUPPORTED") }
+ guard png.count <= RemoteClipboardSize.imageBytes else { throw failure("CLIPBOARD_TOO_LONG") }
let width = png[16..<20].reduce(UInt64(0)) { ($0 << 8) | UInt64($1) }
let height = png[20..<24].reduce(UInt64(0)) { ($0 << 8) | UInt64($1) }
- guard width > 0, height > 0, width * height <= 64_000_000,
- UIImage(data: png) != nil else { throw failure("CLIPBOARD_UNSUPPORTED") }
+ guard RemoteClipboardSize.acceptsImage(width: Double(width), height: Double(height)) else {
+ throw failure("CLIPBOARD_TOO_LONG")
+ }
+ guard UIImage(data: png) != nil else { throw failure("CLIPBOARD_UNSUPPORTED") }
item["public.png"] = png
}
try foreground()
+ if let expectedVersion = expectedVersion, String(UIPasteboard.general.changeCount) != expectedVersion {
+ throw failure("CLIPBOARD_CHANGED")
+ }
UIPasteboard.general.setItems([item], options: [:])
}
}
diff --git a/apps/mobile/modules/cindy-remote-presentation/ios/RemoteClipboardSize.swift b/apps/mobile/modules/cindy-remote-presentation/ios/RemoteClipboardSize.swift
index b3a846054a..321c5d8788 100644
--- a/apps/mobile/modules/cindy-remote-presentation/ios/RemoteClipboardSize.swift
+++ b/apps/mobile/modules/cindy-remote-presentation/ios/RemoteClipboardSize.swift
@@ -4,6 +4,14 @@ enum RemoteClipboardSize {
// CLIPBOARD_MAX_CHARS in packages/device-link/src/remoteClipboard.ts:
// JavaScript string.length counts UTF-16 code units, not UTF-8 bytes or graphemes.
static let limit = 32 * 1024 * 1024
+ static let imageBytes = 8 * 1024 * 1024
+ static let imagePixels = 4_000_000.0
+
+ // Check before UIImage decoding/PNG encoding, including non-finite geometry.
+ static func acceptsImage(width: Double, height: Double) -> Bool {
+ width.isFinite && height.isFinite && width > 0 && height > 0
+ && width <= imagePixels / height
+ }
static func accepts(_ string: String) -> Bool {
string.utf16.count <= limit
diff --git a/apps/mobile/modules/cindy-remote-presentation/src/index.ts b/apps/mobile/modules/cindy-remote-presentation/src/index.ts
index 7266a36342..8b4301858d 100644
--- a/apps/mobile/modules/cindy-remote-presentation/src/index.ts
+++ b/apps/mobile/modules/cindy-remote-presentation/src/index.ts
@@ -2,6 +2,8 @@ import { requireOptionalNativeModule } from "expo-modules-core";
export const remotePresentation = requireOptionalNativeModule<{
readClipboard?(): Promise<string>;
writeClipboard?(json: string): Promise<void>;
- rotate(landscape: boolean): Promise<void>;
+ clipboardVersion?(): Promise<string>;
+ syncClipboard?(json: string, version: string): Promise<string>;
+ rotate?(landscape: boolean): Promise<void>;
playback(enabled: boolean): Promise<void>;
}>("CindyRemotePresentation");
diff --git a/apps/mobile/scripts/fixtures/remote-clipboard-size.swift b/apps/mobile/scripts/fixtures/remote-clipboard-size.swift
index 7793041ead..b02ee48b28 100644
--- a/apps/mobile/scripts/fixtures/remote-clipboard-size.swift
+++ b/apps/mobile/scripts/fixtures/remote-clipboard-size.swift
@@ -4,6 +4,12 @@ import Darwin
@main
struct ClipboardSizeTests {
static func main() throws {
+ precondition(RemoteClipboardSize.acceptsImage(width: 2000, height: 2000))
+ precondition(!RemoteClipboardSize.acceptsImage(width: 2001, height: 2000))
+ precondition(!RemoteClipboardSize.acceptsImage(width: 8000, height: 8000))
+ precondition(!RemoteClipboardSize.acceptsImage(width: 0, height: 2000))
+ precondition(!RemoteClipboardSize.acceptsImage(width: .infinity, height: 1))
+ precondition(!RemoteClipboardSize.acceptsImage(width: 1, height: .nan))
let data = try Data(contentsOf: URL(fileURLWithPath: CommandLine.arguments[1]))
let string = String(data: data, encoding: .utf8)!
let expected = CommandLine.arguments[2] == "true"
diff --git a/apps/mobile/src/i18n/locales/en/remoteDesktop.json b/apps/mobile/src/i18n/locales/en/remoteDesktop.json
index b4f6d7cbc0..aaaeb11e80 100644
--- a/apps/mobile/src/i18n/locales/en/remoteDesktop.json
+++ b/apps/mobile/src/i18n/locales/en/remoteDesktop.json
@@ -1,4 +1,16 @@
{
+ "hostDisconnected": "The computer ended the remote desktop connection.",
+ "privacyScreen": "Computer privacy screen",
+ "privacyScreenHint": "Cover the computer’s screens. Click or press a key on the computer, then confirm to disconnect. Lock on exit applies if enabled.",
+ "privacyActive": "Privacy screen is on. Click or press a key on the computer, then confirm to disconnect. Lock on exit applies if enabled.",
+ "privacyFailed": "Privacy screen could not be enabled. The computer’s screens may still be visible.",
+ "clipboardSync": "Clipboard sync",
+ "clipboardSyncHint": "Sync text, images and rich text both ways while in the foreground. Allow paste access if iPhone asks.",
+ "clipboardSyncConflict": "Both clipboards changed. Both copies were kept. Copy again on either device to sync.",
+ "clipboardSyncFailed": "Sync temporarily failed. Retrying automatically; your setting stays on.",
+ "clipboardSyncSkipped": "This content could not be synced and was skipped. Check paste access, or copy text or an image to try again.",
+ "clipboardSyncPermission": "Sync is paused. Allow paste access, then retry. Your setting stays on.",
+ "clipboardSyncRetry": "Retry sync",
"credentialSigningRequired": "The Mac development build is missing its signing configuration. Restart Cindy on the Mac with signing configured, then try again.",
"credentialPasswordRejected": "The Mac login password was not accepted. Enter it again.",
"credentialCancelled": "Password verification was cancelled. Tap reconnect when you are ready.",
@@ -137,5 +149,8 @@
"autoUnlockUnavailable": "Automatic unlock is unavailable. You can still connect and unlock the Mac manually.",
"lockOnExit": "Lock Computer on Exit",
"lockOnExitHint": "Locks when you leave remote desktop. Reconnecting or switching apps does not lock the computer.",
- "lockOnExitFailed": "Could not confirm that the computer locked. Check the computer before leaving it unattended."
+ "lockOnExitFailed": "Could not confirm that the computer locked. Check the computer before leaving it unattended.",
+ "hostMute": "Mute controlled computer",
+ "hostMuteHint": "Mute the controlled computer while connected",
+ "hostMuteFailed": "Unable to change controlled computer mute state"
}
diff --git a/apps/mobile/src/i18n/locales/ja/remoteDesktop.json b/apps/mobile/src/i18n/locales/ja/remoteDesktop.json
index 2057af49e3..2603576c31 100644
--- a/apps/mobile/src/i18n/locales/ja/remoteDesktop.json
+++ b/apps/mobile/src/i18n/locales/ja/remoteDesktop.json
@@ -1,4 +1,16 @@
{
+ "hostDisconnected": "パソコン側でリモートデスクトップ接続が切断されました。",
+ "privacyScreen": "パソコンのプライバシースクリーン",
+ "privacyScreenHint": "パソコンの画面を隠します。パソコンでクリックまたはキーを押し、確認すると切断します。終了時のロック設定も適用されます。",
+ "privacyActive": "プライバシースクリーンが有効です。パソコンでクリックまたはキーを押し、確認すると切断します。終了時のロック設定も適用されます。",
+ "privacyFailed": "プライバシースクリーンを有効にできませんでした。パソコンの画面が見えている可能性があります。",
+ "clipboardSync": "クリップボード同期",
+ "clipboardSyncHint": "アプリの使用中にテキスト、画像、書式付きテキストを双方向に同期します。iPhone でペーストを求められたら許可してください。",
+ "clipboardSyncConflict": "両方のクリップボードが変更されました。それぞれの内容は保持されています。どちらかのデバイスでもう一度コピーすると同期されます。",
+ "clipboardSyncFailed": "同期に一時的に失敗しました。自動で再試行します。設定はオンのままです。",
+ "clipboardSyncSkipped": "この内容は同期できないためスキップしました。ペーストの許可を確認するか、テキストや画像をコピーして再試行してください。",
+ "clipboardSyncPermission": "同期を一時停止しました。ペーストを許可してから再試行してください。設定はオンのままです。",
+ "clipboardSyncRetry": "同期を再試行",
"credentialSigningRequired": "Mac の開発版に署名設定がありません。Mac で署名を設定して Cindy を再起動し、もう一度お試しください。",
"credentialPasswordRejected": "Mac のログインパスワードを確認できませんでした。もう一度入力してください。",
"credentialCancelled": "パスワードの確認をキャンセルしました。準備ができたら再接続してください。",
@@ -137,5 +149,8 @@
"autoUnlockUnavailable": "自動ロック解除を利用できません。接続して Mac を手動でロック解除できます。",
"lockOnExit": "終了時にパソコンをロック",
"lockOnExitHint": "リモートデスクトップを終了するとロックします。再接続やアプリ切り替えではロックしません。",
- "lockOnExitFailed": "パソコンのロックを確認できませんでした。パソコンの状態を確認してください。"
+ "lockOnExitFailed": "パソコンのロックを確認できませんでした。パソコンの状態を確認してください。",
+ "hostMute": "操作先をミュート",
+ "hostMuteHint": "接続中は操作先の音声をミュートします",
+ "hostMuteFailed": "操作先のミュート状態を変更できません"
}
diff --git a/apps/mobile/src/i18n/locales/ko/remoteDesktop.json b/apps/mobile/src/i18n/locales/ko/remoteDesktop.json
index 80a7ee7767..0f10e15b69 100644
--- a/apps/mobile/src/i18n/locales/ko/remoteDesktop.json
+++ b/apps/mobile/src/i18n/locales/ko/remoteDesktop.json
@@ -1,4 +1,16 @@
{
+ "hostDisconnected": "컴퓨터에서 원격 데스크톱 연결을 종료했습니다.",
+ "privacyScreen": "컴퓨터 프라이버시 화면",
+ "privacyScreenHint": "컴퓨터 화면을 가립니다. 컴퓨터에서 클릭하거나 키를 누른 뒤 확인하면 연결이 끊어집니다. 종료 시 잠금 설정도 적용됩니다.",
+ "privacyActive": "프라이버시 화면이 켜져 있습니다. 컴퓨터에서 클릭하거나 키를 누른 뒤 확인하면 연결이 끊어집니다. 종료 시 잠금 설정도 적용됩니다.",
+ "privacyFailed": "프라이버시 화면을 켜지 못했습니다. 컴퓨터 화면이 여전히 보일 수 있습니다.",
+ "clipboardSync": "클립보드 동기화",
+ "clipboardSyncHint": "앱이 전면에 있을 때 텍스트, 이미지, 서식 있는 텍스트를 양방향으로 동기화합니다. iPhone에서 붙여넣기 권한을 요청하면 허용해 주세요.",
+ "clipboardSyncConflict": "양쪽 클립보드가 변경되어 각각의 내용을 유지했습니다. 어느 기기에서든 다시 복사하면 동기화됩니다.",
+ "clipboardSyncFailed": "동기화가 일시적으로 실패했습니다. 자동으로 재시도하며 설정은 켜진 상태로 유지됩니다.",
+ "clipboardSyncSkipped": "현재 콘텐츠를 동기화할 수 없어 건너뛰었습니다. 붙여넣기 권한을 확인하거나 텍스트 또는 이미지를 복사한 후 다시 시도해 주세요.",
+ "clipboardSyncPermission": "동기화가 일시 중지되었습니다. 붙여넣기를 허용한 후 다시 시도해 주세요. 설정은 켜진 상태로 유지됩니다.",
+ "clipboardSyncRetry": "동기화 재시도",
"credentialSigningRequired": "Mac 개발 버전에 서명 설정이 없습니다. Mac에서 서명을 설정하고 Cindy를 다시 시작한 후 다시 시도해 주세요.",
"credentialPasswordRejected": "Mac 로그인 암호를 확인하지 못했습니다. 다시 입력해 주세요.",
"credentialCancelled": "암호 확인을 취소했습니다. 준비되면 다시 연결해 주세요.",
@@ -137,5 +149,8 @@
"autoUnlockUnavailable": "자동 잠금 해제를 사용할 수 없습니다. 연결한 뒤 Mac을 수동으로 잠금 해제할 수 있습니다.",
"lockOnExit": "종료 시 컴퓨터 잠금",
"lockOnExitHint": "원격 데스크톱을 종료하면 잠급니다. 재연결이나 앱 전환 시에는 잠그지 않습니다.",
- "lockOnExitFailed": "컴퓨터가 잠겼는지 확인하지 못했습니다. 컴퓨터 상태를 확인해 주세요."
+ "lockOnExitFailed": "컴퓨터가 잠겼는지 확인하지 못했습니다. 컴퓨터 상태를 확인해 주세요.",
+ "hostMute": "원격 컴퓨터 음소거",
+ "hostMuteHint": "연결 중 원격 컴퓨터의 소리를 음소거합니다",
+ "hostMuteFailed": "원격 컴퓨터의 음소거 상태를 변경할 수 없습니다"
}
diff --git a/apps/mobile/src/i18n/locales/zh-CN/remoteDesktop.json b/apps/mobile/src/i18n/locales/zh-CN/remoteDesktop.json
index c67515fc25..88e14f4970 100644
--- a/apps/mobile/src/i18n/locales/zh-CN/remoteDesktop.json
+++ b/apps/mobile/src/i18n/locales/zh-CN/remoteDesktop.json
@@ -1,4 +1,16 @@
{
+ "hostDisconnected": "电脑已主动断开远程桌面连接。",
+ "privacyScreen": "电脑防窥",
+ "privacyScreenHint": "遮住电脑屏幕。在电脑上点击鼠标或按键,确认后断开连接,并按设置锁屏。",
+ "privacyActive": "防窥已开启。在电脑上点击鼠标或按键,确认后断开连接,并按设置锁屏。",
+ "privacyFailed": "未能开启防窥,电脑屏幕可能仍可见。",
+ "clipboardSync": "剪贴板同步",
+ "clipboardSyncHint": "前台双向同步文字、图片和富文本。iPhone 如请求粘贴权限,请允许。",
+ "clipboardSyncConflict": "两端剪贴板都发生了变化,已分别保留。请在任一设备上重新复制以同步。",
+ "clipboardSyncFailed": "同步暂时失败,正在自动重试。开关保持开启。",
+ "clipboardSyncSkipped": "当前内容无法同步,已跳过。请检查粘贴权限,或复制文字、图片后重试。",
+ "clipboardSyncPermission": "同步已暂停,请允许粘贴权限后重试。开关保持开启。",
+ "clipboardSyncRetry": "重试同步",
"credentialSigningRequired": "Mac 上的开发版缺少签名配置。请在 Mac 上配置签名并重新启动 Cindy,然后重试。",
"credentialPasswordRejected": "Mac 登录密码验证失败,请重新输入。",
"credentialCancelled": "已取消密码验证,准备好后可重新连接。",
@@ -137,5 +149,8 @@
"autoUnlockUnavailable": "暂时无法自动解锁。你仍可连接,并手动解锁 Mac。",
"lockOnExit": "退出后锁定电脑",
"lockOnExitHint": "退出远程桌面时锁屏。重连或切换 App 不会触发。",
- "lockOnExitFailed": "未能确认电脑已锁屏,请检查电脑的锁屏状态。"
+ "lockOnExitFailed": "未能确认电脑已锁屏,请检查电脑的锁屏状态。",
+ "hostMute": "被控端静音",
+ "hostMuteHint": "连接期间将被控电脑静音",
+ "hostMuteFailed": "无法修改被控端静音状态"
}
diff --git a/apps/mobile/src/i18n/locales/zh-TW/remoteDesktop.json b/apps/mobile/src/i18n/locales/zh-TW/remoteDesktop.json
index be304e8698..4c621e63fe 100644
--- a/apps/mobile/src/i18n/locales/zh-TW/remoteDesktop.json
+++ b/apps/mobile/src/i18n/locales/zh-TW/remoteDesktop.json
@@ -1,4 +1,16 @@
{
+ "hostDisconnected": "電腦已主動中斷遠端桌面連線。",
+ "privacyScreen": "電腦防窺",
+ "privacyScreenHint": "遮住電腦螢幕。在電腦上點擊滑鼠或按鍵,確認後中斷連線,並依設定鎖定螢幕。",
+ "privacyActive": "防窺已開啟。在電腦上點擊滑鼠或按鍵,確認後中斷連線,並依設定鎖定螢幕。",
+ "privacyFailed": "無法開啟防窺,電腦螢幕可能仍可見。",
+ "clipboardSync": "剪貼簿同步",
+ "clipboardSyncHint": "在前景雙向同步文字、圖片和格式文字。iPhone 若請求貼上權限,請允許。",
+ "clipboardSyncConflict": "兩端剪貼簿都發生了變更,已分別保留。請在任一裝置上重新複製以同步。",
+ "clipboardSyncFailed": "同步暫時失敗,正在自動重試。開關保持開啟。",
+ "clipboardSyncSkipped": "目前內容無法同步,已略過。請檢查貼上權限,或複製文字、圖片後重試。",
+ "clipboardSyncPermission": "同步已暫停,請允許貼上權限後重試。開關保持開啟。",
+ "clipboardSyncRetry": "重試同步",
"credentialSigningRequired": "Mac 上的開發版缺少簽名設定。請在 Mac 上設定簽名並重新啟動 Cindy,然後重試。",
"credentialPasswordRejected": "Mac 登入密碼驗證失敗,請重新輸入。",
"credentialCancelled": "已取消密碼驗證,準備好後可重新連線。",
@@ -137,5 +149,8 @@
"autoUnlockUnavailable": "暫時無法自動解鎖。你仍可連線,並手動解鎖 Mac。",
"lockOnExit": "退出後鎖定電腦",
"lockOnExitHint": "離開遠端桌面時鎖定螢幕。重新連線或切換 App 不會觸發。",
- "lockOnExitFailed": "無法確認電腦已鎖定螢幕,請檢查電腦狀態。"
+ "lockOnExitFailed": "無法確認電腦已鎖定螢幕,請檢查電腦狀態。",
+ "hostMute": "被控端靜音",
+ "hostMuteHint": "連線期間將被控電腦靜音",
+ "hostMuteFailed": "無法修改被控端靜音狀態"
}
diff --git a/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx b/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx
index be04cf9504..c40bacb56f 100644
--- a/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx
+++ b/apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx
@@ -87,6 +87,7 @@ import { useInputModePreference } from "./useInputModePreference";
import { useAutoUnlockSettings } from "./useAutoUnlockSettings";
import { supportsAutoUnlock } from "./autoUnlockSupport";
import { useLockOnExitPreference } from "./useLockOnExitPreference";
+import { useRemoteDesktopSafety } from "./useRemoteDesktopSafety";
import { useVideoSettingsPreference } from "./useVideoSettingsPreference";
import { PermissionGuide } from "./PermissionGuide";
import { RemoteDesktopBackButton } from "./RemoteDesktopBackButton";
@@ -268,6 +269,13 @@ export default function RemoteDesktopScreen() {
const [error, setError] = useState<string | null>(null);
const takeoverPromptOpen = useRef(false);
const takeoverAction = useRef<() => void>(() => {});
+ useEffect(() => {
+ if (error === "hostDisconnected")
+ Alert.alert(
+ t("remoteDesktop.disconnected"),
+ t("remoteDesktop.hostDisconnected"),
+ );
+ }, [error, t]);
const [frameReady, setFrameReady] = useState(false);
const [controlReady, setControlReady] = useState(false);
const connectionPending = !error && (!lease || !frameReady || !controlReady);
@@ -412,6 +420,15 @@ export default function RemoteDesktopScreen() {
}),
[request, send, t],
);
+ const safety = useRemoteDesktopSafety(
+ deviceId,
+ lease,
+ !connectionPending && !error,
+ focused,
+ caps,
+ request,
+ exitLock.current,
+ );
const transferClipboard = async (action: "copy" | "paste") => {
const current = active.current;
const epoch = generation.current;
@@ -562,7 +579,9 @@ export default function RemoteDesktopScreen() {
console.debug("[remote-desktop] connection failed", {
code: code ?? "UNKNOWN",
});
- const blocked = remoteDesktopFailureKey(code ?? message);
+ const blocked = /DESKTOP_STOPPED/.test(code ?? message)
+ ? "hostDisconnected"
+ : remoteDesktopFailureKey(code ?? message);
stop(!blocked);
setError(blocked);
if (
@@ -594,6 +613,7 @@ export default function RemoteDesktopScreen() {
],
);
}
+ if (blocked === "hostDisconnected") setOperations(false);
if (blocked) recovery.current.enabled = false;
else {
recovery.current.at = Date.now() + recovery.current.delay;
@@ -1977,6 +1997,7 @@ export default function RemoteDesktopScreen() {
onPage={setControlPage}
security={{
...security,
+ ...safety,
hostPlatform: caps?.platform,
lockOnExit,
lockOnExitAvailable:
@@ -1989,14 +2010,14 @@ export default function RemoteDesktopScreen() {
!lease || busy || connecting.current || !caps?.canControl
}
presentation={{
- canRotate: Boolean(remotePresentation),
+ canRotate: typeof remotePresentation?.rotate === "function",
canPip: Boolean(
remotePresentation && caps?.backgroundViewing && canPip,
),
canAudio: Boolean(caps?.systemAudio),
onRotate: () => {
void remotePresentation
- ?.rotate(!landscape)
+ ?.rotate?.(!landscape)
.then(() => setOperations(false))
.catch(() =>
setSettingNotice(t("remoteDesktop.settingFailed")),
diff --git a/apps/mobile/src/remote-desktop/RemoteDesktopSecuritySettings.tsx b/apps/mobile/src/remote-desktop/RemoteDesktopSecuritySettings.tsx
index af71932a26..1641eb7108 100644
--- a/apps/mobile/src/remote-desktop/RemoteDesktopSecuritySettings.tsx
+++ b/apps/mobile/src/remote-desktop/RemoteDesktopSecuritySettings.tsx
@@ -1,4 +1,10 @@
-import { ActivityIndicator, Platform, StyleSheet, View } from "react-native";
+import {
+ ActivityIndicator,
+ Platform,
+ Pressable,
+ StyleSheet,
+ View,
+} from "react-native";
import { LockKeyhole, ScanFace } from "lucide-react-native";
import { useTranslation } from "react-i18next";
import { Text } from "@/components/AppText";
@@ -26,6 +32,18 @@ export interface RemoteDesktopSecuritySettingsProps {
lockOnExit?: boolean;
lockOnExitAvailable?: boolean;
onLockOnExit?(value: boolean): void;
+ privacy?: boolean;
+ privacyActive?: boolean;
+ privacyAvailable?: boolean;
+ onPrivacy?(value: boolean): void;
+ clipboardSync?: boolean;
+ clipboardSyncAvailable?: boolean;
+ onClipboardSync?(value: boolean): void;
+ onClipboardSyncRetry?(): void;
+ hostMute?: boolean;
+ hostMuteAvailable?: boolean;
+ onHostMute?(value: boolean): void;
+ safetyNotice?: string | null;
}
/** Values are confirmed native vault state, never optimistic password-save state. */
@@ -206,6 +224,87 @@ export function RemoteDesktopSecuritySettings(
{props.notice || t("remoteDesktop.autoUnlockUnavailable")}
</Text>
)}
+ {(
+ [
+ {
+ key: "privacyScreen",
+ value: props.privacy,
+ available: props.privacyAvailable,
+ change: props.onPrivacy,
+ hint: props.privacyActive ? "privacyActive" : "privacyScreenHint",
+ },
+ {
+ key: "clipboardSync",
+ value: props.clipboardSync,
+ available: props.clipboardSyncAvailable,
+ change: props.onClipboardSync,
+ hint: "clipboardSyncHint",
+ },
+ {
+ key: "hostMute",
+ value: props.hostMute,
+ available: props.hostMuteAvailable,
+ change: props.onHostMute,
+ hint: "hostMuteHint",
+ },
+ ] as const
+ ).map((item) => (
+ <View
+ key={item.key}
+ style={{
+ ...row,
+ backgroundColor: colors.sheetActionSurface,
+ borderColor: colors.sheetActionBorder,
+ borderWidth: StyleSheet.hairlineWidth,
+ borderRadius: radius.container,
+ }}
+ >
+ <View style={{ flex: 1, gap: spacing.xs }}>
+ <Text
+ style={{ color: colors.textPrimary, fontSize: typeScale.body }}
+ >
+ {t(`remoteDesktop.${item.key}`)}
+ </Text>
+ <Text style={hint}>
+ {t(
+ `remoteDesktop.${item.available ? item.hint : "settingUnsupported"}`,
+ )}
+ </Text>
+ </View>
+ <View style={switchSlot}>
+ <NativeSwitch
+ accessibilityLabel={t(`remoteDesktop.${item.key}`)}
+ testID={`remoteDesktop.${item.key}`}
+ value={item.value === true}
+ disabled={!item.available && !item.value}
+ onValueChange={(value) => item.change?.(value)}
+ />
+ </View>
+ </View>
+ ))}
+ {props.safetyNotice && (
+ <Text style={hint} accessibilityRole="alert">
+ {t(`remoteDesktop.${props.safetyNotice}`)}
+ </Text>
+ )}
+ {props.clipboardSync &&
+ props.safetyNotice?.startsWith("clipboardSync") && (
+ <Pressable
+ accessibilityRole="button"
+ onPress={props.onClipboardSyncRetry}
+ style={{
+ minHeight: 44,
+ justifyContent: "center",
+ alignItems: "center",
+ borderRadius: radius.pill,
+ backgroundColor: colors.sheetActionSurface,
+ }}
+ >
+ <Text style={{ color: colors.textPrimary }}>
+ {t("remoteDesktop.clipboardSyncRetry")}
+ </Text>
+ </Pressable>
+ )}
</View>
);
}
diff --git a/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx b/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx
index 6e518c691c..c6fafbd210 100644
--- a/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx
+++ b/apps/mobile/src/remote-desktop/__tests__/screen.test.tsx
@@ -28,6 +28,7 @@ vi.mock("../useAutoUnlockSettings", () => ({
},
}));
vi.mock("../useLockOnExitPreference", () => ({
+ useRemoteDesktopPreference: () => [false, vi.fn(), true],
useLockOnExitPreference: () => [
fixture.lockOnExit,
(value: boolean) => {
@@ -2857,5 +2858,11 @@ describe("remote desktop controls", () => {
await act(async () => vi.advanceTimersByTimeAsync(30_000));
expect(requests().filter((r) => r.op === "start")).toHaveLength(1);
expect(button("back").disabled).toBe(false);
+ if (error === "DESKTOP_STOPPED") {
+ expect(fixture.alert).toHaveBeenCalledWith(
+ "remoteDesktop.disconnected",
+ "remoteDesktop.hostDisconnected",
+ );
+ }
});
});
diff --git a/apps/mobile/src/remote-desktop/clipboardSync.test.ts b/apps/mobile/src/remote-desktop/clipboardSync.test.ts
new file mode 100644
index 0000000000..6a4078d6e4
--- /dev/null
+++ b/apps/mobile/src/remote-desktop/clipboardSync.test.ts
@@ -0,0 +1,479 @@
+import { describe, expect, it, vi } from "vitest";
+import {
+ ClipboardSync,
+ clipboardDigest,
+ type ClipboardSyncDeps,
+ type ClipboardSyncBaseline,
+ type LocalClipboardReadCache,
+} from "./clipboardSync";
+import type { RemoteDesktopRequest } from "@cindy/device-link";
+
+function harness() {
+ let local = "1",
+ remote = "1",
+ current = true;
+ let localJson = JSON.stringify({ text: "local-1" });
+ let remoteJson = JSON.stringify({ text: "remote-1" });
+ let buffer = "";
+ const request = vi.fn(
+ async (message: RemoteDesktopRequest): Promise<unknown> => {
+ if (message.op === "clipboardVersion") return { version: remote };
+ if (message.op !== "clipboardContent") return {};
+ if (message.action === "copy")
+ return message.inline
+ ? { data: remoteJson, length: remoteJson.length }
+ : { id: "transfer", length: remoteJson.length };
+ if (message.action === "begin") {
+ buffer = "";
+ return { id: "transfer" };
+ }
+ if (message.action === "read")
+ return {
+ data: remoteJson.slice(message.offset, message.offset + 65536),
+ };
+ if (message.action === "write") buffer += message.data;
+ if (message.action === "commit" || message.action === "paste") {
+ remoteJson = message.action === "paste" ? message.data : buffer;
+ remote = "3";
+ return { version: remote };
+ }
+ return { ok: true };
+ },
+ );
+ const write = vi.fn(async (json: string, version: string) => {
+ if (local !== version) throw new Error("CLIPBOARD_CHANGED");
+ local = "3";
+ localJson = json;
+ return local;
+ });
+ const deps: ClipboardSyncDeps = {
+ lease: "lease",
+ request: request as ClipboardSyncDeps["request"],
+ current: () => current,
+ localVersion: async () => local,
+ readLocal: vi.fn(async () => localJson),
+ writeLocal: write,
+ };
+ return {
+ engine: new ClipboardSync(deps),
+ deps,
+ request,
+ write,
+ local: (
+ value: string,
+ json = JSON.stringify({ text: `local-${value}` }),
+ ) => {
+ local = value;
+ localJson = json;
+ },
+ remote: (
+ value: string,
+ json = JSON.stringify({ text: `remote-${value}` }),
+ ) => {
+ remote = value;
+ remoteJson = json;
+ },
+ stop: () => {
+ current = false;
+ },
+ };
+}
+describe("automatic clipboard synchronization", () => {
+ it("skips an unsupported computer snapshot until its counter changes", async () => {
+ const h = harness();
+ await h.engine.tick();
+ h.request.mockClear();
+ h.remote("2");
+ const original = h.request.getMockImplementation()!;
+ h.request.mockImplementation(async (message) => {
+ if (message.op === "clipboardContent" && message.action === "copy")
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ return original(message);
+ });
+ await expect(h.engine.tick()).resolves.toBeUndefined();
+ await expect(h.engine.tick()).resolves.toBeUndefined();
+ expect(
+ h.request.mock.calls.filter(
+ ([r]) => r.op === "clipboardContent" && r.action === "copy",
+ ),
+ ).toHaveLength(1);
+ expect(h.write).not.toHaveBeenCalled();
+ h.request.mockImplementation(original);
+ h.remote("4");
+ await h.engine.tick();
+ expect(h.write).toHaveBeenCalledTimes(1);
+ });
+ it.each(["CLIPBOARD_UNSUPPORTED", "CLIPBOARD_TOO_LONG"])(
+ "skips %s once, then transfers the next copy",
+ async (code) => {
+ const h = harness();
+ const trace = vi.fn();
+ const engine = new ClipboardSync({ ...h.deps, trace });
+ vi.mocked(h.deps.readLocal).mockRejectedValueOnce(new Error(code));
+ await engine.tick();
+ await engine.tick();
+ expect(h.deps.readLocal).toHaveBeenCalledTimes(1);
+ expect(trace).toHaveBeenCalledWith("content-skipped");
+ h.local("2");
+ await engine.tick();
+ expect(
+ h.request.mock.calls.some(
+ ([r]) => r.op === "clipboardContent" && r.action === "commit",
+ ),
+ ).toBe(true);
+ },
+ );
+ it("reuses only a successful read of the same local version on a new connection", async () => {
+ const h = harness();
+ const localReadCache: LocalClipboardReadCache = {};
+ const deps = { ...h.deps, localReadCache };
+ await new ClipboardSync(deps).tick();
+ await new ClipboardSync(deps).tick();
+ expect(h.deps.readLocal).toHaveBeenCalledTimes(1);
+ h.local("2");
+ await new ClipboardSync(deps).tick();
+ expect(h.deps.readLocal).toHaveBeenCalledTimes(2);
+ });
+ it("does not remember denied permission as a successful read", async () => {
+ const h = harness();
+ const localReadCache: LocalClipboardReadCache = {};
+ vi.mocked(h.deps.readLocal).mockRejectedValueOnce(
+ new Error("PASTE_DENIED"),
+ );
+ await expect(
+ new ClipboardSync({ ...h.deps, localReadCache }).tick(),
+ ).rejects.toThrow("PASTE_DENIED");
+ expect(localReadCache.version).toBeUndefined();
+ await new ClipboardSync({ ...h.deps, localReadCache }).tick();
+ expect(h.deps.readLocal).toHaveBeenCalledTimes(2);
+ });
+ it.each(["local", "remote"] as const)(
+ "transfers small %s content inline and verifies destination normalization",
+ async (direction) => {
+ const h = harness();
+ const engine = new ClipboardSync({ ...h.deps, inline: true });
+ await engine.tick();
+ h.request.mockClear();
+ h[direction]("2");
+ await engine.tick();
+ const payloads = h.request.mock.calls.filter(
+ ([r]) => r.op === "clipboardContent",
+ );
+ expect(payloads).toHaveLength(direction === "local" ? 2 : 1);
+ if (direction === "local")
+ expect(payloads[1][0]).toMatchObject({ action: "copy", inline: true });
+ expect(payloads[0][0]).toMatchObject({
+ action: direction === "local" ? "paste" : "copy",
+ });
+ },
+ );
+ it("keeps large payloads chunked even when inline is supported", async () => {
+ const h = harness();
+ const engine = new ClipboardSync({ ...h.deps, inline: true });
+ await engine.tick();
+ h.local("2", JSON.stringify({ text: "x".repeat(130000) }));
+ await engine.tick();
+ expect(
+ h.request.mock.calls.filter(
+ ([r]) => r.op === "clipboardContent" && r.action === "write",
+ ),
+ ).toHaveLength(2);
+ expect(
+ h.request.mock.calls.some(
+ ([r]) => r.op === "clipboardContent" && r.action === "paste",
+ ),
+ ).toBe(false);
+ });
+ it("ignores key order but preserves different rich representations in its digest", () => {
+ expect(clipboardDigest('{"text":"a","html":"<b>a</b>"}')).toBe(
+ clipboardDigest('{"html":"<b>a</b>","text":"a"}'),
+ );
+ expect(clipboardDigest('{"text":"a"}')).not.toBe(
+ clipboardDigest('{"text":"a","html":"<b>a</b>"}'),
+ );
+ });
+ it("does not resend identical copies or report identical concurrent copies as a conflict", async () => {
+ const h = harness();
+ await h.engine.tick();
+ h.request.mockClear();
+ h.local("2", '{"text":"local-1"}');
+ await h.engine.tick();
+ expect(
+ h.request.mock.calls.some(([r]) => r.op === "clipboardContent"),
+ ).toBe(false);
+ h.local("3", '{"text":"same"}');
+ h.remote("2", '{"text":"same"}');
+ await h.engine.tick();
+ expect(h.write).not.toHaveBeenCalled();
+ expect(
+ h.request.mock.calls.some(
+ ([r]) => r.op === "clipboardContent" && r.action === "commit",
+ ),
+ ).toBe(false);
+ });
+ it.each(["local", "remote"] as const)(
+ "syncs the next %s copy after preserving a conflict",
+ async (direction) => {
+ const h = harness();
+ await h.engine.tick();
+ h.local("2");
+ h.remote("2");
+ await expect(h.engine.tick()).rejects.toThrow("CLIPBOARD_CONFLICT");
+ expect(h.write).not.toHaveBeenCalled();
+ await h.engine.tick();
+ h[direction]("4");
+ await h.engine.tick();
+ await h.engine.tick();
+ expect(h.write).toHaveBeenCalledTimes(direction === "remote" ? 1 : 0);
+ expect(
+ h.request.mock.calls.filter(
+ ([r]) => r.op === "clipboardContent" && r.action === "commit",
+ ),
+ ).toHaveLength(direction === "local" ? 1 : 0);
+ },
+ );
+ it.each(["local", "remote"] as const)(
+ "retains a %s copy made while the app is paused across lease replacement",
+ async (direction) => {
+ const h = harness();
+ const baseline: ClipboardSyncBaseline = { local: "", remote: "" };
+ await new ClipboardSync(h.deps, baseline).tick();
+ h[direction]("2");
+ const resumed = new ClipboardSync(
+ { ...h.deps, lease: "resumed" },
+ baseline,
+ );
+ await resumed.tick();
+ await resumed.tick();
+ expect(h.write).toHaveBeenCalledTimes(direction === "remote" ? 1 : 0);
+ expect(
+ h.request.mock.calls.filter(
+ ([r]) => r.op === "clipboardContent" && r.action === "commit",
+ ),
+ ).toHaveLength(direction === "local" ? 1 : 0);
+ },
+ );
+ it("reads both initial digests without writing existing contents on opt-in", async () => {
+ const h = harness();
+ await h.engine.tick();
+ await h.engine.tick();
+ expect(h.write).not.toHaveBeenCalled();
+ expect(
+ h.request.mock.calls.some(
+ ([request]) =>
+ request.op === "clipboardContent" &&
+ ["begin", "write", "commit", "paste"].includes(request.action),
+ ),
+ ).toBe(false);
+ });
+ it("ignores a computer counter-only rewrite immediately after opt-in", async () => {
+ const h = harness();
+ await h.engine.tick();
+ h.remote("2", JSON.stringify({ text: "remote-1" }));
+ await h.engine.tick();
+ expect(h.write).not.toHaveBeenCalled();
+ h.remote("3");
+ await h.engine.tick();
+ expect(h.write).toHaveBeenCalledTimes(1);
+ });
+ it("does not commit a baseline if local content changes during the remote read", async () => {
+ const h = harness();
+ const baseline: ClipboardSyncBaseline = { local: "", remote: "" };
+ const original = h.request.getMockImplementation()!;
+ h.request.mockImplementation(async (message) => {
+ if (message.op === "clipboardContent" && message.action === "copy")
+ h.local("2");
+ return original(message);
+ });
+ await expect(new ClipboardSync(h.deps, baseline).tick()).rejects.toThrow(
+ "CLIPBOARD_CHANGED",
+ );
+ expect(baseline.local).toBe("");
+ expect(h.write).not.toHaveBeenCalled();
+ });
+ it.each(["local", "remote"] as const)(
+ "syncs a %s change once without echoing its own write",
+ async (direction) => {
+ const h = harness();
+ await h.engine.tick();
+ h[direction]("2");
+ await h.engine.tick();
+ await h.engine.tick();
+ expect(
+ h.request.mock.calls.filter(
+ ([r]) => r.op === "clipboardContent" && r.action === "commit",
+ ),
+ ).toHaveLength(direction === "local" ? 1 : 0);
+ expect(h.write).toHaveBeenCalledTimes(direction === "remote" ? 1 : 0);
+ },
+ );
+ it("does not echo a delayed Android notification for its own write", async () => {
+ const h = harness();
+ await h.engine.tick();
+ const copied = JSON.stringify({ text: "remote-copy" });
+ h.remote("2", copied);
+ await h.engine.tick();
+ expect(h.write).toHaveBeenCalledTimes(1);
+ h.request.mockClear();
+ // setPrimaryClip returned its new token before the platform callback ran.
+ h.local("4", copied);
+ await h.engine.tick();
+ await h.engine.tick();
+ expect(h.write).toHaveBeenCalledTimes(1);
+ expect(
+ h.request.mock.calls.every(([r]) => r.op === "clipboardVersion"),
+ ).toBe(true);
+ // A later real copy still propagates after that counter-only notification.
+ h.local("5");
+ await h.engine.tick();
+ expect(
+ h.request.mock.calls.filter(
+ ([r]) => r.op === "clipboardContent" && r.action === "commit",
+ ),
+ ).toHaveLength(1);
+ });
+ it("preserves concurrent local and remote copies", async () => {
+ const h = harness();
+ await h.engine.tick();
+ h.local("2");
+ h.remote("2");
+ await expect(h.engine.tick()).rejects.toThrow("CLIPBOARD_CONFLICT");
+ expect(h.write).not.toHaveBeenCalled();
+ });
+ it("does not write a remote result after leaving the foreground", async () => {
+ const h = harness();
+ await h.engine.tick();
+ h.remote("2");
+ h.request.mockImplementation(async (message: RemoteDesktopRequest) => {
+ if (message.op === "clipboardVersion") return { version: "2" };
+ if (message.op === "clipboardContent" && message.action === "copy") {
+ h.stop();
+ return { id: "transfer", length: 12 };
+ }
+ return {};
+ });
+ await expect(h.engine.tick()).rejects.toThrow("DESKTOP_LEASE_EXPIRED");
+ expect(h.write).not.toHaveBeenCalled();
+ });
+ it("distinguishes a real new copy from a counter-only change on the other device", async () => {
+ const h = harness();
+ await h.engine.tick();
+ h.local("2");
+ await h.engine.tick();
+ h.request.mockClear();
+ h.local("4");
+ h.remote("5", JSON.stringify({ text: "local-2" }));
+ await h.engine.tick();
+ expect(
+ h.request.mock.calls.filter(
+ ([r]) => r.op === "clipboardContent" && r.action === "commit",
+ ),
+ ).toHaveLength(1);
+ expect(h.write).not.toHaveBeenCalled();
+ });
+});
+
+it.each(["local", "remote"] as const)(
+ "keeps the other digest after skipping an unsupported %s version",
+ async (failed) => {
+ const h = harness();
+ await h.engine.tick();
+ h[failed]("2");
+ if (failed === "local") {
+ vi.mocked(h.deps.readLocal).mockRejectedValueOnce(
+ new Error("CLIPBOARD_UNSUPPORTED"),
+ );
+ } else {
+ const original = h.request.getMockImplementation()!;
+ h.request.mockImplementation(async (message) => {
+ if (message.op === "clipboardContent" && message.action === "copy")
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ return original(message);
+ });
+ }
+ await h.engine.tick();
+ const other = failed === "local" ? "remote" : "local";
+ h[other]("4", JSON.stringify({ text: `${other}-1` }));
+ await h.engine.tick();
+ expect(h.write).not.toHaveBeenCalled();
+ expect(
+ h.request.mock.calls.some(
+ ([r]) => r.op === "clipboardContent" && r.action === "commit",
+ ),
+ ).toBe(false);
+ h[other]("5");
+ await h.engine.tick();
+ expect(
+ h.write.mock.calls.length +
+ h.request.mock.calls.filter(
+ ([r]) => r.op === "clipboardContent" && r.action === "commit",
+ ).length,
+ ).toBe(1);
+ },
+);
+it("retains a successfully read new local digest when the remote read is unsupported", async () => {
+ const h = harness();
+ const baseline: ClipboardSyncBaseline = { local: "", remote: "" };
+ const engine = new ClipboardSync(h.deps, baseline);
+ await engine.tick();
+ h.local("2");
+ h.remote("2");
+ const original = h.request.getMockImplementation()!;
+ h.request.mockImplementation(async (message) => {
+ if (message.op === "clipboardContent" && message.action === "copy")
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ return original(message);
+ });
+ await engine.tick();
+ expect(baseline.localDigest).toBe(
+ clipboardDigest(JSON.stringify({ text: "local-2" })),
+ );
+ expect(baseline.remoteDigest).toBeUndefined();
+});
+
+it.each(["local", "remote"] as const)(
+ "does not echo normalized content after writing to %s",
+ async (destination) => {
+ const h = harness();
+ await h.engine.tick();
+ const source = JSON.stringify({
+ text: "rich",
+ html: "<b>rich</b>",
+ rtf: "rtf rich",
+ });
+ const normalized = JSON.stringify({ text: "rich", html: "<b>rich</b>" });
+ if (destination === "local") {
+ const write = h.write.getMockImplementation()!;
+ h.write.mockImplementation((_, version) => write(normalized, version));
+ h.remote("2", source);
+ } else {
+ const original = h.request.getMockImplementation()!;
+ h.request.mockImplementation(async (message) => {
+ const result = await original(message);
+ if (message.op === "clipboardContent" && message.action === "commit")
+ h.remote("3", normalized);
+ return result;
+ });
+ h.local("2", source);
+ }
+ await h.engine.tick();
+ h.request.mockClear();
+ h.write.mockClear();
+ h[destination]("4", normalized); // delayed native notification
+ await h.engine.tick();
+ expect(h.write).not.toHaveBeenCalled();
+ expect(
+ h.request.mock.calls.some(
+ ([r]) => r.op === "clipboardContent" && r.action === "commit",
+ ),
+ ).toBe(false);
+ h[destination]("5"); // a real subsequent copy still propagates
+ await h.engine.tick();
+ expect(
+ h.write.mock.calls.length +
+ h.request.mock.calls.filter(
+ ([r]) => r.op === "clipboardContent" && r.action === "commit",
+ ).length,
+ ).toBe(1);
+ },
+);
diff --git a/apps/mobile/src/remote-desktop/clipboardSync.ts b/apps/mobile/src/remote-desktop/clipboardSync.ts
new file mode 100644
index 0000000000..a2fe2b7b3f
--- /dev/null
+++ b/apps/mobile/src/remote-desktop/clipboardSync.ts
@@ -0,0 +1,395 @@
+import {
+ CLIPBOARD_CHUNK_CHARS,
+ CLIPBOARD_MAX_CHARS,
+ parseClipboardContent,
+ type RemoteDesktopRequest,
+} from "@cindy/device-link";
+import { sha256 } from "@noble/hashes/sha256";
+import { bytesToHex } from "@noble/hashes/utils";
+import { clipboardSyncErrorCode } from "./clipboardSyncFailure";
+
+function skippableContent(error: unknown): boolean {
+ return [
+ "CLIPBOARD_EMPTY",
+ "CLIPBOARD_UNSUPPORTED",
+ "CLIPBOARD_TOO_LONG",
+ ].includes(clipboardSyncErrorCode(error));
+}
+
+export interface LocalClipboardReadCache {
+ version?: string;
+ digest?: string;
+}
+export interface ClipboardSyncDeps {
+ lease: string;
+ request<T>(message: RemoteDesktopRequest): Promise<T>;
+ localVersion(): Promise<string>;
+ readLocal(): Promise<string>;
+ writeLocal(json: string, version: string): Promise<string>;
+ current(): boolean;
+ inline?: boolean;
+ localReadCache?: LocalClipboardReadCache;
+ trace?(stage: string): void;
+}
+export interface ClipboardSyncBaseline {
+ local: string;
+ remote: string;
+ localDigest?: string;
+ remoteDigest?: string;
+}
+
+/** Hash every portable representation, in stable key order. No content is retained. */
+export function clipboardDigest(json: string): string {
+ const content = parseClipboardContent(json);
+ return bytesToHex(
+ sha256(
+ JSON.stringify([
+ content.text,
+ content.html,
+ content.rtf,
+ content.url,
+ content.png,
+ ]),
+ ),
+ );
+}
+
+/** Serial sync: counter changes trigger reads; content changes trigger writes. */
+export class ClipboardSync {
+ private pending = false;
+ constructor(
+ private readonly deps: ClipboardSyncDeps,
+ private readonly baseline: ClipboardSyncBaseline = {
+ local: "",
+ remote: "",
+ },
+ ) {}
+ private check() {
+ if (!this.deps.current()) throw new Error("DESKTOP_LEASE_EXPIRED");
+ }
+ private async remoteVersion() {
+ const { version } = await this.deps.request<{ version: string }>({
+ op: "clipboardVersion",
+ lease: this.deps.lease,
+ });
+ this.check();
+ if (typeof version !== "string" || !/^\d{1,128}$/.test(version))
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ return version;
+ }
+ private async readLocal(version: string) {
+ this.deps.trace?.("phone-read-start");
+ const json = await this.deps.readLocal();
+ this.check();
+ const digest = clipboardDigest(json);
+ if ((await this.deps.localVersion()) !== version)
+ throw new Error("CLIPBOARD_CHANGED");
+ this.check();
+ this.rememberLocal(version, digest);
+ this.deps.trace?.("phone-read-complete");
+ return { json, digest };
+ }
+ private rememberLocal(version: string, digest: string | undefined) {
+ if (this.deps.localReadCache)
+ Object.assign(this.deps.localReadCache, { version, digest });
+ }
+ private async cancel(id: string) {
+ await this.deps
+ .request({
+ op: "clipboardContent",
+ lease: this.deps.lease,
+ sync: true,
+ action: "cancel",
+ id,
+ })
+ .catch(() => {});
+ }
+ private async readRemote(version: string) {
+ const { lease, request } = this.deps;
+ let id: string | undefined;
+ try {
+ this.deps.trace?.("computer-read-start");
+ const result = await request<{
+ id?: string;
+ length: number;
+ data?: string;
+ }>({
+ op: "clipboardContent",
+ lease,
+ sync: true,
+ version,
+ action: "copy",
+ ...(this.deps.inline ? { inline: true } : {}),
+ });
+ this.check();
+ if (
+ !Number.isSafeInteger(result.length) ||
+ result.length <= 0 ||
+ result.length > CLIPBOARD_MAX_CHARS
+ )
+ throw new Error("CLIPBOARD_TOO_LONG");
+ let json = "";
+ if (this.deps.inline && typeof result.data === "string") {
+ if (
+ result.data.length > CLIPBOARD_CHUNK_CHARS ||
+ result.data.length !== result.length
+ )
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ json = result.data;
+ } else {
+ if (typeof result.id !== "string" || !result.id)
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ id = result.id;
+ while (json.length < result.length) {
+ this.check();
+ const part = await request<{ data: string }>({
+ op: "clipboardContent",
+ lease,
+ sync: true,
+ action: "read",
+ id,
+ offset: json.length,
+ });
+ this.check();
+ if (
+ typeof part.data !== "string" ||
+ !part.data.length ||
+ part.data.length > CLIPBOARD_CHUNK_CHARS ||
+ json.length + part.data.length > result.length
+ )
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ json += part.data;
+ }
+ }
+ const digest = clipboardDigest(json);
+ if ((await this.remoteVersion()) !== version)
+ throw new Error("CLIPBOARD_CHANGED");
+ return { json, digest };
+ } finally {
+ if (id) void this.cancel(id);
+ }
+ }
+ private async writeRemote(json: string, local: string, remote: string) {
+ const { lease, request } = this.deps;
+ let id: string | undefined;
+ try {
+ this.check();
+ let committed: { version: string };
+ if (this.deps.inline && json.length <= CLIPBOARD_CHUNK_CHARS) {
+ if ((await this.deps.localVersion()) !== local)
+ throw new Error("CLIPBOARD_CHANGED");
+ this.check();
+ committed = await request({
+ op: "clipboardContent",
+ lease,
+ sync: true,
+ version: remote,
+ action: "paste",
+ data: json,
+ });
+ } else {
+ const result = await request<{ id: string }>({
+ op: "clipboardContent",
+ lease,
+ sync: true,
+ version: remote,
+ action: "begin",
+ length: json.length,
+ });
+ id = result.id;
+ for (
+ let offset = 0;
+ offset < json.length;
+ offset += CLIPBOARD_CHUNK_CHARS
+ ) {
+ this.check();
+ await request({
+ op: "clipboardContent",
+ lease,
+ sync: true,
+ action: "write",
+ id,
+ offset,
+ data: json.slice(offset, offset + CLIPBOARD_CHUNK_CHARS),
+ });
+ }
+ if ((await this.deps.localVersion()) !== local)
+ throw new Error("CLIPBOARD_CHANGED");
+ this.check();
+ committed = await request({
+ op: "clipboardContent",
+ lease,
+ sync: true,
+ action: "commit",
+ id,
+ });
+ }
+ this.check();
+ if (
+ typeof committed.version !== "string" ||
+ !/^\d{1,128}$/.test(committed.version)
+ )
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ return committed.version;
+ } finally {
+ if (id) void this.cancel(id);
+ }
+ }
+ async tick(): Promise<void> {
+ if (this.pending) return;
+ this.pending = true;
+ let local: string | undefined;
+ let remote: string | undefined;
+ let readLocalDigest: string | undefined;
+ let readRemoteDigest: string | undefined;
+ try {
+ this.check();
+ local = await this.deps.localVersion();
+ this.check();
+ remote = await this.remoteVersion();
+ if (!this.baseline.local) {
+ let digest: string | undefined;
+ const cache = this.deps.localReadCache;
+ if (cache?.version === local) {
+ digest = cache.digest;
+ } else {
+ // Keep the proven initial native permission read. Reuse it only while
+ // the phone pasteboard version remains exactly the same.
+ try {
+ digest = (await this.readLocal(local)).digest;
+ } catch (error) {
+ if (!skippableContent(error)) throw error;
+ this.deps.trace?.("content-skipped");
+ if ((await this.deps.localVersion()) !== local)
+ throw new Error("CLIPBOARD_CHANGED");
+ }
+ }
+ // Establish both content baselines without writing either clipboard.
+ // Counter-only rewrites must not import contents predating opt-in.
+ let remoteDigest: string | undefined;
+ try {
+ remoteDigest = (await this.readRemote(remote)).digest;
+ } catch (error) {
+ if (!skippableContent(error)) throw error;
+ this.deps.trace?.("content-skipped");
+ if ((await this.remoteVersion()) !== remote)
+ throw new Error("CLIPBOARD_CHANGED");
+ }
+ if ((await this.deps.localVersion()) !== local)
+ throw new Error("CLIPBOARD_CHANGED");
+ this.check();
+ this.rememberLocal(local, digest);
+ Object.assign(this.baseline, {
+ local,
+ remote,
+ localDigest: digest,
+ remoteDigest,
+ });
+ return;
+ }
+ let localChanged = local !== this.baseline.local;
+ let remoteChanged = remote !== this.baseline.remote;
+ if (!localChanged && !remoteChanged) return;
+ const localContent = localChanged
+ ? await this.readLocal(local).then((content) => {
+ readLocalDigest = content.digest;
+ return content;
+ })
+ : undefined;
+ const remoteContent = remoteChanged
+ ? await this.readRemote(remote).then((content) => {
+ readRemoteDigest = content.digest;
+ return content;
+ })
+ : undefined;
+ this.check();
+ if ((await this.deps.localVersion()) !== local)
+ throw new Error("CLIPBOARD_CHANGED");
+ this.check();
+ if (localContent && localContent.digest === this.baseline.localDigest)
+ localChanged = false;
+ if (remoteContent && remoteContent.digest === this.baseline.remoteDigest)
+ remoteChanged = false;
+ const localDigest = localContent?.digest ?? this.baseline.localDigest;
+ const remoteDigest = remoteContent?.digest ?? this.baseline.remoteDigest;
+ if (localDigest && localDigest === remoteDigest) {
+ localChanged = false;
+ remoteChanged = false;
+ }
+ if (localChanged && remoteChanged) {
+ Object.assign(this.baseline, {
+ local,
+ remote,
+ localDigest,
+ remoteDigest,
+ });
+ throw new Error("CLIPBOARD_CONFLICT");
+ }
+ if (localChanged && localContent) {
+ const version = await this.writeRemote(
+ localContent.json,
+ local,
+ remote,
+ );
+ // Native clipboards may normalize or drop portable representations.
+ // Record the destination's actual, version-checked content, not its input.
+ const actual = await this.readRemote(version);
+ Object.assign(this.baseline, {
+ local,
+ remote: version,
+ localDigest: localContent.digest,
+ remoteDigest: actual.digest,
+ });
+ this.deps.trace?.("phone-to-computer-complete");
+ } else if (remoteChanged && remoteContent) {
+ const written = await this.deps.writeLocal(remoteContent.json, local);
+ this.check();
+ const actual = await this.readLocal(written);
+ Object.assign(this.baseline, {
+ local: written,
+ remote,
+ localDigest: actual.digest,
+ remoteDigest: remoteContent.digest,
+ });
+ this.deps.trace?.("computer-to-phone-complete");
+ } else {
+ Object.assign(this.baseline, {
+ local,
+ remote,
+ localDigest,
+ remoteDigest,
+ });
+ this.deps.trace?.("unchanged-content");
+ }
+ } catch (error) {
+ // Skip this snapshot, not the user's setting. Read again only after a
+ // new copy; do not hammer iOS with repeated reads of an unreadable item.
+ if (
+ local !== undefined &&
+ remote !== undefined &&
+ skippableContent(error)
+ ) {
+ this.check();
+ Object.assign(this.baseline, {
+ local,
+ remote,
+ // Keep evidence for an unchanged or successfully read peer. Only
+ // an unreadable new version loses its digest, never both peers.
+ localDigest:
+ local === this.baseline.local
+ ? this.baseline.localDigest
+ : readLocalDigest,
+ remoteDigest:
+ remote === this.baseline.remote
+ ? this.baseline.remoteDigest
+ : readRemoteDigest,
+ });
+ this.deps.trace?.("content-skipped");
+ return;
+ }
+ throw error;
+ } finally {
+ this.pending = false;
+ }
+ }
+}
diff --git a/apps/mobile/src/remote-desktop/clipboardSyncFailure.test.ts b/apps/mobile/src/remote-desktop/clipboardSyncFailure.test.ts
new file mode 100644
index 0000000000..f7789b082c
--- /dev/null
+++ b/apps/mobile/src/remote-desktop/clipboardSyncFailure.test.ts
@@ -0,0 +1,38 @@
+import { expect, it } from "vitest";
+import {
+ clipboardSyncFailure,
+ clipboardSyncErrorCode,
+} from "./clipboardSyncFailure";
+
+it("backs off transient failures without changing preferences", () => {
+ expect(clipboardSyncFailure(new Error("INVOKE_TIMEOUT"), 0).delay).toBe(1500);
+ expect(clipboardSyncFailure({ code: "INVOKE_TIMEOUT" }, 99).delay).toBe(
+ 30000,
+ );
+});
+it("waits for deliberate retry after permission denial", () => {
+ expect(clipboardSyncFailure(new Error("PASTE_DENIED"), 0)).toMatchObject({
+ notice: "clipboardSyncPermission",
+ delay: null,
+ });
+});
+
+it.each([
+ "CLIPBOARD_NOT_ALLOWED",
+ "CLIPBOARD_UNSUPPORTED",
+ "CLIPBOARD_CHANGED",
+])("prefers native %s inside a generic Expo error", (code) => {
+ const error = Object.assign(new Error(`Native failure: ${code}`), {
+ code: "ERR_UNEXPECTED",
+ });
+ expect(clipboardSyncErrorCode(error)).toBe(code);
+});
+
+it("retries Android focus loss while preserving permanent permission failures", () => {
+ const error = Object.assign(new Error("CLIPBOARD_NOT_ALLOWED"), {
+ code: "ERR_UNEXPECTED",
+ });
+ expect(clipboardSyncFailure(error, 0).delay).toBe(1500);
+ expect(clipboardSyncFailure(error, 99).delay).toBe(30000);
+ expect(clipboardSyncFailure(new Error("PASTE_DENIED"), 0).delay).toBeNull();
+});
diff --git a/apps/mobile/src/remote-desktop/clipboardSyncFailure.ts b/apps/mobile/src/remote-desktop/clipboardSyncFailure.ts
new file mode 100644
index 0000000000..fcac12e9c6
--- /dev/null
+++ b/apps/mobile/src/remote-desktop/clipboardSyncFailure.ts
@@ -0,0 +1,31 @@
+/** Error metadata only: never include clipboard contents in diagnostics. */
+export function clipboardSyncErrorCode(error: unknown): string {
+ const value = error instanceof Error ? error.message : String(error);
+ const code =
+ error && typeof error === "object" && "code" in error
+ ? String(error.code)
+ : "";
+ return (
+ (value + " " + code).match(/\b(?:CLIPBOARD|PASTE)_[A-Z_]+\b/)?.[0] ??
+ (code + " " + value).match(
+ /\b(?:CLIPBOARD|DESKTOP|INVOKE|PASTE|ERR|E)_[A-Z_]+\b/,
+ )?.[0] ??
+ "UNKNOWN"
+ );
+}
+export function clipboardSyncFailure(error: unknown, failures: number) {
+ const code = clipboardSyncErrorCode(error);
+ // Android uses this exact code for temporary window-focus loss.
+ if (
+ code !== "CLIPBOARD_NOT_ALLOWED" &&
+ /PERMISSION|DENIED|NOT_ALLOWED/.test(code)
+ )
+ return { code, notice: "clipboardSyncPermission", delay: null };
+ if (code === "CLIPBOARD_CONFLICT" || code.endsWith("_CHANGED"))
+ return { code, notice: "clipboardSyncConflict", delay: 1500 };
+ return {
+ code,
+ notice: "clipboardSyncFailed",
+ delay: Math.min(30000, 1500 * 2 ** Math.min(failures, 5)),
+ };
+}
diff --git a/apps/mobile/src/remote-desktop/useLockOnExitPreference.ts b/apps/mobile/src/remote-desktop/useLockOnExitPreference.ts
index af495f3f58..de0cc791cb 100644
--- a/apps/mobile/src/remote-desktop/useLockOnExitPreference.ts
+++ b/apps/mobile/src/remote-desktop/useLockOnExitPreference.ts
@@ -4,7 +4,14 @@ import { useCallback, useEffect, useRef, useState } from "react";
// Non-secret, phone-local override for this computer. Absence follows default off.
let writes: Promise<void> = Promise.resolve();
export function useLockOnExitPreference(deviceId: string) {
- const key = `cindy.mobile.remote-desktop.lock-on-exit.v1.${encodeURIComponent(deviceId)}`;
+ return useRemoteDesktopPreference(deviceId, "lock-on-exit", false);
+}
+export function useRemoteDesktopPreference(
+ deviceId: string,
+ feature: string,
+ defaultValue: boolean,
+) {
+ const key = `cindy.mobile.remote-desktop.${feature}.v1.${encodeURIComponent(deviceId)}`;
const [state, setState] = useState({ key, enabled: false, loaded: false });
const edited = useRef<string | null>(null);
useEffect(() => {
@@ -14,7 +21,11 @@ export function useLockOnExitPreference(deviceId: string) {
.then(() => AsyncStorage.getItem(key))
.then((value) => {
if (active && edited.current !== key)
- setState({ key, enabled: value === "true", loaded: true });
+ setState({
+ key,
+ enabled: value === null ? defaultValue : value === "true",
+ loaded: true,
+ });
})
.catch(() => {
if (active) setState({ key, enabled: false, loaded: true });
@@ -22,7 +33,7 @@ export function useLockOnExitPreference(deviceId: string) {
return () => {
active = false;
};
- }, [key]);
+ }, [key, defaultValue]);
const update = useCallback(
(enabled: boolean) => {
edited.current = key;
diff --git a/apps/mobile/src/remote-desktop/useRemoteDesktopSafety.test.tsx b/apps/mobile/src/remote-desktop/useRemoteDesktopSafety.test.tsx
new file mode 100644
index 0000000000..79ac97f4fa
--- /dev/null
+++ b/apps/mobile/src/remote-desktop/useRemoteDesktopSafety.test.tsx
@@ -0,0 +1,234 @@
+// @vitest-environment jsdom
+import { act, createElement } from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { beforeEach, afterEach, expect, it, vi } from "vitest";
+const h = vi.hoisted(() => ({
+ update: vi.fn(),
+ tick: vi.fn(),
+ request: vi.fn(),
+ privacy: false,
+ hostMute: false,
+ trace: (_stage: string) => {},
+}));
+vi.mock("react-native", () => ({
+ AppState: {
+ currentState: "active",
+ addEventListener: () => ({ remove() {} }),
+ },
+}));
+vi.mock("./useLockOnExitPreference", () => ({
+ useRemoteDesktopPreference: (_device: string, feature: string) => [
+ feature === "clipboard-sync"
+ ? true
+ : feature === "privacy-screen"
+ ? h.privacy
+ : h.hostMute,
+ h.update,
+ true,
+ ],
+}));
+vi.mock("../../modules/cindy-remote-presentation/src", () => ({
+ remotePresentation: {
+ clipboardVersion() {},
+ readClipboard() {},
+ syncClipboard() {},
+ },
+}));
+vi.mock("./clipboardSync", () => ({
+ ClipboardSync: class {
+ constructor(options: { trace: (stage: string) => void }) {
+ h.trace = options.trace;
+ }
+ tick = h.tick;
+ },
+}));
+import { useRemoteDesktopSafety } from "./useRemoteDesktopSafety";
+let root: Root;
+let latest: ReturnType<typeof useRemoteDesktopSafety>;
+const lease = {
+ lease: "lease",
+ controlling: true,
+ display: { id: "1", name: "Main", width: 100, height: 100 },
+};
+const caps = {
+ version: 1 as const,
+ enabled: true,
+ canControl: true,
+ platform: "darwin",
+ displays: [],
+ clipboardSync: true,
+ privacyScreen: false,
+ hostMute: false,
+};
+function Probe({ focused = true }: { focused?: boolean }) {
+ latest = useRemoteDesktopSafety(
+ "computer",
+ lease,
+ true,
+ focused,
+ caps,
+ h.request,
+ );
+ return null;
+}
+beforeEach(() => {
+ h.privacy = false;
+ h.hostMute = false;
+ caps.privacyScreen = false;
+ caps.hostMute = false;
+ (globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
+ vi.useFakeTimers();
+ h.update.mockReset();
+ h.tick.mockReset().mockResolvedValue(undefined);
+ h.request.mockReset().mockResolvedValue({ enabled: true });
+ root = createRoot(document.createElement("div"));
+});
+
+it("privacy requests cannot clear a mute failure, and mute success clears its own failure", async () => {
+ caps.privacyScreen = caps.hostMute = true;
+ h.request.mockImplementation(async (message) => {
+ if (message.op === "hostMute" && !message.enabled)
+ throw new Error("MUTE_FAILED");
+ return { enabled: message.enabled };
+ });
+ await act(async () => root.render(createElement(Probe)));
+ expect(latest.safetyNotice).toBe("hostMuteFailed");
+ h.privacy = true;
+ await act(async () => root.render(createElement(Probe)));
+ expect(latest.safetyNotice).toBe("hostMuteFailed");
+ h.hostMute = true;
+ await act(async () => root.render(createElement(Probe)));
+ expect(latest.safetyNotice).toBeNull();
+});
+
+it("mute success cannot clear a privacy failure", async () => {
+ caps.privacyScreen = caps.hostMute = true;
+ h.request.mockImplementation(async (message) => {
+ if (message.op === "privacyScreen") throw new Error("PRIVACY_FAILED");
+ return { enabled: message.enabled };
+ });
+ await act(async () => root.render(createElement(Probe)));
+ h.hostMute = true;
+ await act(async () => root.render(createElement(Probe)));
+ expect(latest.safetyNotice).toBe("privacyFailed");
+});
+it.each([
+ "clipboardSyncSkipped",
+ "clipboardSyncFailed",
+ "clipboardSyncPermission",
+])(
+ "keeps protection failures visible ahead of %s and preserves the lower-priority notice",
+ async (notice) => {
+ caps.privacyScreen = caps.hostMute = true;
+ h.request.mockImplementation(async (message) => {
+ if (message.enabled && ["privacyScreen", "hostMute"].includes(message.op))
+ throw new Error("PROTECTION_FAILED");
+ return { enabled: message.enabled };
+ });
+ if (notice === "clipboardSyncSkipped")
+ h.tick.mockImplementation(async () => h.trace("content-skipped"));
+ else
+ h.tick.mockRejectedValue(
+ new Error(
+ notice === "clipboardSyncPermission"
+ ? "PASTE_DENIED"
+ : "INVOKE_TIMEOUT",
+ ),
+ );
+ await act(async () => root.render(createElement(Probe)));
+ expect(latest.safetyNotice).toBe(notice);
+ h.privacy = h.hostMute = true;
+ await act(async () => root.render(createElement(Probe)));
+ expect(latest.privacyActive).toBe(false);
+ expect(latest.safetyNotice).toBe("privacyFailed");
+ h.privacy = false;
+ await act(async () => root.render(createElement(Probe)));
+ expect(latest.safetyNotice).toBe("hostMuteFailed");
+ h.hostMute = false;
+ await act(async () => root.render(createElement(Probe)));
+ expect(latest.safetyNotice).toBe(notice);
+ },
+);
+it("clipboard recovery cannot hide a failed privacy screen", async () => {
+ caps.privacyScreen = true;
+ h.privacy = true;
+ h.request.mockImplementation(async (message) => {
+ if (message.op === "privacyScreen") throw new Error("PRIVACY_FAILED");
+ return {};
+ });
+ h.tick.mockRejectedValueOnce(new Error("INVOKE_TIMEOUT"));
+ await act(async () => root.render(createElement(Probe)));
+ expect(latest.safetyNotice).toBe("privacyFailed");
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(1500);
+ });
+ expect(h.tick).toHaveBeenCalledTimes(2);
+ expect(latest.safetyNotice).toBe("privacyFailed");
+});
+afterEach(async () => {
+ await act(async () => root.unmount());
+ vi.useRealTimers();
+});
+it.each(["enable", "transfer", "focus"])(
+ "preserves the enabled preference and recovers from %s failure",
+ async (stage) => {
+ if (stage === "enable")
+ h.request.mockRejectedValueOnce(new Error("INVOKE_TIMEOUT"));
+ else
+ h.tick.mockRejectedValueOnce(
+ new Error(
+ stage === "focus" ? "CLIPBOARD_NOT_ALLOWED" : "INVOKE_TIMEOUT",
+ ),
+ );
+ await act(async () => root.render(createElement(Probe)));
+ expect(latest.clipboardSync).toBe(true);
+ expect(latest.safetyNotice).toBe("clipboardSyncFailed");
+ expect(h.update).not.toHaveBeenCalled();
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(1500);
+ });
+ expect(latest.safetyNotice).toBeNull();
+ expect(latest.clipboardSync).toBe(true);
+ expect(h.update).not.toHaveBeenCalled();
+ },
+);
+it("does not repeatedly request denied permission and supports retry without toggling off", async () => {
+ h.tick.mockRejectedValueOnce(new Error("PASTE_DENIED"));
+ await act(async () => root.render(createElement(Probe)));
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(60000);
+ });
+ expect(h.tick).toHaveBeenCalledTimes(1);
+ expect(latest.safetyNotice).toBe("clipboardSyncPermission");
+ await act(async () => latest.onClipboardSyncRetry());
+ expect(h.tick).toHaveBeenCalledTimes(2);
+ expect(latest.clipboardSync).toBe(true);
+ expect(latest.safetyNotice).toBeNull();
+ expect(h.update).not.toHaveBeenCalled();
+});
+
+it("foreground replacement never sends a stale cleanup disable", async () => {
+ await act(async () => root.render(createElement(Probe)));
+ await act(async () => root.render(createElement(Probe, { focused: false })));
+ await act(async () => root.render(createElement(Probe)));
+ expect(
+ h.request.mock.calls.filter(
+ ([message]) => message.op === "clipboardSync" && !message.enabled,
+ ),
+ ).toHaveLength(0);
+ expect(h.tick).toHaveBeenCalledTimes(2);
+ expect(h.update).not.toHaveBeenCalled();
+});
+it("renews host opt-in after a delayed disable from an earlier client state", async () => {
+ h.tick.mockRejectedValueOnce(new Error("DESKTOP_CLIPBOARD_UNAVAILABLE"));
+ await act(async () => root.render(createElement(Probe)));
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(1500);
+ });
+ expect(
+ h.request.mock.calls.filter(
+ ([message]) => message.op === "clipboardSync" && message.enabled,
+ ),
+ ).toHaveLength(2);
+ expect(latest.safetyNotice).toBeNull();
+});
diff --git a/apps/mobile/src/remote-desktop/useRemoteDesktopSafety.ts b/apps/mobile/src/remote-desktop/useRemoteDesktopSafety.ts
new file mode 100644
index 0000000000..ecd44eca8f
--- /dev/null
+++ b/apps/mobile/src/remote-desktop/useRemoteDesktopSafety.ts
@@ -0,0 +1,324 @@
+import { useEffect, useRef, useState } from "react";
+import { AppState } from "react-native";
+import type {
+ RemoteDesktopCapabilities,
+ RemoteDesktopLease,
+ RemoteDesktopRequest,
+} from "@cindy/device-link";
+import { remotePresentation } from "../../modules/cindy-remote-presentation/src";
+import { useRemoteDesktopPreference } from "./useLockOnExitPreference";
+import {
+ ClipboardSync,
+ type ClipboardSyncBaseline,
+ type LocalClipboardReadCache,
+} from "./clipboardSync";
+import { clipboardSyncFailure } from "./clipboardSyncFailure";
+
+// One phone-local version/digest, memory only; no clipboard text or permission grant.
+const localReadCache: LocalClipboardReadCache = {};
+
+export function useRemoteDesktopSafety(
+ deviceId: string,
+ lease: RemoteDesktopLease | null,
+ connected: boolean,
+ focused: boolean,
+ caps: RemoteDesktopCapabilities | null,
+ request: <T>(
+ message: RemoteDesktopRequest,
+ preSend?: () => void,
+ ) => Promise<T>,
+ lockOnExit = false,
+) {
+ const [privacy, setPrivacy, privacyLoaded] = useRemoteDesktopPreference(
+ deviceId,
+ "privacy-screen",
+ false,
+ );
+ const [sync, setSync, syncLoaded] = useRemoteDesktopPreference(
+ deviceId,
+ "clipboard-sync",
+ false,
+ );
+ const [hostMute, setHostMute, hostMuteLoaded] = useRemoteDesktopPreference(
+ deviceId,
+ "host-mute",
+ false,
+ );
+ const [privacyNotice, setPrivacyNotice] = useState<string | null>(null);
+ const [hostMuteNotice, setHostMuteNotice] = useState<string | null>(null);
+ const [clipboardNotice, setClipboardNotice] = useState<string | null>(null);
+ const [syncAttempt, setSyncAttempt] = useState(0);
+ const [privacyActive, setPrivacyActive] = useState(false);
+ const [foreground, setForeground] = useState(
+ AppState.currentState === "active",
+ );
+ useEffect(() => {
+ const listener = AppState.addEventListener("change", (state) =>
+ setForeground(state === "active"),
+ );
+ return () => listener.remove();
+ }, []);
+ const latest = useRef({ lease, sync, focused, connected });
+ latest.current = { lease, sync, focused, connected };
+ const syncSupported =
+ caps?.clipboardSync === true &&
+ !!remotePresentation?.clipboardVersion &&
+ !!remotePresentation?.syncClipboard &&
+ !!remotePresentation?.readClipboard;
+ const syncBaseline = useRef<{
+ deviceId: string;
+ value: ClipboardSyncBaseline;
+ }>({
+ deviceId,
+ value: { local: "", remote: "" },
+ });
+ // Foreground/control/lease changes pause synchronization, not copy history.
+ // Explicit opt-out and changing computers start a fresh baseline.
+ useEffect(() => {
+ if (!sync || syncBaseline.current.deviceId !== deviceId)
+ syncBaseline.current = { deviceId, value: { local: "", remote: "" } };
+ setClipboardNotice(null);
+ }, [deviceId, sync]);
+ useEffect(() => {
+ console.debug("[clipboard-sync] state", {
+ enabled: sync,
+ loaded: syncLoaded,
+ supported: syncSupported,
+ connected,
+ controlling: lease?.controlling === true,
+ focused,
+ foreground,
+ });
+ }, [
+ sync,
+ syncLoaded,
+ syncSupported,
+ connected,
+ lease?.controlling,
+ focused,
+ foreground,
+ ]);
+ useEffect(() => {
+ setPrivacyActive(false);
+ setPrivacyNotice(null);
+ if (
+ !lease?.controlling ||
+ !connected ||
+ !privacyLoaded ||
+ !caps?.privacyScreen
+ )
+ return;
+ let current = true;
+ const check = () => {
+ if (!current) throw new Error("DESKTOP_LEASE_EXPIRED");
+ };
+ void request<{ enabled: boolean }>(
+ { op: "privacyScreen", lease: lease.lease, enabled: privacy, lockOnExit },
+ check,
+ )
+ .then((result) => {
+ if (current) setPrivacyActive(result.enabled);
+ })
+ .catch(() => {
+ if (current) setPrivacyNotice("privacyFailed");
+ });
+ return () => {
+ current = false;
+ };
+ }, [
+ lease?.lease,
+ lease?.controlling,
+ connected,
+ privacy,
+ privacyLoaded,
+ lockOnExit,
+ caps?.privacyScreen,
+ request,
+ ]);
+ useEffect(() => {
+ setHostMuteNotice(null);
+ if (
+ !lease?.controlling ||
+ !connected ||
+ !hostMuteLoaded ||
+ caps?.hostMute !== true
+ )
+ return;
+ let current = true;
+ void request({
+ op: "hostMute",
+ lease: lease.lease,
+ enabled: hostMute,
+ })
+ .then(() => {
+ if (current) setHostMuteNotice(null);
+ })
+ .catch(() => {
+ if (current) setHostMuteNotice("hostMuteFailed");
+ });
+ return () => {
+ current = false;
+ };
+ }, [
+ lease?.lease,
+ lease?.controlling,
+ connected,
+ hostMute,
+ hostMuteLoaded,
+ caps?.hostMute,
+ request,
+ ]);
+ useEffect(() => {
+ if (!lease || !connected || !syncLoaded || sync || !syncSupported) return;
+ let current = true;
+ void request(
+ { op: "clipboardSync", lease: lease.lease, enabled: false },
+ () => {
+ if (
+ !current ||
+ latest.current.sync ||
+ latest.current.lease?.lease !== lease.lease
+ )
+ throw new Error("DESKTOP_LEASE_EXPIRED");
+ },
+ ).catch(() => {});
+ return () => {
+ current = false;
+ };
+ }, [lease?.lease, connected, syncLoaded, sync, syncSupported, request]);
+ useEffect(() => {
+ if (
+ !lease ||
+ !connected ||
+ !lease.controlling ||
+ !focused ||
+ !foreground ||
+ !sync ||
+ !syncLoaded ||
+ !syncSupported
+ )
+ return;
+ let current = true;
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const valid = () =>
+ current &&
+ latest.current.lease?.lease === lease.lease &&
+ latest.current.lease.controlling &&
+ latest.current.connected &&
+ latest.current.sync &&
+ latest.current.focused &&
+ AppState.currentState === "active";
+ const check = () => {
+ if (!valid()) throw new Error("DESKTOP_LEASE_EXPIRED");
+ };
+ const guardedRequest = <T>(message: RemoteDesktopRequest) =>
+ request<T>(message, check);
+ console.debug("[clipboard-sync] started");
+ const engine = new ClipboardSync(
+ {
+ lease: lease.lease,
+ current: valid,
+ inline: caps?.clipboardInline === true,
+ localReadCache,
+ trace: (stage) => {
+ console.debug("[clipboard-sync] progress", { stage });
+ if (!valid()) return;
+ if (stage === "content-skipped")
+ setClipboardNotice("clipboardSyncSkipped");
+ if (
+ stage === "phone-to-computer-complete" ||
+ stage === "computer-to-phone-complete"
+ )
+ setClipboardNotice(null);
+ },
+ request: guardedRequest,
+ localVersion: () => remotePresentation!.clipboardVersion!(),
+ readLocal: () => remotePresentation!.readClipboard!(),
+ writeLocal: (json, version) =>
+ remotePresentation!.syncClipboard!(json, version),
+ },
+ syncBaseline.current.value,
+ );
+ let enabledOnHost = false;
+ let failures = 0;
+ const run = async () => {
+ try {
+ if (!enabledOnHost) {
+ await guardedRequest({
+ op: "clipboardSync",
+ lease: lease.lease,
+ enabled: true,
+ });
+ check();
+ enabledOnHost = true;
+ }
+ await engine.tick();
+ if (valid())
+ setClipboardNotice((value) =>
+ value === "clipboardSyncFailed" ||
+ value === "clipboardSyncPermission"
+ ? null
+ : value,
+ );
+ failures = 0;
+ if (valid()) timer = setTimeout(() => void run(), 1500);
+ } catch (error) {
+ if (!valid()) return;
+ const failure = clipboardSyncFailure(error, failures++);
+ if (failure.code === "DESKTOP_CLIPBOARD_UNAVAILABLE")
+ enabledOnHost = false;
+ console.debug("[clipboard-sync] failed", {
+ stage: enabledOnHost ? "transfer" : "enable",
+ code: failure.code,
+ });
+ setClipboardNotice(failure.notice);
+ // Permission errors wait for a deliberate retry; transient failures use
+ // bounded backoff. Neither path writes the user's persisted preference.
+ if (failure.delay !== null)
+ timer = setTimeout(() => void run(), failure.delay);
+ }
+ };
+ void run();
+ return () => {
+ console.debug("[clipboard-sync] paused");
+ current = false;
+ clearTimeout(timer);
+ // Pausing stops the caller-owned polling only. The host never initiates
+ // reads; its lease-scoped opt-in ends on control loss or disconnect.
+ // No late cleanup RPC can disable a replacement foreground loop.
+ };
+ }, [
+ deviceId,
+ lease?.lease,
+ lease?.controlling,
+ connected,
+ focused,
+ foreground,
+ sync,
+ syncLoaded,
+ syncSupported,
+ caps?.clipboardInline,
+ syncAttempt,
+ request,
+ setSync,
+ ]);
+ return {
+ privacy,
+ privacyActive,
+ privacyAvailable: privacyLoaded && caps?.privacyScreen === true,
+ onPrivacy: setPrivacy,
+ clipboardSync: sync,
+ clipboardSyncAvailable: syncLoaded && syncSupported,
+ onClipboardSync: setSync,
+ onClipboardSyncRetry: () => {
+ syncBaseline.current = { deviceId, value: { local: "", remote: "" } };
+ setClipboardNotice(null);
+ setSyncAttempt((value) => value + 1);
+ },
+ hostMute,
+ hostMuteAvailable: hostMuteLoaded && caps?.hostMute === true,
+ onHostMute: setHostMute,
+ // Failed screen/audio protection must stay visible over transfer notices.
+ safetyNotice: privacyNotice ?? hostMuteNotice ?? clipboardNotice,
+ };
+}
diff --git a/docs/design-rules/design-inventory.md b/docs/design-rules/design-inventory.md
index 773f31ce93..47cb157fa5 100644
--- a/docs/design-rules/design-inventory.md
+++ b/docs/design-rules/design-inventory.md
@@ -62,7 +62,7 @@
| `mobile.files` | mobile | 任务文件与预览 | app/files/[sessionId].tsx / app/files/preview/[sessionId].tsx | AndroidUpdateSheet, AppText, AuthContext, BlurBackdrop, CenteredScreen, ComposerNativeSection, ComposerNativeSection.ios, ComposerSheet, ComposerSheet.ios, ConnectionBanner, ConnectionNoticeOverlay, ContextSheet, ContextSheet.ios, DeviceLinkContext, HtmlFileReader, ImageLightbox, IncomingShareBridge, MarkdownFileReader, MobileLoginHandoffContext, MobileLoginHandoffStage, MobilePrimitives, PushNotificationsBridge, QuietSyncIndicator, SheetModal, SheetSurface, StartupSplashOverlay, ThemeProvider, [sessionId], _layout, gestureHandler, mediaPlayerWebView, peerFileTransport, useLocale | apps/mobile/app/_layout.tsx, apps/mobile/app/files/[sessionId].tsx, apps/mobile/app/files/preview/[sessionId].tsx, apps/mobile/src/auth/AuthContext.tsx, apps/mobile/src/auth/MobileLoginHandoffContext.tsx, apps/mobile/src/components/AppText.tsx, apps/mobile/src/components/CenteredScreen.tsx, apps/mobile/src/components/ConnectionBanner.tsx, apps/mobile/src/components/ConnectionNoticeOverlay.tsx, apps/mobile/src/components/MobileLoginHandoffStage.tsx, apps/mobile/src/components/MobilePrimitives.tsx, apps/mobile/src/components/QuietSyncIndicator.tsx, apps/mobile/src/components/StartupSplashOverlay.tsx, apps/mobile/src/device-link/DeviceLinkContext.tsx, apps/mobile/src/device-link/peerFileTransport.tsx, apps/mobile/src/i18n/useLocale.tsx, apps/mobile/src/notifications/PushNotificationsBridge.tsx, apps/mobile/src/platform/gestureHandler.tsx, apps/mobile/src/session/BlurBackdrop.tsx, apps/mobile/src/session/ComposerNativeSection.ios.tsx, apps/mobile/src/session/ComposerNativeSection.tsx, apps/mobile/src/session/ComposerSheet.ios.tsx, apps/mobile/src/session/ComposerSheet.tsx, apps/mobile/src/session/ContextSheet.ios.tsx, apps/mobile/src/session/ContextSheet.tsx, apps/mobile/src/session/HtmlFileReader.tsx, apps/mobile/src/session/ImageLightbox.tsx, apps/mobile/src/session/IncomingShareBridge.tsx, apps/mobile/src/session/MarkdownFileReader.tsx, apps/mobile/src/session/SheetModal.tsx, apps/mobile/src/session/SheetSurface.tsx, apps/mobile/src/session/mathWebViewHtml.ts, apps/mobile/src/session/mediaPlayerWebView.tsx, apps/mobile/src/session/mediaPlayerWebViewHtml.ts, apps/mobile/src/session/selectableMarkdownHtml.ts, apps/mobile/src/theme/ThemeProvider.tsx, apps/mobile/src/theme/navigationTheme.ts, apps/mobile/src/theme/tokens.ts, apps/mobile/src/update/AndroidUpdateSheet.tsx | 0 | 202 | 39 |
| `mobile.home` | mobile | 主机与资源首页 | app/index.tsx / app/devices/index.tsx | AccountSwitcherSheet, AndroidUpdateSheet, AppText, AuthContext, AutomationTimerIcon, BlurBackdrop, CenteredScreen, ComposerNativeSection, ComposerNativeSection.ios, ComposerSheet, ComposerSheet.ios, ConnectionBanner, ConnectionNoticeOverlay, ContextSheet, ContextSheet.ios, ConversationSearchFilterSheet, DeviceLinkContext, HomeChromeDrawer, HomeChromeFrost, HomeGlassMenuPanel, HomeHeaderGlassButton, HomeNativeStackHeader, HomeProjectMachineLabel, HomeSearchBar, IncomingShareBridge, MobileAgentMark, MobileLoginHandoffContext, MobileLoginHandoffStage, MobilePrimitives, MobileVendorIcon, NativePullDownMenu, NativeSwitch, PushNotificationsBridge, QuietSyncIndicator, RemoteAccessGuide, RemoteTaskSuggestions, RenameSessionModal, SessionActionSheet, SessionHeaderNativeControls, SessionHeaderNativeControls.ios, SessionOptionsExpoSheet, SheetModal, SheetSurface, SimpleStackHeader, StartupSplashOverlay, SwipeableSessionRow, SwipeableSessionRow.ios, ThemeProvider, _layout, gestureHandler, index, peerFileTransport, useLocale | apps/mobile/app/_layout.tsx, apps/mobile/app/devices/index.tsx, apps/mobile/app/index.tsx, apps/mobile/src/auth/AuthContext.tsx, apps/mobile/src/auth/MobileLoginHandoffContext.tsx, apps/mobile/src/components/AppText.tsx, apps/mobile/src/components/CenteredScreen.tsx, apps/mobile/src/components/ConnectionBanner.tsx, apps/mobile/src/components/ConnectionNoticeOverlay.tsx, apps/mobile/src/components/MobileAgentMark.tsx, apps/mobile/src/components/MobileLoginHandoffStage.tsx, apps/mobile/src/components/MobilePrimitives.tsx, apps/mobile/src/components/MobileVendorIcon.tsx, apps/mobile/src/components/QuietSyncIndicator.tsx, apps/mobile/src/components/RemoteAccessGuide.tsx, apps/mobile/src/components/StartupSplashOverlay.tsx, apps/mobile/src/device-link/DeviceLinkContext.tsx, apps/mobile/src/device-link/peerFileTransport.tsx, apps/mobile/src/i18n/useLocale.tsx, apps/mobile/src/notifications/PushNotificationsBridge.tsx, apps/mobile/src/platform/chrome/HomeNativeStackHeader.tsx, apps/mobile/src/platform/chrome/NativePullDownMenu.tsx, apps/mobile/src/platform/chrome/NativeSwitch.tsx, apps/mobile/src/platform/chrome/SimpleStackHeader.tsx, apps/mobile/src/platform/gestureHandler.tsx, apps/mobile/src/session/AccountSwitcherSheet.tsx, apps/mobile/src/session/AutomationTimerIcon.tsx, apps/mobile/src/session/BlurBackdrop.tsx, apps/mobile/src/session/ComposerNativeSection.ios.tsx, apps/mobile/src/session/ComposerNativeSection.tsx, apps/mobile/src/session/ComposerSheet.ios.tsx, apps/mobile/src/session/ComposerSheet.tsx, apps/mobile/src/session/ContextSheet.ios.tsx, apps/mobile/src/session/ContextSheet.tsx, apps/mobile/src/session/ConversationSearchFilterSheet.tsx, apps/mobile/src/session/HomeChromeDrawer.tsx, apps/mobile/src/session/HomeChromeFrost.tsx, apps/mobile/src/session/HomeGlassMenuPanel.tsx, apps/mobile/src/session/HomeHeaderGlassButton.tsx, apps/mobile/src/session/HomeProjectMachineLabel.tsx, apps/mobile/src/session/HomeSearchBar.tsx, apps/mobile/src/session/IncomingShareBridge.tsx, apps/mobile/src/session/RemoteTaskSuggestions.tsx, apps/mobile/src/session/RenameSessionModal.tsx, apps/mobile/src/session/SessionActionSheet.tsx, apps/mobile/src/session/SessionHeaderNativeControls.ios.tsx, apps/mobile/src/session/SessionHeaderNativeControls.tsx, apps/mobile/src/session/SessionOptionsExpoSheet.tsx, apps/mobile/src/session/SheetModal.tsx, apps/mobile/src/session/SheetSurface.tsx, apps/mobile/src/session/SwipeableSessionRow.ios.tsx, apps/mobile/src/session/SwipeableSessionRow.tsx, apps/mobile/src/theme/ThemeProvider.tsx, apps/mobile/src/theme/navigationTheme.ts, apps/mobile/src/theme/tokens.ts, apps/mobile/src/update/AndroidUpdateSheet.tsx | 0 | 170 | 60 |
| `mobile.overlay.connection-startup` | mobile | 根布局挂载的启动与连接反馈 | app/_layout.tsx → StartupSplashOverlay / ConnectionNoticeProvider;各页面 ConnectionBanner | AndroidUpdateSheet, AppText, AuthContext, BlurBackdrop, CenteredScreen, ComposerNativeSection, ComposerNativeSection.ios, ComposerSheet, ComposerSheet.ios, ConnectionBanner, ConnectionNoticeOverlay, ConnectionNoticeProvider, ContextSheet, ContextSheet.ios, DeviceLinkContext, IncomingShareBridge, MobileLoginHandoffContext, MobileLoginHandoffStage, MobilePrimitives, PushNotificationsBridge, QuietSyncIndicator, SheetModal, SheetSurface, StartupSplashOverlay, ThemeProvider, _layout, gestureHandler, peerFileTransport, useLocale | apps/mobile/app/_layout.tsx, apps/mobile/src/auth/AuthContext.tsx, apps/mobile/src/auth/MobileLoginHandoffContext.tsx, apps/mobile/src/components/AppText.tsx, apps/mobile/src/components/CenteredScreen.tsx, apps/mobile/src/components/ConnectionBanner.tsx, apps/mobile/src/components/ConnectionNoticeOverlay.tsx, apps/mobile/src/components/MobileLoginHandoffStage.tsx, apps/mobile/src/components/MobilePrimitives.tsx, apps/mobile/src/components/QuietSyncIndicator.tsx, apps/mobile/src/components/StartupSplashOverlay.tsx, apps/mobile/src/device-link/DeviceLinkContext.tsx, apps/mobile/src/device-link/peerFileTransport.tsx, apps/mobile/src/i18n/useLocale.tsx, apps/mobile/src/notifications/PushNotificationsBridge.tsx, apps/mobile/src/platform/gestureHandler.tsx, apps/mobile/src/session/BlurBackdrop.tsx, apps/mobile/src/session/ComposerNativeSection.ios.tsx, apps/mobile/src/session/ComposerNativeSection.tsx, apps/mobile/src/session/ComposerSheet.ios.tsx, apps/mobile/src/session/ComposerSheet.tsx, apps/mobile/src/session/ContextSheet.ios.tsx, apps/mobile/src/session/ContextSheet.tsx, apps/mobile/src/session/IncomingShareBridge.tsx, apps/mobile/src/session/SheetModal.tsx, apps/mobile/src/session/SheetSurface.tsx, apps/mobile/src/theme/ThemeProvider.tsx, apps/mobile/src/theme/navigationTheme.ts, apps/mobile/src/theme/tokens.ts, apps/mobile/src/update/AndroidUpdateSheet.tsx | 0 | 170 | 16 |
-| `mobile.remote-desktop` | mobile | 远程桌面 | app/devices/desktop/[deviceId].tsx | AndroidUpdateSheet, AppText, AuthContext, BlurBackdrop, CenteredScreen, ComposerNativeSection, ComposerNativeSection.ios, ComposerSheet, ComposerSheet.ios, ConnectionBanner, ConnectionNoticeOverlay, ContextSheet, ContextSheet.ios, DeviceLinkContext, IncomingShareBridge, MobileLoginHandoffContext, MobileLoginHandoffStage, MobilePrimitives, NativePullDownMenu, NativeSwitch, PermissionGuide, PermissionGuideView, PermissionGuideView.ios, PushNotificationsBridge, QuietSyncIndicator, RemoteDesktopActionButton, RemoteDesktopActionButton.ios, RemoteDesktopBackButton, RemoteDesktopBackButton.ios, RemoteDesktopChrome, RemoteDesktopChrome.ios, RemoteDesktopClipboardButton, RemoteDesktopControls, RemoteDesktopDisplaySettings, RemoteDesktopIcons, RemoteDesktopMouseControls, RemoteDesktopMouseControls.ios, RemoteDesktopNetworkStatus, RemoteDesktopPanelButton, RemoteDesktopPanelButton.ios, RemoteDesktopScreen, RemoteDesktopSecuritySettings, SheetModal, SheetSurface, StartupSplashOverlay, ThemeProvider, [deviceId], _layout, gestureHandler, peerFileTransport, useLocale | apps/mobile/app/_layout.tsx, apps/mobile/app/devices/desktop/[deviceId].tsx, apps/mobile/src/auth/AuthContext.tsx, apps/mobile/src/auth/MobileLoginHandoffContext.tsx, apps/mobile/src/components/AppText.tsx, apps/mobile/src/components/CenteredScreen.tsx, apps/mobile/src/components/ConnectionBanner.tsx, apps/mobile/src/components/ConnectionNoticeOverlay.tsx, apps/mobile/src/components/MobileLoginHandoffStage.tsx, apps/mobile/src/components/MobilePrimitives.tsx, apps/mobile/src/components/QuietSyncIndicator.tsx, apps/mobile/src/components/StartupSplashOverlay.tsx, apps/mobile/src/device-link/DeviceLinkContext.tsx, apps/mobile/src/device-link/peerFileTransport.tsx, apps/mobile/src/i18n/useLocale.tsx, apps/mobile/src/notifications/PushNotificationsBridge.tsx, apps/mobile/src/platform/chrome/NativePullDownMenu.tsx, apps/mobile/src/platform/chrome/NativeSwitch.tsx, apps/mobile/src/platform/gestureHandler.tsx, apps/mobile/src/remote-desktop/PermissionGuide.tsx, apps/mobile/src/remote-desktop/PermissionGuideView.ios.tsx, apps/mobile/src/remote-desktop/PermissionGuideView.tsx, apps/mobile/src/remote-desktop/RemoteDesktopActionButton.ios.tsx, apps/mobile/src/remote-desktop/RemoteDesktopActionButton.tsx, apps/mobile/src/remote-desktop/RemoteDesktopBackButton.ios.tsx, apps/mobile/src/remote-desktop/RemoteDesktopBackButton.tsx, apps/mobile/src/remote-desktop/RemoteDesktopChrome.ios.tsx, apps/mobile/src/remote-desktop/RemoteDesktopChrome.tsx, apps/mobile/src/remote-desktop/RemoteDesktopClipboardButton.tsx, apps/mobile/src/remote-desktop/RemoteDesktopControls.tsx, apps/mobile/src/remote-desktop/RemoteDesktopDisplaySettings.tsx, apps/mobile/src/remote-desktop/RemoteDesktopIcons.tsx, apps/mobile/src/remote-desktop/RemoteDesktopMouseControls.ios.tsx, apps/mobile/src/remote-desktop/RemoteDesktopMouseControls.tsx, apps/mobile/src/remote-desktop/RemoteDesktopNetworkStatus.tsx, apps/mobile/src/remote-desktop/RemoteDesktopPanelButton.ios.tsx, apps/mobile/src/remote-desktop/RemoteDesktopPanelButton.tsx, apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx, apps/mobile/src/remote-desktop/RemoteDesktopSecuritySettings.tsx, apps/mobile/src/remote-desktop/viewerHtml.ts, apps/mobile/src/session/BlurBackdrop.tsx, apps/mobile/src/session/ComposerNativeSection.ios.tsx, apps/mobile/src/session/ComposerNativeSection.tsx, apps/mobile/src/session/ComposerSheet.ios.tsx, apps/mobile/src/session/ComposerSheet.tsx, apps/mobile/src/session/ContextSheet.ios.tsx, apps/mobile/src/session/ContextSheet.tsx, apps/mobile/src/session/IncomingShareBridge.tsx, apps/mobile/src/session/SheetModal.tsx, apps/mobile/src/session/SheetSurface.tsx, apps/mobile/src/theme/ThemeProvider.tsx, apps/mobile/src/theme/navigationTheme.ts, apps/mobile/src/theme/tokens.ts, apps/mobile/src/update/AndroidUpdateSheet.tsx | 0 | 170 | 45 |
+| `mobile.remote-desktop` | mobile | 远程桌面 | app/devices/desktop/[deviceId].tsx | AndroidUpdateSheet, AppText, AuthContext, BlurBackdrop, CenteredScreen, ComposerNativeSection, ComposerNativeSection.ios, ComposerSheet, ComposerSheet.ios, ConnectionBanner, ConnectionNoticeOverlay, ContextSheet, ContextSheet.ios, DeviceLinkContext, IncomingShareBridge, MobileLoginHandoffContext, MobileLoginHandoffStage, MobilePrimitives, NativePullDownMenu, NativeSwitch, PermissionGuide, PermissionGuideView, PermissionGuideView.ios, PushNotificationsBridge, QuietSyncIndicator, RemoteDesktopActionButton, RemoteDesktopActionButton.ios, RemoteDesktopBackButton, RemoteDesktopBackButton.ios, RemoteDesktopChrome, RemoteDesktopChrome.ios, RemoteDesktopClipboardButton, RemoteDesktopControls, RemoteDesktopDisplaySettings, RemoteDesktopIcons, RemoteDesktopMouseControls, RemoteDesktopMouseControls.ios, RemoteDesktopNetworkStatus, RemoteDesktopPanelButton, RemoteDesktopPanelButton.ios, RemoteDesktopScreen, RemoteDesktopSecuritySettings, SheetModal, SheetSurface, StartupSplashOverlay, ThemeProvider, [deviceId], _layout, gestureHandler, peerFileTransport, useLocale | apps/mobile/app/_layout.tsx, apps/mobile/app/devices/desktop/[deviceId].tsx, apps/mobile/src/auth/AuthContext.tsx, apps/mobile/src/auth/MobileLoginHandoffContext.tsx, apps/mobile/src/components/AppText.tsx, apps/mobile/src/components/CenteredScreen.tsx, apps/mobile/src/components/ConnectionBanner.tsx, apps/mobile/src/components/ConnectionNoticeOverlay.tsx, apps/mobile/src/components/MobileLoginHandoffStage.tsx, apps/mobile/src/components/MobilePrimitives.tsx, apps/mobile/src/components/QuietSyncIndicator.tsx, apps/mobile/src/components/StartupSplashOverlay.tsx, apps/mobile/src/device-link/DeviceLinkContext.tsx, apps/mobile/src/device-link/peerFileTransport.tsx, apps/mobile/src/i18n/useLocale.tsx, apps/mobile/src/notifications/PushNotificationsBridge.tsx, apps/mobile/src/platform/chrome/NativePullDownMenu.tsx, apps/mobile/src/platform/chrome/NativeSwitch.tsx, apps/mobile/src/platform/gestureHandler.tsx, apps/mobile/src/remote-desktop/PermissionGuide.tsx, apps/mobile/src/remote-desktop/PermissionGuideView.ios.tsx, apps/mobile/src/remote-desktop/PermissionGuideView.tsx, apps/mobile/src/remote-desktop/RemoteDesktopActionButton.ios.tsx, apps/mobile/src/remote-desktop/RemoteDesktopActionButton.tsx, apps/mobile/src/remote-desktop/RemoteDesktopBackButton.ios.tsx, apps/mobile/src/remote-desktop/RemoteDesktopBackButton.tsx, apps/mobile/src/remote-desktop/RemoteDesktopChrome.ios.tsx, apps/mobile/src/remote-desktop/RemoteDesktopChrome.tsx, apps/mobile/src/remote-desktop/RemoteDesktopClipboardButton.tsx, apps/mobile/src/remote-desktop/RemoteDesktopControls.tsx, apps/mobile/src/remote-desktop/RemoteDesktopDisplaySettings.tsx, apps/mobile/src/remote-desktop/RemoteDesktopIcons.tsx, apps/mobile/src/remote-desktop/RemoteDesktopMouseControls.ios.tsx, apps/mobile/src/remote-desktop/RemoteDesktopMouseControls.tsx, apps/mobile/src/remote-desktop/RemoteDesktopNetworkStatus.tsx, apps/mobile/src/remote-desktop/RemoteDesktopPanelButton.ios.tsx, apps/mobile/src/remote-desktop/RemoteDesktopPanelButton.tsx, apps/mobile/src/remote-desktop/RemoteDesktopScreen.tsx, apps/mobile/src/remote-desktop/RemoteDesktopSecuritySettings.tsx, apps/mobile/src/remote-desktop/viewerHtml.ts, apps/mobile/src/session/BlurBackdrop.tsx, apps/mobile/src/session/ComposerNativeSection.ios.tsx, apps/mobile/src/session/ComposerNativeSection.tsx, apps/mobile/src/session/ComposerSheet.ios.tsx, apps/mobile/src/session/ComposerSheet.tsx, apps/mobile/src/session/ContextSheet.ios.tsx, apps/mobile/src/session/ContextSheet.tsx, apps/mobile/src/session/IncomingShareBridge.tsx, apps/mobile/src/session/SheetModal.tsx, apps/mobile/src/session/SheetSurface.tsx, apps/mobile/src/theme/ThemeProvider.tsx, apps/mobile/src/theme/navigationTheme.ts, apps/mobile/src/theme/tokens.ts, apps/mobile/src/update/AndroidUpdateSheet.tsx | 0 | 170 | 47 |
| `mobile.resources` | mobile | 远程资源列表与详情 | app/resources/[collectionId].tsx / app/resources/[collectionId]/[resourceId].tsx | AndroidUpdateSheet, AppText, AuthContext, BlurBackdrop, CenteredScreen, ComposerNativeSection, ComposerNativeSection.ios, ComposerSheet, ComposerSheet.ios, ConnectionBanner, ConnectionNoticeOverlay, ContextSheet, ContextSheet.ios, DeviceLinkContext, HomeNativeStackHeader, IncomingShareBridge, MobileLoginHandoffContext, MobileLoginHandoffStage, MobilePrimitives, NativePullDownMenu, NativeSwitch, PushNotificationsBridge, QuietSyncIndicator, RemoteCompanionAvatar, SheetModal, SheetSurface, SimpleStackHeader, StartupSplashOverlay, ThemeProvider, [collectionId], [resourceId], _layout, gestureHandler, peerFileTransport, useLocale | apps/mobile/app/_layout.tsx, apps/mobile/app/resources/[collectionId].tsx, apps/mobile/app/resources/[collectionId]/[resourceId].tsx, apps/mobile/src/auth/AuthContext.tsx, apps/mobile/src/auth/MobileLoginHandoffContext.tsx, apps/mobile/src/components/AppText.tsx, apps/mobile/src/components/CenteredScreen.tsx, apps/mobile/src/components/ConnectionBanner.tsx, apps/mobile/src/components/ConnectionNoticeOverlay.tsx, apps/mobile/src/components/MobileLoginHandoffStage.tsx, apps/mobile/src/components/MobilePrimitives.tsx, apps/mobile/src/components/QuietSyncIndicator.tsx, apps/mobile/src/components/RemoteCompanionAvatar.tsx, apps/mobile/src/components/StartupSplashOverlay.tsx, apps/mobile/src/device-link/DeviceLinkContext.tsx, apps/mobile/src/device-link/peerFileTransport.tsx, apps/mobile/src/i18n/useLocale.tsx, apps/mobile/src/notifications/PushNotificationsBridge.tsx, apps/mobile/src/platform/chrome/HomeNativeStackHeader.tsx, apps/mobile/src/platform/chrome/NativePullDownMenu.tsx, apps/mobile/src/platform/chrome/NativeSwitch.tsx, apps/mobile/src/platform/chrome/SimpleStackHeader.tsx, apps/mobile/src/platform/gestureHandler.tsx, apps/mobile/src/session/BlurBackdrop.tsx, apps/mobile/src/session/ComposerNativeSection.ios.tsx, apps/mobile/src/session/ComposerNativeSection.tsx, apps/mobile/src/session/ComposerSheet.ios.tsx, apps/mobile/src/session/ComposerSheet.tsx, apps/mobile/src/session/ContextSheet.ios.tsx, apps/mobile/src/session/ContextSheet.tsx, apps/mobile/src/session/IncomingShareBridge.tsx, apps/mobile/src/session/SheetModal.tsx, apps/mobile/src/session/SheetSurface.tsx, apps/mobile/src/theme/ThemeProvider.tsx, apps/mobile/src/theme/navigationTheme.ts, apps/mobile/src/theme/tokens.ts, apps/mobile/src/update/AndroidUpdateSheet.tsx | 0 | 170 | 22 |
| `mobile.settings` | mobile | 设置(含调试与日志上传可见入口) | app/settings.tsx | AndroidUpdateSheet, AppText, AuthContext, BlurBackdrop, CenteredScreen, ComposerNativeSection, ComposerNativeSection.ios, ComposerSheet, ComposerSheet.ios, ConnectionBanner, ConnectionNoticeOverlay, ContextSheet, ContextSheet.ios, DeviceLinkContext, HomeNativeStackHeader, IncomingShareBridge, MobileChoicePickerList, MobileLoginHandoffContext, MobileLoginHandoffStage, MobilePrimitives, NativePullDownMenu, NativeSwitch, PushNotificationsBridge, QuietSyncIndicator, SheetModal, SheetSurface, SimpleStackHeader, StartupSplashOverlay, ThemeProvider, _layout, gestureHandler, peerFileTransport, settings, useLocale | apps/mobile/app/_layout.tsx, apps/mobile/app/settings.tsx, apps/mobile/src/auth/AuthContext.tsx, apps/mobile/src/auth/MobileLoginHandoffContext.tsx, apps/mobile/src/components/AppText.tsx, apps/mobile/src/components/CenteredScreen.tsx, apps/mobile/src/components/ConnectionBanner.tsx, apps/mobile/src/components/ConnectionNoticeOverlay.tsx, apps/mobile/src/components/MobileLoginHandoffStage.tsx, apps/mobile/src/components/MobilePrimitives.tsx, apps/mobile/src/components/QuietSyncIndicator.tsx, apps/mobile/src/components/StartupSplashOverlay.tsx, apps/mobile/src/device-link/DeviceLinkContext.tsx, apps/mobile/src/device-link/peerFileTransport.tsx, apps/mobile/src/i18n/useLocale.tsx, apps/mobile/src/notifications/PushNotificationsBridge.tsx, apps/mobile/src/platform/chrome/HomeNativeStackHeader.tsx, apps/mobile/src/platform/chrome/NativePullDownMenu.tsx, apps/mobile/src/platform/chrome/NativeSwitch.tsx, apps/mobile/src/platform/chrome/SimpleStackHeader.tsx, apps/mobile/src/platform/gestureHandler.tsx, apps/mobile/src/session/BlurBackdrop.tsx, apps/mobile/src/session/ComposerNativeSection.ios.tsx, apps/mobile/src/session/ComposerNativeSection.tsx, apps/mobile/src/session/ComposerSheet.ios.tsx, apps/mobile/src/session/ComposerSheet.tsx, apps/mobile/src/session/ContextSheet.ios.tsx, apps/mobile/src/session/ContextSheet.tsx, apps/mobile/src/session/IncomingShareBridge.tsx, apps/mobile/src/session/MobileChoicePickerList.tsx, apps/mobile/src/session/SheetModal.tsx, apps/mobile/src/session/SheetSurface.tsx, apps/mobile/src/theme/ThemeProvider.tsx, apps/mobile/src/theme/navigationTheme.ts, apps/mobile/src/theme/tokens.ts, apps/mobile/src/update/AndroidUpdateSheet.tsx | 0 | 170 | 24 |
diff --git a/docs/remote-desktop.md b/docs/remote-desktop.md
index 1d47a30216..51eae651ef 100644
--- a/docs/remote-desktop.md
+++ b/docs/remote-desktop.md
@@ -29,8 +29,9 @@ also restores the local cursor inside the picture.
With the picture focused, Cmd+C/V on macOS or Ctrl+C/V on Windows copies selected
remote text to the local clipboard or pastes local text remotely. Transfers use
the existing authorized Main bridge, are ordered and user-triggered, and report
-failure without reconnecting. There is no background clipboard monitoring or
-automatic context-menu synchronization; images, files and cut are not bridged.
+failure without reconnecting. These Desktop keyboard shortcuts remain text-only;
+images, files and cut are not bridged by them. The opt-in Mobile clipboard sync
+described below is a separate, foreground-only operation.
The shared viewer session marks recovery only when a start is attempted, so an
initial capability-query timeout does not turn a retry against a legacy host into
@@ -546,8 +547,8 @@ the existing remote-desktop business channel. Old desktops show an upgrade hint;
no relay/server protocol changes are required. Both operations require the current
peer-bound controlling lease, reject concurrent transfers, and check revocation
again after asynchronous native work. The phone also discards results after its
-lease changes or it leaves the foreground. No clipboard listeners, background
-synchronization, history, logs containing text, or disk persistence are added.
+lease changes or it leaves the foreground. These explicit text operations do not
+retain clipboard history or log its contents. Opt-in synchronization is described below.
TypeScript and native compilation checks cover this implementation. Keyboard/menu
interaction, application-specific selection support, and physical-phone transfer
@@ -567,7 +568,10 @@ fall back to clipboard content, but protected/failed/stale selection reads do no
Clipboard JSON is transferred sequentially in 64 Ki-character chunks, at most
32 Mi-characters total (native iOS additionally bounds UTF-8 bytes). Images are
-limited to 64 million pixels. Each transfer is peer/lease/control-generation
+limited to 4 million pixels on iOS before PNG encoding or incoming PNG decoding,
+with an 8 MiB encoded PNG limit. Oversized images are rejected, not downsampled.
+Android uses the limits described below.
+Each transfer is peer/lease/control-generation
bound, lives only in memory, expires after 60 seconds, and is discarded on
control changes or disconnect. Commit consumes its transfer before pasting and
is never automatically retried. No transport/global frame limits are changed.
@@ -661,7 +665,46 @@ clipboard. Transfers support text, HTML, RTF, URLs and PNG images through bounde
sequential chunks, with a blocking progress overlay. Arbitrary files and private
application clipboard formats are not supported. Transfer buffers are discarded
on completion, cancellation and lease termination; uncertain paste responses are
-never automatically retried. Native clipboard access requires a new iOS build.
+never automatically retried. Native portable clipboard access and version tracking
+require new iOS and Android native builds; older runtimes retain the text fallback.
+Android supports text, HTML, HTTP(S) URLs and PNG images (not RTF), limits decoded
+clipboard image reads and incoming PNGs to 4 million pixels, with 8 MiB limits on both the source
+and encoded PNG. Incoming Base64 length is checked before decoding, and its PNG
+header and dimensions before publication. Oversized images are rejected before unbounded encoding or
+Base64 copies; images are not downsampled. Android clipboard images
+use grant-scoped cache files. Every successful image, text or URL replacement
+reclaims unreferenced published images, retaining at most three previous images
+(24 MiB) for up to one hour as a read grace period. Current clipboard URIs are
+always preserved. Preparation uses unpublished temporary files; failed or cancelled
+preparation/publication removes only its own file. Cleanup is best effort when
+the clipboard cannot be inspected or the filesystem rejects deletion.
+Subsequent successful replacements also reclaim unpublished files left by older
+processes once they are over one hour old. Filenames include the process ID and
+start time, so current-process preparation survives long suspensions and module
+recreation without an active-file registry.
+
+Desktop manual copy, automatic reads and write verification share a 4 million
+pixel check before native PNG encoding. PNG buffers over 8 MiB are rejected before
+Base64/hash copies. Incoming PNGs have encoded-length, byte and dimension checks
+before native decoding. The pixel limit bounds encoding work; Electron still
+allocates the PNG buffer before its byte length can be checked.
+
+### Opt-in Mobile clipboard synchronization
+
+On supported peers, the security options can enable clipboard synchronization
+while the phone is foregrounded and owns the controlling lease. Every 1.5 seconds
+the phone checks local and remote version tokens, reading portable content only
+when a version changes. Android uses change notifications and description timestamps
+without reading the clipboard body during unchanged polls. Image provider reads,
+conversion and file preparation run off the Android main thread; clipboard access
+and the final foreground/version check and write run on the main thread.
+
+Synchronization compares content digests to avoid echoing its own writes and
+checks the destination version again before writing so it cannot overwrite a
+newer local copy. Disabling sync, leaving the foreground, losing control or
+disconnecting invalidates pending work. It does not poll in the background,
+maintain history or log clipboard contents. Private formats and arbitrary files
+remain unsupported; only portable representations are synchronized.
Only one viewer lease is active. A second viewer sees a busy state and can
explicitly take over when the host advertises `connectionTakeover`. Ordinary
diff --git a/packages/device-link/src/__tests__/remoteClipboardInline.test.ts b/packages/device-link/src/__tests__/remoteClipboardInline.test.ts
new file mode 100644
index 0000000000..1982a61b65
--- /dev/null
+++ b/packages/device-link/src/__tests__/remoteClipboardInline.test.ts
@@ -0,0 +1,43 @@
+import { describe, expect, it } from "vitest";
+import {
+ CLIPBOARD_CHUNK_CHARS,
+ parseClipboardContentRequest,
+} from "../remoteClipboard";
+
+describe("inline clipboard request validation", () => {
+ const paste = {
+ action: "paste",
+ sync: true,
+ version: "1",
+ data: '{"text":"hello"}',
+ };
+ it("requires a version-bound sync write with a bounded portable payload", () => {
+ expect(parseClipboardContentRequest(paste, "lease")).toMatchObject(paste);
+ for (const change of [
+ { sync: false },
+ { version: undefined },
+ { data: "bad" },
+ { data: '{"path":"/tmp/file"}' },
+ { data: "x".repeat(CLIPBOARD_CHUNK_CHARS + 1) },
+ ])
+ expect(() =>
+ parseClipboardContentRequest({ ...paste, ...change }, "lease"),
+ ).toThrow();
+ });
+ it("keeps legacy copy requests unchanged and validates inline opt-in", () => {
+ expect(parseClipboardContentRequest({ action: "copy" }, "lease")).toEqual({
+ op: "clipboardContent",
+ lease: "lease",
+ action: "copy",
+ });
+ expect(() =>
+ parseClipboardContentRequest({ action: "copy", inline: true }, "lease"),
+ ).toThrow();
+ expect(
+ parseClipboardContentRequest(
+ { action: "copy", inline: true, sync: true, version: "1" },
+ "lease",
+ ),
+ ).toMatchObject({ inline: true, sync: true, version: "1" });
+ });
+});
diff --git a/packages/device-link/src/remoteClipboard.ts b/packages/device-link/src/remoteClipboard.ts
index d89faefff3..a9703ff41d 100644
--- a/packages/device-link/src/remoteClipboard.ts
+++ b/packages/device-link/src/remoteClipboard.ts
@@ -8,37 +8,143 @@ export interface RemoteClipboardContent {
}
export const CLIPBOARD_CHUNK_CHARS = 64 * 1024;
export const CLIPBOARD_MAX_CHARS = 32 * 1024 * 1024;
-export type ClipboardContentRequest =
+export type ClipboardContentRequest = {
+ sync?: boolean;
+ version?: string;
+ inline?: boolean;
+} & (
| { op: "clipboardContent"; lease: string; action: "copy" }
+ | { op: "clipboardContent"; lease: string; action: "paste"; data: string }
| { op: "clipboardContent"; lease: string; action: "begin"; length: number }
- | { op: "clipboardContent"; lease: string; action: "read"; id: string; offset: number }
- | { op: "clipboardContent"; lease: string; action: "write"; id: string; offset: number; data: string }
- | { op: "clipboardContent"; lease: string; action: "commit" | "cancel"; id: string };
+ | {
+ op: "clipboardContent";
+ lease: string;
+ action: "read";
+ id: string;
+ offset: number;
+ }
+ | {
+ op: "clipboardContent";
+ lease: string;
+ action: "write";
+ id: string;
+ offset: number;
+ data: string;
+ }
+ | {
+ op: "clipboardContent";
+ lease: string;
+ action: "commit" | "cancel";
+ id: string;
+ }
+);
-export function parseClipboardContentRequest(v: Record<string, unknown>, lease: string): ClipboardContentRequest {
+export function parseClipboardContentRequest(
+ v: Record<string, unknown>,
+ lease: string,
+): ClipboardContentRequest {
+ if (v.sync !== undefined && typeof v.sync !== "boolean")
+ throw new Error("INVALID_REQUEST");
+ if (
+ v.version !== undefined &&
+ (typeof v.version !== "string" || v.version.length > 128 || !v.version)
+ )
+ throw new Error("INVALID_REQUEST");
+ const request = parseTransferRequest(v, lease);
+ if (v.inline !== undefined && typeof v.inline !== "boolean")
+ throw new Error("INVALID_REQUEST");
+ if (
+ (v.inline === true || v.action === "paste") &&
+ (v.sync !== true || typeof v.version !== "string" || !v.version)
+ )
+ throw new Error("INVALID_REQUEST");
+ return {
+ ...request,
+ ...(v.sync === true ? { sync: true } : {}),
+ ...(typeof v.version === "string" ? { version: v.version } : {}),
+ ...(v.inline === true ? { inline: true } : {}),
+ };
+}
+function parseTransferRequest(
+ v: Record<string, unknown>,
+ lease: string,
+): ClipboardContentRequest {
const op = "clipboardContent";
if (v.action === "copy") return { op, lease, action: v.action };
- if (v.action === "begin" && Number.isSafeInteger(v.length) && Number(v.length) > 0 && Number(v.length) <= CLIPBOARD_MAX_CHARS)
+ if (v.action === "paste") {
+ if (
+ typeof v.data !== "string" ||
+ !v.data.length ||
+ v.data.length > CLIPBOARD_CHUNK_CHARS
+ )
+ throw new Error("INVALID_REQUEST");
+ parseClipboardContent(v.data);
+ return { op, lease, action: "paste", data: v.data };
+ }
+ if (
+ v.action === "begin" &&
+ Number.isSafeInteger(v.length) &&
+ Number(v.length) > 0 &&
+ Number(v.length) <= CLIPBOARD_MAX_CHARS
+ )
return { op, lease, action: v.action, length: Number(v.length) };
- if (typeof v.id !== "string" || !/^[a-f0-9-]{36}$/.test(v.id)) throw new Error("INVALID_REQUEST");
- if (v.action === "commit" || v.action === "cancel") return { op, lease, action: v.action, id: v.id };
- if (!Number.isSafeInteger(v.offset) || Number(v.offset) < 0 || Number(v.offset) >= CLIPBOARD_MAX_CHARS) throw new Error("INVALID_REQUEST");
- if (v.action === "read") return { op, lease, action: v.action, id: v.id, offset: Number(v.offset) };
- if (v.action === "write" && typeof v.data === "string" && v.data.length > 0 && v.data.length <= CLIPBOARD_CHUNK_CHARS)
- return { op, lease, action: v.action, id: v.id, offset: Number(v.offset), data: v.data };
+ if (typeof v.id !== "string" || !/^[a-f0-9-]{36}$/.test(v.id))
+ throw new Error("INVALID_REQUEST");
+ if (v.action === "commit" || v.action === "cancel")
+ return { op, lease, action: v.action, id: v.id };
+ if (
+ !Number.isSafeInteger(v.offset) ||
+ Number(v.offset) < 0 ||
+ Number(v.offset) >= CLIPBOARD_MAX_CHARS
+ )
+ throw new Error("INVALID_REQUEST");
+ if (v.action === "read")
+ return { op, lease, action: v.action, id: v.id, offset: Number(v.offset) };
+ if (
+ v.action === "write" &&
+ typeof v.data === "string" &&
+ v.data.length > 0 &&
+ v.data.length <= CLIPBOARD_CHUNK_CHARS
+ )
+ return {
+ op,
+ lease,
+ action: v.action,
+ id: v.id,
+ offset: Number(v.offset),
+ data: v.data,
+ };
throw new Error("INVALID_REQUEST");
}
export function parseClipboardContent(json: string): RemoteClipboardContent {
if (json.length > CLIPBOARD_MAX_CHARS) throw new Error("CLIPBOARD_TOO_LONG");
let value: unknown;
- try { value = JSON.parse(json); } catch { throw new Error("CLIPBOARD_UNSUPPORTED"); }
- if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("CLIPBOARD_UNSUPPORTED");
+ try {
+ value = JSON.parse(json);
+ } catch {
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ }
+ if (!value || typeof value !== "object" || Array.isArray(value))
+ throw new Error("CLIPBOARD_UNSUPPORTED");
const v = value as Record<string, unknown>;
- if (!Object.keys(v).length || Object.entries(v).some(([k, x]) =>
- !["text", "html", "rtf", "url", "png"].includes(k) || typeof x !== "string" || !x))
+ if (
+ !Object.keys(v).length ||
+ Object.entries(v).some(
+ ([k, x]) =>
+ !["text", "html", "rtf", "url", "png"].includes(k) ||
+ typeof x !== "string" ||
+ !x,
+ )
+ )
+ throw new Error("CLIPBOARD_UNSUPPORTED");
+ if (v.url && (typeof v.url !== "string" || !/^https?:\/\//i.test(v.url)))
throw new Error("CLIPBOARD_UNSUPPORTED");
- if (v.url && (typeof v.url !== "string" || !/^https?:\/\//i.test(v.url))) throw new Error("CLIPBOARD_UNSUPPORTED");
- if (v.png && (typeof v.png !== "string" || !/^iVBORw0KGgo[A-Za-z0-9+/]*={0,2}$/.test(v.png) || v.png.length % 4 !== 0))
+ if (
+ v.png &&
+ (typeof v.png !== "string" ||
+ !/^iVBORw0KGgo[A-Za-z0-9+/]*={0,2}$/.test(v.png) ||
+ v.png.length % 4 !== 0)
+ )
throw new Error("CLIPBOARD_UNSUPPORTED");
return v as RemoteClipboardContent;
}
diff --git a/packages/device-link/src/remoteDesktop.ts b/packages/device-link/src/remoteDesktop.ts
index 7661d7ea8b..1cf7783c96 100644
--- a/packages/device-link/src/remoteDesktop.ts
+++ b/packages/device-link/src/remoteDesktop.ts
@@ -140,6 +140,11 @@ export interface RemoteDesktopCapabilities {
cursorOverlay?: boolean;
clipboardText?: boolean;
clipboardContent?: boolean;
+ clipboardSync?: boolean;
+ /** Bounded single-message clipboard payloads, with legacy chunk fallback. */
+ clipboardInline?: boolean;
+ privacyScreen?: boolean;
+ hostMute?: boolean;
}
export type DesktopPermission = "screenRecording" | "accessibility";
export type DesktopPermissionStatus =
@@ -164,6 +169,15 @@ export interface RemoteDesktopLease {
controlling: boolean;
}
export type RemoteDesktopRequest =
+ | {
+ op: "privacyScreen";
+ lease: string;
+ enabled: boolean;
+ lockOnExit?: boolean;
+ }
+ | { op: "hostMute"; lease: string; enabled: boolean }
+ | { op: "clipboardSync"; lease: string; enabled: boolean }
+ | { op: "clipboardVersion"; lease: string }
| RemoteDesktopIceRequest
| ClipboardContentRequest
| { op: "capabilities" }
@@ -219,6 +233,26 @@ export function parseRemoteDesktopRequest(
if (typeof v.lease !== "string" || v.lease.length > 128 || !v.lease)
throw new Error("INVALID_LEASE");
const lease = v.lease;
+ if (v.op === "privacyScreen" && typeof v.enabled === "boolean") {
+ if (v.lockOnExit !== undefined && typeof v.lockOnExit !== "boolean")
+ throw new Error("INVALID_REQUEST");
+ return {
+ op: v.op,
+ lease,
+ enabled: v.enabled,
+ ...(typeof v.lockOnExit === "boolean"
+ ? { lockOnExit: v.lockOnExit }
+ : {}),
+ };
+ }
+ if (
+ (v.op === "privacyScreen" ||
+ v.op === "clipboardSync" ||
+ v.op === "hostMute") &&
+ typeof v.enabled === "boolean"
+ )
+ return { op: v.op, lease, enabled: v.enabled };
+ if (v.op === "clipboardVersion") return { op: v.op, lease };
if (v.op === "ice") {
if (!isDesktopAttemptId(v.attemptId) || !isDesktopIceCursor(v.after))
throw new Error("INVALID_REQUEST");