mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
Implement ondatachannel event
This commit is contained in:
parent
2c5fc3b3c8
commit
f5c930087e
6 changed files with 139 additions and 4 deletions
|
@ -23,6 +23,7 @@ compositionstart
|
|||
compositionupdate
|
||||
controllerchange
|
||||
cursive
|
||||
datachannel
|
||||
date
|
||||
datetime-local
|
||||
dir
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
@ -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(),
|
||||
|
@ -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,
|
||||
)
|
||||
|
|
74
components/script/dom/rtcdatachannelevent.rs
Normal file
74
components/script/dom/rtcdatachannelevent.rs
Normal file
|
@ -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()
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
15
components/script/dom/webidls/RTCDataChannelEvent.webidl
Normal file
15
components/script/dom/webidls/RTCDataChannelEvent.webidl
Normal file
|
@ -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;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue