use std::borrow::ToOwned;
use std::cell::{Cell, RefCell, RefMut};
use std::cmp;
use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use std::default::Default;
use std::ffi::c_void;
use std::io::{Write, stderr, stdout};
use std::ptr::NonNull;
use std::rc::{Rc, Weak};
use std::sync::Arc;
use std::time::{Duration, Instant};
use app_units::Au;
use base64::Engine;
use content_security_policy::Violation;
use content_security_policy::sandboxing_directive::SandboxingFlagSet;
use crossbeam_channel::{Sender, unbounded};
use cssparser::SourceLocation;
use devtools_traits::{ScriptToDevtoolsControlMsg, TimelineMarker, TimelineMarkerType};
use dom_struct::dom_struct;
use embedder_traits::user_contents::UserScript;
use embedder_traits::{
AlertResponse, ConfirmResponse, EmbedderMsg, JavaScriptEvaluationError, PromptResponse,
ScriptToEmbedderChan, SimpleDialogRequest, Theme, UntrustedNodeAddress, ViewportDetails,
WebDriverJSResult, WebDriverLoadStatus,
};
use euclid::default::Rect as UntypedRect;
use euclid::{Point2D, Rect, Scale, Size2D, Vector2D};
use fonts::{CspViolationHandler, FontContext, NetworkTimingHandler, WebFontDocumentContext};
use js::context::JSContext;
use js::glue::DumpJSStack;
use js::jsapi::{GCReason, Heap, JS_GC, JSContext as RawJSContext, JSObject, JSPROP_ENUMERATE};
use js::jsval::{NullValue, UndefinedValue};
use js::realm::{AutoRealm, CurrentRealm};
use js::rust::wrappers::JS_DefineProperty;
use js::rust::{
CustomAutoRooter, CustomAutoRooterGuard, HandleObject, HandleValue, MutableHandleObject,
MutableHandleValue,
};
use layout_api::{
AxesOverflow, BoxAreaType, CSSPixelRectIterator, ElementsFromPointFlags,
ElementsFromPointResult, FragmentType, Layout, LayoutImageDestination, PendingImage,
PendingImageState, PendingRasterizationImage, PhysicalSides, QueryMsg, ReflowGoal,
ReflowPhasesRun, ReflowRequest, ReflowRequestRestyle, ReflowStatistics, RestyleReason,
ScrollContainerQueryFlags, ScrollContainerResponse, TrustedNodeAddress,
combine_id_with_fragment_type,
};
use malloc_size_of::MallocSizeOf;
use media::WindowGLContext;
use net_traits::image_cache::{
ImageCache, ImageCacheResponseCallback, ImageCacheResponseMessage, ImageLoadListener,
ImageResponse, PendingImageId, PendingImageResponse, RasterizationCompleteResponse,
};
use net_traits::request::Referrer;
use net_traits::response::HttpsState;
use net_traits::{ResourceFetchTiming, ResourceThreads};
use num_traits::ToPrimitive;
use paint_api::{CrossProcessPaintApi, PinchZoomInfos};
use profile_traits::generic_channel as ProfiledGenericChannel;
use profile_traits::mem::ProfilerChan as MemProfilerChan;
use profile_traits::time::ProfilerChan as TimeProfilerChan;
use rustc_hash::{FxBuildHasher, FxHashMap};
use script_bindings::codegen::GenericBindings::WindowBinding::ScrollToOptions;
use script_bindings::conversions::SafeToJSValConvertible;
use script_bindings::interfaces::WindowHelpers;
use script_bindings::root::Root;
use script_traits::{ConstellationInputEvent, ScriptThreadMessage};
use selectors::attr::CaseSensitivity;
use servo_arc::Arc as ServoArc;
use servo_base::cross_process_instant::CrossProcessInstant;
use servo_base::generic_channel::{self, GenericCallback, GenericSender};
use servo_base::id::{BrowsingContextId, PipelineId, WebViewId};
#[cfg(feature = "bluetooth")]
use servo_bluetooth_traits::BluetoothRequest;
use servo_canvas_traits::webgl::WebGLChan;
use servo_config::pref;
use servo_constellation_traits::{
LoadData, LoadOrigin, ScreenshotReadinessResponse, ScriptToConstellationChan,
ScriptToConstellationMessage, StructuredSerializedData, WindowSizeType,
};
use servo_geometry::DeviceIndependentIntRect;
use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
use storage_traits::StorageThreads;
use storage_traits::webstorage_thread::WebStorageType;
use style::error_reporting::{ContextualParseError, ParseErrorReporter};
use style::properties::PropertyId;
use style::properties::style_structs::Font;
use style::selector_parser::PseudoElement;
use style::str::HTML_SPACE_CHARACTERS;
use style::stylesheets::UrlExtraData;
use style_traits::CSSPixel;
use stylo_atoms::Atom;
use time::Duration as TimeDuration;
use webrender_api::ExternalScrollId;
use webrender_api::units::{DeviceIntSize, DevicePixel, LayoutPixel, LayoutPoint};
use super::bindings::codegen::Bindings::MessagePortBinding::StructuredSerializeOptions;
use super::bindings::trace::HashMapTracedValues;
use super::performanceresourcetiming::InitiatorType;
use super::types::SVGSVGElement;
use crate::dom::bindings::cell::{DomRefCell, Ref};
use crate::dom::bindings::codegen::Bindings::DocumentBinding::{
DocumentMethods, DocumentReadyState, NamedPropertyValue,
};
use crate::dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElementMethods;
use crate::dom::bindings::codegen::Bindings::HistoryBinding::History_Binding::HistoryMethods;
use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
ImageBitmapOptions, ImageBitmapSource,
};
use crate::dom::bindings::codegen::Bindings::MediaQueryListBinding::MediaQueryList_Binding::MediaQueryListMethods;
use crate::dom::bindings::codegen::Bindings::ReportingObserverBinding::Report;
use crate::dom::bindings::codegen::Bindings::RequestBinding::{RequestInfo, RequestInit};
use crate::dom::bindings::codegen::Bindings::VoidFunctionBinding::VoidFunction;
use crate::dom::bindings::codegen::Bindings::WindowBinding::{
self, DeferredRequestInit, FrameRequestCallback, ScrollBehavior, WindowMethods,
WindowPostMessageOptions,
};
use crate::dom::bindings::codegen::UnionTypes::{
RequestOrUSVString, TrustedScriptOrString, TrustedScriptOrStringOrFunction,
};
use crate::dom::bindings::error::{
Error, ErrorInfo, ErrorResult, Fallible, javascript_error_info_from_error_info,
};
use crate::dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementTypeId, NodeTypeId};
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::{DomGlobal, DomObject};
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::bindings::structuredclone;
use crate::dom::bindings::trace::{CustomTraceable, JSTraceable, RootedTraceableBox};
use crate::dom::bindings::utils::GlobalStaticData;
use crate::dom::bindings::weakref::DOMTracker;
#[cfg(feature = "bluetooth")]
use crate::dom::bluetooth::BluetoothExtraPermissionData;
use crate::dom::cookiestore::CookieStore;
use crate::dom::crypto::Crypto;
use crate::dom::csp::GlobalCspReporting;
use crate::dom::css::cssstyledeclaration::{
CSSModificationAccess, CSSStyleDeclaration, CSSStyleOwner,
};
use crate::dom::customelementregistry::CustomElementRegistry;
use crate::dom::document::focus::FocusableArea;
use crate::dom::document::{
AnimationFrameCallback, Document, SameOriginDescendantNavigablesIterator,
};
use crate::dom::element::Element;
use crate::dom::event::{Event, EventBubbles, EventCancelable};
use crate::dom::eventtarget::EventTarget;
use crate::dom::fetchlaterresult::FetchLaterResult;
use crate::dom::globalscope::GlobalScope;
use crate::dom::history::History;
use crate::dom::html::htmlcollection::{CollectionFilter, HTMLCollection};
use crate::dom::html::htmliframeelement::HTMLIFrameElement;
use crate::dom::idbfactory::IDBFactory;
use crate::dom::inputevent::HitTestResult;
use crate::dom::location::Location;
use crate::dom::medialist::MediaList;
use crate::dom::mediaquerylist::{MediaQueryList, MediaQueryListMatchState};
use crate::dom::mediaquerylistevent::MediaQueryListEvent;
use crate::dom::messageevent::MessageEvent;
use crate::dom::navigator::Navigator;
use crate::dom::node::{Node, NodeDamage, NodeTraits, from_untrusted_node_address};
use crate::dom::performance::performance::Performance;
use crate::dom::promise::Promise;
use crate::dom::reporting::reportingendpoint::{ReportingEndpoint, SendReportsToEndpoints};
use crate::dom::reporting::reportingobserver::ReportingObserver;
use crate::dom::screen::Screen;
use crate::dom::scrolling_box::{ScrollingBox, ScrollingBoxSource};
use crate::dom::selection::Selection;
use crate::dom::shadowroot::ShadowRoot;
use crate::dom::storage::Storage;
#[cfg(feature = "bluetooth")]
use crate::dom::testrunner::TestRunner;
use crate::dom::trustedtypes::trustedtypepolicyfactory::TrustedTypePolicyFactory;
use crate::dom::types::{ImageBitmap, MouseEvent, UIEvent};
use crate::dom::useractivation::UserActivationTimestamp;
use crate::dom::visualviewport::{VisualViewport, VisualViewportChanges};
#[cfg(feature = "webgpu")]
use crate::dom::webgpu::identityhub::IdentityHub;
use crate::dom::windowproxy::{WindowProxy, WindowProxyHandler};
use crate::dom::worklet::Worklet;
use crate::dom::workletglobalscope::WorkletGlobalScopeType;
use crate::layout_image::fetch_image_for_layout;
use crate::messaging::{MainThreadScriptMsg, ScriptEventLoopReceiver, ScriptEventLoopSender};
use crate::microtask::{Microtask, UserMicrotask};
use crate::network_listener::{ResourceTimingListener, submit_timing};
use crate::realms::{enter_auto_realm, enter_realm};
use crate::script_runtime::{CanGc, JSContext as SafeJSContext, Runtime};
use crate::script_thread::ScriptThread;
use crate::script_window_proxies::ScriptWindowProxies;
use crate::task_source::SendableTaskSource;
use crate::timers::{IsInterval, TimerCallback};
use crate::unminify::unminified_path;
use crate::webdriver_handlers::{find_node_by_unique_id_in_document, jsval_to_webdriver};
use crate::{fetch, window_named_properties};
#[derive(MallocSizeOf)]
pub struct PendingImageCallback(
#[ignore_malloc_size_of = "dyn Fn is currently impossible to measure"]
#[expect(clippy::type_complexity)]
Box<dyn Fn(PendingImageResponse, &mut js::context::JSContext) + 'static>,
);
#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf, PartialEq)]
enum WindowState {
Alive,
Zombie, }
const INITIAL_REFLOW_DELAY: Duration = Duration::from_millis(200);
#[derive(Clone, Copy, MallocSizeOf)]
enum LayoutBlocker {
WaitingForParse,
Parsing(Instant),
FiredLoadEventOrParsingTimerExpired,
}
impl LayoutBlocker {
fn layout_blocked(&self) -> bool {
!matches!(self, Self::FiredLoadEventOrParsingTimerExpired)
}
}
#[derive(Clone, Copy, Debug, Default, JSTraceable, MallocSizeOf, PartialEq)]
pub(crate) struct OngoingNavigation(u32);
type PendingImageRasterizationKey = (PendingImageId, DeviceIntSize);
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
#[derive(JSTraceable, MallocSizeOf)]
struct PendingLayoutImageAncillaryData {
node: Dom<Node>,
#[no_trace]
destination: LayoutImageDestination,
}
#[dom_struct]
pub(crate) struct Window {
globalscope: GlobalScope,
#[ignore_malloc_size_of = "Weak does not need to be accounted"]
#[no_trace]
weak_script_thread: Weak<ScriptThread>,
#[no_trace]
webview_id: WebViewId,
script_chan: Sender<MainThreadScriptMsg>,
#[no_trace]
#[ignore_malloc_size_of = "TODO: Add MallocSizeOf support to layout"]
layout: RefCell<Box<dyn Layout>>,
navigator: MutNullableDom<Navigator>,
crypto: MutNullableDom<Crypto>,
#[ignore_malloc_size_of = "ImageCache"]
#[no_trace]
image_cache: Arc<dyn ImageCache>,
#[no_trace]
image_cache_sender: Sender<ImageCacheResponseMessage>,
window_proxy: MutNullableDom<WindowProxy>,
document: MutNullableDom<Document>,
location: MutNullableDom<Location>,
history: MutNullableDom<History>,
custom_element_registry: MutNullableDom<CustomElementRegistry>,
performance: MutNullableDom<Performance>,
#[no_trace]
navigation_start: Cell<CrossProcessInstant>,
screen: MutNullableDom<Screen>,
session_storage: MutNullableDom<Storage>,
local_storage: MutNullableDom<Storage>,
cookie_store: MutNullableDom<CookieStore>,
status: DomRefCell<DOMString>,
trusted_types: MutNullableDom<TrustedTypePolicyFactory>,
ongoing_navigation: Cell<OngoingNavigation>,
#[no_trace]
devtools_markers: DomRefCell<HashSet<TimelineMarkerType>>,
#[no_trace]
devtools_marker_sender: DomRefCell<Option<GenericSender<Option<TimelineMarker>>>>,
#[no_trace]
unhandled_resize_event: DomRefCell<Option<(ViewportDetails, WindowSizeType)>>,
#[no_trace]
theme: Cell<Theme>,
#[no_trace]
parent_info: Option<PipelineId>,
dom_static: GlobalStaticData,
#[conditional_malloc_size_of]
js_runtime: DomRefCell<Option<Rc<Runtime>>>,
#[no_trace]
viewport_details: Cell<ViewportDetails>,
#[no_trace]
#[cfg(feature = "bluetooth")]
bluetooth_thread: GenericSender<BluetoothRequest>,
#[cfg(feature = "bluetooth")]
bluetooth_extra_permission_data: BluetoothExtraPermissionData,
#[no_trace]
layout_blocker: Cell<LayoutBlocker>,
#[no_trace]
webdriver_script_chan: DomRefCell<Option<GenericSender<WebDriverJSResult>>>,
#[no_trace]
webdriver_load_status_sender: RefCell<Option<GenericSender<WebDriverLoadStatus>>>,
current_state: Cell<WindowState>,
error_reporter: CSSErrorReporter,
media_query_lists: DOMTracker<MediaQueryList>,
#[cfg(feature = "bluetooth")]
test_runner: MutNullableDom<TestRunner>,
#[no_trace]
webgl_chan: Option<WebGLChan>,
#[ignore_malloc_size_of = "defined in webxr"]
#[no_trace]
#[cfg(feature = "webxr")]
webxr_registry: Option<webxr_api::Registry>,
#[no_trace]
pending_image_callbacks: DomRefCell<FxHashMap<PendingImageId, Vec<PendingImageCallback>>>,
pending_layout_images: DomRefCell<
HashMapTracedValues<PendingImageId, Vec<PendingLayoutImageAncillaryData>, FxBuildHasher>,
>,
pending_images_for_rasterization: DomRefCell<
HashMapTracedValues<PendingImageRasterizationKey, Vec<Dom<Node>>, FxBuildHasher>,
>,
unminified_css_dir: DomRefCell<Option<String>>,
local_script_source: Option<String>,
test_worklet: MutNullableDom<Worklet>,
paint_worklet: MutNullableDom<Worklet>,
exists_mut_observer: Cell<bool>,
#[no_trace]
paint_api: CrossProcessPaintApi,
#[no_trace]
#[conditional_malloc_size_of]
user_scripts: Rc<Vec<UserScript>>,
#[ignore_malloc_size_of = "defined in script_thread"]
#[no_trace]
player_context: WindowGLContext,
throttled: Cell<bool>,
#[conditional_malloc_size_of]
layout_marker: DomRefCell<Rc<Cell<bool>>>,
current_event: DomRefCell<Option<Dom<Event>>>,
reporting_observer_list: DomRefCell<Vec<DomRoot<ReportingObserver>>>,
report_list: DomRefCell<Vec<Report>>,
#[no_trace]
endpoints_list: DomRefCell<Vec<ReportingEndpoint>>,
#[conditional_malloc_size_of]
script_window_proxies: Rc<ScriptWindowProxies>,
has_pending_screenshot_readiness_request: Cell<bool>,
visual_viewport: MutNullableDom<VisualViewport>,
has_changed_visual_viewport_dimension: Cell<bool>,
#[no_trace]
last_activation_timestamp: Cell<UserActivationTimestamp>,
devtools_wants_updates: Cell<bool>,
}
impl Window {
pub(crate) fn script_thread(&self) -> Rc<ScriptThread> {
Weak::upgrade(&self.weak_script_thread)
.expect("Weak reference should always be upgradable when a ScriptThread is running")
}
pub(crate) fn webview_id(&self) -> WebViewId {
self.webview_id
}
pub(crate) fn as_global_scope(&self) -> &GlobalScope {
self.upcast::<GlobalScope>()
}
pub(crate) fn layout(&self) -> Ref<'_, Box<dyn Layout>> {
self.layout.borrow()
}
pub(crate) fn layout_mut(&self) -> RefMut<'_, Box<dyn Layout>> {
self.layout.borrow_mut()
}
pub(crate) fn get_exists_mut_observer(&self) -> bool {
self.exists_mut_observer.get()
}
pub(crate) fn set_exists_mut_observer(&self) {
self.exists_mut_observer.set(true);
}
#[expect(unsafe_code)]
pub(crate) fn clear_js_runtime_for_script_deallocation(&self) {
self.as_global_scope()
.remove_web_messaging_and_dedicated_workers_infra();
unsafe {
*self.js_runtime.borrow_for_script_deallocation() = None;
self.window_proxy.set(None);
self.current_state.set(WindowState::Zombie);
self.as_global_scope()
.task_manager()
.cancel_all_tasks_and_ignore_future_tasks();
}
}
pub(crate) fn discard_browsing_context(&self) {
let proxy = match self.window_proxy.get() {
Some(proxy) => proxy,
None => panic!("Discarding a BC from a window that has none"),
};
proxy.discard_browsing_context();
self.as_global_scope()
.task_manager()
.cancel_all_tasks_and_ignore_future_tasks();
}
pub(crate) fn time_profiler_chan(&self) -> &TimeProfilerChan {
self.globalscope.time_profiler_chan()
}
pub(crate) fn origin(&self) -> &MutableOrigin {
self.globalscope.origin()
}
#[expect(unsafe_code)]
pub(crate) fn get_cx(&self) -> SafeJSContext {
unsafe { SafeJSContext::from_ptr(js::rust::Runtime::get().unwrap().as_ptr()) }
}
pub(crate) fn get_js_runtime(&self) -> Ref<'_, Option<Rc<Runtime>>> {
self.js_runtime.borrow()
}
pub(crate) fn main_thread_script_chan(&self) -> &Sender<MainThreadScriptMsg> {
&self.script_chan
}
pub(crate) fn parent_info(&self) -> Option<PipelineId> {
self.parent_info
}
pub(crate) fn new_script_pair(&self) -> (ScriptEventLoopSender, ScriptEventLoopReceiver) {
let (sender, receiver) = unbounded();
(
ScriptEventLoopSender::MainThread(sender),
ScriptEventLoopReceiver::MainThread(receiver),
)
}
pub(crate) fn event_loop_sender(&self) -> ScriptEventLoopSender {
ScriptEventLoopSender::MainThread(self.script_chan.clone())
}
pub(crate) fn image_cache(&self) -> Arc<dyn ImageCache> {
self.image_cache.clone()
}
pub(crate) fn window_proxy(&self) -> DomRoot<WindowProxy> {
self.window_proxy.get().unwrap()
}
pub(crate) fn append_reporting_observer(&self, reporting_observer: DomRoot<ReportingObserver>) {
self.reporting_observer_list
.borrow_mut()
.push(reporting_observer);
}
pub(crate) fn remove_reporting_observer(&self, reporting_observer: &ReportingObserver) {
let index = {
let list = self.reporting_observer_list.borrow();
list.iter()
.position(|observer| &**observer == reporting_observer)
};
if let Some(index) = index {
self.reporting_observer_list.borrow_mut().remove(index);
}
}
pub(crate) fn registered_reporting_observers(&self) -> Vec<DomRoot<ReportingObserver>> {
self.reporting_observer_list.borrow().clone()
}
pub(crate) fn append_report(&self, report: Report) {
self.report_list.borrow_mut().push(report);
let trusted_window = Trusted::new(self);
self.upcast::<GlobalScope>()
.task_manager()
.dom_manipulation_task_source()
.queue(task!(send_to_reporting_endpoints: move || {
let window = trusted_window.root();
let reports = std::mem::take(&mut *window.report_list.borrow_mut());
window.upcast::<GlobalScope>().send_reports_to_endpoints(
reports,
window.endpoints_list.borrow().clone(),
);
}));
}
pub(crate) fn buffered_reports(&self) -> Vec<Report> {
self.report_list.borrow().clone()
}
pub(crate) fn set_endpoints_list(&self, endpoints: Vec<ReportingEndpoint>) {
*self.endpoints_list.borrow_mut() = endpoints;
}
pub(crate) fn undiscarded_window_proxy(&self) -> Option<DomRoot<WindowProxy>> {
self.window_proxy.get().and_then(|window_proxy| {
if window_proxy.is_browsing_context_discarded() {
None
} else {
Some(window_proxy)
}
})
}
pub(crate) fn top_level_document_if_local(&self) -> Option<DomRoot<Document>> {
if self.is_top_level() {
return Some(self.Document());
}
let window_proxy = self.undiscarded_window_proxy()?;
self.script_window_proxies
.find_window_proxy(window_proxy.webview_id().into())?
.document()
}
#[cfg(feature = "bluetooth")]
pub(crate) fn bluetooth_thread(&self) -> GenericSender<BluetoothRequest> {
self.bluetooth_thread.clone()
}
#[cfg(feature = "bluetooth")]
pub(crate) fn bluetooth_extra_permission_data(&self) -> &BluetoothExtraPermissionData {
&self.bluetooth_extra_permission_data
}
pub(crate) fn css_error_reporter(&self) -> &CSSErrorReporter {
&self.error_reporter
}
pub(crate) fn webgl_chan(&self) -> Option<WebGLChan> {
self.webgl_chan.clone()
}
pub(crate) fn webgl_chan_value(&self) -> Option<WebGLChan> {
self.webgl_chan.clone()
}
#[cfg(feature = "webxr")]
pub(crate) fn webxr_registry(&self) -> Option<webxr_api::Registry> {
self.webxr_registry.clone()
}
fn new_paint_worklet(&self, cx: &mut JSContext) -> DomRoot<Worklet> {
debug!("Creating new paint worklet.");
Worklet::new(cx, self, WorkletGlobalScopeType::Paint)
}
pub(crate) fn register_image_cache_listener(
&self,
id: PendingImageId,
callback: impl Fn(PendingImageResponse, &mut js::context::JSContext) + 'static,
) -> ImageCacheResponseCallback {
self.pending_image_callbacks
.borrow_mut()
.entry(id)
.or_default()
.push(PendingImageCallback(Box::new(callback)));
let image_cache_sender = self.image_cache_sender.clone();
Box::new(move |message| {
let _ = image_cache_sender.send(message);
})
}
fn pending_layout_image_notification(&self, response: PendingImageResponse) {
let mut images = self.pending_layout_images.borrow_mut();
let nodes = images.entry(response.id);
let nodes = match nodes {
Entry::Occupied(nodes) => nodes,
Entry::Vacant(_) => return,
};
if matches!(
response.response,
ImageResponse::Loaded(_, _) | ImageResponse::FailedToLoadOrDecode
) {
for ancillary_data in nodes.get() {
match ancillary_data.destination {
LayoutImageDestination::BoxTreeConstruction => {
ancillary_data.node.dirty(NodeDamage::Other);
},
LayoutImageDestination::DisplayListBuilding => {
self.layout().set_needs_new_display_list();
},
}
}
}
match response.response {
ImageResponse::MetadataLoaded(_) => {},
ImageResponse::Loaded(_, _) | ImageResponse::FailedToLoadOrDecode => {
nodes.remove();
},
}
}
pub(crate) fn handle_image_rasterization_complete_notification(
&self,
response: RasterizationCompleteResponse,
) {
let mut images = self.pending_images_for_rasterization.borrow_mut();
let nodes = images.entry((response.image_id, response.requested_size));
let nodes = match nodes {
Entry::Occupied(nodes) => nodes,
Entry::Vacant(_) => return,
};
for node in nodes.get() {
node.dirty(NodeDamage::Other);
}
nodes.remove();
}
pub(crate) fn pending_image_notification(
&self,
response: PendingImageResponse,
cx: &mut js::context::JSContext,
) {
let mut images = std::mem::take(&mut *self.pending_image_callbacks.borrow_mut());
let Entry::Occupied(callbacks) = images.entry(response.id) else {
let _ = std::mem::replace(&mut *self.pending_image_callbacks.borrow_mut(), images);
return;
};
for callback in callbacks.get() {
callback.0(response.clone(), cx);
}
match response.response {
ImageResponse::MetadataLoaded(_) => {},
ImageResponse::Loaded(_, _) | ImageResponse::FailedToLoadOrDecode => {
callbacks.remove();
},
}
let _ = std::mem::replace(&mut *self.pending_image_callbacks.borrow_mut(), images);
}
pub(crate) fn paint_api(&self) -> &CrossProcessPaintApi {
&self.paint_api
}
pub(crate) fn userscripts(&self) -> &[UserScript] {
&self.user_scripts
}
pub(crate) fn get_player_context(&self) -> WindowGLContext {
self.player_context.clone()
}
pub(crate) fn dispatch_event_with_target_override(&self, event: &Event, can_gc: CanGc) {
event.dispatch(self.upcast(), true, can_gc);
}
pub(crate) fn font_context(&self) -> &Arc<FontContext> {
self.as_global_scope()
.font_context()
.expect("A `Window` should always have a `FontContext`")
}
pub(crate) fn ongoing_navigation(&self) -> OngoingNavigation {
self.ongoing_navigation.get()
}
pub(crate) fn set_ongoing_navigation(&self) -> OngoingNavigation {
let new_value = self.ongoing_navigation.get().0.wrapping_add(1);
self.ongoing_navigation.set(OngoingNavigation(new_value));
OngoingNavigation(new_value)
}
fn stop_loading(&self, cx: &mut js::context::JSContext) {
let doc = self.Document();
self.set_ongoing_navigation();
doc.abort(cx);
}
fn destroy_top_level_traversable(&self, cx: &mut js::context::JSContext) {
let document = self.Document();
document.destroy_document_and_its_descendants(cx);
self.send_to_constellation(ScriptToConstellationMessage::DiscardTopLevelBrowsingContext);
}
fn definitely_close(&self, cx: &mut js::context::JSContext) {
let document = self.Document();
if !document.check_if_unloading_is_cancelled(false, CanGc::from_cx(cx)) {
return;
}
document.unload(cx, false);
self.destroy_top_level_traversable(cx);
}
fn cannot_show_simple_dialogs(&self) -> bool {
if self
.Document()
.has_active_sandboxing_flag(SandboxingFlagSet::SANDBOXED_MODALS_FLAG)
{
return true;
}
false
}
pub(crate) fn perform_a_microtask_checkpoint(&self, cx: &mut js::context::JSContext) {
self.script_thread().perform_a_microtask_checkpoint(cx);
}
pub(crate) fn web_font_context(&self) -> WebFontDocumentContext {
let global = self.as_global_scope();
WebFontDocumentContext {
policy_container: global.policy_container(),
request_client: global.request_client(),
document_url: global.api_base_url(),
has_trustworthy_ancestor_origin: global.has_trustworthy_ancestor_origin(),
insecure_requests_policy: global.insecure_requests_policy(),
csp_handler: Box::new(FontCspHandler {
global: Trusted::new(global),
task_source: global
.task_manager()
.dom_manipulation_task_source()
.to_sendable(),
}),
network_timing_handler: Box::new(FontNetworkTimingHandler {
global: Trusted::new(global),
task_source: global
.task_manager()
.dom_manipulation_task_source()
.to_sendable(),
}),
}
}
#[expect(unsafe_code)]
pub(crate) fn gc(&self) {
unsafe {
JS_GC(*self.get_cx(), GCReason::API);
}
}
}
#[derive(Debug)]
struct FontCspHandler {
global: Trusted<GlobalScope>,
task_source: SendableTaskSource,
}
impl CspViolationHandler for FontCspHandler {
fn process_violations(&self, violations: Vec<Violation>) {
let global = self.global.clone();
self.task_source.queue(task!(csp_violation: move || {
global.root().report_csp_violations(violations, None, None);
}));
}
fn clone(&self) -> Box<dyn CspViolationHandler> {
Box::new(Self {
global: self.global.clone(),
task_source: self.task_source.clone(),
})
}
}
#[derive(Debug)]
struct FontNetworkTimingHandler {
global: Trusted<GlobalScope>,
task_source: SendableTaskSource,
}
impl NetworkTimingHandler for FontNetworkTimingHandler {
fn submit_timing(&self, url: ServoUrl, response: ResourceFetchTiming) {
let global = self.global.clone();
self.task_source.queue(task!(network_timing: move |cx| {
submit_timing(
cx,
&FontFetchListener {
url,
global
},
&Ok(()),
&response,
);
}));
}
fn clone(&self) -> Box<dyn NetworkTimingHandler> {
Box::new(Self {
global: self.global.clone(),
task_source: self.task_source.clone(),
})
}
}
#[derive(Debug)]
struct FontFetchListener {
global: Trusted<GlobalScope>,
url: ServoUrl,
}
impl ResourceTimingListener for FontFetchListener {
fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
(InitiatorType::Css, self.url.clone())
}
fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
self.global.root()
}
}
pub(crate) fn base64_btoa(input: DOMString) -> Fallible<DOMString> {
if input.str().chars().any(|c: char| c > '\u{FF}') {
Err(Error::InvalidCharacter(None))
} else {
let octets = input
.str()
.chars()
.map(|c: char| c as u8)
.collect::<Vec<u8>>();
let config =
base64::engine::general_purpose::GeneralPurposeConfig::new().with_encode_padding(true);
let engine = base64::engine::GeneralPurpose::new(&base64::alphabet::STANDARD, config);
Ok(DOMString::from(engine.encode(octets)))
}
}
pub(crate) fn base64_atob(input: DOMString) -> Fallible<DOMString> {
fn is_html_space(c: char) -> bool {
HTML_SPACE_CHARACTERS.contains(&c)
}
let without_spaces = input
.str()
.chars()
.filter(|&c| !is_html_space(c))
.collect::<String>();
let mut input = &*without_spaces;
if input.len() % 4 == 0 {
if input.ends_with("==") {
input = &input[..input.len() - 2]
} else if input.ends_with('=') {
input = &input[..input.len() - 1]
}
}
if input.len() % 4 == 1 {
return Err(Error::InvalidCharacter(None));
}
if input
.chars()
.any(|c| c != '+' && c != '/' && !c.is_alphanumeric())
{
return Err(Error::InvalidCharacter(None));
}
let config = base64::engine::general_purpose::GeneralPurposeConfig::new()
.with_decode_padding_mode(base64::engine::DecodePaddingMode::RequireNone)
.with_decode_allow_trailing_bits(true);
let engine = base64::engine::GeneralPurpose::new(&base64::alphabet::STANDARD, config);
let data = engine
.decode(input)
.map_err(|_| Error::InvalidCharacter(None))?;
Ok(data.iter().map(|&b| b as char).collect::<String>().into())
}
impl WindowMethods<crate::DomTypeHolder> for Window {
fn Alert_(&self) {
self.Alert(DOMString::new());
}
fn Alert(&self, mut message: DOMString) {
if self.cannot_show_simple_dialogs() {
return;
}
message.normalize_newlines();
{
let stderr = stderr();
let mut stderr = stderr.lock();
let stdout = stdout();
let mut stdout = stdout.lock();
writeln!(&mut stdout, "\nALERT: {message}").unwrap();
stdout.flush().unwrap();
stderr.flush().unwrap();
}
let (sender, receiver) =
ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
let dialog = SimpleDialogRequest::Alert {
id: self.Document().embedder_controls().next_control_id(),
message: message.to_string(),
response_sender: sender,
};
self.send_to_embedder(EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog));
receiver.recv().unwrap_or_else(|_| {
debug!("Alert dialog was cancelled or failed to show.");
AlertResponse::Ok
});
}
fn Confirm(&self, mut message: DOMString) -> bool {
if self.cannot_show_simple_dialogs() {
return false;
}
message.normalize_newlines();
let (sender, receiver) =
ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
let dialog = SimpleDialogRequest::Confirm {
id: self.Document().embedder_controls().next_control_id(),
message: message.to_string(),
response_sender: sender,
};
self.send_to_embedder(EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog));
match receiver.recv() {
Ok(ConfirmResponse::Ok) => true,
Ok(ConfirmResponse::Cancel) => false,
Err(_) => {
warn!("Confirm dialog was cancelled or failed to show.");
false
},
}
}
fn Prompt(&self, mut message: DOMString, default: DOMString) -> Option<DOMString> {
if self.cannot_show_simple_dialogs() {
return None;
}
message.normalize_newlines();
let (sender, receiver) =
ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
let dialog = SimpleDialogRequest::Prompt {
id: self.Document().embedder_controls().next_control_id(),
message: message.to_string(),
default: default.to_string(),
response_sender: sender,
};
self.send_to_embedder(EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog));
match receiver.recv() {
Ok(PromptResponse::Ok(input)) => Some(input.into()),
Ok(PromptResponse::Cancel) => None,
Err(_) => {
warn!("Prompt dialog was cancelled or failed to show.");
None
},
}
}
fn Stop(&self, cx: &mut js::context::JSContext) {
self.stop_loading(cx);
}
fn Focus(&self, cx: &mut js::context::JSContext) {
let document = self.Document();
if !document.is_active() || self.undiscarded_window_proxy().is_none() {
return;
}
document.focus_handler().focus(cx, FocusableArea::Viewport);
}
fn Blur(&self) {
}
fn Open(
&self,
cx: &mut JSContext,
url: USVString,
target: DOMString,
features: DOMString,
) -> Fallible<Option<DomRoot<WindowProxy>>> {
self.window_proxy().open(cx, url, target, features)
}
fn GetOpener(&self, cx: &mut CurrentRealm, mut retval: MutableHandleValue) -> Fallible<()> {
let current = match self.window_proxy.get() {
Some(proxy) => proxy,
None => {
retval.set(NullValue());
return Ok(());
},
};
if current.is_browsing_context_discarded() {
retval.set(NullValue());
return Ok(());
}
current.opener(cx, retval);
Ok(())
}
#[expect(unsafe_code)]
fn SetOpener(&self, cx: SafeJSContext, value: HandleValue) -> ErrorResult {
if value.is_null() {
if let Some(proxy) = self.window_proxy.get() {
proxy.disown();
}
return Ok(());
}
let obj = self.reflector().get_jsobject();
unsafe {
let result =
JS_DefineProperty(*cx, obj, c"opener".as_ptr(), value, JSPROP_ENUMERATE as u32);
if result { Ok(()) } else { Err(Error::JSFailed) }
}
}
fn Closed(&self) -> bool {
self.window_proxy
.get()
.map(|ref proxy| proxy.is_browsing_context_discarded() || proxy.is_closing())
.unwrap_or(true)
}
fn Close(&self) {
let window_proxy = match self.window_proxy.get() {
Some(proxy) => proxy,
None => return,
};
if window_proxy.is_closing() {
return;
}
if let Ok(history_length) = self.History().GetLength() {
let is_auxiliary = window_proxy.is_auxiliary();
let is_script_closable = (self.is_top_level() && history_length == 1) ||
is_auxiliary ||
pref!(dom_allow_scripts_to_close_windows);
if is_script_closable {
window_proxy.close();
let this = Trusted::new(self);
let task = task!(window_close_browsing_context: move |cx| {
let window = this.root();
window.definitely_close(cx);
});
self.as_global_scope()
.task_manager()
.dom_manipulation_task_source()
.queue(task);
}
}
}
fn Document(&self) -> DomRoot<Document> {
self.document
.get()
.expect("Document accessed before initialization.")
}
fn History(&self) -> DomRoot<History> {
self.history
.or_init(|| History::new(self, CanGc::deprecated_note()))
}
fn IndexedDB(&self) -> DomRoot<IDBFactory> {
self.upcast::<GlobalScope>().get_indexeddb()
}
fn CustomElements(&self) -> DomRoot<CustomElementRegistry> {
self.custom_element_registry
.or_init(|| CustomElementRegistry::new(self, CanGc::deprecated_note()))
}
fn Location(&self, cx: &mut js::context::JSContext) -> DomRoot<Location> {
self.location.or_init(|| Location::new(cx, self))
}
fn GetSessionStorage(&self, cx: &mut js::context::JSContext) -> Fallible<DomRoot<Storage>> {
if let Some(storage) = self.session_storage.get() {
return Ok(storage);
}
if !self.origin().is_tuple() {
return Err(Error::Security(Some(
"Cannot access sessionStorage from opaque origin.".to_string(),
)));
}
let storage = Storage::new(self, WebStorageType::Session, CanGc::from_cx(cx));
self.session_storage.set(Some(&storage));
Ok(storage)
}
fn GetLocalStorage(&self, cx: &mut js::context::JSContext) -> Fallible<DomRoot<Storage>> {
if let Some(storage) = self.local_storage.get() {
return Ok(storage);
}
if !self.origin().is_tuple() {
return Err(Error::Security(Some(
"Cannot access localStorage from opaque origin.".to_string(),
)));
}
let storage = Storage::new(self, WebStorageType::Local, CanGc::from_cx(cx));
self.local_storage.set(Some(&storage));
Ok(storage)
}
fn CookieStore(&self, can_gc: CanGc) -> DomRoot<CookieStore> {
self.cookie_store
.or_init(|| CookieStore::new(self.upcast::<GlobalScope>(), can_gc))
}
fn Crypto(&self) -> DomRoot<Crypto> {
self.crypto
.or_init(|| Crypto::new(self.as_global_scope(), CanGc::deprecated_note()))
}
fn GetFrameElement(&self) -> Option<DomRoot<Element>> {
let window_proxy = self.window_proxy.get()?;
let container = window_proxy.frame_element()?;
let container_doc = container.owner_document();
let current_doc = GlobalScope::current()
.expect("No current global object")
.as_window()
.Document();
if !current_doc
.origin()
.same_origin_domain(&container_doc.origin())
{
return None;
}
Some(DomRoot::from_ref(container))
}
fn ReportError(&self, cx: SafeJSContext, error: HandleValue, can_gc: CanGc) {
self.as_global_scope()
.report_an_exception(cx, error, can_gc);
}
fn Navigator(&self) -> DomRoot<Navigator> {
self.navigator
.or_init(|| Navigator::new(self, CanGc::deprecated_note()))
}
fn ClientInformation(&self) -> DomRoot<Navigator> {
self.Navigator()
}
fn SetTimeout(
&self,
cx: &mut js::context::JSContext,
callback: TrustedScriptOrStringOrFunction,
timeout: i32,
args: Vec<HandleValue>,
) -> Fallible<i32> {
let callback = match callback {
TrustedScriptOrStringOrFunction::String(i) => {
TimerCallback::StringTimerCallback(TrustedScriptOrString::String(i))
},
TrustedScriptOrStringOrFunction::TrustedScript(i) => {
TimerCallback::StringTimerCallback(TrustedScriptOrString::TrustedScript(i))
},
TrustedScriptOrStringOrFunction::Function(i) => TimerCallback::FunctionTimerCallback(i),
};
self.as_global_scope().set_timeout_or_interval(
cx,
callback,
args,
Duration::from_millis(timeout.max(0) as u64),
IsInterval::NonInterval,
)
}
fn ClearTimeout(&self, handle: i32) {
self.as_global_scope().clear_timeout_or_interval(handle);
}
fn SetInterval(
&self,
cx: &mut js::context::JSContext,
callback: TrustedScriptOrStringOrFunction,
timeout: i32,
args: Vec<HandleValue>,
) -> Fallible<i32> {
let callback = match callback {
TrustedScriptOrStringOrFunction::String(i) => {
TimerCallback::StringTimerCallback(TrustedScriptOrString::String(i))
},
TrustedScriptOrStringOrFunction::TrustedScript(i) => {
TimerCallback::StringTimerCallback(TrustedScriptOrString::TrustedScript(i))
},
TrustedScriptOrStringOrFunction::Function(i) => TimerCallback::FunctionTimerCallback(i),
};
self.as_global_scope().set_timeout_or_interval(
cx,
callback,
args,
Duration::from_millis(timeout.max(0) as u64),
IsInterval::Interval,
)
}
fn ClearInterval(&self, handle: i32) {
self.ClearTimeout(handle);
}
fn QueueMicrotask(&self, callback: Rc<VoidFunction>) {
ScriptThread::enqueue_microtask(Microtask::User(UserMicrotask {
callback,
pipeline: self.pipeline_id(),
}));
}
fn CreateImageBitmap(
&self,
realm: &mut CurrentRealm,
image: ImageBitmapSource,
options: &ImageBitmapOptions,
) -> Rc<Promise> {
ImageBitmap::create_image_bitmap(
self.as_global_scope(),
image,
0,
0,
None,
None,
options,
realm,
)
}
fn CreateImageBitmap_(
&self,
realm: &mut CurrentRealm,
image: ImageBitmapSource,
sx: i32,
sy: i32,
sw: i32,
sh: i32,
options: &ImageBitmapOptions,
) -> Rc<Promise> {
ImageBitmap::create_image_bitmap(
self.as_global_scope(),
image,
sx,
sy,
Some(sw),
Some(sh),
options,
realm,
)
}
fn Window(&self) -> DomRoot<WindowProxy> {
self.window_proxy()
}
fn Self_(&self) -> DomRoot<WindowProxy> {
self.window_proxy()
}
fn Frames(&self) -> DomRoot<WindowProxy> {
self.window_proxy()
}
fn Length(&self) -> u32 {
self.Document().iframes().iter().count() as u32
}
fn GetParent(&self) -> Option<DomRoot<WindowProxy>> {
let window_proxy = self.undiscarded_window_proxy()?;
if let Some(parent) = window_proxy.parent() {
return Some(DomRoot::from_ref(parent));
}
Some(window_proxy)
}
fn GetTop(&self) -> Option<DomRoot<WindowProxy>> {
let window_proxy = self.undiscarded_window_proxy()?;
Some(DomRoot::from_ref(window_proxy.top()))
}
fn Performance(&self) -> DomRoot<Performance> {
self.performance.or_init(|| {
Performance::new(
self.as_global_scope(),
self.navigation_start.get(),
CanGc::deprecated_note(),
)
})
}
global_event_handlers!();
window_event_handlers!();
fn Screen(&self, can_gc: CanGc) -> DomRoot<Screen> {
self.screen.or_init(|| Screen::new(self, can_gc))
}
fn GetVisualViewport(&self, can_gc: CanGc) -> Option<DomRoot<VisualViewport>> {
if !self.Document().is_fully_active() {
return None;
}
Some(self.get_or_init_visual_viewport(can_gc))
}
fn Btoa(&self, btoa: DOMString) -> Fallible<DOMString> {
base64_btoa(btoa)
}
fn Atob(&self, atob: DOMString) -> Fallible<DOMString> {
base64_atob(atob)
}
fn RequestAnimationFrame(&self, callback: Rc<FrameRequestCallback>) -> u32 {
self.Document()
.request_animation_frame(AnimationFrameCallback::FrameRequestCallback { callback })
}
fn CancelAnimationFrame(&self, ident: u32) {
let doc = self.Document();
doc.cancel_animation_frame(ident);
}
fn PostMessage(
&self,
cx: &mut JSContext,
message: HandleValue,
target_origin: USVString,
transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
) -> ErrorResult {
let incumbent = GlobalScope::incumbent().expect("no incumbent global?");
let source = incumbent.as_window();
let source_origin = source.Document().origin().immutable().clone();
self.post_message_impl(&target_origin, source_origin, source, cx, message, transfer)
}
fn PostMessage_(
&self,
cx: &mut JSContext,
message: HandleValue,
options: RootedTraceableBox<WindowPostMessageOptions>,
) -> ErrorResult {
let mut rooted = CustomAutoRooter::new(
options
.parent
.transfer
.iter()
.map(|js: &RootedTraceableBox<Heap<*mut JSObject>>| js.get())
.collect(),
);
#[expect(unsafe_code)]
let transfer = unsafe { CustomAutoRooterGuard::new(cx.raw_cx(), &mut rooted) };
let incumbent = GlobalScope::incumbent().expect("no incumbent global?");
let source = incumbent.as_window();
let source_origin = source.Document().origin().immutable().clone();
self.post_message_impl(
&options.targetOrigin,
source_origin,
source,
cx,
message,
transfer,
)
}
fn CaptureEvents(&self) {
}
fn ReleaseEvents(&self) {
}
fn WebdriverCallback(&self, realm: &mut CurrentRealm, value: HandleValue) {
let webdriver_script_sender = self.webdriver_script_chan.borrow_mut().take();
if let Some(webdriver_script_sender) = webdriver_script_sender {
let result = jsval_to_webdriver(realm, &self.globalscope, value);
let _ = webdriver_script_sender.send(result);
}
}
fn WebdriverException(&self, cx: &mut JSContext, value: HandleValue) {
let webdriver_script_sender = self.webdriver_script_chan.borrow_mut().take();
if let Some(webdriver_script_sender) = webdriver_script_sender {
let error_info = ErrorInfo::from_value(value, cx.into(), CanGc::from_cx(cx));
let _ = webdriver_script_sender.send(Err(
JavaScriptEvaluationError::EvaluationFailure(Some(
javascript_error_info_from_error_info(cx, &error_info, value),
)),
));
}
}
fn WebdriverElement(&self, id: DOMString) -> Option<DomRoot<Element>> {
find_node_by_unique_id_in_document(&self.Document(), id.into()).and_then(Root::downcast)
}
fn WebdriverFrame(&self, browsing_context_id: DOMString) -> Option<DomRoot<WindowProxy>> {
self.Document()
.iframes()
.iter()
.find(|iframe| {
iframe
.browsing_context_id()
.as_ref()
.map(BrowsingContextId::to_string) ==
Some(browsing_context_id.to_string())
})
.and_then(|iframe| iframe.GetContentWindow())
}
fn WebdriverWindow(&self, webview_id: DOMString) -> DomRoot<WindowProxy> {
let window_proxy = &self
.window_proxy
.get()
.expect("Should always have a WindowProxy when calling WebdriverWindow");
assert!(
self.is_top_level(),
"Window must be top level browsing context."
);
assert!(self.webview_id().to_string() == webview_id);
DomRoot::from_ref(window_proxy)
}
fn WebdriverShadowRoot(&self, id: DOMString) -> Option<DomRoot<ShadowRoot>> {
find_node_by_unique_id_in_document(&self.Document(), id.into()).and_then(Root::downcast)
}
fn GetComputedStyle(
&self,
element: &Element,
pseudo: Option<DOMString>,
) -> DomRoot<CSSStyleDeclaration> {
let mut is_null = false;
let pseudo = pseudo.map(|mut s| {
s.make_ascii_lowercase();
s
});
let pseudo = match pseudo {
Some(ref pseudo) if pseudo == ":before" || pseudo == "::before" => {
Some(PseudoElement::Before)
},
Some(ref pseudo) if pseudo == ":after" || pseudo == "::after" => {
Some(PseudoElement::After)
},
Some(ref pseudo) if pseudo == "::selection" => Some(PseudoElement::Selection),
Some(ref pseudo) if pseudo == "::marker" => Some(PseudoElement::Marker),
Some(ref pseudo) if pseudo == "::placeholder" => Some(PseudoElement::Placeholder),
Some(ref pseudo) if pseudo.starts_with(':') => {
is_null = true;
None
},
_ => None,
};
CSSStyleDeclaration::new(
self,
if is_null {
CSSStyleOwner::Null
} else {
CSSStyleOwner::Element(Dom::from_ref(element))
},
pseudo,
CSSModificationAccess::Readonly,
CanGc::deprecated_note(),
)
}
fn InnerHeight(&self) -> i32 {
self.viewport_details
.get()
.size
.height
.to_i32()
.unwrap_or(0)
}
fn InnerWidth(&self) -> i32 {
self.viewport_details.get().size.width.to_i32().unwrap_or(0)
}
fn ScrollX(&self) -> i32 {
self.scroll_offset().x as i32
}
fn PageXOffset(&self) -> i32 {
self.ScrollX()
}
fn ScrollY(&self) -> i32 {
self.scroll_offset().y as i32
}
fn PageYOffset(&self) -> i32 {
self.ScrollY()
}
fn Scroll(&self, options: &ScrollToOptions) {
let x = options.left.unwrap_or(0.0) as f32;
let y = options.top.unwrap_or(0.0) as f32;
self.scroll(x, y, options.parent.behavior);
}
fn Scroll_(&self, x: f64, y: f64) {
self.scroll(x as f32, y as f32, ScrollBehavior::Auto);
}
fn ScrollTo(&self, options: &ScrollToOptions) {
self.Scroll(options);
}
fn ScrollTo_(&self, x: f64, y: f64) {
self.Scroll_(x, y)
}
fn ScrollBy(&self, options: &ScrollToOptions) {
let mut options = options.clone();
let x = options.left.unwrap_or(0.0);
let x = if x.is_finite() { x } else { 0.0 };
let y = options.top.unwrap_or(0.0);
let y = if y.is_finite() { y } else { 0.0 };
options.left.replace(x + self.ScrollX() as f64);
options.top.replace(y + self.ScrollY() as f64);
self.Scroll(&options)
}
fn ScrollBy_(&self, x: f64, y: f64) {
let mut options = ScrollToOptions::empty();
options.left.replace(x);
options.top.replace(y);
self.ScrollBy(&options);
}
fn ResizeTo(&self, width: i32, height: i32) {
let window_proxy = match self.window_proxy.get() {
Some(proxy) => proxy,
None => return,
};
if !window_proxy.is_auxiliary() {
return;
}
let dpr = self.device_pixel_ratio();
let size = Size2D::new(width, height).to_f32() * dpr;
self.send_to_embedder(EmbedderMsg::ResizeTo(self.webview_id(), size.to_i32()));
}
fn ResizeBy(&self, x: i32, y: i32) {
let size = self.client_window().size();
self.ResizeTo(x + size.width, y + size.height)
}
fn MoveTo(&self, x: i32, y: i32) {
let dpr = self.device_pixel_ratio();
let point = Point2D::new(x, y).to_f32() * dpr;
let msg = EmbedderMsg::MoveTo(self.webview_id(), point.to_i32());
self.send_to_embedder(msg);
}
fn MoveBy(&self, x: i32, y: i32) {
let origin = self.client_window().min;
self.MoveTo(x + origin.x, y + origin.y)
}
fn ScreenX(&self) -> i32 {
self.client_window().min.x
}
fn ScreenY(&self) -> i32 {
self.client_window().min.y
}
fn OuterHeight(&self) -> i32 {
self.client_window().height()
}
fn OuterWidth(&self) -> i32 {
self.client_window().width()
}
fn DevicePixelRatio(&self) -> Finite<f64> {
Finite::wrap(self.device_pixel_ratio().get() as f64)
}
fn Status(&self) -> DOMString {
self.status.borrow().clone()
}
fn SetStatus(&self, status: DOMString) {
*self.status.borrow_mut() = status
}
fn MatchMedia(&self, query: DOMString) -> DomRoot<MediaQueryList> {
let media_query_list = MediaList::parse_media_list(&query.str(), self);
let document = self.Document();
let mql = MediaQueryList::new(&document, media_query_list, CanGc::deprecated_note());
self.media_query_lists.track(&*mql);
mql
}
fn Fetch(
&self,
realm: &mut CurrentRealm,
input: RequestOrUSVString,
init: RootedTraceableBox<RequestInit>,
) -> Rc<Promise> {
fetch::Fetch(self.upcast(), input, init, realm)
}
fn FetchLater(
&self,
cx: &mut js::context::JSContext,
input: RequestInfo,
init: RootedTraceableBox<DeferredRequestInit>,
) -> Fallible<DomRoot<FetchLaterResult>> {
fetch::FetchLater(cx, self, input, init)
}
#[cfg(feature = "bluetooth")]
fn TestRunner(&self) -> DomRoot<TestRunner> {
self.test_runner
.or_init(|| TestRunner::new(self.upcast(), CanGc::deprecated_note()))
}
fn RunningAnimationCount(&self) -> u32 {
self.document
.get()
.map_or(0, |d| d.animations().running_animation_count() as u32)
}
fn SetName(&self, name: DOMString) {
if let Some(proxy) = self.undiscarded_window_proxy() {
proxy.set_name(name);
}
}
fn Name(&self) -> DOMString {
match self.undiscarded_window_proxy() {
Some(proxy) => proxy.get_name(),
None => "".into(),
}
}
fn Origin(&self) -> USVString {
USVString(self.origin().immutable().ascii_serialization())
}
fn GetSelection(&self, cx: &mut JSContext) -> Option<DomRoot<Selection>> {
self.document.get().and_then(|d| d.GetSelection(cx))
}
fn Event(&self, cx: SafeJSContext, rval: MutableHandleValue) {
if let Some(ref event) = *self.current_event.borrow() {
event
.reflector()
.get_jsobject()
.safe_to_jsval(cx, rval, CanGc::deprecated_note());
}
}
fn IsSecureContext(&self) -> bool {
self.as_global_scope().is_secure_context()
}
fn NamedGetter(
&self,
cx: &mut js::context::JSContext,
name: DOMString,
) -> Option<NamedPropertyValue> {
if name.is_empty() {
return None;
}
let document = self.Document();
let iframes: Vec<_> = document
.iframes()
.iter()
.filter(|iframe| {
if let Some(window) = iframe.GetContentWindow() {
return window.get_name() == name;
}
false
})
.collect();
let iframe_iter = iframes.iter().map(|iframe| iframe.upcast::<Element>());
let name = Atom::from(name);
let elements_with_name = document.get_elements_with_name(&name);
let name_iter = elements_with_name
.iter()
.map(|element| &**element)
.filter(|elem| is_named_element_with_name_attribute(elem));
let elements_with_id = document.get_elements_with_id(&name);
let id_iter = elements_with_id
.iter()
.map(|element| &**element)
.filter(|elem| is_named_element_with_id_attribute(elem));
for elem in iframe_iter.clone() {
if let Some(nested_window_proxy) = elem
.downcast::<HTMLIFrameElement>()
.and_then(|iframe| iframe.GetContentWindow())
{
return Some(NamedPropertyValue::WindowProxy(nested_window_proxy));
}
}
let mut elements = iframe_iter.chain(name_iter).chain(id_iter);
let first = elements.next()?;
if elements.next().is_none() {
return Some(NamedPropertyValue::Element(DomRoot::from_ref(first)));
}
#[derive(JSTraceable, MallocSizeOf)]
struct WindowNamedGetter {
#[no_trace]
name: Atom,
}
impl CollectionFilter for WindowNamedGetter {
fn filter(&self, elem: &Element, _root: &Node) -> bool {
let type_ = match elem.upcast::<Node>().type_id() {
NodeTypeId::Element(ElementTypeId::HTMLElement(type_)) => type_,
_ => return false,
};
if elem.get_id().as_ref() == Some(&self.name) {
return true;
}
match type_ {
HTMLElementTypeId::HTMLEmbedElement |
HTMLElementTypeId::HTMLFormElement |
HTMLElementTypeId::HTMLImageElement |
HTMLElementTypeId::HTMLObjectElement => {
elem.get_name().as_ref() == Some(&self.name)
},
_ => false,
}
}
}
let collection = HTMLCollection::create(
cx,
self,
document.upcast(),
Box::new(WindowNamedGetter { name }),
);
Some(NamedPropertyValue::HTMLCollection(collection))
}
fn SupportedPropertyNames(&self) -> Vec<DOMString> {
let mut names_with_first_named_element_map: HashMap<&Atom, &Element> = HashMap::new();
let document = self.Document();
let name_map = document.name_map();
for (name, elements) in &name_map.0 {
if name.is_empty() {
continue;
}
let mut name_iter = elements
.iter()
.filter(|elem| is_named_element_with_name_attribute(elem));
if let Some(first) = name_iter.next() {
names_with_first_named_element_map.insert(name, first);
}
}
let id_map = document.id_map();
for (id, elements) in &id_map.0 {
if id.is_empty() {
continue;
}
let mut id_iter = elements
.iter()
.filter(|elem| is_named_element_with_id_attribute(elem));
if let Some(first) = id_iter.next() {
match names_with_first_named_element_map.entry(id) {
Entry::Vacant(entry) => drop(entry.insert(first)),
Entry::Occupied(mut entry) => {
if first.upcast::<Node>().is_before(entry.get().upcast()) {
*entry.get_mut() = first;
}
},
}
}
}
let mut names_with_first_named_element_vec: Vec<(&Atom, &Element)> =
names_with_first_named_element_map
.iter()
.map(|(k, v)| (*k, *v))
.collect();
names_with_first_named_element_vec.sort_unstable_by(|a, b| {
if a.1 == b.1 {
a.0.cmp(b.0)
} else if a.1.upcast::<Node>().is_before(b.1.upcast::<Node>()) {
cmp::Ordering::Less
} else {
cmp::Ordering::Greater
}
});
names_with_first_named_element_vec
.iter()
.map(|(k, _v)| DOMString::from(&***k))
.collect()
}
fn StructuredClone(
&self,
cx: &mut JSContext,
value: HandleValue,
options: RootedTraceableBox<StructuredSerializeOptions>,
retval: MutableHandleValue,
) -> Fallible<()> {
self.as_global_scope()
.structured_clone(cx, value, options, retval)
}
fn TrustedTypes(&self, cx: &mut JSContext) -> DomRoot<TrustedTypePolicyFactory> {
self.trusted_types
.or_init(|| TrustedTypePolicyFactory::new(cx, self.as_global_scope()))
}
}
impl Window {
pub(crate) fn scroll_offset(&self) -> Vector2D<f32, LayoutPixel> {
self.scroll_offset_query_with_external_scroll_id(self.pipeline_id().root_scroll_id())
}
pub(crate) fn create_named_properties_object(
cx: SafeJSContext,
proto: HandleObject,
object: MutableHandleObject,
) {
window_named_properties::create(cx, proto, object)
}
pub(crate) fn current_event(&self) -> Option<DomRoot<Event>> {
self.current_event
.borrow()
.as_ref()
.map(|e| DomRoot::from_ref(&**e))
}
pub(crate) fn set_current_event(&self, event: Option<&Event>) -> Option<DomRoot<Event>> {
let current = self.current_event();
*self.current_event.borrow_mut() = event.map(Dom::from_ref);
current
}
fn post_message_impl(
&self,
target_origin: &USVString,
source_origin: ImmutableOrigin,
source: &Window,
cx: &mut JSContext,
message: HandleValue,
transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
) -> ErrorResult {
let data = structuredclone::write(cx.into(), message, Some(transfer))?;
let target_origin = match target_origin.0[..].as_ref() {
"*" => None,
"/" => Some(source_origin.clone()),
url => match ServoUrl::parse(url) {
Ok(url) => Some(url.origin()),
Err(_) => return Err(Error::Syntax(None)),
},
};
self.post_message(target_origin, source_origin, &source.window_proxy(), data);
Ok(())
}
pub(crate) fn paint_worklet(&self, cx: &mut JSContext) -> DomRoot<Worklet> {
self.paint_worklet.or_init(|| self.new_paint_worklet(cx))
}
pub(crate) fn has_document(&self) -> bool {
self.document.get().is_some()
}
pub(crate) fn clear_js_runtime(&self) {
self.as_global_scope()
.remove_web_messaging_and_dedicated_workers_infra();
if let Some(custom_elements) = self.custom_element_registry.get() {
custom_elements.teardown();
}
self.current_state.set(WindowState::Zombie);
*self.js_runtime.borrow_mut() = None;
if let Some(proxy) = self.window_proxy.get() {
let pipeline_id = self.pipeline_id();
if let Some(currently_active) = proxy.currently_active() {
if currently_active == pipeline_id {
self.window_proxy.set(None);
}
}
}
if let Some(performance) = self.performance.get() {
performance.clear_and_disable_performance_entry_buffer();
}
self.as_global_scope()
.task_manager()
.cancel_all_tasks_and_ignore_future_tasks();
}
pub(crate) fn scroll(&self, x: f32, y: f32, behavior: ScrollBehavior) {
let xfinite = if x.is_finite() { x } else { 0.0 };
let yfinite = if y.is_finite() { y } else { 0.0 };
let viewport = self.viewport_details.get().size;
let scrolling_area = self.scrolling_area_query(None).to_f32();
let x = xfinite.clamp(0.0, 0.0f32.max(scrolling_area.width() - viewport.width));
let y = yfinite.clamp(0.0, 0.0f32.max(scrolling_area.height() - viewport.height));
let scroll_offset = self.scroll_offset();
if x == scroll_offset.x && y == scroll_offset.y {
return;
}
self.perform_a_scroll(x, y, self.pipeline_id().root_scroll_id(), behavior, None);
}
pub(crate) fn perform_a_scroll(
&self,
x: f32,
y: f32,
scroll_id: ExternalScrollId,
_behavior: ScrollBehavior,
element: Option<&Element>,
) {
let (reflow_phases_run, _) =
self.reflow(ReflowGoal::UpdateScrollNode(scroll_id, Vector2D::new(x, y)));
if reflow_phases_run.needs_frame() {
self.paint_api()
.generate_frame(vec![self.webview_id().into()]);
}
if reflow_phases_run.contains(ReflowPhasesRun::UpdatedScrollNodeOffset) {
match element {
Some(element) if !scroll_id.is_root() => element.handle_scroll_event(),
_ => self.Document().handle_viewport_scroll_event(),
};
}
}
pub(crate) fn device_pixel_ratio(&self) -> Scale<f32, CSSPixel, DevicePixel> {
self.viewport_details.get().hidpi_scale_factor
}
fn client_window(&self) -> DeviceIndependentIntRect {
let (sender, receiver) = generic_channel::channel().expect("Failed to create IPC channel!");
self.send_to_embedder(EmbedderMsg::GetWindowRect(self.webview_id(), sender));
receiver.recv().unwrap_or_default()
}
pub(crate) fn advance_animation_clock(&self, delta: TimeDuration) {
self.Document()
.advance_animation_timeline_for_testing(delta);
ScriptThread::handle_tick_all_animations_for_testing(self.pipeline_id());
}
pub(crate) fn reflow(&self, reflow_goal: ReflowGoal) -> (ReflowPhasesRun, ReflowStatistics) {
let document = self.Document();
if !document.is_fully_active() {
return Default::default();
}
self.Document().ensure_safe_to_run_script_or_layout();
let pipeline_id = self.pipeline_id();
if reflow_goal == ReflowGoal::UpdateTheRendering &&
self.layout_blocker.get().layout_blocked()
{
debug!("Suppressing pre-load-event reflow pipeline {pipeline_id}");
return Default::default();
}
debug!("script: performing reflow for goal {reflow_goal:?}");
let marker = if self.need_emit_timeline_marker(TimelineMarkerType::Reflow) {
Some(TimelineMarker::start("Reflow".to_owned()))
} else {
None
};
let restyle_reason = document.restyle_reason();
document.clear_restyle_reasons();
let restyle = if restyle_reason.needs_restyle() {
debug!("Invalidating layout cache due to reflow condition {restyle_reason:?}",);
self.layout_marker.borrow().set(false);
*self.layout_marker.borrow_mut() = Rc::new(Cell::new(true));
let stylesheets_changed = document.flush_stylesheets_for_reflow();
let pending_restyles = document.drain_pending_restyles();
let dirty_root = document
.take_dirty_root()
.filter(|_| !stylesheets_changed)
.or_else(|| document.GetDocumentElement())
.map(|root| root.upcast::<Node>().to_trusted_node_address());
Some(ReflowRequestRestyle {
reason: restyle_reason,
dirty_root,
stylesheets_changed,
pending_restyles,
})
} else {
None
};
let document_context = self.web_font_context();
let reflow = ReflowRequest {
document: document.upcast::<Node>().to_trusted_node_address(),
epoch: document.current_rendering_epoch(),
restyle,
viewport_details: self.viewport_details.get(),
origin: self.origin().immutable().clone(),
reflow_goal,
animation_timeline_value: document.current_animation_timeline_value(),
animations: document.animations().sets.clone(),
animating_images: document.image_animation_manager().animating_images(),
highlighted_dom_node: document.highlighted_dom_node().map(|node| node.to_opaque()),
document_context,
};
let Some(reflow_result) = self.layout.borrow_mut().reflow(reflow) else {
return Default::default();
};
debug!("script: layout complete");
if let Some(marker) = marker {
self.emit_timeline_marker(marker.end());
}
self.handle_pending_images_post_reflow(
reflow_result.pending_images,
reflow_result.pending_rasterization_images,
reflow_result.pending_svg_elements_for_serialization,
);
if let Some(iframe_sizes) = reflow_result.iframe_sizes {
document
.iframes_mut()
.handle_new_iframe_sizes_after_layout(self, iframe_sizes);
}
document.update_animations_post_reflow();
(
reflow_result.reflow_phases_run,
reflow_result.reflow_statistics,
)
}
pub(crate) fn request_screenshot_readiness(&self, can_gc: CanGc) {
self.has_pending_screenshot_readiness_request.set(true);
self.maybe_resolve_pending_screenshot_readiness_requests(can_gc);
}
pub(crate) fn maybe_resolve_pending_screenshot_readiness_requests(&self, can_gc: CanGc) {
let pending_request = self.has_pending_screenshot_readiness_request.get();
if !pending_request {
return;
}
let document = self.Document();
if document.ReadyState() != DocumentReadyState::Complete {
return;
}
if document.render_blocking_element_count() > 0 {
return;
}
if document.GetDocumentElement().is_some_and(|elem| {
elem.has_class(&atom!("reftest-wait"), CaseSensitivity::CaseSensitive) ||
elem.has_class(&Atom::from("test-wait"), CaseSensitivity::CaseSensitive)
}) {
return;
}
if self.font_context().web_fonts_still_loading() != 0 {
return;
}
if self.Document().Fonts(can_gc).waiting_to_fullfill_promise() {
return;
}
if !self.pending_layout_images.borrow().is_empty() ||
!self.pending_images_for_rasterization.borrow().is_empty()
{
return;
}
let document = self.Document();
if document.needs_rendering_update() {
return;
}
let epoch = document.current_rendering_epoch();
let pipeline_id = self.pipeline_id();
debug!("Ready to take screenshot of {pipeline_id:?} at epoch={epoch:?}");
self.send_to_constellation(
ScriptToConstellationMessage::RespondToScreenshotReadinessRequest(
ScreenshotReadinessResponse::Ready(epoch),
),
);
self.has_pending_screenshot_readiness_request.set(false);
}
pub(crate) fn reflow_if_reflow_timer_expired(&self) {
if !matches!(
self.layout_blocker.get(),
LayoutBlocker::Parsing(instant) if instant + INITIAL_REFLOW_DELAY < Instant::now()
) {
return;
}
self.allow_layout_if_necessary();
}
pub(crate) fn prevent_layout_until_load_event(&self) {
if !matches!(self.layout_blocker.get(), LayoutBlocker::WaitingForParse) {
return;
}
self.layout_blocker
.set(LayoutBlocker::Parsing(Instant::now()));
}
pub(crate) fn allow_layout_if_necessary(&self) {
if matches!(
self.layout_blocker.get(),
LayoutBlocker::FiredLoadEventOrParsingTimerExpired
) {
return;
}
self.layout_blocker
.set(LayoutBlocker::FiredLoadEventOrParsingTimerExpired);
let document = self.Document();
if !document.is_render_blocked() && document.update_the_rendering().0.needs_frame() {
self.paint_api()
.generate_frame(vec![self.webview_id().into()]);
}
}
pub(crate) fn layout_blocked(&self) -> bool {
self.layout_blocker.get().layout_blocked()
}
pub(crate) fn layout_reflow(&self, query_msg: QueryMsg) {
self.reflow(ReflowGoal::LayoutQuery(query_msg));
}
pub(crate) fn resolved_font_style_query(
&self,
node: &Node,
value: String,
) -> Option<ServoArc<Font>> {
self.layout_reflow(QueryMsg::ResolvedFontStyleQuery);
let document = self.Document();
let animations = document.animations().sets.clone();
self.layout.borrow().query_resolved_font_style(
node.to_trusted_node_address(),
&value,
animations,
document.current_animation_timeline_value(),
)
}
#[expect(unsafe_code)]
pub(crate) fn containing_block_node_query_without_reflow(
&self,
node: &Node,
) -> Option<DomRoot<Node>> {
self.layout
.borrow()
.query_containing_block(node.to_trusted_node_address())
.map(|address| unsafe { from_untrusted_node_address(address) })
}
pub(crate) fn padding_query_without_reflow(&self, node: &Node) -> Option<PhysicalSides> {
let layout = self.layout.borrow();
layout.query_padding(node.to_trusted_node_address())
}
pub(crate) fn box_area_query_without_reflow(
&self,
node: &Node,
area: BoxAreaType,
exclude_transform_and_inline: bool,
) -> Option<Rect<Au, CSSPixel>> {
let layout = self.layout.borrow();
layout.ensure_stacking_context_tree(self.viewport_details.get());
layout.query_box_area(
node.to_trusted_node_address(),
area,
exclude_transform_and_inline,
)
}
pub(crate) fn box_area_query(
&self,
node: &Node,
area: BoxAreaType,
exclude_transform_and_inline: bool,
) -> Option<Rect<Au, CSSPixel>> {
self.layout_reflow(QueryMsg::BoxArea);
self.box_area_query_without_reflow(node, area, exclude_transform_and_inline)
}
pub(crate) fn box_areas_query(&self, node: &Node, area: BoxAreaType) -> CSSPixelRectIterator {
self.layout_reflow(QueryMsg::BoxAreas);
self.layout
.borrow()
.query_box_areas(node.to_trusted_node_address(), area)
}
pub(crate) fn client_rect_query(&self, node: &Node) -> Rect<i32, CSSPixel> {
self.layout_reflow(QueryMsg::ClientRectQuery);
self.layout
.borrow()
.query_client_rect(node.to_trusted_node_address())
}
pub(crate) fn current_css_zoom_query(&self, node: &Node) -> f32 {
self.layout_reflow(QueryMsg::CurrentCSSZoomQuery);
self.layout
.borrow()
.query_current_css_zoom(node.to_trusted_node_address())
}
pub(crate) fn scrolling_area_query(&self, node: Option<&Node>) -> Rect<i32, CSSPixel> {
self.layout_reflow(QueryMsg::ScrollingAreaOrOffsetQuery);
self.layout
.borrow()
.query_scrolling_area(node.map(Node::to_trusted_node_address))
}
pub(crate) fn scroll_offset_query(&self, node: &Node) -> Vector2D<f32, LayoutPixel> {
let external_scroll_id = ExternalScrollId(
combine_id_with_fragment_type(node.to_opaque().id(), FragmentType::FragmentBody),
self.pipeline_id().into(),
);
self.scroll_offset_query_with_external_scroll_id(external_scroll_id)
}
fn scroll_offset_query_with_external_scroll_id(
&self,
external_scroll_id: ExternalScrollId,
) -> Vector2D<f32, LayoutPixel> {
self.layout_reflow(QueryMsg::ScrollingAreaOrOffsetQuery);
self.scroll_offset_query_with_external_scroll_id_no_reflow(external_scroll_id)
}
fn scroll_offset_query_with_external_scroll_id_no_reflow(
&self,
external_scroll_id: ExternalScrollId,
) -> Vector2D<f32, LayoutPixel> {
self.layout
.borrow()
.scroll_offset(external_scroll_id)
.unwrap_or_default()
}
pub(crate) fn scroll_an_element(
&self,
element: &Element,
x: f32,
y: f32,
behavior: ScrollBehavior,
) {
let scroll_id = ExternalScrollId(
combine_id_with_fragment_type(
element.upcast::<Node>().to_opaque().id(),
FragmentType::FragmentBody,
),
self.pipeline_id().into(),
);
self.perform_a_scroll(x, y, scroll_id, behavior, Some(element));
}
pub(crate) fn resolved_style_query(
&self,
element: TrustedNodeAddress,
pseudo: Option<PseudoElement>,
property: PropertyId,
) -> DOMString {
self.layout_reflow(QueryMsg::ResolvedStyleQuery);
let document = self.Document();
let animations = document.animations().sets.clone();
DOMString::from(self.layout.borrow().query_resolved_style(
element,
pseudo,
property,
animations,
document.current_animation_timeline_value(),
))
}
pub(crate) fn get_iframe_viewport_details_if_known(
&self,
browsing_context_id: BrowsingContextId,
) -> Option<ViewportDetails> {
self.layout_reflow(QueryMsg::InnerWindowDimensionsQuery);
self.Document()
.iframes()
.get(browsing_context_id)
.and_then(|iframe| iframe.size)
}
#[expect(unsafe_code)]
pub(crate) fn offset_parent_query(
&self,
node: &Node,
) -> (Option<DomRoot<Element>>, Rect<Au, CSSPixel>) {
self.layout_reflow(QueryMsg::OffsetParentQuery);
let response = self
.layout
.borrow()
.query_offset_parent(node.to_trusted_node_address());
let element = response.node_address.and_then(|parent_node_address| {
let node = unsafe { from_untrusted_node_address(parent_node_address) };
DomRoot::downcast(node)
});
(element, response.rect)
}
pub(crate) fn scroll_container_query(
&self,
node: Option<&Node>,
flags: ScrollContainerQueryFlags,
) -> Option<ScrollContainerResponse> {
self.layout_reflow(QueryMsg::ScrollParentQuery);
self.layout
.borrow()
.query_scroll_container(node.map(Node::to_trusted_node_address), flags)
}
#[expect(unsafe_code)]
pub(crate) fn scrolling_box_query(
&self,
node: Option<&Node>,
flags: ScrollContainerQueryFlags,
) -> Option<ScrollingBox> {
self.scroll_container_query(node, flags)
.and_then(|response| {
Some(match response {
ScrollContainerResponse::Viewport(overflow) => {
(ScrollingBoxSource::Viewport(self.Document()), overflow)
},
ScrollContainerResponse::Element(parent_node_address, overflow) => {
let node = unsafe { from_untrusted_node_address(parent_node_address) };
(
ScrollingBoxSource::Element(DomRoot::downcast(node)?),
overflow,
)
},
})
})
.map(|(source, overflow)| ScrollingBox::new(source, overflow))
}
pub(crate) fn text_index_query_on_node_for_event(
&self,
node: &Node,
mouse_event: &MouseEvent,
) -> Option<usize> {
let point_in_viewport = mouse_event.point_in_viewport()?.map(Au::from_f32_px);
self.layout_reflow(QueryMsg::TextIndexQuery);
self.layout
.borrow()
.query_text_index(node.to_trusted_node_address(), point_in_viewport)
}
pub(crate) fn elements_from_point_query(
&self,
point: LayoutPoint,
flags: ElementsFromPointFlags,
) -> Vec<ElementsFromPointResult> {
self.layout_reflow(QueryMsg::ElementsFromPoint);
self.layout().query_elements_from_point(point, flags)
}
pub(crate) fn query_effective_overflow(&self, node: &Node) -> Option<AxesOverflow> {
self.layout_reflow(QueryMsg::EffectiveOverflow);
self.query_effective_overflow_without_reflow(node)
}
pub(crate) fn query_effective_overflow_without_reflow(
&self,
node: &Node,
) -> Option<AxesOverflow> {
self.layout
.borrow()
.query_effective_overflow(node.to_trusted_node_address())
}
pub(crate) fn hit_test_from_input_event(
&self,
input_event: &ConstellationInputEvent,
) -> Option<HitTestResult> {
self.hit_test_from_point_in_viewport(
input_event.hit_test_result.as_ref()?.point_in_viewport,
)
}
#[expect(unsafe_code)]
pub(crate) fn hit_test_from_point_in_viewport(
&self,
point_in_frame: Point2D<f32, CSSPixel>,
) -> Option<HitTestResult> {
let result = self
.elements_from_point_query(point_in_frame.cast_unit(), ElementsFromPointFlags::empty())
.into_iter()
.nth(0)?;
let point_relative_to_initial_containing_block =
point_in_frame + self.scroll_offset().cast_unit();
let address = UntrustedNodeAddress(result.node.0 as *const c_void);
Some(HitTestResult {
node: unsafe { from_untrusted_node_address(address) },
cursor: result.cursor,
point_in_node: result.point_in_target,
point_in_frame,
point_relative_to_initial_containing_block,
})
}
pub(crate) fn init_window_proxy(&self, window_proxy: &WindowProxy) {
assert!(self.window_proxy.get().is_none());
self.window_proxy.set(Some(window_proxy));
}
pub(crate) fn init_document(&self, document: &Document) {
assert!(self.document.get().is_none());
assert!(document.window() == self);
self.document.set(Some(document));
}
pub(crate) fn load_data_for_document(
&self,
url: ServoUrl,
pipeline_id: PipelineId,
) -> LoadData {
let source_document = self.Document();
let secure_context = if self.is_top_level() {
None
} else {
Some(self.IsSecureContext())
};
LoadData::new(
LoadOrigin::Script(self.origin().snapshot()),
url,
source_document.about_base_url(),
Some(pipeline_id),
Referrer::ReferrerUrl(source_document.url()),
source_document.get_referrer_policy(),
secure_context,
Some(source_document.insecure_requests_policy()),
source_document.has_trustworthy_ancestor_origin(),
source_document.creation_sandboxing_flag_set_considering_parent_iframe(),
)
}
pub(crate) fn set_viewport_details(&self, viewport_details: ViewportDetails) {
self.viewport_details.set(viewport_details);
if !self.layout_mut().set_viewport_details(viewport_details) {
return;
}
self.Document()
.add_restyle_reason(RestyleReason::ViewportChanged);
}
pub(crate) fn viewport_details(&self) -> ViewportDetails {
self.viewport_details.get()
}
pub(crate) fn get_or_init_visual_viewport(&self, can_gc: CanGc) -> DomRoot<VisualViewport> {
self.visual_viewport.or_init(|| {
VisualViewport::new_from_layout_viewport(self, self.viewport_details().size, can_gc)
})
}
pub(crate) fn maybe_update_visual_viewport(
&self,
pinch_zoom_infos: PinchZoomInfos,
can_gc: CanGc,
) {
if pinch_zoom_infos.rect == Rect::from_size(self.viewport_details().size) &&
self.visual_viewport.get().is_none()
{
return;
}
let visual_viewport = self.get_or_init_visual_viewport(can_gc);
let changes = visual_viewport.update_from_pinch_zoom_infos(pinch_zoom_infos);
if changes.intersects(VisualViewportChanges::DimensionChanged) {
self.has_changed_visual_viewport_dimension.set(true);
}
if changes.intersects(VisualViewportChanges::OffsetChanged) {
visual_viewport.handle_scroll_event();
}
}
pub(crate) fn theme(&self) -> Theme {
self.theme.get()
}
pub(crate) fn set_theme(&self, new_theme: Theme) {
self.theme.set(new_theme);
if !self.layout_mut().set_theme(new_theme) {
return;
}
self.Document()
.add_restyle_reason(RestyleReason::ThemeChanged);
}
pub(crate) fn get_url(&self) -> ServoUrl {
self.Document().url()
}
pub(crate) fn windowproxy_handler(&self) -> &'static WindowProxyHandler {
self.dom_static.windowproxy_handler
}
pub(crate) fn add_resize_event(&self, event: ViewportDetails, event_type: WindowSizeType) {
*self.unhandled_resize_event.borrow_mut() = Some((event, event_type))
}
pub(crate) fn take_unhandled_resize_event(&self) -> Option<(ViewportDetails, WindowSizeType)> {
self.unhandled_resize_event.borrow_mut().take()
}
pub(crate) fn has_unhandled_resize_event(&self) -> bool {
self.unhandled_resize_event.borrow().is_some()
}
pub(crate) fn suspend(&self, cx: &mut js::context::JSContext) {
self.as_global_scope().suspend();
if self.window_proxy().currently_active() == Some(self.global().pipeline_id()) {
self.window_proxy().unset_currently_active(cx);
}
self.gc();
}
pub(crate) fn resume(&self, can_gc: CanGc) {
self.as_global_scope().resume();
self.window_proxy().set_currently_active(self, can_gc);
self.Document().title_changed();
}
pub(crate) fn need_emit_timeline_marker(&self, timeline_type: TimelineMarkerType) -> bool {
let markers = self.devtools_markers.borrow();
markers.contains(&timeline_type)
}
pub(crate) fn emit_timeline_marker(&self, marker: TimelineMarker) {
let sender = self.devtools_marker_sender.borrow();
let sender = sender.as_ref().expect("There is no marker sender");
sender.send(Some(marker)).unwrap();
}
pub(crate) fn set_devtools_timeline_markers(
&self,
markers: Vec<TimelineMarkerType>,
reply: GenericSender<Option<TimelineMarker>>,
) {
*self.devtools_marker_sender.borrow_mut() = Some(reply);
self.devtools_markers.borrow_mut().extend(markers);
}
pub(crate) fn drop_devtools_timeline_markers(&self, markers: Vec<TimelineMarkerType>) {
let mut devtools_markers = self.devtools_markers.borrow_mut();
for marker in markers {
devtools_markers.remove(&marker);
}
if devtools_markers.is_empty() {
*self.devtools_marker_sender.borrow_mut() = None;
}
}
pub(crate) fn set_webdriver_script_chan(&self, chan: Option<GenericSender<WebDriverJSResult>>) {
*self.webdriver_script_chan.borrow_mut() = chan;
}
pub(crate) fn set_webdriver_load_status_sender(
&self,
sender: Option<GenericSender<WebDriverLoadStatus>>,
) {
*self.webdriver_load_status_sender.borrow_mut() = sender;
}
pub(crate) fn webdriver_load_status_sender(
&self,
) -> Option<GenericSender<WebDriverLoadStatus>> {
self.webdriver_load_status_sender.borrow().clone()
}
pub(crate) fn is_alive(&self) -> bool {
self.current_state.get() == WindowState::Alive
}
pub(crate) fn is_top_level(&self) -> bool {
self.parent_info.is_none()
}
fn run_resize_steps_for_layout_viewport(&self, cx: &mut js::context::JSContext) -> bool {
let Some((new_size, size_type)) = self.take_unhandled_resize_event() else {
return false;
};
if self.viewport_details() == new_size {
return false;
}
let _realm = enter_realm(self);
debug!(
"Resizing Window for pipeline {:?} from {:?} to {new_size:?}",
self.pipeline_id(),
self.viewport_details(),
);
self.set_viewport_details(new_size);
self.Document()
.add_restyle_reason(RestyleReason::ViewportChanged);
if self.layout().device().used_viewport_size() {
self.Document().dirty_all_nodes();
}
if size_type == WindowSizeType::Resize {
let uievent = UIEvent::new(
self,
atom!("resize"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable,
Some(self),
0i32,
0u32,
CanGc::from_cx(cx),
);
uievent
.upcast::<Event>()
.fire(self.upcast(), CanGc::from_cx(cx));
}
true
}
pub(crate) fn run_the_resize_steps(&self, cx: &mut js::context::JSContext) -> bool {
let layout_viewport_resized = self.run_resize_steps_for_layout_viewport(cx);
if self.has_changed_visual_viewport_dimension.get() {
let visual_viewport = self.get_or_init_visual_viewport(CanGc::from_cx(cx));
let uievent = UIEvent::new(
self,
atom!("resize"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable,
Some(self),
0i32,
0u32,
CanGc::from_cx(cx),
);
uievent
.upcast::<Event>()
.fire(visual_viewport.upcast(), CanGc::from_cx(cx));
self.has_changed_visual_viewport_dimension.set(false);
}
layout_viewport_resized
}
pub(crate) fn evaluate_media_queries_and_report_changes(
&self,
cx: &mut js::context::JSContext,
) {
let mut realm = enter_auto_realm(cx, self);
let cx = &mut realm.current_realm();
rooted_vec!(let mut mql_list);
self.media_query_lists.for_each(|mql| {
if let MediaQueryListMatchState::Changed = mql.evaluate_changes() {
mql_list.push(Dom::from_ref(&*mql));
}
});
for mql in mql_list.iter() {
let event = MediaQueryListEvent::new(
&mql.global(),
atom!("change"),
false,
false,
mql.Media(),
mql.Matches(),
CanGc::from_cx(cx),
);
event
.upcast::<Event>()
.fire(mql.upcast::<EventTarget>(), CanGc::from_cx(cx));
}
}
pub(crate) fn set_throttled(&self, throttled: bool) {
self.throttled.set(throttled);
if throttled {
self.as_global_scope().slow_down_timers();
} else {
self.as_global_scope().speed_up_timers();
}
}
pub(crate) fn throttled(&self) -> bool {
self.throttled.get()
}
pub(crate) fn unminified_css_dir(&self) -> Option<String> {
self.unminified_css_dir.borrow().clone()
}
pub(crate) fn local_script_source(&self) -> &Option<String> {
&self.local_script_source
}
pub(crate) fn set_navigation_start(&self) {
self.navigation_start.set(CrossProcessInstant::now());
}
pub(crate) fn navigation_start(&self) -> CrossProcessInstant {
self.navigation_start.get()
}
pub(crate) fn set_last_activation_timestamp(&self, time: UserActivationTimestamp) {
self.last_activation_timestamp.set(time);
}
pub(crate) fn send_to_embedder(&self, msg: EmbedderMsg) {
self.as_global_scope()
.script_to_embedder_chan()
.send(msg)
.unwrap();
}
pub(crate) fn send_to_constellation(&self, msg: ScriptToConstellationMessage) {
self.as_global_scope()
.script_to_constellation_chan()
.send(msg)
.unwrap();
}
#[cfg(feature = "webxr")]
pub(crate) fn in_immersive_xr_session(&self) -> bool {
self.navigator
.get()
.as_ref()
.and_then(|nav| nav.xr())
.is_some_and(|xr| xr.pending_or_active_session())
}
#[cfg(not(feature = "webxr"))]
pub(crate) fn in_immersive_xr_session(&self) -> bool {
false
}
#[expect(unsafe_code)]
fn handle_pending_images_post_reflow(
&self,
pending_images: Vec<PendingImage>,
pending_rasterization_images: Vec<PendingRasterizationImage>,
pending_svg_element_for_serialization: Vec<UntrustedNodeAddress>,
) {
let pipeline_id = self.pipeline_id();
for image in pending_images {
let id = image.id;
let node = unsafe { from_untrusted_node_address(image.node) };
if let PendingImageState::Unrequested(ref url) = image.state {
fetch_image_for_layout(url.clone(), &node, id, self.image_cache.clone());
}
let mut images = self.pending_layout_images.borrow_mut();
if !images.contains_key(&id) {
let trusted_node = Trusted::new(&*node);
let sender = self.register_image_cache_listener(id, move |response, _| {
trusted_node
.root()
.owner_window()
.pending_layout_image_notification(response);
});
self.image_cache
.add_listener(ImageLoadListener::new(sender, pipeline_id, id));
}
let nodes = images.entry(id).or_default();
if !nodes.iter().any(|n| std::ptr::eq(&*(n.node), &*node)) {
nodes.push(PendingLayoutImageAncillaryData {
node: Dom::from_ref(&*node),
destination: image.destination,
});
}
}
for image in pending_rasterization_images {
let node = unsafe { from_untrusted_node_address(image.node) };
let mut images = self.pending_images_for_rasterization.borrow_mut();
if !images.contains_key(&(image.id, image.size)) {
let image_cache_sender = self.image_cache_sender.clone();
self.image_cache.add_rasterization_complete_listener(
pipeline_id,
image.id,
image.size,
Box::new(move |response| {
let _ = image_cache_sender.send(response);
}),
);
}
let nodes = images.entry((image.id, image.size)).or_default();
if !nodes.iter().any(|n| std::ptr::eq(&**n, &*node)) {
nodes.push(Dom::from_ref(&*node));
}
}
for node in pending_svg_element_for_serialization.into_iter() {
let node = unsafe { from_untrusted_node_address(node) };
let svg = node.downcast::<SVGSVGElement>().unwrap();
svg.serialize_and_cache_subtree();
node.dirty(NodeDamage::Other);
}
}
pub(crate) fn has_sticky_activation(&self) -> bool {
UserActivationTimestamp::TimeStamp(CrossProcessInstant::now()) >=
self.last_activation_timestamp.get()
}
pub(crate) fn has_transient_activation(&self) -> bool {
let current_time = CrossProcessInstant::now();
UserActivationTimestamp::TimeStamp(current_time) >= self.last_activation_timestamp.get() &&
UserActivationTimestamp::TimeStamp(current_time) <
self.last_activation_timestamp.get() +
pref!(dom_transient_activation_duration_ms)
}
pub(crate) fn consume_last_activation_timestamp(&self) {
if self.last_activation_timestamp.get() != UserActivationTimestamp::PositiveInfinity {
self.set_last_activation_timestamp(UserActivationTimestamp::NegativeInfinity);
}
}
pub(crate) fn consume_user_activation(&self) {
if self.undiscarded_window_proxy().is_none() {
return;
}
let Some(top_level_document) = self.top_level_document_if_local() else {
return;
};
top_level_document
.window()
.consume_last_activation_timestamp();
for document in SameOriginDescendantNavigablesIterator::new(top_level_document) {
document.window().consume_last_activation_timestamp();
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
cx: &mut js::context::JSContext,
webview_id: WebViewId,
runtime: Rc<Runtime>,
script_chan: Sender<MainThreadScriptMsg>,
layout: Box<dyn Layout>,
font_context: Arc<FontContext>,
image_cache_sender: Sender<ImageCacheResponseMessage>,
image_cache: Arc<dyn ImageCache>,
resource_threads: ResourceThreads,
storage_threads: StorageThreads,
#[cfg(feature = "bluetooth")] bluetooth_thread: GenericSender<BluetoothRequest>,
mem_profiler_chan: MemProfilerChan,
time_profiler_chan: TimeProfilerChan,
devtools_chan: Option<GenericCallback<ScriptToDevtoolsControlMsg>>,
constellation_chan: ScriptToConstellationChan,
embedder_chan: ScriptToEmbedderChan,
control_chan: GenericSender<ScriptThreadMessage>,
pipeline_id: PipelineId,
parent_info: Option<PipelineId>,
viewport_details: ViewportDetails,
origin: MutableOrigin,
creation_url: ServoUrl,
top_level_creation_url: ServoUrl,
navigation_start: CrossProcessInstant,
webgl_chan: Option<WebGLChan>,
#[cfg(feature = "webxr")] webxr_registry: Option<webxr_api::Registry>,
paint_api: CrossProcessPaintApi,
unminify_js: bool,
unminify_css: bool,
local_script_source: Option<String>,
user_scripts: Rc<Vec<UserScript>>,
player_context: WindowGLContext,
#[cfg(feature = "webgpu")] gpu_id_hub: Arc<IdentityHub>,
inherited_secure_context: Option<bool>,
theme: Theme,
weak_script_thread: Weak<ScriptThread>,
initial_https_state: HttpsState,
) -> DomRoot<Self> {
let error_reporter = CSSErrorReporter {
pipelineid: pipeline_id,
script_chan: control_chan,
};
let win = Box::new(Self {
webview_id,
globalscope: GlobalScope::new_inherited(
pipeline_id,
devtools_chan,
mem_profiler_chan,
time_profiler_chan,
constellation_chan,
embedder_chan,
resource_threads,
storage_threads,
origin,
creation_url,
Some(top_level_creation_url),
#[cfg(feature = "webgpu")]
gpu_id_hub,
inherited_secure_context,
unminify_js,
Some(font_context),
initial_https_state,
),
ongoing_navigation: Default::default(),
script_chan,
layout: RefCell::new(layout),
image_cache_sender,
image_cache,
navigator: Default::default(),
crypto: Default::default(),
location: Default::default(),
history: Default::default(),
custom_element_registry: Default::default(),
window_proxy: Default::default(),
document: Default::default(),
performance: Default::default(),
navigation_start: Cell::new(navigation_start),
screen: Default::default(),
session_storage: Default::default(),
local_storage: Default::default(),
cookie_store: Default::default(),
status: DomRefCell::new(DOMString::new()),
parent_info,
dom_static: GlobalStaticData::new(),
js_runtime: DomRefCell::new(Some(runtime)),
#[cfg(feature = "bluetooth")]
bluetooth_thread,
#[cfg(feature = "bluetooth")]
bluetooth_extra_permission_data: BluetoothExtraPermissionData::new(),
unhandled_resize_event: Default::default(),
viewport_details: Cell::new(viewport_details),
layout_blocker: Cell::new(LayoutBlocker::WaitingForParse),
current_state: Cell::new(WindowState::Alive),
devtools_marker_sender: Default::default(),
devtools_markers: Default::default(),
webdriver_script_chan: Default::default(),
webdriver_load_status_sender: Default::default(),
error_reporter,
media_query_lists: DOMTracker::new(),
#[cfg(feature = "bluetooth")]
test_runner: Default::default(),
webgl_chan,
#[cfg(feature = "webxr")]
webxr_registry,
pending_image_callbacks: Default::default(),
pending_layout_images: Default::default(),
pending_images_for_rasterization: Default::default(),
unminified_css_dir: DomRefCell::new(if unminify_css {
Some(unminified_path("unminified-css"))
} else {
None
}),
local_script_source,
test_worklet: Default::default(),
paint_worklet: Default::default(),
exists_mut_observer: Cell::new(false),
paint_api,
user_scripts,
player_context,
throttled: Cell::new(false),
layout_marker: DomRefCell::new(Rc::new(Cell::new(true))),
current_event: DomRefCell::new(None),
theme: Cell::new(theme),
trusted_types: Default::default(),
reporting_observer_list: Default::default(),
report_list: Default::default(),
endpoints_list: Default::default(),
script_window_proxies: ScriptThread::window_proxies(),
has_pending_screenshot_readiness_request: Default::default(),
visual_viewport: Default::default(),
weak_script_thread,
has_changed_visual_viewport_dimension: Default::default(),
last_activation_timestamp: Cell::new(UserActivationTimestamp::PositiveInfinity),
devtools_wants_updates: Default::default(),
});
WindowBinding::Wrap::<crate::DomTypeHolder>(cx, win)
}
pub(crate) fn pipeline_id(&self) -> PipelineId {
self.as_global_scope().pipeline_id()
}
pub(crate) fn live_devtools_updates(&self) -> bool {
self.devtools_wants_updates.get()
}
pub(crate) fn set_devtools_wants_updates(&self, value: bool) {
self.devtools_wants_updates.set(value);
}
pub(crate) fn cache_layout_value<T>(&self, value: T) -> LayoutValue<T>
where
T: Copy + MallocSizeOf,
{
LayoutValue::new(self.layout_marker.borrow().clone(), value)
}
}
#[derive(MallocSizeOf)]
pub(crate) struct LayoutValue<T: MallocSizeOf> {
#[conditional_malloc_size_of]
is_valid: Rc<Cell<bool>>,
value: T,
}
#[expect(unsafe_code)]
unsafe impl<T: JSTraceable + MallocSizeOf> JSTraceable for LayoutValue<T> {
unsafe fn trace(&self, trc: *mut js::jsapi::JSTracer) {
unsafe { self.value.trace(trc) };
}
}
impl<T: Copy + MallocSizeOf> LayoutValue<T> {
fn new(marker: Rc<Cell<bool>>, value: T) -> Self {
LayoutValue {
is_valid: marker,
value,
}
}
pub(crate) fn get(&self) -> Result<T, ()> {
if self.is_valid.get() {
return Ok(self.value);
}
Err(())
}
}
fn should_move_clip_rect(clip_rect: UntypedRect<Au>, new_viewport: UntypedRect<f32>) -> bool {
let clip_rect = UntypedRect::new(
Point2D::new(
clip_rect.origin.x.to_f32_px(),
clip_rect.origin.y.to_f32_px(),
),
Size2D::new(
clip_rect.size.width.to_f32_px(),
clip_rect.size.height.to_f32_px(),
),
);
static VIEWPORT_SCROLL_MARGIN_SIZE: f32 = 0.5;
let viewport_scroll_margin = new_viewport.size * VIEWPORT_SCROLL_MARGIN_SIZE;
(clip_rect.origin.x - new_viewport.origin.x).abs() <= viewport_scroll_margin.width ||
(clip_rect.max_x() - new_viewport.max_x()).abs() <= viewport_scroll_margin.width ||
(clip_rect.origin.y - new_viewport.origin.y).abs() <= viewport_scroll_margin.height ||
(clip_rect.max_y() - new_viewport.max_y()).abs() <= viewport_scroll_margin.height
}
impl Window {
pub(crate) fn post_message(
&self,
target_origin: Option<ImmutableOrigin>,
source_origin: ImmutableOrigin,
source: &WindowProxy,
data: StructuredSerializedData,
) {
let this = Trusted::new(self);
let source = Trusted::new(source);
let task = task!(post_serialised_message: move |cx| {
let this = this.root();
let source = source.root();
let document = this.Document();
if let Some(ref target_origin) = target_origin {
if !target_origin.same_origin(&*document.origin()) {
return;
}
}
let obj = this.reflector().get_jsobject();
let mut realm = AutoRealm::new(cx, NonNull::new(obj.get()).unwrap());
let cx = &mut *realm;
rooted!(&in(cx) let mut message_clone = UndefinedValue());
if let Ok(ports) = structuredclone::read(this.upcast(), data, message_clone.handle_mut(), CanGc::from_cx(cx)) {
MessageEvent::dispatch_jsval(
this.upcast(),
this.upcast(),
message_clone.handle(),
Some(&source_origin.ascii_serialization()),
Some(&*source),
ports,
CanGc::from_cx(cx),
);
} else {
MessageEvent::dispatch_error(
cx,
this.upcast(),
this.upcast(),
);
}
});
self.as_global_scope()
.task_manager()
.dom_manipulation_task_source()
.queue(task);
}
}
#[derive(Clone, MallocSizeOf)]
pub(crate) struct CSSErrorReporter {
pub(crate) pipelineid: PipelineId,
pub(crate) script_chan: GenericSender<ScriptThreadMessage>,
}
unsafe_no_jsmanaged_fields!(CSSErrorReporter);
impl ParseErrorReporter for CSSErrorReporter {
fn report_error(
&self,
url: &UrlExtraData,
location: SourceLocation,
error: ContextualParseError,
) {
if log_enabled!(log::Level::Info) {
info!(
"Url:\t{}\n{}:{} {}",
url.0.as_str(),
location.line,
location.column,
error
)
}
let _ = self.script_chan.send(ScriptThreadMessage::ReportCSSError(
self.pipelineid,
url.0.to_string(),
location.line,
location.column,
error.to_string(),
));
}
}
fn is_named_element_with_name_attribute(elem: &Element) -> bool {
let type_ = match elem.upcast::<Node>().type_id() {
NodeTypeId::Element(ElementTypeId::HTMLElement(type_)) => type_,
_ => return false,
};
matches!(
type_,
HTMLElementTypeId::HTMLEmbedElement |
HTMLElementTypeId::HTMLFormElement |
HTMLElementTypeId::HTMLImageElement |
HTMLElementTypeId::HTMLObjectElement
)
}
fn is_named_element_with_id_attribute(elem: &Element) -> bool {
elem.is_html_element()
}
#[expect(unsafe_code)]
#[unsafe(no_mangle)]
unsafe extern "C" fn dump_js_stack(cx: *mut RawJSContext) {
unsafe {
DumpJSStack(cx, true, false, false);
}
}
impl WindowHelpers for Window {
fn create_named_properties_object(
cx: SafeJSContext,
proto: HandleObject,
object: MutableHandleObject,
) {
Self::create_named_properties_object(cx, proto, object)
}
}