1use std::borrow::ToOwned;
6use std::cell::{Cell, RefCell, RefMut};
7use std::cmp;
8use std::collections::hash_map::Entry;
9use std::collections::{HashMap, HashSet};
10use std::default::Default;
11use std::ffi::c_void;
12use std::io::{Write, stderr, stdout};
13use std::rc::{Rc, Weak};
14use std::sync::Arc;
15use std::time::{Duration, Instant};
16
17use app_units::Au;
18use base64::Engine;
19use content_security_policy::Violation;
20use content_security_policy::sandboxing_directive::SandboxingFlagSet;
21use crossbeam_channel::{Sender, unbounded};
22use cssparser::SourceLocation;
23use devtools_traits::{ScriptToDevtoolsControlMsg, TimelineMarker, TimelineMarkerType};
24use dom_struct::dom_struct;
25use embedder_traits::user_contents::UserScript;
26use embedder_traits::{
27 AlertResponse, ConfirmResponse, EmbedderMsg, JavaScriptEvaluationError, PromptResponse,
28 ScriptToEmbedderChan, SimpleDialogRequest, Theme, UntrustedNodeAddress, ViewportDetails,
29 WebDriverJSResult, WebDriverLoadStatus,
30};
31use euclid::default::Rect as UntypedRect;
32use euclid::{Point2D, Rect, Scale, Size2D, Vector2D};
33use fonts::{CspViolationHandler, FontContext, NetworkTimingHandler, WebFontDocumentContext};
34use js::context::JSContext;
35use js::glue::DumpJSStack;
36use js::jsapi::{
37 GCReason, Heap, JS_GC, JSAutoRealm, JSContext as RawJSContext, JSObject, JSPROP_ENUMERATE,
38};
39use js::jsval::{NullValue, UndefinedValue};
40use js::realm::CurrentRealm;
41use js::rust::wrappers::JS_DefineProperty;
42use js::rust::{
43 CustomAutoRooter, CustomAutoRooterGuard, HandleObject, HandleValue, MutableHandleObject,
44 MutableHandleValue,
45};
46use layout_api::{
47 AxesOverflow, BoxAreaType, CSSPixelRectIterator, ElementsFromPointFlags,
48 ElementsFromPointResult, FragmentType, Layout, LayoutImageDestination, PendingImage,
49 PendingImageState, PendingRasterizationImage, PhysicalSides, QueryMsg, ReflowGoal,
50 ReflowPhasesRun, ReflowRequest, ReflowRequestRestyle, ReflowStatistics, RestyleReason,
51 ScrollContainerQueryFlags, ScrollContainerResponse, TrustedNodeAddress,
52 combine_id_with_fragment_type,
53};
54use malloc_size_of::MallocSizeOf;
55use media::WindowGLContext;
56use net_traits::image_cache::{
57 ImageCache, ImageCacheResponseCallback, ImageCacheResponseMessage, ImageLoadListener,
58 ImageResponse, PendingImageId, PendingImageResponse, RasterizationCompleteResponse,
59};
60use net_traits::request::Referrer;
61use net_traits::{ResourceFetchTiming, ResourceThreads};
62use num_traits::ToPrimitive;
63use paint_api::{CrossProcessPaintApi, PinchZoomInfos};
64use profile_traits::generic_channel as ProfiledGenericChannel;
65use profile_traits::mem::ProfilerChan as MemProfilerChan;
66use profile_traits::time::ProfilerChan as TimeProfilerChan;
67use rustc_hash::{FxBuildHasher, FxHashMap};
68use script_bindings::codegen::GenericBindings::WindowBinding::ScrollToOptions;
69use script_bindings::conversions::SafeToJSValConvertible;
70use script_bindings::interfaces::WindowHelpers;
71use script_bindings::root::Root;
72use script_traits::{ConstellationInputEvent, ScriptThreadMessage};
73use selectors::attr::CaseSensitivity;
74use servo_arc::Arc as ServoArc;
75use servo_base::cross_process_instant::CrossProcessInstant;
76use servo_base::generic_channel::{self, GenericCallback, GenericSender};
77use servo_base::id::{BrowsingContextId, PipelineId, WebViewId};
78#[cfg(feature = "bluetooth")]
79use servo_bluetooth_traits::BluetoothRequest;
80use servo_canvas_traits::webgl::WebGLChan;
81use servo_config::pref;
82use servo_constellation_traits::{
83 LoadData, LoadOrigin, ScreenshotReadinessResponse, ScriptToConstellationChan,
84 ScriptToConstellationMessage, StructuredSerializedData, WindowSizeType,
85};
86use servo_geometry::DeviceIndependentIntRect;
87use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
88use storage_traits::StorageThreads;
89use storage_traits::webstorage_thread::WebStorageType;
90use style::error_reporting::{ContextualParseError, ParseErrorReporter};
91use style::properties::PropertyId;
92use style::properties::style_structs::Font;
93use style::selector_parser::PseudoElement;
94use style::str::HTML_SPACE_CHARACTERS;
95use style::stylesheets::UrlExtraData;
96use style_traits::CSSPixel;
97use stylo_atoms::Atom;
98use time::Duration as TimeDuration;
99use webrender_api::ExternalScrollId;
100use webrender_api::units::{DeviceIntSize, DevicePixel, LayoutPixel, LayoutPoint};
101
102use super::bindings::codegen::Bindings::MessagePortBinding::StructuredSerializeOptions;
103use super::bindings::trace::HashMapTracedValues;
104use super::performanceresourcetiming::InitiatorType;
105use super::types::SVGSVGElement;
106use crate::dom::bindings::cell::{DomRefCell, Ref};
107use crate::dom::bindings::codegen::Bindings::DocumentBinding::{
108 DocumentMethods, DocumentReadyState, NamedPropertyValue,
109};
110use crate::dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElementMethods;
111use crate::dom::bindings::codegen::Bindings::HistoryBinding::History_Binding::HistoryMethods;
112use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
113 ImageBitmapOptions, ImageBitmapSource,
114};
115use crate::dom::bindings::codegen::Bindings::MediaQueryListBinding::MediaQueryList_Binding::MediaQueryListMethods;
116use crate::dom::bindings::codegen::Bindings::ReportingObserverBinding::Report;
117use crate::dom::bindings::codegen::Bindings::RequestBinding::{RequestInfo, RequestInit};
118use crate::dom::bindings::codegen::Bindings::VoidFunctionBinding::VoidFunction;
119use crate::dom::bindings::codegen::Bindings::WindowBinding::{
120 self, DeferredRequestInit, FrameRequestCallback, ScrollBehavior, WindowMethods,
121 WindowPostMessageOptions,
122};
123use crate::dom::bindings::codegen::UnionTypes::{
124 RequestOrUSVString, TrustedScriptOrString, TrustedScriptOrStringOrFunction,
125};
126use crate::dom::bindings::error::{
127 Error, ErrorInfo, ErrorResult, Fallible, javascript_error_info_from_error_info,
128};
129use crate::dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementTypeId, NodeTypeId};
130use crate::dom::bindings::num::Finite;
131use crate::dom::bindings::refcounted::Trusted;
132use crate::dom::bindings::reflector::{DomGlobal, DomObject};
133use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
134use crate::dom::bindings::str::{DOMString, USVString};
135use crate::dom::bindings::structuredclone;
136use crate::dom::bindings::trace::{CustomTraceable, JSTraceable, RootedTraceableBox};
137use crate::dom::bindings::utils::GlobalStaticData;
138use crate::dom::bindings::weakref::DOMTracker;
139#[cfg(feature = "bluetooth")]
140use crate::dom::bluetooth::BluetoothExtraPermissionData;
141use crate::dom::cookiestore::CookieStore;
142use crate::dom::crypto::Crypto;
143use crate::dom::csp::GlobalCspReporting;
144use crate::dom::css::cssstyledeclaration::{
145 CSSModificationAccess, CSSStyleDeclaration, CSSStyleOwner,
146};
147use crate::dom::customelementregistry::CustomElementRegistry;
148use crate::dom::document::{
149 AnimationFrameCallback, Document, SameOriginDescendantNavigablesIterator,
150};
151use crate::dom::element::Element;
152use crate::dom::event::{Event, EventBubbles, EventCancelable};
153use crate::dom::eventtarget::EventTarget;
154use crate::dom::fetchlaterresult::FetchLaterResult;
155use crate::dom::globalscope::GlobalScope;
156use crate::dom::history::History;
157use crate::dom::html::htmlcollection::{CollectionFilter, HTMLCollection};
158use crate::dom::html::htmliframeelement::HTMLIFrameElement;
159use crate::dom::idbfactory::IDBFactory;
160use crate::dom::inputevent::HitTestResult;
161use crate::dom::location::Location;
162use crate::dom::medialist::MediaList;
163use crate::dom::mediaquerylist::{MediaQueryList, MediaQueryListMatchState};
164use crate::dom::mediaquerylistevent::MediaQueryListEvent;
165use crate::dom::messageevent::MessageEvent;
166use crate::dom::navigator::Navigator;
167use crate::dom::node::{Node, NodeDamage, NodeTraits, from_untrusted_node_address};
168use crate::dom::performance::performance::Performance;
169use crate::dom::promise::Promise;
170use crate::dom::reporting::reportingendpoint::{ReportingEndpoint, SendReportsToEndpoints};
171use crate::dom::reporting::reportingobserver::ReportingObserver;
172use crate::dom::screen::Screen;
173use crate::dom::scrolling_box::{ScrollingBox, ScrollingBoxSource};
174use crate::dom::selection::Selection;
175use crate::dom::shadowroot::ShadowRoot;
176use crate::dom::storage::Storage;
177#[cfg(feature = "bluetooth")]
178use crate::dom::testrunner::TestRunner;
179use crate::dom::trustedtypes::trustedtypepolicyfactory::TrustedTypePolicyFactory;
180use crate::dom::types::{ImageBitmap, MouseEvent, UIEvent};
181use crate::dom::useractivation::UserActivationTimestamp;
182use crate::dom::visualviewport::{VisualViewport, VisualViewportChanges};
183use crate::dom::webgl::webglrenderingcontext::WebGLCommandSender;
184#[cfg(feature = "webgpu")]
185use crate::dom::webgpu::identityhub::IdentityHub;
186use crate::dom::windowproxy::{WindowProxy, WindowProxyHandler};
187use crate::dom::worklet::Worklet;
188use crate::dom::workletglobalscope::WorkletGlobalScopeType;
189use crate::layout_image::fetch_image_for_layout;
190use crate::messaging::{MainThreadScriptMsg, ScriptEventLoopReceiver, ScriptEventLoopSender};
191use crate::microtask::{Microtask, UserMicrotask};
192use crate::network_listener::{ResourceTimingListener, submit_timing};
193use crate::realms::enter_realm;
194use crate::script_runtime::{CanGc, JSContext as SafeJSContext, Runtime};
195use crate::script_thread::ScriptThread;
196use crate::script_window_proxies::ScriptWindowProxies;
197use crate::task_source::SendableTaskSource;
198use crate::timers::{IsInterval, TimerCallback};
199use crate::unminify::unminified_path;
200use crate::webdriver_handlers::{find_node_by_unique_id_in_document, jsval_to_webdriver};
201use crate::{fetch, window_named_properties};
202
203#[derive(MallocSizeOf)]
208pub struct PendingImageCallback(
209 #[ignore_malloc_size_of = "dyn Fn is currently impossible to measure"]
210 #[expect(clippy::type_complexity)]
211 Box<dyn Fn(PendingImageResponse, &mut js::context::JSContext) + 'static>,
212);
213
214#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf, PartialEq)]
216enum WindowState {
217 Alive,
218 Zombie, }
220
221const INITIAL_REFLOW_DELAY: Duration = Duration::from_millis(200);
224
225#[derive(Clone, Copy, MallocSizeOf)]
236enum LayoutBlocker {
237 WaitingForParse,
239 Parsing(Instant),
241 FiredLoadEventOrParsingTimerExpired,
245}
246
247impl LayoutBlocker {
248 fn layout_blocked(&self) -> bool {
249 !matches!(self, Self::FiredLoadEventOrParsingTimerExpired)
250 }
251}
252
253#[derive(Clone, Copy, Debug, Default, JSTraceable, MallocSizeOf, PartialEq)]
256pub(crate) struct OngoingNavigation(u32);
257
258type PendingImageRasterizationKey = (PendingImageId, DeviceIntSize);
259
260#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
264#[derive(JSTraceable, MallocSizeOf)]
265struct PendingLayoutImageAncillaryData {
266 node: Dom<Node>,
267 #[no_trace]
268 destination: LayoutImageDestination,
269}
270
271#[dom_struct]
272pub(crate) struct Window {
273 globalscope: GlobalScope,
274
275 #[ignore_malloc_size_of = "Weak does not need to be accounted"]
279 #[no_trace]
280 weak_script_thread: Weak<ScriptThread>,
281
282 #[no_trace]
286 webview_id: WebViewId,
287 script_chan: Sender<MainThreadScriptMsg>,
288 #[no_trace]
289 #[ignore_malloc_size_of = "TODO: Add MallocSizeOf support to layout"]
290 layout: RefCell<Box<dyn Layout>>,
291 navigator: MutNullableDom<Navigator>,
292 #[ignore_malloc_size_of = "ImageCache"]
293 #[no_trace]
294 image_cache: Arc<dyn ImageCache>,
295 #[no_trace]
296 image_cache_sender: Sender<ImageCacheResponseMessage>,
297 window_proxy: MutNullableDom<WindowProxy>,
298 document: MutNullableDom<Document>,
299 location: MutNullableDom<Location>,
300 history: MutNullableDom<History>,
301 custom_element_registry: MutNullableDom<CustomElementRegistry>,
302 performance: MutNullableDom<Performance>,
303 #[no_trace]
304 navigation_start: Cell<CrossProcessInstant>,
305 screen: MutNullableDom<Screen>,
306 session_storage: MutNullableDom<Storage>,
307 local_storage: MutNullableDom<Storage>,
308 status: DomRefCell<DOMString>,
309 trusted_types: MutNullableDom<TrustedTypePolicyFactory>,
310
311 ongoing_navigation: Cell<OngoingNavigation>,
314
315 #[no_trace]
318 devtools_markers: DomRefCell<HashSet<TimelineMarkerType>>,
319 #[no_trace]
320 devtools_marker_sender: DomRefCell<Option<GenericSender<Option<TimelineMarker>>>>,
321
322 #[no_trace]
324 unhandled_resize_event: DomRefCell<Option<(ViewportDetails, WindowSizeType)>>,
325
326 #[no_trace]
328 theme: Cell<Theme>,
329
330 #[no_trace]
332 parent_info: Option<PipelineId>,
333
334 dom_static: GlobalStaticData,
336
337 #[conditional_malloc_size_of]
339 js_runtime: DomRefCell<Option<Rc<Runtime>>>,
340
341 #[no_trace]
343 viewport_details: Cell<ViewportDetails>,
344
345 #[no_trace]
347 #[cfg(feature = "bluetooth")]
348 bluetooth_thread: GenericSender<BluetoothRequest>,
349
350 #[cfg(feature = "bluetooth")]
351 bluetooth_extra_permission_data: BluetoothExtraPermissionData,
352
353 #[no_trace]
357 layout_blocker: Cell<LayoutBlocker>,
358
359 #[no_trace]
361 webdriver_script_chan: DomRefCell<Option<GenericSender<WebDriverJSResult>>>,
362
363 #[no_trace]
365 webdriver_load_status_sender: RefCell<Option<GenericSender<WebDriverLoadStatus>>>,
366
367 current_state: Cell<WindowState>,
369
370 error_reporter: CSSErrorReporter,
371
372 media_query_lists: DOMTracker<MediaQueryList>,
374
375 #[cfg(feature = "bluetooth")]
376 test_runner: MutNullableDom<TestRunner>,
377
378 #[no_trace]
380 webgl_chan: Option<WebGLChan>,
381
382 #[ignore_malloc_size_of = "defined in webxr"]
383 #[no_trace]
384 #[cfg(feature = "webxr")]
385 webxr_registry: Option<webxr_api::Registry>,
386
387 #[no_trace]
391 pending_image_callbacks: DomRefCell<FxHashMap<PendingImageId, Vec<PendingImageCallback>>>,
392
393 pending_layout_images: DomRefCell<
398 HashMapTracedValues<PendingImageId, Vec<PendingLayoutImageAncillaryData>, FxBuildHasher>,
399 >,
400
401 pending_images_for_rasterization: DomRefCell<
405 HashMapTracedValues<PendingImageRasterizationKey, Vec<Dom<Node>>, FxBuildHasher>,
406 >,
407
408 unminified_css_dir: DomRefCell<Option<String>>,
411
412 local_script_source: Option<String>,
414
415 test_worklet: MutNullableDom<Worklet>,
417 paint_worklet: MutNullableDom<Worklet>,
419
420 exists_mut_observer: Cell<bool>,
422
423 #[no_trace]
425 paint_api: CrossProcessPaintApi,
426
427 has_sent_idle_message: Cell<bool>,
430
431 #[no_trace]
434 #[conditional_malloc_size_of]
435 user_scripts: Rc<Vec<UserScript>>,
436
437 #[ignore_malloc_size_of = "defined in script_thread"]
439 #[no_trace]
440 player_context: WindowGLContext,
441
442 throttled: Cell<bool>,
443
444 #[conditional_malloc_size_of]
448 layout_marker: DomRefCell<Rc<Cell<bool>>>,
449
450 current_event: DomRefCell<Option<Dom<Event>>>,
452
453 reporting_observer_list: DomRefCell<Vec<DomRoot<ReportingObserver>>>,
455
456 report_list: DomRefCell<Vec<Report>>,
458
459 #[no_trace]
461 endpoints_list: DomRefCell<Vec<ReportingEndpoint>>,
462
463 #[conditional_malloc_size_of]
465 script_window_proxies: Rc<ScriptWindowProxies>,
466
467 has_pending_screenshot_readiness_request: Cell<bool>,
469
470 visual_viewport: MutNullableDom<VisualViewport>,
473
474 has_changed_visual_viewport_dimension: Cell<bool>,
476
477 #[no_trace]
479 last_activation_timestamp: Cell<UserActivationTimestamp>,
480}
481
482impl Window {
483 pub(crate) fn script_thread(&self) -> Rc<ScriptThread> {
484 Weak::upgrade(&self.weak_script_thread)
485 .expect("Weak reference should always be upgradable when a ScriptThread is running")
486 }
487
488 pub(crate) fn webview_id(&self) -> WebViewId {
489 self.webview_id
490 }
491
492 pub(crate) fn as_global_scope(&self) -> &GlobalScope {
493 self.upcast::<GlobalScope>()
494 }
495
496 pub(crate) fn layout(&self) -> Ref<'_, Box<dyn Layout>> {
497 self.layout.borrow()
498 }
499
500 pub(crate) fn layout_mut(&self) -> RefMut<'_, Box<dyn Layout>> {
501 self.layout.borrow_mut()
502 }
503
504 pub(crate) fn get_exists_mut_observer(&self) -> bool {
505 self.exists_mut_observer.get()
506 }
507
508 pub(crate) fn set_exists_mut_observer(&self) {
509 self.exists_mut_observer.set(true);
510 }
511
512 #[expect(unsafe_code)]
513 pub(crate) fn clear_js_runtime_for_script_deallocation(&self) {
514 self.as_global_scope()
515 .remove_web_messaging_and_dedicated_workers_infra();
516 unsafe {
517 *self.js_runtime.borrow_for_script_deallocation() = None;
518 self.window_proxy.set(None);
519 self.current_state.set(WindowState::Zombie);
520 self.as_global_scope()
521 .task_manager()
522 .cancel_all_tasks_and_ignore_future_tasks();
523 }
524 }
525
526 pub(crate) fn discard_browsing_context(&self) {
529 let proxy = match self.window_proxy.get() {
530 Some(proxy) => proxy,
531 None => panic!("Discarding a BC from a window that has none"),
532 };
533 proxy.discard_browsing_context();
534 self.as_global_scope()
538 .task_manager()
539 .cancel_all_tasks_and_ignore_future_tasks();
540 }
541
542 pub(crate) fn time_profiler_chan(&self) -> &TimeProfilerChan {
544 self.globalscope.time_profiler_chan()
545 }
546
547 pub(crate) fn origin(&self) -> &MutableOrigin {
548 self.globalscope.origin()
549 }
550
551 #[expect(unsafe_code)]
552 pub(crate) fn get_cx(&self) -> SafeJSContext {
553 unsafe { SafeJSContext::from_ptr(js::rust::Runtime::get().unwrap().as_ptr()) }
554 }
555
556 pub(crate) fn get_js_runtime(&self) -> Ref<'_, Option<Rc<Runtime>>> {
557 self.js_runtime.borrow()
558 }
559
560 pub(crate) fn main_thread_script_chan(&self) -> &Sender<MainThreadScriptMsg> {
561 &self.script_chan
562 }
563
564 pub(crate) fn parent_info(&self) -> Option<PipelineId> {
565 self.parent_info
566 }
567
568 pub(crate) fn new_script_pair(&self) -> (ScriptEventLoopSender, ScriptEventLoopReceiver) {
569 let (sender, receiver) = unbounded();
570 (
571 ScriptEventLoopSender::MainThread(sender),
572 ScriptEventLoopReceiver::MainThread(receiver),
573 )
574 }
575
576 pub(crate) fn event_loop_sender(&self) -> ScriptEventLoopSender {
577 ScriptEventLoopSender::MainThread(self.script_chan.clone())
578 }
579
580 pub(crate) fn image_cache(&self) -> Arc<dyn ImageCache> {
581 self.image_cache.clone()
582 }
583
584 pub(crate) fn window_proxy(&self) -> DomRoot<WindowProxy> {
586 self.window_proxy.get().unwrap()
587 }
588
589 pub(crate) fn append_reporting_observer(&self, reporting_observer: DomRoot<ReportingObserver>) {
590 self.reporting_observer_list
591 .borrow_mut()
592 .push(reporting_observer);
593 }
594
595 pub(crate) fn remove_reporting_observer(&self, reporting_observer: &ReportingObserver) {
596 let index = {
597 let list = self.reporting_observer_list.borrow();
598 list.iter()
599 .position(|observer| &**observer == reporting_observer)
600 };
601
602 if let Some(index) = index {
603 self.reporting_observer_list.borrow_mut().remove(index);
604 }
605 }
606
607 pub(crate) fn registered_reporting_observers(&self) -> Vec<DomRoot<ReportingObserver>> {
608 self.reporting_observer_list.borrow().clone()
609 }
610
611 pub(crate) fn append_report(&self, report: Report) {
612 self.report_list.borrow_mut().push(report);
613 let trusted_window = Trusted::new(self);
614 self.upcast::<GlobalScope>()
615 .task_manager()
616 .dom_manipulation_task_source()
617 .queue(task!(send_to_reporting_endpoints: move || {
618 let window = trusted_window.root();
619 let reports = std::mem::take(&mut *window.report_list.borrow_mut());
620 window.upcast::<GlobalScope>().send_reports_to_endpoints(
621 reports,
622 window.endpoints_list.borrow().clone(),
623 );
624 }));
625 }
626
627 pub(crate) fn buffered_reports(&self) -> Vec<Report> {
628 self.report_list.borrow().clone()
629 }
630
631 pub(crate) fn set_endpoints_list(&self, endpoints: Vec<ReportingEndpoint>) {
632 *self.endpoints_list.borrow_mut() = endpoints;
633 }
634
635 pub(crate) fn undiscarded_window_proxy(&self) -> Option<DomRoot<WindowProxy>> {
638 self.window_proxy.get().and_then(|window_proxy| {
639 if window_proxy.is_browsing_context_discarded() {
640 None
641 } else {
642 Some(window_proxy)
643 }
644 })
645 }
646
647 pub(crate) fn top_level_document_if_local(&self) -> Option<DomRoot<Document>> {
652 if self.is_top_level() {
653 return Some(self.Document());
654 }
655
656 let window_proxy = self.undiscarded_window_proxy()?;
657 self.script_window_proxies
658 .find_window_proxy(window_proxy.webview_id().into())?
659 .document()
660 }
661
662 #[cfg(feature = "bluetooth")]
663 pub(crate) fn bluetooth_thread(&self) -> GenericSender<BluetoothRequest> {
664 self.bluetooth_thread.clone()
665 }
666
667 #[cfg(feature = "bluetooth")]
668 pub(crate) fn bluetooth_extra_permission_data(&self) -> &BluetoothExtraPermissionData {
669 &self.bluetooth_extra_permission_data
670 }
671
672 pub(crate) fn css_error_reporter(&self) -> &CSSErrorReporter {
673 &self.error_reporter
674 }
675
676 pub(crate) fn webgl_chan(&self) -> Option<WebGLCommandSender> {
677 self.webgl_chan
678 .as_ref()
679 .map(|chan| WebGLCommandSender::new(chan.clone()))
680 }
681
682 #[cfg(feature = "webxr")]
683 pub(crate) fn webxr_registry(&self) -> Option<webxr_api::Registry> {
684 self.webxr_registry.clone()
685 }
686
687 fn new_paint_worklet(&self, can_gc: CanGc) -> DomRoot<Worklet> {
688 debug!("Creating new paint worklet.");
689 Worklet::new(self, WorkletGlobalScopeType::Paint, can_gc)
690 }
691
692 pub(crate) fn register_image_cache_listener(
693 &self,
694 id: PendingImageId,
695 callback: impl Fn(PendingImageResponse, &mut js::context::JSContext) + 'static,
696 ) -> ImageCacheResponseCallback {
697 self.pending_image_callbacks
698 .borrow_mut()
699 .entry(id)
700 .or_default()
701 .push(PendingImageCallback(Box::new(callback)));
702
703 let image_cache_sender = self.image_cache_sender.clone();
704 Box::new(move |message| {
705 let _ = image_cache_sender.send(message);
706 })
707 }
708
709 fn pending_layout_image_notification(&self, response: PendingImageResponse) {
710 let mut images = self.pending_layout_images.borrow_mut();
711 let nodes = images.entry(response.id);
712 let nodes = match nodes {
713 Entry::Occupied(nodes) => nodes,
714 Entry::Vacant(_) => return,
715 };
716 if matches!(
717 response.response,
718 ImageResponse::Loaded(_, _) | ImageResponse::FailedToLoadOrDecode
719 ) {
720 for ancillary_data in nodes.get() {
721 match ancillary_data.destination {
722 LayoutImageDestination::BoxTreeConstruction => {
723 ancillary_data.node.dirty(NodeDamage::Other);
724 },
725 LayoutImageDestination::DisplayListBuilding => {
726 self.layout().set_needs_new_display_list();
727 },
728 }
729 }
730 }
731
732 match response.response {
733 ImageResponse::MetadataLoaded(_) => {},
734 ImageResponse::Loaded(_, _) | ImageResponse::FailedToLoadOrDecode => {
735 nodes.remove();
736 },
737 }
738 }
739
740 pub(crate) fn handle_image_rasterization_complete_notification(
741 &self,
742 response: RasterizationCompleteResponse,
743 ) {
744 let mut images = self.pending_images_for_rasterization.borrow_mut();
745 let nodes = images.entry((response.image_id, response.requested_size));
746 let nodes = match nodes {
747 Entry::Occupied(nodes) => nodes,
748 Entry::Vacant(_) => return,
749 };
750 for node in nodes.get() {
751 node.dirty(NodeDamage::Other);
752 }
753 nodes.remove();
754 }
755
756 pub(crate) fn pending_image_notification(
757 &self,
758 response: PendingImageResponse,
759 cx: &mut js::context::JSContext,
760 ) {
761 let mut images = std::mem::take(&mut *self.pending_image_callbacks.borrow_mut());
766 let Entry::Occupied(callbacks) = images.entry(response.id) else {
767 let _ = std::mem::replace(&mut *self.pending_image_callbacks.borrow_mut(), images);
768 return;
769 };
770
771 for callback in callbacks.get() {
772 callback.0(response.clone(), cx);
773 }
774
775 match response.response {
776 ImageResponse::MetadataLoaded(_) => {},
777 ImageResponse::Loaded(_, _) | ImageResponse::FailedToLoadOrDecode => {
778 callbacks.remove();
779 },
780 }
781
782 let _ = std::mem::replace(&mut *self.pending_image_callbacks.borrow_mut(), images);
783 }
784
785 pub(crate) fn paint_api(&self) -> &CrossProcessPaintApi {
786 &self.paint_api
787 }
788
789 pub(crate) fn userscripts(&self) -> &[UserScript] {
790 &self.user_scripts
791 }
792
793 pub(crate) fn get_player_context(&self) -> WindowGLContext {
794 self.player_context.clone()
795 }
796
797 pub(crate) fn dispatch_event_with_target_override(&self, event: &Event, can_gc: CanGc) {
799 event.dispatch(self.upcast(), true, can_gc);
800 }
801
802 pub(crate) fn font_context(&self) -> &Arc<FontContext> {
803 self.as_global_scope()
804 .font_context()
805 .expect("A `Window` should always have a `FontContext`")
806 }
807
808 pub(crate) fn ongoing_navigation(&self) -> OngoingNavigation {
809 self.ongoing_navigation.get()
810 }
811
812 pub(crate) fn set_ongoing_navigation(&self) -> OngoingNavigation {
814 let new_value = self.ongoing_navigation.get().0.wrapping_add(1);
818
819 self.ongoing_navigation.set(OngoingNavigation(new_value));
826
827 OngoingNavigation(new_value)
829 }
830
831 fn stop_loading(&self, cx: &mut js::context::JSContext) {
833 let doc = self.Document();
835
836 self.set_ongoing_navigation();
846
847 doc.abort(cx);
849 }
850
851 fn destroy_top_level_traversable(&self, cx: &mut js::context::JSContext) {
853 let document = self.Document();
859 document.destroy_document_and_its_descendants(cx);
861 self.send_to_constellation(ScriptToConstellationMessage::DiscardTopLevelBrowsingContext);
863 }
864
865 fn definitely_close(&self, cx: &mut js::context::JSContext) {
867 let document = self.Document();
868 if !document.check_if_unloading_is_cancelled(false, CanGc::from_cx(cx)) {
873 return;
874 }
875 document.unload(false, CanGc::from_cx(cx));
879 self.destroy_top_level_traversable(cx);
881 }
882
883 fn cannot_show_simple_dialogs(&self) -> bool {
885 if self
888 .Document()
889 .has_active_sandboxing_flag(SandboxingFlagSet::SANDBOXED_MODALS_FLAG)
890 {
891 return true;
892 }
893
894 false
913 }
914
915 pub(crate) fn perform_a_microtask_checkpoint(&self, cx: &mut js::context::JSContext) {
916 self.script_thread().perform_a_microtask_checkpoint(cx);
917 }
918
919 pub(crate) fn web_font_context(&self) -> WebFontDocumentContext {
920 let global = self.as_global_scope();
921 WebFontDocumentContext {
922 policy_container: global.policy_container(),
923 request_client: global.request_client(),
924 document_url: global.api_base_url(),
925 has_trustworthy_ancestor_origin: global.has_trustworthy_ancestor_origin(),
926 insecure_requests_policy: global.insecure_requests_policy(),
927 csp_handler: Box::new(FontCspHandler {
928 global: Trusted::new(global),
929 task_source: global
930 .task_manager()
931 .dom_manipulation_task_source()
932 .to_sendable(),
933 }),
934 network_timing_handler: Box::new(FontNetworkTimingHandler {
935 global: Trusted::new(global),
936 task_source: global
937 .task_manager()
938 .dom_manipulation_task_source()
939 .to_sendable(),
940 }),
941 }
942 }
943
944 #[expect(unsafe_code)]
945 pub(crate) fn gc(&self) {
946 unsafe {
947 JS_GC(*self.get_cx(), GCReason::API);
948 }
949 }
950}
951
952#[derive(Debug)]
953struct FontCspHandler {
954 global: Trusted<GlobalScope>,
955 task_source: SendableTaskSource,
956}
957
958impl CspViolationHandler for FontCspHandler {
959 fn process_violations(&self, violations: Vec<Violation>) {
960 let global = self.global.clone();
961 self.task_source.queue(task!(csp_violation: move || {
962 global.root().report_csp_violations(violations, None, None);
963 }));
964 }
965
966 fn clone(&self) -> Box<dyn CspViolationHandler> {
967 Box::new(Self {
968 global: self.global.clone(),
969 task_source: self.task_source.clone(),
970 })
971 }
972}
973
974#[derive(Debug)]
975struct FontNetworkTimingHandler {
976 global: Trusted<GlobalScope>,
977 task_source: SendableTaskSource,
978}
979
980impl NetworkTimingHandler for FontNetworkTimingHandler {
981 fn submit_timing(&self, url: ServoUrl, response: ResourceFetchTiming) {
982 let global = self.global.clone();
983 self.task_source.queue(task!(network_timing: move |cx| {
984 submit_timing(
985 cx,
986 &FontFetchListener {
987 url,
988 global
989 },
990 &Ok(()),
991 &response,
992 );
993 }));
994 }
995
996 fn clone(&self) -> Box<dyn NetworkTimingHandler> {
997 Box::new(Self {
998 global: self.global.clone(),
999 task_source: self.task_source.clone(),
1000 })
1001 }
1002}
1003
1004#[derive(Debug)]
1005struct FontFetchListener {
1006 global: Trusted<GlobalScope>,
1007 url: ServoUrl,
1008}
1009
1010impl ResourceTimingListener for FontFetchListener {
1011 fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
1012 (InitiatorType::Css, self.url.clone())
1013 }
1014
1015 fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
1016 self.global.root()
1017 }
1018}
1019
1020pub(crate) fn base64_btoa(input: DOMString) -> Fallible<DOMString> {
1022 if input.str().chars().any(|c: char| c > '\u{FF}') {
1026 Err(Error::InvalidCharacter(None))
1027 } else {
1028 let octets = input
1033 .str()
1034 .chars()
1035 .map(|c: char| c as u8)
1036 .collect::<Vec<u8>>();
1037
1038 let config =
1041 base64::engine::general_purpose::GeneralPurposeConfig::new().with_encode_padding(true);
1042 let engine = base64::engine::GeneralPurpose::new(&base64::alphabet::STANDARD, config);
1043 Ok(DOMString::from(engine.encode(octets)))
1044 }
1045}
1046
1047pub(crate) fn base64_atob(input: DOMString) -> Fallible<DOMString> {
1049 fn is_html_space(c: char) -> bool {
1051 HTML_SPACE_CHARACTERS.contains(&c)
1052 }
1053 let without_spaces = input
1054 .str()
1055 .chars()
1056 .filter(|&c| !is_html_space(c))
1057 .collect::<String>();
1058 let mut input = &*without_spaces;
1059
1060 if input.len() % 4 == 0 {
1064 if input.ends_with("==") {
1065 input = &input[..input.len() - 2]
1066 } else if input.ends_with('=') {
1067 input = &input[..input.len() - 1]
1068 }
1069 }
1070
1071 if input.len() % 4 == 1 {
1074 return Err(Error::InvalidCharacter(None));
1075 }
1076
1077 if input
1085 .chars()
1086 .any(|c| c != '+' && c != '/' && !c.is_alphanumeric())
1087 {
1088 return Err(Error::InvalidCharacter(None));
1089 }
1090
1091 let config = base64::engine::general_purpose::GeneralPurposeConfig::new()
1092 .with_decode_padding_mode(base64::engine::DecodePaddingMode::RequireNone)
1093 .with_decode_allow_trailing_bits(true);
1094 let engine = base64::engine::GeneralPurpose::new(&base64::alphabet::STANDARD, config);
1095
1096 let data = engine
1097 .decode(input)
1098 .map_err(|_| Error::InvalidCharacter(None))?;
1099 Ok(data.iter().map(|&b| b as char).collect::<String>().into())
1100}
1101
1102impl WindowMethods<crate::DomTypeHolder> for Window {
1103 fn Alert_(&self) {
1105 self.Alert(DOMString::new());
1108 }
1109
1110 fn Alert(&self, mut message: DOMString) {
1112 if self.cannot_show_simple_dialogs() {
1114 return;
1115 }
1116
1117 message.normalize_newlines();
1121
1122 {
1133 let stderr = stderr();
1137 let mut stderr = stderr.lock();
1138 let stdout = stdout();
1139 let mut stdout = stdout.lock();
1140 writeln!(&mut stdout, "\nALERT: {message}").unwrap();
1141 stdout.flush().unwrap();
1142 stderr.flush().unwrap();
1143 }
1144
1145 let (sender, receiver) =
1146 ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
1147 let dialog = SimpleDialogRequest::Alert {
1148 id: self.Document().embedder_controls().next_control_id(),
1149 message: message.to_string(),
1150 response_sender: sender,
1151 };
1152 self.send_to_embedder(EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog));
1153 receiver.recv().unwrap_or_else(|_| {
1154 debug!("Alert dialog was cancelled or failed to show.");
1156 AlertResponse::Ok
1157 });
1158
1159 }
1162
1163 fn Confirm(&self, mut message: DOMString) -> bool {
1165 if self.cannot_show_simple_dialogs() {
1167 return false;
1168 }
1169
1170 message.normalize_newlines();
1172
1173 let (sender, receiver) =
1179 ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
1180 let dialog = SimpleDialogRequest::Confirm {
1181 id: self.Document().embedder_controls().next_control_id(),
1182 message: message.to_string(),
1183 response_sender: sender,
1184 };
1185 self.send_to_embedder(EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog));
1186
1187 match receiver.recv() {
1203 Ok(ConfirmResponse::Ok) => true,
1204 Ok(ConfirmResponse::Cancel) => false,
1205 Err(_) => {
1206 warn!("Confirm dialog was cancelled or failed to show.");
1207 false
1208 },
1209 }
1210 }
1211
1212 fn Prompt(&self, mut message: DOMString, default: DOMString) -> Option<DOMString> {
1214 if self.cannot_show_simple_dialogs() {
1216 return None;
1217 }
1218
1219 message.normalize_newlines();
1221
1222 let (sender, receiver) =
1230 ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
1231 let dialog = SimpleDialogRequest::Prompt {
1232 id: self.Document().embedder_controls().next_control_id(),
1233 message: message.to_string(),
1234 default: default.to_string(),
1235 response_sender: sender,
1236 };
1237 self.send_to_embedder(EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog));
1238
1239 match receiver.recv() {
1258 Ok(PromptResponse::Ok(input)) => Some(input.into()),
1259 Ok(PromptResponse::Cancel) => None,
1260 Err(_) => {
1261 warn!("Prompt dialog was cancelled or failed to show.");
1262 None
1263 },
1264 }
1265 }
1266
1267 fn Stop(&self, cx: &mut js::context::JSContext) {
1269 self.stop_loading(cx);
1274 }
1275
1276 fn Focus(&self) {
1278 let current = match self.undiscarded_window_proxy() {
1282 Some(proxy) => proxy,
1283 None => return,
1284 };
1285
1286 current.focus();
1288
1289 }
1295
1296 fn Blur(&self) {
1298 }
1301
1302 fn Open(
1304 &self,
1305 cx: &mut JSContext,
1306 url: USVString,
1307 target: DOMString,
1308 features: DOMString,
1309 ) -> Fallible<Option<DomRoot<WindowProxy>>> {
1310 self.window_proxy().open(cx, url, target, features)
1311 }
1312
1313 fn GetOpener(&self, cx: &mut CurrentRealm, mut retval: MutableHandleValue) -> Fallible<()> {
1315 let current = match self.window_proxy.get() {
1317 Some(proxy) => proxy,
1318 None => {
1320 retval.set(NullValue());
1321 return Ok(());
1322 },
1323 };
1324 if current.is_browsing_context_discarded() {
1329 retval.set(NullValue());
1330 return Ok(());
1331 }
1332 current.opener(cx, retval);
1334 Ok(())
1335 }
1336
1337 #[expect(unsafe_code)]
1338 fn SetOpener(&self, cx: SafeJSContext, value: HandleValue) -> ErrorResult {
1340 if value.is_null() {
1342 if let Some(proxy) = self.window_proxy.get() {
1343 proxy.disown();
1344 }
1345 return Ok(());
1346 }
1347 let obj = self.reflector().get_jsobject();
1349 unsafe {
1350 let result =
1351 JS_DefineProperty(*cx, obj, c"opener".as_ptr(), value, JSPROP_ENUMERATE as u32);
1352
1353 if result { Ok(()) } else { Err(Error::JSFailed) }
1354 }
1355 }
1356
1357 fn Closed(&self) -> bool {
1359 self.window_proxy
1360 .get()
1361 .map(|ref proxy| proxy.is_browsing_context_discarded() || proxy.is_closing())
1362 .unwrap_or(true)
1363 }
1364
1365 fn Close(&self) {
1367 let window_proxy = match self.window_proxy.get() {
1369 Some(proxy) => proxy,
1370 None => return,
1372 };
1373 if window_proxy.is_closing() {
1375 return;
1376 }
1377 if let Ok(history_length) = self.History().GetLength() {
1380 let is_auxiliary = window_proxy.is_auxiliary();
1381
1382 let is_script_closable = (self.is_top_level() && history_length == 1) ||
1384 is_auxiliary ||
1385 pref!(dom_allow_scripts_to_close_windows);
1386
1387 if is_script_closable {
1391 window_proxy.close();
1393
1394 let this = Trusted::new(self);
1396 let task = task!(window_close_browsing_context: move |cx| {
1397 let window = this.root();
1398 window.definitely_close(cx);
1399 });
1400 self.as_global_scope()
1401 .task_manager()
1402 .dom_manipulation_task_source()
1403 .queue(task);
1404 }
1405 }
1406 }
1407
1408 fn Document(&self) -> DomRoot<Document> {
1410 self.document
1411 .get()
1412 .expect("Document accessed before initialization.")
1413 }
1414
1415 fn History(&self) -> DomRoot<History> {
1417 self.history.or_init(|| History::new(self, CanGc::note()))
1418 }
1419
1420 fn IndexedDB(&self) -> DomRoot<IDBFactory> {
1422 self.upcast::<GlobalScope>().get_indexeddb()
1423 }
1424
1425 fn CustomElements(&self) -> DomRoot<CustomElementRegistry> {
1427 self.custom_element_registry
1428 .or_init(|| CustomElementRegistry::new(self, CanGc::note()))
1429 }
1430
1431 fn Location(&self, cx: &mut js::context::JSContext) -> DomRoot<Location> {
1433 self.location.or_init(|| Location::new(cx, self))
1434 }
1435
1436 fn GetSessionStorage(&self, cx: &mut js::context::JSContext) -> Fallible<DomRoot<Storage>> {
1438 if let Some(storage) = self.session_storage.get() {
1441 return Ok(storage);
1442 }
1443
1444 if !self.origin().is_tuple() {
1448 return Err(Error::Security(Some(
1449 "Cannot access sessionStorage from opaque origin.".to_string(),
1450 )));
1451 }
1452
1453 let storage = Storage::new(self, WebStorageType::Session, CanGc::from_cx(cx));
1455
1456 self.session_storage.set(Some(&storage));
1458
1459 Ok(storage)
1461 }
1462
1463 fn GetLocalStorage(&self, cx: &mut js::context::JSContext) -> Fallible<DomRoot<Storage>> {
1465 if let Some(storage) = self.local_storage.get() {
1468 return Ok(storage);
1469 }
1470
1471 if !self.origin().is_tuple() {
1475 return Err(Error::Security(Some(
1476 "Cannot access localStorage from opaque origin.".to_string(),
1477 )));
1478 }
1479
1480 let storage = Storage::new(self, WebStorageType::Local, CanGc::from_cx(cx));
1482
1483 self.local_storage.set(Some(&storage));
1485
1486 Ok(storage)
1488 }
1489
1490 fn CookieStore(&self, can_gc: CanGc) -> DomRoot<CookieStore> {
1492 self.global().cookie_store(can_gc)
1493 }
1494
1495 fn Crypto(&self) -> DomRoot<Crypto> {
1497 self.as_global_scope().crypto(CanGc::note())
1498 }
1499
1500 fn GetFrameElement(&self) -> Option<DomRoot<Element>> {
1502 let window_proxy = self.window_proxy.get()?;
1504
1505 let container = window_proxy.frame_element()?;
1507
1508 let container_doc = container.owner_document();
1510 let current_doc = GlobalScope::current()
1511 .expect("No current global object")
1512 .as_window()
1513 .Document();
1514 if !current_doc
1515 .origin()
1516 .same_origin_domain(&container_doc.origin())
1517 {
1518 return None;
1519 }
1520 Some(DomRoot::from_ref(container))
1522 }
1523
1524 fn ReportError(&self, cx: SafeJSContext, error: HandleValue, can_gc: CanGc) {
1526 self.as_global_scope()
1527 .report_an_exception(cx, error, can_gc);
1528 }
1529
1530 fn Navigator(&self) -> DomRoot<Navigator> {
1532 self.navigator
1533 .or_init(|| Navigator::new(self, CanGc::note()))
1534 }
1535
1536 fn ClientInformation(&self) -> DomRoot<Navigator> {
1538 self.Navigator()
1539 }
1540
1541 fn SetTimeout(
1543 &self,
1544 cx: &mut js::context::JSContext,
1545 callback: TrustedScriptOrStringOrFunction,
1546 timeout: i32,
1547 args: Vec<HandleValue>,
1548 ) -> Fallible<i32> {
1549 let callback = match callback {
1550 TrustedScriptOrStringOrFunction::String(i) => {
1551 TimerCallback::StringTimerCallback(TrustedScriptOrString::String(i))
1552 },
1553 TrustedScriptOrStringOrFunction::TrustedScript(i) => {
1554 TimerCallback::StringTimerCallback(TrustedScriptOrString::TrustedScript(i))
1555 },
1556 TrustedScriptOrStringOrFunction::Function(i) => TimerCallback::FunctionTimerCallback(i),
1557 };
1558 self.as_global_scope().set_timeout_or_interval(
1559 cx,
1560 callback,
1561 args,
1562 Duration::from_millis(timeout.max(0) as u64),
1563 IsInterval::NonInterval,
1564 )
1565 }
1566
1567 fn ClearTimeout(&self, handle: i32) {
1569 self.as_global_scope().clear_timeout_or_interval(handle);
1570 }
1571
1572 fn SetInterval(
1574 &self,
1575 cx: &mut js::context::JSContext,
1576 callback: TrustedScriptOrStringOrFunction,
1577 timeout: i32,
1578 args: Vec<HandleValue>,
1579 ) -> Fallible<i32> {
1580 let callback = match callback {
1581 TrustedScriptOrStringOrFunction::String(i) => {
1582 TimerCallback::StringTimerCallback(TrustedScriptOrString::String(i))
1583 },
1584 TrustedScriptOrStringOrFunction::TrustedScript(i) => {
1585 TimerCallback::StringTimerCallback(TrustedScriptOrString::TrustedScript(i))
1586 },
1587 TrustedScriptOrStringOrFunction::Function(i) => TimerCallback::FunctionTimerCallback(i),
1588 };
1589 self.as_global_scope().set_timeout_or_interval(
1590 cx,
1591 callback,
1592 args,
1593 Duration::from_millis(timeout.max(0) as u64),
1594 IsInterval::Interval,
1595 )
1596 }
1597
1598 fn ClearInterval(&self, handle: i32) {
1600 self.ClearTimeout(handle);
1601 }
1602
1603 fn QueueMicrotask(&self, callback: Rc<VoidFunction>) {
1605 ScriptThread::enqueue_microtask(Microtask::User(UserMicrotask {
1606 callback,
1607 pipeline: self.pipeline_id(),
1608 }));
1609 }
1610
1611 fn CreateImageBitmap(
1613 &self,
1614 realm: &mut CurrentRealm,
1615 image: ImageBitmapSource,
1616 options: &ImageBitmapOptions,
1617 ) -> Rc<Promise> {
1618 ImageBitmap::create_image_bitmap(
1619 self.as_global_scope(),
1620 image,
1621 0,
1622 0,
1623 None,
1624 None,
1625 options,
1626 realm,
1627 )
1628 }
1629
1630 fn CreateImageBitmap_(
1632 &self,
1633 realm: &mut CurrentRealm,
1634 image: ImageBitmapSource,
1635 sx: i32,
1636 sy: i32,
1637 sw: i32,
1638 sh: i32,
1639 options: &ImageBitmapOptions,
1640 ) -> Rc<Promise> {
1641 ImageBitmap::create_image_bitmap(
1642 self.as_global_scope(),
1643 image,
1644 sx,
1645 sy,
1646 Some(sw),
1647 Some(sh),
1648 options,
1649 realm,
1650 )
1651 }
1652
1653 fn Window(&self) -> DomRoot<WindowProxy> {
1655 self.window_proxy()
1656 }
1657
1658 fn Self_(&self) -> DomRoot<WindowProxy> {
1660 self.window_proxy()
1661 }
1662
1663 fn Frames(&self) -> DomRoot<WindowProxy> {
1665 self.window_proxy()
1666 }
1667
1668 fn Length(&self) -> u32 {
1670 self.Document().iframes().iter().count() as u32
1671 }
1672
1673 fn GetParent(&self) -> Option<DomRoot<WindowProxy>> {
1675 let window_proxy = self.undiscarded_window_proxy()?;
1677
1678 if let Some(parent) = window_proxy.parent() {
1680 return Some(DomRoot::from_ref(parent));
1681 }
1682 Some(window_proxy)
1684 }
1685
1686 fn GetTop(&self) -> Option<DomRoot<WindowProxy>> {
1688 let window_proxy = self.undiscarded_window_proxy()?;
1690
1691 Some(DomRoot::from_ref(window_proxy.top()))
1693 }
1694
1695 fn Performance(&self) -> DomRoot<Performance> {
1698 self.performance.or_init(|| {
1699 Performance::new(
1700 self.as_global_scope(),
1701 self.navigation_start.get(),
1702 CanGc::note(),
1703 )
1704 })
1705 }
1706
1707 global_event_handlers!();
1709
1710 window_event_handlers!();
1712
1713 fn Screen(&self, can_gc: CanGc) -> DomRoot<Screen> {
1715 self.screen.or_init(|| Screen::new(self, can_gc))
1716 }
1717
1718 fn GetVisualViewport(&self, can_gc: CanGc) -> Option<DomRoot<VisualViewport>> {
1720 if !self.Document().is_fully_active() {
1724 return None;
1725 }
1726
1727 Some(self.get_or_init_visual_viewport(can_gc))
1728 }
1729
1730 fn Btoa(&self, btoa: DOMString) -> Fallible<DOMString> {
1732 base64_btoa(btoa)
1733 }
1734
1735 fn Atob(&self, atob: DOMString) -> Fallible<DOMString> {
1737 base64_atob(atob)
1738 }
1739
1740 fn RequestAnimationFrame(&self, callback: Rc<FrameRequestCallback>) -> u32 {
1742 self.Document()
1743 .request_animation_frame(AnimationFrameCallback::FrameRequestCallback { callback })
1744 }
1745
1746 fn CancelAnimationFrame(&self, ident: u32) {
1748 let doc = self.Document();
1749 doc.cancel_animation_frame(ident);
1750 }
1751
1752 fn PostMessage(
1754 &self,
1755 cx: &mut JSContext,
1756 message: HandleValue,
1757 target_origin: USVString,
1758 transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
1759 ) -> ErrorResult {
1760 let incumbent = GlobalScope::incumbent().expect("no incumbent global?");
1761 let source = incumbent.as_window();
1762 let source_origin = source.Document().origin().immutable().clone();
1763
1764 self.post_message_impl(&target_origin, source_origin, source, cx, message, transfer)
1765 }
1766
1767 fn PostMessage_(
1769 &self,
1770 cx: &mut JSContext,
1771 message: HandleValue,
1772 options: RootedTraceableBox<WindowPostMessageOptions>,
1773 ) -> ErrorResult {
1774 let mut rooted = CustomAutoRooter::new(
1775 options
1776 .parent
1777 .transfer
1778 .iter()
1779 .map(|js: &RootedTraceableBox<Heap<*mut JSObject>>| js.get())
1780 .collect(),
1781 );
1782 #[expect(unsafe_code)]
1783 let transfer = unsafe { CustomAutoRooterGuard::new(cx.raw_cx(), &mut rooted) };
1784
1785 let incumbent = GlobalScope::incumbent().expect("no incumbent global?");
1786 let source = incumbent.as_window();
1787
1788 let source_origin = source.Document().origin().immutable().clone();
1789
1790 self.post_message_impl(
1791 &options.targetOrigin,
1792 source_origin,
1793 source,
1794 cx,
1795 message,
1796 transfer,
1797 )
1798 }
1799
1800 fn CaptureEvents(&self) {
1802 }
1804
1805 fn ReleaseEvents(&self) {
1807 }
1809
1810 fn WebdriverCallback(&self, realm: &mut CurrentRealm, value: HandleValue) {
1812 let webdriver_script_sender = self.webdriver_script_chan.borrow_mut().take();
1813 if let Some(webdriver_script_sender) = webdriver_script_sender {
1814 let result = jsval_to_webdriver(realm, &self.globalscope, value);
1815 let _ = webdriver_script_sender.send(result);
1816 }
1817 }
1818
1819 fn WebdriverException(&self, cx: &mut JSContext, value: HandleValue) {
1820 let webdriver_script_sender = self.webdriver_script_chan.borrow_mut().take();
1821 if let Some(webdriver_script_sender) = webdriver_script_sender {
1822 let error_info = ErrorInfo::from_value(value, cx.into(), CanGc::from_cx(cx));
1823 let _ = webdriver_script_sender.send(Err(
1824 JavaScriptEvaluationError::EvaluationFailure(Some(
1825 javascript_error_info_from_error_info(cx, &error_info, value),
1826 )),
1827 ));
1828 }
1829 }
1830
1831 fn WebdriverElement(&self, id: DOMString) -> Option<DomRoot<Element>> {
1832 find_node_by_unique_id_in_document(&self.Document(), id.into()).and_then(Root::downcast)
1833 }
1834
1835 fn WebdriverFrame(&self, browsing_context_id: DOMString) -> Option<DomRoot<WindowProxy>> {
1836 self.Document()
1837 .iframes()
1838 .iter()
1839 .find(|iframe| {
1840 iframe
1841 .browsing_context_id()
1842 .as_ref()
1843 .map(BrowsingContextId::to_string) ==
1844 Some(browsing_context_id.to_string())
1845 })
1846 .and_then(|iframe| iframe.GetContentWindow())
1847 }
1848
1849 fn WebdriverWindow(&self, webview_id: DOMString) -> DomRoot<WindowProxy> {
1850 let window_proxy = &self
1851 .window_proxy
1852 .get()
1853 .expect("Should always have a WindowProxy when calling WebdriverWindow");
1854 assert!(
1855 self.is_top_level(),
1856 "Window must be top level browsing context."
1857 );
1858 assert!(self.webview_id().to_string() == webview_id);
1859 DomRoot::from_ref(window_proxy)
1860 }
1861
1862 fn WebdriverShadowRoot(&self, id: DOMString) -> Option<DomRoot<ShadowRoot>> {
1863 find_node_by_unique_id_in_document(&self.Document(), id.into()).and_then(Root::downcast)
1864 }
1865
1866 fn GetComputedStyle(
1868 &self,
1869 element: &Element,
1870 pseudo: Option<DOMString>,
1871 ) -> DomRoot<CSSStyleDeclaration> {
1872 let mut is_null = false;
1876
1877 let pseudo = pseudo.map(|mut s| {
1883 s.make_ascii_lowercase();
1884 s
1885 });
1886 let pseudo = match pseudo {
1887 Some(ref pseudo) if pseudo == ":before" || pseudo == "::before" => {
1888 Some(PseudoElement::Before)
1889 },
1890 Some(ref pseudo) if pseudo == ":after" || pseudo == "::after" => {
1891 Some(PseudoElement::After)
1892 },
1893 Some(ref pseudo) if pseudo == "::selection" => Some(PseudoElement::Selection),
1894 Some(ref pseudo) if pseudo == "::marker" => Some(PseudoElement::Marker),
1895 Some(ref pseudo) if pseudo == "::placeholder" => Some(PseudoElement::Placeholder),
1896 Some(ref pseudo) if pseudo.starts_with(':') => {
1897 is_null = true;
1900 None
1901 },
1902 _ => None,
1903 };
1904
1905 CSSStyleDeclaration::new(
1921 self,
1922 if is_null {
1923 CSSStyleOwner::Null
1924 } else {
1925 CSSStyleOwner::Element(Dom::from_ref(element))
1926 },
1927 pseudo,
1928 CSSModificationAccess::Readonly,
1929 CanGc::note(),
1930 )
1931 }
1932
1933 fn InnerHeight(&self) -> i32 {
1936 self.viewport_details
1937 .get()
1938 .size
1939 .height
1940 .to_i32()
1941 .unwrap_or(0)
1942 }
1943
1944 fn InnerWidth(&self) -> i32 {
1947 self.viewport_details.get().size.width.to_i32().unwrap_or(0)
1948 }
1949
1950 fn ScrollX(&self) -> i32 {
1952 self.scroll_offset().x as i32
1953 }
1954
1955 fn PageXOffset(&self) -> i32 {
1957 self.ScrollX()
1958 }
1959
1960 fn ScrollY(&self) -> i32 {
1962 self.scroll_offset().y as i32
1963 }
1964
1965 fn PageYOffset(&self) -> i32 {
1967 self.ScrollY()
1968 }
1969
1970 fn Scroll(&self, options: &ScrollToOptions) {
1972 let x = options.left.unwrap_or(0.0) as f32;
1977
1978 let y = options.top.unwrap_or(0.0) as f32;
1981
1982 self.scroll(x, y, options.parent.behavior);
1984 }
1985
1986 fn Scroll_(&self, x: f64, y: f64) {
1988 self.scroll(x as f32, y as f32, ScrollBehavior::Auto);
1992 }
1993
1994 fn ScrollTo(&self, options: &ScrollToOptions) {
1999 self.Scroll(options);
2000 }
2001
2002 fn ScrollTo_(&self, x: f64, y: f64) {
2007 self.Scroll_(x, y)
2008 }
2009
2010 fn ScrollBy(&self, options: &ScrollToOptions) {
2012 let mut options = options.clone();
2018 let x = options.left.unwrap_or(0.0);
2019 let x = if x.is_finite() { x } else { 0.0 };
2020 let y = options.top.unwrap_or(0.0);
2021 let y = if y.is_finite() { y } else { 0.0 };
2022
2023 options.left.replace(x + self.ScrollX() as f64);
2025
2026 options.top.replace(y + self.ScrollY() as f64);
2028
2029 self.Scroll(&options)
2031 }
2032
2033 fn ScrollBy_(&self, x: f64, y: f64) {
2035 let mut options = ScrollToOptions::empty();
2039
2040 options.left.replace(x);
2043
2044 options.top.replace(y);
2046
2047 self.ScrollBy(&options);
2049 }
2050
2051 fn ResizeTo(&self, width: i32, height: i32) {
2053 let window_proxy = match self.window_proxy.get() {
2055 Some(proxy) => proxy,
2056 None => return,
2057 };
2058
2059 if !window_proxy.is_auxiliary() {
2062 return;
2063 }
2064
2065 let dpr = self.device_pixel_ratio();
2066 let size = Size2D::new(width, height).to_f32() * dpr;
2067 self.send_to_embedder(EmbedderMsg::ResizeTo(self.webview_id(), size.to_i32()));
2068 }
2069
2070 fn ResizeBy(&self, x: i32, y: i32) {
2072 let size = self.client_window().size();
2073 self.ResizeTo(x + size.width, y + size.height)
2075 }
2076
2077 fn MoveTo(&self, x: i32, y: i32) {
2079 let dpr = self.device_pixel_ratio();
2082 let point = Point2D::new(x, y).to_f32() * dpr;
2083 let msg = EmbedderMsg::MoveTo(self.webview_id(), point.to_i32());
2084 self.send_to_embedder(msg);
2085 }
2086
2087 fn MoveBy(&self, x: i32, y: i32) {
2089 let origin = self.client_window().min;
2090 self.MoveTo(x + origin.x, y + origin.y)
2092 }
2093
2094 fn ScreenX(&self) -> i32 {
2096 self.client_window().min.x
2097 }
2098
2099 fn ScreenY(&self) -> i32 {
2101 self.client_window().min.y
2102 }
2103
2104 fn OuterHeight(&self) -> i32 {
2106 self.client_window().height()
2107 }
2108
2109 fn OuterWidth(&self) -> i32 {
2111 self.client_window().width()
2112 }
2113
2114 fn DevicePixelRatio(&self) -> Finite<f64> {
2116 Finite::wrap(self.device_pixel_ratio().get() as f64)
2117 }
2118
2119 fn Status(&self) -> DOMString {
2121 self.status.borrow().clone()
2122 }
2123
2124 fn SetStatus(&self, status: DOMString) {
2126 *self.status.borrow_mut() = status
2127 }
2128
2129 fn MatchMedia(&self, query: DOMString) -> DomRoot<MediaQueryList> {
2131 let media_query_list = MediaList::parse_media_list(&query.str(), self);
2132 let document = self.Document();
2133 let mql = MediaQueryList::new(&document, media_query_list, CanGc::note());
2134 self.media_query_lists.track(&*mql);
2135 mql
2136 }
2137
2138 fn Fetch(
2140 &self,
2141 realm: &mut CurrentRealm,
2142 input: RequestOrUSVString,
2143 init: RootedTraceableBox<RequestInit>,
2144 ) -> Rc<Promise> {
2145 fetch::Fetch(self.upcast(), input, init, realm)
2146 }
2147
2148 fn FetchLater(
2150 &self,
2151 input: RequestInfo,
2152 init: RootedTraceableBox<DeferredRequestInit>,
2153 can_gc: CanGc,
2154 ) -> Fallible<DomRoot<FetchLaterResult>> {
2155 fetch::FetchLater(self, input, init, can_gc)
2156 }
2157
2158 #[cfg(feature = "bluetooth")]
2159 fn TestRunner(&self) -> DomRoot<TestRunner> {
2160 self.test_runner
2161 .or_init(|| TestRunner::new(self.upcast(), CanGc::note()))
2162 }
2163
2164 fn RunningAnimationCount(&self) -> u32 {
2165 self.document
2166 .get()
2167 .map_or(0, |d| d.animations().running_animation_count() as u32)
2168 }
2169
2170 fn SetName(&self, name: DOMString) {
2172 if let Some(proxy) = self.undiscarded_window_proxy() {
2173 proxy.set_name(name);
2174 }
2175 }
2176
2177 fn Name(&self) -> DOMString {
2179 match self.undiscarded_window_proxy() {
2180 Some(proxy) => proxy.get_name(),
2181 None => "".into(),
2182 }
2183 }
2184
2185 fn Origin(&self) -> USVString {
2187 USVString(self.origin().immutable().ascii_serialization())
2188 }
2189
2190 fn GetSelection(&self) -> Option<DomRoot<Selection>> {
2192 self.document
2193 .get()
2194 .and_then(|d| d.GetSelection(CanGc::note()))
2195 }
2196
2197 fn Event(&self, cx: SafeJSContext, rval: MutableHandleValue) {
2199 if let Some(ref event) = *self.current_event.borrow() {
2200 event
2201 .reflector()
2202 .get_jsobject()
2203 .safe_to_jsval(cx, rval, CanGc::note());
2204 }
2205 }
2206
2207 fn IsSecureContext(&self) -> bool {
2208 self.as_global_scope().is_secure_context()
2209 }
2210
2211 fn NamedGetter(&self, name: DOMString) -> Option<NamedPropertyValue> {
2213 if name.is_empty() {
2214 return None;
2215 }
2216 let document = self.Document();
2217
2218 let iframes: Vec<_> = document
2220 .iframes()
2221 .iter()
2222 .filter(|iframe| {
2223 if let Some(window) = iframe.GetContentWindow() {
2224 return window.get_name() == name;
2225 }
2226 false
2227 })
2228 .collect();
2229
2230 let iframe_iter = iframes.iter().map(|iframe| iframe.upcast::<Element>());
2231
2232 let name = Atom::from(name);
2233
2234 let elements_with_name = document.get_elements_with_name(&name);
2236 let name_iter = elements_with_name
2237 .iter()
2238 .map(|element| &**element)
2239 .filter(|elem| is_named_element_with_name_attribute(elem));
2240 let elements_with_id = document.get_elements_with_id(&name);
2241 let id_iter = elements_with_id
2242 .iter()
2243 .map(|element| &**element)
2244 .filter(|elem| is_named_element_with_id_attribute(elem));
2245
2246 for elem in iframe_iter.clone() {
2248 if let Some(nested_window_proxy) = elem
2249 .downcast::<HTMLIFrameElement>()
2250 .and_then(|iframe| iframe.GetContentWindow())
2251 {
2252 return Some(NamedPropertyValue::WindowProxy(nested_window_proxy));
2253 }
2254 }
2255
2256 let mut elements = iframe_iter.chain(name_iter).chain(id_iter);
2257
2258 let first = elements.next()?;
2259
2260 if elements.next().is_none() {
2261 return Some(NamedPropertyValue::Element(DomRoot::from_ref(first)));
2263 }
2264
2265 #[derive(JSTraceable, MallocSizeOf)]
2267 struct WindowNamedGetter {
2268 #[no_trace]
2269 name: Atom,
2270 }
2271 impl CollectionFilter for WindowNamedGetter {
2272 fn filter(&self, elem: &Element, _root: &Node) -> bool {
2273 let type_ = match elem.upcast::<Node>().type_id() {
2274 NodeTypeId::Element(ElementTypeId::HTMLElement(type_)) => type_,
2275 _ => return false,
2276 };
2277 if elem.get_id().as_ref() == Some(&self.name) {
2278 return true;
2279 }
2280 match type_ {
2281 HTMLElementTypeId::HTMLEmbedElement |
2282 HTMLElementTypeId::HTMLFormElement |
2283 HTMLElementTypeId::HTMLImageElement |
2284 HTMLElementTypeId::HTMLObjectElement => {
2285 elem.get_name().as_ref() == Some(&self.name)
2286 },
2287 _ => false,
2288 }
2289 }
2290 }
2291 let collection = HTMLCollection::create(
2292 self,
2293 document.upcast(),
2294 Box::new(WindowNamedGetter { name }),
2295 CanGc::note(),
2296 );
2297 Some(NamedPropertyValue::HTMLCollection(collection))
2298 }
2299
2300 fn SupportedPropertyNames(&self) -> Vec<DOMString> {
2302 let mut names_with_first_named_element_map: HashMap<&Atom, &Element> = HashMap::new();
2303
2304 let document = self.Document();
2305 let name_map = document.name_map();
2306 for (name, elements) in &name_map.0 {
2307 if name.is_empty() {
2308 continue;
2309 }
2310 let mut name_iter = elements
2311 .iter()
2312 .filter(|elem| is_named_element_with_name_attribute(elem));
2313 if let Some(first) = name_iter.next() {
2314 names_with_first_named_element_map.insert(name, first);
2315 }
2316 }
2317 let id_map = document.id_map();
2318 for (id, elements) in &id_map.0 {
2319 if id.is_empty() {
2320 continue;
2321 }
2322 let mut id_iter = elements
2323 .iter()
2324 .filter(|elem| is_named_element_with_id_attribute(elem));
2325 if let Some(first) = id_iter.next() {
2326 match names_with_first_named_element_map.entry(id) {
2327 Entry::Vacant(entry) => drop(entry.insert(first)),
2328 Entry::Occupied(mut entry) => {
2329 if first.upcast::<Node>().is_before(entry.get().upcast()) {
2330 *entry.get_mut() = first;
2331 }
2332 },
2333 }
2334 }
2335 }
2336
2337 let mut names_with_first_named_element_vec: Vec<(&Atom, &Element)> =
2338 names_with_first_named_element_map
2339 .iter()
2340 .map(|(k, v)| (*k, *v))
2341 .collect();
2342 names_with_first_named_element_vec.sort_unstable_by(|a, b| {
2343 if a.1 == b.1 {
2344 a.0.cmp(b.0)
2347 } else if a.1.upcast::<Node>().is_before(b.1.upcast::<Node>()) {
2348 cmp::Ordering::Less
2349 } else {
2350 cmp::Ordering::Greater
2351 }
2352 });
2353
2354 names_with_first_named_element_vec
2355 .iter()
2356 .map(|(k, _v)| DOMString::from(&***k))
2357 .collect()
2358 }
2359
2360 fn StructuredClone(
2362 &self,
2363 cx: SafeJSContext,
2364 value: HandleValue,
2365 options: RootedTraceableBox<StructuredSerializeOptions>,
2366 can_gc: CanGc,
2367 retval: MutableHandleValue,
2368 ) -> Fallible<()> {
2369 self.as_global_scope()
2370 .structured_clone(cx, value, options, retval, can_gc)
2371 }
2372
2373 fn TrustedTypes(&self, cx: &mut JSContext) -> DomRoot<TrustedTypePolicyFactory> {
2374 self.trusted_types
2375 .or_init(|| TrustedTypePolicyFactory::new(cx, self.as_global_scope()))
2376 }
2377}
2378
2379impl Window {
2380 pub(crate) fn scroll_offset(&self) -> Vector2D<f32, LayoutPixel> {
2381 self.scroll_offset_query_with_external_scroll_id(self.pipeline_id().root_scroll_id())
2382 }
2383
2384 pub(crate) fn create_named_properties_object(
2387 cx: SafeJSContext,
2388 proto: HandleObject,
2389 object: MutableHandleObject,
2390 ) {
2391 window_named_properties::create(cx, proto, object)
2392 }
2393
2394 pub(crate) fn current_event(&self) -> Option<DomRoot<Event>> {
2395 self.current_event
2396 .borrow()
2397 .as_ref()
2398 .map(|e| DomRoot::from_ref(&**e))
2399 }
2400
2401 pub(crate) fn set_current_event(&self, event: Option<&Event>) -> Option<DomRoot<Event>> {
2402 let current = self.current_event();
2403 *self.current_event.borrow_mut() = event.map(Dom::from_ref);
2404 current
2405 }
2406
2407 fn post_message_impl(
2409 &self,
2410 target_origin: &USVString,
2411 source_origin: ImmutableOrigin,
2412 source: &Window,
2413 cx: &mut JSContext,
2414 message: HandleValue,
2415 transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
2416 ) -> ErrorResult {
2417 let data = structuredclone::write(cx.into(), message, Some(transfer))?;
2419
2420 let target_origin = match target_origin.0[..].as_ref() {
2422 "*" => None,
2423 "/" => Some(source_origin.clone()),
2424 url => match ServoUrl::parse(url) {
2425 Ok(url) => Some(url.origin()),
2426 Err(_) => return Err(Error::Syntax(None)),
2427 },
2428 };
2429
2430 self.post_message(target_origin, source_origin, &source.window_proxy(), data);
2432 Ok(())
2433 }
2434
2435 pub(crate) fn paint_worklet(&self) -> DomRoot<Worklet> {
2437 self.paint_worklet
2438 .or_init(|| self.new_paint_worklet(CanGc::note()))
2439 }
2440
2441 pub(crate) fn has_document(&self) -> bool {
2442 self.document.get().is_some()
2443 }
2444
2445 pub(crate) fn clear_js_runtime(&self) {
2446 self.as_global_scope()
2447 .remove_web_messaging_and_dedicated_workers_infra();
2448
2449 if let Some(custom_elements) = self.custom_element_registry.get() {
2452 custom_elements.teardown();
2453 }
2454
2455 self.current_state.set(WindowState::Zombie);
2456 *self.js_runtime.borrow_mut() = None;
2457
2458 if let Some(proxy) = self.window_proxy.get() {
2461 let pipeline_id = self.pipeline_id();
2462 if let Some(currently_active) = proxy.currently_active() {
2463 if currently_active == pipeline_id {
2464 self.window_proxy.set(None);
2465 }
2466 }
2467 }
2468
2469 if let Some(performance) = self.performance.get() {
2470 performance.clear_and_disable_performance_entry_buffer();
2471 }
2472 self.as_global_scope()
2473 .task_manager()
2474 .cancel_all_tasks_and_ignore_future_tasks();
2475 }
2476
2477 pub(crate) fn scroll(&self, x: f32, y: f32, behavior: ScrollBehavior) {
2479 let xfinite = if x.is_finite() { x } else { 0.0 };
2481 let yfinite = if y.is_finite() { y } else { 0.0 };
2482
2483 let viewport = self.viewport_details.get().size;
2493
2494 let scrolling_area = self.scrolling_area_query(None).to_f32();
2513 let x = xfinite.clamp(0.0, 0.0f32.max(scrolling_area.width() - viewport.width));
2514 let y = yfinite.clamp(0.0, 0.0f32.max(scrolling_area.height() - viewport.height));
2515
2516 let scroll_offset = self.scroll_offset();
2519 if x == scroll_offset.x && y == scroll_offset.y {
2520 return;
2521 }
2522
2523 self.perform_a_scroll(x, y, self.pipeline_id().root_scroll_id(), behavior, None);
2528 }
2529
2530 pub(crate) fn perform_a_scroll(
2532 &self,
2533 x: f32,
2534 y: f32,
2535 scroll_id: ExternalScrollId,
2536 _behavior: ScrollBehavior,
2537 element: Option<&Element>,
2538 ) {
2539 let (reflow_phases_run, _) =
2543 self.reflow(ReflowGoal::UpdateScrollNode(scroll_id, Vector2D::new(x, y)));
2544 if reflow_phases_run.needs_frame() {
2545 self.paint_api()
2546 .generate_frame(vec![self.webview_id().into()]);
2547 }
2548
2549 if reflow_phases_run.contains(ReflowPhasesRun::UpdatedScrollNodeOffset) {
2554 match element {
2555 Some(element) if !scroll_id.is_root() => element.handle_scroll_event(),
2556 _ => self.Document().handle_viewport_scroll_event(),
2557 };
2558 }
2559 }
2560
2561 pub(crate) fn device_pixel_ratio(&self) -> Scale<f32, CSSPixel, DevicePixel> {
2562 self.viewport_details.get().hidpi_scale_factor
2563 }
2564
2565 fn client_window(&self) -> DeviceIndependentIntRect {
2566 let (sender, receiver) = generic_channel::channel().expect("Failed to create IPC channel!");
2567
2568 self.send_to_embedder(EmbedderMsg::GetWindowRect(self.webview_id(), sender));
2569
2570 receiver.recv().unwrap_or_default()
2571 }
2572
2573 pub(crate) fn advance_animation_clock(&self, delta: TimeDuration) {
2576 self.Document()
2577 .advance_animation_timeline_for_testing(delta);
2578 ScriptThread::handle_tick_all_animations_for_testing(self.pipeline_id());
2579 }
2580
2581 pub(crate) fn reflow(&self, reflow_goal: ReflowGoal) -> (ReflowPhasesRun, ReflowStatistics) {
2589 let document = self.Document();
2590
2591 if !document.is_fully_active() {
2593 return Default::default();
2594 }
2595
2596 self.Document().ensure_safe_to_run_script_or_layout();
2597
2598 let pipeline_id = self.pipeline_id();
2602 if reflow_goal == ReflowGoal::UpdateTheRendering &&
2603 self.layout_blocker.get().layout_blocked()
2604 {
2605 debug!("Suppressing pre-load-event reflow pipeline {pipeline_id}");
2606 return Default::default();
2607 }
2608
2609 debug!("script: performing reflow for goal {reflow_goal:?}");
2610 let marker = if self.need_emit_timeline_marker(TimelineMarkerType::Reflow) {
2611 Some(TimelineMarker::start("Reflow".to_owned()))
2612 } else {
2613 None
2614 };
2615
2616 let restyle_reason = document.restyle_reason();
2617 document.clear_restyle_reasons();
2618 let restyle = if restyle_reason.needs_restyle() {
2619 debug!("Invalidating layout cache due to reflow condition {restyle_reason:?}",);
2620 self.layout_marker.borrow().set(false);
2622 *self.layout_marker.borrow_mut() = Rc::new(Cell::new(true));
2624
2625 let stylesheets_changed = document.flush_stylesheets_for_reflow();
2626 let pending_restyles = document.drain_pending_restyles();
2627 let dirty_root = document
2628 .take_dirty_root()
2629 .filter(|_| !stylesheets_changed)
2630 .or_else(|| document.GetDocumentElement())
2631 .map(|root| root.upcast::<Node>().to_trusted_node_address());
2632
2633 Some(ReflowRequestRestyle {
2634 reason: restyle_reason,
2635 dirty_root,
2636 stylesheets_changed,
2637 pending_restyles,
2638 })
2639 } else {
2640 None
2641 };
2642
2643 let document_context = self.web_font_context();
2644
2645 let reflow = ReflowRequest {
2647 document: document.upcast::<Node>().to_trusted_node_address(),
2648 epoch: document.current_rendering_epoch(),
2649 restyle,
2650 viewport_details: self.viewport_details.get(),
2651 origin: self.origin().immutable().clone(),
2652 reflow_goal,
2653 dom_count: document.dom_count(),
2654 animation_timeline_value: document.current_animation_timeline_value(),
2655 animations: document.animations().sets.clone(),
2656 animating_images: document.image_animation_manager().animating_images(),
2657 highlighted_dom_node: document.highlighted_dom_node().map(|node| node.to_opaque()),
2658 document_context,
2659 };
2660
2661 let Some(reflow_result) = self.layout.borrow_mut().reflow(reflow) else {
2662 return Default::default();
2663 };
2664
2665 debug!("script: layout complete");
2666 if let Some(marker) = marker {
2667 self.emit_timeline_marker(marker.end());
2668 }
2669
2670 self.handle_pending_images_post_reflow(
2671 reflow_result.pending_images,
2672 reflow_result.pending_rasterization_images,
2673 reflow_result.pending_svg_elements_for_serialization,
2674 );
2675
2676 if let Some(iframe_sizes) = reflow_result.iframe_sizes {
2677 document
2678 .iframes_mut()
2679 .handle_new_iframe_sizes_after_layout(self, iframe_sizes);
2680 }
2681
2682 document.update_animations_post_reflow();
2683
2684 (
2685 reflow_result.reflow_phases_run,
2686 reflow_result.reflow_statistics,
2687 )
2688 }
2689
2690 pub(crate) fn request_screenshot_readiness(&self, can_gc: CanGc) {
2691 self.has_pending_screenshot_readiness_request.set(true);
2692 self.maybe_resolve_pending_screenshot_readiness_requests(can_gc);
2693 }
2694
2695 pub(crate) fn maybe_resolve_pending_screenshot_readiness_requests(&self, can_gc: CanGc) {
2696 let pending_request = self.has_pending_screenshot_readiness_request.get();
2697 if !pending_request {
2698 return;
2699 }
2700
2701 let document = self.Document();
2702 if document.ReadyState() != DocumentReadyState::Complete {
2703 return;
2704 }
2705
2706 if document.render_blocking_element_count() > 0 {
2707 return;
2708 }
2709
2710 if document.GetDocumentElement().is_some_and(|elem| {
2714 elem.has_class(&atom!("reftest-wait"), CaseSensitivity::CaseSensitive) ||
2715 elem.has_class(&Atom::from("test-wait"), CaseSensitivity::CaseSensitive)
2716 }) {
2717 return;
2718 }
2719
2720 if self.font_context().web_fonts_still_loading() != 0 {
2721 return;
2722 }
2723
2724 if self.Document().Fonts(can_gc).waiting_to_fullfill_promise() {
2725 return;
2726 }
2727
2728 if !self.pending_layout_images.borrow().is_empty() ||
2729 !self.pending_images_for_rasterization.borrow().is_empty()
2730 {
2731 return;
2732 }
2733
2734 let document = self.Document();
2735 if document.needs_rendering_update() {
2736 return;
2737 }
2738
2739 let epoch = document.current_rendering_epoch();
2742 let pipeline_id = self.pipeline_id();
2743 debug!("Ready to take screenshot of {pipeline_id:?} at epoch={epoch:?}");
2744
2745 self.send_to_constellation(
2746 ScriptToConstellationMessage::RespondToScreenshotReadinessRequest(
2747 ScreenshotReadinessResponse::Ready(epoch),
2748 ),
2749 );
2750 self.has_pending_screenshot_readiness_request.set(false);
2751 }
2752
2753 pub(crate) fn reflow_if_reflow_timer_expired(&self) {
2756 if !matches!(
2759 self.layout_blocker.get(),
2760 LayoutBlocker::Parsing(instant) if instant + INITIAL_REFLOW_DELAY < Instant::now()
2761 ) {
2762 return;
2763 }
2764 self.allow_layout_if_necessary();
2765 }
2766
2767 pub(crate) fn prevent_layout_until_load_event(&self) {
2771 if !matches!(self.layout_blocker.get(), LayoutBlocker::WaitingForParse) {
2774 return;
2775 }
2776
2777 self.layout_blocker
2778 .set(LayoutBlocker::Parsing(Instant::now()));
2779 }
2780
2781 pub(crate) fn allow_layout_if_necessary(&self) {
2784 if matches!(
2785 self.layout_blocker.get(),
2786 LayoutBlocker::FiredLoadEventOrParsingTimerExpired
2787 ) {
2788 return;
2789 }
2790
2791 self.layout_blocker
2792 .set(LayoutBlocker::FiredLoadEventOrParsingTimerExpired);
2793
2794 let document = self.Document();
2806 if !document.is_render_blocked() && document.update_the_rendering().0.needs_frame() {
2807 self.paint_api()
2808 .generate_frame(vec![self.webview_id().into()]);
2809 }
2810 }
2811
2812 pub(crate) fn layout_blocked(&self) -> bool {
2813 self.layout_blocker.get().layout_blocked()
2814 }
2815
2816 pub(crate) fn layout_reflow(&self, query_msg: QueryMsg) {
2818 self.reflow(ReflowGoal::LayoutQuery(query_msg));
2819 }
2820
2821 pub(crate) fn resolved_font_style_query(
2822 &self,
2823 node: &Node,
2824 value: String,
2825 ) -> Option<ServoArc<Font>> {
2826 self.layout_reflow(QueryMsg::ResolvedFontStyleQuery);
2827
2828 let document = self.Document();
2829 let animations = document.animations().sets.clone();
2830 self.layout.borrow().query_resolved_font_style(
2831 node.to_trusted_node_address(),
2832 &value,
2833 animations,
2834 document.current_animation_timeline_value(),
2835 )
2836 }
2837
2838 pub(crate) fn padding_query_without_reflow(&self, node: &Node) -> Option<PhysicalSides> {
2843 let layout = self.layout.borrow();
2844 layout.query_padding(node.to_trusted_node_address())
2845 }
2846
2847 pub(crate) fn box_area_query_without_reflow(
2852 &self,
2853 node: &Node,
2854 area: BoxAreaType,
2855 exclude_transform_and_inline: bool,
2856 ) -> Option<Rect<Au, CSSPixel>> {
2857 let layout = self.layout.borrow();
2858 layout.ensure_stacking_context_tree(self.viewport_details.get());
2859 layout.query_box_area(
2860 node.to_trusted_node_address(),
2861 area,
2862 exclude_transform_and_inline,
2863 )
2864 }
2865
2866 pub(crate) fn box_area_query(
2867 &self,
2868 node: &Node,
2869 area: BoxAreaType,
2870 exclude_transform_and_inline: bool,
2871 ) -> Option<Rect<Au, CSSPixel>> {
2872 self.layout_reflow(QueryMsg::BoxArea);
2873 self.box_area_query_without_reflow(node, area, exclude_transform_and_inline)
2874 }
2875
2876 pub(crate) fn box_areas_query(&self, node: &Node, area: BoxAreaType) -> CSSPixelRectIterator {
2877 self.layout_reflow(QueryMsg::BoxAreas);
2878 self.layout
2879 .borrow()
2880 .query_box_areas(node.to_trusted_node_address(), area)
2881 }
2882
2883 pub(crate) fn client_rect_query(&self, node: &Node) -> Rect<i32, CSSPixel> {
2884 self.layout_reflow(QueryMsg::ClientRectQuery);
2885 self.layout
2886 .borrow()
2887 .query_client_rect(node.to_trusted_node_address())
2888 }
2889
2890 pub(crate) fn current_css_zoom_query(&self, node: &Node) -> f32 {
2891 self.layout_reflow(QueryMsg::CurrentCSSZoomQuery);
2892 self.layout
2893 .borrow()
2894 .query_current_css_zoom(node.to_trusted_node_address())
2895 }
2896
2897 pub(crate) fn scrolling_area_query(&self, node: Option<&Node>) -> Rect<i32, CSSPixel> {
2900 self.layout_reflow(QueryMsg::ScrollingAreaOrOffsetQuery);
2901 self.layout
2902 .borrow()
2903 .query_scrolling_area(node.map(Node::to_trusted_node_address))
2904 }
2905
2906 pub(crate) fn scroll_offset_query(&self, node: &Node) -> Vector2D<f32, LayoutPixel> {
2907 let external_scroll_id = ExternalScrollId(
2908 combine_id_with_fragment_type(node.to_opaque().id(), FragmentType::FragmentBody),
2909 self.pipeline_id().into(),
2910 );
2911 self.scroll_offset_query_with_external_scroll_id(external_scroll_id)
2912 }
2913
2914 fn scroll_offset_query_with_external_scroll_id(
2915 &self,
2916 external_scroll_id: ExternalScrollId,
2917 ) -> Vector2D<f32, LayoutPixel> {
2918 self.layout_reflow(QueryMsg::ScrollingAreaOrOffsetQuery);
2919 self.scroll_offset_query_with_external_scroll_id_no_reflow(external_scroll_id)
2920 }
2921
2922 fn scroll_offset_query_with_external_scroll_id_no_reflow(
2923 &self,
2924 external_scroll_id: ExternalScrollId,
2925 ) -> Vector2D<f32, LayoutPixel> {
2926 self.layout
2927 .borrow()
2928 .scroll_offset(external_scroll_id)
2929 .unwrap_or_default()
2930 }
2931
2932 pub(crate) fn scroll_an_element(
2935 &self,
2936 element: &Element,
2937 x: f32,
2938 y: f32,
2939 behavior: ScrollBehavior,
2940 ) {
2941 let scroll_id = ExternalScrollId(
2942 combine_id_with_fragment_type(
2943 element.upcast::<Node>().to_opaque().id(),
2944 FragmentType::FragmentBody,
2945 ),
2946 self.pipeline_id().into(),
2947 );
2948
2949 self.perform_a_scroll(x, y, scroll_id, behavior, Some(element));
2953 }
2954
2955 pub(crate) fn resolved_style_query(
2956 &self,
2957 element: TrustedNodeAddress,
2958 pseudo: Option<PseudoElement>,
2959 property: PropertyId,
2960 ) -> DOMString {
2961 self.layout_reflow(QueryMsg::ResolvedStyleQuery);
2962
2963 let document = self.Document();
2964 let animations = document.animations().sets.clone();
2965 DOMString::from(self.layout.borrow().query_resolved_style(
2966 element,
2967 pseudo,
2968 property,
2969 animations,
2970 document.current_animation_timeline_value(),
2971 ))
2972 }
2973
2974 pub(crate) fn get_iframe_viewport_details_if_known(
2978 &self,
2979 browsing_context_id: BrowsingContextId,
2980 ) -> Option<ViewportDetails> {
2981 self.layout_reflow(QueryMsg::InnerWindowDimensionsQuery);
2983 self.Document()
2984 .iframes()
2985 .get(browsing_context_id)
2986 .and_then(|iframe| iframe.size)
2987 }
2988
2989 #[expect(unsafe_code)]
2990 pub(crate) fn offset_parent_query(
2991 &self,
2992 node: &Node,
2993 ) -> (Option<DomRoot<Element>>, Rect<Au, CSSPixel>) {
2994 self.layout_reflow(QueryMsg::OffsetParentQuery);
2995 let response = self
2996 .layout
2997 .borrow()
2998 .query_offset_parent(node.to_trusted_node_address());
2999 let element = response.node_address.and_then(|parent_node_address| {
3000 let node = unsafe { from_untrusted_node_address(parent_node_address) };
3001 DomRoot::downcast(node)
3002 });
3003 (element, response.rect)
3004 }
3005
3006 pub(crate) fn scroll_container_query(
3007 &self,
3008 node: Option<&Node>,
3009 flags: ScrollContainerQueryFlags,
3010 ) -> Option<ScrollContainerResponse> {
3011 self.layout_reflow(QueryMsg::ScrollParentQuery);
3012 self.layout
3013 .borrow()
3014 .query_scroll_container(node.map(Node::to_trusted_node_address), flags)
3015 }
3016
3017 #[expect(unsafe_code)]
3018 pub(crate) fn scrolling_box_query(
3019 &self,
3020 node: Option<&Node>,
3021 flags: ScrollContainerQueryFlags,
3022 ) -> Option<ScrollingBox> {
3023 self.scroll_container_query(node, flags)
3024 .and_then(|response| {
3025 Some(match response {
3026 ScrollContainerResponse::Viewport(overflow) => {
3027 (ScrollingBoxSource::Viewport(self.Document()), overflow)
3028 },
3029 ScrollContainerResponse::Element(parent_node_address, overflow) => {
3030 let node = unsafe { from_untrusted_node_address(parent_node_address) };
3031 (
3032 ScrollingBoxSource::Element(DomRoot::downcast(node)?),
3033 overflow,
3034 )
3035 },
3036 })
3037 })
3038 .map(|(source, overflow)| ScrollingBox::new(source, overflow))
3039 }
3040
3041 pub(crate) fn text_index_query_on_node_for_event(
3042 &self,
3043 node: &Node,
3044 mouse_event: &MouseEvent,
3045 ) -> Option<usize> {
3046 let point_in_viewport = mouse_event.point_in_viewport()?.map(Au::from_f32_px);
3050
3051 self.layout_reflow(QueryMsg::TextIndexQuery);
3052 self.layout
3053 .borrow()
3054 .query_text_index(node.to_trusted_node_address(), point_in_viewport)
3055 }
3056
3057 pub(crate) fn elements_from_point_query(
3058 &self,
3059 point: LayoutPoint,
3060 flags: ElementsFromPointFlags,
3061 ) -> Vec<ElementsFromPointResult> {
3062 self.layout_reflow(QueryMsg::ElementsFromPoint);
3063 self.layout().query_elements_from_point(point, flags)
3064 }
3065
3066 pub(crate) fn query_effective_overflow(&self, node: &Node) -> Option<AxesOverflow> {
3067 self.layout_reflow(QueryMsg::EffectiveOverflow);
3068 self.query_effective_overflow_without_reflow(node)
3069 }
3070
3071 pub(crate) fn query_effective_overflow_without_reflow(
3072 &self,
3073 node: &Node,
3074 ) -> Option<AxesOverflow> {
3075 self.layout
3076 .borrow()
3077 .query_effective_overflow(node.to_trusted_node_address())
3078 }
3079
3080 pub(crate) fn hit_test_from_input_event(
3081 &self,
3082 input_event: &ConstellationInputEvent,
3083 ) -> Option<HitTestResult> {
3084 self.hit_test_from_point_in_viewport(
3085 input_event.hit_test_result.as_ref()?.point_in_viewport,
3086 )
3087 }
3088
3089 #[expect(unsafe_code)]
3090 pub(crate) fn hit_test_from_point_in_viewport(
3091 &self,
3092 point_in_frame: Point2D<f32, CSSPixel>,
3093 ) -> Option<HitTestResult> {
3094 let result = self
3095 .elements_from_point_query(point_in_frame.cast_unit(), ElementsFromPointFlags::empty())
3096 .into_iter()
3097 .nth(0)?;
3098
3099 let point_relative_to_initial_containing_block =
3100 point_in_frame + self.scroll_offset().cast_unit();
3101
3102 let address = UntrustedNodeAddress(result.node.0 as *const c_void);
3105 Some(HitTestResult {
3106 node: unsafe { from_untrusted_node_address(address) },
3107 cursor: result.cursor,
3108 point_in_node: result.point_in_target,
3109 point_in_frame,
3110 point_relative_to_initial_containing_block,
3111 })
3112 }
3113
3114 pub(crate) fn init_window_proxy(&self, window_proxy: &WindowProxy) {
3115 assert!(self.window_proxy.get().is_none());
3116 self.window_proxy.set(Some(window_proxy));
3117 }
3118
3119 pub(crate) fn init_document(&self, document: &Document) {
3120 assert!(self.document.get().is_none());
3121 assert!(document.window() == self);
3122 self.document.set(Some(document));
3123 }
3124
3125 pub(crate) fn load_data_for_document(
3126 &self,
3127 url: ServoUrl,
3128 pipeline_id: PipelineId,
3129 ) -> LoadData {
3130 let source_document = self.Document();
3131 let secure_context = if self.is_top_level() {
3132 None
3133 } else {
3134 Some(self.IsSecureContext())
3135 };
3136 LoadData::new(
3137 LoadOrigin::Script(self.origin().snapshot()),
3138 url,
3139 source_document.about_base_url(),
3140 Some(pipeline_id),
3141 Referrer::ReferrerUrl(source_document.url()),
3142 source_document.get_referrer_policy(),
3143 secure_context,
3144 Some(source_document.insecure_requests_policy()),
3145 source_document.has_trustworthy_ancestor_origin(),
3146 source_document.creation_sandboxing_flag_set_considering_parent_iframe(),
3147 )
3148 }
3149
3150 pub(crate) fn set_viewport_details(&self, viewport_details: ViewportDetails) {
3153 self.viewport_details.set(viewport_details);
3154 if !self.layout_mut().set_viewport_details(viewport_details) {
3155 return;
3156 }
3157 self.Document()
3158 .add_restyle_reason(RestyleReason::ViewportChanged);
3159 }
3160
3161 pub(crate) fn viewport_details(&self) -> ViewportDetails {
3162 self.viewport_details.get()
3163 }
3164
3165 pub(crate) fn get_or_init_visual_viewport(&self, can_gc: CanGc) -> DomRoot<VisualViewport> {
3166 self.visual_viewport.or_init(|| {
3167 VisualViewport::new_from_layout_viewport(self, self.viewport_details().size, can_gc)
3168 })
3169 }
3170
3171 pub(crate) fn maybe_update_visual_viewport(
3173 &self,
3174 pinch_zoom_infos: PinchZoomInfos,
3175 can_gc: CanGc,
3176 ) {
3177 if pinch_zoom_infos.rect == Rect::from_size(self.viewport_details().size) &&
3180 self.visual_viewport.get().is_none()
3181 {
3182 return;
3183 }
3184
3185 let visual_viewport = self.get_or_init_visual_viewport(can_gc);
3186 let changes = visual_viewport.update_from_pinch_zoom_infos(pinch_zoom_infos);
3187
3188 if changes.intersects(VisualViewportChanges::DimensionChanged) {
3189 self.has_changed_visual_viewport_dimension.set(true);
3190 }
3191 if changes.intersects(VisualViewportChanges::OffsetChanged) {
3192 visual_viewport.handle_scroll_event();
3193 }
3194 }
3195
3196 pub(crate) fn theme(&self) -> Theme {
3198 self.theme.get()
3199 }
3200
3201 pub(crate) fn set_theme(&self, new_theme: Theme) {
3203 self.theme.set(new_theme);
3204 if !self.layout_mut().set_theme(new_theme) {
3205 return;
3206 }
3207 self.Document()
3208 .add_restyle_reason(RestyleReason::ThemeChanged);
3209 }
3210
3211 pub(crate) fn get_url(&self) -> ServoUrl {
3212 self.Document().url()
3213 }
3214
3215 pub(crate) fn windowproxy_handler(&self) -> &'static WindowProxyHandler {
3216 self.dom_static.windowproxy_handler
3217 }
3218
3219 pub(crate) fn add_resize_event(&self, event: ViewportDetails, event_type: WindowSizeType) {
3220 *self.unhandled_resize_event.borrow_mut() = Some((event, event_type))
3223 }
3224
3225 pub(crate) fn take_unhandled_resize_event(&self) -> Option<(ViewportDetails, WindowSizeType)> {
3226 self.unhandled_resize_event.borrow_mut().take()
3227 }
3228
3229 pub(crate) fn has_unhandled_resize_event(&self) -> bool {
3231 self.unhandled_resize_event.borrow().is_some()
3232 }
3233
3234 pub(crate) fn suspend(&self, cx: &mut js::context::JSContext) {
3235 self.as_global_scope().suspend();
3237
3238 if self.window_proxy().currently_active() == Some(self.global().pipeline_id()) {
3240 self.window_proxy().unset_currently_active(cx);
3241 }
3242
3243 self.gc();
3248 }
3249
3250 pub(crate) fn resume(&self, can_gc: CanGc) {
3251 self.as_global_scope().resume();
3253
3254 self.window_proxy().set_currently_active(self, can_gc);
3256
3257 self.Document().title_changed();
3260 }
3261
3262 pub(crate) fn need_emit_timeline_marker(&self, timeline_type: TimelineMarkerType) -> bool {
3263 let markers = self.devtools_markers.borrow();
3264 markers.contains(&timeline_type)
3265 }
3266
3267 pub(crate) fn emit_timeline_marker(&self, marker: TimelineMarker) {
3268 let sender = self.devtools_marker_sender.borrow();
3269 let sender = sender.as_ref().expect("There is no marker sender");
3270 sender.send(Some(marker)).unwrap();
3271 }
3272
3273 pub(crate) fn set_devtools_timeline_markers(
3274 &self,
3275 markers: Vec<TimelineMarkerType>,
3276 reply: GenericSender<Option<TimelineMarker>>,
3277 ) {
3278 *self.devtools_marker_sender.borrow_mut() = Some(reply);
3279 self.devtools_markers.borrow_mut().extend(markers);
3280 }
3281
3282 pub(crate) fn drop_devtools_timeline_markers(&self, markers: Vec<TimelineMarkerType>) {
3283 let mut devtools_markers = self.devtools_markers.borrow_mut();
3284 for marker in markers {
3285 devtools_markers.remove(&marker);
3286 }
3287 if devtools_markers.is_empty() {
3288 *self.devtools_marker_sender.borrow_mut() = None;
3289 }
3290 }
3291
3292 pub(crate) fn set_webdriver_script_chan(&self, chan: Option<GenericSender<WebDriverJSResult>>) {
3293 *self.webdriver_script_chan.borrow_mut() = chan;
3294 }
3295
3296 pub(crate) fn set_webdriver_load_status_sender(
3297 &self,
3298 sender: Option<GenericSender<WebDriverLoadStatus>>,
3299 ) {
3300 *self.webdriver_load_status_sender.borrow_mut() = sender;
3301 }
3302
3303 pub(crate) fn webdriver_load_status_sender(
3304 &self,
3305 ) -> Option<GenericSender<WebDriverLoadStatus>> {
3306 self.webdriver_load_status_sender.borrow().clone()
3307 }
3308
3309 pub(crate) fn is_alive(&self) -> bool {
3310 self.current_state.get() == WindowState::Alive
3311 }
3312
3313 pub(crate) fn is_top_level(&self) -> bool {
3315 self.parent_info.is_none()
3316 }
3317
3318 fn run_resize_steps_for_layout_viewport(&self, can_gc: CanGc) -> bool {
3323 let Some((new_size, size_type)) = self.take_unhandled_resize_event() else {
3324 return false;
3325 };
3326
3327 if self.viewport_details() == new_size {
3328 return false;
3329 }
3330
3331 let _realm = enter_realm(self);
3332 debug!(
3333 "Resizing Window for pipeline {:?} from {:?} to {new_size:?}",
3334 self.pipeline_id(),
3335 self.viewport_details(),
3336 );
3337 self.set_viewport_details(new_size);
3338
3339 self.Document()
3343 .add_restyle_reason(RestyleReason::ViewportChanged);
3344
3345 if self.layout().device().used_viewport_size() {
3348 self.Document().dirty_all_nodes();
3349 }
3350
3351 if size_type == WindowSizeType::Resize {
3353 let uievent = UIEvent::new(
3354 self,
3355 atom!("resize"),
3356 EventBubbles::DoesNotBubble,
3357 EventCancelable::NotCancelable,
3358 Some(self),
3359 0i32,
3360 0u32,
3361 can_gc,
3362 );
3363 uievent.upcast::<Event>().fire(self.upcast(), can_gc);
3364 }
3365
3366 true
3367 }
3368
3369 pub(crate) fn run_the_resize_steps(&self, can_gc: CanGc) -> bool {
3374 let layout_viewport_resized = self.run_resize_steps_for_layout_viewport(can_gc);
3375
3376 if self.has_changed_visual_viewport_dimension.get() {
3377 let visual_viewport = self.get_or_init_visual_viewport(can_gc);
3378
3379 let uievent = UIEvent::new(
3380 self,
3381 atom!("resize"),
3382 EventBubbles::DoesNotBubble,
3383 EventCancelable::NotCancelable,
3384 Some(self),
3385 0i32,
3386 0u32,
3387 can_gc,
3388 );
3389 uievent
3390 .upcast::<Event>()
3391 .fire(visual_viewport.upcast(), can_gc);
3392
3393 self.has_changed_visual_viewport_dimension.set(false);
3394 }
3395
3396 layout_viewport_resized
3397 }
3398
3399 pub(crate) fn evaluate_media_queries_and_report_changes(&self, can_gc: CanGc) {
3402 let _realm = enter_realm(self);
3403
3404 rooted_vec!(let mut mql_list);
3405 self.media_query_lists.for_each(|mql| {
3406 if let MediaQueryListMatchState::Changed = mql.evaluate_changes() {
3407 mql_list.push(Dom::from_ref(&*mql));
3409 }
3410 });
3411 for mql in mql_list.iter() {
3413 let event = MediaQueryListEvent::new(
3414 &mql.global(),
3415 atom!("change"),
3416 false,
3417 false,
3418 mql.Media(),
3419 mql.Matches(),
3420 can_gc,
3421 );
3422 event
3423 .upcast::<Event>()
3424 .fire(mql.upcast::<EventTarget>(), can_gc);
3425 }
3426 }
3427
3428 pub(crate) fn set_throttled(&self, throttled: bool) {
3430 self.throttled.set(throttled);
3431 if throttled {
3432 self.as_global_scope().slow_down_timers();
3433 } else {
3434 self.as_global_scope().speed_up_timers();
3435 }
3436 }
3437
3438 pub(crate) fn throttled(&self) -> bool {
3439 self.throttled.get()
3440 }
3441
3442 pub(crate) fn unminified_css_dir(&self) -> Option<String> {
3443 self.unminified_css_dir.borrow().clone()
3444 }
3445
3446 pub(crate) fn local_script_source(&self) -> &Option<String> {
3447 &self.local_script_source
3448 }
3449
3450 pub(crate) fn set_navigation_start(&self) {
3451 self.navigation_start.set(CrossProcessInstant::now());
3452 }
3453
3454 pub(crate) fn navigation_start(&self) -> CrossProcessInstant {
3455 self.navigation_start.get()
3456 }
3457
3458 pub(crate) fn set_last_activation_timestamp(&self, time: UserActivationTimestamp) {
3459 self.last_activation_timestamp.set(time);
3460 }
3461
3462 pub(crate) fn send_to_embedder(&self, msg: EmbedderMsg) {
3463 self.as_global_scope()
3464 .script_to_embedder_chan()
3465 .send(msg)
3466 .unwrap();
3467 }
3468
3469 pub(crate) fn send_to_constellation(&self, msg: ScriptToConstellationMessage) {
3470 self.as_global_scope()
3471 .script_to_constellation_chan()
3472 .send(msg)
3473 .unwrap();
3474 }
3475
3476 #[cfg(feature = "webxr")]
3477 pub(crate) fn in_immersive_xr_session(&self) -> bool {
3478 self.navigator
3479 .get()
3480 .as_ref()
3481 .and_then(|nav| nav.xr())
3482 .is_some_and(|xr| xr.pending_or_active_session())
3483 }
3484
3485 #[cfg(not(feature = "webxr"))]
3486 pub(crate) fn in_immersive_xr_session(&self) -> bool {
3487 false
3488 }
3489
3490 #[expect(unsafe_code)]
3491 fn handle_pending_images_post_reflow(
3492 &self,
3493 pending_images: Vec<PendingImage>,
3494 pending_rasterization_images: Vec<PendingRasterizationImage>,
3495 pending_svg_element_for_serialization: Vec<UntrustedNodeAddress>,
3496 ) {
3497 let pipeline_id = self.pipeline_id();
3498 for image in pending_images {
3499 let id = image.id;
3500 let node = unsafe { from_untrusted_node_address(image.node) };
3501
3502 if let PendingImageState::Unrequested(ref url) = image.state {
3503 fetch_image_for_layout(url.clone(), &node, id, self.image_cache.clone());
3504 }
3505
3506 let mut images = self.pending_layout_images.borrow_mut();
3507 if !images.contains_key(&id) {
3508 let trusted_node = Trusted::new(&*node);
3509 let sender = self.register_image_cache_listener(id, move |response, _| {
3510 trusted_node
3511 .root()
3512 .owner_window()
3513 .pending_layout_image_notification(response);
3514 });
3515
3516 self.image_cache
3517 .add_listener(ImageLoadListener::new(sender, pipeline_id, id));
3518 }
3519
3520 let nodes = images.entry(id).or_default();
3521 if !nodes.iter().any(|n| std::ptr::eq(&*(n.node), &*node)) {
3522 nodes.push(PendingLayoutImageAncillaryData {
3523 node: Dom::from_ref(&*node),
3524 destination: image.destination,
3525 });
3526 }
3527 }
3528
3529 for image in pending_rasterization_images {
3530 let node = unsafe { from_untrusted_node_address(image.node) };
3531
3532 let mut images = self.pending_images_for_rasterization.borrow_mut();
3533 if !images.contains_key(&(image.id, image.size)) {
3534 let image_cache_sender = self.image_cache_sender.clone();
3535 self.image_cache.add_rasterization_complete_listener(
3536 pipeline_id,
3537 image.id,
3538 image.size,
3539 Box::new(move |response| {
3540 let _ = image_cache_sender.send(response);
3541 }),
3542 );
3543 }
3544
3545 let nodes = images.entry((image.id, image.size)).or_default();
3546 if !nodes.iter().any(|n| std::ptr::eq(&**n, &*node)) {
3547 nodes.push(Dom::from_ref(&*node));
3548 }
3549 }
3550
3551 for node in pending_svg_element_for_serialization.into_iter() {
3552 let node = unsafe { from_untrusted_node_address(node) };
3553 let svg = node.downcast::<SVGSVGElement>().unwrap();
3554 svg.serialize_and_cache_subtree();
3555 node.dirty(NodeDamage::Other);
3556 }
3557 }
3558
3559 pub(crate) fn has_sticky_activation(&self) -> bool {
3561 UserActivationTimestamp::TimeStamp(CrossProcessInstant::now()) >=
3563 self.last_activation_timestamp.get()
3564 }
3565
3566 pub(crate) fn has_transient_activation(&self) -> bool {
3568 let current_time = CrossProcessInstant::now();
3571 UserActivationTimestamp::TimeStamp(current_time) >= self.last_activation_timestamp.get() &&
3572 UserActivationTimestamp::TimeStamp(current_time) <
3573 self.last_activation_timestamp.get() +
3574 pref!(dom_transient_activation_duration_ms)
3575 }
3576
3577 pub(crate) fn consume_last_activation_timestamp(&self) {
3578 if self.last_activation_timestamp.get() != UserActivationTimestamp::PositiveInfinity {
3579 self.set_last_activation_timestamp(UserActivationTimestamp::NegativeInfinity);
3580 }
3581 }
3582
3583 pub(crate) fn consume_user_activation(&self) {
3585 if self.undiscarded_window_proxy().is_none() {
3588 return;
3589 }
3590
3591 let Some(top_level_document) = self.top_level_document_if_local() else {
3595 return;
3596 };
3597
3598 top_level_document
3606 .window()
3607 .consume_last_activation_timestamp();
3608 for document in SameOriginDescendantNavigablesIterator::new(top_level_document) {
3609 document.window().consume_last_activation_timestamp();
3610 }
3611 }
3612
3613 #[allow(clippy::too_many_arguments)]
3614 pub(crate) fn new(
3615 cx: &mut js::context::JSContext,
3616 webview_id: WebViewId,
3617 runtime: Rc<Runtime>,
3618 script_chan: Sender<MainThreadScriptMsg>,
3619 layout: Box<dyn Layout>,
3620 font_context: Arc<FontContext>,
3621 image_cache_sender: Sender<ImageCacheResponseMessage>,
3622 image_cache: Arc<dyn ImageCache>,
3623 resource_threads: ResourceThreads,
3624 storage_threads: StorageThreads,
3625 #[cfg(feature = "bluetooth")] bluetooth_thread: GenericSender<BluetoothRequest>,
3626 mem_profiler_chan: MemProfilerChan,
3627 time_profiler_chan: TimeProfilerChan,
3628 devtools_chan: Option<GenericCallback<ScriptToDevtoolsControlMsg>>,
3629 constellation_chan: ScriptToConstellationChan,
3630 embedder_chan: ScriptToEmbedderChan,
3631 control_chan: GenericSender<ScriptThreadMessage>,
3632 pipeline_id: PipelineId,
3633 parent_info: Option<PipelineId>,
3634 viewport_details: ViewportDetails,
3635 origin: MutableOrigin,
3636 creation_url: ServoUrl,
3637 top_level_creation_url: ServoUrl,
3638 navigation_start: CrossProcessInstant,
3639 webgl_chan: Option<WebGLChan>,
3640 #[cfg(feature = "webxr")] webxr_registry: Option<webxr_api::Registry>,
3641 paint_api: CrossProcessPaintApi,
3642 unminify_js: bool,
3643 unminify_css: bool,
3644 local_script_source: Option<String>,
3645 user_scripts: Rc<Vec<UserScript>>,
3646 player_context: WindowGLContext,
3647 #[cfg(feature = "webgpu")] gpu_id_hub: Arc<IdentityHub>,
3648 inherited_secure_context: Option<bool>,
3649 theme: Theme,
3650 weak_script_thread: Weak<ScriptThread>,
3651 ) -> DomRoot<Self> {
3652 let error_reporter = CSSErrorReporter {
3653 pipelineid: pipeline_id,
3654 script_chan: control_chan,
3655 };
3656
3657 let win = Box::new(Self {
3658 webview_id,
3659 globalscope: GlobalScope::new_inherited(
3660 pipeline_id,
3661 devtools_chan,
3662 mem_profiler_chan,
3663 time_profiler_chan,
3664 constellation_chan,
3665 embedder_chan,
3666 resource_threads,
3667 storage_threads,
3668 origin,
3669 creation_url,
3670 Some(top_level_creation_url),
3671 #[cfg(feature = "webgpu")]
3672 gpu_id_hub,
3673 inherited_secure_context,
3674 unminify_js,
3675 Some(font_context),
3676 ),
3677 ongoing_navigation: Default::default(),
3678 script_chan,
3679 layout: RefCell::new(layout),
3680 image_cache_sender,
3681 image_cache,
3682 navigator: Default::default(),
3683 location: Default::default(),
3684 history: Default::default(),
3685 custom_element_registry: Default::default(),
3686 window_proxy: Default::default(),
3687 document: Default::default(),
3688 performance: Default::default(),
3689 navigation_start: Cell::new(navigation_start),
3690 screen: Default::default(),
3691 session_storage: Default::default(),
3692 local_storage: Default::default(),
3693 status: DomRefCell::new(DOMString::new()),
3694 parent_info,
3695 dom_static: GlobalStaticData::new(),
3696 js_runtime: DomRefCell::new(Some(runtime)),
3697 #[cfg(feature = "bluetooth")]
3698 bluetooth_thread,
3699 #[cfg(feature = "bluetooth")]
3700 bluetooth_extra_permission_data: BluetoothExtraPermissionData::new(),
3701 unhandled_resize_event: Default::default(),
3702 viewport_details: Cell::new(viewport_details),
3703 layout_blocker: Cell::new(LayoutBlocker::WaitingForParse),
3704 current_state: Cell::new(WindowState::Alive),
3705 devtools_marker_sender: Default::default(),
3706 devtools_markers: Default::default(),
3707 webdriver_script_chan: Default::default(),
3708 webdriver_load_status_sender: Default::default(),
3709 error_reporter,
3710 media_query_lists: DOMTracker::new(),
3711 #[cfg(feature = "bluetooth")]
3712 test_runner: Default::default(),
3713 webgl_chan,
3714 #[cfg(feature = "webxr")]
3715 webxr_registry,
3716 pending_image_callbacks: Default::default(),
3717 pending_layout_images: Default::default(),
3718 pending_images_for_rasterization: Default::default(),
3719 unminified_css_dir: DomRefCell::new(if unminify_css {
3720 Some(unminified_path("unminified-css"))
3721 } else {
3722 None
3723 }),
3724 local_script_source,
3725 test_worklet: Default::default(),
3726 paint_worklet: Default::default(),
3727 exists_mut_observer: Cell::new(false),
3728 paint_api,
3729 has_sent_idle_message: Cell::new(false),
3730 user_scripts,
3731 player_context,
3732 throttled: Cell::new(false),
3733 layout_marker: DomRefCell::new(Rc::new(Cell::new(true))),
3734 current_event: DomRefCell::new(None),
3735 theme: Cell::new(theme),
3736 trusted_types: Default::default(),
3737 reporting_observer_list: Default::default(),
3738 report_list: Default::default(),
3739 endpoints_list: Default::default(),
3740 script_window_proxies: ScriptThread::window_proxies(),
3741 has_pending_screenshot_readiness_request: Default::default(),
3742 visual_viewport: Default::default(),
3743 weak_script_thread,
3744 has_changed_visual_viewport_dimension: Default::default(),
3745 last_activation_timestamp: Cell::new(UserActivationTimestamp::PositiveInfinity),
3746 });
3747
3748 WindowBinding::Wrap::<crate::DomTypeHolder>(cx, win)
3749 }
3750
3751 pub(crate) fn pipeline_id(&self) -> PipelineId {
3752 self.as_global_scope().pipeline_id()
3753 }
3754
3755 pub(crate) fn cache_layout_value<T>(&self, value: T) -> LayoutValue<T>
3757 where
3758 T: Copy + MallocSizeOf,
3759 {
3760 LayoutValue::new(self.layout_marker.borrow().clone(), value)
3761 }
3762}
3763
3764#[derive(MallocSizeOf)]
3769pub(crate) struct LayoutValue<T: MallocSizeOf> {
3770 #[conditional_malloc_size_of]
3771 is_valid: Rc<Cell<bool>>,
3772 value: T,
3773}
3774
3775#[expect(unsafe_code)]
3776unsafe impl<T: JSTraceable + MallocSizeOf> JSTraceable for LayoutValue<T> {
3777 unsafe fn trace(&self, trc: *mut js::jsapi::JSTracer) {
3778 unsafe { self.value.trace(trc) };
3779 }
3780}
3781
3782impl<T: Copy + MallocSizeOf> LayoutValue<T> {
3783 fn new(marker: Rc<Cell<bool>>, value: T) -> Self {
3784 LayoutValue {
3785 is_valid: marker,
3786 value,
3787 }
3788 }
3789
3790 pub(crate) fn get(&self) -> Result<T, ()> {
3792 if self.is_valid.get() {
3793 return Ok(self.value);
3794 }
3795 Err(())
3796 }
3797}
3798
3799fn should_move_clip_rect(clip_rect: UntypedRect<Au>, new_viewport: UntypedRect<f32>) -> bool {
3800 let clip_rect = UntypedRect::new(
3801 Point2D::new(
3802 clip_rect.origin.x.to_f32_px(),
3803 clip_rect.origin.y.to_f32_px(),
3804 ),
3805 Size2D::new(
3806 clip_rect.size.width.to_f32_px(),
3807 clip_rect.size.height.to_f32_px(),
3808 ),
3809 );
3810
3811 static VIEWPORT_SCROLL_MARGIN_SIZE: f32 = 0.5;
3815 let viewport_scroll_margin = new_viewport.size * VIEWPORT_SCROLL_MARGIN_SIZE;
3816
3817 (clip_rect.origin.x - new_viewport.origin.x).abs() <= viewport_scroll_margin.width ||
3818 (clip_rect.max_x() - new_viewport.max_x()).abs() <= viewport_scroll_margin.width ||
3819 (clip_rect.origin.y - new_viewport.origin.y).abs() <= viewport_scroll_margin.height ||
3820 (clip_rect.max_y() - new_viewport.max_y()).abs() <= viewport_scroll_margin.height
3821}
3822
3823impl Window {
3824 pub(crate) fn post_message(
3826 &self,
3827 target_origin: Option<ImmutableOrigin>,
3828 source_origin: ImmutableOrigin,
3829 source: &WindowProxy,
3830 data: StructuredSerializedData,
3831 ) {
3832 let this = Trusted::new(self);
3833 let source = Trusted::new(source);
3834 let task = task!(post_serialised_message: move || {
3835 let this = this.root();
3836 let source = source.root();
3837 let document = this.Document();
3838
3839 if let Some(ref target_origin) = target_origin {
3841 if !target_origin.same_origin(&*document.origin()) {
3842 return;
3843 }
3844 }
3845
3846 let cx = this.get_cx();
3848 let obj = this.reflector().get_jsobject();
3849 let _ac = JSAutoRealm::new(*cx, obj.get());
3850 rooted!(in(*cx) let mut message_clone = UndefinedValue());
3851 if let Ok(ports) = structuredclone::read(this.upcast(), data, message_clone.handle_mut(), CanGc::note()) {
3852 MessageEvent::dispatch_jsval(
3854 this.upcast(),
3855 this.upcast(),
3856 message_clone.handle(),
3857 Some(&source_origin.ascii_serialization()),
3858 Some(&*source),
3859 ports,
3860 CanGc::note()
3861 );
3862 } else {
3863 MessageEvent::dispatch_error(
3865 this.upcast(),
3866 this.upcast(),
3867 CanGc::note()
3868 );
3869 }
3870 });
3871 self.as_global_scope()
3873 .task_manager()
3874 .dom_manipulation_task_source()
3875 .queue(task);
3876 }
3877}
3878
3879#[derive(Clone, MallocSizeOf)]
3880pub(crate) struct CSSErrorReporter {
3881 pub(crate) pipelineid: PipelineId,
3882 pub(crate) script_chan: GenericSender<ScriptThreadMessage>,
3883}
3884unsafe_no_jsmanaged_fields!(CSSErrorReporter);
3885
3886impl ParseErrorReporter for CSSErrorReporter {
3887 fn report_error(
3888 &self,
3889 url: &UrlExtraData,
3890 location: SourceLocation,
3891 error: ContextualParseError,
3892 ) {
3893 if log_enabled!(log::Level::Info) {
3894 info!(
3895 "Url:\t{}\n{}:{} {}",
3896 url.0.as_str(),
3897 location.line,
3898 location.column,
3899 error
3900 )
3901 }
3902
3903 let _ = self.script_chan.send(ScriptThreadMessage::ReportCSSError(
3905 self.pipelineid,
3906 url.0.to_string(),
3907 location.line,
3908 location.column,
3909 error.to_string(),
3910 ));
3911 }
3912}
3913
3914fn is_named_element_with_name_attribute(elem: &Element) -> bool {
3915 let type_ = match elem.upcast::<Node>().type_id() {
3916 NodeTypeId::Element(ElementTypeId::HTMLElement(type_)) => type_,
3917 _ => return false,
3918 };
3919 matches!(
3920 type_,
3921 HTMLElementTypeId::HTMLEmbedElement |
3922 HTMLElementTypeId::HTMLFormElement |
3923 HTMLElementTypeId::HTMLImageElement |
3924 HTMLElementTypeId::HTMLObjectElement
3925 )
3926}
3927
3928fn is_named_element_with_id_attribute(elem: &Element) -> bool {
3929 elem.is_html_element()
3930}
3931
3932#[expect(unsafe_code)]
3933#[unsafe(no_mangle)]
3934unsafe extern "C" fn dump_js_stack(cx: *mut RawJSContext) {
3936 unsafe {
3937 DumpJSStack(cx, true, false, false);
3938 }
3939}
3940
3941impl WindowHelpers for Window {
3942 fn create_named_properties_object(
3943 cx: SafeJSContext,
3944 proto: HandleObject,
3945 object: MutableHandleObject,
3946 ) {
3947 Self::create_named_properties_object(cx, proto, object)
3948 }
3949}