Commit 07ff1408 authored by amistry's avatar amistry Committed by Commit bot

Implementation of ProxyResolver that uses a Mojo service.

BUG=11746

Review URL: https://codereview.chromium.org/917863005

Cr-Commit-Position: refs/heads/master@{#319848}
parent 9731c54d
......@@ -798,12 +798,16 @@ if (use_v8_in_net && !is_android) {
sources = [
"dns/mojo_host_resolver_impl.cc",
"dns/mojo_host_resolver_impl.h",
"proxy/mojo_proxy_resolver_factory.h",
"proxy/proxy_resolver_mojo.cc",
"proxy/proxy_resolver_mojo.h",
]
public_deps = [
":mojo_type_converters",
":net",
"//base",
"//mojo/common",
"//net/interfaces",
"//third_party/mojo/src/mojo/public/cpp/bindings",
]
......@@ -1405,6 +1409,7 @@ if (!is_android && !is_win && !is_mac) {
"dns/mojo_host_resolver_impl_unittest.cc",
"proxy/mojo_proxy_resolver_factory_impl_unittest.cc",
"proxy/mojo_proxy_resolver_impl_unittest.cc",
"proxy/proxy_resolver_mojo_unittest.cc",
]
}
......
......@@ -627,6 +627,9 @@ NET_ERROR(HTTP_1_1_REQUIRED, -365)
// HTTP_1_1_REQUIRED error code received on HTTP/2 session to proxy.
NET_ERROR(PROXY_HTTP_1_1_REQUIRED, -366)
// The PAC script terminated fatally and must be reloaded.
NET_ERROR(PAC_SCRIPT_TERMINATED, -367)
// The cache does not have the requested entry.
NET_ERROR(CACHE_MISS, -400)
......
......@@ -722,6 +722,7 @@
'dns/mojo_host_resolver_impl_unittest.cc',
'proxy/mojo_proxy_resolver_factory_impl_unittest.cc',
'proxy/mojo_proxy_resolver_impl_unittest.cc',
'proxy/proxy_resolver_mojo_unittest.cc',
],
},
],
......@@ -1277,12 +1278,17 @@
'sources': [
'dns/mojo_host_resolver_impl.cc',
'dns/mojo_host_resolver_impl.h',
'proxy/mojo_proxy_resolver_factory.h',
'proxy/proxy_resolver_mojo.cc',
'proxy/proxy_resolver_mojo.h',
],
'dependencies': [
'mojo_type_converters',
'net',
'net_interfaces',
'../mojo/mojo_base.gyp:mojo_common_lib',
'../mojo/mojo_base.gyp:mojo_environment_chromium',
'../mojo/mojo_base.gyp:mojo_url_type_converters',
'../third_party/mojo/mojo_public.gyp:mojo_cpp_bindings',
],
},
......
......@@ -1449,6 +1449,7 @@
'proxy/proxy_config_unittest.cc',
'proxy/proxy_info_unittest.cc',
'proxy/proxy_list_unittest.cc',
'proxy/proxy_resolver_mojo_unittest.cc',
'proxy/proxy_resolver_v8_tracing_unittest.cc',
'proxy/proxy_resolver_v8_unittest.cc',
'proxy/proxy_script_decider_unittest.cc',
......
// Copyright 2015 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 NET_PROXY_MOJO_PROXY_RESOLVER_FACTORY_H_
#define NET_PROXY_MOJO_PROXY_RESOLVER_FACTORY_H_
#include "net/interfaces/host_resolver_service.mojom.h"
#include "net/interfaces/proxy_resolver_service.mojom.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
namespace net {
// Factory for connecting to Mojo ProxyResolver services.
class MojoProxyResolverFactory {
public:
// Connect to a new ProxyResolver service using request |req|, using
// |host_resolver| as the DNS resolver.
// Note: The connection request |req| may be resolved asynchronously.
virtual void Create(mojo::InterfaceRequest<interfaces::ProxyResolver> req,
interfaces::HostResolverPtr host_resolver) = 0;
};
} // namespace net
#endif // NET_PROXY_MOJO_PROXY_RESOLVER_FACTORY_H_
......@@ -6,6 +6,7 @@
#include "base/logging.h"
#include "net/base/host_port_pair.h"
#include "net/proxy/proxy_info.h"
#include "net/proxy/proxy_server.h"
namespace net {
......@@ -80,4 +81,17 @@ TypeConverter<net::ProxyServer, net::interfaces::ProxyServerPtr>::Convert(
net::HostPortPair(obj->host, obj->port));
}
// static
net::ProxyInfo
TypeConverter<net::ProxyInfo, mojo::Array<net::interfaces::ProxyServerPtr>>::
Convert(const mojo::Array<net::interfaces::ProxyServerPtr>& obj) {
net::ProxyList proxy_list;
for (size_t i = 0; i < obj.size(); i++) {
proxy_list.AddProxyServer(obj[i].To<net::ProxyServer>());
}
net::ProxyInfo info;
info.UseProxyList(proxy_list);
return info;
}
} // namespace mojo
......@@ -9,6 +9,7 @@
#include "third_party/mojo/src/mojo/public/cpp/bindings/type_converter.h"
namespace net {
class ProxyInfo;
class ProxyServer;
}
......@@ -24,6 +25,13 @@ struct TypeConverter<net::ProxyServer, net::interfaces::ProxyServerPtr> {
static net::ProxyServer Convert(const net::interfaces::ProxyServerPtr& obj);
};
template <>
struct TypeConverter<net::ProxyInfo,
mojo::Array<net::interfaces::ProxyServerPtr>> {
static net::ProxyInfo Convert(
const mojo::Array<net::interfaces::ProxyServerPtr>& obj);
};
} // namespace mojo
#endif // NET_PROXY_MOJO_PROXY_TYPE_CONVERTERS_H_
// Copyright 2015 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 "net/proxy/proxy_resolver_mojo.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "mojo/common/common_type_converters.h"
#include "mojo/common/url_type_converters.h"
#include "net/base/net_errors.h"
#include "net/dns/mojo_host_resolver_impl.h"
#include "net/proxy/mojo_proxy_resolver_factory.h"
#include "net/proxy/mojo_proxy_type_converters.h"
#include "net/proxy/proxy_info.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
namespace net {
class ProxyResolverMojo::Job : public interfaces::ProxyResolverRequestClient,
public mojo::ErrorHandler {
public:
Job(ProxyResolverMojo* resolver,
const GURL& url,
ProxyInfo* results,
const net::CompletionCallback& callback);
~Job() override;
// Cancels the job and prevents the callback from being run.
void Cancel();
private:
// Overridden from mojo::ErrorHandler:
void OnConnectionError() override;
// Overridden from interfaces::ProxyResolverRequestClient:
void ReportResult(
int32_t error,
mojo::Array<interfaces::ProxyServerPtr> proxy_servers) override;
ProxyResolverMojo* resolver_;
const GURL url_;
ProxyInfo* results_;
net::CompletionCallback callback_;
base::ThreadChecker thread_checker_;
mojo::Binding<interfaces::ProxyResolverRequestClient> binding_;
};
ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver,
const GURL& url,
ProxyInfo* results,
const net::CompletionCallback& callback)
: resolver_(resolver),
url_(url),
results_(results),
callback_(callback),
binding_(this) {
binding_.set_error_handler(this);
interfaces::ProxyResolverRequestClientPtr client_ptr;
binding_.Bind(mojo::GetProxy(&client_ptr));
resolver_->mojo_proxy_resolver_ptr_->GetProxyForUrl(mojo::String::From(url_),
client_ptr.Pass());
}
ProxyResolverMojo::Job::~Job() {
DCHECK(thread_checker_.CalledOnValidThread());
if (!callback_.is_null())
callback_.Run(ERR_PAC_SCRIPT_TERMINATED);
}
void ProxyResolverMojo::Job::Cancel() {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!callback_.is_null());
callback_.Reset();
}
void ProxyResolverMojo::Job::OnConnectionError() {
DCHECK(thread_checker_.CalledOnValidThread());
DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError";
resolver_->RemoveJob(this);
}
void ProxyResolverMojo::Job::ReportResult(
int32_t error,
mojo::Array<interfaces::ProxyServerPtr> proxy_servers) {
DCHECK(thread_checker_.CalledOnValidThread());
DVLOG(1) << "ProxyResolverMojo::Job::ReportResult: " << error;
if (error == OK) {
*results_ = proxy_servers.To<ProxyInfo>();
DVLOG(1) << "Servers: " << results_->ToPacString();
}
callback_.Run(error);
callback_.Reset();
resolver_->RemoveJob(this);
}
ProxyResolverMojo::ProxyResolverMojo(
MojoProxyResolverFactory* mojo_proxy_resolver_factory,
HostResolver* host_resolver)
: ProxyResolver(true /* |expects_pac_bytes| */),
mojo_proxy_resolver_factory_(mojo_proxy_resolver_factory),
host_resolver_(host_resolver) {
}
ProxyResolverMojo::~ProxyResolverMojo() {
DCHECK(thread_checker_.CalledOnValidThread());
// All pending requests should have been cancelled.
DCHECK(pending_jobs_.empty());
DCHECK(set_pac_script_callback_.IsCancelled());
}
void ProxyResolverMojo::CancelSetPacScript() {
DCHECK(thread_checker_.CalledOnValidThread());
set_pac_script_callback_.Cancel();
}
int ProxyResolverMojo::SetPacScript(
const scoped_refptr<ProxyResolverScriptData>& pac_script,
const net::CompletionCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(set_pac_script_callback_.IsCancelled());
DCHECK(!callback.is_null());
if (pac_script->type() != ProxyResolverScriptData::TYPE_SCRIPT_CONTENTS ||
pac_script->utf16().empty()) {
return ERR_PAC_SCRIPT_FAILED;
}
DVLOG(1) << "ProxyResolverMojo::SetPacScript: " << pac_script->utf16();
set_pac_script_callback_.Reset(
base::Bind(&ProxyResolverMojo::OnSetPacScriptDone, base::Unretained(this),
pac_script, callback));
if (!mojo_proxy_resolver_ptr_)
SetUpServices();
mojo_proxy_resolver_ptr_->SetPacScript(
mojo::String::From(pac_script->utf16()),
set_pac_script_callback_.callback());
return ERR_IO_PENDING;
}
void ProxyResolverMojo::OnSetPacScriptDone(
const scoped_refptr<ProxyResolverScriptData>& pac_script,
const net::CompletionCallback& callback,
int32_t result) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!set_pac_script_callback_.IsCancelled());
DVLOG(1) << "ProxyResolverMojo::OnSetPacScriptDone: " << result;
callback.Run(result);
set_pac_script_callback_.Cancel();
}
void ProxyResolverMojo::SetUpServices() {
DCHECK(thread_checker_.CalledOnValidThread());
// A Mojo service implementation must outlive its binding.
mojo_host_resolver_binding_.reset();
interfaces::HostResolverPtr mojo_host_resolver_ptr;
mojo_host_resolver_.reset(new MojoHostResolverImpl(host_resolver_));
mojo_host_resolver_binding_.reset(new mojo::Binding<interfaces::HostResolver>(
mojo_host_resolver_.get(), mojo::GetProxy(&mojo_host_resolver_ptr)));
mojo_proxy_resolver_ptr_.reset();
mojo_proxy_resolver_factory_->Create(
mojo::GetProxy(&mojo_proxy_resolver_ptr_), mojo_host_resolver_ptr.Pass());
mojo_proxy_resolver_ptr_.set_error_handler(this);
}
void ProxyResolverMojo::AbortPendingRequests() {
DCHECK(thread_checker_.CalledOnValidThread());
if (!set_pac_script_callback_.IsCancelled()) {
set_pac_script_callback_.callback().Run(ERR_PAC_SCRIPT_TERMINATED);
set_pac_script_callback_.Cancel();
}
// Need to use this loop because deleting a Job will cause its callback to be
// run with a failure error code, which may cause other Jobs to be deleted.
while (!pending_jobs_.empty()) {
auto it = pending_jobs_.begin();
Job* job = *it;
pending_jobs_.erase(it);
// Deleting the job will cause its completion callback to be run with an
// ERR_PAC_SCRIPT_TERMINATED error.
delete job;
}
}
void ProxyResolverMojo::OnConnectionError() {
DCHECK(thread_checker_.CalledOnValidThread());
DVLOG(1) << "ProxyResolverMojo::OnConnectionError";
// Disconnect from the Mojo proxy resolver service. An attempt to reconnect
// will happen on the next |SetPacScript()| request.
mojo_proxy_resolver_ptr_.reset();
// Aborting requests will invoke their callbacks, which may call
// |SetPacScript()| and re-create the connection. So disconnect from the Mojo
// service (above) before aborting the pending requests.
AbortPendingRequests();
}
void ProxyResolverMojo::RemoveJob(Job* job) {
DCHECK(thread_checker_.CalledOnValidThread());
size_t num_erased = pending_jobs_.erase(job);
DCHECK(num_erased);
delete job;
}
int ProxyResolverMojo::GetProxyForURL(const GURL& url,
ProxyInfo* results,
const net::CompletionCallback& callback,
RequestHandle* request,
const BoundNetLog& net_log) {
DCHECK(thread_checker_.CalledOnValidThread());
// If the Mojo service is not connected, fail. The Mojo service is connected
// when the script is set, which must be done after construction and after a
// previous request returns ERR_PAC_SCRIPT_TERMINATED due to the Mojo proxy
// resolver process crashing.
if (!mojo_proxy_resolver_ptr_) {
DVLOG(1) << "ProxyResolverMojo::GetProxyForURL: Mojo not connected";
return ERR_PAC_SCRIPT_TERMINATED;
}
Job* job = new Job(this, url, results, callback);
bool inserted = pending_jobs_.insert(job).second;
DCHECK(inserted);
*request = job;
return ERR_IO_PENDING;
}
void ProxyResolverMojo::CancelRequest(RequestHandle request) {
DCHECK(thread_checker_.CalledOnValidThread());
Job* job = static_cast<Job*>(request);
DCHECK(job);
job->Cancel();
RemoveJob(job);
}
LoadState ProxyResolverMojo::GetLoadState(RequestHandle request) const {
// TODO(amistry): Implement real LoadState.
return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
}
} // namespace net
// Copyright 2015 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 NET_PROXY_PROXY_RESOLVER_MOJO_H_
#define NET_PROXY_PROXY_RESOLVER_MOJO_H_
#include <set>
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/thread_checker.h"
#include "net/base/completion_callback.h"
#include "net/base/load_states.h"
#include "net/interfaces/host_resolver_service.mojom.h"
#include "net/interfaces/proxy_resolver_service.mojom.h"
#include "net/proxy/proxy_resolver.h"
#include "net/proxy/proxy_resolver_script_data.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
class GURL;
namespace net {
class BoundNetLog;
class HostResolver;
class ProxyInfo;
class MojoProxyResolverFactory;
// Implementation of ProxyResolver that connects to a Mojo service to evaluate
// PAC scripts. This implementation only knows about Mojo services, and
// therefore that service may live in or out of process.
//
// This implementation handles disconnections from the Mojo service (i.e. if the
// service is out-of-process and that process crashes) and transparently
// re-connects to a new service.
class ProxyResolverMojo : public ProxyResolver, public mojo::ErrorHandler {
public:
// Constructs a ProxyResolverMojo and connects to a new Mojo proxy resolver
// service using |mojo_proxy_resolver_factory|. The new Mojo proxy resolver
// uses |host_resolver| as the DNS resolver. |mojo_proxy_resolver_factory|
// and |host_resolver| are not owned and must outlive this.
// TODO(amistry): Add ProxyResolverErrorObserver and NetLog.
ProxyResolverMojo(MojoProxyResolverFactory* mojo_proxy_resolver_factory,
HostResolver* host_resolver);
~ProxyResolverMojo() override;
// ProxyResolver implementation:
int GetProxyForURL(const GURL& url,
ProxyInfo* results,
const net::CompletionCallback& callback,
RequestHandle* request,
const BoundNetLog& net_log) override;
void CancelRequest(RequestHandle request) override;
LoadState GetLoadState(RequestHandle request) const override;
void CancelSetPacScript() override;
int SetPacScript(const scoped_refptr<ProxyResolverScriptData>& pac_script,
const net::CompletionCallback& callback) override;
private:
class Job;
// Overridden from mojo::ErrorHandler:
void OnConnectionError() override;
// Callback for ProxyResolverService::SetPacScript.
void OnSetPacScriptDone(
const scoped_refptr<ProxyResolverScriptData>& pac_script,
const net::CompletionCallback& callback,
int32_t result);
void SetUpServices();
void AbortPendingRequests();
void RemoveJob(Job* job);
// Connection to the Mojo proxy resolver.
interfaces::ProxyResolverPtr mojo_proxy_resolver_ptr_;
// Mojo host resolver service and binding.
scoped_ptr<interfaces::HostResolver> mojo_host_resolver_;
scoped_ptr<mojo::Binding<interfaces::HostResolver>>
mojo_host_resolver_binding_;
// Factory for connecting to new Mojo proxy resolvers.
// Not owned.
MojoProxyResolverFactory* mojo_proxy_resolver_factory_;
// DNS resolver, saved for creating a new Mojo proxy resolver when the
// existing one disconnects (i.e. when utility process crashes).
HostResolver* host_resolver_;
std::set<Job*> pending_jobs_;
net::CancelableCompletionCallback set_pac_script_callback_;
base::ThreadChecker thread_checker_;
DISALLOW_COPY_AND_ASSIGN(ProxyResolverMojo);
};
} // namespace net
#endif // NET_PROXY_PROXY_RESOLVER_MOJO_H_
This diff is collapsed.
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