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 "web/WebSocketImpl.h"
34 #include "core/dom/Document.h"
35 #include "core/frame/ConsoleTypes.h"
36 #include "modules/websockets/DocumentWebSocketChannel.h"
37 #include "modules/websockets/WebSocketChannel.h"
38 #include "public/platform/WebArrayBuffer.h"
39 #include "public/platform/WebString.h"
40 #include "public/platform/WebURL.h"
41 #include "public/web/WebDocument.h"
42 #include "web/WebSocketChannelClientProxy.h"
43 #include "wtf/ArrayBuffer.h"
44 #include "wtf/text/CString.h"
45 #include "wtf/text/WTFString.h"
49 WebSocketImpl::WebSocketImpl(const WebDocument& document, WebSocketClient* client)
51 , m_channelProxy(WebSocketChannelClientProxy::create(this))
52 , m_binaryType(BinaryTypeBlob)
53 , m_isClosingOrClosed(false)
55 , m_bufferedAmountAfterClose(0)
57 RefPtrWillBeRawPtr<Document> coreDocument = PassRefPtrWillBeRawPtr<Document>(document);
58 m_private = DocumentWebSocketChannel::create(coreDocument.get(), m_channelProxy.get());
61 WebSocketImpl::~WebSocketImpl()
63 m_private->disconnect();
66 WebSocket::BinaryType WebSocketImpl::binaryType() const
71 bool WebSocketImpl::setBinaryType(BinaryType binaryType)
73 if (binaryType > BinaryTypeArrayBuffer)
75 m_binaryType = binaryType;
79 void WebSocketImpl::connect(const WebURL& url, const WebString& protocol)
81 m_private->connect(url, protocol);
84 WebString WebSocketImpl::subprotocol()
89 WebString WebSocketImpl::extensions()
94 bool WebSocketImpl::sendText(const WebString& message)
96 size_t size = message.utf8().length();
97 m_bufferedAmount += size;
98 if (m_isClosingOrClosed)
99 m_bufferedAmountAfterClose += size;
101 // FIXME: Deprecate this call.
102 m_client->didUpdateBufferedAmount(m_bufferedAmount);
104 if (m_isClosingOrClosed)
107 m_private->send(message);
111 bool WebSocketImpl::sendArrayBuffer(const WebArrayBuffer& webArrayBuffer)
113 size_t size = webArrayBuffer.byteLength();
114 m_bufferedAmount += size;
115 if (m_isClosingOrClosed)
116 m_bufferedAmountAfterClose += size;
118 // FIXME: Deprecate this call.
119 m_client->didUpdateBufferedAmount(m_bufferedAmount);
121 if (m_isClosingOrClosed)
124 m_private->send(*PassRefPtr<ArrayBuffer>(webArrayBuffer), 0, webArrayBuffer.byteLength());
128 unsigned long WebSocketImpl::bufferedAmount() const
130 return m_bufferedAmount;
133 void WebSocketImpl::close(int code, const WebString& reason)
135 m_isClosingOrClosed = true;
136 m_private->close(code, reason);
139 void WebSocketImpl::fail(const WebString& reason)
141 m_private->fail(reason, ErrorMessageLevel, String(), 0);
144 void WebSocketImpl::disconnect()
146 m_private->disconnect();
150 void WebSocketImpl::didConnect(const String& subprotocol, const String& extensions)
152 m_client->didConnect(subprotocol, extensions);
154 // FIXME: Deprecate these statements.
155 m_subprotocol = subprotocol;
156 m_extensions = extensions;
157 m_client->didConnect();
160 void WebSocketImpl::didReceiveTextMessage(const String& payload)
162 m_client->didReceiveMessage(WebString(payload));
165 void WebSocketImpl::didReceiveBinaryMessage(PassOwnPtr<Vector<char> > payload)
167 switch (m_binaryType) {
169 // FIXME: Handle Blob after supporting WebBlob.
171 case BinaryTypeArrayBuffer:
172 m_client->didReceiveArrayBuffer(WebArrayBuffer(ArrayBuffer::create(payload->data(), payload->size())));
177 void WebSocketImpl::didError()
179 m_client->didReceiveMessageError();
182 void WebSocketImpl::didConsumeBufferedAmount(unsigned long consumed)
184 m_client->didConsumeBufferedAmount(consumed);
186 // FIXME: Deprecate the following statements.
187 m_bufferedAmount -= consumed;
188 m_client->didUpdateBufferedAmount(m_bufferedAmount);
191 void WebSocketImpl::didStartClosingHandshake()
193 m_client->didStartClosingHandshake();
196 void WebSocketImpl::didClose(WebSocketChannelClient::ClosingHandshakeCompletionStatus status, unsigned short code, const String& reason)
198 m_isClosingOrClosed = true;
199 m_client->didClose(static_cast<WebSocketClient::ClosingHandshakeCompletionStatus>(status), code, WebString(reason));
201 // FIXME: Deprecate this call.
202 m_client->didClose(m_bufferedAmount - m_bufferedAmountAfterClose, static_cast<WebSocketClient::ClosingHandshakeCompletionStatus>(status), code, WebString(reason));