Skip to main content

script/event_loop/
document_collection.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
5use std::collections::hash_map;
6
7use rustc_hash::{FxBuildHasher, FxHashMap};
8use servo_base::id::{BrowsingContextId, PipelineId};
9
10use crate::dom::bindings::inheritance::Castable;
11use crate::dom::bindings::root::{Dom, DomRoot};
12use crate::dom::bindings::trace::HashMapTracedValues;
13use crate::dom::document::Document;
14use crate::dom::globalscope::GlobalScope;
15use crate::dom::html::htmliframeelement::HTMLIFrameElement;
16use crate::dom::window::Window;
17
18/// The collection of all [`Document`]s managed by the [`crate::event_loop::script_thread::ScriptThread`].
19/// This is stored as a mapping of [`PipelineId`] to [`Document`], but for updating the
20/// rendering, [`Document`]s should be processed in order via [`Self::documents_in_order`].
21#[derive(JSTraceable)]
22#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
23pub(crate) struct DocumentCollection {
24    map: HashMapTracedValues<PipelineId, Dom<Document>, FxBuildHasher>,
25}
26
27impl DocumentCollection {
28    pub(crate) fn insert(&mut self, pipeline_id: PipelineId, doc: &Document) {
29        self.map.insert(pipeline_id, Dom::from_ref(doc));
30    }
31
32    pub(crate) fn remove(&mut self, pipeline_id: PipelineId) -> Option<DomRoot<Document>> {
33        self.map
34            .remove(&pipeline_id)
35            .map(|ref doc| DomRoot::from_ref(&**doc))
36    }
37
38    pub(crate) fn find_document(&self, pipeline_id: PipelineId) -> Option<DomRoot<Document>> {
39        self.map
40            .get(&pipeline_id)
41            .map(|doc| DomRoot::from_ref(&**doc))
42            .filter(|document| !document.window_detached())
43    }
44
45    pub(crate) fn find_window(&self, pipeline_id: PipelineId) -> Option<DomRoot<Window>> {
46        self.find_document(pipeline_id)
47            .map(|doc| DomRoot::from_ref(doc.window()))
48            .filter(|window| window.pipeline_id() == pipeline_id)
49    }
50
51    pub(crate) fn find_global(&self, pipeline_id: PipelineId) -> Option<DomRoot<GlobalScope>> {
52        self.find_window(pipeline_id)
53            .map(|window| DomRoot::from_ref(window.upcast()))
54    }
55
56    pub(crate) fn find_iframe(
57        &self,
58        pipeline_id: PipelineId,
59        browsing_context_id: BrowsingContextId,
60    ) -> Option<DomRoot<HTMLIFrameElement>> {
61        self.find_document(pipeline_id)
62            .and_then(|document| document.iframes().element(browsing_context_id))
63    }
64
65    pub(crate) fn iter(&self) -> DocumentsIter<'_> {
66        DocumentsIter {
67            iter: self.map.iter(),
68        }
69    }
70
71    /// Return the documents managed by this [`crate::event_loop::script_thread::ScriptThread`] in the
72    /// order specified by the *[update the rendering][update-the-rendering]* step of the
73    /// HTML specification:
74    ///
75    /// > Let docs be all fully active Document objects whose relevant agent's event loop is
76    /// > eventLoop, sorted arbitrarily except that the following conditions must be met:
77    /// >
78    /// > Any Document B whose container document is A must be listed after A in the list.
79    /// >
80    /// > If there are two documents A and B that both have the same non-null container
81    /// > document C, then the order of A and B in the list must match the shadow-including
82    /// > tree order of their respective navigable containers in C's node tree.
83    /// >
84    /// > In the steps below that iterate over docs, each Document must be processed in the
85    /// > order it is found in the list.
86    ///
87    /// [update-the-rendering]: https://html.spec.whatwg.org/multipage/#update-the-rendering
88    pub(crate) fn documents_in_order(&self) -> Vec<PipelineId> {
89        DocumentTree::new(self).documents_in_order()
90    }
91}
92
93impl Default for DocumentCollection {
94    fn default() -> Self {
95        Self {
96            map: HashMapTracedValues::new_fx(),
97        }
98    }
99}
100
101pub(crate) struct DocumentsIter<'a> {
102    iter: hash_map::Iter<'a, PipelineId, Dom<Document>>,
103}
104
105impl Iterator for DocumentsIter<'_> {
106    type Item = (PipelineId, DomRoot<Document>);
107
108    fn next(&mut self) -> Option<(PipelineId, DomRoot<Document>)> {
109        self.iter
110            .next()
111            .map(|(id, doc)| (*id, DomRoot::from_ref(&**doc)))
112    }
113
114    fn size_hint(&self) -> (usize, Option<usize>) {
115        self.iter.size_hint()
116    }
117}
118
119#[derive(Default)]
120struct DocumentTreeNode {
121    parent: Option<PipelineId>,
122    children: Vec<PipelineId>,
123}
124
125/// A tree representation of [`Document`]s managed by the [`ScriptThread`][st], which is used
126/// to generate an ordered set of [`Document`]s for the *update the rendering* step of the
127/// HTML5 specification.
128///
129/// FIXME: The [`ScriptThread`][st] only has a view of [`Document`]s managed by itself,
130/// so if there are interceding iframes managed by other `ScriptThread`s, then the
131/// order of the [`Document`]s may not be correct. Perhaps the Constellation could
132/// ensure that every [`ScriptThread`][st] has the full view of the frame tree.
133///
134/// [st]: crate::event_loop::script_thread::ScriptThread
135#[derive(Default)]
136struct DocumentTree {
137    tree: FxHashMap<PipelineId, DocumentTreeNode>,
138}
139
140impl DocumentTree {
141    fn new(documents: &DocumentCollection) -> Self {
142        let mut tree = DocumentTree::default();
143        for (id, document) in documents.iter() {
144            let children: Vec<PipelineId> = document
145                .iframes()
146                .iter()
147                .filter_map(|iframe| iframe.pipeline_id())
148                .filter(|iframe_pipeline_id| documents.find_document(*iframe_pipeline_id).is_some())
149                .collect();
150            for child in &children {
151                tree.tree.entry(*child).or_default().parent = Some(id);
152            }
153            tree.tree.entry(id).or_default().children = children;
154        }
155        tree
156    }
157
158    fn documents_in_order(&self) -> Vec<PipelineId> {
159        let mut list = Vec::new();
160        for (id, node) in self.tree.iter() {
161            if node.parent.is_none() {
162                self.process_node_for_documents_in_order(*id, &mut list);
163            }
164        }
165        list
166    }
167
168    fn process_node_for_documents_in_order(&self, id: PipelineId, list: &mut Vec<PipelineId>) {
169        list.push(id);
170        for child in self
171            .tree
172            .get(&id)
173            .expect("Should have found child node")
174            .children
175            .iter()
176        {
177            self.process_node_for_documents_in_order(*child, list);
178        }
179    }
180}