script/
document_loader.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Tracking of pending loads in a document.
6//!
7//! <https://html.spec.whatwg.org/multipage/#the-end>
8
9use net_traits::request::RequestBuilder;
10use net_traits::{BoxedFetchCallback, ResourceThreads, fetch_async};
11use servo_url::ServoUrl;
12
13use crate::dom::bindings::cell::DomRefCell;
14use crate::dom::bindings::root::Dom;
15use crate::dom::document::Document;
16use crate::fetch::FetchCanceller;
17use crate::script_runtime::CanGc;
18
19#[derive(Clone, Debug, JSTraceable, MallocSizeOf, PartialEq)]
20pub(crate) enum LoadType {
21    Image(#[no_trace] ServoUrl),
22    Script(#[no_trace] ServoUrl),
23    Subframe(#[no_trace] ServoUrl),
24    Stylesheet(#[no_trace] ServoUrl),
25    PageSource(#[no_trace] ServoUrl),
26    Media,
27}
28
29/// Canary value ensuring that manually added blocking loads (ie. ones that weren't
30/// created via DocumentLoader::fetch_async) are always removed by the time
31/// that the owner is destroyed.
32#[derive(JSTraceable, MallocSizeOf)]
33#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
34pub(crate) struct LoadBlocker {
35    /// The document whose load event is blocked by this object existing.
36    doc: Dom<Document>,
37    /// The load that is blocking the document's load event.
38    load: Option<LoadType>,
39}
40
41impl LoadBlocker {
42    /// Mark the document's load event as blocked on this new load.
43    pub(crate) fn new(doc: &Document, load: LoadType) -> LoadBlocker {
44        doc.loader_mut().add_blocking_load(load.clone());
45        LoadBlocker {
46            doc: Dom::from_ref(doc),
47            load: Some(load),
48        }
49    }
50
51    /// Remove this load from the associated document's list of blocking loads.
52    pub(crate) fn terminate(blocker: &DomRefCell<Option<LoadBlocker>>, can_gc: CanGc) {
53        let Some(load) = blocker
54            .borrow_mut()
55            .as_mut()
56            .and_then(|blocker| blocker.load.take())
57        else {
58            return;
59        };
60
61        if let Some(blocker) = blocker.borrow().as_ref() {
62            blocker.doc.finish_load(load, can_gc);
63        }
64
65        *blocker.borrow_mut() = None;
66    }
67}
68
69impl Drop for LoadBlocker {
70    fn drop(&mut self) {
71        if let Some(load) = self.load.take() {
72            self.doc.finish_load(load, CanGc::note());
73        }
74    }
75}
76
77#[derive(JSTraceable, MallocSizeOf)]
78pub(crate) struct DocumentLoader {
79    #[no_trace]
80    resource_threads: ResourceThreads,
81    blocking_loads: Vec<LoadType>,
82    events_inhibited: bool,
83    cancellers: Vec<FetchCanceller>,
84}
85
86impl DocumentLoader {
87    pub(crate) fn new(existing: &DocumentLoader) -> DocumentLoader {
88        DocumentLoader::new_with_threads(existing.resource_threads.clone(), None)
89    }
90
91    pub(crate) fn new_with_threads(
92        resource_threads: ResourceThreads,
93        initial_load: Option<ServoUrl>,
94    ) -> DocumentLoader {
95        debug!("Initial blocking load {:?}.", initial_load);
96        let initial_loads = initial_load.into_iter().map(LoadType::PageSource).collect();
97
98        DocumentLoader {
99            resource_threads,
100            blocking_loads: initial_loads,
101            events_inhibited: false,
102            cancellers: Vec::new(),
103        }
104    }
105
106    pub(crate) fn cancel_all_loads(&mut self) -> bool {
107        let canceled_any = !self.cancellers.is_empty();
108        // Associated fetches will be canceled when dropping the canceller.
109        self.cancellers.clear();
110        canceled_any
111    }
112
113    /// Add a load to the list of blocking loads.
114    fn add_blocking_load(&mut self, load: LoadType) {
115        debug!(
116            "Adding blocking load {:?} ({}).",
117            load,
118            self.blocking_loads.len()
119        );
120        self.blocking_loads.push(load);
121    }
122
123    /// Initiate a new fetch given a response callback.
124    pub(crate) fn fetch_async_with_callback(
125        &mut self,
126        load: LoadType,
127        request: RequestBuilder,
128        callback: BoxedFetchCallback,
129    ) {
130        self.add_blocking_load(load);
131        self.fetch_async_background(request, callback);
132    }
133
134    /// Initiate a new fetch that does not block the document load event.
135    pub(crate) fn fetch_async_background(
136        &mut self,
137        request: RequestBuilder,
138        callback: BoxedFetchCallback,
139    ) {
140        self.cancellers.push(FetchCanceller::new(
141            request.id,
142            self.resource_threads.core_thread.clone(),
143        ));
144        fetch_async(&self.resource_threads.core_thread, request, None, callback);
145    }
146
147    /// Mark an in-progress network request complete.
148    pub(crate) fn finish_load(&mut self, load: &LoadType) {
149        debug!(
150            "Removing blocking load {:?} ({}).",
151            load,
152            self.blocking_loads.len()
153        );
154        let idx = self
155            .blocking_loads
156            .iter()
157            .position(|unfinished| *unfinished == *load);
158        match idx {
159            Some(i) => {
160                self.blocking_loads.remove(i);
161            },
162            None => warn!("unknown completed load {:?}", load),
163        }
164    }
165
166    pub(crate) fn is_blocked(&self) -> bool {
167        // TODO: Ensure that we report blocked if parsing is still ongoing.
168        !self.blocking_loads.is_empty()
169    }
170
171    pub(crate) fn is_only_blocked_by_iframes(&self) -> bool {
172        self.blocking_loads
173            .iter()
174            .all(|load| matches!(*load, LoadType::Subframe(_)))
175    }
176
177    pub(crate) fn inhibit_events(&mut self) {
178        self.events_inhibited = true;
179    }
180
181    pub(crate) fn events_inhibited(&self) -> bool {
182        self.events_inhibited
183    }
184
185    pub(crate) fn resource_threads(&self) -> &ResourceThreads {
186        &self.resource_threads
187    }
188}