| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/dns/host_resolver_impl.h" | 5 #include "net/dns/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 <memory> | 14 #include <memory> |
| 15 #include <utility> | 15 #include <utility> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "base/bind.h" | 18 #include "base/bind.h" |
| 19 #include "base/bind_helpers.h" | 19 #include "base/bind_helpers.h" |
| 20 #include "base/callback.h" | 20 #include "base/callback.h" |
| 21 #include "base/callback_helpers.h" |
| 21 #include "base/compiler_specific.h" | 22 #include "base/compiler_specific.h" |
| 22 #include "base/debug/debugger.h" | 23 #include "base/debug/debugger.h" |
| 23 #include "base/debug/leak_annotations.h" | 24 #include "base/debug/leak_annotations.h" |
| 24 #include "base/debug/stack_trace.h" | 25 #include "base/debug/stack_trace.h" |
| 25 #include "base/macros.h" | 26 #include "base/macros.h" |
| 26 #include "base/memory/ptr_util.h" | 27 #include "base/memory/ptr_util.h" |
| 27 #include "base/metrics/field_trial.h" | 28 #include "base/metrics/field_trial.h" |
| 28 #include "base/metrics/histogram_macros.h" | 29 #include "base/metrics/histogram_macros.h" |
| 29 #include "base/metrics/sparse_histogram.h" | 30 #include "base/metrics/sparse_histogram.h" |
| 30 #include "base/profiler/scoped_tracker.h" | 31 #include "base/profiler/scoped_tracker.h" |
| (...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 } | 519 } |
| 519 | 520 |
| 520 return true; | 521 return true; |
| 521 } | 522 } |
| 522 | 523 |
| 523 const unsigned HostResolverImpl::kMaximumDnsFailures = 16; | 524 const unsigned HostResolverImpl::kMaximumDnsFailures = 16; |
| 524 | 525 |
| 525 // Holds the data for a request that could not be completed synchronously. | 526 // Holds the data for a request that could not be completed synchronously. |
| 526 // It is owned by a Job. Canceled Requests are only marked as canceled rather | 527 // It is owned by a Job. Canceled Requests are only marked as canceled rather |
| 527 // than removed from the Job's |requests_| list. | 528 // than removed from the Job's |requests_| list. |
| 528 class HostResolverImpl::Request { | 529 class HostResolverImpl::RequestImpl : public HostResolver::Request { |
| 529 public: | 530 public: |
| 530 Request(const BoundNetLog& source_net_log, | 531 RequestImpl(const BoundNetLog& source_net_log, |
| 531 const RequestInfo& info, | 532 const RequestInfo& info, |
| 532 RequestPriority priority, | 533 RequestPriority priority, |
| 533 const CompletionCallback& callback, | 534 const CompletionCallback& callback, |
| 534 AddressList* addresses) | 535 AddressList* addresses, |
| 536 Job* job) |
| 535 : source_net_log_(source_net_log), | 537 : source_net_log_(source_net_log), |
| 536 info_(info), | 538 info_(info), |
| 537 priority_(priority), | 539 priority_(priority), |
| 538 job_(nullptr), | 540 job_(job), |
| 539 callback_(callback), | 541 callback_(callback), |
| 540 addresses_(addresses), | 542 addresses_(addresses), |
| 541 request_time_(base::TimeTicks::Now()) {} | 543 request_time_(base::TimeTicks::Now()) {} |
| 542 | 544 |
| 543 // Mark the request as canceled. | 545 ~RequestImpl() override; |
| 544 void MarkAsCanceled() { | 546 |
| 547 void ChangeRequestPriority(RequestPriority priority) override; |
| 548 |
| 549 void OnJobCancelled(Job* job) { |
| 550 DCHECK_EQ(job_, job); |
| 545 job_ = nullptr; | 551 job_ = nullptr; |
| 546 addresses_ = nullptr; | 552 addresses_ = nullptr; |
| 547 callback_.Reset(); | 553 callback_.Reset(); |
| 548 } | 554 } |
| 549 | 555 |
| 550 bool was_canceled() const { | |
| 551 return callback_.is_null(); | |
| 552 } | |
| 553 | |
| 554 void set_job(Job* job) { | |
| 555 DCHECK(job); | |
| 556 // Identify which job the request is waiting on. | |
| 557 job_ = job; | |
| 558 } | |
| 559 | |
| 560 // Prepare final AddressList and call completion callback. | 556 // Prepare final AddressList and call completion callback. |
| 561 void OnComplete(int error, const AddressList& addr_list) { | 557 void OnJobCompleted(Job* job, int error, const AddressList& addr_list) { |
| 562 DCHECK(!was_canceled()); | 558 DCHECK_EQ(job_, job); |
| 563 if (error == OK) | 559 if (error == OK) |
| 564 *addresses_ = EnsurePortOnAddressList(addr_list, info_.port()); | 560 *addresses_ = EnsurePortOnAddressList(addr_list, info_.port()); |
| 565 CompletionCallback callback = callback_; | 561 job_ = nullptr; |
| 566 MarkAsCanceled(); | 562 addresses_ = nullptr; |
| 567 callback.Run(error); | 563 base::ResetAndReturn(&callback_).Run(error); |
| 568 } | 564 } |
| 569 | 565 |
| 570 Job* job() const { | 566 Job* job() const { |
| 571 return job_; | 567 return job_; |
| 572 } | 568 } |
| 573 | 569 |
| 574 // NetLog for the source, passed in HostResolver::Resolve. | 570 // NetLog for the source, passed in HostResolver::Resolve. |
| 575 const BoundNetLog& source_net_log() { | 571 const BoundNetLog& source_net_log() { |
| 576 return source_net_log_; | 572 return source_net_log_; |
| 577 } | 573 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 597 Job* job_; | 593 Job* job_; |
| 598 | 594 |
| 599 // The user's callback to invoke when the request completes. | 595 // The user's callback to invoke when the request completes. |
| 600 CompletionCallback callback_; | 596 CompletionCallback callback_; |
| 601 | 597 |
| 602 // The address list to save result into. | 598 // The address list to save result into. |
| 603 AddressList* addresses_; | 599 AddressList* addresses_; |
| 604 | 600 |
| 605 const base::TimeTicks request_time_; | 601 const base::TimeTicks request_time_; |
| 606 | 602 |
| 607 DISALLOW_COPY_AND_ASSIGN(Request); | 603 DISALLOW_COPY_AND_ASSIGN(RequestImpl); |
| 608 }; | 604 }; |
| 609 | 605 |
| 610 //------------------------------------------------------------------------------ | 606 //------------------------------------------------------------------------------ |
| 611 | 607 |
| 612 // Calls HostResolverProc using a worker task runner. Performs retries if | 608 // Calls HostResolverProc using a worker task runner. Performs retries if |
| 613 // necessary. | 609 // necessary. |
| 614 // | 610 // |
| 615 // Whenever we try to resolve the host, we post a delayed task to check if host | 611 // Whenever we try to resolve the host, we post a delayed task to check if host |
| 616 // resolution (OnLookupComplete) is completed or not. If the original attempt | 612 // resolution (OnLookupComplete) is completed or not. If the original attempt |
| 617 // hasn't completed, then we start another attempt for host resolution. We take | 613 // hasn't completed, then we start another attempt for host resolution. We take |
| (...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1314 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, | 1310 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
| 1315 ERR_ABORTED); | 1311 ERR_ABORTED); |
| 1316 } else if (is_queued()) { | 1312 } else if (is_queued()) { |
| 1317 // |resolver_| was destroyed without running this Job. | 1313 // |resolver_| was destroyed without running this Job. |
| 1318 // TODO(szym): is there any benefit in having this distinction? | 1314 // TODO(szym): is there any benefit in having this distinction? |
| 1319 net_log_.AddEvent(NetLog::TYPE_CANCELLED); | 1315 net_log_.AddEvent(NetLog::TYPE_CANCELLED); |
| 1320 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB); | 1316 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB); |
| 1321 } | 1317 } |
| 1322 // else CompleteRequests logged EndEvent. | 1318 // else CompleteRequests logged EndEvent. |
| 1323 | 1319 |
| 1324 // Log any remaining Requests as cancelled. | 1320 if (!requests_.empty()) { |
| 1325 for (const std::unique_ptr<Request>& req : requests_) { | 1321 // Log any remaining Requests as cancelled. |
| 1326 if (req->was_canceled()) | 1322 for (RequestImpl* req : requests_) { |
| 1327 continue; | 1323 DCHECK_EQ(this, req->job()); |
| 1328 DCHECK_EQ(this, req->job()); | 1324 req->OnJobCancelled(this); |
| 1329 LogCancelRequest(req->source_net_log(), req->info()); | 1325 LogCancelRequest(req->source_net_log(), req->info()); |
| 1326 } |
| 1327 requests_.clear(); |
| 1330 } | 1328 } |
| 1331 } | 1329 } |
| 1332 | 1330 |
| 1333 // Add this job to the dispatcher. If "at_head" is true, adds at the front | 1331 // Add this job to the dispatcher. If "at_head" is true, adds at the front |
| 1334 // of the queue. | 1332 // of the queue. |
| 1335 void Schedule(bool at_head) { | 1333 void Schedule(bool at_head) { |
| 1336 DCHECK(!is_queued()); | 1334 DCHECK(!is_queued()); |
| 1337 PrioritizedDispatcher::Handle handle; | 1335 PrioritizedDispatcher::Handle handle; |
| 1338 if (!at_head) { | 1336 if (!at_head) { |
| 1339 handle = resolver_->dispatcher_->Add(this, priority()); | 1337 handle = resolver_->dispatcher_->Add(this, priority()); |
| 1340 } else { | 1338 } else { |
| 1341 handle = resolver_->dispatcher_->AddAtHead(this, priority()); | 1339 handle = resolver_->dispatcher_->AddAtHead(this, priority()); |
| 1342 } | 1340 } |
| 1343 // The dispatcher could have started |this| in the above call to Add, which | 1341 // The dispatcher could have started |this| in the above call to Add, which |
| 1344 // could have called Schedule again. In that case |handle| will be null, | 1342 // could have called Schedule again. In that case |handle| will be null, |
| 1345 // but |handle_| may have been set by the other nested call to Schedule. | 1343 // but |handle_| may have been set by the other nested call to Schedule. |
| 1346 if (!handle.is_null()) { | 1344 if (!handle.is_null()) { |
| 1347 DCHECK(handle_.is_null()); | 1345 DCHECK(handle_.is_null()); |
| 1348 handle_ = handle; | 1346 handle_ = handle; |
| 1349 } | 1347 } |
| 1350 } | 1348 } |
| 1351 | 1349 |
| 1352 void AddRequest(std::unique_ptr<Request> req) { | 1350 void AddRequest(RequestImpl* request) { |
| 1353 DCHECK_EQ(key_.hostname, req->info().hostname()); | 1351 DCHECK_EQ(key_.hostname, request->info().hostname()); |
| 1354 | 1352 |
| 1355 req->set_job(this); | 1353 priority_tracker_.Add(request->priority()); |
| 1356 priority_tracker_.Add(req->priority()); | |
| 1357 | 1354 |
| 1358 req->source_net_log().AddEvent( | 1355 request->source_net_log().AddEvent( |
| 1359 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH, | 1356 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH, |
| 1360 net_log_.source().ToEventParametersCallback()); | 1357 net_log_.source().ToEventParametersCallback()); |
| 1361 | 1358 |
| 1362 net_log_.AddEvent( | 1359 net_log_.AddEvent( |
| 1363 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH, | 1360 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH, |
| 1364 base::Bind(&NetLogJobAttachCallback, | 1361 base::Bind(&NetLogJobAttachCallback, request->source_net_log().source(), |
| 1365 req->source_net_log().source(), | |
| 1366 priority())); | 1362 priority())); |
| 1367 | 1363 |
| 1368 // TODO(szym): Check if this is still needed. | 1364 // TODO(szym): Check if this is still needed. |
| 1369 if (!req->info().is_speculative()) { | 1365 if (!request->info().is_speculative()) { |
| 1370 had_non_speculative_request_ = true; | 1366 had_non_speculative_request_ = true; |
| 1371 if (proc_task_.get()) | 1367 if (proc_task_.get()) |
| 1372 proc_task_->set_had_non_speculative_request(); | 1368 proc_task_->set_had_non_speculative_request(); |
| 1373 } | 1369 } |
| 1374 | 1370 |
| 1375 requests_.push_back(std::move(req)); | 1371 requests_.push_back(request); |
| 1376 | 1372 |
| 1377 UpdatePriority(); | 1373 UpdatePriority(); |
| 1378 } | 1374 } |
| 1379 | 1375 |
| 1380 void ChangeRequestPriority(Request* req, RequestPriority priority) { | 1376 void ChangeRequestPriority(RequestImpl* req, RequestPriority priority) { |
| 1381 DCHECK_EQ(key_.hostname, req->info().hostname()); | 1377 DCHECK_EQ(key_.hostname, req->info().hostname()); |
| 1382 DCHECK(!req->was_canceled()); | |
| 1383 | 1378 |
| 1384 priority_tracker_.Remove(req->priority()); | 1379 priority_tracker_.Remove(req->priority()); |
| 1385 req->set_priority(priority); | 1380 req->set_priority(priority); |
| 1386 priority_tracker_.Add(req->priority()); | 1381 priority_tracker_.Add(req->priority()); |
| 1387 UpdatePriority(); | 1382 UpdatePriority(); |
| 1388 } | 1383 } |
| 1389 | 1384 |
| 1390 // Marks |req| as cancelled. If it was the last active Request, also finishes | 1385 // Detach cancelled request. If it was the last active Request, also finishes |
| 1391 // this Job, marking it as cancelled, and deletes it. | 1386 // this Job. |
| 1392 void CancelRequest(Request* req) { | 1387 void CancelRequest(RequestImpl* request) { |
| 1393 DCHECK_EQ(key_.hostname, req->info().hostname()); | 1388 DCHECK_EQ(key_.hostname, request->info().hostname()); |
| 1394 DCHECK(!req->was_canceled()); | 1389 DCHECK(!requests_.empty()); |
| 1395 | 1390 |
| 1396 // Don't remove it from |requests_| just mark it canceled. | 1391 LogCancelRequest(request->source_net_log(), request->info()); |
| 1397 req->MarkAsCanceled(); | |
| 1398 LogCancelRequest(req->source_net_log(), req->info()); | |
| 1399 | 1392 |
| 1400 priority_tracker_.Remove(req->priority()); | 1393 priority_tracker_.Remove(request->priority()); |
| 1401 net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH, | 1394 net_log_.AddEvent( |
| 1402 base::Bind(&NetLogJobAttachCallback, | 1395 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH, |
| 1403 req->source_net_log().source(), | 1396 base::Bind(&NetLogJobAttachCallback, request->source_net_log().source(), |
| 1404 priority())); | 1397 priority())); |
| 1405 | 1398 |
| 1406 if (num_active_requests() > 0) { | 1399 if (num_active_requests() > 0) { |
| 1407 UpdatePriority(); | 1400 UpdatePriority(); |
| 1401 RemoveRequest(request); |
| 1408 } else { | 1402 } else { |
| 1409 // If we were called from a Request's callback within CompleteRequests, | 1403 // If we were called from a Request's callback within CompleteRequests, |
| 1410 // that Request could not have been cancelled, so num_active_requests() | 1404 // that Request could not have been cancelled, so num_active_requests() |
| 1411 // could not be 0. Therefore, we are not in CompleteRequests(). | 1405 // could not be 0. Therefore, we are not in CompleteRequests(). |
| 1412 CompleteRequestsWithError(OK /* cancelled */); | 1406 CompleteRequestsWithError(OK /* cancelled */); |
| 1413 } | 1407 } |
| 1414 } | 1408 } |
| 1415 | 1409 |
| 1410 void RemoveRequest(RequestImpl* request) { |
| 1411 auto it = std::find(requests_.begin(), requests_.end(), request); |
| 1412 DCHECK(it != requests_.end()); |
| 1413 requests_.erase(it); |
| 1414 } |
| 1415 |
| 1416 // Called from AbortAllInProgressJobs. Completes all requests and destroys | 1416 // Called from AbortAllInProgressJobs. Completes all requests and destroys |
| 1417 // the job. This currently assumes the abort is due to a network change. | 1417 // the job. This currently assumes the abort is due to a network change. |
| 1418 // TODO This should not delete |this|. | 1418 // TODO This should not delete |this|. |
| 1419 void Abort() { | 1419 void Abort() { |
| 1420 DCHECK(is_running()); | 1420 DCHECK(is_running()); |
| 1421 CompleteRequestsWithError(ERR_NETWORK_CHANGED); | 1421 CompleteRequestsWithError(ERR_NETWORK_CHANGED); |
| 1422 } | 1422 } |
| 1423 | 1423 |
| 1424 // If DnsTask present, abort it and fall back to ProcTask. | 1424 // If DnsTask present, abort it and fall back to ProcTask. |
| 1425 void AbortDnsTask() { | 1425 void AbortDnsTask() { |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1766 // configuration. | 1766 // configuration. |
| 1767 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig", | 1767 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig", |
| 1768 resolver_->received_dns_config_); | 1768 resolver_->received_dns_config_); |
| 1769 } | 1769 } |
| 1770 | 1770 |
| 1771 bool did_complete = (entry.error() != ERR_NETWORK_CHANGED) && | 1771 bool did_complete = (entry.error() != ERR_NETWORK_CHANGED) && |
| 1772 (entry.error() != ERR_HOST_RESOLVER_QUEUE_TOO_LARGE); | 1772 (entry.error() != ERR_HOST_RESOLVER_QUEUE_TOO_LARGE); |
| 1773 if (did_complete) | 1773 if (did_complete) |
| 1774 resolver_->CacheResult(key_, entry, ttl); | 1774 resolver_->CacheResult(key_, entry, ttl); |
| 1775 | 1775 |
| 1776 // Complete all of the requests that were attached to the job. | 1776 // Complete all of the requests that were attached to the job and |
| 1777 for (const std::unique_ptr<Request>& req : requests_) { | 1777 // detach them. |
| 1778 if (req->was_canceled()) | 1778 while (!requests_.empty()) { |
| 1779 continue; | 1779 RequestImpl* req = requests_.front(); |
| 1780 | 1780 requests_.pop_front(); |
| 1781 DCHECK_EQ(this, req->job()); | 1781 DCHECK_EQ(this, req->job()); |
| 1782 // Update the net log and notify registered observers. | 1782 // Update the net log and notify registered observers. |
| 1783 LogFinishRequest(req->source_net_log(), req->info(), entry.error()); | 1783 LogFinishRequest(req->source_net_log(), req->info(), entry.error()); |
| 1784 if (did_complete) { | 1784 if (did_complete) { |
| 1785 // Record effective total time from creation to completion. | 1785 // Record effective total time from creation to completion. |
| 1786 RecordTotalTime(had_dns_config_, req->info().is_speculative(), | 1786 RecordTotalTime(had_dns_config_, req->info().is_speculative(), |
| 1787 base::TimeTicks::Now() - req->request_time()); | 1787 base::TimeTicks::Now() - req->request_time()); |
| 1788 } | 1788 } |
| 1789 req->OnComplete(entry.error(), entry.addresses()); | 1789 req->OnJobCompleted(this, entry.error(), entry.addresses()); |
| 1790 | 1790 |
| 1791 // Check if the resolver was destroyed as a result of running the | 1791 // Check if the resolver was destroyed as a result of running the |
| 1792 // callback. If it was, we could continue, but we choose to bail. | 1792 // callback. If it was, we could continue, but we choose to bail. |
| 1793 if (!resolver_.get()) | 1793 if (!resolver_.get()) |
| 1794 return; | 1794 return; |
| 1795 } | 1795 } |
| 1796 } | 1796 } |
| 1797 | 1797 |
| 1798 // Convenience wrapper for CompleteRequests in case of failure. | 1798 // Convenience wrapper for CompleteRequests in case of failure. |
| 1799 void CompleteRequestsWithError(int net_error) { | 1799 void CompleteRequestsWithError(int net_error) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1840 | 1840 |
| 1841 BoundNetLog net_log_; | 1841 BoundNetLog net_log_; |
| 1842 | 1842 |
| 1843 // Resolves the host using a HostResolverProc. | 1843 // Resolves the host using a HostResolverProc. |
| 1844 scoped_refptr<ProcTask> proc_task_; | 1844 scoped_refptr<ProcTask> proc_task_; |
| 1845 | 1845 |
| 1846 // Resolves the host using a DnsTransaction. | 1846 // Resolves the host using a DnsTransaction. |
| 1847 std::unique_ptr<DnsTask> dns_task_; | 1847 std::unique_ptr<DnsTask> dns_task_; |
| 1848 | 1848 |
| 1849 // All Requests waiting for the result of this Job. Some can be canceled. | 1849 // All Requests waiting for the result of this Job. Some can be canceled. |
| 1850 std::vector<std::unique_ptr<Request>> requests_; | 1850 std::deque<RequestImpl*> requests_; |
| 1851 | 1851 |
| 1852 // A handle used in |HostResolverImpl::dispatcher_|. | 1852 // A handle used in |HostResolverImpl::dispatcher_|. |
| 1853 PrioritizedDispatcher::Handle handle_; | 1853 PrioritizedDispatcher::Handle handle_; |
| 1854 }; | 1854 }; |
| 1855 | 1855 |
| 1856 //----------------------------------------------------------------------------- | 1856 //----------------------------------------------------------------------------- |
| 1857 | 1857 |
| 1858 HostResolverImpl::ProcTaskParams::ProcTaskParams( | 1858 HostResolverImpl::ProcTaskParams::ProcTaskParams( |
| 1859 HostResolverProc* resolver_proc, | 1859 HostResolverProc* resolver_proc, |
| 1860 size_t max_retry_attempts) | 1860 size_t max_retry_attempts) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1895 void HostResolverImpl::SetMaxQueuedJobs(size_t value) { | 1895 void HostResolverImpl::SetMaxQueuedJobs(size_t value) { |
| 1896 DCHECK_EQ(0u, dispatcher_->num_queued_jobs()); | 1896 DCHECK_EQ(0u, dispatcher_->num_queued_jobs()); |
| 1897 DCHECK_GT(value, 0u); | 1897 DCHECK_GT(value, 0u); |
| 1898 max_queued_jobs_ = value; | 1898 max_queued_jobs_ = value; |
| 1899 } | 1899 } |
| 1900 | 1900 |
| 1901 int HostResolverImpl::Resolve(const RequestInfo& info, | 1901 int HostResolverImpl::Resolve(const RequestInfo& info, |
| 1902 RequestPriority priority, | 1902 RequestPriority priority, |
| 1903 AddressList* addresses, | 1903 AddressList* addresses, |
| 1904 const CompletionCallback& callback, | 1904 const CompletionCallback& callback, |
| 1905 RequestHandle* out_req, | 1905 std::unique_ptr<Request>* out_req, |
| 1906 const BoundNetLog& source_net_log) { | 1906 const BoundNetLog& source_net_log) { |
| 1907 DCHECK(addresses); | 1907 DCHECK(addresses); |
| 1908 DCHECK(CalledOnValidThread()); | 1908 DCHECK(CalledOnValidThread()); |
| 1909 DCHECK_EQ(false, callback.is_null()); | 1909 DCHECK_EQ(false, callback.is_null()); |
| 1910 DCHECK(out_req); |
| 1910 | 1911 |
| 1911 // Check that the caller supplied a valid hostname to resolve. | 1912 // Check that the caller supplied a valid hostname to resolve. |
| 1912 std::string labeled_hostname; | 1913 std::string labeled_hostname; |
| 1913 if (!DNSDomainFromDot(info.hostname(), &labeled_hostname)) | 1914 if (!DNSDomainFromDot(info.hostname(), &labeled_hostname)) |
| 1914 return ERR_NAME_NOT_RESOLVED; | 1915 return ERR_NAME_NOT_RESOLVED; |
| 1915 | 1916 |
| 1916 LogStartRequest(source_net_log, info); | 1917 LogStartRequest(source_net_log, info); |
| 1917 | 1918 |
| 1918 IPAddress ip_address; | 1919 IPAddress ip_address; |
| 1919 IPAddress* ip_address_ptr = nullptr; | 1920 IPAddress* ip_address_ptr = nullptr; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1952 LogFinishRequest(source_net_log, info, rv); | 1953 LogFinishRequest(source_net_log, info, rv); |
| 1953 return rv; | 1954 return rv; |
| 1954 } | 1955 } |
| 1955 } | 1956 } |
| 1956 jobs_.insert(jobit, std::make_pair(key, job)); | 1957 jobs_.insert(jobit, std::make_pair(key, job)); |
| 1957 } else { | 1958 } else { |
| 1958 job = jobit->second; | 1959 job = jobit->second; |
| 1959 } | 1960 } |
| 1960 | 1961 |
| 1961 // Can't complete synchronously. Create and attach request. | 1962 // Can't complete synchronously. Create and attach request. |
| 1962 std::unique_ptr<Request> req( | 1963 std::unique_ptr<RequestImpl> req(new RequestImpl( |
| 1963 new Request(source_net_log, info, priority, callback, addresses)); | 1964 source_net_log, info, priority, callback, addresses, job)); |
| 1964 if (out_req) | 1965 job->AddRequest(req.get()); |
| 1965 *out_req = reinterpret_cast<RequestHandle>(req.get()); | 1966 *out_req = std::move(req); |
| 1966 | 1967 |
| 1967 job->AddRequest(std::move(req)); | |
| 1968 // Completion happens during Job::CompleteRequests(). | 1968 // Completion happens during Job::CompleteRequests(). |
| 1969 return ERR_IO_PENDING; | 1969 return ERR_IO_PENDING; |
| 1970 } | 1970 } |
| 1971 | 1971 |
| 1972 HostResolverImpl::HostResolverImpl( | 1972 HostResolverImpl::HostResolverImpl( |
| 1973 const Options& options, | 1973 const Options& options, |
| 1974 NetLog* net_log, | 1974 NetLog* net_log, |
| 1975 scoped_refptr<base::TaskRunner> worker_task_runner) | 1975 scoped_refptr<base::TaskRunner> worker_task_runner) |
| 1976 : max_queued_jobs_(0), | 1976 : max_queued_jobs_(0), |
| 1977 proc_params_(NULL, options.max_retry_attempts), | 1977 proc_params_(NULL, options.max_retry_attempts), |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2088 ip_address_ptr = &ip_address; | 2088 ip_address_ptr = &ip_address; |
| 2089 | 2089 |
| 2090 Key key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log); | 2090 Key key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log); |
| 2091 | 2091 |
| 2092 int rv = ResolveHelper(key, info, ip_address_ptr, addresses, false, nullptr, | 2092 int rv = ResolveHelper(key, info, ip_address_ptr, addresses, false, nullptr, |
| 2093 source_net_log); | 2093 source_net_log); |
| 2094 LogFinishRequest(source_net_log, info, rv); | 2094 LogFinishRequest(source_net_log, info, rv); |
| 2095 return rv; | 2095 return rv; |
| 2096 } | 2096 } |
| 2097 | 2097 |
| 2098 void HostResolverImpl::ChangeRequestPriority(RequestHandle req_handle, | |
| 2099 RequestPriority priority) { | |
| 2100 DCHECK(CalledOnValidThread()); | |
| 2101 Request* req = reinterpret_cast<Request*>(req_handle); | |
| 2102 DCHECK(req); | |
| 2103 Job* job = req->job(); | |
| 2104 DCHECK(job); | |
| 2105 job->ChangeRequestPriority(req, priority); | |
| 2106 } | |
| 2107 | |
| 2108 void HostResolverImpl::CancelRequest(RequestHandle req_handle) { | |
| 2109 DCHECK(CalledOnValidThread()); | |
| 2110 Request* req = reinterpret_cast<Request*>(req_handle); | |
| 2111 DCHECK(req); | |
| 2112 Job* job = req->job(); | |
| 2113 DCHECK(job); | |
| 2114 job->CancelRequest(req); | |
| 2115 } | |
| 2116 | |
| 2117 void HostResolverImpl::SetDnsClientEnabled(bool enabled) { | 2098 void HostResolverImpl::SetDnsClientEnabled(bool enabled) { |
| 2118 DCHECK(CalledOnValidThread()); | 2099 DCHECK(CalledOnValidThread()); |
| 2119 #if defined(ENABLE_BUILT_IN_DNS) | 2100 #if defined(ENABLE_BUILT_IN_DNS) |
| 2120 if (enabled && !dns_client_) { | 2101 if (enabled && !dns_client_) { |
| 2121 SetDnsClient(DnsClient::CreateClient(net_log_)); | 2102 SetDnsClient(DnsClient::CreateClient(net_log_)); |
| 2122 } else if (!enabled && dns_client_) { | 2103 } else if (!enabled && dns_client_) { |
| 2123 SetDnsClient(std::unique_ptr<DnsClient>()); | 2104 SetDnsClient(std::unique_ptr<DnsClient>()); |
| 2124 } | 2105 } |
| 2125 #endif | 2106 #endif |
| 2126 } | 2107 } |
| (...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2548 NetworkChangeNotifier::GetDnsConfig(&dns_config); | 2529 NetworkChangeNotifier::GetDnsConfig(&dns_config); |
| 2549 dns_client_->SetConfig(dns_config); | 2530 dns_client_->SetConfig(dns_config); |
| 2550 num_dns_failures_ = 0; | 2531 num_dns_failures_ = 0; |
| 2551 if (dns_client_->GetConfig()) | 2532 if (dns_client_->GetConfig()) |
| 2552 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); | 2533 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
| 2553 } | 2534 } |
| 2554 | 2535 |
| 2555 AbortDnsTasks(); | 2536 AbortDnsTasks(); |
| 2556 } | 2537 } |
| 2557 | 2538 |
| 2539 HostResolverImpl::RequestImpl::~RequestImpl() { |
| 2540 if (job_) |
| 2541 job_->CancelRequest(this); |
| 2542 } |
| 2543 |
| 2544 void HostResolverImpl::RequestImpl::ChangeRequestPriority( |
| 2545 RequestPriority priority) { |
| 2546 job_->ChangeRequestPriority(this, priority); |
| 2547 } |
| 2548 |
| 2558 } // namespace net | 2549 } // namespace net |
| OLD | NEW |