Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): keep ScreenCaptureKit queries out of the camera and microphone pollers and let a late answer confirm the grant

Richie McIlroyd78f67d2ea34
diff --git a/apps/desktop-gpui/src/devices.rs b/apps/desktop-gpui/src/devices.rs
index ad8c703f2..6f6995139 100644
--- a/apps/desktop-gpui/src/devices.rs
+++ b/apps/desktop-gpui/src/devices.rs
@@ -567,7 +567,7 @@ fn list_microphone_names() -> Vec<MicrophoneOption> {
fn list_microphones() -> Vec<MicrophoneOption> {
// CPAL's configuration lookup opens an input AudioUnit and can prompt for consent.
#[cfg(target_os = "macos")]
- if !crate::permissions::check_raw().is_some_and(|permissions| {
+ if !crate::permissions::check_raw_preflight().is_some_and(|permissions| {
permissions.microphone == crate::permissions::MediaAuthorization::Authorized
}) {
return list_microphone_names();
diff --git a/apps/desktop-gpui/src/media_access.rs b/apps/desktop-gpui/src/media_access.rs
index ecc8df56f..f8c3229be 100644
--- a/apps/desktop-gpui/src/media_access.rs
+++ b/apps/desktop-gpui/src/media_access.rs
@@ -82,7 +82,7 @@ impl MediaAccess {
self.refresh = Some(cx.spawn(async move |this, cx| {
let raw = cx
.background_executor()
- .spawn(async { crate::permissions_ui::sweep_raw() })
+ .spawn(async { crate::permissions_ui::sweep_raw_preflight() })
.await;
this.update(cx, |this, cx| {
this.refresh = None;
diff --git a/apps/desktop-gpui/src/permissions.rs b/apps/desktop-gpui/src/permissions.rs
index 386f9e05e..b2e7c1624 100644
--- a/apps/desktop-gpui/src/permissions.rs
+++ b/apps/desktop-gpui/src/permissions.rs
@@ -211,6 +211,20 @@ pub fn check_raw() -> Option<RawPermissions> {
}
}
+/// [`check_raw`] without the ScreenCaptureKit confirmation of a screen
+/// recording grant, for pollers that only need camera / microphone status or
+/// watch for a grant to flip: it never queries `SCShareableContent`.
+pub fn check_raw_preflight() -> Option<RawPermissions> {
+ #[cfg(target_os = "macos")]
+ {
+ Some(macos::check_raw_preflight())
+ }
+ #[cfg(not(target_os = "macos"))]
+ {
+ None
+ }
+}
+
/// The startup / tray gate: screen recording + accessibility.
pub fn necessary_granted() -> bool {
match check_raw() {
@@ -279,6 +293,15 @@ pub async fn ensure_capture_media_permission(permission: OSPermission) -> Result
macos::ensure_capture_media_permission(permission).await
}
+/// Whether a camera or microphone request is still waiting on the user's
+/// answer to the macOS prompt.
+pub fn media_request_pending() -> bool {
+ #[cfg(target_os = "macos")]
+ return macos::media_request_pending();
+ #[cfg(not(target_os = "macos"))]
+ false
+}
+
pub fn open_permission_settings(permission: OSPermission) {
#[cfg(target_os = "macos")]
{
@@ -394,9 +417,16 @@ mod macos {
/// and AVFoundation's status query autoreleases temporaries (the same
/// leak class `do_permissions_check` in the Tauri app plugs the same way).
pub fn check_raw() -> RawPermissions {
- let screen_preflight = unsafe { CGPreflightScreenCaptureAccess() };
+ let raw = check_raw_preflight();
+ RawPermissions {
+ screen_granted: super::screen_capture::available(raw.screen_granted),
+ ..raw
+ }
+ }
+
+ pub fn check_raw_preflight() -> RawPermissions {
objc2::rc::autoreleasepool(|_| RawPermissions {
- screen_granted: super::screen_capture::available(screen_preflight),
+ screen_granted: unsafe { CGPreflightScreenCaptureAccess() },
accessibility_granted: unsafe { AXIsProcessTrusted() },
microphone: media_authorization(false),
camera: media_authorization(true),
@@ -457,6 +487,18 @@ mod macos {
static MICROPHONE_REQUEST: std::sync::Mutex<Option<super::PendingMediaRequest>> =
std::sync::Mutex::new(None);
+ pub fn media_request_pending() -> bool {
+ use futures_util::FutureExt as _;
+ [&CAMERA_REQUEST, &MICROPHONE_REQUEST]
+ .into_iter()
+ .any(|slot| {
+ slot.lock().is_ok_and(|slot| {
+ slot.as_ref()
+ .is_some_and(|pending| pending.clone().now_or_never().is_none())
+ })
+ })
+ }
+
fn shared_media_request(camera: bool) -> Result<super::PendingMediaRequest, String> {
use futures_util::FutureExt as _;
let slot = if camera {
diff --git a/apps/desktop-gpui/src/permissions/screen_capture.rs b/apps/desktop-gpui/src/permissions/screen_capture.rs
index 940958ffc..382a08dd1 100644
--- a/apps/desktop-gpui/src/permissions/screen_capture.rs
+++ b/apps/desktop-gpui/src/permissions/screen_capture.rs
@@ -34,7 +34,8 @@ const FAILED: u8 = 2;
static VALIDATED: AtomicBool = AtomicBool::new(false);
static LAST_OUTCOME: AtomicU8 = AtomicU8::new(UNKNOWN);
static IN_FLIGHT: AtomicBool = AtomicBool::new(false);
-static QUERY_ALIVE: AtomicBool = AtomicBool::new(false);
+static QUERIES_ALIVE: AtomicU8 = AtomicU8::new(0);
+const MAX_QUERIES_ALIVE: u8 = 3;
static LAST_ATTEMPT: Mutex<Option<Instant>> = Mutex::new(None);
pub(super) fn available(preflight_granted: bool) -> bool {
@@ -125,13 +126,17 @@ fn validate() -> bool {
/// Display count, or `None` on an error or timeout. A wedged `replayd` can
/// leave the query unresolved forever, so it runs on a thread of its own that
-/// is abandoned at the timeout.
+/// is abandoned at the timeout. An abandoned query that answers later still
+/// records the grant, and at most [`MAX_QUERIES_ALIVE`] are left waiting, so a
+/// replayd that never recovers strands a bounded number of threads.
fn query_displays(timeout: Duration) -> Option<usize> {
- // A query abandoned at its timeout is still blocked in replayd; starting
- // another would strand one more thread every retry for as long as
- // replayd stays wedged.
- if QUERY_ALIVE.swap(true, Ordering::AcqRel) {
- tracing::debug!("a ScreenCaptureKit shareable content query is still pending");
+ if QUERIES_ALIVE
+ .fetch_update(Ordering::AcqRel, Ordering::Acquire, |alive| {
+ (alive < MAX_QUERIES_ALIVE).then_some(alive + 1)
+ })
+ .is_err()
+ {
+ tracing::debug!("ScreenCaptureKit shareable content queries are still pending");
return None;
}
let (sender, receiver) = mpsc::sync_channel(1);
@@ -141,7 +146,7 @@ fn query_displays(timeout: Duration) -> Option<usize> {
struct AliveReset;
impl Drop for AliveReset {
fn drop(&mut self) {
- QUERY_ALIVE.store(false, Ordering::Release);
+ QUERIES_ALIVE.fetch_sub(1, Ordering::AcqRel);
}
}
let _alive = AliveReset;
@@ -160,10 +165,14 @@ fn query_displays(timeout: Duration) -> Option<usize> {
}
})
});
+ if displays.is_some_and(|displays| displays > 0) {
+ VALIDATED.store(true, Ordering::Release);
+ LAST_OUTCOME.store(OK, Ordering::Release);
+ }
let _ = sender.send(displays);
});
if spawned.is_err() {
- QUERY_ALIVE.store(false, Ordering::Release);
+ QUERIES_ALIVE.fetch_sub(1, Ordering::AcqRel);
return None;
}
match receiver.recv_timeout(timeout) {
diff --git a/apps/desktop-gpui/src/permissions_ui.rs b/apps/desktop-gpui/src/permissions_ui.rs
index 4703c866f..9033394f4 100644
--- a/apps/desktop-gpui/src/permissions_ui.rs
+++ b/apps/desktop-gpui/src/permissions_ui.rs
@@ -180,6 +180,14 @@ pub fn sweep_raw() -> Option<RawPermissions> {
}
}
+/// [`sweep_raw`] built on [`permissions::check_raw_preflight`].
+pub fn sweep_raw_preflight() -> Option<RawPermissions> {
+ match fake_spec() {
+ Some(spec) => parse_fake(&spec),
+ None => permissions::check_raw_preflight(),
+ }
+}
+
fn fake_spec() -> Option<String> {
std::env::var("CAP_GPUI_FAKE_PERMISSIONS")
.ok()