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