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;
14use std::sync::Arc;
15use std::time::{Duration, Instant};
16
17use app_units::Au;
18use backtrace::Backtrace;
19use base::cross_process_instant::CrossProcessInstant;
20use base::generic_channel;
21use base::generic_channel::GenericSender;
22use base::id::{BrowsingContextId, PipelineId, WebViewId};
23use base64::Engine;
24#[cfg(feature = "bluetooth")]
25use bluetooth_traits::BluetoothRequest;
26use canvas_traits::webgl::WebGLChan;
27use compositing_traits::CrossProcessCompositorApi;
28use constellation_traits::{
29 DocumentState, LoadData, LoadOrigin, NavigationHistoryBehavior, ScriptToConstellationChan,
30 ScriptToConstellationMessage, StructuredSerializedData, WindowSizeType,
31};
32use crossbeam_channel::{Sender, unbounded};
33use cssparser::SourceLocation;
34use devtools_traits::{ScriptToDevtoolsControlMsg, TimelineMarker, TimelineMarkerType};
35use dom_struct::dom_struct;
36use embedder_traits::user_content_manager::{UserContentManager, UserScript};
37use embedder_traits::{
38 AlertResponse, ConfirmResponse, EmbedderMsg, PromptResponse, ScriptToEmbedderChan,
39 SimpleDialog, Theme, UntrustedNodeAddress, ViewportDetails, WebDriverJSError,
40 WebDriverJSResult, WebDriverLoadStatus,
41};
42use euclid::default::{Point2D as UntypedPoint2D, Rect as UntypedRect, Size2D as UntypedSize2D};
43use euclid::{Point2D, Scale, Size2D, Vector2D};
44use fonts::FontContext;
45use ipc_channel::ipc::{self, IpcSender};
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::rust::wrappers::JS_DefineProperty;
52use js::rust::{
53 CustomAutoRooter, CustomAutoRooterGuard, HandleObject, HandleValue, MutableHandleObject,
54 MutableHandleValue,
55};
56use layout_api::{
57 BoxAreaType, ElementsFromPointFlags, ElementsFromPointResult, FragmentType, Layout,
58 LayoutImageDestination, PendingImage, PendingImageState, PendingRasterizationImage, QueryMsg,
59 ReflowGoal, ReflowPhasesRun, ReflowRequest, ReflowRequestRestyle, RestyleReason,
60 ScrollContainerQueryType, ScrollContainerResponse, TrustedNodeAddress,
61 combine_id_with_fragment_type,
62};
63use malloc_size_of::MallocSizeOf;
64use media::WindowGLContext;
65use net_traits::ResourceThreads;
66use net_traits::image_cache::{
67 ImageCache, ImageCacheResponseMessage, ImageLoadListener, ImageResponse, PendingImageId,
68 PendingImageResponse, RasterizationCompleteResponse,
69};
70use net_traits::storage_thread::StorageType;
71use num_traits::ToPrimitive;
72use profile_traits::generic_channel as ProfiledGenericChannel;
73use profile_traits::mem::ProfilerChan as MemProfilerChan;
74use profile_traits::time::ProfilerChan as TimeProfilerChan;
75use rustc_hash::{FxBuildHasher, FxHashMap};
76use script_bindings::codegen::GenericBindings::WindowBinding::ScrollToOptions;
77use script_bindings::conversions::SafeToJSValConvertible;
78use script_bindings::interfaces::WindowHelpers;
79use script_bindings::root::Root;
80use script_traits::{ConstellationInputEvent, ScriptThreadMessage};
81use selectors::attr::CaseSensitivity;
82use servo_arc::Arc as ServoArc;
83use servo_config::{opts, pref};
84use servo_geometry::{DeviceIndependentIntRect, f32_rect_to_au_rect};
85use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
86use style::error_reporting::{ContextualParseError, ParseErrorReporter};
87use style::properties::PropertyId;
88use style::properties::style_structs::Font;
89use style::selector_parser::PseudoElement;
90use style::str::HTML_SPACE_CHARACTERS;
91use style::stylesheets::UrlExtraData;
92use style_traits::CSSPixel;
93use stylo_atoms::Atom;
94use url::Position;
95use webrender_api::ExternalScrollId;
96use webrender_api::units::{DeviceIntSize, DevicePixel, LayoutPixel, LayoutPoint};
97
98use super::bindings::codegen::Bindings::MessagePortBinding::StructuredSerializeOptions;
99use super::bindings::trace::HashMapTracedValues;
100use super::types::SVGSVGElement;
101use crate::dom::bindings::cell::{DomRefCell, Ref};
102use crate::dom::bindings::codegen::Bindings::DocumentBinding::{
103 DocumentMethods, DocumentReadyState, NamedPropertyValue,
104};
105use crate::dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElementMethods;
106use crate::dom::bindings::codegen::Bindings::HistoryBinding::History_Binding::HistoryMethods;
107use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
108 ImageBitmapOptions, ImageBitmapSource,
109};
110use crate::dom::bindings::codegen::Bindings::MediaQueryListBinding::MediaQueryList_Binding::MediaQueryListMethods;
111use crate::dom::bindings::codegen::Bindings::ReportingObserverBinding::Report;
112use crate::dom::bindings::codegen::Bindings::RequestBinding::RequestInit;
113use crate::dom::bindings::codegen::Bindings::VoidFunctionBinding::VoidFunction;
114use crate::dom::bindings::codegen::Bindings::WindowBinding::{
115 self, FrameRequestCallback, ScrollBehavior, WindowMethods, WindowPostMessageOptions,
116};
117use crate::dom::bindings::codegen::UnionTypes::{
118 RequestOrUSVString, TrustedScriptOrString, TrustedScriptOrStringOrFunction,
119};
120use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
121use crate::dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementTypeId, NodeTypeId};
122use crate::dom::bindings::num::Finite;
123use crate::dom::bindings::refcounted::Trusted;
124use crate::dom::bindings::reflector::{DomGlobal, DomObject};
125use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
126use crate::dom::bindings::str::{DOMString, USVString};
127use crate::dom::bindings::structuredclone;
128use crate::dom::bindings::trace::{CustomTraceable, JSTraceable, RootedTraceableBox};
129use crate::dom::bindings::utils::GlobalStaticData;
130use crate::dom::bindings::weakref::DOMTracker;
131#[cfg(feature = "bluetooth")]
132use crate::dom::bluetooth::BluetoothExtraPermissionData;
133use crate::dom::cookiestore::CookieStore;
134use crate::dom::crypto::Crypto;
135use crate::dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration, CSSStyleOwner};
136use crate::dom::customelementregistry::CustomElementRegistry;
137use crate::dom::document::{AnimationFrameCallback, Document};
138use crate::dom::element::Element;
139use crate::dom::event::{Event, EventBubbles, EventCancelable};
140use crate::dom::eventtarget::EventTarget;
141use crate::dom::globalscope::GlobalScope;
142use crate::dom::hashchangeevent::HashChangeEvent;
143use crate::dom::history::History;
144use crate::dom::html::htmlcollection::{CollectionFilter, HTMLCollection};
145use crate::dom::html::htmliframeelement::HTMLIFrameElement;
146use crate::dom::idbfactory::IDBFactory;
147use crate::dom::inputevent::HitTestResult;
148use crate::dom::location::Location;
149use crate::dom::medialist::MediaList;
150use crate::dom::mediaquerylist::{MediaQueryList, MediaQueryListMatchState};
151use crate::dom::mediaquerylistevent::MediaQueryListEvent;
152use crate::dom::messageevent::MessageEvent;
153use crate::dom::navigator::Navigator;
154use crate::dom::node::{Node, NodeDamage, NodeTraits, from_untrusted_node_address};
155use crate::dom::performance::Performance;
156use crate::dom::promise::Promise;
157use crate::dom::reportingendpoint::{ReportingEndpoint, SendReportsToEndpoints};
158use crate::dom::reportingobserver::ReportingObserver;
159use crate::dom::screen::Screen;
160use crate::dom::selection::Selection;
161use crate::dom::shadowroot::ShadowRoot;
162use crate::dom::storage::Storage;
163#[cfg(feature = "bluetooth")]
164use crate::dom::testrunner::TestRunner;
165use crate::dom::trustedtypepolicyfactory::TrustedTypePolicyFactory;
166use crate::dom::types::{ImageBitmap, UIEvent};
167use crate::dom::webgl::webglrenderingcontext::WebGLCommandSender;
168#[cfg(feature = "webgpu")]
169use crate::dom::webgpu::identityhub::IdentityHub;
170use crate::dom::windowproxy::{WindowProxy, WindowProxyHandler};
171use crate::dom::worklet::Worklet;
172use crate::dom::workletglobalscope::WorkletGlobalScopeType;
173use crate::layout_image::fetch_image_for_layout;
174use crate::messaging::{MainThreadScriptMsg, ScriptEventLoopReceiver, ScriptEventLoopSender};
175use crate::microtask::MicrotaskQueue;
176use crate::realms::{InRealm, enter_realm};
177use crate::script_runtime::{CanGc, JSContext, Runtime};
178use crate::script_thread::ScriptThread;
179use crate::timers::{IsInterval, TimerCallback};
180use crate::unminify::unminified_path;
181use crate::webdriver_handlers::{find_node_by_unique_id_in_document, jsval_to_webdriver};
182use crate::{fetch, window_named_properties};
183
184#[derive(MallocSizeOf)]
189pub struct PendingImageCallback(
190 #[ignore_malloc_size_of = "dyn Fn is currently impossible to measure"]
191 Box<dyn Fn(PendingImageResponse) + 'static>,
192);
193
194#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf, PartialEq)]
196enum WindowState {
197 Alive,
198 Zombie, }
200
201const INITIAL_REFLOW_DELAY: Duration = Duration::from_millis(200);
204
205#[derive(Clone, Copy, MallocSizeOf)]
216enum LayoutBlocker {
217 WaitingForParse,
219 Parsing(Instant),
221 FiredLoadEventOrParsingTimerExpired,
225}
226
227impl LayoutBlocker {
228 fn layout_blocked(&self) -> bool {
229 !matches!(self, Self::FiredLoadEventOrParsingTimerExpired)
230 }
231}
232
233#[derive(Clone, Copy, Debug, Default, JSTraceable, MallocSizeOf, PartialEq)]
236pub(crate) struct OngoingNavigation(u32);
237
238type PendingImageRasterizationKey = (PendingImageId, DeviceIntSize);
239
240#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
244#[derive(JSTraceable, MallocSizeOf)]
245struct PendingLayoutImageAncillaryData {
246 node: Dom<Node>,
247 #[no_trace]
248 destination: LayoutImageDestination,
249}
250
251#[dom_struct]
252pub(crate) struct Window {
253 globalscope: GlobalScope,
254 #[no_trace]
258 webview_id: WebViewId,
259 script_chan: Sender<MainThreadScriptMsg>,
260 #[no_trace]
261 #[ignore_malloc_size_of = "TODO: Add MallocSizeOf support to layout"]
262 layout: RefCell<Box<dyn Layout>>,
263 navigator: MutNullableDom<Navigator>,
264 #[ignore_malloc_size_of = "Arc"]
265 #[no_trace]
266 image_cache: Arc<dyn ImageCache>,
267 #[no_trace]
268 image_cache_sender: IpcSender<ImageCacheResponseMessage>,
269 window_proxy: MutNullableDom<WindowProxy>,
270 document: MutNullableDom<Document>,
271 location: MutNullableDom<Location>,
272 history: MutNullableDom<History>,
273 indexeddb: MutNullableDom<IDBFactory>,
274 custom_element_registry: MutNullableDom<CustomElementRegistry>,
275 performance: MutNullableDom<Performance>,
276 #[no_trace]
277 navigation_start: Cell<CrossProcessInstant>,
278 screen: MutNullableDom<Screen>,
279 session_storage: MutNullableDom<Storage>,
280 local_storage: MutNullableDom<Storage>,
281 status: DomRefCell<DOMString>,
282 trusted_types: MutNullableDom<TrustedTypePolicyFactory>,
283
284 ongoing_navigation: Cell<OngoingNavigation>,
287
288 #[no_trace]
291 devtools_markers: DomRefCell<HashSet<TimelineMarkerType>>,
292 #[no_trace]
293 devtools_marker_sender: DomRefCell<Option<IpcSender<Option<TimelineMarker>>>>,
294
295 #[no_trace]
297 unhandled_resize_event: DomRefCell<Option<(ViewportDetails, WindowSizeType)>>,
298
299 #[no_trace]
301 theme: Cell<Theme>,
302
303 #[no_trace]
305 parent_info: Option<PipelineId>,
306
307 dom_static: GlobalStaticData,
309
310 #[ignore_malloc_size_of = "Rc<T> is hard"]
312 js_runtime: DomRefCell<Option<Rc<Runtime>>>,
313
314 #[no_trace]
316 viewport_details: Cell<ViewportDetails>,
317
318 #[no_trace]
320 #[cfg(feature = "bluetooth")]
321 bluetooth_thread: IpcSender<BluetoothRequest>,
322
323 #[cfg(feature = "bluetooth")]
324 bluetooth_extra_permission_data: BluetoothExtraPermissionData,
325
326 #[no_trace]
330 layout_blocker: Cell<LayoutBlocker>,
331
332 #[no_trace]
334 webdriver_script_chan: DomRefCell<Option<IpcSender<WebDriverJSResult>>>,
335
336 #[no_trace]
338 webdriver_load_status_sender: RefCell<Option<GenericSender<WebDriverLoadStatus>>>,
339
340 current_state: Cell<WindowState>,
342
343 #[no_trace]
346 current_viewport_size: Cell<UntypedSize2D<Au>>,
347
348 error_reporter: CSSErrorReporter,
349
350 media_query_lists: DOMTracker<MediaQueryList>,
352
353 #[cfg(feature = "bluetooth")]
354 test_runner: MutNullableDom<TestRunner>,
355
356 #[ignore_malloc_size_of = "channels are hard"]
358 #[no_trace]
359 webgl_chan: Option<WebGLChan>,
360
361 #[ignore_malloc_size_of = "defined in webxr"]
362 #[no_trace]
363 #[cfg(feature = "webxr")]
364 webxr_registry: Option<webxr_api::Registry>,
365
366 #[no_trace]
370 pending_image_callbacks: DomRefCell<FxHashMap<PendingImageId, Vec<PendingImageCallback>>>,
371
372 pending_layout_images: DomRefCell<
377 HashMapTracedValues<PendingImageId, Vec<PendingLayoutImageAncillaryData>, FxBuildHasher>,
378 >,
379
380 pending_images_for_rasterization: DomRefCell<
384 HashMapTracedValues<PendingImageRasterizationKey, Vec<Dom<Node>>, FxBuildHasher>,
385 >,
386
387 unminified_css_dir: DomRefCell<Option<String>>,
390
391 local_script_source: Option<String>,
393
394 test_worklet: MutNullableDom<Worklet>,
396 paint_worklet: MutNullableDom<Worklet>,
398
399 exists_mut_observer: Cell<bool>,
401
402 #[ignore_malloc_size_of = "Wraps an IpcSender"]
404 #[no_trace]
405 compositor_api: CrossProcessCompositorApi,
406
407 has_sent_idle_message: Cell<bool>,
410
411 unminify_css: bool,
413
414 #[no_trace]
416 user_content_manager: UserContentManager,
417
418 #[ignore_malloc_size_of = "defined in script_thread"]
420 #[no_trace]
421 player_context: WindowGLContext,
422
423 throttled: Cell<bool>,
424
425 #[ignore_malloc_size_of = "Rc is hard"]
429 layout_marker: DomRefCell<Rc<Cell<bool>>>,
430
431 current_event: DomRefCell<Option<Dom<Event>>>,
433
434 reporting_observer_list: DomRefCell<Vec<DomRoot<ReportingObserver>>>,
436
437 report_list: DomRefCell<Vec<Report>>,
439
440 #[no_trace]
442 endpoints_list: DomRefCell<Vec<ReportingEndpoint>>,
443}
444
445impl Window {
446 pub(crate) fn webview_id(&self) -> WebViewId {
447 self.webview_id
448 }
449
450 pub(crate) fn as_global_scope(&self) -> &GlobalScope {
451 self.upcast::<GlobalScope>()
452 }
453
454 pub(crate) fn layout(&self) -> Ref<'_, Box<dyn Layout>> {
455 self.layout.borrow()
456 }
457
458 pub(crate) fn layout_mut(&self) -> RefMut<'_, Box<dyn Layout>> {
459 self.layout.borrow_mut()
460 }
461
462 pub(crate) fn get_exists_mut_observer(&self) -> bool {
463 self.exists_mut_observer.get()
464 }
465
466 pub(crate) fn set_exists_mut_observer(&self) {
467 self.exists_mut_observer.set(true);
468 }
469
470 #[allow(unsafe_code)]
471 pub(crate) fn clear_js_runtime_for_script_deallocation(&self) {
472 self.as_global_scope()
473 .remove_web_messaging_and_dedicated_workers_infra();
474 unsafe {
475 *self.js_runtime.borrow_for_script_deallocation() = None;
476 self.window_proxy.set(None);
477 self.current_state.set(WindowState::Zombie);
478 self.as_global_scope()
479 .task_manager()
480 .cancel_all_tasks_and_ignore_future_tasks();
481 }
482 }
483
484 pub(crate) fn discard_browsing_context(&self) {
487 let proxy = match self.window_proxy.get() {
488 Some(proxy) => proxy,
489 None => panic!("Discarding a BC from a window that has none"),
490 };
491 proxy.discard_browsing_context();
492 self.as_global_scope()
496 .task_manager()
497 .cancel_all_tasks_and_ignore_future_tasks();
498 }
499
500 pub(crate) fn time_profiler_chan(&self) -> &TimeProfilerChan {
502 self.globalscope.time_profiler_chan()
503 }
504
505 pub(crate) fn origin(&self) -> &MutableOrigin {
506 self.globalscope.origin()
507 }
508
509 #[allow(unsafe_code)]
510 pub(crate) fn get_cx(&self) -> JSContext {
511 unsafe { JSContext::from_ptr(self.js_runtime.borrow().as_ref().unwrap().cx()) }
512 }
513
514 pub(crate) fn get_js_runtime(&self) -> Ref<'_, Option<Rc<Runtime>>> {
515 self.js_runtime.borrow()
516 }
517
518 pub(crate) fn main_thread_script_chan(&self) -> &Sender<MainThreadScriptMsg> {
519 &self.script_chan
520 }
521
522 pub(crate) fn parent_info(&self) -> Option<PipelineId> {
523 self.parent_info
524 }
525
526 pub(crate) fn new_script_pair(&self) -> (ScriptEventLoopSender, ScriptEventLoopReceiver) {
527 let (sender, receiver) = unbounded();
528 (
529 ScriptEventLoopSender::MainThread(sender),
530 ScriptEventLoopReceiver::MainThread(receiver),
531 )
532 }
533
534 pub(crate) fn event_loop_sender(&self) -> ScriptEventLoopSender {
535 ScriptEventLoopSender::MainThread(self.script_chan.clone())
536 }
537
538 pub(crate) fn image_cache(&self) -> Arc<dyn ImageCache> {
539 self.image_cache.clone()
540 }
541
542 pub(crate) fn window_proxy(&self) -> DomRoot<WindowProxy> {
544 self.window_proxy.get().unwrap()
545 }
546
547 pub(crate) fn append_reporting_observer(&self, reporting_observer: DomRoot<ReportingObserver>) {
548 self.reporting_observer_list
549 .borrow_mut()
550 .push(reporting_observer);
551 }
552
553 pub(crate) fn remove_reporting_observer(&self, reporting_observer: &ReportingObserver) {
554 if let Some(index) = self
555 .reporting_observer_list
556 .borrow()
557 .iter()
558 .position(|observer| &**observer == reporting_observer)
559 {
560 self.reporting_observer_list.borrow_mut().remove(index);
561 }
562 }
563
564 pub(crate) fn registered_reporting_observers(&self) -> Vec<DomRoot<ReportingObserver>> {
565 self.reporting_observer_list.borrow().clone()
566 }
567
568 pub(crate) fn append_report(&self, report: Report) {
569 self.report_list.borrow_mut().push(report);
570 let trusted_window = Trusted::new(self);
571 self.upcast::<GlobalScope>()
572 .task_manager()
573 .dom_manipulation_task_source()
574 .queue(task!(send_to_reporting_endpoints: move || {
575 let window = trusted_window.root();
576 let reports = std::mem::take(&mut *window.report_list.borrow_mut());
577 window.upcast::<GlobalScope>().send_reports_to_endpoints(
578 reports,
579 window.endpoints_list.borrow().clone(),
580 );
581 }));
582 }
583
584 pub(crate) fn buffered_reports(&self) -> Vec<Report> {
585 self.report_list.borrow().clone()
586 }
587
588 pub(crate) fn set_endpoints_list(&self, endpoints: Vec<ReportingEndpoint>) {
589 *self.endpoints_list.borrow_mut() = endpoints;
590 }
591
592 pub(crate) fn undiscarded_window_proxy(&self) -> Option<DomRoot<WindowProxy>> {
595 self.window_proxy.get().and_then(|window_proxy| {
596 if window_proxy.is_browsing_context_discarded() {
597 None
598 } else {
599 Some(window_proxy)
600 }
601 })
602 }
603
604 pub(crate) fn webview_window_proxy(&self) -> Option<DomRoot<WindowProxy>> {
607 self.undiscarded_window_proxy().and_then(|window_proxy| {
608 ScriptThread::find_window_proxy(window_proxy.webview_id().into())
609 })
610 }
611
612 #[cfg(feature = "bluetooth")]
613 pub(crate) fn bluetooth_thread(&self) -> IpcSender<BluetoothRequest> {
614 self.bluetooth_thread.clone()
615 }
616
617 #[cfg(feature = "bluetooth")]
618 pub(crate) fn bluetooth_extra_permission_data(&self) -> &BluetoothExtraPermissionData {
619 &self.bluetooth_extra_permission_data
620 }
621
622 pub(crate) fn css_error_reporter(&self) -> Option<&dyn ParseErrorReporter> {
623 Some(&self.error_reporter)
624 }
625
626 pub(crate) fn webgl_chan(&self) -> Option<WebGLCommandSender> {
627 self.webgl_chan
628 .as_ref()
629 .map(|chan| WebGLCommandSender::new(chan.clone()))
630 }
631
632 #[cfg(feature = "webxr")]
633 pub(crate) fn webxr_registry(&self) -> Option<webxr_api::Registry> {
634 self.webxr_registry.clone()
635 }
636
637 fn new_paint_worklet(&self, can_gc: CanGc) -> DomRoot<Worklet> {
638 debug!("Creating new paint worklet.");
639 Worklet::new(self, WorkletGlobalScopeType::Paint, can_gc)
640 }
641
642 pub(crate) fn register_image_cache_listener(
643 &self,
644 id: PendingImageId,
645 callback: impl Fn(PendingImageResponse) + 'static,
646 ) -> IpcSender<ImageCacheResponseMessage> {
647 self.pending_image_callbacks
648 .borrow_mut()
649 .entry(id)
650 .or_default()
651 .push(PendingImageCallback(Box::new(callback)));
652 self.image_cache_sender.clone()
653 }
654
655 fn pending_layout_image_notification(&self, response: PendingImageResponse) {
656 let mut images = self.pending_layout_images.borrow_mut();
657 let nodes = images.entry(response.id);
658 let nodes = match nodes {
659 Entry::Occupied(nodes) => nodes,
660 Entry::Vacant(_) => return,
661 };
662 if matches!(
663 response.response,
664 ImageResponse::Loaded(_, _) | ImageResponse::PlaceholderLoaded(_, _)
665 ) {
666 for ancillary_data in nodes.get() {
667 match ancillary_data.destination {
668 LayoutImageDestination::BoxTreeConstruction => {
669 ancillary_data.node.dirty(NodeDamage::Other);
670 },
671 LayoutImageDestination::DisplayListBuilding => {
672 self.layout().set_needs_new_display_list();
673 },
674 }
675 }
676 }
677
678 match response.response {
679 ImageResponse::MetadataLoaded(_) => {},
680 ImageResponse::Loaded(_, _) |
681 ImageResponse::PlaceholderLoaded(_, _) |
682 ImageResponse::None => {
683 nodes.remove();
684 },
685 }
686 }
687
688 pub(crate) fn handle_image_rasterization_complete_notification(
689 &self,
690 response: RasterizationCompleteResponse,
691 ) {
692 let mut images = self.pending_images_for_rasterization.borrow_mut();
693 let nodes = images.entry((response.image_id, response.requested_size));
694 let nodes = match nodes {
695 Entry::Occupied(nodes) => nodes,
696 Entry::Vacant(_) => return,
697 };
698 for node in nodes.get() {
699 node.dirty(NodeDamage::Other);
700 }
701 nodes.remove();
702 }
703
704 pub(crate) fn pending_image_notification(&self, response: PendingImageResponse) {
705 let mut images = std::mem::take(&mut *self.pending_image_callbacks.borrow_mut());
710 let Entry::Occupied(callbacks) = images.entry(response.id) else {
711 let _ = std::mem::replace(&mut *self.pending_image_callbacks.borrow_mut(), images);
712 return;
713 };
714
715 for callback in callbacks.get() {
716 callback.0(response.clone());
717 }
718
719 match response.response {
720 ImageResponse::MetadataLoaded(_) => {},
721 ImageResponse::Loaded(_, _) |
722 ImageResponse::PlaceholderLoaded(_, _) |
723 ImageResponse::None => {
724 callbacks.remove();
725 },
726 }
727
728 let _ = std::mem::replace(&mut *self.pending_image_callbacks.borrow_mut(), images);
729 }
730
731 pub(crate) fn compositor_api(&self) -> &CrossProcessCompositorApi {
732 &self.compositor_api
733 }
734
735 pub(crate) fn userscripts(&self) -> &[UserScript] {
736 self.user_content_manager.scripts()
737 }
738
739 pub(crate) fn get_player_context(&self) -> WindowGLContext {
740 self.player_context.clone()
741 }
742
743 pub(crate) fn dispatch_event_with_target_override(&self, event: &Event, can_gc: CanGc) {
745 event.dispatch(self.upcast(), true, can_gc);
746 }
747
748 pub(crate) fn font_context(&self) -> &Arc<FontContext> {
749 self.as_global_scope()
750 .font_context()
751 .expect("A `Window` should always have a `FontContext`")
752 }
753
754 pub(crate) fn ongoing_navigation(&self) -> OngoingNavigation {
755 self.ongoing_navigation.get()
756 }
757
758 pub(crate) fn set_ongoing_navigation(&self) -> OngoingNavigation {
760 let new_value = self.ongoing_navigation.get().0.wrapping_add(1);
764
765 self.ongoing_navigation.set(OngoingNavigation(new_value));
772
773 OngoingNavigation(new_value)
775 }
776
777 fn stop_loading(&self, can_gc: CanGc) {
779 let doc = self.Document();
781
782 self.set_ongoing_navigation();
792
793 doc.abort(can_gc);
795 }
796}
797
798pub(crate) fn base64_btoa(input: DOMString) -> Fallible<DOMString> {
800 if input.chars().any(|c: char| c > '\u{FF}') {
804 Err(Error::InvalidCharacter)
805 } else {
806 let octets = input.chars().map(|c: char| c as u8).collect::<Vec<u8>>();
811
812 let config =
815 base64::engine::general_purpose::GeneralPurposeConfig::new().with_encode_padding(true);
816 let engine = base64::engine::GeneralPurpose::new(&base64::alphabet::STANDARD, config);
817 Ok(DOMString::from(engine.encode(octets)))
818 }
819}
820
821pub(crate) fn base64_atob(input: DOMString) -> Fallible<DOMString> {
823 fn is_html_space(c: char) -> bool {
825 HTML_SPACE_CHARACTERS.contains(&c)
826 }
827 let without_spaces = input
828 .chars()
829 .filter(|&c| !is_html_space(c))
830 .collect::<String>();
831 let mut input = &*without_spaces;
832
833 if input.len() % 4 == 0 {
837 if input.ends_with("==") {
838 input = &input[..input.len() - 2]
839 } else if input.ends_with('=') {
840 input = &input[..input.len() - 1]
841 }
842 }
843
844 if input.len() % 4 == 1 {
847 return Err(Error::InvalidCharacter);
848 }
849
850 if input
858 .chars()
859 .any(|c| c != '+' && c != '/' && !c.is_alphanumeric())
860 {
861 return Err(Error::InvalidCharacter);
862 }
863
864 let config = base64::engine::general_purpose::GeneralPurposeConfig::new()
865 .with_decode_padding_mode(base64::engine::DecodePaddingMode::RequireNone)
866 .with_decode_allow_trailing_bits(true);
867 let engine = base64::engine::GeneralPurpose::new(&base64::alphabet::STANDARD, config);
868
869 let data = engine.decode(input).map_err(|_| Error::InvalidCharacter)?;
870 Ok(data.iter().map(|&b| b as char).collect::<String>().into())
871}
872
873impl WindowMethods<crate::DomTypeHolder> for Window {
874 fn Alert_(&self) {
876 self.Alert(DOMString::new());
877 }
878
879 fn Alert(&self, s: DOMString) {
881 {
885 let stderr = stderr();
886 let mut stderr = stderr.lock();
887 let stdout = stdout();
888 let mut stdout = stdout.lock();
889 writeln!(&mut stdout, "\nALERT: {}", s).unwrap();
890 stdout.flush().unwrap();
891 stderr.flush().unwrap();
892 }
893 let (sender, receiver) =
894 ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
895 let dialog = SimpleDialog::Alert {
896 message: s.to_string(),
897 response_sender: sender,
898 };
899 let msg = EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog);
900 self.send_to_embedder(msg);
901 receiver.recv().unwrap_or_else(|_| {
902 debug!("Alert dialog was cancelled or failed to show.");
904 AlertResponse::Ok
905 });
906 }
907
908 fn Confirm(&self, s: DOMString) -> bool {
910 let (sender, receiver) =
911 ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
912 let dialog = SimpleDialog::Confirm {
913 message: s.to_string(),
914 response_sender: sender,
915 };
916 let msg = EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog);
917 self.send_to_embedder(msg);
918 match receiver.recv() {
919 Ok(ConfirmResponse::Ok) => true,
920 Ok(ConfirmResponse::Cancel) => false,
921 Err(_) => {
922 warn!("Confirm dialog was cancelled or failed to show.");
923 false
924 },
925 }
926 }
927
928 fn Prompt(&self, message: DOMString, default: DOMString) -> Option<DOMString> {
930 let (sender, receiver) =
931 ProfiledGenericChannel::channel(self.global().time_profiler_chan().clone()).unwrap();
932 let dialog = SimpleDialog::Prompt {
933 message: message.to_string(),
934 default: default.to_string(),
935 response_sender: sender,
936 };
937 let msg = EmbedderMsg::ShowSimpleDialog(self.webview_id(), dialog);
938 self.send_to_embedder(msg);
939 match receiver.recv() {
940 Ok(PromptResponse::Ok(input)) => Some(input.into()),
941 Ok(PromptResponse::Cancel) => None,
942 Err(_) => {
943 warn!("Prompt dialog was cancelled or failed to show.");
944 None
945 },
946 }
947 }
948
949 fn Stop(&self, can_gc: CanGc) {
951 self.stop_loading(can_gc);
956 }
957
958 fn Focus(&self) {
960 let current = match self.undiscarded_window_proxy() {
964 Some(proxy) => proxy,
965 None => return,
966 };
967
968 current.focus();
970
971 }
977
978 fn Blur(&self) {
980 }
983
984 fn Open(
986 &self,
987 url: USVString,
988 target: DOMString,
989 features: DOMString,
990 can_gc: CanGc,
991 ) -> Fallible<Option<DomRoot<WindowProxy>>> {
992 self.window_proxy().open(url, target, features, can_gc)
993 }
994
995 fn GetOpener(
997 &self,
998 cx: JSContext,
999 in_realm_proof: InRealm,
1000 mut retval: MutableHandleValue,
1001 ) -> Fallible<()> {
1002 let current = match self.window_proxy.get() {
1004 Some(proxy) => proxy,
1005 None => {
1007 retval.set(NullValue());
1008 return Ok(());
1009 },
1010 };
1011 if current.is_browsing_context_discarded() {
1016 retval.set(NullValue());
1017 return Ok(());
1018 }
1019 current.opener(*cx, in_realm_proof, retval);
1021 Ok(())
1022 }
1023
1024 #[allow(unsafe_code)]
1025 fn SetOpener(&self, cx: JSContext, value: HandleValue) -> ErrorResult {
1027 if value.is_null() {
1029 if let Some(proxy) = self.window_proxy.get() {
1030 proxy.disown();
1031 }
1032 return Ok(());
1033 }
1034 let obj = self.reflector().get_jsobject();
1036 unsafe {
1037 let result =
1038 JS_DefineProperty(*cx, obj, c"opener".as_ptr(), value, JSPROP_ENUMERATE as u32);
1039
1040 if result { Ok(()) } else { Err(Error::JSFailed) }
1041 }
1042 }
1043
1044 fn Closed(&self) -> bool {
1046 self.window_proxy
1047 .get()
1048 .map(|ref proxy| proxy.is_browsing_context_discarded() || proxy.is_closing())
1049 .unwrap_or(true)
1050 }
1051
1052 fn Close(&self) {
1054 let window_proxy = match self.window_proxy.get() {
1057 Some(proxy) => proxy,
1058 None => return,
1059 };
1060 if window_proxy.is_closing() {
1061 return;
1062 }
1063 if let Ok(history_length) = self.History().GetLength() {
1066 let is_auxiliary = window_proxy.is_auxiliary();
1067
1068 let is_script_closable = (self.is_top_level() && history_length == 1) ||
1070 is_auxiliary ||
1071 pref!(dom_allow_scripts_to_close_windows);
1072
1073 if is_script_closable {
1077 window_proxy.close();
1079
1080 let this = Trusted::new(self);
1082 let task = task!(window_close_browsing_context: move || {
1083 let window = this.root();
1084 let document = window.Document();
1085 if document.prompt_to_unload(false, CanGc::note()) {
1091 document.unload(false, CanGc::note());
1093
1094 window.discard_browsing_context();
1097
1098 window.send_to_constellation(ScriptToConstellationMessage::DiscardTopLevelBrowsingContext);
1099 }
1100 });
1101 self.as_global_scope()
1102 .task_manager()
1103 .dom_manipulation_task_source()
1104 .queue(task);
1105 }
1106 }
1107 }
1108
1109 fn Document(&self) -> DomRoot<Document> {
1111 self.document
1112 .get()
1113 .expect("Document accessed before initialization.")
1114 }
1115
1116 fn History(&self) -> DomRoot<History> {
1118 self.history.or_init(|| History::new(self, CanGc::note()))
1119 }
1120
1121 fn IndexedDB(&self) -> DomRoot<IDBFactory> {
1123 self.indexeddb.or_init(|| {
1124 let global_scope = self.upcast::<GlobalScope>();
1125 IDBFactory::new(global_scope, CanGc::note())
1126 })
1127 }
1128
1129 fn CustomElements(&self) -> DomRoot<CustomElementRegistry> {
1131 self.custom_element_registry
1132 .or_init(|| CustomElementRegistry::new(self, CanGc::note()))
1133 }
1134
1135 fn Location(&self) -> DomRoot<Location> {
1137 self.location.or_init(|| Location::new(self, CanGc::note()))
1138 }
1139
1140 fn SessionStorage(&self) -> DomRoot<Storage> {
1142 self.session_storage
1143 .or_init(|| Storage::new(self, StorageType::Session, CanGc::note()))
1144 }
1145
1146 fn LocalStorage(&self) -> DomRoot<Storage> {
1148 self.local_storage
1149 .or_init(|| Storage::new(self, StorageType::Local, CanGc::note()))
1150 }
1151
1152 fn CookieStore(&self, can_gc: CanGc) -> DomRoot<CookieStore> {
1154 self.global().cookie_store(can_gc)
1155 }
1156
1157 fn Crypto(&self) -> DomRoot<Crypto> {
1159 self.as_global_scope().crypto(CanGc::note())
1160 }
1161
1162 fn GetFrameElement(&self) -> Option<DomRoot<Element>> {
1164 let window_proxy = self.window_proxy.get()?;
1166
1167 let container = window_proxy.frame_element()?;
1169
1170 let container_doc = container.owner_document();
1172 let current_doc = GlobalScope::current()
1173 .expect("No current global object")
1174 .as_window()
1175 .Document();
1176 if !current_doc
1177 .origin()
1178 .same_origin_domain(container_doc.origin())
1179 {
1180 return None;
1181 }
1182 Some(DomRoot::from_ref(container))
1184 }
1185
1186 fn Navigator(&self) -> DomRoot<Navigator> {
1188 self.navigator
1189 .or_init(|| Navigator::new(self, CanGc::note()))
1190 }
1191
1192 fn SetTimeout(
1194 &self,
1195 _cx: JSContext,
1196 callback: TrustedScriptOrStringOrFunction,
1197 timeout: i32,
1198 args: Vec<HandleValue>,
1199 can_gc: CanGc,
1200 ) -> Fallible<i32> {
1201 let callback = match callback {
1202 TrustedScriptOrStringOrFunction::String(i) => {
1203 TimerCallback::StringTimerCallback(TrustedScriptOrString::String(i))
1204 },
1205 TrustedScriptOrStringOrFunction::TrustedScript(i) => {
1206 TimerCallback::StringTimerCallback(TrustedScriptOrString::TrustedScript(i))
1207 },
1208 TrustedScriptOrStringOrFunction::Function(i) => TimerCallback::FunctionTimerCallback(i),
1209 };
1210 self.as_global_scope().set_timeout_or_interval(
1211 callback,
1212 args,
1213 Duration::from_millis(timeout.max(0) as u64),
1214 IsInterval::NonInterval,
1215 can_gc,
1216 )
1217 }
1218
1219 fn ClearTimeout(&self, handle: i32) {
1221 self.as_global_scope().clear_timeout_or_interval(handle);
1222 }
1223
1224 fn SetInterval(
1226 &self,
1227 _cx: JSContext,
1228 callback: TrustedScriptOrStringOrFunction,
1229 timeout: i32,
1230 args: Vec<HandleValue>,
1231 can_gc: CanGc,
1232 ) -> Fallible<i32> {
1233 let callback = match callback {
1234 TrustedScriptOrStringOrFunction::String(i) => {
1235 TimerCallback::StringTimerCallback(TrustedScriptOrString::String(i))
1236 },
1237 TrustedScriptOrStringOrFunction::TrustedScript(i) => {
1238 TimerCallback::StringTimerCallback(TrustedScriptOrString::TrustedScript(i))
1239 },
1240 TrustedScriptOrStringOrFunction::Function(i) => TimerCallback::FunctionTimerCallback(i),
1241 };
1242 self.as_global_scope().set_timeout_or_interval(
1243 callback,
1244 args,
1245 Duration::from_millis(timeout.max(0) as u64),
1246 IsInterval::Interval,
1247 can_gc,
1248 )
1249 }
1250
1251 fn ClearInterval(&self, handle: i32) {
1253 self.ClearTimeout(handle);
1254 }
1255
1256 fn QueueMicrotask(&self, callback: Rc<VoidFunction>) {
1258 self.as_global_scope().queue_function_as_microtask(callback);
1259 }
1260
1261 fn CreateImageBitmap(
1263 &self,
1264 image: ImageBitmapSource,
1265 options: &ImageBitmapOptions,
1266 can_gc: CanGc,
1267 ) -> Rc<Promise> {
1268 ImageBitmap::create_image_bitmap(
1269 self.as_global_scope(),
1270 image,
1271 0,
1272 0,
1273 None,
1274 None,
1275 options,
1276 can_gc,
1277 )
1278 }
1279
1280 fn CreateImageBitmap_(
1282 &self,
1283 image: ImageBitmapSource,
1284 sx: i32,
1285 sy: i32,
1286 sw: i32,
1287 sh: i32,
1288 options: &ImageBitmapOptions,
1289 can_gc: CanGc,
1290 ) -> Rc<Promise> {
1291 ImageBitmap::create_image_bitmap(
1292 self.as_global_scope(),
1293 image,
1294 sx,
1295 sy,
1296 Some(sw),
1297 Some(sh),
1298 options,
1299 can_gc,
1300 )
1301 }
1302
1303 fn Window(&self) -> DomRoot<WindowProxy> {
1305 self.window_proxy()
1306 }
1307
1308 fn Self_(&self) -> DomRoot<WindowProxy> {
1310 self.window_proxy()
1311 }
1312
1313 fn Frames(&self) -> DomRoot<WindowProxy> {
1315 self.window_proxy()
1316 }
1317
1318 fn Length(&self) -> u32 {
1320 self.Document().iframes().iter().count() as u32
1321 }
1322
1323 fn GetParent(&self) -> Option<DomRoot<WindowProxy>> {
1325 let window_proxy = self.undiscarded_window_proxy()?;
1327
1328 if let Some(parent) = window_proxy.parent() {
1330 return Some(DomRoot::from_ref(parent));
1331 }
1332 Some(window_proxy)
1334 }
1335
1336 fn GetTop(&self) -> Option<DomRoot<WindowProxy>> {
1338 let window_proxy = self.undiscarded_window_proxy()?;
1340
1341 Some(DomRoot::from_ref(window_proxy.top()))
1343 }
1344
1345 fn Performance(&self) -> DomRoot<Performance> {
1348 self.performance.or_init(|| {
1349 Performance::new(
1350 self.as_global_scope(),
1351 self.navigation_start.get(),
1352 CanGc::note(),
1353 )
1354 })
1355 }
1356
1357 global_event_handlers!();
1359
1360 window_event_handlers!();
1362
1363 fn Screen(&self) -> DomRoot<Screen> {
1365 self.screen.or_init(|| Screen::new(self, CanGc::note()))
1366 }
1367
1368 fn Btoa(&self, btoa: DOMString) -> Fallible<DOMString> {
1370 base64_btoa(btoa)
1371 }
1372
1373 fn Atob(&self, atob: DOMString) -> Fallible<DOMString> {
1375 base64_atob(atob)
1376 }
1377
1378 fn RequestAnimationFrame(&self, callback: Rc<FrameRequestCallback>) -> u32 {
1380 self.Document()
1381 .request_animation_frame(AnimationFrameCallback::FrameRequestCallback { callback })
1382 }
1383
1384 fn CancelAnimationFrame(&self, ident: u32) {
1386 let doc = self.Document();
1387 doc.cancel_animation_frame(ident);
1388 }
1389
1390 fn PostMessage(
1392 &self,
1393 cx: JSContext,
1394 message: HandleValue,
1395 target_origin: USVString,
1396 transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
1397 ) -> ErrorResult {
1398 let incumbent = GlobalScope::incumbent().expect("no incumbent global?");
1399 let source = incumbent.as_window();
1400 let source_origin = source.Document().origin().immutable().clone();
1401
1402 self.post_message_impl(&target_origin, source_origin, source, cx, message, transfer)
1403 }
1404
1405 fn PostMessage_(
1407 &self,
1408 cx: JSContext,
1409 message: HandleValue,
1410 options: RootedTraceableBox<WindowPostMessageOptions>,
1411 ) -> ErrorResult {
1412 let mut rooted = CustomAutoRooter::new(
1413 options
1414 .parent
1415 .transfer
1416 .iter()
1417 .map(|js: &RootedTraceableBox<Heap<*mut JSObject>>| js.get())
1418 .collect(),
1419 );
1420 let transfer = CustomAutoRooterGuard::new(*cx, &mut rooted);
1421
1422 let incumbent = GlobalScope::incumbent().expect("no incumbent global?");
1423 let source = incumbent.as_window();
1424
1425 let source_origin = source.Document().origin().immutable().clone();
1426
1427 self.post_message_impl(
1428 &options.targetOrigin,
1429 source_origin,
1430 source,
1431 cx,
1432 message,
1433 transfer,
1434 )
1435 }
1436
1437 fn CaptureEvents(&self) {
1439 }
1441
1442 fn ReleaseEvents(&self) {
1444 }
1446
1447 fn Debug(&self, message: DOMString) {
1449 debug!("{}", message);
1450 }
1451
1452 #[allow(unsafe_code)]
1453 fn Gc(&self) {
1454 unsafe {
1455 JS_GC(*self.get_cx(), GCReason::API);
1456 }
1457 }
1458
1459 #[allow(unsafe_code)]
1460 fn Js_backtrace(&self) {
1461 unsafe {
1462 println!("Current JS stack:");
1463 dump_js_stack(*self.get_cx());
1464 let rust_stack = Backtrace::new();
1465 println!("Current Rust stack:\n{:?}", rust_stack);
1466 }
1467 }
1468
1469 fn WebdriverCallback(&self, cx: JSContext, val: HandleValue, realm: InRealm, can_gc: CanGc) {
1470 let rv = jsval_to_webdriver(cx, &self.globalscope, val, realm, can_gc);
1471 let opt_chan = self.webdriver_script_chan.borrow_mut().take();
1472 if let Some(chan) = opt_chan {
1473 let _ = chan.send(rv);
1474 }
1475 }
1476
1477 fn WebdriverException(&self, cx: JSContext, val: HandleValue, realm: InRealm, can_gc: CanGc) {
1478 let rv = jsval_to_webdriver(cx, &self.globalscope, val, realm, can_gc);
1479 let opt_chan = self.webdriver_script_chan.borrow_mut().take();
1480 if let Some(chan) = opt_chan {
1481 if let Ok(rv) = rv {
1482 let _ = chan.send(Err(WebDriverJSError::JSException(rv)));
1483 } else {
1484 let _ = chan.send(rv);
1485 }
1486 }
1487 }
1488
1489 fn WebdriverTimeout(&self) {
1490 let opt_chan = self.webdriver_script_chan.borrow_mut().take();
1491 if let Some(chan) = opt_chan {
1492 let _ = chan.send(Err(WebDriverJSError::Timeout));
1493 }
1494 }
1495
1496 fn WebdriverElement(&self, id: DOMString) -> Option<DomRoot<Element>> {
1497 find_node_by_unique_id_in_document(&self.Document(), id.into()).and_then(Root::downcast)
1498 }
1499
1500 fn WebdriverFrame(&self, browsing_context_id: DOMString) -> Option<DomRoot<WindowProxy>> {
1501 self.Document()
1502 .iframes()
1503 .iter()
1504 .find(|iframe| {
1505 iframe
1506 .browsing_context_id()
1507 .as_ref()
1508 .map(BrowsingContextId::to_string) ==
1509 Some(browsing_context_id.to_string())
1510 })
1511 .and_then(|iframe| iframe.GetContentWindow())
1512 }
1513
1514 fn WebdriverWindow(&self, webview_id: DOMString) -> Option<DomRoot<WindowProxy>> {
1515 let window_proxy = self.window_proxy.get()?;
1516
1517 if window_proxy.browsing_context_id() != window_proxy.webview_id() {
1519 return None;
1520 }
1521
1522 if self.webview_id().to_string() == webview_id.str() {
1523 Some(DomRoot::from_ref(&window_proxy))
1524 } else {
1525 None
1526 }
1527 }
1528
1529 fn WebdriverShadowRoot(&self, id: DOMString) -> Option<DomRoot<ShadowRoot>> {
1530 find_node_by_unique_id_in_document(&self.Document(), id.into()).and_then(Root::downcast)
1531 }
1532
1533 fn GetComputedStyle(
1535 &self,
1536 element: &Element,
1537 pseudo: Option<DOMString>,
1538 ) -> DomRoot<CSSStyleDeclaration> {
1539 let mut is_null = false;
1543
1544 let pseudo = pseudo.map(|mut s| {
1547 s.make_ascii_lowercase();
1548 s
1549 });
1550 let pseudo = match pseudo {
1551 Some(ref pseudo) if pseudo == ":before" || pseudo == "::before" => {
1552 Some(PseudoElement::Before)
1553 },
1554 Some(ref pseudo) if pseudo == ":after" || pseudo == "::after" => {
1555 Some(PseudoElement::After)
1556 },
1557 Some(ref pseudo) if pseudo == "::selection" => Some(PseudoElement::Selection),
1558 Some(ref pseudo) if pseudo == "::marker" => Some(PseudoElement::Marker),
1559 Some(ref pseudo) if pseudo.starts_with(':') => {
1560 is_null = true;
1563 None
1564 },
1565 _ => None,
1566 };
1567
1568 CSSStyleDeclaration::new(
1584 self,
1585 if is_null {
1586 CSSStyleOwner::Null
1587 } else {
1588 CSSStyleOwner::Element(Dom::from_ref(element))
1589 },
1590 pseudo,
1591 CSSModificationAccess::Readonly,
1592 CanGc::note(),
1593 )
1594 }
1595
1596 fn InnerHeight(&self) -> i32 {
1599 self.viewport_details
1600 .get()
1601 .size
1602 .height
1603 .to_i32()
1604 .unwrap_or(0)
1605 }
1606
1607 fn InnerWidth(&self) -> i32 {
1610 self.viewport_details.get().size.width.to_i32().unwrap_or(0)
1611 }
1612
1613 fn ScrollX(&self) -> i32 {
1615 self.scroll_offset().x as i32
1616 }
1617
1618 fn PageXOffset(&self) -> i32 {
1620 self.ScrollX()
1621 }
1622
1623 fn ScrollY(&self) -> i32 {
1625 self.scroll_offset().y as i32
1626 }
1627
1628 fn PageYOffset(&self) -> i32 {
1630 self.ScrollY()
1631 }
1632
1633 fn Scroll(&self, options: &ScrollToOptions) {
1635 let x = options.left.unwrap_or(0.0) as f32;
1640
1641 let y = options.top.unwrap_or(0.0) as f32;
1644
1645 self.scroll(x, y, options.parent.behavior);
1647 }
1648
1649 fn Scroll_(&self, x: f64, y: f64) {
1651 self.scroll(x as f32, y as f32, ScrollBehavior::Auto);
1655 }
1656
1657 fn ScrollTo(&self, options: &ScrollToOptions) {
1662 self.Scroll(options);
1663 }
1664
1665 fn ScrollTo_(&self, x: f64, y: f64) {
1670 self.Scroll_(x, y)
1671 }
1672
1673 fn ScrollBy(&self, options: &ScrollToOptions) {
1675 let mut options = options.clone();
1681 let x = options.left.unwrap_or(0.0);
1682 let x = if x.is_finite() { x } else { 0.0 };
1683 let y = options.top.unwrap_or(0.0);
1684 let y = if y.is_finite() { y } else { 0.0 };
1685
1686 options.left.replace(x + self.ScrollX() as f64);
1688
1689 options.top.replace(y + self.ScrollY() as f64);
1691
1692 self.Scroll(&options)
1694 }
1695
1696 fn ScrollBy_(&self, x: f64, y: f64) {
1698 let mut options = ScrollToOptions::empty();
1702
1703 options.left.replace(x);
1706
1707 options.top.replace(y);
1709
1710 self.ScrollBy(&options);
1712 }
1713
1714 fn ResizeTo(&self, width: i32, height: i32) {
1716 let window_proxy = match self.window_proxy.get() {
1718 Some(proxy) => proxy,
1719 None => return,
1720 };
1721
1722 if !window_proxy.is_auxiliary() {
1725 return;
1726 }
1727
1728 let dpr = self.device_pixel_ratio();
1729 let size = Size2D::new(width, height).to_f32() * dpr;
1730 self.send_to_embedder(EmbedderMsg::ResizeTo(self.webview_id(), size.to_i32()));
1731 }
1732
1733 fn ResizeBy(&self, x: i32, y: i32) {
1735 let size = self.client_window().size();
1736 self.ResizeTo(x + size.width, y + size.height)
1738 }
1739
1740 fn MoveTo(&self, x: i32, y: i32) {
1742 let dpr = self.device_pixel_ratio();
1745 let point = Point2D::new(x, y).to_f32() * dpr;
1746 let msg = EmbedderMsg::MoveTo(self.webview_id(), point.to_i32());
1747 self.send_to_embedder(msg);
1748 }
1749
1750 fn MoveBy(&self, x: i32, y: i32) {
1752 let origin = self.client_window().min;
1753 self.MoveTo(x + origin.x, y + origin.y)
1755 }
1756
1757 fn ScreenX(&self) -> i32 {
1759 self.client_window().min.x
1760 }
1761
1762 fn ScreenY(&self) -> i32 {
1764 self.client_window().min.y
1765 }
1766
1767 fn OuterHeight(&self) -> i32 {
1769 self.client_window().height()
1770 }
1771
1772 fn OuterWidth(&self) -> i32 {
1774 self.client_window().width()
1775 }
1776
1777 fn DevicePixelRatio(&self) -> Finite<f64> {
1779 Finite::wrap(self.device_pixel_ratio().get() as f64)
1780 }
1781
1782 fn Status(&self) -> DOMString {
1784 self.status.borrow().clone()
1785 }
1786
1787 fn SetStatus(&self, status: DOMString) {
1789 *self.status.borrow_mut() = status
1790 }
1791
1792 fn MatchMedia(&self, query: DOMString) -> DomRoot<MediaQueryList> {
1794 let media_query_list = MediaList::parse_media_list(&query, self);
1795 let document = self.Document();
1796 let mql = MediaQueryList::new(&document, media_query_list, CanGc::note());
1797 self.media_query_lists.track(&*mql);
1798 mql
1799 }
1800
1801 fn Fetch(
1803 &self,
1804 input: RequestOrUSVString,
1805 init: RootedTraceableBox<RequestInit>,
1806 comp: InRealm,
1807 can_gc: CanGc,
1808 ) -> Rc<Promise> {
1809 fetch::Fetch(self.upcast(), input, init, comp, can_gc)
1810 }
1811
1812 #[cfg(feature = "bluetooth")]
1813 fn TestRunner(&self) -> DomRoot<TestRunner> {
1814 self.test_runner
1815 .or_init(|| TestRunner::new(self.upcast(), CanGc::note()))
1816 }
1817
1818 fn RunningAnimationCount(&self) -> u32 {
1819 self.document
1820 .get()
1821 .map_or(0, |d| d.animations().running_animation_count() as u32)
1822 }
1823
1824 fn SetName(&self, name: DOMString) {
1826 if let Some(proxy) = self.undiscarded_window_proxy() {
1827 proxy.set_name(name);
1828 }
1829 }
1830
1831 fn Name(&self) -> DOMString {
1833 match self.undiscarded_window_proxy() {
1834 Some(proxy) => proxy.get_name(),
1835 None => "".into(),
1836 }
1837 }
1838
1839 fn Origin(&self) -> USVString {
1841 USVString(self.origin().immutable().ascii_serialization())
1842 }
1843
1844 fn GetSelection(&self) -> Option<DomRoot<Selection>> {
1846 self.document
1847 .get()
1848 .and_then(|d| d.GetSelection(CanGc::note()))
1849 }
1850
1851 fn Event(&self, cx: JSContext, rval: MutableHandleValue) {
1853 if let Some(ref event) = *self.current_event.borrow() {
1854 event.reflector().get_jsobject().safe_to_jsval(cx, rval);
1855 }
1856 }
1857
1858 fn IsSecureContext(&self) -> bool {
1859 self.as_global_scope().is_secure_context()
1860 }
1861
1862 fn NamedGetter(&self, name: DOMString) -> Option<NamedPropertyValue> {
1864 if name.is_empty() {
1865 return None;
1866 }
1867 let document = self.Document();
1868
1869 let iframes: Vec<_> = document
1871 .iframes()
1872 .iter()
1873 .filter(|iframe| {
1874 if let Some(window) = iframe.GetContentWindow() {
1875 return window.get_name() == name;
1876 }
1877 false
1878 })
1879 .collect();
1880
1881 let iframe_iter = iframes.iter().map(|iframe| iframe.upcast::<Element>());
1882
1883 let name = Atom::from(&*name);
1884
1885 let elements_with_name = document.get_elements_with_name(&name);
1887 let name_iter = elements_with_name
1888 .iter()
1889 .map(|element| &**element)
1890 .filter(|elem| is_named_element_with_name_attribute(elem));
1891 let elements_with_id = document.get_elements_with_id(&name);
1892 let id_iter = elements_with_id
1893 .iter()
1894 .map(|element| &**element)
1895 .filter(|elem| is_named_element_with_id_attribute(elem));
1896
1897 for elem in iframe_iter.clone() {
1899 if let Some(nested_window_proxy) = elem
1900 .downcast::<HTMLIFrameElement>()
1901 .and_then(|iframe| iframe.GetContentWindow())
1902 {
1903 return Some(NamedPropertyValue::WindowProxy(nested_window_proxy));
1904 }
1905 }
1906
1907 let mut elements = iframe_iter.chain(name_iter).chain(id_iter);
1908
1909 let first = elements.next()?;
1910
1911 if elements.next().is_none() {
1912 return Some(NamedPropertyValue::Element(DomRoot::from_ref(first)));
1914 }
1915
1916 #[derive(JSTraceable, MallocSizeOf)]
1918 struct WindowNamedGetter {
1919 #[no_trace]
1920 name: Atom,
1921 }
1922 impl CollectionFilter for WindowNamedGetter {
1923 fn filter(&self, elem: &Element, _root: &Node) -> bool {
1924 let type_ = match elem.upcast::<Node>().type_id() {
1925 NodeTypeId::Element(ElementTypeId::HTMLElement(type_)) => type_,
1926 _ => return false,
1927 };
1928 if elem.get_id().as_ref() == Some(&self.name) {
1929 return true;
1930 }
1931 match type_ {
1932 HTMLElementTypeId::HTMLEmbedElement |
1933 HTMLElementTypeId::HTMLFormElement |
1934 HTMLElementTypeId::HTMLImageElement |
1935 HTMLElementTypeId::HTMLObjectElement => {
1936 elem.get_name().as_ref() == Some(&self.name)
1937 },
1938 _ => false,
1939 }
1940 }
1941 }
1942 let collection = HTMLCollection::create(
1943 self,
1944 document.upcast(),
1945 Box::new(WindowNamedGetter { name }),
1946 CanGc::note(),
1947 );
1948 Some(NamedPropertyValue::HTMLCollection(collection))
1949 }
1950
1951 fn SupportedPropertyNames(&self) -> Vec<DOMString> {
1953 let mut names_with_first_named_element_map: HashMap<&Atom, &Element> = HashMap::new();
1954
1955 let document = self.Document();
1956 let name_map = document.name_map();
1957 for (name, elements) in &name_map.0 {
1958 if name.is_empty() {
1959 continue;
1960 }
1961 let mut name_iter = elements
1962 .iter()
1963 .filter(|elem| is_named_element_with_name_attribute(elem));
1964 if let Some(first) = name_iter.next() {
1965 names_with_first_named_element_map.insert(name, first);
1966 }
1967 }
1968 let id_map = document.id_map();
1969 for (id, elements) in &id_map.0 {
1970 if id.is_empty() {
1971 continue;
1972 }
1973 let mut id_iter = elements
1974 .iter()
1975 .filter(|elem| is_named_element_with_id_attribute(elem));
1976 if let Some(first) = id_iter.next() {
1977 match names_with_first_named_element_map.entry(id) {
1978 Entry::Vacant(entry) => drop(entry.insert(first)),
1979 Entry::Occupied(mut entry) => {
1980 if first.upcast::<Node>().is_before(entry.get().upcast()) {
1981 *entry.get_mut() = first;
1982 }
1983 },
1984 }
1985 }
1986 }
1987
1988 let mut names_with_first_named_element_vec: Vec<(&Atom, &Element)> =
1989 names_with_first_named_element_map
1990 .iter()
1991 .map(|(k, v)| (*k, *v))
1992 .collect();
1993 names_with_first_named_element_vec.sort_unstable_by(|a, b| {
1994 if a.1 == b.1 {
1995 a.0.cmp(b.0)
1998 } else if a.1.upcast::<Node>().is_before(b.1.upcast::<Node>()) {
1999 cmp::Ordering::Less
2000 } else {
2001 cmp::Ordering::Greater
2002 }
2003 });
2004
2005 names_with_first_named_element_vec
2006 .iter()
2007 .map(|(k, _v)| DOMString::from(&***k))
2008 .collect()
2009 }
2010
2011 fn StructuredClone(
2013 &self,
2014 cx: JSContext,
2015 value: HandleValue,
2016 options: RootedTraceableBox<StructuredSerializeOptions>,
2017 retval: MutableHandleValue,
2018 ) -> Fallible<()> {
2019 self.as_global_scope()
2020 .structured_clone(cx, value, options, retval)
2021 }
2022
2023 fn TrustedTypes(&self, can_gc: CanGc) -> DomRoot<TrustedTypePolicyFactory> {
2024 self.trusted_types
2025 .or_init(|| TrustedTypePolicyFactory::new(self.as_global_scope(), can_gc))
2026 }
2027}
2028
2029impl Window {
2030 pub(crate) fn scroll_offset(&self) -> Vector2D<f32, LayoutPixel> {
2031 self.scroll_offset_query_with_external_scroll_id(self.pipeline_id().root_scroll_id())
2032 }
2033
2034 #[allow(unsafe_code)]
2037 pub(crate) fn create_named_properties_object(
2038 cx: JSContext,
2039 proto: HandleObject,
2040 object: MutableHandleObject,
2041 ) {
2042 window_named_properties::create(cx, proto, object)
2043 }
2044
2045 pub(crate) fn current_event(&self) -> Option<DomRoot<Event>> {
2046 self.current_event
2047 .borrow()
2048 .as_ref()
2049 .map(|e| DomRoot::from_ref(&**e))
2050 }
2051
2052 pub(crate) fn set_current_event(&self, event: Option<&Event>) -> Option<DomRoot<Event>> {
2053 let current = self.current_event();
2054 *self.current_event.borrow_mut() = event.map(Dom::from_ref);
2055 current
2056 }
2057
2058 fn post_message_impl(
2060 &self,
2061 target_origin: &USVString,
2062 source_origin: ImmutableOrigin,
2063 source: &Window,
2064 cx: JSContext,
2065 message: HandleValue,
2066 transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
2067 ) -> ErrorResult {
2068 let data = structuredclone::write(cx, message, Some(transfer))?;
2070
2071 let target_origin = match target_origin.0[..].as_ref() {
2073 "*" => None,
2074 "/" => Some(source_origin.clone()),
2075 url => match ServoUrl::parse(url) {
2076 Ok(url) => Some(url.origin().clone()),
2077 Err(_) => return Err(Error::Syntax(None)),
2078 },
2079 };
2080
2081 self.post_message(target_origin, source_origin, &source.window_proxy(), data);
2083 Ok(())
2084 }
2085
2086 pub(crate) fn paint_worklet(&self) -> DomRoot<Worklet> {
2088 self.paint_worklet
2089 .or_init(|| self.new_paint_worklet(CanGc::note()))
2090 }
2091
2092 pub(crate) fn has_document(&self) -> bool {
2093 self.document.get().is_some()
2094 }
2095
2096 pub(crate) fn clear_js_runtime(&self) {
2097 self.as_global_scope()
2098 .remove_web_messaging_and_dedicated_workers_infra();
2099
2100 if let Some(custom_elements) = self.custom_element_registry.get() {
2103 custom_elements.teardown();
2104 }
2105
2106 self.current_state.set(WindowState::Zombie);
2107 *self.js_runtime.borrow_mut() = None;
2108
2109 if let Some(proxy) = self.window_proxy.get() {
2112 let pipeline_id = self.pipeline_id();
2113 if let Some(currently_active) = proxy.currently_active() {
2114 if currently_active == pipeline_id {
2115 self.window_proxy.set(None);
2116 }
2117 }
2118 }
2119
2120 if let Some(performance) = self.performance.get() {
2121 performance.clear_and_disable_performance_entry_buffer();
2122 }
2123 self.as_global_scope()
2124 .task_manager()
2125 .cancel_all_tasks_and_ignore_future_tasks();
2126 }
2127
2128 pub(crate) fn scroll(&self, x: f32, y: f32, behavior: ScrollBehavior) {
2130 let xfinite = if x.is_finite() { x } else { 0.0 };
2132 let yfinite = if y.is_finite() { y } else { 0.0 };
2133
2134 let viewport = self.viewport_details.get().size;
2144
2145 let scrolling_area = self.scrolling_area_query(None).to_f32();
2164 let x = xfinite.clamp(0.0, 0.0f32.max(scrolling_area.width() - viewport.width));
2165 let y = yfinite.clamp(0.0, 0.0f32.max(scrolling_area.height() - viewport.height));
2166
2167 let scroll_offset = self.scroll_offset();
2170 if x == scroll_offset.x && y == scroll_offset.y {
2171 return;
2172 }
2173
2174 self.perform_a_scroll(x, y, self.pipeline_id().root_scroll_id(), behavior, None);
2179 }
2180
2181 pub(crate) fn perform_a_scroll(
2183 &self,
2184 x: f32,
2185 y: f32,
2186 scroll_id: ExternalScrollId,
2187 _behavior: ScrollBehavior,
2188 element: Option<&Element>,
2189 ) {
2190 let reflow_phases_run =
2194 self.reflow(ReflowGoal::UpdateScrollNode(scroll_id, Vector2D::new(x, y)));
2195 if reflow_phases_run.needs_frame() {
2196 self.compositor_api().generate_frame();
2197 }
2198
2199 if reflow_phases_run.contains(ReflowPhasesRun::UpdatedScrollNodeOffset) {
2204 match element {
2205 Some(el) => self.Document().handle_element_scroll_event(el),
2206 None => self.Document().handle_viewport_scroll_event(),
2207 };
2208 }
2209 }
2210
2211 pub(crate) fn device_pixel_ratio(&self) -> Scale<f32, CSSPixel, DevicePixel> {
2212 self.viewport_details.get().hidpi_scale_factor
2213 }
2214
2215 fn client_window(&self) -> DeviceIndependentIntRect {
2216 let (sender, receiver) = generic_channel::channel().expect("Failed to create IPC channel!");
2217
2218 self.send_to_embedder(EmbedderMsg::GetWindowRect(self.webview_id(), sender));
2219
2220 receiver.recv().unwrap_or_default()
2221 }
2222
2223 #[allow(unsafe_code)]
2226 pub(crate) fn advance_animation_clock(&self, delta_ms: i32) {
2227 self.Document()
2228 .advance_animation_timeline_for_testing(delta_ms as f64 / 1000.);
2229 ScriptThread::handle_tick_all_animations_for_testing(self.pipeline_id());
2230 }
2231
2232 pub(crate) fn reflow(&self, reflow_goal: ReflowGoal) -> ReflowPhasesRun {
2240 let document = self.Document();
2241
2242 if !document.is_fully_active() {
2244 return ReflowPhasesRun::empty();
2245 }
2246
2247 self.Document().ensure_safe_to_run_script_or_layout();
2248
2249 let pipeline_id = self.pipeline_id();
2253 if reflow_goal == ReflowGoal::UpdateTheRendering &&
2254 self.layout_blocker.get().layout_blocked()
2255 {
2256 debug!("Suppressing pre-load-event reflow pipeline {pipeline_id}");
2257 return ReflowPhasesRun::empty();
2258 }
2259
2260 debug!("script: performing reflow for goal {reflow_goal:?}");
2261 let marker = if self.need_emit_timeline_marker(TimelineMarkerType::Reflow) {
2262 Some(TimelineMarker::start("Reflow".to_owned()))
2263 } else {
2264 None
2265 };
2266
2267 let restyle_reason = document.restyle_reason();
2268 document.clear_restyle_reasons();
2269 let restyle = if restyle_reason.needs_restyle() {
2270 debug!("Invalidating layout cache due to reflow condition {restyle_reason:?}",);
2271 self.layout_marker.borrow().set(false);
2273 *self.layout_marker.borrow_mut() = Rc::new(Cell::new(true));
2275
2276 let stylesheets_changed = document.flush_stylesheets_for_reflow();
2277 let pending_restyles = document.drain_pending_restyles();
2278 let dirty_root = document
2279 .take_dirty_root()
2280 .filter(|_| !stylesheets_changed)
2281 .or_else(|| document.GetDocumentElement())
2282 .map(|root| root.upcast::<Node>().to_trusted_node_address());
2283
2284 Some(ReflowRequestRestyle {
2285 reason: restyle_reason,
2286 dirty_root,
2287 stylesheets_changed,
2288 pending_restyles,
2289 })
2290 } else {
2291 None
2292 };
2293
2294 let reflow = ReflowRequest {
2295 document: document.upcast::<Node>().to_trusted_node_address(),
2296 restyle,
2297 viewport_details: self.viewport_details.get(),
2298 origin: self.origin().immutable().clone(),
2299 reflow_goal,
2300 dom_count: document.dom_count(),
2301 animation_timeline_value: document.current_animation_timeline_value(),
2302 animations: document.animations().sets.clone(),
2303 node_to_animating_image_map: document.image_animation_manager().node_to_image_map(),
2304 theme: self.theme.get(),
2305 highlighted_dom_node: document.highlighted_dom_node().map(|node| node.to_opaque()),
2306 };
2307
2308 let Some(reflow_result) = self.layout.borrow_mut().reflow(reflow) else {
2309 return ReflowPhasesRun::empty();
2310 };
2311
2312 debug!("script: layout complete");
2313 if let Some(marker) = marker {
2314 self.emit_timeline_marker(marker.end());
2315 }
2316
2317 self.handle_pending_images_post_reflow(
2318 reflow_result.pending_images,
2319 reflow_result.pending_rasterization_images,
2320 reflow_result.pending_svg_elements_for_serialization,
2321 );
2322
2323 if let Some(iframe_sizes) = reflow_result.iframe_sizes {
2324 document
2325 .iframes_mut()
2326 .handle_new_iframe_sizes_after_layout(self, iframe_sizes);
2327 }
2328
2329 document.update_animations_post_reflow();
2330 self.update_constellation_epoch();
2331
2332 reflow_result.reflow_phases_run
2333 }
2334
2335 pub(crate) fn maybe_send_idle_document_state_to_constellation(&self) {
2336 if !opts::get().wait_for_stable_image {
2337 return;
2338 }
2339
2340 if self.has_sent_idle_message.get() {
2341 return;
2342 }
2343
2344 let document = self.Document();
2345 if document.ReadyState() != DocumentReadyState::Complete {
2346 return;
2347 }
2348
2349 if document.GetDocumentElement().is_some_and(|elem| {
2353 elem.has_class(&atom!("reftest-wait"), CaseSensitivity::CaseSensitive) ||
2354 elem.has_class(&Atom::from("test-wait"), CaseSensitivity::CaseSensitive)
2355 }) {
2356 return;
2357 }
2358
2359 if self.font_context().web_fonts_still_loading() != 0 {
2360 return;
2361 }
2362
2363 if !self.pending_layout_images.borrow().is_empty() ||
2364 !self.pending_images_for_rasterization.borrow().is_empty()
2365 {
2366 return;
2367 }
2368
2369 if self.Document().needs_rendering_update() {
2370 return;
2371 }
2372
2373 debug!(
2377 "{:?}: Sending DocumentState::Idle to Constellation",
2378 self.pipeline_id()
2379 );
2380 self.send_to_constellation(ScriptToConstellationMessage::SetDocumentState(
2381 DocumentState::Idle,
2382 ));
2383 self.has_sent_idle_message.set(true);
2384 }
2385
2386 pub(crate) fn reflow_if_reflow_timer_expired(&self) {
2389 if !matches!(
2392 self.layout_blocker.get(),
2393 LayoutBlocker::Parsing(instant) if instant + INITIAL_REFLOW_DELAY < Instant::now()
2394 ) {
2395 return;
2396 }
2397 self.allow_layout_if_necessary();
2398 }
2399
2400 pub(crate) fn prevent_layout_until_load_event(&self) {
2404 if !matches!(self.layout_blocker.get(), LayoutBlocker::WaitingForParse) {
2407 return;
2408 }
2409
2410 self.layout_blocker
2411 .set(LayoutBlocker::Parsing(Instant::now()));
2412 }
2413
2414 pub(crate) fn allow_layout_if_necessary(&self) {
2417 if matches!(
2418 self.layout_blocker.get(),
2419 LayoutBlocker::FiredLoadEventOrParsingTimerExpired
2420 ) {
2421 return;
2422 }
2423
2424 self.layout_blocker
2425 .set(LayoutBlocker::FiredLoadEventOrParsingTimerExpired);
2426
2427 if self.Document().update_the_rendering().needs_frame() {
2439 self.compositor_api().generate_frame();
2440 }
2441 }
2442
2443 pub(crate) fn layout_blocked(&self) -> bool {
2444 self.layout_blocker.get().layout_blocked()
2445 }
2446
2447 pub(crate) fn update_constellation_epoch(&self) {
2450 if !opts::get().wait_for_stable_image {
2451 return;
2452 }
2453
2454 let epoch = self.layout.borrow().current_epoch();
2455 debug!(
2456 "{:?}: Updating constellation epoch: {epoch:?}",
2457 self.pipeline_id()
2458 );
2459 let (sender, receiver) = ipc::channel().expect("Failed to create IPC channel!");
2460 let event = ScriptToConstellationMessage::SetLayoutEpoch(epoch, sender);
2461 self.send_to_constellation(event);
2462 let _ = receiver.recv();
2463 }
2464
2465 pub(crate) fn layout_reflow(&self, query_msg: QueryMsg) {
2467 self.reflow(ReflowGoal::LayoutQuery(query_msg));
2468 }
2469
2470 pub(crate) fn resolved_font_style_query(
2471 &self,
2472 node: &Node,
2473 value: String,
2474 ) -> Option<ServoArc<Font>> {
2475 self.layout_reflow(QueryMsg::ResolvedFontStyleQuery);
2476
2477 let document = self.Document();
2478 let animations = document.animations().sets.clone();
2479 self.layout.borrow().query_resolved_font_style(
2480 node.to_trusted_node_address(),
2481 &value,
2482 animations,
2483 document.current_animation_timeline_value(),
2484 )
2485 }
2486
2487 pub(crate) fn box_area_query_without_reflow(
2492 &self,
2493 node: &Node,
2494 area: BoxAreaType,
2495 ) -> Option<UntypedRect<Au>> {
2496 let layout = self.layout.borrow();
2497 layout.ensure_stacking_context_tree(self.viewport_details.get());
2498 layout.query_box_area(node.to_trusted_node_address(), area)
2499 }
2500
2501 pub(crate) fn box_area_query(&self, node: &Node, area: BoxAreaType) -> Option<UntypedRect<Au>> {
2502 self.layout_reflow(QueryMsg::BoxArea);
2503 self.box_area_query_without_reflow(node, area)
2504 }
2505
2506 pub(crate) fn box_areas_query(&self, node: &Node, area: BoxAreaType) -> Vec<UntypedRect<Au>> {
2507 self.layout_reflow(QueryMsg::BoxAreas);
2508 self.layout
2509 .borrow()
2510 .query_box_areas(node.to_trusted_node_address(), area)
2511 }
2512
2513 pub(crate) fn client_rect_query(&self, node: &Node) -> UntypedRect<i32> {
2514 self.layout_reflow(QueryMsg::ClientRectQuery);
2515 self.layout
2516 .borrow()
2517 .query_client_rect(node.to_trusted_node_address())
2518 }
2519
2520 pub(crate) fn scrolling_area_query(&self, node: Option<&Node>) -> UntypedRect<i32> {
2523 self.layout_reflow(QueryMsg::ScrollingAreaOrOffsetQuery);
2524 self.layout
2525 .borrow()
2526 .query_scrolling_area(node.map(Node::to_trusted_node_address))
2527 }
2528
2529 pub(crate) fn scroll_offset_query(&self, node: &Node) -> Vector2D<f32, LayoutPixel> {
2530 let external_scroll_id = ExternalScrollId(
2531 combine_id_with_fragment_type(node.to_opaque().id(), FragmentType::FragmentBody),
2532 self.pipeline_id().into(),
2533 );
2534 self.scroll_offset_query_with_external_scroll_id(external_scroll_id)
2535 }
2536
2537 fn scroll_offset_query_with_external_scroll_id(
2538 &self,
2539 external_scroll_id: ExternalScrollId,
2540 ) -> Vector2D<f32, LayoutPixel> {
2541 self.layout_reflow(QueryMsg::ScrollingAreaOrOffsetQuery);
2542 self.scroll_offset_query_with_external_scroll_id_no_reflow(external_scroll_id)
2543 }
2544
2545 fn scroll_offset_query_with_external_scroll_id_no_reflow(
2546 &self,
2547 external_scroll_id: ExternalScrollId,
2548 ) -> Vector2D<f32, LayoutPixel> {
2549 self.layout
2550 .borrow()
2551 .scroll_offset(external_scroll_id)
2552 .unwrap_or_default()
2553 }
2554
2555 pub(crate) fn scroll_an_element(
2558 &self,
2559 element: &Element,
2560 x: f32,
2561 y: f32,
2562 behavior: ScrollBehavior,
2563 ) {
2564 let scroll_id = ExternalScrollId(
2565 combine_id_with_fragment_type(
2566 element.upcast::<Node>().to_opaque().id(),
2567 FragmentType::FragmentBody,
2568 ),
2569 self.pipeline_id().into(),
2570 );
2571
2572 self.perform_a_scroll(x, y, scroll_id, behavior, Some(element));
2576 }
2577
2578 pub(crate) fn resolved_style_query(
2579 &self,
2580 element: TrustedNodeAddress,
2581 pseudo: Option<PseudoElement>,
2582 property: PropertyId,
2583 ) -> DOMString {
2584 self.layout_reflow(QueryMsg::ResolvedStyleQuery);
2585
2586 let document = self.Document();
2587 let animations = document.animations().sets.clone();
2588 DOMString::from(self.layout.borrow().query_resolved_style(
2589 element,
2590 pseudo,
2591 property,
2592 animations,
2593 document.current_animation_timeline_value(),
2594 ))
2595 }
2596
2597 pub(crate) fn get_iframe_viewport_details_if_known(
2601 &self,
2602 browsing_context_id: BrowsingContextId,
2603 ) -> Option<ViewportDetails> {
2604 self.layout_reflow(QueryMsg::InnerWindowDimensionsQuery);
2606 self.Document()
2607 .iframes()
2608 .get(browsing_context_id)
2609 .and_then(|iframe| iframe.size)
2610 }
2611
2612 #[allow(unsafe_code)]
2613 pub(crate) fn offset_parent_query(
2614 &self,
2615 node: &Node,
2616 ) -> (Option<DomRoot<Element>>, UntypedRect<Au>) {
2617 self.layout_reflow(QueryMsg::OffsetParentQuery);
2618 let response = self
2619 .layout
2620 .borrow()
2621 .query_offset_parent(node.to_trusted_node_address());
2622 let element = response.node_address.and_then(|parent_node_address| {
2623 let node = unsafe { from_untrusted_node_address(parent_node_address) };
2624 DomRoot::downcast(node)
2625 });
2626 (element, response.rect)
2627 }
2628
2629 pub(crate) fn scroll_container_query(
2630 &self,
2631 node: &Node,
2632 query_type: ScrollContainerQueryType,
2633 ) -> Option<ScrollContainerResponse> {
2634 self.layout_reflow(QueryMsg::ScrollParentQuery);
2635 self.layout
2636 .borrow()
2637 .query_scroll_container(node.to_trusted_node_address(), query_type)
2638 }
2639
2640 pub(crate) fn text_index_query(
2641 &self,
2642 node: &Node,
2643 point_in_node: UntypedPoint2D<f32>,
2644 ) -> Option<usize> {
2645 self.layout_reflow(QueryMsg::TextIndexQuery);
2646 self.layout
2647 .borrow()
2648 .query_text_indext(node.to_opaque(), point_in_node)
2649 }
2650
2651 pub(crate) fn elements_from_point_query(
2652 &self,
2653 point: LayoutPoint,
2654 flags: ElementsFromPointFlags,
2655 ) -> Vec<ElementsFromPointResult> {
2656 self.layout_reflow(QueryMsg::ElementsFromPoint);
2657 self.layout().query_elements_from_point(point, flags)
2658 }
2659
2660 pub(crate) fn hit_test_from_input_event(
2661 &self,
2662 input_event: &ConstellationInputEvent,
2663 ) -> Option<HitTestResult> {
2664 self.hit_test_from_point_in_viewport(
2665 input_event.hit_test_result.as_ref()?.point_in_viewport,
2666 )
2667 }
2668
2669 #[allow(unsafe_code)]
2670 pub(crate) fn hit_test_from_point_in_viewport(
2671 &self,
2672 point_in_frame: Point2D<f32, CSSPixel>,
2673 ) -> Option<HitTestResult> {
2674 let result = self
2675 .elements_from_point_query(point_in_frame.cast_unit(), ElementsFromPointFlags::empty())
2676 .into_iter()
2677 .nth(0)?;
2678
2679 let point_relative_to_initial_containing_block =
2680 point_in_frame + self.scroll_offset().cast_unit();
2681
2682 let address = UntrustedNodeAddress(result.node.0 as *const c_void);
2685 Some(HitTestResult {
2686 node: unsafe { from_untrusted_node_address(address) },
2687 cursor: result.cursor,
2688 point_in_node: result.point_in_target,
2689 point_in_frame,
2690 point_relative_to_initial_containing_block,
2691 })
2692 }
2693
2694 #[allow(unsafe_code)]
2695 pub(crate) fn init_window_proxy(&self, window_proxy: &WindowProxy) {
2696 assert!(self.window_proxy.get().is_none());
2697 self.window_proxy.set(Some(window_proxy));
2698 }
2699
2700 #[allow(unsafe_code)]
2701 pub(crate) fn init_document(&self, document: &Document) {
2702 assert!(self.document.get().is_none());
2703 assert!(document.window() == self);
2704 self.document.set(Some(document));
2705
2706 if self.unminify_css {
2707 *self.unminified_css_dir.borrow_mut() = Some(unminified_path("unminified-css"));
2708 }
2709 }
2710
2711 pub(crate) fn load_url(
2715 &self,
2716 history_handling: NavigationHistoryBehavior,
2717 force_reload: bool,
2718 load_data: LoadData,
2719 can_gc: CanGc,
2720 ) {
2721 let doc = self.Document();
2722
2723 let initiator_origin_snapshot = &load_data.load_origin;
2725
2726 if !force_reload &&
2729 load_data.url.as_url()[..Position::AfterQuery] ==
2730 doc.url().as_url()[..Position::AfterQuery]
2731 {
2732 if let Some(fragment) = load_data.url.fragment() {
2735 let webdriver_sender = self.webdriver_load_status_sender.borrow().clone();
2736 if let Some(ref sender) = webdriver_sender {
2737 let _ = sender.send(WebDriverLoadStatus::NavigationStart);
2738 }
2739
2740 self.send_to_constellation(ScriptToConstellationMessage::NavigatedToFragment(
2741 load_data.url.clone(),
2742 history_handling,
2743 ));
2744 doc.check_and_scroll_fragment(fragment);
2745 let this = Trusted::new(self);
2746 let old_url = doc.url().into_string();
2747 let new_url = load_data.url.clone().into_string();
2748 let task = task!(hashchange_event: move || {
2749 let this = this.root();
2750 let event = HashChangeEvent::new(
2751 &this,
2752 atom!("hashchange"),
2753 false,
2754 false,
2755 old_url,
2756 new_url,
2757 CanGc::note());
2758 event.upcast::<Event>().fire(this.upcast::<EventTarget>(), CanGc::note());
2759 if let Some(sender) = webdriver_sender {
2760 let _ = sender.send(WebDriverLoadStatus::NavigationStop);
2761 }
2762 });
2763 self.as_global_scope()
2764 .task_manager()
2765 .dom_manipulation_task_source()
2766 .queue(task);
2767 doc.set_url(load_data.url.clone());
2768 return;
2769 }
2770 }
2771
2772 let pipeline_id = self.pipeline_id();
2774 let window_proxy = self.window_proxy();
2775 if let Some(active) = window_proxy.currently_active() {
2776 if pipeline_id == active && doc.is_prompting_or_unloading() {
2777 return;
2778 }
2779 }
2780
2781 if doc.prompt_to_unload(false, can_gc) {
2783 let window_proxy = self.window_proxy();
2784 if window_proxy.parent().is_some() {
2785 window_proxy.start_delaying_load_events_mode();
2789 }
2790
2791 let resolved_history_handling = if history_handling == NavigationHistoryBehavior::Auto {
2793 if let LoadOrigin::Script(initiator_origin) = initiator_origin_snapshot {
2799 if load_data.url == doc.url() && initiator_origin.same_origin(doc.origin()) {
2800 NavigationHistoryBehavior::Replace
2801 } else {
2802 NavigationHistoryBehavior::Push
2803 }
2804 } else {
2805 NavigationHistoryBehavior::Push
2807 }
2808 } else if load_data.url.scheme() == "javascript" || doc.is_initial_about_blank() {
2811 NavigationHistoryBehavior::Replace
2812 } else {
2813 NavigationHistoryBehavior::Push
2814 };
2815
2816 if let Some(sender) = self.webdriver_load_status_sender.borrow().as_ref() {
2817 let _ = sender.send(WebDriverLoadStatus::NavigationStart);
2818 }
2819
2820 ScriptThread::navigate(pipeline_id, load_data, resolved_history_handling);
2822 };
2823 }
2824
2825 pub(crate) fn set_viewport_details(&self, size: ViewportDetails) {
2826 self.viewport_details.set(size);
2827 }
2828
2829 pub(crate) fn viewport_details(&self) -> ViewportDetails {
2830 self.viewport_details.get()
2831 }
2832
2833 pub(crate) fn theme(&self) -> Theme {
2835 self.theme.get()
2836 }
2837
2838 pub(crate) fn handle_theme_change(&self, new_theme: Theme) {
2840 if self.theme.get() == new_theme {
2841 return;
2842 }
2843 self.theme.set(new_theme);
2844 self.Document()
2845 .add_restyle_reason(RestyleReason::ThemeChanged);
2846 }
2847
2848 pub(crate) fn get_url(&self) -> ServoUrl {
2849 self.Document().url()
2850 }
2851
2852 pub(crate) fn windowproxy_handler(&self) -> &'static WindowProxyHandler {
2853 self.dom_static.windowproxy_handler
2854 }
2855
2856 pub(crate) fn add_resize_event(&self, event: ViewportDetails, event_type: WindowSizeType) {
2857 *self.unhandled_resize_event.borrow_mut() = Some((event, event_type))
2860 }
2861
2862 pub(crate) fn take_unhandled_resize_event(&self) -> Option<(ViewportDetails, WindowSizeType)> {
2863 self.unhandled_resize_event.borrow_mut().take()
2864 }
2865
2866 pub(crate) fn has_unhandled_resize_event(&self) -> bool {
2868 self.unhandled_resize_event.borrow().is_some()
2869 }
2870
2871 pub(crate) fn set_viewport_size(&self, new_viewport_size: UntypedSize2D<f32>) {
2872 let new_viewport_size = Size2D::new(
2873 Au::from_f32_px(new_viewport_size.width),
2874 Au::from_f32_px(new_viewport_size.height),
2875 );
2876 if new_viewport_size == self.current_viewport_size.get() {
2877 return;
2878 }
2879
2880 self.current_viewport_size.set(new_viewport_size);
2881
2882 self.Document()
2885 .add_restyle_reason(RestyleReason::ViewportSizeChanged);
2886
2887 if self.layout().device().used_viewport_units() {
2890 self.Document().dirty_all_nodes();
2891 }
2892 }
2893
2894 pub(crate) fn suspend(&self, can_gc: CanGc) {
2895 self.as_global_scope().suspend();
2897
2898 if self.window_proxy().currently_active() == Some(self.global().pipeline_id()) {
2900 self.window_proxy().unset_currently_active(can_gc);
2901 }
2902
2903 self.Gc();
2908 }
2909
2910 pub(crate) fn resume(&self, can_gc: CanGc) {
2911 self.as_global_scope().resume();
2913
2914 self.window_proxy().set_currently_active(self, can_gc);
2916
2917 self.Document().title_changed();
2920 }
2921
2922 pub(crate) fn need_emit_timeline_marker(&self, timeline_type: TimelineMarkerType) -> bool {
2923 let markers = self.devtools_markers.borrow();
2924 markers.contains(&timeline_type)
2925 }
2926
2927 pub(crate) fn emit_timeline_marker(&self, marker: TimelineMarker) {
2928 let sender = self.devtools_marker_sender.borrow();
2929 let sender = sender.as_ref().expect("There is no marker sender");
2930 sender.send(Some(marker)).unwrap();
2931 }
2932
2933 pub(crate) fn set_devtools_timeline_markers(
2934 &self,
2935 markers: Vec<TimelineMarkerType>,
2936 reply: IpcSender<Option<TimelineMarker>>,
2937 ) {
2938 *self.devtools_marker_sender.borrow_mut() = Some(reply);
2939 self.devtools_markers.borrow_mut().extend(markers);
2940 }
2941
2942 pub(crate) fn drop_devtools_timeline_markers(&self, markers: Vec<TimelineMarkerType>) {
2943 let mut devtools_markers = self.devtools_markers.borrow_mut();
2944 for marker in markers {
2945 devtools_markers.remove(&marker);
2946 }
2947 if devtools_markers.is_empty() {
2948 *self.devtools_marker_sender.borrow_mut() = None;
2949 }
2950 }
2951
2952 pub(crate) fn set_webdriver_script_chan(&self, chan: Option<IpcSender<WebDriverJSResult>>) {
2953 *self.webdriver_script_chan.borrow_mut() = chan;
2954 }
2955
2956 pub(crate) fn set_webdriver_load_status_sender(
2957 &self,
2958 sender: Option<GenericSender<WebDriverLoadStatus>>,
2959 ) {
2960 *self.webdriver_load_status_sender.borrow_mut() = sender;
2961 }
2962
2963 pub(crate) fn is_alive(&self) -> bool {
2964 self.current_state.get() == WindowState::Alive
2965 }
2966
2967 pub(crate) fn is_top_level(&self) -> bool {
2969 self.parent_info.is_none()
2970 }
2971
2972 pub(crate) fn run_the_resize_steps(&self, can_gc: CanGc) -> bool {
2977 let Some((new_size, size_type)) = self.take_unhandled_resize_event() else {
2978 return false;
2979 };
2980
2981 if self.viewport_details() == new_size {
2982 return false;
2983 }
2984
2985 let _realm = enter_realm(self);
2986 debug!(
2987 "Resizing Window for pipeline {:?} from {:?} to {new_size:?}",
2988 self.pipeline_id(),
2989 self.viewport_details(),
2990 );
2991 self.set_viewport_details(new_size);
2992
2993 self.Document()
2997 .add_restyle_reason(RestyleReason::ViewportSizeChanged);
2998
2999 if self.layout().device().used_viewport_units() {
3002 self.Document().dirty_all_nodes();
3003 }
3004
3005 if size_type == WindowSizeType::Resize {
3007 let uievent = UIEvent::new(
3008 self,
3009 DOMString::from("resize"),
3010 EventBubbles::DoesNotBubble,
3011 EventCancelable::NotCancelable,
3012 Some(self),
3013 0i32,
3014 can_gc,
3015 );
3016 uievent.upcast::<Event>().fire(self.upcast(), can_gc);
3017 }
3018
3019 true
3020 }
3021
3022 pub(crate) fn evaluate_media_queries_and_report_changes(&self, can_gc: CanGc) {
3025 let _realm = enter_realm(self);
3026
3027 rooted_vec!(let mut mql_list);
3028 self.media_query_lists.for_each(|mql| {
3029 if let MediaQueryListMatchState::Changed = mql.evaluate_changes() {
3030 mql_list.push(Dom::from_ref(&*mql));
3032 }
3033 });
3034 for mql in mql_list.iter() {
3036 let event = MediaQueryListEvent::new(
3037 &mql.global(),
3038 atom!("change"),
3039 false,
3040 false,
3041 mql.Media(),
3042 mql.Matches(),
3043 can_gc,
3044 );
3045 event
3046 .upcast::<Event>()
3047 .fire(mql.upcast::<EventTarget>(), can_gc);
3048 }
3049 }
3050
3051 pub(crate) fn set_throttled(&self, throttled: bool) {
3053 self.throttled.set(throttled);
3054 if throttled {
3055 self.as_global_scope().slow_down_timers();
3056 } else {
3057 self.as_global_scope().speed_up_timers();
3058 }
3059 }
3060
3061 pub(crate) fn throttled(&self) -> bool {
3062 self.throttled.get()
3063 }
3064
3065 pub(crate) fn unminified_css_dir(&self) -> Option<String> {
3066 self.unminified_css_dir.borrow().clone()
3067 }
3068
3069 pub(crate) fn local_script_source(&self) -> &Option<String> {
3070 &self.local_script_source
3071 }
3072
3073 pub(crate) fn set_navigation_start(&self) {
3074 self.navigation_start.set(CrossProcessInstant::now());
3075 }
3076
3077 pub(crate) fn send_to_embedder(&self, msg: EmbedderMsg) {
3078 self.as_global_scope()
3079 .script_to_embedder_chan()
3080 .send(msg)
3081 .unwrap();
3082 }
3083
3084 pub(crate) fn send_to_constellation(&self, msg: ScriptToConstellationMessage) {
3085 self.as_global_scope()
3086 .script_to_constellation_chan()
3087 .send(msg)
3088 .unwrap();
3089 }
3090
3091 #[cfg(feature = "webxr")]
3092 pub(crate) fn in_immersive_xr_session(&self) -> bool {
3093 self.navigator
3094 .get()
3095 .as_ref()
3096 .and_then(|nav| nav.xr())
3097 .is_some_and(|xr| xr.pending_or_active_session())
3098 }
3099
3100 #[cfg(not(feature = "webxr"))]
3101 pub(crate) fn in_immersive_xr_session(&self) -> bool {
3102 false
3103 }
3104
3105 #[allow(unsafe_code)]
3106 fn handle_pending_images_post_reflow(
3107 &self,
3108 pending_images: Vec<PendingImage>,
3109 pending_rasterization_images: Vec<PendingRasterizationImage>,
3110 pending_svg_element_for_serialization: Vec<UntrustedNodeAddress>,
3111 ) {
3112 let pipeline_id = self.pipeline_id();
3113 for image in pending_images {
3114 let id = image.id;
3115 let node = unsafe { from_untrusted_node_address(image.node) };
3116
3117 if let PendingImageState::Unrequested(ref url) = image.state {
3118 fetch_image_for_layout(url.clone(), &node, id, self.image_cache.clone());
3119 }
3120
3121 let mut images = self.pending_layout_images.borrow_mut();
3122 if !images.contains_key(&id) {
3123 let trusted_node = Trusted::new(&*node);
3124 let sender = self.register_image_cache_listener(id, move |response| {
3125 trusted_node
3126 .root()
3127 .owner_window()
3128 .pending_layout_image_notification(response);
3129 });
3130
3131 self.image_cache
3132 .add_listener(ImageLoadListener::new(sender, pipeline_id, id));
3133 }
3134
3135 let nodes = images.entry(id).or_default();
3136 if !nodes.iter().any(|n| std::ptr::eq(&*(n.node), &*node)) {
3137 nodes.push(PendingLayoutImageAncillaryData {
3138 node: Dom::from_ref(&*node),
3139 destination: image.destination,
3140 });
3141 }
3142 }
3143
3144 for image in pending_rasterization_images {
3145 let node = unsafe { from_untrusted_node_address(image.node) };
3146
3147 let mut images = self.pending_images_for_rasterization.borrow_mut();
3148 if !images.contains_key(&(image.id, image.size)) {
3149 self.image_cache.add_rasterization_complete_listener(
3150 pipeline_id,
3151 image.id,
3152 image.size,
3153 self.image_cache_sender.clone(),
3154 );
3155 }
3156
3157 let nodes = images.entry((image.id, image.size)).or_default();
3158 if !nodes.iter().any(|n| std::ptr::eq(&**n, &*node)) {
3159 nodes.push(Dom::from_ref(&*node));
3160 }
3161 }
3162
3163 for node in pending_svg_element_for_serialization.into_iter() {
3164 let node = unsafe { from_untrusted_node_address(node) };
3165 let svg = node.downcast::<SVGSVGElement>().unwrap();
3166 svg.serialize_and_cache_subtree();
3167 node.dirty(NodeDamage::Other);
3168 }
3169 }
3170}
3171
3172impl Window {
3173 #[allow(unsafe_code)]
3174 #[allow(clippy::too_many_arguments)]
3175 pub(crate) fn new(
3176 webview_id: WebViewId,
3177 runtime: Rc<Runtime>,
3178 script_chan: Sender<MainThreadScriptMsg>,
3179 layout: Box<dyn Layout>,
3180 font_context: Arc<FontContext>,
3181 image_cache_sender: IpcSender<ImageCacheResponseMessage>,
3182 image_cache: Arc<dyn ImageCache>,
3183 resource_threads: ResourceThreads,
3184 #[cfg(feature = "bluetooth")] bluetooth_thread: IpcSender<BluetoothRequest>,
3185 mem_profiler_chan: MemProfilerChan,
3186 time_profiler_chan: TimeProfilerChan,
3187 devtools_chan: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
3188 constellation_chan: ScriptToConstellationChan,
3189 embedder_chan: ScriptToEmbedderChan,
3190 control_chan: GenericSender<ScriptThreadMessage>,
3191 pipeline_id: PipelineId,
3192 parent_info: Option<PipelineId>,
3193 viewport_details: ViewportDetails,
3194 origin: MutableOrigin,
3195 creation_url: ServoUrl,
3196 top_level_creation_url: ServoUrl,
3197 navigation_start: CrossProcessInstant,
3198 webgl_chan: Option<WebGLChan>,
3199 #[cfg(feature = "webxr")] webxr_registry: Option<webxr_api::Registry>,
3200 microtask_queue: Rc<MicrotaskQueue>,
3201 compositor_api: CrossProcessCompositorApi,
3202 unminify_js: bool,
3203 unminify_css: bool,
3204 local_script_source: Option<String>,
3205 user_content_manager: UserContentManager,
3206 player_context: WindowGLContext,
3207 #[cfg(feature = "webgpu")] gpu_id_hub: Arc<IdentityHub>,
3208 inherited_secure_context: Option<bool>,
3209 theme: Theme,
3210 ) -> DomRoot<Self> {
3211 let error_reporter = CSSErrorReporter {
3212 pipelineid: pipeline_id,
3213 script_chan: control_chan,
3214 };
3215
3216 let initial_viewport = f32_rect_to_au_rect(UntypedRect::new(
3217 Point2D::zero(),
3218 viewport_details.size.to_untyped(),
3219 ));
3220
3221 let win = Box::new(Self {
3222 webview_id,
3223 globalscope: GlobalScope::new_inherited(
3224 pipeline_id,
3225 devtools_chan,
3226 mem_profiler_chan,
3227 time_profiler_chan,
3228 constellation_chan,
3229 embedder_chan,
3230 resource_threads,
3231 origin,
3232 creation_url,
3233 Some(top_level_creation_url),
3234 microtask_queue,
3235 #[cfg(feature = "webgpu")]
3236 gpu_id_hub,
3237 inherited_secure_context,
3238 unminify_js,
3239 Some(font_context.clone()),
3240 ),
3241 ongoing_navigation: Default::default(),
3242 script_chan,
3243 layout: RefCell::new(layout),
3244 image_cache_sender,
3245 image_cache,
3246 navigator: Default::default(),
3247 location: Default::default(),
3248 history: Default::default(),
3249 indexeddb: Default::default(),
3250 custom_element_registry: Default::default(),
3251 window_proxy: Default::default(),
3252 document: Default::default(),
3253 performance: Default::default(),
3254 navigation_start: Cell::new(navigation_start),
3255 screen: Default::default(),
3256 session_storage: Default::default(),
3257 local_storage: Default::default(),
3258 status: DomRefCell::new(DOMString::new()),
3259 parent_info,
3260 dom_static: GlobalStaticData::new(),
3261 js_runtime: DomRefCell::new(Some(runtime.clone())),
3262 #[cfg(feature = "bluetooth")]
3263 bluetooth_thread,
3264 #[cfg(feature = "bluetooth")]
3265 bluetooth_extra_permission_data: BluetoothExtraPermissionData::new(),
3266 unhandled_resize_event: Default::default(),
3267 viewport_details: Cell::new(viewport_details),
3268 current_viewport_size: Cell::new(initial_viewport.to_untyped().size),
3269 layout_blocker: Cell::new(LayoutBlocker::WaitingForParse),
3270 current_state: Cell::new(WindowState::Alive),
3271 devtools_marker_sender: Default::default(),
3272 devtools_markers: Default::default(),
3273 webdriver_script_chan: Default::default(),
3274 webdriver_load_status_sender: Default::default(),
3275 error_reporter,
3276 media_query_lists: DOMTracker::new(),
3277 #[cfg(feature = "bluetooth")]
3278 test_runner: Default::default(),
3279 webgl_chan,
3280 #[cfg(feature = "webxr")]
3281 webxr_registry,
3282 pending_image_callbacks: Default::default(),
3283 pending_layout_images: Default::default(),
3284 pending_images_for_rasterization: Default::default(),
3285 unminified_css_dir: Default::default(),
3286 local_script_source,
3287 test_worklet: Default::default(),
3288 paint_worklet: Default::default(),
3289 exists_mut_observer: Cell::new(false),
3290 compositor_api,
3291 has_sent_idle_message: Cell::new(false),
3292 unminify_css,
3293 user_content_manager,
3294 player_context,
3295 throttled: Cell::new(false),
3296 layout_marker: DomRefCell::new(Rc::new(Cell::new(true))),
3297 current_event: DomRefCell::new(None),
3298 theme: Cell::new(theme),
3299 trusted_types: Default::default(),
3300 reporting_observer_list: Default::default(),
3301 report_list: Default::default(),
3302 endpoints_list: Default::default(),
3303 });
3304
3305 WindowBinding::Wrap::<crate::DomTypeHolder>(GlobalScope::get_cx(), win)
3306 }
3307
3308 pub(crate) fn pipeline_id(&self) -> PipelineId {
3309 self.as_global_scope().pipeline_id()
3310 }
3311
3312 pub(crate) fn cache_layout_value<T>(&self, value: T) -> LayoutValue<T>
3314 where
3315 T: Copy + MallocSizeOf,
3316 {
3317 LayoutValue::new(self.layout_marker.borrow().clone(), value)
3318 }
3319}
3320
3321#[derive(MallocSizeOf)]
3326pub(crate) struct LayoutValue<T: MallocSizeOf> {
3327 #[ignore_malloc_size_of = "Rc is hard"]
3328 is_valid: Rc<Cell<bool>>,
3329 value: T,
3330}
3331
3332#[allow(unsafe_code)]
3333unsafe impl<T: JSTraceable + MallocSizeOf> JSTraceable for LayoutValue<T> {
3334 unsafe fn trace(&self, trc: *mut js::jsapi::JSTracer) {
3335 unsafe { self.value.trace(trc) };
3336 }
3337}
3338
3339impl<T: Copy + MallocSizeOf> LayoutValue<T> {
3340 fn new(marker: Rc<Cell<bool>>, value: T) -> Self {
3341 LayoutValue {
3342 is_valid: marker,
3343 value,
3344 }
3345 }
3346
3347 pub(crate) fn get(&self) -> Result<T, ()> {
3349 if self.is_valid.get() {
3350 return Ok(self.value);
3351 }
3352 Err(())
3353 }
3354}
3355
3356fn should_move_clip_rect(clip_rect: UntypedRect<Au>, new_viewport: UntypedRect<f32>) -> bool {
3357 let clip_rect = UntypedRect::new(
3358 Point2D::new(
3359 clip_rect.origin.x.to_f32_px(),
3360 clip_rect.origin.y.to_f32_px(),
3361 ),
3362 Size2D::new(
3363 clip_rect.size.width.to_f32_px(),
3364 clip_rect.size.height.to_f32_px(),
3365 ),
3366 );
3367
3368 static VIEWPORT_SCROLL_MARGIN_SIZE: f32 = 0.5;
3372 let viewport_scroll_margin = new_viewport.size * VIEWPORT_SCROLL_MARGIN_SIZE;
3373
3374 (clip_rect.origin.x - new_viewport.origin.x).abs() <= viewport_scroll_margin.width ||
3375 (clip_rect.max_x() - new_viewport.max_x()).abs() <= viewport_scroll_margin.width ||
3376 (clip_rect.origin.y - new_viewport.origin.y).abs() <= viewport_scroll_margin.height ||
3377 (clip_rect.max_y() - new_viewport.max_y()).abs() <= viewport_scroll_margin.height
3378}
3379
3380impl Window {
3381 pub(crate) fn post_message(
3383 &self,
3384 target_origin: Option<ImmutableOrigin>,
3385 source_origin: ImmutableOrigin,
3386 source: &WindowProxy,
3387 data: StructuredSerializedData,
3388 ) {
3389 let this = Trusted::new(self);
3390 let source = Trusted::new(source);
3391 let task = task!(post_serialised_message: move || {
3392 let this = this.root();
3393 let source = source.root();
3394 let document = this.Document();
3395
3396 if let Some(ref target_origin) = target_origin {
3398 if !target_origin.same_origin(document.origin()) {
3399 return;
3400 }
3401 }
3402
3403 let cx = this.get_cx();
3405 let obj = this.reflector().get_jsobject();
3406 let _ac = JSAutoRealm::new(*cx, obj.get());
3407 rooted!(in(*cx) let mut message_clone = UndefinedValue());
3408 if let Ok(ports) = structuredclone::read(this.upcast(), data, message_clone.handle_mut()) {
3409 MessageEvent::dispatch_jsval(
3411 this.upcast(),
3412 this.upcast(),
3413 message_clone.handle(),
3414 Some(&source_origin.ascii_serialization()),
3415 Some(&*source),
3416 ports,
3417 CanGc::note()
3418 );
3419 } else {
3420 MessageEvent::dispatch_error(
3422 this.upcast(),
3423 this.upcast(),
3424 CanGc::note()
3425 );
3426 }
3427 });
3428 self.as_global_scope()
3430 .task_manager()
3431 .dom_manipulation_task_source()
3432 .queue(task);
3433 }
3434}
3435
3436#[derive(MallocSizeOf)]
3437pub(crate) struct CSSErrorReporter {
3438 pub(crate) pipelineid: PipelineId,
3439 pub(crate) script_chan: GenericSender<ScriptThreadMessage>,
3440}
3441unsafe_no_jsmanaged_fields!(CSSErrorReporter);
3442
3443impl ParseErrorReporter for CSSErrorReporter {
3444 fn report_error(
3445 &self,
3446 url: &UrlExtraData,
3447 location: SourceLocation,
3448 error: ContextualParseError,
3449 ) {
3450 if log_enabled!(log::Level::Info) {
3451 info!(
3452 "Url:\t{}\n{}:{} {}",
3453 url.0.as_str(),
3454 location.line,
3455 location.column,
3456 error
3457 )
3458 }
3459
3460 let _ = self.script_chan.send(ScriptThreadMessage::ReportCSSError(
3462 self.pipelineid,
3463 url.0.to_string(),
3464 location.line,
3465 location.column,
3466 error.to_string(),
3467 ));
3468 }
3469}
3470
3471fn is_named_element_with_name_attribute(elem: &Element) -> bool {
3472 let type_ = match elem.upcast::<Node>().type_id() {
3473 NodeTypeId::Element(ElementTypeId::HTMLElement(type_)) => type_,
3474 _ => return false,
3475 };
3476 matches!(
3477 type_,
3478 HTMLElementTypeId::HTMLEmbedElement |
3479 HTMLElementTypeId::HTMLFormElement |
3480 HTMLElementTypeId::HTMLImageElement |
3481 HTMLElementTypeId::HTMLObjectElement
3482 )
3483}
3484
3485fn is_named_element_with_id_attribute(elem: &Element) -> bool {
3486 elem.is_html_element()
3487}
3488
3489#[allow(unsafe_code)]
3490#[unsafe(no_mangle)]
3491unsafe extern "C" fn dump_js_stack(cx: *mut RawJSContext) {
3493 unsafe {
3494 DumpJSStack(cx, true, false, false);
3495 }
3496}
3497
3498impl WindowHelpers for Window {
3499 fn create_named_properties_object(
3500 cx: JSContext,
3501 proto: HandleObject,
3502 object: MutableHandleObject,
3503 ) {
3504 Self::create_named_properties_object(cx, proto, object)
3505 }
3506}