Home | History | Annotate | Download | only in web
      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 "core/dom/Document.h"
     36 #include "core/frame/ConsoleTypes.h"
     37 #include "core/frame/Settings.h"
     38 #include "modules/websockets/MainThreadWebSocketChannel.h"
     39 #include "modules/websockets/WebSocketChannel.h"
     40 #include "public/platform/WebArrayBuffer.h"
     41 #include "public/platform/WebString.h"
     42 #include "public/platform/WebURL.h"
     43 #include "wtf/ArrayBuffer.h"
     44 
     45 using namespace WebCore;
     46 
     47 namespace blink {
     48 
     49 WebSocketImpl::WebSocketImpl(const WebDocument& document, WebSocketClient* client)
     50     : m_client(client)
     51     , m_binaryType(BinaryTypeBlob)
     52 {
     53     RefPtr<Document> coreDocument = PassRefPtr<Document>(document);
     54     Settings* settings = coreDocument->settings();
     55     if (settings && settings->experimentalWebSocketEnabled()) {
     56         // FIXME: Create an "experimental" WebSocketChannel instead of a MainThreadWebSocketChannel.
     57         m_private = MainThreadWebSocketChannel::create(coreDocument.get(), this);
     58     } else
     59         m_private = MainThreadWebSocketChannel::create(coreDocument.get(), this);
     60 }
     61 
     62 WebSocketImpl::~WebSocketImpl()
     63 {
     64     m_private->disconnect();
     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 {
     82     m_private->connect(url, protocol);
     83 }
     84 
     85 WebString WebSocketImpl::subprotocol()
     86 {
     87     return m_private->subprotocol();
     88 }
     89 
     90 WebString WebSocketImpl::extensions()
     91 {
     92     return m_private->extensions();
     93 }
     94 
     95 bool WebSocketImpl::sendText(const WebString& message)
     96 {
     97     return m_private->send(message) == WebSocketChannel::SendSuccess;
     98 }
     99 
    100 bool WebSocketImpl::sendArrayBuffer(const WebArrayBuffer& webArrayBuffer)
    101 {
    102     return m_private->send(*PassRefPtr<ArrayBuffer>(webArrayBuffer), 0, webArrayBuffer.byteLength()) == WebSocketChannel::SendSuccess;
    103 }
    104 
    105 unsigned long WebSocketImpl::bufferedAmount() const
    106 {
    107     return m_private->bufferedAmount();
    108 }
    109 
    110 void WebSocketImpl::close(int code, const WebString& reason)
    111 {
    112     m_private->close(code, reason);
    113 }
    114 
    115 void WebSocketImpl::fail(const WebString& reason)
    116 {
    117     m_private->fail(reason, ErrorMessageLevel);
    118 }
    119 
    120 void WebSocketImpl::disconnect()
    121 {
    122     m_private->disconnect();
    123     m_client = 0;
    124 }
    125 
    126 void WebSocketImpl::didConnect()
    127 {
    128     m_client->didConnect();
    129 }
    130 
    131 void WebSocketImpl::didReceiveMessage(const String& message)
    132 {
    133     m_client->didReceiveMessage(WebString(message));
    134 }
    135 
    136 void WebSocketImpl::didReceiveBinaryData(PassOwnPtr<Vector<char> > binaryData)
    137 {
    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     }
    146 }
    147 
    148 void WebSocketImpl::didReceiveMessageError()
    149 {
    150     m_client->didReceiveMessageError();
    151 }
    152 
    153 void WebSocketImpl::didUpdateBufferedAmount(unsigned long bufferedAmount)
    154 {
    155     m_client->didUpdateBufferedAmount(bufferedAmount);
    156 }
    157 
    158 void WebSocketImpl::didStartClosingHandshake()
    159 {
    160     m_client->didStartClosingHandshake();
    161 }
    162 
    163 void WebSocketImpl::didClose(unsigned long bufferedAmount, ClosingHandshakeCompletionStatus status, unsigned short code, const String& reason)
    164 {
    165     m_client->didClose(bufferedAmount, static_cast<WebSocketClient::ClosingHandshakeCompletionStatus>(status), code, WebString(reason));
    166 }
    167 
    168 } // namespace blink
    169