Commit 8e3e6c83 authored by Jun Cai's avatar Jun Cai Committed by Commit Bot

Network Service: Convert port_forwarding_controller.cc to mojo host resolver

Bug: 874651
Change-Id: I4762526cc8fc4dbf18512b8ab7fbeeda8434336d
Reviewed-on: https://chromium-review.googlesource.com/1184306Reviewed-by: default avatarAndrey Kosyakov <caseq@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Commit-Queue: Jun Cai <juncai@chromium.org>
Cr-Commit-Position: refs/heads/master@{#585658}
parent f2729620
......@@ -20,7 +20,10 @@
#include "chrome/common/pref_names.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "net/base/address_list.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
......@@ -142,41 +145,51 @@ net::NetworkTrafficAnnotationTag kPortForwardingControllerTrafficAnnotation =
"here."
})");
class SocketTunnel {
class SocketTunnel : public network::mojom::ResolveHostClient {
public:
static void StartTunnel(const std::string& host,
static void StartTunnel(Profile* profile,
const std::string& host,
int port,
int result,
std::unique_ptr<net::StreamSocket> socket) {
if (result == net::OK)
new SocketTunnel(std::move(socket), host, port);
new SocketTunnel(profile, std::move(socket), host, port);
}
private:
SocketTunnel(std::unique_ptr<net::StreamSocket> socket,
SocketTunnel(Profile* profile,
std::unique_ptr<net::StreamSocket> socket,
const std::string& host,
int port)
: remote_socket_(std::move(socket)),
: binding_(this),
remote_socket_(std::move(socket)),
pending_writes_(0),
pending_destruction_(false) {
host_resolver_ = net::HostResolver::CreateDefaultResolver(nullptr);
net::HostResolver::RequestInfo request_info(net::HostPortPair(host, port));
int result = host_resolver_->Resolve(
request_info, net::DEFAULT_PRIORITY, &address_list_,
base::Bind(&SocketTunnel::OnResolved, base::Unretained(this)),
&request_, net::NetLogWithSource());
if (result != net::ERR_IO_PENDING)
OnResolved(result);
DCHECK(!binding_);
network::mojom::ResolveHostClientPtr client_ptr;
binding_.Bind(mojo::MakeRequest(&client_ptr));
binding_.set_connection_error_handler(
base::BindOnce(&SocketTunnel::OnComplete, base::Unretained(this),
net::ERR_FAILED, base::nullopt));
net::HostPortPair host_port_pair(host, port);
content::BrowserContext::GetDefaultStoragePartition(profile)
->GetNetworkContext()
->ResolveHost(host_port_pair, nullptr, std::move(client_ptr));
}
void OnResolved(int result) {
// network::mojom::ResolveHostClient:
void OnComplete(
int result,
const base::Optional<net::AddressList>& resolved_addresses) override {
if (result < 0) {
SelfDestruct();
return;
}
host_socket_.reset(new net::TCPClientSocket(address_list_, nullptr, nullptr,
net::NetLogSource()));
DCHECK(resolved_addresses && !resolved_addresses->empty());
host_socket_.reset(new net::TCPClientSocket(
resolved_addresses.value(), nullptr, nullptr, net::NetLogSource()));
result = host_socket_->Connect(base::Bind(&SocketTunnel::OnConnected,
base::Unretained(this)));
if (result != net::ERR_IO_PENDING)
......@@ -270,11 +283,9 @@ class SocketTunnel {
delete this;
}
mojo::Binding<network::mojom::ResolveHostClient> binding_;
std::unique_ptr<net::StreamSocket> remote_socket_;
std::unique_ptr<net::StreamSocket> host_socket_;
std::unique_ptr<net::HostResolver> host_resolver_;
std::unique_ptr<net::HostResolver::Request> request_;
net::AddressList address_list_;
int pending_writes_;
bool pending_destruction_;
};
......@@ -284,7 +295,8 @@ class SocketTunnel {
class PortForwardingController::Connection
: public AndroidDeviceManager::AndroidWebSocket::Delegate {
public:
Connection(Registry* registry,
Connection(Profile* profile,
Registry* registry,
scoped_refptr<AndroidDeviceManager::Device> device,
scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser,
const ForwardingMap& forwarding_map);
......@@ -322,6 +334,7 @@ class PortForwardingController::Connection
void OnFrameRead(const std::string& message) override;
void OnSocketClosed() override;
Profile* profile_;
PortForwardingController::Registry* registry_;
scoped_refptr<AndroidDeviceManager::Device> device_;
scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser_;
......@@ -336,11 +349,13 @@ class PortForwardingController::Connection
};
PortForwardingController::Connection::Connection(
Profile* profile,
Registry* registry,
scoped_refptr<AndroidDeviceManager::Device> device,
scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser,
const ForwardingMap& forwarding_map)
: registry_(registry),
: profile_(profile),
registry_(registry),
device_(device),
browser_(browser),
command_id_(0),
......@@ -503,15 +518,13 @@ void PortForwardingController::Connection::OnFrameRead(
return;
std::string destination_host = tokens[0];
device_->OpenSocket(
connection_id.c_str(),
base::Bind(&SocketTunnel::StartTunnel,
destination_host,
destination_port));
device_->OpenSocket(connection_id.c_str(),
base::Bind(&SocketTunnel::StartTunnel, profile_,
destination_host, destination_port));
}
PortForwardingController::PortForwardingController(Profile* profile)
: pref_service_(profile->GetPrefs()) {
: profile_(profile), pref_service_(profile->GetPrefs()) {
pref_change_registrar_.Init(pref_service_);
base::Closure callback = base::Bind(
&PortForwardingController::OnPrefsChange, base::Unretained(this));
......@@ -538,8 +551,8 @@ PortForwardingController::DeviceListChanged(
Registry::iterator rit = registry_.find(remote_device->serial());
if (rit == registry_.end()) {
if (remote_device->browsers().size() > 0) {
new Connection(&registry_, device, remote_device->browsers()[0],
forwarding_map_);
new Connection(profile_, &registry_, device,
remote_device->browsers()[0], forwarding_map_);
}
} else {
status.push_back(std::make_pair(rit->second->browser(),
......
......@@ -40,6 +40,7 @@ class PortForwardingController {
void UpdateConnections();
Profile* profile_;
PrefService* pref_service_;
PrefChangeRegistrar pref_change_registrar_;
Registry registry_;
......
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