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