Skip to content

Commit

Permalink
Implement ondatachannel event
Browse files Browse the repository at this point in the history
  • Loading branch information
ferjm committed Jun 29, 2020
1 parent 2c5fc3b commit f5c9300
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 4 deletions.
1 change: 1 addition & 0 deletions components/atoms/static_atoms.txt
Expand Up @@ -23,6 +23,7 @@ compositionstart
compositionupdate
controllerchange
cursive
datachannel
date
datetime-local
dir
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/mod.rs
Expand Up @@ -487,6 +487,7 @@ pub mod readablestream;
pub mod request;
pub mod response;
pub mod rtcdatachannel;
pub mod rtcdatachannelevent;
pub mod rtcerror;
pub mod rtcerrorevent;
pub mod rtcicecandidate;
Expand Down
11 changes: 9 additions & 2 deletions components/script/dom/rtcdatachannel.rs
Expand Up @@ -50,6 +50,7 @@ impl RTCDataChannel {
webrtc_controller: &DomRefCell<Option<WebRtcController>>,
label: USVString,
options: &RTCDataChannelInit,
channel: Option<Box<dyn WebRtcDataChannelBackend>>,
) -> RTCDataChannel {
let webrtc = webrtc_controller.borrow();
let webrtc = webrtc.as_ref().unwrap();
Expand All @@ -59,8 +60,12 @@ impl RTCDataChannel {
let mut init: WebRtcDataChannelInit = options.into();
init.label = label.to_string();

webrtc.create_data_channel(init, sender);
let channel = receiver.recv().unwrap();
let channel = if channel.is_none() {
webrtc.create_data_channel(init, sender);
receiver.recv().unwrap()
} else {
channel.unwrap()
};

let rtc_data_channel = RTCDataChannel {
eventtarget: EventTarget::new_inherited(),
Expand Down Expand Up @@ -140,12 +145,14 @@ impl RTCDataChannel {
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,
)
Expand Down
74 changes: 74 additions & 0 deletions components/script/dom/rtcdatachannelevent.rs
@@ -0,0 +1,74 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use crate::dom::bindings::codegen::Bindings::RTCDataChannelEventBinding::RTCDataChannelEventInit;
use crate::dom::bindings::codegen::Bindings::RTCDataChannelEventBinding::RTCDataChannelEventMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::event::Event;
use crate::dom::globalscope::GlobalScope;
use crate::dom::rtcdatachannel::RTCDataChannel;
use crate::dom::window::Window;
use dom_struct::dom_struct;
use servo_atoms::Atom;

#[dom_struct]
pub struct RTCDataChannelEvent {
event: Event,
channel: Dom<RTCDataChannel>,
}

impl RTCDataChannelEvent {
fn new_inherited(channel: &RTCDataChannel) -> RTCDataChannelEvent {
RTCDataChannelEvent {
event: Event::new_inherited(),
channel: Dom::from_ref(channel),
}
}

pub fn new(
global: &GlobalScope,
type_: Atom,
bubbles: bool,
cancelable: bool,
channel: &RTCDataChannel,
) -> DomRoot<RTCDataChannelEvent> {
let event = reflect_dom_object(
Box::new(RTCDataChannelEvent::new_inherited(&channel)),
global,
);
{
let event = event.upcast::<Event>();
event.init_event(type_, bubbles, cancelable);
}
event
}

pub fn Constructor(
window: &Window,
type_: DOMString,
init: &RTCDataChannelEventInit,
) -> DomRoot<RTCDataChannelEvent> {
RTCDataChannelEvent::new(
&window.global(),
Atom::from(type_),
init.parent.bubbles,
init.parent.cancelable,
&init.channel,
)
}
}

impl RTCDataChannelEventMethods for RTCDataChannelEvent {
fn Channel(&self) -> DomRoot<RTCDataChannel> {
DomRoot::from_ref(&*self.channel)
}

fn IsTrusted(&self) -> bool {
self.event.IsTrusted()
}
}
41 changes: 39 additions & 2 deletions components/script/dom/rtcpeerconnection.rs
Expand Up @@ -29,6 +29,7 @@ use crate::dom::mediastream::MediaStream;
use crate::dom::mediastreamtrack::MediaStreamTrack;
use crate::dom::promise::Promise;
use crate::dom::rtcdatachannel::RTCDataChannel;
use crate::dom::rtcdatachannelevent::RTCDataChannelEvent;
use crate::dom::rtcicecandidate::RTCIceCandidate;
use crate::dom::rtcpeerconnectioniceevent::RTCPeerConnectionIceEvent;
use crate::dom::rtcsessiondescription::RTCSessionDescription;
Expand All @@ -44,7 +45,7 @@ use servo_media::streams::registry::MediaStreamId;
use servo_media::streams::MediaStreamType;
use servo_media::webrtc::{
BundlePolicy, GatheringState, IceCandidate, IceConnectionState, SdpType, SessionDescription,
SignalingState, WebRtcController, WebRtcSignaller,
SignalingState, WebRtcController, WebRtcDataChannelBackend, WebRtcSignaller,
};
use servo_media::ServoMedia;

Expand Down Expand Up @@ -145,6 +146,18 @@ impl WebRtcSignaller for RTCSignaller {
);
}

fn on_data_channel(&self, channel: Box<dyn WebRtcDataChannelBackend>) {
// XXX(ferjm) get label and options from channel properties.
let this = self.trusted.clone();
let _ = self.task_source.queue_with_canceller(
task!(on_data_channel: move || {
let this = this.root();
this.on_data_channel(channel);
}),
&self.canceller,
);
}

fn close(&self) {
// do nothing
}
Expand Down Expand Up @@ -259,6 +272,24 @@ impl RTCPeerConnection {
event.upcast::<Event>().fire(self.upcast());
}

fn on_data_channel(&self, channel: Box<dyn WebRtcDataChannelBackend>) {
if self.closed.get() {
return;
}

let channel = RTCDataChannel::new(
&self.global(),
&self.controller,
USVString::from("".to_owned()),
&RTCDataChannelInit::empty(),
Some(channel),
);

let event =
RTCDataChannelEvent::new(&self.global(), atom!("datachannel"), false, false, &channel);
event.upcast::<Event>().fire(self.upcast());
}

/// https://www.w3.org/TR/webrtc/#update-ice-gathering-state
fn update_gathering_state(&self, state: GatheringState) {
// step 1
Expand Down Expand Up @@ -646,7 +677,13 @@ impl RTCPeerConnectionMethods for RTCPeerConnection {
label: USVString,
dataChannelDict: &RTCDataChannelInit,
) -> DomRoot<RTCDataChannel> {
RTCDataChannel::new(&self.global(), &self.controller, label, dataChannelDict)
RTCDataChannel::new(
&self.global(),
&self.controller,
label,
dataChannelDict,
None,
)
}
}

Expand Down
15 changes: 15 additions & 0 deletions components/script/dom/webidls/RTCDataChannelEvent.webidl
@@ -0,0 +1,15 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

// https://www.w3.org/TR/webrtc/#rtcdatachannelevent

[Exposed=Window]
interface RTCDataChannelEvent : Event {
constructor(DOMString type, RTCDataChannelEventInit eventInitDict);
readonly attribute RTCDataChannel channel;
};

dictionary RTCDataChannelEventInit : EventInit {
required RTCDataChannel channel;
};

0 comments on commit f5c9300

Please sign in to comment.