| 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 <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 base::RunLoop run_loop_; | 251 base::RunLoop run_loop_; |
| 252 size_t url_visit_count_; | 252 size_t url_visit_count_; |
| 253 PageRequestSummary summary_; | 253 PageRequestSummary summary_; |
| 254 bool match_navigation_id_; | 254 bool match_navigation_id_; |
| 255 std::set<NavigationID> current_navigation_ids_; | 255 std::set<NavigationID> current_navigation_ids_; |
| 256 | 256 |
| 257 DISALLOW_COPY_AND_ASSIGN(LearningObserver); | 257 DISALLOW_COPY_AND_ASSIGN(LearningObserver); |
| 258 }; | 258 }; |
| 259 | 259 |
| 260 // Helper class to track and allow waiting for a single OnPrefetchingFinished | 260 // Helper class to track and allow waiting for a single OnPrefetchingFinished |
| 261 // event. No learning events should be fired while this observer is active. | 261 // event. Checks also that {Start,Stop}Prefetching are called with the right |
| 262 // argument. |
| 262 class PrefetchingObserver : public TestObserver { | 263 class PrefetchingObserver : public TestObserver { |
| 263 public: | 264 public: |
| 264 PrefetchingObserver(ResourcePrefetchPredictor* predictor, | 265 PrefetchingObserver(ResourcePrefetchPredictor* predictor, |
| 265 const GURL& expected_main_frame_url) | 266 const GURL& expected_main_frame_url, |
| 266 : TestObserver(predictor), main_frame_url_(expected_main_frame_url) {} | 267 bool is_learning_allowed) |
| 268 : TestObserver(predictor), |
| 269 main_frame_url_(expected_main_frame_url), |
| 270 is_learning_allowed_(is_learning_allowed) {} |
| 267 | 271 |
| 268 // TestObserver: | 272 // TestObserver: |
| 269 void OnNavigationLearned(size_t url_visit_count, | 273 void OnNavigationLearned(size_t url_visit_count, |
| 270 const PageRequestSummary& summary) override { | 274 const PageRequestSummary& summary) override { |
| 271 ADD_FAILURE() << "Prefetching shouldn't activate learning"; | 275 if (!is_learning_allowed_) |
| 276 ADD_FAILURE() << "Prefetching shouldn't activate learning"; |
| 277 } |
| 278 |
| 279 void OnPrefetchingStarted(const GURL& main_frame_url) override { |
| 280 EXPECT_EQ(main_frame_url_, main_frame_url); |
| 281 } |
| 282 |
| 283 void OnPrefetchingStopped(const GURL& main_frame_url) override { |
| 284 EXPECT_EQ(main_frame_url_, main_frame_url); |
| 272 } | 285 } |
| 273 | 286 |
| 274 void OnPrefetchingFinished(const GURL& main_frame_url) override { | 287 void OnPrefetchingFinished(const GURL& main_frame_url) override { |
| 275 EXPECT_EQ(main_frame_url_, main_frame_url); | 288 EXPECT_EQ(main_frame_url_, main_frame_url); |
| 276 run_loop_.Quit(); | 289 run_loop_.Quit(); |
| 277 } | 290 } |
| 278 | 291 |
| 279 void Wait() { run_loop_.Run(); } | 292 void Wait() { run_loop_.Run(); } |
| 280 | 293 |
| 281 private: | 294 private: |
| 282 base::RunLoop run_loop_; | 295 base::RunLoop run_loop_; |
| 283 GURL main_frame_url_; | 296 GURL main_frame_url_; |
| 297 bool is_learning_allowed_; |
| 284 | 298 |
| 285 DISALLOW_COPY_AND_ASSIGN(PrefetchingObserver); | 299 DISALLOW_COPY_AND_ASSIGN(PrefetchingObserver); |
| 286 }; | 300 }; |
| 287 | 301 |
| 288 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { | 302 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { |
| 289 protected: | 303 protected: |
| 290 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 304 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
| 291 | 305 |
| 292 void SetUpCommandLine(base::CommandLine* command_line) override { | 306 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 293 command_line->AppendSwitchASCII("force-fieldtrials", "trial/group"); | 307 command_line->AppendSwitchASCII("force-fieldtrials", "trial/group"); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 observer.Wait(); | 388 observer.Wait(); |
| 375 | 389 |
| 376 for (auto& kv : resources_) { | 390 for (auto& kv : resources_) { |
| 377 if (kv.second.is_observable) | 391 if (kv.second.is_observable) |
| 378 kv.second.request.was_cached = true; | 392 kv.second.request.was_cached = true; |
| 379 } | 393 } |
| 380 for (const auto& nav : observer.current_navigation_ids()) | 394 for (const auto& nav : observer.current_navigation_ids()) |
| 381 navigation_id_history_.insert(nav); | 395 navigation_id_history_.insert(nav); |
| 382 } | 396 } |
| 383 | 397 |
| 398 void NavigateToURLAndCheckPrefetching(const GURL& main_frame_url) { |
| 399 PrefetchingObserver observer(predictor_, main_frame_url, true); |
| 400 ui_test_utils::NavigateToURL(browser(), main_frame_url); |
| 401 observer.Wait(); |
| 402 for (auto& kv : resources_) { |
| 403 if (kv.second.is_observable) |
| 404 kv.second.request.was_cached = true; |
| 405 } |
| 406 } |
| 407 |
| 384 void PrefetchURL(const GURL& main_frame_url) { | 408 void PrefetchURL(const GURL& main_frame_url) { |
| 385 PrefetchingObserver observer(predictor_, main_frame_url); | 409 PrefetchingObserver observer(predictor_, main_frame_url, false); |
| 386 predictor_->StartPrefetching(main_frame_url, PrefetchOrigin::EXTERNAL); | 410 predictor_->StartPrefetching(main_frame_url, PrefetchOrigin::EXTERNAL); |
| 387 observer.Wait(); | 411 observer.Wait(); |
| 388 for (auto& kv : resources_) { | 412 for (auto& kv : resources_) { |
| 389 if (kv.second.is_observable) | 413 if (kv.second.is_observable) |
| 390 kv.second.request.was_cached = true; | 414 kv.second.request.was_cached = true; |
| 391 } | 415 } |
| 392 } | 416 } |
| 393 | 417 |
| 394 void TryToPrefetchURL(const GURL& main_frame_url) { | 418 void TryToPrefetchURL(const GURL& main_frame_url) { |
| 395 predictor_->StartPrefetching(main_frame_url, PrefetchOrigin::EXTERNAL); | 419 predictor_->StartPrefetching(main_frame_url, PrefetchOrigin::EXTERNAL); |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 } | 654 } |
| 631 | 655 |
| 632 ResourcePrefetchPredictor* predictor_; | 656 ResourcePrefetchPredictor* predictor_; |
| 633 std::unique_ptr<net::EmbeddedTestServer> https_server_; | 657 std::unique_ptr<net::EmbeddedTestServer> https_server_; |
| 634 std::map<GURL, ResourceSummary> resources_; | 658 std::map<GURL, ResourceSummary> resources_; |
| 635 std::map<GURL, RedirectEdge> redirects_; | 659 std::map<GURL, RedirectEdge> redirects_; |
| 636 std::map<GURL, size_t> visit_count_; | 660 std::map<GURL, size_t> visit_count_; |
| 637 std::set<NavigationID> navigation_id_history_; | 661 std::set<NavigationID> navigation_id_history_; |
| 638 }; | 662 }; |
| 639 | 663 |
| 664 // Subclass to test PrefetchOrigin::NAVIGATION. |
| 665 class ResourcePrefetchPredictorPrefetchingBrowserTest |
| 666 : public ResourcePrefetchPredictorBrowserTest { |
| 667 protected: |
| 668 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 669 command_line->AppendSwitchASCII("force-fieldtrials", "trial/group"); |
| 670 std::string parameter = base::StringPrintf( |
| 671 "trial.group:%s/%s", kModeParamName, kPrefetchingMode); |
| 672 command_line->AppendSwitchASCII("force-fieldtrial-params", parameter); |
| 673 std::string enabled_feature = base::StringPrintf( |
| 674 "%s<trial", kSpeculativeResourcePrefetchingFeatureName); |
| 675 command_line->AppendSwitchASCII("enable-features", enabled_feature); |
| 676 } |
| 677 }; |
| 678 |
| 640 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) { | 679 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) { |
| 641 // These resources have default priorities that correspond to | 680 // These resources have default priorities that correspond to |
| 642 // blink::typeToPriority function. | 681 // blink::typeToPriority function. |
| 643 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 682 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
| 644 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 683 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
| 645 net::HIGHEST); | 684 net::HIGHEST); |
| 646 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 685 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
| 647 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | 686 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, |
| 648 net::HIGHEST); | 687 net::HIGHEST); |
| 649 TestLearningAndPrefetching(GetURL(kHtmlSubresourcesPath)); | 688 TestLearningAndPrefetching(GetURL(kHtmlSubresourcesPath)); |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 892 // the predictor database corresponding the client-side redirect. | 931 // the predictor database corresponding the client-side redirect. |
| 893 TryToPrefetchURL(initial_url); | 932 TryToPrefetchURL(initial_url); |
| 894 NavigateToURLAndCheckSubresources(initial_url); | 933 NavigateToURLAndCheckSubresources(initial_url); |
| 895 ClearCache(); | 934 ClearCache(); |
| 896 // But the predictor database contains all subresources for the endpoint url | 935 // But the predictor database contains all subresources for the endpoint url |
| 897 // so this prefetch works. | 936 // so this prefetch works. |
| 898 PrefetchURL(GetURL(kHtmlSubresourcesPath)); | 937 PrefetchURL(GetURL(kHtmlSubresourcesPath)); |
| 899 NavigateToURLAndCheckSubresourcesAllCached(GetURL(kHtmlSubresourcesPath)); | 938 NavigateToURLAndCheckSubresourcesAllCached(GetURL(kHtmlSubresourcesPath)); |
| 900 } | 939 } |
| 901 | 940 |
| 941 // Makes sure that {Stop,Start}Prefetching are called with the same argument. |
| 942 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorPrefetchingBrowserTest, |
| 943 Simple) { |
| 944 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
| 945 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
| 946 net::HIGHEST); |
| 947 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
| 948 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, |
| 949 net::HIGHEST); |
| 950 |
| 951 GURL main_frame_url = GetURL(kHtmlSubresourcesPath); |
| 952 NavigateToURLAndCheckSubresources(main_frame_url); |
| 953 ClearCache(); |
| 954 NavigateToURLAndCheckSubresources(main_frame_url); |
| 955 ClearCache(); |
| 956 NavigateToURLAndCheckPrefetching(main_frame_url); |
| 957 } |
| 958 |
| 959 // Makes sure that {Stop,Start}Prefetching are called with the same argument in |
| 960 // presence of redirect. |
| 961 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorPrefetchingBrowserTest, |
| 962 Redirect) { |
| 963 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); |
| 964 AddRedirectChain(initial_url, {{net::HTTP_MOVED_PERMANENTLY, |
| 965 GetURL(kHtmlSubresourcesPath)}}); |
| 966 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
| 967 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
| 968 net::HIGHEST); |
| 969 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
| 970 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, |
| 971 net::HIGHEST); |
| 972 |
| 973 NavigateToURLAndCheckSubresources(initial_url); |
| 974 ClearCache(); |
| 975 NavigateToURLAndCheckSubresources(initial_url); |
| 976 ClearCache(); |
| 977 NavigateToURLAndCheckPrefetching(initial_url); |
| 978 } |
| 979 |
| 902 } // namespace predictors | 980 } // namespace predictors |
| OLD | NEW |