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