| 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 RequestImpl(base::WeakPtr<Job> job); |
| 155 |
| 156 ~RequestImpl() override; |
| 157 |
| 158 LoadState GetLoadState() override; |
| 159 |
| 160 private: |
| 161 base::WeakPtr<Job> job_; |
| 162 }; |
| 163 |
| 152 class ProxyResolverMojo::Job | 164 class ProxyResolverMojo::Job |
| 153 : public ClientMixin<interfaces::ProxyResolverRequestClient> { | 165 : public ClientMixin<interfaces::ProxyResolverRequestClient>, |
| 166 public base::SupportsWeakPtr<Job>, |
| 167 public base::RefCounted<Job> { |
| 154 public: | 168 public: |
| 155 Job(ProxyResolverMojo* resolver, | 169 Job(ProxyResolverMojo* resolver, |
| 156 const GURL& url, | 170 const GURL& url, |
| 157 ProxyInfo* results, | 171 ProxyInfo* results, |
| 158 const CompletionCallback& callback, | 172 const CompletionCallback& callback, |
| 159 const BoundNetLog& net_log); | 173 const BoundNetLog& net_log); |
| 160 ~Job() override; | |
| 161 | 174 |
| 162 // Cancels the job and prevents the callback from being run. | 175 // Cancels the job and prevents the callback from being run. |
| 163 void Cancel(); | 176 void Cancel(); |
| 164 | 177 |
| 165 // Returns the LoadState of this job. | 178 // Returns the LoadState of this job. |
| 166 LoadState GetLoadState(); | 179 LoadState GetLoadState(); |
| 167 | 180 |
| 181 ProxyResolverMojo* GetResolver(); |
| 182 |
| 183 // Delete seld owned refpointer |
| 184 void Remove(); |
| 185 |
| 168 private: | 186 private: |
| 187 ~Job() override; |
| 188 friend class base::RefCounted<Job>; |
| 169 // Mojo error handler. | 189 // Mojo error handler. |
| 170 void OnConnectionError(); | 190 void OnConnectionError(); |
| 171 | 191 |
| 172 // Overridden from interfaces::ProxyResolverRequestClient: | 192 // Overridden from interfaces::ProxyResolverRequestClient: |
| 173 void ReportResult( | 193 void ReportResult( |
| 174 int32_t error, | 194 int32_t error, |
| 175 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) override; | 195 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) override; |
| 176 | 196 |
| 177 ProxyResolverMojo* resolver_; | 197 ProxyResolverMojo* resolver_; |
| 178 const GURL url_; | 198 const GURL url_; |
| 179 ProxyInfo* results_; | 199 ProxyInfo* results_; |
| 180 CompletionCallback callback_; | 200 CompletionCallback callback_; |
| 181 | 201 |
| 182 base::ThreadChecker thread_checker_; | 202 base::ThreadChecker thread_checker_; |
| 183 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; | 203 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; |
| 204 |
| 205 // The job holds a reference to itself to ensure that it remains alive until |
| 206 // either completion or cancellation. This is the only owner. |
| 207 scoped_refptr<Job> owned_self_reference_ = this; |
| 184 }; | 208 }; |
| 185 | 209 |
| 210 ProxyResolverMojo::RequestImpl::RequestImpl(base::WeakPtr<Job> job) |
| 211 : job_(job) {} |
| 212 |
| 213 ProxyResolverMojo::RequestImpl::~RequestImpl() { |
| 214 if (job_) { |
| 215 job_->Cancel(); |
| 216 job_->GetResolver()->RemoveJob(job_.get()); |
| 217 } |
| 218 } |
| 219 |
| 220 LoadState ProxyResolverMojo::RequestImpl::GetLoadState() { |
| 221 CHECK_EQ(1u, job_->GetResolver()->pending_jobs_.count(job_.get())); |
| 222 return job_->GetLoadState(); |
| 223 } |
| 224 |
| 186 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver, | 225 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver, |
| 187 const GURL& url, | 226 const GURL& url, |
| 188 ProxyInfo* results, | 227 ProxyInfo* results, |
| 189 const CompletionCallback& callback, | 228 const CompletionCallback& callback, |
| 190 const BoundNetLog& net_log) | 229 const BoundNetLog& net_log) |
| 191 : ClientMixin<interfaces::ProxyResolverRequestClient>( | 230 : ClientMixin<interfaces::ProxyResolverRequestClient>( |
| 192 resolver->host_resolver_, | 231 resolver->host_resolver_, |
| 193 resolver->error_observer_.get(), | 232 resolver->error_observer_.get(), |
| 194 resolver->net_log_, | 233 resolver->net_log_, |
| 195 net_log), | 234 net_log), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 216 DCHECK(thread_checker_.CalledOnValidThread()); | 255 DCHECK(thread_checker_.CalledOnValidThread()); |
| 217 DCHECK(!callback_.is_null()); | 256 DCHECK(!callback_.is_null()); |
| 218 callback_.Reset(); | 257 callback_.Reset(); |
| 219 } | 258 } |
| 220 | 259 |
| 221 LoadState ProxyResolverMojo::Job::GetLoadState() { | 260 LoadState ProxyResolverMojo::Job::GetLoadState() { |
| 222 return dns_request_in_progress() ? LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT | 261 return dns_request_in_progress() ? LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT |
| 223 : LOAD_STATE_RESOLVING_PROXY_FOR_URL; | 262 : LOAD_STATE_RESOLVING_PROXY_FOR_URL; |
| 224 } | 263 } |
| 225 | 264 |
| 265 ProxyResolverMojo* ProxyResolverMojo::Job::GetResolver() { |
| 266 return resolver_; |
| 267 }; |
| 268 |
| 269 void ProxyResolverMojo::Job::Remove() { |
| 270 owned_self_reference_ = NULL; |
| 271 } |
| 272 |
| 226 void ProxyResolverMojo::Job::OnConnectionError() { | 273 void ProxyResolverMojo::Job::OnConnectionError() { |
| 227 DCHECK(thread_checker_.CalledOnValidThread()); | 274 DCHECK(thread_checker_.CalledOnValidThread()); |
| 228 DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError"; | 275 DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError"; |
| 229 resolver_->RemoveJob(this); | 276 resolver_->RemoveJob(this); |
| 230 } | 277 } |
| 231 | 278 |
| 232 void ProxyResolverMojo::Job::ReportResult( | 279 void ProxyResolverMojo::Job::ReportResult( |
| 233 int32_t error, | 280 int32_t error, |
| 234 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) { | 281 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) { |
| 235 DCHECK(thread_checker_.CalledOnValidThread()); | 282 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 DVLOG(1) << "ProxyResolverMojo::OnConnectionError"; | 319 DVLOG(1) << "ProxyResolverMojo::OnConnectionError"; |
| 273 | 320 |
| 274 // Disconnect from the Mojo proxy resolver service. | 321 // Disconnect from the Mojo proxy resolver service. |
| 275 mojo_proxy_resolver_ptr_.reset(); | 322 mojo_proxy_resolver_ptr_.reset(); |
| 276 } | 323 } |
| 277 | 324 |
| 278 void ProxyResolverMojo::RemoveJob(Job* job) { | 325 void ProxyResolverMojo::RemoveJob(Job* job) { |
| 279 DCHECK(thread_checker_.CalledOnValidThread()); | 326 DCHECK(thread_checker_.CalledOnValidThread()); |
| 280 size_t num_erased = pending_jobs_.erase(job); | 327 size_t num_erased = pending_jobs_.erase(job); |
| 281 DCHECK(num_erased); | 328 DCHECK(num_erased); |
| 282 delete job; | 329 job->Remove(); |
| 283 } | 330 } |
| 284 | 331 |
| 285 int ProxyResolverMojo::GetProxyForURL(const GURL& url, | 332 int ProxyResolverMojo::GetProxyForURL(const GURL& url, |
| 286 ProxyInfo* results, | 333 ProxyInfo* results, |
| 287 const CompletionCallback& callback, | 334 const CompletionCallback& callback, |
| 288 RequestHandle* request, | 335 scoped_ptr<Request>* request, |
| 289 const BoundNetLog& net_log) { | 336 const BoundNetLog& net_log) { |
| 290 DCHECK(thread_checker_.CalledOnValidThread()); | 337 DCHECK(thread_checker_.CalledOnValidThread()); |
| 291 | 338 |
| 292 if (!mojo_proxy_resolver_ptr_) | 339 if (!mojo_proxy_resolver_ptr_) |
| 293 return ERR_PAC_SCRIPT_TERMINATED; | 340 return ERR_PAC_SCRIPT_TERMINATED; |
| 294 | 341 |
| 295 Job* job = new Job(this, url, results, callback, net_log); | 342 // Job owns itselfs. |
| 296 bool inserted = pending_jobs_.insert(job).second; | 343 scoped_refptr<Job> job = new Job(this, url, results, callback, net_log); |
| 344 bool inserted = pending_jobs_.insert(job.get()).second; |
| 297 DCHECK(inserted); | 345 DCHECK(inserted); |
| 298 *request = job; | 346 request->reset(new RequestImpl(job->AsWeakPtr())); |
| 299 | 347 |
| 300 return ERR_IO_PENDING; | 348 return ERR_IO_PENDING; |
| 301 } | 349 } |
| 302 | 350 |
| 303 void ProxyResolverMojo::CancelRequest(RequestHandle request) { | |
| 304 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 305 Job* job = static_cast<Job*>(request); | |
| 306 DCHECK(job); | |
| 307 job->Cancel(); | |
| 308 RemoveJob(job); | |
| 309 } | |
| 310 | 351 |
| 311 LoadState ProxyResolverMojo::GetLoadState(RequestHandle request) const { | |
| 312 Job* job = static_cast<Job*>(request); | |
| 313 CHECK_EQ(1u, pending_jobs_.count(job)); | |
| 314 return job->GetLoadState(); | |
| 315 } | |
| 316 | 352 |
| 317 } // namespace | 353 } // namespace |
| 318 | 354 |
| 319 // A Job to create a ProxyResolver instance. | 355 // A Job to create a ProxyResolver instance. |
| 320 // | 356 // |
| 321 // Note: a Job instance is not tied to a particular resolve request, and hence | 357 // Note: a Job instance is not tied to a particular resolve request, and hence |
| 322 // there is no per-request logging to be done (any netlog events are only sent | 358 // there is no per-request logging to be done (any netlog events are only sent |
| 323 // globally) so this always uses an empty BoundNetLog. | 359 // globally) so this always uses an empty BoundNetLog. |
| 324 class ProxyResolverFactoryMojo::Job | 360 class ProxyResolverFactoryMojo::Job |
| 325 : public ClientMixin<interfaces::ProxyResolverFactoryRequestClient>, | 361 : public ClientMixin<interfaces::ProxyResolverFactoryRequestClient>, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 return ERR_PAC_SCRIPT_FAILED; | 441 return ERR_PAC_SCRIPT_FAILED; |
| 406 } | 442 } |
| 407 request->reset(new Job(this, pac_script, resolver, callback, | 443 request->reset(new Job(this, pac_script, resolver, callback, |
| 408 error_observer_factory_.is_null() | 444 error_observer_factory_.is_null() |
| 409 ? nullptr | 445 ? nullptr |
| 410 : error_observer_factory_.Run())); | 446 : error_observer_factory_.Run())); |
| 411 return ERR_IO_PENDING; | 447 return ERR_IO_PENDING; |
| 412 } | 448 } |
| 413 | 449 |
| 414 } // namespace net | 450 } // namespace net |
| OLD | NEW |