Skip to content

armgit / moli

Public Imported from GitHub
0

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

10.6 KB
Raw Blame
use super::helpers::{
    window_child_context_handle, window_hidden_value, window_host_ptr, window_receiver,
};
use super::*;

fn window_inner_surface_dimension<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    receiver: v8::Local<'s, v8::Object>,
    width: bool,
) -> f64 {
    let fallback = if width {
        DEFAULT_WINDOW_SURFACE_PROFILE.inner_width
    } else {
        DEFAULT_WINDOW_SURFACE_PROFILE.inner_height
    };
    let Some(host_ptr) = window_host_ptr(scope, receiver) else {
        return fallback;
    };
    let host = unsafe { &*host_ptr };
    if let Some(frame_handle) = window_child_context_handle(scope, receiver) {
        return crate::native_bridge::element::iframe_handle_viewport(host, frame_handle)
            .and_then(|viewport| {
                if width {
                    viewport.width
                } else {
                    viewport.height
                }
            })
            .unwrap_or(fallback);
    }
    host.viewport_surface()
        .map(|viewport| {
            f64::from(if width {
                viewport.inner_width
            } else {
                viewport.inner_height
            })
        })
        .unwrap_or(fallback)
}

pub(in crate::context_bootstrap) fn window_inner_surface_width<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    receiver: v8::Local<'s, v8::Object>,
) -> f64 {
    window_inner_surface_dimension(scope, receiver, true)
}

pub(in crate::context_bootstrap) fn window_inner_surface_height<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    receiver: v8::Local<'s, v8::Object>,
) -> f64 {
    window_inner_surface_dimension(scope, receiver, false)
}

fn set_receiver_slot_or_undefined<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: &v8::FunctionCallbackArguments<'s>,
    slot: &'static str,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(receiver) = window_receiver(scope, args) else {
        return;
    };
    let value = if let Some(value) = window_hidden_value(scope, receiver, slot) {
        Some(value)
    } else {
        match super::super::window_lazy_surface::ensure_window_lazy_surface_value(
            scope, receiver, slot,
        ) {
            Ok(value) => value,
            Err(error) => {
                throw_error(
                    scope,
                    &format!("Failed to materialize Window surface: {error}"),
                );
                return;
            }
        }
    };
    match value {
        Some(value) => rv.set(value),
        None => rv.set_undefined(),
    }
}

fn set_receiver_window_alias<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: &v8::FunctionCallbackArguments<'s>,
    slot: &'static str,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(receiver) = window_receiver(scope, args) else {
        return;
    };
    if let Some(value) = super::values::window_alias_value(scope, receiver, slot) {
        rv.set(value);
    }
}

pub(crate) fn window_opener_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(receiver) = window_receiver(scope, &args) else {
        return;
    };
    if let Some(value) = super::values::window_opener_value(scope, receiver) {
        rv.set(value);
    }
}

pub(in crate::context_bootstrap) fn window_opener_setter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    _rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(receiver) = window_receiver(scope, &args) else {
        return;
    };
    let value = args.get(0);
    if value.is_null()
        && window_child_context_handle(scope, receiver).is_none()
        && let Some(host) = receiver
            .get_creation_context(scope)
            .and_then(context_host_ptr_from_context_slot)
        && let Some(environment) = unsafe { &*host }.page_script_environment()
    {
        environment.set_opener(None);
    }
    let key = crate::util::v8str(scope, "opener");
    let _ = receiver.define_own_property(scope, key.into(), value, v8::PropertyAttribute::NONE);
}

pub(in crate::context_bootstrap) fn window_inner_width_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(receiver) = window_receiver(scope, &args) else {
        return;
    };
    let width = window_inner_surface_width(scope, receiver);
    rv.set(v8::Number::new(scope, width).into());
}

pub(in crate::context_bootstrap) fn window_inner_height_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(receiver) = window_receiver(scope, &args) else {
        return;
    };
    let height = window_inner_surface_height(scope, receiver);
    rv.set(v8::Number::new(scope, height).into());
}

pub(in crate::context_bootstrap) fn window_outer_width_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(receiver) = window_receiver(scope, &args) else {
        return;
    };
    let value = window_host_ptr(scope, receiver)
        .and_then(|host_ptr| unsafe { &*host_ptr }.viewport_surface())
        .map_or(DEFAULT_WINDOW_SURFACE_PROFILE.inner_width, |v| {
            f64::from(v.outer_width)
        });
    rv.set(v8::Number::new(scope, value).into());
}

pub(in crate::context_bootstrap) fn window_outer_height_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(receiver) = window_receiver(scope, &args) else {
        return;
    };
    let value = window_host_ptr(scope, receiver)
        .and_then(|host_ptr| unsafe { &*host_ptr }.viewport_surface())
        .map_or(DEFAULT_WINDOW_SURFACE_PROFILE.inner_height, |v| {
            f64::from(v.outer_height)
        });
    rv.set(v8::Number::new(scope, value).into());
}

pub(in crate::context_bootstrap) fn window_device_pixel_ratio_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(receiver) = window_receiver(scope, &args) else {
        return;
    };
    let value = window_host_ptr(scope, receiver)
        .and_then(|host_ptr| unsafe { &*host_ptr }.viewport_surface())
        .map_or(DEFAULT_WINDOW_SURFACE_PROFILE.device_pixel_ratio, |v| {
            v.device_pixel_ratio
        });
    rv.set(v8::Number::new(scope, value).into());
}

pub(in crate::context_bootstrap) fn window_scroll_x_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(receiver) = window_receiver(scope, &args) else {
        return;
    };
    let value = window_hidden_value(scope, receiver, WINDOW_SCROLL_X_SLOT)
        .and_then(|value| value.number_value(scope))
        .unwrap_or(0.0);
    rv.set(v8::Number::new(scope, value).into());
}

pub(in crate::context_bootstrap) fn window_scroll_y_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(receiver) = window_receiver(scope, &args) else {
        return;
    };
    let value = window_hidden_value(scope, receiver, WINDOW_SCROLL_Y_SLOT)
        .and_then(|value| value.number_value(scope))
        .unwrap_or(0.0);
    rv.set(v8::Number::new(scope, value).into());
}

pub(in crate::context_bootstrap) fn window_navigator_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    rv: v8::ReturnValue<'_, v8::Value>,
) {
    set_receiver_slot_or_undefined(scope, &args, WINDOW_NAVIGATOR_SLOT, rv);
}

pub(in crate::context_bootstrap) fn window_screen_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    rv: v8::ReturnValue<'_, v8::Value>,
) {
    set_receiver_slot_or_undefined(scope, &args, WINDOW_SCREEN_SLOT, rv);
}

pub(in crate::context_bootstrap) fn window_speech_synthesis_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    rv: v8::ReturnValue<'_, v8::Value>,
) {
    set_receiver_slot_or_undefined(scope, &args, WINDOW_SPEECH_SYNTHESIS_SLOT, rv);
}

pub(in crate::context_bootstrap) fn window_custom_elements_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    rv: v8::ReturnValue<'_, v8::Value>,
) {
    set_receiver_slot_or_undefined(scope, &args, WINDOW_CUSTOM_ELEMENTS_SLOT, rv);
}

pub(in crate::context_bootstrap) fn window_performance_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    rv: v8::ReturnValue<'_, v8::Value>,
) {
    set_receiver_slot_or_undefined(scope, &args, WINDOW_PERFORMANCE_SLOT, rv);
}

pub(in crate::context_bootstrap) fn window_visual_viewport_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    rv: v8::ReturnValue<'_, v8::Value>,
) {
    set_receiver_slot_or_undefined(scope, &args, WINDOW_VISUAL_VIEWPORT_SLOT, rv);
}

pub(in crate::context_bootstrap) fn window_window_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    rv: v8::ReturnValue<'_, v8::Value>,
) {
    set_receiver_window_alias(scope, &args, WINDOW_SELF_SLOT, rv);
}

pub(crate) fn window_top_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    rv: v8::ReturnValue<'_, v8::Value>,
) {
    set_receiver_window_alias(scope, &args, WINDOW_TOP_SLOT, rv);
}

pub(crate) fn window_parent_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    rv: v8::ReturnValue<'_, v8::Value>,
) {
    set_receiver_window_alias(scope, &args, WINDOW_PARENT_SLOT, rv);
}

pub(in crate::context_bootstrap) fn window_frames_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    rv: v8::ReturnValue<'_, v8::Value>,
) {
    set_receiver_window_alias(scope, &args, WINDOW_FRAMES_SLOT, rv);
}

pub(in crate::context_bootstrap) fn window_self_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    rv: v8::ReturnValue<'_, v8::Value>,
) {
    set_receiver_window_alias(scope, &args, WINDOW_SELF_SLOT, rv);
}

pub(crate) fn window_closed_getter<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'_, v8::Value>,
) {
    let Some(receiver) = window_receiver(scope, &args) else {
        return;
    };
    if let Some(value) = super::values::window_closed_value(scope, receiver) {
        rv.set(value);
    }
}