OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/proxy/proxy_resolver_factory_mojo.h" | 5 #include "net/proxy/proxy_resolver_factory_mojo.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "mojo/common/common_type_converters.h" | 14 #include "mojo/common/common_type_converters.h" |
| 15 #include "mojo/common/strong_binding_set.h" |
15 #include "mojo/common/url_type_converters.h" | 16 #include "mojo/common/url_type_converters.h" |
16 #include "mojo/public/cpp/bindings/binding.h" | 17 #include "mojo/public/cpp/bindings/binding.h" |
17 #include "net/base/load_states.h" | 18 #include "net/base/load_states.h" |
18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
19 #include "net/dns/mojo_host_resolver_impl.h" | 20 #include "net/dns/mojo_host_resolver_impl.h" |
20 #include "net/interfaces/host_resolver_service.mojom.h" | 21 #include "net/interfaces/host_resolver_service.mojom.h" |
21 #include "net/interfaces/proxy_resolver_service.mojom.h" | 22 #include "net/interfaces/proxy_resolver_service.mojom.h" |
22 #include "net/proxy/mojo_proxy_resolver_factory.h" | 23 #include "net/proxy/mojo_proxy_resolver_factory.h" |
23 #include "net/proxy/mojo_proxy_type_converters.h" | 24 #include "net/proxy/mojo_proxy_type_converters.h" |
24 #include "net/proxy/proxy_info.h" | 25 #include "net/proxy/proxy_info.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 // ProxyResolver implementation: | 116 // ProxyResolver implementation: |
116 int GetProxyForURL(const GURL& url, | 117 int GetProxyForURL(const GURL& url, |
117 ProxyInfo* results, | 118 ProxyInfo* results, |
118 const net::CompletionCallback& callback, | 119 const net::CompletionCallback& callback, |
119 RequestHandle* request, | 120 RequestHandle* request, |
120 const BoundNetLog& net_log) override; | 121 const BoundNetLog& net_log) override; |
121 void CancelRequest(RequestHandle request) override; | 122 void CancelRequest(RequestHandle request) override; |
122 LoadState GetLoadState(RequestHandle request) const override; | 123 LoadState GetLoadState(RequestHandle request) const override; |
123 | 124 |
124 private: | 125 private: |
125 class Job; | 126 class Job : public ClientMixin<interfaces::ProxyResolverRequestClient> { |
| 127 public: |
| 128 Job(ProxyResolverMojo* resolver, |
| 129 ProxyInfo* results, |
| 130 const CompletionCallback& callback, |
| 131 const BoundNetLog& net_log); |
| 132 ~Job() override; |
| 133 |
| 134 // Cancels the job and prevents the callback from being run. |
| 135 void Cancel(); |
| 136 |
| 137 // Returns the LoadState of this job. |
| 138 LoadState GetLoadState(); |
| 139 |
| 140 private: |
| 141 // Overridden from interfaces::ProxyResolverRequestClient: |
| 142 void ReportResult( |
| 143 int32_t error, |
| 144 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) override; |
| 145 |
| 146 ProxyResolverMojo* resolver_; |
| 147 ProxyInfo* results_; |
| 148 CompletionCallback callback_; |
| 149 |
| 150 base::ThreadChecker thread_checker_; |
| 151 }; |
126 | 152 |
127 // Mojo error handler. | 153 // Mojo error handler. |
128 void OnConnectionError(); | 154 void OnConnectionError(); |
129 | 155 |
130 void RemoveJob(Job* job); | |
131 | |
132 // Connection to the Mojo proxy resolver. | 156 // Connection to the Mojo proxy resolver. |
133 interfaces::ProxyResolverPtr mojo_proxy_resolver_ptr_; | 157 interfaces::ProxyResolverPtr mojo_proxy_resolver_ptr_; |
134 | 158 |
135 HostResolver* host_resolver_; | 159 HostResolver* host_resolver_; |
136 | 160 |
137 scoped_ptr<ProxyResolverErrorObserver> error_observer_; | 161 scoped_ptr<ProxyResolverErrorObserver> error_observer_; |
138 | 162 |
139 NetLog* net_log_; | 163 NetLog* net_log_; |
140 | 164 |
141 std::set<Job*> pending_jobs_; | 165 mojo::StrongBindingSet<Job> pending_jobs_; |
142 | 166 |
143 base::ThreadChecker thread_checker_; | 167 base::ThreadChecker thread_checker_; |
144 | 168 |
145 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner_; | 169 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner_; |
146 | 170 |
147 DISALLOW_COPY_AND_ASSIGN(ProxyResolverMojo); | 171 DISALLOW_COPY_AND_ASSIGN(ProxyResolverMojo); |
148 }; | 172 }; |
149 | 173 |
150 class ProxyResolverMojo::Job | |
151 : public ClientMixin<interfaces::ProxyResolverRequestClient> { | |
152 public: | |
153 Job(ProxyResolverMojo* resolver, | |
154 const GURL& url, | |
155 ProxyInfo* results, | |
156 const CompletionCallback& callback, | |
157 const BoundNetLog& net_log); | |
158 ~Job() override; | |
159 | |
160 // Cancels the job and prevents the callback from being run. | |
161 void Cancel(); | |
162 | |
163 // Returns the LoadState of this job. | |
164 LoadState GetLoadState(); | |
165 | |
166 private: | |
167 // Mojo error handler. | |
168 void OnConnectionError(); | |
169 | |
170 // Overridden from interfaces::ProxyResolverRequestClient: | |
171 void ReportResult( | |
172 int32_t error, | |
173 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) override; | |
174 | |
175 ProxyResolverMojo* resolver_; | |
176 const GURL url_; | |
177 ProxyInfo* results_; | |
178 CompletionCallback callback_; | |
179 | |
180 base::ThreadChecker thread_checker_; | |
181 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; | |
182 }; | |
183 | |
184 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver, | 174 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver, |
185 const GURL& url, | |
186 ProxyInfo* results, | 175 ProxyInfo* results, |
187 const CompletionCallback& callback, | 176 const CompletionCallback& callback, |
188 const BoundNetLog& net_log) | 177 const BoundNetLog& net_log) |
189 : ClientMixin<interfaces::ProxyResolverRequestClient>( | 178 : ClientMixin<interfaces::ProxyResolverRequestClient>( |
190 resolver->host_resolver_, | 179 resolver->host_resolver_, |
191 resolver->error_observer_.get(), | 180 resolver->error_observer_.get(), |
192 resolver->net_log_, | 181 resolver->net_log_, |
193 net_log), | 182 net_log), |
194 resolver_(resolver), | 183 resolver_(resolver), |
195 url_(url), | |
196 results_(results), | 184 results_(results), |
197 callback_(callback), | 185 callback_(callback) {} |
198 binding_(this) { | |
199 binding_.set_connection_error_handler(base::Bind( | |
200 &ProxyResolverMojo::Job::OnConnectionError, base::Unretained(this))); | |
201 | |
202 interfaces::ProxyResolverRequestClientPtr client_ptr; | |
203 binding_.Bind(mojo::GetProxy(&client_ptr)); | |
204 resolver_->mojo_proxy_resolver_ptr_->GetProxyForUrl(mojo::String::From(url_), | |
205 client_ptr.Pass()); | |
206 } | |
207 | 186 |
208 ProxyResolverMojo::Job::~Job() { | 187 ProxyResolverMojo::Job::~Job() { |
209 DCHECK(thread_checker_.CalledOnValidThread()); | 188 DCHECK(thread_checker_.CalledOnValidThread()); |
210 if (!callback_.is_null()) | 189 if (!callback_.is_null()) |
211 callback_.Run(ERR_PAC_SCRIPT_TERMINATED); | 190 callback_.Run(ERR_PAC_SCRIPT_TERMINATED); |
212 } | 191 } |
213 | 192 |
214 void ProxyResolverMojo::Job::Cancel() { | 193 void ProxyResolverMojo::Job::Cancel() { |
215 DCHECK(thread_checker_.CalledOnValidThread()); | 194 DCHECK(thread_checker_.CalledOnValidThread()); |
216 DCHECK(!callback_.is_null()); | 195 DCHECK(!callback_.is_null()); |
217 callback_.Reset(); | 196 callback_.Reset(); |
218 } | 197 } |
219 | 198 |
220 LoadState ProxyResolverMojo::Job::GetLoadState() { | 199 LoadState ProxyResolverMojo::Job::GetLoadState() { |
221 return dns_request_in_progress() ? LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT | 200 return dns_request_in_progress() ? LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT |
222 : LOAD_STATE_RESOLVING_PROXY_FOR_URL; | 201 : LOAD_STATE_RESOLVING_PROXY_FOR_URL; |
223 } | 202 } |
224 | 203 |
225 void ProxyResolverMojo::Job::OnConnectionError() { | |
226 DCHECK(thread_checker_.CalledOnValidThread()); | |
227 DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError"; | |
228 resolver_->RemoveJob(this); | |
229 } | |
230 | |
231 void ProxyResolverMojo::Job::ReportResult( | 204 void ProxyResolverMojo::Job::ReportResult( |
232 int32_t error, | 205 int32_t error, |
233 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) { | 206 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) { |
234 DCHECK(thread_checker_.CalledOnValidThread()); | 207 DCHECK(thread_checker_.CalledOnValidThread()); |
235 DVLOG(1) << "ProxyResolverMojo::Job::ReportResult: " << error; | 208 DVLOG(1) << "ProxyResolverMojo::Job::ReportResult: " << error; |
236 | 209 |
237 if (error == OK) { | 210 if (error == OK) { |
238 *results_ = proxy_servers.To<ProxyInfo>(); | 211 *results_ = proxy_servers.To<ProxyInfo>(); |
239 DVLOG(1) << "Servers: " << results_->ToPacString(); | 212 DVLOG(1) << "Servers: " << results_->ToPacString(); |
240 } | 213 } |
241 | 214 |
242 CompletionCallback callback = callback_; | 215 CompletionCallback callback = callback_; |
243 callback_.Reset(); | 216 callback_.Reset(); |
244 resolver_->RemoveJob(this); | 217 resolver_->pending_jobs_.DestroyService(this); |
245 callback.Run(error); | 218 callback.Run(error); |
246 } | 219 } |
247 | 220 |
248 ProxyResolverMojo::ProxyResolverMojo( | 221 ProxyResolverMojo::ProxyResolverMojo( |
249 interfaces::ProxyResolverPtr resolver_ptr, | 222 interfaces::ProxyResolverPtr resolver_ptr, |
250 HostResolver* host_resolver, | 223 HostResolver* host_resolver, |
251 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner, | 224 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner, |
252 scoped_ptr<ProxyResolverErrorObserver> error_observer, | 225 scoped_ptr<ProxyResolverErrorObserver> error_observer, |
253 NetLog* net_log) | 226 NetLog* net_log) |
254 : mojo_proxy_resolver_ptr_(resolver_ptr.Pass()), | 227 : mojo_proxy_resolver_ptr_(resolver_ptr.Pass()), |
(...skipping 12 matching lines...) Expand all Loading... |
267 } | 240 } |
268 | 241 |
269 void ProxyResolverMojo::OnConnectionError() { | 242 void ProxyResolverMojo::OnConnectionError() { |
270 DCHECK(thread_checker_.CalledOnValidThread()); | 243 DCHECK(thread_checker_.CalledOnValidThread()); |
271 DVLOG(1) << "ProxyResolverMojo::OnConnectionError"; | 244 DVLOG(1) << "ProxyResolverMojo::OnConnectionError"; |
272 | 245 |
273 // Disconnect from the Mojo proxy resolver service. | 246 // Disconnect from the Mojo proxy resolver service. |
274 mojo_proxy_resolver_ptr_.reset(); | 247 mojo_proxy_resolver_ptr_.reset(); |
275 } | 248 } |
276 | 249 |
277 void ProxyResolverMojo::RemoveJob(Job* job) { | |
278 DCHECK(thread_checker_.CalledOnValidThread()); | |
279 size_t num_erased = pending_jobs_.erase(job); | |
280 DCHECK(num_erased); | |
281 delete job; | |
282 } | |
283 | |
284 int ProxyResolverMojo::GetProxyForURL(const GURL& url, | 250 int ProxyResolverMojo::GetProxyForURL(const GURL& url, |
285 ProxyInfo* results, | 251 ProxyInfo* results, |
286 const CompletionCallback& callback, | 252 const CompletionCallback& callback, |
287 RequestHandle* request, | 253 RequestHandle* request, |
288 const BoundNetLog& net_log) { | 254 const BoundNetLog& net_log) { |
289 DCHECK(thread_checker_.CalledOnValidThread()); | 255 DCHECK(thread_checker_.CalledOnValidThread()); |
290 | 256 |
291 if (!mojo_proxy_resolver_ptr_) | 257 if (!mojo_proxy_resolver_ptr_) |
292 return ERR_PAC_SCRIPT_TERMINATED; | 258 return ERR_PAC_SCRIPT_TERMINATED; |
293 | 259 |
294 Job* job = new Job(this, url, results, callback, net_log); | 260 interfaces::ProxyResolverRequestClientPtr client_ptr; |
295 bool inserted = pending_jobs_.insert(job).second; | 261 *request = pending_jobs_.EmplaceService(mojo::GetProxy(&client_ptr), this, |
296 DCHECK(inserted); | 262 results, callback, net_log); |
297 *request = job; | 263 mojo_proxy_resolver_ptr_->GetProxyForUrl(mojo::String::From(url), |
298 | 264 client_ptr.Pass()); |
299 return ERR_IO_PENDING; | 265 return ERR_IO_PENDING; |
300 } | 266 } |
301 | 267 |
302 void ProxyResolverMojo::CancelRequest(RequestHandle request) { | 268 void ProxyResolverMojo::CancelRequest(RequestHandle request) { |
303 DCHECK(thread_checker_.CalledOnValidThread()); | 269 DCHECK(thread_checker_.CalledOnValidThread()); |
304 Job* job = static_cast<Job*>(request); | 270 pending_jobs_.DestroyService(static_cast<Job*>(request)); |
305 DCHECK(job); | |
306 job->Cancel(); | |
307 RemoveJob(job); | |
308 } | 271 } |
309 | 272 |
310 LoadState ProxyResolverMojo::GetLoadState(RequestHandle request) const { | 273 LoadState ProxyResolverMojo::GetLoadState(RequestHandle request) const { |
311 Job* job = static_cast<Job*>(request); | 274 Job* job = static_cast<Job*>(request); |
312 CHECK_EQ(1u, pending_jobs_.count(job)); | |
313 return job->GetLoadState(); | 275 return job->GetLoadState(); |
314 } | 276 } |
315 | 277 |
316 } // namespace | 278 } // namespace |
317 | 279 |
318 // A Job to create a ProxyResolver instance. | 280 // A Job to create a ProxyResolver instance. |
319 // | 281 // |
320 // Note: a Job instance is not tied to a particular resolve request, and hence | 282 // Note: a Job instance is not tied to a particular resolve request, and hence |
321 // there is no per-request logging to be done (any netlog events are only sent | 283 // there is no per-request logging to be done (any netlog events are only sent |
322 // globally) so this always uses an empty BoundNetLog. | 284 // globally) so this always uses an empty BoundNetLog. |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 return ERR_PAC_SCRIPT_FAILED; | 366 return ERR_PAC_SCRIPT_FAILED; |
405 } | 367 } |
406 request->reset(new Job(this, pac_script, resolver, callback, | 368 request->reset(new Job(this, pac_script, resolver, callback, |
407 error_observer_factory_.is_null() | 369 error_observer_factory_.is_null() |
408 ? nullptr | 370 ? nullptr |
409 : error_observer_factory_.Run())); | 371 : error_observer_factory_.Run())); |
410 return ERR_IO_PENDING; | 372 return ERR_IO_PENDING; |
411 } | 373 } |
412 | 374 |
413 } // namespace net | 375 } // namespace net |
OLD | NEW |