Chromium Code Reviews| 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 { | 305 bool preconnect_enabled() const { |
|
mmenke
2016/05/12 16:30:23
Naming scheme should be changed, and method de-inl
mmenke
2016/05/12 16:30:23
optional: Could use an atomic instead of a lock h
Charlie Harrison
2016/05/12 20:17:03
Done.
Charlie Harrison
2016/05/12 20:17:03
Kept the lock for simplicity.
| |
| 306 base::AutoLock lock(lock_); | |
| 303 return preconnect_enabled_; | 307 return preconnect_enabled_; |
| 304 } | 308 } |
| 305 | 309 |
| 306 bool predictor_enabled() const { | 310 bool predictor_enabled() const { |
| 307 return predictor_enabled_; | 311 return predictor_enabled_; |
| 308 } | 312 } |
| 309 | 313 |
| 314 // Used only for testing. Overrides command line flag to disable preconnect, | |
| 315 // which is added in the browser test fixture. | |
| 316 void set_preconnect_enabled_for_test(bool preconnect_enabled) { | |
|
mmenke
2016/05/12 16:30:22
Again, naming scheme should be changed, and should
Charlie Harrison
2016/05/12 20:17:03
Done.
| |
| 317 base::AutoLock lock(lock_); | |
| 318 preconnect_enabled_ = preconnect_enabled; | |
| 319 } | |
| 320 | |
| 321 net::URLRequestContextGetter* url_request_context_getter_for_test() { | |
| 322 return url_request_context_getter_.get(); | |
| 323 } | |
| 310 | 324 |
| 311 private: | 325 private: |
| 312 FRIEND_TEST_ALL_PREFIXES(PredictorTest, BenefitLookupTest); | 326 FRIEND_TEST_ALL_PREFIXES(PredictorTest, BenefitLookupTest); |
| 313 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ShutdownWhenResolutionIsPendingTest); | 327 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ShutdownWhenResolutionIsPendingTest); |
| 314 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTest); | 328 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTest); |
| 315 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ConcurrentLookupTest); | 329 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ConcurrentLookupTest); |
| 316 FRIEND_TEST_ALL_PREFIXES(PredictorTest, MassiveConcurrentLookupTest); | 330 FRIEND_TEST_ALL_PREFIXES(PredictorTest, MassiveConcurrentLookupTest); |
| 317 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueuePushPopTest); | 331 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueuePushPopTest); |
| 318 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueueReorderTest); | 332 FRIEND_TEST_ALL_PREFIXES(PredictorTest, PriorityQueueReorderTest); |
| 319 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ReferrerSerializationTrimTest); | 333 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ReferrerSerializationTrimTest); |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 557 // The SSLConfigService we query SNI support from (used in querying HSTS | 571 // The SSLConfigService we query SNI support from (used in querying HSTS |
| 558 // redirects). | 572 // redirects). |
| 559 net::SSLConfigService* ssl_config_service_; | 573 net::SSLConfigService* ssl_config_service_; |
| 560 | 574 |
| 561 // The ProxyService, used to determine whether preresolve is useful. | 575 // The ProxyService, used to determine whether preresolve is useful. |
| 562 net::ProxyService* proxy_service_; | 576 net::ProxyService* proxy_service_; |
| 563 | 577 |
| 564 // Are we currently using preconnection, rather than just DNS resolution, for | 578 // Are we currently using preconnection, rather than just DNS resolution, for |
| 565 // subresources and omni-box search URLs. | 579 // subresources and omni-box search URLs. |
| 566 // This is false if and only if disabled by a command line switch. | 580 // This is false if and only if disabled by a command line switch. |
| 567 const bool preconnect_enabled_; | 581 bool preconnect_enabled_; |
|
mmenke
2016/05/12 16:30:23
Should mention it's protected by the lock. Or mak
Charlie Harrison
2016/05/12 20:17:03
Done.
| |
| 568 | 582 |
| 569 // Most recent suggestion from Omnibox provided via AnticipateOmniboxUrl(). | 583 // Most recent suggestion from Omnibox provided via AnticipateOmniboxUrl(). |
| 570 std::string last_omnibox_host_; | 584 std::string last_omnibox_host_; |
| 571 | 585 |
| 572 // The time when the last preresolve was done for last_omnibox_host_. | 586 // The time when the last preresolve was done for last_omnibox_host_. |
| 573 base::TimeTicks last_omnibox_preresolve_; | 587 base::TimeTicks last_omnibox_preresolve_; |
| 574 | 588 |
| 575 // The number of consecutive requests to AnticipateOmniboxUrl() that suggested | 589 // The number of consecutive requests to AnticipateOmniboxUrl() that suggested |
| 576 // preconnecting (because it was to a search service). | 590 // preconnecting (because it was to a search service). |
| 577 int consecutive_omnibox_preconnect_count_; | 591 int consecutive_omnibox_preconnect_count_; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 590 std::vector<GURL> urls_being_trimmed_; | 604 std::vector<GURL> urls_being_trimmed_; |
| 591 | 605 |
| 592 // A time after which we need to do more trimming of referrers. | 606 // A time after which we need to do more trimming of referrers. |
| 593 base::TimeTicks next_trim_time_; | 607 base::TimeTicks next_trim_time_; |
| 594 | 608 |
| 595 // An observer for testing. | 609 // An observer for testing. |
| 596 PredictorObserver* observer_; | 610 PredictorObserver* observer_; |
| 597 | 611 |
| 598 std::unique_ptr<base::WeakPtrFactory<Predictor>> weak_factory_; | 612 std::unique_ptr<base::WeakPtrFactory<Predictor>> weak_factory_; |
| 599 | 613 |
| 614 mutable base::Lock lock_; | |
|
mmenke
2016/05/12 16:30:23
Should mention what this protects, and could even
Charlie Harrison
2016/05/12 20:17:03
Done.
| |
| 615 | |
| 600 DISALLOW_COPY_AND_ASSIGN(Predictor); | 616 DISALLOW_COPY_AND_ASSIGN(Predictor); |
| 601 }; | 617 }; |
| 602 | 618 |
| 603 // This version of the predictor is used for testing. | 619 // This version of the predictor is used for testing. |
| 604 class SimplePredictor : public Predictor { | 620 class SimplePredictor : public Predictor { |
| 605 public: | 621 public: |
| 606 explicit SimplePredictor(bool preconnect_enabled, bool predictor_enabled) | 622 explicit SimplePredictor(bool preconnect_enabled, bool predictor_enabled) |
| 607 : Predictor(preconnect_enabled, predictor_enabled) {} | 623 : Predictor(preconnect_enabled, predictor_enabled) {} |
| 608 ~SimplePredictor() override {} | 624 ~SimplePredictor() override {} |
| 609 void InitNetworkPredictor(PrefService* user_prefs, | 625 void InitNetworkPredictor(PrefService* user_prefs, |
| 610 IOThread* io_thread, | 626 IOThread* io_thread, |
| 611 net::URLRequestContextGetter* getter, | 627 net::URLRequestContextGetter* getter, |
| 612 ProfileIOData* profile_io_data) override; | 628 ProfileIOData* profile_io_data) override; |
| 613 void ShutdownOnUIThread() override; | 629 void ShutdownOnUIThread() override; |
| 614 | 630 |
| 615 private: | 631 private: |
| 616 // These member functions return True for unittests. | 632 // These member functions return True for unittests. |
| 617 bool CanPrefetchAndPrerender() const override; | 633 bool CanPrefetchAndPrerender() const override; |
| 618 bool CanPreresolveAndPreconnect() const override; | 634 bool CanPreresolveAndPreconnect() const override; |
| 619 }; | 635 }; |
| 620 | 636 |
| 621 } // namespace chrome_browser_net | 637 } // namespace chrome_browser_net |
| 622 | 638 |
| 623 #endif // CHROME_BROWSER_NET_PREDICTOR_H_ | 639 #endif // CHROME_BROWSER_NET_PREDICTOR_H_ |
| OLD | NEW |