Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(799)

Side by Side Diff: Source/modules/websockets/NewWebSocketChannelImpl.cpp

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

Powered by Google App Engine
This is Rietveld 408576698