script/
iframe_collection.rs1use std::default::Default;
6
7use base::id::BrowsingContextId;
8use constellation_traits::{IFrameSizeMsg, ScriptToConstellationMessage, WindowSizeType};
9use embedder_traits::ViewportDetails;
10use fnv::FnvHashMap;
11use layout_api::IFrameSizes;
12
13use crate::dom::bindings::inheritance::Castable;
14use crate::dom::bindings::root::{Dom, DomRoot};
15use crate::dom::html::htmliframeelement::HTMLIFrameElement;
16use crate::dom::node::{Node, ShadowIncluding};
17use crate::dom::types::{Document, Window};
18use crate::script_thread::with_script_thread;
19
20#[derive(JSTraceable, MallocSizeOf)]
21#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
22pub(crate) struct IFrame {
23 pub(crate) element: Dom<HTMLIFrameElement>,
24 #[no_trace]
25 pub(crate) size: Option<ViewportDetails>,
26}
27
28#[derive(Default, JSTraceable, MallocSizeOf)]
29#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
30pub(crate) struct IFrameCollection {
31 iframes: Vec<IFrame>,
33 invalid: bool,
35}
36
37impl IFrameCollection {
38 pub(crate) fn new() -> Self {
39 Self {
40 iframes: vec![],
41 invalid: true,
42 }
43 }
44
45 pub(crate) fn invalidate(&mut self) {
46 self.invalid = true;
47 }
48
49 pub(crate) fn validate(&mut self, document: &Document) {
52 if !self.invalid {
53 return;
54 }
55 let document_node = DomRoot::from_ref(document.upcast::<Node>());
56
57 let mut old_sizes: FnvHashMap<_, _> = self
60 .iframes
61 .iter()
62 .filter_map(
63 |iframe| match (iframe.element.browsing_context_id(), iframe.size) {
64 (Some(browsing_context_id), Some(size)) => Some((browsing_context_id, size)),
65 _ => None,
66 },
67 )
68 .collect();
69
70 self.iframes = document_node
71 .traverse_preorder(ShadowIncluding::Yes)
72 .filter_map(DomRoot::downcast::<HTMLIFrameElement>)
73 .map(|element| {
74 let size = element
75 .browsing_context_id()
76 .and_then(|browsing_context_id| old_sizes.remove(&browsing_context_id));
77 IFrame {
78 element: element.as_traced(),
79 size,
80 }
81 })
82 .collect();
83 self.invalid = false;
84 }
85
86 pub(crate) fn get(&self, browsing_context_id: BrowsingContextId) -> Option<&IFrame> {
87 self.iframes
88 .iter()
89 .find(|iframe| iframe.element.browsing_context_id() == Some(browsing_context_id))
90 }
91
92 pub(crate) fn get_mut(
93 &mut self,
94 browsing_context_id: BrowsingContextId,
95 ) -> Option<&mut IFrame> {
96 self.iframes
97 .iter_mut()
98 .find(|iframe| iframe.element.browsing_context_id() == Some(browsing_context_id))
99 }
100
101 pub(crate) fn set_viewport_details(
104 &mut self,
105 browsing_context_id: BrowsingContextId,
106 new_size: ViewportDetails,
107 ) -> Option<ViewportDetails> {
108 self.get_mut(browsing_context_id)
109 .expect("Tried to set a size for an unknown <iframe>")
110 .size
111 .replace(new_size)
112 }
113
114 pub(crate) fn handle_new_iframe_sizes_after_layout(
118 &mut self,
119 window: &Window,
120 new_iframe_sizes: IFrameSizes,
121 ) {
122 if new_iframe_sizes.is_empty() {
123 return;
124 }
125
126 let size_messages: Vec<_> = new_iframe_sizes
127 .into_iter()
128 .filter_map(|(browsing_context_id, iframe_size)| {
129 let viewport_details = iframe_size.viewport_details;
133 with_script_thread(|script_thread| {
134 script_thread.handle_resize_message(
135 iframe_size.pipeline_id,
136 viewport_details,
137 WindowSizeType::Resize,
138 );
139 });
140
141 let old_viewport_details =
142 self.set_viewport_details(browsing_context_id, viewport_details);
143 if old_viewport_details == Some(viewport_details) {
146 return None;
147 }
148
149 let size_type = match old_viewport_details {
150 Some(_) => WindowSizeType::Resize,
151 None => WindowSizeType::Initial,
152 };
153
154 Some(IFrameSizeMsg {
155 browsing_context_id,
156 size: viewport_details,
157 type_: size_type,
158 })
159 })
160 .collect();
161
162 if !size_messages.is_empty() {
163 window.send_to_constellation(ScriptToConstellationMessage::IFrameSizes(size_messages));
164 }
165 }
166
167 pub(crate) fn iter(&self) -> impl Iterator<Item = DomRoot<HTMLIFrameElement>> + use<'_> {
168 self.iframes.iter().map(|iframe| iframe.element.as_rooted())
169 }
170}