| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #ifndef NET_BASE_HOST_RESOLVER_H_ | 5 #ifndef NET_BASE_HOST_RESOLVER_H_ |
| 6 #define NET_BASE_HOST_RESOLVER_H_ | 6 #define NET_BASE_HOST_RESOLVER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | |
| 10 | 9 |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/lock.h" | |
| 13 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
| 14 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
| 15 #include "net/base/completion_callback.h" | 12 #include "net/base/completion_callback.h" |
| 16 #include "net/base/host_cache.h" | |
| 17 | 13 |
| 18 class MessageLoop; | 14 class MessageLoop; |
| 19 | 15 |
| 20 namespace net { | 16 namespace net { |
| 21 | 17 |
| 22 class AddressList; | 18 class AddressList; |
| 23 class HostMapper; | |
| 24 | 19 |
| 25 // This class represents the task of resolving hostnames (or IP address | 20 // This class represents the task of resolving hostnames (or IP address |
| 26 // literal) to an AddressList object. | 21 // literal) to an AddressList object. |
| 27 // | 22 // |
| 28 // HostResolver handles multiple requests at a time, so when cancelling a | 23 // HostResolver can handle multiple requests at a time, so when cancelling a |
| 29 // request the Request* handle that was returned by Resolve() needs to be | 24 // request the RequestHandle that was returned by Resolve() needs to be |
| 30 // given. A simpler alternative for consumers that only have 1 outstanding | 25 // given. A simpler alternative for consumers that only have 1 outstanding |
| 31 // request at a time is to create a SingleRequestHostResolver wrapper around | 26 // request at a time is to create a SingleRequestHostResolver wrapper around |
| 32 // HostResolver (which will automatically cancel the single request when it | 27 // HostResolver (which will automatically cancel the single request when it |
| 33 // goes out of scope). | 28 // goes out of scope). |
| 34 // | |
| 35 // For each hostname that is requested, HostResolver creates a | |
| 36 // HostResolver::Job. This job gets dispatched to a thread in the global | |
| 37 // WorkerPool, where it runs "getaddrinfo(hostname)". If requests for that same | |
| 38 // host are made while the job is already outstanding, then they are attached | |
| 39 // to the existing job rather than creating a new one. This avoids doing | |
| 40 // parallel resolves for the same host. | |
| 41 // | |
| 42 // The way these classes fit together is illustrated by: | |
| 43 // | |
| 44 // | |
| 45 // +------------- HostResolver ---------------+ | |
| 46 // | | | | |
| 47 // Job Job Job | |
| 48 // (for host1) (for host2) (for hostX) | |
| 49 // / | | / | | / | | | |
| 50 // Request ... Request Request ... Request Request ... Request | |
| 51 // (port1) (port2) (port3) (port4) (port5) (portX) | |
| 52 // | |
| 53 // | |
| 54 // When a HostResolver::Job finishes its work in the threadpool, the callbacks | |
| 55 // of each waiting request are run on the origin thread. | |
| 56 // | |
| 57 // Thread safety: This class is not threadsafe, and must only be called | |
| 58 // from one thread! | |
| 59 // | |
| 60 class HostResolver : public base::RefCounted<HostResolver> { | 29 class HostResolver : public base::RefCounted<HostResolver> { |
| 61 public: | 30 public: |
| 62 // The parameters for doing a Resolve(). |hostname| and |port| are required, | 31 // The parameters for doing a Resolve(). |hostname| and |port| are required, |
| 63 // the rest are optional (and have reasonable defaults). | 32 // the rest are optional (and have reasonable defaults). |
| 64 class RequestInfo { | 33 class RequestInfo { |
| 65 public: | 34 public: |
| 66 RequestInfo(const std::string& hostname, int port) | 35 RequestInfo(const std::string& hostname, int port) |
| 67 : hostname_(hostname), | 36 : hostname_(hostname), |
| 68 port_(port), | 37 port_(port), |
| 69 allow_cached_response_(true), | 38 allow_cached_response_(true), |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 // cancelled, OnCancelResolution() will be called instead. | 82 // cancelled, OnCancelResolution() will be called instead. |
| 114 virtual void OnFinishResolutionWithStatus(int id, bool was_resolved, | 83 virtual void OnFinishResolutionWithStatus(int id, bool was_resolved, |
| 115 const RequestInfo& info) = 0; | 84 const RequestInfo& info) = 0; |
| 116 | 85 |
| 117 // Called when request |id| has been cancelled. A request is "cancelled" | 86 // Called when request |id| has been cancelled. A request is "cancelled" |
| 118 // if either the HostResolver is destroyed while a resolution is in | 87 // if either the HostResolver is destroyed while a resolution is in |
| 119 // progress, or HostResolver::CancelRequest() is called. | 88 // progress, or HostResolver::CancelRequest() is called. |
| 120 virtual void OnCancelResolution(int id, const RequestInfo& info) = 0; | 89 virtual void OnCancelResolution(int id, const RequestInfo& info) = 0; |
| 121 }; | 90 }; |
| 122 | 91 |
| 123 // Creates a HostResolver that caches up to |max_cache_entries| for | 92 // Opaque type used to cancel a request. |
| 124 // |cache_duration_ms| milliseconds. | 93 typedef void* RequestHandle; |
| 125 // | 94 |
| 126 // TODO(eroman): Get rid of the default parameters as it violate google | 95 HostResolver() {} |
| 127 // style. This is temporary to help with refactoring. | |
| 128 HostResolver(int max_cache_entries = 100, int cache_duration_ms = 60000); | |
| 129 | 96 |
| 130 // If any completion callbacks are pending when the resolver is destroyed, | 97 // If any completion callbacks are pending when the resolver is destroyed, |
| 131 // the host resolutions are cancelled, and the completion callbacks will not | 98 // the host resolutions are cancelled, and the completion callbacks will not |
| 132 // be called. | 99 // be called. |
| 133 ~HostResolver(); | 100 virtual ~HostResolver() {} |
| 134 | |
| 135 // Opaque type used to cancel a request. | |
| 136 class Request; | |
| 137 | 101 |
| 138 // Resolves the given hostname (or IP address literal), filling out the | 102 // Resolves the given hostname (or IP address literal), filling out the |
| 139 // |addresses| object upon success. The |info.port| parameter will be set as | 103 // |addresses| object upon success. The |info.port| parameter will be set as |
| 140 // the sin(6)_port field of the sockaddr_in{6} struct. Returns OK if | 104 // the sin(6)_port field of the sockaddr_in{6} struct. Returns OK if |
| 141 // successful or an error code upon failure. | 105 // successful or an error code upon failure. |
| 142 // | 106 // |
| 143 // When callback is null, the operation completes synchronously. | 107 // When callback is null, the operation completes synchronously. |
| 144 // | 108 // |
| 145 // When callback is non-null, the operation will be performed asynchronously. | 109 // When callback is non-null, the operation will be performed asynchronously. |
| 146 // ERR_IO_PENDING is returned if it has been scheduled successfully. Real | 110 // ERR_IO_PENDING is returned if it has been scheduled successfully. Real |
| 147 // result code will be passed to the completion callback. If |req| is | 111 // result code will be passed to the completion callback. If |req| is |
| 148 // non-NULL, then |*req| will be filled with a handle to the async request. | 112 // non-NULL, then |*req| will be filled with a handle to the async request. |
| 149 // This handle is not valid after the request has completed. | 113 // This handle is not valid after the request has completed. |
| 150 int Resolve(const RequestInfo& info, AddressList* addresses, | 114 virtual int Resolve(const RequestInfo& info, AddressList* addresses, |
| 151 CompletionCallback* callback, Request** req); | 115 CompletionCallback* callback, RequestHandle* out_req) = 0; |
| 152 | 116 |
| 153 // Cancels the specified request. |req| is the handle returned by Resolve(). | 117 // Cancels the specified request. |req| is the handle returned by Resolve(). |
| 154 // After a request is cancelled, its completion callback will not be called. | 118 // After a request is cancelled, its completion callback will not be called. |
| 155 void CancelRequest(Request* req); | 119 virtual void CancelRequest(RequestHandle req) = 0; |
| 156 | 120 |
| 157 // Adds an observer to this resolver. The observer will be notified of the | 121 // Adds an observer to this resolver. The observer will be notified of the |
| 158 // start and completion of all requests (excluding cancellation). |observer| | 122 // start and completion of all requests (excluding cancellation). |observer| |
| 159 // must remain valid for the duration of this HostResolver's lifetime. | 123 // must remain valid for the duration of this HostResolver's lifetime. |
| 160 void AddObserver(Observer* observer); | 124 virtual void AddObserver(Observer* observer) = 0; |
| 161 | 125 |
| 162 // Unregisters an observer previously added by AddObserver(). | 126 // Unregisters an observer previously added by AddObserver(). |
| 163 void RemoveObserver(Observer* observer); | 127 virtual void RemoveObserver(Observer* observer) = 0; |
| 164 | 128 |
| 165 // TODO(eroman): temp hack for http://crbug.com/15513 | 129 // TODO(eroman): temp hack for http://crbug.com/15513 |
| 166 void Shutdown(); | 130 virtual void Shutdown() = 0; |
| 167 | |
| 168 private: | |
| 169 class Job; | |
| 170 typedef std::vector<Request*> RequestsList; | |
| 171 typedef base::hash_map<std::string, scoped_refptr<Job> > JobMap; | |
| 172 typedef std::vector<Observer*> ObserversList; | |
| 173 | |
| 174 // Adds a job to outstanding jobs list. | |
| 175 void AddOutstandingJob(Job* job); | |
| 176 | |
| 177 // Returns the outstanding job for |hostname|, or NULL if there is none. | |
| 178 Job* FindOutstandingJob(const std::string& hostname); | |
| 179 | |
| 180 // Removes |job| from the outstanding jobs list. | |
| 181 void RemoveOutstandingJob(Job* job); | |
| 182 | |
| 183 // Callback for when |job| has completed with |error| and |addrlist|. | |
| 184 void OnJobComplete(Job* job, int error, const AddressList& addrlist); | |
| 185 | |
| 186 // Notify all observers of the start of a resolve request. | |
| 187 void NotifyObserversStartRequest(int request_id, | |
| 188 const RequestInfo& info); | |
| 189 | |
| 190 // Notify all observers of the completion of a resolve request. | |
| 191 void NotifyObserversFinishRequest(int request_id, | |
| 192 const RequestInfo& info, | |
| 193 int error); | |
| 194 | |
| 195 // Notify all observers of the cancellation of a resolve request. | |
| 196 void NotifyObserversCancelRequest(int request_id, | |
| 197 const RequestInfo& info); | |
| 198 | |
| 199 // Cache of host resolution results. | |
| 200 HostCache cache_; | |
| 201 | |
| 202 // Map from hostname to outstanding job. | |
| 203 JobMap jobs_; | |
| 204 | |
| 205 // The job that OnJobComplete() is currently processing (needed in case | |
| 206 // HostResolver gets deleted from within the callback). | |
| 207 scoped_refptr<Job> cur_completing_job_; | |
| 208 | |
| 209 // The observers to notify when a request starts/ends. | |
| 210 ObserversList observers_; | |
| 211 | |
| 212 // Monotonically increasing ID number to assign to the next request. | |
| 213 // Observers are the only consumers of this ID number. | |
| 214 int next_request_id_; | |
| 215 | |
| 216 // TODO(eroman): temp hack for http://crbug.com/15513 | |
| 217 bool shutdown_; | |
| 218 | 131 |
| 219 DISALLOW_COPY_AND_ASSIGN(HostResolver); | 132 DISALLOW_COPY_AND_ASSIGN(HostResolver); |
| 220 }; | 133 }; |
| 221 | 134 |
| 222 // This class represents the task of resolving a hostname (or IP address | 135 // This class represents the task of resolving a hostname (or IP address |
| 223 // literal) to an AddressList object. It wraps HostResolver to resolve only a | 136 // literal) to an AddressList object. It wraps HostResolver to resolve only a |
| 224 // single hostname at a time and cancels this request when going out of scope. | 137 // single hostname at a time and cancels this request when going out of scope. |
| 225 class SingleRequestHostResolver { | 138 class SingleRequestHostResolver { |
| 226 public: | 139 public: |
| 227 explicit SingleRequestHostResolver(HostResolver* resolver); | 140 explicit SingleRequestHostResolver(HostResolver* resolver); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 238 | 151 |
| 239 private: | 152 private: |
| 240 // Callback for when the request to |resolver_| completes, so we dispatch | 153 // Callback for when the request to |resolver_| completes, so we dispatch |
| 241 // to the user's callback. | 154 // to the user's callback. |
| 242 void OnResolveCompletion(int result); | 155 void OnResolveCompletion(int result); |
| 243 | 156 |
| 244 // The actual host resolver that will handle the request. | 157 // The actual host resolver that will handle the request. |
| 245 scoped_refptr<HostResolver> resolver_; | 158 scoped_refptr<HostResolver> resolver_; |
| 246 | 159 |
| 247 // The current request (if any). | 160 // The current request (if any). |
| 248 HostResolver::Request* cur_request_; | 161 HostResolver::RequestHandle cur_request_; |
| 249 CompletionCallback* cur_request_callback_; | 162 CompletionCallback* cur_request_callback_; |
| 250 | 163 |
| 251 // Completion callback for when request to |resolver_| completes. | 164 // Completion callback for when request to |resolver_| completes. |
| 252 net::CompletionCallbackImpl<SingleRequestHostResolver> callback_; | 165 net::CompletionCallbackImpl<SingleRequestHostResolver> callback_; |
| 253 | 166 |
| 254 DISALLOW_COPY_AND_ASSIGN(SingleRequestHostResolver); | 167 DISALLOW_COPY_AND_ASSIGN(SingleRequestHostResolver); |
| 255 }; | 168 }; |
| 256 | 169 |
| 257 // A helper class used in unit tests to alter hostname mappings. See | 170 // Creates a HostResolver implementation that queries the underlying system. |
| 258 // SetHostMapper for details. | 171 // (Except if a unit-test has changed the global HostResolverProc using |
| 259 class HostMapper : public base::RefCountedThreadSafe<HostMapper> { | 172 // ScopedHostResolverProc to intercept requests to the system). |
| 260 public: | 173 HostResolver* CreateSystemHostResolver(); |
| 261 virtual ~HostMapper() {} | |
| 262 | |
| 263 // Returns possibly altered hostname, or empty string to simulate | |
| 264 // a failed lookup. | |
| 265 virtual std::string Map(const std::string& host) = 0; | |
| 266 | |
| 267 protected: | |
| 268 // Ask previous host mapper (if set) for mapping of given host. | |
| 269 std::string MapUsingPrevious(const std::string& host); | |
| 270 | |
| 271 private: | |
| 272 friend class ScopedHostMapper; | |
| 273 | |
| 274 // Set mapper to ask when this mapper doesn't want to modify the result. | |
| 275 void set_previous_mapper(HostMapper* mapper) { | |
| 276 previous_mapper_ = mapper; | |
| 277 } | |
| 278 | |
| 279 scoped_refptr<HostMapper> previous_mapper_; | |
| 280 }; | |
| 281 | |
| 282 #ifdef UNIT_TEST | |
| 283 // This function is designed to allow unit tests to override the behavior of | |
| 284 // HostResolver. For example, a HostMapper instance can force all hostnames | |
| 285 // to map to a fixed IP address such as 127.0.0.1. | |
| 286 // | |
| 287 // The previously set HostMapper (or NULL if there was none) is returned. | |
| 288 // | |
| 289 // NOTE: This function is not thread-safe, so take care to only call this | |
| 290 // function while there are no outstanding HostResolver instances. | |
| 291 // | |
| 292 // NOTE: In most cases, you should use ScopedHostMapper instead, which is | |
| 293 // defined in host_resolver_unittest.h | |
| 294 // | |
| 295 HostMapper* SetHostMapper(HostMapper* host_mapper); | |
| 296 #endif | |
| 297 | 174 |
| 298 } // namespace net | 175 } // namespace net |
| 299 | 176 |
| 300 #endif // NET_BASE_HOST_RESOLVER_H_ | 177 #endif // NET_BASE_HOST_RESOLVER_H_ |
| OLD | NEW |