Chromium Code Reviews| Index: content/child/websocket_bridge.cc |
| diff --git a/content/child/websocket_bridge.cc b/content/child/websocket_bridge.cc |
| index 7cfff1ad50a942960024e79ac8d98377e363642d..e0a76f7316efb3bba7aae6b916f7b6db27e9b37c 100644 |
| --- a/content/child/websocket_bridge.cc |
| +++ b/content/child/websocket_bridge.cc |
| @@ -5,18 +5,15 @@ |
| #include "content/child/websocket_bridge.h" |
| #include <stdint.h> |
| +#include <string.h> |
| + |
| #include <string> |
| #include <utility> |
| #include <vector> |
| #include "base/logging.h" |
| #include "base/strings/string_util.h" |
| -#include "content/child/child_thread_impl.h" |
| -#include "content/child/websocket_dispatcher.h" |
| -#include "content/common/websocket.h" |
| -#include "content/common/websocket_messages.h" |
| -#include "ipc/ipc_message.h" |
| -#include "ipc/ipc_message_macros.h" |
| +#include "content/public/common/service_registry.h" |
| #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" |
| #include "third_party/WebKit/public/platform/WebString.h" |
| #include "third_party/WebKit/public/platform/WebURL.h" |
| @@ -39,113 +36,97 @@ namespace content { |
| namespace { |
| -const unsigned short kAbnormalShutdownOpCode = 1006; |
| +// XXX const unsigned short kAbnormalShutdownOpCode = 1006; |
| } // namespace |
| WebSocketBridge::WebSocketBridge() |
| - : channel_id_(kInvalidChannelId), |
| - render_frame_id_(MSG_ROUTING_NONE), |
| - client_(NULL) {} |
| + : client_(nullptr), |
| + client_binding_(this) {} |
| -WebSocketBridge::~WebSocketBridge() { |
| - if (channel_id_ != kInvalidChannelId) { |
| - // The connection is abruptly disconnected by the renderer without |
| - // closing handshake. |
| - ChildThreadImpl::current()->Send( |
| - new WebSocketMsg_DropChannel(channel_id_, |
| - false, |
| - kAbnormalShutdownOpCode, |
| - std::string())); |
| - } |
| - Disconnect(); |
| +void WebSocketBridge::Initialize(ServiceRegistry* registry) { |
| + registry->ConnectToRemoteService(mojo::GetProxy(&websocket_)); |
| + |
| + mojom::WebSocketClientPtr client; |
|
Ben Goodger (Google)
2016/07/01 21:19:51
delete these two lines and then...
darin (slow to review)
2016/07/06 16:57:21
Done.
|
| + client_binding_.Bind(&client); |
| + |
| + websocket_->Initialize(std::move(client)); |
|
Ben Goodger (Google)
2016/07/01 21:19:51
websocket_->Initialize(client_binding_.CreateInter
darin (slow to review)
2016/07/06 16:57:21
Done.
|
| } |
| -bool WebSocketBridge::OnMessageReceived(const IPC::Message& msg) { |
| - bool handled = true; |
| - IPC_BEGIN_MESSAGE_MAP(WebSocketBridge, msg) |
| - IPC_MESSAGE_HANDLER(WebSocketMsg_AddChannelResponse, DidConnect) |
| - IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyStartOpeningHandshake, |
| - DidStartOpeningHandshake) |
| - IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyFinishOpeningHandshake, |
| - DidFinishOpeningHandshake) |
| - IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyFailure, DidFail) |
| - IPC_MESSAGE_HANDLER(WebSocketMsg_SendFrame, DidReceiveData) |
| - IPC_MESSAGE_HANDLER(WebSocketMsg_FlowControl, DidReceiveFlowControl) |
| - IPC_MESSAGE_HANDLER(WebSocketMsg_DropChannel, DidClose) |
| - IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyClosing, |
| - DidStartClosingHandshake) |
| - IPC_MESSAGE_UNHANDLED(handled = false) |
| - IPC_END_MESSAGE_MAP() |
| - return handled; |
| +WebSocketBridge::~WebSocketBridge() { |
| } |
| -void WebSocketBridge::DidConnect(const std::string& selected_protocol, |
| - const std::string& extensions) { |
| +void WebSocketBridge::OnFailChannel(const mojo::String& message) { |
| + DVLOG(1) << "Bridge @" << reinterpret_cast<void*>(this) |
| + << " OnFailChannel(" << message << ")"; |
| + |
| WebSocketHandleClient* client = client_; |
| - DVLOG(1) << "WebSocketBridge::DidConnect(" |
| - << selected_protocol << ", " |
| - << extensions << ")"; |
| + Disconnect(); |
| if (!client) |
| return; |
| - WebString protocol_to_pass = WebString::fromUTF8(selected_protocol); |
| - WebString extensions_to_pass = WebString::fromUTF8(extensions); |
| - client->didConnect(this, protocol_to_pass, extensions_to_pass); |
| + client->didFail(this, WebString::fromUTF8(message)); |
| // |this| can be deleted here. |
| } |
| -void WebSocketBridge::DidStartOpeningHandshake( |
| - const WebSocketHandshakeRequest& request) { |
| - DVLOG(1) << "WebSocketBridge::DidStartOpeningHandshake(" |
| - << request.url << ")"; |
| +void WebSocketBridge::OnStartOpeningHandshake( |
| + mojom::WebSocketHandshakeRequestPtr request) { |
| + DVLOG(1) << "Bridge @" << reinterpret_cast<void*>(this) |
| + << " OnStartOpeningHandshake(" << request->url << ")"; |
| // All strings are already encoded to ASCII in the browser. |
| blink::WebSocketHandshakeRequestInfo request_to_pass; |
| - request_to_pass.setURL(WebURL(request.url)); |
| - for (size_t i = 0; i < request.headers.size(); ++i) { |
| - const std::pair<std::string, std::string>& header = request.headers[i]; |
| - request_to_pass.addHeaderField(WebString::fromLatin1(header.first), |
| - WebString::fromLatin1(header.second)); |
| + request_to_pass.setURL(WebURL(request->url)); |
| + for (size_t i = 0; i < request->headers.size(); ++i) { |
| + const mojom::HttpHeaderPtr& header = request->headers[i]; |
| + request_to_pass.addHeaderField(WebString::fromLatin1(header->name), |
| + WebString::fromLatin1(header->value)); |
| } |
| - request_to_pass.setHeadersText(WebString::fromLatin1(request.headers_text)); |
| + request_to_pass.setHeadersText(WebString::fromLatin1(request->headers_text)); |
| client_->didStartOpeningHandshake(this, request_to_pass); |
| } |
| -void WebSocketBridge::DidFinishOpeningHandshake( |
| - const WebSocketHandshakeResponse& response) { |
| - DVLOG(1) << "WebSocketBridge::DidFinishOpeningHandshake(" |
| - << response.url << ")"; |
| +void WebSocketBridge::OnFinishOpeningHandshake( |
| + mojom::WebSocketHandshakeResponsePtr response) { |
| + DVLOG(1) << "Bridge @" << reinterpret_cast<void*>(this) |
| + << " OnFinishOpeningHandshake(" << response->url << ")"; |
| + |
| // All strings are already encoded to ASCII in the browser. |
| blink::WebSocketHandshakeResponseInfo response_to_pass; |
| - response_to_pass.setStatusCode(response.status_code); |
| - response_to_pass.setStatusText(WebString::fromLatin1(response.status_text)); |
| - for (size_t i = 0; i < response.headers.size(); ++i) { |
| - const std::pair<std::string, std::string>& header = response.headers[i]; |
| - response_to_pass.addHeaderField(WebString::fromLatin1(header.first), |
| - WebString::fromLatin1(header.second)); |
| + response_to_pass.setStatusCode(response->status_code); |
| + response_to_pass.setStatusText(WebString::fromLatin1(response->status_text)); |
| + for (size_t i = 0; i < response->headers.size(); ++i) { |
| + const mojom::HttpHeaderPtr& header = response->headers[i]; |
| + response_to_pass.addHeaderField(WebString::fromLatin1(header->name), |
| + WebString::fromLatin1(header->value)); |
| } |
| - response_to_pass.setHeadersText(WebString::fromLatin1(response.headers_text)); |
| + response_to_pass.setHeadersText( |
| + WebString::fromLatin1(response->headers_text)); |
| client_->didFinishOpeningHandshake(this, response_to_pass); |
| } |
| -void WebSocketBridge::DidFail(const std::string& message) { |
| - DVLOG(1) << "WebSocketBridge::DidFail(" << message << ")"; |
| - WebSocketHandleClient* client = client_; |
| - Disconnect(); |
| - if (!client) |
| +void WebSocketBridge::OnAddChannelResponse( |
| + const mojo::String& protocol, |
| + const mojo::String& extensions) { |
| + DVLOG(1) << "Bridge @" << reinterpret_cast<void*>(this) |
| + << " OnAddChannelResponse(" |
| + << protocol << ", " << extensions << ")"; |
| + |
| + if (!client_) |
| return; |
| - WebString message_to_pass = WebString::fromUTF8(message); |
| - client->didFail(this, message_to_pass); |
| + client_->didConnect(this, |
| + WebString::fromUTF8(protocol), |
| + WebString::fromUTF8(extensions)); |
| // |this| can be deleted here. |
| } |
| -void WebSocketBridge::DidReceiveData(bool fin, |
| - WebSocketMessageType type, |
| - const std::vector<char>& data) { |
| - DVLOG(1) << "WebSocketBridge::DidReceiveData(" |
| - << fin << ", " |
| - << type << ", " |
| +void WebSocketBridge::OnBlobSent() { |
| +} |
| + |
| +void WebSocketBridge::OnDataFrame(bool fin, mojom::WebSocketMessageType type, |
| + mojo::Array<uint8_t> data) { |
| + DVLOG(1) << "Bridge @" << reinterpret_cast<void*>(this) |
| + << " OnDataFrame(" << fin << ", " << type << ", " |
| << "(data size = " << data.size() << "))"; |
| if (!client_) |
| return; |
| @@ -153,23 +134,25 @@ void WebSocketBridge::DidReceiveData(bool fin, |
| WebSocketHandle::MessageType type_to_pass = |
| WebSocketHandle::MessageTypeContinuation; |
| switch (type) { |
| - case WEB_SOCKET_MESSAGE_TYPE_CONTINUATION: |
| + case mojom::WebSocketMessageType::CONTINUATION: |
| type_to_pass = WebSocketHandle::MessageTypeContinuation; |
| break; |
| - case WEB_SOCKET_MESSAGE_TYPE_TEXT: |
| + case mojom::WebSocketMessageType::TEXT: |
| type_to_pass = WebSocketHandle::MessageTypeText; |
| break; |
| - case WEB_SOCKET_MESSAGE_TYPE_BINARY: |
| + case mojom::WebSocketMessageType::BINARY: |
| type_to_pass = WebSocketHandle::MessageTypeBinary; |
| break; |
| } |
| - const char* data_to_pass = data.empty() ? NULL : &data[0]; |
| + const char* data_to_pass = |
| + reinterpret_cast<const char*>(data.empty() ? nullptr : &data[0]); |
| client_->didReceiveData(this, fin, type_to_pass, data_to_pass, data.size()); |
| // |this| can be deleted here. |
| } |
| -void WebSocketBridge::DidReceiveFlowControl(int64_t quota) { |
| - DVLOG(1) << "WebSocketBridge::DidReceiveFlowControl(" << quota << ")"; |
| +void WebSocketBridge::OnFlowControl(int64_t quota) { |
| + DVLOG(1) << "Bridge @" << reinterpret_cast<void*>(this) |
| + << " OnFlowControl(" << quota << ")"; |
| if (!client_) |
| return; |
| @@ -177,25 +160,24 @@ void WebSocketBridge::DidReceiveFlowControl(int64_t quota) { |
| // |this| can be deleted here. |
| } |
| -void WebSocketBridge::DidClose(bool was_clean, |
| - unsigned short code, |
| - const std::string& reason) { |
| - DVLOG(1) << "WebSocketBridge::DidClose(" |
| - << was_clean << ", " |
| - << code << ", " |
| +void WebSocketBridge::OnDropChannel(bool was_clean, uint16_t code, |
| + const mojo::String& reason) { |
| + DVLOG(1) << "Bridge @" << reinterpret_cast<void*>(this) |
| + << " OnDropChannel(" << was_clean << ", " << code << ", " |
| << reason << ")"; |
| + |
| WebSocketHandleClient* client = client_; |
| Disconnect(); |
| if (!client) |
| return; |
| - WebString reason_to_pass = WebString::fromUTF8(reason); |
| - client->didClose(this, was_clean, code, reason_to_pass); |
| + client->didClose(this, was_clean, code, WebString::fromUTF8(reason)); |
| // |this| can be deleted here. |
| } |
| -void WebSocketBridge::DidStartClosingHandshake() { |
| - DVLOG(1) << "WebSocketBridge::DidStartClosingHandshake()"; |
| +void WebSocketBridge::OnClosingHandshake() { |
| + DVLOG(1) << "Bridge @" << reinterpret_cast<void*>(this) |
| + << " OnClosingHandshake()"; |
| if (!client_) |
| return; |
| @@ -207,87 +189,77 @@ void WebSocketBridge::connect(const WebURL& url, |
| const WebVector<WebString>& protocols, |
| const WebSecurityOrigin& origin, |
| WebSocketHandleClient* client) { |
| - DCHECK_EQ(kInvalidChannelId, channel_id_); |
| - WebSocketDispatcher* dispatcher = |
| - ChildThreadImpl::current()->websocket_dispatcher(); |
| - channel_id_ = dispatcher->AddBridge(this); |
| + DCHECK(websocket_); |
| + |
| + DVLOG(1) << "Bridge @" << reinterpret_cast<void*>(this) |
| + << " Connect(" << url << ", " << origin.toString().utf8() << ")"; |
| + |
| + DCHECK(!client_); |
| client_ = client; |
| - std::vector<std::string> protocols_to_pass; |
| + mojo::Array<mojo::String> protocols_to_pass; |
| for (size_t i = 0; i < protocols.size(); ++i) |
| - protocols_to_pass.push_back(protocols[i].utf8()); |
| - |
| - DVLOG(1) << "Bridge#" << channel_id_ << " Connect(" << url << ", (" |
| - << base::JoinString(protocols_to_pass, ", ") << "), " |
| - << origin.toString().utf8() << ")"; |
| + protocols_to_pass[i] = protocols[i].utf8(); |
| - ChildThreadImpl::current()->Send(new WebSocketHostMsg_AddChannelRequest( |
| - channel_id_, url, protocols_to_pass, origin, render_frame_id_)); |
| + websocket_->AddChannelRequest(url, std::move(protocols_to_pass), origin); |
| } |
| void WebSocketBridge::send(bool fin, |
| WebSocketHandle::MessageType type, |
| const char* data, |
| size_t size) { |
| - if (channel_id_ == kInvalidChannelId) |
| - return; |
| + DCHECK(websocket_); |
| - WebSocketMessageType type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; |
| + mojom::WebSocketMessageType type_to_pass; |
| switch (type) { |
| case WebSocketHandle::MessageTypeContinuation: |
| - type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; |
| + type_to_pass = mojom::WebSocketMessageType::CONTINUATION; |
| break; |
| case WebSocketHandle::MessageTypeText: |
| - type_to_pass = WEB_SOCKET_MESSAGE_TYPE_TEXT; |
| + type_to_pass = mojom::WebSocketMessageType::TEXT; |
| break; |
| case WebSocketHandle::MessageTypeBinary: |
| - type_to_pass = WEB_SOCKET_MESSAGE_TYPE_BINARY; |
| + type_to_pass = mojom::WebSocketMessageType::BINARY; |
| break; |
| + default: |
| + NOTREACHED(); |
| + return; |
| } |
| - DVLOG(1) << "Bridge #" << channel_id_ << " Send(" |
| - << fin << ", " << type_to_pass << ", " |
| + DVLOG(1) << "Bridge @" << reinterpret_cast<void*>(this) |
| + << " Send(" << fin << ", " << type_to_pass << ", " |
| << "(data size = " << size << "))"; |
| - ChildThreadImpl::current()->Send( |
| - new WebSocketMsg_SendFrame(channel_id_, |
| - fin, |
| - type_to_pass, |
| - std::vector<char>(data, data + size))); |
| + mojo::Array<uint8_t> data_to_pass(size); |
| + memcpy(&data_to_pass[0], data, size); |
| + |
| + websocket_->SendFrame(fin, type_to_pass, std::move(data_to_pass)); |
| } |
| void WebSocketBridge::flowControl(int64_t quota) { |
| - if (channel_id_ == kInvalidChannelId) |
| - return; |
| + DCHECK(websocket_); |
| - DVLOG(1) << "Bridge #" << channel_id_ << " FlowControl(" << quota << ")"; |
| + DVLOG(1) << "Bridge @" << reinterpret_cast<void*>(this) |
| + << " FlowControl(" << quota << ")"; |
| - ChildThreadImpl::current()->Send( |
| - new WebSocketMsg_FlowControl(channel_id_, quota)); |
| + websocket_->SendFlowControl(quota); |
| } |
| void WebSocketBridge::close(unsigned short code, |
| const WebString& reason) { |
| - if (channel_id_ == kInvalidChannelId) |
| - return; |
| + DCHECK(websocket_); |
| std::string reason_to_pass = reason.utf8(); |
| - DVLOG(1) << "Bridge #" << channel_id_ << " Close(" |
| - << code << ", " << reason_to_pass << ")"; |
| - // This method is for closing handshake and hence |was_clean| shall be true. |
| - ChildThreadImpl::current()->Send( |
| - new WebSocketMsg_DropChannel(channel_id_, true, code, reason_to_pass)); |
| + |
| + DVLOG(1) << "Bridge @" << reinterpret_cast<void*>(this) |
| + << " Close(" << code << ", " << reason_to_pass << ")"; |
| + |
| + websocket_->StartClosingHandshake(code, reason_to_pass); |
| } |
| void WebSocketBridge::Disconnect() { |
| - if (channel_id_ == kInvalidChannelId) |
| - return; |
| - WebSocketDispatcher* dispatcher = |
| - ChildThreadImpl::current()->websocket_dispatcher(); |
| - dispatcher->RemoveBridge(channel_id_); |
| - |
| - channel_id_ = kInvalidChannelId; |
| - client_ = NULL; |
| + websocket_.reset(); |
| + client_ = nullptr; |
| } |
| } // namespace content |