| 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 |
| 11 // before it is really needed, and hence reduce latency in the standard lookup | 11 // before it is really needed, and hence reduce latency in the standard lookup |
| 12 // paths. | 12 // paths. |
| 13 // Subresource relationships are usually acquired from the referrer field in a | 13 // Subresource relationships are usually acquired from the referrer field in a |
| 14 // navigation. A subresource URL may be associated with a referrer URL. Later | 14 // navigation. A subresource URL may be associated with a referrer URL. Later |
| 15 // navigations may, if the likelihood of needing the subresource is high enough, | 15 // navigations may, if the likelihood of needing the subresource is high enough, |
| 16 // cause this module to speculatively create a TCP/IP connection. If there is | 16 // cause this module to speculatively create a TCP/IP connection. If there is |
| 17 // only a low likelihood, then a DNS pre-resolution operation may be performed. | 17 // only a low likelihood, then a DNS pre-resolution operation may be performed. |
| 18 | 18 |
| 19 #ifndef CHROME_BROWSER_NET_PREDICTOR_H_ | 19 #ifndef CHROME_BROWSER_NET_PREDICTOR_H_ |
| 20 #define CHROME_BROWSER_NET_PREDICTOR_H_ | 20 #define CHROME_BROWSER_NET_PREDICTOR_H_ |
| 21 | 21 |
| 22 #include <stddef.h> | 22 #include <stddef.h> |
| 23 #include <stdint.h> | 23 #include <stdint.h> |
| 24 | 24 |
| 25 #include <map> | 25 #include <map> |
| 26 #include <memory> | 26 #include <memory> |
| 27 #include <queue> | 27 #include <queue> |
| 28 #include <set> | |
| 29 #include <string> | 28 #include <string> |
| 30 #include <vector> | 29 #include <vector> |
| 31 | 30 |
| 32 #include "base/gtest_prod_util.h" | 31 #include "base/gtest_prod_util.h" |
| 33 #include "base/macros.h" | 32 #include "base/macros.h" |
| 34 #include "base/memory/weak_ptr.h" | 33 #include "base/memory/weak_ptr.h" |
| 35 #include "chrome/browser/net/prediction_options.h" | 34 #include "chrome/browser/net/prediction_options.h" |
| 36 #include "chrome/browser/net/referrer.h" | 35 #include "chrome/browser/net/referrer.h" |
| 37 #include "chrome/browser/net/timed_cache.h" | 36 #include "chrome/browser/net/timed_cache.h" |
| 38 #include "chrome/browser/net/url_info.h" | 37 #include "chrome/browser/net/url_info.h" |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 // instigated this activity. | 271 // instigated this activity. |
| 273 void PredictFrameSubresources(const GURL& url, | 272 void PredictFrameSubresources(const GURL& url, |
| 274 const GURL& first_party_for_cookies); | 273 const GURL& first_party_for_cookies); |
| 275 | 274 |
| 276 // Put URL in canonical form, including a scheme, host, and port. | 275 // Put URL in canonical form, including a scheme, host, and port. |
| 277 // Returns GURL::EmptyGURL() if the scheme is not http/https or if the url | 276 // Returns GURL::EmptyGURL() if the scheme is not http/https or if the url |
| 278 // cannot be otherwise canonicalized. | 277 // cannot be otherwise canonicalized. |
| 279 static GURL CanonicalizeUrl(const GURL& url); | 278 static GURL CanonicalizeUrl(const GURL& url); |
| 280 | 279 |
| 281 // Used for testing. | 280 // Used for testing. |
| 282 void SetHostResolver(net::HostResolver* host_resolver) { | |
| 283 host_resolver_ = host_resolver; | |
| 284 } | |
| 285 // Used for testing. | |
| 286 void SetTransportSecurityState( | 281 void SetTransportSecurityState( |
| 287 net::TransportSecurityState* transport_security_state) { | 282 net::TransportSecurityState* transport_security_state) { |
| 288 transport_security_state_ = transport_security_state; | 283 transport_security_state_ = transport_security_state; |
| 289 } | 284 } |
| 290 // Used for testing. | 285 // Used for testing. |
| 291 size_t max_concurrent_dns_lookups() const { | 286 size_t max_concurrent_dns_lookups() const { |
| 292 return max_concurrent_dns_lookups_; | 287 return max_concurrent_dns_lookups_; |
| 293 } | 288 } |
| 294 // Used for testing. | 289 // Used for testing. |
| 295 void SetShutdown(bool shutdown) { | 290 void SetShutdown(bool shutdown) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTestWithDisabledAdvisor); | 322 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTestWithDisabledAdvisor); |
| 328 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTestWithEnabledAdvisor); | 323 FRIEND_TEST_ALL_PREFIXES(PredictorTest, SingleLookupTestWithEnabledAdvisor); |
| 329 FRIEND_TEST_ALL_PREFIXES(PredictorTest, TestSimplePreconnectAdvisor); | 324 FRIEND_TEST_ALL_PREFIXES(PredictorTest, TestSimplePreconnectAdvisor); |
| 330 FRIEND_TEST_ALL_PREFIXES(PredictorTest, NoProxyService); | 325 FRIEND_TEST_ALL_PREFIXES(PredictorTest, NoProxyService); |
| 331 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyDefinitelyEnabled); | 326 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyDefinitelyEnabled); |
| 332 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyDefinitelyNotEnabled); | 327 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyDefinitelyNotEnabled); |
| 333 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyMaybeEnabled); | 328 FRIEND_TEST_ALL_PREFIXES(PredictorTest, ProxyMaybeEnabled); |
| 334 friend class WaitForResolutionHelper; // For testing. | 329 friend class WaitForResolutionHelper; // For testing. |
| 335 friend class PredictorBrowserTest; | 330 friend class PredictorBrowserTest; |
| 336 | 331 |
| 337 class LookupRequest; | |
| 338 | |
| 339 // A simple priority queue for handling host names. | 332 // A simple priority queue for handling host names. |
| 340 // Some names that are queued up have |motivation| that requires very rapid | 333 // Some names that are queued up have |motivation| that requires very rapid |
| 341 // handling. For example, a sub-resource name lookup MUST be done before the | 334 // handling. For example, a sub-resource name lookup MUST be done before the |
| 342 // actual sub-resource is fetched. In contrast, a name that was speculatively | 335 // actual sub-resource is fetched. In contrast, a name that was speculatively |
| 343 // noted in a page has to be resolved before the user "gets around to" | 336 // noted in a page has to be resolved before the user "gets around to" |
| 344 // clicking on a link. By tagging (with a motivation) each push we make into | 337 // clicking on a link. By tagging (with a motivation) each push we make into |
| 345 // this FIFO queue, the queue can re-order the more important names to service | 338 // this FIFO queue, the queue can re-order the more important names to service |
| 346 // them sooner (relative to some low priority background resolutions). | 339 // them sooner (relative to some low priority background resolutions). |
| 347 class HostNameQueue { | 340 class HostNameQueue { |
| 348 public: | 341 public: |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 | 423 |
| 431 // ------------- Start IO thread methods. | 424 // ------------- Start IO thread methods. |
| 432 | 425 |
| 433 // Perform actual resolution or preconnection to subresources now. This is | 426 // Perform actual resolution or preconnection to subresources now. This is |
| 434 // an internal worker method that is reached via a post task from | 427 // an internal worker method that is reached via a post task from |
| 435 // PredictFrameSubresources(). | 428 // PredictFrameSubresources(). |
| 436 void PrepareFrameSubresources(const GURL& url, | 429 void PrepareFrameSubresources(const GURL& url, |
| 437 const GURL& first_party_for_cookies); | 430 const GURL& first_party_for_cookies); |
| 438 | 431 |
| 439 // Access method for use by async lookup request to pass resolution result. | 432 // Access method for use by async lookup request to pass resolution result. |
| 440 void OnLookupFinished(LookupRequest* request, const GURL& url, bool found); | 433 void OnLookupFinished(const GURL& url, int result); |
| 441 | 434 |
| 442 // Underlying method for both async and synchronous lookup to update state. | 435 // Underlying method for both async and synchronous lookup to update state. |
| 443 void LookupFinished(LookupRequest* request, | 436 void LookupFinished(const GURL& url, bool found); |
| 444 const GURL& url, bool found); | |
| 445 | 437 |
| 446 // Queue hostname for resolution. If queueing was done, return the pointer | 438 // Queues hostname for resolution. If queueing was done, return the pointer |
| 447 // to the queued instance, otherwise return NULL. If the proxy advisor is | 439 // to the queued instance, otherwise return nullptr. If the proxy advisor is |
| 448 // enabled, and |url| is likely to be proxied, the hostname will not be | 440 // enabled, and |url| is likely to be proxied, the hostname will not be |
| 449 // queued as the browser is not expected to fetch it directly. | 441 // queued as the browser is not expected to fetch it directly. |
| 450 UrlInfo* AppendToResolutionQueue(const GURL& url, | 442 UrlInfo* AppendToResolutionQueue(const GURL& url, |
| 451 UrlInfo::ResolutionMotivation motivation); | 443 UrlInfo::ResolutionMotivation motivation); |
| 452 | 444 |
| 453 // Check to see if too much queuing delay has been noted for the given info, | 445 // Check to see if too much queuing delay has been noted for the given info, |
| 454 // which indicates that there is "congestion" or growing delay in handling the | 446 // which indicates that there is "congestion" or growing delay in handling the |
| 455 // resolution of names. Rather than letting this congestion potentially grow | 447 // resolution of names. Rather than letting this congestion potentially grow |
| 456 // without bounds, we abandon our queued efforts at pre-resolutions in such a | 448 // without bounds, we abandon our queued efforts at pre-resolutions in such a |
| 457 // case. | 449 // case. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 // This is set by InitNetworkPredictor and used for calling | 504 // This is set by InitNetworkPredictor and used for calling |
| 513 // CanPrefetchAndPrerenderIO and CanPreresolveAndPreconnectIO. | 505 // CanPrefetchAndPrerenderIO and CanPreresolveAndPreconnectIO. |
| 514 ProfileIOData* profile_io_data_; | 506 ProfileIOData* profile_io_data_; |
| 515 | 507 |
| 516 // work_queue_ holds a list of names we need to look up. | 508 // work_queue_ holds a list of names we need to look up. |
| 517 HostNameQueue work_queue_; | 509 HostNameQueue work_queue_; |
| 518 | 510 |
| 519 // results_ contains information for existing/prior prefetches. | 511 // results_ contains information for existing/prior prefetches. |
| 520 Results results_; | 512 Results results_; |
| 521 | 513 |
| 522 std::set<LookupRequest*> pending_lookups_; | 514 size_t num_pending_lookups_; |
| 523 | 515 |
| 524 // For testing, to verify that we don't exceed the limit. | 516 // For testing, to verify that we don't exceed the limit. |
| 525 size_t peak_pending_lookups_; | 517 size_t peak_pending_lookups_; |
| 526 | 518 |
| 527 // When true, we don't make new lookup requests. | 519 // When true, we don't make new lookup requests. |
| 528 bool shutdown_; | 520 bool shutdown_; |
| 529 | 521 |
| 530 // The number of concurrent speculative lookups currently allowed to be sent | 522 // The number of concurrent speculative lookups currently allowed to be sent |
| 531 // to the resolver. Any additional lookups will be queued to avoid exceeding | 523 // to the resolver. Any additional lookups will be queued to avoid exceeding |
| 532 // this value. The queue is a priority queue that will accelerate | 524 // this value. The queue is a priority queue that will accelerate |
| 533 // sub-resource speculation, and retard resolutions suggested by page scans. | 525 // sub-resource speculation, and retard resolutions suggested by page scans. |
| 534 const size_t max_concurrent_dns_lookups_; | 526 const size_t max_concurrent_dns_lookups_; |
| 535 | 527 |
| 536 // The maximum queueing delay that is acceptable before we enter congestion | 528 // The maximum queueing delay that is acceptable before we enter congestion |
| 537 // reduction mode, and discard all queued (but not yet assigned) resolutions. | 529 // reduction mode, and discard all queued (but not yet assigned) resolutions. |
| 538 const base::TimeDelta max_dns_queue_delay_; | 530 const base::TimeDelta max_dns_queue_delay_; |
| 539 | 531 |
| 540 // The host resolver we warm DNS entries for. | |
| 541 net::HostResolver* host_resolver_; | |
| 542 | |
| 543 // The TransportSecurityState instance we query HSTS redirects from. | 532 // The TransportSecurityState instance we query HSTS redirects from. |
| 544 net::TransportSecurityState* transport_security_state_; | 533 net::TransportSecurityState* transport_security_state_; |
| 545 | 534 |
| 546 // The SSLConfigService we query SNI support from (used in querying HSTS | 535 // The SSLConfigService we query SNI support from (used in querying HSTS |
| 547 // redirects). | 536 // redirects). |
| 548 net::SSLConfigService* ssl_config_service_; | 537 net::SSLConfigService* ssl_config_service_; |
| 549 | 538 |
| 550 // The ProxyService, used to determine whether preresolve is useful. | 539 // The ProxyService, used to determine whether preresolve is useful. |
| 551 net::ProxyService* proxy_service_; | 540 net::ProxyService* proxy_service_; |
| 552 | 541 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 | 598 |
| 610 private: | 599 private: |
| 611 // These member functions return True for unittests. | 600 // These member functions return True for unittests. |
| 612 bool CanPrefetchAndPrerender() const override; | 601 bool CanPrefetchAndPrerender() const override; |
| 613 bool CanPreresolveAndPreconnect() const override; | 602 bool CanPreresolveAndPreconnect() const override; |
| 614 }; | 603 }; |
| 615 | 604 |
| 616 } // namespace chrome_browser_net | 605 } // namespace chrome_browser_net |
| 617 | 606 |
| 618 #endif // CHROME_BROWSER_NET_PREDICTOR_H_ | 607 #endif // CHROME_BROWSER_NET_PREDICTOR_H_ |
| OLD | NEW |