2 * Copyright (C) 2011, 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "WebSocketImpl.h"
34 #include "RuntimeEnabledFeatures.h"
35 #include "WebDocument.h"
36 #include "core/dom/Document.h"
37 #include "core/frame/ConsoleTypes.h"
38 #include "modules/websockets/MainThreadWebSocketChannel.h"
39 #include "modules/websockets/NewWebSocketChannelImpl.h"
40 #include "modules/websockets/WebSocketChannel.h"
41 #include "public/platform/WebArrayBuffer.h"
42 #include "public/platform/WebString.h"
43 #include "public/platform/WebURL.h"
44 #include "wtf/ArrayBuffer.h"
46 using namespace WebCore;
50 WebSocketImpl::WebSocketImpl(const WebDocument& document, WebSocketClient* client)
52 , m_binaryType(BinaryTypeBlob)
54 RefPtr<Document> coreDocument = PassRefPtr<Document>(document);
55 if (RuntimeEnabledFeatures::experimentalWebSocketEnabled()) {
56 m_private = NewWebSocketChannelImpl::create(coreDocument.get(), this);
58 m_private = MainThreadWebSocketChannel::create(coreDocument.get(), this);
62 WebSocketImpl::~WebSocketImpl()
64 m_private->disconnect();
67 WebSocket::BinaryType WebSocketImpl::binaryType() const
72 bool WebSocketImpl::setBinaryType(BinaryType binaryType)
74 if (binaryType > BinaryTypeArrayBuffer)
76 m_binaryType = binaryType;
80 void WebSocketImpl::connect(const WebURL& url, const WebString& protocol)
82 m_private->connect(url, protocol);
85 WebString WebSocketImpl::subprotocol()
87 return m_private->subprotocol();
90 WebString WebSocketImpl::extensions()
92 return m_private->extensions();
95 bool WebSocketImpl::sendText(const WebString& message)
97 return m_private->send(message) == WebSocketChannel::SendSuccess;
100 bool WebSocketImpl::sendArrayBuffer(const WebArrayBuffer& webArrayBuffer)
102 return m_private->send(*PassRefPtr<ArrayBuffer>(webArrayBuffer), 0, webArrayBuffer.byteLength()) == WebSocketChannel::SendSuccess;
105 unsigned long WebSocketImpl::bufferedAmount() const
107 return m_private->bufferedAmount();
110 void WebSocketImpl::close(int code, const WebString& reason)
112 m_private->close(code, reason);
115 void WebSocketImpl::fail(const WebString& reason)
117 m_private->fail(reason, ErrorMessageLevel);
120 void WebSocketImpl::disconnect()
122 m_private->disconnect();
126 void WebSocketImpl::didConnect()
128 m_client->didConnect();
131 void WebSocketImpl::didReceiveMessage(const String& message)
133 m_client->didReceiveMessage(WebString(message));
136 void WebSocketImpl::didReceiveBinaryData(PassOwnPtr<Vector<char> > binaryData)
138 switch (m_binaryType) {
140 // FIXME: Handle Blob after supporting WebBlob.
142 case BinaryTypeArrayBuffer:
143 m_client->didReceiveArrayBuffer(WebArrayBuffer(ArrayBuffer::create(binaryData->data(), binaryData->size())));
148 void WebSocketImpl::didReceiveMessageError()
150 m_client->didReceiveMessageError();
153 void WebSocketImpl::didUpdateBufferedAmount(unsigned long bufferedAmount)
155 m_client->didUpdateBufferedAmount(bufferedAmount);
158 void WebSocketImpl::didStartClosingHandshake()
160 m_client->didStartClosingHandshake();
163 void WebSocketImpl::didClose(unsigned long bufferedAmount, ClosingHandshakeCompletionStatus status, unsigned short code, const String& reason)
165 m_client->didClose(bufferedAmount, static_cast<WebSocketClient::ClosingHandshakeCompletionStatus>(status), code, WebString(reason));