servo_constellation/
browsingcontext.rs1use 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#[derive(Debug)]
21pub struct NewBrowsingContextInfo {
22 pub parent_pipeline_id: Option<PipelineId>,
25
26 pub is_private: bool,
28
29 pub inherited_secure_context: Option<bool>,
31
32 pub upgrade_insecure_navigations_set: HashSet<UpgradeInsecureNavigations>,
34}
35
36pub struct BrowsingContext {
43 pub id: BrowsingContextId,
45
46 pub webview_id: WebViewId,
48
49 pub viewport_details: ViewportDetails,
51
52 pub is_private: bool,
54
55 pub inherited_secure_context: Option<bool>,
57
58 pub upgrade_insecure_navigations_set: HashSet<UpgradeInsecureNavigations>,
60
61 pub pipeline_id: PipelineId,
63
64 pub parent_pipeline_id: Option<PipelineId>,
67
68 pub pipelines: FxHashSet<PipelineId>,
71}
72
73impl BrowsingContext {
74 #[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 pub fn is_top_level(&self) -> bool {
108 self.id == self.webview_id
109 }
110}
111
112pub struct FullyActiveBrowsingContextsIterator<'a> {
116 pub stack: Vec<BrowsingContextId>,
118
119 pub browsing_contexts: &'a FxHashMap<BrowsingContextId, BrowsingContext>,
121
122 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
173pub struct AllBrowsingContextsIterator<'a> {
177 pub stack: Vec<BrowsingContextId>,
179
180 pub browsing_contexts: &'a FxHashMap<BrowsingContextId, BrowsingContext>,
182
183 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}