Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / WebSocketImpl.cpp
blob9f453f13a1310e4b483d5e26c27698bcec77ed41
1 /*
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
6 * met:
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.
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.
31 #include "config.h"
32 #include "web/WebSocketImpl.h"
34 #include "core/dom/DOMArrayBuffer.h"
35 #include "core/dom/Document.h"
36 #include "core/frame/ConsoleTypes.h"
37 #include "modules/websockets/DocumentWebSocketChannel.h"
38 #include "modules/websockets/WebSocketChannel.h"
39 #include "public/platform/WebString.h"
40 #include "public/platform/WebURL.h"
41 #include "public/web/WebArrayBuffer.h"
42 #include "public/web/WebDocument.h"
43 #include "web/WebSocketChannelClientProxy.h"
44 #include "wtf/text/CString.h"
45 #include "wtf/text/WTFString.h"
47 namespace blink {
49 WebSocketImpl::WebSocketImpl(const WebDocument& document, WebSocketClient* client)
50 : m_client(client)
51 , m_channelProxy(WebSocketChannelClientProxy::create(this))
52 , m_binaryType(BinaryTypeBlob)
53 , m_isClosingOrClosed(false)
54 , m_bufferedAmount(0)
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
68 return m_binaryType;
71 bool WebSocketImpl::setBinaryType(BinaryType binaryType)
73 if (binaryType > BinaryTypeArrayBuffer)
74 return false;
75 m_binaryType = binaryType;
76 return true;
79 void WebSocketImpl::connect(const WebURL& url, const WebString& protocol)
81 m_private->connect(url, protocol);
84 WebString WebSocketImpl::subprotocol()
86 return m_subprotocol;
89 WebString WebSocketImpl::extensions()
91 return m_extensions;
94 bool WebSocketImpl::sendText(const WebString& message)
96 String coreMessage = message;
97 CString encodedMessage = coreMessage.utf8();
98 size_t size = encodedMessage.length();
99 m_bufferedAmount += size;
100 if (m_isClosingOrClosed)
101 m_bufferedAmountAfterClose += size;
103 // FIXME: Deprecate this call.
104 m_client->didUpdateBufferedAmount(m_bufferedAmount);
106 if (m_isClosingOrClosed)
107 return true;
109 m_private->send(encodedMessage);
110 return true;
113 bool WebSocketImpl::sendArrayBuffer(const WebArrayBuffer& webArrayBuffer)
115 size_t size = webArrayBuffer.byteLength();
116 m_bufferedAmount += size;
117 if (m_isClosingOrClosed)
118 m_bufferedAmountAfterClose += size;
120 // FIXME: Deprecate this call.
121 m_client->didUpdateBufferedAmount(m_bufferedAmount);
123 if (m_isClosingOrClosed)
124 return true;
126 RefPtr<DOMArrayBuffer> arrayBuffer = PassRefPtr<DOMArrayBuffer>(webArrayBuffer);
127 m_private->send(*arrayBuffer, 0, arrayBuffer->byteLength());
128 return true;
131 unsigned long WebSocketImpl::bufferedAmount() const
133 return m_bufferedAmount;
136 void WebSocketImpl::close(int code, const WebString& reason)
138 m_isClosingOrClosed = true;
139 m_private->close(code, reason);
142 void WebSocketImpl::fail(const WebString& reason)
144 m_private->fail(reason, ErrorMessageLevel, String(), 0);
147 void WebSocketImpl::disconnect()
149 m_private->disconnect();
150 m_client = nullptr;
153 void WebSocketImpl::didConnect(const String& subprotocol, const String& extensions)
155 m_client->didConnect(subprotocol, extensions);
157 // FIXME: Deprecate these statements.
158 m_subprotocol = subprotocol;
159 m_extensions = extensions;
160 m_client->didConnect();
163 void WebSocketImpl::didReceiveTextMessage(const String& payload)
165 m_client->didReceiveMessage(WebString(payload));
168 void WebSocketImpl::didReceiveBinaryMessage(PassOwnPtr<Vector<char>> payload)
170 switch (m_binaryType) {
171 case BinaryTypeBlob:
172 // FIXME: Handle Blob after supporting WebBlob.
173 break;
174 case BinaryTypeArrayBuffer:
175 m_client->didReceiveArrayBuffer(WebArrayBuffer(DOMArrayBuffer::create(payload->data(), payload->size())));
176 break;
180 void WebSocketImpl::didError()
182 m_client->didReceiveMessageError();
185 void WebSocketImpl::didConsumeBufferedAmount(unsigned long consumed)
187 m_client->didConsumeBufferedAmount(consumed);
189 // FIXME: Deprecate the following statements.
190 m_bufferedAmount -= consumed;
191 m_client->didUpdateBufferedAmount(m_bufferedAmount);
194 void WebSocketImpl::didStartClosingHandshake()
196 m_client->didStartClosingHandshake();
199 void WebSocketImpl::didClose(WebSocketChannelClient::ClosingHandshakeCompletionStatus status, unsigned short code, const String& reason)
201 m_isClosingOrClosed = true;
202 m_client->didClose(static_cast<WebSocketClient::ClosingHandshakeCompletionStatus>(status), code, WebString(reason));
204 // FIXME: Deprecate this call.
205 m_client->didClose(m_bufferedAmount - m_bufferedAmountAfterClose, static_cast<WebSocketClient::ClosingHandshakeCompletionStatus>(status), code, WebString(reason));
208 } // namespace blink