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