Chromium Code Reviews| 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 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 int id, | 265 int id, |
| 265 const RequestInfo& info, | 266 const RequestInfo& info, |
| 266 OldCompletionCallback* callback, | 267 const CompletionCallback& callback, |
| 267 AddressList* addresses) | 268 AddressList* addresses) |
| 268 : source_net_log_(source_net_log), | 269 : source_net_log_(source_net_log), |
| 269 request_net_log_(request_net_log), | 270 request_net_log_(request_net_log), |
| 270 id_(id), | 271 id_(id), |
| 271 info_(info), | 272 info_(info), |
| 272 job_(NULL), | 273 job_(NULL), |
| 273 callback_(callback), | 274 callback_(callback), |
| 274 addresses_(addresses) { | 275 addresses_(addresses) { |
| 275 } | 276 } |
| 276 | 277 |
| 277 // Mark the request as cancelled. | 278 // Mark the request as cancelled. |
| 278 void MarkAsCancelled() { | 279 void MarkAsCancelled() { |
| 279 job_ = NULL; | 280 job_ = NULL; |
| 280 callback_ = NULL; | |
| 281 addresses_ = NULL; | 281 addresses_ = NULL; |
| 282 callback_.Reset(); | |
| 282 } | 283 } |
| 283 | 284 |
| 284 bool was_cancelled() const { | 285 bool was_cancelled() const { |
| 285 return callback_ == NULL; | 286 return callback_.is_null(); |
| 286 } | 287 } |
| 287 | 288 |
| 288 void set_job(Job* job) { | 289 void set_job(Job* job) { |
| 289 DCHECK(job != NULL); | 290 DCHECK(job != NULL); |
| 290 // Identify which job the request is waiting on. | 291 // Identify which job the request is waiting on. |
| 291 job_ = job; | 292 job_ = job; |
| 292 } | 293 } |
| 293 | 294 |
| 294 void OnComplete(int error, const AddressList& addrlist) { | 295 void OnComplete(int error, const AddressList& addrlist) { |
| 295 if (error == OK) | 296 if (error == OK) |
| 296 *addresses_ = CreateAddressListUsingPort(addrlist, port()); | 297 *addresses_ = CreateAddressListUsingPort(addrlist, port()); |
| 297 OldCompletionCallback* callback = callback_; | 298 CompletionCallback callback = callback_; |
| 298 MarkAsCancelled(); | 299 MarkAsCancelled(); |
| 299 callback->Run(error); | 300 callback.Run(error); |
| 300 } | 301 } |
| 301 | 302 |
| 302 int port() const { | 303 int port() const { |
| 303 return info_.port(); | 304 return info_.port(); |
| 304 } | 305 } |
| 305 | 306 |
| 306 Job* job() const { | 307 Job* job() const { |
| 307 return job_; | 308 return job_; |
| 308 } | 309 } |
| 309 | 310 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 330 // Unique ID for this request. Used by observers to identify requests. | 331 // Unique ID for this request. Used by observers to identify requests. |
| 331 int id_; | 332 int id_; |
| 332 | 333 |
| 333 // The request info that started the request. | 334 // The request info that started the request. |
| 334 RequestInfo info_; | 335 RequestInfo info_; |
| 335 | 336 |
| 336 // The resolve job (running in worker pool) that this request is dependent on. | 337 // The resolve job (running in worker pool) that this request is dependent on. |
| 337 Job* job_; | 338 Job* job_; |
| 338 | 339 |
| 339 // The user's callback to invoke when the request completes. | 340 // The user's callback to invoke when the request completes. |
| 340 OldCompletionCallback* callback_; | 341 CompletionCallback callback_; |
| 341 | 342 |
| 342 // The address list to save result into. | 343 // The address list to save result into. |
| 343 AddressList* addresses_; | 344 AddressList* addresses_; |
| 344 | 345 |
| 345 DISALLOW_COPY_AND_ASSIGN(Request); | 346 DISALLOW_COPY_AND_ASSIGN(Request); |
| 346 }; | 347 }; |
| 347 | 348 |
| 348 //------------------------------------------------------------------------------ | 349 //------------------------------------------------------------------------------ |
| 349 | 350 |
| 350 // Provide a common macro to simplify code and readability. We must use a | 351 // Provide a common macro to simplify code and readability. We must use a |
| (...skipping 772 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1123 DCHECK(CalledOnValidThread()); | 1124 DCHECK(CalledOnValidThread()); |
| 1124 CHECK_GE(pool_index, 0); | 1125 CHECK_GE(pool_index, 0); |
| 1125 CHECK_LT(pool_index, POOL_COUNT); | 1126 CHECK_LT(pool_index, POOL_COUNT); |
| 1126 CHECK(jobs_.empty()) << "Can only set constraints during setup"; | 1127 CHECK(jobs_.empty()) << "Can only set constraints during setup"; |
| 1127 JobPool* pool = job_pools_[pool_index]; | 1128 JobPool* pool = job_pools_[pool_index]; |
| 1128 pool->SetConstraints(max_outstanding_jobs, max_pending_requests); | 1129 pool->SetConstraints(max_outstanding_jobs, max_pending_requests); |
| 1129 } | 1130 } |
| 1130 | 1131 |
| 1131 int HostResolverImpl::Resolve(const RequestInfo& info, | 1132 int HostResolverImpl::Resolve(const RequestInfo& info, |
| 1132 AddressList* addresses, | 1133 AddressList* addresses, |
| 1133 OldCompletionCallback* callback, | 1134 const CompletionCallback& callback, |
| 1134 RequestHandle* out_req, | 1135 RequestHandle* out_req, |
| 1135 const BoundNetLog& source_net_log) { | 1136 const BoundNetLog& source_net_log) { |
| 1136 DCHECK(addresses); | 1137 DCHECK(addresses); |
| 1137 DCHECK(callback); | |
| 1138 DCHECK(CalledOnValidThread()); | 1138 DCHECK(CalledOnValidThread()); |
| 1139 DCHECK_EQ(false, callback.is_null()); | |
|
willchan no longer on Chromium
2011/11/14 16:45:12
DCHECK(!callback.is_null())? What you have is fine
| |
| 1139 | 1140 |
| 1140 // Choose a unique ID number for observers to see. | 1141 // Choose a unique ID number for observers to see. |
| 1141 int request_id = next_request_id_++; | 1142 int request_id = next_request_id_++; |
| 1142 | 1143 |
| 1143 // Make a log item for the request. | 1144 // Make a log item for the request. |
| 1144 BoundNetLog request_net_log = BoundNetLog::Make(net_log_, | 1145 BoundNetLog request_net_log = BoundNetLog::Make(net_log_, |
| 1145 NetLog::SOURCE_HOST_RESOLVER_IMPL_REQUEST); | 1146 NetLog::SOURCE_HOST_RESOLVER_IMPL_REQUEST); |
| 1146 | 1147 |
| 1147 // Update the net log and notify registered observers. | 1148 // Update the net log and notify registered observers. |
| 1148 OnStartRequest(source_net_log, request_net_log, request_id, info); | 1149 OnStartRequest(source_net_log, request_net_log, request_id, info); |
| 1149 | 1150 |
| 1150 // Build a key that identifies the request in the cache and in the | 1151 // Build a key that identifies the request in the cache and in the |
| 1151 // outstanding jobs map. | 1152 // outstanding jobs map. |
| 1152 Key key = GetEffectiveKeyForRequest(info); | 1153 Key key = GetEffectiveKeyForRequest(info); |
| 1153 | 1154 |
| 1154 int rv = ResolveHelper(request_id, key, info, addresses, request_net_log); | 1155 int rv = ResolveHelper(request_id, key, info, addresses, request_net_log); |
| 1155 if (rv != ERR_DNS_CACHE_MISS) { | 1156 if (rv != ERR_DNS_CACHE_MISS) { |
| 1156 OnFinishRequest(source_net_log, request_net_log, request_id, info, | 1157 OnFinishRequest(source_net_log, request_net_log, request_id, info, |
| 1157 rv, | 1158 rv, |
| 1158 0 /* os_error (unknown since from cache) */); | 1159 0 /* os_error (unknown since from cache) */); |
| 1159 return rv; | 1160 return rv; |
| 1160 } | 1161 } |
| 1161 | 1162 |
| 1162 // Create a handle for this request, and pass it back to the user if they | 1163 // Create a handle for this request, and pass it back to the user if they |
| 1163 // asked for it (out_req != NULL). | 1164 // asked for it (out_req != NULL). |
| 1164 Request* req = new Request(source_net_log, request_net_log, request_id, info, | 1165 Request* req = new Request(source_net_log, request_net_log, request_id, info, |
| 1165 callback, addresses); | 1166 callback, addresses); |
| 1166 if (out_req) | 1167 if (out_req) |
| 1167 *out_req = reinterpret_cast<RequestHandle>(req); | 1168 *out_req = reinterpret_cast<RequestHandle>(req); |
| 1168 | 1169 |
| 1169 // Next we need to attach our request to a "job". This job is responsible for | 1170 // Next we need to attach our request to a "job". This job is responsible for |
| 1170 // calling "getaddrinfo(hostname)" on a worker thread. | 1171 // calling "getaddrinfo(hostname)" on a worker thread. |
| 1171 scoped_refptr<Job> job; | 1172 scoped_refptr<Job> job; |
| 1172 | 1173 |
| (...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1654 // resolv.conf changes so we don't need to do anything to clear that cache. | 1655 // resolv.conf changes so we don't need to do anything to clear that cache. |
| 1655 if (cache_.get()) | 1656 if (cache_.get()) |
| 1656 cache_->clear(); | 1657 cache_->clear(); |
| 1657 // Existing jobs will have been sent to the original server so they need to | 1658 // Existing jobs will have been sent to the original server so they need to |
| 1658 // be aborted. TODO(Craig): Should these jobs be restarted? | 1659 // be aborted. TODO(Craig): Should these jobs be restarted? |
| 1659 AbortAllInProgressJobs(); | 1660 AbortAllInProgressJobs(); |
| 1660 // |this| may be deleted inside AbortAllInProgressJobs(). | 1661 // |this| may be deleted inside AbortAllInProgressJobs(). |
| 1661 } | 1662 } |
| 1662 | 1663 |
| 1663 } // namespace net | 1664 } // namespace net |
| OLD | NEW |