script/dom/
window.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::borrow::ToOwned;
6use std::cell::{Cell, RefCell, RefMut};
7use std::cmp;
8use std::collections::hash_map::Entry;
9use std::collections::{HashMap, HashSet};
10use std::default::Default;
11use std::ffi::c_void;
12use std::io::{Write, stderr, stdout};
13use std::rc::{Rc, Weak};
14use std::sync::Arc;
15use std::time::{Duration, Instant};
16
17use app_units::Au;
18use base::cross_process_instant::CrossProcessInstant;
19use base::generic_channel::{self, GenericCallback, GenericSender};
20use base::id::{BrowsingContextId, PipelineId, WebViewId};
21use base64::Engine;
22#[cfg(feature = "bluetooth")]
23use bluetooth_traits::BluetoothRequest;
24use canvas_traits::webgl::WebGLChan;
25use constellation_traits::{
26    LoadData, LoadOrigin, NavigationHistoryBehavior, ScreenshotReadinessResponse,
27    ScriptToConstellationChan, ScriptToConstellationMessage, StructuredSerializedData,
28    WindowSizeType,
29};
30use content_security_policy::Violation;
31use content_security_policy::sandboxing_directive::SandboxingFlagSet;
32use crossbeam_channel::{Sender, unbounded};
33use cssparser::SourceLocation;
34use devtools_traits::{ScriptToDevtoolsControlMsg, TimelineMarker, TimelineMarkerType};
35use dom_struct::dom_struct;
36use embedder_traits::user_contents::UserScript;
37use embedder_traits::{
38    AlertResponse, ConfirmResponse, EmbedderMsg, JavaScriptEvaluationError, PromptResponse,
39    ScriptToEmbedderChan, SimpleDialogRequest, Theme, UntrustedNodeAddress, ViewportDetails,
40    WebDriverJSResult, WebDriverLoadStatus,
41};
42use euclid::default::Rect as UntypedRect;
43use euclid::{Point2D, Rect, Scale, Size2D, Vector2D};
44use fonts::{CspViolationHandler, FontContext, NetworkTimingHandler, WebFontDocumentContext};
45use js::context::JSContext;
46use js::glue::DumpJSStack;
47use js::jsapi::{
48    GCReason, Heap, JS_GC, JSAutoRealm, JSContext as RawJSContext, JSObject, JSPROP_ENUMERATE,
49};
50use js::jsval::{NullValue, UndefinedValue};
51use js::realm::CurrentRealm;
52use js::rust::wrappers::JS_DefineProperty;
53use js::rust::{
54    CustomAutoRooter, CustomAutoRooterGuard, HandleObject, HandleValue, MutableHandleObject,
55    MutableHandleValue,
56};
57use layout_api::{
58    AxesOverflow, BoxAreaType, CSSPixelRectIterator, ElementsFromPointFlags,
59    ElementsFromPointResult, FragmentType, Layout, LayoutImageDestination, PendingImage,
60    PendingImageState, PendingRasterizationImage, PhysicalSides, QueryMsg, ReflowGoal,
61    ReflowPhasesRun, ReflowRequest, ReflowRequestRestyle, ReflowStatistics, RestyleReason,
62    ScrollContainerQueryFlags, ScrollContainerResponse, TrustedNodeAddress,
63    combine_id_with_fragment_type,
64};
65use malloc_size_of::MallocSizeOf;
66use media::WindowGLContext;
67use net_traits::image_cache::{
68    ImageCache, ImageCacheResponseCallback, ImageCacheResponseMessage, ImageLoadListener,
69    ImageResponse, PendingImageId, PendingImageResponse, RasterizationCompleteResponse,
70};
71use net_traits::request::Referrer;
72use net_traits::{ResourceFetchTiming, ResourceThreads};
73use num_traits::ToPrimitive;
74use paint_api::{CrossProcessPaintApi, PinchZoomInfos};
75use profile_traits::generic_channel as ProfiledGenericChannel;
76use profile_traits::mem::ProfilerChan as MemProfilerChan;
77use profile_traits::time::ProfilerChan as TimeProfilerChan;
78use rustc_hash::{FxBuildHasher, FxHashMap};
79use script_bindings::codegen::GenericBindings::WindowBinding::ScrollToOptions;
80use script_bindings::conversions::SafeToJSValConvertible;
81use script_bindings::interfaces::WindowHelpers;
82use script_bindings::root::Root;
83use script_traits::{ConstellationInputEvent, ScriptThreadMessage};
84use selectors::attr::CaseSensitivity;
85use servo_arc::Arc as ServoArc;
86use servo_config::pref;
87use servo_geometry::DeviceIndependentIntRect;
88use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
89use storage_traits::StorageThreads;
90use storage_traits::webstorage_thread::WebStorageType;
91use style::error_reporting::{ContextualParseError, ParseErrorReporter};
92use style::properties::PropertyId;
93use style::properties::style_structs::Font;
94use style::selector_parser::PseudoElement;
95use style::str::HTML_SPACE_CHARACTERS;
96use style::stylesheets::UrlExtraData;
97use style_traits::CSSPixel;
98use stylo_atoms::Atom;
99use url::Position;
100use webrender_api::ExternalScrollId;
101use webrender_api::units::{DeviceIntSize, DevicePixel, LayoutPixel, LayoutPoint};
102
103use super::bindings::codegen::Bindings::MessagePortBinding::StructuredSerializeOptions;
104use super::bindings::trace::HashMapTracedValues;
105use super::performanceresourcetiming::InitiatorType;
106use super::types::SVGSVGElement;
107use crate::dom::bindings::cell::{DomRefCell, Ref};
108use crate::dom::bindings::codegen::Bindings::DocumentBinding::{
109    DocumentMethods, DocumentReadyState, NamedPropertyValue,
110};
111use crate::dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElementMethods;
112use crate::dom::bindings::codegen::Bindings::HistoryBinding::History_Binding::HistoryMethods;
113use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
114    ImageBitmapOptions, ImageBitmapSource,
115};
116use crate::dom::bindings::codegen::Bindings::MediaQueryListBinding::MediaQueryList_Binding::MediaQueryListMethods;
117use crate::dom::bindings::codegen::Bindings::ReportingObserverBinding::Report;
118use crate::dom::bindings::codegen::Bindings::RequestBinding::{RequestInfo, RequestInit};
119use crate::dom::bindings::codegen::Bindings::VoidFunctionBinding::VoidFunction;
120use crate::dom::bindings::codegen::Bindings::WindowBinding::{
121    self, DeferredRequestInit, FrameRequestCallback, ScrollBehavior, WindowMethods,
122    WindowPostMessageOptions,
123};
124use crate::dom::bindings::codegen::UnionTypes::{
125    RequestOrUSVString, TrustedScriptOrString, TrustedScriptOrStringOrFunction,
126};
127use crate::dom::bindings::error::{
128    Error, ErrorInfo, ErrorResult, Fallible, javascript_error_info_from_error_info,
129};
130use crate::dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementTypeId, NodeTypeId};
131use crate::dom::bindings::num::Finite;
132use crate::dom::bindings::refcounted::Trusted;
133use crate::dom::bindings::reflector::{DomGlobal, DomObject};
134use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
135use crate::dom::bindings::str::{DOMString, USVString};
136use crate::dom::bindings::structuredclone;
137use crate::dom::bindings::trace::{CustomTraceable, JSTraceable, RootedTraceableBox};
138use crate::dom::bindings::utils::GlobalStaticData;
139use crate::dom::bindings::weakref::DOMTracker;
140#[cfg(feature = "bluetooth")]
141use crate::dom::bluetooth::BluetoothExtraPermissionData;
142use crate::dom::cookiestore::CookieStore;
143use crate::dom::crypto::Crypto;
144use crate::dom::csp::GlobalCspReporting;
145use crate::dom::css::cssstyledeclaration::{
146    CSSModificationAccess, CSSStyleDeclaration, CSSStyleOwner,
147};
148use crate::dom::customelementregistry::CustomElementRegistry;
149use crate::dom::document::{
150    AnimationFrameCallback, Document, SameOriginDescendantNavigablesIterator,
151};
152use crate::dom::element::Element;
153use crate::dom::event::{Event, EventBubbles, EventCancelable};
154use crate::dom::eventtarget::EventTarget;
155use crate::dom::fetchlaterresult::FetchLaterResult;
156use crate::dom::globalscope::GlobalScope;
157use crate::dom::history::History;
158use crate::dom::html::htmlcollection::{CollectionFilter, HTMLCollection};
159use crate::dom::html::htmliframeelement::HTMLIFrameElement;
160use crate::dom::idbfactory::IDBFactory;
161use crate::dom::inputevent::HitTestResult;
162use crate::dom::location::Location;
163use crate::dom::medialist::MediaList;
164use crate::dom::mediaquerylist::{MediaQueryList, MediaQueryListMatchState};
165use crate::dom::mediaquerylistevent::MediaQueryListEvent;
166use crate::dom::messageevent::MessageEvent;
167use crate::dom::navigator::Navigator;
168use crate::dom::node::{Node, NodeDamage, NodeTraits, from_untrusted_node_address};
169use crate::dom::performance::performance::Performance;
170use crate::dom::promise::Promise;
171use crate::dom::reporting::reportingendpoint::{ReportingEndpoint, SendReportsToEndpoints};
172use crate::dom::reporting::reportingobserver::ReportingObserver;
173use crate::dom::screen::Screen;
174use crate::dom::scrolling_box::{ScrollingBox, ScrollingBoxSource};
175use crate::dom::selection::Selection;
176use crate::dom::shadowroot::ShadowRoot;
177use crate::dom::storage::Storage;
178#[cfg(feature = "bluetooth")]
179use crate::dom::testrunner::TestRunner;
180use crate::dom::trustedtypes::trustedtypepolicyfactory::TrustedTypePolicyFactory;
181use crate::dom::types::{ImageBitmap, MouseEvent, UIEvent};
182use crate::dom::useractivation::UserActivationTimestamp;
183use crate::dom::visualviewport::{VisualViewport, VisualViewportChanges};
184use crate::dom::webgl::webglrenderingcontext::WebGLCommandSender;
185#[cfg(feature = "webgpu")]
186use crate::dom::webgpu::identityhub::IdentityHub;
187use crate::dom::windowproxy::{WindowProxy, WindowProxyHandler};
188use crate::dom::worklet::Worklet;
189use crate::dom::workletglobalscope::WorkletGlobalScopeType;
190use crate::layout_image::fetch_image_for_layout;
191use crate::messaging::{MainThreadScriptMsg, ScriptEventLoopReceiver, ScriptEventLoopSender};
192use crate::microtask::{Microtask, UserMicrotask};
193use crate::network_listener::{ResourceTimingListener, submit_timing};
194use crate::realms::enter_realm;
195use crate::script_runtime::{CanGc, JSContext as SafeJSContext, Runtime};
196use crate::script_thread::ScriptThread;
197use crate::script_window_proxies::ScriptWindowProxies;
198use crate::task_source::SendableTaskSource;
199use crate::timers::{IsInterval, TimerCallback};
200use crate::unminify::unminified_path;
201use crate::webdriver_handlers::{find_node_by_unique_id_in_document, jsval_to_webdriver};
202use crate::{fetch, window_named_properties};
203
204/// A callback to call when a response comes back from the `ImageCache`.
205///
206/// This is wrapped in a struct so that we can implement `MallocSizeOf`
207/// for this type.
208#[derive(MallocSizeOf)]
209pub struct PendingImageCallback(
210    #[ignore_malloc_size_of = "dyn Fn is currently impossible to measure"]
211    #[expect(clippy::type_complexity)]
212    Box<dyn Fn(PendingImageResponse, &mut js::context::JSContext) + 'static>,
213);
214
215/// Current state of the window object
216#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf, PartialEq)]
217enum WindowState {
218    Alive,
219    Zombie, // Pipeline is closed, but the window hasn't been GCed yet.
220}
221
222/// How long we should wait before performing the initial reflow after `<body>` is parsed,
223/// assuming that `<body>` take this long to parse.
224const INITIAL_REFLOW_DELAY: Duration = Duration::from_millis(200);
225
226/// During loading and parsing, layouts are suppressed to avoid flashing incomplete page
227/// contents.
228///
229/// Exceptions:
230///  - Parsing the body takes so long, that layouts are no longer suppressed in order
231///    to show the user that the page is loading.
232///  - Script triggers a layout query or scroll event in which case, we want to layout
233///    but not display the contents.
234///
235/// For more information see: <https://github.com/servo/servo/pull/6028>.
236#[derive(Clone, Copy, MallocSizeOf)]
237enum LayoutBlocker {
238    /// The first load event hasn't been fired and we have not started to parse the `<body>` yet.
239    WaitingForParse,
240    /// The body is being parsed the `<body>` starting at the `Instant` specified.
241    Parsing(Instant),
242    /// The body finished parsing and the `load` event has been fired or parsing took so
243    /// long, that we are going to do layout anyway. Note that subsequent changes to the body
244    /// can trigger parsing again, but the `Window` stays in this state.
245    FiredLoadEventOrParsingTimerExpired,
246}
247
248impl LayoutBlocker {
249    fn layout_blocked(&self) -> bool {
250        !matches!(self, Self::FiredLoadEventOrParsingTimerExpired)
251    }
252}
253
254/// An id used to cancel navigations; for now only used for planned form navigations.
255/// Loosely based on <https://html.spec.whatwg.org/multipage/#ongoing-navigation>.
256#[derive(Clone, Copy, Debug, Default, JSTraceable, MallocSizeOf, PartialEq)]
257pub(crate) struct OngoingNavigation(u32);
258
259type PendingImageRasterizationKey = (PendingImageId, DeviceIntSize);
260
261/// Ancillary data of pending image request that was initiated by layout during a reflow.
262/// This data is used to faciliate invalidating layout when the image data becomes available
263/// at some point in the future.
264#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
265#[derive(JSTraceable, MallocSizeOf)]
266struct PendingLayoutImageAncillaryData {
267    node: Dom<Node>,
268    #[no_trace]
269    destination: LayoutImageDestination,
270}
271
272#[dom_struct]
273pub(crate) struct Window {
274    globalscope: GlobalScope,
275
276    /// A `Weak` reference to this [`ScriptThread`] used to give to child [`Window`]s so
277    /// they can more easily call methods on the [`ScriptThread`] without constantly having
278    /// to pass it everywhere.
279    #[ignore_malloc_size_of = "Weak does not need to be accounted"]
280    #[no_trace]
281    weak_script_thread: Weak<ScriptThread>,
282
283    /// The webview that contains this [`Window`].
284    ///
285    /// This may not be the top-level [`Window`], in the case of frames.
286    #[no_trace]
287    webview_id: WebViewId,
288    script_chan: Sender<MainThreadScriptMsg>,
289    #[no_trace]
290    #[ignore_malloc_size_of = "TODO: Add MallocSizeOf support to layout"]
291    layout: RefCell<Box<dyn Layout>>,
292    navigator: MutNullableDom<Navigator>,
293    #[ignore_malloc_size_of = "ImageCache"]
294    #[no_trace]
295    image_cache: Arc<dyn ImageCache>,
296    #[no_trace]
297    image_cache_sender: Sender<ImageCacheResponseMessage>,
298    window_proxy: MutNullableDom<WindowProxy>,
299    document: MutNullableDom<Document>,
300    location: MutNullableDom<Location>,
301    history: MutNullableDom<History>,
302    custom_element_registry: MutNullableDom<CustomElementRegistry>,
303    performance: MutNullableDom<Performance>,
304    #[no_trace]
305    navigation_start: Cell<CrossProcessInstant>,
306    screen: MutNullableDom<Screen>,
307    session_storage: MutNullableDom<Storage>,
308    local_storage: MutNullableDom<Storage>,
309    status: DomRefCell<DOMString>,
310    trusted_types: MutNullableDom<TrustedTypePolicyFactory>,
311
312    /// The start of something resembling
313    /// <https://html.spec.whatwg.org/multipage/#ongoing-navigation>
314    ongoing_navigation: Cell<OngoingNavigation>,
315
316    /// For sending timeline markers. Will be ignored if
317    /// no devtools server
318    #[no_trace]
319    devtools_markers: DomRefCell<HashSet<TimelineMarkerType>>,
320    #[no_trace]
321    devtools_marker_sender: DomRefCell<Option<GenericSender<Option<TimelineMarker>>>>,
322
323    /// Most recent unhandled resize event, if any.
324    #[no_trace]
325    unhandled_resize_event: DomRefCell<Option<(ViewportDetails, WindowSizeType)>>,
326
327    /// Platform theme.
328    #[no_trace]
329    theme: Cell<Theme>,
330
331    /// Parent id associated with this page, if any.
332    #[no_trace]
333    parent_info: Option<PipelineId>,
334
335    /// Global static data related to the DOM.
336    dom_static: GlobalStaticData,
337
338    /// The JavaScript runtime.
339    #[conditional_malloc_size_of]
340    js_runtime: DomRefCell<Option<Rc<Runtime>>>,
341
342    /// The [`ViewportDetails`] of this [`Window`]'s frame.
343    #[no_trace]
344    viewport_details: Cell<ViewportDetails>,
345
346    /// A handle for communicating messages to the bluetooth thread.
347    #[no_trace]
348    #[cfg(feature = "bluetooth")]
349    bluetooth_thread: GenericSender<BluetoothRequest>,
350
351    #[cfg(feature = "bluetooth")]
352    bluetooth_extra_permission_data: BluetoothExtraPermissionData,
353
354    /// See the documentation for [`LayoutBlocker`]. Essentially, this flag prevents
355    /// layouts from happening before the first load event, apart from a few exceptional
356    /// cases.
357    #[no_trace]
358    layout_blocker: Cell<LayoutBlocker>,
359
360    /// A channel for communicating results of async scripts back to the webdriver server
361    #[no_trace]
362    webdriver_script_chan: DomRefCell<Option<GenericSender<WebDriverJSResult>>>,
363
364    /// A channel to notify webdriver if there is a navigation
365    #[no_trace]
366    webdriver_load_status_sender: RefCell<Option<GenericSender<WebDriverLoadStatus>>>,
367
368    /// The current state of the window object
369    current_state: Cell<WindowState>,
370
371    error_reporter: CSSErrorReporter,
372
373    /// All the MediaQueryLists we need to update
374    media_query_lists: DOMTracker<MediaQueryList>,
375
376    #[cfg(feature = "bluetooth")]
377    test_runner: MutNullableDom<TestRunner>,
378
379    /// A handle for communicating messages to the WebGL thread, if available.
380    #[ignore_malloc_size_of = "channels are hard"]
381    #[no_trace]
382    webgl_chan: Option<WebGLChan>,
383
384    #[ignore_malloc_size_of = "defined in webxr"]
385    #[no_trace]
386    #[cfg(feature = "webxr")]
387    webxr_registry: Option<webxr_api::Registry>,
388
389    /// When an element triggers an image load or starts watching an image load from the
390    /// `ImageCache` it adds an entry to this list. When those loads are triggered from
391    /// layout, they also add an etry to [`Self::pending_layout_images`].
392    #[no_trace]
393    pending_image_callbacks: DomRefCell<FxHashMap<PendingImageId, Vec<PendingImageCallback>>>,
394
395    /// All of the elements that have an outstanding image request that was
396    /// initiated by layout during a reflow. They are stored in the [`ScriptThread`]
397    /// to ensure that the element can be marked dirty when the image data becomes
398    /// available at some point in the future.
399    pending_layout_images: DomRefCell<
400        HashMapTracedValues<PendingImageId, Vec<PendingLayoutImageAncillaryData>, FxBuildHasher>,
401    >,
402
403    /// Vector images for which layout has intiated rasterization at a specific size
404    /// and whose results are not yet available. They are stored in the [`ScriptThread`]
405    /// so that the element can be marked dirty once the rasterization is completed.
406    pending_images_for_rasterization: DomRefCell<
407        HashMapTracedValues<PendingImageRasterizationKey, Vec<Dom<Node>>, FxBuildHasher>,
408    >,
409
410    /// Directory to store unminified css for this window if unminify-css
411    /// opt is enabled.
412    unminified_css_dir: DomRefCell<Option<String>>,
413
414    /// Directory with stored unminified scripts
415    local_script_source: Option<String>,
416
417    /// Worklets
418    test_worklet: MutNullableDom<Worklet>,
419    /// <https://drafts.css-houdini.org/css-paint-api-1/#paint-worklet>
420    paint_worklet: MutNullableDom<Worklet>,
421
422    /// Flag to identify whether mutation observers are present(true)/absent(false)
423    exists_mut_observer: Cell<bool>,
424
425    /// Cross-process access to `Paint`.
426    #[no_trace]
427    paint_api: CrossProcessPaintApi,
428
429    /// Indicate whether a SetDocumentStatus message has been sent after a reflow is complete.
430    /// It is used to avoid sending idle message more than once, which is unnecessary.
431    has_sent_idle_message: Cell<bool>,
432
433    /// Unminify Css.
434    unminify_css: bool,
435
436    /// The [`UserScript`]s added via `UserContentManager`. These are potentially shared with other
437    /// `WebView`s in this `ScriptThread`.
438    #[no_trace]
439    #[conditional_malloc_size_of]
440    user_scripts: Rc<Vec<UserScript>>,
441
442    /// Window's GL context from application
443    #[ignore_malloc_size_of = "defined in script_thread"]
444    #[no_trace]
445    player_context: WindowGLContext,
446
447    throttled: Cell<bool>,
448
449    /// A shared marker for the validity of any cached layout values. A value of true
450    /// indicates that any such values remain valid; any new layout that invalidates
451    /// those values will cause the marker to be set to false.
452    #[conditional_malloc_size_of]
453    layout_marker: DomRefCell<Rc<Cell<bool>>>,
454
455    /// <https://dom.spec.whatwg.org/#window-current-event>
456    current_event: DomRefCell<Option<Dom<Event>>>,
457
458    /// <https://w3c.github.io/reporting/#windoworworkerglobalscope-registered-reporting-observer-list>
459    reporting_observer_list: DomRefCell<Vec<DomRoot<ReportingObserver>>>,
460
461    /// <https://w3c.github.io/reporting/#windoworworkerglobalscope-reports>
462    report_list: DomRefCell<Vec<Report>>,
463
464    /// <https://w3c.github.io/reporting/#windoworworkerglobalscope-endpoints>
465    #[no_trace]
466    endpoints_list: DomRefCell<Vec<ReportingEndpoint>>,
467
468    /// The window proxies the script thread knows.
469    #[conditional_malloc_size_of]
470    script_window_proxies: Rc<ScriptWindowProxies>,
471
472    /// Whether or not this [`Window`] has a pending screenshot readiness request.
473    has_pending_screenshot_readiness_request: Cell<bool>,
474
475    /// Visual viewport interface that is associated to this [`Window`].
476    /// <https://drafts.csswg.org/cssom-view/#dom-window-visualviewport>
477    visual_viewport: MutNullableDom<VisualViewport>,
478
479    /// [`VisualViewport`] dimension changed and we need to process it on the next tick.
480    has_changed_visual_viewport_dimension: Cell<bool>,
481
482    /// <https://html.spec.whatwg.org/multipage/#last-activation-timestamp>
483    #[no_trace]
484    last_activation_timestamp: Cell<UserActivationTimestamp>,
485}
486
487impl Window {
488    pub(crate) fn script_thread(&self) -> Rc<ScriptThread> {
489        Weak::upgrade(&self.weak_script_thread)
490            .expect("Weak reference should always be upgradable when a ScriptThread is running")
491    }
492
493    pub(crate) fn webview_id(&self) -> WebViewId {
494        self.webview_id
495    }
496
497    pub(crate) fn as_global_scope(&self) -> &GlobalScope {
498        self.upcast::<GlobalScope>()
499    }
500
501    pub(crate) fn layout(&self) -> Ref<'_, Box<dyn Layout>> {
502        self.layout.borrow()
503    }
504
505    pub(crate) fn layout_mut(&self) -> RefMut<'_, Box<dyn Layout>> {
506        self.layout.borrow_mut()
507    }
508
509    pub(crate) fn get_exists_mut_observer(&self) -> bool {
510        self.exists_mut_observer.get()
511    }
512
513    pub(crate) fn set_exists_mut_observer(&self) {
514        self.exists_mut_observer.set(true);
515    }
516
517    #[expect(unsafe_code)]
518    pub(crate) fn clear_js_runtime_for_script_deallocation(&self) {
519        self.as_global_scope()
520            .remove_web_messaging_and_dedicated_workers_infra();
521        unsafe {
522            *self.js_runtime.borrow_for_script_deallocation() = None;
523            self.window_proxy.set(None);
524            self.current_state.set(WindowState::Zombie);
525            self.as_global_scope()
526                .task_manager()
527                .cancel_all_tasks_and_ignore_future_tasks();
528        }
529    }
530
531    /// A convenience method for
532    /// <https://html.spec.whatwg.org/multipage/#a-browsing-context-is-discarded>
533    pub(crate) fn discard_browsing_context(&self) {
534        let proxy = match self.window_proxy.get() {
535            Some(proxy) => proxy,
536            None => panic!("Discarding a BC from a window that has none"),
537        };
538        proxy.discard_browsing_context();
539        // Step 4 of https://html.spec.whatwg.org/multipage/#discard-a-document
540        // Other steps performed when the `PipelineExit` message
541        // is handled by the ScriptThread.
542        self.as_global_scope()
543            .task_manager()
544            .cancel_all_tasks_and_ignore_future_tasks();
545    }
546
547    /// Get a sender to the time profiler thread.
548    pub(crate) fn time_profiler_chan(&self) -> &TimeProfilerChan {
549        self.globalscope.time_profiler_chan()
550    }
551
552    pub(crate) fn origin(&self) -> &MutableOrigin {
553        self.globalscope.origin()
554    }
555
556    #[expect(unsafe_code)]
557    pub(crate) fn get_cx(&self) -> SafeJSContext {
558        unsafe { SafeJSContext::from_ptr(js::rust::Runtime::get().unwrap().as_ptr()) }
559    }
560
561    pub(crate) fn get_js_runtime(&self) -> Ref<'_, Option<Rc<Runtime>>> {
562        self.js_runtime.borrow()
563    }
564
565    pub(crate) fn main_thread_script_chan(&self) -> &Sender<MainThreadScriptMsg> {
566        &self.script_chan
567    }
568
569    pub(crate) fn parent_info(&self) -> Option<PipelineId> {
570        self.parent_info
571    }
572
573    pub(crate) fn new_script_pair(&self) -> (ScriptEventLoopSender, ScriptEventLoopReceiver) {
574        let (sender, receiver) = unbounded();
575        (
576            ScriptEventLoopSender::MainThread(sender),
577            ScriptEventLoopReceiver::MainThread(receiver),
578        )
579    }
580
581    pub(crate) fn event_loop_sender(&self) -> ScriptEventLoopSender {
582        ScriptEventLoopSender::MainThread(self.script_chan.clone())
583    }
584
585    pub(crate) fn image_cache(&self) -> Arc<dyn ImageCache> {
586        self.image_cache.clone()
587    }
588
589    /// This can panic if it is called after the browsing context has been discarded
590    pub(crate) fn window_proxy(&self) -> DomRoot<WindowProxy> {
591        self.window_proxy.get().unwrap()
592    }
593
594    pub(crate) fn append_reporting_observer(&self, reporting_observer: DomRoot<ReportingObserver>) {
595        self.reporting_observer_list
596            .borrow_mut()
597            .push(reporting_observer);
598    }
599
600    pub(crate) fn remove_reporting_observer(&self, reporting_observer: &ReportingObserver) {
601        let index = {
602            let list = self.reporting_observer_list.borrow();
603            list.iter()
604                .position(|observer| &**observer == reporting_observer)
605        };
606
607        if let Some(index) = index {
608            self.reporting_observer_list.borrow_mut().remove(index);
609        }
610    }
611
612    pub(crate) fn registered_reporting_observers(&self) -> Vec<DomRoot<ReportingObserver>> {
613        self.reporting_observer_list.borrow().clone()
614    }
615
616    pub(crate) fn append_report(&self, report: Report) {
617        self.report_list.borrow_mut().push(report);
618        let trusted_window = Trusted::new(self);
619        self.upcast::<GlobalScope>()
620            .task_manager()
621            .dom_manipulation_task_source()
622            .queue(task!(send_to_reporting_endpoints: move || {
623                let window = trusted_window.root();
624                let reports = std::mem::take(&mut *window.report_list.borrow_mut());
625                window.upcast::<GlobalScope>().send_reports_to_endpoints(
626                    reports,
627                    window.endpoints_list.borrow().clone(),
628                );
629            }));
630    }
631
632    pub(crate) fn buffered_reports(&self) -> Vec<Report> {
633        self.report_list.borrow().clone()
634    }
635
636    pub(crate) fn set_endpoints_list(&self, endpoints: Vec<ReportingEndpoint>) {
637        *self.endpoints_list.borrow_mut() = endpoints;
638    }
639
640    /// Returns the window proxy if it has not been discarded.
641    /// <https://html.spec.whatwg.org/multipage/#a-browsing-context-is-discarded>
642    pub(crate) fn undiscarded_window_proxy(&self) -> Option<DomRoot<WindowProxy>> {
643        self.window_proxy.get().and_then(|window_proxy| {
644            if window_proxy.is_browsing_context_discarded() {
645                None
646            } else {
647                Some(window_proxy)
648            }
649        })
650    }
651
652    /// Get the active [`Document`] of top-level browsing context, or return [`Window`]'s [`Document`]
653    /// if it's browing context is the top-level browsing context. Returning none if the [`WindowProxy`]
654    /// is discarded or the [`Document`] is in another `ScriptThread`.
655    /// <https://html.spec.whatwg.org/multipage/#top-level-browsing-context>
656    pub(crate) fn top_level_document_if_local(&self) -> Option<DomRoot<Document>> {
657        if self.is_top_level() {
658            return Some(self.Document());
659        }
660
661        let window_proxy = self.undiscarded_window_proxy()?;
662        self.script_window_proxies
663            .find_window_proxy(window_proxy.webview_id().into())?
664            .document()
665    }
666
667    #[cfg(feature = "bluetooth")]
668    pub(crate) fn bluetooth_thread(&self) -> GenericSender<BluetoothRequest> {
669        self.bluetooth_thread.clone()
670    }
671
672    #[cfg(feature = "bluetooth")]
673    pub(crate) fn bluetooth_extra_permission_data(&self) -> &BluetoothExtraPermissionData {
674        &self.bluetooth_extra_permission_data
675    }
676
677    pub(crate) fn css_error_reporter(&self) -> &CSSErrorReporter {
678        &self.error_reporter
679    }
680
681    pub(crate) fn webgl_chan(&self) -> Option<WebGLCommandSender> {
682        self.webgl_chan
683            .as_ref()
684            .map(|chan| WebGLCommandSender::new(chan.clone()))
685    }
686
687    #[cfg(feature = "webxr")]
688    pub(crate) fn webxr_registry(&self) -> Option<webxr_api::Registry> {
689        self.webxr_registry.clone()
690    }
691
692    fn new_paint_worklet(&self, can_gc: CanGc) -> DomRoot<Worklet> {
693        debug!("Creating new paint worklet.");
694        Worklet::new(self, WorkletGlobalScopeType::Paint, can_gc)
695    }
696
697    pub(crate) fn register_image_cache_listener(
698        &self,
699        id: PendingImageId,
700        callback: impl Fn(PendingImageResponse, &mut js::context::JSContext) + 'static,
701    ) -> ImageCacheResponseCallback {
702        self.pending_image_callbacks
703            .borrow_mut()
704            .entry(id)
705            .or_default()
706            .push(PendingImageCallback(Box::new(callback)));
707
708        let image_cache_sender = self.image_cache_sender.clone();
709        Box::new(move |message| {
710            let _ = image_cache_sender.send(message);
711        })
712    }
713
714    fn pending_layout_image_notification(&self, response: PendingImageResponse) {
715        let mut images = self.pending_layout_images.borrow_mut();
716        let nodes = images.entry(response.id);
717        let nodes = match nodes {
718            Entry::Occupied(nodes) => nodes,
719            Entry::Vacant(_) => return,
720        };
721        if matches!(
722            response.response,
723            ImageResponse::Loaded(_, _) | ImageResponse::FailedToLoadOrDecode
724        ) {
725            for ancillary_data in nodes.get() {
726                match ancillary_data.destination {
727                    LayoutImageDestination::BoxTreeConstruction => {
728                        ancillary_data.node.dirty(NodeDamage::Other);
729                    },
730                    LayoutImageDestination::DisplayListBuilding => {
731                        self.layout().set_needs_new_display_list();
732                    },
733                }
734            }
735        }
736
737        match response.response {
738            ImageResponse::MetadataLoaded(_) => {},
739            ImageResponse::Loaded(_, _) | ImageResponse::FailedToLoadOrDecode => {
740                nodes.remove();
741            },
742        }
743    }
744
745    pub(crate) fn handle_image_rasterization_complete_notification(
746        &self,
747        response: RasterizationCompleteResponse,
748    ) {
749        let mut images = self.pending_images_for_rasterization.borrow_mut();
750        let nodes = images.entry((response.image_id, response.requested_size));
751        let nodes = match nodes {
752            Entry::Occupied(nodes) => nodes,
753            Entry::Vacant(_) => return,
754        };
755        for node in nodes.get() {
756            node.dirty(NodeDamage::Other);
757        }
758        nodes.remove();
759    }
760
761    pub(crate) fn pending_image_notification(
762        &self,
763        response: PendingImageResponse,
764        cx: &mut js::context::JSContext,
765    ) {
766        // We take the images here, in order to prevent maintaining a mutable borrow when
767        // image callbacks are called. These, in turn, can trigger garbage collection.
768        // Normally this shouldn't trigger more pending image notifications, but just in
769        // case we do not want to cause a double borrow here.
770        let mut images = std::mem::take(&mut *self.pending_image_callbacks.borrow_mut());
771        let Entry::Occupied(callbacks) = images.entry(response.id) else {
772            let _ = std::mem::replace(&mut *self.pending_image_callbacks.borrow_mut(), images);
773            return;
774        };
775
776        for callback in callbacks.get() {
777            callback.0(response.clone(), cx);
778        }
779
780        match response.response {
781            ImageResponse::MetadataLoaded(_) => {},
782            ImageResponse::Loaded(_, _) | ImageResponse::FailedToLoadOrDecode => {
783                callbacks.remove();
784            },
785        }
786
787        let _ = std::mem::replace(&mut *self.pending_image_callbacks.borrow_mut(), images);
788    }
789
790    pub(crate) fn paint_api(&self) -> &CrossProcessPaintApi {
791        &self.paint_api
792    }
793
794    pub(crate) fn userscripts(&self) -> &[UserScript] {
795        &self.user_scripts
796    }
797
798    pub(crate) fn get_player_context(&self) -> WindowGLContext {
799        self.player_context.clone()
800    }
801
802    // see note at https://dom.spec.whatwg.org/#concept-event-dispatch step 2
803    pub(crate) fn dispatch_event_with_target_override(&self, event: &Event, can_gc: CanGc) {
804        event.dispatch(self.upcast(), true, can_gc);
805    }
806
807    pub(crate) fn font_context(&self) -> &Arc<FontContext> {
808        self.as_global_scope()
809            .font_context()
810            .expect("A `Window` should always have a `FontContext`")
811    }
812
813    pub(crate) fn ongoing_navigation(&self) -> OngoingNavigation {
814        self.ongoing_navigation.get()
815    }
816
817    /// <https://html.spec.whatwg.org/multipage/#set-the-ongoing-navigation>
818    pub(crate) fn set_ongoing_navigation(&self) -> OngoingNavigation {
819        // Note: since this value, for now, is only used in a single `ScriptThread`,
820        // we just increment it (it is not a uuid), which implies not
821        // using a `newValue` variable.
822        let new_value = self.ongoing_navigation.get().0.wrapping_add(1);
823
824        // 1. If navigable's ongoing navigation is equal to newValue, then return.
825        // Note: cannot happen in the way it is currently used.
826
827        // TODO: 2. Inform the navigation API about aborting navigation given navigable.
828
829        // 3. Set navigable's ongoing navigation to newValue.
830        self.ongoing_navigation.set(OngoingNavigation(new_value));
831
832        // Note: Return the ongoing navigation for the caller to use.
833        OngoingNavigation(new_value)
834    }
835
836    /// <https://html.spec.whatwg.org/multipage/#nav-stop>
837    fn stop_loading(&self, cx: &mut js::context::JSContext) {
838        // 1. Let document be navigable's active document.
839        let doc = self.Document();
840
841        // 2. If document's unload counter is 0,
842        // and navigable's ongoing navigation is a navigation ID,
843        // then set the ongoing navigation for navigable to null.
844        //
845        // Note: since the concept of `navigable` is nascent in Servo,
846        // for now we do two things:
847        // - increment the `ongoing_navigation`(preventing planned form navigations).
848        // - Send a `AbortLoadUrl` message(in case the navigation
849        // already started at the constellation).
850        self.set_ongoing_navigation();
851
852        // 3. Abort a document and its descendants given document.
853        doc.abort(cx);
854    }
855
856    /// <https://html.spec.whatwg.org/multipage/#destroy-a-top-level-traversable>
857    fn destroy_top_level_traversable(&self, cx: &mut js::context::JSContext) {
858        // Step 1. Let browsingContext be traversable's active browsing context.
859        // TODO
860        // Step 2. For each historyEntry in traversable's session history entries:
861        // TODO
862        // Step 2.1. Let document be historyEntry's document.
863        let document = self.Document();
864        // Step 2.2. If document is not null, then destroy a document and its descendants given document.
865        document.destroy_document_and_its_descendants(cx);
866        // Step 3-6.
867        self.send_to_constellation(ScriptToConstellationMessage::DiscardTopLevelBrowsingContext);
868    }
869
870    /// <https://html.spec.whatwg.org/multipage/#definitely-close-a-top-level-traversable>
871    fn definitely_close(&self, cx: &mut js::context::JSContext) {
872        let document = self.Document();
873        // Step 1. Let toUnload be traversable's active document's inclusive descendant navigables.
874        //
875        // Implemented by passing `false` into the method below
876        // Step 2. If the result of checking if unloading is canceled for toUnload is not "continue", then return.
877        if !document.check_if_unloading_is_cancelled(false, CanGc::from_cx(cx)) {
878            return;
879        }
880        // Step 3. Append the following session history traversal steps to traversable:
881        // TODO
882        // Step 3.2. Unload a document and its descendants given traversable's active document, null, and afterAllUnloads.
883        document.unload(false, CanGc::from_cx(cx));
884        // Step 3.1. Let afterAllUnloads be an algorithm step which destroys traversable.
885        self.destroy_top_level_traversable(cx);
886    }
887
888    /// <https://html.spec.whatwg.org/multipage/#cannot-show-simple-dialogs>
889    fn cannot_show_simple_dialogs(&self) -> bool {
890        // Step 1: If the active sandboxing flag set of window's associated Document has
891        // the sandboxed modals flag set, then return true.
892        if self
893            .Document()
894            .has_active_sandboxing_flag(SandboxingFlagSet::SANDBOXED_MODALS_FLAG)
895        {
896            return true;
897        }
898
899        // Step 2: If window's relevant settings object's origin and window's relevant settings
900        // object's top-level origin are not same origin-domain, then return true.
901        //
902        // TODO: This check doesn't work currently because it seems that comparing two
903        // opaque domains doesn't work between GlobalScope::top_level_creation_url and
904        // Document::origin().
905
906        // Step 3: If window's relevant agent's event loop's termination nesting level is nonzero,
907        // then optionally return true.
908        // TODO: This is unsupported currently.
909
910        // Step 4: Optionally, return true. (For example, the user agent might give the
911        // user the option to ignore all modal dialogs, and would thus abort at this step
912        // whenever the method was invoked.)
913        // TODO: The embedder currently cannot block an alert before it is sent to the embedder. This
914        // requires changes to the API.
915
916        // Step 5: Return false.
917        false
918    }
919
920    pub(crate) fn perform_a_microtask_checkpoint(&self, cx: &mut js::context::JSContext) {
921        self.script_thread().perform_a_microtask_checkpoint(cx);
922    }
923
924    pub(crate) fn web_font_context(&self) -> WebFontDocumentContext {
925        let global = self.as_global_scope();
926        WebFontDocumentContext {
927            policy_container: global.policy_container(),
928            request_client: global.request_client(),
929            document_url: global.api_base_url(),
930            has_trustworthy_ancestor_origin: global.has_trustworthy_ancestor_origin(),
931            insecure_requests_policy: global.insecure_requests_policy(),
932            csp_handler: Box::new(FontCspHandler {
933                global: Trusted::new(global),
934                task_source: global
935                    .task_manager()
936                    .dom_manipulation_task_source()
937                    .to_sendable(),
938            }),
939            network_timing_handler: Box::new(FontNetworkTimingHandler {
940                global: Trusted::new(global),
941                task_source: global
942                    .task_manager()
943                    .dom_manipulation_task_source()
944                    .to_sendable(),
945            }),
946        }
947    }
948
949    #[expect(unsafe_code)]
950    pub(crate) fn gc(&self) {
951        unsafe {
952            JS_GC(*self.get_cx(), GCReason::API);
953        }
954    }
955}
956
957#[derive(Debug)]
958struct FontCspHandler {
959    global: Trusted<GlobalScope>,
960    task_source: SendableTaskSource,
961}
962
963impl CspViolationHandler for FontCspHandler {
964    fn process_violations(&self, violations: Vec<Violation>) {
965        let global = self.global.clone();
966        self.task_source.queue(task!(csp_violation: move || {
967            global.root().report_csp_violations(violations, None, None);
968        }));
969    }
970
971    fn clone(&self) -> Box<dyn CspViolationHandler> {
972        Box::new(Self {
973            global: self.global.clone(),
974            task_source: self.task_source.clone(),
975        })
976    }
977}
978
979#[derive(Debug)]
980struct FontNetworkTimingHandler {
981    global: Trusted<GlobalScope>,
982    task_source: SendableTaskSource,
983}
984
985impl NetworkTimingHandler for FontNetworkTimingHandler {
986    fn submit_timing(&self, url: ServoUrl, response: ResourceFetchTiming) {
987        let global = self.global.clone();
988        self.task_source.queue(task!(network_timing: move || {
989            submit_timing(
990                &FontFetchListener {
991                    url,
992                    global
993                },
994                &Ok(()),
995                &response,
996                CanGc::note(),
997            );
998        }));
999    }
1000
1001    fn clone(&self) -> Box<dyn NetworkTimingHandler> {
1002        Box::new(Self {
1003            global: self.global.clone(),
1004            task_source: self.task_source.clone(),
1005        })
1006    }
1007}
1008
1009#[derive(Debug)]
1010struct FontFetchListener {
1011    global: Trusted<GlobalScope>,
1012    url: ServoUrl,
1013}
1014
1015impl ResourceTimingListener for FontFetchListener {
1016    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
1017        (InitiatorType::Css, self.url.clone())
1018    }
1019
1020    fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
1021        self.global.root()
1022    }
1023}
1024
1025// https://html.spec.whatwg.org/multipage/#atob
1026pub(crate) fn base64_btoa(input: DOMString) -> Fallible<DOMString> {
1027    // "The btoa() method must throw an InvalidCharacterError exception if
1028    //  the method's first argument contains any character whose code point
1029    //  is greater than U+00FF."
1030    if input.str().chars().any(|c: char| c > '\u{FF}') {
1031        Err(Error::InvalidCharacter(None))
1032    } else {
1033        // "Otherwise, the user agent must convert that argument to a
1034        //  sequence of octets whose nth octet is the eight-bit
1035        //  representation of the code point of the nth character of
1036        //  the argument,"
1037        let octets = input
1038            .str()
1039            .chars()
1040            .map(|c: char| c as u8)
1041            .collect::<Vec<u8>>();
1042
1043        // "and then must apply the base64 algorithm to that sequence of
1044        //  octets, and return the result. [RFC4648]"
1045        let config =
1046            base64::engine::general_purpose::GeneralPurposeConfig::new().with_encode_padding(true);
1047        let engine = base64::engine::GeneralPurpose::new(&base64::alphabet::STANDARD, config);
1048        Ok(DOMString::from(engine.encode(octets)))
1049    }
1050}
1051
1052// https://html.spec.whatwg.org/multipage/#atob
1053pub(crate) fn base64_atob(input: DOMString) -> Fallible<DOMString> {
1054    // "Remove all space characters from input."
1055    fn is_html_space(c: char) -> bool {
1056        HTML_SPACE_CHARACTERS.contains(&c)
1057    }
1058    let without_spaces = input
1059        .str()
1060        .chars()
1061        .filter(|&c| !is_html_space(c))
1062        .collect::<String>();
1063    let mut input = &*without_spaces;
1064
1065    // "If the length of input divides by 4 leaving no remainder, then:
1066    //  if input ends with one or two U+003D EQUALS SIGN (=) characters,
1067    //  remove them from input."
1068    if input.len() % 4 == 0 {
1069        if input.ends_with("==") {
1070            input = &input[..input.len() - 2]
1071        } else if input.ends_with('=') {
1072            input = &input[..input.len() - 1]
1073        }
1074    }
1075
1076    // "If the length of input divides by 4 leaving a remainder of 1,
1077    //  throw an InvalidCharacterError exception and abort these steps."
1078    if input.len() % 4 == 1 {
1079        return Err(Error::InvalidCharacter(None));
1080    }
1081
1082    // "If input contains a character that is not in the following list of
1083    //  characters and character ranges, throw an InvalidCharacterError
1084    //  exception and abort these steps:
1085    //
1086    //  U+002B PLUS SIGN (+)
1087    //  U+002F SOLIDUS (/)
1088    //  Alphanumeric ASCII characters"
1089    if input
1090        .chars()
1091        .any(|c| c != '+' && c != '/' && !c.is_alphanumeric())
1092    {
1093        return Err(Error::InvalidCharacter(None));
1094    }
1095
1096    let config = base64::engine::general_purpose::GeneralPurposeConfig::new()
1097        .with_decode_padding_mode(base64::engine::DecodePaddingMode::RequireNone)
1098        .with_decode_allow_trailing_bits(true);
1099    let engine = base64::engine::GeneralPurpose::new(&base64::alphabet::STANDARD, config);
1100
1101    let data = engine
1102        .decode(input)
1103        .map_err(|_| Error::InvalidCharacter(None))?;
1104    Ok(data.iter().map(|&b| b as char).collect::<String>().into())
1105}
1106
1107impl WindowMethods<crate::DomTypeHolder> for Window {
1108    /// <https://html.spec.whatwg.org/multipage/#dom-alert>
1109    fn Alert_(&self) {
1110        // Step 2: If the method was invoked with no arguments, then let message be the
1111        // empty string; otherwise, let message be the method's first argument.
1112        self.Alert(DOMString::new());
1113    }
1114
1115    /// <https://html.spec.whatwg.org/multipage/#dom-alert>
1116    fn Alert(&self, mut message: DOMString) {
1117        // Step 1: If we cannot show simple dialogs for this, then return.
1118        if self.cannot_show_simple_dialogs() {
1119            return;
1120        }
1121
1122        // Step 2 is handled in the other variant of this method.
1123        //
1124        // Step 3: Set message to the result of normalizing newlines given message.
1125        message.normalize_newlines();
1126
1127        // Step 4. Set message to the result of optionally truncating message.
1128        // This is up to the embedder.
1129
1130        // Step 5: Let userPromptHandler be WebDriver BiDi user prompt opened with this,
1131        // "alert", and message.
1132        // TODO: Add support for WebDriver BiDi.
1133
1134        // Step 6: If userPromptHandler is "none", then:
1135        //  1. Show message to the user, treating U+000A LF as a line break.
1136        //  2. Optionally, pause while waiting for the user to acknowledge the message.
1137        {
1138            // Print to the console.
1139            // Ensure that stderr doesn't trample through the alert() we use to
1140            // communicate test results (see executorservo.py in wptrunner).
1141            let stderr = stderr();
1142            let mut stderr = stderr.lock();
1143            let stdout = stdout();
1144            let mut stdout = stdout.lock();
1145            writeln!(&mut stdout, "\nALERT: {message}").unwrap();
1146            stdout.flush().unwrap();
1147            stderr.flush().unwrap();
1148        }
1149
1150        let (sender, receiver) =
1151            ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
1152        let dialog = SimpleDialogRequest::Alert {
1153            id: self.Document().embedder_controls().next_control_id(),
1154            message: message.to_string(),
1155            response_sender: sender,
1156        };
1157        self.send_to_embedder(EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog));
1158        receiver.recv().unwrap_or_else(|_| {
1159            // If the receiver is closed, we assume the dialog was cancelled.
1160            debug!("Alert dialog was cancelled or failed to show.");
1161            AlertResponse::Ok
1162        });
1163
1164        // Step 7: Invoke WebDriver BiDi user prompt closed with this, "alert", and true.
1165        // TODO: Implement support for WebDriver BiDi.
1166    }
1167
1168    /// <https://html.spec.whatwg.org/multipage/#dom-confirm>
1169    fn Confirm(&self, mut message: DOMString) -> bool {
1170        // Step 1: If we cannot show simple dialogs for this, then return false.
1171        if self.cannot_show_simple_dialogs() {
1172            return false;
1173        }
1174
1175        // Step 2: Set message to the result of normalizing newlines given message.
1176        message.normalize_newlines();
1177
1178        // Step 3: Set message to the result of optionally truncating message.
1179        // We let the embedder handle this.
1180
1181        // Step 4: Show message to the user, treating U+000A LF as a line break, and ask
1182        // the user to respond with a positive or negative response.
1183        let (sender, receiver) =
1184            ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
1185        let dialog = SimpleDialogRequest::Confirm {
1186            id: self.Document().embedder_controls().next_control_id(),
1187            message: message.to_string(),
1188            response_sender: sender,
1189        };
1190        self.send_to_embedder(EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog));
1191
1192        // Step 5: Let userPromptHandler be WebDriver BiDi user prompt opened with this,
1193        // "confirm", and message.
1194        //
1195        // Step 6: Let accepted be false.
1196        //
1197        // Step 7: If userPromptHandler is "none", then:
1198        //  1. Pause until the user responds either positively or negatively.
1199        //  2. If the user responded positively, then set accepted to true.
1200        //
1201        // Step 8: If userPromptHandler is "accept", then set accepted to true.
1202        //
1203        // Step 9: Invoke WebDriver BiDi user prompt closed with this, "confirm", and accepted.
1204        // TODO: Implement WebDriver BiDi and handle these steps.
1205        //
1206        // Step 10: Return accepted.
1207        match receiver.recv() {
1208            Ok(ConfirmResponse::Ok) => true,
1209            Ok(ConfirmResponse::Cancel) => false,
1210            Err(_) => {
1211                warn!("Confirm dialog was cancelled or failed to show.");
1212                false
1213            },
1214        }
1215    }
1216
1217    /// <https://html.spec.whatwg.org/multipage/#dom-prompt>
1218    fn Prompt(&self, mut message: DOMString, default: DOMString) -> Option<DOMString> {
1219        // Step 1: If we cannot show simple dialogs for this, then return null.
1220        if self.cannot_show_simple_dialogs() {
1221            return None;
1222        }
1223
1224        // Step 2: Set message to the result of normalizing newlines given message.
1225        message.normalize_newlines();
1226
1227        // Step 3. Set message to the result of optionally truncating message.
1228        // Step 4: Set default to the result of optionally truncating default.
1229        // We let the embedder handle these steps.
1230
1231        // Step 5: Show message to the user, treating U+000A LF as a line break, and ask
1232        // the user to either respond with a string value or abort. The response must be
1233        // defaulted to the value given by default.
1234        let (sender, receiver) =
1235            ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
1236        let dialog = SimpleDialogRequest::Prompt {
1237            id: self.Document().embedder_controls().next_control_id(),
1238            message: message.to_string(),
1239            default: default.to_string(),
1240            response_sender: sender,
1241        };
1242        self.send_to_embedder(EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog));
1243
1244        // Step 6: Let userPromptHandler be WebDriver BiDi user prompt opened with this,
1245        // "prompt", and message.
1246        // TODO: Add support for WebDriver BiDi.
1247        //
1248        // Step 7: Let result be null.
1249        //
1250        // Step 8: If userPromptHandler is "none", then:
1251        //  1. Pause while waiting for the user's response.
1252        //  2. If the user did not abort, then set result to the string that the user responded with.
1253        //
1254        // Step 9: Otherwise, if userPromptHandler is "accept", then set result to the empty string.
1255        // TODO: Implement this.
1256        //
1257        // Step 10: Invoke WebDriver BiDi user prompt closed with this, "prompt", false if
1258        // result is null or true otherwise, and result.
1259        // TODO: Add support for WebDriver BiDi.
1260        //
1261        // Step 11: Return result.
1262        match receiver.recv() {
1263            Ok(PromptResponse::Ok(input)) => Some(input.into()),
1264            Ok(PromptResponse::Cancel) => None,
1265            Err(_) => {
1266                warn!("Prompt dialog was cancelled or failed to show.");
1267                None
1268            },
1269        }
1270    }
1271
1272    /// <https://html.spec.whatwg.org/multipage/#dom-window-stop>
1273    fn Stop(&self, cx: &mut js::context::JSContext) {
1274        // 1. If this's navigable is null, then return.
1275        // Note: Servo doesn't have a concept of navigable yet.
1276
1277        // 2. Stop loading this's navigable.
1278        self.stop_loading(cx);
1279    }
1280
1281    /// <https://html.spec.whatwg.org/multipage/#dom-window-focus>
1282    fn Focus(&self) {
1283        // > 1. Let `current` be this `Window` object's browsing context.
1284        // >
1285        // > 2. If `current` is null, then return.
1286        let current = match self.undiscarded_window_proxy() {
1287            Some(proxy) => proxy,
1288            None => return,
1289        };
1290
1291        // > 3. Run the focusing steps with `current`.
1292        current.focus();
1293
1294        // > 4. If current is a top-level browsing context, user agents are
1295        // >    encouraged to trigger some sort of notification to indicate to
1296        // >    the user that the page is attempting to gain focus.
1297        //
1298        // TODO: Step 4
1299    }
1300
1301    /// <https://html.spec.whatwg.org/multipage/#dom-window-blur>
1302    fn Blur(&self) {
1303        // > User agents are encouraged to ignore calls to this `blur()` method
1304        // > entirely.
1305    }
1306
1307    /// <https://html.spec.whatwg.org/multipage/#dom-open>
1308    fn Open(
1309        &self,
1310        cx: &mut JSContext,
1311        url: USVString,
1312        target: DOMString,
1313        features: DOMString,
1314    ) -> Fallible<Option<DomRoot<WindowProxy>>> {
1315        self.window_proxy().open(cx, url, target, features)
1316    }
1317
1318    /// <https://html.spec.whatwg.org/multipage/#dom-opener>
1319    fn GetOpener(&self, cx: &mut CurrentRealm, mut retval: MutableHandleValue) -> Fallible<()> {
1320        // Step 1, Let current be this Window object's browsing context.
1321        let current = match self.window_proxy.get() {
1322            Some(proxy) => proxy,
1323            // Step 2, If current is null, then return null.
1324            None => {
1325                retval.set(NullValue());
1326                return Ok(());
1327            },
1328        };
1329        // Still step 2, since the window's BC is the associated doc's BC,
1330        // see https://html.spec.whatwg.org/multipage/#window-bc
1331        // and a doc's BC is null if it has been discarded.
1332        // see https://html.spec.whatwg.org/multipage/#concept-document-bc
1333        if current.is_browsing_context_discarded() {
1334            retval.set(NullValue());
1335            return Ok(());
1336        }
1337        // Step 3 to 5.
1338        current.opener(cx, retval);
1339        Ok(())
1340    }
1341
1342    #[expect(unsafe_code)]
1343    /// <https://html.spec.whatwg.org/multipage/#dom-opener>
1344    fn SetOpener(&self, cx: SafeJSContext, value: HandleValue) -> ErrorResult {
1345        // Step 1.
1346        if value.is_null() {
1347            if let Some(proxy) = self.window_proxy.get() {
1348                proxy.disown();
1349            }
1350            return Ok(());
1351        }
1352        // Step 2.
1353        let obj = self.reflector().get_jsobject();
1354        unsafe {
1355            let result =
1356                JS_DefineProperty(*cx, obj, c"opener".as_ptr(), value, JSPROP_ENUMERATE as u32);
1357
1358            if result { Ok(()) } else { Err(Error::JSFailed) }
1359        }
1360    }
1361
1362    /// <https://html.spec.whatwg.org/multipage/#dom-window-closed>
1363    fn Closed(&self) -> bool {
1364        self.window_proxy
1365            .get()
1366            .map(|ref proxy| proxy.is_browsing_context_discarded() || proxy.is_closing())
1367            .unwrap_or(true)
1368    }
1369
1370    /// <https://html.spec.whatwg.org/multipage/#dom-window-close>
1371    fn Close(&self) {
1372        // Step 1. Let thisTraversable be this's navigable.
1373        let window_proxy = match self.window_proxy.get() {
1374            Some(proxy) => proxy,
1375            // Step 2. If thisTraversable is not a top-level traversable, then return.
1376            None => return,
1377        };
1378        // Step 3. If thisTraversable's is closing is true, then return.
1379        if window_proxy.is_closing() {
1380            return;
1381        }
1382        // Note: check the length of the "session history", as opposed to the joint session history?
1383        // see https://github.com/whatwg/html/issues/3734
1384        if let Ok(history_length) = self.History().GetLength() {
1385            let is_auxiliary = window_proxy.is_auxiliary();
1386
1387            // https://html.spec.whatwg.org/multipage/#script-closable
1388            let is_script_closable = (self.is_top_level() && history_length == 1) ||
1389                is_auxiliary ||
1390                pref!(dom_allow_scripts_to_close_windows);
1391
1392            // TODO: rest of Step 3:
1393            // Is the incumbent settings object's responsible browsing context familiar with current?
1394            // Is the incumbent settings object's responsible browsing context allowed to navigate current?
1395            if is_script_closable {
1396                // Step 6.1. Set thisTraversable's is closing to true.
1397                window_proxy.close();
1398
1399                // Step 6.2. Queue a task on the DOM manipulation task source to definitely close thisTraversable.
1400                let this = Trusted::new(self);
1401                let task = task!(window_close_browsing_context: move |cx| {
1402                    let window = this.root();
1403                    window.definitely_close(cx);
1404                });
1405                self.as_global_scope()
1406                    .task_manager()
1407                    .dom_manipulation_task_source()
1408                    .queue(task);
1409            }
1410        }
1411    }
1412
1413    /// <https://html.spec.whatwg.org/multipage/#dom-document-2>
1414    fn Document(&self) -> DomRoot<Document> {
1415        self.document
1416            .get()
1417            .expect("Document accessed before initialization.")
1418    }
1419
1420    /// <https://html.spec.whatwg.org/multipage/#dom-history>
1421    fn History(&self) -> DomRoot<History> {
1422        self.history.or_init(|| History::new(self, CanGc::note()))
1423    }
1424
1425    /// <https://w3c.github.io/IndexedDB/#factory-interface>
1426    fn IndexedDB(&self) -> DomRoot<IDBFactory> {
1427        self.upcast::<GlobalScope>().get_indexeddb()
1428    }
1429
1430    /// <https://html.spec.whatwg.org/multipage/#dom-window-customelements>
1431    fn CustomElements(&self) -> DomRoot<CustomElementRegistry> {
1432        self.custom_element_registry
1433            .or_init(|| CustomElementRegistry::new(self, CanGc::note()))
1434    }
1435
1436    /// <https://html.spec.whatwg.org/multipage/#dom-location>
1437    fn Location(&self) -> DomRoot<Location> {
1438        self.location.or_init(|| Location::new(self, CanGc::note()))
1439    }
1440
1441    /// <https://html.spec.whatwg.org/multipage/#dom-sessionstorage>
1442    fn SessionStorage(&self) -> DomRoot<Storage> {
1443        self.session_storage
1444            .or_init(|| Storage::new(self, WebStorageType::Session, CanGc::note()))
1445    }
1446
1447    /// <https://html.spec.whatwg.org/multipage/#dom-localstorage>
1448    fn LocalStorage(&self) -> DomRoot<Storage> {
1449        self.local_storage
1450            .or_init(|| Storage::new(self, WebStorageType::Local, CanGc::note()))
1451    }
1452
1453    /// <https://cookiestore.spec.whatwg.org/#Window>
1454    fn CookieStore(&self, can_gc: CanGc) -> DomRoot<CookieStore> {
1455        self.global().cookie_store(can_gc)
1456    }
1457
1458    /// <https://dvcs.w3.org/hg/webcrypto-api/raw-file/tip/spec/Overview.html#dfn-GlobalCrypto>
1459    fn Crypto(&self) -> DomRoot<Crypto> {
1460        self.as_global_scope().crypto(CanGc::note())
1461    }
1462
1463    /// <https://html.spec.whatwg.org/multipage/#dom-frameelement>
1464    fn GetFrameElement(&self) -> Option<DomRoot<Element>> {
1465        // Steps 1-3.
1466        let window_proxy = self.window_proxy.get()?;
1467
1468        // Step 4-5.
1469        let container = window_proxy.frame_element()?;
1470
1471        // Step 6.
1472        let container_doc = container.owner_document();
1473        let current_doc = GlobalScope::current()
1474            .expect("No current global object")
1475            .as_window()
1476            .Document();
1477        if !current_doc
1478            .origin()
1479            .same_origin_domain(container_doc.origin())
1480        {
1481            return None;
1482        }
1483        // Step 7.
1484        Some(DomRoot::from_ref(container))
1485    }
1486
1487    /// <https://html.spec.whatwg.org/multipage/#dom-reporterror>
1488    fn ReportError(&self, cx: SafeJSContext, error: HandleValue, can_gc: CanGc) {
1489        self.as_global_scope()
1490            .report_an_exception(cx, error, can_gc);
1491    }
1492
1493    /// <https://html.spec.whatwg.org/multipage/#dom-navigator>
1494    fn Navigator(&self) -> DomRoot<Navigator> {
1495        self.navigator
1496            .or_init(|| Navigator::new(self, CanGc::note()))
1497    }
1498
1499    /// <https://html.spec.whatwg.org/multipage/#dom-clientinformation>
1500    fn ClientInformation(&self) -> DomRoot<Navigator> {
1501        self.Navigator()
1502    }
1503
1504    /// <https://html.spec.whatwg.org/multipage/#dom-settimeout>
1505    fn SetTimeout(
1506        &self,
1507        cx: &mut js::context::JSContext,
1508        callback: TrustedScriptOrStringOrFunction,
1509        timeout: i32,
1510        args: Vec<HandleValue>,
1511    ) -> Fallible<i32> {
1512        let callback = match callback {
1513            TrustedScriptOrStringOrFunction::String(i) => {
1514                TimerCallback::StringTimerCallback(TrustedScriptOrString::String(i))
1515            },
1516            TrustedScriptOrStringOrFunction::TrustedScript(i) => {
1517                TimerCallback::StringTimerCallback(TrustedScriptOrString::TrustedScript(i))
1518            },
1519            TrustedScriptOrStringOrFunction::Function(i) => TimerCallback::FunctionTimerCallback(i),
1520        };
1521        self.as_global_scope().set_timeout_or_interval(
1522            cx,
1523            callback,
1524            args,
1525            Duration::from_millis(timeout.max(0) as u64),
1526            IsInterval::NonInterval,
1527        )
1528    }
1529
1530    /// <https://html.spec.whatwg.org/multipage/#dom-windowtimers-cleartimeout>
1531    fn ClearTimeout(&self, handle: i32) {
1532        self.as_global_scope().clear_timeout_or_interval(handle);
1533    }
1534
1535    /// <https://html.spec.whatwg.org/multipage/#dom-windowtimers-setinterval>
1536    fn SetInterval(
1537        &self,
1538        cx: &mut js::context::JSContext,
1539        callback: TrustedScriptOrStringOrFunction,
1540        timeout: i32,
1541        args: Vec<HandleValue>,
1542    ) -> Fallible<i32> {
1543        let callback = match callback {
1544            TrustedScriptOrStringOrFunction::String(i) => {
1545                TimerCallback::StringTimerCallback(TrustedScriptOrString::String(i))
1546            },
1547            TrustedScriptOrStringOrFunction::TrustedScript(i) => {
1548                TimerCallback::StringTimerCallback(TrustedScriptOrString::TrustedScript(i))
1549            },
1550            TrustedScriptOrStringOrFunction::Function(i) => TimerCallback::FunctionTimerCallback(i),
1551        };
1552        self.as_global_scope().set_timeout_or_interval(
1553            cx,
1554            callback,
1555            args,
1556            Duration::from_millis(timeout.max(0) as u64),
1557            IsInterval::Interval,
1558        )
1559    }
1560
1561    /// <https://html.spec.whatwg.org/multipage/#dom-windowtimers-clearinterval>
1562    fn ClearInterval(&self, handle: i32) {
1563        self.ClearTimeout(handle);
1564    }
1565
1566    /// <https://html.spec.whatwg.org/multipage/#dom-queuemicrotask>
1567    fn QueueMicrotask(&self, callback: Rc<VoidFunction>) {
1568        ScriptThread::enqueue_microtask(Microtask::User(UserMicrotask {
1569            callback,
1570            pipeline: self.pipeline_id(),
1571        }));
1572    }
1573
1574    /// <https://html.spec.whatwg.org/multipage/#dom-createimagebitmap>
1575    fn CreateImageBitmap(
1576        &self,
1577        realm: &mut CurrentRealm,
1578        image: ImageBitmapSource,
1579        options: &ImageBitmapOptions,
1580    ) -> Rc<Promise> {
1581        ImageBitmap::create_image_bitmap(
1582            self.as_global_scope(),
1583            image,
1584            0,
1585            0,
1586            None,
1587            None,
1588            options,
1589            realm,
1590        )
1591    }
1592
1593    /// <https://html.spec.whatwg.org/multipage/#dom-createimagebitmap>
1594    fn CreateImageBitmap_(
1595        &self,
1596        realm: &mut CurrentRealm,
1597        image: ImageBitmapSource,
1598        sx: i32,
1599        sy: i32,
1600        sw: i32,
1601        sh: i32,
1602        options: &ImageBitmapOptions,
1603    ) -> Rc<Promise> {
1604        ImageBitmap::create_image_bitmap(
1605            self.as_global_scope(),
1606            image,
1607            sx,
1608            sy,
1609            Some(sw),
1610            Some(sh),
1611            options,
1612            realm,
1613        )
1614    }
1615
1616    /// <https://html.spec.whatwg.org/multipage/#dom-window>
1617    fn Window(&self) -> DomRoot<WindowProxy> {
1618        self.window_proxy()
1619    }
1620
1621    /// <https://html.spec.whatwg.org/multipage/#dom-self>
1622    fn Self_(&self) -> DomRoot<WindowProxy> {
1623        self.window_proxy()
1624    }
1625
1626    /// <https://html.spec.whatwg.org/multipage/#dom-frames>
1627    fn Frames(&self) -> DomRoot<WindowProxy> {
1628        self.window_proxy()
1629    }
1630
1631    /// <https://html.spec.whatwg.org/multipage/#accessing-other-browsing-contexts>
1632    fn Length(&self) -> u32 {
1633        self.Document().iframes().iter().count() as u32
1634    }
1635
1636    /// <https://html.spec.whatwg.org/multipage/#dom-parent>
1637    fn GetParent(&self) -> Option<DomRoot<WindowProxy>> {
1638        // Steps 1-3.
1639        let window_proxy = self.undiscarded_window_proxy()?;
1640
1641        // Step 4.
1642        if let Some(parent) = window_proxy.parent() {
1643            return Some(DomRoot::from_ref(parent));
1644        }
1645        // Step 5.
1646        Some(window_proxy)
1647    }
1648
1649    /// <https://html.spec.whatwg.org/multipage/#dom-top>
1650    fn GetTop(&self) -> Option<DomRoot<WindowProxy>> {
1651        // Steps 1-3.
1652        let window_proxy = self.undiscarded_window_proxy()?;
1653
1654        // Steps 4-5.
1655        Some(DomRoot::from_ref(window_proxy.top()))
1656    }
1657
1658    // https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/
1659    // NavigationTiming/Overview.html#sec-window.performance-attribute
1660    fn Performance(&self) -> DomRoot<Performance> {
1661        self.performance.or_init(|| {
1662            Performance::new(
1663                self.as_global_scope(),
1664                self.navigation_start.get(),
1665                CanGc::note(),
1666            )
1667        })
1668    }
1669
1670    // https://html.spec.whatwg.org/multipage/#globaleventhandlers
1671    global_event_handlers!();
1672
1673    // https://html.spec.whatwg.org/multipage/#windoweventhandlers
1674    window_event_handlers!();
1675
1676    /// <https://developer.mozilla.org/en-US/docs/Web/API/Window/screen>
1677    fn Screen(&self, can_gc: CanGc) -> DomRoot<Screen> {
1678        self.screen.or_init(|| Screen::new(self, can_gc))
1679    }
1680
1681    /// <https://drafts.csswg.org/cssom-view/#dom-window-visualviewport>
1682    fn GetVisualViewport(&self, can_gc: CanGc) -> Option<DomRoot<VisualViewport>> {
1683        // > If the associated document is fully active, the visualViewport attribute must return the
1684        // > VisualViewport object associated with the Window object’s associated document. Otherwise,
1685        // > it must return null.
1686        if !self.Document().is_fully_active() {
1687            return None;
1688        }
1689
1690        Some(self.get_or_init_visual_viewport(can_gc))
1691    }
1692
1693    /// <https://html.spec.whatwg.org/multipage/#dom-windowbase64-btoa>
1694    fn Btoa(&self, btoa: DOMString) -> Fallible<DOMString> {
1695        base64_btoa(btoa)
1696    }
1697
1698    /// <https://html.spec.whatwg.org/multipage/#dom-windowbase64-atob>
1699    fn Atob(&self, atob: DOMString) -> Fallible<DOMString> {
1700        base64_atob(atob)
1701    }
1702
1703    /// <https://html.spec.whatwg.org/multipage/#dom-window-requestanimationframe>
1704    fn RequestAnimationFrame(&self, callback: Rc<FrameRequestCallback>) -> u32 {
1705        self.Document()
1706            .request_animation_frame(AnimationFrameCallback::FrameRequestCallback { callback })
1707    }
1708
1709    /// <https://html.spec.whatwg.org/multipage/#dom-window-cancelanimationframe>
1710    fn CancelAnimationFrame(&self, ident: u32) {
1711        let doc = self.Document();
1712        doc.cancel_animation_frame(ident);
1713    }
1714
1715    /// <https://html.spec.whatwg.org/multipage/#dom-window-postmessage>
1716    fn PostMessage(
1717        &self,
1718        cx: &mut JSContext,
1719        message: HandleValue,
1720        target_origin: USVString,
1721        transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
1722    ) -> ErrorResult {
1723        let incumbent = GlobalScope::incumbent().expect("no incumbent global?");
1724        let source = incumbent.as_window();
1725        let source_origin = source.Document().origin().immutable().clone();
1726
1727        self.post_message_impl(&target_origin, source_origin, source, cx, message, transfer)
1728    }
1729
1730    /// <https://html.spec.whatwg.org/multipage/#dom-messageport-postmessage>
1731    fn PostMessage_(
1732        &self,
1733        cx: &mut JSContext,
1734        message: HandleValue,
1735        options: RootedTraceableBox<WindowPostMessageOptions>,
1736    ) -> ErrorResult {
1737        let mut rooted = CustomAutoRooter::new(
1738            options
1739                .parent
1740                .transfer
1741                .iter()
1742                .map(|js: &RootedTraceableBox<Heap<*mut JSObject>>| js.get())
1743                .collect(),
1744        );
1745        #[expect(unsafe_code)]
1746        let transfer = unsafe { CustomAutoRooterGuard::new(cx.raw_cx(), &mut rooted) };
1747
1748        let incumbent = GlobalScope::incumbent().expect("no incumbent global?");
1749        let source = incumbent.as_window();
1750
1751        let source_origin = source.Document().origin().immutable().clone();
1752
1753        self.post_message_impl(
1754            &options.targetOrigin,
1755            source_origin,
1756            source,
1757            cx,
1758            message,
1759            transfer,
1760        )
1761    }
1762
1763    /// <https://html.spec.whatwg.org/multipage/#dom-window-captureevents>
1764    fn CaptureEvents(&self) {
1765        // This method intentionally does nothing
1766    }
1767
1768    /// <https://html.spec.whatwg.org/multipage/#dom-window-releaseevents>
1769    fn ReleaseEvents(&self) {
1770        // This method intentionally does nothing
1771    }
1772
1773    // check-tidy: no specs after this line
1774    fn WebdriverCallback(&self, realm: &mut CurrentRealm, value: HandleValue) {
1775        let webdriver_script_sender = self.webdriver_script_chan.borrow_mut().take();
1776        if let Some(webdriver_script_sender) = webdriver_script_sender {
1777            let result = jsval_to_webdriver(realm, &self.globalscope, value);
1778            let _ = webdriver_script_sender.send(result);
1779        }
1780    }
1781
1782    fn WebdriverException(&self, cx: &mut JSContext, value: HandleValue) {
1783        let webdriver_script_sender = self.webdriver_script_chan.borrow_mut().take();
1784        if let Some(webdriver_script_sender) = webdriver_script_sender {
1785            let error_info = ErrorInfo::from_value(value, cx.into(), CanGc::from_cx(cx));
1786            let _ = webdriver_script_sender.send(Err(
1787                JavaScriptEvaluationError::EvaluationFailure(Some(
1788                    javascript_error_info_from_error_info(cx, &error_info, value),
1789                )),
1790            ));
1791        }
1792    }
1793
1794    fn WebdriverElement(&self, id: DOMString) -> Option<DomRoot<Element>> {
1795        find_node_by_unique_id_in_document(&self.Document(), id.into()).and_then(Root::downcast)
1796    }
1797
1798    fn WebdriverFrame(&self, browsing_context_id: DOMString) -> Option<DomRoot<WindowProxy>> {
1799        self.Document()
1800            .iframes()
1801            .iter()
1802            .find(|iframe| {
1803                iframe
1804                    .browsing_context_id()
1805                    .as_ref()
1806                    .map(BrowsingContextId::to_string) ==
1807                    Some(browsing_context_id.to_string())
1808            })
1809            .and_then(|iframe| iframe.GetContentWindow())
1810    }
1811
1812    fn WebdriverWindow(&self, webview_id: DOMString) -> DomRoot<WindowProxy> {
1813        let window_proxy = &self
1814            .window_proxy
1815            .get()
1816            .expect("Should always have a WindowProxy when calling WebdriverWindow");
1817        assert!(
1818            self.is_top_level(),
1819            "Window must be top level browsing context."
1820        );
1821        assert!(self.webview_id().to_string() == webview_id);
1822        DomRoot::from_ref(window_proxy)
1823    }
1824
1825    fn WebdriverShadowRoot(&self, id: DOMString) -> Option<DomRoot<ShadowRoot>> {
1826        find_node_by_unique_id_in_document(&self.Document(), id.into()).and_then(Root::downcast)
1827    }
1828
1829    /// <https://drafts.csswg.org/cssom/#dom-window-getcomputedstyle>
1830    fn GetComputedStyle(
1831        &self,
1832        element: &Element,
1833        pseudo: Option<DOMString>,
1834    ) -> DomRoot<CSSStyleDeclaration> {
1835        // Step 2: Let obj be elt.
1836        // We don't store CSSStyleOwner directly because it stores a `Dom` which must be
1837        // rooted. This avoids the rooting the value temporarily.
1838        let mut is_null = false;
1839
1840        // Step 3: If pseudoElt is provided, is not the empty string, and starts with a colon, then:
1841        // Step 3.1: Parse pseudoElt as a <pseudo-element-selector>, and let type be the result.
1842        // TODO(#43095): This is quite hacky and it would be better to have a parsing function that
1843        // is integrated with stylo `PseudoElement` itself. Comparing with stylo, we are now currently
1844        // missing `::backdrop`, `::color-swatch`, and `::details-content`.
1845        let pseudo = pseudo.map(|mut s| {
1846            s.make_ascii_lowercase();
1847            s
1848        });
1849        let pseudo = match pseudo {
1850            Some(ref pseudo) if pseudo == ":before" || pseudo == "::before" => {
1851                Some(PseudoElement::Before)
1852            },
1853            Some(ref pseudo) if pseudo == ":after" || pseudo == "::after" => {
1854                Some(PseudoElement::After)
1855            },
1856            Some(ref pseudo) if pseudo == "::selection" => Some(PseudoElement::Selection),
1857            Some(ref pseudo) if pseudo == "::marker" => Some(PseudoElement::Marker),
1858            Some(ref pseudo) if pseudo == "::placeholder" => Some(PseudoElement::Placeholder),
1859            Some(ref pseudo) if pseudo.starts_with(':') => {
1860                // Step 3.2: If type is failure, or is a ::slotted() or ::part()
1861                // pseudo-element, let obj be null.
1862                is_null = true;
1863                None
1864            },
1865            _ => None,
1866        };
1867
1868        // Step 4. Let decls be an empty list of CSS declarations.
1869        // Step 5: If obj is not null, and elt is connected, part of the flat tree, and
1870        // its shadow-including root has a browsing context which either doesn’t have a
1871        // browsing context container, or whose browsing context container is being
1872        // rendered, set decls to a list of all longhand properties that are supported CSS
1873        // properties, in lexicographical order, with the value being the resolved value
1874        // computed for obj using the style rules associated with doc.  Additionally,
1875        // append to decls all the custom properties whose computed value for obj is not
1876        // the guaranteed-invalid value.
1877        //
1878        // Note: The specification says to generate the list of declarations beforehand, yet
1879        // also says the list should be alive. This is why we do not do step 4 and 5 here.
1880        // See: https://github.com/w3c/csswg-drafts/issues/6144
1881        //
1882        // Step 6:  Return a live CSSStyleProperties object with the following properties:
1883        CSSStyleDeclaration::new(
1884            self,
1885            if is_null {
1886                CSSStyleOwner::Null
1887            } else {
1888                CSSStyleOwner::Element(Dom::from_ref(element))
1889            },
1890            pseudo,
1891            CSSModificationAccess::Readonly,
1892            CanGc::note(),
1893        )
1894    }
1895
1896    // https://drafts.csswg.org/cssom-view/#dom-window-innerheight
1897    // TODO Include Scrollbar
1898    fn InnerHeight(&self) -> i32 {
1899        self.viewport_details
1900            .get()
1901            .size
1902            .height
1903            .to_i32()
1904            .unwrap_or(0)
1905    }
1906
1907    // https://drafts.csswg.org/cssom-view/#dom-window-innerwidth
1908    // TODO Include Scrollbar
1909    fn InnerWidth(&self) -> i32 {
1910        self.viewport_details.get().size.width.to_i32().unwrap_or(0)
1911    }
1912
1913    /// <https://drafts.csswg.org/cssom-view/#dom-window-scrollx>
1914    fn ScrollX(&self) -> i32 {
1915        self.scroll_offset().x as i32
1916    }
1917
1918    /// <https://drafts.csswg.org/cssom-view/#dom-window-pagexoffset>
1919    fn PageXOffset(&self) -> i32 {
1920        self.ScrollX()
1921    }
1922
1923    /// <https://drafts.csswg.org/cssom-view/#dom-window-scrolly>
1924    fn ScrollY(&self) -> i32 {
1925        self.scroll_offset().y as i32
1926    }
1927
1928    /// <https://drafts.csswg.org/cssom-view/#dom-window-pageyoffset>
1929    fn PageYOffset(&self) -> i32 {
1930        self.ScrollY()
1931    }
1932
1933    /// <https://drafts.csswg.org/cssom-view/#dom-window-scroll>
1934    fn Scroll(&self, options: &ScrollToOptions) {
1935        // Step 1: If invoked with one argument, follow these substeps:
1936        // Step 1.1: Let options be the argument.
1937        // Step 1.2: Let x be the value of the left dictionary member of options, if
1938        // present, or the viewport’s current scroll position on the x axis otherwise.
1939        let x = options.left.unwrap_or(0.0) as f32;
1940
1941        // Step 1.3: Let y be the value of the top dictionary member of options, if
1942        // present, or the viewport’s current scroll position on the y axis otherwise.
1943        let y = options.top.unwrap_or(0.0) as f32;
1944
1945        // The rest of the specification continues from `Self::scroll`.
1946        self.scroll(x, y, options.parent.behavior);
1947    }
1948
1949    /// <https://drafts.csswg.org/cssom-view/#dom-window-scroll>
1950    fn Scroll_(&self, x: f64, y: f64) {
1951        // Step 2: If invoked with two arguments, follow these substeps:
1952        // Step 2.1 Let options be null converted to a ScrollToOptions dictionary. [WEBIDL]
1953        // Step 2.2: Let x and y be the arguments, respectively.
1954        self.scroll(x as f32, y as f32, ScrollBehavior::Auto);
1955    }
1956
1957    /// <https://drafts.csswg.org/cssom-view/#dom-window-scrollto>
1958    ///
1959    /// > When the scrollTo() method is invoked, the user agent must act as if the
1960    /// > scroll() method was invoked with the same arguments.
1961    fn ScrollTo(&self, options: &ScrollToOptions) {
1962        self.Scroll(options);
1963    }
1964
1965    /// <https://drafts.csswg.org/cssom-view/#dom-window-scrollto>:
1966    ///
1967    /// > When the scrollTo() method is invoked, the user agent must act as if the
1968    /// > scroll() method was invoked with the same arguments.
1969    fn ScrollTo_(&self, x: f64, y: f64) {
1970        self.Scroll_(x, y)
1971    }
1972
1973    /// <https://drafts.csswg.org/cssom-view/#dom-window-scrollby>
1974    fn ScrollBy(&self, options: &ScrollToOptions) {
1975        // When the scrollBy() method is invoked, the user agent must run these steps:
1976        // Step 1: If invoked with two arguments, follow these substeps:
1977        //   This doesn't apply here.
1978
1979        // Step 2: Normalize non-finite values for the left and top dictionary members of options.
1980        let mut options = options.clone();
1981        let x = options.left.unwrap_or(0.0);
1982        let x = if x.is_finite() { x } else { 0.0 };
1983        let y = options.top.unwrap_or(0.0);
1984        let y = if y.is_finite() { y } else { 0.0 };
1985
1986        // Step 3: Add the value of scrollX to the left dictionary member.
1987        options.left.replace(x + self.ScrollX() as f64);
1988
1989        // Step 4. Add the value of scrollY to the top dictionary member.
1990        options.top.replace(y + self.ScrollY() as f64);
1991
1992        // Step 5: Act as if the scroll() method was invoked with options as the only argument.
1993        self.Scroll(&options)
1994    }
1995
1996    /// <https://drafts.csswg.org/cssom-view/#dom-window-scrollby>
1997    fn ScrollBy_(&self, x: f64, y: f64) {
1998        // When the scrollBy() method is invoked, the user agent must run these steps:
1999        // Step 1: If invoked with two arguments, follow these substeps:
2000        // Step 1.1: Let options be null converted to a ScrollToOptions dictionary.
2001        let mut options = ScrollToOptions::empty();
2002
2003        // Step 1.2: Let x and y be the arguments, respectively.
2004        // Step 1.3: Let the left dictionary member of options have the value x.
2005        options.left.replace(x);
2006
2007        // Step 1.5:  Let the top dictionary member of options have the value y.
2008        options.top.replace(y);
2009
2010        // Now follow the specification for the one argument option.
2011        self.ScrollBy(&options);
2012    }
2013
2014    /// <https://drafts.csswg.org/cssom-view/#dom-window-resizeto>
2015    fn ResizeTo(&self, width: i32, height: i32) {
2016        // Step 1
2017        let window_proxy = match self.window_proxy.get() {
2018            Some(proxy) => proxy,
2019            None => return,
2020        };
2021
2022        // If target is not an auxiliary browsing context that was created by a script
2023        // (as opposed to by an action of the user), then return.
2024        if !window_proxy.is_auxiliary() {
2025            return;
2026        }
2027
2028        let dpr = self.device_pixel_ratio();
2029        let size = Size2D::new(width, height).to_f32() * dpr;
2030        self.send_to_embedder(EmbedderMsg::ResizeTo(self.webview_id(), size.to_i32()));
2031    }
2032
2033    /// <https://drafts.csswg.org/cssom-view/#dom-window-resizeby>
2034    fn ResizeBy(&self, x: i32, y: i32) {
2035        let size = self.client_window().size();
2036        // Step 1
2037        self.ResizeTo(x + size.width, y + size.height)
2038    }
2039
2040    /// <https://drafts.csswg.org/cssom-view/#dom-window-moveto>
2041    fn MoveTo(&self, x: i32, y: i32) {
2042        // Step 1
2043        // TODO determine if this operation is allowed
2044        let dpr = self.device_pixel_ratio();
2045        let point = Point2D::new(x, y).to_f32() * dpr;
2046        let msg = EmbedderMsg::MoveTo(self.webview_id(), point.to_i32());
2047        self.send_to_embedder(msg);
2048    }
2049
2050    /// <https://drafts.csswg.org/cssom-view/#dom-window-moveby>
2051    fn MoveBy(&self, x: i32, y: i32) {
2052        let origin = self.client_window().min;
2053        // Step 1
2054        self.MoveTo(x + origin.x, y + origin.y)
2055    }
2056
2057    /// <https://drafts.csswg.org/cssom-view/#dom-window-screenx>
2058    fn ScreenX(&self) -> i32 {
2059        self.client_window().min.x
2060    }
2061
2062    /// <https://drafts.csswg.org/cssom-view/#dom-window-screeny>
2063    fn ScreenY(&self) -> i32 {
2064        self.client_window().min.y
2065    }
2066
2067    /// <https://drafts.csswg.org/cssom-view/#dom-window-outerheight>
2068    fn OuterHeight(&self) -> i32 {
2069        self.client_window().height()
2070    }
2071
2072    /// <https://drafts.csswg.org/cssom-view/#dom-window-outerwidth>
2073    fn OuterWidth(&self) -> i32 {
2074        self.client_window().width()
2075    }
2076
2077    /// <https://drafts.csswg.org/cssom-view/#dom-window-devicepixelratio>
2078    fn DevicePixelRatio(&self) -> Finite<f64> {
2079        Finite::wrap(self.device_pixel_ratio().get() as f64)
2080    }
2081
2082    /// <https://html.spec.whatwg.org/multipage/#dom-window-status>
2083    fn Status(&self) -> DOMString {
2084        self.status.borrow().clone()
2085    }
2086
2087    /// <https://html.spec.whatwg.org/multipage/#dom-window-status>
2088    fn SetStatus(&self, status: DOMString) {
2089        *self.status.borrow_mut() = status
2090    }
2091
2092    /// <https://drafts.csswg.org/cssom-view/#dom-window-matchmedia>
2093    fn MatchMedia(&self, query: DOMString) -> DomRoot<MediaQueryList> {
2094        let media_query_list = MediaList::parse_media_list(&query.str(), self);
2095        let document = self.Document();
2096        let mql = MediaQueryList::new(&document, media_query_list, CanGc::note());
2097        self.media_query_lists.track(&*mql);
2098        mql
2099    }
2100
2101    /// <https://fetch.spec.whatwg.org/#dom-global-fetch>
2102    fn Fetch(
2103        &self,
2104        realm: &mut CurrentRealm,
2105        input: RequestOrUSVString,
2106        init: RootedTraceableBox<RequestInit>,
2107    ) -> Rc<Promise> {
2108        fetch::Fetch(self.upcast(), input, init, realm)
2109    }
2110
2111    /// <https://fetch.spec.whatwg.org/#dom-window-fetchlater>
2112    fn FetchLater(
2113        &self,
2114        input: RequestInfo,
2115        init: RootedTraceableBox<DeferredRequestInit>,
2116        can_gc: CanGc,
2117    ) -> Fallible<DomRoot<FetchLaterResult>> {
2118        fetch::FetchLater(self, input, init, can_gc)
2119    }
2120
2121    #[cfg(feature = "bluetooth")]
2122    fn TestRunner(&self) -> DomRoot<TestRunner> {
2123        self.test_runner
2124            .or_init(|| TestRunner::new(self.upcast(), CanGc::note()))
2125    }
2126
2127    fn RunningAnimationCount(&self) -> u32 {
2128        self.document
2129            .get()
2130            .map_or(0, |d| d.animations().running_animation_count() as u32)
2131    }
2132
2133    /// <https://html.spec.whatwg.org/multipage/#dom-name>
2134    fn SetName(&self, name: DOMString) {
2135        if let Some(proxy) = self.undiscarded_window_proxy() {
2136            proxy.set_name(name);
2137        }
2138    }
2139
2140    /// <https://html.spec.whatwg.org/multipage/#dom-name>
2141    fn Name(&self) -> DOMString {
2142        match self.undiscarded_window_proxy() {
2143            Some(proxy) => proxy.get_name(),
2144            None => "".into(),
2145        }
2146    }
2147
2148    /// <https://html.spec.whatwg.org/multipage/#dom-origin>
2149    fn Origin(&self) -> USVString {
2150        USVString(self.origin().immutable().ascii_serialization())
2151    }
2152
2153    /// <https://w3c.github.io/selection-api/#dom-window-getselection>
2154    fn GetSelection(&self) -> Option<DomRoot<Selection>> {
2155        self.document
2156            .get()
2157            .and_then(|d| d.GetSelection(CanGc::note()))
2158    }
2159
2160    /// <https://dom.spec.whatwg.org/#dom-window-event>
2161    fn Event(&self, cx: SafeJSContext, rval: MutableHandleValue) {
2162        if let Some(ref event) = *self.current_event.borrow() {
2163            event
2164                .reflector()
2165                .get_jsobject()
2166                .safe_to_jsval(cx, rval, CanGc::note());
2167        }
2168    }
2169
2170    fn IsSecureContext(&self) -> bool {
2171        self.as_global_scope().is_secure_context()
2172    }
2173
2174    /// <https://html.spec.whatwg.org/multipage/#dom-window-nameditem>
2175    fn NamedGetter(&self, name: DOMString) -> Option<NamedPropertyValue> {
2176        if name.is_empty() {
2177            return None;
2178        }
2179        let document = self.Document();
2180
2181        // https://html.spec.whatwg.org/multipage/#document-tree-child-browsing-context-name-property-set
2182        let iframes: Vec<_> = document
2183            .iframes()
2184            .iter()
2185            .filter(|iframe| {
2186                if let Some(window) = iframe.GetContentWindow() {
2187                    return window.get_name() == name;
2188                }
2189                false
2190            })
2191            .collect();
2192
2193        let iframe_iter = iframes.iter().map(|iframe| iframe.upcast::<Element>());
2194
2195        let name = Atom::from(name);
2196
2197        // Step 1.
2198        let elements_with_name = document.get_elements_with_name(&name);
2199        let name_iter = elements_with_name
2200            .iter()
2201            .map(|element| &**element)
2202            .filter(|elem| is_named_element_with_name_attribute(elem));
2203        let elements_with_id = document.get_elements_with_id(&name);
2204        let id_iter = elements_with_id
2205            .iter()
2206            .map(|element| &**element)
2207            .filter(|elem| is_named_element_with_id_attribute(elem));
2208
2209        // Step 2.
2210        for elem in iframe_iter.clone() {
2211            if let Some(nested_window_proxy) = elem
2212                .downcast::<HTMLIFrameElement>()
2213                .and_then(|iframe| iframe.GetContentWindow())
2214            {
2215                return Some(NamedPropertyValue::WindowProxy(nested_window_proxy));
2216            }
2217        }
2218
2219        let mut elements = iframe_iter.chain(name_iter).chain(id_iter);
2220
2221        let first = elements.next()?;
2222
2223        if elements.next().is_none() {
2224            // Step 3.
2225            return Some(NamedPropertyValue::Element(DomRoot::from_ref(first)));
2226        }
2227
2228        // Step 4.
2229        #[derive(JSTraceable, MallocSizeOf)]
2230        struct WindowNamedGetter {
2231            #[no_trace]
2232            name: Atom,
2233        }
2234        impl CollectionFilter for WindowNamedGetter {
2235            fn filter(&self, elem: &Element, _root: &Node) -> bool {
2236                let type_ = match elem.upcast::<Node>().type_id() {
2237                    NodeTypeId::Element(ElementTypeId::HTMLElement(type_)) => type_,
2238                    _ => return false,
2239                };
2240                if elem.get_id().as_ref() == Some(&self.name) {
2241                    return true;
2242                }
2243                match type_ {
2244                    HTMLElementTypeId::HTMLEmbedElement |
2245                    HTMLElementTypeId::HTMLFormElement |
2246                    HTMLElementTypeId::HTMLImageElement |
2247                    HTMLElementTypeId::HTMLObjectElement => {
2248                        elem.get_name().as_ref() == Some(&self.name)
2249                    },
2250                    _ => false,
2251                }
2252            }
2253        }
2254        let collection = HTMLCollection::create(
2255            self,
2256            document.upcast(),
2257            Box::new(WindowNamedGetter { name }),
2258            CanGc::note(),
2259        );
2260        Some(NamedPropertyValue::HTMLCollection(collection))
2261    }
2262
2263    /// <https://html.spec.whatwg.org/multipage/#dom-tree-accessors:supported-property-names>
2264    fn SupportedPropertyNames(&self) -> Vec<DOMString> {
2265        let mut names_with_first_named_element_map: HashMap<&Atom, &Element> = HashMap::new();
2266
2267        let document = self.Document();
2268        let name_map = document.name_map();
2269        for (name, elements) in &name_map.0 {
2270            if name.is_empty() {
2271                continue;
2272            }
2273            let mut name_iter = elements
2274                .iter()
2275                .filter(|elem| is_named_element_with_name_attribute(elem));
2276            if let Some(first) = name_iter.next() {
2277                names_with_first_named_element_map.insert(name, first);
2278            }
2279        }
2280        let id_map = document.id_map();
2281        for (id, elements) in &id_map.0 {
2282            if id.is_empty() {
2283                continue;
2284            }
2285            let mut id_iter = elements
2286                .iter()
2287                .filter(|elem| is_named_element_with_id_attribute(elem));
2288            if let Some(first) = id_iter.next() {
2289                match names_with_first_named_element_map.entry(id) {
2290                    Entry::Vacant(entry) => drop(entry.insert(first)),
2291                    Entry::Occupied(mut entry) => {
2292                        if first.upcast::<Node>().is_before(entry.get().upcast()) {
2293                            *entry.get_mut() = first;
2294                        }
2295                    },
2296                }
2297            }
2298        }
2299
2300        let mut names_with_first_named_element_vec: Vec<(&Atom, &Element)> =
2301            names_with_first_named_element_map
2302                .iter()
2303                .map(|(k, v)| (*k, *v))
2304                .collect();
2305        names_with_first_named_element_vec.sort_unstable_by(|a, b| {
2306            if a.1 == b.1 {
2307                // This can happen if an img has an id different from its name,
2308                // spec does not say which string to put first.
2309                a.0.cmp(b.0)
2310            } else if a.1.upcast::<Node>().is_before(b.1.upcast::<Node>()) {
2311                cmp::Ordering::Less
2312            } else {
2313                cmp::Ordering::Greater
2314            }
2315        });
2316
2317        names_with_first_named_element_vec
2318            .iter()
2319            .map(|(k, _v)| DOMString::from(&***k))
2320            .collect()
2321    }
2322
2323    /// <https://html.spec.whatwg.org/multipage/#dom-structuredclone>
2324    fn StructuredClone(
2325        &self,
2326        cx: SafeJSContext,
2327        value: HandleValue,
2328        options: RootedTraceableBox<StructuredSerializeOptions>,
2329        can_gc: CanGc,
2330        retval: MutableHandleValue,
2331    ) -> Fallible<()> {
2332        self.as_global_scope()
2333            .structured_clone(cx, value, options, retval, can_gc)
2334    }
2335
2336    fn TrustedTypes(&self, cx: &mut JSContext) -> DomRoot<TrustedTypePolicyFactory> {
2337        self.trusted_types
2338            .or_init(|| TrustedTypePolicyFactory::new(cx, self.as_global_scope()))
2339    }
2340}
2341
2342impl Window {
2343    pub(crate) fn scroll_offset(&self) -> Vector2D<f32, LayoutPixel> {
2344        self.scroll_offset_query_with_external_scroll_id(self.pipeline_id().root_scroll_id())
2345    }
2346
2347    // https://heycam.github.io/webidl/#named-properties-object
2348    // https://html.spec.whatwg.org/multipage/#named-access-on-the-window-object
2349    pub(crate) fn create_named_properties_object(
2350        cx: SafeJSContext,
2351        proto: HandleObject,
2352        object: MutableHandleObject,
2353    ) {
2354        window_named_properties::create(cx, proto, object)
2355    }
2356
2357    pub(crate) fn current_event(&self) -> Option<DomRoot<Event>> {
2358        self.current_event
2359            .borrow()
2360            .as_ref()
2361            .map(|e| DomRoot::from_ref(&**e))
2362    }
2363
2364    pub(crate) fn set_current_event(&self, event: Option<&Event>) -> Option<DomRoot<Event>> {
2365        let current = self.current_event();
2366        *self.current_event.borrow_mut() = event.map(Dom::from_ref);
2367        current
2368    }
2369
2370    /// <https://html.spec.whatwg.org/multipage/#window-post-message-steps>
2371    fn post_message_impl(
2372        &self,
2373        target_origin: &USVString,
2374        source_origin: ImmutableOrigin,
2375        source: &Window,
2376        cx: &mut JSContext,
2377        message: HandleValue,
2378        transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
2379    ) -> ErrorResult {
2380        // Step 1-2, 6-8.
2381        let data = structuredclone::write(cx.into(), message, Some(transfer))?;
2382
2383        // Step 3-5.
2384        let target_origin = match target_origin.0[..].as_ref() {
2385            "*" => None,
2386            "/" => Some(source_origin.clone()),
2387            url => match ServoUrl::parse(url) {
2388                Ok(url) => Some(url.origin()),
2389                Err(_) => return Err(Error::Syntax(None)),
2390            },
2391        };
2392
2393        // Step 9.
2394        self.post_message(target_origin, source_origin, &source.window_proxy(), data);
2395        Ok(())
2396    }
2397
2398    // https://drafts.css-houdini.org/css-paint-api-1/#paint-worklet
2399    pub(crate) fn paint_worklet(&self) -> DomRoot<Worklet> {
2400        self.paint_worklet
2401            .or_init(|| self.new_paint_worklet(CanGc::note()))
2402    }
2403
2404    pub(crate) fn has_document(&self) -> bool {
2405        self.document.get().is_some()
2406    }
2407
2408    pub(crate) fn clear_js_runtime(&self) {
2409        self.as_global_scope()
2410            .remove_web_messaging_and_dedicated_workers_infra();
2411
2412        // Clean up any active promises
2413        // https://github.com/servo/servo/issues/15318
2414        if let Some(custom_elements) = self.custom_element_registry.get() {
2415            custom_elements.teardown();
2416        }
2417
2418        self.current_state.set(WindowState::Zombie);
2419        *self.js_runtime.borrow_mut() = None;
2420
2421        // If this is the currently active pipeline,
2422        // nullify the window_proxy.
2423        if let Some(proxy) = self.window_proxy.get() {
2424            let pipeline_id = self.pipeline_id();
2425            if let Some(currently_active) = proxy.currently_active() {
2426                if currently_active == pipeline_id {
2427                    self.window_proxy.set(None);
2428                }
2429            }
2430        }
2431
2432        if let Some(performance) = self.performance.get() {
2433            performance.clear_and_disable_performance_entry_buffer();
2434        }
2435        self.as_global_scope()
2436            .task_manager()
2437            .cancel_all_tasks_and_ignore_future_tasks();
2438    }
2439
2440    /// <https://drafts.csswg.org/cssom-view/#dom-window-scroll>
2441    pub(crate) fn scroll(&self, x: f32, y: f32, behavior: ScrollBehavior) {
2442        // Step 3: Normalize non-finite values for x and y.
2443        let xfinite = if x.is_finite() { x } else { 0.0 };
2444        let yfinite = if y.is_finite() { y } else { 0.0 };
2445
2446        // Step 4: If there is no viewport, abort these steps.
2447        // Currently every frame has a viewport in Servo.
2448
2449        // Step 5. Let `viewport width` be the width of the viewport excluding the width
2450        // of the scroll bar, if any.
2451        // Step 6. `Let viewport height` be the height of the viewport excluding the
2452        // height of the scroll bar, if any.
2453        //
2454        // TODO: Servo does not yet support scrollbars.
2455        let viewport = self.viewport_details.get().size;
2456
2457        // Step 7:
2458        // If the viewport has rightward overflow direction
2459        //    Let x be max(0, min(x, viewport scrolling area width - viewport width)).
2460        // If the viewport has leftward overflow direction
2461        //    Let x be min(0, max(x, viewport width - viewport scrolling area width)).
2462        // TODO: Implement this.
2463
2464        // Step 8:
2465        // If the viewport has downward overflow direction
2466        //    Let y be max(0, min(y, viewport scrolling area height - viewport height)).
2467        // If the viewport has upward overflow direction
2468        //    Let y be min(0, max(y, viewport height - viewport scrolling area height)).
2469        // TODO: Implement this.
2470
2471        // Step 9: Let position be the scroll position the viewport would have by aligning
2472        // the x-coordinate x of the viewport scrolling area with the left of the viewport
2473        // and aligning the y-coordinate y of the viewport scrolling area with the top of
2474        // the viewport.
2475        let scrolling_area = self.scrolling_area_query(None).to_f32();
2476        let x = xfinite.clamp(0.0, 0.0f32.max(scrolling_area.width() - viewport.width));
2477        let y = yfinite.clamp(0.0, 0.0f32.max(scrolling_area.height() - viewport.height));
2478
2479        // Step 10: If position is the same as the viewport’s current scroll position, and
2480        // the viewport does not have an ongoing smooth scroll, abort these steps.
2481        let scroll_offset = self.scroll_offset();
2482        if x == scroll_offset.x && y == scroll_offset.y {
2483            return;
2484        }
2485
2486        // Step 11: Let document be the viewport’s associated Document.
2487        // Step 12: Perform a scroll of the viewport to position, document’s root element
2488        // as the associated element, if there is one, or null otherwise, and the scroll
2489        // behavior being the value of the behavior dictionary member of options.
2490        self.perform_a_scroll(x, y, self.pipeline_id().root_scroll_id(), behavior, None);
2491    }
2492
2493    /// <https://drafts.csswg.org/cssom-view/#perform-a-scroll>
2494    pub(crate) fn perform_a_scroll(
2495        &self,
2496        x: f32,
2497        y: f32,
2498        scroll_id: ExternalScrollId,
2499        _behavior: ScrollBehavior,
2500        element: Option<&Element>,
2501    ) {
2502        // TODO Step 1
2503        // TODO(mrobinson, #18709): Add smooth scrolling support to WebRender so that we can
2504        // properly process ScrollBehavior here.
2505        let (reflow_phases_run, _) =
2506            self.reflow(ReflowGoal::UpdateScrollNode(scroll_id, Vector2D::new(x, y)));
2507        if reflow_phases_run.needs_frame() {
2508            self.paint_api()
2509                .generate_frame(vec![self.webview_id().into()]);
2510        }
2511
2512        // > If the scroll position did not change as a result of the user interaction or programmatic
2513        // > invocation, where no translations were applied as a result, then no scrollend event fires
2514        // > because no scrolling occurred.
2515        // Even though the note mention the scrollend, it is relevant to the scroll as well.
2516        if reflow_phases_run.contains(ReflowPhasesRun::UpdatedScrollNodeOffset) {
2517            match element {
2518                Some(el) => self.Document().handle_element_scroll_event(el),
2519                None => self.Document().handle_viewport_scroll_event(),
2520            };
2521        }
2522    }
2523
2524    pub(crate) fn device_pixel_ratio(&self) -> Scale<f32, CSSPixel, DevicePixel> {
2525        self.viewport_details.get().hidpi_scale_factor
2526    }
2527
2528    fn client_window(&self) -> DeviceIndependentIntRect {
2529        let (sender, receiver) = generic_channel::channel().expect("Failed to create IPC channel!");
2530
2531        self.send_to_embedder(EmbedderMsg::GetWindowRect(self.webview_id(), sender));
2532
2533        receiver.recv().unwrap_or_default()
2534    }
2535
2536    /// Prepares to tick animations and then does a reflow which also advances the
2537    /// layout animation clock.
2538    pub(crate) fn advance_animation_clock(&self, delta_ms: i32) {
2539        self.Document()
2540            .advance_animation_timeline_for_testing(delta_ms as f64 / 1000.);
2541        ScriptThread::handle_tick_all_animations_for_testing(self.pipeline_id());
2542    }
2543
2544    /// Reflows the page unconditionally if possible and not suppressed. This method will wait for
2545    /// the layout to complete. If there is no window size yet, the page is presumed invisible and
2546    /// no reflow is performed. If reflow is suppressed, no reflow will be performed for ForDisplay
2547    /// goals.
2548    ///
2549    /// NOTE: This method should almost never be called directly! Layout and rendering updates should
2550    /// happen as part of the HTML event loop via *update the rendering*.
2551    pub(crate) fn reflow(&self, reflow_goal: ReflowGoal) -> (ReflowPhasesRun, ReflowStatistics) {
2552        let document = self.Document();
2553
2554        // Never reflow inactive Documents.
2555        if !document.is_fully_active() {
2556            return Default::default();
2557        }
2558
2559        self.Document().ensure_safe_to_run_script_or_layout();
2560
2561        // If layouts are blocked, we block all layouts that are for display only. Other
2562        // layouts (for queries and scrolling) are not blocked, as they do not display
2563        // anything and script expects the layout to be up-to-date after they run.
2564        let pipeline_id = self.pipeline_id();
2565        if reflow_goal == ReflowGoal::UpdateTheRendering &&
2566            self.layout_blocker.get().layout_blocked()
2567        {
2568            debug!("Suppressing pre-load-event reflow pipeline {pipeline_id}");
2569            return Default::default();
2570        }
2571
2572        debug!("script: performing reflow for goal {reflow_goal:?}");
2573        let marker = if self.need_emit_timeline_marker(TimelineMarkerType::Reflow) {
2574            Some(TimelineMarker::start("Reflow".to_owned()))
2575        } else {
2576            None
2577        };
2578
2579        let restyle_reason = document.restyle_reason();
2580        document.clear_restyle_reasons();
2581        let restyle = if restyle_reason.needs_restyle() {
2582            debug!("Invalidating layout cache due to reflow condition {restyle_reason:?}",);
2583            // Invalidate any existing cached layout values.
2584            self.layout_marker.borrow().set(false);
2585            // Create a new layout caching token.
2586            *self.layout_marker.borrow_mut() = Rc::new(Cell::new(true));
2587
2588            let stylesheets_changed = document.flush_stylesheets_for_reflow();
2589            let pending_restyles = document.drain_pending_restyles();
2590            let dirty_root = document
2591                .take_dirty_root()
2592                .filter(|_| !stylesheets_changed)
2593                .or_else(|| document.GetDocumentElement())
2594                .map(|root| root.upcast::<Node>().to_trusted_node_address());
2595
2596            Some(ReflowRequestRestyle {
2597                reason: restyle_reason,
2598                dirty_root,
2599                stylesheets_changed,
2600                pending_restyles,
2601            })
2602        } else {
2603            None
2604        };
2605
2606        let document_context = self.web_font_context();
2607
2608        // Send new document and relevant styles to layout.
2609        let reflow = ReflowRequest {
2610            document: document.upcast::<Node>().to_trusted_node_address(),
2611            epoch: document.current_rendering_epoch(),
2612            restyle,
2613            viewport_details: self.viewport_details.get(),
2614            origin: self.origin().immutable().clone(),
2615            reflow_goal,
2616            dom_count: document.dom_count(),
2617            animation_timeline_value: document.current_animation_timeline_value(),
2618            animations: document.animations().sets.clone(),
2619            animating_images: document.image_animation_manager().animating_images(),
2620            highlighted_dom_node: document.highlighted_dom_node().map(|node| node.to_opaque()),
2621            document_context,
2622        };
2623
2624        let Some(reflow_result) = self.layout.borrow_mut().reflow(reflow) else {
2625            return Default::default();
2626        };
2627
2628        debug!("script: layout complete");
2629        if let Some(marker) = marker {
2630            self.emit_timeline_marker(marker.end());
2631        }
2632
2633        self.handle_pending_images_post_reflow(
2634            reflow_result.pending_images,
2635            reflow_result.pending_rasterization_images,
2636            reflow_result.pending_svg_elements_for_serialization,
2637        );
2638
2639        if let Some(iframe_sizes) = reflow_result.iframe_sizes {
2640            document
2641                .iframes_mut()
2642                .handle_new_iframe_sizes_after_layout(self, iframe_sizes);
2643        }
2644
2645        document.update_animations_post_reflow();
2646
2647        (
2648            reflow_result.reflow_phases_run,
2649            reflow_result.reflow_statistics,
2650        )
2651    }
2652
2653    pub(crate) fn request_screenshot_readiness(&self, can_gc: CanGc) {
2654        self.has_pending_screenshot_readiness_request.set(true);
2655        self.maybe_resolve_pending_screenshot_readiness_requests(can_gc);
2656    }
2657
2658    pub(crate) fn maybe_resolve_pending_screenshot_readiness_requests(&self, can_gc: CanGc) {
2659        let pending_request = self.has_pending_screenshot_readiness_request.get();
2660        if !pending_request {
2661            return;
2662        }
2663
2664        let document = self.Document();
2665        if document.ReadyState() != DocumentReadyState::Complete {
2666            return;
2667        }
2668
2669        if document.render_blocking_element_count() > 0 {
2670            return;
2671        }
2672
2673        // Checks if the html element has reftest-wait attribute present.
2674        // See http://testthewebforward.org/docs/reftests.html
2675        // and https://web-platform-tests.org/writing-tests/crashtest.html
2676        if document.GetDocumentElement().is_some_and(|elem| {
2677            elem.has_class(&atom!("reftest-wait"), CaseSensitivity::CaseSensitive) ||
2678                elem.has_class(&Atom::from("test-wait"), CaseSensitivity::CaseSensitive)
2679        }) {
2680            return;
2681        }
2682
2683        if self.font_context().web_fonts_still_loading() != 0 {
2684            return;
2685        }
2686
2687        if self.Document().Fonts(can_gc).waiting_to_fullfill_promise() {
2688            return;
2689        }
2690
2691        if !self.pending_layout_images.borrow().is_empty() ||
2692            !self.pending_images_for_rasterization.borrow().is_empty()
2693        {
2694            return;
2695        }
2696
2697        let document = self.Document();
2698        if document.needs_rendering_update() {
2699            return;
2700        }
2701
2702        // When all these conditions are met, notify the Constellation that we are ready to
2703        // have our screenshot taken, when the given layout Epoch has been rendered.
2704        let epoch = document.current_rendering_epoch();
2705        let pipeline_id = self.pipeline_id();
2706        debug!("Ready to take screenshot of {pipeline_id:?} at epoch={epoch:?}");
2707
2708        self.send_to_constellation(
2709            ScriptToConstellationMessage::RespondToScreenshotReadinessRequest(
2710                ScreenshotReadinessResponse::Ready(epoch),
2711            ),
2712        );
2713        self.has_pending_screenshot_readiness_request.set(false);
2714    }
2715
2716    /// If parsing has taken a long time and reflows are still waiting for the `load` event,
2717    /// start allowing them. See <https://github.com/servo/servo/pull/6028>.
2718    pub(crate) fn reflow_if_reflow_timer_expired(&self) {
2719        // Only trigger a long parsing time reflow if we are in the first parse of `<body>`
2720        // and it started more than `INITIAL_REFLOW_DELAY` ago.
2721        if !matches!(
2722            self.layout_blocker.get(),
2723            LayoutBlocker::Parsing(instant) if instant + INITIAL_REFLOW_DELAY < Instant::now()
2724        ) {
2725            return;
2726        }
2727        self.allow_layout_if_necessary();
2728    }
2729
2730    /// Block layout for this `Window` until parsing is done. If parsing takes a long time,
2731    /// we want to layout anyway, so schedule a moment in the future for when layouts are
2732    /// allowed even though parsing isn't finished and we havne't sent a load event.
2733    pub(crate) fn prevent_layout_until_load_event(&self) {
2734        // If we have already started parsing or have already fired a load event, then
2735        // don't delay the first layout any longer.
2736        if !matches!(self.layout_blocker.get(), LayoutBlocker::WaitingForParse) {
2737            return;
2738        }
2739
2740        self.layout_blocker
2741            .set(LayoutBlocker::Parsing(Instant::now()));
2742    }
2743
2744    /// Inform the [`Window`] that layout is allowed either because `load` has happened
2745    /// or because parsing the `<body>` took so long that we cannot wait any longer.
2746    pub(crate) fn allow_layout_if_necessary(&self) {
2747        if matches!(
2748            self.layout_blocker.get(),
2749            LayoutBlocker::FiredLoadEventOrParsingTimerExpired
2750        ) {
2751            return;
2752        }
2753
2754        self.layout_blocker
2755            .set(LayoutBlocker::FiredLoadEventOrParsingTimerExpired);
2756
2757        // We do this immediately instead of scheduling a future task, because this can
2758        // happen if parsing is taking a very long time, which means that the
2759        // `ScriptThread` is busy doing the parsing and not doing layouts.
2760        //
2761        // TOOD(mrobinson): It's expected that this is necessary when in the process of
2762        // parsing, as we need to interrupt it to update contents, but why is this
2763        // necessary when parsing finishes? Not doing the synchronous update in that case
2764        // causes iframe tests to become flaky. It seems there's an issue with the timing of
2765        // iframe size updates.
2766        //
2767        // See <https://github.com/servo/servo/issues/14719>
2768        let document = self.Document();
2769        if !document.is_render_blocked() && document.update_the_rendering().0.needs_frame() {
2770            self.paint_api()
2771                .generate_frame(vec![self.webview_id().into()]);
2772        }
2773    }
2774
2775    pub(crate) fn layout_blocked(&self) -> bool {
2776        self.layout_blocker.get().layout_blocked()
2777    }
2778
2779    /// Trigger a reflow that is required by a certain queries.
2780    pub(crate) fn layout_reflow(&self, query_msg: QueryMsg) {
2781        self.reflow(ReflowGoal::LayoutQuery(query_msg));
2782    }
2783
2784    pub(crate) fn resolved_font_style_query(
2785        &self,
2786        node: &Node,
2787        value: String,
2788    ) -> Option<ServoArc<Font>> {
2789        self.layout_reflow(QueryMsg::ResolvedFontStyleQuery);
2790
2791        let document = self.Document();
2792        let animations = document.animations().sets.clone();
2793        self.layout.borrow().query_resolved_font_style(
2794            node.to_trusted_node_address(),
2795            &value,
2796            animations,
2797            document.current_animation_timeline_value(),
2798        )
2799    }
2800
2801    /// Query the used padding values for the given node, but do not force a reflow.
2802    /// This is used for things like `ResizeObserver` which should observe the value
2803    /// from the most recent reflow, but do not need it to reflect the current state of
2804    /// the DOM / style.
2805    pub(crate) fn padding_query_without_reflow(&self, node: &Node) -> Option<PhysicalSides> {
2806        let layout = self.layout.borrow();
2807        layout.query_padding(node.to_trusted_node_address())
2808    }
2809
2810    /// Do the same kind of query as `Self::box_area_query`, but do not force a reflow.
2811    /// This is used for things like `IntersectionObserver` which should observe the value
2812    /// from the most recent reflow, but do not need it to reflect the current state of
2813    /// the DOM / style.
2814    pub(crate) fn box_area_query_without_reflow(
2815        &self,
2816        node: &Node,
2817        area: BoxAreaType,
2818        exclude_transform_and_inline: bool,
2819    ) -> Option<Rect<Au, CSSPixel>> {
2820        let layout = self.layout.borrow();
2821        layout.ensure_stacking_context_tree(self.viewport_details.get());
2822        layout.query_box_area(
2823            node.to_trusted_node_address(),
2824            area,
2825            exclude_transform_and_inline,
2826        )
2827    }
2828
2829    pub(crate) fn box_area_query(
2830        &self,
2831        node: &Node,
2832        area: BoxAreaType,
2833        exclude_transform_and_inline: bool,
2834    ) -> Option<Rect<Au, CSSPixel>> {
2835        self.layout_reflow(QueryMsg::BoxArea);
2836        self.box_area_query_without_reflow(node, area, exclude_transform_and_inline)
2837    }
2838
2839    pub(crate) fn box_areas_query(&self, node: &Node, area: BoxAreaType) -> CSSPixelRectIterator {
2840        self.layout_reflow(QueryMsg::BoxAreas);
2841        self.layout
2842            .borrow()
2843            .query_box_areas(node.to_trusted_node_address(), area)
2844    }
2845
2846    pub(crate) fn client_rect_query(&self, node: &Node) -> Rect<i32, CSSPixel> {
2847        self.layout_reflow(QueryMsg::ClientRectQuery);
2848        self.layout
2849            .borrow()
2850            .query_client_rect(node.to_trusted_node_address())
2851    }
2852
2853    pub(crate) fn current_css_zoom_query(&self, node: &Node) -> f32 {
2854        self.layout_reflow(QueryMsg::CurrentCSSZoomQuery);
2855        self.layout
2856            .borrow()
2857            .query_current_css_zoom(node.to_trusted_node_address())
2858    }
2859
2860    /// Find the scroll area of the given node, if it is not None. If the node
2861    /// is None, find the scroll area of the viewport.
2862    pub(crate) fn scrolling_area_query(&self, node: Option<&Node>) -> Rect<i32, CSSPixel> {
2863        self.layout_reflow(QueryMsg::ScrollingAreaOrOffsetQuery);
2864        self.layout
2865            .borrow()
2866            .query_scrolling_area(node.map(Node::to_trusted_node_address))
2867    }
2868
2869    pub(crate) fn scroll_offset_query(&self, node: &Node) -> Vector2D<f32, LayoutPixel> {
2870        let external_scroll_id = ExternalScrollId(
2871            combine_id_with_fragment_type(node.to_opaque().id(), FragmentType::FragmentBody),
2872            self.pipeline_id().into(),
2873        );
2874        self.scroll_offset_query_with_external_scroll_id(external_scroll_id)
2875    }
2876
2877    fn scroll_offset_query_with_external_scroll_id(
2878        &self,
2879        external_scroll_id: ExternalScrollId,
2880    ) -> Vector2D<f32, LayoutPixel> {
2881        self.layout_reflow(QueryMsg::ScrollingAreaOrOffsetQuery);
2882        self.scroll_offset_query_with_external_scroll_id_no_reflow(external_scroll_id)
2883    }
2884
2885    fn scroll_offset_query_with_external_scroll_id_no_reflow(
2886        &self,
2887        external_scroll_id: ExternalScrollId,
2888    ) -> Vector2D<f32, LayoutPixel> {
2889        self.layout
2890            .borrow()
2891            .scroll_offset(external_scroll_id)
2892            .unwrap_or_default()
2893    }
2894
2895    /// <https://drafts.csswg.org/cssom-view/#scroll-an-element>
2896    // TODO(stevennovaryo): Need to update the scroll API to follow the spec since it is quite outdated.
2897    pub(crate) fn scroll_an_element(
2898        &self,
2899        element: &Element,
2900        x: f32,
2901        y: f32,
2902        behavior: ScrollBehavior,
2903    ) {
2904        let scroll_id = ExternalScrollId(
2905            combine_id_with_fragment_type(
2906                element.upcast::<Node>().to_opaque().id(),
2907                FragmentType::FragmentBody,
2908            ),
2909            self.pipeline_id().into(),
2910        );
2911
2912        // Step 6.
2913        // > Perform a scroll of box to position, element as the associated element and behavior as
2914        // > the scroll behavior.
2915        self.perform_a_scroll(x, y, scroll_id, behavior, Some(element));
2916    }
2917
2918    pub(crate) fn resolved_style_query(
2919        &self,
2920        element: TrustedNodeAddress,
2921        pseudo: Option<PseudoElement>,
2922        property: PropertyId,
2923    ) -> DOMString {
2924        self.layout_reflow(QueryMsg::ResolvedStyleQuery);
2925
2926        let document = self.Document();
2927        let animations = document.animations().sets.clone();
2928        DOMString::from(self.layout.borrow().query_resolved_style(
2929            element,
2930            pseudo,
2931            property,
2932            animations,
2933            document.current_animation_timeline_value(),
2934        ))
2935    }
2936
2937    /// If the given |browsing_context_id| refers to an `<iframe>` that is an element
2938    /// in this [`Window`] and that `<iframe>` has been laid out, return its size.
2939    /// Otherwise, return `None`.
2940    pub(crate) fn get_iframe_viewport_details_if_known(
2941        &self,
2942        browsing_context_id: BrowsingContextId,
2943    ) -> Option<ViewportDetails> {
2944        // Reflow might fail, but do a best effort to return the right size.
2945        self.layout_reflow(QueryMsg::InnerWindowDimensionsQuery);
2946        self.Document()
2947            .iframes()
2948            .get(browsing_context_id)
2949            .and_then(|iframe| iframe.size)
2950    }
2951
2952    #[expect(unsafe_code)]
2953    pub(crate) fn offset_parent_query(
2954        &self,
2955        node: &Node,
2956    ) -> (Option<DomRoot<Element>>, Rect<Au, CSSPixel>) {
2957        self.layout_reflow(QueryMsg::OffsetParentQuery);
2958        let response = self
2959            .layout
2960            .borrow()
2961            .query_offset_parent(node.to_trusted_node_address());
2962        let element = response.node_address.and_then(|parent_node_address| {
2963            let node = unsafe { from_untrusted_node_address(parent_node_address) };
2964            DomRoot::downcast(node)
2965        });
2966        (element, response.rect)
2967    }
2968
2969    pub(crate) fn scroll_container_query(
2970        &self,
2971        node: Option<&Node>,
2972        flags: ScrollContainerQueryFlags,
2973    ) -> Option<ScrollContainerResponse> {
2974        self.layout_reflow(QueryMsg::ScrollParentQuery);
2975        self.layout
2976            .borrow()
2977            .query_scroll_container(node.map(Node::to_trusted_node_address), flags)
2978    }
2979
2980    #[expect(unsafe_code)]
2981    pub(crate) fn scrolling_box_query(
2982        &self,
2983        node: Option<&Node>,
2984        flags: ScrollContainerQueryFlags,
2985    ) -> Option<ScrollingBox> {
2986        self.scroll_container_query(node, flags)
2987            .and_then(|response| {
2988                Some(match response {
2989                    ScrollContainerResponse::Viewport(overflow) => {
2990                        (ScrollingBoxSource::Viewport(self.Document()), overflow)
2991                    },
2992                    ScrollContainerResponse::Element(parent_node_address, overflow) => {
2993                        let node = unsafe { from_untrusted_node_address(parent_node_address) };
2994                        (
2995                            ScrollingBoxSource::Element(DomRoot::downcast(node)?),
2996                            overflow,
2997                        )
2998                    },
2999                })
3000            })
3001            .map(|(source, overflow)| ScrollingBox::new(source, overflow))
3002    }
3003
3004    pub(crate) fn text_index_query_on_node_for_event(
3005        &self,
3006        node: &Node,
3007        mouse_event: &MouseEvent,
3008    ) -> Option<usize> {
3009        // dispatch_key_event (document.rs) triggers a click event when releasing
3010        // the space key. There's no nice way to catch this so let's use this for
3011        // now.
3012        let point_in_viewport = mouse_event.point_in_viewport()?.map(Au::from_f32_px);
3013
3014        self.layout_reflow(QueryMsg::TextIndexQuery);
3015        self.layout
3016            .borrow()
3017            .query_text_index(node.to_trusted_node_address(), point_in_viewport)
3018    }
3019
3020    pub(crate) fn elements_from_point_query(
3021        &self,
3022        point: LayoutPoint,
3023        flags: ElementsFromPointFlags,
3024    ) -> Vec<ElementsFromPointResult> {
3025        self.layout_reflow(QueryMsg::ElementsFromPoint);
3026        self.layout().query_elements_from_point(point, flags)
3027    }
3028
3029    pub(crate) fn query_effective_overflow(&self, node: &Node) -> Option<AxesOverflow> {
3030        self.layout_reflow(QueryMsg::EffectiveOverflow);
3031        self.query_effective_overflow_without_reflow(node)
3032    }
3033
3034    pub(crate) fn query_effective_overflow_without_reflow(
3035        &self,
3036        node: &Node,
3037    ) -> Option<AxesOverflow> {
3038        self.layout
3039            .borrow()
3040            .query_effective_overflow(node.to_trusted_node_address())
3041    }
3042
3043    pub(crate) fn hit_test_from_input_event(
3044        &self,
3045        input_event: &ConstellationInputEvent,
3046    ) -> Option<HitTestResult> {
3047        self.hit_test_from_point_in_viewport(
3048            input_event.hit_test_result.as_ref()?.point_in_viewport,
3049        )
3050    }
3051
3052    #[expect(unsafe_code)]
3053    pub(crate) fn hit_test_from_point_in_viewport(
3054        &self,
3055        point_in_frame: Point2D<f32, CSSPixel>,
3056    ) -> Option<HitTestResult> {
3057        let result = self
3058            .elements_from_point_query(point_in_frame.cast_unit(), ElementsFromPointFlags::empty())
3059            .into_iter()
3060            .nth(0)?;
3061
3062        let point_relative_to_initial_containing_block =
3063            point_in_frame + self.scroll_offset().cast_unit();
3064
3065        // SAFETY: This is safe because `Window::query_elements_from_point` has ensured that
3066        // layout has run and any OpaqueNodes that no longer refer to real nodes are gone.
3067        let address = UntrustedNodeAddress(result.node.0 as *const c_void);
3068        Some(HitTestResult {
3069            node: unsafe { from_untrusted_node_address(address) },
3070            cursor: result.cursor,
3071            point_in_node: result.point_in_target,
3072            point_in_frame,
3073            point_relative_to_initial_containing_block,
3074        })
3075    }
3076
3077    pub(crate) fn init_window_proxy(&self, window_proxy: &WindowProxy) {
3078        assert!(self.window_proxy.get().is_none());
3079        self.window_proxy.set(Some(window_proxy));
3080    }
3081
3082    pub(crate) fn init_document(&self, document: &Document) {
3083        assert!(self.document.get().is_none());
3084        assert!(document.window() == self);
3085        self.document.set(Some(document));
3086
3087        if self.unminify_css {
3088            *self.unminified_css_dir.borrow_mut() = Some(unminified_path("unminified-css"));
3089        }
3090    }
3091
3092    /// <https://html.spec.whatwg.org/multipage/#navigate-fragid>
3093    fn navigate_to_fragment(&self, url: &ServoUrl, history_handling: NavigationHistoryBehavior) {
3094        let doc = self.Document();
3095        // Step 1. Let navigation be navigable's active window's navigation API.
3096        // TODO
3097        // Step 2. Let destinationNavigationAPIState be navigable's active session history entry's navigation API state.
3098        // TODO
3099        // Step 3. If navigationAPIState is not null, then set destinationNavigationAPIState to navigationAPIState.
3100        // TODO
3101
3102        // Step 4. Let continue be the result of firing a push/replace/reload navigate event
3103        // at navigation with navigationType set to historyHandling, isSameDocument set to true,
3104        // userInvolvement set to userInvolvement, sourceElement set to sourceElement,
3105        // destinationURL set to url, and navigationAPIState set to destinationNavigationAPIState.
3106        // TODO
3107        // Step 5. If continue is false, then return.
3108        // TODO
3109
3110        // Step 6. Let historyEntry be a new session history entry, with
3111        // Step 7. Let entryToReplace be navigable's active session history entry if historyHandling is "replace", otherwise null.
3112        // Step 8. Let history be navigable's active document's history object.
3113        // Step 9. Let scriptHistoryIndex be history's index.
3114        // Step 10. Let scriptHistoryLength be history's length.
3115        // Step 11. If historyHandling is "push", then:
3116        // Step 13. Set navigable's active session history entry to historyEntry.
3117        self.send_to_constellation(ScriptToConstellationMessage::NavigatedToFragment(
3118            url.clone(),
3119            history_handling,
3120        ));
3121        // Step 12. Set navigable's active document's URL to url.
3122        let old_url = doc.url();
3123        doc.set_url(url.clone());
3124        // Step 14. Update document for history step application given navigable's active document,
3125        // historyEntry, true, scriptHistoryIndex, scriptHistoryLength, and historyHandling.
3126        doc.update_document_for_history_step_application(&old_url, url);
3127        // Step 15. Scroll to the fragment given navigable's active document.
3128        let Some(fragment) = url.fragment() else {
3129            unreachable!("Must always have a fragment");
3130        };
3131        doc.scroll_to_the_fragment(fragment);
3132        // Step 16. Let traversable be navigable's traversable navigable.
3133        // TODO
3134        // Step 17. Append the following session history synchronous navigation steps involving navigable to traversable:
3135        // TODO
3136    }
3137
3138    pub(crate) fn load_data_for_document(
3139        &self,
3140        url: ServoUrl,
3141        pipeline_id: PipelineId,
3142    ) -> LoadData {
3143        let source_document = self.Document();
3144        let secure_context = if self.is_top_level() {
3145            None
3146        } else {
3147            Some(self.IsSecureContext())
3148        };
3149        LoadData::new(
3150            LoadOrigin::Script(self.origin().snapshot()),
3151            url,
3152            source_document.about_base_url(),
3153            Some(pipeline_id),
3154            Referrer::ReferrerUrl(source_document.url()),
3155            source_document.get_referrer_policy(),
3156            secure_context,
3157            Some(source_document.insecure_requests_policy()),
3158            source_document.has_trustworthy_ancestor_origin(),
3159            source_document.creation_sandboxing_flag_set_considering_parent_iframe(),
3160        )
3161    }
3162
3163    /// <https://html.spec.whatwg.org/multipage/#navigate>
3164    pub(crate) fn load_url(
3165        &self,
3166        history_handling: NavigationHistoryBehavior,
3167        force_reload: bool,
3168        load_data: LoadData,
3169        can_gc: CanGc,
3170    ) {
3171        let doc = self.Document();
3172
3173        // Step 3. Let initiatorOriginSnapshot be sourceDocument's origin.
3174        let initiator_origin_snapshot = &load_data.load_origin;
3175
3176        // TODO: Important re security. See https://github.com/servo/servo/issues/23373
3177        // Step 5. check that the source browsing-context is "allowed to navigate" this window.
3178
3179        // Step 4 and 5
3180        let pipeline_id = self.pipeline_id();
3181        let window_proxy = self.window_proxy();
3182        if let Some(active) = window_proxy.currently_active() {
3183            if pipeline_id == active && doc.is_prompting_or_unloading() {
3184                return;
3185            }
3186        }
3187
3188        // Step 23. Let unloadPromptCanceled be the result of checking if unloading
3189        // is canceled for navigable's active document's inclusive descendant navigables.
3190        if doc.check_if_unloading_is_cancelled(false, can_gc) {
3191            // Step 12. If historyHandling is "auto", then:
3192            let history_handling = if history_handling == NavigationHistoryBehavior::Auto {
3193                // Step 12.1. If url equals navigable's active document's URL, and
3194                // initiatorOriginSnapshot is same origin with targetNavigable's active document's
3195                // origin, then set historyHandling to "replace".
3196                //
3197                // Note: `targetNavigable` is not actually defined in the spec, "active document" is
3198                // assumed to be the correct reference based on WPT results
3199                if let LoadOrigin::Script(initiator_origin) = initiator_origin_snapshot {
3200                    if load_data.url == doc.url() && initiator_origin.same_origin(doc.origin()) {
3201                        NavigationHistoryBehavior::Replace
3202                    } else {
3203                        // Step 12.2. Otherwise, set historyHandling to "push".
3204                        NavigationHistoryBehavior::Push
3205                    }
3206                } else {
3207                    // Step 12.2. Otherwise, set historyHandling to "push".
3208                    NavigationHistoryBehavior::Push
3209                }
3210            } else {
3211                history_handling
3212            };
3213            // Step 13. If the navigation must be a replace given url and navigable's active
3214            // document, then set historyHandling to "replace".
3215            //
3216            // Inlines implementation of https://html.spec.whatwg.org/multipage/#the-navigation-must-be-a-replace
3217            let history_handling =
3218                if load_data.url.scheme() == "javascript" || doc.is_initial_about_blank() {
3219                    NavigationHistoryBehavior::Replace
3220                } else {
3221                    history_handling
3222                };
3223
3224            // Step 14. If all of the following are true:
3225            // > documentResource is null;
3226            // > response is null;
3227            if !force_reload
3228                // > url equals navigable's active session history entry's URL with exclude fragments set to true; and
3229                && load_data.url.as_url()[..Position::AfterQuery] ==
3230                    doc.url().as_url()[..Position::AfterQuery]
3231                // > url's fragment is non-null,
3232                && load_data.url.fragment().is_some()
3233            {
3234                // Step 14.1. Navigate to a fragment given navigable, url, historyHandling,
3235                // userInvolvement, sourceElement, navigationAPIState, and navigationId.
3236                let webdriver_sender = self.webdriver_load_status_sender.borrow().clone();
3237                if let Some(ref sender) = webdriver_sender {
3238                    let _ = sender.send(WebDriverLoadStatus::NavigationStart);
3239                }
3240                self.navigate_to_fragment(&load_data.url, history_handling);
3241                // Step 14.2. Return.
3242                if let Some(sender) = webdriver_sender {
3243                    let _ = sender.send(WebDriverLoadStatus::NavigationStop);
3244                }
3245                return;
3246            }
3247
3248            // Step 15. If navigable's parent is non-null, then set navigable's is delaying load events to true.
3249            let window_proxy = self.window_proxy();
3250            if window_proxy.parent().is_some() {
3251                window_proxy.start_delaying_load_events_mode();
3252            }
3253
3254            if let Some(sender) = self.webdriver_load_status_sender.borrow().as_ref() {
3255                let _ = sender.send(WebDriverLoadStatus::NavigationStart);
3256            }
3257
3258            // Step 13
3259            ScriptThread::navigate(self.webview_id, pipeline_id, load_data, history_handling);
3260        };
3261    }
3262
3263    /// Handle a potential change to the [`ViewportDetails`] of this [`Window`],
3264    /// triggering a reflow if any change occurred.
3265    pub(crate) fn set_viewport_details(&self, viewport_details: ViewportDetails) {
3266        self.viewport_details.set(viewport_details);
3267        if !self.layout_mut().set_viewport_details(viewport_details) {
3268            return;
3269        }
3270        self.Document()
3271            .add_restyle_reason(RestyleReason::ViewportChanged);
3272    }
3273
3274    pub(crate) fn viewport_details(&self) -> ViewportDetails {
3275        self.viewport_details.get()
3276    }
3277
3278    pub(crate) fn get_or_init_visual_viewport(&self, can_gc: CanGc) -> DomRoot<VisualViewport> {
3279        self.visual_viewport.or_init(|| {
3280            VisualViewport::new_from_layout_viewport(self, self.viewport_details().size, can_gc)
3281        })
3282    }
3283
3284    /// Update the [`VisualViewport`] of this [`Window`] if necessary and note the changes to be processed in the event loop.
3285    pub(crate) fn maybe_update_visual_viewport(
3286        &self,
3287        pinch_zoom_infos: PinchZoomInfos,
3288        can_gc: CanGc,
3289    ) {
3290        // We doesn't need to do anything if the following condition is fulfilled. Since there are no JS listener
3291        // to fire and we could reconstruct visual viewport from layout viewport in case JS access it.
3292        if pinch_zoom_infos.rect == Rect::from_size(self.viewport_details().size) &&
3293            self.visual_viewport.get().is_none()
3294        {
3295            return;
3296        }
3297
3298        let visual_viewport = self.get_or_init_visual_viewport(can_gc);
3299        let changes = visual_viewport.update_from_pinch_zoom_infos(pinch_zoom_infos);
3300
3301        if changes.intersects(VisualViewportChanges::DimensionChanged) {
3302            self.has_changed_visual_viewport_dimension.set(true);
3303        }
3304        if changes.intersects(VisualViewportChanges::OffsetChanged) {
3305            self.Document()
3306                .handle_visual_viewport_scroll_event(&visual_viewport);
3307        }
3308    }
3309
3310    /// Get the theme of this [`Window`].
3311    pub(crate) fn theme(&self) -> Theme {
3312        self.theme.get()
3313    }
3314
3315    /// Handle a theme change request, triggering a reflow is any actual change occurred.
3316    pub(crate) fn set_theme(&self, new_theme: Theme) {
3317        self.theme.set(new_theme);
3318        if !self.layout_mut().set_theme(new_theme) {
3319            return;
3320        }
3321        self.Document()
3322            .add_restyle_reason(RestyleReason::ThemeChanged);
3323    }
3324
3325    pub(crate) fn get_url(&self) -> ServoUrl {
3326        self.Document().url()
3327    }
3328
3329    pub(crate) fn windowproxy_handler(&self) -> &'static WindowProxyHandler {
3330        self.dom_static.windowproxy_handler
3331    }
3332
3333    pub(crate) fn add_resize_event(&self, event: ViewportDetails, event_type: WindowSizeType) {
3334        // Whenever we receive a new resize event we forget about all the ones that came before
3335        // it, to avoid unnecessary relayouts
3336        *self.unhandled_resize_event.borrow_mut() = Some((event, event_type))
3337    }
3338
3339    pub(crate) fn take_unhandled_resize_event(&self) -> Option<(ViewportDetails, WindowSizeType)> {
3340        self.unhandled_resize_event.borrow_mut().take()
3341    }
3342
3343    /// Whether or not this [`Window`] has any resize events that have not been processed.
3344    pub(crate) fn has_unhandled_resize_event(&self) -> bool {
3345        self.unhandled_resize_event.borrow().is_some()
3346    }
3347
3348    pub(crate) fn suspend(&self, cx: &mut js::context::JSContext) {
3349        // Suspend timer events.
3350        self.as_global_scope().suspend();
3351
3352        // Set the window proxy to be a cross-origin window.
3353        if self.window_proxy().currently_active() == Some(self.global().pipeline_id()) {
3354            self.window_proxy().unset_currently_active(cx);
3355        }
3356
3357        // A hint to the JS runtime that now would be a good time to
3358        // GC any unreachable objects generated by user script,
3359        // or unattached DOM nodes. Attached DOM nodes can't be GCd yet,
3360        // as the document might be reactivated later.
3361        self.gc();
3362    }
3363
3364    pub(crate) fn resume(&self, can_gc: CanGc) {
3365        // Resume timer events.
3366        self.as_global_scope().resume();
3367
3368        // Set the window proxy to be this object.
3369        self.window_proxy().set_currently_active(self, can_gc);
3370
3371        // Push the document title to `Paint` since we are
3372        // activating this document due to a navigation.
3373        self.Document().title_changed();
3374    }
3375
3376    pub(crate) fn need_emit_timeline_marker(&self, timeline_type: TimelineMarkerType) -> bool {
3377        let markers = self.devtools_markers.borrow();
3378        markers.contains(&timeline_type)
3379    }
3380
3381    pub(crate) fn emit_timeline_marker(&self, marker: TimelineMarker) {
3382        let sender = self.devtools_marker_sender.borrow();
3383        let sender = sender.as_ref().expect("There is no marker sender");
3384        sender.send(Some(marker)).unwrap();
3385    }
3386
3387    pub(crate) fn set_devtools_timeline_markers(
3388        &self,
3389        markers: Vec<TimelineMarkerType>,
3390        reply: GenericSender<Option<TimelineMarker>>,
3391    ) {
3392        *self.devtools_marker_sender.borrow_mut() = Some(reply);
3393        self.devtools_markers.borrow_mut().extend(markers);
3394    }
3395
3396    pub(crate) fn drop_devtools_timeline_markers(&self, markers: Vec<TimelineMarkerType>) {
3397        let mut devtools_markers = self.devtools_markers.borrow_mut();
3398        for marker in markers {
3399            devtools_markers.remove(&marker);
3400        }
3401        if devtools_markers.is_empty() {
3402            *self.devtools_marker_sender.borrow_mut() = None;
3403        }
3404    }
3405
3406    pub(crate) fn set_webdriver_script_chan(&self, chan: Option<GenericSender<WebDriverJSResult>>) {
3407        *self.webdriver_script_chan.borrow_mut() = chan;
3408    }
3409
3410    pub(crate) fn set_webdriver_load_status_sender(
3411        &self,
3412        sender: Option<GenericSender<WebDriverLoadStatus>>,
3413    ) {
3414        *self.webdriver_load_status_sender.borrow_mut() = sender;
3415    }
3416
3417    pub(crate) fn is_alive(&self) -> bool {
3418        self.current_state.get() == WindowState::Alive
3419    }
3420
3421    // https://html.spec.whatwg.org/multipage/#top-level-browsing-context
3422    pub(crate) fn is_top_level(&self) -> bool {
3423        self.parent_info.is_none()
3424    }
3425
3426    /// Layout viewport part of:
3427    /// <https://drafts.csswg.org/cssom-view/#document-run-the-resize-steps>
3428    ///
3429    /// Handle the pending viewport resize.
3430    fn run_resize_steps_for_layout_viewport(&self, can_gc: CanGc) -> bool {
3431        let Some((new_size, size_type)) = self.take_unhandled_resize_event() else {
3432            return false;
3433        };
3434
3435        if self.viewport_details() == new_size {
3436            return false;
3437        }
3438
3439        let _realm = enter_realm(self);
3440        debug!(
3441            "Resizing Window for pipeline {:?} from {:?} to {new_size:?}",
3442            self.pipeline_id(),
3443            self.viewport_details(),
3444        );
3445        self.set_viewport_details(new_size);
3446
3447        // The document needs to be repainted, because the initial containing
3448        // block is now a different size. This should be triggered before the
3449        // event is fired below so that any script queries trigger a restyle.
3450        self.Document()
3451            .add_restyle_reason(RestyleReason::ViewportChanged);
3452
3453        // If viewport units were used, all nodes need to be restyled, because
3454        // we currently do not track which ones rely on viewport units.
3455        if self.layout().device().used_viewport_size() {
3456            self.Document().dirty_all_nodes();
3457        }
3458
3459        // http://dev.w3.org/csswg/cssom-view/#resizing-viewports
3460        if size_type == WindowSizeType::Resize {
3461            let uievent = UIEvent::new(
3462                self,
3463                atom!("resize"),
3464                EventBubbles::DoesNotBubble,
3465                EventCancelable::NotCancelable,
3466                Some(self),
3467                0i32,
3468                0u32,
3469                can_gc,
3470            );
3471            uievent.upcast::<Event>().fire(self.upcast(), can_gc);
3472        }
3473
3474        true
3475    }
3476
3477    /// An implementation of:
3478    /// <https://drafts.csswg.org/cssom-view/#document-run-the-resize-steps>
3479    ///
3480    /// Returns true if there were any pending viewport resize events.
3481    pub(crate) fn run_the_resize_steps(&self, can_gc: CanGc) -> bool {
3482        let layout_viewport_resized = self.run_resize_steps_for_layout_viewport(can_gc);
3483
3484        if self.has_changed_visual_viewport_dimension.get() {
3485            let visual_viewport = self.get_or_init_visual_viewport(can_gc);
3486
3487            let uievent = UIEvent::new(
3488                self,
3489                atom!("resize"),
3490                EventBubbles::DoesNotBubble,
3491                EventCancelable::NotCancelable,
3492                Some(self),
3493                0i32,
3494                0u32,
3495                can_gc,
3496            );
3497            uievent
3498                .upcast::<Event>()
3499                .fire(visual_viewport.upcast(), can_gc);
3500
3501            self.has_changed_visual_viewport_dimension.set(false);
3502        }
3503
3504        layout_viewport_resized
3505    }
3506
3507    /// Evaluate media query lists and report changes
3508    /// <https://drafts.csswg.org/cssom-view/#evaluate-media-queries-and-report-changes>
3509    pub(crate) fn evaluate_media_queries_and_report_changes(&self, can_gc: CanGc) {
3510        let _realm = enter_realm(self);
3511
3512        rooted_vec!(let mut mql_list);
3513        self.media_query_lists.for_each(|mql| {
3514            if let MediaQueryListMatchState::Changed = mql.evaluate_changes() {
3515                // Recording list of changed Media Queries
3516                mql_list.push(Dom::from_ref(&*mql));
3517            }
3518        });
3519        // Sending change events for all changed Media Queries
3520        for mql in mql_list.iter() {
3521            let event = MediaQueryListEvent::new(
3522                &mql.global(),
3523                atom!("change"),
3524                false,
3525                false,
3526                mql.Media(),
3527                mql.Matches(),
3528                can_gc,
3529            );
3530            event
3531                .upcast::<Event>()
3532                .fire(mql.upcast::<EventTarget>(), can_gc);
3533        }
3534    }
3535
3536    /// Set whether to use less resources by running timers at a heavily limited rate.
3537    pub(crate) fn set_throttled(&self, throttled: bool) {
3538        self.throttled.set(throttled);
3539        if throttled {
3540            self.as_global_scope().slow_down_timers();
3541        } else {
3542            self.as_global_scope().speed_up_timers();
3543        }
3544    }
3545
3546    pub(crate) fn throttled(&self) -> bool {
3547        self.throttled.get()
3548    }
3549
3550    pub(crate) fn unminified_css_dir(&self) -> Option<String> {
3551        self.unminified_css_dir.borrow().clone()
3552    }
3553
3554    pub(crate) fn local_script_source(&self) -> &Option<String> {
3555        &self.local_script_source
3556    }
3557
3558    pub(crate) fn set_navigation_start(&self) {
3559        self.navigation_start.set(CrossProcessInstant::now());
3560    }
3561
3562    pub(crate) fn set_last_activation_timestamp(&self, time: UserActivationTimestamp) {
3563        self.last_activation_timestamp.set(time);
3564    }
3565
3566    pub(crate) fn send_to_embedder(&self, msg: EmbedderMsg) {
3567        self.as_global_scope()
3568            .script_to_embedder_chan()
3569            .send(msg)
3570            .unwrap();
3571    }
3572
3573    pub(crate) fn send_to_constellation(&self, msg: ScriptToConstellationMessage) {
3574        self.as_global_scope()
3575            .script_to_constellation_chan()
3576            .send(msg)
3577            .unwrap();
3578    }
3579
3580    #[cfg(feature = "webxr")]
3581    pub(crate) fn in_immersive_xr_session(&self) -> bool {
3582        self.navigator
3583            .get()
3584            .as_ref()
3585            .and_then(|nav| nav.xr())
3586            .is_some_and(|xr| xr.pending_or_active_session())
3587    }
3588
3589    #[cfg(not(feature = "webxr"))]
3590    pub(crate) fn in_immersive_xr_session(&self) -> bool {
3591        false
3592    }
3593
3594    #[expect(unsafe_code)]
3595    fn handle_pending_images_post_reflow(
3596        &self,
3597        pending_images: Vec<PendingImage>,
3598        pending_rasterization_images: Vec<PendingRasterizationImage>,
3599        pending_svg_element_for_serialization: Vec<UntrustedNodeAddress>,
3600    ) {
3601        let pipeline_id = self.pipeline_id();
3602        for image in pending_images {
3603            let id = image.id;
3604            let node = unsafe { from_untrusted_node_address(image.node) };
3605
3606            if let PendingImageState::Unrequested(ref url) = image.state {
3607                fetch_image_for_layout(url.clone(), &node, id, self.image_cache.clone());
3608            }
3609
3610            let mut images = self.pending_layout_images.borrow_mut();
3611            if !images.contains_key(&id) {
3612                let trusted_node = Trusted::new(&*node);
3613                let sender = self.register_image_cache_listener(id, move |response, _| {
3614                    trusted_node
3615                        .root()
3616                        .owner_window()
3617                        .pending_layout_image_notification(response);
3618                });
3619
3620                self.image_cache
3621                    .add_listener(ImageLoadListener::new(sender, pipeline_id, id));
3622            }
3623
3624            let nodes = images.entry(id).or_default();
3625            if !nodes.iter().any(|n| std::ptr::eq(&*(n.node), &*node)) {
3626                nodes.push(PendingLayoutImageAncillaryData {
3627                    node: Dom::from_ref(&*node),
3628                    destination: image.destination,
3629                });
3630            }
3631        }
3632
3633        for image in pending_rasterization_images {
3634            let node = unsafe { from_untrusted_node_address(image.node) };
3635
3636            let mut images = self.pending_images_for_rasterization.borrow_mut();
3637            if !images.contains_key(&(image.id, image.size)) {
3638                let image_cache_sender = self.image_cache_sender.clone();
3639                self.image_cache.add_rasterization_complete_listener(
3640                    pipeline_id,
3641                    image.id,
3642                    image.size,
3643                    Box::new(move |response| {
3644                        let _ = image_cache_sender.send(response);
3645                    }),
3646                );
3647            }
3648
3649            let nodes = images.entry((image.id, image.size)).or_default();
3650            if !nodes.iter().any(|n| std::ptr::eq(&**n, &*node)) {
3651                nodes.push(Dom::from_ref(&*node));
3652            }
3653        }
3654
3655        for node in pending_svg_element_for_serialization.into_iter() {
3656            let node = unsafe { from_untrusted_node_address(node) };
3657            let svg = node.downcast::<SVGSVGElement>().unwrap();
3658            svg.serialize_and_cache_subtree();
3659            node.dirty(NodeDamage::Other);
3660        }
3661    }
3662
3663    /// <https://html.spec.whatwg.org/multipage/#sticky-activation>
3664    pub(crate) fn has_sticky_activation(&self) -> bool {
3665        // > When the current high resolution time given W is greater than or equal to the last activation timestamp in W, W is said to have sticky activation.
3666        UserActivationTimestamp::TimeStamp(CrossProcessInstant::now()) >=
3667            self.last_activation_timestamp.get()
3668    }
3669
3670    /// <https://html.spec.whatwg.org/multipage/#transient-activation>
3671    pub(crate) fn has_transient_activation(&self) -> bool {
3672        // > When the current high resolution time given W is greater than or equal to the last activation timestamp in W, and less than the last activation
3673        // > timestamp in W plus the transient activation duration, then W is said to have transient activation.
3674        let current_time = CrossProcessInstant::now();
3675        UserActivationTimestamp::TimeStamp(current_time) >= self.last_activation_timestamp.get() &&
3676            UserActivationTimestamp::TimeStamp(current_time) <
3677                self.last_activation_timestamp.get() +
3678                    pref!(dom_transient_activation_duration_ms)
3679    }
3680
3681    pub(crate) fn consume_last_activation_timestamp(&self) {
3682        if self.last_activation_timestamp.get() != UserActivationTimestamp::PositiveInfinity {
3683            self.set_last_activation_timestamp(UserActivationTimestamp::NegativeInfinity);
3684        }
3685    }
3686
3687    /// <https://html.spec.whatwg.org/multipage/#consume-user-activation>
3688    pub(crate) fn consume_user_activation(&self) {
3689        // Step 1.
3690        // > If W's navigable is null, then return.
3691        if self.undiscarded_window_proxy().is_none() {
3692            return;
3693        }
3694
3695        // Step 2.
3696        // > Let top be W's navigable's top-level traversable.
3697        // TODO: This wouldn't work if top level document is in another ScriptThread.
3698        let Some(top_level_document) = self.top_level_document_if_local() else {
3699            return;
3700        };
3701
3702        // Step 3.
3703        // > Let navigables be the inclusive descendant navigables of top's active document.
3704        // Step 4.
3705        // > Let windows be the list of Window objects constructed by taking the active window of each item in navigables.
3706        // Step 5.
3707        // > For each window in windows, if window's last activation timestamp is not positive infinity, then set window's last activation timestamp to negative infinity.
3708        // TODO: this would not work for disimilar origin descendant, since we doesn't store the document in this script thread.
3709        top_level_document
3710            .window()
3711            .consume_last_activation_timestamp();
3712        for document in SameOriginDescendantNavigablesIterator::new(top_level_document) {
3713            document.window().consume_last_activation_timestamp();
3714        }
3715    }
3716
3717    #[allow(clippy::too_many_arguments)]
3718    pub(crate) fn new(
3719        cx: &mut js::context::JSContext,
3720        webview_id: WebViewId,
3721        runtime: Rc<Runtime>,
3722        script_chan: Sender<MainThreadScriptMsg>,
3723        layout: Box<dyn Layout>,
3724        font_context: Arc<FontContext>,
3725        image_cache_sender: Sender<ImageCacheResponseMessage>,
3726        image_cache: Arc<dyn ImageCache>,
3727        resource_threads: ResourceThreads,
3728        storage_threads: StorageThreads,
3729        #[cfg(feature = "bluetooth")] bluetooth_thread: GenericSender<BluetoothRequest>,
3730        mem_profiler_chan: MemProfilerChan,
3731        time_profiler_chan: TimeProfilerChan,
3732        devtools_chan: Option<GenericCallback<ScriptToDevtoolsControlMsg>>,
3733        constellation_chan: ScriptToConstellationChan,
3734        embedder_chan: ScriptToEmbedderChan,
3735        control_chan: GenericSender<ScriptThreadMessage>,
3736        pipeline_id: PipelineId,
3737        parent_info: Option<PipelineId>,
3738        viewport_details: ViewportDetails,
3739        origin: MutableOrigin,
3740        creation_url: ServoUrl,
3741        top_level_creation_url: ServoUrl,
3742        navigation_start: CrossProcessInstant,
3743        webgl_chan: Option<WebGLChan>,
3744        #[cfg(feature = "webxr")] webxr_registry: Option<webxr_api::Registry>,
3745        paint_api: CrossProcessPaintApi,
3746        unminify_js: bool,
3747        unminify_css: bool,
3748        local_script_source: Option<String>,
3749        user_scripts: Rc<Vec<UserScript>>,
3750        player_context: WindowGLContext,
3751        #[cfg(feature = "webgpu")] gpu_id_hub: Arc<IdentityHub>,
3752        inherited_secure_context: Option<bool>,
3753        theme: Theme,
3754        weak_script_thread: Weak<ScriptThread>,
3755    ) -> DomRoot<Self> {
3756        let error_reporter = CSSErrorReporter {
3757            pipelineid: pipeline_id,
3758            script_chan: control_chan,
3759        };
3760
3761        let win = Box::new(Self {
3762            webview_id,
3763            globalscope: GlobalScope::new_inherited(
3764                pipeline_id,
3765                devtools_chan,
3766                mem_profiler_chan,
3767                time_profiler_chan,
3768                constellation_chan,
3769                embedder_chan,
3770                resource_threads,
3771                storage_threads,
3772                origin,
3773                creation_url,
3774                Some(top_level_creation_url),
3775                #[cfg(feature = "webgpu")]
3776                gpu_id_hub,
3777                inherited_secure_context,
3778                unminify_js,
3779                Some(font_context),
3780            ),
3781            ongoing_navigation: Default::default(),
3782            script_chan,
3783            layout: RefCell::new(layout),
3784            image_cache_sender,
3785            image_cache,
3786            navigator: Default::default(),
3787            location: Default::default(),
3788            history: Default::default(),
3789            custom_element_registry: Default::default(),
3790            window_proxy: Default::default(),
3791            document: Default::default(),
3792            performance: Default::default(),
3793            navigation_start: Cell::new(navigation_start),
3794            screen: Default::default(),
3795            session_storage: Default::default(),
3796            local_storage: Default::default(),
3797            status: DomRefCell::new(DOMString::new()),
3798            parent_info,
3799            dom_static: GlobalStaticData::new(),
3800            js_runtime: DomRefCell::new(Some(runtime)),
3801            #[cfg(feature = "bluetooth")]
3802            bluetooth_thread,
3803            #[cfg(feature = "bluetooth")]
3804            bluetooth_extra_permission_data: BluetoothExtraPermissionData::new(),
3805            unhandled_resize_event: Default::default(),
3806            viewport_details: Cell::new(viewport_details),
3807            layout_blocker: Cell::new(LayoutBlocker::WaitingForParse),
3808            current_state: Cell::new(WindowState::Alive),
3809            devtools_marker_sender: Default::default(),
3810            devtools_markers: Default::default(),
3811            webdriver_script_chan: Default::default(),
3812            webdriver_load_status_sender: Default::default(),
3813            error_reporter,
3814            media_query_lists: DOMTracker::new(),
3815            #[cfg(feature = "bluetooth")]
3816            test_runner: Default::default(),
3817            webgl_chan,
3818            #[cfg(feature = "webxr")]
3819            webxr_registry,
3820            pending_image_callbacks: Default::default(),
3821            pending_layout_images: Default::default(),
3822            pending_images_for_rasterization: Default::default(),
3823            unminified_css_dir: Default::default(),
3824            local_script_source,
3825            test_worklet: Default::default(),
3826            paint_worklet: Default::default(),
3827            exists_mut_observer: Cell::new(false),
3828            paint_api,
3829            has_sent_idle_message: Cell::new(false),
3830            unminify_css,
3831            user_scripts,
3832            player_context,
3833            throttled: Cell::new(false),
3834            layout_marker: DomRefCell::new(Rc::new(Cell::new(true))),
3835            current_event: DomRefCell::new(None),
3836            theme: Cell::new(theme),
3837            trusted_types: Default::default(),
3838            reporting_observer_list: Default::default(),
3839            report_list: Default::default(),
3840            endpoints_list: Default::default(),
3841            script_window_proxies: ScriptThread::window_proxies(),
3842            has_pending_screenshot_readiness_request: Default::default(),
3843            visual_viewport: Default::default(),
3844            weak_script_thread,
3845            has_changed_visual_viewport_dimension: Default::default(),
3846            last_activation_timestamp: Cell::new(UserActivationTimestamp::PositiveInfinity),
3847        });
3848
3849        WindowBinding::Wrap::<crate::DomTypeHolder>(cx, win)
3850    }
3851
3852    pub(crate) fn pipeline_id(&self) -> PipelineId {
3853        self.as_global_scope().pipeline_id()
3854    }
3855
3856    /// Create a new cached instance of the given value.
3857    pub(crate) fn cache_layout_value<T>(&self, value: T) -> LayoutValue<T>
3858    where
3859        T: Copy + MallocSizeOf,
3860    {
3861        LayoutValue::new(self.layout_marker.borrow().clone(), value)
3862    }
3863}
3864
3865/// An instance of a value associated with a particular snapshot of layout. This stored
3866/// value can only be read as long as the associated layout marker that is considered
3867/// valid. It will automatically become unavailable when the next layout operation is
3868/// performed.
3869#[derive(MallocSizeOf)]
3870pub(crate) struct LayoutValue<T: MallocSizeOf> {
3871    #[conditional_malloc_size_of]
3872    is_valid: Rc<Cell<bool>>,
3873    value: T,
3874}
3875
3876#[expect(unsafe_code)]
3877unsafe impl<T: JSTraceable + MallocSizeOf> JSTraceable for LayoutValue<T> {
3878    unsafe fn trace(&self, trc: *mut js::jsapi::JSTracer) {
3879        unsafe { self.value.trace(trc) };
3880    }
3881}
3882
3883impl<T: Copy + MallocSizeOf> LayoutValue<T> {
3884    fn new(marker: Rc<Cell<bool>>, value: T) -> Self {
3885        LayoutValue {
3886            is_valid: marker,
3887            value,
3888        }
3889    }
3890
3891    /// Retrieve the stored value if it is still valid.
3892    pub(crate) fn get(&self) -> Result<T, ()> {
3893        if self.is_valid.get() {
3894            return Ok(self.value);
3895        }
3896        Err(())
3897    }
3898}
3899
3900fn should_move_clip_rect(clip_rect: UntypedRect<Au>, new_viewport: UntypedRect<f32>) -> bool {
3901    let clip_rect = UntypedRect::new(
3902        Point2D::new(
3903            clip_rect.origin.x.to_f32_px(),
3904            clip_rect.origin.y.to_f32_px(),
3905        ),
3906        Size2D::new(
3907            clip_rect.size.width.to_f32_px(),
3908            clip_rect.size.height.to_f32_px(),
3909        ),
3910    );
3911
3912    // We only need to move the clip rect if the viewport is getting near the edge of
3913    // our preexisting clip rect. We use half of the size of the viewport as a heuristic
3914    // for "close."
3915    static VIEWPORT_SCROLL_MARGIN_SIZE: f32 = 0.5;
3916    let viewport_scroll_margin = new_viewport.size * VIEWPORT_SCROLL_MARGIN_SIZE;
3917
3918    (clip_rect.origin.x - new_viewport.origin.x).abs() <= viewport_scroll_margin.width ||
3919        (clip_rect.max_x() - new_viewport.max_x()).abs() <= viewport_scroll_margin.width ||
3920        (clip_rect.origin.y - new_viewport.origin.y).abs() <= viewport_scroll_margin.height ||
3921        (clip_rect.max_y() - new_viewport.max_y()).abs() <= viewport_scroll_margin.height
3922}
3923
3924impl Window {
3925    // https://html.spec.whatwg.org/multipage/#dom-window-postmessage step 7.
3926    pub(crate) fn post_message(
3927        &self,
3928        target_origin: Option<ImmutableOrigin>,
3929        source_origin: ImmutableOrigin,
3930        source: &WindowProxy,
3931        data: StructuredSerializedData,
3932    ) {
3933        let this = Trusted::new(self);
3934        let source = Trusted::new(source);
3935        let task = task!(post_serialised_message: move || {
3936            let this = this.root();
3937            let source = source.root();
3938            let document = this.Document();
3939
3940            // Step 7.1.
3941            if let Some(ref target_origin) = target_origin {
3942                if !target_origin.same_origin(document.origin()) {
3943                    return;
3944                }
3945            }
3946
3947            // Steps 7.2.-7.5.
3948            let cx = this.get_cx();
3949            let obj = this.reflector().get_jsobject();
3950            let _ac = JSAutoRealm::new(*cx, obj.get());
3951            rooted!(in(*cx) let mut message_clone = UndefinedValue());
3952            if let Ok(ports) = structuredclone::read(this.upcast(), data, message_clone.handle_mut(), CanGc::note()) {
3953                // Step 7.6, 7.7
3954                MessageEvent::dispatch_jsval(
3955                    this.upcast(),
3956                    this.upcast(),
3957                    message_clone.handle(),
3958                    Some(&source_origin.ascii_serialization()),
3959                    Some(&*source),
3960                    ports,
3961                    CanGc::note()
3962                );
3963            } else {
3964                // Step 4, fire messageerror.
3965                MessageEvent::dispatch_error(
3966                    this.upcast(),
3967                    this.upcast(),
3968                    CanGc::note()
3969                );
3970            }
3971        });
3972        // TODO(#12718): Use the "posted message task source".
3973        self.as_global_scope()
3974            .task_manager()
3975            .dom_manipulation_task_source()
3976            .queue(task);
3977    }
3978}
3979
3980#[derive(Clone, MallocSizeOf)]
3981pub(crate) struct CSSErrorReporter {
3982    pub(crate) pipelineid: PipelineId,
3983    pub(crate) script_chan: GenericSender<ScriptThreadMessage>,
3984}
3985unsafe_no_jsmanaged_fields!(CSSErrorReporter);
3986
3987impl ParseErrorReporter for CSSErrorReporter {
3988    fn report_error(
3989        &self,
3990        url: &UrlExtraData,
3991        location: SourceLocation,
3992        error: ContextualParseError,
3993    ) {
3994        if log_enabled!(log::Level::Info) {
3995            info!(
3996                "Url:\t{}\n{}:{} {}",
3997                url.0.as_str(),
3998                location.line,
3999                location.column,
4000                error
4001            )
4002        }
4003
4004        // TODO: report a real filename
4005        let _ = self.script_chan.send(ScriptThreadMessage::ReportCSSError(
4006            self.pipelineid,
4007            url.0.to_string(),
4008            location.line,
4009            location.column,
4010            error.to_string(),
4011        ));
4012    }
4013}
4014
4015fn is_named_element_with_name_attribute(elem: &Element) -> bool {
4016    let type_ = match elem.upcast::<Node>().type_id() {
4017        NodeTypeId::Element(ElementTypeId::HTMLElement(type_)) => type_,
4018        _ => return false,
4019    };
4020    matches!(
4021        type_,
4022        HTMLElementTypeId::HTMLEmbedElement |
4023            HTMLElementTypeId::HTMLFormElement |
4024            HTMLElementTypeId::HTMLImageElement |
4025            HTMLElementTypeId::HTMLObjectElement
4026    )
4027}
4028
4029fn is_named_element_with_id_attribute(elem: &Element) -> bool {
4030    elem.is_html_element()
4031}
4032
4033#[expect(unsafe_code)]
4034#[unsafe(no_mangle)]
4035/// Helper for interactive debugging sessions in lldb/gdb.
4036unsafe extern "C" fn dump_js_stack(cx: *mut RawJSContext) {
4037    unsafe {
4038        DumpJSStack(cx, true, false, false);
4039    }
4040}
4041
4042impl WindowHelpers for Window {
4043    fn create_named_properties_object(
4044        cx: SafeJSContext,
4045        proto: HandleObject,
4046        object: MutableHandleObject,
4047    ) {
4048        Self::create_named_properties_object(cx, proto, object)
4049    }
4050}