| 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "content/browser/frame_host/cross_site_transferring_request.h" | 8 #include "content/browser/frame_host/cross_site_transferring_request.h" |
| 9 #include "content/browser/frame_host/interstitial_page_impl.h" | 9 #include "content/browser/frame_host/interstitial_page_impl.h" |
| 10 #include "content/browser/frame_host/navigation_entry_impl.h" | 10 #include "content/browser/frame_host/navigation_entry_impl.h" |
| 11 #include "content/browser/frame_host/render_frame_host_impl.h" | 11 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 12 #include "content/browser/media/audio_state_provider.h" | 12 #include "content/browser/media/audio_state_provider.h" |
| 13 #include "content/browser/renderer_host/render_view_host_impl.h" | 13 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 14 #include "content/browser/site_instance_impl.h" | 14 #include "content/browser/site_instance_impl.h" |
| 15 #include "content/browser/webui/content_web_ui_controller_factory.h" |
| 15 #include "content/browser/webui/web_ui_controller_factory_registry.h" | 16 #include "content/browser/webui/web_ui_controller_factory_registry.h" |
| 16 #include "content/common/frame_messages.h" | 17 #include "content/common/frame_messages.h" |
| 17 #include "content/common/input/synthetic_web_input_event_builders.h" | 18 #include "content/common/input/synthetic_web_input_event_builders.h" |
| 18 #include "content/common/view_messages.h" | 19 #include "content/common/view_messages.h" |
| 19 #include "content/public/browser/global_request_id.h" | 20 #include "content/public/browser/global_request_id.h" |
| 20 #include "content/public/browser/interstitial_page_delegate.h" | 21 #include "content/public/browser/interstitial_page_delegate.h" |
| 21 #include "content/public/browser/navigation_details.h" | 22 #include "content/public/browser/navigation_details.h" |
| 22 #include "content/public/browser/notification_details.h" | 23 #include "content/public/browser/notification_details.h" |
| 23 #include "content/public/browser/notification_source.h" | 24 #include "content/public/browser/notification_source.h" |
| 24 #include "content/public/browser/render_widget_host_view.h" | 25 #include "content/public/browser/render_widget_host_view.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 35 #include "content/test/test_content_browser_client.h" | 36 #include "content/test/test_content_browser_client.h" |
| 36 #include "content/test/test_content_client.h" | 37 #include "content/test/test_content_client.h" |
| 37 #include "content/test/test_render_frame_host.h" | 38 #include "content/test/test_render_frame_host.h" |
| 38 #include "content/test/test_render_view_host.h" | 39 #include "content/test/test_render_view_host.h" |
| 39 #include "content/test/test_web_contents.h" | 40 #include "content/test/test_web_contents.h" |
| 40 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 41 | 42 |
| 42 namespace content { | 43 namespace content { |
| 43 namespace { | 44 namespace { |
| 44 | 45 |
| 45 const char kTestWebUIUrl[] = "chrome://blah"; | |
| 46 | |
| 47 class WebContentsImplTestWebUIControllerFactory | |
| 48 : public WebUIControllerFactory { | |
| 49 public: | |
| 50 WebUIController* CreateWebUIControllerForURL(WebUI* web_ui, | |
| 51 const GURL& url) const override { | |
| 52 if (!UseWebUI(url)) | |
| 53 return nullptr; | |
| 54 return new WebUIController(web_ui); | |
| 55 } | |
| 56 | |
| 57 WebUI::TypeID GetWebUIType(BrowserContext* browser_context, | |
| 58 const GURL& url) const override { | |
| 59 return WebUI::kNoWebUI; | |
| 60 } | |
| 61 | |
| 62 bool UseWebUIForURL(BrowserContext* browser_context, | |
| 63 const GURL& url) const override { | |
| 64 return UseWebUI(url); | |
| 65 } | |
| 66 | |
| 67 bool UseWebUIBindingsForURL(BrowserContext* browser_context, | |
| 68 const GURL& url) const override { | |
| 69 return UseWebUI(url); | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 bool UseWebUI(const GURL& url) const { | |
| 74 return url == GURL(kTestWebUIUrl); | |
| 75 } | |
| 76 }; | |
| 77 | |
| 78 class TestInterstitialPage; | 46 class TestInterstitialPage; |
| 79 | 47 |
| 80 class TestInterstitialPageDelegate : public InterstitialPageDelegate { | 48 class TestInterstitialPageDelegate : public InterstitialPageDelegate { |
| 81 public: | 49 public: |
| 82 explicit TestInterstitialPageDelegate(TestInterstitialPage* interstitial_page) | 50 explicit TestInterstitialPageDelegate(TestInterstitialPage* interstitial_page) |
| 83 : interstitial_page_(interstitial_page) {} | 51 : interstitial_page_(interstitial_page) {} |
| 84 void CommandReceived(const std::string& command) override; | 52 void CommandReceived(const std::string& command) override; |
| 85 std::string GetHTMLContents() override { return std::string(); } | 53 std::string GetHTMLContents() override { return std::string(); } |
| 86 void OnDontProceed() override; | 54 void OnDontProceed() override; |
| 87 void OnProceed() override; | 55 void OnProceed() override; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 } | 223 } |
| 256 | 224 |
| 257 private: | 225 private: |
| 258 bool assign_site_for_url_; | 226 bool assign_site_for_url_; |
| 259 }; | 227 }; |
| 260 | 228 |
| 261 class WebContentsImplTest : public RenderViewHostImplTestHarness { | 229 class WebContentsImplTest : public RenderViewHostImplTestHarness { |
| 262 public: | 230 public: |
| 263 void SetUp() override { | 231 void SetUp() override { |
| 264 RenderViewHostImplTestHarness::SetUp(); | 232 RenderViewHostImplTestHarness::SetUp(); |
| 265 WebUIControllerFactory::RegisterFactory(&factory_); | 233 WebUIControllerFactory::RegisterFactory( |
| 234 ContentWebUIControllerFactory::GetInstance()); |
| 266 } | 235 } |
| 267 | 236 |
| 268 void TearDown() override { | 237 void TearDown() override { |
| 269 WebUIControllerFactory::UnregisterFactoryForTesting(&factory_); | 238 WebUIControllerFactory::UnregisterFactoryForTesting( |
| 239 ContentWebUIControllerFactory::GetInstance()); |
| 270 RenderViewHostImplTestHarness::TearDown(); | 240 RenderViewHostImplTestHarness::TearDown(); |
| 271 } | 241 } |
| 272 | |
| 273 private: | |
| 274 WebContentsImplTestWebUIControllerFactory factory_; | |
| 275 }; | 242 }; |
| 276 | 243 |
| 277 class TestWebContentsObserver : public WebContentsObserver { | 244 class TestWebContentsObserver : public WebContentsObserver { |
| 278 public: | 245 public: |
| 279 explicit TestWebContentsObserver(WebContents* contents) | 246 explicit TestWebContentsObserver(WebContents* contents) |
| 280 : WebContentsObserver(contents) { | 247 : WebContentsObserver(contents) { |
| 281 } | 248 } |
| 282 ~TestWebContentsObserver() override {} | 249 ~TestWebContentsObserver() override {} |
| 283 | 250 |
| 284 void DidFinishLoad(RenderFrameHost* render_frame_host, | 251 void DidFinishLoad(RenderFrameHost* render_frame_host, |
| (...skipping 800 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1085 EXPECT_FALSE(orig_rfh->IsWaitingForBeforeUnloadACK()); | 1052 EXPECT_FALSE(orig_rfh->IsWaitingForBeforeUnloadACK()); |
| 1086 SiteInstance* instance2 = contents()->GetSiteInstance(); | 1053 SiteInstance* instance2 = contents()->GetSiteInstance(); |
| 1087 EXPECT_FALSE(contents()->CrossProcessNavigationPending()); | 1054 EXPECT_FALSE(contents()->CrossProcessNavigationPending()); |
| 1088 EXPECT_EQ(orig_rfh, contents()->GetMainFrame()); | 1055 EXPECT_EQ(orig_rfh, contents()->GetMainFrame()); |
| 1089 EXPECT_EQ(instance1, instance2); | 1056 EXPECT_EQ(instance1, instance2); |
| 1090 EXPECT_EQ(nullptr, contents()->GetPendingMainFrame()); | 1057 EXPECT_EQ(nullptr, contents()->GetPendingMainFrame()); |
| 1091 } | 1058 } |
| 1092 | 1059 |
| 1093 TEST_F(WebContentsImplTest, CrossSiteNavigationBackPreempted) { | 1060 TEST_F(WebContentsImplTest, CrossSiteNavigationBackPreempted) { |
| 1094 // Start with a web ui page, which gets a new RVH with WebUI bindings. | 1061 // Start with a web ui page, which gets a new RVH with WebUI bindings. |
| 1095 const GURL url1("chrome://blah"); | 1062 const GURL url1("chrome://gpu"); |
| 1096 controller().LoadURL( | 1063 controller().LoadURL( |
| 1097 url1, Referrer(), ui::PAGE_TRANSITION_TYPED, std::string()); | 1064 url1, Referrer(), ui::PAGE_TRANSITION_TYPED, std::string()); |
| 1098 int entry_id = controller().GetPendingEntry()->GetUniqueID(); | 1065 int entry_id = controller().GetPendingEntry()->GetUniqueID(); |
| 1099 TestRenderFrameHost* ntp_rfh = contents()->GetMainFrame(); | 1066 TestRenderFrameHost* ntp_rfh = contents()->GetMainFrame(); |
| 1100 ntp_rfh->PrepareForCommit(); | 1067 ntp_rfh->PrepareForCommit(); |
| 1101 contents()->TestDidNavigate(ntp_rfh, 1, entry_id, true, url1, | 1068 contents()->TestDidNavigate(ntp_rfh, 1, entry_id, true, url1, |
| 1102 ui::PAGE_TRANSITION_TYPED); | 1069 ui::PAGE_TRANSITION_TYPED); |
| 1103 NavigationEntry* entry1 = controller().GetLastCommittedEntry(); | 1070 NavigationEntry* entry1 = controller().GetLastCommittedEntry(); |
| 1104 SiteInstance* instance1 = contents()->GetSiteInstance(); | 1071 SiteInstance* instance1 = contents()->GetSiteInstance(); |
| 1105 | 1072 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1233 orig_rfh->PrepareForCommit(); | 1200 orig_rfh->PrepareForCommit(); |
| 1234 EXPECT_FALSE(orig_rfh->IsWaitingForBeforeUnloadACK()); | 1201 EXPECT_FALSE(orig_rfh->IsWaitingForBeforeUnloadACK()); |
| 1235 EXPECT_TRUE(contents()->CrossProcessNavigationPending()); | 1202 EXPECT_TRUE(contents()->CrossProcessNavigationPending()); |
| 1236 } | 1203 } |
| 1237 | 1204 |
| 1238 // Test that a cross-site navigation is not preempted if the previous | 1205 // Test that a cross-site navigation is not preempted if the previous |
| 1239 // renderer sends a FrameNavigate message just before being told to stop. | 1206 // renderer sends a FrameNavigate message just before being told to stop. |
| 1240 // We should only preempt the cross-site navigation if the previous renderer | 1207 // We should only preempt the cross-site navigation if the previous renderer |
| 1241 // has started a new navigation. See http://crbug.com/79176. | 1208 // has started a new navigation. See http://crbug.com/79176. |
| 1242 TEST_F(WebContentsImplTest, CrossSiteNotPreemptedDuringBeforeUnload) { | 1209 TEST_F(WebContentsImplTest, CrossSiteNotPreemptedDuringBeforeUnload) { |
| 1243 // Navigate to NTP URL. | 1210 // Navigate to WebUI URL. |
| 1244 const GURL url("chrome://blah"); | 1211 const GURL url("chrome://gpu"); |
| 1245 controller().LoadURL( | 1212 controller().LoadURL( |
| 1246 url, Referrer(), ui::PAGE_TRANSITION_TYPED, std::string()); | 1213 url, Referrer(), ui::PAGE_TRANSITION_TYPED, std::string()); |
| 1247 int entry1_id = controller().GetPendingEntry()->GetUniqueID(); | 1214 int entry1_id = controller().GetPendingEntry()->GetUniqueID(); |
| 1248 TestRenderFrameHost* orig_rfh = contents()->GetMainFrame(); | 1215 TestRenderFrameHost* orig_rfh = contents()->GetMainFrame(); |
| 1249 EXPECT_FALSE(contents()->CrossProcessNavigationPending()); | 1216 EXPECT_FALSE(contents()->CrossProcessNavigationPending()); |
| 1250 | 1217 |
| 1251 // Navigate to new site, with the beforeunload request in flight. | 1218 // Navigate to new site, with the beforeunload request in flight. |
| 1252 const GURL url2("http://www.yahoo.com"); | 1219 const GURL url2("http://www.yahoo.com"); |
| 1253 controller().LoadURL( | 1220 controller().LoadURL( |
| 1254 url2, Referrer(), ui::PAGE_TRANSITION_TYPED, std::string()); | 1221 url2, Referrer(), ui::PAGE_TRANSITION_TYPED, std::string()); |
| 1255 int entry2_id = controller().GetPendingEntry()->GetUniqueID(); | 1222 int entry2_id = controller().GetPendingEntry()->GetUniqueID(); |
| 1256 TestRenderFrameHost* pending_rfh = contents()->GetPendingMainFrame(); | 1223 TestRenderFrameHost* pending_rfh = contents()->GetPendingMainFrame(); |
| 1257 EXPECT_TRUE(contents()->CrossProcessNavigationPending()); | 1224 EXPECT_TRUE(contents()->CrossProcessNavigationPending()); |
| 1258 EXPECT_TRUE(orig_rfh->IsWaitingForBeforeUnloadACK()); | 1225 EXPECT_TRUE(orig_rfh->IsWaitingForBeforeUnloadACK()); |
| 1259 | 1226 |
| 1260 // Suppose the first navigation tries to commit now, with a | 1227 // Suppose the first navigation tries to commit now, with a |
| 1261 // FrameMsg_Stop in flight. This should not cancel the pending navigation, | 1228 // FrameMsg_Stop in flight. This should not cancel the pending navigation, |
| 1262 // but it should act as if the beforeunload ack arrived. | 1229 // but it should act as if the beforeunload ack arrived. |
| 1263 orig_rfh->SendNavigate(1, entry1_id, true, GURL("chrome://blah")); | 1230 orig_rfh->SendNavigate(1, entry1_id, true, GURL("chrome://gpu")); |
| 1264 EXPECT_TRUE(contents()->CrossProcessNavigationPending()); | 1231 EXPECT_TRUE(contents()->CrossProcessNavigationPending()); |
| 1265 EXPECT_EQ(orig_rfh, contents()->GetMainFrame()); | 1232 EXPECT_EQ(orig_rfh, contents()->GetMainFrame()); |
| 1266 EXPECT_FALSE(orig_rfh->IsWaitingForBeforeUnloadACK()); | 1233 EXPECT_FALSE(orig_rfh->IsWaitingForBeforeUnloadACK()); |
| 1267 | 1234 |
| 1268 // The pending navigation should be able to commit successfully. | 1235 // The pending navigation should be able to commit successfully. |
| 1269 contents()->TestDidNavigate(pending_rfh, 1, entry2_id, true, url2, | 1236 contents()->TestDidNavigate(pending_rfh, 1, entry2_id, true, url2, |
| 1270 ui::PAGE_TRANSITION_TYPED); | 1237 ui::PAGE_TRANSITION_TYPED); |
| 1271 EXPECT_FALSE(contents()->CrossProcessNavigationPending()); | 1238 EXPECT_FALSE(contents()->CrossProcessNavigationPending()); |
| 1272 EXPECT_EQ(pending_rfh, contents()->GetMainFrame()); | 1239 EXPECT_EQ(pending_rfh, contents()->GetMainFrame()); |
| 1273 } | 1240 } |
| (...skipping 1524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2798 EXPECT_EQ(0u, instance->GetRelatedActiveContentsCount()); | 2765 EXPECT_EQ(0u, instance->GetRelatedActiveContentsCount()); |
| 2799 | 2766 |
| 2800 scoped_ptr<TestWebContents> contents( | 2767 scoped_ptr<TestWebContents> contents( |
| 2801 TestWebContents::Create(browser_context(), instance.get())); | 2768 TestWebContents::Create(browser_context(), instance.get())); |
| 2802 EXPECT_EQ(1u, instance->GetRelatedActiveContentsCount()); | 2769 EXPECT_EQ(1u, instance->GetRelatedActiveContentsCount()); |
| 2803 | 2770 |
| 2804 // Navigate to a URL. | 2771 // Navigate to a URL. |
| 2805 contents->NavigateAndCommit(GURL("http://a.com")); | 2772 contents->NavigateAndCommit(GURL("http://a.com")); |
| 2806 EXPECT_EQ(1u, instance->GetRelatedActiveContentsCount()); | 2773 EXPECT_EQ(1u, instance->GetRelatedActiveContentsCount()); |
| 2807 | 2774 |
| 2775 // Navigate to a URL which sort of looks like a chrome:// url. |
| 2776 contents->NavigateAndCommit(GURL("http://gpu")); |
| 2777 EXPECT_EQ(1u, instance->GetRelatedActiveContentsCount()); |
| 2778 |
| 2808 // Navigate to a URL with WebUI. This will change BrowsingInstances. | 2779 // Navigate to a URL with WebUI. This will change BrowsingInstances. |
| 2809 const GURL kWebUIUrl = GURL(kTestWebUIUrl); | 2780 const GURL kWebUIUrl = GURL("chrome://gpu"); |
| 2810 contents->GetController().LoadURL(kWebUIUrl, | 2781 contents->GetController().LoadURL(kWebUIUrl, |
| 2811 Referrer(), | 2782 Referrer(), |
| 2812 ui::PAGE_TRANSITION_TYPED, | 2783 ui::PAGE_TRANSITION_TYPED, |
| 2813 std::string()); | 2784 std::string()); |
| 2814 int entry_id = contents->GetController().GetPendingEntry()->GetUniqueID(); | 2785 int entry_id = contents->GetController().GetPendingEntry()->GetUniqueID(); |
| 2815 contents->GetMainFrame()->PrepareForCommit(); | 2786 contents->GetMainFrame()->PrepareForCommit(); |
| 2816 EXPECT_TRUE(contents->CrossProcessNavigationPending()); | 2787 EXPECT_TRUE(contents->CrossProcessNavigationPending()); |
| 2817 scoped_refptr<SiteInstance> instance_webui( | 2788 scoped_refptr<SiteInstance> instance_webui( |
| 2818 contents->GetPendingMainFrame()->GetSiteInstance()); | 2789 contents->GetPendingMainFrame()->GetSiteInstance()); |
| 2819 EXPECT_FALSE(instance->IsRelatedSiteInstance(instance_webui.get())); | 2790 EXPECT_FALSE(instance->IsRelatedSiteInstance(instance_webui.get())); |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3140 | 3111 |
| 3141 // Crash the renderer. | 3112 // Crash the renderer. |
| 3142 contents()->GetMainFrame()->GetProcess()->SimulateCrash(); | 3113 contents()->GetMainFrame()->GetProcess()->SimulateCrash(); |
| 3143 | 3114 |
| 3144 // Verify that all the power save blockers have been released. | 3115 // Verify that all the power save blockers have been released. |
| 3145 EXPECT_FALSE(contents()->has_video_power_save_blocker_for_testing()); | 3116 EXPECT_FALSE(contents()->has_video_power_save_blocker_for_testing()); |
| 3146 EXPECT_FALSE(contents()->has_audio_power_save_blocker_for_testing()); | 3117 EXPECT_FALSE(contents()->has_audio_power_save_blocker_for_testing()); |
| 3147 } | 3118 } |
| 3148 | 3119 |
| 3149 } // namespace content | 3120 } // namespace content |
| OLD | NEW |