| 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 <deque> | 6 #include <deque> |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <unordered_map> | 9 #include <unordered_map> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 #include "chrome/browser/net/prediction_options.h" | 42 #include "chrome/browser/net/prediction_options.h" |
| 43 #include "chrome/browser/predictors/autocomplete_action_predictor.h" | 43 #include "chrome/browser/predictors/autocomplete_action_predictor.h" |
| 44 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h" | 44 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h" |
| 45 #include "chrome/browser/prerender/prerender_contents.h" | 45 #include "chrome/browser/prerender/prerender_contents.h" |
| 46 #include "chrome/browser/prerender/prerender_field_trial.h" | 46 #include "chrome/browser/prerender/prerender_field_trial.h" |
| 47 #include "chrome/browser/prerender/prerender_handle.h" | 47 #include "chrome/browser/prerender/prerender_handle.h" |
| 48 #include "chrome/browser/prerender/prerender_link_manager.h" | 48 #include "chrome/browser/prerender/prerender_link_manager.h" |
| 49 #include "chrome/browser/prerender/prerender_link_manager_factory.h" | 49 #include "chrome/browser/prerender/prerender_link_manager_factory.h" |
| 50 #include "chrome/browser/prerender/prerender_manager.h" | 50 #include "chrome/browser/prerender/prerender_manager.h" |
| 51 #include "chrome/browser/prerender/prerender_manager_factory.h" | 51 #include "chrome/browser/prerender/prerender_manager_factory.h" |
| 52 #include "chrome/browser/prerender/prerender_test_utils.h" |
| 52 #include "chrome/browser/profiles/profile.h" | 53 #include "chrome/browser/profiles/profile.h" |
| 53 #include "chrome/browser/profiles/profile_io_data.h" | 54 #include "chrome/browser/profiles/profile_io_data.h" |
| 54 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.
h" | |
| 55 #include "chrome/browser/safe_browsing/local_database_manager.h" | |
| 56 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" | |
| 57 #include "chrome/browser/task_manager/mock_web_contents_task_manager.h" | 55 #include "chrome/browser/task_manager/mock_web_contents_task_manager.h" |
| 58 #include "chrome/browser/task_manager/providers/web_contents/web_contents_tags_m
anager.h" | 56 #include "chrome/browser/task_manager/providers/web_contents/web_contents_tags_m
anager.h" |
| 59 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" | 57 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" |
| 60 #include "chrome/browser/ui/browser.h" | 58 #include "chrome/browser/ui/browser.h" |
| 61 #include "chrome/browser/ui/browser_commands.h" | 59 #include "chrome/browser/ui/browser_commands.h" |
| 62 #include "chrome/browser/ui/browser_finder.h" | 60 #include "chrome/browser/ui/browser_finder.h" |
| 63 #include "chrome/browser/ui/browser_window.h" | 61 #include "chrome/browser/ui/browser_window.h" |
| 64 #include "chrome/browser/ui/location_bar/location_bar.h" | 62 #include "chrome/browser/ui/location_bar/location_bar.h" |
| 65 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 63 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 66 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" | 64 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" |
| 67 #include "chrome/common/chrome_paths.h" | 65 #include "chrome/common/chrome_paths.h" |
| 68 #include "chrome/common/chrome_switches.h" | 66 #include "chrome/common/chrome_switches.h" |
| 69 #include "chrome/common/pref_names.h" | |
| 70 #include "chrome/grit/generated_resources.h" | 67 #include "chrome/grit/generated_resources.h" |
| 71 #include "chrome/test/base/in_process_browser_test.h" | |
| 72 #include "chrome/test/base/ui_test_utils.h" | 68 #include "chrome/test/base/ui_test_utils.h" |
| 73 #include "components/content_settings/core/browser/host_content_settings_map.h" | 69 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 74 #include "components/favicon/content/content_favicon_driver.h" | 70 #include "components/favicon/content/content_favicon_driver.h" |
| 75 #include "components/favicon/core/favicon_driver_observer.h" | 71 #include "components/favicon/core/favicon_driver_observer.h" |
| 76 #include "components/omnibox/browser/omnibox_edit_model.h" | 72 #include "components/omnibox/browser/omnibox_edit_model.h" |
| 77 #include "components/omnibox/browser/omnibox_popup_model.h" | 73 #include "components/omnibox/browser/omnibox_popup_model.h" |
| 78 #include "components/omnibox/browser/omnibox_view.h" | 74 #include "components/omnibox/browser/omnibox_view.h" |
| 79 #include "components/prefs/pref_service.h" | |
| 80 #include "components/safe_browsing_db/database_manager.h" | 75 #include "components/safe_browsing_db/database_manager.h" |
| 81 #include "components/safe_browsing_db/test_database_manager.h" | |
| 82 #include "components/safe_browsing_db/util.h" | 76 #include "components/safe_browsing_db/util.h" |
| 83 #include "components/variations/entropy_provider.h" | 77 #include "components/variations/entropy_provider.h" |
| 84 #include "components/variations/variations_associated_data.h" | 78 #include "components/variations/variations_associated_data.h" |
| 85 #include "content/public/browser/browser_message_filter.h" | 79 #include "content/public/browser/browser_message_filter.h" |
| 86 #include "content/public/browser/devtools_agent_host.h" | 80 #include "content/public/browser/devtools_agent_host.h" |
| 87 #include "content/public/browser/navigation_controller.h" | 81 #include "content/public/browser/navigation_controller.h" |
| 88 #include "content/public/browser/navigation_entry.h" | 82 #include "content/public/browser/navigation_entry.h" |
| 89 #include "content/public/browser/notification_service.h" | 83 #include "content/public/browser/notification_service.h" |
| 90 #include "content/public/browser/render_frame_host.h" | 84 #include "content/public/browser/render_frame_host.h" |
| 91 #include "content/public/browser/render_process_host.h" | 85 #include "content/public/browser/render_process_host.h" |
| 92 #include "content/public/browser/render_view_host.h" | 86 #include "content/public/browser/render_view_host.h" |
| 93 #include "content/public/browser/site_instance.h" | 87 #include "content/public/browser/site_instance.h" |
| 94 #include "content/public/browser/web_contents.h" | 88 #include "content/public/browser/web_contents.h" |
| 95 #include "content/public/browser/web_contents_observer.h" | 89 #include "content/public/browser/web_contents_observer.h" |
| 96 #include "content/public/common/content_switches.h" | 90 #include "content/public/common/content_switches.h" |
| 97 #include "content/public/common/resource_request_body.h" | 91 #include "content/public/common/resource_request_body.h" |
| 98 #include "content/public/common/url_constants.h" | 92 #include "content/public/common/url_constants.h" |
| 99 #include "content/public/test/browser_test_utils.h" | 93 #include "content/public/test/browser_test_utils.h" |
| 100 #include "content/public/test/ppapi_test_utils.h" | |
| 101 #include "content/public/test/test_navigation_observer.h" | 94 #include "content/public/test/test_navigation_observer.h" |
| 102 #include "content/public/test/test_utils.h" | 95 #include "content/public/test/test_utils.h" |
| 103 #include "extensions/common/constants.h" | 96 #include "extensions/common/constants.h" |
| 104 #include "extensions/common/extension_urls.h" | 97 #include "extensions/common/extension_urls.h" |
| 105 #include "extensions/common/manifest_handlers/mime_types_handler.h" | 98 #include "extensions/common/manifest_handlers/mime_types_handler.h" |
| 106 #include "extensions/common/switches.h" | 99 #include "extensions/common/switches.h" |
| 107 #include "extensions/test/result_catcher.h" | 100 #include "extensions/test/result_catcher.h" |
| 108 #include "net/base/escape.h" | 101 #include "net/base/escape.h" |
| 109 #include "net/cert/x509_certificate.h" | 102 #include "net/cert/x509_certificate.h" |
| 110 #include "net/dns/mock_host_resolver.h" | 103 #include "net/dns/mock_host_resolver.h" |
| 111 #include "net/ssl/client_cert_store.h" | 104 #include "net/ssl/client_cert_store.h" |
| 112 #include "net/ssl/ssl_cert_request_info.h" | 105 #include "net/ssl/ssl_cert_request_info.h" |
| 113 #include "net/ssl/ssl_server_config.h" | 106 #include "net/ssl/ssl_server_config.h" |
| 114 #include "net/test/cert_test_util.h" | 107 #include "net/test/cert_test_util.h" |
| 115 #include "net/test/embedded_test_server/embedded_test_server.h" | 108 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 116 #include "net/test/embedded_test_server/request_handler_util.h" | 109 #include "net/test/embedded_test_server/request_handler_util.h" |
| 117 #include "net/test/test_data_directory.h" | 110 #include "net/test/test_data_directory.h" |
| 118 #include "net/test/url_request/url_request_mock_http_job.h" | |
| 119 #include "net/url_request/url_request_context.h" | 111 #include "net/url_request/url_request_context.h" |
| 120 #include "net/url_request/url_request_context_getter.h" | 112 #include "net/url_request/url_request_context_getter.h" |
| 121 #include "net/url_request/url_request_filter.h" | 113 #include "net/url_request/url_request_filter.h" |
| 122 #include "net/url_request/url_request_interceptor.h" | |
| 123 #include "net/url_request/url_request_job.h" | 114 #include "net/url_request/url_request_job.h" |
| 124 #include "ppapi/shared_impl/ppapi_switches.h" | |
| 125 #include "testing/gmock/include/gmock/gmock.h" | 115 #include "testing/gmock/include/gmock/gmock.h" |
| 126 #include "testing/gtest/include/gtest/gtest.h" | 116 #include "testing/gtest/include/gtest/gtest.h" |
| 127 #include "ui/base/l10n/l10n_util.h" | 117 #include "ui/base/l10n/l10n_util.h" |
| 128 #include "url/gurl.h" | 118 #include "url/gurl.h" |
| 129 | 119 |
| 130 using chrome_browser_net::NetworkPredictionOptions; | 120 using chrome_browser_net::NetworkPredictionOptions; |
| 131 using content::BrowserThread; | 121 using content::BrowserThread; |
| 132 using content::DevToolsAgentHost; | 122 using content::DevToolsAgentHost; |
| 133 using content::NavigationController; | 123 using content::NavigationController; |
| 134 using content::OpenURLParams; | 124 using content::OpenURLParams; |
| 135 using content::Referrer; | 125 using content::Referrer; |
| 136 using content::RenderFrameHost; | 126 using content::RenderFrameHost; |
| 137 using content::RenderViewHost; | 127 using content::RenderViewHost; |
| 138 using content::RenderWidgetHost; | 128 using content::RenderWidgetHost; |
| 139 using content::TestNavigationObserver; | 129 using content::TestNavigationObserver; |
| 140 using content::WebContents; | 130 using content::WebContents; |
| 141 using content::WebContentsObserver; | 131 using content::WebContentsObserver; |
| 142 using net::NetworkChangeNotifier; | 132 using net::NetworkChangeNotifier; |
| 143 using safe_browsing::LocalSafeBrowsingDatabaseManager; | 133 using prerender::test_utils::RequestCounter; |
| 144 using safe_browsing::SafeBrowsingService; | 134 using prerender::test_utils::CreateCountingInterceptorOnIO; |
| 145 using safe_browsing::SBThreatType; | 135 using prerender::test_utils::CreateMockInterceptorOnIO; |
| 136 using prerender::test_utils::TestPrerender; |
| 137 using prerender::test_utils::TestPrerenderContents; |
| 146 using task_manager::browsertest_util::WaitForTaskManagerRows; | 138 using task_manager::browsertest_util::WaitForTaskManagerRows; |
| 147 | 139 |
| 148 // Prerender tests work as follows: | 140 // Prerender tests work as follows: |
| 149 // | 141 // |
| 150 // A page with a prefetch link to the test page is loaded. Once prerendered, | 142 // A page with a prefetch link to the test page is loaded. Once prerendered, |
| 151 // its Javascript function DidPrerenderPass() is called, which returns true if | 143 // its Javascript function DidPrerenderPass() is called, which returns true if |
| 152 // the page behaves as expected when prerendered. | 144 // the page behaves as expected when prerendered. |
| 153 // | 145 // |
| 154 // The prerendered page is then displayed on a tab. The Javascript function | 146 // The prerendered page is then displayed on a tab. The Javascript function |
| 155 // DidDisplayPass() is called, and returns true if the page behaved as it | 147 // DidDisplayPass() is called, and returns true if the page behaved as it |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 new_tab)); | 458 new_tab)); |
| 467 swap_observer_->set_did_start_loading(); | 459 swap_observer_->set_did_start_loading(); |
| 468 return true; | 460 return true; |
| 469 } | 461 } |
| 470 | 462 |
| 471 private: | 463 private: |
| 472 content::WindowedNotificationObserver new_tab_observer_; | 464 content::WindowedNotificationObserver new_tab_observer_; |
| 473 std::unique_ptr<NavigationOrSwapObserver> swap_observer_; | 465 std::unique_ptr<NavigationOrSwapObserver> swap_observer_; |
| 474 }; | 466 }; |
| 475 | 467 |
| 476 // PrerenderContents that stops the UI message loop on DidStopLoading(). | |
| 477 class TestPrerenderContents : public PrerenderContents { | |
| 478 public: | |
| 479 TestPrerenderContents(PrerenderManager* prerender_manager, | |
| 480 Profile* profile, | |
| 481 const GURL& url, | |
| 482 const content::Referrer& referrer, | |
| 483 Origin origin, | |
| 484 FinalStatus expected_final_status) | |
| 485 : PrerenderContents(prerender_manager, profile, url, referrer, origin), | |
| 486 expected_final_status_(expected_final_status), | |
| 487 new_render_view_host_(nullptr), | |
| 488 was_hidden_(false), | |
| 489 was_shown_(false), | |
| 490 should_be_shown_(expected_final_status == FINAL_STATUS_USED), | |
| 491 skip_final_checks_(false) {} | |
| 492 | |
| 493 ~TestPrerenderContents() override { | |
| 494 if (skip_final_checks_) | |
| 495 return; | |
| 496 | |
| 497 EXPECT_EQ(expected_final_status_, final_status()) | |
| 498 << " when testing URL " << prerender_url().path() | |
| 499 << " (Expected: " << NameFromFinalStatus(expected_final_status_) | |
| 500 << ", Actual: " << NameFromFinalStatus(final_status()) << ")"; | |
| 501 | |
| 502 // Prerendering RenderViewHosts should be hidden before the first | |
| 503 // navigation, so this should be happen for every PrerenderContents for | |
| 504 // which a RenderViewHost is created, regardless of whether or not it's | |
| 505 // used. | |
| 506 if (new_render_view_host_) | |
| 507 EXPECT_TRUE(was_hidden_); | |
| 508 | |
| 509 // A used PrerenderContents will only be destroyed when we swap out | |
| 510 // WebContents, at the end of a navigation caused by a call to | |
| 511 // NavigateToURLImpl(). | |
| 512 if (final_status() == FINAL_STATUS_USED) | |
| 513 EXPECT_TRUE(new_render_view_host_); | |
| 514 | |
| 515 EXPECT_EQ(should_be_shown_, was_shown_); | |
| 516 } | |
| 517 | |
| 518 void RenderProcessGone(base::TerminationStatus status) override { | |
| 519 // On quit, it's possible to end up here when render processes are closed | |
| 520 // before the PrerenderManager is destroyed. As a result, it's possible to | |
| 521 // get either FINAL_STATUS_APP_TERMINATING or FINAL_STATUS_RENDERER_CRASHED | |
| 522 // on quit. | |
| 523 // | |
| 524 // It's also possible for this to be called after we've been notified of | |
| 525 // app termination, but before we've been deleted, which is why the second | |
| 526 // check is needed. | |
| 527 if (expected_final_status_ == FINAL_STATUS_APP_TERMINATING && | |
| 528 final_status() != expected_final_status_) { | |
| 529 expected_final_status_ = FINAL_STATUS_RENDERER_CRASHED; | |
| 530 } | |
| 531 | |
| 532 PrerenderContents::RenderProcessGone(status); | |
| 533 } | |
| 534 | |
| 535 bool CheckURL(const GURL& url) override { | |
| 536 // Prevent FINAL_STATUS_UNSUPPORTED_SCHEME when navigating to about:crash in | |
| 537 // the PrerenderRendererCrash test. | |
| 538 if (url.spec() != content::kChromeUICrashURL) | |
| 539 return PrerenderContents::CheckURL(url); | |
| 540 return true; | |
| 541 } | |
| 542 | |
| 543 // For tests that open the prerender in a new background tab, the RenderView | |
| 544 // will not have been made visible when the PrerenderContents is destroyed | |
| 545 // even though it is used. | |
| 546 void set_should_be_shown(bool value) { should_be_shown_ = value; } | |
| 547 | |
| 548 // For tests which do not know whether the prerender will be used. | |
| 549 void set_skip_final_checks(bool value) { skip_final_checks_ = value; } | |
| 550 | |
| 551 FinalStatus expected_final_status() const { return expected_final_status_; } | |
| 552 | |
| 553 private: | |
| 554 void OnRenderViewHostCreated(RenderViewHost* new_render_view_host) override { | |
| 555 // Used to make sure the RenderViewHost is hidden and, if used, | |
| 556 // subsequently shown. | |
| 557 notification_registrar().Add( | |
| 558 this, content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED, | |
| 559 content::Source<RenderWidgetHost>(new_render_view_host->GetWidget())); | |
| 560 | |
| 561 new_render_view_host_ = new_render_view_host; | |
| 562 | |
| 563 PrerenderContents::OnRenderViewHostCreated(new_render_view_host); | |
| 564 } | |
| 565 | |
| 566 void Observe(int type, | |
| 567 const content::NotificationSource& source, | |
| 568 const content::NotificationDetails& details) override { | |
| 569 if (type == | |
| 570 content::NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED) { | |
| 571 EXPECT_EQ(new_render_view_host_->GetWidget(), | |
| 572 content::Source<RenderWidgetHost>(source).ptr()); | |
| 573 bool is_visible = *content::Details<bool>(details).ptr(); | |
| 574 | |
| 575 if (!is_visible) { | |
| 576 was_hidden_ = true; | |
| 577 } else if (is_visible && was_hidden_) { | |
| 578 // Once hidden, a prerendered RenderViewHost should only be shown after | |
| 579 // being removed from the PrerenderContents for display. | |
| 580 EXPECT_FALSE(GetRenderViewHost()); | |
| 581 was_shown_ = true; | |
| 582 } | |
| 583 return; | |
| 584 } | |
| 585 PrerenderContents::Observe(type, source, details); | |
| 586 } | |
| 587 | |
| 588 FinalStatus expected_final_status_; | |
| 589 | |
| 590 // The RenderViewHost created for the prerender, if any. | |
| 591 RenderViewHost* new_render_view_host_; | |
| 592 // Set to true when the prerendering RenderWidget is hidden. | |
| 593 bool was_hidden_; | |
| 594 // Set to true when the prerendering RenderWidget is shown, after having been | |
| 595 // hidden. | |
| 596 bool was_shown_; | |
| 597 // Expected final value of was_shown_. Defaults to true for | |
| 598 // FINAL_STATUS_USED, and false otherwise. | |
| 599 bool should_be_shown_; | |
| 600 // If true, |expected_final_status_| and other shutdown checks are skipped. | |
| 601 bool skip_final_checks_; | |
| 602 }; | |
| 603 | |
| 604 // A handle to a TestPrerenderContents whose lifetime is under the caller's | |
| 605 // control. A PrerenderContents may be destroyed at any point. This allows | |
| 606 // tracking the final status, etc. | |
| 607 class TestPrerender : public PrerenderContents::Observer, | |
| 608 public base::SupportsWeakPtr<TestPrerender> { | |
| 609 public: | |
| 610 TestPrerender() | |
| 611 : contents_(nullptr), number_of_loads_(0), expected_number_of_loads_(0) {} | |
| 612 ~TestPrerender() override { | |
| 613 if (contents_) | |
| 614 contents_->RemoveObserver(this); | |
| 615 } | |
| 616 | |
| 617 TestPrerenderContents* contents() const { return contents_; } | |
| 618 int number_of_loads() const { return number_of_loads_; } | |
| 619 | |
| 620 void WaitForCreate() { create_loop_.Run(); } | |
| 621 void WaitForStart() { start_loop_.Run(); } | |
| 622 void WaitForStop() { stop_loop_.Run(); } | |
| 623 | |
| 624 // Waits for |number_of_loads()| to be at least |expected_number_of_loads| OR | |
| 625 // for the prerender to stop running (just to avoid a timeout if the prerender | |
| 626 // dies). Note: this does not assert equality on the number of loads; the | |
| 627 // caller must do it instead. | |
| 628 void WaitForLoads(int expected_number_of_loads) { | |
| 629 DCHECK(!load_waiter_); | |
| 630 DCHECK(!expected_number_of_loads_); | |
| 631 if (number_of_loads_ < expected_number_of_loads) { | |
| 632 load_waiter_.reset(new base::RunLoop); | |
| 633 expected_number_of_loads_ = expected_number_of_loads; | |
| 634 load_waiter_->Run(); | |
| 635 load_waiter_.reset(); | |
| 636 expected_number_of_loads_ = 0; | |
| 637 } | |
| 638 EXPECT_LE(expected_number_of_loads, number_of_loads_); | |
| 639 } | |
| 640 | |
| 641 void OnPrerenderCreated(TestPrerenderContents* contents) { | |
| 642 DCHECK(!contents_); | |
| 643 contents_ = contents; | |
| 644 contents_->AddObserver(this); | |
| 645 create_loop_.Quit(); | |
| 646 } | |
| 647 | |
| 648 // PrerenderContents::Observer implementation: | |
| 649 void OnPrerenderStart(PrerenderContents* contents) override { | |
| 650 start_loop_.Quit(); | |
| 651 } | |
| 652 | |
| 653 void OnPrerenderStopLoading(PrerenderContents* contents) override { | |
| 654 number_of_loads_++; | |
| 655 if (load_waiter_ && number_of_loads_ >= expected_number_of_loads_) | |
| 656 load_waiter_->Quit(); | |
| 657 } | |
| 658 | |
| 659 void OnPrerenderStop(PrerenderContents* contents) override { | |
| 660 DCHECK(contents_); | |
| 661 contents_ = nullptr; | |
| 662 stop_loop_.Quit(); | |
| 663 // If there is a WaitForLoads call and it has yet to see the expected number | |
| 664 // of loads, stop the loop so the test fails instead of timing out. | |
| 665 if (load_waiter_) | |
| 666 load_waiter_->Quit(); | |
| 667 } | |
| 668 | |
| 669 private: | |
| 670 TestPrerenderContents* contents_; | |
| 671 int number_of_loads_; | |
| 672 | |
| 673 int expected_number_of_loads_; | |
| 674 std::unique_ptr<base::RunLoop> load_waiter_; | |
| 675 | |
| 676 base::RunLoop create_loop_; | |
| 677 base::RunLoop start_loop_; | |
| 678 base::RunLoop stop_loop_; | |
| 679 | |
| 680 DISALLOW_COPY_AND_ASSIGN(TestPrerender); | |
| 681 }; | |
| 682 | |
| 683 // PrerenderManager that uses TestPrerenderContents. | |
| 684 class TestPrerenderContentsFactory : public PrerenderContents::Factory { | |
| 685 public: | |
| 686 TestPrerenderContentsFactory() {} | |
| 687 | |
| 688 ~TestPrerenderContentsFactory() override { | |
| 689 EXPECT_TRUE(expected_contents_queue_.empty()); | |
| 690 } | |
| 691 | |
| 692 std::unique_ptr<TestPrerender> ExpectPrerenderContents( | |
| 693 FinalStatus final_status) { | |
| 694 std::unique_ptr<TestPrerender> handle(new TestPrerender()); | |
| 695 expected_contents_queue_.push_back( | |
| 696 ExpectedContents(final_status, handle->AsWeakPtr())); | |
| 697 return handle; | |
| 698 } | |
| 699 | |
| 700 PrerenderContents* CreatePrerenderContents( | |
| 701 PrerenderManager* prerender_manager, | |
| 702 Profile* profile, | |
| 703 const GURL& url, | |
| 704 const content::Referrer& referrer, | |
| 705 Origin origin) override { | |
| 706 ExpectedContents expected; | |
| 707 if (!expected_contents_queue_.empty()) { | |
| 708 expected = expected_contents_queue_.front(); | |
| 709 expected_contents_queue_.pop_front(); | |
| 710 } | |
| 711 VLOG(1) << "Creating prerender contents for " << url.path() << | |
| 712 " with expected final status " << expected.final_status; | |
| 713 VLOG(1) << expected_contents_queue_.size() << " left in the queue."; | |
| 714 TestPrerenderContents* contents = | |
| 715 new TestPrerenderContents(prerender_manager, | |
| 716 profile, url, referrer, origin, | |
| 717 expected.final_status); | |
| 718 if (expected.handle) | |
| 719 expected.handle->OnPrerenderCreated(contents); | |
| 720 return contents; | |
| 721 } | |
| 722 | |
| 723 private: | |
| 724 struct ExpectedContents { | |
| 725 ExpectedContents() : final_status(FINAL_STATUS_MAX) { } | |
| 726 ExpectedContents(FinalStatus final_status, | |
| 727 const base::WeakPtr<TestPrerender>& handle) | |
| 728 : final_status(final_status), | |
| 729 handle(handle) { | |
| 730 } | |
| 731 | |
| 732 FinalStatus final_status; | |
| 733 base::WeakPtr<TestPrerender> handle; | |
| 734 }; | |
| 735 | |
| 736 std::deque<ExpectedContents> expected_contents_queue_; | |
| 737 }; | |
| 738 | |
| 739 // A SafeBrowsingDatabaseManager implementation that returns a fixed result for | |
| 740 // a given URL. | |
| 741 class FakeSafeBrowsingDatabaseManager | |
| 742 : public safe_browsing::TestSafeBrowsingDatabaseManager { | |
| 743 public: | |
| 744 FakeSafeBrowsingDatabaseManager() {} | |
| 745 | |
| 746 // Called on the IO thread to check if the given url is safe or not. If we | |
| 747 // can synchronously determine that the url is safe, CheckUrl returns true. | |
| 748 // Otherwise it returns false, and "client" is called asynchronously with the | |
| 749 // result when it is ready. | |
| 750 // Returns true, indicating a SAFE result, unless the URL is the fixed URL | |
| 751 // specified by the user, and the user-specified result is not SAFE | |
| 752 // (in which that result will be communicated back via a call into the | |
| 753 // client, and false will be returned). | |
| 754 // Overrides SafeBrowsingDatabaseManager::CheckBrowseUrl. | |
| 755 bool CheckBrowseUrl(const GURL& gurl, Client* client) override { | |
| 756 if (bad_urls_.find(gurl.spec()) == bad_urls_.end() || | |
| 757 bad_urls_[gurl.spec()] == safe_browsing::SB_THREAT_TYPE_SAFE) { | |
| 758 return true; | |
| 759 } | |
| 760 | |
| 761 BrowserThread::PostTask( | |
| 762 BrowserThread::IO, FROM_HERE, | |
| 763 base::Bind(&FakeSafeBrowsingDatabaseManager::OnCheckBrowseURLDone, | |
| 764 this, gurl, client)); | |
| 765 return false; | |
| 766 } | |
| 767 | |
| 768 void SetThreatTypeForUrl(const GURL& url, SBThreatType threat_type) { | |
| 769 bad_urls_[url.spec()] = threat_type; | |
| 770 } | |
| 771 | |
| 772 // These are called when checking URLs, so we implement them. | |
| 773 bool IsSupported() const override { return true; } | |
| 774 bool ChecksAreAlwaysAsync() const override { return false; } | |
| 775 bool CanCheckResourceType( | |
| 776 content::ResourceType /* resource_type */) const override { | |
| 777 return true; | |
| 778 } | |
| 779 | |
| 780 bool CheckExtensionIDs(const std::set<std::string>& extension_ids, | |
| 781 Client* client) override { | |
| 782 return true; | |
| 783 } | |
| 784 | |
| 785 private: | |
| 786 ~FakeSafeBrowsingDatabaseManager() override {} | |
| 787 | |
| 788 void OnCheckBrowseURLDone(const GURL& gurl, Client* client) { | |
| 789 std::vector<SBThreatType> expected_threats; | |
| 790 expected_threats.push_back(safe_browsing::SB_THREAT_TYPE_URL_MALWARE); | |
| 791 expected_threats.push_back(safe_browsing::SB_THREAT_TYPE_URL_PHISHING); | |
| 792 // TODO(nparker): Replace SafeBrowsingCheck w/ a call to | |
| 793 // client->OnCheckBrowseUrlResult() | |
| 794 LocalSafeBrowsingDatabaseManager::SafeBrowsingCheck sb_check( | |
| 795 std::vector<GURL>(1, gurl), | |
| 796 std::vector<safe_browsing::SBFullHash>(), | |
| 797 client, | |
| 798 safe_browsing::MALWARE, | |
| 799 expected_threats); | |
| 800 sb_check.url_results[0] = bad_urls_[gurl.spec()]; | |
| 801 sb_check.OnSafeBrowsingResult(); | |
| 802 } | |
| 803 | |
| 804 std::unordered_map<std::string, SBThreatType> bad_urls_; | |
| 805 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager); | |
| 806 }; | |
| 807 | |
| 808 class FakeDevToolsClient : public content::DevToolsAgentHostClient { | 468 class FakeDevToolsClient : public content::DevToolsAgentHostClient { |
| 809 public: | 469 public: |
| 810 FakeDevToolsClient() {} | 470 FakeDevToolsClient() {} |
| 811 ~FakeDevToolsClient() override {} | 471 ~FakeDevToolsClient() override {} |
| 812 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, | 472 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, |
| 813 const std::string& message) override {} | 473 const std::string& message) override {} |
| 814 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override {} | 474 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override {} |
| 815 }; | 475 }; |
| 816 | 476 |
| 817 class RestorePrerenderMode { | 477 class RestorePrerenderMode { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 878 // the UI thread. | 538 // the UI thread. |
| 879 void CreateHangingFirstRequestInterceptorOnIO( | 539 void CreateHangingFirstRequestInterceptorOnIO( |
| 880 const GURL& url, const base::FilePath& file, base::Closure callback) { | 540 const GURL& url, const base::FilePath& file, base::Closure callback) { |
| 881 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 541 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 882 std::unique_ptr<net::URLRequestInterceptor> never_respond_handler( | 542 std::unique_ptr<net::URLRequestInterceptor> never_respond_handler( |
| 883 new HangingFirstRequestInterceptor(file, callback)); | 543 new HangingFirstRequestInterceptor(file, callback)); |
| 884 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( | 544 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( |
| 885 url, std::move(never_respond_handler)); | 545 url, std::move(never_respond_handler)); |
| 886 } | 546 } |
| 887 | 547 |
| 888 // Wrapper over URLRequestMockHTTPJob that exposes extra callbacks. | |
| 889 class MockHTTPJob : public net::URLRequestMockHTTPJob { | |
| 890 public: | |
| 891 MockHTTPJob(net::URLRequest* request, | |
| 892 net::NetworkDelegate* delegate, | |
| 893 const base::FilePath& file) | |
| 894 : net::URLRequestMockHTTPJob( | |
| 895 request, | |
| 896 delegate, | |
| 897 file, | |
| 898 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | |
| 899 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)) {} | |
| 900 | |
| 901 void set_start_callback(const base::Closure& start_callback) { | |
| 902 start_callback_ = start_callback; | |
| 903 } | |
| 904 | |
| 905 void Start() override { | |
| 906 if (!start_callback_.is_null()) | |
| 907 start_callback_.Run(); | |
| 908 net::URLRequestMockHTTPJob::Start(); | |
| 909 } | |
| 910 | |
| 911 private: | |
| 912 ~MockHTTPJob() override {} | |
| 913 | |
| 914 base::Closure start_callback_; | |
| 915 }; | |
| 916 | |
| 917 // Dummy counter class to live on the UI thread for counting requests. | |
| 918 class RequestCounter : public base::SupportsWeakPtr<RequestCounter> { | |
| 919 public: | |
| 920 RequestCounter() : count_(0), expected_count_(-1) {} | |
| 921 int count() const { return count_; } | |
| 922 | |
| 923 void RequestStarted() { | |
| 924 count_++; | |
| 925 if (loop_ && count_ == expected_count_) | |
| 926 loop_->Quit(); | |
| 927 } | |
| 928 | |
| 929 void WaitForCount(int expected_count) { | |
| 930 ASSERT_TRUE(!loop_); | |
| 931 ASSERT_EQ(-1, expected_count_); | |
| 932 if (count_ < expected_count) { | |
| 933 expected_count_ = expected_count; | |
| 934 loop_.reset(new base::RunLoop); | |
| 935 loop_->Run(); | |
| 936 expected_count_ = -1; | |
| 937 loop_.reset(); | |
| 938 } | |
| 939 | |
| 940 EXPECT_EQ(expected_count, count_); | |
| 941 } | |
| 942 | |
| 943 private: | |
| 944 int count_; | |
| 945 int expected_count_; | |
| 946 std::unique_ptr<base::RunLoop> loop_; | |
| 947 }; | |
| 948 | |
| 949 // Protocol handler which counts the number of requests that start. | |
| 950 class CountingInterceptor : public net::URLRequestInterceptor { | |
| 951 public: | |
| 952 CountingInterceptor(const base::FilePath& file, | |
| 953 const base::WeakPtr<RequestCounter>& counter) | |
| 954 : file_(file), | |
| 955 counter_(counter), | |
| 956 weak_factory_(this) { | |
| 957 } | |
| 958 ~CountingInterceptor() override {} | |
| 959 | |
| 960 net::URLRequestJob* MaybeInterceptRequest( | |
| 961 net::URLRequest* request, | |
| 962 net::NetworkDelegate* network_delegate) const override { | |
| 963 MockHTTPJob* job = new MockHTTPJob(request, network_delegate, file_); | |
| 964 job->set_start_callback(base::Bind(&CountingInterceptor::RequestStarted, | |
| 965 weak_factory_.GetWeakPtr())); | |
| 966 return job; | |
| 967 } | |
| 968 | |
| 969 void RequestStarted() { | |
| 970 BrowserThread::PostTask( | |
| 971 BrowserThread::UI, FROM_HERE, | |
| 972 base::Bind(&RequestCounter::RequestStarted, counter_)); | |
| 973 } | |
| 974 | |
| 975 private: | |
| 976 base::FilePath file_; | |
| 977 base::WeakPtr<RequestCounter> counter_; | |
| 978 mutable base::WeakPtrFactory<CountingInterceptor> weak_factory_; | |
| 979 }; | |
| 980 | |
| 981 // Makes |url| respond to requests with the contents of |file|, counting the | |
| 982 // number that start in |counter|. | |
| 983 void CreateCountingInterceptorOnIO( | |
| 984 const GURL& url, | |
| 985 const base::FilePath& file, | |
| 986 const base::WeakPtr<RequestCounter>& counter) { | |
| 987 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 988 std::unique_ptr<net::URLRequestInterceptor> request_interceptor( | |
| 989 new CountingInterceptor(file, counter)); | |
| 990 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( | |
| 991 url, std::move(request_interceptor)); | |
| 992 } | |
| 993 | |
| 994 // Makes |url| respond to requests with the contents of |file|. | |
| 995 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) { | |
| 996 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 997 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( | |
| 998 url, | |
| 999 net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile( | |
| 1000 file, BrowserThread::GetBlockingPool())); | |
| 1001 } | |
| 1002 | |
| 1003 // A ContentBrowserClient that cancels all prerenderers on OpenURL. | 548 // A ContentBrowserClient that cancels all prerenderers on OpenURL. |
| 1004 class TestContentBrowserClient : public ChromeContentBrowserClient { | 549 class TestContentBrowserClient : public ChromeContentBrowserClient { |
| 1005 public: | 550 public: |
| 1006 TestContentBrowserClient() {} | 551 TestContentBrowserClient() {} |
| 1007 ~TestContentBrowserClient() override {} | 552 ~TestContentBrowserClient() override {} |
| 1008 | 553 |
| 1009 // ChromeContentBrowserClient: | 554 // ChromeContentBrowserClient: |
| 1010 bool ShouldAllowOpenURL(content::SiteInstance* site_instance, | 555 bool ShouldAllowOpenURL(content::SiteInstance* site_instance, |
| 1011 const GURL& url) override { | 556 const GURL& url) override { |
| 1012 PrerenderManagerFactory::GetForProfile( | 557 PrerenderManagerFactory::GetForProfile( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1031 content::ResourceContext* resource_context, | 576 content::ResourceContext* resource_context, |
| 1032 const GURL& current_url, | 577 const GURL& current_url, |
| 1033 const GURL& new_url) override { | 578 const GURL& new_url) override { |
| 1034 return true; | 579 return true; |
| 1035 } | 580 } |
| 1036 | 581 |
| 1037 private: | 582 private: |
| 1038 DISALLOW_COPY_AND_ASSIGN(SwapProcessesContentBrowserClient); | 583 DISALLOW_COPY_AND_ASSIGN(SwapProcessesContentBrowserClient); |
| 1039 }; | 584 }; |
| 1040 | 585 |
| 1041 // An ExternalProtocolHandler that blocks everything and asserts it never is | |
| 1042 // called. | |
| 1043 class NeverRunsExternalProtocolHandlerDelegate | |
| 1044 : public ExternalProtocolHandler::Delegate { | |
| 1045 public: | |
| 1046 // ExternalProtocolHandler::Delegate implementation. | |
| 1047 scoped_refptr<shell_integration::DefaultProtocolClientWorker> | |
| 1048 CreateShellWorker( | |
| 1049 const shell_integration::DefaultWebClientWorkerCallback& callback, | |
| 1050 const std::string& protocol) override { | |
| 1051 NOTREACHED(); | |
| 1052 // This will crash, but it shouldn't get this far with BlockState::BLOCK | |
| 1053 // anyway. | |
| 1054 return nullptr; | |
| 1055 } | |
| 1056 ExternalProtocolHandler::BlockState GetBlockState( | |
| 1057 const std::string& scheme) override { | |
| 1058 // Block everything and fail the test. | |
| 1059 ADD_FAILURE(); | |
| 1060 return ExternalProtocolHandler::BLOCK; | |
| 1061 } | |
| 1062 void BlockRequest() override {} | |
| 1063 void RunExternalProtocolDialog(const GURL& url, | |
| 1064 int render_process_host_id, | |
| 1065 int routing_id, | |
| 1066 ui::PageTransition page_transition, | |
| 1067 bool has_user_gesture) override { | |
| 1068 NOTREACHED(); | |
| 1069 } | |
| 1070 void LaunchUrlWithoutSecurityCheck(const GURL& url) override { NOTREACHED(); } | |
| 1071 void FinishedProcessingCheck() override { NOTREACHED(); } | |
| 1072 }; | |
| 1073 | |
| 1074 base::FilePath GetTestPath(const std::string& file_name) { | 586 base::FilePath GetTestPath(const std::string& file_name) { |
| 1075 return ui_test_utils::GetTestFilePath( | 587 return ui_test_utils::GetTestFilePath( |
| 1076 base::FilePath(FILE_PATH_LITERAL("prerender")), | 588 base::FilePath(FILE_PATH_LITERAL("prerender")), |
| 1077 base::FilePath().AppendASCII(file_name)); | 589 base::FilePath().AppendASCII(file_name)); |
| 1078 } | 590 } |
| 1079 | 591 |
| 1080 } // namespace | 592 } // namespace |
| 1081 | 593 |
| 1082 class PrerenderBrowserTest : virtual public InProcessBrowserTest { | 594 class PrerenderBrowserTest : public test_utils::PrerenderInProcessBrowserTest { |
| 1083 public: | 595 public: |
| 1084 PrerenderBrowserTest() | 596 PrerenderBrowserTest() |
| 1085 : autostart_test_server_(true), | 597 : call_javascript_(true), |
| 1086 prerender_contents_factory_(nullptr), | |
| 1087 safe_browsing_factory_( | |
| 1088 new safe_browsing::TestSafeBrowsingServiceFactory()), | |
| 1089 call_javascript_(true), | |
| 1090 check_load_events_(true), | 598 check_load_events_(true), |
| 1091 loader_path_("/prerender/prerender_loader.html"), | 599 loader_path_("/prerender/prerender_loader.html") {} |
| 1092 explicitly_set_browser_(nullptr) {} | |
| 1093 | 600 |
| 1094 ~PrerenderBrowserTest() override {} | 601 ~PrerenderBrowserTest() override {} |
| 1095 | 602 |
| 1096 content::SessionStorageNamespace* GetSessionStorageNamespace() const { | |
| 1097 WebContents* web_contents = GetActiveWebContents(); | |
| 1098 if (!web_contents) | |
| 1099 return nullptr; | |
| 1100 return web_contents->GetController().GetDefaultSessionStorageNamespace(); | |
| 1101 } | |
| 1102 | |
| 1103 void SetUpInProcessBrowserTestFixture() override { | |
| 1104 safe_browsing_factory_->SetTestDatabaseManager( | |
| 1105 new FakeSafeBrowsingDatabaseManager()); | |
| 1106 SafeBrowsingService::RegisterFactory(safe_browsing_factory_); | |
| 1107 } | |
| 1108 | |
| 1109 void TearDownInProcessBrowserTestFixture() override { | |
| 1110 SafeBrowsingService::RegisterFactory(nullptr); | |
| 1111 } | |
| 1112 | |
| 1113 void SetUpCommandLine(base::CommandLine* command_line) override { | 603 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 604 PrerenderInProcessBrowserTest::SetUpCommandLine(command_line); |
| 1114 command_line->AppendSwitchASCII(switches::kPrerenderMode, | 605 command_line->AppendSwitchASCII(switches::kPrerenderMode, |
| 1115 switches::kPrerenderModeSwitchValueEnabled); | 606 switches::kPrerenderModeSwitchValueEnabled); |
| 1116 command_line->AppendSwitch(switches::kEnablePepperTesting); | |
| 1117 command_line->AppendSwitchASCII( | |
| 1118 switches::kOverridePluginPowerSaverForTesting, "ignore-list"); | |
| 1119 | |
| 1120 ASSERT_TRUE(ppapi::RegisterPowerSaverTestPlugin(command_line)); | |
| 1121 } | |
| 1122 | |
| 1123 void SetUpOnMainThread() override { | |
| 1124 current_browser()->profile()->GetPrefs()->SetBoolean( | |
| 1125 prefs::kPromptForDownload, false); | |
| 1126 IncreasePrerenderMemory(); | |
| 1127 if (autostart_test_server_) | |
| 1128 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 1129 ChromeResourceDispatcherHostDelegate:: | |
| 1130 SetExternalProtocolHandlerDelegateForTesting( | |
| 1131 &external_protocol_handler_delegate_); | |
| 1132 | |
| 1133 PrerenderManager* prerender_manager = GetPrerenderManager(); | |
| 1134 ASSERT_TRUE(prerender_manager); | |
| 1135 prerender_manager->mutable_config().rate_limit_enabled = false; | |
| 1136 ASSERT_FALSE(prerender_contents_factory_); | |
| 1137 prerender_contents_factory_ = new TestPrerenderContentsFactory; | |
| 1138 prerender_manager->SetPrerenderContentsFactoryForTest( | |
| 1139 prerender_contents_factory_); | |
| 1140 ASSERT_TRUE(safe_browsing_factory_->test_safe_browsing_service()); | |
| 1141 } | |
| 1142 | |
| 1143 // Convenience function to get the currently active WebContents in | |
| 1144 // current_browser(). | |
| 1145 WebContents* GetActiveWebContents() const { | |
| 1146 return current_browser()->tab_strip_model()->GetActiveWebContents(); | |
| 1147 } | |
| 1148 | |
| 1149 // Overload for a single expected final status | |
| 1150 std::unique_ptr<TestPrerender> PrerenderTestURL( | |
| 1151 const std::string& html_file, | |
| 1152 FinalStatus expected_final_status, | |
| 1153 int expected_number_of_loads) { | |
| 1154 GURL url = embedded_test_server()->GetURL(html_file); | |
| 1155 return PrerenderTestURL(url, | |
| 1156 expected_final_status, | |
| 1157 expected_number_of_loads); | |
| 1158 } | |
| 1159 | |
| 1160 ScopedVector<TestPrerender> PrerenderTestURL( | |
| 1161 const std::string& html_file, | |
| 1162 const std::vector<FinalStatus>& expected_final_status_queue, | |
| 1163 int expected_number_of_loads) { | |
| 1164 GURL url = embedded_test_server()->GetURL(html_file); | |
| 1165 return PrerenderTestURLImpl(url, | |
| 1166 expected_final_status_queue, | |
| 1167 expected_number_of_loads); | |
| 1168 } | |
| 1169 | |
| 1170 std::unique_ptr<TestPrerender> PrerenderTestURL( | |
| 1171 const GURL& url, | |
| 1172 FinalStatus expected_final_status, | |
| 1173 int expected_number_of_loads) { | |
| 1174 std::vector<FinalStatus> expected_final_status_queue( | |
| 1175 1, expected_final_status); | |
| 1176 std::vector<TestPrerender*> prerenders; | |
| 1177 PrerenderTestURLImpl(url, | |
| 1178 expected_final_status_queue, | |
| 1179 expected_number_of_loads).release(&prerenders); | |
| 1180 CHECK_EQ(1u, prerenders.size()); | |
| 1181 return std::unique_ptr<TestPrerender>(prerenders[0]); | |
| 1182 } | 607 } |
| 1183 | 608 |
| 1184 void NavigateToDestURL() const { | 609 void NavigateToDestURL() const { |
| 1185 NavigateToDestURLWithDisposition(CURRENT_TAB, true); | 610 NavigateToDestURLWithDisposition(CURRENT_TAB, true); |
| 1186 } | 611 } |
| 1187 | 612 |
| 1188 // Opens the url in a new tab, with no opener. | 613 // Opens the url in a new tab, with no opener. |
| 1189 void NavigateToDestURLWithDisposition( | 614 void NavigateToDestURLWithDisposition( |
| 1190 WindowOpenDisposition disposition, | 615 WindowOpenDisposition disposition, |
| 1191 bool expect_swap_to_succeed) const { | 616 bool expect_swap_to_succeed) const { |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1300 chrome::GoBack(current_browser(), CURRENT_TAB); | 725 chrome::GoBack(current_browser(), CURRENT_TAB); |
| 1301 back_nav_observer.Wait(); | 726 back_nav_observer.Wait(); |
| 1302 bool js_result; | 727 bool js_result; |
| 1303 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | 728 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 1304 tab, | 729 tab, |
| 1305 "window.domAutomationController.send(DidBackToOriginalPagePass())", | 730 "window.domAutomationController.send(DidBackToOriginalPagePass())", |
| 1306 &js_result)); | 731 &js_result)); |
| 1307 EXPECT_TRUE(js_result); | 732 EXPECT_TRUE(js_result); |
| 1308 } | 733 } |
| 1309 | 734 |
| 1310 bool UrlIsInPrerenderManager(const std::string& html_file) const { | |
| 1311 return UrlIsInPrerenderManager(embedded_test_server()->GetURL(html_file)); | |
| 1312 } | |
| 1313 | |
| 1314 bool UrlIsInPrerenderManager(const GURL& url) const { | |
| 1315 return GetPrerenderManager()->FindPrerenderData( | |
| 1316 url, GetSessionStorageNamespace()) != nullptr; | |
| 1317 } | |
| 1318 | |
| 1319 void UseHttpsSrcServer() { | 735 void UseHttpsSrcServer() { |
| 1320 if (https_src_server_) | 736 if (https_src_server_) |
| 1321 return; | 737 return; |
| 1322 https_src_server_.reset( | 738 https_src_server_.reset( |
| 1323 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); | 739 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); |
| 1324 https_src_server_->ServeFilesFromSourceDirectory("chrome/test/data"); | 740 https_src_server_->ServeFilesFromSourceDirectory("chrome/test/data"); |
| 1325 CHECK(https_src_server_->Start()); | 741 CHECK(https_src_server_->Start()); |
| 1326 } | 742 } |
| 1327 | 743 |
| 1328 void DisableJavascriptCalls() { | 744 void DisableJavascriptCalls() { |
| 1329 call_javascript_ = false; | 745 call_javascript_ = false; |
| 1330 } | 746 } |
| 1331 | 747 |
| 1332 void DisableLoadEventCheck() { | 748 void DisableLoadEventCheck() { |
| 1333 check_load_events_ = false; | 749 check_load_events_ = false; |
| 1334 } | 750 } |
| 1335 | 751 |
| 1336 PrerenderManager* GetPrerenderManager() const { | |
| 1337 PrerenderManager* prerender_manager = | |
| 1338 PrerenderManagerFactory::GetForProfile(current_browser()->profile()); | |
| 1339 return prerender_manager; | |
| 1340 } | |
| 1341 | |
| 1342 const PrerenderLinkManager* GetPrerenderLinkManager() const { | 752 const PrerenderLinkManager* GetPrerenderLinkManager() const { |
| 1343 PrerenderLinkManager* prerender_link_manager = | 753 PrerenderLinkManager* prerender_link_manager = |
| 1344 PrerenderLinkManagerFactory::GetForProfile( | 754 PrerenderLinkManagerFactory::GetForProfile( |
| 1345 current_browser()->profile()); | 755 current_browser()->profile()); |
| 1346 return prerender_link_manager; | 756 return prerender_link_manager; |
| 1347 } | 757 } |
| 1348 | 758 |
| 1349 int GetPrerenderEventCount(int index, const std::string& type) const { | 759 int GetPrerenderEventCount(int index, const std::string& type) const { |
| 1350 int event_count; | 760 int event_count; |
| 1351 std::string expression = base::StringPrintf( | 761 std::string expression = base::StringPrintf( |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1422 std::unique_ptr<base::DictionaryValue> prerender_dict = | 832 std::unique_ptr<base::DictionaryValue> prerender_dict = |
| 1423 GetPrerenderManager()->GetAsValue(); | 833 GetPrerenderManager()->GetAsValue(); |
| 1424 if (!prerender_dict) | 834 if (!prerender_dict) |
| 1425 return std::numeric_limits<size_t>::max(); | 835 return std::numeric_limits<size_t>::max(); |
| 1426 base::ListValue* history_list; | 836 base::ListValue* history_list; |
| 1427 if (!prerender_dict->GetList("history", &history_list)) | 837 if (!prerender_dict->GetList("history", &history_list)) |
| 1428 return std::numeric_limits<size_t>::max(); | 838 return std::numeric_limits<size_t>::max(); |
| 1429 return history_list->GetSize(); | 839 return history_list->GetSize(); |
| 1430 } | 840 } |
| 1431 | 841 |
| 1432 FakeSafeBrowsingDatabaseManager* GetFakeSafeBrowsingDatabaseManager() { | 842 test_utils::FakeSafeBrowsingDatabaseManager* |
| 1433 return static_cast<FakeSafeBrowsingDatabaseManager*>( | 843 GetFakeSafeBrowsingDatabaseManager() { |
| 1434 safe_browsing_factory_->test_safe_browsing_service() | 844 return static_cast<test_utils::FakeSafeBrowsingDatabaseManager*>( |
| 845 safe_browsing_factory()->test_safe_browsing_service() |
| 1435 ->database_manager() | 846 ->database_manager() |
| 1436 .get()); | 847 .get()); |
| 1437 } | 848 } |
| 1438 | 849 |
| 1439 TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const { | |
| 1440 PrerenderManager::PrerenderData* prerender_data = | |
| 1441 GetPrerenderManager()->FindPrerenderData(url, nullptr); | |
| 1442 return static_cast<TestPrerenderContents*>( | |
| 1443 prerender_data ? prerender_data->contents() : nullptr); | |
| 1444 } | |
| 1445 | |
| 1446 void SetLoaderHostOverride(const std::string& host) { | 850 void SetLoaderHostOverride(const std::string& host) { |
| 1447 loader_host_override_ = host; | 851 loader_host_override_ = host; |
| 1448 host_resolver()->AddRule(host, "127.0.0.1"); | 852 host_resolver()->AddRule(host, "127.0.0.1"); |
| 1449 } | 853 } |
| 1450 | 854 |
| 1451 void set_loader_path(const std::string& path) { | 855 void set_loader_path(const std::string& path) { |
| 1452 loader_path_ = path; | 856 loader_path_ = path; |
| 1453 } | 857 } |
| 1454 | 858 |
| 1455 void set_loader_query(const std::string& query) { | 859 void set_loader_query(const std::string& query) { |
| 1456 loader_query_ = query; | 860 loader_query_ = query; |
| 1457 } | 861 } |
| 1458 | 862 |
| 1459 GURL GetCrossDomainTestUrl(const std::string& path) { | 863 GURL GetCrossDomainTestUrl(const std::string& path) { |
| 1460 static const std::string secondary_domain = "www.foo.com"; | 864 static const std::string secondary_domain = "www.foo.com"; |
| 1461 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); | 865 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); |
| 1462 std::string url_str(base::StringPrintf( | 866 std::string url_str(base::StringPrintf( |
| 1463 "http://%s:%d/%s", secondary_domain.c_str(), | 867 "http://%s:%d/%s", secondary_domain.c_str(), |
| 1464 embedded_test_server()->host_port_pair().port(), path.c_str())); | 868 embedded_test_server()->host_port_pair().port(), path.c_str())); |
| 1465 return GURL(url_str); | 869 return GURL(url_str); |
| 1466 } | 870 } |
| 1467 | 871 |
| 1468 void set_browser(Browser* browser) { | |
| 1469 explicitly_set_browser_ = browser; | |
| 1470 } | |
| 1471 | |
| 1472 Browser* current_browser() const { | |
| 1473 return explicitly_set_browser_ ? explicitly_set_browser_ : browser(); | |
| 1474 } | |
| 1475 | |
| 1476 const GURL& dest_url() const { | 872 const GURL& dest_url() const { |
| 1477 return dest_url_; | 873 return dest_url_; |
| 1478 } | 874 } |
| 1479 | 875 |
| 1480 void IncreasePrerenderMemory() { | |
| 1481 // Increase the memory allowed in a prerendered page above normal settings. | |
| 1482 // Debug build bots occasionally run against the default limit, and tests | |
| 1483 // were failing because the prerender was canceled due to memory exhaustion. | |
| 1484 // http://crbug.com/93076 | |
| 1485 GetPrerenderManager()->mutable_config().max_bytes = 2000 * 1024 * 1024; | |
| 1486 } | |
| 1487 | |
| 1488 bool DidPrerenderPass(WebContents* web_contents) const { | 876 bool DidPrerenderPass(WebContents* web_contents) const { |
| 1489 bool prerender_test_result = false; | 877 bool prerender_test_result = false; |
| 1490 if (!content::ExecuteScriptAndExtractBool( | 878 if (!content::ExecuteScriptAndExtractBool( |
| 1491 web_contents, | 879 web_contents, |
| 1492 "window.domAutomationController.send(DidPrerenderPass())", | 880 "window.domAutomationController.send(DidPrerenderPass())", |
| 1493 &prerender_test_result)) | 881 &prerender_test_result)) |
| 1494 return false; | 882 return false; |
| 1495 return prerender_test_result; | 883 return prerender_test_result; |
| 1496 } | 884 } |
| 1497 | 885 |
| 1498 bool DidDisplayPass(WebContents* web_contents) const { | 886 bool DidDisplayPass(WebContents* web_contents) const { |
| 1499 bool display_test_result = false; | 887 bool display_test_result = false; |
| 1500 if (!content::ExecuteScriptAndExtractBool( | 888 if (!content::ExecuteScriptAndExtractBool( |
| 1501 web_contents, | 889 web_contents, |
| 1502 "window.domAutomationController.send(DidDisplayPass())", | 890 "window.domAutomationController.send(DidDisplayPass())", |
| 1503 &display_test_result)) | 891 &display_test_result)) |
| 1504 return false; | 892 return false; |
| 1505 return display_test_result; | 893 return display_test_result; |
| 1506 } | 894 } |
| 1507 | 895 |
| 1508 std::unique_ptr<TestPrerender> ExpectPrerender( | 896 std::unique_ptr<TestPrerender> ExpectPrerender( |
| 1509 FinalStatus expected_final_status) { | 897 FinalStatus expected_final_status) { |
| 1510 return prerender_contents_factory_->ExpectPrerenderContents( | 898 return prerender_contents_factory()->ExpectPrerenderContents( |
| 1511 expected_final_status); | 899 expected_final_status); |
| 1512 } | 900 } |
| 1513 | 901 |
| 1514 void AddPrerender(const GURL& url, int index) { | 902 void AddPrerender(const GURL& url, int index) { |
| 1515 std::string javascript = base::StringPrintf( | 903 std::string javascript = base::StringPrintf( |
| 1516 "AddPrerender('%s', %d)", url.spec().c_str(), index); | 904 "AddPrerender('%s', %d)", url.spec().c_str(), index); |
| 1517 RenderFrameHost* render_frame_host = GetActiveWebContents()->GetMainFrame(); | 905 RenderFrameHost* render_frame_host = GetActiveWebContents()->GetMainFrame(); |
| 1518 render_frame_host->ExecuteJavaScriptForTests( | 906 render_frame_host->ExecuteJavaScriptForTests( |
| 1519 base::ASCIIToUTF16(javascript)); | 907 base::ASCIIToUTF16(javascript)); |
| 1520 } | 908 } |
| 1521 | 909 |
| 1522 // Returns a string for pattern-matching TaskManager tab entries. | 910 // Returns a string for pattern-matching TaskManager tab entries. |
| 1523 base::string16 MatchTaskManagerTab(const char* page_title) { | 911 base::string16 MatchTaskManagerTab(const char* page_title) { |
| 1524 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX, | 912 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_TAB_PREFIX, |
| 1525 base::ASCIIToUTF16(page_title)); | 913 base::ASCIIToUTF16(page_title)); |
| 1526 } | 914 } |
| 1527 | 915 |
| 1528 // Returns a string for pattern-matching TaskManager prerender entries. | 916 // Returns a string for pattern-matching TaskManager prerender entries. |
| 1529 base::string16 MatchTaskManagerPrerender(const char* page_title) { | 917 base::string16 MatchTaskManagerPrerender(const char* page_title) { |
| 1530 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX, | 918 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX, |
| 1531 base::ASCIIToUTF16(page_title)); | 919 base::ASCIIToUTF16(page_title)); |
| 1532 } | 920 } |
| 1533 | 921 |
| 1534 const base::HistogramTester& histogram_tester() { return histogram_tester_; } | 922 const base::HistogramTester& histogram_tester() { return histogram_tester_; } |
| 1535 | 923 |
| 1536 protected: | |
| 1537 bool autostart_test_server_; | |
| 1538 | |
| 1539 private: | 924 private: |
| 1540 // TODO(davidben): Remove this altogether so the tests don't globally assume | 925 // TODO(davidben): Remove this altogether so the tests don't globally assume |
| 1541 // only one prerender. | 926 // only one prerender. |
| 1542 TestPrerenderContents* GetPrerenderContents() const { | 927 TestPrerenderContents* GetPrerenderContents() const { |
| 1543 return GetPrerenderContentsFor(dest_url_); | 928 return GetPrerenderContentsFor(dest_url_); |
| 1544 } | 929 } |
| 1545 | 930 |
| 1546 ScopedVector<TestPrerender> PrerenderTestURLImpl( | 931 ScopedVector<TestPrerender> PrerenderTestURLImpl( |
| 1547 const GURL& prerender_url, | 932 const GURL& prerender_url, |
| 1548 const std::vector<FinalStatus>& expected_final_status_queue, | 933 const std::vector<FinalStatus>& expected_final_status_queue, |
| 1549 int expected_number_of_loads) { | 934 int expected_number_of_loads) override { |
| 1550 dest_url_ = prerender_url; | 935 dest_url_ = prerender_url; |
| 1551 | 936 |
| 1552 base::StringPairs replacement_text; | 937 base::StringPairs replacement_text; |
| 1553 replacement_text.push_back( | 938 replacement_text.push_back( |
| 1554 make_pair("REPLACE_WITH_PRERENDER_URL", prerender_url.spec())); | 939 make_pair("REPLACE_WITH_PRERENDER_URL", prerender_url.spec())); |
| 1555 std::string replacement_path; | 940 std::string replacement_path; |
| 1556 net::test_server::GetFilePathWithReplacements( | 941 net::test_server::GetFilePathWithReplacements( |
| 1557 loader_path_, replacement_text, &replacement_path); | 942 loader_path_, replacement_text, &replacement_path); |
| 1558 | 943 |
| 1559 const net::EmbeddedTestServer* src_server = embedded_test_server(); | 944 const net::EmbeddedTestServer* src_server = embedded_test_server(); |
| 1560 if (https_src_server_) | 945 if (https_src_server_) |
| 1561 src_server = https_src_server_.get(); | 946 src_server = https_src_server_.get(); |
| 1562 GURL loader_url = src_server->GetURL( | 947 GURL loader_url = src_server->GetURL( |
| 1563 replacement_path + "&" + loader_query_); | 948 replacement_path + "&" + loader_query_); |
| 1564 | 949 |
| 1565 GURL::Replacements loader_replacements; | 950 GURL::Replacements loader_replacements; |
| 1566 if (!loader_host_override_.empty()) | 951 if (!loader_host_override_.empty()) |
| 1567 loader_replacements.SetHostStr(loader_host_override_); | 952 loader_replacements.SetHostStr(loader_host_override_); |
| 1568 loader_url = loader_url.ReplaceComponents(loader_replacements); | 953 loader_url = loader_url.ReplaceComponents(loader_replacements); |
| 1569 | 954 |
| 1570 VLOG(1) << "Running test with queue length " << | |
| 1571 expected_final_status_queue.size(); | |
| 1572 CHECK(!expected_final_status_queue.empty()); | 955 CHECK(!expected_final_status_queue.empty()); |
| 1573 ScopedVector<TestPrerender> prerenders; | 956 ScopedVector<TestPrerender> prerenders; |
| 1574 for (size_t i = 0; i < expected_final_status_queue.size(); i++) { | 957 for (size_t i = 0; i < expected_final_status_queue.size(); i++) { |
| 1575 prerenders.push_back( | 958 prerenders.push_back( |
| 1576 prerender_contents_factory_->ExpectPrerenderContents( | 959 prerender_contents_factory()->ExpectPrerenderContents( |
| 1577 expected_final_status_queue[i]).release()); | 960 expected_final_status_queue[i]).release()); |
| 1578 } | 961 } |
| 1579 | 962 |
| 1580 FinalStatus expected_final_status = expected_final_status_queue.front(); | 963 FinalStatus expected_final_status = expected_final_status_queue.front(); |
| 1581 | 964 |
| 1582 // Navigate to the loader URL and then wait for the first prerender to be | 965 // Navigate to the loader URL and then wait for the first prerender to be |
| 1583 // created. | 966 // created. |
| 1584 ui_test_utils::NavigateToURL(current_browser(), loader_url); | 967 ui_test_utils::NavigateToURL(current_browser(), loader_url); |
| 1585 prerenders[0]->WaitForCreate(); | 968 prerenders[0]->WaitForCreate(); |
| 1586 prerenders[0]->WaitForLoads(expected_number_of_loads); | 969 prerenders[0]->WaitForLoads(expected_number_of_loads); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1662 observer.Wait(); | 1045 observer.Wait(); |
| 1663 } else { | 1046 } else { |
| 1664 NavigationOrSwapObserver observer(current_browser()->tab_strip_model(), | 1047 NavigationOrSwapObserver observer(current_browser()->tab_strip_model(), |
| 1665 web_contents); | 1048 web_contents); |
| 1666 render_frame_host->ExecuteJavaScriptForTests( | 1049 render_frame_host->ExecuteJavaScriptForTests( |
| 1667 base::ASCIIToUTF16(javascript)); | 1050 base::ASCIIToUTF16(javascript)); |
| 1668 observer.Wait(); | 1051 observer.Wait(); |
| 1669 } | 1052 } |
| 1670 } | 1053 } |
| 1671 | 1054 |
| 1672 TestPrerenderContentsFactory* prerender_contents_factory_; | |
| 1673 safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory_; | |
| 1674 NeverRunsExternalProtocolHandlerDelegate external_protocol_handler_delegate_; | |
| 1675 GURL dest_url_; | 1055 GURL dest_url_; |
| 1676 std::unique_ptr<net::EmbeddedTestServer> https_src_server_; | 1056 std::unique_ptr<net::EmbeddedTestServer> https_src_server_; |
| 1677 bool call_javascript_; | 1057 bool call_javascript_; |
| 1678 bool check_load_events_; | 1058 bool check_load_events_; |
| 1679 std::string loader_host_override_; | 1059 std::string loader_host_override_; |
| 1680 std::string loader_path_; | 1060 std::string loader_path_; |
| 1681 std::string loader_query_; | 1061 std::string loader_query_; |
| 1682 Browser* explicitly_set_browser_; | |
| 1683 base::HistogramTester histogram_tester_; | 1062 base::HistogramTester histogram_tester_; |
| 1684 }; | 1063 }; |
| 1685 | 1064 |
| 1686 // Checks that a page is correctly prerendered in the case of a | 1065 // Checks that a page is correctly prerendered in the case of a |
| 1687 // <link rel=prerender> tag and then loaded into a tab in response to a | 1066 // <link rel=prerender> tag and then loaded into a tab in response to a |
| 1688 // navigation. | 1067 // navigation. |
| 1689 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) { | 1068 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPage) { |
| 1690 PrerenderTestURL("/prerender/prerender_page.html", FINAL_STATUS_USED, 1); | 1069 PrerenderTestURL("/prerender/prerender_page.html", FINAL_STATUS_USED, 1); |
| 1691 EXPECT_EQ(1, GetPrerenderDomContentLoadedEventCountForLinkNumber(0)); | 1070 EXPECT_EQ(1, GetPrerenderDomContentLoadedEventCountForLinkNumber(0)); |
| 1692 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1); | 1071 histogram_tester().ExpectTotalCount("Prerender.none_PerceivedPLT", 1); |
| (...skipping 1619 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3312 content::SetBrowserClientForTesting(original_browser_client); | 2691 content::SetBrowserClientForTesting(original_browser_client); |
| 3313 } | 2692 } |
| 3314 | 2693 |
| 3315 // Test interaction of the webNavigation and tabs API with prerender. | 2694 // Test interaction of the webNavigation and tabs API with prerender. |
| 3316 class PrerenderBrowserTestWithExtensions : public PrerenderBrowserTest, | 2695 class PrerenderBrowserTestWithExtensions : public PrerenderBrowserTest, |
| 3317 public ExtensionApiTest { | 2696 public ExtensionApiTest { |
| 3318 public: | 2697 public: |
| 3319 PrerenderBrowserTestWithExtensions() { | 2698 PrerenderBrowserTestWithExtensions() { |
| 3320 // The individual tests start the test server through ExtensionApiTest, so | 2699 // The individual tests start the test server through ExtensionApiTest, so |
| 3321 // the port number can be passed through to the extension. | 2700 // the port number can be passed through to the extension. |
| 3322 autostart_test_server_ = false; | 2701 set_autostart_test_server(false); |
| 3323 } | 2702 } |
| 3324 | 2703 |
| 3325 void SetUp() override { PrerenderBrowserTest::SetUp(); } | 2704 void SetUp() override { PrerenderBrowserTest::SetUp(); } |
| 3326 | 2705 |
| 3327 void SetUpCommandLine(base::CommandLine* command_line) override { | 2706 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 3328 PrerenderBrowserTest::SetUpCommandLine(command_line); | 2707 PrerenderBrowserTest::SetUpCommandLine(command_line); |
| 3329 ExtensionApiTest::SetUpCommandLine(command_line); | 2708 ExtensionApiTest::SetUpCommandLine(command_line); |
| 3330 } | 2709 } |
| 3331 | 2710 |
| 3332 void SetUpInProcessBrowserTestFixture() override { | 2711 void SetUpInProcessBrowserTestFixture() override { |
| (...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3974 browser()->tab_strip_model()->GetActiveWebContents(); | 3353 browser()->tab_strip_model()->GetActiveWebContents(); |
| 3975 bool display_test_result = false; | 3354 bool display_test_result = false; |
| 3976 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(web_contents, | 3355 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(web_contents, |
| 3977 "DidDisplayReallyPass()", | 3356 "DidDisplayReallyPass()", |
| 3978 &display_test_result)); | 3357 &display_test_result)); |
| 3979 ASSERT_TRUE(display_test_result); | 3358 ASSERT_TRUE(display_test_result); |
| 3980 } | 3359 } |
| 3981 #endif // !defined(DISABLE_NACL) | 3360 #endif // !defined(DISABLE_NACL) |
| 3982 | 3361 |
| 3983 } // namespace prerender | 3362 } // namespace prerender |
| OLD | NEW |