Skip to content
Permalink
Browse files

use dom and new_inherited in (extendable)messageevent

  • Loading branch information
gterzian committed Jan 9, 2020
1 parent b876168 commit a9fcd7379b428f45707085abbf43ea2dd9a8bf13
Showing with 66 additions and 18 deletions.
  1. +27 −7 components/script/dom/extendablemessageevent.rs
  2. +39 −11 components/script/dom/messageevent.rs
@@ -7,7 +7,7 @@ use crate::dom::bindings::codegen::Bindings::ExtendableMessageEventBinding::Exte
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::trace::RootedTraceableBox;
use crate::dom::bindings::utils::message_ports_to_frozen_array;
@@ -31,10 +31,27 @@ pub struct ExtendableMessageEvent {
data: Heap<JSVal>,
origin: DOMString,
lastEventId: DOMString,
ports: Vec<DomRoot<MessagePort>>,
ports: Vec<Dom<MessagePort>>,
}

impl ExtendableMessageEvent {
pub fn new_inherited(
origin: DOMString,
lastEventId: DOMString,
ports: Vec<DomRoot<MessagePort>>,
) -> ExtendableMessageEvent {
ExtendableMessageEvent {
event: ExtendableEvent::new_inherited(),
data: Heap::default(),
origin: origin,
lastEventId: lastEventId,
ports: ports
.into_iter()
.map(|port| Dom::from_ref(&*port))
.collect(),
}
}

pub fn new(
global: &GlobalScope,
type_: Atom,
@@ -45,13 +62,11 @@ impl ExtendableMessageEvent {
lastEventId: DOMString,
ports: Vec<DomRoot<MessagePort>>,
) -> DomRoot<ExtendableMessageEvent> {
let ev = Box::new(ExtendableMessageEvent {
event: ExtendableEvent::new_inherited(),
data: Heap::default(),
let ev = Box::new(ExtendableMessageEvent::new_inherited(
origin,
lastEventId,
ports,
});
));
let ev = reflect_dom_object(ev, global, ExtendableMessageEventBinding::Wrap);
{
let event = ev.upcast::<Event>();
@@ -126,6 +141,11 @@ impl ExtendableMessageEventMethods for ExtendableMessageEvent {

/// https://w3c.github.io/ServiceWorker/#extendablemessage-event-ports
fn Ports(&self, cx: JSContext) -> JSVal {
message_ports_to_frozen_array(self.ports.as_slice(), cx)
let ports: Vec<DomRoot<MessagePort>> = self
.ports
.iter()
.map(|port| DomRoot::from_ref(&**port))
.collect();
message_ports_to_frozen_array(ports.as_slice(), cx)
}
}
@@ -60,10 +60,31 @@ pub struct MessageEvent {
origin: DomRefCell<DOMString>,
source: DomRefCell<Option<SrcObject>>,
lastEventId: DomRefCell<DOMString>,
ports: DomRefCell<Vec<DomRoot<MessagePort>>>,
ports: DomRefCell<Vec<Dom<MessagePort>>>,
}

impl MessageEvent {
pub fn new_inherited(
origin: DOMString,
source: Option<&WindowProxyOrMessagePortOrServiceWorker>,
lastEventId: DOMString,
ports: Vec<DomRoot<MessagePort>>,
) -> MessageEvent {
MessageEvent {
event: Event::new_inherited(),
data: Heap::default(),
source: DomRefCell::new(source.map(|source| source.into())),
origin: DomRefCell::new(origin),
lastEventId: DomRefCell::new(lastEventId),
ports: DomRefCell::new(
ports
.into_iter()
.map(|port| Dom::from_ref(&*port))
.collect(),
),
}
}

pub fn new_uninitialized(global: &GlobalScope) -> DomRoot<MessageEvent> {
MessageEvent::new_initialized(
global,
@@ -83,14 +104,12 @@ impl MessageEvent {
lastEventId: DOMString,
ports: Vec<DomRoot<MessagePort>>,
) -> DomRoot<MessageEvent> {
let ev = Box::new(MessageEvent {
event: Event::new_inherited(),
data: Heap::default(),
source: DomRefCell::new(source.map(|source| source.into())),
origin: DomRefCell::new(origin),
lastEventId: DomRefCell::new(lastEventId),
ports: DomRefCell::new(ports),
});
let ev = Box::new(MessageEvent::new_inherited(
origin,
source,
lastEventId,
ports,
));
let ev = reflect_dom_object(ev, global, MessageEventBinding::Wrap);
ev.data.set(data.get());

@@ -219,7 +238,13 @@ impl MessageEventMethods for MessageEvent {

/// <https://html.spec.whatwg.org/multipage/#dom-messageevent-ports>
fn Ports(&self, cx: JSContext) -> JSVal {
message_ports_to_frozen_array(self.ports.borrow().as_slice(), cx)
let ports: Vec<DomRoot<MessagePort>> = self
.ports
.borrow()
.iter()
.map(|port| DomRoot::from_ref(&**port))
.collect();
message_ports_to_frozen_array(ports.as_slice(), cx)
}

/// <https://html.spec.whatwg.org/multipage/#dom-messageevent-initmessageevent>
@@ -239,7 +264,10 @@ impl MessageEventMethods for MessageEvent {
*self.origin.borrow_mut() = origin.clone();
*self.source.borrow_mut() = source.as_ref().map(|source| source.into());
*self.lastEventId.borrow_mut() = lastEventId.clone();
*self.ports.borrow_mut() = ports;
*self.ports.borrow_mut() = ports
.into_iter()
.map(|port| Dom::from_ref(&*port))
.collect();
self.event
.init_event(Atom::from(type_), bubbles, cancelable);
}

0 comments on commit a9fcd73

Please sign in to comment.
You can’t perform that action at this time.