script/dom/
dissimilaroriginwindow.rs1use base::id::PipelineId;
6use constellation_traits::{ScriptToConstellationMessage, StructuredSerializedData};
7use dom_struct::dom_struct;
8use js::jsapi::{Heap, JSObject};
9use js::jsval::UndefinedValue;
10use js::rust::{CustomAutoRooter, CustomAutoRooterGuard, HandleValue, MutableHandleValue};
11use script_bindings::script_runtime::temp_cx;
12use servo_url::ServoUrl;
13
14use crate::dom::bindings::codegen::Bindings::DissimilarOriginWindowBinding;
15use crate::dom::bindings::codegen::Bindings::DissimilarOriginWindowBinding::DissimilarOriginWindowMethods;
16use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowPostMessageOptions;
17use crate::dom::bindings::error::{Error, ErrorResult};
18use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
19use crate::dom::bindings::str::USVString;
20use crate::dom::bindings::structuredclone;
21use crate::dom::bindings::trace::RootedTraceableBox;
22use crate::dom::dissimilaroriginlocation::DissimilarOriginLocation;
23use crate::dom::globalscope::GlobalScope;
24use crate::dom::windowproxy::WindowProxy;
25use crate::script_runtime::{CanGc, JSContext};
26
27#[dom_struct]
37pub(crate) struct DissimilarOriginWindow {
38 globalscope: GlobalScope,
40
41 window_proxy: Dom<WindowProxy>,
43
44 location: MutNullableDom<DissimilarOriginLocation>,
46}
47
48impl DissimilarOriginWindow {
49 #[expect(unsafe_code)]
50 pub(crate) fn new(
51 global_to_clone_from: &GlobalScope,
52 window_proxy: &WindowProxy,
53 ) -> DomRoot<Self> {
54 let mut cx = unsafe { temp_cx() };
55 let win = Box::new(Self {
56 globalscope: GlobalScope::new_inherited(
57 PipelineId::new(),
58 global_to_clone_from.devtools_chan().cloned(),
59 global_to_clone_from.mem_profiler_chan().clone(),
60 global_to_clone_from.time_profiler_chan().clone(),
61 global_to_clone_from.script_to_constellation_chan().clone(),
62 global_to_clone_from.script_to_embedder_chan().clone(),
63 global_to_clone_from.resource_threads().clone(),
64 global_to_clone_from.storage_threads().clone(),
65 global_to_clone_from.origin().clone(),
66 global_to_clone_from.creation_url().clone(),
67 global_to_clone_from.top_level_creation_url().clone(),
68 #[cfg(feature = "webgpu")]
69 global_to_clone_from.wgpu_id_hub(),
70 Some(global_to_clone_from.is_secure_context()),
71 false,
72 global_to_clone_from.font_context().cloned(),
73 ),
74 window_proxy: Dom::from_ref(window_proxy),
75 location: Default::default(),
76 });
77 DissimilarOriginWindowBinding::Wrap::<crate::DomTypeHolder>(&mut cx, win)
78 }
79
80 pub(crate) fn window_proxy(&self) -> DomRoot<WindowProxy> {
81 DomRoot::from_ref(&*self.window_proxy)
82 }
83}
84
85impl DissimilarOriginWindowMethods<crate::DomTypeHolder> for DissimilarOriginWindow {
86 fn Window(&self) -> DomRoot<WindowProxy> {
88 self.window_proxy()
89 }
90
91 fn Self_(&self) -> DomRoot<WindowProxy> {
93 self.window_proxy()
94 }
95
96 fn Frames(&self) -> DomRoot<WindowProxy> {
98 self.window_proxy()
99 }
100
101 fn GetParent(&self) -> Option<DomRoot<WindowProxy>> {
103 if self.window_proxy.is_browsing_context_discarded() {
105 return None;
106 }
107 if let Some(parent) = self.window_proxy.parent() {
109 return Some(DomRoot::from_ref(parent));
110 }
111 Some(DomRoot::from_ref(&*self.window_proxy))
113 }
114
115 fn GetTop(&self) -> Option<DomRoot<WindowProxy>> {
117 if self.window_proxy.is_browsing_context_discarded() {
119 return None;
120 }
121 Some(DomRoot::from_ref(self.window_proxy.top()))
123 }
124
125 fn Length(&self) -> u32 {
127 0
129 }
130
131 fn Close(&self) {
133 }
135
136 fn Closed(&self) -> bool {
138 false
140 }
141
142 fn PostMessage(
144 &self,
145 cx: JSContext,
146 message: HandleValue,
147 target_origin: USVString,
148 transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
149 ) -> ErrorResult {
150 self.post_message_impl(&target_origin, cx, message, transfer)
151 }
152
153 fn PostMessage_(
155 &self,
156 cx: JSContext,
157 message: HandleValue,
158 options: RootedTraceableBox<WindowPostMessageOptions>,
159 ) -> ErrorResult {
160 let mut rooted = CustomAutoRooter::new(
161 options
162 .parent
163 .transfer
164 .iter()
165 .map(|js: &RootedTraceableBox<Heap<*mut JSObject>>| js.get())
166 .collect(),
167 );
168 let transfer = CustomAutoRooterGuard::new(*cx, &mut rooted);
169
170 self.post_message_impl(&options.targetOrigin, cx, message, transfer)
171 }
172
173 fn Opener(&self, _: JSContext, mut retval: MutableHandleValue) {
175 retval.set(UndefinedValue());
177 }
178
179 fn SetOpener(&self, _: JSContext, _: HandleValue) {
181 }
183
184 fn Blur(&self) {
186 }
189
190 fn Focus(&self) {
192 self.window_proxy().focus();
193 }
194
195 fn Location(&self, can_gc: CanGc) -> DomRoot<DissimilarOriginLocation> {
197 self.location
198 .or_init(|| DissimilarOriginLocation::new(self, can_gc))
199 }
200}
201
202impl DissimilarOriginWindow {
203 fn post_message_impl(
205 &self,
206 target_origin: &USVString,
207 cx: JSContext,
208 message: HandleValue,
209 transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
210 ) -> ErrorResult {
211 let data = structuredclone::write(cx, message, Some(transfer))?;
213
214 self.post_message(target_origin, data)
215 }
216
217 pub(crate) fn post_message(
219 &self,
220 target_origin: &USVString,
221 data: StructuredSerializedData,
222 ) -> ErrorResult {
223 let target = self.window_proxy.browsing_context_id();
225 let incumbent = match GlobalScope::incumbent() {
227 None => panic!("postMessage called with no incumbent global"),
228 Some(incumbent) => incumbent,
229 };
230
231 let source_origin = incumbent.origin().immutable().clone();
232
233 let target_origin = match target_origin.0[..].as_ref() {
235 "*" => None,
236 "/" => Some(source_origin.clone()),
237 url => match ServoUrl::parse(url) {
238 Ok(url) => Some(url.origin().clone()),
239 Err(_) => return Err(Error::Syntax(None)),
240 },
241 };
242 let msg = ScriptToConstellationMessage::PostMessage {
243 target,
244 source: incumbent.pipeline_id(),
245 source_origin,
246 target_origin,
247 data,
248 };
249 let _ = incumbent.script_to_constellation_chan().send(msg);
251 Ok(())
252 }
253}