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 34 #include "WebDocument.h" 35 #include "WebSocketClient.h" 36 #include "core/dom/Document.h" 37 #include "core/page/ConsoleTypes.h" 38 #include "core/page/Settings.h" 39 #include "modules/websockets/MainThreadWebSocketChannel.h" 40 #include "modules/websockets/WebSocketChannel.h" 41 #include "modules/websockets/WebSocketChannelClient.h" 42 #include "public/platform/WebArrayBuffer.h" 43 #include "public/platform/WebString.h" 44 #include "public/platform/WebURL.h" 45 #include "wtf/ArrayBuffer.h" 46 47 using namespace WebCore; 48 49 namespace WebKit { 50 51 WebSocketImpl::WebSocketImpl(const WebDocument& document, WebSocketClient* client) 52 : m_client(client) 53 , m_binaryType(BinaryTypeBlob) 54 { 55 RefPtr<Document> coreDocument = PassRefPtr<Document>(document); 56 Settings* settings = coreDocument->settings(); 57 if (settings && settings->experimentalWebSocketEnabled()) { 58 // FIXME: Create an "experimental" WebSocketChannel instead of a MainThreadWebSocketChannel. 59 m_private = MainThreadWebSocketChannel::create(coreDocument.get(), this); 60 } else 61 m_private = MainThreadWebSocketChannel::create(coreDocument.get(), this); 62 } 63 64 WebSocketImpl::~WebSocketImpl() 65 { 66 m_private->disconnect(); 67 } 68 69 WebSocket::BinaryType WebSocketImpl::binaryType() const 70 { 71 return m_binaryType; 72 } 73 74 bool WebSocketImpl::setBinaryType(BinaryType binaryType) 75 { 76 if (binaryType > BinaryTypeArrayBuffer) 77 return false; 78 m_binaryType = binaryType; 79 return true; 80 } 81 82 void WebSocketImpl::connect(const WebURL& url, const WebString& protocol) 83 { 84 m_private->connect(url, protocol); 85 } 86 87 WebString WebSocketImpl::subprotocol() 88 { 89 return m_private->subprotocol(); 90 } 91 92 WebString WebSocketImpl::extensions() 93 { 94 return m_private->extensions(); 95 } 96 97 bool WebSocketImpl::sendText(const WebString& message) 98 { 99 return m_private->send(message) == WebSocketChannel::SendSuccess; 100 } 101 102 bool WebSocketImpl::sendArrayBuffer(const WebArrayBuffer& webArrayBuffer) 103 { 104 return m_private->send(*PassRefPtr<ArrayBuffer>(webArrayBuffer), 0, webArrayBuffer.byteLength()) == WebSocketChannel::SendSuccess; 105 } 106 107 unsigned long WebSocketImpl::bufferedAmount() const 108 { 109 return m_private->bufferedAmount(); 110 } 111 112 void WebSocketImpl::close(int code, const WebString& reason) 113 { 114 m_private->close(code, reason); 115 } 116 117 void WebSocketImpl::fail(const WebString& reason) 118 { 119 m_private->fail(reason, ErrorMessageLevel); 120 } 121 122 void WebSocketImpl::disconnect() 123 { 124 m_private->disconnect(); 125 m_client = 0; 126 } 127 128 void WebSocketImpl::didConnect() 129 { 130 m_client->didConnect(); 131 } 132 133 void WebSocketImpl::didReceiveMessage(const String& message) 134 { 135 m_client->didReceiveMessage(WebString(message)); 136 } 137 138 void WebSocketImpl::didReceiveBinaryData(PassOwnPtr<Vector<char> > binaryData) 139 { 140 switch (m_binaryType) { 141 case BinaryTypeBlob: 142 // FIXME: Handle Blob after supporting WebBlob. 143 break; 144 case BinaryTypeArrayBuffer: 145 m_client->didReceiveArrayBuffer(WebArrayBuffer(ArrayBuffer::create(binaryData->data(), binaryData->size()))); 146 break; 147 } 148 } 149 150 void WebSocketImpl::didReceiveMessageError() 151 { 152 m_client->didReceiveMessageError(); 153 } 154 155 void WebSocketImpl::didUpdateBufferedAmount(unsigned long bufferedAmount) 156 { 157 m_client->didUpdateBufferedAmount(bufferedAmount); 158 } 159 160 void WebSocketImpl::didStartClosingHandshake() 161 { 162 m_client->didStartClosingHandshake(); 163 } 164 165 void WebSocketImpl::didClose(unsigned long bufferedAmount, ClosingHandshakeCompletionStatus status, unsigned short code, const String& reason) 166 { 167 m_client->didClose(bufferedAmount, static_cast<WebSocketClient::ClosingHandshakeCompletionStatus>(status), code, WebString(reason)); 168 } 169 170 } // namespace WebKit 171