Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 | 6 |
| 7 #include <algorithm> | |
| 7 #include <set> | 8 #include <set> |
| 8 | 9 |
| 9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 12 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
| 13 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 14 #include "chrome/browser/browsing_data/browsing_data_helper.h" |
| 14 #include "chrome/browser/browsing_data/browsing_data_remover.h" | 15 #include "chrome/browser/browsing_data/browsing_data_remover.h" |
| 15 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" | 16 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" |
| 16 #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" | 17 #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" |
| 17 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 18 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 18 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" | 19 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" |
| 19 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | 20 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 23 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
| 24 #include "chrome/test/base/in_process_browser_test.h" | 25 #include "chrome/test/base/in_process_browser_test.h" |
| 25 #include "chrome/test/base/ui_test_utils.h" | 26 #include "chrome/test/base/ui_test_utils.h" |
| 26 #include "net/base/host_port_pair.h" | 27 #include "net/base/host_port_pair.h" |
| 28 #include "net/base/url_util.h" | |
| 27 #include "net/dns/mock_host_resolver.h" | 29 #include "net/dns/mock_host_resolver.h" |
| 28 #include "net/test/embedded_test_server/http_request.h" | 30 #include "net/test/embedded_test_server/http_request.h" |
| 29 #include "net/test/embedded_test_server/http_response.h" | 31 #include "net/test/embedded_test_server/http_response.h" |
| 32 #include "net/test/embedded_test_server/request_handler_util.h" | |
| 30 #include "testing/gmock/include/gmock/gmock.h" | 33 #include "testing/gmock/include/gmock/gmock.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 35 |
| 33 namespace predictors { | 36 namespace predictors { |
| 34 | 37 |
| 35 namespace { | 38 namespace { |
| 36 | 39 |
| 40 const char kLocalHost[] = "127.0.0.1"; | |
| 37 const char kFooHost[] = "foo.com"; | 41 const char kFooHost[] = "foo.com"; |
| 38 const char kBarHost[] = "bar.com"; | 42 const char kBarHost[] = "bar.com"; |
| 39 const char kBazHost[] = "baz.com"; | 43 const char kBazHost[] = "baz.com"; |
| 40 | 44 |
| 41 const char kImageMime[] = "image/png"; | 45 const char kImageMime[] = "image/png"; |
| 42 const char kStyleMime[] = "text/css"; | 46 const char kStyleMime[] = "text/css"; |
| 43 const char kJavascriptMime[] = "application/javascript"; | 47 const char kJavascriptMime[] = "application/javascript"; |
| 44 | 48 |
| 45 // Paths to resources handled by a custom request handler. They return empty | 49 // Paths to resources handled by a custom request handler. They return empty |
| 46 // responses with controllable response headers. | 50 // responses with controllable response headers. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 62 const char kHtmlAppendChildPath[] = "/predictors/append_child.html"; | 66 const char kHtmlAppendChildPath[] = "/predictors/append_child.html"; |
| 63 const char kScriptAppendChildPath[] = "/predictors/append_child.js"; | 67 const char kScriptAppendChildPath[] = "/predictors/append_child.js"; |
| 64 const char kHtmlInnerHtmlPath[] = "/predictors/inner_html.html"; | 68 const char kHtmlInnerHtmlPath[] = "/predictors/inner_html.html"; |
| 65 const char kScriptInnerHtmlPath[] = "/predictors/inner_html.js"; | 69 const char kScriptInnerHtmlPath[] = "/predictors/inner_html.js"; |
| 66 const char kHtmlXHRPath[] = "/predictors/xhr.html"; | 70 const char kHtmlXHRPath[] = "/predictors/xhr.html"; |
| 67 const char kScriptXHRPath[] = "/predictors/xhr.js"; | 71 const char kScriptXHRPath[] = "/predictors/xhr.js"; |
| 68 const char kHtmlIframePath[] = "/predictors/html_iframe.html"; | 72 const char kHtmlIframePath[] = "/predictors/html_iframe.html"; |
| 69 const char kHtmlJavascriptRedirectPath[] = | 73 const char kHtmlJavascriptRedirectPath[] = |
| 70 "/predictors/javascript_redirect.html"; | 74 "/predictors/javascript_redirect.html"; |
| 71 | 75 |
| 76 const std::pair<std::string, std::string> kScript = {kFooHost, kScriptPath}; | |
| 77 const std::pair<std::string, std::string> kImage = {kBarHost, kImagePath}; | |
| 78 const std::pair<std::string, std::string> kFont = {kFooHost, kFontPath}; | |
| 79 const std::pair<std::string, std::string> kStyle = {kBazHost, kStylePath}; | |
|
alexilin
2017/04/19 09:31:06
The compilation error (warning, actually) is a MSV
Benoit L
2017/04/19 12:59:21
Done.
| |
| 80 | |
| 72 struct ResourceSummary { | 81 struct ResourceSummary { |
| 73 ResourceSummary() | 82 ResourceSummary() |
| 74 : version(0), | 83 : version(0), |
| 75 is_no_store(false), | 84 is_no_store(false), |
| 76 is_external(false), | 85 is_external(false), |
| 77 is_observable(true), | 86 is_observable(true), |
| 78 is_prohibited(false) {} | 87 is_prohibited(false) {} |
| 79 | 88 |
| 80 ResourcePrefetchPredictor::URLRequestSummary request; | 89 ResourcePrefetchPredictor::URLRequestSummary request; |
| 81 // Allows to update HTTP ETag. | 90 // Allows to update HTTP ETag. |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 command_line->AppendSwitchASCII("force-fieldtrial-params", parameter); | 320 command_line->AppendSwitchASCII("force-fieldtrial-params", parameter); |
| 312 std::string enabled_feature = base::StringPrintf( | 321 std::string enabled_feature = base::StringPrintf( |
| 313 "%s<trial", kSpeculativeResourcePrefetchingFeatureName); | 322 "%s<trial", kSpeculativeResourcePrefetchingFeatureName); |
| 314 command_line->AppendSwitchASCII("enable-features", enabled_feature); | 323 command_line->AppendSwitchASCII("enable-features", enabled_feature); |
| 315 } | 324 } |
| 316 | 325 |
| 317 void SetUpOnMainThread() override { | 326 void SetUpOnMainThread() override { |
| 318 // Resolving all hosts to local allows us to have | 327 // Resolving all hosts to local allows us to have |
| 319 // cross domains navigations (matching url_visit_count_, etc). | 328 // cross domains navigations (matching url_visit_count_, etc). |
| 320 host_resolver()->AddRule("*", "127.0.0.1"); | 329 host_resolver()->AddRule("*", "127.0.0.1"); |
| 321 embedded_test_server()->RegisterRequestHandler( | 330 |
| 322 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, | 331 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( |
| 323 base::Unretained(this))); | 332 net::EmbeddedTestServer::TYPE_HTTPS); |
| 324 embedded_test_server()->RegisterRequestHandler( | 333 |
| 325 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | 334 for (auto* server : {embedded_test_server(), https_server()}) { |
| 326 base::Unretained(this))); | 335 server->AddDefaultHandlers( |
| 327 embedded_test_server()->RegisterRequestMonitor(base::Bind( | 336 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); |
| 328 &ResourcePrefetchPredictorBrowserTest::MonitorResourceRequest, | 337 server->RegisterRequestHandler(base::Bind( |
| 329 base::Unretained(this))); | 338 &ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, |
| 330 ASSERT_TRUE(embedded_test_server()->Start()); | 339 base::Unretained(this))); |
| 340 server->RegisterRequestHandler(base::Bind( | |
| 341 &ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | |
| 342 base::Unretained(this))); | |
| 343 server->RegisterRequestMonitor(base::Bind( | |
| 344 &ResourcePrefetchPredictorBrowserTest::MonitorResourceRequest, | |
| 345 base::Unretained(this))); | |
| 346 ASSERT_TRUE(server->Start()); | |
| 347 } | |
| 348 | |
| 331 predictor_ = | 349 predictor_ = |
| 332 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); | 350 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); |
| 333 ASSERT_TRUE(predictor_); | 351 ASSERT_TRUE(predictor_); |
| 334 // URLs from the test server contain a port number. | 352 // URLs from the test server contain a port number. |
| 335 ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(true); | 353 ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(true); |
| 336 EnsurePredictorInitialized(); | 354 EnsurePredictorInitialized(); |
| 337 histogram_tester_.reset(new base::HistogramTester()); | 355 histogram_tester_.reset(new base::HistogramTester()); |
| 338 } | 356 } |
| 339 | 357 |
| 340 void TearDownOnMainThread() override { | 358 void TearDownOnMainThread() override { |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 477 BrowsingDataRemoverObserver observer(remover); | 495 BrowsingDataRemoverObserver observer(remover); |
| 478 remover->RemoveAndReply( | 496 remover->RemoveAndReply( |
| 479 base::Time(), base::Time::Max(), BrowsingDataRemover::DATA_TYPE_CACHE, | 497 base::Time(), base::Time::Max(), BrowsingDataRemover::DATA_TYPE_CACHE, |
| 480 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB, &observer); | 498 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB, &observer); |
| 481 observer.Wait(); | 499 observer.Wait(); |
| 482 | 500 |
| 483 for (auto& kv : resources_) | 501 for (auto& kv : resources_) |
| 484 kv.second.request.was_cached = false; | 502 kv.second.request.was_cached = false; |
| 485 } | 503 } |
| 486 | 504 |
| 487 // Shortcut for convenience. | 505 // Shortcuts for convenience. |
| 488 GURL GetURL(const std::string& path) const { | 506 GURL GetURL(const std::string& path) const { |
| 489 return embedded_test_server()->GetURL(path); | 507 return embedded_test_server()->GetURL(path); |
| 490 } | 508 } |
| 491 | 509 |
| 492 void EnableHttpsServer() { | 510 GURL GetURLWithHost(const std::string& host, |
| 493 ASSERT_FALSE(https_server_); | 511 const std::string& path, |
| 494 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( | 512 bool https = false) const { |
| 495 net::EmbeddedTestServer::TYPE_HTTPS); | 513 auto* server = https ? https_server() : embedded_test_server(); |
| 496 https_server()->AddDefaultHandlers( | 514 return server->GetURL(host, path); |
| 497 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | |
| 498 https_server()->RegisterRequestHandler( | |
| 499 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, | |
| 500 base::Unretained(this))); | |
| 501 https_server()->RegisterRequestHandler( | |
| 502 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | |
| 503 base::Unretained(this))); | |
| 504 https_server()->RegisterRequestMonitor(base::Bind( | |
| 505 &ResourcePrefetchPredictorBrowserTest::MonitorResourceRequest, | |
| 506 base::Unretained(this))); | |
| 507 ASSERT_TRUE(https_server()->Start()); | |
| 508 } | 515 } |
| 509 | 516 |
| 510 // Returns the embedded test server working over HTTPS. Must be enabled by | 517 GURL GetSubresource( |
| 511 // calling EnableHttpsServer() before use. | 518 const std::pair<std::string, std::string>& host_path) const { |
| 519 return GetURLWithHost(host_path.first, host_path.second); | |
| 520 } | |
| 521 | |
| 522 std::string GetPathWithReplacements(const std::string& path, | |
| 523 bool https) const { | |
|
alexilin
2017/04/19 09:31:07
unused parameter
Probably you wanted to use it as
Benoit L
2017/04/19 12:59:21
That was the plan, yes, you guessed what was the p
| |
| 524 std::string port = base::StringPrintf("%d", embedded_test_server()->port()); | |
| 525 std::string path_with_replacements; | |
| 526 net::test_server::GetFilePathWithReplacements( | |
| 527 path, {{"REPLACE_WITH_PORT", port}}, &path_with_replacements); | |
| 528 return path_with_replacements; | |
| 529 } | |
| 530 | |
| 531 GURL GetPageURLWithReplacements(const std::string& host, | |
|
alexilin
2017/04/19 09:31:07
Add a warning comment that the result URL is only
Benoit L
2017/04/19 12:59:21
Done.
| |
| 532 const std::string& path, | |
| 533 bool https = false) const { | |
| 534 std::string path_with_replacements = GetPathWithReplacements(path, https); | |
|
alexilin
2017/04/19 09:31:07
just nit:
Scheme also could be set with replacemen
Benoit L
2017/04/19 12:59:21
As above...
| |
| 535 return GetURLWithHost(host, path_with_replacements, https); | |
| 536 } | |
| 537 | |
| 538 // Returns the embedded test server working over HTTPS. | |
| 512 const net::EmbeddedTestServer* https_server() const { | 539 const net::EmbeddedTestServer* https_server() const { |
| 513 return https_server_.get(); | 540 return https_server_.get(); |
| 514 } | 541 } |
| 515 | 542 |
| 516 net::EmbeddedTestServer* https_server() { return https_server_.get(); } | 543 net::EmbeddedTestServer* https_server() { return https_server_.get(); } |
| 517 | 544 |
| 518 size_t navigation_ids_history_size() const { | 545 size_t navigation_ids_history_size() const { |
| 519 return navigation_id_history_.size(); | 546 return navigation_id_history_.size(); |
| 520 } | 547 } |
| 521 | 548 |
| 549 // Expects the resources from kHtmlSubresourcesPath. | |
| 550 std::vector<ResourceSummary*> AddResourcesFromSubresourceHtml() { | |
| 551 // These resources have default priorities that correspond to | |
| 552 // blink::TypeToPriority(). | |
| 553 return {AddResource(GetSubresource(kImage), content::RESOURCE_TYPE_IMAGE, | |
| 554 net::LOWEST), | |
| 555 AddResource(GetSubresource(kStyle), | |
| 556 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST), | |
| 557 AddResource(GetSubresource(kScript), content::RESOURCE_TYPE_SCRIPT, | |
| 558 net::MEDIUM), | |
| 559 AddResource(GetSubresource(kFont), | |
| 560 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST)}; | |
| 561 } | |
| 562 | |
| 522 std::unique_ptr<base::HistogramTester> histogram_tester_; | 563 std::unique_ptr<base::HistogramTester> histogram_tester_; |
| 523 | 564 |
| 524 private: | 565 private: |
| 525 // ResourcePrefetchPredictor needs to be initialized before the navigation | 566 // ResourcePrefetchPredictor needs to be initialized before the navigation |
| 526 // happens otherwise this navigation will be ignored by predictor. | 567 // happens otherwise this navigation will be ignored by predictor. |
| 527 void EnsurePredictorInitialized() { | 568 void EnsurePredictorInitialized() { |
| 528 if (predictor_->initialization_state_ == | 569 if (predictor_->initialization_state_ == |
| 529 ResourcePrefetchPredictor::INITIALIZED) { | 570 ResourcePrefetchPredictor::INITIALIZED) { |
| 530 return; | 571 return; |
| 531 } | 572 } |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 677 std::string parameter = base::StringPrintf( | 718 std::string parameter = base::StringPrintf( |
| 678 "trial.group:%s/%s", kModeParamName, kPrefetchingMode); | 719 "trial.group:%s/%s", kModeParamName, kPrefetchingMode); |
| 679 command_line->AppendSwitchASCII("force-fieldtrial-params", parameter); | 720 command_line->AppendSwitchASCII("force-fieldtrial-params", parameter); |
| 680 std::string enabled_feature = base::StringPrintf( | 721 std::string enabled_feature = base::StringPrintf( |
| 681 "%s<trial", kSpeculativeResourcePrefetchingFeatureName); | 722 "%s<trial", kSpeculativeResourcePrefetchingFeatureName); |
| 682 command_line->AppendSwitchASCII("enable-features", enabled_feature); | 723 command_line->AppendSwitchASCII("enable-features", enabled_feature); |
| 683 } | 724 } |
| 684 }; | 725 }; |
| 685 | 726 |
| 686 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) { | 727 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) { |
| 687 // These resources have default priorities that correspond to | 728 AddResourcesFromSubresourceHtml(); |
| 688 // blink::typeToPriority function. | 729 GURL url = GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath); |
| 689 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 730 TestLearningAndPrefetching(url); |
| 690 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
| 691 net::HIGHEST); | |
| 692 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 693 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
| 694 net::HIGHEST); | |
| 695 TestLearningAndPrefetching(GetURL(kHtmlSubresourcesPath)); | |
| 696 | 731 |
| 697 // The local cache is cleared. | 732 // The local cache is cleared. |
| 698 histogram_tester_->ExpectBucketCount( | 733 histogram_tester_->ExpectBucketCount( |
| 699 internal::kResourcePrefetchPredictorPrefetchMissesCountCached, 0, 1); | 734 internal::kResourcePrefetchPredictorPrefetchMissesCountCached, 0, 1); |
| 700 histogram_tester_->ExpectBucketCount( | 735 histogram_tester_->ExpectBucketCount( |
| 701 internal::kResourcePrefetchPredictorPrefetchMissesCountNotCached, 0, 1); | 736 internal::kResourcePrefetchPredictorPrefetchMissesCountNotCached, 0, 1); |
| 702 histogram_tester_->ExpectBucketCount( | 737 histogram_tester_->ExpectBucketCount( |
| 703 internal::kResourcePrefetchPredictorPrefetchHitsCountCached, 0, 1); | 738 internal::kResourcePrefetchPredictorPrefetchHitsCountCached, 0, 1); |
| 704 histogram_tester_->ExpectBucketCount( | 739 histogram_tester_->ExpectBucketCount( |
| 705 internal::kResourcePrefetchPredictorPrefetchHitsCountNotCached, 4, 1); | 740 internal::kResourcePrefetchPredictorPrefetchHitsCountNotCached, 4, 1); |
| 706 | 741 |
| 707 histogram_tester_->ExpectBucketCount( | 742 histogram_tester_->ExpectBucketCount( |
| 708 internal::kResourcePrefetchPredictorPrefetchMissesSize, 0, 1); | 743 internal::kResourcePrefetchPredictorPrefetchMissesSize, 0, 1); |
| 709 // Each request is ~1k, see HandleResourceRequest() above. | 744 // Each request is ~1k, see HandleResourceRequest() above. |
| 710 histogram_tester_->ExpectBucketCount( | 745 histogram_tester_->ExpectBucketCount( |
| 711 internal::kResourcePrefetchPredictorPrefetchHitsSize, 4, 1); | 746 internal::kResourcePrefetchPredictorPrefetchHitsSize, 4, 1); |
| 712 } | 747 } |
| 713 | 748 |
| 714 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Redirect) { | 749 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Redirect) { |
| 715 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); | 750 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); |
| 716 AddRedirectChain(initial_url, {{net::HTTP_MOVED_PERMANENTLY, | 751 GURL redirected_url = |
| 717 GetURL(kHtmlSubresourcesPath)}}); | 752 GetPageURLWithReplacements(kBarHost, kHtmlSubresourcesPath); |
| 718 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 753 AddRedirectChain(initial_url, |
| 719 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 754 {{net::HTTP_MOVED_PERMANENTLY, redirected_url}}); |
| 720 net::HIGHEST); | 755 AddResourcesFromSubresourceHtml(); |
| 721 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 722 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
| 723 net::HIGHEST); | |
| 724 TestLearningAndPrefetching(initial_url); | 756 TestLearningAndPrefetching(initial_url); |
| 725 } | 757 } |
| 726 | 758 |
| 727 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, RedirectChain) { | 759 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, RedirectChain) { |
| 728 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); | 760 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); |
| 761 GURL redirected_url = | |
| 762 GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath); | |
| 729 AddRedirectChain(initial_url, | 763 AddRedirectChain(initial_url, |
| 730 {{net::HTTP_FOUND, | 764 {{net::HTTP_FOUND, |
| 731 embedded_test_server()->GetURL(kBarHost, kRedirectPath2)}, | 765 embedded_test_server()->GetURL(kBarHost, kRedirectPath2)}, |
| 732 {net::HTTP_MOVED_PERMANENTLY, | 766 {net::HTTP_MOVED_PERMANENTLY, |
| 733 embedded_test_server()->GetURL(kBazHost, kRedirectPath3)}, | 767 embedded_test_server()->GetURL(kBazHost, kRedirectPath3)}, |
| 734 {net::HTTP_FOUND, GetURL(kHtmlSubresourcesPath)}}); | 768 {net::HTTP_FOUND, redirected_url}}); |
| 735 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 769 AddResourcesFromSubresourceHtml(); |
| 736 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
| 737 net::HIGHEST); | |
| 738 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 739 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
| 740 net::HIGHEST); | |
| 741 TestLearningAndPrefetching(initial_url); | 770 TestLearningAndPrefetching(initial_url); |
| 742 } | 771 } |
| 743 | 772 |
| 744 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 773 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
| 745 HttpToHttpsRedirect) { | 774 HttpToHttpsRedirect) { |
| 746 EnableHttpsServer(); | 775 GURL initial_url = GetURLWithHost(kFooHost, kRedirectPath); |
| 747 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); | 776 GURL redirected_url = |
| 777 GetPageURLWithReplacements(kLocalHost, kHtmlSubresourcesPath, true); | |
| 778 | |
| 779 // Only one resource, as HTTP images in HTTPS pages are only a warning, not | |
| 780 // an error. | |
| 781 AddResource(GetSubresource(kImage), content::RESOURCE_TYPE_IMAGE, | |
| 782 net::LOWEST); | |
| 783 | |
| 748 AddRedirectChain(initial_url, | 784 AddRedirectChain(initial_url, |
| 749 {{net::HTTP_MOVED_PERMANENTLY, | 785 {{net::HTTP_MOVED_PERMANENTLY, redirected_url}}); |
| 750 https_server()->GetURL(kHtmlSubresourcesPath)}}); | |
| 751 AddResource(https_server()->GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, | |
| 752 net::LOWEST); | |
| 753 AddResource(https_server()->GetURL(kStylePath), | |
| 754 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); | |
| 755 AddResource(https_server()->GetURL(kScriptPath), | |
| 756 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 757 AddResource(https_server()->GetURL(kFontPath), | |
| 758 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | |
| 759 TestLearningAndPrefetching(initial_url); | 786 TestLearningAndPrefetching(initial_url); |
| 760 } | 787 } |
| 761 | 788 |
| 762 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 789 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
| 763 JavascriptDocumentWrite) { | 790 JavascriptDocumentWrite) { |
| 764 auto* externalScript = | 791 auto* externalScript = |
| 765 AddExternalResource(GetURL(kScriptDocumentWritePath), | 792 AddExternalResource(GetURL(kScriptDocumentWritePath), |
| 766 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 793 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
| 767 externalScript->request.mime_type = kJavascriptMime; | 794 externalScript->request.mime_type = kJavascriptMime; |
| 768 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 795 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 828 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 855 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
| 829 // Included from <iframe src="html_subresources.html"> and shouldn't be | 856 // Included from <iframe src="html_subresources.html"> and shouldn't be |
| 830 // observed. | 857 // observed. |
| 831 AddUnobservableResources({GetURL(kImagePath), GetURL(kStylePath), | 858 AddUnobservableResources({GetURL(kImagePath), GetURL(kStylePath), |
| 832 GetURL(kScriptPath), GetURL(kFontPath)}); | 859 GetURL(kScriptPath), GetURL(kFontPath)}); |
| 833 TestLearningAndPrefetching(GetURL(kHtmlIframePath)); | 860 TestLearningAndPrefetching(GetURL(kHtmlIframePath)); |
| 834 } | 861 } |
| 835 | 862 |
| 836 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 863 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
| 837 CrossSiteNavigation) { | 864 CrossSiteNavigation) { |
| 838 AddResource(embedded_test_server()->GetURL(kFooHost, kImagePath), | 865 AddResourcesFromSubresourceHtml(); |
| 839 content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 866 GURL url = GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath); |
| 840 AddResource(embedded_test_server()->GetURL(kFooHost, kStylePath), | 867 TestLearningAndPrefetching(url); |
| 841 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); | 868 |
| 842 AddResource(embedded_test_server()->GetURL(kFooHost, kScriptPath), | |
| 843 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 844 AddResource(embedded_test_server()->GetURL(kFooHost, kFontPath), | |
| 845 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | |
| 846 TestLearningAndPrefetching( | |
| 847 embedded_test_server()->GetURL(kFooHost, kHtmlSubresourcesPath)); | |
| 848 ClearResources(); | 869 ClearResources(); |
| 849 | 870 ClearCache(); |
| 850 AddResource(embedded_test_server()->GetURL(kBarHost, kImagePath), | 871 AddResourcesFromSubresourceHtml(); |
| 851 content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
| 852 AddResource(embedded_test_server()->GetURL(kBarHost, kStylePath), | |
| 853 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); | |
| 854 AddResource(embedded_test_server()->GetURL(kBarHost, kScriptPath), | |
| 855 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 856 AddResource(embedded_test_server()->GetURL(kBarHost, kFontPath), | |
| 857 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | |
| 858 // Navigating to kBarHost, although done in the same tab, will generate a new | 872 // Navigating to kBarHost, although done in the same tab, will generate a new |
| 859 // process. | 873 // process. |
| 860 TestLearningAndPrefetching( | 874 url = GetPageURLWithReplacements(kBarHost, kHtmlSubresourcesPath); |
| 861 embedded_test_server()->GetURL(kBarHost, kHtmlSubresourcesPath)); | 875 TestLearningAndPrefetching(url); |
| 862 } | 876 } |
| 863 | 877 |
| 864 // In this test we are trying to assess if : | 878 // In this test we are trying to assess if : |
| 865 // - Reloading in the same tab is using the same NavigationID. | 879 // - Reloading in the same tab is using the same NavigationID. |
| 866 // - Navigation into a new tab, window or popup yields different NavigationID's. | 880 // - Navigation into a new tab, window or popup yields different NavigationID's. |
| 867 // - Navigating twice to a new browser/popup yields different NavigationID's. | 881 // - Navigating twice to a new browser/popup yields different NavigationID's. |
| 868 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 882 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
| 869 TabIdBehavingAsExpected) { | 883 TabIdBehavingAsExpected) { |
| 870 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 884 AddResourcesFromSubresourceHtml(); |
| 871 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 885 GURL url = GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath); |
| 872 net::HIGHEST); | 886 NavigateToURLAndCheckSubresources(url); |
| 873 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 874 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
| 875 net::HIGHEST); | |
| 876 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath)); | |
| 877 EXPECT_EQ(navigation_ids_history_size(), 1U); | 887 EXPECT_EQ(navigation_ids_history_size(), 1U); |
| 878 ClearCache(); | 888 ClearCache(); |
| 879 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath)); | 889 NavigateToURLAndCheckSubresources(url); |
| 880 EXPECT_EQ(navigation_ids_history_size(), 1U); | 890 EXPECT_EQ(navigation_ids_history_size(), 1U); |
| 881 ClearCache(); | 891 ClearCache(); |
| 882 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | 892 NavigateToURLAndCheckSubresources(url, |
| 883 WindowOpenDisposition::NEW_BACKGROUND_TAB); | 893 WindowOpenDisposition::NEW_BACKGROUND_TAB); |
| 884 EXPECT_EQ(navigation_ids_history_size(), 2U); | 894 EXPECT_EQ(navigation_ids_history_size(), 2U); |
| 885 ClearCache(); | 895 ClearCache(); |
| 886 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | 896 NavigateToURLAndCheckSubresources(url, WindowOpenDisposition::NEW_WINDOW); |
| 887 WindowOpenDisposition::NEW_WINDOW); | |
| 888 EXPECT_EQ(navigation_ids_history_size(), 3U); | 897 EXPECT_EQ(navigation_ids_history_size(), 3U); |
| 889 ClearCache(); | 898 ClearCache(); |
| 890 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | 899 NavigateToURLAndCheckSubresources(url, WindowOpenDisposition::NEW_POPUP); |
| 891 WindowOpenDisposition::NEW_POPUP); | |
| 892 EXPECT_EQ(navigation_ids_history_size(), 4U); | 900 EXPECT_EQ(navigation_ids_history_size(), 4U); |
| 893 } | 901 } |
| 894 | 902 |
| 895 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, AlwaysRevalidate) { | 903 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, AlwaysRevalidate) { |
| 896 std::vector<ResourceSummary*> resources = { | 904 auto resources = AddResourcesFromSubresourceHtml(); |
| 897 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, | 905 GURL url = GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath); |
| 898 net::LOWEST), | |
| 899 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
| 900 net::HIGHEST), | |
| 901 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, | |
| 902 net::MEDIUM), | |
| 903 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
| 904 net::HIGHEST), | |
| 905 }; | |
| 906 for (auto* resource : resources) | 906 for (auto* resource : resources) |
| 907 resource->request.always_revalidate = true; | 907 resource->request.always_revalidate = true; |
| 908 TestLearningAndPrefetching(GetURL(kHtmlSubresourcesPath)); | 908 TestLearningAndPrefetching(url); |
| 909 } | 909 } |
| 910 | 910 |
| 911 // Client-side redirects currently aren't tracked by ResourcePrefetchPredictor. | 911 // Client-side redirects currently aren't tracked by ResourcePrefetchPredictor. |
| 912 // A client-side redirect initiates a new navigation to the redirect destination | 912 // A client-side redirect initiates a new navigation to the redirect destination |
| 913 // URL and aborts the current navigation so that the OnLoad event is not fired. | 913 // URL and aborts the current navigation so that the OnLoad event is not fired. |
| 914 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 914 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
| 915 JavascriptRedirectsAreNotHandled) { | 915 JavascriptRedirectsAreNotHandled) { |
| 916 std::string redirect_path_with_query = | 916 GURL redirected_url = |
| 917 std::string(kHtmlJavascriptRedirectPath) + "?url=" + | 917 GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath); |
| 918 GetURL(kHtmlSubresourcesPath).spec(); | |
| 919 GURL initial_url = | 918 GURL initial_url = |
| 920 embedded_test_server()->GetURL(kBarHost, redirect_path_with_query); | 919 embedded_test_server()->GetURL(kBarHost, kHtmlJavascriptRedirectPath); |
| 921 AddRedirectChain(initial_url, {{net::HTTP_TEMPORARY_REDIRECT, | 920 initial_url = |
| 922 GetURL(kHtmlSubresourcesPath), true}}); | 921 net::AppendQueryParameter(initial_url, "url", redirected_url.spec()); |
| 923 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 922 |
| 924 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 923 AddRedirectChain(initial_url, |
| 925 net::HIGHEST); | 924 {{net::HTTP_TEMPORARY_REDIRECT, redirected_url, true}}); |
| 926 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 925 AddResourcesFromSubresourceHtml(); |
| 927 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
| 928 net::HIGHEST); | |
| 929 | 926 |
| 930 // Two navigations will occur. LearningObserver will get events only for the | 927 // Two navigations will occur. LearningObserver will get events only for the |
| 931 // second navigation because the first one will be aborted. | 928 // second navigation because the first one will be aborted. |
| 932 NavigateToURLAndCheckSubresources(initial_url); | 929 NavigateToURLAndCheckSubresources(initial_url); |
| 933 ClearCache(); | 930 ClearCache(); |
| 934 // It is needed to have at least two resource hits to trigger prefetch. | 931 // It is needed to have at least two resource hits to trigger prefetch. |
| 935 NavigateToURLAndCheckSubresources(initial_url); | 932 NavigateToURLAndCheckSubresources(initial_url); |
| 936 ClearCache(); | 933 ClearCache(); |
| 937 // Prefetching of |initial_url| has no effect because there is no entry in | 934 // Prefetching of |initial_url| has no effect because there is no entry in |
| 938 // the predictor database corresponding the client-side redirect. | 935 // the predictor database corresponding the client-side redirect. |
| 939 TryToPrefetchURL(initial_url); | 936 TryToPrefetchURL(initial_url); |
| 940 NavigateToURLAndCheckSubresources(initial_url); | 937 NavigateToURLAndCheckSubresources(initial_url); |
| 941 ClearCache(); | 938 ClearCache(); |
| 942 // But the predictor database contains all subresources for the endpoint url | 939 // But the predictor database contains all subresources for the endpoint url |
| 943 // so this prefetch works. | 940 // so this prefetch works. |
| 944 PrefetchURL(GetURL(kHtmlSubresourcesPath)); | 941 PrefetchURL(redirected_url); |
| 945 NavigateToURLAndCheckSubresourcesAllCached(GetURL(kHtmlSubresourcesPath)); | 942 NavigateToURLAndCheckSubresourcesAllCached(redirected_url); |
| 946 } | 943 } |
| 947 | 944 |
| 948 // Makes sure that {Stop,Start}Prefetching are called with the same argument. | 945 // Makes sure that {Stop,Start}Prefetching are called with the same argument. |
| 949 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorPrefetchingBrowserTest, | 946 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorPrefetchingBrowserTest, |
| 950 Simple) { | 947 Simple) { |
| 951 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 948 AddResourcesFromSubresourceHtml(); |
| 952 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
| 953 net::HIGHEST); | |
| 954 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 955 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
| 956 net::HIGHEST); | |
| 957 | 949 |
| 958 GURL main_frame_url = GetURL(kHtmlSubresourcesPath); | 950 GURL main_frame_url = |
| 951 GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath); | |
| 959 NavigateToURLAndCheckSubresources(main_frame_url); | 952 NavigateToURLAndCheckSubresources(main_frame_url); |
| 960 ClearCache(); | 953 ClearCache(); |
| 961 NavigateToURLAndCheckSubresources(main_frame_url); | 954 NavigateToURLAndCheckSubresources(main_frame_url); |
| 962 ClearCache(); | 955 ClearCache(); |
| 963 NavigateToURLAndCheckPrefetching(main_frame_url); | 956 NavigateToURLAndCheckPrefetching(main_frame_url); |
| 964 } | 957 } |
| 965 | 958 |
| 966 // Makes sure that {Stop,Start}Prefetching are called with the same argument in | 959 // Makes sure that {Stop,Start}Prefetching are called with the same argument in |
| 967 // presence of redirect. | 960 // presence of redirect. |
| 968 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorPrefetchingBrowserTest, | 961 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorPrefetchingBrowserTest, |
| 969 Redirect) { | 962 Redirect) { |
| 970 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); | 963 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); |
| 971 AddRedirectChain(initial_url, {{net::HTTP_MOVED_PERMANENTLY, | 964 GURL redirected_url = |
| 972 GetURL(kHtmlSubresourcesPath)}}); | 965 GetPageURLWithReplacements(kFooHost, kHtmlSubresourcesPath); |
|
alexilin
2017/04/19 09:31:06
s/kFooHost/kBarHost to preserve cross-host redirec
Benoit L
2017/04/19 12:59:21
Oops.
Done.
| |
| 973 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 966 AddRedirectChain(initial_url, |
| 974 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 967 {{net::HTTP_MOVED_PERMANENTLY, redirected_url}}); |
| 975 net::HIGHEST); | 968 AddResourcesFromSubresourceHtml(); |
| 976 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 977 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
| 978 net::HIGHEST); | |
| 979 | 969 |
| 980 NavigateToURLAndCheckSubresources(initial_url); | 970 NavigateToURLAndCheckSubresources(initial_url); |
| 981 ClearCache(); | 971 ClearCache(); |
| 982 NavigateToURLAndCheckSubresources(initial_url); | 972 NavigateToURLAndCheckSubresources(initial_url); |
| 983 ClearCache(); | 973 ClearCache(); |
| 984 NavigateToURLAndCheckPrefetching(initial_url); | 974 NavigateToURLAndCheckPrefetching(initial_url); |
| 985 } | 975 } |
| 986 | 976 |
| 987 } // namespace predictors | 977 } // namespace predictors |
| OLD | NEW |