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