mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Use task! for posting messages
This commit is contained in:
parent
c051fc1995
commit
a6af86824b
1 changed files with 43 additions and 52 deletions
|
@ -1969,59 +1969,50 @@ fn debug_reflow_events(id: PipelineId, goal: &ReflowGoal, query_type: &ReflowQue
|
||||||
println!("{}", debug_msg);
|
println!("{}", debug_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct PostMessageHandler {
|
|
||||||
destination: Trusted<Window>,
|
|
||||||
origin: Option<ImmutableOrigin>,
|
|
||||||
message: StructuredCloneData,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PostMessageHandler {
|
|
||||||
fn new(window: &Window,
|
|
||||||
origin: Option<ImmutableOrigin>,
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let cx = window.get_cx();
|
|
||||||
let globalhandle = window.reflector().get_jsobject();
|
|
||||||
let _ac = JSAutoCompartment::new(cx, globalhandle.get());
|
|
||||||
|
|
||||||
rooted!(in(cx) let mut message = UndefinedValue());
|
|
||||||
this.message.read(window.upcast(), message.handle_mut());
|
|
||||||
|
|
||||||
// Step 11-12.
|
|
||||||
// TODO(#12719): set the other attributes.
|
|
||||||
MessageEvent::dispatch_jsval(window.upcast(),
|
|
||||||
window.upcast(),
|
|
||||||
message.handle());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Window {
|
impl Window {
|
||||||
pub fn post_message(&self, origin: Option<ImmutableOrigin>, data: StructuredCloneData) {
|
// https://html.spec.whatwg.org/multipage/#dom-window-postmessage step 7.
|
||||||
let task = PostMessageHandler::new(self, origin, data);
|
pub fn post_message(
|
||||||
let task = self.task_canceller().wrap_task(box task);
|
&self,
|
||||||
let msg = CommonScriptMsg::Task(ScriptThreadEventCategory::DomEvent, task);
|
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();
|
||||||
|
|
||||||
|
// Step 7.1.
|
||||||
|
if let Some(target_origin) = target_origin {
|
||||||
|
if !target_origin.same_origin(this.Document().origin()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Steps 7.2.-7.5.
|
||||||
|
let cx = this.get_cx();
|
||||||
|
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(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Step 7.6.
|
||||||
|
// TODO: MessagePort array.
|
||||||
|
|
||||||
|
// Step 7.7.
|
||||||
|
// TODO(#12719): Set the other attributes.
|
||||||
|
MessageEvent::dispatch_jsval(
|
||||||
|
this.upcast(),
|
||||||
|
this.upcast(),
|
||||||
|
message.handle(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
// FIXME(nox): Why are errors silenced here?
|
||||||
// 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),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue