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