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> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/lock.h" | 12 #include "base/lock.h" |
13 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
15 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
16 #include "net/base/host_cache.h" | 16 #include "net/base/host_cache.h" |
17 | 17 |
18 class MessageLoop; | 18 class MessageLoop; |
19 | 19 |
20 namespace net { | 20 namespace net { |
21 | 21 |
22 class AddressList; | 22 class AddressList; |
23 class DnsResolutionObserver; | |
24 class HostMapper; | 23 class HostMapper; |
25 | 24 |
26 // This class represents the task of resolving hostnames (or IP address | 25 // This class represents the task of resolving hostnames (or IP address |
27 // literal) to an AddressList object. | 26 // literal) to an AddressList object. |
28 // | 27 // |
29 // HostResolver handles multiple requests at a time, so when cancelling a | 28 // HostResolver handles multiple requests at a time, so when cancelling a |
30 // request the Request* handle that was returned by Resolve() needs to be | 29 // request the Request* handle that was returned by Resolve() needs to be |
31 // given. A simpler alternative for consumers that only have 1 outstanding | 30 // given. A simpler alternative for consumers that only have 1 outstanding |
32 // request at a time is to create a SingleRequestHostResolver wrapper around | 31 // request at a time is to create a SingleRequestHostResolver wrapper around |
33 // HostResolver (which will automatically cancel the single request when it | 32 // HostResolver (which will automatically cancel the single request when it |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 bool allow_cached_response_; | 92 bool allow_cached_response_; |
94 | 93 |
95 // Whether this request was started by the DNS prefetcher. | 94 // Whether this request was started by the DNS prefetcher. |
96 bool is_speculative_; | 95 bool is_speculative_; |
97 | 96 |
98 // Optional data for consumption by observers. This is the URL of the | 97 // Optional data for consumption by observers. This is the URL of the |
99 // page that lead us to the navigation, for DNS prefetcher's benefit. | 98 // page that lead us to the navigation, for DNS prefetcher's benefit. |
100 GURL referrer_; | 99 GURL referrer_; |
101 }; | 100 }; |
102 | 101 |
| 102 // Interface for observing the requests that flow through a HostResolver. |
| 103 class Observer { |
| 104 public: |
| 105 virtual ~Observer() {} |
| 106 |
| 107 // Called at the start of HostResolver::Resolve(). |id| is a unique number |
| 108 // given to the request, so it can be matched up with a corresponding call |
| 109 // to OnFinishResolutionWithStatus(). |
| 110 virtual void OnStartResolution(int id, const RequestInfo& info) = 0; |
| 111 |
| 112 // Called on completion of request |id|. Note that if the request was |
| 113 // cancelled, OnFinishResolutionWithStatus() will not be called. |
| 114 virtual void OnFinishResolutionWithStatus(int id, bool was_resolved, |
| 115 const RequestInfo& info) = 0; |
| 116 }; |
| 117 |
103 // Creates a HostResolver that caches up to |max_cache_entries| for | 118 // Creates a HostResolver that caches up to |max_cache_entries| for |
104 // |cache_duration_ms| milliseconds. | 119 // |cache_duration_ms| milliseconds. |
105 // | 120 // |
106 // TODO(eroman): Get rid of the default parameters as it violate google | 121 // TODO(eroman): Get rid of the default parameters as it violate google |
107 // style. This is temporary to help with refactoring. | 122 // style. This is temporary to help with refactoring. |
108 HostResolver(int max_cache_entries = 100, int cache_duration_ms = 60000); | 123 HostResolver(int max_cache_entries = 100, int cache_duration_ms = 60000); |
109 | 124 |
110 // If any completion callbacks are pending when the resolver is destroyed, | 125 // If any completion callbacks are pending when the resolver is destroyed, |
111 // the host resolutions are cancelled, and the completion callbacks will not | 126 // the host resolutions are cancelled, and the completion callbacks will not |
112 // be called. | 127 // be called. |
(...skipping 17 matching lines...) Expand all Loading... |
130 int Resolve(const RequestInfo& info, AddressList* addresses, | 145 int Resolve(const RequestInfo& info, AddressList* addresses, |
131 CompletionCallback* callback, Request** req); | 146 CompletionCallback* callback, Request** req); |
132 | 147 |
133 // Cancels the specified request. |req| is the handle returned by Resolve(). | 148 // Cancels the specified request. |req| is the handle returned by Resolve(). |
134 // After a request is cancelled, its completion callback will not be called. | 149 // After a request is cancelled, its completion callback will not be called. |
135 void CancelRequest(Request* req); | 150 void CancelRequest(Request* req); |
136 | 151 |
137 // Adds an observer to this resolver. The observer will be notified of the | 152 // Adds an observer to this resolver. The observer will be notified of the |
138 // start and completion of all requests (excluding cancellation). |observer| | 153 // start and completion of all requests (excluding cancellation). |observer| |
139 // must remain valid for the duration of this HostResolver's lifetime. | 154 // must remain valid for the duration of this HostResolver's lifetime. |
140 void AddObserver(DnsResolutionObserver* observer); | 155 void AddObserver(Observer* observer); |
141 | 156 |
142 // Unregisters an observer previously added by AddObserver(). | 157 // Unregisters an observer previously added by AddObserver(). |
143 void RemoveObserver(DnsResolutionObserver* observer); | 158 void RemoveObserver(Observer* observer); |
144 | 159 |
145 private: | 160 private: |
146 class Job; | 161 class Job; |
147 typedef std::vector<Request*> RequestsList; | 162 typedef std::vector<Request*> RequestsList; |
148 typedef base::hash_map<std::string, scoped_refptr<Job> > JobMap; | 163 typedef base::hash_map<std::string, scoped_refptr<Job> > JobMap; |
149 typedef std::vector<DnsResolutionObserver*> ObserversList; | 164 typedef std::vector<Observer*> ObserversList; |
150 | 165 |
151 // Adds a job to outstanding jobs list. | 166 // Adds a job to outstanding jobs list. |
152 void AddOutstandingJob(Job* job); | 167 void AddOutstandingJob(Job* job); |
153 | 168 |
154 // Returns the outstanding job for |hostname|, or NULL if there is none. | 169 // Returns the outstanding job for |hostname|, or NULL if there is none. |
155 Job* FindOutstandingJob(const std::string& hostname); | 170 Job* FindOutstandingJob(const std::string& hostname); |
156 | 171 |
157 // Removes |job| from the outstanding jobs list. | 172 // Removes |job| from the outstanding jobs list. |
158 void RemoveOutstandingJob(Job* job); | 173 void RemoveOutstandingJob(Job* job); |
159 | 174 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 // | 276 // |
262 // NOTE: In most cases, you should use ScopedHostMapper instead, which is | 277 // NOTE: In most cases, you should use ScopedHostMapper instead, which is |
263 // defined in host_resolver_unittest.h | 278 // defined in host_resolver_unittest.h |
264 // | 279 // |
265 HostMapper* SetHostMapper(HostMapper* host_mapper); | 280 HostMapper* SetHostMapper(HostMapper* host_mapper); |
266 #endif | 281 #endif |
267 | 282 |
268 } // namespace net | 283 } // namespace net |
269 | 284 |
270 #endif // NET_BASE_HOST_RESOLVER_H_ | 285 #endif // NET_BASE_HOST_RESOLVER_H_ |
OLD | NEW |