Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
use super::helpers::{window_current_dispatch_scope, window_host_ptr};
use crate::document_runtime::DomHandle;
use crate::native_bridge::named_access::{
build_window_named_items_collection, window_named_item_handles,
};
use crate::native_bridge::{JsContextHost, OwnerDispatchScope};
use crate::util::serialize_v8_iter_array;
use moli_webapi_declare::DataPropertyDescriptorDeclaration;
fn is_reserved_window_name(name: &str) -> bool {
matches!(name, "window" | "self" | "top" | "parent" | "frames")
}
fn window_indexed_child_handle<'s>(
scope: &mut v8::PinScope<'s, '_>,
holder: v8::Local<'s, v8::Object>,
index: u32,
) -> Option<(*mut JsContextHost, DomHandle, OwnerDispatchScope)> {
let host_ptr = window_host_ptr(scope, holder)?;
let host = unsafe { &mut *host_ptr };
let owner = window_current_dispatch_scope(scope, holder, host)?;
let handle = if let Some(parent) = owner.child_window() {
let document = host.child_browsing_context_document_handle(parent)?;
host.sync_child_browsing_context_subtree(scope, document);
host.child_browsing_context_child_frame_handle_by_index(parent, index as usize)
} else {
host.sync_child_browsing_context_subtree(scope, host.document_handle());
host.child_browsing_context_handle_by_index(index as usize)
}?;
Some((host_ptr, handle, owner))
}
pub(in crate::context_bootstrap) fn window_indexed_property_getter<'s>(
scope: &mut v8::PinScope<'s, '_>,
index: u32,
args: v8::PropertyCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) -> v8::Intercepted {
let holder = args.holder();
let Some((host_ptr, handle, owner)) = window_indexed_child_handle(scope, holder, index) else {
return v8::Intercepted::kNo;
};
let host = unsafe { &mut *host_ptr };
let window = if owner.child_window().is_none() {
host.child_browsing_context_window_proxy_for_accessing_context(scope, handle)
} else {
host.child_browsing_context_window_wrapper(scope, handle)
};
let Some(window) = window else {
return v8::Intercepted::kNo;
};
rv.set(window.into());
v8::Intercepted::kYes
}
pub(in crate::context_bootstrap) fn window_indexed_property_query<'s>(
scope: &mut v8::PinScope<'s, '_>,
index: u32,
args: v8::PropertyCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Integer>,
) -> v8::Intercepted {
if window_indexed_child_handle(scope, args.holder(), index).is_none() {
return v8::Intercepted::kNo;
}
rv.set_int32(v8::PropertyAttribute::READ_ONLY.as_u32() as i32);
v8::Intercepted::kYes
}
pub(in crate::context_bootstrap) fn window_indexed_property_enumerator<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::PropertyCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Array>,
) {
let holder = args.holder();
let Some(host_ptr) = window_host_ptr(scope, holder) else {
rv.set(v8::Array::new(scope, 0));
return;
};
let host = unsafe { &mut *host_ptr };
let Some(owner) = window_current_dispatch_scope(scope, holder, host) else {
rv.set(v8::Array::new(scope, 0));
return;
};
let count = if let Some(parent) = owner.child_window() {
if let Some(document) = host.child_browsing_context_document_handle(parent) {
host.sync_child_browsing_context_subtree(scope, document);
}
host.child_browsing_context_child_frame_handles(parent)
.len()
} else {
host.sync_child_browsing_context_subtree(scope, host.document_handle());
host.child_browsing_context_count()
};
let array = serialize_v8_iter_array(scope, (0..count).map(|index| index as u32))
.unwrap_or_else(|| v8::Array::new(scope, 0));
rv.set(array);
}
pub(in crate::context_bootstrap) fn window_indexed_property_descriptor<'s>(
scope: &mut v8::PinScope<'s, '_>,
index: u32,
args: v8::PropertyCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) -> v8::Intercepted {
let Some((host_ptr, handle, owner)) = window_indexed_child_handle(scope, args.holder(), index)
else {
return v8::Intercepted::kNo;
};
let host = unsafe { &mut *host_ptr };
let window = if owner.child_window().is_none() {
host.child_browsing_context_window_proxy_for_accessing_context(scope, handle)
} else {
host.child_browsing_context_window_wrapper(scope, handle)
};
let Some(window) = window else {
return v8::Intercepted::kNo;
};
let Ok(descriptor) =
DataPropertyDescriptorDeclaration::new(window.into(), false, true).bind(scope)
else {
return v8::Intercepted::kNo;
};
rv.set(descriptor.into());
v8::Intercepted::kYes
}
pub(in crate::context_bootstrap) fn window_named_property_getter<'s>(
scope: &mut v8::PinScope<'s, '_>,
key: v8::Local<'s, v8::Name>,
args: v8::PropertyCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) -> v8::Intercepted {
let Some(value) = window_named_access_value(scope, key, args.holder()) else {
return v8::Intercepted::kNo;
};
rv.set(value);
v8::Intercepted::kYes
}
pub(in crate::context_bootstrap) fn window_named_property_query<'s>(
scope: &mut v8::PinScope<'s, '_>,
key: v8::Local<'s, v8::Name>,
args: v8::PropertyCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Integer>,
) -> v8::Intercepted {
if window_named_access_value(scope, key, args.holder()).is_none() {
return v8::Intercepted::kNo;
}
rv.set_int32(v8::PropertyAttribute::DONT_ENUM.as_u32() as i32);
v8::Intercepted::kYes
}
fn window_named_property_is_shadowed_by_prototype(
scope: &mut v8::PinScope<'_, '_>,
holder: v8::Local<'_, v8::Object>,
key: v8::Local<'_, v8::String>,
) -> bool {
// Web IDL's named-property visibility algorithm makes the anonymous
// WindowProperties getter yield to own properties on later prototypes.
let mut prototype = holder.get_prototype(scope);
while let Some(value) = prototype {
if value.is_null_or_undefined() {
return false;
}
let Ok(object) = v8::Local::<v8::Object>::try_from(value) else {
return false;
};
if object.has_own_property(scope, key.into()).unwrap_or(false) {
return true;
}
prototype = object.get_prototype(scope);
}
false
}
fn window_named_access_value<'s>(
scope: &mut v8::PinScope<'s, '_>,
key: v8::Local<'s, v8::Name>,
holder: v8::Local<'s, v8::Object>,
) -> Option<v8::Local<'s, v8::Value>> {
let key = v8::Local::<v8::String>::try_from(key).ok()?;
if window_named_property_is_shadowed_by_prototype(scope, holder, key) {
return None;
}
let key_name = key.to_rust_string_lossy(scope);
if key_name.is_empty() || key_name.parse::<u32>().is_ok() {
return None;
}
if is_reserved_window_name(&key_name) {
return None;
}
let host_ptr = window_host_ptr(scope, holder)?;
let host = unsafe { &*host_ptr };
let child_handle = window_current_dispatch_scope(scope, holder, host)?.child_window();
// Browsing-context names win over document id/name exposure, matching the
// Window named access ordering. Both paths are fast miss paths now: child
// contexts return immediately when empty, and document name lookup is indexed.
let named_child = host.child_browsing_context_named_object_handle(child_handle, &key_name);
if let Some(handle) = named_child
&& host.child_browsing_context_child_is_same_origin(child_handle, handle)
&& let Some(window) =
unsafe { &mut *host_ptr }.child_browsing_context_window_wrapper(scope, handle)
{
return Some(window.into());
}
let host = unsafe { &*host_ptr };
let document = match child_handle {
Some(handle) => host.child_browsing_context_document_handle(handle)?,
None => host.document_handle(),
};
let handles = window_named_item_handles(host.dom_host(), document, &key_name);
if !handles.is_empty()
&& let Some(handle) = named_child
&& let Some(window) =
unsafe { &mut *host_ptr }.child_browsing_context_window_wrapper(scope, handle)
{
return Some(window.into());
}
if moli_trace::window_message_trace_enabled() {
tracing::info!(
target: "moli_window_message_trace",
property = %key_name,
child_handle = child_handle.map(|handle| handle.index()),
document_handle = document.index(),
matched_handle = handles.first().map(|handle| handle.index()),
match_count = handles.len(),
receiver_is_current_global = holder
.strict_equals(scope.get_current_context().global(scope).into()),
stage = "window_named_property_lookup",
);
}
let context = holder.get_creation_context(scope)?;
let scope = &mut v8::ContextScope::new(scope, context);
match handles.as_slice() {
[] => None,
[handle] => unsafe { &mut *host_ptr }
.native_bridge_mut()
.wrap_handle(scope, host_ptr, *handle)
.map(Into::into),
_ => build_window_named_items_collection(scope, host_ptr, document, &key_name)
.map(Into::into),
}
}