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::history_runtime::state::{history_window_owner, window_has_shared_history};
use super::location_history_storage::WINDOW_RUNTIME_OWNER_SLOT;
use super::location_runtime::{
build_location_runtime_object, install_location_runtime_state,
sync_window_location_history_navigation_runtime_surface,
};
use super::navigation_activation::{
install_navigation_activation_runtime_state, set_navigation_current_entry,
};
use super::navigation_entry::{set_history_entries, set_history_index};
use super::navigation_result::clear_active_cross_document_navigation_if_matches;
use super::navigation_seed::{
build_current_navigation_entry_from_seed, build_history_entries_from_seed,
initial_navigation_history_seed,
};
use super::navigation_surface::{
build_history_runtime_state, build_navigation_runtime_state,
install_history_scroll_restoration_runtime_state, install_history_state_runtime_state,
};
use super::navigation_window::set_runtime_window_owner;
use super::*;
use crate::context_bootstrap::navigation_entry::wrappers as entry_wrappers;
use crate::util::{get_private_value, set_private_value};
use crate::web_api_interfaces;
use anyhow::Result;
use moli_webapi_declare::WebApiObject;
#[derive(WebApiObject)]
#[webapi(interface = web_api_interfaces::Location, require_prototype)]
struct LocationRuntimeObjectDeclaration<'scope> {
#[webapi(slot = WINDOW_RUNTIME_OWNER_SLOT)]
owner: v8::Local<'scope, v8::Object>,
#[webapi(slot = WINDOW_LOCATION_HREF_SLOT)]
href: String,
}
fn new_location_runtime_object<'s>(
scope: &mut v8::PinScope<'s, '_>,
window: v8::Local<'s, v8::Object>,
href: &str,
) -> Result<v8::Local<'s, v8::Object>> {
let location = build_location_runtime_object(scope)?;
let owner = history_window_owner(scope, window);
LocationRuntimeObjectDeclaration::new(owner, href.to_owned())
.initialize(scope, location)
.map_err(|error| anyhow::anyhow!("failed to initialize Location object: {error}"))?;
Ok(location)
}
pub(crate) fn install_window_location_history_navigation_runtime_state<'s>(
scope: &mut v8::PinScope<'s, '_>,
window: v8::Local<'s, v8::Object>,
href: &str,
) -> Result<()> {
let location = new_location_runtime_object(scope, window, href)?;
install_location_runtime_state(scope, location, href)?;
set_private_value(scope, window, WINDOW_LOCATION_SLOT, location.into());
let initial_seed = initial_navigation_history_seed(scope, window, href);
let history = build_history_runtime_state(scope, window, &initial_seed)?;
let owner = history_window_owner(scope, window);
set_runtime_window_owner(scope, history, owner);
set_private_value(scope, window, WINDOW_HISTORY_SLOT, history.into());
let navigation = build_navigation_runtime_state(scope, window, &initial_seed)?;
set_runtime_window_owner(scope, navigation, owner);
set_private_value(scope, window, WINDOW_NAVIGATION_SLOT, navigation.into());
if crate::native_bridge::lightweight_popup_id_from_window(scope, window).is_some() {
super::session_history::initialize(scope, window, &initial_seed);
}
if !window.strict_equals(scope.get_current_context().global(scope).into()) {
sync_window_location_history_navigation_runtime_surface(scope, window);
}
Ok(())
}
pub(crate) fn reset_window_location_history_navigation_runtime_state<'s>(
scope: &mut v8::PinScope<'s, '_>,
window: v8::Local<'s, v8::Object>,
href: &str,
) -> Result<()> {
// Creating/rebinding an isolated realm must never reset the Window's
// shared history or its structured-clone state.
if window_has_shared_history(scope, window) {
return Ok(());
}
let location = match window_runtime_object(scope, window, WINDOW_LOCATION_SLOT) {
Some(location) => location,
None => {
let location = new_location_runtime_object(scope, window, href)?;
set_private_value(scope, window, WINDOW_LOCATION_SLOT, location.into());
location
}
};
LocationRuntimeObjectDeclaration::new(window, href.to_owned())
.initialize(scope, location)
.map_err(|error| anyhow::anyhow!("failed to initialize Location object: {error}"))?;
install_location_runtime_state(scope, location, href)?;
let initial_seed = initial_navigation_history_seed(scope, window, href);
let history = match window_runtime_object(scope, window, WINDOW_HISTORY_SLOT) {
Some(history) => history,
None => build_history_runtime_state(scope, window, &initial_seed)?,
};
set_runtime_window_owner(scope, history, window);
install_history_scroll_restoration_runtime_state(scope, history, "auto");
install_history_state_runtime_state(scope, history, v8::null(scope).into());
let entries = build_history_entries_from_seed(&initial_seed);
let current_entry = entries
.get(initial_seed.current_index as usize)
.map(|entry| entry_wrappers::for_window(scope, window, entry.clone()))
.unwrap_or_else(|| {
build_current_navigation_entry_from_seed(
scope,
window,
&initial_seed,
v8::null(scope).into(),
)
});
set_history_entries(scope, history, entries);
set_history_index(scope, history, initial_seed.current_index);
set_private_value(scope, window, WINDOW_HISTORY_SLOT, history.into());
let navigation = match window_runtime_object(scope, window, WINDOW_NAVIGATION_SLOT) {
Some(navigation) => navigation,
None => build_navigation_runtime_state(scope, window, &initial_seed)?,
};
set_runtime_window_owner(scope, navigation, window);
set_navigation_current_entry(scope, navigation, current_entry);
clear_active_cross_document_navigation_if_matches(scope, navigation, href);
install_navigation_activation_runtime_state(
scope,
navigation,
current_entry,
initial_seed.activation.as_ref(),
);
set_private_value(scope, window, WINDOW_NAVIGATION_SLOT, navigation.into());
sync_window_location_history_navigation_runtime_surface(scope, window);
Ok(())
}
fn window_runtime_object<'s>(
scope: &mut v8::PinScope<'s, '_>,
window: v8::Local<'s, v8::Object>,
slot: &'static str,
) -> Option<v8::Local<'s, v8::Object>> {
get_private_value(scope, window, slot)
.filter(|value| !value.is_undefined())
.and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())
}