Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix rooting issues with data channels callbacks
  • Loading branch information
ferjm committed Jun 29, 2020
1 parent f5c9300 commit ace0d77
Showing 1 changed file with 53 additions and 46 deletions.
99 changes: 53 additions & 46 deletions components/script/dom/rtcdatachannel.rs
Expand Up @@ -45,7 +45,6 @@ pub struct RTCDataChannel {
}

impl RTCDataChannel {
#[allow(unrooted_must_root)]
pub fn new_inherited(
webrtc_controller: &DomRefCell<Option<WebRtcController>>,
label: USVString,
Expand All @@ -67,7 +66,7 @@ impl RTCDataChannel {
channel.unwrap()
};

let rtc_data_channel = RTCDataChannel {
RTCDataChannel {
eventtarget: EventTarget::new_inherited(),
channel,
label,
Expand All @@ -77,87 +76,95 @@ impl RTCDataChannel {
protocol: options.protocol.clone(),
negotiated: options.negotiated,
id: options.id,
};
}
}

let trusted = Trusted::new(&rtc_data_channel);
pub fn new(
global: &GlobalScope,
webrtc_controller: &DomRefCell<Option<WebRtcController>>,
label: USVString,
options: &RTCDataChannelInit,
channel: Option<Box<dyn WebRtcDataChannelBackend>>,
) -> DomRoot<RTCDataChannel> {
let rtc_data_channel = reflect_dom_object(
Box::new(RTCDataChannel::new_inherited(
webrtc_controller,
label,
options,
channel,
)),
global,
);

let trusted = Trusted::new(&*rtc_data_channel);
let (task_source, canceller) = global
.as_window()
.task_manager()
.networking_task_source_with_canceller();

let this = trusted.clone();
rtc_data_channel.channel.set_on_open(Box::new(move || {
let this_ = this.clone();
let global = this.root().global();
let task_source = global.networking_task_source();
let _ = task_source.queue(
let this = this.clone();
let _ = task_source.queue_with_canceller(
task!(on_open: move || {
this_.root().on_open();
this.root().on_open();
}),
global.upcast(),
&canceller,
);
}));

let this = trusted.clone();
let (task_source, canceller) = global
.as_window()
.task_manager()
.networking_task_source_with_canceller();
rtc_data_channel.channel.set_on_close(Box::new(move || {
let this_ = this.clone();
let global = this.root().global();
let task_source = global.networking_task_source();
let _ = task_source.queue(
let this = this.clone();
let _ = task_source.queue_with_canceller(
task!(on_close: move || {
this_.root().on_close();
this.root().on_close();
}),
global.upcast(),
&canceller,
);
}));

let this = trusted.clone();
let (task_source, canceller) = global
.as_window()
.task_manager()
.networking_task_source_with_canceller();
rtc_data_channel
.channel
.set_on_error(Box::new(move |error| {
let this_ = this.clone();
let global = this.root().global();
let task_source = global.networking_task_source();
let _ = task_source.queue(
let this = this.clone();
let _ = task_source.queue_with_canceller(
task!(on_error: move || {
this_.root().on_error(error);
this.root().on_error(error);
}),
global.upcast(),
&canceller,
);
}));

let this = trusted.clone();
let (task_source, canceller) = global
.as_window()
.task_manager()
.networking_task_source_with_canceller();
rtc_data_channel
.channel
.set_on_message(Box::new(move |message| {
let this_ = this.clone();
let global = this.root().global();
let task_source = global.networking_task_source();
let _ = task_source.queue(
let this = this.clone();
let _ = task_source.queue_with_canceller(
task!(on_message: move || {
this_.root().on_message(message);
this.root().on_message(message);
}),
global.upcast(),
&canceller,
);
}));

rtc_data_channel
}

pub fn new(
global: &GlobalScope,
webrtc_controller: &DomRefCell<Option<WebRtcController>>,
label: USVString,
options: &RTCDataChannelInit,
channel: Option<Box<dyn WebRtcDataChannelBackend>>,
) -> DomRoot<RTCDataChannel> {
reflect_dom_object(
Box::new(RTCDataChannel::new_inherited(
webrtc_controller,
label,
options,
channel,
)),
global,
)
}

fn on_open(&self) {
let event = Event::new(
&self.global(),
Expand Down

0 comments on commit ace0d77

Please sign in to comment.