blob: 92a45997283a99647db88a5eb7769facca232ce1 [file] [log] [blame]
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +00001/*
2 * Copyright (C) 2011, 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 are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31#include "config.h"
32#include "WebSocketImpl.h"
33
Torne (Richard Coles)09380292014-02-21 12:17:33 +000034#include "RuntimeEnabledFeatures.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000035#include "WebDocument.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010036#include "core/dom/Document.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010037#include "core/frame/ConsoleTypes.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010038#include "modules/websockets/MainThreadWebSocketChannel.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000039#include "modules/websockets/NewWebSocketChannelImpl.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010040#include "modules/websockets/WebSocketChannel.h"
Ben Murdoch0019e4e2013-07-18 11:57:54 +010041#include "public/platform/WebArrayBuffer.h"
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010042#include "public/platform/WebString.h"
43#include "public/platform/WebURL.h"
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010044#include "wtf/ArrayBuffer.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000045
46using namespace WebCore;
47
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000048namespace blink {
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000049
50WebSocketImpl::WebSocketImpl(const WebDocument& document, WebSocketClient* client)
51 : m_client(client)
52 , m_binaryType(BinaryTypeBlob)
53{
Torne (Richard Coles)e5249552013-05-15 11:35:13 +010054 RefPtr<Document> coreDocument = PassRefPtr<Document>(document);
Torne (Richard Coles)09380292014-02-21 12:17:33 +000055 if (RuntimeEnabledFeatures::experimentalWebSocketEnabled()) {
56 m_private = NewWebSocketChannelImpl::create(coreDocument.get(), this);
57 } else {
Torne (Richard Coles)e5249552013-05-15 11:35:13 +010058 m_private = MainThreadWebSocketChannel::create(coreDocument.get(), this);
Torne (Richard Coles)09380292014-02-21 12:17:33 +000059 }
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000060}
61
62WebSocketImpl::~WebSocketImpl()
63{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000064 m_private->disconnect();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000065}
66
67WebSocket::BinaryType WebSocketImpl::binaryType() const
68{
69 return m_binaryType;
70}
71
72bool WebSocketImpl::setBinaryType(BinaryType binaryType)
73{
74 if (binaryType > BinaryTypeArrayBuffer)
75 return false;
76 m_binaryType = binaryType;
77 return true;
78}
79
80void WebSocketImpl::connect(const WebURL& url, const WebString& protocol)
81{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000082 m_private->connect(url, protocol);
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000083}
84
85WebString WebSocketImpl::subprotocol()
86{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000087 return m_private->subprotocol();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000088}
89
90WebString WebSocketImpl::extensions()
91{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000092 return m_private->extensions();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000093}
94
95bool WebSocketImpl::sendText(const WebString& message)
96{
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010097 return m_private->send(message) == WebSocketChannel::SendSuccess;
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000098}
99
100bool WebSocketImpl::sendArrayBuffer(const WebArrayBuffer& webArrayBuffer)
101{
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100102 return m_private->send(*PassRefPtr<ArrayBuffer>(webArrayBuffer), 0, webArrayBuffer.byteLength()) == WebSocketChannel::SendSuccess;
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000103}
104
105unsigned long WebSocketImpl::bufferedAmount() const
106{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000107 return m_private->bufferedAmount();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000108}
109
110void WebSocketImpl::close(int code, const WebString& reason)
111{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000112 m_private->close(code, reason);
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000113}
114
115void WebSocketImpl::fail(const WebString& reason)
116{
Torne (Richard Coles)81a51572013-05-13 16:52:28 +0100117 m_private->fail(reason, ErrorMessageLevel);
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000118}
119
120void WebSocketImpl::disconnect()
121{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000122 m_private->disconnect();
123 m_client = 0;
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000124}
125
126void WebSocketImpl::didConnect()
127{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000128 m_client->didConnect();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000129}
130
131void WebSocketImpl::didReceiveMessage(const String& message)
132{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000133 m_client->didReceiveMessage(WebString(message));
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000134}
135
136void WebSocketImpl::didReceiveBinaryData(PassOwnPtr<Vector<char> > binaryData)
137{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000138 switch (m_binaryType) {
139 case BinaryTypeBlob:
140 // FIXME: Handle Blob after supporting WebBlob.
141 break;
142 case BinaryTypeArrayBuffer:
143 m_client->didReceiveArrayBuffer(WebArrayBuffer(ArrayBuffer::create(binaryData->data(), binaryData->size())));
144 break;
145 }
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000146}
147
148void WebSocketImpl::didReceiveMessageError()
149{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000150 m_client->didReceiveMessageError();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000151}
152
153void WebSocketImpl::didUpdateBufferedAmount(unsigned long bufferedAmount)
154{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000155 m_client->didUpdateBufferedAmount(bufferedAmount);
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000156}
157
158void WebSocketImpl::didStartClosingHandshake()
159{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000160 m_client->didStartClosingHandshake();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000161}
162
163void WebSocketImpl::didClose(unsigned long bufferedAmount, ClosingHandshakeCompletionStatus status, unsigned short code, const String& reason)
164{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000165 m_client->didClose(bufferedAmount, static_cast<WebSocketClient::ClosingHandshakeCompletionStatus>(status), code, WebString(reason));
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000166}
167
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000168} // namespace blink