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/load_state_change_coalescer.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"
18 const int kLoadStateChangeCoalesceTimeoutMilliseconds
= 10;
21 class MojoProxyResolverImpl::Job
: public mojo::ErrorHandler
{
23 Job(interfaces::ProxyResolverRequestClientPtr client
,
24 MojoProxyResolverImpl
* resolver
,
30 // Invoked when the LoadState for this job changes.
31 void LoadStateChanged(LoadState load_state
);
33 net::ProxyResolver::RequestHandle
request_handle() { return request_handle_
; }
36 // mojo::ErrorHandler override.
37 // This is invoked in response to the client disconnecting, indicating
39 void OnConnectionError() override
;
41 void GetProxyDone(int error
);
43 void SendLoadStateChanged(LoadState load_state
);
45 MojoProxyResolverImpl
* resolver_
;
47 interfaces::ProxyResolverRequestClientPtr client_
;
50 net::ProxyResolver::RequestHandle request_handle_
;
52 LoadStateChangeCoalescer load_state_change_coalescer_
;
54 DISALLOW_COPY_AND_ASSIGN(Job
);
57 MojoProxyResolverImpl::MojoProxyResolverImpl(
58 scoped_ptr
<net::ProxyResolver
> resolver
)
59 : resolver_(resolver
.Pass()) {
60 DCHECK(resolver_
->expects_pac_bytes());
63 MojoProxyResolverImpl::~MojoProxyResolverImpl() {
64 if (!set_pac_script_requests_
.empty())
65 resolver_
->CancelSetPacScript();
66 STLDeleteElements(&resolve_jobs_
);
69 void MojoProxyResolverImpl::LoadStateChanged(
70 net::ProxyResolver::RequestHandle handle
,
71 LoadState load_state
) {
72 auto it
= request_handle_to_job_
.find(handle
);
73 DCHECK(it
!= request_handle_to_job_
.end());
74 it
->second
->LoadStateChanged(load_state
);
77 void MojoProxyResolverImpl::SetPacScript(
78 const mojo::String
& data
,
79 const mojo::Callback
<void(int32_t)>& callback
) {
80 DVLOG(1) << "SetPacScript(" << data
<< ")";
81 set_pac_script_requests_
.push(
82 SetPacScriptRequest(ProxyResolverScriptData::FromUTF8(data
), callback
));
83 if (set_pac_script_requests_
.size() == 1)
87 void MojoProxyResolverImpl::GetProxyForUrl(
88 const mojo::String
& url
,
89 interfaces::ProxyResolverRequestClientPtr client
) {
90 DVLOG(1) << "GetProxyForUrl(" << url
<< ")";
91 Job
* job
= new Job(client
.Pass(), this, url
.To
<GURL
>());
92 bool inserted
= resolve_jobs_
.insert(job
).second
;
97 void MojoProxyResolverImpl::DeleteJob(Job
* job
) {
98 if (job
->request_handle())
99 request_handle_to_job_
.erase(job
->request_handle());
101 size_t num_erased
= resolve_jobs_
.erase(job
);
106 void MojoProxyResolverImpl::StartSetPacScript() {
107 DCHECK(!set_pac_script_requests_
.empty());
108 int result
= resolver_
->SetPacScript(
109 set_pac_script_requests_
.front().script_data
,
110 base::Bind(&MojoProxyResolverImpl::SetPacScriptDone
,
111 base::Unretained(this)));
112 if (result
!= ERR_IO_PENDING
)
113 SetPacScriptDone(result
);
116 void MojoProxyResolverImpl::SetPacScriptDone(int result
) {
117 DVLOG(1) << "SetPacScript finished with error " << result
;
118 DCHECK(!set_pac_script_requests_
.empty());
119 set_pac_script_requests_
.front().callback
.Run(result
);
120 set_pac_script_requests_
.pop();
121 if (!set_pac_script_requests_
.empty())
125 MojoProxyResolverImpl::Job::Job(
126 interfaces::ProxyResolverRequestClientPtr client
,
127 MojoProxyResolverImpl
* resolver
,
129 : resolver_(resolver
),
130 client_(client
.Pass()),
132 request_handle_(nullptr),
134 load_state_change_coalescer_(
135 base::Bind(&MojoProxyResolverImpl::Job::SendLoadStateChanged
,
136 base::Unretained(this)),
137 base::TimeDelta::FromMilliseconds(
138 kLoadStateChangeCoalesceTimeoutMilliseconds
),
139 LOAD_STATE_RESOLVING_PROXY_FOR_URL
) {
142 MojoProxyResolverImpl::Job::~Job() {
143 if (request_handle_
&& !done_
)
144 resolver_
->resolver_
->CancelRequest(request_handle_
);
147 void MojoProxyResolverImpl::Job::Start() {
148 int result
= resolver_
->resolver_
->GetProxyForURL(
149 url_
, &result_
, base::Bind(&Job::GetProxyDone
, base::Unretained(this)),
150 &request_handle_
, BoundNetLog());
151 if (result
!= ERR_IO_PENDING
) {
152 GetProxyDone(result
);
155 client_
.set_error_handler(this);
156 resolver_
->request_handle_to_job_
.insert(
157 std::make_pair(request_handle_
, this));
160 void MojoProxyResolverImpl::Job::LoadStateChanged(LoadState load_state
) {
161 load_state_change_coalescer_
.LoadStateChanged(load_state
);
164 void MojoProxyResolverImpl::Job::GetProxyDone(int error
) {
166 DVLOG(1) << "GetProxyForUrl(" << url_
<< ") finished with error " << error
167 << ". " << result_
.proxy_list().size() << " Proxies returned:";
168 for (const auto& proxy
: result_
.proxy_list().GetAll()) {
169 DVLOG(1) << proxy
.ToURI();
171 mojo::Array
<interfaces::ProxyServerPtr
> result
;
173 result
= mojo::Array
<interfaces::ProxyServerPtr
>::From(
174 result_
.proxy_list().GetAll());
176 client_
->ReportResult(error
, result
.Pass());
177 resolver_
->DeleteJob(this);
180 void MojoProxyResolverImpl::Job::OnConnectionError() {
181 resolver_
->DeleteJob(this);
184 void MojoProxyResolverImpl::Job::SendLoadStateChanged(LoadState load_state
) {
185 client_
->LoadStateChanged(load_state
);
188 MojoProxyResolverImpl::SetPacScriptRequest::SetPacScriptRequest(
189 const scoped_refptr
<ProxyResolverScriptData
>& script_data
,
190 const mojo::Callback
<void(int32_t)>& callback
)
191 : script_data(script_data
), callback(callback
) {
194 MojoProxyResolverImpl::SetPacScriptRequest::~SetPacScriptRequest() = default;