| 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 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 #include "base/gtest_prod_util.h" | 32 #include "base/gtest_prod_util.h" |
| 33 #include "base/macros.h" | 33 #include "base/macros.h" |
| 34 #include "base/memory/weak_ptr.h" | 34 #include "base/memory/weak_ptr.h" |
| 35 #include "chrome/browser/net/prediction_options.h" | 35 #include "chrome/browser/net/prediction_options.h" |
| 36 #include "chrome/browser/net/referrer.h" | 36 #include "chrome/browser/net/referrer.h" |
| 37 #include "chrome/browser/net/timed_cache.h" | 37 #include "chrome/browser/net/timed_cache.h" |
| 38 #include "chrome/browser/net/url_info.h" | 38 #include "chrome/browser/net/url_info.h" |
| 39 #include "components/network_hints/common/network_hints_common.h" | 39 #include "components/network_hints/common/network_hints_common.h" |
| 40 #include "net/base/host_port_pair.h" | 40 #include "net/base/host_port_pair.h" |
| 41 #include "url/gurl.h" |
| 41 | 42 |
| 42 class IOThread; | 43 class IOThread; |
| 43 class PrefService; | 44 class PrefService; |
| 44 class Profile; | 45 class Profile; |
| 45 class ProfileIOData; | 46 class ProfileIOData; |
| 46 | 47 |
| 47 namespace base { | 48 namespace base { |
| 48 class ListValue; | 49 class ListValue; |
| 49 class WaitableEvent; | 50 class WaitableEvent; |
| 50 } | 51 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 68 typedef std::map<GURL, UrlInfo> Results; | 69 typedef std::map<GURL, UrlInfo> Results; |
| 69 | 70 |
| 70 // An observer for testing. | 71 // An observer for testing. |
| 71 class PredictorObserver { | 72 class PredictorObserver { |
| 72 public: | 73 public: |
| 73 virtual ~PredictorObserver() {} | 74 virtual ~PredictorObserver() {} |
| 74 | 75 |
| 75 virtual void OnPreconnectUrl(const GURL& original_url, | 76 virtual void OnPreconnectUrl(const GURL& original_url, |
| 76 const GURL& first_party_for_cookies, | 77 const GURL& first_party_for_cookies, |
| 77 UrlInfo::ResolutionMotivation motivation, | 78 UrlInfo::ResolutionMotivation motivation, |
| 78 int count) = 0; | 79 int count) {} |
| 80 virtual void OnLearnFromNavigation(const GURL& referring_url, |
| 81 const GURL& target_url) {} |
| 79 }; | 82 }; |
| 80 | 83 |
| 81 // Predictor is constructed during Profile construction (on the UI thread), | 84 // Predictor is constructed during Profile construction (on the UI thread), |
| 82 // but it is destroyed on the IO thread when ProfileIOData goes away. All of | 85 // but it is destroyed on the IO thread when ProfileIOData goes away. All of |
| 83 // its core state and functionality happens on the IO thread. The only UI | 86 // its core state and functionality happens on the IO thread. The only UI |
| 84 // methods are initialization / shutdown related (including preconnect | 87 // methods are initialization / shutdown related (including preconnect |
| 85 // initialization), or convenience methods that internally forward calls to | 88 // initialization), or convenience methods that internally forward calls to |
| 86 // the IO thread. | 89 // the IO thread. |
| 87 class Predictor { | 90 class Predictor { |
| 88 public: | 91 public: |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 } | 295 } |
| 293 // Used for testing. | 296 // Used for testing. |
| 294 void SetObserver(PredictorObserver* observer) { | 297 void SetObserver(PredictorObserver* observer) { |
| 295 observer_ = observer; | 298 observer_ = observer; |
| 296 } | 299 } |
| 297 | 300 |
| 298 ProfileIOData* profile_io_data() const { | 301 ProfileIOData* profile_io_data() const { |
| 299 return profile_io_data_; | 302 return profile_io_data_; |
| 300 } | 303 } |
| 301 | 304 |
| 302 bool preconnect_enabled() const { | |
| 303 return preconnect_enabled_; | |
| 304 } | |
| 305 | |
| 306 bool predictor_enabled() const { | 305 bool predictor_enabled() const { |
| 307 return predictor_enabled_; | 306 return predictor_enabled_; |
| 308 } | 307 } |
| 309 | 308 |
| 309 bool PreconnectEnabled() const; |
| 310 |
| 311 // Used only for testing. Overrides command line flag to disable preconnect, |
| 312 // which is added in the browser test fixture. |
| 313 void SetPreconnectEnabledForTest(bool preconnect_enabled); |
| 314 |
| 315 net::URLRequestContextGetter* url_request_context_getter_for_test() { |
| 316 return url_request_context_getter_.get(); |
| 317 } |
| 310 | 318 |
| 311 private: | 319 private: |
| 312 FRIEND_TEST_ALL_PREFIXES(PredictorTest, BenefitLookupTest); | 320 FRIEND_TEST_ALL_PREFIXES(PredictorTest, BenefitLookupTest); |
| 313 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ShutdownWhenResolutionIsPendingTest); | 321 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ShutdownWhenResolutionIsPendingTest); |
| 314 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTest); | 322 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTest); |
| 315 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ConcurrentLookupTest); | 323 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ConcurrentLookupTest); |
| 316 FRIEND_TEST_ALL_PREFIXES(PredictorTest, MassiveConcurrentLookupTest); | 324 FRIEND_TEST_ALL_PREFIXES(PredictorTest, MassiveConcurrentLookupTest); |
| 317 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueuePushPopTest); | 325 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueuePushPopTest); |
| 318 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueueReorderTest); | 326 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueueReorderTest); |
| 319 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ReferrerSerializationTrimTest); | 327 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ReferrerSerializationTrimTest); |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 net::TransportSecurityState* transport_security_state_; | 563 net::TransportSecurityState* transport_security_state_; |
| 556 | 564 |
| 557 // The SSLConfigService we query SNI support from (used in querying HSTS | 565 // The SSLConfigService we query SNI support from (used in querying HSTS |
| 558 // redirects). | 566 // redirects). |
| 559 net::SSLConfigService* ssl_config_service_; | 567 net::SSLConfigService* ssl_config_service_; |
| 560 | 568 |
| 561 // The ProxyService, used to determine whether preresolve is useful. | 569 // The ProxyService, used to determine whether preresolve is useful. |
| 562 net::ProxyService* proxy_service_; | 570 net::ProxyService* proxy_service_; |
| 563 | 571 |
| 564 // Are we currently using preconnection, rather than just DNS resolution, for | 572 // Are we currently using preconnection, rather than just DNS resolution, for |
| 565 // subresources and omni-box search URLs. | 573 // subresources and omni-box search URLs. This is false if and only if |
| 566 // This is false if and only if disabled by a command line switch. | 574 // disabled by a command line switch. Protected by |preconnect_enabled_lock_|, |
| 567 const bool preconnect_enabled_; | 575 // which is used by tests to bypass the command line flags. |
| 576 bool preconnect_enabled_; |
| 568 | 577 |
| 569 // Most recent suggestion from Omnibox provided via AnticipateOmniboxUrl(). | 578 // Most recent suggestion from Omnibox provided via AnticipateOmniboxUrl(). |
| 570 std::string last_omnibox_host_; | 579 std::string last_omnibox_host_; |
| 571 | 580 |
| 572 // The time when the last preresolve was done for last_omnibox_host_. | 581 // The time when the last preresolve was done for last_omnibox_host_. |
| 573 base::TimeTicks last_omnibox_preresolve_; | 582 base::TimeTicks last_omnibox_preresolve_; |
| 574 | 583 |
| 575 // The number of consecutive requests to AnticipateOmniboxUrl() that suggested | 584 // The number of consecutive requests to AnticipateOmniboxUrl() that suggested |
| 576 // preconnecting (because it was to a search service). | 585 // preconnecting (because it was to a search service). |
| 577 int consecutive_omnibox_preconnect_count_; | 586 int consecutive_omnibox_preconnect_count_; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 590 std::vector<GURL> urls_being_trimmed_; | 599 std::vector<GURL> urls_being_trimmed_; |
| 591 | 600 |
| 592 // A time after which we need to do more trimming of referrers. | 601 // A time after which we need to do more trimming of referrers. |
| 593 base::TimeTicks next_trim_time_; | 602 base::TimeTicks next_trim_time_; |
| 594 | 603 |
| 595 // An observer for testing. | 604 // An observer for testing. |
| 596 PredictorObserver* observer_; | 605 PredictorObserver* observer_; |
| 597 | 606 |
| 598 std::unique_ptr<base::WeakPtrFactory<Predictor>> weak_factory_; | 607 std::unique_ptr<base::WeakPtrFactory<Predictor>> weak_factory_; |
| 599 | 608 |
| 609 // Protects |preconnect_enabled_|. |
| 610 mutable base::Lock preconnect_enabled_lock_; |
| 611 |
| 600 DISALLOW_COPY_AND_ASSIGN(Predictor); | 612 DISALLOW_COPY_AND_ASSIGN(Predictor); |
| 601 }; | 613 }; |
| 602 | 614 |
| 603 // This version of the predictor is used for testing. | 615 // This version of the predictor is used for testing. |
| 604 class SimplePredictor : public Predictor { | 616 class SimplePredictor : public Predictor { |
| 605 public: | 617 public: |
| 606 explicit SimplePredictor(bool preconnect_enabled, bool predictor_enabled) | 618 explicit SimplePredictor(bool preconnect_enabled, bool predictor_enabled) |
| 607 : Predictor(preconnect_enabled, predictor_enabled) {} | 619 : Predictor(preconnect_enabled, predictor_enabled) {} |
| 608 ~SimplePredictor() override {} | 620 ~SimplePredictor() override {} |
| 609 void InitNetworkPredictor(PrefService* user_prefs, | 621 void InitNetworkPredictor(PrefService* user_prefs, |
| 610 IOThread* io_thread, | 622 IOThread* io_thread, |
| 611 net::URLRequestContextGetter* getter, | 623 net::URLRequestContextGetter* getter, |
| 612 ProfileIOData* profile_io_data) override; | 624 ProfileIOData* profile_io_data) override; |
| 613 void ShutdownOnUIThread() override; | 625 void ShutdownOnUIThread() override; |
| 614 | 626 |
| 615 private: | 627 private: |
| 616 // These member functions return True for unittests. | 628 // These member functions return True for unittests. |
| 617 bool CanPrefetchAndPrerender() const override; | 629 bool CanPrefetchAndPrerender() const override; |
| 618 bool CanPreresolveAndPreconnect() const override; | 630 bool CanPreresolveAndPreconnect() const override; |
| 619 }; | 631 }; |
| 620 | 632 |
| 621 } // namespace chrome_browser_net | 633 } // namespace chrome_browser_net |
| 622 | 634 |
| 623 #endif // CHROME_BROWSER_NET_PREDICTOR_H_ | 635 #endif // CHROME_BROWSER_NET_PREDICTOR_H_ |
| OLD | NEW |