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