| OLD | NEW |
| (Empty) |
| 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 "web/WebSocketImpl.h" | |
| 32 | |
| 33 #include "core/dom/DOMArrayBuffer.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/WebString.h" | |
| 39 #include "public/platform/WebURL.h" | |
| 40 #include "public/web/WebArrayBuffer.h" | |
| 41 #include "public/web/WebDocument.h" | |
| 42 #include "web/WebSocketChannelClientProxy.h" | |
| 43 #include "wtf/text/CString.h" | |
| 44 #include "wtf/text/WTFString.h" | |
| 45 | |
| 46 namespace blink { | |
| 47 | |
| 48 WebSocketImpl::WebSocketImpl(const WebDocument& document, WebSocketClient* clien
t) | |
| 49 : m_client(client) | |
| 50 , m_channelProxy(WebSocketChannelClientProxy::create(this)) | |
| 51 , m_binaryType(BinaryTypeBlob) | |
| 52 , m_isClosingOrClosed(false) | |
| 53 , m_bufferedAmount(0) | |
| 54 , m_bufferedAmountAfterClose(0) | |
| 55 { | |
| 56 RefPtrWillBeRawPtr<Document> coreDocument = PassRefPtrWillBeRawPtr<Document>
(document); | |
| 57 m_private = DocumentWebSocketChannel::create(coreDocument.get(), m_channelPr
oxy.get()); | |
| 58 } | |
| 59 | |
| 60 WebSocketImpl::~WebSocketImpl() | |
| 61 { | |
| 62 m_private->disconnect(); | |
| 63 } | |
| 64 | |
| 65 WebSocket::BinaryType WebSocketImpl::binaryType() const | |
| 66 { | |
| 67 return m_binaryType; | |
| 68 } | |
| 69 | |
| 70 bool WebSocketImpl::setBinaryType(BinaryType binaryType) | |
| 71 { | |
| 72 if (binaryType > BinaryTypeArrayBuffer) | |
| 73 return false; | |
| 74 m_binaryType = binaryType; | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 78 void WebSocketImpl::connect(const WebURL& url, const WebString& protocol) | |
| 79 { | |
| 80 m_private->connect(url, protocol); | |
| 81 } | |
| 82 | |
| 83 WebString WebSocketImpl::subprotocol() | |
| 84 { | |
| 85 return m_subprotocol; | |
| 86 } | |
| 87 | |
| 88 WebString WebSocketImpl::extensions() | |
| 89 { | |
| 90 return m_extensions; | |
| 91 } | |
| 92 | |
| 93 bool WebSocketImpl::sendText(const WebString& message) | |
| 94 { | |
| 95 String coreMessage = message; | |
| 96 CString encodedMessage = coreMessage.utf8(); | |
| 97 size_t size = encodedMessage.length(); | |
| 98 m_bufferedAmount += size; | |
| 99 if (m_isClosingOrClosed) | |
| 100 m_bufferedAmountAfterClose += size; | |
| 101 | |
| 102 // FIXME: Deprecate this call. | |
| 103 m_client->didUpdateBufferedAmount(m_bufferedAmount); | |
| 104 | |
| 105 if (m_isClosingOrClosed) | |
| 106 return true; | |
| 107 | |
| 108 m_private->send(encodedMessage); | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 bool WebSocketImpl::sendArrayBuffer(const WebArrayBuffer& webArrayBuffer) | |
| 113 { | |
| 114 size_t size = webArrayBuffer.byteLength(); | |
| 115 m_bufferedAmount += size; | |
| 116 if (m_isClosingOrClosed) | |
| 117 m_bufferedAmountAfterClose += size; | |
| 118 | |
| 119 // FIXME: Deprecate this call. | |
| 120 m_client->didUpdateBufferedAmount(m_bufferedAmount); | |
| 121 | |
| 122 if (m_isClosingOrClosed) | |
| 123 return true; | |
| 124 | |
| 125 RefPtr<DOMArrayBuffer> arrayBuffer = PassRefPtr<DOMArrayBuffer>(webArrayBuff
er); | |
| 126 m_private->send(*arrayBuffer, 0, arrayBuffer->byteLength()); | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 unsigned long WebSocketImpl::bufferedAmount() const | |
| 131 { | |
| 132 return m_bufferedAmount; | |
| 133 } | |
| 134 | |
| 135 void WebSocketImpl::close(int code, const WebString& reason) | |
| 136 { | |
| 137 m_isClosingOrClosed = true; | |
| 138 m_private->close(code, reason); | |
| 139 } | |
| 140 | |
| 141 void WebSocketImpl::fail(const WebString& reason) | |
| 142 { | |
| 143 m_private->fail(reason, ErrorMessageLevel, String(), 0); | |
| 144 } | |
| 145 | |
| 146 void WebSocketImpl::disconnect() | |
| 147 { | |
| 148 m_private->disconnect(); | |
| 149 m_client = nullptr; | |
| 150 } | |
| 151 | |
| 152 void WebSocketImpl::didConnect(const String& subprotocol, const String& extensio
ns) | |
| 153 { | |
| 154 m_client->didConnect(subprotocol, extensions); | |
| 155 | |
| 156 // FIXME: Deprecate these statements. | |
| 157 m_subprotocol = subprotocol; | |
| 158 m_extensions = extensions; | |
| 159 m_client->didConnect(); | |
| 160 } | |
| 161 | |
| 162 void WebSocketImpl::didReceiveTextMessage(const String& payload) | |
| 163 { | |
| 164 m_client->didReceiveMessage(WebString(payload)); | |
| 165 } | |
| 166 | |
| 167 void WebSocketImpl::didReceiveBinaryMessage(PassOwnPtr<Vector<char>> payload) | |
| 168 { | |
| 169 switch (m_binaryType) { | |
| 170 case BinaryTypeBlob: | |
| 171 // FIXME: Handle Blob after supporting WebBlob. | |
| 172 break; | |
| 173 case BinaryTypeArrayBuffer: | |
| 174 m_client->didReceiveArrayBuffer(WebArrayBuffer(DOMArrayBuffer::create(pa
yload->data(), payload->size()))); | |
| 175 break; | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 void WebSocketImpl::didError() | |
| 180 { | |
| 181 m_client->didReceiveMessageError(); | |
| 182 } | |
| 183 | |
| 184 void WebSocketImpl::didConsumeBufferedAmount(unsigned long consumed) | |
| 185 { | |
| 186 m_client->didConsumeBufferedAmount(consumed); | |
| 187 | |
| 188 // FIXME: Deprecate the following statements. | |
| 189 m_bufferedAmount -= consumed; | |
| 190 m_client->didUpdateBufferedAmount(m_bufferedAmount); | |
| 191 } | |
| 192 | |
| 193 void WebSocketImpl::didStartClosingHandshake() | |
| 194 { | |
| 195 m_client->didStartClosingHandshake(); | |
| 196 } | |
| 197 | |
| 198 void WebSocketImpl::didClose(WebSocketChannelClient::ClosingHandshakeCompletionS
tatus status, unsigned short code, const String& reason) | |
| 199 { | |
| 200 m_isClosingOrClosed = true; | |
| 201 m_client->didClose(static_cast<WebSocketClient::ClosingHandshakeCompletionSt
atus>(status), code, WebString(reason)); | |
| 202 | |
| 203 // FIXME: Deprecate this call. | |
| 204 m_client->didClose(m_bufferedAmount - m_bufferedAmountAfterClose, static_cas
t<WebSocketClient::ClosingHandshakeCompletionStatus>(status), code, WebString(re
ason)); | |
| 205 } | |
| 206 | |
| 207 } // namespace blink | |
| OLD | NEW |