Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 1 | /* |
| 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) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 34 | #include "RuntimeEnabledFeatures.h" |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 35 | #include "WebDocument.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 36 | #include "core/dom/Document.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 37 | #include "core/frame/ConsoleTypes.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 38 | #include "modules/websockets/MainThreadWebSocketChannel.h" |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 39 | #include "modules/websockets/NewWebSocketChannelImpl.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 40 | #include "modules/websockets/WebSocketChannel.h" |
Ben Murdoch | 0019e4e | 2013-07-18 11:57:54 +0100 | [diff] [blame] | 41 | #include "public/platform/WebArrayBuffer.h" |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 42 | #include "public/platform/WebString.h" |
| 43 | #include "public/platform/WebURL.h" |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 44 | #include "wtf/ArrayBuffer.h" |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 45 | |
| 46 | using namespace WebCore; |
| 47 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 48 | namespace blink { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 49 | |
| 50 | WebSocketImpl::WebSocketImpl(const WebDocument& document, WebSocketClient* client) |
| 51 | : m_client(client) |
| 52 | , m_binaryType(BinaryTypeBlob) |
| 53 | { |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 54 | RefPtr<Document> coreDocument = PassRefPtr<Document>(document); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 55 | if (RuntimeEnabledFeatures::experimentalWebSocketEnabled()) { |
| 56 | m_private = NewWebSocketChannelImpl::create(coreDocument.get(), this); |
| 57 | } else { |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 58 | m_private = MainThreadWebSocketChannel::create(coreDocument.get(), this); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 59 | } |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | WebSocketImpl::~WebSocketImpl() |
| 63 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 64 | m_private->disconnect(); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | WebSocket::BinaryType WebSocketImpl::binaryType() const |
| 68 | { |
| 69 | return m_binaryType; |
| 70 | } |
| 71 | |
| 72 | bool WebSocketImpl::setBinaryType(BinaryType binaryType) |
| 73 | { |
| 74 | if (binaryType > BinaryTypeArrayBuffer) |
| 75 | return false; |
| 76 | m_binaryType = binaryType; |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | void WebSocketImpl::connect(const WebURL& url, const WebString& protocol) |
| 81 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 82 | m_private->connect(url, protocol); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | WebString WebSocketImpl::subprotocol() |
| 86 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 87 | return m_private->subprotocol(); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | WebString WebSocketImpl::extensions() |
| 91 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 92 | return m_private->extensions(); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | bool WebSocketImpl::sendText(const WebString& message) |
| 96 | { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 97 | return m_private->send(message) == WebSocketChannel::SendSuccess; |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | bool WebSocketImpl::sendArrayBuffer(const WebArrayBuffer& webArrayBuffer) |
| 101 | { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 102 | return m_private->send(*PassRefPtr<ArrayBuffer>(webArrayBuffer), 0, webArrayBuffer.byteLength()) == WebSocketChannel::SendSuccess; |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | unsigned long WebSocketImpl::bufferedAmount() const |
| 106 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 107 | return m_private->bufferedAmount(); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void WebSocketImpl::close(int code, const WebString& reason) |
| 111 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 112 | m_private->close(code, reason); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void WebSocketImpl::fail(const WebString& reason) |
| 116 | { |
Torne (Richard Coles) | 81a5157 | 2013-05-13 16:52:28 +0100 | [diff] [blame] | 117 | m_private->fail(reason, ErrorMessageLevel); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void WebSocketImpl::disconnect() |
| 121 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 122 | m_private->disconnect(); |
| 123 | m_client = 0; |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void WebSocketImpl::didConnect() |
| 127 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 128 | m_client->didConnect(); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void WebSocketImpl::didReceiveMessage(const String& message) |
| 132 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 133 | m_client->didReceiveMessage(WebString(message)); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void WebSocketImpl::didReceiveBinaryData(PassOwnPtr<Vector<char> > binaryData) |
| 137 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 138 | 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) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void WebSocketImpl::didReceiveMessageError() |
| 149 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 150 | m_client->didReceiveMessageError(); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void WebSocketImpl::didUpdateBufferedAmount(unsigned long bufferedAmount) |
| 154 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 155 | m_client->didUpdateBufferedAmount(bufferedAmount); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void WebSocketImpl::didStartClosingHandshake() |
| 159 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 160 | m_client->didStartClosingHandshake(); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void WebSocketImpl::didClose(unsigned long bufferedAmount, ClosingHandshakeCompletionStatus status, unsigned short code, const String& reason) |
| 164 | { |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 165 | m_client->didClose(bufferedAmount, static_cast<WebSocketClient::ClosingHandshakeCompletionStatus>(status), code, WebString(reason)); |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 168 | } // namespace blink |