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