use base::id::PipelineId;
use dom_struct::dom_struct;
use js::jsapi::{Heap, JSObject};
use js::jsval::UndefinedValue;
use js::rust::{CustomAutoRooter, CustomAutoRooterGuard, HandleValue, MutableHandleValue};
use script_traits::{ScriptMsg, StructuredSerializedData};
use servo_url::ServoUrl;
use crate::dom::bindings::codegen::Bindings::DissimilarOriginWindowBinding;
use crate::dom::bindings::codegen::Bindings::DissimilarOriginWindowBinding::DissimilarOriginWindowMethods;
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowPostMessageOptions;
use crate::dom::bindings::error::{Error, ErrorResult};
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
use crate::dom::bindings::str::USVString;
use crate::dom::bindings::structuredclone;
use crate::dom::bindings::trace::RootedTraceableBox;
use crate::dom::dissimilaroriginlocation::DissimilarOriginLocation;
use crate::dom::globalscope::GlobalScope;
use crate::dom::windowproxy::WindowProxy;
use crate::script_runtime::JSContext;
#[dom_struct]
pub struct DissimilarOriginWindow {
globalscope: GlobalScope,
window_proxy: Dom<WindowProxy>,
location: MutNullableDom<DissimilarOriginLocation>,
}
impl DissimilarOriginWindow {
#[allow(unsafe_code)]
pub fn new(global_to_clone_from: &GlobalScope, window_proxy: &WindowProxy) -> DomRoot<Self> {
let cx = GlobalScope::get_cx();
let win = Box::new(Self {
globalscope: GlobalScope::new_inherited(
PipelineId::new(),
global_to_clone_from.devtools_chan().cloned(),
global_to_clone_from.mem_profiler_chan().clone(),
global_to_clone_from.time_profiler_chan().clone(),
global_to_clone_from.script_to_constellation_chan().clone(),
global_to_clone_from.scheduler_chan().clone(),
global_to_clone_from.resource_threads().clone(),
global_to_clone_from.origin().clone(),
global_to_clone_from.creation_url().clone(),
global_to_clone_from.microtask_queue().clone(),
global_to_clone_from.is_headless(),
global_to_clone_from.get_user_agent(),
#[cfg(feature = "webgpu")]
global_to_clone_from.wgpu_id_hub(),
Some(global_to_clone_from.is_secure_context()),
false,
),
window_proxy: Dom::from_ref(window_proxy),
location: Default::default(),
});
unsafe { DissimilarOriginWindowBinding::Wrap(cx, win) }
}
pub fn window_proxy(&self) -> DomRoot<WindowProxy> {
DomRoot::from_ref(&*self.window_proxy)
}
}
impl DissimilarOriginWindowMethods<crate::DomTypeHolder> for DissimilarOriginWindow {
fn Window(&self) -> DomRoot<WindowProxy> {
self.window_proxy()
}
fn Self_(&self) -> DomRoot<WindowProxy> {
self.window_proxy()
}
fn Frames(&self) -> DomRoot<WindowProxy> {
self.window_proxy()
}
fn GetParent(&self) -> Option<DomRoot<WindowProxy>> {
if self.window_proxy.is_browsing_context_discarded() {
return None;
}
if let Some(parent) = self.window_proxy.parent() {
return Some(DomRoot::from_ref(parent));
}
Some(DomRoot::from_ref(&*self.window_proxy))
}
fn GetTop(&self) -> Option<DomRoot<WindowProxy>> {
if self.window_proxy.is_browsing_context_discarded() {
return None;
}
Some(DomRoot::from_ref(self.window_proxy.top()))
}
fn Length(&self) -> u32 {
0
}
fn Close(&self) {
}
fn Closed(&self) -> bool {
false
}
fn PostMessage(
&self,
cx: JSContext,
message: HandleValue,
target_origin: USVString,
transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
) -> ErrorResult {
self.post_message_impl(&target_origin, cx, message, transfer)
}
fn PostMessage_(
&self,
cx: JSContext,
message: HandleValue,
options: RootedTraceableBox<WindowPostMessageOptions>,
) -> ErrorResult {
let mut rooted = CustomAutoRooter::new(
options
.parent
.transfer
.iter()
.map(|js: &RootedTraceableBox<Heap<*mut JSObject>>| js.get())
.collect(),
);
let transfer = CustomAutoRooterGuard::new(*cx, &mut rooted);
self.post_message_impl(&options.targetOrigin, cx, message, transfer)
}
fn Opener(&self, _: JSContext, mut retval: MutableHandleValue) {
retval.set(UndefinedValue());
}
fn SetOpener(&self, _: JSContext, _: HandleValue) {
}
fn Blur(&self) {
}
fn Focus(&self) {
}
fn Location(&self) -> DomRoot<DissimilarOriginLocation> {
self.location
.or_init(|| DissimilarOriginLocation::new(self))
}
}
impl DissimilarOriginWindow {
fn post_message_impl(
&self,
target_origin: &USVString,
cx: JSContext,
message: HandleValue,
transfer: CustomAutoRooterGuard<Vec<*mut JSObject>>,
) -> ErrorResult {
let data = structuredclone::write(cx, message, Some(transfer))?;
self.post_message(target_origin, data)
}
pub fn post_message(
&self,
target_origin: &USVString,
data: StructuredSerializedData,
) -> ErrorResult {
let target = self.window_proxy.browsing_context_id();
let incumbent = match GlobalScope::incumbent() {
None => panic!("postMessage called with no incumbent global"),
Some(incumbent) => incumbent,
};
let source_origin = incumbent.origin().immutable().clone();
let target_origin = match target_origin.0[..].as_ref() {
"*" => None,
"/" => Some(source_origin.clone()),
url => match ServoUrl::parse(url) {
Ok(url) => Some(url.origin().clone()),
Err(_) => return Err(Error::Syntax),
},
};
let msg = ScriptMsg::PostMessage {
target,
source: incumbent.pipeline_id(),
source_origin,
target_origin,
data,
};
let _ = incumbent.script_to_constellation_chan().send(msg);
Ok(())
}
}