Home | History | Annotate | Download | only in base
      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
      3 // found in the LICENSE file.
      4 
      5 #include "net/base/host_resolver.h"
      6 
      7 #include "base/compiler_specific.h"
      8 #include "base/logging.h"
      9 #include "net/base/net_errors.h"
     10 
     11 namespace net {
     12 
     13 HostResolver::RequestInfo::RequestInfo(const HostPortPair& host_port_pair)
     14     : host_port_pair_(host_port_pair),
     15       address_family_(ADDRESS_FAMILY_UNSPECIFIED),
     16       host_resolver_flags_(0),
     17       allow_cached_response_(true),
     18       only_use_cached_response_(false),
     19       is_speculative_(false),
     20       priority_(MEDIUM) {
     21 }
     22 
     23 HostResolver::~HostResolver() {
     24 }
     25 
     26 AddressFamily HostResolver::GetDefaultAddressFamily() const {
     27   return ADDRESS_FAMILY_UNSPECIFIED;
     28 }
     29 
     30 HostResolverImpl* HostResolver::GetAsHostResolverImpl() {
     31   return NULL;
     32 }
     33 
     34 HostResolver::HostResolver() {
     35 }
     36 
     37 SingleRequestHostResolver::SingleRequestHostResolver(HostResolver* resolver)
     38     : resolver_(resolver),
     39       cur_request_(NULL),
     40       cur_request_callback_(NULL),
     41       ALLOW_THIS_IN_INITIALIZER_LIST(
     42           callback_(this, &SingleRequestHostResolver::OnResolveCompletion)) {
     43   DCHECK(resolver_ != NULL);
     44 }
     45 
     46 SingleRequestHostResolver::~SingleRequestHostResolver() {
     47   Cancel();
     48 }
     49 
     50 int SingleRequestHostResolver::Resolve(const HostResolver::RequestInfo& info,
     51                                        AddressList* addresses,
     52                                        CompletionCallback* callback,
     53                                        const BoundNetLog& net_log) {
     54   DCHECK(!cur_request_ && !cur_request_callback_) << "resolver already in use";
     55 
     56   HostResolver::RequestHandle request = NULL;
     57 
     58   // We need to be notified of completion before |callback| is called, so that
     59   // we can clear out |cur_request_*|.
     60   CompletionCallback* transient_callback = callback ? &callback_ : NULL;
     61 
     62   int rv = resolver_->Resolve(
     63       info, addresses, transient_callback, &request, net_log);
     64 
     65   if (rv == ERR_IO_PENDING) {
     66     // Cleared in OnResolveCompletion().
     67     cur_request_ = request;
     68     cur_request_callback_ = callback;
     69   }
     70 
     71   return rv;
     72 }
     73 
     74 void SingleRequestHostResolver::Cancel() {
     75   if (cur_request_) {
     76     resolver_->CancelRequest(cur_request_);
     77     cur_request_ = NULL;
     78   }
     79 }
     80 
     81 void SingleRequestHostResolver::OnResolveCompletion(int result) {
     82   DCHECK(cur_request_ && cur_request_callback_);
     83 
     84   CompletionCallback* callback = cur_request_callback_;
     85 
     86   // Clear the outstanding request information.
     87   cur_request_ = NULL;
     88   cur_request_callback_ = NULL;
     89 
     90   // Call the user's original callback.
     91   callback->Run(result);
     92 }
     93 
     94 }  // namespace net
     95