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 #include "chrome/browser/net/dns_global.h" | 5 #include "chrome/browser/net/dns_global.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/singleton.h" | 10 #include "base/singleton.h" |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 // When we navigate, we may know in advance some other domains that will need to | 126 // When we navigate, we may know in advance some other domains that will need to |
127 // be resolved. This function initiates those side effects. | 127 // be resolved. This function initiates those side effects. |
128 static void NavigatingTo(const std::string& host_name) { | 128 static void NavigatingTo(const std::string& host_name) { |
129 if (!dns_prefetch_enabled || NULL == dns_master) | 129 if (!dns_prefetch_enabled || NULL == dns_master) |
130 return; | 130 return; |
131 dns_master->NavigatingTo(host_name); | 131 dns_master->NavigatingTo(host_name); |
132 } | 132 } |
133 | 133 |
134 // The observer class needs to connect starts and finishes of HTTP network | 134 // The observer class needs to connect starts and finishes of HTTP network |
135 // resolutions. We use the following type for that map. | 135 // resolutions. We use the following type for that map. |
136 typedef std::map<void*, DnsHostInfo> ObservedResolutionMap; | 136 typedef std::map<int, DnsHostInfo> ObservedResolutionMap; |
137 | 137 |
138 // There will only be one instance ever created of the following Observer | 138 // There will only be one instance ever created of the following Observer |
139 // class. As a result, we get away with using static members for data local | 139 // class. As a result, we get away with using static members for data local |
140 // to that instance (to better comply with a google style guide exemption). | 140 // to that instance (to better comply with a google style guide exemption). |
141 class PrefetchObserver : public net::DnsResolutionObserver { | 141 class PrefetchObserver : public net::DnsResolutionObserver { |
142 public: | 142 public: |
143 PrefetchObserver(); | 143 PrefetchObserver(); |
144 ~PrefetchObserver(); | 144 ~PrefetchObserver(); |
145 | 145 |
146 virtual void OnStartResolution(const std::string& host_name, | 146 virtual void OnStartResolution( |
147 void* context); | 147 int request_id, |
148 virtual void OnFinishResolutionWithStatus(bool was_resolved, | 148 const net::HostResolver::RequestInfo& request_info); |
149 const GURL& referrer, | 149 virtual void OnFinishResolutionWithStatus( |
150 void* context); | 150 int request_id, |
| 151 bool was_resolved, |
| 152 const net::HostResolver::RequestInfo& request_info); |
151 | 153 |
152 static void DnsGetFirstResolutionsHtml(std::string* output); | 154 static void DnsGetFirstResolutionsHtml(std::string* output); |
153 static void SaveStartupListAsPref(PrefService* local_state); | 155 static void SaveStartupListAsPref(PrefService* local_state); |
154 | 156 |
155 private: | 157 private: |
156 static void StartupListAppend(const DnsHostInfo& navigation_info); | 158 static void StartupListAppend(const DnsHostInfo& navigation_info); |
157 | 159 |
158 // We avoid using member variables to better comply with the style guide. | 160 // We avoid using member variables to better comply with the style guide. |
159 // We had permission to instantiate only a very minimal class as a global | 161 // We had permission to instantiate only a very minimal class as a global |
160 // data item, so we avoid putting members in that class. | 162 // data item, so we avoid putting members in that class. |
(...skipping 21 matching lines...) Expand all Loading... |
182 PrefetchObserver::~PrefetchObserver() { | 184 PrefetchObserver::~PrefetchObserver() { |
183 DCHECK(lock && resolutions && first_resolutions); | 185 DCHECK(lock && resolutions && first_resolutions); |
184 delete first_resolutions; | 186 delete first_resolutions; |
185 first_resolutions = NULL; | 187 first_resolutions = NULL; |
186 delete resolutions; | 188 delete resolutions; |
187 resolutions = NULL; | 189 resolutions = NULL; |
188 delete lock; | 190 delete lock; |
189 lock = NULL; | 191 lock = NULL; |
190 } | 192 } |
191 | 193 |
192 void PrefetchObserver::OnStartResolution(const std::string& host_name, | 194 void PrefetchObserver::OnStartResolution( |
193 void* context) { | 195 int request_id, |
194 DCHECK_NE(0U, host_name.length()); | 196 const net::HostResolver::RequestInfo& request_info) { |
| 197 if (request_info.is_speculative()) |
| 198 return; // One of our own requests. |
| 199 DCHECK_NE(0U, request_info.hostname().length()); |
195 DnsHostInfo navigation_info; | 200 DnsHostInfo navigation_info; |
196 navigation_info.SetHostname(host_name); | 201 navigation_info.SetHostname(request_info.hostname()); |
197 navigation_info.SetStartedState(); | 202 navigation_info.SetStartedState(); |
198 | 203 |
199 NavigatingTo(host_name); | 204 NavigatingTo(request_info.hostname()); |
| 205 |
| 206 // TODO(eroman): If the resolve request is cancelled, then |
| 207 // OnFinishResolutionWithStatus will not be called, and |resolutions| will |
| 208 // grow unbounded! |
200 | 209 |
201 AutoLock auto_lock(*lock); | 210 AutoLock auto_lock(*lock); |
202 (*resolutions)[context] = navigation_info; | 211 (*resolutions)[request_id] = navigation_info; |
203 } | 212 } |
204 | 213 |
205 void PrefetchObserver::OnFinishResolutionWithStatus(bool was_resolved, | 214 void PrefetchObserver::OnFinishResolutionWithStatus( |
206 const GURL& referrer, | 215 int request_id, |
207 void* context) { | 216 bool was_resolved, |
| 217 const net::HostResolver::RequestInfo& request_info) { |
| 218 if (request_info.is_speculative()) |
| 219 return; // One of our own requests. |
208 DnsHostInfo navigation_info; | 220 DnsHostInfo navigation_info; |
209 size_t startup_count; | 221 size_t startup_count; |
210 { | 222 { |
211 AutoLock auto_lock(*lock); | 223 AutoLock auto_lock(*lock); |
212 ObservedResolutionMap::iterator it = resolutions->find(context); | 224 ObservedResolutionMap::iterator it = resolutions->find(request_id); |
213 if (resolutions->end() == it) { | 225 if (resolutions->end() == it) { |
214 DCHECK(false); | 226 DCHECK(false); |
215 return; | 227 return; |
216 } | 228 } |
217 navigation_info = it->second; | 229 navigation_info = it->second; |
218 resolutions->erase(it); | 230 resolutions->erase(it); |
219 startup_count = first_resolutions->size(); | 231 startup_count = first_resolutions->size(); |
220 } | 232 } |
221 navigation_info.SetFinishedState(was_resolved); // Get timing info | 233 navigation_info.SetFinishedState(was_resolved); // Get timing info |
222 AccruePrefetchBenefits(referrer, &navigation_info); | 234 AccruePrefetchBenefits(request_info.referrer(), &navigation_info); |
223 if (kStartupResolutionCount <= startup_count || !was_resolved) | 235 if (kStartupResolutionCount <= startup_count || !was_resolved) |
224 return; | 236 return; |
225 // TODO(jar): Don't add host to our list if it is a non-linked lookup, and | 237 // TODO(jar): Don't add host to our list if it is a non-linked lookup, and |
226 // instead rely on Referrers to pull this in automatically with the enclosing | 238 // instead rely on Referrers to pull this in automatically with the enclosing |
227 // page load (once we start to persist elements of our referrer tree). | 239 // page load (once we start to persist elements of our referrer tree). |
228 StartupListAppend(navigation_info); | 240 StartupListAppend(navigation_info); |
229 } | 241 } |
230 | 242 |
231 // static | 243 // static |
232 void PrefetchObserver::StartupListAppend(const DnsHostInfo& navigation_info) { | 244 void PrefetchObserver::StartupListAppend(const DnsHostInfo& navigation_info) { |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 Singleton<OffTheRecordObserver>::get()->Register(); | 404 Singleton<OffTheRecordObserver>::get()->Register(); |
393 | 405 |
394 if (user_prefs) { | 406 if (user_prefs) { |
395 bool enabled = user_prefs->GetBoolean(prefs::kDnsPrefetchingEnabled); | 407 bool enabled = user_prefs->GetBoolean(prefs::kDnsPrefetchingEnabled); |
396 EnableDnsPrefetch(enabled); | 408 EnableDnsPrefetch(enabled); |
397 } | 409 } |
398 | 410 |
399 DLOG(INFO) << "DNS Prefetch service started"; | 411 DLOG(INFO) << "DNS Prefetch service started"; |
400 | 412 |
401 // Start observing real HTTP stack resolutions. | 413 // Start observing real HTTP stack resolutions. |
402 net::AddDnsResolutionObserver(&dns_resolution_observer); | 414 // TODO(eroman): really this should be called from IO thread (since that is |
| 415 // where the host resolver lives). Since this occurs before requests have |
| 416 // started it is not a race yet. |
| 417 GetGlobalHostResolver()->AddObserver(&dns_resolution_observer); |
403 } | 418 } |
404 } | 419 } |
405 | 420 |
406 void EnsureDnsPrefetchShutdown() { | 421 void EnsureDnsPrefetchShutdown() { |
407 if (NULL != dns_master) | 422 if (NULL != dns_master) |
408 dns_master->Shutdown(); | 423 dns_master->Shutdown(); |
409 FreeGlobalHostResolver(); | 424 FreeGlobalHostResolver(); |
410 } | 425 } |
411 | 426 |
412 void FreeDnsPrefetchResources() { | 427 void FreeDnsPrefetchResources() { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 dns_master->DeserializeReferrers(*referral_list); | 530 dns_master->DeserializeReferrers(*referral_list); |
516 } | 531 } |
517 | 532 |
518 void TrimSubresourceReferrers() { | 533 void TrimSubresourceReferrers() { |
519 if (NULL == dns_master) | 534 if (NULL == dns_master) |
520 return; | 535 return; |
521 dns_master->TrimReferrers(); | 536 dns_master->TrimReferrers(); |
522 } | 537 } |
523 | 538 |
524 } // namespace chrome_browser_net | 539 } // namespace chrome_browser_net |
OLD | NEW |