Chromium Code Reviews| Index: content/child/websocket_bridge.cc |
| diff --git a/content/child/websocket_bridge.cc b/content/child/websocket_bridge.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b7e09bb16ae2f754cce563333d6930310c9e5d45 |
| --- /dev/null |
| +++ b/content/child/websocket_bridge.cc |
| @@ -0,0 +1,159 @@ |
| +// Copyright (c) 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 "content/child/websocket_bridge.h" |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/logging.h" |
| +#include "base/strings/string_util.h" |
| +#include "content/child/child_thread.h" |
| +#include "content/child/websocket_dispatcher.h" |
| +#include "content/common/websocket_messages.h" |
| +#include "ipc/ipc_message.h" |
| +#include "ipc/ipc_message_macros.h" |
| +#include "url/gurl.h" |
| +#include "third_party/WebKit/public/platform/WebSocketHandle.h" |
| +#include "third_party/WebKit/public/platform/WebSocketHandleClient.h" |
| +#include "third_party/WebKit/public/platform/WebString.h" |
| +#include "third_party/WebKit/public/platform/WebURL.h" |
| +#include "third_party/WebKit/public/platform/WebVector.h" |
| + |
| +using WebKit::WebSocketHandle; |
| +using WebKit::WebSocketHandleClient; |
| +using WebKit::WebString; |
| +using WebKit::WebURL; |
| +using WebKit::WebVector; |
| + |
| +namespace content { |
| + |
| +WebSocketBridge::WebSocketBridge() |
| + : channel_id_(kInvalidChannelId), client_(NULL) {} |
| + |
| +WebSocketBridge::~WebSocketBridge() { |
| + Disconnect(); |
| +} |
| + |
| +void WebSocketBridge::DidConnect(bool fail, |
| + const WebString& selected_protocol, |
| + const WebString& extensions) { |
| + if (client_) |
| + client_->didConnect(this, fail, selected_protocol, extensions); |
|
Adam Rice
2013/09/17 14:49:43
I think maybe you need to call Disconnect() if |fa
yhirano
2013/09/18 00:49:30
Done.
|
| +} |
| + |
| +void WebSocketBridge::DidReceiveData( |
| + bool fin, |
| + WebSocketHandle::MessageType type, |
| + const char* data, |
| + size_t size) { |
| + if (client_) |
| + client_->didReceiveData(this, fin, type, data, size); |
| +} |
| + |
| +void WebSocketBridge::DidReceiveFlowControl(int64_t quota) { |
| + if (client_) |
| + client_->didReceiveFlowControl(this, quota); |
| +} |
| + |
| +void WebSocketBridge::DidClose(unsigned short code, |
| + const WebString& reason) { |
| + if (client_) |
| + client_->didClose(this, code, reason); |
| + Disconnect(); |
| +} |
| + |
| +void WebSocketBridge::connect( |
| + const WebURL& url, |
| + const WebVector<WebString>& protocols, |
| + const WebString& origin, |
| + WebSocketHandleClient* client) { |
| + |
| + DCHECK_EQ(kInvalidChannelId, channel_id_); |
| + WebSocketDispatcher* dispatcher = |
| + ChildThread::current()->websocket_dispatcher(); |
| + channel_id_ = dispatcher->AddBridge(this); |
| + client_ = client; |
| + |
| + std::vector<std::string> protocols_to_pass; |
| + for (size_t i = 0; i < protocols.size(); ++i) |
| + protocols_to_pass.push_back(protocols[i].utf8()); |
| + GURL origin_to_pass(origin.utf8()); |
| + |
| + DVLOG(1) << "Bridge#" << channel_id_ << " Connect(" |
| + << url << ", (" << JoinString(protocols_to_pass, ", ") << "), " |
| + << origin_to_pass << ")"; |
| + |
| + ChildThread::current()->Send( |
| + new WebSocketHostMsg_AddChannelRequest(channel_id_, |
| + url, |
| + protocols_to_pass, |
| + origin_to_pass)); |
| +} |
| + |
| +void WebSocketBridge::send(bool fin, |
| + WebSocketHandle::MessageType type, |
| + const char* data, |
| + size_t size) { |
| + if (channel_id_ == kInvalidChannelId) |
| + return; |
| + |
| + WebSocketMessageType type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; |
| + switch (type) { |
| + case WebSocketHandle::MessageTypeContinuation: |
| + type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; |
| + break; |
| + case WebSocketHandle::MessageTypeText: |
| + type_to_pass = WEB_SOCKET_MESSAGE_TYPE_TEXT; |
| + break; |
| + case WebSocketHandle::MessageTypeBinary: |
| + type_to_pass = WEB_SOCKET_MESSAGE_TYPE_BINARY; |
| + break; |
| + } |
| + |
| + DVLOG(1) << "Bridge #" << channel_id_ << " Send(" |
| + << fin << ", " << type_to_pass << ", " |
| + << "(data size = " << size << "))"; |
| + |
| + ChildThread::current()->Send( |
| + new WebSocketMsg_SendFrame(channel_id_, |
| + fin, |
| + type_to_pass, |
| + std::vector<char>(&data[0], &data[size]))); |
|
Adam Rice
2013/09/17 14:49:43
FYI: Personally I find vector(data, data + size) e
yhirano
2013/09/18 00:49:30
Done.
|
| +} |
| + |
| +void WebSocketBridge::flowControl(int64_t quota) { |
| + if (channel_id_ == kInvalidChannelId) |
| + return; |
| + |
| + DVLOG(1) << "Bridge #" << channel_id_ << " FlowControl(" << quota << ")"; |
| + |
| + ChildThread::current()->Send( |
| + new WebSocketMsg_FlowControl(channel_id_, quota)); |
| +} |
| + |
| +void WebSocketBridge::close(unsigned short code, |
| + const WebString& reason) { |
| + if (channel_id_ == kInvalidChannelId) |
| + return; |
| + |
| + std::string reason_to_pass = reason.utf8(); |
| + DVLOG(1) << "Bridge #" << channel_id_ << " Close(" |
| + << code << ", " << reason_to_pass << ")"; |
| + ChildThread::current()->Send( |
| + new WebSocketMsg_DropChannel(channel_id_, code, reason_to_pass)); |
| +} |
| + |
| +void WebSocketBridge::Disconnect() { |
| + if (channel_id_ == kInvalidChannelId) |
| + return; |
| + WebSocketDispatcher* dispatcher = |
| + ChildThread::current()->websocket_dispatcher(); |
| + dispatcher->RemoveBridge(channel_id_); |
| + |
| + channel_id_ = kInvalidChannelId; |
| + client_ = NULL; |
| +} |
| + |
| +} // namespace content |