Skip to main content

servo_constellation/
browsingcontext.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 embedder_traits::ViewportDetails;
6use log::warn;
7use rustc_hash::{FxHashMap, FxHashSet};
8use servo_base::id::{BrowsingContextGroupId, BrowsingContextId, PipelineId, WebViewId};
9
10use crate::pipeline::Pipeline;
11
12/// Because a browsing context is only constructed once the document that's
13/// going to be in it becomes active (i.e. not when a pipeline is spawned), some
14/// values needed in browsing context are not easily available at the point of
15/// constructing it. Thus, every time a pipeline is created for a browsing
16/// context which doesn't exist yet, these values needed for the new browsing
17/// context are stored here so that they may be available later.
18#[derive(Debug)]
19pub struct NewBrowsingContextInfo {
20    /// The parent pipeline that contains this browsing context. `None` if this
21    /// is a top level browsing context.
22    pub parent_pipeline_id: Option<PipelineId>,
23
24    /// Whether this browsing context is in private browsing mode.
25    pub is_private: bool,
26
27    /// Whether this browsing context inherits a secure context.
28    pub inherited_secure_context: Option<bool>,
29
30    /// Whether this browsing context should be throttled, using less resources
31    /// by stopping animations and running timers at a heavily limited rate.
32    pub throttled: bool,
33}
34
35/// The constellation's view of a browsing context.
36/// Each browsing context has a session history, caused by navigation and
37/// traversing the history. Each browsing context has its current entry, plus
38/// past and future entries. The past is sorted chronologically, the future is
39/// sorted reverse chronologically: in particular prev.pop() is the latest
40/// past entry, and next.pop() is the earliest future entry.
41pub struct BrowsingContext {
42    /// The browsing context group id where the top-level of this bc is found.
43    pub bc_group_id: BrowsingContextGroupId,
44
45    /// The browsing context id.
46    pub id: BrowsingContextId,
47
48    /// The top-level browsing context ancestor
49    pub webview_id: WebViewId,
50
51    /// The [`ViewportDetails`] of the frame that this [`BrowsingContext`] represents.
52    pub viewport_details: ViewportDetails,
53
54    /// Whether this browsing context is in private browsing mode.
55    pub is_private: bool,
56
57    /// Whether this browsing context inherits a secure context.
58    pub inherited_secure_context: Option<bool>,
59
60    /// Whether this browsing context should be throttled, using less resources
61    /// by stopping animations and running timers at a heavily limited rate.
62    pub throttled: bool,
63
64    /// The pipeline for the current session history entry.
65    pub pipeline_id: PipelineId,
66
67    /// The parent pipeline that contains this browsing context. `None` if this
68    /// is a top level browsing context.
69    pub parent_pipeline_id: Option<PipelineId>,
70
71    /// All the pipelines that have been presented or will be presented in
72    /// this browsing context.
73    pub pipelines: FxHashSet<PipelineId>,
74}
75
76impl BrowsingContext {
77    /// Create a new browsing context.
78    /// Note this just creates the browsing context, it doesn't add it to the constellation's set of browsing contexts.
79    #[expect(clippy::too_many_arguments)]
80    pub fn new(
81        bc_group_id: BrowsingContextGroupId,
82        id: BrowsingContextId,
83        webview_id: WebViewId,
84        pipeline_id: PipelineId,
85        parent_pipeline_id: Option<PipelineId>,
86        viewport_details: ViewportDetails,
87        is_private: bool,
88        inherited_secure_context: Option<bool>,
89        throttled: bool,
90    ) -> BrowsingContext {
91        let mut pipelines = FxHashSet::default();
92        pipelines.insert(pipeline_id);
93        BrowsingContext {
94            bc_group_id,
95            id,
96            webview_id,
97            viewport_details,
98            is_private,
99            inherited_secure_context,
100            throttled,
101            pipeline_id,
102            parent_pipeline_id,
103            pipelines,
104        }
105    }
106
107    pub fn update_current_entry(&mut self, pipeline_id: PipelineId) {
108        self.pipeline_id = pipeline_id;
109    }
110
111    /// Is this a top-level browsing context?
112    pub fn is_top_level(&self) -> bool {
113        self.id == self.webview_id
114    }
115}
116
117/// An iterator over browsing contexts, returning the descendant
118/// contexts whose active documents are fully active, in depth-first
119/// order.
120pub struct FullyActiveBrowsingContextsIterator<'a> {
121    /// The browsing contexts still to iterate over.
122    pub stack: Vec<BrowsingContextId>,
123
124    /// The set of all browsing contexts.
125    pub browsing_contexts: &'a FxHashMap<BrowsingContextId, BrowsingContext>,
126
127    /// The set of all pipelines.  We use this to find the active
128    /// children of a frame, which are the iframes in the currently
129    /// active document.
130    pub pipelines: &'a FxHashMap<PipelineId, Pipeline>,
131}
132
133impl<'a> FullyActiveBrowsingContextsIterator<'a> {
134    pub(crate) fn new(
135        browsing_context_id: BrowsingContextId,
136        browsing_contexts: &'a FxHashMap<BrowsingContextId, BrowsingContext>,
137        pipelines: &'a FxHashMap<PipelineId, Pipeline>,
138    ) -> Self {
139        FullyActiveBrowsingContextsIterator {
140            stack: vec![browsing_context_id],
141            pipelines,
142            browsing_contexts,
143        }
144    }
145}
146
147impl<'a> Iterator for FullyActiveBrowsingContextsIterator<'a> {
148    type Item = &'a BrowsingContext;
149    fn next(&mut self) -> Option<&'a BrowsingContext> {
150        loop {
151            let browsing_context_id = self.stack.pop()?;
152            let browsing_context = match self.browsing_contexts.get(&browsing_context_id) {
153                Some(browsing_context) => browsing_context,
154                None => {
155                    warn!(
156                        "BrowsingContext {:?} iterated after closure.",
157                        browsing_context_id
158                    );
159                    continue;
160                },
161            };
162            let pipeline = match self.pipelines.get(&browsing_context.pipeline_id) {
163                Some(pipeline) => pipeline,
164                None => {
165                    warn!(
166                        "Pipeline {:?} iterated after closure.",
167                        browsing_context.pipeline_id
168                    );
169                    continue;
170                },
171            };
172            self.stack.extend(pipeline.children.iter());
173            return Some(browsing_context);
174        }
175    }
176}
177
178/// An iterator over browsing contexts, returning all descendant
179/// contexts in depth-first order. Note that this iterator returns all
180/// contexts, not just the fully active ones.
181pub struct AllBrowsingContextsIterator<'a> {
182    /// The browsing contexts still to iterate over.
183    pub stack: Vec<BrowsingContextId>,
184
185    /// The set of all browsing contexts.
186    pub browsing_contexts: &'a FxHashMap<BrowsingContextId, BrowsingContext>,
187
188    /// The set of all pipelines.  We use this to find the
189    /// children of a browsing context, which are the iframes in all documents
190    /// in the session history.
191    pub pipelines: &'a FxHashMap<PipelineId, Pipeline>,
192}
193
194impl<'a> Iterator for AllBrowsingContextsIterator<'a> {
195    type Item = &'a BrowsingContext;
196    fn next(&mut self) -> Option<&'a BrowsingContext> {
197        let pipelines = self.pipelines;
198        loop {
199            let browsing_context_id = self.stack.pop()?;
200            let browsing_context = match self.browsing_contexts.get(&browsing_context_id) {
201                Some(browsing_context) => browsing_context,
202                None => {
203                    warn!(
204                        "BrowsingContext {:?} iterated after closure.",
205                        browsing_context_id
206                    );
207                    continue;
208                },
209            };
210            let child_browsing_context_ids = browsing_context
211                .pipelines
212                .iter()
213                .filter_map(|pipeline_id| pipelines.get(pipeline_id))
214                .flat_map(|pipeline| pipeline.children.iter());
215            self.stack.extend(child_browsing_context_ids);
216            return Some(browsing_context);
217        }
218    }
219}