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 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 HostResolver* host_resolver, | 111 HostResolver* host_resolver, |
112 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner, | 112 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner, |
113 scoped_ptr<ProxyResolverErrorObserver> error_observer, | 113 scoped_ptr<ProxyResolverErrorObserver> error_observer, |
114 NetLog* net_log); | 114 NetLog* net_log); |
115 ~ProxyResolverMojo() override; | 115 ~ProxyResolverMojo() override; |
116 | 116 |
117 // ProxyResolver implementation: | 117 // ProxyResolver implementation: |
118 int GetProxyForURL(const GURL& url, | 118 int GetProxyForURL(const GURL& url, |
119 ProxyInfo* results, | 119 ProxyInfo* results, |
120 const net::CompletionCallback& callback, | 120 const net::CompletionCallback& callback, |
121 RequestHandle* request, | 121 scoped_ptr<Request>* request, |
122 const BoundNetLog& net_log) override; | 122 const BoundNetLog& net_log) override; |
123 void CancelRequest(RequestHandle request) override; | |
124 LoadState GetLoadState(RequestHandle request) const override; | |
125 | 123 |
126 private: | 124 private: |
127 class Job; | 125 class Job; |
126 class RequestImpl; | |
127 | |
128 base::ThreadChecker thread_checker_; | |
128 | 129 |
129 // Mojo error handler. | 130 // Mojo error handler. |
130 void OnConnectionError(); | 131 void OnConnectionError(); |
131 | 132 |
132 void RemoveJob(Job* job); | 133 void RemoveJob(Job* job); |
133 | 134 |
134 // Connection to the Mojo proxy resolver. | 135 // Connection to the Mojo proxy resolver. |
135 interfaces::ProxyResolverPtr mojo_proxy_resolver_ptr_; | 136 interfaces::ProxyResolverPtr mojo_proxy_resolver_ptr_; |
136 | 137 |
137 HostResolver* host_resolver_; | 138 HostResolver* host_resolver_; |
138 | 139 |
139 scoped_ptr<ProxyResolverErrorObserver> error_observer_; | 140 scoped_ptr<ProxyResolverErrorObserver> error_observer_; |
140 | 141 |
141 NetLog* net_log_; | 142 NetLog* net_log_; |
142 | 143 |
143 std::set<Job*> pending_jobs_; | 144 std::set<Job*> pending_jobs_; |
144 | 145 |
145 base::ThreadChecker thread_checker_; | |
146 | 146 |
147 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner_; | 147 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner_; |
148 | 148 |
149 DISALLOW_COPY_AND_ASSIGN(ProxyResolverMojo); | 149 DISALLOW_COPY_AND_ASSIGN(ProxyResolverMojo); |
150 }; | 150 }; |
151 | 151 |
152 class ProxyResolverMojo::RequestImpl : public ProxyResolver::Request { | |
153 public: | |
154 explicit RequestImpl(scoped_ptr<Job> job); | |
155 | |
156 ~RequestImpl() override; | |
157 | |
158 LoadState GetLoadState() override; | |
159 | |
160 private: | |
161 scoped_ptr<Job> job_; | |
162 }; | |
163 | |
152 class ProxyResolverMojo::Job | 164 class ProxyResolverMojo::Job |
153 : public ClientMixin<interfaces::ProxyResolverRequestClient> { | 165 : public ClientMixin<interfaces::ProxyResolverRequestClient> { |
154 public: | 166 public: |
155 Job(ProxyResolverMojo* resolver, | 167 Job(ProxyResolverMojo* resolver, |
156 const GURL& url, | 168 const GURL& url, |
157 ProxyInfo* results, | 169 ProxyInfo* results, |
158 const CompletionCallback& callback, | 170 const CompletionCallback& callback, |
159 const BoundNetLog& net_log); | 171 const BoundNetLog& net_log); |
160 ~Job() override; | 172 ~Job() override; |
161 | 173 |
162 // Cancels the job and prevents the callback from being run. | 174 void PacScriptTerminated(); |
eroman
2016/02/24 19:20:29
Delete this (unused)
| |
163 void Cancel(); | |
164 | |
165 // Returns the LoadState of this job. | 175 // Returns the LoadState of this job. |
166 LoadState GetLoadState(); | 176 LoadState GetLoadState(); |
167 | 177 |
178 ProxyResolverMojo* resolver(); | |
179 | |
180 void CompleteRequest(int result); | |
181 | |
168 private: | 182 private: |
183 friend class base::RefCounted<Job>; | |
169 // Mojo error handler. | 184 // Mojo error handler. |
170 void OnConnectionError(); | 185 void OnConnectionError(); |
171 | 186 |
172 // Overridden from interfaces::ProxyResolverRequestClient: | 187 // Overridden from interfaces::ProxyResolverRequestClient: |
173 void ReportResult( | 188 void ReportResult( |
174 int32_t error, | 189 int32_t error, |
175 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) override; | 190 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) override; |
176 | 191 |
177 ProxyResolverMojo* resolver_; | 192 ProxyResolverMojo* resolver_; |
178 const GURL url_; | 193 const GURL url_; |
179 ProxyInfo* results_; | 194 ProxyInfo* results_; |
180 CompletionCallback callback_; | 195 CompletionCallback callback_; |
181 | 196 |
182 base::ThreadChecker thread_checker_; | 197 base::ThreadChecker thread_checker_; |
183 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; | 198 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; |
184 }; | 199 }; |
185 | 200 |
201 ProxyResolverMojo::RequestImpl::RequestImpl(scoped_ptr<Job> job) | |
202 : job_(std::move(job)) {} | |
203 | |
204 ProxyResolverMojo::RequestImpl::~RequestImpl() { | |
205 job_->CompleteRequest(0); | |
eroman
2016/02/24 19:20:29
Instead of 0, this should be ERR_PAC_SCRIPT_TERMIN
| |
206 } | |
207 | |
208 LoadState ProxyResolverMojo::RequestImpl::GetLoadState() { | |
209 CHECK_EQ(1u, job_->resolver()->pending_jobs_.count(job_.get())); | |
210 return job_->GetLoadState(); | |
211 } | |
212 | |
186 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver, | 213 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver, |
187 const GURL& url, | 214 const GURL& url, |
188 ProxyInfo* results, | 215 ProxyInfo* results, |
189 const CompletionCallback& callback, | 216 const CompletionCallback& callback, |
190 const BoundNetLog& net_log) | 217 const BoundNetLog& net_log) |
191 : ClientMixin<interfaces::ProxyResolverRequestClient>( | 218 : ClientMixin<interfaces::ProxyResolverRequestClient>( |
192 resolver->host_resolver_, | 219 resolver->host_resolver_, |
193 resolver->error_observer_.get(), | 220 resolver->error_observer_.get(), |
194 resolver->net_log_, | 221 resolver->net_log_, |
195 net_log), | 222 net_log), |
196 resolver_(resolver), | 223 resolver_(resolver), |
197 url_(url), | 224 url_(url), |
198 results_(results), | 225 results_(results), |
199 callback_(callback), | 226 callback_(callback), |
200 binding_(this) { | 227 binding_(this) { |
201 resolver_->mojo_proxy_resolver_ptr_->GetProxyForUrl( | 228 resolver_->mojo_proxy_resolver_ptr_->GetProxyForUrl( |
202 mojo::String::From(url_), binding_.CreateInterfacePtrAndBind()); | 229 mojo::String::From(url_), binding_.CreateInterfacePtrAndBind()); |
203 binding_.set_connection_error_handler(base::Bind( | 230 binding_.set_connection_error_handler(base::Bind( |
204 &ProxyResolverMojo::Job::OnConnectionError, base::Unretained(this))); | 231 &ProxyResolverMojo::Job::OnConnectionError, base::Unretained(this))); |
205 } | 232 } |
206 | 233 |
207 ProxyResolverMojo::Job::~Job() { | 234 void ProxyResolverMojo::Job::PacScriptTerminated() { |
eroman
2016/02/24 19:20:29
Delete this (unused)
| |
208 DCHECK(thread_checker_.CalledOnValidThread()); | 235 DCHECK(thread_checker_.CalledOnValidThread()); |
209 if (!callback_.is_null()) | 236 if (!callback_.is_null()) |
210 callback_.Run(ERR_PAC_SCRIPT_TERMINATED); | 237 callback_.Run(ERR_PAC_SCRIPT_TERMINATED); |
238 callback_.Reset(); | |
239 resolver_ = nullptr; | |
211 } | 240 } |
212 | 241 |
213 void ProxyResolverMojo::Job::Cancel() { | 242 ProxyResolverMojo::Job::~Job() {} |
214 DCHECK(thread_checker_.CalledOnValidThread()); | |
215 DCHECK(!callback_.is_null()); | |
216 callback_.Reset(); | |
217 } | |
218 | 243 |
219 LoadState ProxyResolverMojo::Job::GetLoadState() { | 244 LoadState ProxyResolverMojo::Job::GetLoadState() { |
220 return dns_request_in_progress() ? LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT | 245 return dns_request_in_progress() ? LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT |
221 : LOAD_STATE_RESOLVING_PROXY_FOR_URL; | 246 : LOAD_STATE_RESOLVING_PROXY_FOR_URL; |
222 } | 247 } |
223 | 248 |
249 ProxyResolverMojo* ProxyResolverMojo::Job::resolver() { | |
250 return resolver_; | |
251 }; | |
252 | |
224 void ProxyResolverMojo::Job::OnConnectionError() { | 253 void ProxyResolverMojo::Job::OnConnectionError() { |
225 DCHECK(thread_checker_.CalledOnValidThread()); | 254 DCHECK(thread_checker_.CalledOnValidThread()); |
226 DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError"; | 255 DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError"; |
227 resolver_->RemoveJob(this); | 256 CompleteRequest(ERR_PAC_SCRIPT_TERMINATED); |
eroman
2016/02/24 19:20:29
Thanks, this is easier to follow!
| |
257 } | |
258 | |
259 void ProxyResolverMojo::Job::CompleteRequest(int result) { | |
260 CompletionCallback callback = callback_; | |
261 callback_.Reset(); | |
262 if (resolver_) | |
263 resolver_->RemoveJob(this); | |
264 resolver_ = nullptr; | |
265 if (!callback.is_null()) | |
266 callback.Run(result); | |
228 } | 267 } |
229 | 268 |
230 void ProxyResolverMojo::Job::ReportResult( | 269 void ProxyResolverMojo::Job::ReportResult( |
231 int32_t error, | 270 int32_t error, |
232 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) { | 271 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) { |
233 DCHECK(thread_checker_.CalledOnValidThread()); | 272 DCHECK(thread_checker_.CalledOnValidThread()); |
234 DVLOG(1) << "ProxyResolverMojo::Job::ReportResult: " << error; | 273 DVLOG(1) << "ProxyResolverMojo::Job::ReportResult: " << error; |
235 | 274 |
236 if (error == OK) { | 275 if (error == OK) { |
237 *results_ = proxy_servers.To<ProxyInfo>(); | 276 *results_ = proxy_servers.To<ProxyInfo>(); |
238 DVLOG(1) << "Servers: " << results_->ToPacString(); | 277 DVLOG(1) << "Servers: " << results_->ToPacString(); |
239 } | 278 } |
240 | 279 |
241 CompletionCallback callback = callback_; | 280 CompleteRequest(error); |
242 callback_.Reset(); | |
243 resolver_->RemoveJob(this); | |
244 callback.Run(error); | |
245 } | 281 } |
246 | 282 |
247 ProxyResolverMojo::ProxyResolverMojo( | 283 ProxyResolverMojo::ProxyResolverMojo( |
248 interfaces::ProxyResolverPtr resolver_ptr, | 284 interfaces::ProxyResolverPtr resolver_ptr, |
249 HostResolver* host_resolver, | 285 HostResolver* host_resolver, |
250 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner, | 286 scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner, |
251 scoped_ptr<ProxyResolverErrorObserver> error_observer, | 287 scoped_ptr<ProxyResolverErrorObserver> error_observer, |
252 NetLog* net_log) | 288 NetLog* net_log) |
253 : mojo_proxy_resolver_ptr_(std::move(resolver_ptr)), | 289 : mojo_proxy_resolver_ptr_(std::move(resolver_ptr)), |
254 host_resolver_(host_resolver), | 290 host_resolver_(host_resolver), |
(...skipping 13 matching lines...) Expand all Loading... | |
268 void ProxyResolverMojo::OnConnectionError() { | 304 void ProxyResolverMojo::OnConnectionError() { |
269 DCHECK(thread_checker_.CalledOnValidThread()); | 305 DCHECK(thread_checker_.CalledOnValidThread()); |
270 DVLOG(1) << "ProxyResolverMojo::OnConnectionError"; | 306 DVLOG(1) << "ProxyResolverMojo::OnConnectionError"; |
271 | 307 |
272 // Disconnect from the Mojo proxy resolver service. | 308 // Disconnect from the Mojo proxy resolver service. |
273 mojo_proxy_resolver_ptr_.reset(); | 309 mojo_proxy_resolver_ptr_.reset(); |
274 } | 310 } |
275 | 311 |
276 void ProxyResolverMojo::RemoveJob(Job* job) { | 312 void ProxyResolverMojo::RemoveJob(Job* job) { |
277 DCHECK(thread_checker_.CalledOnValidThread()); | 313 DCHECK(thread_checker_.CalledOnValidThread()); |
278 size_t num_erased = pending_jobs_.erase(job); | 314 pending_jobs_.erase(job); |
279 DCHECK(num_erased); | |
280 delete job; | |
281 } | 315 } |
282 | 316 |
283 int ProxyResolverMojo::GetProxyForURL(const GURL& url, | 317 int ProxyResolverMojo::GetProxyForURL(const GURL& url, |
284 ProxyInfo* results, | 318 ProxyInfo* results, |
285 const CompletionCallback& callback, | 319 const CompletionCallback& callback, |
286 RequestHandle* request, | 320 scoped_ptr<Request>* request, |
287 const BoundNetLog& net_log) { | 321 const BoundNetLog& net_log) { |
288 DCHECK(thread_checker_.CalledOnValidThread()); | 322 DCHECK(thread_checker_.CalledOnValidThread()); |
289 | 323 |
290 if (!mojo_proxy_resolver_ptr_) | 324 if (!mojo_proxy_resolver_ptr_) |
291 return ERR_PAC_SCRIPT_TERMINATED; | 325 return ERR_PAC_SCRIPT_TERMINATED; |
292 | 326 |
293 Job* job = new Job(this, url, results, callback, net_log); | 327 scoped_ptr<Job> job(new Job(this, url, results, callback, net_log)); |
294 bool inserted = pending_jobs_.insert(job).second; | 328 bool inserted = pending_jobs_.insert(job.get()).second; |
295 DCHECK(inserted); | 329 DCHECK(inserted); |
296 *request = job; | 330 request->reset(new RequestImpl(std::move(job))); |
297 | 331 |
298 return ERR_IO_PENDING; | 332 return ERR_IO_PENDING; |
299 } | 333 } |
300 | 334 |
301 void ProxyResolverMojo::CancelRequest(RequestHandle request) { | |
302 DCHECK(thread_checker_.CalledOnValidThread()); | |
303 Job* job = static_cast<Job*>(request); | |
304 DCHECK(job); | |
305 job->Cancel(); | |
306 RemoveJob(job); | |
307 } | |
308 | 335 |
309 LoadState ProxyResolverMojo::GetLoadState(RequestHandle request) const { | |
310 Job* job = static_cast<Job*>(request); | |
311 CHECK_EQ(1u, pending_jobs_.count(job)); | |
312 return job->GetLoadState(); | |
313 } | |
314 | 336 |
315 } // namespace | 337 } // namespace |
316 | 338 |
317 // A Job to create a ProxyResolver instance. | 339 // A Job to create a ProxyResolver instance. |
318 // | 340 // |
319 // Note: a Job instance is not tied to a particular resolve request, and hence | 341 // Note: a Job instance is not tied to a particular resolve request, and hence |
320 // there is no per-request logging to be done (any netlog events are only sent | 342 // there is no per-request logging to be done (any netlog events are only sent |
321 // globally) so this always uses an empty BoundNetLog. | 343 // globally) so this always uses an empty BoundNetLog. |
322 class ProxyResolverFactoryMojo::Job | 344 class ProxyResolverFactoryMojo::Job |
323 : public ClientMixin<interfaces::ProxyResolverFactoryRequestClient>, | 345 : public ClientMixin<interfaces::ProxyResolverFactoryRequestClient>, |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
401 return ERR_PAC_SCRIPT_FAILED; | 423 return ERR_PAC_SCRIPT_FAILED; |
402 } | 424 } |
403 request->reset(new Job(this, pac_script, resolver, callback, | 425 request->reset(new Job(this, pac_script, resolver, callback, |
404 error_observer_factory_.is_null() | 426 error_observer_factory_.is_null() |
405 ? nullptr | 427 ? nullptr |
406 : error_observer_factory_.Run())); | 428 : error_observer_factory_.Run())); |
407 return ERR_IO_PENDING; | 429 return ERR_IO_PENDING; |
408 } | 430 } |
409 | 431 |
410 } // namespace net | 432 } // namespace net |
OLD | NEW |