tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebKit / chromium / src / WebSocketImpl.cpp
1 /*
2  * Copyright (C) 2011 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 "Document.h"
35 #include "KURL.h"
36 #if ENABLE(WEB_SOCKETS)
37 #include "WebSocketChannel.h"
38 #include "WebSocketChannelClient.h"
39 #else
40 namespace WebCore {
41 class WebSocketChannel {
42 };
43 } // namespace WebCore
44 #endif
45
46 #include "WebData.h"
47 #include "WebDocument.h"
48 #include "WebSocketClient.h"
49 #include "WebString.h"
50 #include "WebURL.h"
51
52 using namespace WebCore;
53
54 namespace WebKit {
55
56 WebSocketImpl::WebSocketImpl(const WebDocument& document, WebSocketClient* client)
57     : m_client(client)
58 {
59 #if ENABLE(WEB_SOCKETS)
60     m_private = WebSocketChannel::create(PassRefPtr<Document>(document).get(), this);
61 #else
62     ASSERT_NOT_REACHED();
63 #endif
64 }
65
66 WebSocketImpl::~WebSocketImpl()
67 {
68 #if ENABLE(WEB_SOCKETS)
69     m_private->disconnect();
70 #else
71     ASSERT_NOT_REACHED();
72 #endif
73 }
74
75 void WebSocketImpl::connect(const WebURL& url, const WebString& protocol)
76 {
77 #if ENABLE(WEB_SOCKETS)
78     m_private->connect(url, protocol);
79 #else
80     ASSERT_NOT_REACHED();
81 #endif
82 }
83
84 WebString WebSocketImpl::subprotocol()
85 {
86 #if ENABLE(WEB_SOCKETS)
87     return m_private->subprotocol();
88 #else
89     ASSERT_NOT_REACHED();
90 #endif
91 }
92
93 bool WebSocketImpl::sendText(const WebString& message)
94 {
95 #if ENABLE(WEB_SOCKETS)
96     return m_private->send(message);
97 #else
98     ASSERT_NOT_REACHED();
99 #endif
100 }
101
102 bool WebSocketImpl::sendBinary(const WebData& binaryData)
103 {
104 #if ENABLE(WEB_SOCKETS)
105     return m_private->send(binaryData.data(), binaryData.size());
106 #else
107     ASSERT_NOT_REACHED();
108 #endif
109 }
110
111 unsigned long WebSocketImpl::bufferedAmount() const
112 {
113 #if ENABLE(WEB_SOCKETS)
114     return m_private->bufferedAmount();
115 #else
116     ASSERT_NOT_REACHED();
117 #endif
118 }
119
120 void WebSocketImpl::close(int code, const WebString& reason)
121 {
122 #if ENABLE(WEB_SOCKETS)
123     m_private->close(code, reason);
124 #else
125     ASSERT_NOT_REACHED();
126 #endif
127 }
128
129 void WebSocketImpl::fail(const WebString& reason)
130 {
131 #if ENABLE(WEB_SOCKETS)
132     m_private->fail(reason);
133 #else
134     ASSERT_NOT_REACHED();
135 #endif
136 }
137
138 void WebSocketImpl::disconnect()
139 {
140 #if ENABLE(WEB_SOCKETS)
141     m_private->disconnect();
142     m_client = 0;
143 #else
144     ASSERT_NOT_REACHED();
145 #endif
146 }
147
148 void WebSocketImpl::didConnect()
149 {
150 #if ENABLE(WEB_SOCKETS)
151     m_client->didConnect();
152 #else
153     ASSERT_NOT_REACHED();
154 #endif
155 }
156
157 void WebSocketImpl::didReceiveMessage(const String& message)
158 {
159 #if ENABLE(WEB_SOCKETS)
160     m_client->didReceiveMessage(WebString(message));
161 #else
162     ASSERT_NOT_REACHED();
163 #endif
164 }
165
166 void WebSocketImpl::didReceiveBinaryData(PassOwnPtr<Vector<char> > binaryData)
167 {
168 #if ENABLE(WEB_SOCKETS)
169     m_client->didReceiveBinaryData(WebData(binaryData->data(), binaryData->size()));
170 #else
171     ASSERT_NOT_REACHED();
172 #endif
173 }
174
175 void WebSocketImpl::didReceiveMessageError()
176 {
177 #if ENABLE(WEB_SOCKETS)
178     m_client->didReceiveMessageError();
179 #else
180     ASSERT_NOT_REACHED();
181 #endif
182 }
183
184 void WebSocketImpl::didStartClosingHandshake()
185 {
186 #if ENABLE(WEB_SOCKETS)
187     m_client->didStartClosingHandshake();
188 #else
189     ASSERT_NOT_REACHED();
190 #endif
191 }
192
193 void WebSocketImpl::didClose(unsigned long bufferedAmount, ClosingHandshakeCompletionStatus status, unsigned short code, const String& reason)
194 {
195 #if ENABLE(WEB_SOCKETS)
196     m_client->didClose(bufferedAmount, static_cast<WebSocketClient::ClosingHandshakeCompletionStatus>(status), code, WebString(reason));
197 #else
198     ASSERT_NOT_REACHED();
199 #endif
200 }
201
202 } // namespace WebKit