| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/base/host_resolver_impl.h" | 5 #include "net/base/host_resolver_impl.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <Winsock2.h> | 8 #include <Winsock2.h> |
| 9 #elif defined(OS_POSIX) | 9 #elif defined(OS_POSIX) |
| 10 #include <netdb.h> | 10 #include <netdb.h> |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include <cmath> | 13 #include <cmath> |
| 14 #include <deque> | 14 #include <deque> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
| 18 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 19 #include "base/debug/debugger.h" | 19 #include "base/debug/debugger.h" |
| 20 #include "base/debug/stack_trace.h" | 20 #include "base/debug/stack_trace.h" |
| 21 #include "base/lock.h" | |
| 22 #include "base/message_loop.h" | 21 #include "base/message_loop.h" |
| 23 #include "base/metrics/field_trial.h" | 22 #include "base/metrics/field_trial.h" |
| 24 #include "base/metrics/histogram.h" | 23 #include "base/metrics/histogram.h" |
| 25 #include "base/stl_util-inl.h" | 24 #include "base/stl_util-inl.h" |
| 26 #include "base/string_util.h" | 25 #include "base/string_util.h" |
| 26 #include "base/synchronization/lock.h" |
| 27 #include "base/threading/worker_pool.h" | 27 #include "base/threading/worker_pool.h" |
| 28 #include "base/time.h" | 28 #include "base/time.h" |
| 29 #include "base/utf_string_conversions.h" | 29 #include "base/utf_string_conversions.h" |
| 30 #include "base/values.h" | 30 #include "base/values.h" |
| 31 #include "net/base/address_list.h" | 31 #include "net/base/address_list.h" |
| 32 #include "net/base/address_list_net_log_param.h" | 32 #include "net/base/address_list_net_log_param.h" |
| 33 #include "net/base/host_port_pair.h" | 33 #include "net/base/host_port_pair.h" |
| 34 #include "net/base/host_resolver_proc.h" | 34 #include "net/base/host_resolver_proc.h" |
| 35 #include "net/base/net_errors.h" | 35 #include "net/base/net_errors.h" |
| 36 #include "net/base/net_log.h" | 36 #include "net/base/net_log.h" |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 // Cancels the current job. Callable from origin thread. | 398 // Cancels the current job. Callable from origin thread. |
| 399 void Cancel() { | 399 void Cancel() { |
| 400 net_log_.AddEvent(NetLog::TYPE_CANCELLED, NULL); | 400 net_log_.AddEvent(NetLog::TYPE_CANCELLED, NULL); |
| 401 | 401 |
| 402 HostResolver* resolver = resolver_; | 402 HostResolver* resolver = resolver_; |
| 403 resolver_ = NULL; | 403 resolver_ = NULL; |
| 404 | 404 |
| 405 // Mark the job as cancelled, so when worker thread completes it will | 405 // Mark the job as cancelled, so when worker thread completes it will |
| 406 // not try to post completion to origin loop. | 406 // not try to post completion to origin loop. |
| 407 { | 407 { |
| 408 AutoLock locked(origin_loop_lock_); | 408 base::AutoLock locked(origin_loop_lock_); |
| 409 origin_loop_ = NULL; | 409 origin_loop_ = NULL; |
| 410 } | 410 } |
| 411 | 411 |
| 412 // End here to prevent issues when a Job outlives the HostResolver that | 412 // End here to prevent issues when a Job outlives the HostResolver that |
| 413 // spawned it. | 413 // spawned it. |
| 414 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, NULL); | 414 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, NULL); |
| 415 | 415 |
| 416 // We will call HostResolverImpl::CancelRequest(Request*) on each one | 416 // We will call HostResolverImpl::CancelRequest(Request*) on each one |
| 417 // in order to notify any observers. | 417 // in order to notify any observers. |
| 418 for (RequestsList::const_iterator it = requests_.begin(); | 418 for (RequestsList::const_iterator it = requests_.begin(); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 error_ = ResolveAddrInfo(resolver_proc_, | 475 error_ = ResolveAddrInfo(resolver_proc_, |
| 476 key_.hostname, | 476 key_.hostname, |
| 477 key_.address_family, | 477 key_.address_family, |
| 478 key_.host_resolver_flags, | 478 key_.host_resolver_flags, |
| 479 &results_, | 479 &results_, |
| 480 &os_error_); | 480 &os_error_); |
| 481 | 481 |
| 482 // The origin loop could go away while we are trying to post to it, so we | 482 // The origin loop could go away while we are trying to post to it, so we |
| 483 // need to call its PostTask method inside a lock. See ~HostResolver. | 483 // need to call its PostTask method inside a lock. See ~HostResolver. |
| 484 { | 484 { |
| 485 AutoLock locked(origin_loop_lock_); | 485 base::AutoLock locked(origin_loop_lock_); |
| 486 if (origin_loop_) { | 486 if (origin_loop_) { |
| 487 origin_loop_->PostTask(FROM_HERE, | 487 origin_loop_->PostTask(FROM_HERE, |
| 488 NewRunnableMethod(this, &Job::OnLookupComplete)); | 488 NewRunnableMethod(this, &Job::OnLookupComplete)); |
| 489 } | 489 } |
| 490 } | 490 } |
| 491 } | 491 } |
| 492 | 492 |
| 493 // Callback for when DoLookup() completes (runs on origin thread). | 493 // Callback for when DoLookup() completes (runs on origin thread). |
| 494 void OnLookupComplete() { | 494 void OnLookupComplete() { |
| 495 // Should be running on origin loop. | 495 // Should be running on origin loop. |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 const int id_; | 596 const int id_; |
| 597 | 597 |
| 598 // Set on the origin thread, read on the worker thread. | 598 // Set on the origin thread, read on the worker thread. |
| 599 Key key_; | 599 Key key_; |
| 600 | 600 |
| 601 // Only used on the origin thread (where Resolve was called). | 601 // Only used on the origin thread (where Resolve was called). |
| 602 HostResolverImpl* resolver_; | 602 HostResolverImpl* resolver_; |
| 603 RequestsList requests_; // The requests waiting on this job. | 603 RequestsList requests_; // The requests waiting on this job. |
| 604 | 604 |
| 605 // Used to post ourselves onto the origin thread. | 605 // Used to post ourselves onto the origin thread. |
| 606 Lock origin_loop_lock_; | 606 base::Lock origin_loop_lock_; |
| 607 MessageLoop* origin_loop_; | 607 MessageLoop* origin_loop_; |
| 608 | 608 |
| 609 // Hold an owning reference to the HostResolverProc that we are going to use. | 609 // Hold an owning reference to the HostResolverProc that we are going to use. |
| 610 // This may not be the current resolver procedure by the time we call | 610 // This may not be the current resolver procedure by the time we call |
| 611 // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning | 611 // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning |
| 612 // reference ensures that it remains valid until we are done. | 612 // reference ensures that it remains valid until we are done. |
| 613 scoped_refptr<HostResolverProc> resolver_proc_; | 613 scoped_refptr<HostResolverProc> resolver_proc_; |
| 614 | 614 |
| 615 // Assigned on the worker thread, read on the origin thread. | 615 // Assigned on the worker thread, read on the origin thread. |
| 616 int error_; | 616 int error_; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 654 FROM_HERE, NewRunnableMethod(this, &IPv6ProbeJob::DoProbe), kIsSlow); | 654 FROM_HERE, NewRunnableMethod(this, &IPv6ProbeJob::DoProbe), kIsSlow); |
| 655 } | 655 } |
| 656 | 656 |
| 657 // Cancels the current job. | 657 // Cancels the current job. |
| 658 void Cancel() { | 658 void Cancel() { |
| 659 if (was_cancelled()) | 659 if (was_cancelled()) |
| 660 return; | 660 return; |
| 661 DCHECK(IsOnOriginThread()); | 661 DCHECK(IsOnOriginThread()); |
| 662 resolver_ = NULL; // Read/write ONLY on origin thread. | 662 resolver_ = NULL; // Read/write ONLY on origin thread. |
| 663 { | 663 { |
| 664 AutoLock locked(origin_loop_lock_); | 664 base::AutoLock locked(origin_loop_lock_); |
| 665 // Origin loop may be destroyed before we can use it! | 665 // Origin loop may be destroyed before we can use it! |
| 666 origin_loop_ = NULL; // Write ONLY on origin thread. | 666 origin_loop_ = NULL; // Write ONLY on origin thread. |
| 667 } | 667 } |
| 668 } | 668 } |
| 669 | 669 |
| 670 private: | 670 private: |
| 671 friend class base::RefCountedThreadSafe<HostResolverImpl::IPv6ProbeJob>; | 671 friend class base::RefCountedThreadSafe<HostResolverImpl::IPv6ProbeJob>; |
| 672 | 672 |
| 673 ~IPv6ProbeJob() { | 673 ~IPv6ProbeJob() { |
| 674 } | 674 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 688 // Do actual testing on this thread, as it takes 40-100ms. | 688 // Do actual testing on this thread, as it takes 40-100ms. |
| 689 AddressFamily family = IPv6Supported() ? ADDRESS_FAMILY_UNSPECIFIED | 689 AddressFamily family = IPv6Supported() ? ADDRESS_FAMILY_UNSPECIFIED |
| 690 : ADDRESS_FAMILY_IPV4; | 690 : ADDRESS_FAMILY_IPV4; |
| 691 | 691 |
| 692 Task* reply = NewRunnableMethod(this, &IPv6ProbeJob::OnProbeComplete, | 692 Task* reply = NewRunnableMethod(this, &IPv6ProbeJob::OnProbeComplete, |
| 693 family); | 693 family); |
| 694 | 694 |
| 695 // The origin loop could go away while we are trying to post to it, so we | 695 // The origin loop could go away while we are trying to post to it, so we |
| 696 // need to call its PostTask method inside a lock. See ~HostResolver. | 696 // need to call its PostTask method inside a lock. See ~HostResolver. |
| 697 { | 697 { |
| 698 AutoLock locked(origin_loop_lock_); | 698 base::AutoLock locked(origin_loop_lock_); |
| 699 if (origin_loop_) { | 699 if (origin_loop_) { |
| 700 origin_loop_->PostTask(FROM_HERE, reply); | 700 origin_loop_->PostTask(FROM_HERE, reply); |
| 701 return; | 701 return; |
| 702 } | 702 } |
| 703 } | 703 } |
| 704 | 704 |
| 705 // We didn't post, so delete the reply. | 705 // We didn't post, so delete the reply. |
| 706 delete reply; | 706 delete reply; |
| 707 } | 707 } |
| 708 | 708 |
| 709 // Callback for when DoProbe() completes (runs on origin thread). | 709 // Callback for when DoProbe() completes (runs on origin thread). |
| 710 void OnProbeComplete(AddressFamily address_family) { | 710 void OnProbeComplete(AddressFamily address_family) { |
| 711 if (was_cancelled()) | 711 if (was_cancelled()) |
| 712 return; | 712 return; |
| 713 DCHECK(IsOnOriginThread()); | 713 DCHECK(IsOnOriginThread()); |
| 714 resolver_->IPv6ProbeSetDefaultAddressFamily(address_family); | 714 resolver_->IPv6ProbeSetDefaultAddressFamily(address_family); |
| 715 } | 715 } |
| 716 | 716 |
| 717 bool IsOnOriginThread() const { | 717 bool IsOnOriginThread() const { |
| 718 return !MessageLoop::current() || origin_loop_ == MessageLoop::current(); | 718 return !MessageLoop::current() || origin_loop_ == MessageLoop::current(); |
| 719 } | 719 } |
| 720 | 720 |
| 721 // Used/set only on origin thread. | 721 // Used/set only on origin thread. |
| 722 HostResolverImpl* resolver_; | 722 HostResolverImpl* resolver_; |
| 723 | 723 |
| 724 // Used to post ourselves onto the origin thread. | 724 // Used to post ourselves onto the origin thread. |
| 725 Lock origin_loop_lock_; | 725 base::Lock origin_loop_lock_; |
| 726 MessageLoop* origin_loop_; | 726 MessageLoop* origin_loop_; |
| 727 | 727 |
| 728 DISALLOW_COPY_AND_ASSIGN(IPv6ProbeJob); | 728 DISALLOW_COPY_AND_ASSIGN(IPv6ProbeJob); |
| 729 }; | 729 }; |
| 730 | 730 |
| 731 //----------------------------------------------------------------------------- | 731 //----------------------------------------------------------------------------- |
| 732 | 732 |
| 733 // We rely on the priority enum values being sequential having starting at 0, | 733 // We rely on the priority enum values being sequential having starting at 0, |
| 734 // and increasing for lower priorities. | 734 // and increasing for lower priorities. |
| 735 COMPILE_ASSERT(HIGHEST == 0u && | 735 COMPILE_ASSERT(HIGHEST == 0u && |
| (...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1452 job_pools_[i]->ResetNumOutstandingJobs(); | 1452 job_pools_[i]->ResetNumOutstandingJobs(); |
| 1453 JobMap jobs; | 1453 JobMap jobs; |
| 1454 jobs.swap(jobs_); | 1454 jobs.swap(jobs_); |
| 1455 for (JobMap::iterator it = jobs.begin(); it != jobs.end(); ++it) { | 1455 for (JobMap::iterator it = jobs.begin(); it != jobs.end(); ++it) { |
| 1456 AbortJob(it->second); | 1456 AbortJob(it->second); |
| 1457 it->second->Cancel(); | 1457 it->second->Cancel(); |
| 1458 } | 1458 } |
| 1459 } | 1459 } |
| 1460 | 1460 |
| 1461 } // namespace net | 1461 } // namespace net |
| OLD | NEW |