OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 // |this| is deleted here. | 108 // |this| is deleted here. |
109 } | 109 } |
110 | 110 |
111 NewWebSocketChannelImpl::NewWebSocketChannelImpl(ExecutionContext* context, WebS
ocketChannelClient* client, const String& sourceURL, unsigned lineNumber) | 111 NewWebSocketChannelImpl::NewWebSocketChannelImpl(ExecutionContext* context, WebS
ocketChannelClient* client, const String& sourceURL, unsigned lineNumber) |
112 : ContextLifecycleObserver(context) | 112 : ContextLifecycleObserver(context) |
113 , m_handle(adoptPtr(blink::Platform::current()->createWebSocketHandle())) | 113 , m_handle(adoptPtr(blink::Platform::current()->createWebSocketHandle())) |
114 , m_client(client) | 114 , m_client(client) |
115 , m_identifier(0) | 115 , m_identifier(0) |
116 , m_sendingQuota(0) | 116 , m_sendingQuota(0) |
117 , m_receivedDataSizeForFlowControl(receivedDataSizeForFlowControlHighWaterMa
rk * 2) // initial quota | 117 , m_receivedDataSizeForFlowControl(receivedDataSizeForFlowControlHighWaterMa
rk * 2) // initial quota |
118 , m_bufferedAmount(0) | |
119 , m_sentSizeOfTopMessage(0) | 118 , m_sentSizeOfTopMessage(0) |
120 , m_sourceURLAtConstruction(sourceURL) | 119 , m_sourceURLAtConstruction(sourceURL) |
121 , m_lineNumberAtConstruction(lineNumber) | 120 , m_lineNumberAtConstruction(lineNumber) |
122 { | 121 { |
123 if (context->isDocument() && toDocument(context)->page()) | 122 if (context->isDocument() && toDocument(context)->page()) |
124 m_identifier = createUniqueIdentifier(); | 123 m_identifier = createUniqueIdentifier(); |
125 } | 124 } |
126 | 125 |
127 NewWebSocketChannelImpl::~NewWebSocketChannelImpl() | 126 NewWebSocketChannelImpl::~NewWebSocketChannelImpl() |
128 { | 127 { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 if (m_identifier) { | 221 if (m_identifier) { |
223 // FIXME: Change the inspector API to show the entire message instead | 222 // FIXME: Change the inspector API to show the entire message instead |
224 // of individual frames. | 223 // of individual frames. |
225 InspectorInstrumentation::didSendWebSocketFrame(document(), m_identifier
, WebSocketFrame::OpCodeBinary, true, data->data(), data->size()); | 224 InspectorInstrumentation::didSendWebSocketFrame(document(), m_identifier
, WebSocketFrame::OpCodeBinary, true, data->data(), data->size()); |
226 } | 225 } |
227 m_messages.append(adoptPtr(new Message(data))); | 226 m_messages.append(adoptPtr(new Message(data))); |
228 sendInternal(); | 227 sendInternal(); |
229 return SendSuccess; | 228 return SendSuccess; |
230 } | 229 } |
231 | 230 |
232 unsigned long NewWebSocketChannelImpl::bufferedAmount() const | |
233 { | |
234 WTF_LOG(Network, "NewWebSocketChannelImpl %p bufferedAmount()", this); | |
235 return m_bufferedAmount; | |
236 } | |
237 | |
238 void NewWebSocketChannelImpl::close(int code, const String& reason) | 231 void NewWebSocketChannelImpl::close(int code, const String& reason) |
239 { | 232 { |
240 WTF_LOG(Network, "NewWebSocketChannelImpl %p close(%d, %s)", this, code, rea
son.utf8().data()); | 233 WTF_LOG(Network, "NewWebSocketChannelImpl %p close(%d, %s)", this, code, rea
son.utf8().data()); |
241 ASSERT(m_handle); | 234 ASSERT(m_handle); |
242 unsigned short codeToSend = static_cast<unsigned short>(code == CloseEventCo
deNotSpecified ? CloseEventCodeNoStatusRcvd : code); | 235 unsigned short codeToSend = static_cast<unsigned short>(code == CloseEventCo
deNotSpecified ? CloseEventCodeNoStatusRcvd : code); |
243 m_handle->close(codeToSend, reason); | 236 m_handle->close(codeToSend, reason); |
244 } | 237 } |
245 | 238 |
246 void NewWebSocketChannelImpl::fail(const String& reason, MessageLevel level, con
st String& sourceURL, unsigned lineNumber) | 239 void NewWebSocketChannelImpl::fail(const String& reason, MessageLevel level, con
st String& sourceURL, unsigned lineNumber) |
247 { | 240 { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 : type(MessageTypeArrayBuffer) | 291 : type(MessageTypeArrayBuffer) |
299 , arrayBuffer(arrayBuffer) { } | 292 , arrayBuffer(arrayBuffer) { } |
300 | 293 |
301 NewWebSocketChannelImpl::Message::Message(PassOwnPtr<Vector<char> > vectorData) | 294 NewWebSocketChannelImpl::Message::Message(PassOwnPtr<Vector<char> > vectorData) |
302 : type(MessageTypeVector) | 295 : type(MessageTypeVector) |
303 , vectorData(vectorData) { } | 296 , vectorData(vectorData) { } |
304 | 297 |
305 void NewWebSocketChannelImpl::sendInternal() | 298 void NewWebSocketChannelImpl::sendInternal() |
306 { | 299 { |
307 ASSERT(m_handle); | 300 ASSERT(m_handle); |
308 unsigned long bufferedAmount = m_bufferedAmount; | 301 unsigned long consumedBufferedAmount = 0; |
309 while (!m_messages.isEmpty() && m_sendingQuota > 0 && !m_blobLoader) { | 302 while (!m_messages.isEmpty() && m_sendingQuota > 0 && !m_blobLoader) { |
310 bool final = false; | 303 bool final = false; |
311 Message* message = m_messages.first().get(); | 304 Message* message = m_messages.first().get(); |
312 switch (message->type) { | 305 switch (message->type) { |
313 case MessageTypeText: { | 306 case MessageTypeText: { |
314 WebSocketHandle::MessageType type = | 307 WebSocketHandle::MessageType type = |
315 m_sentSizeOfTopMessage ? WebSocketHandle::MessageTypeContinuatio
n : WebSocketHandle::MessageTypeText; | 308 m_sentSizeOfTopMessage ? WebSocketHandle::MessageTypeContinuatio
n : WebSocketHandle::MessageTypeText; |
316 size_t size = std::min(static_cast<size_t>(m_sendingQuota), message-
>text.length() - m_sentSizeOfTopMessage); | 309 size_t size = std::min(static_cast<size_t>(m_sendingQuota), message-
>text.length() - m_sentSizeOfTopMessage); |
317 final = (m_sentSizeOfTopMessage + size == message->text.length()); | 310 final = (m_sentSizeOfTopMessage + size == message->text.length()); |
318 m_handle->send(final, type, message->text.data() + m_sentSizeOfTopMe
ssage, size); | 311 m_handle->send(final, type, message->text.data() + m_sentSizeOfTopMe
ssage, size); |
319 m_sentSizeOfTopMessage += size; | 312 m_sentSizeOfTopMessage += size; |
320 m_sendingQuota -= size; | 313 m_sendingQuota -= size; |
| 314 consumedBufferedAmount += size; |
321 break; | 315 break; |
322 } | 316 } |
323 case MessageTypeBlob: | 317 case MessageTypeBlob: |
324 ASSERT(!m_blobLoader); | 318 ASSERT(!m_blobLoader); |
325 m_blobLoader = adoptPtrWillBeNoop(new BlobLoader(message->blobDataHa
ndle, this)); | 319 m_blobLoader = adoptPtrWillBeNoop(new BlobLoader(message->blobDataHa
ndle, this)); |
326 break; | 320 break; |
327 case MessageTypeArrayBuffer: { | 321 case MessageTypeArrayBuffer: { |
328 WebSocketHandle::MessageType type = | 322 WebSocketHandle::MessageType type = |
329 m_sentSizeOfTopMessage ? WebSocketHandle::MessageTypeContinuatio
n : WebSocketHandle::MessageTypeBinary; | 323 m_sentSizeOfTopMessage ? WebSocketHandle::MessageTypeContinuatio
n : WebSocketHandle::MessageTypeBinary; |
330 size_t size = std::min(static_cast<size_t>(m_sendingQuota), message-
>arrayBuffer->byteLength() - m_sentSizeOfTopMessage); | 324 size_t size = std::min(static_cast<size_t>(m_sendingQuota), message-
>arrayBuffer->byteLength() - m_sentSizeOfTopMessage); |
331 final = (m_sentSizeOfTopMessage + size == message->arrayBuffer->byte
Length()); | 325 final = (m_sentSizeOfTopMessage + size == message->arrayBuffer->byte
Length()); |
332 m_handle->send(final, type, static_cast<const char*>(message->arrayB
uffer->data()) + m_sentSizeOfTopMessage, size); | 326 m_handle->send(final, type, static_cast<const char*>(message->arrayB
uffer->data()) + m_sentSizeOfTopMessage, size); |
333 m_sentSizeOfTopMessage += size; | 327 m_sentSizeOfTopMessage += size; |
334 m_sendingQuota -= size; | 328 m_sendingQuota -= size; |
| 329 consumedBufferedAmount += size; |
335 break; | 330 break; |
336 } | 331 } |
337 case MessageTypeVector: { | 332 case MessageTypeVector: { |
338 WebSocketHandle::MessageType type = | 333 WebSocketHandle::MessageType type = |
339 m_sentSizeOfTopMessage ? WebSocketHandle::MessageTypeContinuatio
n : WebSocketHandle::MessageTypeBinary; | 334 m_sentSizeOfTopMessage ? WebSocketHandle::MessageTypeContinuatio
n : WebSocketHandle::MessageTypeBinary; |
340 size_t size = std::min(static_cast<size_t>(m_sendingQuota), message-
>vectorData->size() - m_sentSizeOfTopMessage); | 335 size_t size = std::min(static_cast<size_t>(m_sendingQuota), message-
>vectorData->size() - m_sentSizeOfTopMessage); |
341 final = (m_sentSizeOfTopMessage + size == message->vectorData->size(
)); | 336 final = (m_sentSizeOfTopMessage + size == message->vectorData->size(
)); |
342 m_handle->send(final, type, message->vectorData->data() + m_sentSize
OfTopMessage, size); | 337 m_handle->send(final, type, message->vectorData->data() + m_sentSize
OfTopMessage, size); |
343 m_sentSizeOfTopMessage += size; | 338 m_sentSizeOfTopMessage += size; |
344 m_sendingQuota -= size; | 339 m_sendingQuota -= size; |
| 340 consumedBufferedAmount += size; |
345 break; | 341 break; |
346 } | 342 } |
347 } | 343 } |
348 if (final) { | 344 if (final) { |
349 m_messages.removeFirst(); | 345 m_messages.removeFirst(); |
350 m_sentSizeOfTopMessage = 0; | 346 m_sentSizeOfTopMessage = 0; |
351 } | 347 } |
352 } | 348 } |
353 if (m_client && m_bufferedAmount != bufferedAmount) { | 349 if (m_client && consumedBufferedAmount > 0) |
354 m_client->didUpdateBufferedAmount(m_bufferedAmount); | 350 m_client->didConsumeBufferedAmount(consumedBufferedAmount); |
355 } | |
356 } | 351 } |
357 | 352 |
358 void NewWebSocketChannelImpl::flowControlIfNecessary() | 353 void NewWebSocketChannelImpl::flowControlIfNecessary() |
359 { | 354 { |
360 if (!m_handle || m_receivedDataSizeForFlowControl < receivedDataSizeForFlowC
ontrolHighWaterMark) { | 355 if (!m_handle || m_receivedDataSizeForFlowControl < receivedDataSizeForFlowC
ontrolHighWaterMark) { |
361 return; | 356 return; |
362 } | 357 } |
363 m_handle->flowControl(m_receivedDataSizeForFlowControl); | 358 m_handle->flowControl(m_receivedDataSizeForFlowControl); |
364 m_receivedDataSizeForFlowControl = 0; | 359 m_receivedDataSizeForFlowControl = 0; |
365 } | 360 } |
(...skipping 10 matching lines...) Expand all Loading... |
376 { | 371 { |
377 m_handle.clear(); | 372 m_handle.clear(); |
378 abortAsyncOperations(); | 373 abortAsyncOperations(); |
379 if (!m_client) { | 374 if (!m_client) { |
380 return; | 375 return; |
381 } | 376 } |
382 WebSocketChannelClient* client = m_client; | 377 WebSocketChannelClient* client = m_client; |
383 m_client = 0; | 378 m_client = 0; |
384 WebSocketChannelClient::ClosingHandshakeCompletionStatus status = | 379 WebSocketChannelClient::ClosingHandshakeCompletionStatus status = |
385 wasClean ? WebSocketChannelClient::ClosingHandshakeComplete : WebSocketC
hannelClient::ClosingHandshakeIncomplete; | 380 wasClean ? WebSocketChannelClient::ClosingHandshakeComplete : WebSocketC
hannelClient::ClosingHandshakeIncomplete; |
386 client->didClose(m_bufferedAmount, status, code, reason); | 381 client->didClose(status, code, reason); |
387 // client->didClose may delete this object. | 382 // client->didClose may delete this object. |
388 } | 383 } |
389 | 384 |
390 Document* NewWebSocketChannelImpl::document() | 385 Document* NewWebSocketChannelImpl::document() |
391 { | 386 { |
392 ASSERT(m_identifier); | 387 ASSERT(m_identifier); |
393 ExecutionContext* context = executionContext(); | 388 ExecutionContext* context = executionContext(); |
394 ASSERT(context->isDocument()); | 389 ASSERT(context->isDocument()); |
395 return toDocument(context); | 390 return toDocument(context); |
396 } | 391 } |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 // |this| can be deleted here. | 543 // |this| can be deleted here. |
549 } | 544 } |
550 | 545 |
551 void NewWebSocketChannelImpl::trace(Visitor* visitor) | 546 void NewWebSocketChannelImpl::trace(Visitor* visitor) |
552 { | 547 { |
553 visitor->trace(m_blobLoader); | 548 visitor->trace(m_blobLoader); |
554 WebSocketChannel::trace(visitor); | 549 WebSocketChannel::trace(visitor); |
555 } | 550 } |
556 | 551 |
557 } // namespace WebCore | 552 } // namespace WebCore |
OLD | NEW |