Skip to content

Visual Studio Code, rebuilt in Rust

Rust
3.0 KB
Raw Blame
//! nativePolicyService.ts over @vscode/policy-watcher's macOS side: each policy is read with
//! `CFPreferencesCopyAppValue(name, darwinBundleIdentifier)`, so a managed profile (or `defaults write`) sets it.
//! On Linux, main.ts uses filePolicyService.ts on `LINUX_SYSTEM_POLICY_FILE_PATH` instead.

use config::policy::{PolicyValues, policy_configurations};
use config::registry::Registry;
use serde_json::Value;

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "linux")]
pub use linux::{WATCHED_PATH, watch_policy_changes};
#[cfg(target_os = "macos")]
pub use macos::{WATCHED_PATH, watch_policy_changes};
#[cfg(target_os = "windows")]
mod windows;
#[cfg(target_os = "windows")]
pub use windows::{WATCHED_PATH, watch_policy_changes};

/// product.json `darwinBundleIdentifier`.
pub const BUNDLE_IDENTIFIER: &str = "com.rokartur.vsrustium";

/// `createWatcher(productName, policyDefinitions)`'s first update: the value of every policy the registry
/// defines, read as the type `PolicyConfiguration.toPolicyDefinitionType` gives its setting.
/// filePolicyService.ts on Linux reads any JSON value under a defined policy name: no bundle id, no type check.
#[cfg_attr(any(target_os = "linux", target_os = "windows"), allow(unused_variables))]
pub fn read_native_policies(registry: &Registry, application: &str) -> PolicyValues {
    #[cfg(target_os = "linux")]
    let file = linux::read_policy_file(std::path::Path::new(WATCHED_PATH));
    let mut values = PolicyValues::new();
    for (name, settings) in policy_configurations(registry) {
        // The owner's schema decides the type; a reference only stands in when no owner is registered.
        let schema = settings.iter().find(|(_, schema)| schema.get("policy").is_some()).unwrap_or(&settings[0]).1;
        let Some(kind) = definition_type(&schema["type"]) else { continue };
        #[cfg(target_os = "linux")]
        let value = file.get(&name).cloned();
        #[cfg(target_os = "macos")]
        let value = macos::read_preference(application, &name, kind);
        #[cfg(target_os = "windows")]
        let value: Option<Value> = None;
        if let Some(value) = value {
            values.insert(name, value);
        }
    }
    values
}

#[derive(Clone, Copy, PartialEq, Debug)]
enum Kind {
    String,
    Number,
    Boolean,
}

/// `toPolicyDefinitionType`: number over boolean over string; arrays and objects travel as JSON strings.
fn definition_type(schema_type: &Value) -> Option<Kind> {
    let types: Vec<&Value> = match schema_type {
        Value::Array(types) => types.iter().collect(),
        kind => vec![kind],
    };
    let supported: Vec<&str> = types.iter().filter_map(|kind| kind.as_str()).filter(|kind| ["string", "number", "array", "object", "boolean"].contains(kind)).collect();
    if supported.is_empty() {
        return None;
    }
    Some(if supported.contains(&"number") {
        Kind::Number
    } else if supported.contains(&"boolean") {
        Kind::Boolean
    } else {
        Kind::String
    })
}

#[cfg(all(test, target_os = "macos"))]
mod tests;