Skip to content

Commit

Permalink
RTCDataChannel.readyState getter
Browse files Browse the repository at this point in the history
  • Loading branch information
ferjm committed Jun 29, 2020
1 parent 9d456d5 commit 7db485a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions components/atoms/static_atoms.txt
Expand Up @@ -16,6 +16,7 @@ characteristicvaluechanged
checkbox
click
close
closing
color
complete
compositionend
Expand Down
45 changes: 43 additions & 2 deletions components/script/dom/rtcdatachannel.rs
Expand Up @@ -2,8 +2,10 @@
* 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::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::RTCDataChannelBinding::RTCDataChannelInit;
use crate::dom::bindings::codegen::Bindings::RTCDataChannelBinding::RTCDataChannelMethods;
use crate::dom::bindings::codegen::Bindings::RTCDataChannelBinding::RTCDataChannelState;
use crate::dom::bindings::codegen::Bindings::RTCErrorBinding::{RTCErrorDetailType, RTCErrorInit};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
Expand All @@ -20,7 +22,9 @@ use dom_struct::dom_struct;
use js::conversions::ToJSValConvertible;
use js::jsapi::JSAutoRealm;
use js::jsval::UndefinedValue;
use servo_media::webrtc::{DataChannelId, DataChannelInit, DataChannelMessage, WebRtcError};
use servo_media::webrtc::{
DataChannelId, DataChannelInit, DataChannelMessage, DataChannelState, WebRtcError,
};

#[dom_struct]
pub struct RTCDataChannel {
Expand All @@ -35,6 +39,7 @@ pub struct RTCDataChannel {
protocol: USVString,
negotiated: bool,
id: Option<u16>,
ready_state: DomRefCell<RTCDataChannelState>,
}

impl RTCDataChannel {
Expand Down Expand Up @@ -68,6 +73,7 @@ impl RTCDataChannel {
protocol: options.protocol.clone(),
negotiated: options.negotiated,
id: options.id,
ready_state: DomRefCell::new(RTCDataChannelState::Connecting),
};

peer_connection.register_data_channel(servo_media_id, &channel);
Expand Down Expand Up @@ -158,6 +164,22 @@ impl RTCDataChannel {
DataChannelMessage::Binary(_) => {},
}
}

pub fn on_state_change(&self, state: DataChannelState) {
match state {
DataChannelState::Closing => {
let event = Event::new(
&self.global(),
atom!("closing"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable,
);
event.upcast::<Event>().fire(self.upcast());
},
_ => {},
};
*self.ready_state.borrow_mut() = state.into();
}
}

impl Drop for RTCDataChannel {
Expand Down Expand Up @@ -219,7 +241,13 @@ impl RTCDataChannelMethods for RTCDataChannel {
self.id
}

// fn ReadyState(&self) -> RTCDataChannelState;
fn ReadyState(&self) -> RTCDataChannelState {
*self.ready_state.borrow()
}

// XXX We need a way to know when the underlying data transport
// actually sends data from its queue to decrease buffered amount.

// fn BufferedAmount(&self) -> u32;
// fn BufferedAmountLowThreshold(&self) -> u32;
// fn SetBufferedAmountLowThreshold(&self, value: u32) -> ();
Expand Down Expand Up @@ -268,3 +296,16 @@ impl From<&RTCDataChannelInit> for DataChannelInit {
}
}
}

impl From<DataChannelState> for RTCDataChannelState {
fn from(state: DataChannelState) -> RTCDataChannelState {
match state {
DataChannelState::New |
DataChannelState::Connecting |
DataChannelState::__Unknown(_) => RTCDataChannelState::Connecting,
DataChannelState::Open => RTCDataChannelState::Open,
DataChannelState::Closing => RTCDataChannelState::Closing,
DataChannelState::Closed => RTCDataChannelState::Closed,
}
}
}
3 changes: 2 additions & 1 deletion components/script/dom/rtcpeerconnection.rs
Expand Up @@ -320,7 +320,8 @@ impl RTCPeerConnection {
DataChannelEvent::Close => channel.on_close(),
DataChannelEvent::Error(error) => channel.on_error(error),
DataChannelEvent::OnMessage(message) => channel.on_message(message),
_ => unreachable!(),
DataChannelEvent::StateChange(state) => channel.on_state_change(state),
DataChannelEvent::NewChannel => unreachable!(),
}
} else {
debug_assert!(false, "Got an event for an unregistered data channel");
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/webidls/RTCDataChannel.webidl
Expand Up @@ -13,7 +13,7 @@ interface RTCDataChannel : EventTarget {
readonly attribute USVString protocol;
readonly attribute boolean negotiated;
readonly attribute unsigned short? id;
//readonly attribute RTCDataChannelState readyState;
readonly attribute RTCDataChannelState readyState;
//readonly attribute unsigned long bufferedAmount;
//attribute unsigned long bufferedAmountLowThreshold;
attribute EventHandler onopen;
Expand Down

0 comments on commit 7db485a

Please sign in to comment.