| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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.h" | 5 #include "net/base/host_resolver.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <ws2tcpip.h> | 8 #include <ws2tcpip.h> |
| 9 #include <wspiapi.h> // Needed for Win2k compat. | 9 #include <wspiapi.h> // Needed for Win2k compat. |
| 10 #elif defined(OS_POSIX) | 10 #elif defined(OS_POSIX) |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 // Assigned on the worker thread, read on the origin thread. | 391 // Assigned on the worker thread, read on the origin thread. |
| 392 int error_; | 392 int error_; |
| 393 struct addrinfo* results_; | 393 struct addrinfo* results_; |
| 394 | 394 |
| 395 DISALLOW_COPY_AND_ASSIGN(Job); | 395 DISALLOW_COPY_AND_ASSIGN(Job); |
| 396 }; | 396 }; |
| 397 | 397 |
| 398 //----------------------------------------------------------------------------- | 398 //----------------------------------------------------------------------------- |
| 399 | 399 |
| 400 HostResolver::HostResolver(int max_cache_entries, int cache_duration_ms) | 400 HostResolver::HostResolver(int max_cache_entries, int cache_duration_ms) |
| 401 : cache_(max_cache_entries, cache_duration_ms), next_request_id_(0) { | 401 : cache_(max_cache_entries, cache_duration_ms), next_request_id_(0), |
| 402 shutdown_(false) { |
| 402 #if defined(OS_WIN) | 403 #if defined(OS_WIN) |
| 403 EnsureWinsockInit(); | 404 EnsureWinsockInit(); |
| 404 #endif | 405 #endif |
| 405 } | 406 } |
| 406 | 407 |
| 407 HostResolver::~HostResolver() { | 408 HostResolver::~HostResolver() { |
| 408 // Cancel the outstanding jobs. Those jobs may contain several attached | 409 // Cancel the outstanding jobs. Those jobs may contain several attached |
| 409 // requests, which will also be cancelled. | 410 // requests, which will also be cancelled. |
| 410 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ++it) | 411 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ++it) |
| 411 it->second->Cancel(); | 412 it->second->Cancel(); |
| 412 | 413 |
| 413 // In case we are being deleted during the processing of a callback. | 414 // In case we are being deleted during the processing of a callback. |
| 414 if (cur_completing_job_) | 415 if (cur_completing_job_) |
| 415 cur_completing_job_->Cancel(); | 416 cur_completing_job_->Cancel(); |
| 416 } | 417 } |
| 417 | 418 |
| 418 // TODO(eroman): Don't create cache entries for hostnames which are simply IP | 419 // TODO(eroman): Don't create cache entries for hostnames which are simply IP |
| 419 // address literals. | 420 // address literals. |
| 420 int HostResolver::Resolve(const RequestInfo& info, | 421 int HostResolver::Resolve(const RequestInfo& info, |
| 421 AddressList* addresses, | 422 AddressList* addresses, |
| 422 CompletionCallback* callback, | 423 CompletionCallback* callback, |
| 423 Request** out_req) { | 424 Request** out_req) { |
| 425 if (shutdown_) |
| 426 return ERR_UNEXPECTED; |
| 427 |
| 424 // Choose a unique ID number for observers to see. | 428 // Choose a unique ID number for observers to see. |
| 425 int request_id = next_request_id_++; | 429 int request_id = next_request_id_++; |
| 426 | 430 |
| 427 // Notify registered observers. | 431 // Notify registered observers. |
| 428 NotifyObserversStartRequest(request_id, info); | 432 NotifyObserversStartRequest(request_id, info); |
| 429 | 433 |
| 430 // If we have an unexpired cache entry, use it. | 434 // If we have an unexpired cache entry, use it. |
| 431 if (info.allow_cached_response()) { | 435 if (info.allow_cached_response()) { |
| 432 const HostCache::Entry* cache_entry = cache_.Lookup( | 436 const HostCache::Entry* cache_entry = cache_.Lookup( |
| 433 info.hostname(), base::TimeTicks::Now()); | 437 info.hostname(), base::TimeTicks::Now()); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 void HostResolver::RemoveObserver(Observer* observer) { | 514 void HostResolver::RemoveObserver(Observer* observer) { |
| 511 ObserversList::iterator it = | 515 ObserversList::iterator it = |
| 512 std::find(observers_.begin(), observers_.end(), observer); | 516 std::find(observers_.begin(), observers_.end(), observer); |
| 513 | 517 |
| 514 // Observer must exist. | 518 // Observer must exist. |
| 515 DCHECK(it != observers_.end()); | 519 DCHECK(it != observers_.end()); |
| 516 | 520 |
| 517 observers_.erase(it); | 521 observers_.erase(it); |
| 518 } | 522 } |
| 519 | 523 |
| 524 void HostResolver::Shutdown() { |
| 525 shutdown_ = true; |
| 526 |
| 527 // Cancel the outstanding jobs. |
| 528 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ++it) |
| 529 it->second->Cancel(); |
| 530 jobs_.clear(); |
| 531 } |
| 532 |
| 520 void HostResolver::AddOutstandingJob(Job* job) { | 533 void HostResolver::AddOutstandingJob(Job* job) { |
| 521 scoped_refptr<Job>& found_job = jobs_[job->host()]; | 534 scoped_refptr<Job>& found_job = jobs_[job->host()]; |
| 522 DCHECK(!found_job); | 535 DCHECK(!found_job); |
| 523 found_job = job; | 536 found_job = job; |
| 524 } | 537 } |
| 525 | 538 |
| 526 HostResolver::Job* HostResolver::FindOutstandingJob( | 539 HostResolver::Job* HostResolver::FindOutstandingJob( |
| 527 const std::string& hostname) { | 540 const std::string& hostname) { |
| 528 JobMap::iterator it = jobs_.find(hostname); | 541 JobMap::iterator it = jobs_.find(hostname); |
| 529 if (it != jobs_.end()) | 542 if (it != jobs_.end()) |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 | 658 |
| 646 // Clear the outstanding request information. | 659 // Clear the outstanding request information. |
| 647 cur_request_ = NULL; | 660 cur_request_ = NULL; |
| 648 cur_request_callback_ = NULL; | 661 cur_request_callback_ = NULL; |
| 649 | 662 |
| 650 // Call the user's original callback. | 663 // Call the user's original callback. |
| 651 callback->Run(result); | 664 callback->Run(result); |
| 652 } | 665 } |
| 653 | 666 |
| 654 } // namespace net | 667 } // namespace net |
| OLD | NEW |