1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/proxy/mojo_proxy_resolver_impl.h"
7 #include "base/stl_util.h"
8 #include "mojo/common/url_type_converters.h"
9 #include "net/base/net_errors.h"
10 #include "net/log/net_log.h"
11 #include "net/proxy/mojo_proxy_resolver_v8_tracing_bindings.h"
12 #include "net/proxy/mojo_proxy_type_converters.h"
13 #include "net/proxy/proxy_info.h"
14 #include "net/proxy/proxy_resolver_script_data.h"
15 #include "net/proxy/proxy_resolver_v8_tracing.h"
19 class MojoProxyResolverImpl::Job
{
21 Job(interfaces::ProxyResolverRequestClientPtr client
,
22 MojoProxyResolverImpl
* resolver
,
28 net::ProxyResolver::RequestHandle
request_handle() { return request_handle_
; }
31 // Mojo error handler. This is invoked in response to the client
32 // disconnecting, indicating cancellation.
33 void OnConnectionError();
35 void GetProxyDone(int error
);
37 MojoProxyResolverImpl
* resolver_
;
39 interfaces::ProxyResolverRequestClientPtr client_
;
42 net::ProxyResolver::RequestHandle request_handle_
;
45 base::WeakPtrFactory
<interfaces::ProxyResolverRequestClient
> weak_factory_
;
47 DISALLOW_COPY_AND_ASSIGN(Job
);
50 MojoProxyResolverImpl::MojoProxyResolverImpl(
51 scoped_ptr
<ProxyResolverV8Tracing
> resolver
)
52 : resolver_(resolver
.Pass()) {
55 MojoProxyResolverImpl::~MojoProxyResolverImpl() {
56 STLDeleteElements(&resolve_jobs_
);
59 void MojoProxyResolverImpl::GetProxyForUrl(
60 const mojo::String
& url
,
61 interfaces::ProxyResolverRequestClientPtr client
) {
62 DVLOG(1) << "GetProxyForUrl(" << url
<< ")";
63 Job
* job
= new Job(client
.Pass(), this, url
.To
<GURL
>());
64 bool inserted
= resolve_jobs_
.insert(job
).second
;
69 void MojoProxyResolverImpl::DeleteJob(Job
* job
) {
70 if (job
->request_handle())
71 request_handle_to_job_
.erase(job
->request_handle());
73 size_t num_erased
= resolve_jobs_
.erase(job
);
78 MojoProxyResolverImpl::Job::Job(
79 interfaces::ProxyResolverRequestClientPtr client
,
80 MojoProxyResolverImpl
* resolver
,
82 : resolver_(resolver
),
83 client_(client
.Pass()),
85 request_handle_(nullptr),
87 weak_factory_(client_
.get()) {
90 MojoProxyResolverImpl::Job::~Job() {
91 if (request_handle_
&& !done_
)
92 resolver_
->resolver_
->CancelRequest(request_handle_
);
95 void MojoProxyResolverImpl::Job::Start() {
96 resolver_
->resolver_
->GetProxyForURL(
97 url_
, &result_
, base::Bind(&Job::GetProxyDone
, base::Unretained(this)),
99 make_scoped_ptr(new MojoProxyResolverV8TracingBindings
<
100 interfaces::ProxyResolverRequestClient
>(weak_factory_
.GetWeakPtr())));
101 client_
.set_connection_error_handler(base::Bind(
102 &MojoProxyResolverImpl::Job::OnConnectionError
, base::Unretained(this)));
103 resolver_
->request_handle_to_job_
.insert(
104 std::make_pair(request_handle_
, this));
107 void MojoProxyResolverImpl::Job::GetProxyDone(int error
) {
109 DVLOG(1) << "GetProxyForUrl(" << url_
<< ") finished with error " << error
110 << ". " << result_
.proxy_list().size() << " Proxies returned:";
111 for (const auto& proxy
: result_
.proxy_list().GetAll()) {
112 DVLOG(1) << proxy
.ToURI();
114 mojo::Array
<interfaces::ProxyServerPtr
> result
;
116 result
= mojo::Array
<interfaces::ProxyServerPtr
>::From(
117 result_
.proxy_list().GetAll());
119 client_
->ReportResult(error
, result
.Pass());
120 resolver_
->DeleteJob(this);
123 void MojoProxyResolverImpl::Job::OnConnectionError() {
124 resolver_
->DeleteJob(this);