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
,
29 // Mojo error handler. This is invoked in response to the client
30 // disconnecting, indicating cancellation.
31 void OnConnectionError();
33 void GetProxyDone(int error
);
35 MojoProxyResolverImpl
* resolver_
;
37 interfaces::ProxyResolverRequestClientPtr client_
;
40 net::ProxyResolver::RequestHandle request_handle_
;
43 DISALLOW_COPY_AND_ASSIGN(Job
);
46 MojoProxyResolverImpl::MojoProxyResolverImpl(
47 scoped_ptr
<ProxyResolverV8Tracing
> resolver
)
48 : resolver_(resolver
.Pass()) {
51 MojoProxyResolverImpl::~MojoProxyResolverImpl() {
52 STLDeleteElements(&resolve_jobs_
);
55 void MojoProxyResolverImpl::GetProxyForUrl(
56 const mojo::String
& url
,
57 interfaces::ProxyResolverRequestClientPtr client
) {
58 DVLOG(1) << "GetProxyForUrl(" << url
<< ")";
59 Job
* job
= new Job(client
.Pass(), this, url
.To
<GURL
>());
60 bool inserted
= resolve_jobs_
.insert(job
).second
;
65 void MojoProxyResolverImpl::DeleteJob(Job
* job
) {
66 size_t num_erased
= resolve_jobs_
.erase(job
);
71 MojoProxyResolverImpl::Job::Job(
72 interfaces::ProxyResolverRequestClientPtr client
,
73 MojoProxyResolverImpl
* resolver
,
75 : resolver_(resolver
),
76 client_(client
.Pass()),
78 request_handle_(nullptr),
81 MojoProxyResolverImpl::Job::~Job() {
82 if (request_handle_
&& !done_
)
83 resolver_
->resolver_
->CancelRequest(request_handle_
);
86 void MojoProxyResolverImpl::Job::Start() {
87 resolver_
->resolver_
->GetProxyForURL(
88 url_
, &result_
, base::Bind(&Job::GetProxyDone
, base::Unretained(this)),
90 make_scoped_ptr(new MojoProxyResolverV8TracingBindings
<
91 interfaces::ProxyResolverRequestClient
>(client_
.get())));
92 client_
.set_connection_error_handler(base::Bind(
93 &MojoProxyResolverImpl::Job::OnConnectionError
, base::Unretained(this)));
96 void MojoProxyResolverImpl::Job::GetProxyDone(int error
) {
98 DVLOG(1) << "GetProxyForUrl(" << url_
<< ") finished with error " << error
99 << ". " << result_
.proxy_list().size() << " Proxies returned:";
100 for (const auto& proxy
: result_
.proxy_list().GetAll()) {
101 DVLOG(1) << proxy
.ToURI();
103 mojo::Array
<interfaces::ProxyServerPtr
> result
;
105 result
= mojo::Array
<interfaces::ProxyServerPtr
>::From(
106 result_
.proxy_list().GetAll());
108 client_
->ReportResult(error
, result
.Pass());
109 resolver_
->DeleteJob(this);
112 void MojoProxyResolverImpl::Job::OnConnectionError() {
113 resolver_
->DeleteJob(this);