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