Chromium Code Reviews| Index: third_party/WebKit/Source/modules/websockets/WebSocketHandle.cpp |
| diff --git a/third_party/WebKit/Source/modules/websockets/WebSocketHandle.cpp b/third_party/WebKit/Source/modules/websockets/WebSocketHandle.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c86317a271115d011247eacf7fd02a1c9030dc20 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/websockets/WebSocketHandle.cpp |
| @@ -0,0 +1,252 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "modules/websockets/WebSocketHandle.h" |
| + |
| +#include "modules/websockets/WebSocketHandleClient.h" |
| +#include "platform/network/WebSocketHandshakeRequest.h" |
| +#include "platform/network/WebSocketHandshakeResponse.h" |
| +#include "platform/weborigin/KURL.h" |
| +#include "platform/weborigin/SecurityOrigin.h" |
| +#include "public/platform/InterfaceProvider.h" |
| +#include "public/platform/Platform.h" |
| +#include "public/platform/WebScheduler.h" |
| +#include "wtf/Functional.h" |
|
yhirano
2016/08/24 05:01:00
+wtf/text/WTFString.h
darin (slow to review)
2016/08/24 17:15:05
Done.
|
| + |
| +namespace blink { |
| +namespace { |
| + |
| +const uint16_t kAbnormalShutdownOpCode = 1006; |
| + |
| +} // namespace |
| + |
| +WebSocketHandle::WebSocketHandle() |
| + : m_client(nullptr) |
| + , m_clientBinding(this) |
| +{ |
| + DVLOG(1) << "WebSocketHandle @" << this << " created"; |
| +} |
| + |
| +WebSocketHandle::~WebSocketHandle() |
| +{ |
| + DVLOG(1) << "WebSocketHandle @" << this << " deleted"; |
| + |
| + if (m_websocket) |
| + m_websocket->StartClosingHandshake(kAbnormalShutdownOpCode, emptyString()); |
| +} |
| + |
| +void WebSocketHandle::initialize(InterfaceProvider* interfaceProvider) |
| +{ |
| + DVLOG(1) << "WebSocketHandle @" << this << " initialize(...)"; |
| + |
| + DCHECK(!m_websocket); |
| + interfaceProvider->getInterface(mojo::GetProxy(&m_websocket)); |
| + |
| + m_websocket.set_connection_error_handler( |
| + convertToBaseCallback(bind(&WebSocketHandle::onConnectionError, unretained(this)))); |
| +} |
| + |
| +void WebSocketHandle::connect(const KURL& url, const Vector<String>& protocols, SecurityOrigin* origin, const KURL& firstPartyForCookies, const String& userAgentOverride, WebSocketHandleClient* client) |
| +{ |
| + DCHECK(m_websocket); |
| + |
| + DVLOG(1) << "WebSocketHandle @" << this << " connect(" << url.getString() << ", " << origin->toString() << ")"; |
| + |
| + DCHECK(!m_client); |
| + DCHECK(client); |
| + m_client = client; |
| + |
| + m_websocket->AddChannelRequest( |
| + url, |
| + protocols, |
| + origin, |
| + firstPartyForCookies, |
| + userAgentOverride.isNull() ? emptyString() : userAgentOverride, |
| + m_clientBinding.CreateInterfacePtrAndBind( |
| + Platform::current()->currentThread()->scheduler()->loadingTaskRunner()->taskRunner())); |
| +} |
| + |
| +void WebSocketHandle::send(bool fin, WebSocketHandle::MessageType type, const char* data, size_t size) |
| +{ |
| + DCHECK(m_websocket); |
| + |
| + mojom::blink::WebSocketMessageType typeToPass; |
| + switch (type) { |
| + case WebSocketHandle::MessageTypeContinuation: |
| + typeToPass = mojom::blink::WebSocketMessageType::CONTINUATION; |
| + break; |
| + case WebSocketHandle::MessageTypeText: |
| + typeToPass = mojom::blink::WebSocketMessageType::TEXT; |
| + break; |
| + case WebSocketHandle::MessageTypeBinary: |
| + typeToPass = mojom::blink::WebSocketMessageType::BINARY; |
| + break; |
| + default: |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + DVLOG(1) << "WebSocketHandle @" << this << " send(" << fin << ", " << typeToPass << ", " << "(data size = " << size << "))"; |
| + |
| + // TODO(darin): Avoid this copy. |
| + Vector<uint8_t> dataToPass(size); |
| + std::copy(data, data + size, dataToPass.begin()); |
| + |
| + m_websocket->SendFrame(fin, typeToPass, dataToPass); |
| +} |
| + |
| +void WebSocketHandle::flowControl(int64_t quota) |
| +{ |
| + DCHECK(m_websocket); |
| + |
| + DVLOG(1) << "WebSocketHandle @" << this << " flowControl(" << quota << ")"; |
| + |
| + m_websocket->SendFlowControl(quota); |
| +} |
| + |
| +void WebSocketHandle::close(unsigned short code, const String& reason) |
| +{ |
| + DCHECK(m_websocket); |
| + |
| + DVLOG(1) << "WebSocketHandle @" << this << " close(" << code << ", " << reason << ")"; |
| + |
| + m_websocket->StartClosingHandshake(code, reason.isNull() ? emptyString() : reason); |
| +} |
| + |
| +void WebSocketHandle::disconnect() |
| +{ |
| + m_websocket.reset(); |
| + m_client = nullptr; |
| +} |
| + |
| +void WebSocketHandle::onConnectionError() |
| +{ |
| + if (!Platform::current()) { |
| + // In the renderer shutdown sequence, mojo channels are destructed and |
| + // this function is called. On the other hand, blink objects became |
| + // invalid *silently*, which means we must not touch |*client_| any |
| + // more. |
| + // TODO(yhirano): Remove this code once the shutdown sequence is fixed. |
| + disconnect(); |
| + return; |
| + } |
| + |
| + // Our connection to the WebSocket was dropped. This could be due to |
| + // exceeding the maximum number of concurrent websockets from this process. |
| + |
| + // TODO(darin): Communicate a more specific error here (see crbug/634502). |
| + OnFailChannel( |
| + "Error in connection establishment: net:" |
| + ":ERR_INSUFFICIENT_RESOURCES"); |
| +} |
| + |
| +void WebSocketHandle::OnFailChannel(const String& message) |
| +{ |
| + DVLOG(1) << "WebSocketHandle @" << this << " OnFailChannel(" << message << ")"; |
| + |
| + WebSocketHandleClient* client = m_client; |
| + disconnect(); |
| + if (!client) |
| + return; |
| + |
| + client->didFail(this, message); |
| + // |this| can be deleted here. |
| +} |
| + |
| +void WebSocketHandle::OnStartOpeningHandshake(mojom::blink::WebSocketHandshakeRequestPtr request) |
| +{ |
| + DVLOG(1) << "WebSocketHandle @" << this << " OnStartOpeningHandshake(" << request->url.getString() << ")"; |
| + |
| + RefPtr<WebSocketHandshakeRequest> requestToPass = WebSocketHandshakeRequest::create(request->url); |
| + for (size_t i = 0; i < request->headers.size(); ++i) { |
| + const mojom::blink::HttpHeaderPtr& header = request->headers[i]; |
| + requestToPass->addHeaderField(AtomicString(header->name), AtomicString(header->value)); |
| + } |
| + requestToPass->setHeadersText(request->headers_text); |
| + m_client->didStartOpeningHandshake(this, requestToPass); |
| +} |
| + |
| +void WebSocketHandle::OnFinishOpeningHandshake(mojom::blink::WebSocketHandshakeResponsePtr response) |
| +{ |
| + DVLOG(1) << "WebSocketHandle @" << this << " OnFinishOpeningHandshake(" << response->url.getString() << ")"; |
| + |
| + WebSocketHandshakeResponse responseToPass; |
| + responseToPass.setStatusCode(response->status_code); |
| + responseToPass.setStatusText(response->status_text); |
| + for (size_t i = 0; i < response->headers.size(); ++i) { |
| + const mojom::blink::HttpHeaderPtr& header = response->headers[i]; |
| + responseToPass.addHeaderField(AtomicString(header->name), AtomicString(header->value)); |
| + } |
| + responseToPass.setHeadersText(response->headers_text); |
| + m_client->didFinishOpeningHandshake(this, &responseToPass); |
| +} |
| + |
| +void WebSocketHandle::OnAddChannelResponse(const String& protocol, const String& extensions) |
| +{ |
| + DVLOG(1) << "WebSocketHandle @" << this << " OnAddChannelResponse(" << protocol << ", " << extensions << ")"; |
| + |
| + if (!m_client) |
| + return; |
| + |
| + m_client->didConnect(this, protocol, extensions); |
| + // |this| can be deleted here. |
| +} |
| + |
| +void WebSocketHandle::OnDataFrame(bool fin, mojom::blink::WebSocketMessageType type, const Vector<uint8_t>& data) |
| +{ |
| + DVLOG(1) << "WebSocketHandle @" << this << " OnDataFrame(" << fin << ", " << type << ", " << "(data size = " << data.size() << "))"; |
| + if (!m_client) |
| + return; |
| + |
| + WebSocketHandle::MessageType typeToPass = WebSocketHandle::MessageTypeContinuation; |
| + switch (type) { |
| + case mojom::blink::WebSocketMessageType::CONTINUATION: |
| + typeToPass = WebSocketHandle::MessageTypeContinuation; |
| + break; |
| + case mojom::blink::WebSocketMessageType::TEXT: |
| + typeToPass = WebSocketHandle::MessageTypeText; |
| + break; |
| + case mojom::blink::WebSocketMessageType::BINARY: |
| + typeToPass = WebSocketHandle::MessageTypeBinary; |
| + break; |
| + } |
| + const char* dataToPass = reinterpret_cast<const char*>(data.isEmpty() ? nullptr : &data[0]); |
| + m_client->didReceiveData(this, fin, typeToPass, dataToPass, data.size()); |
| + // |this| can be deleted here. |
| +} |
| + |
| +void WebSocketHandle::OnFlowControl(int64_t quota) |
| +{ |
| + DVLOG(1) << "WebSocketHandle @" << this << " OnFlowControl(" << quota << ")"; |
| + if (!m_client) |
| + return; |
| + |
| + m_client->didReceiveFlowControl(this, quota); |
| + // |this| can be deleted here. |
| +} |
| + |
| +void WebSocketHandle::OnDropChannel(bool wasClean, uint16_t code, const String& reason) |
| +{ |
| + DVLOG(1) << "WebSocketHandle @" << this << " OnDropChannel(" << wasClean << ", " << code << ", " << reason << ")"; |
| + |
| + WebSocketHandleClient* client = m_client; |
| + disconnect(); |
| + if (!client) |
| + return; |
| + |
| + client->didClose(this, wasClean, code, reason); |
| + // |this| can be deleted here. |
| +} |
| + |
| +void WebSocketHandle::OnClosingHandshake() |
| +{ |
| + DVLOG(1) << "WebSocketHandle @" << this << " OnClosingHandshake()"; |
| + if (!m_client) |
| + return; |
| + |
| + m_client->didStartClosingHandshake(this); |
| + // |this| can be deleted here. |
| +} |
| + |
| +} // namespace blink |