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 base::cross_process_instant::CrossProcessInstant;
19use base::generic_channel::{self, GenericCallback, GenericSender};
20use base::id::{BrowsingContextId, PipelineId, WebViewId};
21use base64::Engine;
22#[cfg(feature = "bluetooth")]
23use bluetooth_traits::BluetoothRequest;
24use canvas_traits::webgl::WebGLChan;
25use constellation_traits::{
26 LoadData, LoadOrigin, NavigationHistoryBehavior, ScreenshotReadinessResponse,
27 ScriptToConstellationChan, ScriptToConstellationMessage, StructuredSerializedData,
28 WindowSizeType,
29};
30use content_security_policy::Violation;
31use content_security_policy::sandboxing_directive::SandboxingFlagSet;
32use crossbeam_channel::{Sender, unbounded};
33use cssparser::SourceLocation;
34use devtools_traits::{ScriptToDevtoolsControlMsg, TimelineMarker, TimelineMarkerType};
35use dom_struct::dom_struct;
36use embedder_traits::user_contents::UserScript;
37use embedder_traits::{
38 AlertResponse, ConfirmResponse, EmbedderMsg, JavaScriptEvaluationError, PromptResponse,
39 ScriptToEmbedderChan, SimpleDialogRequest, Theme, UntrustedNodeAddress, ViewportDetails,
40 WebDriverJSResult, WebDriverLoadStatus,
41};
42use euclid::default::Rect as UntypedRect;
43use euclid::{Point2D, Rect, Scale, Size2D, Vector2D};
44use fonts::{CspViolationHandler, FontContext, NetworkTimingHandler, WebFontDocumentContext};
45use js::context::JSContext;
46use js::glue::DumpJSStack;
47use js::jsapi::{
48 GCReason, Heap, JS_GC, JSAutoRealm, JSContext as RawJSContext, JSObject, JSPROP_ENUMERATE,
49};
50use js::jsval::{NullValue, UndefinedValue};
51use js::realm::CurrentRealm;
52use js::rust::wrappers::JS_DefineProperty;
53use js::rust::{
54 CustomAutoRooter, CustomAutoRooterGuard, HandleObject, HandleValue, MutableHandleObject,
55 MutableHandleValue,
56};
57use layout_api::{
58 AxesOverflow, BoxAreaType, CSSPixelRectIterator, ElementsFromPointFlags,
59 ElementsFromPointResult, FragmentType, Layout, LayoutImageDestination, PendingImage,
60 PendingImageState, PendingRasterizationImage, PhysicalSides, QueryMsg, ReflowGoal,
61 ReflowPhasesRun, ReflowRequest, ReflowRequestRestyle, ReflowStatistics, RestyleReason,
62 ScrollContainerQueryFlags, ScrollContainerResponse, TrustedNodeAddress,
63 combine_id_with_fragment_type,
64};
65use malloc_size_of::MallocSizeOf;
66use media::WindowGLContext;
67use net_traits::image_cache::{
68 ImageCache, ImageCacheResponseCallback, ImageCacheResponseMessage, ImageLoadListener,
69 ImageResponse, PendingImageId, PendingImageResponse, RasterizationCompleteResponse,
70};
71use net_traits::request::Referrer;
72use net_traits::{ResourceFetchTiming, ResourceThreads};
73use num_traits::ToPrimitive;
74use paint_api::{CrossProcessPaintApi, PinchZoomInfos};
75use profile_traits::generic_channel as ProfiledGenericChannel;
76use profile_traits::mem::ProfilerChan as MemProfilerChan;
77use profile_traits::time::ProfilerChan as TimeProfilerChan;
78use rustc_hash::{FxBuildHasher, FxHashMap};
79use script_bindings::codegen::GenericBindings::WindowBinding::ScrollToOptions;
80use script_bindings::conversions::SafeToJSValConvertible;
81use script_bindings::interfaces::WindowHelpers;
82use script_bindings::root::Root;
83use script_traits::{ConstellationInputEvent, ScriptThreadMessage};
84use selectors::attr::CaseSensitivity;
85use servo_arc::Arc as ServoArc;
86use servo_config::pref;
87use servo_geometry::DeviceIndependentIntRect;
88use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
89use storage_traits::StorageThreads;
90use storage_traits::webstorage_thread::WebStorageType;
91use style::error_reporting::{ContextualParseError, ParseErrorReporter};
92use style::properties::PropertyId;
93use style::properties::style_structs::Font;
94use style::selector_parser::PseudoElement;
95use style::str::HTML_SPACE_CHARACTERS;
96use style::stylesheets::UrlExtraData;
97use style_traits::CSSPixel;
98use stylo_atoms::Atom;
99use url::Position;
100use webrender_api::ExternalScrollId;
101use webrender_api::units::{DeviceIntSize, DevicePixel, LayoutPixel, LayoutPoint};
102
103use super::bindings::codegen::Bindings::MessagePortBinding::StructuredSerializeOptions;
104use super::bindings::trace::HashMapTracedValues;
105use super::performanceresourcetiming::InitiatorType;
106use super::types::SVGSVGElement;
107use crate::dom::bindings::cell::{DomRefCell, Ref};
108use crate::dom::bindings::codegen::Bindings::DocumentBinding::{
109 DocumentMethods, DocumentReadyState, NamedPropertyValue,
110};
111use crate::dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElementMethods;
112use crate::dom::bindings::codegen::Bindings::HistoryBinding::History_Binding::HistoryMethods;
113use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
114 ImageBitmapOptions, ImageBitmapSource,
115};
116use crate::dom::bindings::codegen::Bindings::MediaQueryListBinding::MediaQueryList_Binding::MediaQueryListMethods;
117use crate::dom::bindings::codegen::Bindings::ReportingObserverBinding::Report;
118use crate::dom::bindings::codegen::Bindings::RequestBinding::{RequestInfo, RequestInit};
119use crate::dom::bindings::codegen::Bindings::VoidFunctionBinding::VoidFunction;
120use crate::dom::bindings::codegen::Bindings::WindowBinding::{
121 self, DeferredRequestInit, FrameRequestCallback, ScrollBehavior, WindowMethods,
122 WindowPostMessageOptions,
123};
124use crate::dom::bindings::codegen::UnionTypes::{
125 RequestOrUSVString, TrustedScriptOrString, TrustedScriptOrStringOrFunction,
126};
127use crate::dom::bindings::error::{
128 Error, ErrorInfo, ErrorResult, Fallible, javascript_error_info_from_error_info,
129};
130use crate::dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementTypeId, NodeTypeId};
131use crate::dom::bindings::num::Finite;
132use crate::dom::bindings::refcounted::Trusted;
133use crate::dom::bindings::reflector::{DomGlobal, DomObject};
134use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
135use crate::dom::bindings::str::{DOMString, USVString};
136use crate::dom::bindings::structuredclone;
137use crate::dom::bindings::trace::{CustomTraceable, JSTraceable, RootedTraceableBox};
138use crate::dom::bindings::utils::GlobalStaticData;
139use crate::dom::bindings::weakref::DOMTracker;
140#[cfg(feature = "bluetooth")]
141use crate::dom::bluetooth::BluetoothExtraPermissionData;
142use crate::dom::cookiestore::CookieStore;
143use crate::dom::crypto::Crypto;
144use crate::dom::csp::GlobalCspReporting;
145use crate::dom::css::cssstyledeclaration::{
146 CSSModificationAccess, CSSStyleDeclaration, CSSStyleOwner,
147};
148use crate::dom::customelementregistry::CustomElementRegistry;
149use crate::dom::document::{
150 AnimationFrameCallback, Document, SameOriginDescendantNavigablesIterator,
151};
152use crate::dom::element::Element;
153use crate::dom::event::{Event, EventBubbles, EventCancelable};
154use crate::dom::eventtarget::EventTarget;
155use crate::dom::fetchlaterresult::FetchLaterResult;
156use crate::dom::globalscope::GlobalScope;
157use crate::dom::history::History;
158use crate::dom::html::htmlcollection::{CollectionFilter, HTMLCollection};
159use crate::dom::html::htmliframeelement::HTMLIFrameElement;
160use crate::dom::idbfactory::IDBFactory;
161use crate::dom::inputevent::HitTestResult;
162use crate::dom::location::Location;
163use crate::dom::medialist::MediaList;
164use crate::dom::mediaquerylist::{MediaQueryList, MediaQueryListMatchState};
165use crate::dom::mediaquerylistevent::MediaQueryListEvent;
166use crate::dom::messageevent::MessageEvent;
167use crate::dom::navigator::Navigator;
168use crate::dom::node::{Node, NodeDamage, NodeTraits, from_untrusted_node_address};
169use crate::dom::performance::performance::Performance;
170use crate::dom::promise::Promise;
171use crate::dom::reporting::reportingendpoint::{ReportingEndpoint, SendReportsToEndpoints};
172use crate::dom::reporting::reportingobserver::ReportingObserver;
173use crate::dom::screen::Screen;
174use crate::dom::scrolling_box::{ScrollingBox, ScrollingBoxSource};
175use crate::dom::selection::Selection;
176use crate::dom::shadowroot::ShadowRoot;
177use crate::dom::storage::Storage;
178#[cfg(feature = "bluetooth")]
179use crate::dom::testrunner::TestRunner;
180use crate::dom::trustedtypes::trustedtypepolicyfactory::TrustedTypePolicyFactory;
181use crate::dom::types::{ImageBitmap, MouseEvent, UIEvent};
182use crate::dom::useractivation::UserActivationTimestamp;
183use crate::dom::visualviewport::{VisualViewport, VisualViewportChanges};
184use crate::dom::webgl::webglrenderingcontext::WebGLCommandSender;
185#[cfg(feature = "webgpu")]
186use crate::dom::webgpu::identityhub::IdentityHub;
187use crate::dom::windowproxy::{WindowProxy, WindowProxyHandler};
188use crate::dom::worklet::Worklet;
189use crate::dom::workletglobalscope::WorkletGlobalScopeType;
190use crate::layout_image::fetch_image_for_layout;
191use crate::messaging::{MainThreadScriptMsg, ScriptEventLoopReceiver, ScriptEventLoopSender};
192use crate::microtask::{Microtask, UserMicrotask};
193use crate::network_listener::{ResourceTimingListener, submit_timing};
194use crate::realms::enter_realm;
195use crate::script_runtime::{CanGc, JSContext as SafeJSContext, Runtime};
196use crate::script_thread::ScriptThread;
197use crate::script_window_proxies::ScriptWindowProxies;
198use crate::task_source::SendableTaskSource;
199use crate::timers::{IsInterval, TimerCallback};
200use crate::unminify::unminified_path;
201use crate::webdriver_handlers::{find_node_by_unique_id_in_document, jsval_to_webdriver};
202use crate::{fetch, window_named_properties};
203
204#[derive(MallocSizeOf)]
209pub struct PendingImageCallback(
210 #[ignore_malloc_size_of = "dyn Fn is currently impossible to measure"]
211 #[expect(clippy::type_complexity)]
212 Box<dyn Fn(PendingImageResponse, &mut js::context::JSContext) + 'static>,
213);
214
215#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf, PartialEq)]
217enum WindowState {
218 Alive,
219 Zombie, }
221
222const INITIAL_REFLOW_DELAY: Duration = Duration::from_millis(200);
225
226#[derive(Clone, Copy, MallocSizeOf)]
237enum LayoutBlocker {
238 WaitingForParse,
240 Parsing(Instant),
242 FiredLoadEventOrParsingTimerExpired,
246}
247
248impl LayoutBlocker {
249 fn layout_blocked(&self) -> bool {
250 !matches!(self, Self::FiredLoadEventOrParsingTimerExpired)
251 }
252}
253
254#[derive(Clone, Copy, Debug, Default, JSTraceable, MallocSizeOf, PartialEq)]
257pub(crate) struct OngoingNavigation(u32);
258
259type PendingImageRasterizationKey = (PendingImageId, DeviceIntSize);
260
261#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
265#[derive(JSTraceable, MallocSizeOf)]
266struct PendingLayoutImageAncillaryData {
267 node: Dom<Node>,
268 #[no_trace]
269 destination: LayoutImageDestination,
270}
271
272#[dom_struct]
273pub(crate) struct Window {
274 globalscope: GlobalScope,
275
276 #[ignore_malloc_size_of = "Weak does not need to be accounted"]
280 #[no_trace]
281 weak_script_thread: Weak<ScriptThread>,
282
283 #[no_trace]
287 webview_id: WebViewId,
288 script_chan: Sender<MainThreadScriptMsg>,
289 #[no_trace]
290 #[ignore_malloc_size_of = "TODO: Add MallocSizeOf support to layout"]
291 layout: RefCell<Box<dyn Layout>>,
292 navigator: MutNullableDom<Navigator>,
293 #[ignore_malloc_size_of = "ImageCache"]
294 #[no_trace]
295 image_cache: Arc<dyn ImageCache>,
296 #[no_trace]
297 image_cache_sender: Sender<ImageCacheResponseMessage>,
298 window_proxy: MutNullableDom<WindowProxy>,
299 document: MutNullableDom<Document>,
300 location: MutNullableDom<Location>,
301 history: MutNullableDom<History>,
302 custom_element_registry: MutNullableDom<CustomElementRegistry>,
303 performance: MutNullableDom<Performance>,
304 #[no_trace]
305 navigation_start: Cell<CrossProcessInstant>,
306 screen: MutNullableDom<Screen>,
307 session_storage: MutNullableDom<Storage>,
308 local_storage: MutNullableDom<Storage>,
309 status: DomRefCell<DOMString>,
310 trusted_types: MutNullableDom<TrustedTypePolicyFactory>,
311
312 ongoing_navigation: Cell<OngoingNavigation>,
315
316 #[no_trace]
319 devtools_markers: DomRefCell<HashSet<TimelineMarkerType>>,
320 #[no_trace]
321 devtools_marker_sender: DomRefCell<Option<GenericSender<Option<TimelineMarker>>>>,
322
323 #[no_trace]
325 unhandled_resize_event: DomRefCell<Option<(ViewportDetails, WindowSizeType)>>,
326
327 #[no_trace]
329 theme: Cell<Theme>,
330
331 #[no_trace]
333 parent_info: Option<PipelineId>,
334
335 dom_static: GlobalStaticData,
337
338 #[conditional_malloc_size_of]
340 js_runtime: DomRefCell<Option<Rc<Runtime>>>,
341
342 #[no_trace]
344 viewport_details: Cell<ViewportDetails>,
345
346 #[no_trace]
348 #[cfg(feature = "bluetooth")]
349 bluetooth_thread: GenericSender<BluetoothRequest>,
350
351 #[cfg(feature = "bluetooth")]
352 bluetooth_extra_permission_data: BluetoothExtraPermissionData,
353
354 #[no_trace]
358 layout_blocker: Cell<LayoutBlocker>,
359
360 #[no_trace]
362 webdriver_script_chan: DomRefCell<Option<GenericSender<WebDriverJSResult>>>,
363
364 #[no_trace]
366 webdriver_load_status_sender: RefCell<Option<GenericSender<WebDriverLoadStatus>>>,
367
368 current_state: Cell<WindowState>,
370
371 error_reporter: CSSErrorReporter,
372
373 media_query_lists: DOMTracker<MediaQueryList>,
375
376 #[cfg(feature = "bluetooth")]
377 test_runner: MutNullableDom<TestRunner>,
378
379 #[ignore_malloc_size_of = "channels are hard"]
381 #[no_trace]
382 webgl_chan: Option<WebGLChan>,
383
384 #[ignore_malloc_size_of = "defined in webxr"]
385 #[no_trace]
386 #[cfg(feature = "webxr")]
387 webxr_registry: Option<webxr_api::Registry>,
388
389 #[no_trace]
393 pending_image_callbacks: DomRefCell<FxHashMap<PendingImageId, Vec<PendingImageCallback>>>,
394
395 pending_layout_images: DomRefCell<
400 HashMapTracedValues<PendingImageId, Vec<PendingLayoutImageAncillaryData>, FxBuildHasher>,
401 >,
402
403 pending_images_for_rasterization: DomRefCell<
407 HashMapTracedValues<PendingImageRasterizationKey, Vec<Dom<Node>>, FxBuildHasher>,
408 >,
409
410 unminified_css_dir: DomRefCell<Option<String>>,
413
414 local_script_source: Option<String>,
416
417 test_worklet: MutNullableDom<Worklet>,
419 paint_worklet: MutNullableDom<Worklet>,
421
422 exists_mut_observer: Cell<bool>,
424
425 #[no_trace]
427 paint_api: CrossProcessPaintApi,
428
429 has_sent_idle_message: Cell<bool>,
432
433 unminify_css: bool,
435
436 #[no_trace]
439 #[conditional_malloc_size_of]
440 user_scripts: Rc<Vec<UserScript>>,
441
442 #[ignore_malloc_size_of = "defined in script_thread"]
444 #[no_trace]
445 player_context: WindowGLContext,
446
447 throttled: Cell<bool>,
448
449 #[conditional_malloc_size_of]
453 layout_marker: DomRefCell<Rc<Cell<bool>>>,
454
455 current_event: DomRefCell<Option<Dom<Event>>>,
457
458 reporting_observer_list: DomRefCell<Vec<DomRoot<ReportingObserver>>>,
460
461 report_list: DomRefCell<Vec<Report>>,
463
464 #[no_trace]
466 endpoints_list: DomRefCell<Vec<ReportingEndpoint>>,
467
468 #[conditional_malloc_size_of]
470 script_window_proxies: Rc<ScriptWindowProxies>,
471
472 has_pending_screenshot_readiness_request: Cell<bool>,
474
475 visual_viewport: MutNullableDom<VisualViewport>,
478
479 has_changed_visual_viewport_dimension: Cell<bool>,
481
482 #[no_trace]
484 last_activation_timestamp: Cell<UserActivationTimestamp>,
485}
486
487impl Window {
488 pub(crate) fn script_thread(&self) -> Rc<ScriptThread> {
489 Weak::upgrade(&self.weak_script_thread)
490 .expect("Weak reference should always be upgradable when a ScriptThread is running")
491 }
492
493 pub(crate) fn webview_id(&self) -> WebViewId {
494 self.webview_id
495 }
496
497 pub(crate) fn as_global_scope(&self) -> &GlobalScope {
498 self.upcast::<GlobalScope>()
499 }
500
501 pub(crate) fn layout(&self) -> Ref<'_, Box<dyn Layout>> {
502 self.layout.borrow()
503 }
504
505 pub(crate) fn layout_mut(&self) -> RefMut<'_, Box<dyn Layout>> {
506 self.layout.borrow_mut()
507 }
508
509 pub(crate) fn get_exists_mut_observer(&self) -> bool {
510 self.exists_mut_observer.get()
511 }
512
513 pub(crate) fn set_exists_mut_observer(&self) {
514 self.exists_mut_observer.set(true);
515 }
516
517 #[expect(unsafe_code)]
518 pub(crate) fn clear_js_runtime_for_script_deallocation(&self) {
519 self.as_global_scope()
520 .remove_web_messaging_and_dedicated_workers_infra();
521 unsafe {
522 *self.js_runtime.borrow_for_script_deallocation() = None;
523 self.window_proxy.set(None);
524 self.current_state.set(WindowState::Zombie);
525 self.as_global_scope()
526 .task_manager()
527 .cancel_all_tasks_and_ignore_future_tasks();
528 }
529 }
530
531 pub(crate) fn discard_browsing_context(&self) {
534 let proxy = match self.window_proxy.get() {
535 Some(proxy) => proxy,
536 None => panic!("Discarding a BC from a window that has none"),
537 };
538 proxy.discard_browsing_context();
539 self.as_global_scope()
543 .task_manager()
544 .cancel_all_tasks_and_ignore_future_tasks();
545 }
546
547 pub(crate) fn time_profiler_chan(&self) -> &TimeProfilerChan {
549 self.globalscope.time_profiler_chan()
550 }
551
552 pub(crate) fn origin(&self) -> &MutableOrigin {
553 self.globalscope.origin()
554 }
555
556 #[expect(unsafe_code)]
557 pub(crate) fn get_cx(&self) -> SafeJSContext {
558 unsafe { SafeJSContext::from_ptr(js::rust::Runtime::get().unwrap().as_ptr()) }
559 }
560
561 pub(crate) fn get_js_runtime(&self) -> Ref<'_, Option<Rc<Runtime>>> {
562 self.js_runtime.borrow()
563 }
564
565 pub(crate) fn main_thread_script_chan(&self) -> &Sender<MainThreadScriptMsg> {
566 &self.script_chan
567 }
568
569 pub(crate) fn parent_info(&self) -> Option<PipelineId> {
570 self.parent_info
571 }
572
573 pub(crate) fn new_script_pair(&self) -> (ScriptEventLoopSender, ScriptEventLoopReceiver) {
574 let (sender, receiver) = unbounded();
575 (
576 ScriptEventLoopSender::MainThread(sender),
577 ScriptEventLoopReceiver::MainThread(receiver),
578 )
579 }
580
581 pub(crate) fn event_loop_sender(&self) -> ScriptEventLoopSender {
582 ScriptEventLoopSender::MainThread(self.script_chan.clone())
583 }
584
585 pub(crate) fn image_cache(&self) -> Arc<dyn ImageCache> {
586 self.image_cache.clone()
587 }
588
589 pub(crate) fn window_proxy(&self) -> DomRoot<WindowProxy> {
591 self.window_proxy.get().unwrap()
592 }
593
594 pub(crate) fn append_reporting_observer(&self, reporting_observer: DomRoot<ReportingObserver>) {
595 self.reporting_observer_list
596 .borrow_mut()
597 .push(reporting_observer);
598 }
599
600 pub(crate) fn remove_reporting_observer(&self, reporting_observer: &ReportingObserver) {
601 let index = {
602 let list = self.reporting_observer_list.borrow();
603 list.iter()
604 .position(|observer| &**observer == reporting_observer)
605 };
606
607 if let Some(index) = index {
608 self.reporting_observer_list.borrow_mut().remove(index);
609 }
610 }
611
612 pub(crate) fn registered_reporting_observers(&self) -> Vec<DomRoot<ReportingObserver>> {
613 self.reporting_observer_list.borrow().clone()
614 }
615
616 pub(crate) fn append_report(&self, report: Report) {
617 self.report_list.borrow_mut().push(report);
618 let trusted_window = Trusted::new(self);
619 self.upcast::<GlobalScope>()
620 .task_manager()
621 .dom_manipulation_task_source()
622 .queue(task!(send_to_reporting_endpoints: move || {
623 let window = trusted_window.root();
624 let reports = std::mem::take(&mut *window.report_list.borrow_mut());
625 window.upcast::<GlobalScope>().send_reports_to_endpoints(
626 reports,
627 window.endpoints_list.borrow().clone(),
628 );
629 }));
630 }
631
632 pub(crate) fn buffered_reports(&self) -> Vec<Report> {
633 self.report_list.borrow().clone()
634 }
635
636 pub(crate) fn set_endpoints_list(&self, endpoints: Vec<ReportingEndpoint>) {
637 *self.endpoints_list.borrow_mut() = endpoints;
638 }
639
640 pub(crate) fn undiscarded_window_proxy(&self) -> Option<DomRoot<WindowProxy>> {
643 self.window_proxy.get().and_then(|window_proxy| {
644 if window_proxy.is_browsing_context_discarded() {
645 None
646 } else {
647 Some(window_proxy)
648 }
649 })
650 }
651
652 pub(crate) fn top_level_document_if_local(&self) -> Option<DomRoot<Document>> {
657 if self.is_top_level() {
658 return Some(self.Document());
659 }
660
661 let window_proxy = self.undiscarded_window_proxy()?;
662 self.script_window_proxies
663 .find_window_proxy(window_proxy.webview_id().into())?
664 .document()
665 }
666
667 #[cfg(feature = "bluetooth")]
668 pub(crate) fn bluetooth_thread(&self) -> GenericSender<BluetoothRequest> {
669 self.bluetooth_thread.clone()
670 }
671
672 #[cfg(feature = "bluetooth")]
673 pub(crate) fn bluetooth_extra_permission_data(&self) -> &BluetoothExtraPermissionData {
674 &self.bluetooth_extra_permission_data
675 }
676
677 pub(crate) fn css_error_reporter(&self) -> &CSSErrorReporter {
678 &self.error_reporter
679 }
680
681 pub(crate) fn webgl_chan(&self) -> Option<WebGLCommandSender> {
682 self.webgl_chan
683 .as_ref()
684 .map(|chan| WebGLCommandSender::new(chan.clone()))
685 }
686
687 #[cfg(feature = "webxr")]
688 pub(crate) fn webxr_registry(&self) -> Option<webxr_api::Registry> {
689 self.webxr_registry.clone()
690 }
691
692 fn new_paint_worklet(&self, can_gc: CanGc) -> DomRoot<Worklet> {
693 debug!("Creating new paint worklet.");
694 Worklet::new(self, WorkletGlobalScopeType::Paint, can_gc)
695 }
696
697 pub(crate) fn register_image_cache_listener(
698 &self,
699 id: PendingImageId,
700 callback: impl Fn(PendingImageResponse, &mut js::context::JSContext) + 'static,
701 ) -> ImageCacheResponseCallback {
702 self.pending_image_callbacks
703 .borrow_mut()
704 .entry(id)
705 .or_default()
706 .push(PendingImageCallback(Box::new(callback)));
707
708 let image_cache_sender = self.image_cache_sender.clone();
709 Box::new(move |message| {
710 let _ = image_cache_sender.send(message);
711 })
712 }
713
714 fn pending_layout_image_notification(&self, response: PendingImageResponse) {
715 let mut images = self.pending_layout_images.borrow_mut();
716 let nodes = images.entry(response.id);
717 let nodes = match nodes {
718 Entry::Occupied(nodes) => nodes,
719 Entry::Vacant(_) => return,
720 };
721 if matches!(
722 response.response,
723 ImageResponse::Loaded(_, _) | ImageResponse::FailedToLoadOrDecode
724 ) {
725 for ancillary_data in nodes.get() {
726 match ancillary_data.destination {
727 LayoutImageDestination::BoxTreeConstruction => {
728 ancillary_data.node.dirty(NodeDamage::Other);
729 },
730 LayoutImageDestination::DisplayListBuilding => {
731 self.layout().set_needs_new_display_list();
732 },
733 }
734 }
735 }
736
737 match response.response {
738 ImageResponse::MetadataLoaded(_) => {},
739 ImageResponse::Loaded(_, _) | ImageResponse::FailedToLoadOrDecode => {
740 nodes.remove();
741 },
742 }
743 }
744
745 pub(crate) fn handle_image_rasterization_complete_notification(
746 &self,
747 response: RasterizationCompleteResponse,
748 ) {
749 let mut images = self.pending_images_for_rasterization.borrow_mut();
750 let nodes = images.entry((response.image_id, response.requested_size));
751 let nodes = match nodes {
752 Entry::Occupied(nodes) => nodes,
753 Entry::Vacant(_) => return,
754 };
755 for node in nodes.get() {
756 node.dirty(NodeDamage::Other);
757 }
758 nodes.remove();
759 }
760
761 pub(crate) fn pending_image_notification(
762 &self,
763 response: PendingImageResponse,
764 cx: &mut js::context::JSContext,
765 ) {
766 let mut images = std::mem::take(&mut *self.pending_image_callbacks.borrow_mut());
771 let Entry::Occupied(callbacks) = images.entry(response.id) else {
772 let _ = std::mem::replace(&mut *self.pending_image_callbacks.borrow_mut(), images);
773 return;
774 };
775
776 for callback in callbacks.get() {
777 callback.0(response.clone(), cx);
778 }
779
780 match response.response {
781 ImageResponse::MetadataLoaded(_) => {},
782 ImageResponse::Loaded(_, _) | ImageResponse::FailedToLoadOrDecode => {
783 callbacks.remove();
784 },
785 }
786
787 let _ = std::mem::replace(&mut *self.pending_image_callbacks.borrow_mut(), images);
788 }
789
790 pub(crate) fn paint_api(&self) -> &CrossProcessPaintApi {
791 &self.paint_api
792 }
793
794 pub(crate) fn userscripts(&self) -> &[UserScript] {
795 &self.user_scripts
796 }
797
798 pub(crate) fn get_player_context(&self) -> WindowGLContext {
799 self.player_context.clone()
800 }
801
802 pub(crate) fn dispatch_event_with_target_override(&self, event: &Event, can_gc: CanGc) {
804 event.dispatch(self.upcast(), true, can_gc);
805 }
806
807 pub(crate) fn font_context(&self) -> &Arc<FontContext> {
808 self.as_global_scope()
809 .font_context()
810 .expect("A `Window` should always have a `FontContext`")
811 }
812
813 pub(crate) fn ongoing_navigation(&self) -> OngoingNavigation {
814 self.ongoing_navigation.get()
815 }
816
817 pub(crate) fn set_ongoing_navigation(&self) -> OngoingNavigation {
819 let new_value = self.ongoing_navigation.get().0.wrapping_add(1);
823
824 self.ongoing_navigation.set(OngoingNavigation(new_value));
831
832 OngoingNavigation(new_value)
834 }
835
836 fn stop_loading(&self, cx: &mut js::context::JSContext) {
838 let doc = self.Document();
840
841 self.set_ongoing_navigation();
851
852 doc.abort(cx);
854 }
855
856 fn destroy_top_level_traversable(&self, cx: &mut js::context::JSContext) {
858 let document = self.Document();
864 document.destroy_document_and_its_descendants(cx);
866 self.send_to_constellation(ScriptToConstellationMessage::DiscardTopLevelBrowsingContext);
868 }
869
870 fn definitely_close(&self, cx: &mut js::context::JSContext) {
872 let document = self.Document();
873 if !document.check_if_unloading_is_cancelled(false, CanGc::from_cx(cx)) {
878 return;
879 }
880 document.unload(false, CanGc::from_cx(cx));
884 self.destroy_top_level_traversable(cx);
886 }
887
888 fn cannot_show_simple_dialogs(&self) -> bool {
890 if self
893 .Document()
894 .has_active_sandboxing_flag(SandboxingFlagSet::SANDBOXED_MODALS_FLAG)
895 {
896 return true;
897 }
898
899 false
918 }
919
920 pub(crate) fn perform_a_microtask_checkpoint(&self, cx: &mut js::context::JSContext) {
921 self.script_thread().perform_a_microtask_checkpoint(cx);
922 }
923
924 pub(crate) fn web_font_context(&self) -> WebFontDocumentContext {
925 let global = self.as_global_scope();
926 WebFontDocumentContext {
927 policy_container: global.policy_container(),
928 request_client: global.request_client(),
929 document_url: global.api_base_url(),
930 has_trustworthy_ancestor_origin: global.has_trustworthy_ancestor_origin(),
931 insecure_requests_policy: global.insecure_requests_policy(),
932 csp_handler: Box::new(FontCspHandler {
933 global: Trusted::new(global),
934 task_source: global
935 .task_manager()
936 .dom_manipulation_task_source()
937 .to_sendable(),
938 }),
939 network_timing_handler: Box::new(FontNetworkTimingHandler {
940 global: Trusted::new(global),
941 task_source: global
942 .task_manager()
943 .dom_manipulation_task_source()
944 .to_sendable(),
945 }),
946 }
947 }
948
949 #[expect(unsafe_code)]
950 pub(crate) fn gc(&self) {
951 unsafe {
952 JS_GC(*self.get_cx(), GCReason::API);
953 }
954 }
955}
956
957#[derive(Debug)]
958struct FontCspHandler {
959 global: Trusted<GlobalScope>,
960 task_source: SendableTaskSource,
961}
962
963impl CspViolationHandler for FontCspHandler {
964 fn process_violations(&self, violations: Vec<Violation>) {
965 let global = self.global.clone();
966 self.task_source.queue(task!(csp_violation: move || {
967 global.root().report_csp_violations(violations, None, None);
968 }));
969 }
970
971 fn clone(&self) -> Box<dyn CspViolationHandler> {
972 Box::new(Self {
973 global: self.global.clone(),
974 task_source: self.task_source.clone(),
975 })
976 }
977}
978
979#[derive(Debug)]
980struct FontNetworkTimingHandler {
981 global: Trusted<GlobalScope>,
982 task_source: SendableTaskSource,
983}
984
985impl NetworkTimingHandler for FontNetworkTimingHandler {
986 fn submit_timing(&self, url: ServoUrl, response: ResourceFetchTiming) {
987 let global = self.global.clone();
988 self.task_source.queue(task!(network_timing: move || {
989 submit_timing(
990 &FontFetchListener {
991 url,
992 global
993 },
994 &Ok(()),
995 &response,
996 CanGc::note(),
997 );
998 }));
999 }
1000
1001 fn clone(&self) -> Box<dyn NetworkTimingHandler> {
1002 Box::new(Self {
1003 global: self.global.clone(),
1004 task_source: self.task_source.clone(),
1005 })
1006 }
1007}
1008
1009#[derive(Debug)]
1010struct FontFetchListener {
1011 global: Trusted<GlobalScope>,
1012 url: ServoUrl,
1013}
1014
1015impl ResourceTimingListener for FontFetchListener {
1016 fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
1017 (InitiatorType::Css, self.url.clone())
1018 }
1019
1020 fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
1021 self.global.root()
1022 }
1023}
1024
1025pub(crate) fn base64_btoa(input: DOMString) -> Fallible<DOMString> {
1027 if input.str().chars().any(|c: char| c > '\u{FF}') {
1031 Err(Error::InvalidCharacter(None))
1032 } else {
1033 let octets = input
1038 .str()
1039 .chars()
1040 .map(|c: char| c as u8)
1041 .collect::<Vec<u8>>();
1042
1043 let config =
1046 base64::engine::general_purpose::GeneralPurposeConfig::new().with_encode_padding(true);
1047 let engine = base64::engine::GeneralPurpose::new(&base64::alphabet::STANDARD, config);
1048 Ok(DOMString::from(engine.encode(octets)))
1049 }
1050}
1051
1052pub(crate) fn base64_atob(input: DOMString) -> Fallible<DOMString> {
1054 fn is_html_space(c: char) -> bool {
1056 HTML_SPACE_CHARACTERS.contains(&c)
1057 }
1058 let without_spaces = input
1059 .str()
1060 .chars()
1061 .filter(|&c| !is_html_space(c))
1062 .collect::<String>();
1063 let mut input = &*without_spaces;
1064
1065 if input.len() % 4 == 0 {
1069 if input.ends_with("==") {
1070 input = &input[..input.len() - 2]
1071 } else if input.ends_with('=') {
1072 input = &input[..input.len() - 1]
1073 }
1074 }
1075
1076 if input.len() % 4 == 1 {
1079 return Err(Error::InvalidCharacter(None));
1080 }
1081
1082 if input
1090 .chars()
1091 .any(|c| c != '+' && c != '/' && !c.is_alphanumeric())
1092 {
1093 return Err(Error::InvalidCharacter(None));
1094 }
1095
1096 let config = base64::engine::general_purpose::GeneralPurposeConfig::new()
1097 .with_decode_padding_mode(base64::engine::DecodePaddingMode::RequireNone)
1098 .with_decode_allow_trailing_bits(true);
1099 let engine = base64::engine::GeneralPurpose::new(&base64::alphabet::STANDARD, config);
1100
1101 let data = engine
1102 .decode(input)
1103 .map_err(|_| Error::InvalidCharacter(None))?;
1104 Ok(data.iter().map(|&b| b as char).collect::<String>().into())
1105}
1106
1107impl WindowMethods<crate::DomTypeHolder> for Window {
1108 fn Alert_(&self) {
1110 self.Alert(DOMString::new());
1113 }
1114
1115 fn Alert(&self, mut message: DOMString) {
1117 if self.cannot_show_simple_dialogs() {
1119 return;
1120 }
1121
1122 message.normalize_newlines();
1126
1127 {
1138 let stderr = stderr();
1142 let mut stderr = stderr.lock();
1143 let stdout = stdout();
1144 let mut stdout = stdout.lock();
1145 writeln!(&mut stdout, "\nALERT: {message}").unwrap();
1146 stdout.flush().unwrap();
1147 stderr.flush().unwrap();
1148 }
1149
1150 let (sender, receiver) =
1151 ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
1152 let dialog = SimpleDialogRequest::Alert {
1153 id: self.Document().embedder_controls().next_control_id(),
1154 message: message.to_string(),
1155 response_sender: sender,
1156 };
1157 self.send_to_embedder(EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog));
1158 receiver.recv().unwrap_or_else(|_| {
1159 debug!("Alert dialog was cancelled or failed to show.");
1161 AlertResponse::Ok
1162 });
1163
1164 }
1167
1168 fn Confirm(&self, mut message: DOMString) -> bool {
1170 if self.cannot_show_simple_dialogs() {
1172 return false;
1173 }
1174
1175 message.normalize_newlines();
1177
1178 let (sender, receiver) =
1184 ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
1185 let dialog = SimpleDialogRequest::Confirm {
1186 id: self.Document().embedder_controls().next_control_id(),
1187 message: message.to_string(),
1188 response_sender: sender,
1189 };
1190 self.send_to_embedder(EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog));
1191
1192 match receiver.recv() {
1208 Ok(ConfirmResponse::Ok) => true,
1209 Ok(ConfirmResponse::Cancel) => false,
1210 Err(_) => {
1211 warn!("Confirm dialog was cancelled or failed to show.");
1212 false
1213 },
1214 }
1215 }
1216
1217 fn Prompt(&self, mut message: DOMString, default: DOMString) -> Option<DOMString> {
1219 if self.cannot_show_simple_dialogs() {
1221 return None;
1222 }
1223
1224 message.normalize_newlines();
1226
1227 let (sender, receiver) =
1235 ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
1236 let dialog = SimpleDialogRequest::Prompt {
1237 id: self.Document().embedder_controls().next_control_id(),
1238 message: message.to_string(),
1239 default: default.to_string(),
1240 response_sender: sender,
1241 };
1242 self.send_to_embedder(EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog));
1243
1244 match receiver.recv() {
1263 Ok(PromptResponse::Ok(input)) => Some(input.into()),
1264 Ok(PromptResponse::Cancel) => None,
1265 Err(_) => {
1266 warn!("Prompt dialog was cancelled or failed to show.");
1267 None
1268 },
1269 }
1270 }
1271
1272 fn Stop(&self, cx: &mut js::context::JSContext) {
1274 self.stop_loading(cx);
1279 }
1280
1281 fn Focus(&self) {
1283 let current = match self.undiscarded_window_proxy() {
1287 Some(proxy) => proxy,
1288 None => return,
1289 };
1290
1291 current.focus();
1293
1294 }
1300
1301 fn Blur(&self) {
1303 }
1306
1307 fn Open(
1309 &self,
1310 cx: &mut JSContext,
1311 url: USVString,
1312 target: DOMString,
1313 features: DOMString,
1314 ) -> Fallible<Option<DomRoot<WindowProxy>>> {
1315 self.window_proxy().open(cx, url, target, features)
1316 }
1317
1318 fn GetOpener(&self, cx: &mut CurrentRealm, mut retval: MutableHandleValue) -> Fallible<()> {
1320 let current = match self.window_proxy.get() {
1322 Some(proxy) => proxy,
1323 None => {
1325 retval.set(NullValue());
1326 return Ok(());
1327 },
1328 };
1329 if current.is_browsing_context_discarded() {
1334 retval.set(NullValue());
1335 return Ok(());
1336 }
1337 current.opener(cx, retval);
1339 Ok(())
1340 }
1341
1342 #[expect(unsafe_code)]
1343 fn SetOpener(&self, cx: SafeJSContext, value: HandleValue) -> ErrorResult {
1345 if value.is_null() {
1347 if let Some(proxy) = self.window_proxy.get() {
1348 proxy.disown();
1349 }
1350 return Ok(());
1351 }
1352 let obj = self.reflector().get_jsobject();
1354 unsafe {
1355 let result =
1356 JS_DefineProperty(*cx, obj, c"opener".as_ptr(), value, JSPROP_ENUMERATE as u32);
1357
1358 if result { Ok(()) } else { Err(Error::JSFailed) }
1359 }
1360 }
1361
1362 fn Closed(&self) -> bool {
1364 self.window_proxy
1365 .get()
1366 .map(|ref proxy| proxy.is_browsing_context_discarded() || proxy.is_closing())
1367 .unwrap_or(true)
1368 }
1369
1370 fn Close(&self) {
1372 let window_proxy = match self.window_proxy.get() {
1374 Some(proxy) => proxy,
1375 None => return,
1377 };
1378 if window_proxy.is_closing() {
1380 return;
1381 }
1382 if let Ok(history_length) = self.History().GetLength() {
1385 let is_auxiliary = window_proxy.is_auxiliary();
1386
1387 let is_script_closable = (self.is_top_level() && history_length == 1) ||
1389 is_auxiliary ||
1390 pref!(dom_allow_scripts_to_close_windows);
1391
1392 if is_script_closable {
1396 window_proxy.close();
1398
1399 let this = Trusted::new(self);
1401 let task = task!(window_close_browsing_context: move |cx| {
1402 let window = this.root();
1403 window.definitely_close(cx);
1404 });
1405 self.as_global_scope()
1406 .task_manager()
1407 .dom_manipulation_task_source()
1408 .queue(task);
1409 }
1410 }
1411 }
1412
1413 fn Document(&self) -> DomRoot<Document> {
1415 self.document
1416 .get()
1417 .expect("Document accessed before initialization.")
1418 }
1419
1420 fn History(&self) -> DomRoot<History> {
1422 self.history.or_init(|| History::new(self, CanGc::note()))
1423 }
1424
1425 fn IndexedDB(&self) -> DomRoot<IDBFactory> {
1427 self.upcast::<GlobalScope>().get_indexeddb()
1428 }
1429
1430 fn CustomElements(&self) -> DomRoot<CustomElementRegistry> {
1432 self.custom_element_registry
1433 .or_init(|| CustomElementRegistry::new(self, CanGc::note()))
1434 }
1435
1436 fn Location(&self) -> DomRoot<Location> {
1438 self.location.or_init(|| Location::new(self, CanGc::note()))
1439 }
1440
1441 fn SessionStorage(&self) -> DomRoot<Storage> {
1443 self.session_storage
1444 .or_init(|| Storage::new(self, WebStorageType::Session, CanGc::note()))
1445 }
1446
1447 fn LocalStorage(&self) -> DomRoot<Storage> {
1449 self.local_storage
1450 .or_init(|| Storage::new(self, WebStorageType::Local, CanGc::note()))
1451 }
1452
1453 fn CookieStore(&self, can_gc: CanGc) -> DomRoot<CookieStore> {
1455 self.global().cookie_store(can_gc)
1456 }
1457
1458 fn Crypto(&self) -> DomRoot<Crypto> {
1460 self.as_global_scope().crypto(CanGc::note())
1461 }
1462
1463 fn GetFrameElement(&self) -> Option<DomRoot<Element>> {
1465 let window_proxy = self.window_proxy.get()?;
1467
1468 let container = window_proxy.frame_element()?;
1470
1471 let container_doc = container.owner_document();
1473 let current_doc = GlobalScope::current()
1474 .expect("No current global object")
1475 .as_window()
1476 .Document();
1477 if !current_doc
1478 .origin()
1479 .same_origin_domain(container_doc.origin())
1480 {
1481 return None;
1482 }
1483 Some(DomRoot::from_ref(container))
1485 }
1486
1487 fn ReportError(&self, cx: SafeJSContext, error: HandleValue, can_gc: CanGc) {
1489 self.as_global_scope()
1490 .report_an_exception(cx, error, can_gc);
1491 }
1492
1493 fn Navigator(&self) -> DomRoot<Navigator> {
1495 self.navigator
1496 .or_init(|| Navigator::new(self, CanGc::note()))
1497 }
1498
1499 fn ClientInformation(&self) -> DomRoot<Navigator> {
1501 self.Navigator()
1502 }
1503
1504 fn SetTimeout(
1506 &self,
1507 cx: &mut js::context::JSContext,
1508 callback: TrustedScriptOrStringOrFunction,
1509 timeout: i32,
1510 args: Vec<HandleValue>,
1511 ) -> Fallible<i32> {
1512 let callback = match callback {
1513 TrustedScriptOrStringOrFunction::String(i) => {
1514 TimerCallback::StringTimerCallback(TrustedScriptOrString::String(i))
1515 },
1516 TrustedScriptOrStringOrFunction::TrustedScript(i) => {
1517 TimerCallback::StringTimerCallback(TrustedScriptOrString::TrustedScript(i))
1518 },
1519 TrustedScriptOrStringOrFunction::Function(i) => TimerCallback::FunctionTimerCallback(i),
1520 };
1521 self.as_global_scope().set_timeout_or_interval(
1522 cx,
1523 callback,
1524 args,
1525 Duration::from_millis(timeout.max(0) as u64),
1526 IsInterval::NonInterval,
1527 )
1528 }
1529
1530 fn ClearTimeout(&self, handle: i32) {
1532 self.as_global_scope().clear_timeout_or_interval(handle);
1533 }
1534
1535 fn SetInterval(
1537 &self,
1538 cx: &mut js::context::JSContext,
1539 callback: TrustedScriptOrStringOrFunction,
1540 timeout: i32,
1541 args: Vec<HandleValue>,
1542 ) -> Fallible<i32> {
1543 let callback = match callback {
1544 TrustedScriptOrStringOrFunction::String(i) => {
1545 TimerCallback::StringTimerCallback(TrustedScriptOrString::String(i))
1546 },
1547 TrustedScriptOrStringOrFunction::TrustedScript(i) => {
1548 TimerCallback::StringTimerCallback(TrustedScriptOrString::TrustedScript(i))
1549 },
1550 TrustedScriptOrStringOrFunction::Function(i) => TimerCallback::FunctionTimerCallback(i),
1551 };
1552 self.as_global_scope().set_timeout_or_interval(
1553 cx,
1554 callback,
1555 args,
1556 Duration::from_millis(timeout.max(0) as u64),
1557 IsInterval::Interval,
1558 )
1559 }
1560
1561 fn ClearInterval(&self, handle: i32) {
1563 self.ClearTimeout(handle);
1564 }
1565
1566 fn QueueMicrotask(&self, callback: Rc<VoidFunction>) {
1568 ScriptThread::enqueue_microtask(Microtask::User(UserMicrotask {
1569 callback,
1570 pipeline: self.pipeline_id(),
1571 }));
1572 }
1573
1574 fn CreateImageBitmap(
1576 &self,
1577 realm: &mut CurrentRealm,
1578 image: ImageBitmapSource,
1579 options: &ImageBitmapOptions,
1580 ) -> Rc<Promise> {
1581 ImageBitmap::create_image_bitmap(
1582 self.as_global_scope(),
1583 image,
1584 0,
1585 0,
1586 None,
1587 None,
1588 options,
1589 realm,
1590 )
1591 }
1592
1593 fn CreateImageBitmap_(
1595 &self,
1596 realm: &mut CurrentRealm,
1597 image: ImageBitmapSource,
1598 sx: i32,
1599 sy: i32,
1600 sw: i32,
1601 sh: i32,
1602 options: &ImageBitmapOptions,
1603 ) -> Rc<Promise> {
1604 ImageBitmap::create_image_bitmap(
1605 self.as_global_scope(),
1606 image,
1607 sx,
1608 sy,
1609 Some(sw),
1610 Some(sh),
1611 options,
1612 realm,
1613 )
1614 }
1615
1616 fn Window(&self) -> DomRoot<WindowProxy> {
1618 self.window_proxy()
1619 }
1620
1621 fn Self_(&self) -> DomRoot<WindowProxy> {
1623 self.window_proxy()
1624 }
1625
1626 fn Frames(&self) -> DomRoot<WindowProxy> {
1628 self.window_proxy()
1629 }
1630
1631 fn Length(&self) -> u32 {
1633 self.Document().iframes().iter().count() as u32
1634 }
1635
1636 fn GetParent(&self) -> Option<DomRoot<WindowProxy>> {
1638 let window_proxy = self.undiscarded_window_proxy()?;
1640
1641 if let Some(parent) = window_proxy.parent() {
1643 return Some(DomRoot::from_ref(parent));
1644 }
1645 Some(window_proxy)
1647 }
1648
1649 fn GetTop(&self) -> Option<DomRoot<WindowProxy>> {
1651 let window_proxy = self.undiscarded_window_proxy()?;
1653
1654 Some(DomRoot::from_ref(window_proxy.top()))
1656 }
1657
1658 fn Performance(&self) -> DomRoot<Performance> {
1661 self.performance.or_init(|| {
1662 Performance::new(
1663 self.as_global_scope(),
1664 self.navigation_start.get(),
1665 CanGc::note(),
1666 )
1667 })
1668 }
1669
1670 global_event_handlers!();
1672
1673 window_event_handlers!();
1675
1676 fn Screen(&self, can_gc: CanGc) -> DomRoot<Screen> {
1678 self.screen.or_init(|| Screen::new(self, can_gc))
1679 }
1680
1681 fn GetVisualViewport(&self, can_gc: CanGc) -> Option<DomRoot<VisualViewport>> {
1683 if !self.Document().is_fully_active() {
1687 return None;
1688 }
1689
1690 Some(self.get_or_init_visual_viewport(can_gc))
1691 }
1692
1693 fn Btoa(&self, btoa: DOMString) -> Fallible<DOMString> {
1695 base64_btoa(btoa)
1696 }
1697
1698 fn Atob(&self, atob: DOMString) -> Fallible<DOMString> {
1700 base64_atob(atob)
1701 }
1702
1703 fn RequestAnimationFrame(&self, callback: Rc<FrameRequestCallback>) -> u32 {
1705 self.Document()
1706 .request_animation_frame(AnimationFrameCallback::FrameRequestCallback { callback })
1707 }
1708
1709 fn CancelAnimationFrame(&self, ident: u32) {
1711 let doc = self.Document();
1712 doc.cancel_animation_frame(ident);
1713 }
1714
1715 fn PostMessage(
1717 &self,
1718 cx: &mut JSContext,
1719 message: HandleValue,
1720 target_origin: USVString,
1721 transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
1722 ) -> ErrorResult {
1723 let incumbent = GlobalScope::incumbent().expect("no incumbent global?");
1724 let source = incumbent.as_window();
1725 let source_origin = source.Document().origin().immutable().clone();
1726
1727 self.post_message_impl(&target_origin, source_origin, source, cx, message, transfer)
1728 }
1729
1730 fn PostMessage_(
1732 &self,
1733 cx: &mut JSContext,
1734 message: HandleValue,
1735 options: RootedTraceableBox<WindowPostMessageOptions>,
1736 ) -> ErrorResult {
1737 let mut rooted = CustomAutoRooter::new(
1738 options
1739 .parent
1740 .transfer
1741 .iter()
1742 .map(|js: &RootedTraceableBox<Heap<*mut JSObject>>| js.get())
1743 .collect(),
1744 );
1745 #[expect(unsafe_code)]
1746 let transfer = unsafe { CustomAutoRooterGuard::new(cx.raw_cx(), &mut rooted) };
1747
1748 let incumbent = GlobalScope::incumbent().expect("no incumbent global?");
1749 let source = incumbent.as_window();
1750
1751 let source_origin = source.Document().origin().immutable().clone();
1752
1753 self.post_message_impl(
1754 &options.targetOrigin,
1755 source_origin,
1756 source,
1757 cx,
1758 message,
1759 transfer,
1760 )
1761 }
1762
1763 fn CaptureEvents(&self) {
1765 }
1767
1768 fn ReleaseEvents(&self) {
1770 }
1772
1773 fn WebdriverCallback(&self, realm: &mut CurrentRealm, value: HandleValue) {
1775 let webdriver_script_sender = self.webdriver_script_chan.borrow_mut().take();
1776 if let Some(webdriver_script_sender) = webdriver_script_sender {
1777 let result = jsval_to_webdriver(realm, &self.globalscope, value);
1778 let _ = webdriver_script_sender.send(result);
1779 }
1780 }
1781
1782 fn WebdriverException(&self, cx: &mut JSContext, value: HandleValue) {
1783 let webdriver_script_sender = self.webdriver_script_chan.borrow_mut().take();
1784 if let Some(webdriver_script_sender) = webdriver_script_sender {
1785 let error_info = ErrorInfo::from_value(value, cx.into(), CanGc::from_cx(cx));
1786 let _ = webdriver_script_sender.send(Err(
1787 JavaScriptEvaluationError::EvaluationFailure(Some(
1788 javascript_error_info_from_error_info(cx, &error_info, value),
1789 )),
1790 ));
1791 }
1792 }
1793
1794 fn WebdriverElement(&self, id: DOMString) -> Option<DomRoot<Element>> {
1795 find_node_by_unique_id_in_document(&self.Document(), id.into()).and_then(Root::downcast)
1796 }
1797
1798 fn WebdriverFrame(&self, browsing_context_id: DOMString) -> Option<DomRoot<WindowProxy>> {
1799 self.Document()
1800 .iframes()
1801 .iter()
1802 .find(|iframe| {
1803 iframe
1804 .browsing_context_id()
1805 .as_ref()
1806 .map(BrowsingContextId::to_string) ==
1807 Some(browsing_context_id.to_string())
1808 })
1809 .and_then(|iframe| iframe.GetContentWindow())
1810 }
1811
1812 fn WebdriverWindow(&self, webview_id: DOMString) -> DomRoot<WindowProxy> {
1813 let window_proxy = &self
1814 .window_proxy
1815 .get()
1816 .expect("Should always have a WindowProxy when calling WebdriverWindow");
1817 assert!(
1818 self.is_top_level(),
1819 "Window must be top level browsing context."
1820 );
1821 assert!(self.webview_id().to_string() == webview_id);
1822 DomRoot::from_ref(window_proxy)
1823 }
1824
1825 fn WebdriverShadowRoot(&self, id: DOMString) -> Option<DomRoot<ShadowRoot>> {
1826 find_node_by_unique_id_in_document(&self.Document(), id.into()).and_then(Root::downcast)
1827 }
1828
1829 fn GetComputedStyle(
1831 &self,
1832 element: &Element,
1833 pseudo: Option<DOMString>,
1834 ) -> DomRoot<CSSStyleDeclaration> {
1835 let mut is_null = false;
1839
1840 let pseudo = pseudo.map(|mut s| {
1846 s.make_ascii_lowercase();
1847 s
1848 });
1849 let pseudo = match pseudo {
1850 Some(ref pseudo) if pseudo == ":before" || pseudo == "::before" => {
1851 Some(PseudoElement::Before)
1852 },
1853 Some(ref pseudo) if pseudo == ":after" || pseudo == "::after" => {
1854 Some(PseudoElement::After)
1855 },
1856 Some(ref pseudo) if pseudo == "::selection" => Some(PseudoElement::Selection),
1857 Some(ref pseudo) if pseudo == "::marker" => Some(PseudoElement::Marker),
1858 Some(ref pseudo) if pseudo == "::placeholder" => Some(PseudoElement::Placeholder),
1859 Some(ref pseudo) if pseudo.starts_with(':') => {
1860 is_null = true;
1863 None
1864 },
1865 _ => None,
1866 };
1867
1868 CSSStyleDeclaration::new(
1884 self,
1885 if is_null {
1886 CSSStyleOwner::Null
1887 } else {
1888 CSSStyleOwner::Element(Dom::from_ref(element))
1889 },
1890 pseudo,
1891 CSSModificationAccess::Readonly,
1892 CanGc::note(),
1893 )
1894 }
1895
1896 fn InnerHeight(&self) -> i32 {
1899 self.viewport_details
1900 .get()
1901 .size
1902 .height
1903 .to_i32()
1904 .unwrap_or(0)
1905 }
1906
1907 fn InnerWidth(&self) -> i32 {
1910 self.viewport_details.get().size.width.to_i32().unwrap_or(0)
1911 }
1912
1913 fn ScrollX(&self) -> i32 {
1915 self.scroll_offset().x as i32
1916 }
1917
1918 fn PageXOffset(&self) -> i32 {
1920 self.ScrollX()
1921 }
1922
1923 fn ScrollY(&self) -> i32 {
1925 self.scroll_offset().y as i32
1926 }
1927
1928 fn PageYOffset(&self) -> i32 {
1930 self.ScrollY()
1931 }
1932
1933 fn Scroll(&self, options: &ScrollToOptions) {
1935 let x = options.left.unwrap_or(0.0) as f32;
1940
1941 let y = options.top.unwrap_or(0.0) as f32;
1944
1945 self.scroll(x, y, options.parent.behavior);
1947 }
1948
1949 fn Scroll_(&self, x: f64, y: f64) {
1951 self.scroll(x as f32, y as f32, ScrollBehavior::Auto);
1955 }
1956
1957 fn ScrollTo(&self, options: &ScrollToOptions) {
1962 self.Scroll(options);
1963 }
1964
1965 fn ScrollTo_(&self, x: f64, y: f64) {
1970 self.Scroll_(x, y)
1971 }
1972
1973 fn ScrollBy(&self, options: &ScrollToOptions) {
1975 let mut options = options.clone();
1981 let x = options.left.unwrap_or(0.0);
1982 let x = if x.is_finite() { x } else { 0.0 };
1983 let y = options.top.unwrap_or(0.0);
1984 let y = if y.is_finite() { y } else { 0.0 };
1985
1986 options.left.replace(x + self.ScrollX() as f64);
1988
1989 options.top.replace(y + self.ScrollY() as f64);
1991
1992 self.Scroll(&options)
1994 }
1995
1996 fn ScrollBy_(&self, x: f64, y: f64) {
1998 let mut options = ScrollToOptions::empty();
2002
2003 options.left.replace(x);
2006
2007 options.top.replace(y);
2009
2010 self.ScrollBy(&options);
2012 }
2013
2014 fn ResizeTo(&self, width: i32, height: i32) {
2016 let window_proxy = match self.window_proxy.get() {
2018 Some(proxy) => proxy,
2019 None => return,
2020 };
2021
2022 if !window_proxy.is_auxiliary() {
2025 return;
2026 }
2027
2028 let dpr = self.device_pixel_ratio();
2029 let size = Size2D::new(width, height).to_f32() * dpr;
2030 self.send_to_embedder(EmbedderMsg::ResizeTo(self.webview_id(), size.to_i32()));
2031 }
2032
2033 fn ResizeBy(&self, x: i32, y: i32) {
2035 let size = self.client_window().size();
2036 self.ResizeTo(x + size.width, y + size.height)
2038 }
2039
2040 fn MoveTo(&self, x: i32, y: i32) {
2042 let dpr = self.device_pixel_ratio();
2045 let point = Point2D::new(x, y).to_f32() * dpr;
2046 let msg = EmbedderMsg::MoveTo(self.webview_id(), point.to_i32());
2047 self.send_to_embedder(msg);
2048 }
2049
2050 fn MoveBy(&self, x: i32, y: i32) {
2052 let origin = self.client_window().min;
2053 self.MoveTo(x + origin.x, y + origin.y)
2055 }
2056
2057 fn ScreenX(&self) -> i32 {
2059 self.client_window().min.x
2060 }
2061
2062 fn ScreenY(&self) -> i32 {
2064 self.client_window().min.y
2065 }
2066
2067 fn OuterHeight(&self) -> i32 {
2069 self.client_window().height()
2070 }
2071
2072 fn OuterWidth(&self) -> i32 {
2074 self.client_window().width()
2075 }
2076
2077 fn DevicePixelRatio(&self) -> Finite<f64> {
2079 Finite::wrap(self.device_pixel_ratio().get() as f64)
2080 }
2081
2082 fn Status(&self) -> DOMString {
2084 self.status.borrow().clone()
2085 }
2086
2087 fn SetStatus(&self, status: DOMString) {
2089 *self.status.borrow_mut() = status
2090 }
2091
2092 fn MatchMedia(&self, query: DOMString) -> DomRoot<MediaQueryList> {
2094 let media_query_list = MediaList::parse_media_list(&query.str(), self);
2095 let document = self.Document();
2096 let mql = MediaQueryList::new(&document, media_query_list, CanGc::note());
2097 self.media_query_lists.track(&*mql);
2098 mql
2099 }
2100
2101 fn Fetch(
2103 &self,
2104 realm: &mut CurrentRealm,
2105 input: RequestOrUSVString,
2106 init: RootedTraceableBox<RequestInit>,
2107 ) -> Rc<Promise> {
2108 fetch::Fetch(self.upcast(), input, init, realm)
2109 }
2110
2111 fn FetchLater(
2113 &self,
2114 input: RequestInfo,
2115 init: RootedTraceableBox<DeferredRequestInit>,
2116 can_gc: CanGc,
2117 ) -> Fallible<DomRoot<FetchLaterResult>> {
2118 fetch::FetchLater(self, input, init, can_gc)
2119 }
2120
2121 #[cfg(feature = "bluetooth")]
2122 fn TestRunner(&self) -> DomRoot<TestRunner> {
2123 self.test_runner
2124 .or_init(|| TestRunner::new(self.upcast(), CanGc::note()))
2125 }
2126
2127 fn RunningAnimationCount(&self) -> u32 {
2128 self.document
2129 .get()
2130 .map_or(0, |d| d.animations().running_animation_count() as u32)
2131 }
2132
2133 fn SetName(&self, name: DOMString) {
2135 if let Some(proxy) = self.undiscarded_window_proxy() {
2136 proxy.set_name(name);
2137 }
2138 }
2139
2140 fn Name(&self) -> DOMString {
2142 match self.undiscarded_window_proxy() {
2143 Some(proxy) => proxy.get_name(),
2144 None => "".into(),
2145 }
2146 }
2147
2148 fn Origin(&self) -> USVString {
2150 USVString(self.origin().immutable().ascii_serialization())
2151 }
2152
2153 fn GetSelection(&self) -> Option<DomRoot<Selection>> {
2155 self.document
2156 .get()
2157 .and_then(|d| d.GetSelection(CanGc::note()))
2158 }
2159
2160 fn Event(&self, cx: SafeJSContext, rval: MutableHandleValue) {
2162 if let Some(ref event) = *self.current_event.borrow() {
2163 event
2164 .reflector()
2165 .get_jsobject()
2166 .safe_to_jsval(cx, rval, CanGc::note());
2167 }
2168 }
2169
2170 fn IsSecureContext(&self) -> bool {
2171 self.as_global_scope().is_secure_context()
2172 }
2173
2174 fn NamedGetter(&self, name: DOMString) -> Option<NamedPropertyValue> {
2176 if name.is_empty() {
2177 return None;
2178 }
2179 let document = self.Document();
2180
2181 let iframes: Vec<_> = document
2183 .iframes()
2184 .iter()
2185 .filter(|iframe| {
2186 if let Some(window) = iframe.GetContentWindow() {
2187 return window.get_name() == name;
2188 }
2189 false
2190 })
2191 .collect();
2192
2193 let iframe_iter = iframes.iter().map(|iframe| iframe.upcast::<Element>());
2194
2195 let name = Atom::from(name);
2196
2197 let elements_with_name = document.get_elements_with_name(&name);
2199 let name_iter = elements_with_name
2200 .iter()
2201 .map(|element| &**element)
2202 .filter(|elem| is_named_element_with_name_attribute(elem));
2203 let elements_with_id = document.get_elements_with_id(&name);
2204 let id_iter = elements_with_id
2205 .iter()
2206 .map(|element| &**element)
2207 .filter(|elem| is_named_element_with_id_attribute(elem));
2208
2209 for elem in iframe_iter.clone() {
2211 if let Some(nested_window_proxy) = elem
2212 .downcast::<HTMLIFrameElement>()
2213 .and_then(|iframe| iframe.GetContentWindow())
2214 {
2215 return Some(NamedPropertyValue::WindowProxy(nested_window_proxy));
2216 }
2217 }
2218
2219 let mut elements = iframe_iter.chain(name_iter).chain(id_iter);
2220
2221 let first = elements.next()?;
2222
2223 if elements.next().is_none() {
2224 return Some(NamedPropertyValue::Element(DomRoot::from_ref(first)));
2226 }
2227
2228 #[derive(JSTraceable, MallocSizeOf)]
2230 struct WindowNamedGetter {
2231 #[no_trace]
2232 name: Atom,
2233 }
2234 impl CollectionFilter for WindowNamedGetter {
2235 fn filter(&self, elem: &Element, _root: &Node) -> bool {
2236 let type_ = match elem.upcast::<Node>().type_id() {
2237 NodeTypeId::Element(ElementTypeId::HTMLElement(type_)) => type_,
2238 _ => return false,
2239 };
2240 if elem.get_id().as_ref() == Some(&self.name) {
2241 return true;
2242 }
2243 match type_ {
2244 HTMLElementTypeId::HTMLEmbedElement |
2245 HTMLElementTypeId::HTMLFormElement |
2246 HTMLElementTypeId::HTMLImageElement |
2247 HTMLElementTypeId::HTMLObjectElement => {
2248 elem.get_name().as_ref() == Some(&self.name)
2249 },
2250 _ => false,
2251 }
2252 }
2253 }
2254 let collection = HTMLCollection::create(
2255 self,
2256 document.upcast(),
2257 Box::new(WindowNamedGetter { name }),
2258 CanGc::note(),
2259 );
2260 Some(NamedPropertyValue::HTMLCollection(collection))
2261 }
2262
2263 fn SupportedPropertyNames(&self) -> Vec<DOMString> {
2265 let mut names_with_first_named_element_map: HashMap<&Atom, &Element> = HashMap::new();
2266
2267 let document = self.Document();
2268 let name_map = document.name_map();
2269 for (name, elements) in &name_map.0 {
2270 if name.is_empty() {
2271 continue;
2272 }
2273 let mut name_iter = elements
2274 .iter()
2275 .filter(|elem| is_named_element_with_name_attribute(elem));
2276 if let Some(first) = name_iter.next() {
2277 names_with_first_named_element_map.insert(name, first);
2278 }
2279 }
2280 let id_map = document.id_map();
2281 for (id, elements) in &id_map.0 {
2282 if id.is_empty() {
2283 continue;
2284 }
2285 let mut id_iter = elements
2286 .iter()
2287 .filter(|elem| is_named_element_with_id_attribute(elem));
2288 if let Some(first) = id_iter.next() {
2289 match names_with_first_named_element_map.entry(id) {
2290 Entry::Vacant(entry) => drop(entry.insert(first)),
2291 Entry::Occupied(mut entry) => {
2292 if first.upcast::<Node>().is_before(entry.get().upcast()) {
2293 *entry.get_mut() = first;
2294 }
2295 },
2296 }
2297 }
2298 }
2299
2300 let mut names_with_first_named_element_vec: Vec<(&Atom, &Element)> =
2301 names_with_first_named_element_map
2302 .iter()
2303 .map(|(k, v)| (*k, *v))
2304 .collect();
2305 names_with_first_named_element_vec.sort_unstable_by(|a, b| {
2306 if a.1 == b.1 {
2307 a.0.cmp(b.0)
2310 } else if a.1.upcast::<Node>().is_before(b.1.upcast::<Node>()) {
2311 cmp::Ordering::Less
2312 } else {
2313 cmp::Ordering::Greater
2314 }
2315 });
2316
2317 names_with_first_named_element_vec
2318 .iter()
2319 .map(|(k, _v)| DOMString::from(&***k))
2320 .collect()
2321 }
2322
2323 fn StructuredClone(
2325 &self,
2326 cx: SafeJSContext,
2327 value: HandleValue,
2328 options: RootedTraceableBox<StructuredSerializeOptions>,
2329 can_gc: CanGc,
2330 retval: MutableHandleValue,
2331 ) -> Fallible<()> {
2332 self.as_global_scope()
2333 .structured_clone(cx, value, options, retval, can_gc)
2334 }
2335
2336 fn TrustedTypes(&self, cx: &mut JSContext) -> DomRoot<TrustedTypePolicyFactory> {
2337 self.trusted_types
2338 .or_init(|| TrustedTypePolicyFactory::new(cx, self.as_global_scope()))
2339 }
2340}
2341
2342impl Window {
2343 pub(crate) fn scroll_offset(&self) -> Vector2D<f32, LayoutPixel> {
2344 self.scroll_offset_query_with_external_scroll_id(self.pipeline_id().root_scroll_id())
2345 }
2346
2347 pub(crate) fn create_named_properties_object(
2350 cx: SafeJSContext,
2351 proto: HandleObject,
2352 object: MutableHandleObject,
2353 ) {
2354 window_named_properties::create(cx, proto, object)
2355 }
2356
2357 pub(crate) fn current_event(&self) -> Option<DomRoot<Event>> {
2358 self.current_event
2359 .borrow()
2360 .as_ref()
2361 .map(|e| DomRoot::from_ref(&**e))
2362 }
2363
2364 pub(crate) fn set_current_event(&self, event: Option<&Event>) -> Option<DomRoot<Event>> {
2365 let current = self.current_event();
2366 *self.current_event.borrow_mut() = event.map(Dom::from_ref);
2367 current
2368 }
2369
2370 fn post_message_impl(
2372 &self,
2373 target_origin: &USVString,
2374 source_origin: ImmutableOrigin,
2375 source: &Window,
2376 cx: &mut JSContext,
2377 message: HandleValue,
2378 transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
2379 ) -> ErrorResult {
2380 let data = structuredclone::write(cx.into(), message, Some(transfer))?;
2382
2383 let target_origin = match target_origin.0[..].as_ref() {
2385 "*" => None,
2386 "/" => Some(source_origin.clone()),
2387 url => match ServoUrl::parse(url) {
2388 Ok(url) => Some(url.origin()),
2389 Err(_) => return Err(Error::Syntax(None)),
2390 },
2391 };
2392
2393 self.post_message(target_origin, source_origin, &source.window_proxy(), data);
2395 Ok(())
2396 }
2397
2398 pub(crate) fn paint_worklet(&self) -> DomRoot<Worklet> {
2400 self.paint_worklet
2401 .or_init(|| self.new_paint_worklet(CanGc::note()))
2402 }
2403
2404 pub(crate) fn has_document(&self) -> bool {
2405 self.document.get().is_some()
2406 }
2407
2408 pub(crate) fn clear_js_runtime(&self) {
2409 self.as_global_scope()
2410 .remove_web_messaging_and_dedicated_workers_infra();
2411
2412 if let Some(custom_elements) = self.custom_element_registry.get() {
2415 custom_elements.teardown();
2416 }
2417
2418 self.current_state.set(WindowState::Zombie);
2419 *self.js_runtime.borrow_mut() = None;
2420
2421 if let Some(proxy) = self.window_proxy.get() {
2424 let pipeline_id = self.pipeline_id();
2425 if let Some(currently_active) = proxy.currently_active() {
2426 if currently_active == pipeline_id {
2427 self.window_proxy.set(None);
2428 }
2429 }
2430 }
2431
2432 if let Some(performance) = self.performance.get() {
2433 performance.clear_and_disable_performance_entry_buffer();
2434 }
2435 self.as_global_scope()
2436 .task_manager()
2437 .cancel_all_tasks_and_ignore_future_tasks();
2438 }
2439
2440 pub(crate) fn scroll(&self, x: f32, y: f32, behavior: ScrollBehavior) {
2442 let xfinite = if x.is_finite() { x } else { 0.0 };
2444 let yfinite = if y.is_finite() { y } else { 0.0 };
2445
2446 let viewport = self.viewport_details.get().size;
2456
2457 let scrolling_area = self.scrolling_area_query(None).to_f32();
2476 let x = xfinite.clamp(0.0, 0.0f32.max(scrolling_area.width() - viewport.width));
2477 let y = yfinite.clamp(0.0, 0.0f32.max(scrolling_area.height() - viewport.height));
2478
2479 let scroll_offset = self.scroll_offset();
2482 if x == scroll_offset.x && y == scroll_offset.y {
2483 return;
2484 }
2485
2486 self.perform_a_scroll(x, y, self.pipeline_id().root_scroll_id(), behavior, None);
2491 }
2492
2493 pub(crate) fn perform_a_scroll(
2495 &self,
2496 x: f32,
2497 y: f32,
2498 scroll_id: ExternalScrollId,
2499 _behavior: ScrollBehavior,
2500 element: Option<&Element>,
2501 ) {
2502 let (reflow_phases_run, _) =
2506 self.reflow(ReflowGoal::UpdateScrollNode(scroll_id, Vector2D::new(x, y)));
2507 if reflow_phases_run.needs_frame() {
2508 self.paint_api()
2509 .generate_frame(vec![self.webview_id().into()]);
2510 }
2511
2512 if reflow_phases_run.contains(ReflowPhasesRun::UpdatedScrollNodeOffset) {
2517 match element {
2518 Some(el) => self.Document().handle_element_scroll_event(el),
2519 None => self.Document().handle_viewport_scroll_event(),
2520 };
2521 }
2522 }
2523
2524 pub(crate) fn device_pixel_ratio(&self) -> Scale<f32, CSSPixel, DevicePixel> {
2525 self.viewport_details.get().hidpi_scale_factor
2526 }
2527
2528 fn client_window(&self) -> DeviceIndependentIntRect {
2529 let (sender, receiver) = generic_channel::channel().expect("Failed to create IPC channel!");
2530
2531 self.send_to_embedder(EmbedderMsg::GetWindowRect(self.webview_id(), sender));
2532
2533 receiver.recv().unwrap_or_default()
2534 }
2535
2536 pub(crate) fn advance_animation_clock(&self, delta_ms: i32) {
2539 self.Document()
2540 .advance_animation_timeline_for_testing(delta_ms as f64 / 1000.);
2541 ScriptThread::handle_tick_all_animations_for_testing(self.pipeline_id());
2542 }
2543
2544 pub(crate) fn reflow(&self, reflow_goal: ReflowGoal) -> (ReflowPhasesRun, ReflowStatistics) {
2552 let document = self.Document();
2553
2554 if !document.is_fully_active() {
2556 return Default::default();
2557 }
2558
2559 self.Document().ensure_safe_to_run_script_or_layout();
2560
2561 let pipeline_id = self.pipeline_id();
2565 if reflow_goal == ReflowGoal::UpdateTheRendering &&
2566 self.layout_blocker.get().layout_blocked()
2567 {
2568 debug!("Suppressing pre-load-event reflow pipeline {pipeline_id}");
2569 return Default::default();
2570 }
2571
2572 debug!("script: performing reflow for goal {reflow_goal:?}");
2573 let marker = if self.need_emit_timeline_marker(TimelineMarkerType::Reflow) {
2574 Some(TimelineMarker::start("Reflow".to_owned()))
2575 } else {
2576 None
2577 };
2578
2579 let restyle_reason = document.restyle_reason();
2580 document.clear_restyle_reasons();
2581 let restyle = if restyle_reason.needs_restyle() {
2582 debug!("Invalidating layout cache due to reflow condition {restyle_reason:?}",);
2583 self.layout_marker.borrow().set(false);
2585 *self.layout_marker.borrow_mut() = Rc::new(Cell::new(true));
2587
2588 let stylesheets_changed = document.flush_stylesheets_for_reflow();
2589 let pending_restyles = document.drain_pending_restyles();
2590 let dirty_root = document
2591 .take_dirty_root()
2592 .filter(|_| !stylesheets_changed)
2593 .or_else(|| document.GetDocumentElement())
2594 .map(|root| root.upcast::<Node>().to_trusted_node_address());
2595
2596 Some(ReflowRequestRestyle {
2597 reason: restyle_reason,
2598 dirty_root,
2599 stylesheets_changed,
2600 pending_restyles,
2601 })
2602 } else {
2603 None
2604 };
2605
2606 let document_context = self.web_font_context();
2607
2608 let reflow = ReflowRequest {
2610 document: document.upcast::<Node>().to_trusted_node_address(),
2611 epoch: document.current_rendering_epoch(),
2612 restyle,
2613 viewport_details: self.viewport_details.get(),
2614 origin: self.origin().immutable().clone(),
2615 reflow_goal,
2616 dom_count: document.dom_count(),
2617 animation_timeline_value: document.current_animation_timeline_value(),
2618 animations: document.animations().sets.clone(),
2619 animating_images: document.image_animation_manager().animating_images(),
2620 highlighted_dom_node: document.highlighted_dom_node().map(|node| node.to_opaque()),
2621 document_context,
2622 };
2623
2624 let Some(reflow_result) = self.layout.borrow_mut().reflow(reflow) else {
2625 return Default::default();
2626 };
2627
2628 debug!("script: layout complete");
2629 if let Some(marker) = marker {
2630 self.emit_timeline_marker(marker.end());
2631 }
2632
2633 self.handle_pending_images_post_reflow(
2634 reflow_result.pending_images,
2635 reflow_result.pending_rasterization_images,
2636 reflow_result.pending_svg_elements_for_serialization,
2637 );
2638
2639 if let Some(iframe_sizes) = reflow_result.iframe_sizes {
2640 document
2641 .iframes_mut()
2642 .handle_new_iframe_sizes_after_layout(self, iframe_sizes);
2643 }
2644
2645 document.update_animations_post_reflow();
2646
2647 (
2648 reflow_result.reflow_phases_run,
2649 reflow_result.reflow_statistics,
2650 )
2651 }
2652
2653 pub(crate) fn request_screenshot_readiness(&self, can_gc: CanGc) {
2654 self.has_pending_screenshot_readiness_request.set(true);
2655 self.maybe_resolve_pending_screenshot_readiness_requests(can_gc);
2656 }
2657
2658 pub(crate) fn maybe_resolve_pending_screenshot_readiness_requests(&self, can_gc: CanGc) {
2659 let pending_request = self.has_pending_screenshot_readiness_request.get();
2660 if !pending_request {
2661 return;
2662 }
2663
2664 let document = self.Document();
2665 if document.ReadyState() != DocumentReadyState::Complete {
2666 return;
2667 }
2668
2669 if document.render_blocking_element_count() > 0 {
2670 return;
2671 }
2672
2673 if document.GetDocumentElement().is_some_and(|elem| {
2677 elem.has_class(&atom!("reftest-wait"), CaseSensitivity::CaseSensitive) ||
2678 elem.has_class(&Atom::from("test-wait"), CaseSensitivity::CaseSensitive)
2679 }) {
2680 return;
2681 }
2682
2683 if self.font_context().web_fonts_still_loading() != 0 {
2684 return;
2685 }
2686
2687 if self.Document().Fonts(can_gc).waiting_to_fullfill_promise() {
2688 return;
2689 }
2690
2691 if !self.pending_layout_images.borrow().is_empty() ||
2692 !self.pending_images_for_rasterization.borrow().is_empty()
2693 {
2694 return;
2695 }
2696
2697 let document = self.Document();
2698 if document.needs_rendering_update() {
2699 return;
2700 }
2701
2702 let epoch = document.current_rendering_epoch();
2705 let pipeline_id = self.pipeline_id();
2706 debug!("Ready to take screenshot of {pipeline_id:?} at epoch={epoch:?}");
2707
2708 self.send_to_constellation(
2709 ScriptToConstellationMessage::RespondToScreenshotReadinessRequest(
2710 ScreenshotReadinessResponse::Ready(epoch),
2711 ),
2712 );
2713 self.has_pending_screenshot_readiness_request.set(false);
2714 }
2715
2716 pub(crate) fn reflow_if_reflow_timer_expired(&self) {
2719 if !matches!(
2722 self.layout_blocker.get(),
2723 LayoutBlocker::Parsing(instant) if instant + INITIAL_REFLOW_DELAY < Instant::now()
2724 ) {
2725 return;
2726 }
2727 self.allow_layout_if_necessary();
2728 }
2729
2730 pub(crate) fn prevent_layout_until_load_event(&self) {
2734 if !matches!(self.layout_blocker.get(), LayoutBlocker::WaitingForParse) {
2737 return;
2738 }
2739
2740 self.layout_blocker
2741 .set(LayoutBlocker::Parsing(Instant::now()));
2742 }
2743
2744 pub(crate) fn allow_layout_if_necessary(&self) {
2747 if matches!(
2748 self.layout_blocker.get(),
2749 LayoutBlocker::FiredLoadEventOrParsingTimerExpired
2750 ) {
2751 return;
2752 }
2753
2754 self.layout_blocker
2755 .set(LayoutBlocker::FiredLoadEventOrParsingTimerExpired);
2756
2757 let document = self.Document();
2769 if !document.is_render_blocked() && document.update_the_rendering().0.needs_frame() {
2770 self.paint_api()
2771 .generate_frame(vec![self.webview_id().into()]);
2772 }
2773 }
2774
2775 pub(crate) fn layout_blocked(&self) -> bool {
2776 self.layout_blocker.get().layout_blocked()
2777 }
2778
2779 pub(crate) fn layout_reflow(&self, query_msg: QueryMsg) {
2781 self.reflow(ReflowGoal::LayoutQuery(query_msg));
2782 }
2783
2784 pub(crate) fn resolved_font_style_query(
2785 &self,
2786 node: &Node,
2787 value: String,
2788 ) -> Option<ServoArc<Font>> {
2789 self.layout_reflow(QueryMsg::ResolvedFontStyleQuery);
2790
2791 let document = self.Document();
2792 let animations = document.animations().sets.clone();
2793 self.layout.borrow().query_resolved_font_style(
2794 node.to_trusted_node_address(),
2795 &value,
2796 animations,
2797 document.current_animation_timeline_value(),
2798 )
2799 }
2800
2801 pub(crate) fn padding_query_without_reflow(&self, node: &Node) -> Option<PhysicalSides> {
2806 let layout = self.layout.borrow();
2807 layout.query_padding(node.to_trusted_node_address())
2808 }
2809
2810 pub(crate) fn box_area_query_without_reflow(
2815 &self,
2816 node: &Node,
2817 area: BoxAreaType,
2818 exclude_transform_and_inline: bool,
2819 ) -> Option<Rect<Au, CSSPixel>> {
2820 let layout = self.layout.borrow();
2821 layout.ensure_stacking_context_tree(self.viewport_details.get());
2822 layout.query_box_area(
2823 node.to_trusted_node_address(),
2824 area,
2825 exclude_transform_and_inline,
2826 )
2827 }
2828
2829 pub(crate) fn box_area_query(
2830 &self,
2831 node: &Node,
2832 area: BoxAreaType,
2833 exclude_transform_and_inline: bool,
2834 ) -> Option<Rect<Au, CSSPixel>> {
2835 self.layout_reflow(QueryMsg::BoxArea);
2836 self.box_area_query_without_reflow(node, area, exclude_transform_and_inline)
2837 }
2838
2839 pub(crate) fn box_areas_query(&self, node: &Node, area: BoxAreaType) -> CSSPixelRectIterator {
2840 self.layout_reflow(QueryMsg::BoxAreas);
2841 self.layout
2842 .borrow()
2843 .query_box_areas(node.to_trusted_node_address(), area)
2844 }
2845
2846 pub(crate) fn client_rect_query(&self, node: &Node) -> Rect<i32, CSSPixel> {
2847 self.layout_reflow(QueryMsg::ClientRectQuery);
2848 self.layout
2849 .borrow()
2850 .query_client_rect(node.to_trusted_node_address())
2851 }
2852
2853 pub(crate) fn current_css_zoom_query(&self, node: &Node) -> f32 {
2854 self.layout_reflow(QueryMsg::CurrentCSSZoomQuery);
2855 self.layout
2856 .borrow()
2857 .query_current_css_zoom(node.to_trusted_node_address())
2858 }
2859
2860 pub(crate) fn scrolling_area_query(&self, node: Option<&Node>) -> Rect<i32, CSSPixel> {
2863 self.layout_reflow(QueryMsg::ScrollingAreaOrOffsetQuery);
2864 self.layout
2865 .borrow()
2866 .query_scrolling_area(node.map(Node::to_trusted_node_address))
2867 }
2868
2869 pub(crate) fn scroll_offset_query(&self, node: &Node) -> Vector2D<f32, LayoutPixel> {
2870 let external_scroll_id = ExternalScrollId(
2871 combine_id_with_fragment_type(node.to_opaque().id(), FragmentType::FragmentBody),
2872 self.pipeline_id().into(),
2873 );
2874 self.scroll_offset_query_with_external_scroll_id(external_scroll_id)
2875 }
2876
2877 fn scroll_offset_query_with_external_scroll_id(
2878 &self,
2879 external_scroll_id: ExternalScrollId,
2880 ) -> Vector2D<f32, LayoutPixel> {
2881 self.layout_reflow(QueryMsg::ScrollingAreaOrOffsetQuery);
2882 self.scroll_offset_query_with_external_scroll_id_no_reflow(external_scroll_id)
2883 }
2884
2885 fn scroll_offset_query_with_external_scroll_id_no_reflow(
2886 &self,
2887 external_scroll_id: ExternalScrollId,
2888 ) -> Vector2D<f32, LayoutPixel> {
2889 self.layout
2890 .borrow()
2891 .scroll_offset(external_scroll_id)
2892 .unwrap_or_default()
2893 }
2894
2895 pub(crate) fn scroll_an_element(
2898 &self,
2899 element: &Element,
2900 x: f32,
2901 y: f32,
2902 behavior: ScrollBehavior,
2903 ) {
2904 let scroll_id = ExternalScrollId(
2905 combine_id_with_fragment_type(
2906 element.upcast::<Node>().to_opaque().id(),
2907 FragmentType::FragmentBody,
2908 ),
2909 self.pipeline_id().into(),
2910 );
2911
2912 self.perform_a_scroll(x, y, scroll_id, behavior, Some(element));
2916 }
2917
2918 pub(crate) fn resolved_style_query(
2919 &self,
2920 element: TrustedNodeAddress,
2921 pseudo: Option<PseudoElement>,
2922 property: PropertyId,
2923 ) -> DOMString {
2924 self.layout_reflow(QueryMsg::ResolvedStyleQuery);
2925
2926 let document = self.Document();
2927 let animations = document.animations().sets.clone();
2928 DOMString::from(self.layout.borrow().query_resolved_style(
2929 element,
2930 pseudo,
2931 property,
2932 animations,
2933 document.current_animation_timeline_value(),
2934 ))
2935 }
2936
2937 pub(crate) fn get_iframe_viewport_details_if_known(
2941 &self,
2942 browsing_context_id: BrowsingContextId,
2943 ) -> Option<ViewportDetails> {
2944 self.layout_reflow(QueryMsg::InnerWindowDimensionsQuery);
2946 self.Document()
2947 .iframes()
2948 .get(browsing_context_id)
2949 .and_then(|iframe| iframe.size)
2950 }
2951
2952 #[expect(unsafe_code)]
2953 pub(crate) fn offset_parent_query(
2954 &self,
2955 node: &Node,
2956 ) -> (Option<DomRoot<Element>>, Rect<Au, CSSPixel>) {
2957 self.layout_reflow(QueryMsg::OffsetParentQuery);
2958 let response = self
2959 .layout
2960 .borrow()
2961 .query_offset_parent(node.to_trusted_node_address());
2962 let element = response.node_address.and_then(|parent_node_address| {
2963 let node = unsafe { from_untrusted_node_address(parent_node_address) };
2964 DomRoot::downcast(node)
2965 });
2966 (element, response.rect)
2967 }
2968
2969 pub(crate) fn scroll_container_query(
2970 &self,
2971 node: Option<&Node>,
2972 flags: ScrollContainerQueryFlags,
2973 ) -> Option<ScrollContainerResponse> {
2974 self.layout_reflow(QueryMsg::ScrollParentQuery);
2975 self.layout
2976 .borrow()
2977 .query_scroll_container(node.map(Node::to_trusted_node_address), flags)
2978 }
2979
2980 #[expect(unsafe_code)]
2981 pub(crate) fn scrolling_box_query(
2982 &self,
2983 node: Option<&Node>,
2984 flags: ScrollContainerQueryFlags,
2985 ) -> Option<ScrollingBox> {
2986 self.scroll_container_query(node, flags)
2987 .and_then(|response| {
2988 Some(match response {
2989 ScrollContainerResponse::Viewport(overflow) => {
2990 (ScrollingBoxSource::Viewport(self.Document()), overflow)
2991 },
2992 ScrollContainerResponse::Element(parent_node_address, overflow) => {
2993 let node = unsafe { from_untrusted_node_address(parent_node_address) };
2994 (
2995 ScrollingBoxSource::Element(DomRoot::downcast(node)?),
2996 overflow,
2997 )
2998 },
2999 })
3000 })
3001 .map(|(source, overflow)| ScrollingBox::new(source, overflow))
3002 }
3003
3004 pub(crate) fn text_index_query_on_node_for_event(
3005 &self,
3006 node: &Node,
3007 mouse_event: &MouseEvent,
3008 ) -> Option<usize> {
3009 let point_in_viewport = mouse_event.point_in_viewport()?.map(Au::from_f32_px);
3013
3014 self.layout_reflow(QueryMsg::TextIndexQuery);
3015 self.layout
3016 .borrow()
3017 .query_text_index(node.to_trusted_node_address(), point_in_viewport)
3018 }
3019
3020 pub(crate) fn elements_from_point_query(
3021 &self,
3022 point: LayoutPoint,
3023 flags: ElementsFromPointFlags,
3024 ) -> Vec<ElementsFromPointResult> {
3025 self.layout_reflow(QueryMsg::ElementsFromPoint);
3026 self.layout().query_elements_from_point(point, flags)
3027 }
3028
3029 pub(crate) fn query_effective_overflow(&self, node: &Node) -> Option<AxesOverflow> {
3030 self.layout_reflow(QueryMsg::EffectiveOverflow);
3031 self.query_effective_overflow_without_reflow(node)
3032 }
3033
3034 pub(crate) fn query_effective_overflow_without_reflow(
3035 &self,
3036 node: &Node,
3037 ) -> Option<AxesOverflow> {
3038 self.layout
3039 .borrow()
3040 .query_effective_overflow(node.to_trusted_node_address())
3041 }
3042
3043 pub(crate) fn hit_test_from_input_event(
3044 &self,
3045 input_event: &ConstellationInputEvent,
3046 ) -> Option<HitTestResult> {
3047 self.hit_test_from_point_in_viewport(
3048 input_event.hit_test_result.as_ref()?.point_in_viewport,
3049 )
3050 }
3051
3052 #[expect(unsafe_code)]
3053 pub(crate) fn hit_test_from_point_in_viewport(
3054 &self,
3055 point_in_frame: Point2D<f32, CSSPixel>,
3056 ) -> Option<HitTestResult> {
3057 let result = self
3058 .elements_from_point_query(point_in_frame.cast_unit(), ElementsFromPointFlags::empty())
3059 .into_iter()
3060 .nth(0)?;
3061
3062 let point_relative_to_initial_containing_block =
3063 point_in_frame + self.scroll_offset().cast_unit();
3064
3065 let address = UntrustedNodeAddress(result.node.0 as *const c_void);
3068 Some(HitTestResult {
3069 node: unsafe { from_untrusted_node_address(address) },
3070 cursor: result.cursor,
3071 point_in_node: result.point_in_target,
3072 point_in_frame,
3073 point_relative_to_initial_containing_block,
3074 })
3075 }
3076
3077 pub(crate) fn init_window_proxy(&self, window_proxy: &WindowProxy) {
3078 assert!(self.window_proxy.get().is_none());
3079 self.window_proxy.set(Some(window_proxy));
3080 }
3081
3082 pub(crate) fn init_document(&self, document: &Document) {
3083 assert!(self.document.get().is_none());
3084 assert!(document.window() == self);
3085 self.document.set(Some(document));
3086
3087 if self.unminify_css {
3088 *self.unminified_css_dir.borrow_mut() = Some(unminified_path("unminified-css"));
3089 }
3090 }
3091
3092 fn navigate_to_fragment(&self, url: &ServoUrl, history_handling: NavigationHistoryBehavior) {
3094 let doc = self.Document();
3095 self.send_to_constellation(ScriptToConstellationMessage::NavigatedToFragment(
3118 url.clone(),
3119 history_handling,
3120 ));
3121 let old_url = doc.url();
3123 doc.set_url(url.clone());
3124 doc.update_document_for_history_step_application(&old_url, url);
3127 let Some(fragment) = url.fragment() else {
3129 unreachable!("Must always have a fragment");
3130 };
3131 doc.scroll_to_the_fragment(fragment);
3132 }
3137
3138 pub(crate) fn load_data_for_document(
3139 &self,
3140 url: ServoUrl,
3141 pipeline_id: PipelineId,
3142 ) -> LoadData {
3143 let source_document = self.Document();
3144 let secure_context = if self.is_top_level() {
3145 None
3146 } else {
3147 Some(self.IsSecureContext())
3148 };
3149 LoadData::new(
3150 LoadOrigin::Script(self.origin().snapshot()),
3151 url,
3152 source_document.about_base_url(),
3153 Some(pipeline_id),
3154 Referrer::ReferrerUrl(source_document.url()),
3155 source_document.get_referrer_policy(),
3156 secure_context,
3157 Some(source_document.insecure_requests_policy()),
3158 source_document.has_trustworthy_ancestor_origin(),
3159 source_document.creation_sandboxing_flag_set_considering_parent_iframe(),
3160 )
3161 }
3162
3163 pub(crate) fn load_url(
3165 &self,
3166 history_handling: NavigationHistoryBehavior,
3167 force_reload: bool,
3168 load_data: LoadData,
3169 can_gc: CanGc,
3170 ) {
3171 let doc = self.Document();
3172
3173 let initiator_origin_snapshot = &load_data.load_origin;
3175
3176 let pipeline_id = self.pipeline_id();
3181 let window_proxy = self.window_proxy();
3182 if let Some(active) = window_proxy.currently_active() {
3183 if pipeline_id == active && doc.is_prompting_or_unloading() {
3184 return;
3185 }
3186 }
3187
3188 if doc.check_if_unloading_is_cancelled(false, can_gc) {
3191 let history_handling = if history_handling == NavigationHistoryBehavior::Auto {
3193 if let LoadOrigin::Script(initiator_origin) = initiator_origin_snapshot {
3200 if load_data.url == doc.url() && initiator_origin.same_origin(doc.origin()) {
3201 NavigationHistoryBehavior::Replace
3202 } else {
3203 NavigationHistoryBehavior::Push
3205 }
3206 } else {
3207 NavigationHistoryBehavior::Push
3209 }
3210 } else {
3211 history_handling
3212 };
3213 let history_handling =
3218 if load_data.url.scheme() == "javascript" || doc.is_initial_about_blank() {
3219 NavigationHistoryBehavior::Replace
3220 } else {
3221 history_handling
3222 };
3223
3224 if !force_reload
3228 && load_data.url.as_url()[..Position::AfterQuery] ==
3230 doc.url().as_url()[..Position::AfterQuery]
3231 && load_data.url.fragment().is_some()
3233 {
3234 let webdriver_sender = self.webdriver_load_status_sender.borrow().clone();
3237 if let Some(ref sender) = webdriver_sender {
3238 let _ = sender.send(WebDriverLoadStatus::NavigationStart);
3239 }
3240 self.navigate_to_fragment(&load_data.url, history_handling);
3241 if let Some(sender) = webdriver_sender {
3243 let _ = sender.send(WebDriverLoadStatus::NavigationStop);
3244 }
3245 return;
3246 }
3247
3248 let window_proxy = self.window_proxy();
3250 if window_proxy.parent().is_some() {
3251 window_proxy.start_delaying_load_events_mode();
3252 }
3253
3254 if let Some(sender) = self.webdriver_load_status_sender.borrow().as_ref() {
3255 let _ = sender.send(WebDriverLoadStatus::NavigationStart);
3256 }
3257
3258 ScriptThread::navigate(self.webview_id, pipeline_id, load_data, history_handling);
3260 };
3261 }
3262
3263 pub(crate) fn set_viewport_details(&self, viewport_details: ViewportDetails) {
3266 self.viewport_details.set(viewport_details);
3267 if !self.layout_mut().set_viewport_details(viewport_details) {
3268 return;
3269 }
3270 self.Document()
3271 .add_restyle_reason(RestyleReason::ViewportChanged);
3272 }
3273
3274 pub(crate) fn viewport_details(&self) -> ViewportDetails {
3275 self.viewport_details.get()
3276 }
3277
3278 pub(crate) fn get_or_init_visual_viewport(&self, can_gc: CanGc) -> DomRoot<VisualViewport> {
3279 self.visual_viewport.or_init(|| {
3280 VisualViewport::new_from_layout_viewport(self, self.viewport_details().size, can_gc)
3281 })
3282 }
3283
3284 pub(crate) fn maybe_update_visual_viewport(
3286 &self,
3287 pinch_zoom_infos: PinchZoomInfos,
3288 can_gc: CanGc,
3289 ) {
3290 if pinch_zoom_infos.rect == Rect::from_size(self.viewport_details().size) &&
3293 self.visual_viewport.get().is_none()
3294 {
3295 return;
3296 }
3297
3298 let visual_viewport = self.get_or_init_visual_viewport(can_gc);
3299 let changes = visual_viewport.update_from_pinch_zoom_infos(pinch_zoom_infos);
3300
3301 if changes.intersects(VisualViewportChanges::DimensionChanged) {
3302 self.has_changed_visual_viewport_dimension.set(true);
3303 }
3304 if changes.intersects(VisualViewportChanges::OffsetChanged) {
3305 self.Document()
3306 .handle_visual_viewport_scroll_event(&visual_viewport);
3307 }
3308 }
3309
3310 pub(crate) fn theme(&self) -> Theme {
3312 self.theme.get()
3313 }
3314
3315 pub(crate) fn set_theme(&self, new_theme: Theme) {
3317 self.theme.set(new_theme);
3318 if !self.layout_mut().set_theme(new_theme) {
3319 return;
3320 }
3321 self.Document()
3322 .add_restyle_reason(RestyleReason::ThemeChanged);
3323 }
3324
3325 pub(crate) fn get_url(&self) -> ServoUrl {
3326 self.Document().url()
3327 }
3328
3329 pub(crate) fn windowproxy_handler(&self) -> &'static WindowProxyHandler {
3330 self.dom_static.windowproxy_handler
3331 }
3332
3333 pub(crate) fn add_resize_event(&self, event: ViewportDetails, event_type: WindowSizeType) {
3334 *self.unhandled_resize_event.borrow_mut() = Some((event, event_type))
3337 }
3338
3339 pub(crate) fn take_unhandled_resize_event(&self) -> Option<(ViewportDetails, WindowSizeType)> {
3340 self.unhandled_resize_event.borrow_mut().take()
3341 }
3342
3343 pub(crate) fn has_unhandled_resize_event(&self) -> bool {
3345 self.unhandled_resize_event.borrow().is_some()
3346 }
3347
3348 pub(crate) fn suspend(&self, cx: &mut js::context::JSContext) {
3349 self.as_global_scope().suspend();
3351
3352 if self.window_proxy().currently_active() == Some(self.global().pipeline_id()) {
3354 self.window_proxy().unset_currently_active(cx);
3355 }
3356
3357 self.gc();
3362 }
3363
3364 pub(crate) fn resume(&self, can_gc: CanGc) {
3365 self.as_global_scope().resume();
3367
3368 self.window_proxy().set_currently_active(self, can_gc);
3370
3371 self.Document().title_changed();
3374 }
3375
3376 pub(crate) fn need_emit_timeline_marker(&self, timeline_type: TimelineMarkerType) -> bool {
3377 let markers = self.devtools_markers.borrow();
3378 markers.contains(&timeline_type)
3379 }
3380
3381 pub(crate) fn emit_timeline_marker(&self, marker: TimelineMarker) {
3382 let sender = self.devtools_marker_sender.borrow();
3383 let sender = sender.as_ref().expect("There is no marker sender");
3384 sender.send(Some(marker)).unwrap();
3385 }
3386
3387 pub(crate) fn set_devtools_timeline_markers(
3388 &self,
3389 markers: Vec<TimelineMarkerType>,
3390 reply: GenericSender<Option<TimelineMarker>>,
3391 ) {
3392 *self.devtools_marker_sender.borrow_mut() = Some(reply);
3393 self.devtools_markers.borrow_mut().extend(markers);
3394 }
3395
3396 pub(crate) fn drop_devtools_timeline_markers(&self, markers: Vec<TimelineMarkerType>) {
3397 let mut devtools_markers = self.devtools_markers.borrow_mut();
3398 for marker in markers {
3399 devtools_markers.remove(&marker);
3400 }
3401 if devtools_markers.is_empty() {
3402 *self.devtools_marker_sender.borrow_mut() = None;
3403 }
3404 }
3405
3406 pub(crate) fn set_webdriver_script_chan(&self, chan: Option<GenericSender<WebDriverJSResult>>) {
3407 *self.webdriver_script_chan.borrow_mut() = chan;
3408 }
3409
3410 pub(crate) fn set_webdriver_load_status_sender(
3411 &self,
3412 sender: Option<GenericSender<WebDriverLoadStatus>>,
3413 ) {
3414 *self.webdriver_load_status_sender.borrow_mut() = sender;
3415 }
3416
3417 pub(crate) fn is_alive(&self) -> bool {
3418 self.current_state.get() == WindowState::Alive
3419 }
3420
3421 pub(crate) fn is_top_level(&self) -> bool {
3423 self.parent_info.is_none()
3424 }
3425
3426 fn run_resize_steps_for_layout_viewport(&self, can_gc: CanGc) -> bool {
3431 let Some((new_size, size_type)) = self.take_unhandled_resize_event() else {
3432 return false;
3433 };
3434
3435 if self.viewport_details() == new_size {
3436 return false;
3437 }
3438
3439 let _realm = enter_realm(self);
3440 debug!(
3441 "Resizing Window for pipeline {:?} from {:?} to {new_size:?}",
3442 self.pipeline_id(),
3443 self.viewport_details(),
3444 );
3445 self.set_viewport_details(new_size);
3446
3447 self.Document()
3451 .add_restyle_reason(RestyleReason::ViewportChanged);
3452
3453 if self.layout().device().used_viewport_size() {
3456 self.Document().dirty_all_nodes();
3457 }
3458
3459 if size_type == WindowSizeType::Resize {
3461 let uievent = UIEvent::new(
3462 self,
3463 atom!("resize"),
3464 EventBubbles::DoesNotBubble,
3465 EventCancelable::NotCancelable,
3466 Some(self),
3467 0i32,
3468 0u32,
3469 can_gc,
3470 );
3471 uievent.upcast::<Event>().fire(self.upcast(), can_gc);
3472 }
3473
3474 true
3475 }
3476
3477 pub(crate) fn run_the_resize_steps(&self, can_gc: CanGc) -> bool {
3482 let layout_viewport_resized = self.run_resize_steps_for_layout_viewport(can_gc);
3483
3484 if self.has_changed_visual_viewport_dimension.get() {
3485 let visual_viewport = self.get_or_init_visual_viewport(can_gc);
3486
3487 let uievent = UIEvent::new(
3488 self,
3489 atom!("resize"),
3490 EventBubbles::DoesNotBubble,
3491 EventCancelable::NotCancelable,
3492 Some(self),
3493 0i32,
3494 0u32,
3495 can_gc,
3496 );
3497 uievent
3498 .upcast::<Event>()
3499 .fire(visual_viewport.upcast(), can_gc);
3500
3501 self.has_changed_visual_viewport_dimension.set(false);
3502 }
3503
3504 layout_viewport_resized
3505 }
3506
3507 pub(crate) fn evaluate_media_queries_and_report_changes(&self, can_gc: CanGc) {
3510 let _realm = enter_realm(self);
3511
3512 rooted_vec!(let mut mql_list);
3513 self.media_query_lists.for_each(|mql| {
3514 if let MediaQueryListMatchState::Changed = mql.evaluate_changes() {
3515 mql_list.push(Dom::from_ref(&*mql));
3517 }
3518 });
3519 for mql in mql_list.iter() {
3521 let event = MediaQueryListEvent::new(
3522 &mql.global(),
3523 atom!("change"),
3524 false,
3525 false,
3526 mql.Media(),
3527 mql.Matches(),
3528 can_gc,
3529 );
3530 event
3531 .upcast::<Event>()
3532 .fire(mql.upcast::<EventTarget>(), can_gc);
3533 }
3534 }
3535
3536 pub(crate) fn set_throttled(&self, throttled: bool) {
3538 self.throttled.set(throttled);
3539 if throttled {
3540 self.as_global_scope().slow_down_timers();
3541 } else {
3542 self.as_global_scope().speed_up_timers();
3543 }
3544 }
3545
3546 pub(crate) fn throttled(&self) -> bool {
3547 self.throttled.get()
3548 }
3549
3550 pub(crate) fn unminified_css_dir(&self) -> Option<String> {
3551 self.unminified_css_dir.borrow().clone()
3552 }
3553
3554 pub(crate) fn local_script_source(&self) -> &Option<String> {
3555 &self.local_script_source
3556 }
3557
3558 pub(crate) fn set_navigation_start(&self) {
3559 self.navigation_start.set(CrossProcessInstant::now());
3560 }
3561
3562 pub(crate) fn set_last_activation_timestamp(&self, time: UserActivationTimestamp) {
3563 self.last_activation_timestamp.set(time);
3564 }
3565
3566 pub(crate) fn send_to_embedder(&self, msg: EmbedderMsg) {
3567 self.as_global_scope()
3568 .script_to_embedder_chan()
3569 .send(msg)
3570 .unwrap();
3571 }
3572
3573 pub(crate) fn send_to_constellation(&self, msg: ScriptToConstellationMessage) {
3574 self.as_global_scope()
3575 .script_to_constellation_chan()
3576 .send(msg)
3577 .unwrap();
3578 }
3579
3580 #[cfg(feature = "webxr")]
3581 pub(crate) fn in_immersive_xr_session(&self) -> bool {
3582 self.navigator
3583 .get()
3584 .as_ref()
3585 .and_then(|nav| nav.xr())
3586 .is_some_and(|xr| xr.pending_or_active_session())
3587 }
3588
3589 #[cfg(not(feature = "webxr"))]
3590 pub(crate) fn in_immersive_xr_session(&self) -> bool {
3591 false
3592 }
3593
3594 #[expect(unsafe_code)]
3595 fn handle_pending_images_post_reflow(
3596 &self,
3597 pending_images: Vec<PendingImage>,
3598 pending_rasterization_images: Vec<PendingRasterizationImage>,
3599 pending_svg_element_for_serialization: Vec<UntrustedNodeAddress>,
3600 ) {
3601 let pipeline_id = self.pipeline_id();
3602 for image in pending_images {
3603 let id = image.id;
3604 let node = unsafe { from_untrusted_node_address(image.node) };
3605
3606 if let PendingImageState::Unrequested(ref url) = image.state {
3607 fetch_image_for_layout(url.clone(), &node, id, self.image_cache.clone());
3608 }
3609
3610 let mut images = self.pending_layout_images.borrow_mut();
3611 if !images.contains_key(&id) {
3612 let trusted_node = Trusted::new(&*node);
3613 let sender = self.register_image_cache_listener(id, move |response, _| {
3614 trusted_node
3615 .root()
3616 .owner_window()
3617 .pending_layout_image_notification(response);
3618 });
3619
3620 self.image_cache
3621 .add_listener(ImageLoadListener::new(sender, pipeline_id, id));
3622 }
3623
3624 let nodes = images.entry(id).or_default();
3625 if !nodes.iter().any(|n| std::ptr::eq(&*(n.node), &*node)) {
3626 nodes.push(PendingLayoutImageAncillaryData {
3627 node: Dom::from_ref(&*node),
3628 destination: image.destination,
3629 });
3630 }
3631 }
3632
3633 for image in pending_rasterization_images {
3634 let node = unsafe { from_untrusted_node_address(image.node) };
3635
3636 let mut images = self.pending_images_for_rasterization.borrow_mut();
3637 if !images.contains_key(&(image.id, image.size)) {
3638 let image_cache_sender = self.image_cache_sender.clone();
3639 self.image_cache.add_rasterization_complete_listener(
3640 pipeline_id,
3641 image.id,
3642 image.size,
3643 Box::new(move |response| {
3644 let _ = image_cache_sender.send(response);
3645 }),
3646 );
3647 }
3648
3649 let nodes = images.entry((image.id, image.size)).or_default();
3650 if !nodes.iter().any(|n| std::ptr::eq(&**n, &*node)) {
3651 nodes.push(Dom::from_ref(&*node));
3652 }
3653 }
3654
3655 for node in pending_svg_element_for_serialization.into_iter() {
3656 let node = unsafe { from_untrusted_node_address(node) };
3657 let svg = node.downcast::<SVGSVGElement>().unwrap();
3658 svg.serialize_and_cache_subtree();
3659 node.dirty(NodeDamage::Other);
3660 }
3661 }
3662
3663 pub(crate) fn has_sticky_activation(&self) -> bool {
3665 UserActivationTimestamp::TimeStamp(CrossProcessInstant::now()) >=
3667 self.last_activation_timestamp.get()
3668 }
3669
3670 pub(crate) fn has_transient_activation(&self) -> bool {
3672 let current_time = CrossProcessInstant::now();
3675 UserActivationTimestamp::TimeStamp(current_time) >= self.last_activation_timestamp.get() &&
3676 UserActivationTimestamp::TimeStamp(current_time) <
3677 self.last_activation_timestamp.get() +
3678 pref!(dom_transient_activation_duration_ms)
3679 }
3680
3681 pub(crate) fn consume_last_activation_timestamp(&self) {
3682 if self.last_activation_timestamp.get() != UserActivationTimestamp::PositiveInfinity {
3683 self.set_last_activation_timestamp(UserActivationTimestamp::NegativeInfinity);
3684 }
3685 }
3686
3687 pub(crate) fn consume_user_activation(&self) {
3689 if self.undiscarded_window_proxy().is_none() {
3692 return;
3693 }
3694
3695 let Some(top_level_document) = self.top_level_document_if_local() else {
3699 return;
3700 };
3701
3702 top_level_document
3710 .window()
3711 .consume_last_activation_timestamp();
3712 for document in SameOriginDescendantNavigablesIterator::new(top_level_document) {
3713 document.window().consume_last_activation_timestamp();
3714 }
3715 }
3716
3717 #[allow(clippy::too_many_arguments)]
3718 pub(crate) fn new(
3719 cx: &mut js::context::JSContext,
3720 webview_id: WebViewId,
3721 runtime: Rc<Runtime>,
3722 script_chan: Sender<MainThreadScriptMsg>,
3723 layout: Box<dyn Layout>,
3724 font_context: Arc<FontContext>,
3725 image_cache_sender: Sender<ImageCacheResponseMessage>,
3726 image_cache: Arc<dyn ImageCache>,
3727 resource_threads: ResourceThreads,
3728 storage_threads: StorageThreads,
3729 #[cfg(feature = "bluetooth")] bluetooth_thread: GenericSender<BluetoothRequest>,
3730 mem_profiler_chan: MemProfilerChan,
3731 time_profiler_chan: TimeProfilerChan,
3732 devtools_chan: Option<GenericCallback<ScriptToDevtoolsControlMsg>>,
3733 constellation_chan: ScriptToConstellationChan,
3734 embedder_chan: ScriptToEmbedderChan,
3735 control_chan: GenericSender<ScriptThreadMessage>,
3736 pipeline_id: PipelineId,
3737 parent_info: Option<PipelineId>,
3738 viewport_details: ViewportDetails,
3739 origin: MutableOrigin,
3740 creation_url: ServoUrl,
3741 top_level_creation_url: ServoUrl,
3742 navigation_start: CrossProcessInstant,
3743 webgl_chan: Option<WebGLChan>,
3744 #[cfg(feature = "webxr")] webxr_registry: Option<webxr_api::Registry>,
3745 paint_api: CrossProcessPaintApi,
3746 unminify_js: bool,
3747 unminify_css: bool,
3748 local_script_source: Option<String>,
3749 user_scripts: Rc<Vec<UserScript>>,
3750 player_context: WindowGLContext,
3751 #[cfg(feature = "webgpu")] gpu_id_hub: Arc<IdentityHub>,
3752 inherited_secure_context: Option<bool>,
3753 theme: Theme,
3754 weak_script_thread: Weak<ScriptThread>,
3755 ) -> DomRoot<Self> {
3756 let error_reporter = CSSErrorReporter {
3757 pipelineid: pipeline_id,
3758 script_chan: control_chan,
3759 };
3760
3761 let win = Box::new(Self {
3762 webview_id,
3763 globalscope: GlobalScope::new_inherited(
3764 pipeline_id,
3765 devtools_chan,
3766 mem_profiler_chan,
3767 time_profiler_chan,
3768 constellation_chan,
3769 embedder_chan,
3770 resource_threads,
3771 storage_threads,
3772 origin,
3773 creation_url,
3774 Some(top_level_creation_url),
3775 #[cfg(feature = "webgpu")]
3776 gpu_id_hub,
3777 inherited_secure_context,
3778 unminify_js,
3779 Some(font_context),
3780 ),
3781 ongoing_navigation: Default::default(),
3782 script_chan,
3783 layout: RefCell::new(layout),
3784 image_cache_sender,
3785 image_cache,
3786 navigator: Default::default(),
3787 location: Default::default(),
3788 history: Default::default(),
3789 custom_element_registry: Default::default(),
3790 window_proxy: Default::default(),
3791 document: Default::default(),
3792 performance: Default::default(),
3793 navigation_start: Cell::new(navigation_start),
3794 screen: Default::default(),
3795 session_storage: Default::default(),
3796 local_storage: Default::default(),
3797 status: DomRefCell::new(DOMString::new()),
3798 parent_info,
3799 dom_static: GlobalStaticData::new(),
3800 js_runtime: DomRefCell::new(Some(runtime)),
3801 #[cfg(feature = "bluetooth")]
3802 bluetooth_thread,
3803 #[cfg(feature = "bluetooth")]
3804 bluetooth_extra_permission_data: BluetoothExtraPermissionData::new(),
3805 unhandled_resize_event: Default::default(),
3806 viewport_details: Cell::new(viewport_details),
3807 layout_blocker: Cell::new(LayoutBlocker::WaitingForParse),
3808 current_state: Cell::new(WindowState::Alive),
3809 devtools_marker_sender: Default::default(),
3810 devtools_markers: Default::default(),
3811 webdriver_script_chan: Default::default(),
3812 webdriver_load_status_sender: Default::default(),
3813 error_reporter,
3814 media_query_lists: DOMTracker::new(),
3815 #[cfg(feature = "bluetooth")]
3816 test_runner: Default::default(),
3817 webgl_chan,
3818 #[cfg(feature = "webxr")]
3819 webxr_registry,
3820 pending_image_callbacks: Default::default(),
3821 pending_layout_images: Default::default(),
3822 pending_images_for_rasterization: Default::default(),
3823 unminified_css_dir: Default::default(),
3824 local_script_source,
3825 test_worklet: Default::default(),
3826 paint_worklet: Default::default(),
3827 exists_mut_observer: Cell::new(false),
3828 paint_api,
3829 has_sent_idle_message: Cell::new(false),
3830 unminify_css,
3831 user_scripts,
3832 player_context,
3833 throttled: Cell::new(false),
3834 layout_marker: DomRefCell::new(Rc::new(Cell::new(true))),
3835 current_event: DomRefCell::new(None),
3836 theme: Cell::new(theme),
3837 trusted_types: Default::default(),
3838 reporting_observer_list: Default::default(),
3839 report_list: Default::default(),
3840 endpoints_list: Default::default(),
3841 script_window_proxies: ScriptThread::window_proxies(),
3842 has_pending_screenshot_readiness_request: Default::default(),
3843 visual_viewport: Default::default(),
3844 weak_script_thread,
3845 has_changed_visual_viewport_dimension: Default::default(),
3846 last_activation_timestamp: Cell::new(UserActivationTimestamp::PositiveInfinity),
3847 });
3848
3849 WindowBinding::Wrap::<crate::DomTypeHolder>(cx, win)
3850 }
3851
3852 pub(crate) fn pipeline_id(&self) -> PipelineId {
3853 self.as_global_scope().pipeline_id()
3854 }
3855
3856 pub(crate) fn cache_layout_value<T>(&self, value: T) -> LayoutValue<T>
3858 where
3859 T: Copy + MallocSizeOf,
3860 {
3861 LayoutValue::new(self.layout_marker.borrow().clone(), value)
3862 }
3863}
3864
3865#[derive(MallocSizeOf)]
3870pub(crate) struct LayoutValue<T: MallocSizeOf> {
3871 #[conditional_malloc_size_of]
3872 is_valid: Rc<Cell<bool>>,
3873 value: T,
3874}
3875
3876#[expect(unsafe_code)]
3877unsafe impl<T: JSTraceable + MallocSizeOf> JSTraceable for LayoutValue<T> {
3878 unsafe fn trace(&self, trc: *mut js::jsapi::JSTracer) {
3879 unsafe { self.value.trace(trc) };
3880 }
3881}
3882
3883impl<T: Copy + MallocSizeOf> LayoutValue<T> {
3884 fn new(marker: Rc<Cell<bool>>, value: T) -> Self {
3885 LayoutValue {
3886 is_valid: marker,
3887 value,
3888 }
3889 }
3890
3891 pub(crate) fn get(&self) -> Result<T, ()> {
3893 if self.is_valid.get() {
3894 return Ok(self.value);
3895 }
3896 Err(())
3897 }
3898}
3899
3900fn should_move_clip_rect(clip_rect: UntypedRect<Au>, new_viewport: UntypedRect<f32>) -> bool {
3901 let clip_rect = UntypedRect::new(
3902 Point2D::new(
3903 clip_rect.origin.x.to_f32_px(),
3904 clip_rect.origin.y.to_f32_px(),
3905 ),
3906 Size2D::new(
3907 clip_rect.size.width.to_f32_px(),
3908 clip_rect.size.height.to_f32_px(),
3909 ),
3910 );
3911
3912 static VIEWPORT_SCROLL_MARGIN_SIZE: f32 = 0.5;
3916 let viewport_scroll_margin = new_viewport.size * VIEWPORT_SCROLL_MARGIN_SIZE;
3917
3918 (clip_rect.origin.x - new_viewport.origin.x).abs() <= viewport_scroll_margin.width ||
3919 (clip_rect.max_x() - new_viewport.max_x()).abs() <= viewport_scroll_margin.width ||
3920 (clip_rect.origin.y - new_viewport.origin.y).abs() <= viewport_scroll_margin.height ||
3921 (clip_rect.max_y() - new_viewport.max_y()).abs() <= viewport_scroll_margin.height
3922}
3923
3924impl Window {
3925 pub(crate) fn post_message(
3927 &self,
3928 target_origin: Option<ImmutableOrigin>,
3929 source_origin: ImmutableOrigin,
3930 source: &WindowProxy,
3931 data: StructuredSerializedData,
3932 ) {
3933 let this = Trusted::new(self);
3934 let source = Trusted::new(source);
3935 let task = task!(post_serialised_message: move || {
3936 let this = this.root();
3937 let source = source.root();
3938 let document = this.Document();
3939
3940 if let Some(ref target_origin) = target_origin {
3942 if !target_origin.same_origin(document.origin()) {
3943 return;
3944 }
3945 }
3946
3947 let cx = this.get_cx();
3949 let obj = this.reflector().get_jsobject();
3950 let _ac = JSAutoRealm::new(*cx, obj.get());
3951 rooted!(in(*cx) let mut message_clone = UndefinedValue());
3952 if let Ok(ports) = structuredclone::read(this.upcast(), data, message_clone.handle_mut(), CanGc::note()) {
3953 MessageEvent::dispatch_jsval(
3955 this.upcast(),
3956 this.upcast(),
3957 message_clone.handle(),
3958 Some(&source_origin.ascii_serialization()),
3959 Some(&*source),
3960 ports,
3961 CanGc::note()
3962 );
3963 } else {
3964 MessageEvent::dispatch_error(
3966 this.upcast(),
3967 this.upcast(),
3968 CanGc::note()
3969 );
3970 }
3971 });
3972 self.as_global_scope()
3974 .task_manager()
3975 .dom_manipulation_task_source()
3976 .queue(task);
3977 }
3978}
3979
3980#[derive(Clone, MallocSizeOf)]
3981pub(crate) struct CSSErrorReporter {
3982 pub(crate) pipelineid: PipelineId,
3983 pub(crate) script_chan: GenericSender<ScriptThreadMessage>,
3984}
3985unsafe_no_jsmanaged_fields!(CSSErrorReporter);
3986
3987impl ParseErrorReporter for CSSErrorReporter {
3988 fn report_error(
3989 &self,
3990 url: &UrlExtraData,
3991 location: SourceLocation,
3992 error: ContextualParseError,
3993 ) {
3994 if log_enabled!(log::Level::Info) {
3995 info!(
3996 "Url:\t{}\n{}:{} {}",
3997 url.0.as_str(),
3998 location.line,
3999 location.column,
4000 error
4001 )
4002 }
4003
4004 let _ = self.script_chan.send(ScriptThreadMessage::ReportCSSError(
4006 self.pipelineid,
4007 url.0.to_string(),
4008 location.line,
4009 location.column,
4010 error.to_string(),
4011 ));
4012 }
4013}
4014
4015fn is_named_element_with_name_attribute(elem: &Element) -> bool {
4016 let type_ = match elem.upcast::<Node>().type_id() {
4017 NodeTypeId::Element(ElementTypeId::HTMLElement(type_)) => type_,
4018 _ => return false,
4019 };
4020 matches!(
4021 type_,
4022 HTMLElementTypeId::HTMLEmbedElement |
4023 HTMLElementTypeId::HTMLFormElement |
4024 HTMLElementTypeId::HTMLImageElement |
4025 HTMLElementTypeId::HTMLObjectElement
4026 )
4027}
4028
4029fn is_named_element_with_id_attribute(elem: &Element) -> bool {
4030 elem.is_html_element()
4031}
4032
4033#[expect(unsafe_code)]
4034#[unsafe(no_mangle)]
4035unsafe extern "C" fn dump_js_stack(cx: *mut RawJSContext) {
4037 unsafe {
4038 DumpJSStack(cx, true, false, false);
4039 }
4040}
4041
4042impl WindowHelpers for Window {
4043 fn create_named_properties_object(
4044 cx: SafeJSContext,
4045 proto: HandleObject,
4046 object: MutableHandleObject,
4047 ) {
4048 Self::create_named_properties_object(cx, proto, object)
4049 }
4050}