Skip to main content

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 script_bindings::cell::DomRefCell;
12use script_bindings::script_runtime::runtime_is_alive;
13use servo_url::ServoUrl;
14
15use crate::dom::bindings::root::Dom;
16use crate::dom::document::Document;
17use crate::fetch::FetchCanceller;
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(
53        blocker: &DomRefCell<Option<LoadBlocker>>,
54        cx: &mut js::context::JSContext,
55    ) {
56        let Some(load) = blocker
57            .borrow_mut()
58            .as_mut()
59            .and_then(|blocker| blocker.load.take())
60        else {
61            return;
62        };
63
64        if let Some(blocker) = blocker.borrow().as_ref() {
65            blocker.doc.finish_load(load, cx);
66        }
67
68        *blocker.borrow_mut() = None;
69    }
70}
71
72impl Drop for LoadBlocker {
73    fn drop(&mut self) {
74        // We need to check here if the whole runtime is alive, otherwise
75        // we interact with a document that is also being dropped. That
76        // would panic, since we should no longer schedule a task for
77        // a dropped runtime. Therefore, we should only run the drop logic
78        // in case this element is dropped, but its containing document
79        // is still alive.
80        if runtime_is_alive() &&
81            let Some(load) = self.load.take()
82        {
83            self.doc.finish_load_for_dropped_blocker(load);
84        }
85    }
86}
87
88#[derive(JSTraceable, MallocSizeOf)]
89pub(crate) struct DocumentLoader {
90    #[no_trace]
91    resource_threads: ResourceThreads,
92    blocking_loads: Vec<LoadType>,
93    events_inhibited: bool,
94    cancellers: Vec<FetchCanceller>,
95}
96
97impl DocumentLoader {
98    pub(crate) fn new(existing: &DocumentLoader) -> DocumentLoader {
99        DocumentLoader::new_with_threads(existing.resource_threads.clone(), None)
100    }
101
102    pub(crate) fn new_with_threads(
103        resource_threads: ResourceThreads,
104        initial_load: Option<ServoUrl>,
105    ) -> DocumentLoader {
106        debug!("Initial blocking load {:?}.", initial_load);
107        let initial_loads = initial_load.into_iter().map(LoadType::PageSource).collect();
108
109        DocumentLoader {
110            resource_threads,
111            blocking_loads: initial_loads,
112            events_inhibited: false,
113            cancellers: Vec::new(),
114        }
115    }
116
117    /// <https://fetch.spec.whatwg.org/#concept-fetch-group-terminate>
118    pub(crate) fn cancel_all_loads(&mut self) -> Vec<FetchCanceller> {
119        self.cancellers.drain(..).collect()
120    }
121
122    /// Add a load to the list of blocking loads.
123    fn add_blocking_load(&mut self, load: LoadType) {
124        debug!(
125            "Adding blocking load {:?} ({}).",
126            load,
127            self.blocking_loads.len()
128        );
129        self.blocking_loads.push(load);
130    }
131
132    /// Initiate a new fetch given a response callback.
133    pub(crate) fn fetch_async_with_callback(
134        &mut self,
135        load: LoadType,
136        request: RequestBuilder,
137        callback: BoxedFetchCallback,
138    ) {
139        self.add_blocking_load(load);
140        self.fetch_async_background(request, callback);
141    }
142
143    /// Initiate a new fetch that does not block the document load event.
144    pub(crate) fn fetch_async_background(
145        &mut self,
146        request: RequestBuilder,
147        callback: BoxedFetchCallback,
148    ) {
149        self.cancellers.push(FetchCanceller::new(
150            request.id,
151            request.keep_alive,
152            self.resource_threads.core_thread.clone(),
153        ));
154        fetch_async(&self.resource_threads.core_thread, request, None, callback);
155    }
156
157    /// Mark an in-progress network request complete.
158    pub(crate) fn finish_load(&mut self, load: &LoadType) {
159        debug!(
160            "Removing blocking load {:?} ({}).",
161            load,
162            self.blocking_loads.len()
163        );
164        let idx = self
165            .blocking_loads
166            .iter()
167            .position(|unfinished| *unfinished == *load);
168        match idx {
169            Some(i) => {
170                self.blocking_loads.remove(i);
171            },
172            None => warn!("unknown completed load {:?}", load),
173        }
174    }
175
176    pub(crate) fn is_blocked(&self) -> bool {
177        // TODO: Ensure that we report blocked if parsing is still ongoing.
178        !self.blocking_loads.is_empty()
179    }
180
181    pub(crate) fn is_only_blocked_by_iframes(&self) -> bool {
182        self.blocking_loads
183            .iter()
184            .all(|load| matches!(*load, LoadType::Subframe(_)))
185    }
186
187    pub(crate) fn inhibit_events(&mut self) {
188        self.events_inhibited = true;
189    }
190
191    pub(crate) fn events_inhibited(&self) -> bool {
192        self.events_inhibited
193    }
194
195    pub(crate) fn resource_threads(&self) -> &ResourceThreads {
196        &self.resource_threads
197    }
198}