2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "modules/mediastream/RTCDataChannel.h"
28 #include "bindings/core/v8/ExceptionState.h"
29 #include "core/dom/ExceptionCode.h"
30 #include "core/dom/ExecutionContext.h"
31 #include "core/events/MessageEvent.h"
32 #include "core/fileapi/Blob.h"
33 #include "modules/mediastream/RTCPeerConnection.h"
34 #include "public/platform/WebRTCPeerConnectionHandler.h"
35 #include "wtf/ArrayBuffer.h"
36 #include "wtf/ArrayBufferView.h"
40 static void throwNotOpenException(ExceptionState& exceptionState)
42 exceptionState.throwDOMException(InvalidStateError, "RTCDataChannel.readyState is not 'open'");
45 static void throwCouldNotSendDataException(ExceptionState& exceptionState)
47 exceptionState.throwDOMException(NetworkError, "Could not send data");
50 static void throwNoBlobSupportException(ExceptionState& exceptionState)
52 exceptionState.throwDOMException(NotSupportedError, "Blob support not implemented yet");
55 RTCDataChannel* RTCDataChannel::create(ExecutionContext* context, RTCPeerConnection* connection, PassOwnPtr<WebRTCDataChannelHandler> handler)
58 return adoptRefCountedGarbageCollectedWillBeNoop(new RTCDataChannel(context, connection, handler));
61 RTCDataChannel* RTCDataChannel::create(ExecutionContext* context, RTCPeerConnection* connection, WebRTCPeerConnectionHandler* peerConnectionHandler, const String& label, const WebRTCDataChannelInit& init, ExceptionState& exceptionState)
63 OwnPtr<WebRTCDataChannelHandler> handler = adoptPtr(peerConnectionHandler->createDataChannel(label, init));
65 exceptionState.throwDOMException(NotSupportedError, "RTCDataChannel is not supported");
68 return adoptRefCountedGarbageCollectedWillBeNoop(new RTCDataChannel(context, connection, handler.release()));
71 RTCDataChannel::RTCDataChannel(ExecutionContext* context, RTCPeerConnection* connection, PassOwnPtr<WebRTCDataChannelHandler> handler)
72 : m_executionContext(context)
75 , m_readyState(ReadyStateConnecting)
76 , m_binaryType(BinaryTypeArrayBuffer)
77 , m_scheduledEventTimer(this, &RTCDataChannel::scheduledEventTimerFired)
78 , m_connection(connection)
80 m_handler->setClient(this);
83 RTCDataChannel::~RTCDataChannel()
85 // If the peer connection and the data channel die in the same
86 // GC cycle stop has not been called and we need to notify the
87 // client that the channel is gone.
89 m_handler->setClient(0);
92 String RTCDataChannel::label() const
94 return m_handler->label();
97 bool RTCDataChannel::reliable() const
99 return m_handler->isReliable();
102 bool RTCDataChannel::ordered() const
104 return m_handler->ordered();
107 unsigned short RTCDataChannel::maxRetransmitTime() const
109 return m_handler->maxRetransmitTime();
112 unsigned short RTCDataChannel::maxRetransmits() const
114 return m_handler->maxRetransmits();
117 String RTCDataChannel::protocol() const
119 return m_handler->protocol();
122 bool RTCDataChannel::negotiated() const
124 return m_handler->negotiated();
127 unsigned short RTCDataChannel::id() const
129 return m_handler->id();
132 String RTCDataChannel::readyState() const
134 switch (m_readyState) {
135 case ReadyStateConnecting:
139 case ReadyStateClosing:
141 case ReadyStateClosed:
145 ASSERT_NOT_REACHED();
149 unsigned long RTCDataChannel::bufferedAmount() const
151 return m_handler->bufferedAmount();
154 String RTCDataChannel::binaryType() const
156 switch (m_binaryType) {
159 case BinaryTypeArrayBuffer:
160 return "arraybuffer";
162 ASSERT_NOT_REACHED();
166 void RTCDataChannel::setBinaryType(const String& binaryType, ExceptionState& exceptionState)
168 if (binaryType == "blob")
169 throwNoBlobSupportException(exceptionState);
170 else if (binaryType == "arraybuffer")
171 m_binaryType = BinaryTypeArrayBuffer;
173 exceptionState.throwDOMException(TypeMismatchError, "Unknown binary type : " + binaryType);
176 void RTCDataChannel::send(const String& data, ExceptionState& exceptionState)
178 if (m_readyState != ReadyStateOpen) {
179 throwNotOpenException(exceptionState);
182 if (!m_handler->sendStringData(data)) {
183 // FIXME: This should not throw an exception but instead forcefully close the data channel.
184 throwCouldNotSendDataException(exceptionState);
188 void RTCDataChannel::send(PassRefPtr<ArrayBuffer> prpData, ExceptionState& exceptionState)
190 if (m_readyState != ReadyStateOpen) {
191 throwNotOpenException(exceptionState);
195 RefPtr<ArrayBuffer> data = prpData;
197 size_t dataLength = data->byteLength();
201 if (!m_handler->sendRawData(static_cast<const char*>((data->data())), dataLength)) {
202 // FIXME: This should not throw an exception but instead forcefully close the data channel.
203 throwCouldNotSendDataException(exceptionState);
207 void RTCDataChannel::send(PassRefPtr<ArrayBufferView> data, ExceptionState& exceptionState)
209 if (!m_handler->sendRawData(static_cast<const char*>(data->baseAddress()), data->byteLength())) {
210 // FIXME: This should not throw an exception but instead forcefully close the data channel.
211 throwCouldNotSendDataException(exceptionState);
215 void RTCDataChannel::send(PassRefPtrWillBeRawPtr<Blob> data, ExceptionState& exceptionState)
218 throwNoBlobSupportException(exceptionState);
221 void RTCDataChannel::close()
229 void RTCDataChannel::didChangeReadyState(WebRTCDataChannelHandlerClient::ReadyState newState)
231 if (m_stopped || m_readyState == ReadyStateClosed)
234 m_readyState = newState;
236 switch (m_readyState) {
238 scheduleDispatchEvent(Event::create(EventTypeNames::open));
240 case ReadyStateClosed:
241 scheduleDispatchEvent(Event::create(EventTypeNames::close));
248 void RTCDataChannel::didReceiveStringData(const WebString& text)
253 scheduleDispatchEvent(MessageEvent::create(text));
256 void RTCDataChannel::didReceiveRawData(const char* data, size_t dataLength)
261 if (m_binaryType == BinaryTypeBlob) {
265 if (m_binaryType == BinaryTypeArrayBuffer) {
266 RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(data, dataLength);
267 scheduleDispatchEvent(MessageEvent::create(buffer.release()));
270 ASSERT_NOT_REACHED();
273 void RTCDataChannel::didDetectError()
278 scheduleDispatchEvent(Event::create(EventTypeNames::error));
281 const AtomicString& RTCDataChannel::interfaceName() const
283 return EventTargetNames::RTCDataChannel;
286 ExecutionContext* RTCDataChannel::executionContext() const
288 return m_executionContext;
291 void RTCDataChannel::stop()
294 m_readyState = ReadyStateClosed;
295 m_handler->setClient(0);
296 m_executionContext = 0;
299 void RTCDataChannel::scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event> event)
301 m_scheduledEvents.append(event);
303 if (!m_scheduledEventTimer.isActive())
304 m_scheduledEventTimer.startOneShot(0, FROM_HERE);
307 void RTCDataChannel::scheduledEventTimerFired(Timer<RTCDataChannel>*)
312 WillBeHeapVector<RefPtrWillBeMember<Event> > events;
313 events.swap(m_scheduledEvents);
315 WillBeHeapVector<RefPtrWillBeMember<Event> >::iterator it = events.begin();
316 for (; it != events.end(); ++it)
317 dispatchEvent((*it).release());
322 void RTCDataChannel::clearWeakMembers(Visitor* visitor)
324 if (visitor->isAlive(m_connection))
327 m_connection = nullptr;
330 void RTCDataChannel::trace(Visitor* visitor)
332 visitor->trace(m_scheduledEvents);
333 visitor->registerWeakMembers<RTCDataChannel, &RTCDataChannel::clearWeakMembers>(this);
334 EventTargetWithInlineData::trace(visitor);