summaryrefslogtreecommitdiffstats
path: root/remoting
diff options
context:
space:
mode:
authorsergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-12-08 02:42:31 +0000
committersergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-12-08 02:42:31 +0000
commit04f6b1aeab2b2588f0f7a1514f77dd2b121f1b58 (patch)
treefb0ff370de839d75159b22fb73de4d82c0cc5436 /remoting
parent989101545664a80a3a15528f9e368065372d2a33 (diff)
downloadchromium_src-04f6b1aeab2b2588f0f7a1514f77dd2b121f1b58.zip
chromium_src-04f6b1aeab2b2588f0f7a1514f77dd2b121f1b58.tar.gz
chromium_src-04f6b1aeab2b2588f0f7a1514f77dd2b121f1b58.tar.bz2
Remove WebSocket server and HostStatusService from remoting host.
We are not planning to use WebSockets for host configuration. Removing websocket-related code from remoting host. Review URL: https://codereview.chromium.org/11475037 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171915 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting')
-rw-r--r--remoting/host/host_status_service.cc224
-rw-r--r--remoting/host/host_status_service.h63
-rw-r--r--remoting/host/remoting_me2me_host.cc16
-rw-r--r--remoting/host/websocket_connection.cc495
-rw-r--r--remoting/host/websocket_connection.h139
-rw-r--r--remoting/host/websocket_connection_unittest.cc333
-rw-r--r--remoting/host/websocket_listener.cc96
-rw-r--r--remoting/host/websocket_listener.h69
-rw-r--r--remoting/remoting.gyp7
9 files changed, 0 insertions, 1442 deletions
diff --git a/remoting/host/host_status_service.cc b/remoting/host/host_status_service.cc
deleted file mode 100644
index 3141308..0000000
--- a/remoting/host/host_status_service.cc
+++ /dev/null
@@ -1,224 +0,0 @@
-// Copyright (c) 2012 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 "remoting/host/host_status_service.h"
-
-#include "base/json/json_reader.h"
-#include "base/json/json_writer.h"
-#include "base/stl_util.h"
-#include "base/string_number_conversions.h"
-#include "base/string_util.h"
-#include "base/stringize_macros.h"
-#include "base/values.h"
-#include "net/base/ip_endpoint.h"
-#include "net/base/net_util.h"
-#include "remoting/host/websocket_connection.h"
-#include "remoting/host/websocket_listener.h"
-
-namespace remoting {
-
-namespace {
-
-// HostStatusService uses the first port available in the following range.
-const int kPortRangeMin = 12810;
-const int kPortRangeMax = 12820;
-
-const char kChromeExtensionUrlSchemePrefix[] = "chrome-extension://";
-
-#if defined(NDEBUG)
-const char* const kAllowedWebApplicationIds[] = {
- "gbchcmhmhahfdphkhkmpfmihenigjmpp", // Chrome Remote Desktop
- "kgngmbheleoaphbjbaiobfdepmghbfah", // Pre-release Chrome Remote Desktop
- "odkaodonbgfohohmklejpjiejmcipmib", // Dogfood Chrome Remote Desktop
- "ojoimpklfciegopdfgeenehpalipignm", // Chromoting canary
-};
-#endif
-
-// All messages we expect should be smaller than 64k.
-const uint64 kMaximumIncomingMessageSize = 65536;
-
-} // namespace
-
-class HostStatusService::Connection : public WebSocketConnection::Delegate {
- public:
- // |service| owns Connection connection objects and must outlive them.
- Connection(HostStatusService* service,
- scoped_ptr<WebSocketConnection> websocket);
- virtual ~Connection();
-
- // WebSocketConnection::Delegate interface.
- virtual void OnWebSocketMessage(const std::string& message) OVERRIDE;
- virtual void OnWebSocketClosed() OVERRIDE;
-
- private:
- // Sends message with the specified |method| and |data|.
- void SendMessage(const std::string& method,
- scoped_ptr<base::DictionaryValue> data);
-
- // Closes the connection and destroys this object.
- void Close();
-
- HostStatusService* service_;
- scoped_ptr<WebSocketConnection> websocket_;
-
- DISALLOW_COPY_AND_ASSIGN(Connection);
-};
-
-HostStatusService::Connection::Connection(
- HostStatusService* service,
- scoped_ptr<WebSocketConnection> websocket)
- : service_(service),
- websocket_(websocket.Pass()) {
- websocket_->Accept(this);
- websocket_->set_maximum_message_size(kMaximumIncomingMessageSize);
-}
-
-HostStatusService::Connection::~Connection() {
-}
-
-void HostStatusService::Connection::OnWebSocketMessage(
- const std::string& message) {
- scoped_ptr<base::Value> json(
- base::JSONReader::Read(message, base::JSON_ALLOW_TRAILING_COMMAS));
-
- // Verify that we've received a valid JSON dictionary and extract |method| and
- // |data| fields from it.
- base::DictionaryValue* message_dict = NULL;
- std::string method;
- base::DictionaryValue* data = NULL;
- if (!json.get() ||
- !json->GetAsDictionary(&message_dict) ||
- !message_dict->GetString("method", &method) ||
- !message_dict->GetDictionary("data", &data)) {
- LOG(ERROR) << "Received invalid message: " << message;
- Close();
- return;
- }
-
- if (method == "getHostStatus") {
- SendMessage("hostStatus", service_->GetStatusMessage());
- } else {
- LOG(ERROR) << "Received message with unknown method: " << message;
- scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue());
- response->SetString("method", method);
- SendMessage("unsupportedMethod", response.Pass());
- return;
- }
-}
-
-void HostStatusService::Connection::OnWebSocketClosed() {
- Close();
-}
-
-void HostStatusService::Connection::SendMessage(
- const std::string& method,
- scoped_ptr<base::DictionaryValue> data) {
- scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue());
- message->SetString("method", method);
- message->Set("data", data.release());
-
- std::string message_json;
- base::JSONWriter::Write(message.get(), &message_json);
- websocket_->SendText(message_json);
-}
-
-void HostStatusService::Connection::Close() {
- websocket_.reset();
- service_->OnConnectionClosed(this);
-}
-
-HostStatusService::HostStatusService()
- : started_(false) {
- // TODO(sergeyu): Do we need to listen on IPv6 port too?
- char ip[] = {127, 0, 0, 1};
- net::IPAddressNumber localhost(ip, ip + sizeof(ip));
- for (int port = kPortRangeMin; port < kPortRangeMax; ++port) {
- net::IPEndPoint endpoint(localhost, port);
- // base::Unretained is safe because we own |websocket_listener_|.
- if (websocket_listener_.Listen(
- endpoint,
- base::Bind(&HostStatusService::OnNewConnection,
- base::Unretained(this)))) {
- service_host_name_ = "localhost:" + base::UintToString(port);
- LOG(INFO) << "Listening for WebSocket connections on localhost:" << port;
- break;
- }
- }
-}
-
-HostStatusService::~HostStatusService() {
- STLDeleteElements(&connections_);
-}
-
-void HostStatusService::SetHostIsUp(const std::string& host_id) {
- started_ = true;
- host_id_ = host_id;
-}
-void HostStatusService::SetHostIsDown() {
- started_ = false;
- host_id_.clear();
-}
-
-// static
-bool HostStatusService::IsAllowedOrigin(const std::string& origin) {
-#ifndef NDEBUG
- // Allow all chrome extensions in Debug builds.
- return StartsWithASCII(origin, kChromeExtensionUrlSchemePrefix, false);
-#else
- // For Release builds allow only specific set of clients.
- //
- // TODO(sergeyu): Allow whitelisting of origins different from the ones in
- // kAllowedWebApplicationIds (e.g. specify them in the host config).
- std::string prefix(kChromeExtensionUrlSchemePrefix);
- for (size_t i = 0; i < arraysize(kAllowedWebApplicationIds); ++i) {
- if (origin == prefix + kAllowedWebApplicationIds[i]) {
- return true;
- }
- }
- return false;
-#endif
-}
-
-void HostStatusService::OnNewConnection(
- scoped_ptr<WebSocketConnection> connection) {
- if (connection->request_host() != service_host_name_) {
- LOG(ERROR) << "Received connection for invalid host: "
- << connection->request_host()
- << ". Expected " << service_host_name_;
- connection->Reject();
- return;
- }
-
- if (connection->request_path() != "/remoting_host_status") {
- LOG(ERROR) << "Received connection for unknown path: "
- << connection->request_path();
- connection->Reject();
- return;
- }
-
- if (!IsAllowedOrigin(connection->origin())) {
- LOG(ERROR) << "Rejecting connection from unknown origin: "
- << connection->origin();
- connection->Reject();
- return;
- }
-
- // Accept connection.
- connections_.insert(new Connection(this, connection.Pass()));
-}
-
-void HostStatusService::OnConnectionClosed(Connection* connection) {
- connections_.erase(connection);
- delete connection;
-}
-
-scoped_ptr<base::DictionaryValue> HostStatusService::GetStatusMessage() {
- scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
- result->SetString("state", started_ ? "STARTED" : "STOPPED");
- result->SetString("version", STRINGIZE(VERSION));
- result->SetString("hostId", host_id_);
- return result.Pass();
-}
-
-} // namespace remoting
diff --git a/remoting/host/host_status_service.h b/remoting/host/host_status_service.h
deleted file mode 100644
index 2e2d083..0000000
--- a/remoting/host/host_status_service.h
+++ /dev/null
@@ -1,63 +0,0 @@
-// Copyright (c) 2012 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.
-
-#ifndef REMOTING_HOST_HOST_STATUS_SERVICE_H_
-#define REMOTING_HOST_HOST_STATUS_SERVICE_H_
-
-#include <string>
-
-#include "base/memory/scoped_ptr.h"
-#include "remoting/host/websocket_listener.h"
-
-namespace base {
-class DictionaryValue;
-} // namespace remoting
-
-namespace remoting {
-
-// HostStatusService implements a WebSocket server for the remoting web app to
-// use to query the current status of the host.
-class HostStatusService {
- public:
- HostStatusService();
- ~HostStatusService();
-
- // Must be called whenever state of the host changes.
- void SetHostIsUp(const std::string& host_id);
- void SetHostIsDown();
-
- private:
- class Connection;
- friend class Connection;
-
- // Returns true if |origin| is one of the remoting extension URLs.
- static bool IsAllowedOrigin(const std::string& origin);
-
- // Callback from WebsocketListener.
- void OnNewConnection(scoped_ptr<WebSocketConnection> connection);
-
- // Called from Connection instances when |connection| is closed and the
- // Connection object should be destroyed.
- void OnConnectionClosed(Connection* connection);
-
- // Creates a status message that should be sent as a response to an incoming
- // getHostState message.
- scoped_ptr<base::DictionaryValue> GetStatusMessage();
-
- WebSocketListener websocket_listener_;
- std::set<Connection*> connections_;
-
- // Host name we expect in incoming connections, e.g. "localhost:12810".
- std::string service_host_name_;
-
- // Current state of the host to provide to clients.
- bool started_;
- std::string host_id_;
-
- DISALLOW_COPY_AND_ASSIGN(HostStatusService);
-};
-
-} // namespace remoting
-
-#endif // REMOTING_HOST_HOST_STATUS_SERVICE_H_
diff --git a/remoting/host/remoting_me2me_host.cc b/remoting/host/remoting_me2me_host.cc
index f8e8a97..2561fc4 100644
--- a/remoting/host/remoting_me2me_host.cc
+++ b/remoting/host/remoting_me2me_host.cc
@@ -49,7 +49,6 @@
#include "remoting/host/host_config.h"
#include "remoting/host/host_event_logger.h"
#include "remoting/host/host_exit_codes.h"
-#include "remoting/host/host_status_service.h"
#include "remoting/host/host_user_interface.h"
#include "remoting/host/ipc_constants.h"
#include "remoting/host/ipc_desktop_environment_factory.h"
@@ -105,9 +104,6 @@ const char kVersionSwitchName[] = "version";
// linux.
const char kAudioPipeSwitchName[] = "audio-pipe-name";
-// The command line switch used to enable host status service.
-const char kEnableStatusServiceSwitchName[] = "enable-status-service";
-
void QuitMessageLoop(MessageLoop* message_loop) {
message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure());
}
@@ -276,7 +272,6 @@ class HostProcess
HostState state_;
scoped_ptr<ConfigFileWatcher> config_watcher_;
- scoped_ptr<HostStatusService> status_service_;
std::string host_id_;
protocol::SharedSecretHash host_secret_hash_;
@@ -462,11 +457,6 @@ void HostProcess::OnConfigWatcherError() {
void HostProcess::StartOnNetworkThread() {
DCHECK(context_->network_task_runner()->BelongsToCurrentThread());
- if (CommandLine::ForCurrentProcess()->HasSwitch(
- kEnableStatusServiceSwitchName)) {
- status_service_.reset(new HostStatusService());
- }
-
#if !defined(REMOTING_MULTI_PROCESS)
// Start watching the host configuration file.
config_watcher_.reset(new ConfigFileWatcher(context_->network_task_runner(),
@@ -938,9 +928,6 @@ void HostProcess::StartHost() {
host_, base::Bind(&HostProcess::OnDisconnectRequested, this));
}
- if (status_service_)
- status_service_->SetHostIsUp(host_id_);
-
host_->Start(xmpp_login_);
CreateAuthenticatorFactory();
@@ -997,8 +984,6 @@ void HostProcess::ShutdownHost(int exit_code) {
case HOST_STARTED:
state_ = HOST_STOPPING;
- if (status_service_)
- status_service_->SetHostIsDown();
host_->Shutdown(base::Bind(&HostProcess::ShutdownOnNetworkThread, this));
break;
@@ -1038,7 +1023,6 @@ void HostProcess::ShutdownOnNetworkThread() {
}
config_watcher_.reset();
- status_service_.reset();
// Complete the rest of shutdown on the main thread.
context_->ui_task_runner()->PostTask(
diff --git a/remoting/host/websocket_connection.cc b/remoting/host/websocket_connection.cc
deleted file mode 100644
index 02394ac..0000000
--- a/remoting/host/websocket_connection.cc
+++ /dev/null
@@ -1,495 +0,0 @@
-// Copyright (c) 2012 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 "remoting/host/websocket_connection.h"
-
-#include <map>
-#include <vector>
-
-#include "base/base64.h"
-#include "base/compiler_specific.h"
-#include "base/location.h"
-#include "base/sha1.h"
-#include "base/single_thread_task_runner.h"
-#include "base/string_split.h"
-#include "base/sys_byteorder.h"
-#include "base/thread_task_runner_handle.h"
-#include "net/base/net_errors.h"
-#include "net/socket/stream_socket.h"
-
-namespace remoting {
-
-namespace {
-
-const int kReadBufferSize = 1024;
-const char kLineSeparator[] = "\r\n";
-const char kHeaderEndMarker[] = "\r\n\r\n";
-const char kHeaderKeyValueSeparator[] = ": ";
-const int kMaskLength = 4;
-
-// Maximum frame length that can be encoded without extended length filed.
-const uint32 kMaxNotExtendedLength = 125;
-
-// Maximum frame length that can be encoded in 16 bits.
-const uint32 kMax16BitLength = 65535;
-
-// Special values of the length field used to extend frame length to 16 or 64
-// bits.
-const uint32 kLength16BitMarker = 126;
-const uint32 kLength64BitMarker = 127;
-
-// Fixed value specified in RFC6455. It's used to compute accept token sent to
-// the client in Sec-WebSocket-Accept key.
-const char kWebsocketKeySalt[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
-
-} // namespace
-
-WebSocketConnection::WebSocketConnection()
- : delegate_(NULL),
- maximum_message_size_(0),
- state_(READING_HEADERS),
- receiving_message_(false),
- ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
-}
-
-WebSocketConnection::~WebSocketConnection() {
- Close();
-}
-
-void WebSocketConnection::Start(
- scoped_ptr<net::StreamSocket> socket,
- ConnectedCallback connected_callback) {
- socket_ = socket.Pass();
- connected_callback_ = connected_callback;
- reader_.Init(socket_.get(), base::Bind(
- &WebSocketConnection::OnSocketReadResult, base::Unretained(this)));
- writer_.Init(socket_.get(), base::Bind(
- &WebSocketConnection::OnSocketWriteError, base::Unretained(this)));
-}
-
-void WebSocketConnection::Accept(Delegate* delegate) {
- DCHECK_EQ(state_, HEADERS_READ);
-
- state_ = ACCEPTED;
- delegate_ = delegate;
-
- std::string accept_key =
- base::SHA1HashString(websocket_key_ + kWebsocketKeySalt);
- std::string accept_key_base64;
- bool result = base::Base64Encode(accept_key, &accept_key_base64);
- DCHECK(result);
-
- std::string handshake;
- handshake += "HTTP/1.1 101 Switching Protocol";
- handshake += kLineSeparator;
- handshake += "Upgrade: websocket";
- handshake += kLineSeparator;
- handshake += "Connection: Upgrade";
- handshake += kLineSeparator;
- handshake += "Sec-WebSocket-Accept: " + accept_key_base64;
- handshake += kHeaderEndMarker;
-
- scoped_refptr<net::IOBufferWithSize> buffer =
- new net::IOBufferWithSize(handshake.size());
- memcpy(buffer->data(), handshake.data(), handshake.size());
- writer_.Write(buffer, base::Closure());
-}
-
-void WebSocketConnection::Reject() {
- DCHECK_EQ(state_, HEADERS_READ);
-
- state_ = CLOSED;
- std::string response = "HTTP/1.1 401 Unauthorized";
- response += kHeaderEndMarker;
- scoped_refptr<net::IOBufferWithSize> buffer =
- new net::IOBufferWithSize(response.size());
- memcpy(buffer->data(), response.data(), response.size());
- writer_.Write(buffer, base::Closure());
-}
-
-void WebSocketConnection::set_maximum_message_size(uint64 size) {
- maximum_message_size_ = size;
-}
-
-void WebSocketConnection::SendText(const std::string& text) {
- SendFragment(OPCODE_TEXT_FRAME, text);
-}
-
-void WebSocketConnection::Close() {
- switch (state_) {
- case READING_HEADERS:
- break;
-
- case HEADERS_READ:
- Reject();
- break;
-
- case ACCEPTED:
- SendFragment(OPCODE_CLOSE, std::string());
- break;
-
- case CLOSED:
- break;
- }
- state_ = CLOSED;
-}
-
-void WebSocketConnection::CloseOnError() {
- State old_state_ = state_;
- Close();
- if (old_state_ == ACCEPTED) {
- DCHECK(delegate_);
- delegate_->OnWebSocketClosed();
- }
-}
-
-void WebSocketConnection::OnSocketReadResult(scoped_refptr<net::IOBuffer> data,
- int result) {
- if (result <= 0) {
- if (result != 0) {
- LOG(ERROR) << "Error when trying to read from WebSocket connection: "
- << result;
- }
- CloseOnError();
- return;
- }
-
- switch (state_) {
- case READING_HEADERS: {
- headers_.append(data->data(), data->data() + result);
- size_t header_end_pos = headers_.find(kHeaderEndMarker);
- if (header_end_pos != std::string::npos) {
- bool result;
- if (header_end_pos != headers_.size() - strlen(kHeaderEndMarker)) {
- LOG(ERROR) << "WebSocket client tried writing data before handshake "
- "has finished.";
- DCHECK(!connected_callback_.is_null());
- state_ = CLOSED;
- result = false;
- } else {
- // Crop newline symbols from the end.
- headers_.resize(header_end_pos);
-
- result = ParseHeaders();
- if (!result) {
- state_ = CLOSED;
- } else {
- state_ = HEADERS_READ;
- }
- }
- ConnectedCallback cb(connected_callback_);
- connected_callback_.Reset();
- cb.Run(result);
- }
- break;
- }
-
- case HEADERS_READ:
- LOG(ERROR) << "Received unexpected data before websocket "
- "connection is accepted.";
- CloseOnError();
- break;
-
- case ACCEPTED:
- DCHECK(delegate_);
- received_data_.append(data->data(), data->data() + result);
- ProcessData();
-
- case CLOSED:
- // Ignore anything received after connection is rejected or closed.
- break;
- }
-}
-
-void WebSocketConnection::ProcessData() {
- DCHECK_EQ(state_, ACCEPTED);
-
- if (received_data_.size() < 2) {
- // Header hasn't been received yet.
- return;
- }
-
- bool fin_bit = (received_data_.data()[0] & 0x80) != 0;
-
- // 3 bits after FIN are reserved for WebSocket extensions. RFC6455 requires
- // that endpoint fails connection if any of these bits is set while no
- // extension that uses these bits was negotiated.
- int rsv_bits = received_data_.data()[0] & 0x70;
- if (rsv_bits != 0) {
- LOG(ERROR) << "Incoming has unsupported RSV bits set.";
- CloseOnError();
- return;
- }
-
- int opcode = received_data_.data()[0] & 0x0f;
-
- int mask_bit = received_data_.data()[1] & 0x80;
- if (mask_bit == 0) {
- LOG(ERROR) << "Incoming frame is not masked.";
- CloseOnError();
- return;
- }
-
- // Length field has variable size in each WebSocket frame - it's either 1, 3
- // or 9 bytes with the first bit always reserved for MASK flag. The first byte
- // is set to 126 or 127 for 16 and 64 bit extensions respectively. Code below
- // extracts |length| value and sets |length_field_size| accordingly.
- int length_field_size = 1;
- uint64 length = received_data_.data()[1] & 0x7F;
- if (length == kLength16BitMarker) {
- if (received_data_.size() < 4) {
- // Haven't received the whole frame header yet.
- return;
- }
- length_field_size = 3;
- length = base::NetToHost16(
- *reinterpret_cast<const uint16*>(received_data_.data() + 2));
- } else if (length == kLength64BitMarker) {
- if (received_data_.size() < 10) {
- // Haven't received the whole frame header yet.
- return;
- }
- length_field_size = 9;
- length = base::NetToHost64(
- *reinterpret_cast<const uint64*>(received_data_.data() + 2));
- }
-
- int payload_position = 1 + length_field_size + kMaskLength;
-
- // Check that the size of the frame is below the limit. It needs to be done
- // before we read the payload to avoid allocating buffer for a bogus frame
- // that is too big.
- if (maximum_message_size_ > 0 && length > maximum_message_size_) {
- LOG(ERROR) << "Client tried to send a fragment that is bigger than "
- "the maximum message size of " << maximum_message_size_;
- CloseOnError();
- return;
- }
-
- if (received_data_.size() < payload_position + length) {
- // Haven't received the whole frame yet.
- return;
- }
-
- // Unmask the payload.
- if (mask_bit) {
- const char* mask = received_data_.data() + length_field_size + 1;
- UnmaskPayload(
- mask,
- const_cast<char*>(received_data_.data()) + payload_position, length);
- }
-
- const char* payload = received_data_.data() + payload_position;
-
- if (opcode < 0x8) {
- if (maximum_message_size_ > 0 &&
- current_message_.size() + length > maximum_message_size_) {
- LOG(ERROR) << "Client tried to send a message that is bigger than "
- "the maximum message size of " << maximum_message_size_;
- CloseOnError();
- return;
- }
-
- // Non-control message.
- current_message_.append(payload, payload + length);
- } else {
- // Control message.
- if (!fin_bit) {
- LOG(ERROR) << "Received fragmented control message.";
- CloseOnError();
- return;
- }
- if (length > kMaxNotExtendedLength) {
- LOG(ERROR) << "Received control message that is larger than 125 bytes.";
- CloseOnError();
- return;
- }
- }
-
- switch (opcode) {
- case OPCODE_CONTINUATION:
- if (!receiving_message_) {
- LOG(ERROR) << "Received unexpected continuation frame.";
- CloseOnError();
- return;
- }
- break;
-
- case OPCODE_TEXT_FRAME:
- case OPCODE_BINARY_FRAME:
- if (receiving_message_) {
- LOG(ERROR) << "Received unexpected new start frame in a middle of "
- "a message.";
- CloseOnError();
- return;
- }
- break;
-
- case OPCODE_CLOSE:
- Close();
- delegate_->OnWebSocketClosed();
- return;
-
- case OPCODE_PING:
- SendFragment(OPCODE_PONG, std::string(payload, payload + length));
- break;
-
- case OPCODE_PONG:
- break;
-
- default:
- LOG(ERROR) << "Received invalid opcode: " << opcode;
- CloseOnError();
- return;
- }
-
- // Remove the frame from |received_data_|.
- received_data_.erase(0, payload_position + length);
-
- // Post a task to process the data left in the buffer, if any.
- if (!received_data_.empty()) {
- base::ThreadTaskRunnerHandle::Get()->PostTask(
- FROM_HERE, base::Bind(&WebSocketConnection::ProcessData,
- weak_factory_.GetWeakPtr()));
- }
-
- // Handle payload in non-control messages. Delegate can be called only at the
- // end of this function
- if (opcode < 0x8) {
- if (!fin_bit) {
- receiving_message_ = true;
- } else {
- receiving_message_ = false;
- std::string msg;
- msg.swap(current_message_);
- delegate_->OnWebSocketMessage(msg);
- }
- }
-}
-
-void WebSocketConnection::SendFragment(WebsocketOpcode opcode,
- const std::string& payload) {
- DCHECK_EQ(state_, ACCEPTED);
-
- int length_field_size = 1;
- if (payload.size() > kMax16BitLength) {
- length_field_size = 9;
- } else if (payload.size() > kMaxNotExtendedLength) {
- length_field_size = 3;
- }
-
- scoped_refptr<net::IOBufferWithSize> buffer =
- new net::IOBufferWithSize(1 + length_field_size + payload.size());
-
- // Always set FIN flag because we never fragment outgoing messages.
- buffer->data()[0] = opcode | 0x80;
-
- if (payload.size() > kMax16BitLength) {
- uint64 size = base::HostToNet64(payload.size());
- buffer->data()[1] = kLength64BitMarker;
- memcpy(buffer->data() + 2, reinterpret_cast<char*>(&size), sizeof(size));
- } else if (payload.size() > kMaxNotExtendedLength) {
- uint16 size = base::HostToNet16(payload.size());
- buffer->data()[1] = kLength16BitMarker;
- memcpy(buffer->data() + 2, reinterpret_cast<char*>(&size), sizeof(size));
- } else {
- buffer->data()[1] = payload.size();
- }
- memcpy(buffer->data() + 1 + length_field_size,
- payload.data(), payload.size());
-
- writer_.Write(buffer, base::Closure());
-}
-
-bool WebSocketConnection::ParseHeaders() {
- std::vector<std::string> lines;
- base::SplitStringUsingSubstr(headers_, kLineSeparator, &lines);
-
- // Parse request line.
- std::vector<std::string> request_parts;
- base::SplitString(lines[0], ' ', &request_parts);
- if (request_parts.size() != 3 ||
- request_parts[0] != "GET" ||
- request_parts[2] != "HTTP/1.1") {
- LOG(ERROR) << "Invalid Request-Line: " << headers_[0];
- return false;
- }
- request_path_ = request_parts[1];
-
- std::map<std::string, std::string> headers;
-
- for (size_t i = 1; i < lines.size(); ++i) {
- std::string separator(kHeaderKeyValueSeparator);
- size_t pos = lines[i].find(separator);
- if (pos == std::string::npos || pos == 0) {
- LOG(ERROR) << "Invalid header line: " << lines[i];
- return false;
- }
- std::string key = lines[i].substr(0, pos);
- if (headers.find(key) != headers.end()) {
- LOG(ERROR) << "Duplicate header value: " << key;
- return false;
- }
- headers[key] = lines[i].substr(pos + separator.size());
- }
-
- std::map<std::string, std::string>::iterator it = headers.find("Connection");
- if (it == headers.end() || it->second != "Upgrade") {
- LOG(ERROR) << "Connection header is missing or invalid.";
- return false;
- }
-
- it = headers.find("Upgrade");
- if (it == headers.end() || it->second != "websocket") {
- LOG(ERROR) << "Upgrade header is missing or invalid.";
- return false;
- }
-
- it = headers.find("Host");
- if (it == headers.end()) {
- LOG(ERROR) << "Host header is missing.";
- return false;
- }
- request_host_ = it->second;
-
- it = headers.find("Sec-WebSocket-Version");
- if (it == headers.end()) {
- LOG(ERROR) << "Sec-WebSocket-Version header is missing.";
- return false;
- }
- if (it->second != "13") {
- LOG(ERROR) << "Unsupported WebSocket protocol version: " << it->second;
- return false;
- }
-
- it = headers.find("Origin");
- if (it == headers.end()) {
- LOG(ERROR) << "Origin header is missing.";
- return false;
- }
- origin_ = it->second;
-
- it = headers.find("Sec-WebSocket-Key");
- if (it == headers.end()) {
- LOG(ERROR) << "Sec-WebSocket-Key header is missing.";
- return false;
- }
- websocket_key_ = it->second;
-
- return true;
-}
-
-void WebSocketConnection::UnmaskPayload(const char* mask,
- char* payload, int payload_length) {
- for (int i = 0; i < payload_length; ++i) {
- payload[i] = payload[i] ^ mask[i % kMaskLength];
- }
-}
-
-void WebSocketConnection::OnSocketWriteError(int error) {
- LOG(ERROR) << "Failed to write to a WebSocket. Error: " << error;
- CloseOnError();
-}
-
-} // namespace remoting
diff --git a/remoting/host/websocket_connection.h b/remoting/host/websocket_connection.h
deleted file mode 100644
index 762cfb0..0000000
--- a/remoting/host/websocket_connection.h
+++ /dev/null
@@ -1,139 +0,0 @@
-// Copyright (c) 2012 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.
-
-#ifndef REMOTING_HOST_WEBSOCKET_CONNECTION_H_
-#define REMOTING_HOST_WEBSOCKET_CONNECTION_H_
-
-#include <string>
-
-#include "base/callback.h"
-#include "base/memory/ref_counted.h"
-#include "base/memory/scoped_ptr.h"
-#include "base/memory/weak_ptr.h"
-#include "remoting/base/socket_reader.h"
-#include "remoting/protocol/buffered_socket_writer.h"
-
-namespace net {
-class StreamSocket;
-} // namespace net
-
-namespace remoting {
-
-class WebSocketConnection {
- public:
- typedef base::Callback<void (bool connected)> ConnectedCallback;
- class Delegate {
- public:
- virtual void OnWebSocketMessage(const std::string& message) = 0;
- virtual void OnWebSocketClosed() = 0;
- };
-
- WebSocketConnection();
- virtual ~WebSocketConnection();
-
- // Initialize WebSocket connection for the specified |socket|.
- // |connected_callback| is called with |connected|=true after handshake
- // headers have been received from the client and parsed or with
- // |connected|=false if the connection failed (e.g. headers were invalid).
- void Start(scoped_ptr<net::StreamSocket> socket,
- ConnectedCallback connected_callback);
-
- // Connection parameters received in the header. Valid only once Start() has
- // successfully completed.
- const std::string& request_path() { return request_path_; }
- const std::string& request_host() { return request_host_; }
- const std::string& origin() { return origin_; }
-
- // Accept or Reject new connection. Accept() or Reject() must be called once
- // after Start() completes successfully.
- void Accept(Delegate* delegate);
- void Reject();
-
- // Sets the maximum incoming message size to allow. The connection will be
- // closed if a message exceeding this size is received. |size| may be 0 to
- // allow any message size. By default size of incoming messages is unlimited.
- void set_maximum_message_size(uint64 size);
-
- // Send the specified text message. Sending data messages is not supported.
- void SendText(const std::string& text);
-
- // Closes the connection and sends Close frame to the client if necessary.
- void Close();
-
- private:
- enum State {
- READING_HEADERS,
- HEADERS_READ,
- ACCEPTED,
- CLOSED,
- };
-
- enum WebsocketOpcode {
- OPCODE_CONTINUATION = 0,
- OPCODE_TEXT_FRAME = 1,
- OPCODE_BINARY_FRAME = 2,
- OPCODE_CLOSE = 8,
- OPCODE_PING = 9,
- OPCODE_PONG = 10,
- };
-
- // Closes the socket in response to a protocol error, and notifies
- // Delegate::OnWebSocketClosed().
- void CloseOnError();
-
- // Result handler for |reader_|.
- void OnSocketReadResult(scoped_refptr<net::IOBuffer> data, int size);
-
- // Parses websocket headers in |header_| and returns false when headers are
- // invalid.
- bool ParseHeaders();
-
- // Parses incoming data in |received_data_| and dispatches delegate calls when
- // a new message is received.
- void ProcessData();
-
- // Error handler for |writer_|.
- void OnSocketWriteError(int error);
-
- // Sends outgoing frame with the specified |opcode| and |payload|.
- void SendFragment(WebsocketOpcode opcode, const std::string& payload);
-
- // Unmasks fragment |payload| using specified |mask|
- void UnmaskPayload(const char* mask, char* payload, int payload_length);
-
- scoped_ptr<net::StreamSocket> socket_;
- ConnectedCallback connected_callback_;
- Delegate* delegate_;
-
- uint64 maximum_message_size_;
-
- SocketReader reader_;
- protocol::BufferedSocketWriter writer_;
-
- State state_;
-
- std::string headers_;
-
- // Header fields. Set in ParseHeaders().
- std::string request_path_;
- std::string request_host_;
- std::string origin_;
- std::string websocket_key_;
-
- // Raw data that has been received but hasn't been parsed.
- std::string received_data_;
-
- // When receiving a fragmented message |receiving_message_| is set to true and
- // |current_message_| contains the fragments that we've already received.
- bool receiving_message_;
- std::string current_message_;
-
- base::WeakPtrFactory<WebSocketConnection> weak_factory_;
-
- DISALLOW_COPY_AND_ASSIGN(WebSocketConnection);
-};
-
-} // namespace remoting
-
-#endif // REMOTING_HOST_WEBSOCKET_CONNECTION_H_
diff --git a/remoting/host/websocket_connection_unittest.cc b/remoting/host/websocket_connection_unittest.cc
deleted file mode 100644
index 9a2ec58..0000000
--- a/remoting/host/websocket_connection_unittest.cc
+++ /dev/null
@@ -1,333 +0,0 @@
-// Copyright (c) 2012 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 "remoting/host/websocket_connection.h"
-
-#include "base/message_loop.h"
-#include "base/run_loop.h"
-#include "net/base/ip_endpoint.h"
-#include "net/base/net_errors.h"
-#include "net/base/net_util.h"
-#include "net/socket/tcp_client_socket.h"
-#include "remoting/base/socket_reader.h"
-#include "remoting/host/websocket_listener.h"
-#include "testing/gmock/include/gmock/gmock.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace remoting {
-
-namespace {
-
-const int kPortRangeMin = 12800;
-const int kPortRangeMax = 12810;
-const char kHeaderEndMarker[] = "\r\n\r\n";
-
-const uint8 kHelloMessage[] = { 0x81, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f,
- 0x9f, 0x4d, 0x51, 0x58 };
-const char kHelloText[] = "Hello";
-
-} // namespace
-
-class WebSocketTestReader {
- public:
- WebSocketTestReader(net::Socket* socket, const base::Closure& on_data)
- : on_data_(on_data),
- closed_(false),
- reading_header_(true) {
- reader_.Init(socket, base::Bind(&WebSocketTestReader::OnReadResult,
- base::Unretained(this)));
- }
- ~WebSocketTestReader() {
- }
-
- bool closed() { return closed_; }
-
- bool reading_header() { return reading_header_; }
- const std::string& header() { return header_; }
- const std::string& data_receved() { return data_receved_; }
-
- protected:
- void OnReadResult(scoped_refptr<net::IOBuffer> buffer, int result) {
- if (result <= 0) {
- closed_ = true;
- } else if (reading_header_) {
- header_.append(buffer->data(), buffer->data() + result);
- size_t end_pos = header_.find(kHeaderEndMarker);
- if (end_pos != std::string::npos) {
- data_receved_ = header_.substr(end_pos + strlen(kHeaderEndMarker));
- header_ = header_.substr(0, end_pos);
- reading_header_ = false;
- }
- } else {
- data_receved_.append(buffer->data(), buffer->data() + result);
- }
- on_data_.Run();
- }
-
- private:
- SocketReader reader_;
- base::Closure on_data_;
- bool closed_;
- bool reading_header_;
- std::string header_;
- std::string data_receved_;
-};
-
-class WebSocketConnectionTest : public testing::Test,
- public WebSocketConnection::Delegate {
- public:
- virtual void OnWebSocketMessage(const std::string& message) OVERRIDE {
- last_message_ = message;
- if (message_run_loop_.get()) {
- message_run_loop_->Quit();
- }
- }
-
- virtual void OnWebSocketClosed() OVERRIDE {
- connection_.reset();
- if (closed_run_loop_.get()) {
- closed_run_loop_->Quit();
- }
- }
-
- protected:
- void Initialize() {
- listener_.reset(new WebSocketListener());
- net::IPAddressNumber localhost;
- ASSERT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &localhost));
- for (int port = kPortRangeMin; port < kPortRangeMax; ++port) {
- endpoint_ = net::IPEndPoint(localhost, port);
- if (listener_->Listen(
- endpoint_, base::Bind(&WebSocketConnectionTest::OnNewConnection,
- base::Unretained(this))) == net::OK) {
- return;
- }
- }
- }
-
- void OnNewConnection(scoped_ptr<WebSocketConnection> connection) {
- EXPECT_TRUE(connection_.get() == NULL);
- connection_ = connection.Pass();
- connection_->Accept(this);
- }
-
- void ConnectSocket() {
- client_.reset(new net::TCPClientSocket(
- net::AddressList(endpoint_), NULL, net::NetLog::Source()));
- client_connect_result_ = -1;
- client_->Connect(base::Bind(&WebSocketConnectionTest::OnClientConnected,
- base::Unretained(this)));
- connect_run_loop_.reset(new base::RunLoop());
- connect_run_loop_->Run();
- ASSERT_EQ(client_connect_result_, 0);
- client_writer_.reset(new protocol::BufferedSocketWriter());
- client_writer_->Init(
- client_.get(), protocol::BufferedSocketWriter::WriteFailedCallback());
- client_reader_.reset(new WebSocketTestReader(
- client_.get(),
- base::Bind(&WebSocketConnectionTest::OnClientDataReceived,
- base::Unretained(this))));
- }
-
- void OnClientConnected(int result) {
- client_connect_result_ = result;
- connect_run_loop_->Quit();
- }
-
- void OnClientDataReceived() {
- if (handshake_run_loop_.get() && !client_reader_->reading_header()) {
- handshake_run_loop_->Quit();
- }
- if (closed_run_loop_.get() && client_reader_->closed()) {
- closed_run_loop_->Quit();
- }
- if (data_received_run_loop_.get()) {
- data_received_run_loop_->Quit();
- }
- }
-
- void Send(const std::string& data) {
- scoped_refptr<net::IOBufferWithSize> buffer =
- new net::IOBufferWithSize(data.size());
- memcpy(buffer->data(), data.data(), data.size());
- client_writer_->Write(buffer, base::Closure());
- }
-
- void Handshake() {
- Send("GET /chat HTTP/1.1\r\n"
- "Host: server.example.com\r\n"
- "Upgrade: websocket\r\n"
- "Connection: Upgrade\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Origin: http://example.com\r\n"
- "Sec-WebSocket-Version: 13\r\n\r\n");
- handshake_run_loop_.reset(new base::RunLoop());
- handshake_run_loop_->Run();
- EXPECT_EQ("http://example.com", connection_->origin());
- EXPECT_EQ("server.example.com", connection_->request_host());
- EXPECT_EQ("/chat", connection_->request_path());
- EXPECT_EQ("HTTP/1.1 101 Switching Protocol\r\n"
- "Upgrade: websocket\r\n"
- "Connection: Upgrade\r\n"
- "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
- client_reader_->header());
- }
-
- void ReceiveFrame(std::string expected_frame) {
- while (client_reader_->data_receved().size() < expected_frame.size()) {
- data_received_run_loop_.reset(new base::RunLoop());
- data_received_run_loop_->Run();
- }
- EXPECT_EQ(expected_frame, client_reader_->data_receved());
- }
-
- MessageLoopForIO message_loop_;
-
- scoped_ptr<base::RunLoop> connect_run_loop_;
- scoped_ptr<base::RunLoop> handshake_run_loop_;
- scoped_ptr<base::RunLoop> closed_run_loop_;
- scoped_ptr<base::RunLoop> data_received_run_loop_;
- scoped_ptr<base::RunLoop> message_run_loop_;
-
- scoped_ptr<WebSocketListener> listener_;
- net::IPEndPoint endpoint_;
- scoped_ptr<WebSocketConnection> connection_;
- scoped_ptr<net::TCPClientSocket> client_;
- scoped_ptr<protocol::BufferedSocketWriter> client_writer_;
- scoped_ptr<WebSocketTestReader> client_reader_;
- int client_connect_result_;
- std::string last_message_;
-};
-
-TEST_F(WebSocketConnectionTest, ConnectSocket) {
- ASSERT_NO_FATAL_FAILURE(Initialize());
- ASSERT_NO_FATAL_FAILURE(ConnectSocket());
-}
-
-TEST_F(WebSocketConnectionTest, SuccessfulHandshake) {
- ASSERT_NO_FATAL_FAILURE(Initialize());
- ASSERT_NO_FATAL_FAILURE(ConnectSocket());
- ASSERT_NO_FATAL_FAILURE(Handshake());
- ASSERT_TRUE(connection_.get() != NULL);
-}
-
-TEST_F(WebSocketConnectionTest, ConnectAndReconnect) {
- ASSERT_NO_FATAL_FAILURE(Initialize());
- ASSERT_NO_FATAL_FAILURE(ConnectSocket());
- ASSERT_NO_FATAL_FAILURE(Handshake());
- ASSERT_TRUE(connection_.get() != NULL);
-
- client_.reset();
- closed_run_loop_.reset(new base::RunLoop());
- closed_run_loop_->Run();
- EXPECT_TRUE(connection_.get() == NULL);
-
- ASSERT_NO_FATAL_FAILURE(ConnectSocket());
- ASSERT_NO_FATAL_FAILURE(Handshake());
- ASSERT_TRUE(connection_.get() != NULL);
-}
-
-TEST_F(WebSocketConnectionTest, CloseFrameIsSent) {
- ASSERT_NO_FATAL_FAILURE(Initialize());
- ASSERT_NO_FATAL_FAILURE(ConnectSocket());
- ASSERT_NO_FATAL_FAILURE(Handshake());
-
- connection_->Close();
-
- // Expect to receive Close frame.
- uint8 expected_frame[] = { 0x88, 0x00 };
- ReceiveFrame(std::string(expected_frame,
- expected_frame + sizeof(expected_frame)));
-}
-
-TEST_F(WebSocketConnectionTest, ConnectFailsOnNonWebSocketHeader) {
- ASSERT_NO_FATAL_FAILURE(Initialize());
- ASSERT_NO_FATAL_FAILURE(ConnectSocket());
- EXPECT_TRUE(connection_.get() == NULL);
- Send("GET /chat HTTP/1.1\r\n"
- "Host: server.example.com\r\n\r\n");
- closed_run_loop_.reset(new base::RunLoop());
- closed_run_loop_->Run();
- EXPECT_TRUE(connection_.get() == NULL);
-}
-
-TEST_F(WebSocketConnectionTest, SendMessage) {
- ASSERT_NO_FATAL_FAILURE(Initialize());
- ASSERT_NO_FATAL_FAILURE(ConnectSocket());
- ASSERT_NO_FATAL_FAILURE(Handshake());
- connection_->SendText(kHelloText);
- uint8 expected_frame[] = { 0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f };
- ReceiveFrame(std::string(expected_frame,
- expected_frame + sizeof(expected_frame)));
-}
-
-TEST_F(WebSocketConnectionTest, ReceiveMessage) {
- ASSERT_NO_FATAL_FAILURE(Initialize());
- ASSERT_NO_FATAL_FAILURE(ConnectSocket());
- ASSERT_NO_FATAL_FAILURE(Handshake());
- uint8 kHelloMessage[] = { 0x81, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f,
- 0x9f, 0x4d, 0x51, 0x58 };
- Send(std::string(kHelloMessage, kHelloMessage + sizeof(kHelloMessage)));
-
- message_run_loop_.reset(new base::RunLoop());
- message_run_loop_->Run();
-
- EXPECT_EQ(kHelloText, last_message_);
-}
-
-TEST_F(WebSocketConnectionTest, FragmentedMessage) {
- ASSERT_NO_FATAL_FAILURE(Initialize());
- ASSERT_NO_FATAL_FAILURE(ConnectSocket());
- ASSERT_NO_FATAL_FAILURE(Handshake());
-
- uint8 fragment1[] = { 0x01, 0x83, 0x37, 0xfa, 0x21, 0x3d, 0x7f, 0x9f, 0x4d };
- Send(std::string(fragment1, fragment1 + sizeof(fragment1)));
- uint8 fragment2[] = { 0x80, 0x82, 0x3d, 0x37, 0x12, 0x42, 0x51, 0x58 };
- Send(std::string(fragment2, fragment2 + sizeof(fragment2)));
-
- message_run_loop_.reset(new base::RunLoop());
- message_run_loop_->Run();
-
- EXPECT_EQ(kHelloText, last_message_);
-}
-
-TEST_F(WebSocketConnectionTest, PingResponse) {
- ASSERT_NO_FATAL_FAILURE(Initialize());
- ASSERT_NO_FATAL_FAILURE(ConnectSocket());
- ASSERT_NO_FATAL_FAILURE(Handshake());
-
- uint8 ping_frame[] = { 0x89, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f, 0x9f,
- 0x4d, 0x51, 0x58 };
- Send(std::string(ping_frame, ping_frame + sizeof(ping_frame)));
-
- uint8 expected_frame[] = { 0x8a, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f };
- ReceiveFrame(std::string(expected_frame,
- expected_frame + sizeof(expected_frame)));
-}
-
-TEST_F(WebSocketConnectionTest, MessageSizeLimit) {
- ASSERT_NO_FATAL_FAILURE(Initialize());
- ASSERT_NO_FATAL_FAILURE(ConnectSocket());
- ASSERT_NO_FATAL_FAILURE(Handshake());
-
- connection_->set_maximum_message_size(strlen(kHelloText));
- Send(std::string(kHelloMessage, kHelloMessage + sizeof(kHelloMessage)));
-
- message_run_loop_.reset(new base::RunLoop());
- message_run_loop_->Run();
-
- EXPECT_EQ(kHelloText, last_message_);
-
- // Set lower message size limit and try sending the same message again.
- connection_->set_maximum_message_size(strlen(kHelloText) - 1);
-
- Send(std::string(kHelloMessage, kHelloMessage + sizeof(kHelloMessage)));
-
- // Expect to receive Close frame.
- uint8 expected_frame[] = { 0x88, 0x00 };
- ReceiveFrame(std::string(expected_frame,
- expected_frame + sizeof(expected_frame)));
-}
-
-} // namespace remoting
diff --git a/remoting/host/websocket_listener.cc b/remoting/host/websocket_listener.cc
deleted file mode 100644
index b552222..0000000
--- a/remoting/host/websocket_listener.cc
+++ /dev/null
@@ -1,96 +0,0 @@
-// Copyright (c) 2012 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 "remoting/host/websocket_listener.h"
-
-#include <vector>
-
-#include "base/compiler_specific.h"
-#include "base/location.h"
-#include "base/single_thread_task_runner.h"
-#include "base/stl_util.h"
-#include "base/sys_byteorder.h"
-#include "base/thread_task_runner_handle.h"
-#include "net/base/net_errors.h"
-#include "net/socket/stream_socket.h"
-#include "remoting/host/websocket_connection.h"
-
-namespace remoting {
-
-namespace {
-const int kTcpListenBacklog = 5;
-} // namespace
-
-WebSocketListener::WebSocketListener()
- : tcp_socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())),
- ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
-}
-
-WebSocketListener::~WebSocketListener() {
- STLDeleteElements(&pending_connections_);
-}
-
-int WebSocketListener::Listen(const net::IPEndPoint& address,
- const NewConnectionCallback& callback) {
- new_connection_callback_ = callback;
-
- int result = tcp_socket_->Listen(address, kTcpListenBacklog);
- if (result == net::OK) {
- DoAccept();
- }
-
- return result;
-}
-
-void WebSocketListener::DoAccept() {
- while (true) {
- int result = tcp_socket_->Accept(
- &accepted_socket_, base::Bind(&WebSocketListener::OnAccepted,
- base::Unretained(this)));
- if (result != net::ERR_IO_PENDING) {
- HandleAcceptResult(result);
- }
- if (result != net::OK) {
- break;
- }
- }
-}
-
-void WebSocketListener::OnAccepted(int result) {
- DCHECK_NE(result, net::ERR_IO_PENDING);
- if (result == net::OK) {
- // The call for WebSocketConnection::Start() may result in delegate being
- // called which is allowed to destroyed this object, so we need to post a
- // task to call DoAccept() again asynchronously.
- base::ThreadTaskRunnerHandle::Get()->PostTask(
- FROM_HERE, base::Bind(&WebSocketListener::DoAccept,
- weak_factory_.GetWeakPtr()));
- }
- HandleAcceptResult(result);
-}
-
-void WebSocketListener::HandleAcceptResult(int result) {
- if (result == net::OK) {
- WebSocketConnection* connection = new WebSocketConnection();
- pending_connections_.insert(connection);
- connection->Start(
- accepted_socket_.Pass(),
- base::Bind(&WebSocketListener::OnConnected, weak_factory_.GetWeakPtr(),
- connection));
- } else {
- LOG(ERROR) << "Error when trying to accept WebSocket connection: "
- << result;
- }
-}
-
-void WebSocketListener::OnConnected(WebSocketConnection* connection_ptr,
- bool handshake_result) {
- scoped_ptr<WebSocketConnection> connection(connection_ptr);
- pending_connections_.erase(connection_ptr);
- if (handshake_result) {
- new_connection_callback_.Run(connection.Pass());
- }
-}
-
-} // namespace remoting
diff --git a/remoting/host/websocket_listener.h b/remoting/host/websocket_listener.h
deleted file mode 100644
index dd8436c..0000000
--- a/remoting/host/websocket_listener.h
+++ /dev/null
@@ -1,69 +0,0 @@
-// Copyright (c) 2012 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.
-
-#ifndef REMOTING_HOST_WEBSOCKET_LISTENER_H_
-#define REMOTING_HOST_WEBSOCKET_LISTENER_H_
-
-#include <set>
-
-#include "base/memory/ref_counted.h"
-#include "base/memory/scoped_ptr.h"
-#include "base/memory/weak_ptr.h"
-#include "net/socket/tcp_server_socket.h"
-
-namespace net {
-class IPEndPoint;
-class StreamSocket;
-} // namespace net
-
-namespace remoting {
-
-class WebSocketConnection;
-
-class WebSocketListener {
- public:
- typedef base::Callback<
- void(scoped_ptr<WebSocketConnection> connection)> NewConnectionCallback;
-
- WebSocketListener();
- ~WebSocketListener();
-
- // Starts listening on the specified address and returns net::Error code in
- // case of a failure. When successful, the specified |callback| will be called
- // for each incoming until the listener is destroyed.
- int Listen(const net::IPEndPoint& address,
- const NewConnectionCallback& callback);
-
- private:
- // Calls ServerSocket::Accept() to accept new connections.
- void DoAccept();
-
- // Callback for ServerSocket::Accept().
- void OnAccepted(int result);
-
- // Handles Accept() result from DoAccept() or OnAccepted().
- void HandleAcceptResult(int result);
-
- // Callback for WebSocketConnection::Start().
- void OnConnected(WebSocketConnection* connection_ptr, bool handshake_result);
-
- scoped_ptr<net::TCPServerSocket> tcp_socket_;
- NewConnectionCallback new_connection_callback_;
-
- // Object passed to Accept() when accepting incoming connections.
- scoped_ptr<net::StreamSocket> accepted_socket_;
-
- // Incoming connections that have been accepted, but for which we haven't
- // received headers yet and haven't called |new_connection_callback_|. They
- // are owned by the listener until we receive headers and call the callback.
- std::set<WebSocketConnection*> pending_connections_;
-
- base::WeakPtrFactory<WebSocketListener> weak_factory_;
-
- DISALLOW_COPY_AND_ASSIGN(WebSocketListener);
-};
-
-} // namespace remoting
-
-#endif // REMOTING_HOST_WEBSOCKET_LISTENER_H_
diff --git a/remoting/remoting.gyp b/remoting/remoting.gyp
index 216a855..e4616d0 100644
--- a/remoting/remoting.gyp
+++ b/remoting/remoting.gyp
@@ -346,8 +346,6 @@
'host/host_secret.cc',
'host/host_secret.h',
'host/host_status_observer.h',
- 'host/host_status_service.cc',
- 'host/host_status_service.h',
'host/host_user_interface.cc',
'host/host_user_interface.h',
'host/in_memory_host_config.cc',
@@ -436,10 +434,6 @@
'host/video_scheduler.h',
'host/vlog_net_log.cc',
'host/vlog_net_log.h',
- 'host/websocket_connection.cc',
- 'host/websocket_connection.h',
- 'host/websocket_listener.cc',
- 'host/websocket_listener.h',
'host/win/desktop.cc',
'host/win/desktop.h',
'host/win/launch_process_with_token.cc',
@@ -2218,7 +2212,6 @@
'host/video_frame_capturer_mac_unittest.cc',
'host/video_frame_capturer_unittest.cc',
'host/video_scheduler_unittest.cc',
- 'host/websocket_connection_unittest.cc',
'host/win/launch_process_with_token.cc',
'host/win/launch_process_with_token.h',
'host/win/worker_process_launcher.cc',