OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // A Predictor object is instantiated once in the browser process, and manages | 5 // A Predictor object is instantiated once in the browser process, and manages |
6 // both preresolution of hostnames, as well as TCP/IP preconnection to expected | 6 // both preresolution of hostnames, as well as TCP/IP preconnection to expected |
7 // subresources. | 7 // subresources. |
8 // Most hostname lists are provided by the renderer processes, and include URLs | 8 // Most hostname lists are provided by the renderer processes, and include URLs |
9 // that *might* be used in the near future by the browsing user. One goal of | 9 // that *might* be used in the near future by the browsing user. One goal of |
10 // this class is to cause the underlying DNS structure to lookup a hostname | 10 // this class is to cause the underlying DNS structure to lookup a hostname |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 #include <map> | 22 #include <map> |
23 #include <queue> | 23 #include <queue> |
24 #include <set> | 24 #include <set> |
25 #include <string> | 25 #include <string> |
26 #include <vector> | 26 #include <vector> |
27 | 27 |
28 #include "base/gtest_prod_util.h" | 28 #include "base/gtest_prod_util.h" |
29 #include "base/memory/scoped_ptr.h" | 29 #include "base/memory/scoped_ptr.h" |
30 #include "base/memory/weak_ptr.h" | 30 #include "base/memory/weak_ptr.h" |
| 31 #include "chrome/browser/net/prediction_options.h" |
31 #include "chrome/browser/net/referrer.h" | 32 #include "chrome/browser/net/referrer.h" |
32 #include "chrome/browser/net/spdyproxy/proxy_advisor.h" | 33 #include "chrome/browser/net/spdyproxy/proxy_advisor.h" |
33 #include "chrome/browser/net/timed_cache.h" | 34 #include "chrome/browser/net/timed_cache.h" |
34 #include "chrome/browser/net/url_info.h" | 35 #include "chrome/browser/net/url_info.h" |
35 #include "chrome/common/net/predictor_common.h" | 36 #include "chrome/common/net/predictor_common.h" |
36 #include "net/base/host_port_pair.h" | 37 #include "net/base/host_port_pair.h" |
37 | 38 |
38 class IOThread; | 39 class IOThread; |
39 class PrefService; | 40 class PrefService; |
40 class Profile; | 41 class Profile; |
| 42 class ProfileIOData; |
41 | 43 |
42 namespace base { | 44 namespace base { |
43 class ListValue; | 45 class ListValue; |
44 class WaitableEvent; | 46 class WaitableEvent; |
45 } | 47 } |
46 | 48 |
47 namespace net { | 49 namespace net { |
48 class HostResolver; | 50 class HostResolver; |
49 class SSLConfigService; | 51 class SSLConfigService; |
50 class TransportSecurityState; | 52 class TransportSecurityState; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 110 |
109 // We don't bother learning to preconnect via a GET if the original URL | 111 // We don't bother learning to preconnect via a GET if the original URL |
110 // navigation was so long ago, that a preconnection would have been dropped | 112 // navigation was so long ago, that a preconnection would have been dropped |
111 // anyway. We believe most servers will drop the connection in 10 seconds, so | 113 // anyway. We believe most servers will drop the connection in 10 seconds, so |
112 // we currently estimate this time-till-drop at 10 seconds. | 114 // we currently estimate this time-till-drop at 10 seconds. |
113 // TODO(jar): We should do a persistent field trial to validate/optimize this. | 115 // TODO(jar): We should do a persistent field trial to validate/optimize this. |
114 static const int kMaxUnusedSocketLifetimeSecondsWithoutAGet; | 116 static const int kMaxUnusedSocketLifetimeSecondsWithoutAGet; |
115 | 117 |
116 // |max_concurrent| specifies how many concurrent (parallel) prefetches will | 118 // |max_concurrent| specifies how many concurrent (parallel) prefetches will |
117 // be performed. Host lookups will be issued through |host_resolver|. | 119 // be performed. Host lookups will be issued through |host_resolver|. |
118 explicit Predictor(bool preconnect_enabled); | 120 explicit Predictor(bool preconnect_enabled, bool predictor_enabled); |
119 | 121 |
120 virtual ~Predictor(); | 122 virtual ~Predictor(); |
121 | 123 |
122 // This function is used to create a predictor. For testing, we can create | 124 // This function is used to create a predictor. For testing, we can create |
123 // a version which does a simpler shutdown. | 125 // a version which does a simpler shutdown. |
124 static Predictor* CreatePredictor(bool preconnect_enabled, | 126 static Predictor* CreatePredictor(bool preconnect_enabled, |
| 127 bool predictor_enabled, |
125 bool simple_shutdown); | 128 bool simple_shutdown); |
126 | 129 |
127 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 130 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
128 | 131 |
129 // ------------- Start UI thread methods. | 132 // ------------- Start UI thread methods. |
130 | 133 |
131 virtual void InitNetworkPredictor(PrefService* user_prefs, | 134 virtual void InitNetworkPredictor(PrefService* user_prefs, |
132 PrefService* local_state, | 135 PrefService* local_state, |
133 IOThread* io_thread, | 136 IOThread* io_thread, |
134 net::URLRequestContextGetter* getter); | 137 net::URLRequestContextGetter* getter, |
| 138 ProfileIOData* profile_io_data); |
135 | 139 |
136 // The Omnibox has proposed a given url to the user, and if it is a search | 140 // The Omnibox has proposed a given url to the user, and if it is a search |
137 // URL, then it also indicates that this is preconnectable (i.e., we could | 141 // URL, then it also indicates that this is preconnectable (i.e., we could |
138 // preconnect to the search server). | 142 // preconnect to the search server). |
139 void AnticipateOmniboxUrl(const GURL& url, bool preconnectable); | 143 void AnticipateOmniboxUrl(const GURL& url, bool preconnectable); |
140 | 144 |
141 // Preconnect a URL and all of its subresource domains. | 145 // Preconnect a URL and all of its subresource domains. |
142 void PreconnectUrlAndSubresources(const GURL& url, | 146 void PreconnectUrlAndSubresources(const GURL& url, |
143 const GURL& first_party_for_cookies); | 147 const GURL& first_party_for_cookies); |
144 | 148 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 void DeserializeReferrers(const base::ListValue& referral_list); | 209 void DeserializeReferrers(const base::ListValue& referral_list); |
206 | 210 |
207 void DeserializeReferrersThenDelete(base::ListValue* referral_list); | 211 void DeserializeReferrersThenDelete(base::ListValue* referral_list); |
208 | 212 |
209 void DiscardInitialNavigationHistory(); | 213 void DiscardInitialNavigationHistory(); |
210 | 214 |
211 void FinalizeInitializationOnIOThread( | 215 void FinalizeInitializationOnIOThread( |
212 const std::vector<GURL>& urls_to_prefetch, | 216 const std::vector<GURL>& urls_to_prefetch, |
213 base::ListValue* referral_list, | 217 base::ListValue* referral_list, |
214 IOThread* io_thread, | 218 IOThread* io_thread, |
215 bool predictor_enabled); | 219 ProfileIOData* profile_io_data); |
216 | 220 |
217 // During startup, we learn what the first N urls visited are, and then | 221 // During startup, we learn what the first N urls visited are, and then |
218 // resolve the associated hosts ASAP during our next startup. | 222 // resolve the associated hosts ASAP during our next startup. |
219 void LearnAboutInitialNavigation(const GURL& url); | 223 void LearnAboutInitialNavigation(const GURL& url); |
220 | 224 |
221 // Renderer bundles up list and sends to this browser API via IPC. | 225 // Renderer bundles up list and sends to this browser API via IPC. |
222 // TODO(jar): Use UrlList instead to include port and scheme. | 226 // TODO(jar): Use UrlList instead to include port and scheme. |
223 void DnsPrefetchList(const NameList& hostnames); | 227 void DnsPrefetchList(const NameList& hostnames); |
224 | 228 |
225 // May be called from either the IO or UI thread and will PostTask | 229 // May be called from either the IO or UI thread and will PostTask |
226 // to the IO thread if necessary. | 230 // to the IO thread if necessary. |
227 void DnsPrefetchMotivatedList(const UrlList& urls, | 231 void DnsPrefetchMotivatedList(const UrlList& urls, |
228 UrlInfo::ResolutionMotivation motivation); | 232 UrlInfo::ResolutionMotivation motivation); |
229 | 233 |
230 // May be called from either the IO or UI thread and will PostTask | 234 // May be called from either the IO or UI thread and will PostTask |
231 // to the IO thread if necessary. | 235 // to the IO thread if necessary. |
232 void SaveStateForNextStartupAndTrim(PrefService* prefs); | 236 void SaveStateForNextStartupAndTrim(); |
233 | 237 |
234 void SaveDnsPrefetchStateForNextStartupAndTrim( | 238 void SaveDnsPrefetchStateForNextStartupAndTrim( |
235 base::ListValue* startup_list, | 239 base::ListValue* startup_list, |
236 base::ListValue* referral_list, | 240 base::ListValue* referral_list, |
237 base::WaitableEvent* completion); | 241 base::WaitableEvent* completion); |
238 | 242 |
239 // May be called from either the IO or UI thread and will PostTask | 243 // May be called from either the IO or UI thread and will PostTask |
240 // to the IO thread if necessary. | 244 // to the IO thread if necessary. |
241 void EnablePredictor(bool enable); | |
242 | |
243 void EnablePredictorOnIOThread(bool enable); | |
244 | |
245 // May be called from either the IO or UI thread and will PostTask | |
246 // to the IO thread if necessary. | |
247 void PreconnectUrl(const GURL& url, const GURL& first_party_for_cookies, | 245 void PreconnectUrl(const GURL& url, const GURL& first_party_for_cookies, |
248 UrlInfo::ResolutionMotivation motivation, int count); | 246 UrlInfo::ResolutionMotivation motivation, int count); |
249 | 247 |
250 void PreconnectUrlOnIOThread(const GURL& url, | 248 void PreconnectUrlOnIOThread(const GURL& url, |
251 const GURL& first_party_for_cookies, | 249 const GURL& first_party_for_cookies, |
252 UrlInfo::ResolutionMotivation motivation, | 250 UrlInfo::ResolutionMotivation motivation, |
253 int count); | 251 int count); |
254 | 252 |
255 // ------------- End IO thread methods. | 253 // ------------- End IO thread methods. |
256 | 254 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 } | 286 } |
289 // Used for testing. | 287 // Used for testing. |
290 void SetShutdown(bool shutdown) { | 288 void SetShutdown(bool shutdown) { |
291 shutdown_ = shutdown; | 289 shutdown_ = shutdown; |
292 } | 290 } |
293 // Used for testing. | 291 // Used for testing. |
294 void SetObserver(PredictorObserver* observer) { | 292 void SetObserver(PredictorObserver* observer) { |
295 observer_ = observer; | 293 observer_ = observer; |
296 } | 294 } |
297 | 295 |
298 // Flag setting to use preconnection instead of just DNS pre-fetching. | 296 ProfileIOData* profile_io_data() const { |
| 297 return profile_io_data_; |
| 298 } |
| 299 |
299 bool preconnect_enabled() const { | 300 bool preconnect_enabled() const { |
300 return preconnect_enabled_; | 301 return preconnect_enabled_; |
301 } | 302 } |
302 | 303 |
303 // Flag setting for whether we are prefetching dns lookups. | |
304 bool predictor_enabled() const { | 304 bool predictor_enabled() const { |
305 return predictor_enabled_; | 305 return predictor_enabled_; |
306 } | 306 } |
307 | 307 |
308 | 308 |
309 private: | 309 private: |
310 FRIEND_TEST_ALL_PREFIXES(PredictorTest, BenefitLookupTest); | 310 FRIEND_TEST_ALL_PREFIXES(PredictorTest, BenefitLookupTest); |
311 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ShutdownWhenResolutionIsPendingTest); | 311 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ShutdownWhenResolutionIsPendingTest); |
312 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTest); | 312 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTest); |
313 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ConcurrentLookupTest); | 313 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ConcurrentLookupTest); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 // Only for testing; | 428 // Only for testing; |
429 size_t peak_pending_lookups() const { return peak_pending_lookups_; } | 429 size_t peak_pending_lookups() const { return peak_pending_lookups_; } |
430 | 430 |
431 // If a proxy advisor is defined, let it know that |url| will be prefetched or | 431 // If a proxy advisor is defined, let it know that |url| will be prefetched or |
432 // preconnected to. Can be called on either UI or IO threads and will post to | 432 // preconnected to. Can be called on either UI or IO threads and will post to |
433 // the IO thread if necessary, invoking AdviseProxyOnIOThread(). | 433 // the IO thread if necessary, invoking AdviseProxyOnIOThread(). |
434 void AdviseProxy(const GURL& url, | 434 void AdviseProxy(const GURL& url, |
435 UrlInfo::ResolutionMotivation motivation, | 435 UrlInfo::ResolutionMotivation motivation, |
436 bool is_preconnect); | 436 bool is_preconnect); |
437 | 437 |
| 438 // There two members call the corresponding global functions in |
| 439 // prediction_options.cc. |
| 440 virtual bool CanPredictNetworkActionsUI(); |
| 441 virtual bool CanPredictNetworkActionsIO(); |
| 442 |
438 // ------------- Start IO thread methods. | 443 // ------------- Start IO thread methods. |
439 | 444 |
440 // Perform actual resolution or preconnection to subresources now. This is | 445 // Perform actual resolution or preconnection to subresources now. This is |
441 // an internal worker method that is reached via a post task from | 446 // an internal worker method that is reached via a post task from |
442 // PredictFrameSubresources(). | 447 // PredictFrameSubresources(). |
443 void PrepareFrameSubresources(const GURL& url, | 448 void PrepareFrameSubresources(const GURL& url, |
444 const GURL& first_party_for_cookies); | 449 const GURL& first_party_for_cookies); |
445 | 450 |
446 // Access method for use by async lookup request to pass resolution result. | 451 // Access method for use by async lookup request to pass resolution result. |
447 void OnLookupFinished(LookupRequest* request, const GURL& url, bool found); | 452 void OnLookupFinished(LookupRequest* request, const GURL& url, bool found); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 | 506 |
502 // ------------- End IO thread methods. | 507 // ------------- End IO thread methods. |
503 | 508 |
504 scoped_ptr<InitialObserver> initial_observer_; | 509 scoped_ptr<InitialObserver> initial_observer_; |
505 | 510 |
506 // Reference to URLRequestContextGetter from the Profile which owns the | 511 // Reference to URLRequestContextGetter from the Profile which owns the |
507 // predictor. Used by Preconnect. | 512 // predictor. Used by Preconnect. |
508 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 513 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
509 | 514 |
510 // Status of speculative DNS resolution and speculative TCP/IP connection | 515 // Status of speculative DNS resolution and speculative TCP/IP connection |
511 // feature. | 516 // feature. This is false if and only if disabled by a command line switch. |
512 bool predictor_enabled_; | 517 const bool predictor_enabled_; |
| 518 |
| 519 // This is set by InitNetworkPredictor and used for calling |
| 520 // chrome_browser_net::CanPredictNetworkActionsUI. |
| 521 PrefService* user_prefs_; |
| 522 |
| 523 // This is set by InitNetworkPredictor and used for calling |
| 524 // chrome_browser_net::CanPredictNetworkActionsIO. |
| 525 ProfileIOData* profile_io_data_; |
513 | 526 |
514 // work_queue_ holds a list of names we need to look up. | 527 // work_queue_ holds a list of names we need to look up. |
515 HostNameQueue work_queue_; | 528 HostNameQueue work_queue_; |
516 | 529 |
517 // results_ contains information for existing/prior prefetches. | 530 // results_ contains information for existing/prior prefetches. |
518 Results results_; | 531 Results results_; |
519 | 532 |
520 std::set<LookupRequest*> pending_lookups_; | 533 std::set<LookupRequest*> pending_lookups_; |
521 | 534 |
522 // For testing, to verify that we don't exceed the limit. | 535 // For testing, to verify that we don't exceed the limit. |
(...skipping 17 matching lines...) Expand all Loading... |
540 | 553 |
541 // The TransportSecurityState instance we query HSTS redirects from. | 554 // The TransportSecurityState instance we query HSTS redirects from. |
542 net::TransportSecurityState* transport_security_state_; | 555 net::TransportSecurityState* transport_security_state_; |
543 | 556 |
544 // The SSLConfigService we query SNI support from (used in querying HSTS | 557 // The SSLConfigService we query SNI support from (used in querying HSTS |
545 // redirects). | 558 // redirects). |
546 net::SSLConfigService* ssl_config_service_; | 559 net::SSLConfigService* ssl_config_service_; |
547 | 560 |
548 // Are we currently using preconnection, rather than just DNS resolution, for | 561 // Are we currently using preconnection, rather than just DNS resolution, for |
549 // subresources and omni-box search URLs. | 562 // subresources and omni-box search URLs. |
550 bool preconnect_enabled_; | 563 // This is false if and only if disabled by a command line switch. |
| 564 const bool preconnect_enabled_; |
551 | 565 |
552 // Most recent suggestion from Omnibox provided via AnticipateOmniboxUrl(). | 566 // Most recent suggestion from Omnibox provided via AnticipateOmniboxUrl(). |
553 std::string last_omnibox_host_; | 567 std::string last_omnibox_host_; |
554 | 568 |
555 // The time when the last preresolve was done for last_omnibox_host_. | 569 // The time when the last preresolve was done for last_omnibox_host_. |
556 base::TimeTicks last_omnibox_preresolve_; | 570 base::TimeTicks last_omnibox_preresolve_; |
557 | 571 |
558 // The number of consecutive requests to AnticipateOmniboxUrl() that suggested | 572 // The number of consecutive requests to AnticipateOmniboxUrl() that suggested |
559 // preconnecting (because it was to a search service). | 573 // preconnecting (because it was to a search service). |
560 int consecutive_omnibox_preconnect_count_; | 574 int consecutive_omnibox_preconnect_count_; |
(...skipping 20 matching lines...) Expand all Loading... |
581 | 595 |
582 // An observer for testing. | 596 // An observer for testing. |
583 PredictorObserver* observer_; | 597 PredictorObserver* observer_; |
584 | 598 |
585 DISALLOW_COPY_AND_ASSIGN(Predictor); | 599 DISALLOW_COPY_AND_ASSIGN(Predictor); |
586 }; | 600 }; |
587 | 601 |
588 // This version of the predictor is used for testing. | 602 // This version of the predictor is used for testing. |
589 class SimplePredictor : public Predictor { | 603 class SimplePredictor : public Predictor { |
590 public: | 604 public: |
591 explicit SimplePredictor(bool preconnect_enabled) | 605 explicit SimplePredictor(bool preconnect_enabled, bool predictor_enabled) |
592 : Predictor(preconnect_enabled) {} | 606 : Predictor(preconnect_enabled, predictor_enabled) {} |
593 virtual ~SimplePredictor() {} | 607 virtual ~SimplePredictor() {} |
594 virtual void InitNetworkPredictor( | 608 virtual void InitNetworkPredictor( |
595 PrefService* user_prefs, | 609 PrefService* user_prefs, |
596 PrefService* local_state, | 610 PrefService* local_state, |
597 IOThread* io_thread, | 611 IOThread* io_thread, |
598 net::URLRequestContextGetter* getter) OVERRIDE; | 612 net::URLRequestContextGetter* getter, |
| 613 ProfileIOData* profile_io_data) OVERRIDE; |
599 virtual void ShutdownOnUIThread() OVERRIDE; | 614 virtual void ShutdownOnUIThread() OVERRIDE; |
| 615 private: |
| 616 // There member functions return True for unittests. |
| 617 virtual bool CanPredictNetworkActionsUI() OVERRIDE; |
| 618 virtual bool CanPredictNetworkActionsIO() OVERRIDE; |
600 }; | 619 }; |
601 | 620 |
602 } // namespace chrome_browser_net | 621 } // namespace chrome_browser_net |
603 | 622 |
604 #endif // CHROME_BROWSER_NET_PREDICTOR_H_ | 623 #endif // CHROME_BROWSER_NET_PREDICTOR_H_ |
OLD | NEW |