OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (C) 2013 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 "modules/websockets/NewWebSocketChannelImpl.h" | |
33 | |
34 #include "core/dom/ScriptExecutionContext.h" | |
35 #include "core/fileapi/Blob.h" | |
36 #include "core/fileapi/FileReaderLoader.h" | |
37 #include "core/inspector/InspectorInstrumentation.h" | |
38 #include "core/loader/UniqueIdentifier.h" | |
39 #include "core/platform/Logging.h" | |
40 #include "modules/websockets/WebSocketChannel.h" | |
41 #include "modules/websockets/WebSocketChannelClient.h" | |
42 #include "public/platform/Platform.h" | |
43 #include "public/platform/WebData.h" | |
44 #include "public/platform/WebSocketHandle.h" | |
45 #include "public/platform/WebString.h" | |
46 #include "public/platform/WebURL.h" | |
47 #include "public/platform/WebVector.h" | |
48 #include "weborigin/SecurityOrigin.h" | |
49 #include "wtf/ArrayBuffer.h" | |
50 #include "wtf/OwnPtr.h" | |
51 #include "wtf/PassRefPtr.h" | |
52 #include "wtf/RefPtr.h" | |
53 #include "wtf/Vector.h" | |
54 #include "wtf/text/WTFString.h" | |
55 | |
56 // FIXME: The following notifications are not implemented: | |
57 // InspectorInstrument::willSendWebSocketHandshake | |
58 // InspectorInstrument::didReceiveWebSocketHandshakeResponse | |
59 // InspectorInstrument::didReceiveWebSocketFrame | |
60 // InspectorInstrument::didSendWebSocketFrame | |
61 | |
62 namespace WebCore { | |
63 | |
64 namespace { | |
65 | |
66 bool isClean(int code) | |
67 { | |
68 return code == WebSocketChannel::CloseEventCodeNormalClosure | |
69 || (WebSocketChannel::CloseEventCodeMinimumUserDefined <= code | |
70 && code <= WebSocketChannel::CloseEventCodeMaximumUserDefined); | |
71 } | |
72 | |
73 } // namespace | |
74 | |
75 NewWebSocketChannelImpl::NewWebSocketChannelImpl(ScriptExecutionContext* context , WebSocketChannelClient* client, const String& sourceURL, unsigned lineNumber) | |
76 : m_context(context) | |
77 , m_handle(adoptPtr(WebKit::Platform::current()->createWebSocketHandle())) | |
78 , m_client(client) | |
79 , m_identifier(0) | |
80 , m_state(NotConnected) | |
81 , m_sendingQuota(0) | |
82 , m_receivedDataSizeForFlowControl(0) | |
83 , m_bufferedAmount(0) | |
84 , m_sentSizeOfTopMessage(0) | |
85 , m_hasAlreadyFailed(false) | |
86 , m_isSuspended(false) | |
87 , m_sourceURLAtConnection(sourceURL) | |
88 , m_lineNumberAtConnection(lineNumber) | |
89 { | |
90 if (context->isDocument() && toDocument(context)->page()) { | |
91 m_identifier = createUniqueIdentifier(); | |
92 } | |
93 } | |
94 | |
95 void NewWebSocketChannelImpl::connect(const KURL& url, const String& protocol) | |
96 { | |
97 ASSERT(m_state == NotConnected); | |
98 LOG(Network, "NewWebSocketChannelImpl %p connect()", this); | |
99 if (m_identifier) { | |
100 InspectorInstrumentation::didCreateWebSocket(toDocument(m_context), m_id entifier, url, protocol); | |
101 } | |
102 m_state = Connecting; | |
103 m_url = url; | |
104 Vector<String> protocols; | |
105 // Since protocol is already verified and escaped, we can simply split it. | |
106 protocol.split(", ", true, protocols); | |
107 WebKit::WebVector<WebKit::WebString> webProtocols(protocols.size()); | |
108 for (size_t i = 0; i < protocols.size(); ++i) { | |
109 webProtocols[i] = protocols[i]; | |
110 } | |
111 m_handle->connect(url, webProtocols, m_context->securityOrigin()->toString() , this); | |
tyoshino (SeeGerritForStatus)
2013/08/26 08:04:11
TODO to remember porting callStack copying code?
yhirano
2013/08/26 08:36:18
Thanks, I added an implementation.
| |
112 } | |
113 | |
114 String NewWebSocketChannelImpl::subprotocol() | |
115 { | |
116 LOG(Network, "NewWebSocketChannelImpl %p subprotocol()", this); | |
117 return m_subprotocol; | |
118 } | |
119 | |
120 String NewWebSocketChannelImpl::extensions() | |
121 { | |
122 LOG(Network, "NewWebSocketChannelImpl %p extensions()", this); | |
123 return m_extensions; | |
124 } | |
125 | |
126 WebSocketChannel::SendResult NewWebSocketChannelImpl::send(const String& message ) | |
127 { | |
128 LOG(Network, "NewWebSocketChannelImpl %p sendText(%s)", this, message.utf8() .data()); | |
129 if (m_state != Open) { | |
130 return SendFail; | |
131 } | |
132 m_messages.append(Message(message)); | |
133 sendInternal(); | |
134 return SendSuccess; | |
135 } | |
136 | |
137 WebSocketChannel::SendResult NewWebSocketChannelImpl::send(const Blob& blob) | |
138 { | |
139 LOG(Network, "NewWebSocketChannelImpl %p sendBlob()", this); | |
140 if (m_state != Open) { | |
141 return SendFail; | |
142 } | |
143 m_messages.append(Message(blob)); | |
144 sendInternal(); | |
145 return SendSuccess; | |
146 } | |
147 | |
148 WebSocketChannel::SendResult NewWebSocketChannelImpl::send(const ArrayBuffer& bu ffer, unsigned byteOffset, unsigned byteLength) | |
149 { | |
150 LOG(Network, "NewWebSocketChannelImpl %p sendArrayBuffer(%p, %u, %u)", this, buffer.data(), byteOffset, byteLength); | |
151 if (m_state != Open) { | |
152 return SendFail; | |
153 } | |
154 m_messages.append(buffer.slice(byteOffset, byteOffset + byteLength)); | |
155 sendInternal(); | |
156 return SendSuccess; | |
157 } | |
158 | |
159 unsigned long NewWebSocketChannelImpl::bufferedAmount() const | |
160 { | |
161 LOG(Network, "NewWebSocketChannelImpl %p bufferedAmount()", this); | |
162 return m_bufferedAmount; | |
163 } | |
164 | |
165 void NewWebSocketChannelImpl::close(int code, const String& reason) | |
166 { | |
167 LOG(Network, "NewWebSocketChannelImpl %p close(%d, %s)", this, code, reason. utf8().data()); | |
168 if (m_state == Closing || m_state == Closed) { | |
169 return; | |
170 } | |
171 ASSERT(m_handle); | |
172 if (m_state == Open) { | |
173 m_handle->close(static_cast<unsigned short>(code), reason); | |
tyoshino (SeeGerritForStatus)
2013/08/26 08:04:11
closing handshake timeout will be handled by lower
yhirano
2013/08/26 08:36:18
Yes.
| |
174 } | |
175 m_state = Closing; | |
176 } | |
177 | |
178 void NewWebSocketChannelImpl::fail(const String& reason, MessageLevel level, con st String& sourceURL, unsigned lineNumber) | |
179 { | |
180 LOG(Network, "NewWebSocketChannelImpl %p fail(%s)", this, reason.utf8().data ()); | |
181 // m_handle and m_client can be null here. | |
182 const String message = "WebSocket connection to '" + m_url.elidedString() + "' failed: " + reason; | |
183 m_context->addConsoleMessage(JSMessageSource, level, message, sourceURL, lin eNumber); | |
184 if (m_identifier) { | |
185 InspectorInstrumentation::didReceiveWebSocketFrameError(toDocument(m_con text), m_identifier, reason); | |
186 } | |
187 | |
188 if (m_client && !m_hasAlreadyFailed) { | |
189 if (m_isSuspended) { | |
190 m_pendingEvents.append(PendingEvent(PendingEvent::DidReceiveError)); | |
191 } else { | |
192 m_client->didReceiveMessageError(); | |
193 } | |
194 } | |
195 m_hasAlreadyFailed = true; | |
196 if (m_state != Closing && m_state != Closed) { | |
197 disconnect(); | |
198 } | |
199 } | |
200 | |
201 void NewWebSocketChannelImpl::disconnect() | |
202 { | |
203 LOG(Network, "NewWebSocketChannelImpl %p disconnect()", this); | |
204 if (m_state == Closed) { | |
205 return; | |
206 } | |
207 if (m_identifier) { | |
208 InspectorInstrumentation::didCloseWebSocket(toDocument(m_context), m_ide ntifier); | |
209 } | |
210 ASSERT(m_handle); | |
211 if (m_state != Closing) { | |
212 m_handle->close(CloseEventCodeAbnormalClosure, ""); | |
213 } | |
214 m_state = Closed; | |
215 m_handle = 0; | |
216 m_client = 0; | |
217 } | |
218 | |
219 void NewWebSocketChannelImpl::suspend() | |
220 { | |
221 LOG(Network, "NewWebSocketChannelImpl %p suspend()", this); | |
222 m_isSuspended = true; | |
223 } | |
224 | |
225 void NewWebSocketChannelImpl::resume() | |
226 { | |
227 LOG(Network, "NewWebSocketChannelImpl %p resume()", this); | |
228 m_isSuspended = false; | |
229 if (!m_client) { | |
230 ASSERT(m_state == Closed); | |
231 ASSERT(m_pendingEvents.isEmpty()); | |
232 return; | |
233 } | |
234 sendInternal(); | |
235 flowControlIfNecessary(); | |
236 processPendingEvents(); | |
237 // processPendingEvents may delete this object. | |
238 } | |
239 | |
240 NewWebSocketChannelImpl::Message::Message(const String& text) | |
241 : type(MessageTypeText) | |
242 , text(text.utf8(String::StrictConversionReplacingUnpairedSurrogatesWithFFFD )) { } | |
243 | |
244 NewWebSocketChannelImpl::Message::Message(const Blob& blob) | |
245 : type(MessageTypeBlob) | |
246 , blob(Blob::create(blob.url(), blob.type(), blob.size())) { } | |
247 | |
248 NewWebSocketChannelImpl::Message::Message(PassRefPtr<ArrayBuffer> arrayBuffer) | |
249 : type(MessageTypeArrayBuffer) | |
250 , arrayBuffer(arrayBuffer) { } | |
251 | |
252 void NewWebSocketChannelImpl::sendInternal() | |
253 { | |
254 if (m_state != Open || m_blobLoader || !m_sendingQuota) { | |
255 return; | |
256 } | |
257 ASSERT(m_handle); | |
258 ASSERT(m_client); | |
259 unsigned long bufferedAmount = m_bufferedAmount; | |
260 size_t i; | |
261 for (i = 0; i < m_messages.size(); ++i) { | |
262 if (!m_sendingQuota) { | |
263 break; | |
264 } | |
265 const Message& message = m_messages[i]; | |
266 switch (message.type) { | |
267 case MessageTypeText: { | |
268 WebKit::WebSocketHandle::MessageType type = | |
269 m_sentSizeOfTopMessage ? WebKit::WebSocketHandle::MessageTypeCon tinuation : WebKit::WebSocketHandle::MessageTypeText; | |
270 size_t size = std::min(static_cast<size_t>(m_sendingQuota), message. text.length() - m_sentSizeOfTopMessage); | |
271 m_handle->send(type, message.text.data() + m_sentSizeOfTopMessage, s ize, m_sendingQuota == size); | |
272 m_sentSizeOfTopMessage += size; | |
273 m_sendingQuota -= size; | |
274 break; | |
275 } | |
276 case MessageTypeBlob: | |
277 startLoadingBlob(*message.blob); | |
278 break; | |
279 case MessageTypeArrayBuffer: { | |
280 WebKit::WebSocketHandle::MessageType type = | |
281 m_sentSizeOfTopMessage ? WebKit::WebSocketHandle::MessageTypeCon tinuation : WebKit::WebSocketHandle::MessageTypeBinary; | |
282 size_t size = std::min(static_cast<size_t>(m_sendingQuota), message. arrayBuffer->byteLength() - m_sentSizeOfTopMessage); | |
283 m_handle->send(type, static_cast<const char*>(message.arrayBuffer->d ata()) + m_sentSizeOfTopMessage, size, m_sendingQuota == size); | |
284 m_sentSizeOfTopMessage += size; | |
285 m_sendingQuota -= size; | |
286 break; | |
287 } | |
288 default: | |
289 ASSERT_NOT_REACHED(); | |
290 } | |
291 if (m_blobLoader || !m_sendingQuota) { | |
292 break; | |
293 } | |
294 | |
295 m_sentSizeOfTopMessage = 0; | |
296 } | |
297 // Drop consumed messages. | |
298 m_messages.remove(0, i); | |
299 if (!m_isSuspended && m_bufferedAmount != bufferedAmount) { | |
300 m_client->didUpdateBufferedAmount(m_bufferedAmount); | |
301 } | |
302 } | |
303 | |
304 void NewWebSocketChannelImpl::flowControlIfNecessary() | |
305 { | |
306 if (m_state != Open) { | |
307 return; | |
308 } | |
309 ASSERT(m_handle); | |
310 if (m_receivedDataSizeForFlowControl < receivedDataSizeForFlowControlHighWat erMark) { | |
311 return; | |
312 } | |
313 m_handle->flowControl(m_receivedDataSizeForFlowControl); | |
314 m_receivedDataSizeForFlowControl = 0; | |
315 } | |
316 | |
317 void NewWebSocketChannelImpl::didConnect(WebKit::WebSocketHandle* handle, bool s ucceed, const WebKit::WebString& selectedProtocol, const WebKit::WebString& exte nsions) | |
318 { | |
319 LOG(Network, "NewWebSocketChannelImpl %p didConnect(%p, %d, %s, %s)", this, handle, succeed, selectedProtocol.utf8().data(), extensions.utf8().data()); | |
320 if (m_state != Connecting) { | |
321 return; | |
322 } | |
323 ASSERT(handle == m_handle); | |
324 ASSERT(m_client); | |
325 if (!succeed) { | |
326 failAsError("Cannot connect to " + m_url.string() + "."); | |
327 return; | |
328 } | |
329 m_state = Open; | |
330 m_subprotocol = selectedProtocol; | |
331 m_extensions = extensions; | |
332 if (m_isSuspended) { | |
333 m_pendingEvents.append(PendingEvent(PendingEvent::DidConnectComplete)); | |
334 } else { | |
335 m_client->didConnect(); | |
336 } | |
337 } | |
338 | |
339 void NewWebSocketChannelImpl::didReceiveData(WebKit::WebSocketHandle* handle, We bKit::WebSocketHandle::MessageType type, const char* data, size_t size, bool fin ) | |
340 { | |
341 LOG(Network, "NewWebSocketChannelImpl %p didReceiveData(%p, (%p, %zu), %d, % d)", this, handle, data.data(), data.size(), type, fin); | |
342 if (m_state != Open) { | |
343 return; | |
344 } | |
345 ASSERT(handle == m_handle); | |
346 ASSERT(m_client); | |
347 if (type == WebKit::WebSocketHandle::MessageTypeText) { | |
348 ASSERT(m_receivingMessageData.isEmpty()); | |
349 m_receivingMessageTypeIsText = true; | |
350 } else if (type == WebKit::WebSocketHandle::MessageTypeBinary) { | |
351 ASSERT(m_receivingMessageData.isEmpty()); | |
352 m_receivingMessageTypeIsText = false; | |
353 } else { | |
354 ASSERT(type == WebKit::WebSocketHandle::MessageTypeContinuation); | |
355 ASSERT(!m_receivingMessageData.isEmpty()); | |
356 } | |
357 m_receivingMessageData.append(data, size); | |
358 m_receivedDataSizeForFlowControl += size; | |
359 flowControlIfNecessary(); | |
360 if (fin) { | |
361 if (m_isSuspended) { | |
362 m_pendingEvents.append(PendingEvent(m_receivingMessageTypeIsText ? P endingEvent::DidReceiveTextMessage : PendingEvent::DidReceiveBinaryMessage)); | |
363 m_pendingEvents.last().message.swap(m_receivingMessageData); | |
364 } else { | |
365 if (m_receivingMessageTypeIsText) { | |
366 String message = ""; | |
367 if (m_receivingMessageData.size() > 0) { | |
368 message = String::fromUTF8(m_receivingMessageData.data(), m_ receivingMessageData.size()); | |
369 } | |
370 if (message.isNull()) { | |
371 failAsError("Could not decode a text frame as UTF-8."); | |
372 } else { | |
373 m_client->didReceiveMessage(message); | |
374 } | |
375 } else { | |
376 OwnPtr<Vector<char> > binaryData = adoptPtr(new Vector<char>); | |
377 m_receivingMessageData.swap(*binaryData); | |
378 m_client->didReceiveBinaryData(binaryData.release()); | |
379 } | |
380 } | |
381 m_receivingMessageData.clear(); | |
382 } | |
383 } | |
384 | |
385 void NewWebSocketChannelImpl::didClose(WebKit::WebSocketHandle* handle, unsigned short code, const WebKit::WebString& reason) | |
386 { | |
387 // FIXME: Maybe we should notify an error to m_client for some didClose mess ages. | |
388 LOG(Network, "NewWebSocketChannelImpl %p didClose(%p, %d, %s)", this, code, String(reason).utf8().data()); | |
389 if (m_state == Closed) { | |
390 return; | |
391 } | |
392 if (m_identifier) { | |
393 InspectorInstrumentation::didCloseWebSocket(toDocument(m_context), m_ide ntifier); | |
394 } | |
395 ASSERT(handle == m_handle); | |
396 m_handle = 0; | |
397 m_state = Closed; | |
398 if (m_isSuspended) { | |
399 m_pendingEvents.append(PendingEvent(code, reason)); | |
400 return; | |
401 } | |
402 WebSocketChannelClient* client = m_client; | |
403 m_client = 0; | |
404 ASSERT(client); | |
405 client->didClose(m_bufferedAmount, | |
406 isClean(code) ? WebSocketChannelClient::ClosingHandshakeComplete : WebSo cketChannelClient::ClosingHandshakeIncomplete, | |
407 code, | |
408 reason); | |
409 // client->didClose may delete this object. | |
410 } | |
411 | |
412 void NewWebSocketChannelImpl::didFinishLoading() | |
413 { | |
414 // m_client can be invalid here. | |
415 LOG(Network, "NewWebSocketChannelImpl %p didFinishLoading()", this); | |
416 if (m_state == Open) { | |
417 ASSERT(m_handle); | |
418 // The loaded blob is always placed on m_messages[0]. | |
419 ASSERT(m_messages.size() > 0 && m_messages[0].type == MessageTypeBlob); | |
420 // We replace it with the loaded blob. | |
421 m_messages[0] = Message(m_blobLoader->arrayBufferResult()); | |
422 sendInternal(); | |
423 } | |
424 m_blobLoader.clear(); | |
425 | |
426 deref(); | |
427 // deref() may delete this object. | |
428 } | |
429 | |
430 void NewWebSocketChannelImpl::didFail(FileError::ErrorCode errorCode) | |
431 { | |
432 // m_client can be invalid here. | |
433 LOG(Network, "NewWebSocketChannelImpl %p didFail(%d)", this, errorCode); | |
434 m_blobLoader.clear(); | |
435 failAsError("Failed to load Blob: error code = " + String::number(errorCode) ); // FIXME: Generate human-friendly reason message. | |
436 deref(); | |
437 // deref() may delete this object. | |
438 } | |
439 | |
440 void NewWebSocketChannelImpl::startLoadingBlob(const Blob& blob) | |
441 { | |
442 LOG(Network, "NewWebSocketChannelImpl %p startLoadingBlob(%s)", this, blob.u rl().string().utf8().data()); | |
443 ASSERT(!m_blobLoader); | |
444 // Protect this object until the loading completes or fails. | |
445 ref(); | |
446 | |
447 m_blobLoader = adoptPtr(new FileReaderLoader(FileReaderLoader::ReadAsArrayBu ffer, this)); | |
448 m_blobLoader->start(m_context, blob); | |
449 } | |
450 | |
451 void NewWebSocketChannelImpl::processPendingEvents() | |
452 { | |
453 RefPtr<NewWebSocketChannelImpl> protect(this); | |
454 ASSERT(!m_isSuspended); | |
455 | |
456 for (size_t i = 0; i < m_pendingEvents.size(); ++i) { | |
457 ASSERT(m_client); | |
458 PendingEvent& event = m_pendingEvents[i]; | |
459 switch (event.type) { | |
460 case PendingEvent::DidConnectComplete: | |
461 m_client->didConnect(); | |
462 break; | |
463 case PendingEvent::DidReceiveTextMessage: { | |
464 String message = ""; | |
465 if (event.message.size() > 0) { | |
466 message = String::fromUTF8(event.message.data(), event.message.s ize()); | |
467 } | |
468 if (message.isNull()) { | |
469 failAsError("Could not decode a text frame as UTF-8."); | |
470 // m_client can be null here. | |
471 } else { | |
472 m_client->didReceiveMessage(message); | |
473 } | |
474 break; | |
475 } | |
476 case PendingEvent::DidReceiveBinaryMessage: { | |
477 OwnPtr<Vector<char> > binaryData = adoptPtr(new Vector<char>); | |
478 event.message.swap(*binaryData); | |
479 m_client->didReceiveBinaryData(binaryData.release()); | |
480 break; | |
481 } | |
482 case PendingEvent::DidReceiveError: | |
483 m_client->didReceiveMessageError(); | |
484 break; | |
485 case PendingEvent::DidClose: | |
486 ASSERT(m_state == Closed); | |
487 ASSERT(!m_handle); | |
488 m_client->didClose(m_bufferedAmount, | |
489 isClean(event.closingCode) ? WebSocketChannelClient::ClosingHand shakeComplete : WebSocketChannelClient::ClosingHandshakeIncomplete, | |
490 event.closingCode, | |
491 event.closingReason); | |
492 // m_client can be invalid here. | |
493 m_client = 0; | |
494 break; | |
495 default: | |
496 ASSERT_NOT_REACHED(); | |
497 break; | |
498 } | |
499 | |
500 if (event.type == PendingEvent::DidClose || !m_client) { | |
501 // Drop remaining messages. | |
502 break; | |
503 } | |
504 } | |
505 m_pendingEvents.clear(); | |
506 } | |
507 | |
508 } // namespace WebCore | |
OLD | NEW |