diff options
Diffstat (limited to 'Source/WebCore/Modules/websockets/WebSocketChannel.cpp')
-rw-r--r-- | Source/WebCore/Modules/websockets/WebSocketChannel.cpp | 277 |
1 files changed, 138 insertions, 139 deletions
diff --git a/Source/WebCore/Modules/websockets/WebSocketChannel.cpp b/Source/WebCore/Modules/websockets/WebSocketChannel.cpp index e13d63885..e76f6b9cf 100644 --- a/Source/WebCore/Modules/websockets/WebSocketChannel.cpp +++ b/Source/WebCore/Modules/websockets/WebSocketChannel.cpp @@ -37,31 +37,24 @@ #include "Blob.h" #include "CookieJar.h" #include "Document.h" -#include "ExceptionCodePlaceholder.h" #include "FileError.h" #include "FileReaderLoader.h" #include "Frame.h" -#include "FrameLoader.h" -#include "FrameLoaderClient.h" #include "InspectorInstrumentation.h" #include "Logging.h" #include "Page.h" #include "ProgressTracker.h" #include "ResourceRequest.h" -#include "ScriptCallStack.h" #include "ScriptExecutionContext.h" -#include "Settings.h" +#include "SocketProvider.h" #include "SocketStreamError.h" #include "SocketStreamHandle.h" +#include "UserContentProvider.h" #include "WebSocketChannelClient.h" #include "WebSocketHandshake.h" - #include <runtime/ArrayBuffer.h> -#include <wtf/Deque.h> #include <wtf/FastMalloc.h> #include <wtf/HashMap.h> -#include <wtf/OwnPtr.h> -#include <wtf/PassOwnPtr.h> #include <wtf/text/CString.h> #include <wtf/text/StringHash.h> #include <wtf/text/WTFString.h> @@ -70,29 +63,17 @@ namespace WebCore { const double TCPMaximumSegmentLifetime = 2 * 60.0; -WebSocketChannel::WebSocketChannel(Document* document, WebSocketChannelClient* client) - : m_document(document) - , m_client(client) - , m_resumeTimer(this, &WebSocketChannel::resumeTimerFired) - , m_suspended(false) - , m_closing(false) - , m_receivedClosingHandshake(false) - , m_closingTimer(this, &WebSocketChannel::closingTimerFired) - , m_closed(false) - , m_shouldDiscardReceivedData(false) - , m_unhandledBufferedAmount(0) - , m_identifier(0) - , m_hasContinuousFrame(false) - , m_closeEventCode(CloseEventCodeAbnormalClosure) - , m_outgoingFrameQueueStatus(OutgoingFrameQueueOpen) -#if ENABLE(BLOB) - , m_blobLoaderStatus(BlobLoaderNotStarted) -#endif +WebSocketChannel::WebSocketChannel(Document& document, WebSocketChannelClient& client, SocketProvider& provider) + : m_document(&document) + , m_client(&client) + , m_resumeTimer(*this, &WebSocketChannel::resumeTimerFired) + , m_closingTimer(*this, &WebSocketChannel::closingTimerFired) + , m_socketProvider(provider) { - if (Page* page = m_document->page()) + if (Page* page = document.page()) m_identifier = page->progress().createUniqueIdentifier(); - LOG(Network, "WebSocketChannel %p ctor, identifier %lu", this, m_identifier); + LOG(Network, "WebSocketChannel %p ctor, identifier %u", this, m_identifier); } WebSocketChannel::~WebSocketChannel() @@ -100,29 +81,62 @@ WebSocketChannel::~WebSocketChannel() LOG(Network, "WebSocketChannel %p dtor", this); } -void WebSocketChannel::connect(const URL& url, const String& protocol) +void WebSocketChannel::connect(const URL& requestedURL, const String& protocol) { LOG(Network, "WebSocketChannel %p connect()", this); + + URL url = requestedURL; + bool allowCookies = true; +#if ENABLE(CONTENT_EXTENSIONS) + if (auto* page = m_document->page()) { + if (auto* documentLoader = m_document->loader()) { + auto blockedStatus = page->userContentProvider().processContentExtensionRulesForLoad(url, ResourceType::Raw, *documentLoader); + if (blockedStatus.blockedLoad) { + Ref<WebSocketChannel> protectedThis(*this); + callOnMainThread([protectedThis = WTFMove(protectedThis)] { + if (protectedThis->m_client) + protectedThis->m_client->didReceiveMessageError(); + }); + return; + } + if (blockedStatus.madeHTTPS) { + ASSERT(url.protocolIs("ws")); + url.setProtocol("wss"); + if (m_client) + m_client->didUpgradeURL(); + } + if (blockedStatus.blockedCookies) + allowCookies = false; + } + } +#endif + ASSERT(!m_handle); ASSERT(!m_suspended); - m_handshake = adoptPtr(new WebSocketHandshake(url, protocol, m_document)); + m_handshake = std::make_unique<WebSocketHandshake>(url, protocol, m_document, allowCookies); m_handshake->reset(); if (m_deflateFramer.canDeflate()) m_handshake->addExtensionProcessor(m_deflateFramer.createExtensionProcessor()); if (m_identifier) - InspectorInstrumentation::didCreateWebSocket(m_document, m_identifier, url, m_document->url(), protocol); - ref(); - m_handle = SocketStreamHandle::create(m_handshake->url(), this); + InspectorInstrumentation::didCreateWebSocket(m_document, m_identifier, url); + + if (Frame* frame = m_document->frame()) { + ref(); + Page* page = frame->page(); + SessionID sessionID = page ? page->sessionID() : SessionID::defaultSessionID(); + String partition = m_document->topDocument().securityOrigin().domainForCachePartition(); + m_handle = m_socketProvider->createSocketStreamHandle(m_handshake->url(), *this, sessionID, partition); + } } String WebSocketChannel::subprotocol() { LOG(Network, "WebSocketChannel %p subprotocol()", this); if (!m_handshake || m_handshake->mode() != WebSocketHandshake::Connected) - return ""; + return emptyString(); String serverProtocol = m_handshake->serverWebSocketProtocol(); if (serverProtocol.isNull()) - return ""; + return emptyString(); return serverProtocol; } @@ -130,10 +144,10 @@ String WebSocketChannel::extensions() { LOG(Network, "WebSocketChannel %p extensions()", this); if (!m_handshake || m_handshake->mode() != WebSocketHandshake::Connected) - return ""; + return emptyString(); String extensions = m_handshake->acceptedExtensions(); if (extensions.isNull()) - return ""; + return emptyString(); return extensions; } @@ -160,9 +174,9 @@ ThreadableWebSocketChannel::SendResult WebSocketChannel::send(const ArrayBuffer& return ThreadableWebSocketChannel::SendSuccess; } -ThreadableWebSocketChannel::SendResult WebSocketChannel::send(const Blob& binaryData) +ThreadableWebSocketChannel::SendResult WebSocketChannel::send(Blob& binaryData) { - LOG(Network, "WebSocketChannel %p send() Sending Blob '%s'", this, binaryData.url().stringCenterEllipsizedToLength().utf8().data()); + LOG(Network, "WebSocketChannel %p send() Sending Blob '%s'", this, binaryData.url().string().utf8().data()); enqueueBlobFrame(WebSocketFrame::OpCodeBinary, binaryData); processOutgoingFrameQueue(); return ThreadableWebSocketChannel::SendSuccess; @@ -176,7 +190,7 @@ bool WebSocketChannel::send(const char* data, int length) return true; } -unsigned long WebSocketChannel::bufferedAmount() const +unsigned WebSocketChannel::bufferedAmount() const { LOG(Network, "WebSocketChannel %p bufferedAmount()", this); ASSERT(m_handle); @@ -190,7 +204,7 @@ void WebSocketChannel::close(int code, const String& reason) ASSERT(!m_suspended); if (!m_handle) return; - Ref<WebSocketChannel> protect(*this); // An attempt to send closing handshake may fail, which will get the channel closed and dereferenced. + Ref<WebSocketChannel> protectedThis(*this); // An attempt to send closing handshake may fail, which will get the channel closed and dereferenced. startClosingHandshake(code, reason); if (m_closing && !m_closingTimer.isActive()) m_closingTimer.startOneShot(2 * TCPMaximumSegmentLifetime); @@ -202,12 +216,19 @@ void WebSocketChannel::fail(const String& reason) ASSERT(!m_suspended); if (m_document) { InspectorInstrumentation::didReceiveWebSocketFrameError(m_document, m_identifier, reason); - m_document->addConsoleMessage(NetworkMessageSource, ErrorMessageLevel, "WebSocket connection to '" + m_handshake->url().stringCenterEllipsizedToLength() + "' failed: " + reason); + + String consoleMessage; + if (m_handshake) + consoleMessage = makeString("WebSocket connection to '", m_handshake->url().stringCenterEllipsizedToLength(), "' failed: ", reason); + else + consoleMessage = makeString("WebSocket connection failed: ", reason); + + m_document->addConsoleMessage(MessageSource::Network, MessageLevel::Error, consoleMessage); } // Hybi-10 specification explicitly states we must not continue to handle incoming data // once the WebSocket connection is failed (section 7.1.7). - Ref<WebSocketChannel> protect(*this); // The client can close the channel, potentially removing the last reference. + Ref<WebSocketChannel> protectedThis(*this); // The client can close the channel, potentially removing the last reference. m_shouldDiscardReceivedData = true; if (!m_buffer.isEmpty()) skipBuffer(m_buffer.size()); // Save memory. @@ -219,7 +240,8 @@ void WebSocketChannel::fail(const String& reason) if (m_handle && !m_closed) m_handle->disconnect(); // Will call didClose(). - ASSERT(m_closed); + // We should be closed by now, but if we never got a handshake then we never even opened. + ASSERT(m_closed || !m_handshake); } void WebSocketChannel::disconnect() @@ -228,9 +250,9 @@ void WebSocketChannel::disconnect() if (m_identifier && m_document) InspectorInstrumentation::didCloseWebSocket(m_document, m_identifier); if (m_handshake) - m_handshake->clearScriptExecutionContext(); - m_client = 0; - m_document = 0; + m_handshake->clearDocument(); + m_client = nullptr; + m_document = nullptr; if (m_handle) m_handle->disconnect(); } @@ -247,33 +269,25 @@ void WebSocketChannel::resume() m_resumeTimer.startOneShot(0); } -void WebSocketChannel::willOpenSocketStream(SocketStreamHandle* handle) -{ - LOG(Network, "WebSocketChannel %p willOpenSocketStream()", this); - ASSERT(handle); - if (m_document->frame()) - m_document->frame()->loader().client().dispatchWillOpenSocketStream(handle); -} - -void WebSocketChannel::didOpenSocketStream(SocketStreamHandle* handle) +void WebSocketChannel::didOpenSocketStream(SocketStreamHandle& handle) { LOG(Network, "WebSocketChannel %p didOpenSocketStream()", this); - ASSERT(handle == m_handle); + ASSERT(&handle == m_handle); if (!m_document) return; if (m_identifier) InspectorInstrumentation::willSendWebSocketHandshakeRequest(m_document, m_identifier, m_handshake->clientHandshakeRequest()); CString handshakeMessage = m_handshake->clientHandshakeMessage(); - if (!handle->send(handshakeMessage.data(), handshakeMessage.length())) + if (!handle.send(handshakeMessage.data(), handshakeMessage.length())) fail("Failed to send WebSocket handshake."); } -void WebSocketChannel::didCloseSocketStream(SocketStreamHandle* handle) +void WebSocketChannel::didCloseSocketStream(SocketStreamHandle& handle) { LOG(Network, "WebSocketChannel %p didCloseSocketStream()", this); if (m_identifier && m_document) InspectorInstrumentation::didCloseWebSocket(m_document, m_identifier); - ASSERT_UNUSED(handle, handle == m_handle || !m_handle); + ASSERT_UNUSED(handle, &handle == m_handle || !m_handle); m_closed = true; if (m_closingTimer.isActive()) m_closingTimer.stop(); @@ -284,54 +298,58 @@ void WebSocketChannel::didCloseSocketStream(SocketStreamHandle* handle) if (m_suspended) return; WebSocketChannelClient* client = m_client; - m_client = 0; - m_document = 0; - m_handle = 0; + m_client = nullptr; + m_document = nullptr; + m_handle = nullptr; if (client) client->didClose(m_unhandledBufferedAmount, m_receivedClosingHandshake ? WebSocketChannelClient::ClosingHandshakeComplete : WebSocketChannelClient::ClosingHandshakeIncomplete, m_closeEventCode, m_closeEventReason); } deref(); } -void WebSocketChannel::didReceiveSocketStreamData(SocketStreamHandle* handle, const char* data, int len) +void WebSocketChannel::didReceiveSocketStreamData(SocketStreamHandle& handle, const char* data, std::optional<size_t> len) { - LOG(Network, "WebSocketChannel %p didReceiveSocketStreamData() Received %d bytes", this, len); - Ref<WebSocketChannel> protect(*this); // The client can close the channel, potentially removing the last reference. - ASSERT(handle == m_handle); + if (len) + LOG(Network, "WebSocketChannel %p didReceiveSocketStreamData() Received %zu bytes", this, len.value()); + else + LOG(Network, "WebSocketChannel %p didReceiveSocketStreamData() Received no bytes", this); + Ref<WebSocketChannel> protectedThis(*this); // The client can close the channel, potentially removing the last reference. + ASSERT(&handle == m_handle); if (!m_document) { return; } - if (len <= 0) { - handle->disconnect(); + if (!len || !len.value()) { + handle.disconnect(); return; } if (!m_client) { m_shouldDiscardReceivedData = true; - handle->disconnect(); + handle.disconnect(); return; } if (m_shouldDiscardReceivedData) return; - if (!appendToBuffer(data, len)) { + if (!appendToBuffer(data, len.value())) { m_shouldDiscardReceivedData = true; fail("Ran out of memory while receiving WebSocket data."); return; } - while (!m_suspended && m_client && !m_buffer.isEmpty()) + while (!m_suspended && m_client && !m_buffer.isEmpty()) { if (!processBuffer()) break; + } } -void WebSocketChannel::didUpdateBufferedAmount(SocketStreamHandle*, size_t bufferedAmount) +void WebSocketChannel::didUpdateBufferedAmount(SocketStreamHandle&, size_t bufferedAmount) { if (m_client) m_client->didUpdateBufferedAmount(bufferedAmount); } -void WebSocketChannel::didFailSocketStream(SocketStreamHandle* handle, const SocketStreamError& error) +void WebSocketChannel::didFailSocketStream(SocketStreamHandle& handle, const SocketStreamError& error) { LOG(Network, "WebSocketChannel %p didFailSocketStream()", this); - ASSERT(handle == m_handle || !m_handle); + ASSERT(&handle == m_handle || !m_handle); if (m_document) { String message; if (error.isNull()) @@ -341,21 +359,12 @@ void WebSocketChannel::didFailSocketStream(SocketStreamHandle* handle, const Soc else message = "WebSocket network error: " + error.localizedDescription(); InspectorInstrumentation::didReceiveWebSocketFrameError(m_document, m_identifier, message); - m_document->addConsoleMessage(NetworkMessageSource, ErrorMessageLevel, message); + m_document->addConsoleMessage(MessageSource::Network, MessageLevel::Error, message); } m_shouldDiscardReceivedData = true; - handle->disconnect(); -} - -void WebSocketChannel::didReceiveAuthenticationChallenge(SocketStreamHandle*, const AuthenticationChallenge&) -{ + handle.disconnect(); } -void WebSocketChannel::didCancelAuthenticationChallenge(SocketStreamHandle*, const AuthenticationChallenge&) -{ -} - -#if ENABLE(BLOB) void WebSocketChannel::didStartLoading() { LOG(Network, "WebSocketChannel %p didStartLoading()", this); @@ -385,18 +394,17 @@ void WebSocketChannel::didFail(int errorCode) LOG(Network, "WebSocketChannel %p didFail() errorCode=%d", this, errorCode); ASSERT(m_blobLoader); ASSERT(m_blobLoaderStatus == BlobLoaderStarted); - m_blobLoader.clear(); + m_blobLoader = nullptr; m_blobLoaderStatus = BlobLoaderFailed; fail("Failed to load Blob: error code = " + String::number(errorCode)); // FIXME: Generate human-friendly reason message. deref(); } -#endif bool WebSocketChannel::appendToBuffer(const char* data, size_t len) { size_t newBufferSize = m_buffer.size() + len; if (newBufferSize < m_buffer.size()) { - LOG(Network, "WebSocketChannel %p appendToBuffer() Buffer overflow (%lu bytes already in receive buffer and appending %lu bytes)", this, static_cast<unsigned long>(m_buffer.size()), static_cast<unsigned long>(len)); + LOG(Network, "WebSocketChannel %p appendToBuffer() Buffer overflow (%u bytes already in receive buffer and appending %u bytes)", this, static_cast<unsigned>(m_buffer.size()), static_cast<unsigned>(len)); return false; } m_buffer.append(data, len); @@ -415,7 +423,7 @@ bool WebSocketChannel::processBuffer() ASSERT(!m_suspended); ASSERT(m_client); ASSERT(!m_buffer.isEmpty()); - LOG(Network, "WebSocketChannel %p processBuffer() Receive buffer has %lu bytes", this, static_cast<unsigned long>(m_buffer.size())); + LOG(Network, "WebSocketChannel %p processBuffer() Receive buffer has %u bytes", this, static_cast<unsigned>(m_buffer.size())); if (m_shouldDiscardReceivedData) return false; @@ -425,7 +433,7 @@ bool WebSocketChannel::processBuffer() return false; } - Ref<WebSocketChannel> protect(*this); // The client can close the channel, potentially removing the last reference. + Ref<WebSocketChannel> protectedThis(*this); // The client can close the channel, potentially removing the last reference. if (m_handshake->mode() == WebSocketHandshake::Incomplete) { int headerLength = m_handshake->readServerHandshake(m_buffer.data(), m_buffer.size()); @@ -435,16 +443,16 @@ bool WebSocketChannel::processBuffer() if (m_identifier) InspectorInstrumentation::didReceiveWebSocketHandshakeResponse(m_document, m_identifier, m_handshake->serverHandshakeResponse()); if (!m_handshake->serverSetCookie().isEmpty()) { - if (cookiesEnabled(m_document)) { + if (m_document && cookiesEnabled(*m_document)) { // Exception (for sandboxed documents) ignored. - m_document->setCookie(m_handshake->serverSetCookie(), IGNORE_EXCEPTION); + m_document->setCookie(m_handshake->serverSetCookie()); } } // FIXME: handle set-cookie2. LOG(Network, "WebSocketChannel %p Connected", this); skipBuffer(headerLength); m_client->didConnect(); - LOG(Network, "WebSocketChannel %p %lu bytes remaining in m_buffer", this, static_cast<unsigned long>(m_buffer.size())); + LOG(Network, "WebSocketChannel %p %u bytes remaining in m_buffer", this, static_cast<unsigned>(m_buffer.size())); return !m_buffer.isEmpty(); } ASSERT(m_handshake->mode() == WebSocketHandshake::Failed); @@ -460,16 +468,14 @@ bool WebSocketChannel::processBuffer() return processFrame(); } -void WebSocketChannel::resumeTimerFired(Timer<WebSocketChannel>* timer) +void WebSocketChannel::resumeTimerFired() { - ASSERT_UNUSED(timer, timer == &m_resumeTimer); - - Ref<WebSocketChannel> protect(*this); // The client can close the channel, potentially removing the last reference. + Ref<WebSocketChannel> protectedThis(*this); // The client can close the channel, potentially removing the last reference. while (!m_suspended && m_client && !m_buffer.isEmpty()) if (!processBuffer()) break; if (!m_suspended && m_client && m_closed && m_handle) - didCloseSocketStream(m_handle.get()); + didCloseSocketStream(*m_handle); } void WebSocketChannel::startClosingHandshake(int code, const String& reason) @@ -489,7 +495,7 @@ void WebSocketChannel::startClosingHandshake(int code, const String& reason) buf.append(reason.utf8().data(), reason.utf8().length()); } enqueueRawFrame(WebSocketFrame::OpCodeClose, buf.data(), buf.size()); - Ref<WebSocketChannel> protect(*this); // An attempt to send closing handshake may fail, which will get the channel closed and dereferenced. + Ref<WebSocketChannel> protectedThis(*this); // An attempt to send closing handshake may fail, which will get the channel closed and dereferenced. processOutgoingFrameQueue(); if (m_closed) { @@ -502,10 +508,9 @@ void WebSocketChannel::startClosingHandshake(int code, const String& reason) m_client->didStartClosingHandshake(); } -void WebSocketChannel::closingTimerFired(Timer<WebSocketChannel>* timer) +void WebSocketChannel::closingTimerFired() { LOG(Network, "WebSocketChannel %p closingTimerFired()", this); - ASSERT_UNUSED(timer, &m_closingTimer == timer); if (m_handle) m_handle->disconnect(); } @@ -529,7 +534,7 @@ bool WebSocketChannel::processFrame() ASSERT(m_buffer.data() < frameEnd); ASSERT(frameEnd <= m_buffer.data() + m_buffer.size()); - OwnPtr<InflateResultHolder> inflateResult = m_deflateFramer.inflate(frame); + auto inflateResult = m_deflateFramer.inflate(frame); if (!inflateResult->succeeded()) { fail(inflateResult->failureReason()); return false; @@ -587,22 +592,20 @@ bool WebSocketChannel::processFrame() // so we should pretend that we have finished to read this frame and // make sure that the member variables are in a consistent state before // the handler is invoked. - // Vector<char>::swap() is used here to clear m_continuousFrameData. - OwnPtr<Vector<char>> continuousFrameData = adoptPtr(new Vector<char>); - m_continuousFrameData.swap(*continuousFrameData); + Vector<uint8_t> continuousFrameData = WTFMove(m_continuousFrameData); m_hasContinuousFrame = false; if (m_continuousFrameOpCode == WebSocketFrame::OpCodeText) { String message; - if (continuousFrameData->size()) - message = String::fromUTF8(continuousFrameData->data(), continuousFrameData->size()); + if (continuousFrameData.size()) + message = String::fromUTF8(continuousFrameData.data(), continuousFrameData.size()); else - message = ""; + message = emptyString(); if (message.isNull()) fail("Could not decode a text frame as UTF-8."); else m_client->didReceiveMessage(message); } else if (m_continuousFrameOpCode == WebSocketFrame::OpCodeBinary) - m_client->didReceiveBinaryData(continuousFrameData.release()); + m_client->didReceiveBinaryData(WTFMove(continuousFrameData)); } break; @@ -612,7 +615,7 @@ bool WebSocketChannel::processFrame() if (frame.payloadLength) message = String::fromUTF8(frame.payload, frame.payloadLength); else - message = ""; + message = emptyString(); skipBuffer(frameEnd - m_buffer.data()); if (message.isNull()) fail("Could not decode a text frame as UTF-8."); @@ -629,10 +632,10 @@ bool WebSocketChannel::processFrame() case WebSocketFrame::OpCodeBinary: if (frame.final) { - OwnPtr<Vector<char>> binaryData = adoptPtr(new Vector<char>(frame.payloadLength)); - memcpy(binaryData->data(), frame.payload, frame.payloadLength); + Vector<uint8_t> binaryData(frame.payloadLength); + memcpy(binaryData.data(), frame.payload, frame.payloadLength); skipBuffer(frameEnd - m_buffer.data()); - m_client->didReceiveBinaryData(binaryData.release()); + m_client->didReceiveBinaryData(WTFMove(binaryData)); } else { m_hasContinuousFrame = true; m_continuousFrameOpCode = WebSocketFrame::OpCodeBinary; @@ -662,12 +665,13 @@ bool WebSocketChannel::processFrame() if (frame.payloadLength >= 3) m_closeEventReason = String::fromUTF8(&frame.payload[2], frame.payloadLength - 2); else - m_closeEventReason = ""; + m_closeEventReason = emptyString(); skipBuffer(frameEnd - m_buffer.data()); m_receivedClosingHandshake = true; startClosingHandshake(m_closeEventCode, m_closeEventReason); if (m_closing) { - m_outgoingFrameQueueStatus = OutgoingFrameQueueClosing; + if (m_outgoingFrameQueueStatus == OutgoingFrameQueueOpen) + m_outgoingFrameQueueStatus = OutgoingFrameQueueClosing; processOutgoingFrameQueue(); } break; @@ -696,33 +700,33 @@ bool WebSocketChannel::processFrame() void WebSocketChannel::enqueueTextFrame(const CString& string) { ASSERT(m_outgoingFrameQueueStatus == OutgoingFrameQueueOpen); - OwnPtr<QueuedFrame> frame = adoptPtr(new QueuedFrame); + auto frame = std::make_unique<QueuedFrame>(); frame->opCode = WebSocketFrame::OpCodeText; frame->frameType = QueuedFrameTypeString; frame->stringData = string; - m_outgoingFrameQueue.append(frame.release()); + m_outgoingFrameQueue.append(WTFMove(frame)); } void WebSocketChannel::enqueueRawFrame(WebSocketFrame::OpCode opCode, const char* data, size_t dataLength) { ASSERT(m_outgoingFrameQueueStatus == OutgoingFrameQueueOpen); - OwnPtr<QueuedFrame> frame = adoptPtr(new QueuedFrame); + auto frame = std::make_unique<QueuedFrame>(); frame->opCode = opCode; frame->frameType = QueuedFrameTypeVector; frame->vectorData.resize(dataLength); if (dataLength) memcpy(frame->vectorData.data(), data, dataLength); - m_outgoingFrameQueue.append(frame.release()); + m_outgoingFrameQueue.append(WTFMove(frame)); } -void WebSocketChannel::enqueueBlobFrame(WebSocketFrame::OpCode opCode, const Blob& blob) +void WebSocketChannel::enqueueBlobFrame(WebSocketFrame::OpCode opCode, Blob& blob) { ASSERT(m_outgoingFrameQueueStatus == OutgoingFrameQueueOpen); - OwnPtr<QueuedFrame> frame = adoptPtr(new QueuedFrame); + auto frame = std::make_unique<QueuedFrame>(); frame->opCode = opCode; frame->frameType = QueuedFrameTypeBlob; - frame->blobData = Blob::create(blob.url(), blob.type(), blob.size()); - m_outgoingFrameQueue.append(frame.release()); + frame->blobData = &blob; + m_outgoingFrameQueue.append(WTFMove(frame)); } void WebSocketChannel::processOutgoingFrameQueue() @@ -730,10 +734,10 @@ void WebSocketChannel::processOutgoingFrameQueue() if (m_outgoingFrameQueueStatus == OutgoingFrameQueueClosed) return; - Ref<WebSocketChannel> protect(*this); // Any call to fail() will get the channel closed and dereferenced. + Ref<WebSocketChannel> protectedThis(*this); // Any call to fail() will get the channel closed and dereferenced. while (!m_outgoingFrameQueue.isEmpty()) { - OwnPtr<QueuedFrame> frame = m_outgoingFrameQueue.takeFirst(); + auto frame = m_outgoingFrameQueue.takeFirst(); switch (frame->frameType) { case QueuedFrameTypeString: { if (!sendFrame(frame->opCode, frame->stringData.data(), frame->stringData.length())) @@ -747,34 +751,31 @@ void WebSocketChannel::processOutgoingFrameQueue() break; case QueuedFrameTypeBlob: { -#if ENABLE(BLOB) switch (m_blobLoaderStatus) { case BlobLoaderNotStarted: ref(); // Will be derefed after didFinishLoading() or didFail(). ASSERT(!m_blobLoader); - m_blobLoader = adoptPtr(new FileReaderLoader(FileReaderLoader::ReadAsArrayBuffer, this)); + ASSERT(frame->blobData); + m_blobLoader = std::make_unique<FileReaderLoader>(FileReaderLoader::ReadAsArrayBuffer, this); m_blobLoaderStatus = BlobLoaderStarted; - m_blobLoader->start(m_document, frame->blobData.get()); - m_outgoingFrameQueue.prepend(frame.release()); + m_blobLoader->start(m_document, *frame->blobData); + m_outgoingFrameQueue.prepend(WTFMove(frame)); return; case BlobLoaderStarted: case BlobLoaderFailed: - m_outgoingFrameQueue.prepend(frame.release()); + m_outgoingFrameQueue.prepend(WTFMove(frame)); return; case BlobLoaderFinished: { RefPtr<ArrayBuffer> result = m_blobLoader->arrayBufferResult(); - m_blobLoader.clear(); + m_blobLoader = nullptr; m_blobLoaderStatus = BlobLoaderNotStarted; if (!sendFrame(frame->opCode, static_cast<const char*>(result->data()), result->byteLength())) fail("Failed to send WebSocket frame."); break; } } -#else - fail("FileReader is not available. Could not send a Blob as WebSocket binary message."); -#endif break; } @@ -795,12 +796,10 @@ void WebSocketChannel::abortOutgoingFrameQueue() { m_outgoingFrameQueue.clear(); m_outgoingFrameQueueStatus = OutgoingFrameQueueClosed; -#if ENABLE(BLOB) if (m_blobLoaderStatus == BlobLoaderStarted) { m_blobLoader->cancel(); didFail(FileError::ABORT_ERR); } -#endif } bool WebSocketChannel::sendFrame(WebSocketFrame::OpCode opCode, const char* data, size_t dataLength) @@ -811,7 +810,7 @@ bool WebSocketChannel::sendFrame(WebSocketFrame::OpCode opCode, const char* data WebSocketFrame frame(opCode, true, false, true, data, dataLength); InspectorInstrumentation::didSendWebSocketFrame(m_document, m_identifier, frame); - OwnPtr<DeflateResultHolder> deflateResult = m_deflateFramer.deflate(frame); + auto deflateResult = m_deflateFramer.deflate(frame); if (!deflateResult->succeeded()) { fail(deflateResult->failureReason()); return false; |