Skip to main content

agent_hub_runtime/core/
system_permissions.rs

1//! Read-only OS permission diagnostics and allowlisted settings links.
2use serde::{Deserialize, Serialize};
3#[derive(Clone, Copy, Deserialize)]
4#[serde(rename_all = "snake_case")]
5pub enum Permission {
6    Accessibility,
7    ScreenCapture,
8    FilesFolders,
9    FullDisk,
10}
11#[derive(Serialize)]
12pub struct Status {
13    platform: &'static str,
14    accessibility: Option<bool>,
15    screen_capture: Option<bool>,
16}
17pub fn status() -> Status {
18    #[cfg(target_os = "macos")]
19    {
20        #[link(name = "ApplicationServices", kind = "framework")]
21        extern "C" {
22            fn AXIsProcessTrusted() -> u8;
23        }
24        #[link(name = "CoreGraphics", kind = "framework")]
25        extern "C" {
26            fn CGPreflightScreenCaptureAccess() -> bool;
27        }
28        // Preflight only: these APIs never present a grant prompt or change TCC.
29        Status {
30            platform: "macos",
31            accessibility: Some(unsafe { AXIsProcessTrusted() != 0 }),
32            screen_capture: Some(unsafe { CGPreflightScreenCaptureAccess() }),
33        }
34    }
35    #[cfg(not(target_os = "macos"))]
36    Status {
37        platform: std::env::consts::OS,
38        accessibility: None,
39        screen_capture: None,
40    }
41}
42pub fn macos_settings_url(permission: Permission, major: u32) -> String {
43    let pane = if major >= 13 {
44        "com.apple.settings.PrivacySecurity.extension"
45    } else {
46        "com.apple.preference.security"
47    };
48    let section = match permission {
49        Permission::Accessibility => "Privacy_Accessibility",
50        Permission::ScreenCapture => "Privacy_ScreenCapture",
51        Permission::FilesFolders => "Privacy_FilesAndFolders",
52        Permission::FullDisk => "Privacy_AllFiles",
53    };
54    format!("x-apple.systempreferences:{pane}?{section}")
55}
56pub fn open_settings(permission: Permission) -> Result<(), String> {
57    #[cfg(target_os = "macos")]
58    {
59        use std::process::{Command, Stdio};
60        let version =
61            crate::core::cli_process::read_command("/usr/bin/sw_vers", &["-productVersion"])?;
62        let major = version
63            .trim()
64            .split('.')
65            .next()
66            .and_then(|value| value.parse().ok())
67            .ok_or("Unable to identify macOS version")?;
68        let mut child = Command::new("/usr/bin/open")
69            .arg(macos_settings_url(permission, major))
70            .stdin(Stdio::null())
71            .stdout(Stdio::null())
72            .stderr(Stdio::null())
73            .spawn()
74            .map_err(|e| e.to_string())?;
75        std::thread::spawn(move || {
76            let _ = child.wait();
77        });
78        Ok(())
79    }
80    #[cfg(not(target_os = "macos"))]
81    {
82        let _ = permission;
83        Err("PERMISSION_SETTINGS_UNAVAILABLE".into())
84    }
85}
86#[cfg(test)]
87mod tests;