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 #include <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <memory> | 9 #include <memory> |
10 #include <set> | 10 #include <set> |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 same_site_preconnected_(0), | 313 same_site_preconnected_(0), |
314 dns_run_loop_(nullptr), | 314 dns_run_loop_(nullptr), |
315 strict_(true) {} | 315 strict_(true) {} |
316 | 316 |
317 void OnPreconnectUrl( | 317 void OnPreconnectUrl( |
318 const GURL& original_url, | 318 const GURL& original_url, |
319 const GURL& first_party_for_cookies, | 319 const GURL& first_party_for_cookies, |
320 chrome_browser_net::UrlInfo::ResolutionMotivation motivation, | 320 chrome_browser_net::UrlInfo::ResolutionMotivation motivation, |
321 int count) override { | 321 int count) override { |
322 base::AutoLock lock(lock_); | 322 base::AutoLock lock(lock_); |
| 323 preconnect_url_attempts_.insert(original_url); |
323 if (original_url == cross_site_host_) { | 324 if (original_url == cross_site_host_) { |
324 cross_site_preconnected_ = std::max(cross_site_preconnected_, count); | 325 cross_site_preconnected_ = std::max(cross_site_preconnected_, count); |
325 } else if (original_url == source_host_) { | 326 } else if (original_url == source_host_) { |
326 same_site_preconnected_ = std::max(same_site_preconnected_, count); | 327 same_site_preconnected_ = std::max(same_site_preconnected_, count); |
327 } else if (strict_) { | 328 } else if (strict_) { |
328 ADD_FAILURE() << "Preconnected " << original_url | 329 ADD_FAILURE() << "Preconnected " << original_url |
329 << " when should only be preconnecting the source host: " | 330 << " when should only be preconnecting the source host: " |
330 << source_host_ | 331 << source_host_ |
331 << " or the cross site host: " << cross_site_host_; | 332 << " or the cross site host: " << cross_site_host_; |
332 } | 333 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 lock_.AssertAcquired(); | 405 lock_.AssertAcquired(); |
405 return base::ContainsKey(successful_dns_lookups_, url) || | 406 return base::ContainsKey(successful_dns_lookups_, url) || |
406 base::ContainsKey(unsuccessful_dns_lookups_, url); | 407 base::ContainsKey(unsuccessful_dns_lookups_, url); |
407 } | 408 } |
408 | 409 |
409 bool HasHostBeenLookedUp(const GURL& url) { | 410 bool HasHostBeenLookedUp(const GURL& url) { |
410 base::AutoLock lock(lock_); | 411 base::AutoLock lock(lock_); |
411 return HasHostBeenLookedUpLocked(url); | 412 return HasHostBeenLookedUpLocked(url); |
412 } | 413 } |
413 | 414 |
| 415 bool HasHostAttemptedToPreconnect(const GURL& url) { |
| 416 base::AutoLock lock(lock_); |
| 417 return base::ContainsKey(preconnect_url_attempts_, url); |
| 418 } |
| 419 |
414 void CheckForWaitingLoop() { | 420 void CheckForWaitingLoop() { |
415 lock_.AssertAcquired(); | 421 lock_.AssertAcquired(); |
416 if (waiting_on_dns_.is_empty()) | 422 if (waiting_on_dns_.is_empty()) |
417 return; | 423 return; |
418 if (!HasHostBeenLookedUpLocked(waiting_on_dns_)) | 424 if (!HasHostBeenLookedUpLocked(waiting_on_dns_)) |
419 return; | 425 return; |
420 DCHECK(dns_run_loop_); | 426 DCHECK(dns_run_loop_); |
421 DCHECK(task_runner_); | 427 DCHECK(task_runner_); |
422 waiting_on_dns_ = GURL(); | 428 waiting_on_dns_ = GURL(); |
423 task_runner_->PostTask(FROM_HERE, dns_run_loop_->QuitClosure()); | 429 task_runner_->PostTask(FROM_HERE, dns_run_loop_->QuitClosure()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 464 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
459 | 465 |
460 // Protects all following members. They are read and updated from different | 466 // Protects all following members. They are read and updated from different |
461 // threads. | 467 // threads. |
462 base::Lock lock_; | 468 base::Lock lock_; |
463 | 469 |
464 int cross_site_learned_; | 470 int cross_site_learned_; |
465 int cross_site_preconnected_; | 471 int cross_site_preconnected_; |
466 int same_site_preconnected_; | 472 int same_site_preconnected_; |
467 | 473 |
| 474 std::set<GURL> preconnect_url_attempts_; |
468 std::set<GURL> successful_dns_lookups_; | 475 std::set<GURL> successful_dns_lookups_; |
469 std::set<GURL> unsuccessful_dns_lookups_; | 476 std::set<GURL> unsuccessful_dns_lookups_; |
470 base::RunLoop* dns_run_loop_; | 477 base::RunLoop* dns_run_loop_; |
471 | 478 |
472 // This member can be set to optionally allow url learning other than from | 479 // This member can be set to optionally allow url learning other than from |
473 // source => source, source => target, or target => target. It will also allow | 480 // source => source, source => target, or target => target. It will also allow |
474 // preconnects to other hosts. | 481 // preconnects to other hosts. |
475 bool strict_; | 482 bool strict_; |
476 | 483 |
477 DISALLOW_COPY_AND_ASSIGN(CrossSitePredictorObserver); | 484 DISALLOW_COPY_AND_ASSIGN(CrossSitePredictorObserver); |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
868 } | 875 } |
869 | 876 |
870 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, SimplePreconnectFour) { | 877 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, SimplePreconnectFour) { |
871 predictor()->PreconnectUrl( | 878 predictor()->PreconnectUrl( |
872 embedded_test_server()->base_url(), GURL(), | 879 embedded_test_server()->base_url(), GURL(), |
873 UrlInfo::ResolutionMotivation::EARLY_LOAD_MOTIVATED, | 880 UrlInfo::ResolutionMotivation::EARLY_LOAD_MOTIVATED, |
874 false /* allow credentials */, 4); | 881 false /* allow credentials */, 4); |
875 connection_listener_->WaitForAcceptedConnectionsOnUI(4u); | 882 connection_listener_->WaitForAcceptedConnectionsOnUI(4u); |
876 } | 883 } |
877 | 884 |
| 885 // Regression test for crbug.com/721981. |
| 886 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PreconnectNonHttpScheme) { |
| 887 GURL url("chrome-native://dummyurl"); |
| 888 predictor()->PreconnectUrlAndSubresources(url, GURL()); |
| 889 base::RunLoop().RunUntilIdle(); |
| 890 // Since |url| is non-HTTP(s) scheme, Predictor will canonicalize it to an |
| 891 // empty url. Make sure that there is no attempt to preconnect |url| or an |
| 892 // empty url. |
| 893 EXPECT_FALSE(observer()->HasHostAttemptedToPreconnect(url)); |
| 894 EXPECT_FALSE(observer()->HasHostAttemptedToPreconnect(GURL())); |
| 895 } |
| 896 |
878 // Test the html test harness used to initiate cross site fetches. These | 897 // Test the html test harness used to initiate cross site fetches. These |
879 // initiate cross site subresource requests to the cross site test server. | 898 // initiate cross site subresource requests to the cross site test server. |
880 // Inspect the predictor's internal state to make sure that they are properly | 899 // Inspect the predictor's internal state to make sure that they are properly |
881 // logged. | 900 // logged. |
882 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, CrossSiteUseOneSocket) { | 901 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, CrossSiteUseOneSocket) { |
883 StopInterceptingCrossSiteOnUI(); | 902 StopInterceptingCrossSiteOnUI(); |
884 NavigateToCrossSiteHtmlUrl(1 /* num_cors */, "" /* file_suffix */); | 903 NavigateToCrossSiteHtmlUrl(1 /* num_cors */, "" /* file_suffix */); |
885 EXPECT_EQ(1u, cross_site_connection_listener_->GetAcceptedSocketCount()); | 904 EXPECT_EQ(1u, cross_site_connection_listener_->GetAcceptedSocketCount()); |
886 EXPECT_EQ(1, observer()->CrossSiteLearned()); | 905 EXPECT_EQ(1, observer()->CrossSiteLearned()); |
887 EXPECT_EQ(2, observer()->SameSitePreconnected()); | 906 EXPECT_EQ(2, observer()->SameSitePreconnected()); |
(...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1565 // Second navigation to content with an img. | 1584 // Second navigation to content with an img. |
1566 std::string img_content = | 1585 std::string img_content = |
1567 "<img src=\"" + preconnect_url.spec() + "test.gif\">"; | 1586 "<img src=\"" + preconnect_url.spec() + "test.gif\">"; |
1568 NavigateToDataURLWithContent(img_content); | 1587 NavigateToDataURLWithContent(img_content); |
1569 connection_listener_->WaitUntilFirstConnectionRead(); | 1588 connection_listener_->WaitUntilFirstConnectionRead(); |
1570 EXPECT_EQ(2u, connection_listener_->GetAcceptedSocketCount()); | 1589 EXPECT_EQ(2u, connection_listener_->GetAcceptedSocketCount()); |
1571 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount()); | 1590 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount()); |
1572 } | 1591 } |
1573 | 1592 |
1574 } // namespace chrome_browser_net | 1593 } // namespace chrome_browser_net |
OLD | NEW |