| 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/dns/async_host_resolver.h" | 5 #include "net/dns/async_host_resolver.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 // cancelled, which can be either through specific CancelRequest call or by | 77 // cancelled, which can be either through specific CancelRequest call or by |
| 78 // the destruction of AsyncHostResolver, which would destruct pending or | 78 // the destruction of AsyncHostResolver, which would destruct pending or |
| 79 // in-progress requests, causing them to be cancelled. Synchronous | 79 // in-progress requests, causing them to be cancelled. Synchronous |
| 80 // resolution sets |callback_| to NULL, thus, if in the destructor we still | 80 // resolution sets |callback_| to NULL, thus, if in the destructor we still |
| 81 // have a non-NULL |callback_|, we are being cancelled. | 81 // have a non-NULL |callback_|, we are being cancelled. |
| 82 class AsyncHostResolver::Request { | 82 class AsyncHostResolver::Request { |
| 83 public: | 83 public: |
| 84 Request(AsyncHostResolver* resolver, | 84 Request(AsyncHostResolver* resolver, |
| 85 const BoundNetLog& source_net_log, | 85 const BoundNetLog& source_net_log, |
| 86 const BoundNetLog& request_net_log, | 86 const BoundNetLog& request_net_log, |
| 87 int id, | |
| 88 const HostResolver::RequestInfo& info, | 87 const HostResolver::RequestInfo& info, |
| 89 OldCompletionCallback* callback, | 88 OldCompletionCallback* callback, |
| 90 AddressList* addresses) | 89 AddressList* addresses) |
| 91 : resolver_(resolver), | 90 : resolver_(resolver), |
| 92 source_net_log_(source_net_log), | 91 source_net_log_(source_net_log), |
| 93 request_net_log_(request_net_log), | 92 request_net_log_(request_net_log), |
| 94 id_(id), | |
| 95 info_(info), | 93 info_(info), |
| 96 callback_(callback), | 94 callback_(callback), |
| 97 addresses_(addresses), | 95 addresses_(addresses), |
| 98 result_(ERR_UNEXPECTED) { | 96 result_(ERR_UNEXPECTED) { |
| 99 DCHECK(addresses_); | 97 DCHECK(addresses_); |
| 100 DCHECK(resolver_); | 98 DCHECK(resolver_); |
| 101 resolver_->OnStart(this); | 99 resolver_->OnStart(this); |
| 102 std::string dns_name; | 100 std::string dns_name; |
| 103 if (DNSDomainFromDot(info.hostname(), &dns_name)) | 101 if (DNSDomainFromDot(info.hostname(), &dns_name)) |
| 104 key_ = Key(dns_name, QueryTypeFromAddressFamily(info.address_family())); | 102 key_ = Key(dns_name, QueryTypeFromAddressFamily(info.address_family())); |
| 105 } | 103 } |
| 106 | 104 |
| 107 ~Request() { | 105 ~Request() { |
| 108 if (callback_) | 106 if (callback_) |
| 109 resolver_->OnCancel(this); | 107 resolver_->OnCancel(this); |
| 110 } | 108 } |
| 111 | 109 |
| 112 int id() const { return id_; } | |
| 113 int result() const { return result_; } | 110 int result() const { return result_; } |
| 114 const Key& key() const { | 111 const Key& key() const { |
| 115 DCHECK(IsValid()); | 112 DCHECK(IsValid()); |
| 116 return key_; | 113 return key_; |
| 117 } | 114 } |
| 118 const HostResolver::RequestInfo& info() const { return info_; } | 115 const HostResolver::RequestInfo& info() const { return info_; } |
| 119 RequestPriority priority() const { return info_.priority(); } | 116 RequestPriority priority() const { return info_.priority(); } |
| 120 const BoundNetLog& source_net_log() const { return source_net_log_; } | 117 const BoundNetLog& source_net_log() const { return source_net_log_; } |
| 121 const BoundNetLog& request_net_log() const { return request_net_log_; } | 118 const BoundNetLog& request_net_log() const { return request_net_log_; } |
| 122 | 119 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 | 178 |
| 182 // Returns true if request has a validly formed hostname. | 179 // Returns true if request has a validly formed hostname. |
| 183 bool IsValid() const { | 180 bool IsValid() const { |
| 184 return !info_.hostname().empty() && !key_.first.empty(); | 181 return !info_.hostname().empty() && !key_.first.empty(); |
| 185 } | 182 } |
| 186 | 183 |
| 187 private: | 184 private: |
| 188 AsyncHostResolver* resolver_; | 185 AsyncHostResolver* resolver_; |
| 189 BoundNetLog source_net_log_; | 186 BoundNetLog source_net_log_; |
| 190 BoundNetLog request_net_log_; | 187 BoundNetLog request_net_log_; |
| 191 const int id_; | |
| 192 const HostResolver::RequestInfo info_; | 188 const HostResolver::RequestInfo info_; |
| 193 Key key_; | 189 Key key_; |
| 194 OldCompletionCallback* callback_; | 190 OldCompletionCallback* callback_; |
| 195 AddressList* addresses_; | 191 AddressList* addresses_; |
| 196 int result_; | 192 int result_; |
| 197 }; | 193 }; |
| 198 | 194 |
| 199 //----------------------------------------------------------------------------- | 195 //----------------------------------------------------------------------------- |
| 200 AsyncHostResolver::AsyncHostResolver(const IPEndPoint& dns_server, | 196 AsyncHostResolver::AsyncHostResolver(const IPEndPoint& dns_server, |
| 201 size_t max_transactions, | 197 size_t max_transactions, |
| 202 size_t max_pending_requests, | 198 size_t max_pending_requests, |
| 203 const RandIntCallback& rand_int_cb, | 199 const RandIntCallback& rand_int_cb, |
| 204 HostCache* cache, | 200 HostCache* cache, |
| 205 ClientSocketFactory* factory, | 201 ClientSocketFactory* factory, |
| 206 NetLog* net_log) | 202 NetLog* net_log) |
| 207 : max_transactions_(max_transactions), | 203 : max_transactions_(max_transactions), |
| 208 max_pending_requests_(max_pending_requests), | 204 max_pending_requests_(max_pending_requests), |
| 209 dns_server_(dns_server), | 205 dns_server_(dns_server), |
| 210 rand_int_cb_(rand_int_cb), | 206 rand_int_cb_(rand_int_cb), |
| 211 cache_(cache), | 207 cache_(cache), |
| 212 factory_(factory), | 208 factory_(factory), |
| 213 next_request_id_(0), | |
| 214 net_log_(net_log) { | 209 net_log_(net_log) { |
| 215 } | 210 } |
| 216 | 211 |
| 217 AsyncHostResolver::~AsyncHostResolver() { | 212 AsyncHostResolver::~AsyncHostResolver() { |
| 218 // Destroy request lists. | 213 // Destroy request lists. |
| 219 for (KeyRequestListMap::iterator it = requestlist_map_.begin(); | 214 for (KeyRequestListMap::iterator it = requestlist_map_.begin(); |
| 220 it != requestlist_map_.end(); ++it) | 215 it != requestlist_map_.end(); ++it) |
| 221 STLDeleteElements(&it->second); | 216 STLDeleteElements(&it->second); |
| 222 | 217 |
| 223 // Destroy transactions. | 218 // Destroy transactions. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 DCHECK(request); | 275 DCHECK(request); |
| 281 | 276 |
| 282 request->source_net_log().BeginEvent( | 277 request->source_net_log().BeginEvent( |
| 283 NetLog::TYPE_ASYNC_HOST_RESOLVER, | 278 NetLog::TYPE_ASYNC_HOST_RESOLVER, |
| 284 make_scoped_refptr(new NetLogSourceParameter( | 279 make_scoped_refptr(new NetLogSourceParameter( |
| 285 "source_dependency", request->request_net_log().source()))); | 280 "source_dependency", request->request_net_log().source()))); |
| 286 request->request_net_log().BeginEvent( | 281 request->request_net_log().BeginEvent( |
| 287 NetLog::TYPE_ASYNC_HOST_RESOLVER_REQUEST, | 282 NetLog::TYPE_ASYNC_HOST_RESOLVER_REQUEST, |
| 288 make_scoped_refptr(new RequestParameters( | 283 make_scoped_refptr(new RequestParameters( |
| 289 request->info(), request->source_net_log().source()))); | 284 request->info(), request->source_net_log().source()))); |
| 290 | |
| 291 FOR_EACH_OBSERVER( | |
| 292 HostResolver::Observer, observers_, | |
| 293 OnStartResolution(request->id(), request->info())); | |
| 294 } | 285 } |
| 295 | 286 |
| 296 void AsyncHostResolver::OnFinish(Request* request, int result) { | 287 void AsyncHostResolver::OnFinish(Request* request, int result) { |
| 297 DCHECK(request); | 288 DCHECK(request); |
| 298 bool was_resolved = result == OK; | |
| 299 | |
| 300 FOR_EACH_OBSERVER( | |
| 301 HostResolver::Observer, observers_, | |
| 302 OnFinishResolutionWithStatus( | |
| 303 request->id(), was_resolved, request->info())); | |
| 304 | |
| 305 request->request_net_log().EndEventWithNetErrorCode( | 289 request->request_net_log().EndEventWithNetErrorCode( |
| 306 NetLog::TYPE_ASYNC_HOST_RESOLVER_REQUEST, result); | 290 NetLog::TYPE_ASYNC_HOST_RESOLVER_REQUEST, result); |
| 307 request->source_net_log().EndEvent( | 291 request->source_net_log().EndEvent( |
| 308 NetLog::TYPE_ASYNC_HOST_RESOLVER, NULL); | 292 NetLog::TYPE_ASYNC_HOST_RESOLVER, NULL); |
| 309 } | 293 } |
| 310 | 294 |
| 311 void AsyncHostResolver::OnCancel(Request* request) { | 295 void AsyncHostResolver::OnCancel(Request* request) { |
| 312 DCHECK(request); | 296 DCHECK(request); |
| 313 | 297 |
| 314 FOR_EACH_OBSERVER( | |
| 315 HostResolver::Observer, observers_, | |
| 316 OnCancelResolution(request->id(), request->info())); | |
| 317 | |
| 318 request->request_net_log().AddEvent( | 298 request->request_net_log().AddEvent( |
| 319 NetLog::TYPE_CANCELLED, NULL); | 299 NetLog::TYPE_CANCELLED, NULL); |
| 320 request->request_net_log().EndEvent( | 300 request->request_net_log().EndEvent( |
| 321 NetLog::TYPE_ASYNC_HOST_RESOLVER_REQUEST, NULL); | 301 NetLog::TYPE_ASYNC_HOST_RESOLVER_REQUEST, NULL); |
| 322 request->source_net_log().EndEvent( | 302 request->source_net_log().EndEvent( |
| 323 NetLog::TYPE_ASYNC_HOST_RESOLVER, NULL); | 303 NetLog::TYPE_ASYNC_HOST_RESOLVER, NULL); |
| 324 } | 304 } |
| 325 | 305 |
| 326 void AsyncHostResolver::CancelRequest(RequestHandle req_handle) { | 306 void AsyncHostResolver::CancelRequest(RequestHandle req_handle) { |
| 327 scoped_ptr<Request> request(reinterpret_cast<Request*>(req_handle)); | 307 scoped_ptr<Request> request(reinterpret_cast<Request*>(req_handle)); |
| 328 DCHECK(request.get()); | 308 DCHECK(request.get()); |
| 329 | 309 |
| 330 KeyRequestListMap::iterator it = requestlist_map_.find(request->key()); | 310 KeyRequestListMap::iterator it = requestlist_map_.find(request->key()); |
| 331 if (it != requestlist_map_.end()) | 311 if (it != requestlist_map_.end()) |
| 332 it->second.remove(request.get()); | 312 it->second.remove(request.get()); |
| 333 else | 313 else |
| 334 pending_requests_[request->priority()].remove(request.get()); | 314 pending_requests_[request->priority()].remove(request.get()); |
| 335 } | 315 } |
| 336 | 316 |
| 337 void AsyncHostResolver::AddObserver(HostResolver::Observer* observer) { | |
| 338 observers_.AddObserver(observer); | |
| 339 } | |
| 340 | |
| 341 void AsyncHostResolver::RemoveObserver(HostResolver::Observer* observer) { | |
| 342 observers_.RemoveObserver(observer); | |
| 343 } | |
| 344 | |
| 345 void AsyncHostResolver::SetDefaultAddressFamily( | 317 void AsyncHostResolver::SetDefaultAddressFamily( |
| 346 AddressFamily address_family) { | 318 AddressFamily address_family) { |
| 347 NOTIMPLEMENTED(); | 319 NOTIMPLEMENTED(); |
| 348 } | 320 } |
| 349 | 321 |
| 350 AddressFamily AsyncHostResolver::GetDefaultAddressFamily() const { | 322 AddressFamily AsyncHostResolver::GetDefaultAddressFamily() const { |
| 351 return ADDRESS_FAMILY_IPV4; | 323 return ADDRESS_FAMILY_IPV4; |
| 352 } | 324 } |
| 353 | 325 |
| 354 HostCache* AsyncHostResolver::GetHostCache() { | 326 HostCache* AsyncHostResolver::GetHostCache() { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 ProcessPending(); | 380 ProcessPending(); |
| 409 } | 381 } |
| 410 | 382 |
| 411 AsyncHostResolver::Request* AsyncHostResolver::CreateNewRequest( | 383 AsyncHostResolver::Request* AsyncHostResolver::CreateNewRequest( |
| 412 const RequestInfo& info, | 384 const RequestInfo& info, |
| 413 OldCompletionCallback* callback, | 385 OldCompletionCallback* callback, |
| 414 AddressList* addresses, | 386 AddressList* addresses, |
| 415 const BoundNetLog& source_net_log) { | 387 const BoundNetLog& source_net_log) { |
| 416 BoundNetLog request_net_log = BoundNetLog::Make(net_log_, | 388 BoundNetLog request_net_log = BoundNetLog::Make(net_log_, |
| 417 NetLog::SOURCE_ASYNC_HOST_RESOLVER_REQUEST); | 389 NetLog::SOURCE_ASYNC_HOST_RESOLVER_REQUEST); |
| 418 int id = next_request_id_++; | |
| 419 return new Request( | 390 return new Request( |
| 420 this, source_net_log, request_net_log, id, info, callback, addresses); | 391 this, source_net_log, request_net_log, info, callback, addresses); |
| 421 } | 392 } |
| 422 | 393 |
| 423 bool AsyncHostResolver::AttachToRequestList(Request* request) { | 394 bool AsyncHostResolver::AttachToRequestList(Request* request) { |
| 424 KeyRequestListMap::iterator it = requestlist_map_.find(request->key()); | 395 KeyRequestListMap::iterator it = requestlist_map_.find(request->key()); |
| 425 if (it == requestlist_map_.end()) | 396 if (it == requestlist_map_.end()) |
| 426 return false; | 397 return false; |
| 427 it->second.push_back(request); | 398 it->second.push_back(request); |
| 428 return true; | 399 return true; |
| 429 } | 400 } |
| 430 | 401 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 it = requests.erase(it); | 487 it = requests.erase(it); |
| 517 } else { | 488 } else { |
| 518 ++it; | 489 ++it; |
| 519 } | 490 } |
| 520 } | 491 } |
| 521 } | 492 } |
| 522 StartNewTransactionFor(request); | 493 StartNewTransactionFor(request); |
| 523 } | 494 } |
| 524 | 495 |
| 525 } // namespace net | 496 } // namespace net |
| OLD | NEW |