Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

4.5 KB
Raw Blame
//! Device interface shells and native Gamepad polling without a device backend.
//!
//! Polling returns no devices. Do not create fake device snapshots or actuator
//! results; Event payloads are implemented by the shared value-event bindings.

use super::super::context_host_ptr_from_global_bridge;
use crate::{web_api_interfaces, webidl};
use moli_webapi_declare::WebApiFunctionTemplate;

#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::Gamepad, enumerable, receiver)]
struct GamepadPrototypeDeclaration {
    #[webapi(accessor_property, getter = device_backend_unavailable)]
    id: (),
    #[webapi(accessor_property, getter = device_backend_unavailable)]
    index: (),
    #[webapi(accessor_property, getter = device_backend_unavailable)]
    connected: (),
    #[webapi(accessor_property, getter = device_backend_unavailable)]
    timestamp: (),
    #[webapi(accessor_property, getter = device_backend_unavailable)]
    mapping: (),
    #[webapi(accessor_property, getter = device_backend_unavailable)]
    axes: (),
    #[webapi(accessor_property, getter = device_backend_unavailable)]
    buttons: (),
    #[webapi(accessor_property, getter = device_backend_unavailable)]
    touches: (),
    #[webapi(accessor_property, getter = device_backend_unavailable)]
    vibration_actuator: (),
}

#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::GamepadButton, enumerable, receiver)]
struct GamepadButtonPrototypeDeclaration {
    #[webapi(accessor_property, getter = device_backend_unavailable)]
    pressed: (),
    #[webapi(accessor_property, getter = device_backend_unavailable)]
    touched: (),
    #[webapi(accessor_property, getter = device_backend_unavailable)]
    value: (),
}

#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::GamepadHapticActuator, enumerable, receiver)]
struct GamepadHapticActuatorPrototypeDeclaration {
    #[webapi(accessor_property, getter = device_backend_unavailable)]
    effects: (),
    #[webapi(method, returns_promise, length = 1, callback = device_backend_unavailable)]
    play_effect: (),
    #[webapi(method, returns_promise, length = 0, callback = device_backend_unavailable)]
    reset: (),
}

pub(super) fn install<'s>(
    scope: &mut v8::PinScope<'s, '_, ()>,
    template: v8::Local<'s, v8::FunctionTemplate>,
    interface_name: &str,
) {
    let prototype = template.prototype_template(scope);
    match interface_name {
        "Gamepad" => GamepadPrototypeDeclaration::initialize_prototype_template(scope, prototype),
        "GamepadButton" => {
            GamepadButtonPrototypeDeclaration::initialize_prototype_template(scope, prototype)
        }
        "GamepadHapticActuator" => {
            GamepadHapticActuatorPrototypeDeclaration::initialize_prototype_template(
                scope, prototype,
            )
        }
        _ => {}
    }
}

fn device_backend_unavailable<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    _args: v8::FunctionCallbackArguments<'s>,
    _rv: v8::ReturnValue<'s>,
) {
    webidl::throw_dom_exception(
        scope,
        "NotSupportedError",
        "Gamepad device and haptic backends are not implemented.",
    );
}

pub(super) fn navigator_get_gamepads_callback<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    _args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'s, v8::Value>,
) {
    // The Gamepad algorithm uses the current global's document, including
    // when a method from one Window is borrowed by another Navigator.
    let context = scope.get_current_context();
    if let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) {
        // SAFETY: the bridge owns the host for the lifetime of this callback.
        let host = unsafe { &*host_ptr };
        if let Some(identity) =
            host.window_execution_context_identity_for_v8_context(scope, context)
            && host.window_execution_context_identity_is_current(identity)
            && host
                .document_permissions_policy_for_owner(identity.dispatch_scope())
                .is_some_and(|policy| !policy.gamepad_enabled())
        {
            webidl::throw_dom_exception(
                scope,
                "SecurityError",
                "Access to gamepads is disallowed by permissions policy.",
            );
            return;
        }
    }

    // No gamepad backend is connected. An inactive document also returns an
    // empty sequence before checking policy. Each call produces a new Array.
    rv.set(v8::Array::new(scope, 0).into());
}