script/
iframe_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::default::Default;
6
7use base::id::BrowsingContextId;
8use constellation_traits::{IFrameSizeMsg, ScriptToConstellationMessage, WindowSizeType};
9use embedder_traits::ViewportDetails;
10use layout_api::IFrameSizes;
11use paint_api::PinchZoomInfos;
12use rustc_hash::FxHashMap;
13use script_bindings::script_runtime::CanGc;
14
15use crate::dom::bindings::inheritance::Castable;
16use crate::dom::bindings::root::{Dom, DomRoot};
17use crate::dom::html::htmliframeelement::HTMLIFrameElement;
18use crate::dom::node::{Node, ShadowIncluding};
19use crate::dom::types::{Document, Window};
20use crate::script_thread::with_script_thread;
21
22#[derive(JSTraceable, MallocSizeOf)]
23#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
24pub(crate) struct IFrame {
25    pub(crate) element: Dom<HTMLIFrameElement>,
26    #[no_trace]
27    pub(crate) size: Option<ViewportDetails>,
28}
29
30#[derive(Default, JSTraceable, MallocSizeOf)]
31#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
32pub(crate) struct IFrameCollection {
33    /// The `<iframe>`s in the collection.
34    iframes: Vec<IFrame>,
35    /// When true, the collection will need to be rebuilt.
36    invalid: bool,
37}
38
39impl IFrameCollection {
40    pub(crate) fn new() -> Self {
41        Self {
42            iframes: vec![],
43            invalid: true,
44        }
45    }
46
47    pub(crate) fn invalidate(&mut self) {
48        self.invalid = true;
49    }
50
51    /// Validate that the collection is up-to-date with the given [`Document`]. If it isn't up-to-date
52    /// rebuild it.
53    pub(crate) fn validate(&mut self, document: &Document) {
54        if !self.invalid {
55            return;
56        }
57        let document_node = DomRoot::from_ref(document.upcast::<Node>());
58
59        // Preserve any old sizes, but only for `<iframe>`s that already have a
60        // BrowsingContextId and a set size.
61        let mut old_sizes: FxHashMap<_, _> = self
62            .iframes
63            .iter()
64            .filter_map(
65                |iframe| match (iframe.element.browsing_context_id(), iframe.size) {
66                    (Some(browsing_context_id), Some(size)) => Some((browsing_context_id, size)),
67                    _ => None,
68                },
69            )
70            .collect();
71
72        self.iframes = document_node
73            .traverse_preorder(ShadowIncluding::Yes)
74            .filter_map(DomRoot::downcast::<HTMLIFrameElement>)
75            .map(|element| {
76                let size = element
77                    .browsing_context_id()
78                    .and_then(|browsing_context_id| old_sizes.remove(&browsing_context_id));
79                IFrame {
80                    element: element.as_traced(),
81                    size,
82                }
83            })
84            .collect();
85        self.invalid = false;
86    }
87
88    pub(crate) fn get(&self, browsing_context_id: BrowsingContextId) -> Option<&IFrame> {
89        self.iframes
90            .iter()
91            .find(|iframe| iframe.element.browsing_context_id() == Some(browsing_context_id))
92    }
93
94    pub(crate) fn get_mut(
95        &mut self,
96        browsing_context_id: BrowsingContextId,
97    ) -> Option<&mut IFrame> {
98        self.iframes
99            .iter_mut()
100            .find(|iframe| iframe.element.browsing_context_id() == Some(browsing_context_id))
101    }
102
103    /// Set the size of an `<iframe>` in the collection given its `BrowsingContextId` and
104    /// the new size. Returns the old size.
105    pub(crate) fn set_viewport_details(
106        &mut self,
107        browsing_context_id: BrowsingContextId,
108        new_size: ViewportDetails,
109    ) -> Option<ViewportDetails> {
110        self.get_mut(browsing_context_id)
111            .expect("Tried to set a size for an unknown <iframe>")
112            .size
113            .replace(new_size)
114    }
115
116    /// Update the recorded iframe sizes of the contents of layout. Return a
117    /// [`Vec<IFrameSizeMsg>`] containing the messages to send to the `Constellation`. A
118    /// message is only sent when the size actually changes.
119    pub(crate) fn handle_new_iframe_sizes_after_layout(
120        &mut self,
121        window: &Window,
122        new_iframe_sizes: IFrameSizes,
123    ) {
124        if new_iframe_sizes.is_empty() {
125            return;
126        }
127
128        let size_messages: Vec<_> = new_iframe_sizes
129            .into_iter()
130            .filter_map(|(browsing_context_id, iframe_size)| {
131                // Batch resize message to any local `Pipeline`s now, rather than waiting for them
132                // to filter asynchronously through the `Constellation`. This allows the new value
133                // to be reflected immediately in layout.
134                let viewport_details = iframe_size.viewport_details;
135                with_script_thread(|script_thread| {
136                    script_thread.handle_resize_message(
137                        iframe_size.pipeline_id,
138                        viewport_details,
139                        WindowSizeType::Resize,
140                    );
141                    // Additionally, update the `VisualViewport` of the `Iframe`. This allows us
142                    // to process the resize for `VisualViewport` in the corrent timing. Note that
143                    // `VisualViewport` for iframes would practically follow layout viewport.
144                    script_thread.handle_update_pinch_zoom_infos(
145                        iframe_size.pipeline_id,
146                        PinchZoomInfos::new_from_viewport_size(viewport_details.size),
147                        // Theoritically it wouldn't do GC since it is impossible to initialize
148                        // the `VisualViewport` interface here.
149                        CanGc::note(),
150                    )
151                });
152
153                let old_viewport_details =
154                    self.set_viewport_details(browsing_context_id, viewport_details);
155                // The `Constellation` should be up-to-date even when the in-ScriptThread pipelines
156                // might not be.
157                if old_viewport_details == Some(viewport_details) {
158                    return None;
159                }
160
161                let size_type = match old_viewport_details {
162                    Some(_) => WindowSizeType::Resize,
163                    None => WindowSizeType::Initial,
164                };
165
166                Some(IFrameSizeMsg {
167                    browsing_context_id,
168                    size: viewport_details,
169                    type_: size_type,
170                })
171            })
172            .collect();
173
174        if !size_messages.is_empty() {
175            window.send_to_constellation(ScriptToConstellationMessage::IFrameSizes(size_messages));
176        }
177    }
178
179    pub(crate) fn iter(&self) -> impl Iterator<Item = DomRoot<HTMLIFrameElement>> + use<'_> {
180        self.iframes.iter().map(|iframe| iframe.element.as_rooted())
181    }
182}