Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(473)

Side by Side Diff: net/base/host_resolver.h

Issue 125107: * Move the global "DnsResolutionObserver" code depended on by DNS prefetcher,... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Address jar's comments Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/dns_resolution_observer.cc ('k') | net/base/host_resolver.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "net/base/completion_callback.h" 15 #include "net/base/completion_callback.h"
15 #include "net/base/host_cache.h" 16 #include "net/base/host_cache.h"
16 17
17 class MessageLoop; 18 class MessageLoop;
18 19
19 namespace net { 20 namespace net {
20 21
21 class AddressList; 22 class AddressList;
23 class DnsResolutionObserver;
22 class HostMapper; 24 class HostMapper;
23 25
24 // This class represents the task of resolving hostnames (or IP address 26 // This class represents the task of resolving hostnames (or IP address
25 // literal) to an AddressList object. 27 // literal) to an AddressList object.
26 // 28 //
27 // HostResolver handles multiple requests at a time, so when cancelling a 29 // HostResolver handles multiple requests at a time, so when cancelling a
28 // request the Request* handle that was returned by Resolve() needs to be 30 // request the Request* handle that was returned by Resolve() needs to be
29 // given. A simpler alternative for consumers that only have 1 outstanding 31 // given. A simpler alternative for consumers that only have 1 outstanding
30 // request at a time is to create a SingleRequestHostResolver wrapper around 32 // request at a time is to create a SingleRequestHostResolver wrapper around
31 // HostResolver (which will automatically cancel the single request when it 33 // HostResolver (which will automatically cancel the single request when it
(...skipping 19 matching lines...) Expand all
51 // 53 //
52 // 54 //
53 // When a HostResolver::Job finishes its work in the threadpool, the callbacks 55 // When a HostResolver::Job finishes its work in the threadpool, the callbacks
54 // of each waiting request are run on the origin thread. 56 // of each waiting request are run on the origin thread.
55 // 57 //
56 // Thread safety: This class is not threadsafe, and must only be called 58 // Thread safety: This class is not threadsafe, and must only be called
57 // from one thread! 59 // from one thread!
58 // 60 //
59 class HostResolver { 61 class HostResolver {
60 public: 62 public:
63 // The parameters for doing a Resolve(). |hostname| and |port| are required,
64 // the rest are optional (and have reasonable defaults).
65 class RequestInfo {
66 public:
67 RequestInfo(const std::string& hostname, int port)
68 : hostname_(hostname),
69 port_(port),
70 allow_cached_response_(true),
71 is_speculative_(false) {}
72
73 const int port() const { return port_; }
74 const std::string& hostname() const { return hostname_; }
75
76 bool allow_cached_response() const { return allow_cached_response_; }
77 void set_allow_cached_response(bool b) { allow_cached_response_ = b; }
78
79 bool is_speculative() const { return is_speculative_; }
80 void set_is_speculative(bool b) { is_speculative_ = b; }
81
82 const GURL& referrer() const { return referrer_; }
83 void set_referrer(const GURL& referrer) { referrer_ = referrer; }
84
85 private:
86 // The hostname to resolve.
87 std::string hostname_;
88
89 // The port number to set in the result's sockaddrs.
90 int port_;
91
92 // Whether it is ok to return a result from the host cache.
93 bool allow_cached_response_;
94
95 // Whether this request was started by the DNS prefetcher.
96 bool is_speculative_;
97
98 // 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.
100 GURL referrer_;
101 };
102
61 // Creates a HostResolver that caches up to |max_cache_entries| for 103 // Creates a HostResolver that caches up to |max_cache_entries| for
62 // |cache_duration_ms| milliseconds. 104 // |cache_duration_ms| milliseconds.
63 // 105 //
64 // TODO(eroman): Get rid of the default parameters as it violate google 106 // TODO(eroman): Get rid of the default parameters as it violate google
65 // style. This is temporary to help with refactoring. 107 // style. This is temporary to help with refactoring.
66 HostResolver(int max_cache_entries = 100, int cache_duration_ms = 60000); 108 HostResolver(int max_cache_entries = 100, int cache_duration_ms = 60000);
67 109
68 // If any completion callbacks are pending when the resolver is destroyed, 110 // If any completion callbacks are pending when the resolver is destroyed,
69 // the host resolutions are cancelled, and the completion callbacks will not 111 // the host resolutions are cancelled, and the completion callbacks will not
70 // be called. 112 // be called.
71 ~HostResolver(); 113 ~HostResolver();
72 114
73 // Opaque type used to cancel a request. 115 // Opaque type used to cancel a request.
74 class Request; 116 class Request;
75 117
76 // Resolves the given hostname (or IP address literal), filling out the 118 // Resolves the given hostname (or IP address literal), filling out the
77 // |addresses| object upon success. The |port| parameter will be set as the 119 // |addresses| object upon success. The |info.port| parameter will be set as
78 // sin(6)_port field of the sockaddr_in{6} struct. Returns OK if successful 120 // the sin(6)_port field of the sockaddr_in{6} struct. Returns OK if
79 // or an error code upon failure. 121 // successful or an error code upon failure.
80 // 122 //
81 // When callback is null, the operation completes synchronously. 123 // When callback is null, the operation completes synchronously.
82 // 124 //
83 // When callback is non-null, the operation will be performed asynchronously. 125 // When callback is non-null, the operation will be performed asynchronously.
84 // ERR_IO_PENDING is returned if it has been scheduled successfully. Real 126 // ERR_IO_PENDING is returned if it has been scheduled successfully. Real
85 // result code will be passed to the completion callback. If |req| is 127 // result code will be passed to the completion callback. If |req| is
86 // non-NULL, then |*req| will be filled with a handle to the async request. 128 // non-NULL, then |*req| will be filled with a handle to the async request.
87 // This handle is not valid after the request has completed. 129 // This handle is not valid after the request has completed.
88 int Resolve(const std::string& hostname, int port, 130 int Resolve(const RequestInfo& info, AddressList* addresses,
89 AddressList* addresses, CompletionCallback* callback, 131 CompletionCallback* callback, Request** req);
90 Request** req);
91 132
92 // Cancels the specified request. |req| is the handle returned by Resolve(). 133 // Cancels the specified request. |req| is the handle returned by Resolve().
93 // After a request is cancelled, its completion callback will not be called. 134 // After a request is cancelled, its completion callback will not be called.
94 void CancelRequest(Request* req); 135 void CancelRequest(Request* req);
95 136
137 // Adds an observer to this resolver. The observer will be notified of the
138 // start and completion of all requests (excluding cancellation). |observer|
139 // must remain valid for the duration of this HostResolver's lifetime.
140 void AddObserver(DnsResolutionObserver* observer);
141
142 // Unregisters an observer previously added by AddObserver().
143 void RemoveObserver(DnsResolutionObserver* observer);
144
96 private: 145 private:
97 class Job; 146 class Job;
98 typedef std::vector<Request*> RequestsList; 147 typedef std::vector<Request*> RequestsList;
99 typedef base::hash_map<std::string, scoped_refptr<Job> > JobMap; 148 typedef base::hash_map<std::string, scoped_refptr<Job> > JobMap;
149 typedef std::vector<DnsResolutionObserver*> ObserversList;
100 150
101 // Adds a job to outstanding jobs list. 151 // Adds a job to outstanding jobs list.
102 void AddOutstandingJob(Job* job); 152 void AddOutstandingJob(Job* job);
103 153
104 // Returns the outstanding job for |hostname|, or NULL if there is none. 154 // Returns the outstanding job for |hostname|, or NULL if there is none.
105 Job* FindOutstandingJob(const std::string& hostname); 155 Job* FindOutstandingJob(const std::string& hostname);
106 156
107 // Removes |job| from the outstanding jobs list. 157 // Removes |job| from the outstanding jobs list.
108 void RemoveOutstandingJob(Job* job); 158 void RemoveOutstandingJob(Job* job);
109 159
110 // Callback for when |job| has completed with |error| and |addrlist|. 160 // Callback for when |job| has completed with |error| and |addrlist|.
111 void OnJobComplete(Job* job, int error, const AddressList& addrlist); 161 void OnJobComplete(Job* job, int error, const AddressList& addrlist);
112 162
163 // Notify all obsevers of the start of a resolve request.
164 void NotifyObserversStartRequest(int request_id,
165 const RequestInfo& info);
166
167 // Notify all obsevers of the completion of a resolve request.
168 void NotifyObserversFinishRequest(int request_id,
169 const RequestInfo& info,
170 int error);
171
113 // Cache of host resolution results. 172 // Cache of host resolution results.
114 HostCache cache_; 173 HostCache cache_;
115 174
116 // Map from hostname to outstanding job. 175 // Map from hostname to outstanding job.
117 JobMap jobs_; 176 JobMap jobs_;
118 177
119 // The job that OnJobComplete() is currently processing (needed in case 178 // The job that OnJobComplete() is currently processing (needed in case
120 // HostResolver gets deleted from within the callback). 179 // HostResolver gets deleted from within the callback).
121 scoped_refptr<Job> cur_completing_job_; 180 scoped_refptr<Job> cur_completing_job_;
122 181
182 // The observers to notify when a request starts/ends.
183 ObserversList observers_;
184
185 // Monotonically increasing ID number to assign to the next request.
186 // Observers are the only consumers of this ID number.
187 int next_request_id_;
188
123 DISALLOW_COPY_AND_ASSIGN(HostResolver); 189 DISALLOW_COPY_AND_ASSIGN(HostResolver);
124 }; 190 };
125 191
126 // This class represents the task of resolving a hostname (or IP address 192 // This class represents the task of resolving a hostname (or IP address
127 // literal) to an AddressList object. It wraps HostResolver to resolve only a 193 // literal) to an AddressList object. It wraps HostResolver to resolve only a
128 // single hostname at a time and cancels this request when going out of scope. 194 // single hostname at a time and cancels this request when going out of scope.
129 class SingleRequestHostResolver { 195 class SingleRequestHostResolver {
130 public: 196 public:
131 explicit SingleRequestHostResolver(HostResolver* resolver); 197 explicit SingleRequestHostResolver(HostResolver* resolver);
132 198
133 // If a completion callback is pending when the resolver is destroyed, the 199 // If a completion callback is pending when the resolver is destroyed, the
134 // host resolution is cancelled, and the completion callback will not be 200 // host resolution is cancelled, and the completion callback will not be
135 // called. 201 // called.
136 ~SingleRequestHostResolver(); 202 ~SingleRequestHostResolver();
137 203
138 // Resolves the given hostname (or IP address literal), filling out the 204 // Resolves the given hostname (or IP address literal), filling out the
139 // |addresses| object upon success. See HostResolver::Resolve() for details. 205 // |addresses| object upon success. See HostResolver::Resolve() for details.
140 int Resolve(const std::string& hostname, int port, 206 int Resolve(const HostResolver::RequestInfo& info,
141 AddressList* addresses, CompletionCallback* callback); 207 AddressList* addresses, CompletionCallback* callback);
142 208
143 private: 209 private:
144 // Callback for when the request to |resolver_| completes, so we dispatch 210 // Callback for when the request to |resolver_| completes, so we dispatch
145 // to the user's callback. 211 // to the user's callback.
146 void OnResolveCompletion(int result); 212 void OnResolveCompletion(int result);
147 213
148 // The actual host resolver that will handle the request. 214 // The actual host resolver that will handle the request.
149 HostResolver* resolver_; 215 HostResolver* resolver_;
150 216
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 // 261 //
196 // NOTE: In most cases, you should use ScopedHostMapper instead, which is 262 // NOTE: In most cases, you should use ScopedHostMapper instead, which is
197 // defined in host_resolver_unittest.h 263 // defined in host_resolver_unittest.h
198 // 264 //
199 HostMapper* SetHostMapper(HostMapper* host_mapper); 265 HostMapper* SetHostMapper(HostMapper* host_mapper);
200 #endif 266 #endif
201 267
202 } // namespace net 268 } // namespace net
203 269
204 #endif // NET_BASE_HOST_RESOLVER_H_ 270 #endif // NET_BASE_HOST_RESOLVER_H_
OLDNEW
« no previous file with comments | « net/base/dns_resolution_observer.cc ('k') | net/base/host_resolver.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698