diff options
Diffstat (limited to 'Source/WebCore/Modules/mediastream/RTCDataChannel.cpp')
-rw-r--r-- | Source/WebCore/Modules/mediastream/RTCDataChannel.cpp | 190 |
1 files changed, 67 insertions, 123 deletions
diff --git a/Source/WebCore/Modules/mediastream/RTCDataChannel.cpp b/Source/WebCore/Modules/mediastream/RTCDataChannel.cpp index 4e876bb7e..76723e4ee 100644 --- a/Source/WebCore/Modules/mediastream/RTCDataChannel.cpp +++ b/Source/WebCore/Modules/mediastream/RTCDataChannel.cpp @@ -23,18 +23,16 @@ */ #include "config.h" - -#if ENABLE(MEDIA_STREAM) - #include "RTCDataChannel.h" +#if ENABLE(WEB_RTC) + #include "Blob.h" -#include "Dictionary.h" #include "Event.h" +#include "EventNames.h" #include "ExceptionCode.h" #include "MessageEvent.h" #include "RTCDataChannelHandler.h" -#include "RTCPeerConnectionHandler.h" #include "ScriptExecutionContext.h" #include <runtime/ArrayBuffer.h> #include <runtime/ArrayBufferView.h> @@ -54,87 +52,30 @@ static const AtomicString& arraybufferKeyword() return arraybuffer; } -PassRefPtr<RTCDataChannel> RTCDataChannel::create(ScriptExecutionContext* context, RTCPeerConnectionHandler* peerConnectionHandler, const String& label, const Dictionary& options, ExceptionCode& ec) -{ - RTCDataChannelInit initData; - options.get("ordered", initData.ordered); - options.get("negotiated", initData.negotiated); - options.get("id", initData.id); - options.get("maxRetransmits", initData.maxRetransmits); - options.get("maxRetransmitTime", initData.maxRetransmitTime); - options.get("protocol", initData.protocol); - - std::unique_ptr<RTCDataChannelHandler> handler = peerConnectionHandler->createDataChannel(label, initData); - if (!handler) { - ec = NOT_SUPPORTED_ERR; - return nullptr; - } - return adoptRef(new RTCDataChannel(context, std::move(handler))); -} - -PassRefPtr<RTCDataChannel> RTCDataChannel::create(ScriptExecutionContext* context, std::unique_ptr<RTCDataChannelHandler> handler) +Ref<RTCDataChannel> RTCDataChannel::create(ScriptExecutionContext& context, std::unique_ptr<RTCDataChannelHandler>&& handler, String&& label, RTCDataChannelInit&& options) { ASSERT(handler); - return adoptRef(new RTCDataChannel(context, std::move(handler))); -} - -RTCDataChannel::RTCDataChannel(ScriptExecutionContext* context, std::unique_ptr<RTCDataChannelHandler> handler) - : m_scriptExecutionContext(context) - , m_handler(std::move(handler)) - , m_stopped(false) - , m_readyState(ReadyStateConnecting) - , m_binaryType(BinaryTypeArrayBuffer) - , m_scheduledEventTimer(this, &RTCDataChannel::scheduledEventTimerFired) -{ - m_handler->setClient(this); -} - -RTCDataChannel::~RTCDataChannel() -{ -} - -String RTCDataChannel::label() const -{ - return m_handler->label(); -} - -bool RTCDataChannel::ordered() const -{ - return m_handler->ordered(); -} - -unsigned short RTCDataChannel::maxRetransmitTime() const -{ - return m_handler->maxRetransmitTime(); -} - -unsigned short RTCDataChannel::maxRetransmits() const -{ -return m_handler->maxRetransmits(); -} - -String RTCDataChannel::protocol() const -{ - return m_handler->protocol(); -} - -bool RTCDataChannel::negotiated() const -{ - return m_handler->negotiated(); + auto channel = adoptRef(*new RTCDataChannel(context, WTFMove(handler), WTFMove(label), WTFMove(options))); + channel->m_handler->setClient(&channel.get()); + return channel; } -unsigned short RTCDataChannel::id() const +RTCDataChannel::RTCDataChannel(ScriptExecutionContext& context, std::unique_ptr<RTCDataChannelHandler>&& handler, String&& label, RTCDataChannelInit&& options) + : m_scriptExecutionContext(&context) + , m_handler(WTFMove(handler)) + , m_scheduledEventTimer(*this, &RTCDataChannel::scheduledEventTimerFired) + , m_label(WTFMove(label)) + , m_options(WTFMove(options)) { - return m_handler->id(); } -AtomicString RTCDataChannel::readyState() const +const AtomicString& RTCDataChannel::readyState() const { static NeverDestroyed<AtomicString> connectingState("connecting", AtomicString::ConstructFromLiteral); static NeverDestroyed<AtomicString> openState("open", AtomicString::ConstructFromLiteral); static NeverDestroyed<AtomicString> closingState("closing", AtomicString::ConstructFromLiteral); static NeverDestroyed<AtomicString> closedState("closed", AtomicString::ConstructFromLiteral); - + switch (m_readyState) { case ReadyStateConnecting: return connectingState; @@ -150,17 +91,17 @@ AtomicString RTCDataChannel::readyState() const return emptyAtom; } -unsigned long RTCDataChannel::bufferedAmount() const +size_t RTCDataChannel::bufferedAmount() const { return m_handler->bufferedAmount(); } -AtomicString RTCDataChannel::binaryType() const +const AtomicString& RTCDataChannel::binaryType() const { switch (m_binaryType) { - case BinaryTypeBlob: + case BinaryType::Blob: return blobKeyword(); - case BinaryTypeArrayBuffer: + case BinaryType::ArrayBuffer: return arraybufferKeyword(); } @@ -168,60 +109,58 @@ AtomicString RTCDataChannel::binaryType() const return emptyAtom; } -void RTCDataChannel::setBinaryType(const AtomicString& binaryType, ExceptionCode& ec) +ExceptionOr<void> RTCDataChannel::setBinaryType(const AtomicString& binaryType) { if (binaryType == blobKeyword()) - ec = NOT_SUPPORTED_ERR; - else if (binaryType == arraybufferKeyword()) - m_binaryType = BinaryTypeArrayBuffer; - else - ec = TYPE_MISMATCH_ERR; + return Exception { NOT_SUPPORTED_ERR }; + if (binaryType == arraybufferKeyword()) { + m_binaryType = BinaryType::ArrayBuffer; + return { }; + } + return Exception { TYPE_MISMATCH_ERR }; } -void RTCDataChannel::send(const String& data, ExceptionCode& ec) +ExceptionOr<void> RTCDataChannel::send(const String& data) { - if (m_readyState != ReadyStateOpen) { - ec = INVALID_STATE_ERR; - return; - } + if (m_readyState != ReadyStateOpen) + return Exception { INVALID_STATE_ERR }; if (!m_handler->sendStringData(data)) { // FIXME: Decide what the right exception here is. - ec = SYNTAX_ERR; + return Exception { SYNTAX_ERR }; } + + return { }; } -void RTCDataChannel::send(PassRefPtr<ArrayBuffer> prpData, ExceptionCode& ec) +ExceptionOr<void> RTCDataChannel::send(ArrayBuffer& data) { - if (m_readyState != ReadyStateOpen) { - ec = INVALID_STATE_ERR; - return; - } - - RefPtr<ArrayBuffer> data = prpData; + if (m_readyState != ReadyStateOpen) + return Exception { INVALID_STATE_ERR }; - size_t dataLength = data->byteLength(); + size_t dataLength = data.byteLength(); if (!dataLength) - return; + return { }; - const char* dataPointer = static_cast<const char*>(data->data()); + const char* dataPointer = static_cast<const char*>(data.data()); if (!m_handler->sendRawData(dataPointer, dataLength)) { // FIXME: Decide what the right exception here is. - ec = SYNTAX_ERR; + return Exception { SYNTAX_ERR }; } + + return { }; } -void RTCDataChannel::send(PassRefPtr<ArrayBufferView> data, ExceptionCode& ec) +ExceptionOr<void> RTCDataChannel::send(ArrayBufferView& data) { - RefPtr<ArrayBuffer> arrayBuffer(data->buffer()); - send(arrayBuffer.release(), ec); + return send(*data.unsharedBuffer()); } -void RTCDataChannel::send(PassRefPtr<Blob>, ExceptionCode& ec) +ExceptionOr<void> RTCDataChannel::send(Blob&) { - // FIXME: implement - ec = NOT_SUPPORTED_ERR; + // FIXME: Implement. + return Exception { NOT_SUPPORTED_ERR }; } void RTCDataChannel::close() @@ -264,14 +203,13 @@ void RTCDataChannel::didReceiveRawData(const char* data, size_t dataLength) if (m_stopped) return; - if (m_binaryType == BinaryTypeBlob) { + if (m_binaryType == BinaryType::Blob) { // FIXME: Implement. return; } - if (m_binaryType == BinaryTypeArrayBuffer) { - RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(data, dataLength); - scheduleDispatchEvent(MessageEvent::create(buffer.release())); + if (m_binaryType == BinaryType::ArrayBuffer) { + scheduleDispatchEvent(MessageEvent::create(ArrayBuffer::create(data, dataLength))); return; } ASSERT_NOT_REACHED(); @@ -285,37 +223,43 @@ void RTCDataChannel::didDetectError() scheduleDispatchEvent(Event::create(eventNames().errorEvent, false, false)); } +void RTCDataChannel::bufferedAmountIsDecreasing() +{ + if (m_stopped) + return; + + if (bufferedAmount() <= m_bufferedAmountLowThreshold) + scheduleDispatchEvent(Event::create(eventNames().bufferedAmountLowThresholdEvent, false, false)); +} + void RTCDataChannel::stop() { m_stopped = true; m_readyState = ReadyStateClosed; - m_handler->setClient(0); - m_scriptExecutionContext = 0; + m_handler->setClient(nullptr); + m_scriptExecutionContext = nullptr; } -void RTCDataChannel::scheduleDispatchEvent(PassRefPtr<Event> event) +void RTCDataChannel::scheduleDispatchEvent(Ref<Event>&& event) { - m_scheduledEvents.append(event); + m_scheduledEvents.append(WTFMove(event)); if (!m_scheduledEventTimer.isActive()) m_scheduledEventTimer.startOneShot(0); } -void RTCDataChannel::scheduledEventTimerFired(Timer<RTCDataChannel>*) +void RTCDataChannel::scheduledEventTimerFired() { if (m_stopped) return; - Vector<RefPtr<Event>> events; + Vector<Ref<Event>> events; events.swap(m_scheduledEvents); - Vector<RefPtr<Event>>::iterator it = events.begin(); - for (; it != events.end(); ++it) - dispatchEvent((*it).release()); - - events.clear(); + for (auto& event : events) + dispatchEvent(event); } } // namespace WebCore -#endif // ENABLE(MEDIA_STREAM) +#endif // ENABLE(WEB_RTC) |