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 e6a7c25831..59ba87214b 100644 --- a/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts +++ b/apps/desktop/src/main/remote-desktop/__tests__/controller.test.ts @@ -1624,9 +1624,7 @@ describe('remote desktop authority and lifecycle', () => { await h.controller.request('phone', { op: 'hostMute', lease, enabled: true }); await h.controller.request('phone', { op: 'clipboardSync', lease, enabled: true }); if (failed) - vi.mocked(h.deps.startInput).mockRejectedValueOnce( - new Error('DESKTOP_INPUT_UNAVAILABLE'), - ); + vi.mocked(h.deps.startInput).mockRejectedValueOnce(new Error('DESKTOP_INPUT_UNAVAILABLE')); await h.controller.releaseControl(true); expect(h.deps.stopPrivacyScreen).toHaveBeenCalledTimes(failed ? 1 : 0); expect(h.deps.stopHostMute).toHaveBeenCalledTimes(failed ? 1 : 0); @@ -1635,7 +1633,7 @@ describe('remote desktop authority and lifecycle', () => { }, ); }); -it.each(['control', 'presentation', 'failure', 'stop'] as const)( +it.each(['control', 'failure', 'stop'] as const)( 'releases input before failing safety restoration on %s, without stopping replacement input', async (kind) => { const h = harness(), @@ -1652,8 +1650,7 @@ it.each(['control', 'presentation', 'failure', 'stop'] as const)( let result: Promise | 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' }); + else result = h.controller.request('phone', { op: kind, lease, enabled: false }); // Critical cleanup cannot wait for slow/failing operating-system restoration. expect(h.deps.stopInput).toHaveBeenCalledOnce(); expect(h.deps.stopPrivacyScreen).toHaveBeenCalledOnce(); @@ -1668,7 +1665,7 @@ it.each(['control', 'presentation', 'failure', 'stop'] as const)( expect(h.controller.state?.controlling).toBe(true); }, ); -it.each(['control', 'presentation', 'failure'] as const)( +it.each(['control', 'failure'] as const)( 'revokes safety effects on %s control loss', async (kind) => { const h = harness(), @@ -1687,12 +1684,71 @@ it.each(['control', 'presentation', 'failure'] as const)( 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' }); + else await h.controller.request('phone', { op: kind, lease, enabled: false }); expect(h.deps.stopPrivacyScreen).toHaveBeenCalledOnce(); expect(h.deps.stopHostMute).toHaveBeenCalledOnce(); expect(h.deps.stopVideo).not.toHaveBeenCalled(); }, ); +it('keeps privacy and host mute through phone picture-in-picture, stopping only clipboard sync', async () => { + const h = harness(), + { lease } = await h.start(); + let privacyCurrent!: () => boolean; + h.deps.privacyScreen = vi.fn(async (_enabled, current) => { + privacyCurrent = current; + }); + h.deps.hostMute = vi.fn(async () => {}); + h.deps.stopPrivacyScreen = vi.fn(); + h.deps.stopHostMute = vi.fn(async () => {}); + h.deps.clipboardVersion = vi.fn(async () => '1'); + h.deps.stopClipboardVersion = vi.fn(); + 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 }); + await h.controller.request('phone', { op: 'clipboardSync', lease, enabled: true }); + await h.controller.request('phone', { op: 'presentation', lease, enabled: true }); + expect(h.controller.state?.controlling).toBe(false); + expect(h.controller.viewOnlyBackground).toBe(true); + // Long-lived mask owners (Linux) poll this while the phone shows PiP. + expect(privacyCurrent()).toBe(true); + expect(h.deps.stopPrivacyScreen).not.toHaveBeenCalled(); + expect(h.deps.stopHostMute).not.toHaveBeenCalled(); + expect(h.deps.stopClipboardVersion).toHaveBeenCalledOnce(); + await expect(h.controller.request('phone', { op: 'clipboardVersion', lease })).rejects.toThrow( + 'DESKTOP_CLIPBOARD_UNAVAILABLE', + ); + // Returning from PiP re-sends the unchanged preference and joins the live mask. + await h.controller.request('phone', { op: 'presentation', lease, enabled: false }); + await h.controller.request('phone', { op: 'control', lease, enabled: true }); + await expect( + h.controller.request('phone', { op: 'privacyScreen', lease, enabled: true }), + ).resolves.toEqual({ enabled: true }); + expect(h.deps.privacyScreen).toHaveBeenCalledOnce(); + await h.controller.request('phone', { op: 'stop', lease }); + expect(h.deps.stopPrivacyScreen).toHaveBeenCalledOnce(); + expect(privacyCurrent()).toBe(false); +}); +it('clears masks kept for picture-in-picture when control cannot be reacquired', async () => { + 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(async () => {}); + 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 }); + await h.controller.request('phone', { op: 'presentation', lease, enabled: true }); + vi.mocked(h.deps.startInput).mockRejectedValueOnce(new Error('DESKTOP_INPUT_UNAVAILABLE')); + await expect( + h.controller.request('phone', { op: 'presentation', lease, enabled: false }), + ).resolves.toEqual({ controlling: false }); + await expect( + h.controller.request('phone', { op: 'control', lease, enabled: true }), + ).rejects.toThrow('DESKTOP_INPUT_UNAVAILABLE'); + expect(h.deps.stopPrivacyScreen).toHaveBeenCalledOnce(); + expect(h.deps.stopHostMute).toHaveBeenCalledOnce(); +}); it('joins duplicate privacy initialization and invalidates it on control loss', async () => { const h = harness(), { lease } = await h.start(); diff --git a/apps/desktop/src/main/remote-desktop/controller.ts b/apps/desktop/src/main/remote-desktop/controller.ts index 3962a3cee0..d37f3fba1d 100644 --- a/apps/desktop/src/main/remote-desktop/controller.ts +++ b/apps/desktop/src/main/remote-desktop/controller.ts @@ -222,12 +222,15 @@ export class RemoteDesktopController { private privacyGeneration = 0; private privacyOperation: { enabled: boolean; promise: Promise<{ enabled: boolean }> } | null = null; - private async clearSafety(): Promise { - this.privacyGeneration++; - this.privacyOperation = null; + private stopClipboardSync(): void { this.syncGeneration++; this.deps.stopClipboardVersion?.(); if (this.active) this.active.clipboardSync = false; + } + private async clearSafety(): Promise { + this.privacyGeneration++; + this.privacyOperation = null; + this.stopClipboardSync(); try { this.deps.stopPrivacyScreen?.(); } finally { @@ -295,6 +298,10 @@ export class RemoteDesktopController { if (active?.handoff?.requestId === requestId) void this.stop(active.peer, false, 'handoff-ended'); } + /** Phone picture-in-picture views the lease without input: nothing to pause or restore. */ + get viewOnlyBackground(): boolean { + return this.active?.backgroundViewing === true && !this.active.controlling; + } isBackgroundViewing(lease: string): boolean { return this.active?.lease === lease && this.active.backgroundViewing === true; } @@ -666,6 +673,14 @@ export class RemoteDesktopController { } this.require(peer, active.lease); active.controlling = true; + } catch (error) { + // Leaving picture-in-picture kept the masks for a viewer about to resume + // control. If it cannot, a view-only lease must not stay masked and muted. + if (!recovering && this.active === active && generation === this.controlGeneration) + await this.clearSafety().catch((restoreError) => + log.warn('Safety restoration failed after control reacquisition', restoreError), + ); + throw error; } finally { this.inputStarting = false; } @@ -956,11 +971,11 @@ export class RemoteDesktopController { if (this.privacyOperation?.enabled === request.enabled) return this.privacyOperation.promise; const generation = ++this.privacyGeneration; + // Masks outlive control while PiP views the lease; every revocation + // that must expose the desktop clears safety and bumps the generation. const current = () => { this.tick(); - return ( - this.active === active && active.controlling && generation === this.privacyGeneration - ); + return this.active === active && generation === this.privacyGeneration; }; const promise = this.deps .privacyScreen(request.enabled, current) @@ -1119,7 +1134,10 @@ export class RemoteDesktopController { case 'presentation': { this.setBackgroundViewing(active, request.enabled); if (request.enabled) { - await this.revokeControl(); + // PiP keeps watching the same lease: the masked, muted desktop stays + // protected; only input and clipboard sync need control. + await this.revokeControl(true); + this.stopClipboardSync(); } else { this.deps.changed(); } diff --git a/apps/desktop/src/main/remote-desktop/index.ts b/apps/desktop/src/main/remote-desktop/index.ts index 352327644f..b40b8ed737 100644 --- a/apps/desktop/src/main/remote-desktop/index.ts +++ b/apps/desktop/src/main/remote-desktop/index.ts @@ -176,6 +176,11 @@ let privacyExcluded: number[] = []; let overlayExcluded: number[] = []; const applyExcludedWindows = () => nativeCapture.setExcludedWindows([...privacyExcluded, ...overlayExcluded]); +// While the phone shows picture-in-picture the input helper is stopped on purpose; +// the local exit confirmation then has nothing to pause, and cancelling it must not +// bring remote control back. +const pauseInputForPrivacy = (): Promise<() => Promise> => + remoteDesktop.viewOnlyBackground ? Promise.resolve(async () => {}) : input.pauseForPrivacy(); const privacyScreen = new PrivacyScreen( (ids) => { privacyExcluded = ids; @@ -186,11 +191,11 @@ const privacyScreen = new PrivacyScreen( .stopPrivacyByUser() .catch((error) => console.error('[remote-desktop] privacy exit lock failed', error)); }, - () => input.pauseForPrivacy(), + pauseInputForPrivacy, ); const linuxPrivacy = new LinuxPrivacyScreen( () => remoteDesktop.stopPrivacyByUser(), - () => input.pauseForPrivacy(), + pauseInputForPrivacy, ); const supportsPrivacyScreen = (process.platform === 'darwin' &&