Use task! for posting messages

This commit is contained in:
Anthony Ramine 2017-09-18 09:32:24 +02:00
parent c051fc1995
commit a6af86824b

View file

@ -1969,59 +1969,50 @@ fn debug_reflow_events(id: PipelineId, goal: &ReflowGoal, query_type: &ReflowQue
println!("{}", debug_msg); println!("{}", debug_msg);
} }
struct PostMessageHandler { impl Window {
destination: Trusted<Window>, // https://html.spec.whatwg.org/multipage/#dom-window-postmessage step 7.
origin: Option<ImmutableOrigin>, pub fn post_message(
message: StructuredCloneData, &self,
} target_origin: Option<ImmutableOrigin>,
serialize_with_transfer_result: StructuredCloneData,
) {
let this = Trusted::new(self);
let task = box task!(post_serialised_message: move || {
let this = this.root();
impl PostMessageHandler { // Step 7.1.
fn new(window: &Window, if let Some(target_origin) = target_origin {
origin: Option<ImmutableOrigin>, if !target_origin.same_origin(this.Document().origin()) {
message: StructuredCloneData) -> PostMessageHandler {
PostMessageHandler {
destination: Trusted::new(window),
origin: origin,
message: message,
}
}
}
impl Task for PostMessageHandler {
// https://html.spec.whatwg.org/multipage/#dom-window-postmessage steps 10-12.
fn run(self: Box<Self>) {
let this = *self;
let window = this.destination.root();
// Step 10.
let doc = window.Document();
if let Some(source) = this.origin {
if !source.same_origin(doc.origin()) {
return; return;
} }
} }
let cx = window.get_cx(); // Steps 7.2.-7.5.
let globalhandle = window.reflector().get_jsobject(); let cx = this.get_cx();
let _ac = JSAutoCompartment::new(cx, globalhandle.get()); let obj = this.reflector().get_jsobject();
let _ac = JSAutoCompartment::new(cx, obj.get());
rooted!(in(cx) let mut message_clone = UndefinedValue());
serialize_with_transfer_result.read(
this.upcast(),
message_clone.handle_mut(),
);
rooted!(in(cx) let mut message = UndefinedValue()); // Step 7.6.
this.message.read(window.upcast(), message.handle_mut()); // TODO: MessagePort array.
// Step 11-12. // Step 7.7.
// TODO(#12719): set the other attributes. // TODO(#12719): Set the other attributes.
MessageEvent::dispatch_jsval(window.upcast(), MessageEvent::dispatch_jsval(
window.upcast(), this.upcast(),
message.handle()); this.upcast(),
} message.handle(),
} );
});
impl Window { // FIXME(nox): Why are errors silenced here?
pub fn post_message(&self, origin: Option<ImmutableOrigin>, data: StructuredCloneData) {
let task = PostMessageHandler::new(self, origin, data);
let task = self.task_canceller().wrap_task(box task);
let msg = CommonScriptMsg::Task(ScriptThreadEventCategory::DomEvent, task);
// TODO(#12718): Use the "posted message task source". // TODO(#12718): Use the "posted message task source".
let _ = self.script_chan.send(msg); let _ = self.script_chan.send(CommonScriptMsg::Task(
ScriptThreadEventCategory::DomEvent,
self.task_canceller().wrap_task(task),
));
} }
} }