| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "web/WebSocketImpl.h" | 31 #include "web/WebPepperSocketImpl.h" |
| 32 | 32 |
| 33 #include "core/dom/DOMArrayBuffer.h" | 33 #include "core/dom/DOMArrayBuffer.h" |
| 34 #include "core/dom/Document.h" | 34 #include "core/dom/Document.h" |
| 35 #include "core/frame/ConsoleTypes.h" | 35 #include "core/frame/ConsoleTypes.h" |
| 36 #include "modules/websockets/DocumentWebSocketChannel.h" | 36 #include "modules/websockets/DocumentWebSocketChannel.h" |
| 37 #include "modules/websockets/WebSocketChannel.h" | 37 #include "modules/websockets/WebSocketChannel.h" |
| 38 #include "public/platform/WebString.h" | 38 #include "public/platform/WebString.h" |
| 39 #include "public/platform/WebURL.h" | 39 #include "public/platform/WebURL.h" |
| 40 #include "public/web/WebArrayBuffer.h" | 40 #include "public/web/WebArrayBuffer.h" |
| 41 #include "public/web/WebDocument.h" | 41 #include "public/web/WebDocument.h" |
| 42 #include "web/WebSocketChannelClientProxy.h" | 42 #include "web/WebPepperSocketChannelClientProxy.h" |
| 43 #include "wtf/text/CString.h" | 43 #include "wtf/text/CString.h" |
| 44 #include "wtf/text/WTFString.h" | 44 #include "wtf/text/WTFString.h" |
| 45 | 45 |
| 46 namespace blink { | 46 namespace blink { |
| 47 | 47 |
| 48 WebSocketImpl::WebSocketImpl(const WebDocument& document, WebSocketClient* clien
t) | 48 WebPepperSocketImpl::WebPepperSocketImpl(const WebDocument& document, WebPepperS
ocketClient* client) |
| 49 : m_client(client) | 49 : m_client(client) |
| 50 , m_channelProxy(WebSocketChannelClientProxy::create(this)) | 50 , m_channelProxy(WebPepperSocketChannelClientProxy::create(this)) |
| 51 , m_binaryType(BinaryTypeBlob) | 51 , m_binaryType(BinaryTypeBlob) |
| 52 , m_isClosingOrClosed(false) | 52 , m_isClosingOrClosed(false) |
| 53 , m_bufferedAmount(0) | 53 , m_bufferedAmount(0) |
| 54 , m_bufferedAmountAfterClose(0) | 54 , m_bufferedAmountAfterClose(0) |
| 55 { | 55 { |
| 56 RefPtrWillBeRawPtr<Document> coreDocument = PassRefPtrWillBeRawPtr<Document>
(document); | 56 RefPtrWillBeRawPtr<Document> coreDocument = PassRefPtrWillBeRawPtr<Document>
(document); |
| 57 m_private = DocumentWebSocketChannel::create(coreDocument.get(), m_channelPr
oxy.get()); | 57 m_private = DocumentWebSocketChannel::create(coreDocument.get(), m_channelPr
oxy.get()); |
| 58 } | 58 } |
| 59 | 59 |
| 60 WebSocketImpl::~WebSocketImpl() | 60 WebPepperSocketImpl::~WebPepperSocketImpl() |
| 61 { | 61 { |
| 62 m_private->disconnect(); | 62 m_private->disconnect(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 WebSocket::BinaryType WebSocketImpl::binaryType() const | 65 WebPepperSocket::BinaryType WebPepperSocketImpl::binaryType() const |
| 66 { | 66 { |
| 67 return m_binaryType; | 67 return m_binaryType; |
| 68 } | 68 } |
| 69 | 69 |
| 70 bool WebSocketImpl::setBinaryType(BinaryType binaryType) | 70 bool WebPepperSocketImpl::setBinaryType(BinaryType binaryType) |
| 71 { | 71 { |
| 72 if (binaryType > BinaryTypeArrayBuffer) | 72 if (binaryType > BinaryTypeArrayBuffer) |
| 73 return false; | 73 return false; |
| 74 m_binaryType = binaryType; | 74 m_binaryType = binaryType; |
| 75 return true; | 75 return true; |
| 76 } | 76 } |
| 77 | 77 |
| 78 void WebSocketImpl::connect(const WebURL& url, const WebString& protocol) | 78 void WebPepperSocketImpl::connect(const WebURL& url, const WebString& protocol) |
| 79 { | 79 { |
| 80 m_private->connect(url, protocol); | 80 m_private->connect(url, protocol); |
| 81 } | 81 } |
| 82 | 82 |
| 83 WebString WebSocketImpl::subprotocol() | 83 WebString WebPepperSocketImpl::subprotocol() |
| 84 { | 84 { |
| 85 return m_subprotocol; | 85 return m_subprotocol; |
| 86 } | 86 } |
| 87 | 87 |
| 88 WebString WebSocketImpl::extensions() | 88 WebString WebPepperSocketImpl::extensions() |
| 89 { | 89 { |
| 90 return m_extensions; | 90 return m_extensions; |
| 91 } | 91 } |
| 92 | 92 |
| 93 bool WebSocketImpl::sendText(const WebString& message) | 93 bool WebPepperSocketImpl::sendText(const WebString& message) |
| 94 { | 94 { |
| 95 String coreMessage = message; | 95 String coreMessage = message; |
| 96 CString encodedMessage = coreMessage.utf8(); | 96 CString encodedMessage = coreMessage.utf8(); |
| 97 size_t size = encodedMessage.length(); | 97 size_t size = encodedMessage.length(); |
| 98 m_bufferedAmount += size; | 98 m_bufferedAmount += size; |
| 99 if (m_isClosingOrClosed) | 99 if (m_isClosingOrClosed) |
| 100 m_bufferedAmountAfterClose += size; | 100 m_bufferedAmountAfterClose += size; |
| 101 | 101 |
| 102 // FIXME: Deprecate this call. | 102 // FIXME: Deprecate this call. |
| 103 m_client->didUpdateBufferedAmount(m_bufferedAmount); | 103 m_client->didUpdateBufferedAmount(m_bufferedAmount); |
| 104 | 104 |
| 105 if (m_isClosingOrClosed) | 105 if (m_isClosingOrClosed) |
| 106 return true; | 106 return true; |
| 107 | 107 |
| 108 m_private->send(encodedMessage); | 108 m_private->send(encodedMessage); |
| 109 return true; | 109 return true; |
| 110 } | 110 } |
| 111 | 111 |
| 112 bool WebSocketImpl::sendArrayBuffer(const WebArrayBuffer& webArrayBuffer) | 112 bool WebPepperSocketImpl::sendArrayBuffer(const WebArrayBuffer& webArrayBuffer) |
| 113 { | 113 { |
| 114 size_t size = webArrayBuffer.byteLength(); | 114 size_t size = webArrayBuffer.byteLength(); |
| 115 m_bufferedAmount += size; | 115 m_bufferedAmount += size; |
| 116 if (m_isClosingOrClosed) | 116 if (m_isClosingOrClosed) |
| 117 m_bufferedAmountAfterClose += size; | 117 m_bufferedAmountAfterClose += size; |
| 118 | 118 |
| 119 // FIXME: Deprecate this call. | 119 // FIXME: Deprecate this call. |
| 120 m_client->didUpdateBufferedAmount(m_bufferedAmount); | 120 m_client->didUpdateBufferedAmount(m_bufferedAmount); |
| 121 | 121 |
| 122 if (m_isClosingOrClosed) | 122 if (m_isClosingOrClosed) |
| 123 return true; | 123 return true; |
| 124 | 124 |
| 125 RefPtr<DOMArrayBuffer> arrayBuffer = PassRefPtr<DOMArrayBuffer>(webArrayBuff
er); | 125 RefPtr<DOMArrayBuffer> arrayBuffer = PassRefPtr<DOMArrayBuffer>(webArrayBuff
er); |
| 126 m_private->send(*arrayBuffer, 0, arrayBuffer->byteLength()); | 126 m_private->send(*arrayBuffer, 0, arrayBuffer->byteLength()); |
| 127 return true; | 127 return true; |
| 128 } | 128 } |
| 129 | 129 |
| 130 unsigned long WebSocketImpl::bufferedAmount() const | 130 unsigned long WebPepperSocketImpl::bufferedAmount() const |
| 131 { | 131 { |
| 132 return m_bufferedAmount; | 132 return m_bufferedAmount; |
| 133 } | 133 } |
| 134 | 134 |
| 135 void WebSocketImpl::close(int code, const WebString& reason) | 135 void WebPepperSocketImpl::close(int code, const WebString& reason) |
| 136 { | 136 { |
| 137 m_isClosingOrClosed = true; | 137 m_isClosingOrClosed = true; |
| 138 m_private->close(code, reason); | 138 m_private->close(code, reason); |
| 139 } | 139 } |
| 140 | 140 |
| 141 void WebSocketImpl::fail(const WebString& reason) | 141 void WebPepperSocketImpl::fail(const WebString& reason) |
| 142 { | 142 { |
| 143 m_private->fail(reason, ErrorMessageLevel, String(), 0); | 143 m_private->fail(reason, ErrorMessageLevel, String(), 0); |
| 144 } | 144 } |
| 145 | 145 |
| 146 void WebSocketImpl::disconnect() | 146 void WebPepperSocketImpl::disconnect() |
| 147 { | 147 { |
| 148 m_private->disconnect(); | 148 m_private->disconnect(); |
| 149 m_client = nullptr; | 149 m_client = nullptr; |
| 150 } | 150 } |
| 151 | 151 |
| 152 void WebSocketImpl::didConnect(const String& subprotocol, const String& extensio
ns) | 152 void WebPepperSocketImpl::didConnect(const String& subprotocol, const String& ex
tensions) |
| 153 { | 153 { |
| 154 m_client->didConnect(subprotocol, extensions); | 154 m_client->didConnect(subprotocol, extensions); |
| 155 | 155 |
| 156 // FIXME: Deprecate these statements. | 156 // FIXME: Deprecate these statements. |
| 157 m_subprotocol = subprotocol; | 157 m_subprotocol = subprotocol; |
| 158 m_extensions = extensions; | 158 m_extensions = extensions; |
| 159 m_client->didConnect(); | 159 m_client->didConnect(); |
| 160 } | 160 } |
| 161 | 161 |
| 162 void WebSocketImpl::didReceiveTextMessage(const String& payload) | 162 void WebPepperSocketImpl::didReceiveTextMessage(const String& payload) |
| 163 { | 163 { |
| 164 m_client->didReceiveMessage(WebString(payload)); | 164 m_client->didReceiveMessage(WebString(payload)); |
| 165 } | 165 } |
| 166 | 166 |
| 167 void WebSocketImpl::didReceiveBinaryMessage(PassOwnPtr<Vector<char>> payload) | 167 void WebPepperSocketImpl::didReceiveBinaryMessage(PassOwnPtr<Vector<char>> paylo
ad) |
| 168 { | 168 { |
| 169 switch (m_binaryType) { | 169 switch (m_binaryType) { |
| 170 case BinaryTypeBlob: | 170 case BinaryTypeBlob: |
| 171 // FIXME: Handle Blob after supporting WebBlob. | 171 // FIXME: Handle Blob after supporting WebBlob. |
| 172 break; | 172 break; |
| 173 case BinaryTypeArrayBuffer: | 173 case BinaryTypeArrayBuffer: |
| 174 m_client->didReceiveArrayBuffer(WebArrayBuffer(DOMArrayBuffer::create(pa
yload->data(), payload->size()))); | 174 m_client->didReceiveArrayBuffer(WebArrayBuffer(DOMArrayBuffer::create(pa
yload->data(), payload->size()))); |
| 175 break; | 175 break; |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 void WebSocketImpl::didError() | 179 void WebPepperSocketImpl::didError() |
| 180 { | 180 { |
| 181 m_client->didReceiveMessageError(); | 181 m_client->didReceiveMessageError(); |
| 182 } | 182 } |
| 183 | 183 |
| 184 void WebSocketImpl::didConsumeBufferedAmount(unsigned long consumed) | 184 void WebPepperSocketImpl::didConsumeBufferedAmount(unsigned long consumed) |
| 185 { | 185 { |
| 186 m_client->didConsumeBufferedAmount(consumed); | 186 m_client->didConsumeBufferedAmount(consumed); |
| 187 | 187 |
| 188 // FIXME: Deprecate the following statements. | 188 // FIXME: Deprecate the following statements. |
| 189 m_bufferedAmount -= consumed; | 189 m_bufferedAmount -= consumed; |
| 190 m_client->didUpdateBufferedAmount(m_bufferedAmount); | 190 m_client->didUpdateBufferedAmount(m_bufferedAmount); |
| 191 } | 191 } |
| 192 | 192 |
| 193 void WebSocketImpl::didStartClosingHandshake() | 193 void WebPepperSocketImpl::didStartClosingHandshake() |
| 194 { | 194 { |
| 195 m_client->didStartClosingHandshake(); | 195 m_client->didStartClosingHandshake(); |
| 196 } | 196 } |
| 197 | 197 |
| 198 void WebSocketImpl::didClose(WebSocketChannelClient::ClosingHandshakeCompletionS
tatus status, unsigned short code, const String& reason) | 198 void WebPepperSocketImpl::didClose(WebSocketChannelClient::ClosingHandshakeCompl
etionStatus status, unsigned short code, const String& reason) |
| 199 { | 199 { |
| 200 m_isClosingOrClosed = true; | 200 m_isClosingOrClosed = true; |
| 201 m_client->didClose(static_cast<WebSocketClient::ClosingHandshakeCompletionSt
atus>(status), code, WebString(reason)); | 201 m_client->didClose(static_cast<WebPepperSocketClient::ClosingHandshakeComple
tionStatus>(status), code, WebString(reason)); |
| 202 | 202 |
| 203 // FIXME: Deprecate this call. | 203 // FIXME: Deprecate this call. |
| 204 m_client->didClose(m_bufferedAmount - m_bufferedAmountAfterClose, static_cas
t<WebSocketClient::ClosingHandshakeCompletionStatus>(status), code, WebString(re
ason)); | 204 m_client->didClose(m_bufferedAmount - m_bufferedAmountAfterClose, static_cas
t<WebPepperSocketClient::ClosingHandshakeCompletionStatus>(status), code, WebStr
ing(reason)); |
| 205 } | 205 } |
| 206 | 206 |
| 207 } // namespace blink | 207 } // namespace blink |
| OLD | NEW |