script/
script_window_proxies.rs1use base::id::{BrowsingContextId, PipelineId, WebViewId};
6use constellation_traits::ScriptToConstellationMessage;
7use ipc_channel::ipc;
8use rustc_hash::FxBuildHasher;
9use script_bindings::inheritance::Castable;
10use script_bindings::root::{Dom, DomRoot};
11use script_bindings::str::DOMString;
12
13use crate::document_collection::DocumentCollection;
14use crate::dom::bindings::cell::DomRefCell;
15use crate::dom::bindings::trace::HashMapTracedValues;
16use crate::dom::node::NodeTraits;
17use crate::dom::types::{GlobalScope, Window};
18use crate::dom::windowproxy::{CreatorBrowsingContextInfo, WindowProxy};
19use crate::messaging::ScriptThreadSenders;
20
21#[derive(JSTraceable, Default, MallocSizeOf)]
22#[cfg_attr(crown, crown::unrooted_must_root_lint::allow_unrooted_in_rc)]
23#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
24pub(crate) struct ScriptWindowProxies {
25 map: DomRefCell<HashMapTracedValues<BrowsingContextId, Dom<WindowProxy>, FxBuildHasher>>,
26}
27
28impl ScriptWindowProxies {
29 pub(crate) fn find_window_proxy(&self, id: BrowsingContextId) -> Option<DomRoot<WindowProxy>> {
30 self.map
31 .borrow()
32 .get(&id)
33 .map(|context| DomRoot::from_ref(&**context))
34 }
35
36 pub(crate) fn find_window_proxy_by_name(
37 &self,
38 name: &DOMString,
39 ) -> Option<DomRoot<WindowProxy>> {
40 for (_, proxy) in self.map.borrow().iter() {
41 if proxy.get_name() == *name {
42 return Some(DomRoot::from_ref(&**proxy));
43 }
44 }
45 None
46 }
47
48 pub(crate) fn get(&self, id: BrowsingContextId) -> Option<DomRoot<WindowProxy>> {
49 self.map
50 .borrow()
51 .get(&id)
52 .map(|context| DomRoot::from_ref(&**context))
53 }
54
55 pub(crate) fn insert(&self, id: BrowsingContextId, proxy: DomRoot<WindowProxy>) {
56 self.map.borrow_mut().insert(id, Dom::from_ref(&*proxy));
57 }
58
59 pub(crate) fn remove(&self, id: BrowsingContextId) {
60 self.map.borrow_mut().remove(&id);
61 }
62
63 pub(crate) fn remote_window_proxy(
70 &self,
71 senders: &ScriptThreadSenders,
72 global_to_clone: &GlobalScope,
73 webview_id: WebViewId,
74 pipeline_id: PipelineId,
75 opener: Option<BrowsingContextId>,
76 ) -> Option<DomRoot<WindowProxy>> {
77 let (browsing_context_id, parent_pipeline_id) =
78 self.ask_constellation_for_browsing_context_info(senders, webview_id, pipeline_id)?;
79 if let Some(window_proxy) = self.get(browsing_context_id) {
80 return Some(window_proxy);
81 }
82
83 let parent_browsing_context = parent_pipeline_id.and_then(|parent_id| {
84 self.remote_window_proxy(senders, global_to_clone, webview_id, parent_id, opener)
85 });
86
87 let opener_browsing_context = opener.and_then(|id| self.find_window_proxy(id));
88
89 let creator = CreatorBrowsingContextInfo::from(
90 parent_browsing_context.as_deref(),
91 opener_browsing_context.as_deref(),
92 );
93
94 let window_proxy = WindowProxy::new_dissimilar_origin(
95 global_to_clone,
96 browsing_context_id,
97 webview_id,
98 parent_browsing_context.as_deref(),
99 opener,
100 creator,
101 );
102 self.insert(browsing_context_id, DomRoot::from_ref(&*window_proxy));
103 Some(window_proxy)
104 }
105
106 #[expect(clippy::too_many_arguments)]
113 pub(crate) fn local_window_proxy(
114 &self,
115 senders: &ScriptThreadSenders,
116 documents: &DomRefCell<DocumentCollection>,
117 window: &Window,
118 browsing_context_id: BrowsingContextId,
119 webview_id: WebViewId,
120 parent_info: Option<PipelineId>,
121 opener: Option<BrowsingContextId>,
122 ) -> DomRoot<WindowProxy> {
123 if let Some(window_proxy) = self.get(browsing_context_id) {
124 return window_proxy;
127 }
128 let iframe = parent_info.and_then(|parent_id| {
129 documents
130 .borrow()
131 .find_iframe(parent_id, browsing_context_id)
132 });
133 let parent_browsing_context = match (parent_info, iframe.as_ref()) {
134 (_, Some(iframe)) => Some(iframe.owner_window().window_proxy()),
135 (Some(parent_id), _) => {
136 self.remote_window_proxy(senders, window.upcast(), webview_id, parent_id, opener)
137 },
138 _ => None,
139 };
140
141 let opener_browsing_context = opener.and_then(|id| self.find_window_proxy(id));
142
143 let creator = CreatorBrowsingContextInfo::from(
144 parent_browsing_context.as_deref(),
145 opener_browsing_context.as_deref(),
146 );
147
148 let window_proxy = WindowProxy::new(
149 window,
150 browsing_context_id,
151 webview_id,
152 iframe.as_deref().map(Castable::upcast),
153 parent_browsing_context.as_deref(),
154 opener,
155 creator,
156 );
157 self.insert(browsing_context_id, DomRoot::from_ref(&*window_proxy));
158 window_proxy
159 }
160
161 fn ask_constellation_for_browsing_context_info(
162 &self,
163 senders: &ScriptThreadSenders,
164 webview_id: WebViewId,
165 pipeline_id: PipelineId,
166 ) -> Option<(BrowsingContextId, Option<PipelineId>)> {
167 let (result_sender, result_receiver) = ipc::channel().unwrap();
168 let msg = ScriptToConstellationMessage::GetBrowsingContextInfo(pipeline_id, result_sender);
169 senders
170 .pipeline_to_constellation_sender
171 .send((webview_id, pipeline_id, msg))
172 .expect("Failed to send to constellation.");
173 result_receiver
174 .recv()
175 .expect("Failed to get browsing context info from constellation.")
176 }
177}