Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
feat(window): expose legacy navigator alias and External objects
ldm0a553acc7e466
diff --git a/moli-renderer-v8/src/context_bootstrap.rs b/moli-renderer-v8/src/context_bootstrap.rsindex e7c907295..ba7ca69a5 100644--- a/moli-renderer-v8/src/context_bootstrap.rs+++ b/moli-renderer-v8/src/context_bootstrap.rs@@ -3,6 +3,8 @@ pub(crate) mod abort_signal_events;mod animation_runtime;mod assets;pub(crate) mod bridge_descriptor;+mod external;+pub(crate) use runtime_state::install_lightweight_popup_legacy_objects;mod broadcast_channel;mod canvas;mod chrome_runtime;diff --git a/moli-renderer-v8/src/context_bootstrap/assets/prototype_bindings.rs b/moli-renderer-v8/src/context_bootstrap/assets/prototype_bindings.rsindex 3c3dc19b7..5bfe63fc3 100644--- a/moli-renderer-v8/src/context_bootstrap/assets/prototype_bindings.rs+++ b/moli-renderer-v8/src/context_bootstrap/assets/prototype_bindings.rs@@ -525,6 +525,11 @@ pub(super) fn install_constructor_template_bindings<'s>(install_crypto_template_bindings(scope, template, spec.interface.name());install_navigator_template_bindings(scope, template, spec.interface.name());install_screen_template_bindings(scope, template, spec.interface.name());+ crate::context_bootstrap::external::install_external_template_bindings(+ scope,+ template,+ spec.interface.name(),+ );install_visual_viewport_template_bindings(scope, template, spec.interface.name());install_speech_synthesis_template_bindings(scope, template, spec.interface.name());install_storage_access_template_bindings(scope, template, spec.interface.name());diff --git a/moli-renderer-v8/src/context_bootstrap/external.rs b/moli-renderer-v8/src/context_bootstrap/external.rsnew file mode 100644index 000000000..b313d6d3d--- /dev/null+++ b/moli-renderer-v8/src/context_bootstrap/external.rs@@ -0,0 +1,49 @@+//! HTML's legacy External object. Both operations are specified to do nothing.++use anyhow::Result;+use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject};++use crate::web_api_interfaces;++#[derive(WebApiObject)]+#[webapi(interface = web_api_interfaces::External)]+struct ExternalObjectDeclaration<'s> {+ #[webapi(prototype)]+ prototype: v8::Local<'s, v8::Object>,+}++#[derive(WebApiFunctionTemplate)]+#[webapi(interface = web_api_interfaces::External, enumerable, receiver)]+struct ExternalPrototypeDeclaration {+ #[webapi(method = "AddSearchProvider", length = 0, callback = external_noop)]+ add_search_provider: (),+ #[webapi(method = "IsSearchProviderInstalled", length = 0, callback = external_noop)]+ is_search_provider_installed: (),+}++pub(super) fn install_external_template_bindings<'s>(+ scope: &mut v8::PinScope<'s, '_, ()>,+ template: v8::Local<'s, v8::FunctionTemplate>,+ name: &str,+) {+ if name == "External" {+ ExternalPrototypeDeclaration::initialize_prototype_template(+ scope,+ template.prototype_template(scope),+ );+ }+}++pub(super) fn build_external_object<'s>(+ scope: &mut v8::PinScope<'s, '_>,+) -> Result<v8::Local<'s, v8::Object>> {+ let prototype = super::ensure_intrinsic_interface_prototype(scope, "External")?;+ Ok(ExternalObjectDeclaration::new(prototype).bind(scope)?)+}++fn external_noop<'s>(+ _scope: &mut v8::PinScope<'s, '_>,+ _args: v8::FunctionCallbackArguments<'s>,+ _rv: v8::ReturnValue<'s, v8::Value>,+) {+}diff --git a/moli-renderer-v8/src/context_bootstrap/runtime_state.rs b/moli-renderer-v8/src/context_bootstrap/runtime_state.rsindex 32c90bcbc..76c8fbf63 100644--- a/moli-renderer-v8/src/context_bootstrap/runtime_state.rs+++ b/moli-renderer-v8/src/context_bootstrap/runtime_state.rs@@ -144,6 +144,31 @@ struct WindowComputedStyleMethodDeclaration {get_computed_style: (),}+#[derive(WebApiObject)]+#[webapi(plain)]+struct WindowLegacySurfaceAccessorsDeclaration<'scope> {+ navigator_slot: v8::Local<'scope, v8::Value>,+ external_slot: v8::Local<'scope, v8::Value>,+ #[webapi(+ accessor_property = "clientInformation",+ enumerable,+ getter = window_surface_slot_getter,+ setter = window_surface_replaceable_setter,+ data = self.navigator_slot,+ setter_data = callback_data_index_value(scope, window_surface_replaceable_name_index("clientInformation").unwrap())+ )]+ client_information: (),+ #[webapi(+ accessor_property,+ enumerable,+ getter = window_surface_slot_getter,+ setter = window_surface_replaceable_setter,+ data = self.external_slot,+ setter_data = callback_data_index_value(scope, window_surface_replaceable_name_index("external").unwrap())+ )]+ external: (),+}+#[derive(WebApiObject)]#[webapi(plain)]struct WindowPublicSurfaceAccessorsDeclaration<'scope> {@@ -837,6 +862,18 @@ fn window_surface_slot_getter<'s>(}return;}+ // Lightweight popup shells share their opener's V8 realm but have their+ // own associated objects. Do not materialize the opener's lazy surface.+ if matches!(slot, WINDOW_NAVIGATOR_SLOT | WINDOW_EXTERNAL_SLOT)+ && crate::native_bridge::lightweight_popup_id_from_window(scope, receiver).is_some()+ {+ rv.set(+ get_private_value(scope, receiver, slot)+ .or(detached_fallback)+ .unwrap_or_else(|| v8::undefined(scope).into()),+ );+ return;+ }match super::window_lazy_surface::ensure_window_lazy_surface_value(scope, receiver, slot) {Ok(Some(value)) => {rv.set(value);@@ -911,6 +948,7 @@ fn install_public_window_surface_accessors<'s>(let history_slot = window_surface_callback_data(scope, global, WINDOW_HISTORY_SLOT);let navigation_slot = window_surface_callback_data(scope, global, WINDOW_NAVIGATION_SLOT);let navigator_slot = window_surface_callback_data(scope, global, WINDOW_NAVIGATOR_SLOT);+ let external_slot = window_surface_callback_data(scope, global, WINDOW_EXTERNAL_SLOT);let screen_slot = window_surface_callback_data(scope, global, WINDOW_SCREEN_SLOT);let speech_synthesis_slot =window_surface_callback_data(scope, global, WINDOW_SPEECH_SYNTHESIS_SLOT);@@ -935,6 +973,7 @@ fn install_public_window_surface_accessors<'s>(performance_slot,visual_viewport_slot,indexed_db_slot,+ external_slot,],);set_private_value(@@ -1030,6 +1069,13 @@ fn install_public_window_surface_accessors<'s>(name: (),}.initialize(scope, global)?;+ WindowLegacySurfaceAccessorsDeclaration {+ navigator_slot,+ external_slot,+ client_information: (),+ external: (),+ }+ .initialize(scope, global)?;WindowAdditionalReplaceableAccessorsDeclaration {origin_name,inner_width_name,@@ -1056,6 +1102,33 @@ fn install_public_window_surface_accessors<'s>(Ok(())}+pub(crate) fn install_lightweight_popup_legacy_objects<'s>(+ scope: &mut v8::PinScope<'s, '_>,+ window: v8::Local<'s, v8::Object>,+ navigator: v8::Local<'s, v8::Object>,+) -> Result<()> {+ let external = super::external::build_external_object(scope)?;+ set_private_value(scope, window, WINDOW_NAVIGATOR_SLOT, navigator.into());+ set_private_value(scope, window, WINDOW_EXTERNAL_SLOT, external.into());+ let navigator_slot = window_surface_callback_data(scope, window, WINDOW_NAVIGATOR_SLOT);+ let external_slot = window_surface_callback_data(scope, window, WINDOW_EXTERNAL_SLOT);+ WindowLegacySurfaceAccessorsDeclaration {+ navigator_slot,+ external_slot,+ client_information: (),+ external: (),+ }+ .initialize(scope, window)?;+ let constructor = super::ensure_intrinsic_interface_constructor(scope, "External")?;+ crate::util::define_non_enumerable_static_property(+ scope,+ window,+ "External",+ constructor.into(),+ );+ Ok(())+}+fn window_surface_callback_data<'s>(scope: &mut v8::PinScope<'s, '_>,global: v8::Local<'s, v8::Object>,@@ -1110,11 +1183,14 @@ const WINDOW_SURFACE_SLOTS: &[&str] = &[WINDOW_PERFORMANCE_SLOT,WINDOW_VISUAL_VIEWPORT_SLOT,WINDOW_INDEXED_DB_SURFACE_SLOT,+ WINDOW_EXTERNAL_SLOT,];const WINDOW_SURFACE_CALLBACK_DATA_REGISTRY_SLOT: &str = "__moliWindowSurfaceCallbackDataRegistry";const WINDOW_SURFACE_REPLACEABLE_NAMES: &[&str] = &[+ "clientInformation",+ "external","navigation","screen","performance",diff --git a/moli-renderer-v8/src/context_bootstrap/shared/slots.rs b/moli-renderer-v8/src/context_bootstrap/shared/slots.rsindex 3478314a3..4f153a42b 100644--- a/moli-renderer-v8/src/context_bootstrap/shared/slots.rs+++ b/moli-renderer-v8/src/context_bootstrap/shared/slots.rs@@ -11,6 +11,7 @@ pub(in crate::context_bootstrap) const WINDOW_ONUNHANDLEDREJECTION_SLOT: &str =pub(in crate::context_bootstrap) const WINDOW_ONREJECTIONHANDLED_SLOT: &str ="__moliWindowOnRejectionHandled";pub(in crate::context_bootstrap) const WINDOW_NAVIGATOR_SLOT: &str = "__moliWindowNavigator";+pub(in crate::context_bootstrap) const WINDOW_EXTERNAL_SLOT: &str = "__moliWindowExternal";pub(in crate::context_bootstrap) const NAVIGATOR_RUNTIME_DATA_SLOT: &str ="__moliNavigatorRuntimeData";pub(in crate::context_bootstrap) const WINDOW_HISTORY_SLOT: &str = "__moliWindowHistory";diff --git a/moli-renderer-v8/src/context_bootstrap/specs/registry.rs b/moli-renderer-v8/src/context_bootstrap/specs/registry.rsindex 8ed008eef..ec51932f2 100644--- a/moli-renderer-v8/src/context_bootstrap/specs/registry.rs+++ b/moli-renderer-v8/src/context_bootstrap/specs/registry.rs@@ -4,6 +4,10 @@ use crate::web_api_interfaces;use std::collections::HashSet;const CONSTRUCTOR_SPECS_BEFORE_STREAMS: &[ConstructorSpec] = &[+ ConstructorSpec {+ interface: web_api_interfaces::External::DESCRIPTOR,+ kind: ConstructorKind::Illegal,+ },ConstructorSpec {interface: web_api_interfaces::NodeList::DESCRIPTOR,kind: ConstructorKind::Illegal,diff --git a/moli-renderer-v8/src/context_bootstrap/window_lazy_surface/kind.rs b/moli-renderer-v8/src/context_bootstrap/window_lazy_surface/kind.rsindex d80336249..652b0d454 100644--- a/moli-renderer-v8/src/context_bootstrap/window_lazy_surface/kind.rs+++ b/moli-renderer-v8/src/context_bootstrap/window_lazy_surface/kind.rs@@ -1,5 +1,5 @@use super::super::{- WINDOW_CRYPTO_SLOT, WINDOW_CUSTOM_ELEMENTS_SLOT, WINDOW_NAVIGATOR_SLOT,+ WINDOW_CRYPTO_SLOT, WINDOW_CUSTOM_ELEMENTS_SLOT, WINDOW_EXTERNAL_SLOT, WINDOW_NAVIGATOR_SLOT,WINDOW_PERFORMANCE_SLOT, WINDOW_SCREEN_SLOT, WINDOW_SPEECH_SYNTHESIS_SLOT,WINDOW_VISUAL_VIEWPORT_SLOT,};@@ -7,6 +7,7 @@ use super::super::{#[derive(Clone, Copy, Debug, Eq, PartialEq)]pub(crate) enum WindowLazySurface {Navigator,+ External,Performance,CustomElements,Screen,@@ -19,6 +20,7 @@ impl WindowLazySurface {pub(super) fn from_slot(slot: &str) -> Option<Self> {match slot {WINDOW_NAVIGATOR_SLOT => Some(Self::Navigator),+ WINDOW_EXTERNAL_SLOT => Some(Self::External),WINDOW_PERFORMANCE_SLOT => Some(Self::Performance),WINDOW_CUSTOM_ELEMENTS_SLOT => Some(Self::CustomElements),WINDOW_SCREEN_SLOT => Some(Self::Screen),@@ -32,6 +34,7 @@ impl WindowLazySurface {pub(crate) const fn slot(self) -> &'static str {match self {Self::Navigator => WINDOW_NAVIGATOR_SLOT,+ Self::External => WINDOW_EXTERNAL_SLOT,Self::Performance => WINDOW_PERFORMANCE_SLOT,Self::CustomElements => WINDOW_CUSTOM_ELEMENTS_SLOT,Self::Screen => WINDOW_SCREEN_SLOT,@@ -44,6 +47,7 @@ impl WindowLazySurface {pub(super) const fn materializing_slot(self) -> &'static str {match self {Self::Navigator => "__moliWindowNavigatorMaterializing",+ Self::External => "__moliWindowExternalMaterializing",Self::Performance => "__moliWindowPerformanceMaterializing",Self::CustomElements => "__moliWindowCustomElementsMaterializing",Self::Screen => "__moliWindowScreenMaterializing",@@ -56,6 +60,7 @@ impl WindowLazySurface {pub(super) const fn label(self) -> &'static str {match self {Self::Navigator => "Navigator",+ Self::External => "External",Self::Performance => "Performance",Self::CustomElements => "CustomElementRegistry",Self::Screen => "Screen",diff --git a/moli-renderer-v8/src/context_bootstrap/window_lazy_surface/materialize.rs b/moli-renderer-v8/src/context_bootstrap/window_lazy_surface/materialize.rsindex 4e8d34d29..9e94da699 100644--- a/moli-renderer-v8/src/context_bootstrap/window_lazy_surface/materialize.rs+++ b/moli-renderer-v8/src/context_bootstrap/window_lazy_surface/materialize.rs@@ -11,6 +11,7 @@ pub(super) fn build_window_lazy_surface<'s>(super::super::navigator_runtime::build_window_navigator_for_receiver(scope, window)?.into()}+ WindowLazySurface::External => super::super::external::build_external_object(scope)?.into(),WindowLazySurface::Performance => {super::super::performance_runtime::build_window_performance_for_receiver(scope, window).into()diff --git a/moli-renderer-v8/src/native_bridge/context_host/popups.rs b/moli-renderer-v8/src/native_bridge/context_host/popups.rsindex 973d8435e..be9a18402 100644--- a/moli-renderer-v8/src/native_bridge/context_host/popups.rs+++ b/moli-renderer-v8/src/native_bridge/context_host/popups.rs@@ -799,6 +799,11 @@ impl JsContextHost {){set_object_slot(scope, window, "navigator", navigator.into());+ if let Err(error) = crate::context_bootstrap::install_lightweight_popup_legacy_objects(+ scope, window, navigator,+ ) {+ tracing::warn!(%error, "failed to initialize popup legacy Window objects");+ }}let _ = install_storage_aliases_for_window(scope, window);install_simple_event_target_methods(diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/mod.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/mod.rsindex 85ea20881..774887d01 100644--- a/moli-renderer-v8/src/script_vm/tests/browser_api/mod.rs+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/mod.rs@@ -53,7 +53,7 @@ mod trusted_types;mod value_events;mod web_audio;mod webrtc;-+mod window_legacy_objects;mod worker_listener_invocation;mod document_domain_lifetime;diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/window_legacy_objects.js b/moli-renderer-v8/src/script_vm/tests/browser_api/window_legacy_objects.jsnew file mode 100644index 000000000..b6ffaa5b6--- /dev/null+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/window_legacy_objects.js@@ -0,0 +1,65 @@+(() => {+ function assert(value, message) { if (!value) throw new Error(message); }+ function throws(C, fn, message) {+ try { fn(); } catch (error) { assert(error instanceof C, message + ': ' + error); return; }+ throw new Error(message + ': did not throw');+ }+ const iframe = document.createElement('iframe');+ document.body.appendChild(iframe);+ const child = iframe.contentWindow;+ try {+ for (const realm of [window, child]) {+ const originalNavigator = realm.navigator;+ const navigatorDescriptor = Object.getOwnPropertyDescriptor(realm, 'navigator');+ const alias = Object.getOwnPropertyDescriptor(realm, 'clientInformation');+ assert(alias.enumerable && alias.configurable && typeof alias.get === 'function' && typeof alias.set === 'function', 'clientInformation descriptor');+ assert(realm.clientInformation === originalNavigator, 'Navigator identity');+ Object.defineProperty(realm, 'navigator', {configurable: true, value: 42});+ assert(realm.clientInformation === originalNavigator, 'alias ignores public navigator shadow');+ realm.clientInformation = {replacement: true};+ const replaced = Object.getOwnPropertyDescriptor(realm, 'clientInformation');+ assert(replaced.writable && replaced.configurable && replaced.enumerable && !replaced.get && replaced.value.replacement, 'Replaceable alias');+ assert(alias.get.call(realm) === originalNavigator, 'retained alias getter keeps association');+ Object.defineProperty(realm, 'clientInformation', alias);+ Object.defineProperty(realm, 'navigator', navigatorDescriptor);++ const external = realm.external;+ const descriptor = Object.getOwnPropertyDescriptor(realm, 'external');+ assert(external === realm.external && external instanceof realm.External, 'External SameObject');+ assert(Object.getPrototypeOf(external) === realm.External.prototype && Object.prototype.toString.call(external) === '[object External]', 'External prototype and brand');+ assert(descriptor.enumerable && descriptor.configurable && typeof descriptor.get === 'function' && typeof descriptor.set === 'function', 'external descriptor');+ throws(realm.TypeError, () => new realm.External(), 'External illegal constructor');+ throws(realm.TypeError, () => realm.External(), 'External requires native creation');+ const hostile = {toString() {throw new Error('conversion');},valueOf() {throw new Error('conversion');}};+ for (const name of ['AddSearchProvider', 'IsSearchProviderInstalled']) {+ const method = Object.getOwnPropertyDescriptor(realm.External.prototype, name);+ assert(method.writable && method.enumerable && method.configurable && method.value.length === 0 && method.value.name === name, name + ' descriptor');+ assert(method.value.call(external, hostile) === undefined, name + ' ignores arguments');+ assert(method.value.call(window.external) === undefined, name + ' cross-realm brand');+ let traps = 0;+ const proxy = new Proxy(external, {get(){traps++;throw new Error('trap');},getPrototypeOf(){traps++;throw new Error('trap');}});+ const revoked = Proxy.revocable(external, {}); revoked.revoke();+ for (const fake of [{},Object.create(external),proxy,revoked.proxy,originalNavigator]) {+ throws(realm.TypeError, () => method.value.call(fake, hostile), name + ' receiver');+ }+ assert(traps === 0, name + ' must not inspect author Proxy');+ }+ realm.external = 21;+ const externalReplacement = Object.getOwnPropertyDescriptor(realm, 'external');+ assert(externalReplacement.value === 21 && externalReplacement.writable && externalReplacement.enumerable && externalReplacement.configurable, 'Replaceable external');+ assert(descriptor.get.call(realm) === external, 'retained external identity');+ Object.defineProperty(realm, 'external', descriptor);+ for (const property of ['clientInformation','external']) {+ const getter = Object.getOwnPropertyDescriptor(realm,property).get;+ throws(realm.TypeError, () => getter.call({}), property + ' Window receiver');+ assert(getter.call(window) === window[property], property + ' cross-realm Window receiver');+ }+ }+ assert(child.external !== window.external && child.clientInformation !== window.clientInformation, 'per-Window identity');+ const navigator = child.clientInformation;+ const external = child.external;+ iframe.remove();+ assert(child.clientInformation === navigator && child.external === external, 'detachment retains identity');+ return true;+ } finally { iframe.remove(); }+})()diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/window_legacy_objects.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/window_legacy_objects.rsnew file mode 100644index 000000000..2a94e2eac--- /dev/null+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/window_legacy_objects.rs@@ -0,0 +1,51 @@+use super::*;++#[test]+fn legacy_window_objects_preserve_association_brand_and_replaceability() {+ let mut vm = new_parsed_test_vm(+ "https://legacy-window.test/",+ "<!doctype html><body></body>",+ );+ let result = vm+ .eval(include_str!("window_legacy_objects.js"))+ .expect("legacy Window objects should preserve native identity and Web IDL semantics");+ assert_eq!(result, "true");+}++#[test]+fn lightweight_popup_legacy_objects_belong_to_the_popup_window() {+ let mut vm = new_storage_test_vm("https://legacy-popup.test/");+ let result = vm+ .eval(+ r#"+(() => {+ const popup = open('about:blank');+ try {+ const navigator = popup.navigator;+ const external = popup.external;+ const aliasGetter = Object.getOwnPropertyDescriptor(window, 'clientInformation').get;+ const externalGetter = Object.getOwnPropertyDescriptor(window, 'external').get;+ if (popup.clientInformation !== navigator || navigator === window.navigator ||+ aliasGetter.call(popup) !== navigator) throw new Error('popup Navigator association');+ if (!(external instanceof popup.External) || external === window.external ||+ popup.external !== external || externalGetter.call(popup) !== external) {+ throw new Error('popup External association');+ }+ if (external.AddSearchProvider() !== undefined ||+ external.IsSearchProviderInstalled() !== undefined) throw new Error('External methods');+ popup.navigator = 'shadow';+ if (popup.clientInformation !== navigator) throw new Error('public navigator shadow');+ popup.clientInformation = 42;+ popup.external = 17;+ if (popup.clientInformation !== 42 || popup.external !== 17 ||+ aliasGetter.call(popup) !== navigator || externalGetter.call(popup) !== external) {+ throw new Error('popup Replaceable properties');+ }+ return true;+ } finally { popup.close(); }+})()+"#,+ )+ .expect("popup legacy objects should use popup associations");+ assert_eq!(result, "true");+}diff --git a/moli-renderer-v8/src/web_api_interfaces.rs b/moli-renderer-v8/src/web_api_interfaces.rsindex 00b287078..6d4baead6 100644--- a/moli-renderer-v8/src/web_api_interfaces.rs+++ b/moli-renderer-v8/src/web_api_interfaces.rs@@ -142,6 +142,7 @@ interfaces! {EventCountsIterator = "EventCounts Iterator";EventSource: EventTarget;EventTarget;+ External;ExtendableEvent: Event;ExtendableMessageEvent: ExtendableEvent;FetchEvent: ExtendableEvent;