Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / mediastream / RTCDataChannel.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
23  */
24
25 #include "config.h"
26 #include "modules/mediastream/RTCDataChannel.h"
27
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"
37
38 namespace WebCore {
39
40 static void throwNotOpenException(ExceptionState& exceptionState)
41 {
42     exceptionState.throwDOMException(InvalidStateError, "RTCDataChannel.readyState is not 'open'");
43 }
44
45 static void throwCouldNotSendDataException(ExceptionState& exceptionState)
46 {
47     exceptionState.throwDOMException(NetworkError, "Could not send data");
48 }
49
50 static void throwNoBlobSupportException(ExceptionState& exceptionState)
51 {
52     exceptionState.throwDOMException(NotSupportedError, "Blob support not implemented yet");
53 }
54
55 PassRefPtr<RTCDataChannel> RTCDataChannel::create(ExecutionContext* context, PassOwnPtr<blink::WebRTCDataChannelHandler> handler)
56 {
57     ASSERT(handler);
58     return adoptRef(new RTCDataChannel(context, handler));
59 }
60
61 PassRefPtr<RTCDataChannel> RTCDataChannel::create(ExecutionContext* context, blink::WebRTCPeerConnectionHandler* peerConnectionHandler, const String& label, const blink::WebRTCDataChannelInit& init, ExceptionState& exceptionState)
62 {
63     OwnPtr<blink::WebRTCDataChannelHandler> handler = adoptPtr(peerConnectionHandler->createDataChannel(label, init));
64     if (!handler) {
65         exceptionState.throwDOMException(NotSupportedError, "RTCDataChannel is not supported");
66         return nullptr;
67     }
68     return adoptRef(new RTCDataChannel(context, handler.release()));
69 }
70
71 RTCDataChannel::RTCDataChannel(ExecutionContext* context, PassOwnPtr<blink::WebRTCDataChannelHandler> handler)
72     : m_executionContext(context)
73     , m_handler(handler)
74     , m_stopped(false)
75     , m_readyState(ReadyStateConnecting)
76     , m_binaryType(BinaryTypeArrayBuffer)
77     , m_scheduledEventTimer(this, &RTCDataChannel::scheduledEventTimerFired)
78 {
79     ScriptWrappable::init(this);
80     m_handler->setClient(this);
81 }
82
83 RTCDataChannel::~RTCDataChannel()
84 {
85 }
86
87 String RTCDataChannel::label() const
88 {
89     return m_handler->label();
90 }
91
92 bool RTCDataChannel::reliable() const
93 {
94     return m_handler->isReliable();
95 }
96
97 bool RTCDataChannel::ordered() const
98 {
99     return m_handler->ordered();
100 }
101
102 unsigned short RTCDataChannel::maxRetransmitTime() const
103 {
104     return m_handler->maxRetransmitTime();
105 }
106
107 unsigned short RTCDataChannel::maxRetransmits() const
108 {
109     return m_handler->maxRetransmits();
110 }
111
112 String RTCDataChannel::protocol() const
113 {
114     return m_handler->protocol();
115 }
116
117 bool RTCDataChannel::negotiated() const
118 {
119     return m_handler->negotiated();
120 }
121
122 unsigned short RTCDataChannel::id() const
123 {
124     return m_handler->id();
125 }
126
127 String RTCDataChannel::readyState() const
128 {
129     switch (m_readyState) {
130     case ReadyStateConnecting:
131         return "connecting";
132     case ReadyStateOpen:
133         return "open";
134     case ReadyStateClosing:
135         return "closing";
136     case ReadyStateClosed:
137         return "closed";
138     }
139
140     ASSERT_NOT_REACHED();
141     return String();
142 }
143
144 unsigned long RTCDataChannel::bufferedAmount() const
145 {
146     return m_handler->bufferedAmount();
147 }
148
149 String RTCDataChannel::binaryType() const
150 {
151     switch (m_binaryType) {
152     case BinaryTypeBlob:
153         return "blob";
154     case BinaryTypeArrayBuffer:
155         return "arraybuffer";
156     }
157     ASSERT_NOT_REACHED();
158     return String();
159 }
160
161 void RTCDataChannel::setBinaryType(const String& binaryType, ExceptionState& exceptionState)
162 {
163     if (binaryType == "blob")
164         throwNoBlobSupportException(exceptionState);
165     else if (binaryType == "arraybuffer")
166         m_binaryType = BinaryTypeArrayBuffer;
167     else
168         exceptionState.throwDOMException(TypeMismatchError, "Unknown binary type : " + binaryType);
169 }
170
171 void RTCDataChannel::send(const String& data, ExceptionState& exceptionState)
172 {
173     if (m_readyState != ReadyStateOpen) {
174         throwNotOpenException(exceptionState);
175         return;
176     }
177     if (!m_handler->sendStringData(data)) {
178         // FIXME: This should not throw an exception but instead forcefully close the data channel.
179         throwCouldNotSendDataException(exceptionState);
180     }
181 }
182
183 void RTCDataChannel::send(PassRefPtr<ArrayBuffer> prpData, ExceptionState& exceptionState)
184 {
185     if (m_readyState != ReadyStateOpen) {
186         throwNotOpenException(exceptionState);
187         return;
188     }
189
190     RefPtr<ArrayBuffer> data = prpData;
191
192     size_t dataLength = data->byteLength();
193     if (!dataLength)
194         return;
195
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);
199     }
200 }
201
202 void RTCDataChannel::send(PassRefPtr<ArrayBufferView> data, ExceptionState& exceptionState)
203 {
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);
207     }
208 }
209
210 void RTCDataChannel::send(PassRefPtrWillBeRawPtr<Blob> data, ExceptionState& exceptionState)
211 {
212     // FIXME: implement
213     throwNoBlobSupportException(exceptionState);
214 }
215
216 void RTCDataChannel::close()
217 {
218     if (m_stopped)
219         return;
220
221     m_handler->close();
222 }
223
224 void RTCDataChannel::didChangeReadyState(blink::WebRTCDataChannelHandlerClient::ReadyState newState)
225 {
226     if (m_stopped || m_readyState == ReadyStateClosed)
227         return;
228
229     m_readyState = newState;
230
231     switch (m_readyState) {
232     case ReadyStateOpen:
233         scheduleDispatchEvent(Event::create(EventTypeNames::open));
234         break;
235     case ReadyStateClosed:
236         scheduleDispatchEvent(Event::create(EventTypeNames::close));
237         break;
238     default:
239         break;
240     }
241 }
242
243 void RTCDataChannel::didReceiveStringData(const blink::WebString& text)
244 {
245     if (m_stopped)
246         return;
247
248     scheduleDispatchEvent(MessageEvent::create(text));
249 }
250
251 void RTCDataChannel::didReceiveRawData(const char* data, size_t dataLength)
252 {
253     if (m_stopped)
254         return;
255
256     if (m_binaryType == BinaryTypeBlob) {
257         // FIXME: Implement.
258         return;
259     }
260     if (m_binaryType == BinaryTypeArrayBuffer) {
261         RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(data, dataLength);
262         scheduleDispatchEvent(MessageEvent::create(buffer.release()));
263         return;
264     }
265     ASSERT_NOT_REACHED();
266 }
267
268 void RTCDataChannel::didDetectError()
269 {
270     if (m_stopped)
271         return;
272
273     scheduleDispatchEvent(Event::create(EventTypeNames::error));
274 }
275
276 const AtomicString& RTCDataChannel::interfaceName() const
277 {
278     return EventTargetNames::RTCDataChannel;
279 }
280
281 ExecutionContext* RTCDataChannel::executionContext() const
282 {
283     return m_executionContext;
284 }
285
286 void RTCDataChannel::stop()
287 {
288     m_stopped = true;
289     m_readyState = ReadyStateClosed;
290     m_handler->setClient(0);
291     m_executionContext = 0;
292 }
293
294 void RTCDataChannel::scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event> event)
295 {
296     m_scheduledEvents.append(event);
297
298     if (!m_scheduledEventTimer.isActive())
299         m_scheduledEventTimer.startOneShot(0, FROM_HERE);
300 }
301
302 void RTCDataChannel::scheduledEventTimerFired(Timer<RTCDataChannel>*)
303 {
304     if (m_stopped)
305         return;
306
307     WillBeHeapVector<RefPtrWillBeMember<Event> > events;
308     events.swap(m_scheduledEvents);
309
310     WillBeHeapVector<RefPtrWillBeMember<Event> >::iterator it = events.begin();
311     for (; it != events.end(); ++it)
312         dispatchEvent((*it).release());
313
314     events.clear();
315 }
316
317 } // namespace WebCore