• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef NET_BASE_HOST_RESOLVER_H_
6 #define NET_BASE_HOST_RESOLVER_H_
7 #pragma once
8 
9 #include <string>
10 
11 #include "base/memory/scoped_ptr.h"
12 #include "googleurl/src/gurl.h"
13 #include "net/base/address_family.h"
14 #include "net/base/completion_callback.h"
15 #include "net/base/host_port_pair.h"
16 #include "net/base/request_priority.h"
17 
18 namespace net {
19 
20 class AddressList;
21 class BoundNetLog;
22 class HostResolverImpl;
23 class HostResolverProc;
24 class NetLog;
25 
26 // This class represents the task of resolving hostnames (or IP address
27 // literal) to an AddressList object.
28 //
29 // HostResolver can handle multiple requests at a time, so when cancelling a
30 // request the RequestHandle that was returned by Resolve() needs to be
31 // given.  A simpler alternative for consumers that only have 1 outstanding
32 // request at a time is to create a SingleRequestHostResolver wrapper around
33 // HostResolver (which will automatically cancel the single request when it
34 // goes out of scope).
35 class HostResolver {
36  public:
37   // The parameters for doing a Resolve(). A hostname and port are required,
38   // the rest are optional (and have reasonable defaults).
39   class RequestInfo {
40    public:
41     explicit RequestInfo(const HostPortPair& host_port_pair);
42 
host_port_pair()43     const HostPortPair& host_port_pair() const { return host_port_pair_; }
set_host_port_pair(const HostPortPair & host_port_pair)44     void set_host_port_pair(const HostPortPair& host_port_pair) {
45       host_port_pair_ = host_port_pair;
46     }
47 
port()48     int port() const { return host_port_pair_.port(); }
hostname()49     const std::string& hostname() const { return host_port_pair_.host(); }
50 
address_family()51     AddressFamily address_family() const { return address_family_; }
set_address_family(AddressFamily address_family)52     void set_address_family(AddressFamily address_family) {
53       address_family_ = address_family;
54     }
55 
host_resolver_flags()56     HostResolverFlags host_resolver_flags() const {
57       return host_resolver_flags_;
58     }
set_host_resolver_flags(HostResolverFlags host_resolver_flags)59     void set_host_resolver_flags(HostResolverFlags host_resolver_flags) {
60       host_resolver_flags_ = host_resolver_flags;
61     }
62 
allow_cached_response()63     bool allow_cached_response() const { return allow_cached_response_; }
set_allow_cached_response(bool b)64     void set_allow_cached_response(bool b) { allow_cached_response_ = b; }
65 
only_use_cached_response()66     bool only_use_cached_response() const { return only_use_cached_response_; }
set_only_use_cached_response(bool b)67     void set_only_use_cached_response(bool b) { only_use_cached_response_ = b; }
68 
is_speculative()69     bool is_speculative() const { return is_speculative_; }
set_is_speculative(bool b)70     void set_is_speculative(bool b) { is_speculative_ = b; }
71 
priority()72     RequestPriority priority() const { return priority_; }
set_priority(RequestPriority priority)73     void set_priority(RequestPriority priority) { priority_ = priority; }
74 
referrer()75     const GURL& referrer() const { return referrer_; }
set_referrer(const GURL & referrer)76     void set_referrer(const GURL& referrer) { referrer_ = referrer; }
77 
78    private:
79     // The hostname to resolve, and the port to use in resulting sockaddrs.
80     HostPortPair host_port_pair_;
81 
82     // The address family to restrict results to.
83     AddressFamily address_family_;
84 
85     // Flags to use when resolving this request.
86     HostResolverFlags host_resolver_flags_;
87 
88     // Whether it is ok to return a result from the host cache.
89     bool allow_cached_response_;
90 
91     // Whether the response will only use the cache.
92     bool only_use_cached_response_;
93 
94     // Whether this request was started by the DNS prefetcher.
95     bool is_speculative_;
96 
97     // The priority for the request.
98     RequestPriority priority_;
99 
100     // Optional data for consumption by observers. This is the URL of the
101     // page that lead us to the navigation, for DNS prefetcher's benefit.
102     GURL referrer_;
103   };
104 
105   // Interface for observing the requests that flow through a HostResolver.
106   class Observer {
107    public:
~Observer()108     virtual ~Observer() {}
109 
110     // Called at the start of HostResolver::Resolve(). |id| is a unique number
111     // given to the request, so it can be matched up with a corresponding call
112     // to OnFinishResolutionWithStatus() or OnCancelResolution().
113     virtual void OnStartResolution(int id, const RequestInfo& info) = 0;
114 
115     // Called on completion of request |id|. Note that if the request was
116     // cancelled, OnCancelResolution() will be called instead.
117     virtual void OnFinishResolutionWithStatus(int id, bool was_resolved,
118                                               const RequestInfo& info) = 0;
119 
120     // Called when request |id| has been cancelled. A request is "cancelled"
121     // if either the HostResolver is destroyed while a resolution is in
122     // progress, or HostResolver::CancelRequest() is called.
123     virtual void OnCancelResolution(int id, const RequestInfo& info) = 0;
124   };
125 
126   // Opaque type used to cancel a request.
127   typedef void* RequestHandle;
128 
129   // This value can be passed into CreateSystemHostResolver as the
130   // |max_concurrent_resolves| parameter. It will select a default level of
131   // concurrency.
132   static const size_t kDefaultParallelism = 0;
133 
134   // If any completion callbacks are pending when the resolver is destroyed,
135   // the host resolutions are cancelled, and the completion callbacks will not
136   // be called.
137   virtual ~HostResolver();
138 
139   // Resolves the given hostname (or IP address literal), filling out the
140   // |addresses| object upon success.  The |info.port| parameter will be set as
141   // the sin(6)_port field of the sockaddr_in{6} struct.  Returns OK if
142   // successful or an error code upon failure.
143   //
144   // When callback is null, the operation completes synchronously.
145   //
146   // When callback is non-null, the operation may be performed asynchronously.
147   // If the operation cannnot be completed synchronously, ERR_IO_PENDING will
148   // be returned and the real result code will be passed to the completion
149   // callback.  Otherwise the result code is returned immediately from this
150   // call.
151   // If |out_req| is non-NULL, then |*out_req| will be filled with a handle to
152   // the async request. This handle is not valid after the request has
153   // completed.
154   //
155   // Profiling information for the request is saved to |net_log| if non-NULL.
156   virtual int Resolve(const RequestInfo& info,
157                       AddressList* addresses,
158                       CompletionCallback* callback,
159                       RequestHandle* out_req,
160                       const BoundNetLog& net_log) = 0;
161 
162   // Cancels the specified request. |req| is the handle returned by Resolve().
163   // After a request is cancelled, its completion callback will not be called.
164   virtual void CancelRequest(RequestHandle req) = 0;
165 
166   // Adds an observer to this resolver. The observer will be notified of the
167   // start and completion of all requests (excluding cancellation). |observer|
168   // must remain valid for the duration of this HostResolver's lifetime.
169   virtual void AddObserver(Observer* observer) = 0;
170 
171   // Unregisters an observer previously added by AddObserver().
172   virtual void RemoveObserver(Observer* observer) = 0;
173 
174   // Sets the default AddressFamily to use when requests have left it
175   // unspecified. For example, this could be used to restrict resolution
176   // results to AF_INET by passing in ADDRESS_FAMILY_IPV4, or to
177   // AF_INET6 by passing in ADDRESS_FAMILY_IPV6.
SetDefaultAddressFamily(AddressFamily address_family)178   virtual void SetDefaultAddressFamily(AddressFamily address_family) {}
179   virtual AddressFamily GetDefaultAddressFamily() const;
180 
181   // Returns |this| cast to a HostResolverImpl*, or NULL if the subclass
182   // is not compatible with HostResolverImpl. Used primarily to expose
183   // additional functionality on the about:net-internals page.
184   virtual HostResolverImpl* GetAsHostResolverImpl();
185 
186   // Does additional cleanup prior to destruction.
Shutdown()187   virtual void Shutdown() {}
188 
189  protected:
190   HostResolver();
191 
192  private:
193   DISALLOW_COPY_AND_ASSIGN(HostResolver);
194 };
195 
196 // This class represents the task of resolving a hostname (or IP address
197 // literal) to an AddressList object.  It wraps HostResolver to resolve only a
198 // single hostname at a time and cancels this request when going out of scope.
199 class SingleRequestHostResolver {
200  public:
201   // |resolver| must remain valid for the lifetime of |this|.
202   explicit SingleRequestHostResolver(HostResolver* resolver);
203 
204   // If a completion callback is pending when the resolver is destroyed, the
205   // host resolution is cancelled, and the completion callback will not be
206   // called.
207   ~SingleRequestHostResolver();
208 
209   // Resolves the given hostname (or IP address literal), filling out the
210   // |addresses| object upon success. See HostResolver::Resolve() for details.
211   int Resolve(const HostResolver::RequestInfo& info,
212               AddressList* addresses,
213               CompletionCallback* callback,
214               const BoundNetLog& net_log);
215 
216   // Cancels the in-progress request, if any. This prevents the callback
217   // from being invoked. Resolve() can be called again after cancelling.
218   void Cancel();
219 
220  private:
221   // Callback for when the request to |resolver_| completes, so we dispatch
222   // to the user's callback.
223   void OnResolveCompletion(int result);
224 
225   // The actual host resolver that will handle the request.
226   HostResolver* const resolver_;
227 
228   // The current request (if any).
229   HostResolver::RequestHandle cur_request_;
230   CompletionCallback* cur_request_callback_;
231 
232   // Completion callback for when request to |resolver_| completes.
233   CompletionCallbackImpl<SingleRequestHostResolver> callback_;
234 
235   DISALLOW_COPY_AND_ASSIGN(SingleRequestHostResolver);
236 };
237 
238 // Creates a HostResolver implementation using |resolver_proc| as resolver,
239 // (which if NULL, will default to getaddrinfo() wrapper) that queries the
240 // underlying system, |max_concurrent_resolves| is how many resolve
241 // requests will be allowed to run in parallel. Pass
242 // HostResolver::kDefaultParallelism to choose a default value.
243 HostResolver* CreateSystemHostResolver(size_t max_concurrent_resolves,
244                                        HostResolverProc* resolver_proc,
245                                        NetLog* net_log);
246 
247 }  // namespace net
248 
249 #endif  // NET_BASE_HOST_RESOLVER_H_
250