Commit 14014dc1 authored by nyquist's avatar nyquist Committed by Commit bot

Move more client network code to //blimp/client/core/session.

The BlimpClientSession will be going away, so this CL takes part of it
and splits it up into smaller parts and moves them to
//blimp/client/core/session. They will be used by the
BlimpClientContextImpl.

BUG=611097

Review-Url: https://codereview.chromium.org/2187893002
Cr-Commit-Position: refs/heads/master@{#408802}
parent e001515d
...@@ -16,10 +16,16 @@ source_set("session") { ...@@ -16,10 +16,16 @@ source_set("session") {
sources = [ sources = [
"assignment_source.cc", "assignment_source.cc",
"assignment_source.h", "assignment_source.h",
"client_network_components.cc",
"client_network_components.h",
"cross_thread_network_event_observer.cc",
"cross_thread_network_event_observer.h",
"network_event_observer.h",
] ]
public_deps = [ public_deps = [
"//base", "//base",
"//blimp/net",
"//components/safe_json", "//components/safe_json",
"//net", "//net",
"//url", "//url",
......
// Copyright 2016 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 "blimp/client/core/session/client_network_components.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "blimp/net/ssl_client_transport.h"
#include "net/base/net_errors.h"
namespace blimp {
namespace client {
ClientNetworkComponents::ClientNetworkComponents(
std::unique_ptr<NetworkEventObserver> network_observer,
std::unique_ptr<BlimpConnectionStatistics> statistics)
: connection_handler_(),
network_observer_(std::move(network_observer)),
connection_statistics_(std::move(statistics)) {
DCHECK(connection_statistics_);
}
ClientNetworkComponents::~ClientNetworkComponents() {
DCHECK(io_thread_checker_.CalledOnValidThread());
}
void ClientNetworkComponents::Initialize() {
io_thread_checker_.DetachFromThread();
DCHECK(!connection_manager_);
connection_manager_ = base::MakeUnique<ClientConnectionManager>(this);
}
void ClientNetworkComponents::ConnectWithAssignment(
const Assignment& assignment) {
DCHECK(io_thread_checker_.CalledOnValidThread());
DCHECK(connection_manager_);
connection_manager_->set_client_token(assignment.client_token);
const char* transport_type = "UNKNOWN";
switch (assignment.transport_protocol) {
case Assignment::SSL:
DCHECK(assignment.cert);
connection_manager_->AddTransport(base::MakeUnique<SSLClientTransport>(
assignment.engine_endpoint, std::move(assignment.cert),
connection_statistics_.get(), nullptr));
transport_type = "SSL";
break;
case Assignment::TCP:
connection_manager_->AddTransport(base::MakeUnique<TCPClientTransport>(
assignment.engine_endpoint, connection_statistics_.get(), nullptr));
transport_type = "TCP";
break;
case Assignment::UNKNOWN:
LOG(FATAL) << "Unknown transport type.";
break;
}
VLOG(1) << "Connecting to " << assignment.engine_endpoint.ToString() << " ("
<< transport_type << ")";
connection_manager_->Connect();
}
BrowserConnectionHandler*
ClientNetworkComponents::GetBrowserConnectionHandler() {
DCHECK(io_thread_checker_.CalledOnValidThread());
return &connection_handler_;
}
void ClientNetworkComponents::HandleConnection(
std::unique_ptr<BlimpConnection> connection) {
DCHECK(io_thread_checker_.CalledOnValidThread());
VLOG(1) << "Connection established.";
connection->AddConnectionErrorObserver(this);
connection_handler_.HandleConnection(std::move(connection));
network_observer_->OnConnected();
}
void ClientNetworkComponents::OnConnectionError(int result) {
DCHECK(io_thread_checker_.CalledOnValidThread());
if (result >= 0) {
VLOG(1) << "Disconnected with reason: " << result;
} else {
VLOG(1) << "Connection error: " << net::ErrorToString(result);
}
network_observer_->OnDisconnected(result);
}
} // namespace client
} // namespace blimp
// Copyright 2016 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 BLIMP_CLIENT_CORE_SESSION_CLIENT_NETWORK_COMPONENTS_H_
#define BLIMP_CLIENT_CORE_SESSION_CLIENT_NETWORK_COMPONENTS_H_
#include <memory>
#include "base/threading/thread_checker.h"
#include "blimp/client/core/session/assignment_source.h"
#include "blimp/client/core/session/network_event_observer.h"
#include "blimp/net/blimp_connection.h"
#include "blimp/net/blimp_connection_statistics.h"
#include "blimp/net/browser_connection_handler.h"
#include "blimp/net/client_connection_manager.h"
namespace blimp {
namespace client {
// This class's functions and destruction must all be invoked on the IO thread
// by the owner. It is OK to construct the object on the main thread. The
// ThreadChecker is initialized in the call to Initialize.
class ClientNetworkComponents : public ConnectionHandler,
public ConnectionErrorObserver {
public:
// Can be created on any thread.
ClientNetworkComponents(
std::unique_ptr<NetworkEventObserver> observer,
std::unique_ptr<BlimpConnectionStatistics> blimp_connection_statistics);
~ClientNetworkComponents() override;
// Sets up network components.
void Initialize();
// Starts the connection to the engine using the given |assignment|.
// It is required to first call Initialize.
void ConnectWithAssignment(const Assignment& assignment);
BrowserConnectionHandler* GetBrowserConnectionHandler();
private:
// ConnectionHandler implementation.
void HandleConnection(std::unique_ptr<BlimpConnection> connection) override;
// ConnectionErrorObserver implementation.
void OnConnectionError(int error) override;
// The ThreadChecker is reset during the call to Initialize.
base::ThreadChecker io_thread_checker_;
BrowserConnectionHandler connection_handler_;
std::unique_ptr<ClientConnectionManager> connection_manager_;
std::unique_ptr<NetworkEventObserver> network_observer_;
std::unique_ptr<BlimpConnectionStatistics> connection_statistics_;
DISALLOW_COPY_AND_ASSIGN(ClientNetworkComponents);
};
} // namespace client
} // namespace blimp
#endif // BLIMP_CLIENT_CORE_SESSION_CLIENT_NETWORK_COMPONENTS_H_
// Copyright 2016 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 "blimp/client/core/session/cross_thread_network_event_observer.h"
#include "base/bind.h"
#include "base/location.h"
namespace blimp {
namespace client {
CrossThreadNetworkEventObserver::CrossThreadNetworkEventObserver(
const base::WeakPtr<NetworkEventObserver>& target,
const scoped_refptr<base::SequencedTaskRunner>& task_runner)
: target_(target), task_runner_(task_runner) {}
CrossThreadNetworkEventObserver::~CrossThreadNetworkEventObserver() {}
void CrossThreadNetworkEventObserver::OnConnected() {
task_runner_->PostTask(
FROM_HERE, base::Bind(&NetworkEventObserver::OnConnected, target_));
}
void CrossThreadNetworkEventObserver::OnDisconnected(int result) {
task_runner_->PostTask(
FROM_HERE,
base::Bind(&NetworkEventObserver::OnDisconnected, target_, result));
}
} // namespace client
} // namespace blimp
// Copyright 2016 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 BLIMP_CLIENT_CORE_SESSION_CROSS_THREAD_NETWORK_EVENT_OBSERVER_H_
#define BLIMP_CLIENT_CORE_SESSION_CROSS_THREAD_NETWORK_EVENT_OBSERVER_H_
#include <memory>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/sequenced_task_runner.h"
#include "blimp/client/core/session/network_event_observer.h"
namespace blimp {
namespace client {
// Posts NetworkEvents to an observer across the IO/UI thread boundary.
class CrossThreadNetworkEventObserver : public NetworkEventObserver {
public:
CrossThreadNetworkEventObserver(
const base::WeakPtr<NetworkEventObserver>& target,
const scoped_refptr<base::SequencedTaskRunner>& task_runner);
~CrossThreadNetworkEventObserver() override;
// NetworkEventObserver implementation.
void OnConnected() override;
void OnDisconnected(int result) override;
private:
base::WeakPtr<NetworkEventObserver> target_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;
DISALLOW_COPY_AND_ASSIGN(CrossThreadNetworkEventObserver);
};
} // namespace client
} // namespace blimp
#endif // BLIMP_CLIENT_CORE_SESSION_CROSS_THREAD_NETWORK_EVENT_OBSERVER_H_
// Copyright 2016 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 BLIMP_CLIENT_CORE_SESSION_NETWORK_EVENT_OBSERVER_H_
#define BLIMP_CLIENT_CORE_SESSION_NETWORK_EVENT_OBSERVER_H_
namespace blimp {
namespace client {
// A NetworkEventObserver is informed whenever changes happen to the connection
// to the Blimp engine.
class NetworkEventObserver {
public:
NetworkEventObserver() {}
virtual ~NetworkEventObserver() {}
virtual void OnConnected() = 0;
virtual void OnDisconnected(int result) = 0;
};
} // namespace client
} // namespace blimp
#endif // BLIMP_CLIENT_CORE_SESSION_NETWORK_EVENT_OBSERVER_H_
...@@ -6,180 +6,32 @@ ...@@ -6,180 +6,32 @@
#include <vector> #include <vector>
#include "base/bind.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/numerics/safe_conversions.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/strings/string_number_conversions.h"
#include "base/threading/sequenced_task_runner_handle.h" #include "base/threading/sequenced_task_runner_handle.h"
#include "base/threading/thread.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "blimp/client/core/blimp_client_switches.h" #include "blimp/client/core/blimp_client_switches.h"
#include "blimp/client/core/session/client_network_components.h"
#include "blimp/client/core/session/cross_thread_network_event_observer.h"
#include "blimp/client/feature/ime_feature.h" #include "blimp/client/feature/ime_feature.h"
#include "blimp/client/feature/navigation_feature.h" #include "blimp/client/feature/navigation_feature.h"
#include "blimp/client/feature/render_widget_feature.h" #include "blimp/client/feature/render_widget_feature.h"
#include "blimp/client/feature/settings_feature.h" #include "blimp/client/feature/settings_feature.h"
#include "blimp/client/feature/tab_control_feature.h" #include "blimp/client/feature/tab_control_feature.h"
#include "blimp/common/blob_cache/in_memory_blob_cache.h" #include "blimp/common/blob_cache/in_memory_blob_cache.h"
#include "blimp/net/blimp_connection.h"
#include "blimp/net/blimp_message_processor.h"
#include "blimp/net/blimp_message_thread_pipe.h" #include "blimp/net/blimp_message_thread_pipe.h"
#include "blimp/net/blob_channel/blob_channel_receiver.h" #include "blimp/net/blob_channel/blob_channel_receiver.h"
#include "blimp/net/blob_channel/helium_blob_receiver_delegate.h" #include "blimp/net/blob_channel/helium_blob_receiver_delegate.h"
#include "blimp/net/browser_connection_handler.h"
#include "blimp/net/client_connection_manager.h"
#include "blimp/net/common.h"
#include "blimp/net/connection_handler.h"
#include "blimp/net/null_blimp_message_processor.h"
#include "blimp/net/ssl_client_transport.h"
#include "blimp/net/tcp_client_transport.h"
#include "blimp/net/thread_pipe_manager.h" #include "blimp/net/thread_pipe_manager.h"
#include "net/base/address_list.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace blimp { namespace blimp {
namespace client { namespace client {
namespace {
// Posts network events to an observer across the IO/UI thread boundary.
class CrossThreadNetworkEventObserver : public NetworkEventObserver {
public:
CrossThreadNetworkEventObserver(
const base::WeakPtr<NetworkEventObserver>& target,
const scoped_refptr<base::SequencedTaskRunner>& task_runner)
: target_(target), task_runner_(task_runner) {}
~CrossThreadNetworkEventObserver() override {}
void OnConnected() override {
task_runner_->PostTask(
FROM_HERE, base::Bind(&NetworkEventObserver::OnConnected, target_));
}
void OnDisconnected(int result) override {
task_runner_->PostTask(
FROM_HERE,
base::Bind(&NetworkEventObserver::OnDisconnected, target_, result));
}
private:
base::WeakPtr<NetworkEventObserver> target_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;
DISALLOW_COPY_AND_ASSIGN(CrossThreadNetworkEventObserver);
};
} // namespace
// This class's functions and destruction are all invoked on the IO thread by
// the BlimpClientSession.
class ClientNetworkComponents : public ConnectionHandler,
public ConnectionErrorObserver {
public:
// Can be created on any thread.
ClientNetworkComponents(
std::unique_ptr<NetworkEventObserver> observer,
std::unique_ptr<BlimpConnectionStatistics> blimp_connection_statistics);
~ClientNetworkComponents() override;
// Sets up network components.
void Initialize();
// Starts the connection to the engine using the given |assignment|.
// It is required to first call Initialize.
void ConnectWithAssignment(const Assignment& assignment);
BrowserConnectionHandler* GetBrowserConnectionHandler();
void DropCurrentConnection();
private:
// ConnectionHandler implementation.
void HandleConnection(std::unique_ptr<BlimpConnection> connection) override;
// ConnectionErrorObserver implementation.
void OnConnectionError(int error) override;
std::unique_ptr<BrowserConnectionHandler> connection_handler_;
std::unique_ptr<ClientConnectionManager> connection_manager_;
std::unique_ptr<NetworkEventObserver> network_observer_;
std::unique_ptr<BlimpConnectionStatistics> connection_statistics_;
DISALLOW_COPY_AND_ASSIGN(ClientNetworkComponents);
};
ClientNetworkComponents::ClientNetworkComponents(
std::unique_ptr<NetworkEventObserver> network_observer,
std::unique_ptr<BlimpConnectionStatistics> statistics)
: connection_handler_(new BrowserConnectionHandler),
network_observer_(std::move(network_observer)),
connection_statistics_(std::move(statistics)) {
DCHECK(connection_statistics_);
}
ClientNetworkComponents::~ClientNetworkComponents() {}
void ClientNetworkComponents::Initialize() {
DCHECK(!connection_manager_);
connection_manager_ = base::WrapUnique(new ClientConnectionManager(this));
}
void ClientNetworkComponents::ConnectWithAssignment(
const Assignment& assignment) {
DCHECK(connection_manager_);
connection_manager_->set_client_token(assignment.client_token);
const char* transport_type = "UNKNOWN";
switch (assignment.transport_protocol) {
case Assignment::SSL:
DCHECK(assignment.cert);
connection_manager_->AddTransport(base::WrapUnique(new SSLClientTransport(
assignment.engine_endpoint, std::move(assignment.cert),
connection_statistics_.get(), nullptr)));
transport_type = "SSL";
break;
case Assignment::TCP:
connection_manager_->AddTransport(base::WrapUnique(new TCPClientTransport(
assignment.engine_endpoint, connection_statistics_.get(), nullptr)));
transport_type = "TCP";
break;
case Assignment::UNKNOWN:
LOG(FATAL) << "Unknown transport type.";
break;
}
VLOG(1) << "Connecting to " << assignment.engine_endpoint.ToString() << " ("
<< transport_type << ")";
connection_manager_->Connect();
}
BrowserConnectionHandler*
ClientNetworkComponents::GetBrowserConnectionHandler() {
return connection_handler_.get();
}
void ClientNetworkComponents::DropCurrentConnection() {
connection_handler_->DropCurrentConnection();
}
void ClientNetworkComponents::HandleConnection(
std::unique_ptr<BlimpConnection> connection) {
VLOG(1) << "Connection established.";
connection->AddConnectionErrorObserver(this);
network_observer_->OnConnected();
connection_handler_->HandleConnection(std::move(connection));
}
void ClientNetworkComponents::OnConnectionError(int result) {
if (result >= 0) {
VLOG(1) << "Disconnected with reason: " << result;
} else {
VLOG(1) << "Connection error: " << net::ErrorToString(result);
}
network_observer_->OnDisconnected(result);
}
BlimpClientSession::BlimpClientSession(const GURL& assigner_endpoint) BlimpClientSession::BlimpClientSession(const GURL& assigner_endpoint)
: io_thread_("BlimpIOThread"), : io_thread_("BlimpIOThread"),
...@@ -295,8 +147,10 @@ void BlimpClientSession::OnDisconnected(int result) {} ...@@ -295,8 +147,10 @@ void BlimpClientSession::OnDisconnected(int result) {}
void BlimpClientSession::OnImageDecodeError() { void BlimpClientSession::OnImageDecodeError() {
io_thread_.task_runner()->PostTask( io_thread_.task_runner()->PostTask(
FROM_HERE, base::Bind(&ClientNetworkComponents::DropCurrentConnection, FROM_HERE,
base::Unretained(net_components_.get()))); base::Bind(
&BrowserConnectionHandler::DropCurrentConnection,
base::Unretained(net_components_->GetBrowserConnectionHandler())));
} }
TabControlFeature* BlimpClientSession::GetTabControlFeature() const { TabControlFeature* BlimpClientSession::GetTabControlFeature() const {
......
...@@ -10,9 +10,11 @@ ...@@ -10,9 +10,11 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
#include "blimp/client/core/compositor/blob_image_serialization_processor.h" #include "blimp/client/core/compositor/blob_image_serialization_processor.h"
#include "blimp/client/core/session/assignment_source.h" #include "blimp/client/core/session/assignment_source.h"
#include "blimp/client/core/session/network_event_observer.h"
#include "blimp/common/proto/blimp_message.pb.h" #include "blimp/common/proto/blimp_message.pb.h"
#include "blimp/net/blimp_connection_statistics.h" #include "blimp/net/blimp_connection_statistics.h"
#include "blimp/net/blimp_message_processor.h" #include "blimp/net/blimp_message_processor.h"
...@@ -40,15 +42,6 @@ class RenderWidgetFeature; ...@@ -40,15 +42,6 @@ class RenderWidgetFeature;
class SettingsFeature; class SettingsFeature;
class TabControlFeature; class TabControlFeature;
class NetworkEventObserver {
public:
NetworkEventObserver() {}
virtual ~NetworkEventObserver() {}
virtual void OnConnected() = 0;
virtual void OnDisconnected(int result) = 0;
};
// BlimpClientSession represents a single active session of Blimp on the client // BlimpClientSession represents a single active session of Blimp on the client
// regardless of whether or not the client application is in the background or // regardless of whether or not the client application is in the background or
// foreground. The only time this session is invalid is during initialization // foreground. The only time this session is invalid is during initialization
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment