From 7db485aeb22ea9dffa122c5ab3ff1ccc4659ece5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Jim=C3=A9nez=20Moreno?= Date: Thu, 11 Jun 2020 12:07:55 +0200 Subject: [PATCH] RTCDataChannel.readyState getter --- components/atoms/static_atoms.txt | 1 + components/script/dom/rtcdatachannel.rs | 45 ++++++++++++++++++- components/script/dom/rtcpeerconnection.rs | 3 +- .../script/dom/webidls/RTCDataChannel.webidl | 2 +- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/components/atoms/static_atoms.txt b/components/atoms/static_atoms.txt index b546092a4ac..a21efef4ff4 100644 --- a/components/atoms/static_atoms.txt +++ b/components/atoms/static_atoms.txt @@ -16,6 +16,7 @@ characteristicvaluechanged checkbox click close +closing color complete compositionend diff --git a/components/script/dom/rtcdatachannel.rs b/components/script/dom/rtcdatachannel.rs index 4b5aa8eeed2..75d8d48ccd8 100644 --- a/components/script/dom/rtcdatachannel.rs +++ b/components/script/dom/rtcdatachannel.rs @@ -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}; @@ -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 { @@ -35,6 +39,7 @@ pub struct RTCDataChannel { protocol: USVString, negotiated: bool, id: Option, + ready_state: DomRefCell, } impl RTCDataChannel { @@ -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); @@ -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::().fire(self.upcast()); + }, + _ => {}, + }; + *self.ready_state.borrow_mut() = state.into(); + } } impl Drop for RTCDataChannel { @@ -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) -> (); @@ -268,3 +296,16 @@ impl From<&RTCDataChannelInit> for DataChannelInit { } } } + +impl From 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, + } + } +} diff --git a/components/script/dom/rtcpeerconnection.rs b/components/script/dom/rtcpeerconnection.rs index 1ec1cc4e337..7ffbe5c72a7 100644 --- a/components/script/dom/rtcpeerconnection.rs +++ b/components/script/dom/rtcpeerconnection.rs @@ -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"); diff --git a/components/script/dom/webidls/RTCDataChannel.webidl b/components/script/dom/webidls/RTCDataChannel.webidl index 910353dbbd3..8de4986750f 100644 --- a/components/script/dom/webidls/RTCDataChannel.webidl +++ b/components/script/dom/webidls/RTCDataChannel.webidl @@ -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;