script/
document_collection.rs1use std::collections::hash_map;
6
7use base::id::{BrowsingContextId, PipelineId};
8use rustc_hash::{FxBuildHasher, FxHashMap};
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#[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 }
43
44 pub(crate) fn find_window(&self, pipeline_id: PipelineId) -> Option<DomRoot<Window>> {
45 self.find_document(pipeline_id)
46 .map(|doc| DomRoot::from_ref(doc.window()))
47 }
48
49 pub(crate) fn find_global(&self, pipeline_id: PipelineId) -> Option<DomRoot<GlobalScope>> {
50 self.find_window(pipeline_id)
51 .map(|window| DomRoot::from_ref(window.upcast()))
52 }
53
54 pub(crate) fn find_iframe(
55 &self,
56 pipeline_id: PipelineId,
57 browsing_context_id: BrowsingContextId,
58 ) -> Option<DomRoot<HTMLIFrameElement>> {
59 self.find_document(pipeline_id).and_then(|document| {
60 document
61 .iframes()
62 .get(browsing_context_id)
63 .map(|iframe| iframe.element.as_rooted())
64 })
65 }
66
67 pub(crate) fn iter(&self) -> DocumentsIter<'_> {
68 DocumentsIter {
69 iter: self.map.iter(),
70 }
71 }
72
73 pub(crate) fn documents_in_order(&self) -> Vec<PipelineId> {
91 DocumentTree::new(self).documents_in_order()
92 }
93}
94
95impl Default for DocumentCollection {
96 #[cfg_attr(crown, allow(crown::unrooted_must_root))]
97 fn default() -> Self {
98 Self {
99 map: HashMapTracedValues::new_fx(),
100 }
101 }
102}
103
104#[cfg_attr(crown, allow(crown::unrooted_must_root))]
105pub(crate) struct DocumentsIter<'a> {
106 iter: hash_map::Iter<'a, PipelineId, Dom<Document>>,
107}
108
109impl Iterator for DocumentsIter<'_> {
110 type Item = (PipelineId, DomRoot<Document>);
111
112 fn next(&mut self) -> Option<(PipelineId, DomRoot<Document>)> {
113 self.iter
114 .next()
115 .map(|(id, doc)| (*id, DomRoot::from_ref(&**doc)))
116 }
117}
118
119#[derive(Default)]
120struct DocumentTreeNode {
121 parent: Option<PipelineId>,
122 children: Vec<PipelineId>,
123}
124
125#[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}