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/v8/ExceptionState.h"
29 #include "core/dom/ExceptionCode.h"
30 #include "core/dom/ExecutionContext.h"
31 #include "core/events/Event.h"
32 #include "core/events/MessageEvent.h"
33 #include "core/fileapi/Blob.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 PassRefPtr<RTCDataChannel> RTCDataChannel::create(ExecutionContext* context, PassOwnPtr<blink::WebRTCDataChannelHandler> handler)
58 return adoptRef(new RTCDataChannel(context, handler));
61 PassRefPtr<RTCDataChannel> RTCDataChannel::create(ExecutionContext* context, blink::WebRTCPeerConnectionHandler* peerConnectionHandler, const String& label, const blink::WebRTCDataChannelInit& init, ExceptionState& exceptionState)
63 OwnPtr<blink::WebRTCDataChannelHandler> handler = adoptPtr(peerConnectionHandler->createDataChannel(label, init));
65 exceptionState.throwDOMException(NotSupportedError, "RTCDataChannel is not supported");
68 return adoptRef(new RTCDataChannel(context, handler.release()));
71 RTCDataChannel::RTCDataChannel(ExecutionContext* context, PassOwnPtr<blink::WebRTCDataChannelHandler> handler)
72 : m_executionContext(context)
75 , m_readyState(ReadyStateConnecting)
76 , m_binaryType(BinaryTypeArrayBuffer)
77 , m_scheduledEventTimer(this, &RTCDataChannel::scheduledEventTimerFired)
79 ScriptWrappable::init(this);
80 m_handler->setClient(this);
83 RTCDataChannel::~RTCDataChannel()
87 String RTCDataChannel::label() const
89 return m_handler->label();
92 bool RTCDataChannel::reliable() const
94 return m_handler->isReliable();
97 bool RTCDataChannel::ordered() const
99 return m_handler->ordered();
102 unsigned short RTCDataChannel::maxRetransmitTime() const
104 return m_handler->maxRetransmitTime();
107 unsigned short RTCDataChannel::maxRetransmits() const
109 return m_handler->maxRetransmits();
112 String RTCDataChannel::protocol() const
114 return m_handler->protocol();
117 bool RTCDataChannel::negotiated() const
119 return m_handler->negotiated();
122 unsigned short RTCDataChannel::id() const
124 return m_handler->id();
127 String RTCDataChannel::readyState() const
129 switch (m_readyState) {
130 case ReadyStateConnecting:
134 case ReadyStateClosing:
136 case ReadyStateClosed:
140 ASSERT_NOT_REACHED();
144 unsigned long RTCDataChannel::bufferedAmount() const
146 return m_handler->bufferedAmount();
149 String RTCDataChannel::binaryType() const
151 switch (m_binaryType) {
154 case BinaryTypeArrayBuffer:
155 return "arraybuffer";
157 ASSERT_NOT_REACHED();
161 void RTCDataChannel::setBinaryType(const String& binaryType, ExceptionState& exceptionState)
163 if (binaryType == "blob")
164 throwNoBlobSupportException(exceptionState);
165 else if (binaryType == "arraybuffer")
166 m_binaryType = BinaryTypeArrayBuffer;
168 exceptionState.throwDOMException(TypeMismatchError, "Unknown binary type : " + binaryType);
171 void RTCDataChannel::send(const String& data, ExceptionState& exceptionState)
173 if (m_readyState != ReadyStateOpen) {
174 throwNotOpenException(exceptionState);
177 if (!m_handler->sendStringData(data)) {
178 // FIXME: This should not throw an exception but instead forcefully close the data channel.
179 throwCouldNotSendDataException(exceptionState);
183 void RTCDataChannel::send(PassRefPtr<ArrayBuffer> prpData, ExceptionState& exceptionState)
185 if (m_readyState != ReadyStateOpen) {
186 throwNotOpenException(exceptionState);
190 RefPtr<ArrayBuffer> data = prpData;
192 size_t dataLength = data->byteLength();
196 if (!m_handler->sendRawData(static_cast<const char*>((data->data())), dataLength)) {
197 // FIXME: This should not throw an exception but instead forcefully close the data channel.
198 throwCouldNotSendDataException(exceptionState);
202 void RTCDataChannel::send(PassRefPtr<ArrayBufferView> data, ExceptionState& exceptionState)
204 if (!m_handler->sendRawData(static_cast<const char*>(data->baseAddress()), data->byteLength())) {
205 // FIXME: This should not throw an exception but instead forcefully close the data channel.
206 throwCouldNotSendDataException(exceptionState);
210 void RTCDataChannel::send(PassRefPtrWillBeRawPtr<Blob> data, ExceptionState& exceptionState)
213 throwNoBlobSupportException(exceptionState);
216 void RTCDataChannel::close()
224 void RTCDataChannel::didChangeReadyState(blink::WebRTCDataChannelHandlerClient::ReadyState newState)
226 if (m_stopped || m_readyState == ReadyStateClosed)
229 m_readyState = newState;
231 switch (m_readyState) {
233 scheduleDispatchEvent(Event::create(EventTypeNames::open));
235 case ReadyStateClosed:
236 scheduleDispatchEvent(Event::create(EventTypeNames::close));
243 void RTCDataChannel::didReceiveStringData(const blink::WebString& text)
248 scheduleDispatchEvent(MessageEvent::create(text));
251 void RTCDataChannel::didReceiveRawData(const char* data, size_t dataLength)
256 if (m_binaryType == BinaryTypeBlob) {
260 if (m_binaryType == BinaryTypeArrayBuffer) {
261 RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(data, dataLength);
262 scheduleDispatchEvent(MessageEvent::create(buffer.release()));
265 ASSERT_NOT_REACHED();
268 void RTCDataChannel::didDetectError()
273 scheduleDispatchEvent(Event::create(EventTypeNames::error));
276 const AtomicString& RTCDataChannel::interfaceName() const
278 return EventTargetNames::RTCDataChannel;
281 ExecutionContext* RTCDataChannel::executionContext() const
283 return m_executionContext;
286 void RTCDataChannel::stop()
289 m_readyState = ReadyStateClosed;
290 m_handler->setClient(0);
291 m_executionContext = 0;
294 void RTCDataChannel::scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event> event)
296 m_scheduledEvents.append(event);
298 if (!m_scheduledEventTimer.isActive())
299 m_scheduledEventTimer.startOneShot(0, FROM_HERE);
302 void RTCDataChannel::scheduledEventTimerFired(Timer<RTCDataChannel>*)
307 WillBeHeapVector<RefPtrWillBeMember<Event> > events;
308 events.swap(m_scheduledEvents);
310 WillBeHeapVector<RefPtrWillBeMember<Event> >::iterator it = events.begin();
311 for (; it != events.end(); ++it)
312 dispatchEvent((*it).release());
317 } // namespace WebCore