| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/bind.h" | 18 #include "base/bind.h" |
| 19 #include "base/bind_helpers.h" |
| 19 #include "base/compiler_specific.h" | 20 #include "base/compiler_specific.h" |
| 20 #include "base/debug/debugger.h" | 21 #include "base/debug/debugger.h" |
| 21 #include "base/debug/stack_trace.h" | 22 #include "base/debug/stack_trace.h" |
| 22 #include "base/message_loop_proxy.h" | 23 #include "base/message_loop_proxy.h" |
| 23 #include "base/metrics/field_trial.h" | 24 #include "base/metrics/field_trial.h" |
| 24 #include "base/metrics/histogram.h" | 25 #include "base/metrics/histogram.h" |
| 25 #include "base/stl_util.h" | 26 #include "base/stl_util.h" |
| 26 #include "base/string_util.h" | 27 #include "base/string_util.h" |
| 27 #include "base/threading/worker_pool.h" | 28 #include "base/threading/worker_pool.h" |
| 28 #include "base/time.h" | 29 #include "base/time.h" |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 const NetLog::Source source_; | 256 const NetLog::Source source_; |
| 256 }; | 257 }; |
| 257 | 258 |
| 258 //----------------------------------------------------------------------------- | 259 //----------------------------------------------------------------------------- |
| 259 | 260 |
| 260 class HostResolverImpl::Request { | 261 class HostResolverImpl::Request { |
| 261 public: | 262 public: |
| 262 Request(const BoundNetLog& source_net_log, | 263 Request(const BoundNetLog& source_net_log, |
| 263 const BoundNetLog& request_net_log, | 264 const BoundNetLog& request_net_log, |
| 264 const RequestInfo& info, | 265 const RequestInfo& info, |
| 265 OldCompletionCallback* callback, | 266 const CompletionCallback& callback, |
| 266 AddressList* addresses) | 267 AddressList* addresses) |
| 267 : source_net_log_(source_net_log), | 268 : source_net_log_(source_net_log), |
| 268 request_net_log_(request_net_log), | 269 request_net_log_(request_net_log), |
| 269 info_(info), | 270 info_(info), |
| 270 job_(NULL), | 271 job_(NULL), |
| 271 callback_(callback), | 272 callback_(callback), |
| 272 addresses_(addresses) { | 273 addresses_(addresses) { |
| 273 } | 274 } |
| 274 | 275 |
| 275 // Mark the request as cancelled. | 276 // Mark the request as cancelled. |
| 276 void MarkAsCancelled() { | 277 void MarkAsCancelled() { |
| 277 job_ = NULL; | 278 job_ = NULL; |
| 278 callback_ = NULL; | |
| 279 addresses_ = NULL; | 279 addresses_ = NULL; |
| 280 callback_.Reset(); |
| 280 } | 281 } |
| 281 | 282 |
| 282 bool was_cancelled() const { | 283 bool was_cancelled() const { |
| 283 return callback_ == NULL; | 284 return callback_.is_null(); |
| 284 } | 285 } |
| 285 | 286 |
| 286 void set_job(Job* job) { | 287 void set_job(Job* job) { |
| 287 DCHECK(job != NULL); | 288 DCHECK(job != NULL); |
| 288 // Identify which job the request is waiting on. | 289 // Identify which job the request is waiting on. |
| 289 job_ = job; | 290 job_ = job; |
| 290 } | 291 } |
| 291 | 292 |
| 292 void OnComplete(int error, const AddressList& addrlist) { | 293 void OnComplete(int error, const AddressList& addrlist) { |
| 293 if (error == OK) | 294 if (error == OK) |
| 294 *addresses_ = CreateAddressListUsingPort(addrlist, port()); | 295 *addresses_ = CreateAddressListUsingPort(addrlist, port()); |
| 295 OldCompletionCallback* callback = callback_; | 296 CompletionCallback callback = callback_; |
| 296 MarkAsCancelled(); | 297 MarkAsCancelled(); |
| 297 callback->Run(error); | 298 callback.Run(error); |
| 298 } | 299 } |
| 299 | 300 |
| 300 int port() const { | 301 int port() const { |
| 301 return info_.port(); | 302 return info_.port(); |
| 302 } | 303 } |
| 303 | 304 |
| 304 Job* job() const { | 305 Job* job() const { |
| 305 return job_; | 306 return job_; |
| 306 } | 307 } |
| 307 | 308 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 321 BoundNetLog source_net_log_; | 322 BoundNetLog source_net_log_; |
| 322 BoundNetLog request_net_log_; | 323 BoundNetLog request_net_log_; |
| 323 | 324 |
| 324 // The request info that started the request. | 325 // The request info that started the request. |
| 325 RequestInfo info_; | 326 RequestInfo info_; |
| 326 | 327 |
| 327 // The resolve job (running in worker pool) that this request is dependent on. | 328 // The resolve job (running in worker pool) that this request is dependent on. |
| 328 Job* job_; | 329 Job* job_; |
| 329 | 330 |
| 330 // The user's callback to invoke when the request completes. | 331 // The user's callback to invoke when the request completes. |
| 331 OldCompletionCallback* callback_; | 332 CompletionCallback callback_; |
| 332 | 333 |
| 333 // The address list to save result into. | 334 // The address list to save result into. |
| 334 AddressList* addresses_; | 335 AddressList* addresses_; |
| 335 | 336 |
| 336 DISALLOW_COPY_AND_ASSIGN(Request); | 337 DISALLOW_COPY_AND_ASSIGN(Request); |
| 337 }; | 338 }; |
| 338 | 339 |
| 339 //------------------------------------------------------------------------------ | 340 //------------------------------------------------------------------------------ |
| 340 | 341 |
| 341 // Provide a common macro to simplify code and readability. We must use a | 342 // Provide a common macro to simplify code and readability. We must use a |
| (...skipping 771 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1113 DCHECK(CalledOnValidThread()); | 1114 DCHECK(CalledOnValidThread()); |
| 1114 CHECK_GE(pool_index, 0); | 1115 CHECK_GE(pool_index, 0); |
| 1115 CHECK_LT(pool_index, POOL_COUNT); | 1116 CHECK_LT(pool_index, POOL_COUNT); |
| 1116 CHECK(jobs_.empty()) << "Can only set constraints during setup"; | 1117 CHECK(jobs_.empty()) << "Can only set constraints during setup"; |
| 1117 JobPool* pool = job_pools_[pool_index]; | 1118 JobPool* pool = job_pools_[pool_index]; |
| 1118 pool->SetConstraints(max_outstanding_jobs, max_pending_requests); | 1119 pool->SetConstraints(max_outstanding_jobs, max_pending_requests); |
| 1119 } | 1120 } |
| 1120 | 1121 |
| 1121 int HostResolverImpl::Resolve(const RequestInfo& info, | 1122 int HostResolverImpl::Resolve(const RequestInfo& info, |
| 1122 AddressList* addresses, | 1123 AddressList* addresses, |
| 1123 OldCompletionCallback* callback, | 1124 const CompletionCallback& callback, |
| 1124 RequestHandle* out_req, | 1125 RequestHandle* out_req, |
| 1125 const BoundNetLog& source_net_log) { | 1126 const BoundNetLog& source_net_log) { |
| 1126 DCHECK(addresses); | 1127 DCHECK(addresses); |
| 1127 DCHECK(callback); | |
| 1128 DCHECK(CalledOnValidThread()); | 1128 DCHECK(CalledOnValidThread()); |
| 1129 DCHECK_EQ(false, callback.is_null()); |
| 1129 | 1130 |
| 1130 // Make a log item for the request. | 1131 // Make a log item for the request. |
| 1131 BoundNetLog request_net_log = BoundNetLog::Make(net_log_, | 1132 BoundNetLog request_net_log = BoundNetLog::Make(net_log_, |
| 1132 NetLog::SOURCE_HOST_RESOLVER_IMPL_REQUEST); | 1133 NetLog::SOURCE_HOST_RESOLVER_IMPL_REQUEST); |
| 1133 | 1134 |
| 1134 // Update the net log and notify registered observers. | 1135 // Update the net log and notify registered observers. |
| 1135 OnStartRequest(source_net_log, request_net_log, info); | 1136 OnStartRequest(source_net_log, request_net_log, info); |
| 1136 | 1137 |
| 1137 // Build a key that identifies the request in the cache and in the | 1138 // Build a key that identifies the request in the cache and in the |
| 1138 // outstanding jobs map. | 1139 // outstanding jobs map. |
| 1139 Key key = GetEffectiveKeyForRequest(info); | 1140 Key key = GetEffectiveKeyForRequest(info); |
| 1140 | 1141 |
| 1141 int rv = ResolveHelper(key, info, addresses, request_net_log); | 1142 int rv = ResolveHelper(key, info, addresses, request_net_log); |
| 1142 if (rv != ERR_DNS_CACHE_MISS) { | 1143 if (rv != ERR_DNS_CACHE_MISS) { |
| 1143 OnFinishRequest(source_net_log, request_net_log, info, | 1144 OnFinishRequest(source_net_log, request_net_log, info, |
| 1144 rv, | 1145 rv, |
| 1145 0 /* os_error (unknown since from cache) */); | 1146 0 /* os_error (unknown since from cache) */); |
| 1146 return rv; | 1147 return rv; |
| 1147 } | 1148 } |
| 1148 | 1149 |
| 1149 // Create a handle for this request, and pass it back to the user if they | 1150 // Create a handle for this request, and pass it back to the user if they |
| 1150 // asked for it (out_req != NULL). | 1151 // asked for it (out_req != NULL). |
| 1151 Request* req = new Request(source_net_log, request_net_log, info, | 1152 Request* req = new Request(source_net_log, request_net_log, info, |
| 1152 callback, addresses); | 1153 callback, addresses); |
| 1153 if (out_req) | 1154 if (out_req) |
| 1154 *out_req = reinterpret_cast<RequestHandle>(req); | 1155 *out_req = reinterpret_cast<RequestHandle>(req); |
| 1155 | 1156 |
| 1156 // Next we need to attach our request to a "job". This job is responsible for | 1157 // Next we need to attach our request to a "job". This job is responsible for |
| 1157 // calling "getaddrinfo(hostname)" on a worker thread. | 1158 // calling "getaddrinfo(hostname)" on a worker thread. |
| 1158 scoped_refptr<Job> job; | 1159 scoped_refptr<Job> job; |
| 1159 | 1160 |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1591 // resolv.conf changes so we don't need to do anything to clear that cache. | 1592 // resolv.conf changes so we don't need to do anything to clear that cache. |
| 1592 if (cache_.get()) | 1593 if (cache_.get()) |
| 1593 cache_->clear(); | 1594 cache_->clear(); |
| 1594 // Existing jobs will have been sent to the original server so they need to | 1595 // Existing jobs will have been sent to the original server so they need to |
| 1595 // be aborted. TODO(Craig): Should these jobs be restarted? | 1596 // be aborted. TODO(Craig): Should these jobs be restarted? |
| 1596 AbortAllInProgressJobs(); | 1597 AbortAllInProgressJobs(); |
| 1597 // |this| may be deleted inside AbortAllInProgressJobs(). | 1598 // |this| may be deleted inside AbortAllInProgressJobs(). |
| 1598 } | 1599 } |
| 1599 | 1600 |
| 1600 } // namespace net | 1601 } // namespace net |
| OLD | NEW |