| 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/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
| 6 #include "base/stl_util.h" | 6 #include "base/stl_util.h" |
| 7 #include "base/string16.h" | 7 #include "base/string16.h" |
| 8 #include "content/browser/browser_thread_impl.h" | 8 #include "content/browser/browser_thread_impl.h" |
| 9 #include "content/browser/browsing_instance.h" | 9 #include "content/browser/browsing_instance.h" |
| 10 #include "content/browser/child_process_security_policy.h" | 10 #include "content/browser/child_process_security_policy.h" |
| 11 #include "content/browser/mock_content_browser_client.h" | 11 #include "content/browser/mock_content_browser_client.h" |
| 12 #include "content/browser/renderer_host/render_process_host_impl.h" | 12 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 13 #include "content/browser/renderer_host/render_view_host.h" | 13 #include "content/browser/renderer_host/render_view_host.h" |
| 14 #include "content/browser/renderer_host/test_render_view_host.h" | 14 #include "content/browser/renderer_host/test_render_view_host.h" |
| 15 #include "content/browser/site_instance.h" | 15 #include "content/browser/site_instance_impl.h" |
| 16 #include "content/browser/tab_contents/navigation_entry_impl.h" | 16 #include "content/browser/tab_contents/navigation_entry_impl.h" |
| 17 #include "content/browser/tab_contents/tab_contents.h" | 17 #include "content/browser/tab_contents/tab_contents.h" |
| 18 #include "content/browser/webui/empty_web_ui_factory.h" | 18 #include "content/browser/webui/empty_web_ui_factory.h" |
| 19 #include "content/public/common/content_client.h" | 19 #include "content/public/common/content_client.h" |
| 20 #include "content/public/common/content_constants.h" | 20 #include "content/public/common/content_constants.h" |
| 21 #include "content/public/common/url_constants.h" | 21 #include "content/public/common/url_constants.h" |
| 22 #include "content/test/test_browser_context.h" | 22 #include "content/test/test_browser_context.h" |
| 23 #include "googleurl/src/url_util.h" | 23 #include "googleurl/src/url_util.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 25 |
| 26 using content::BrowserThread; | 26 using content::BrowserThread; |
| 27 using content::BrowserThreadImpl; | 27 using content::BrowserThreadImpl; |
| 28 using content::NavigationEntry; | 28 using content::NavigationEntry; |
| 29 using content::NavigationEntryImpl; | 29 using content::NavigationEntryImpl; |
| 30 using content::SiteInstance; |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 const char kSameAsAnyInstanceURL[] = "about:internets"; | 34 const char kSameAsAnyInstanceURL[] = "about:internets"; |
| 34 | 35 |
| 35 const char kPrivilegedScheme[] = "privileged"; | 36 const char kPrivilegedScheme[] = "privileged"; |
| 36 | 37 |
| 37 class SiteInstanceTestWebUIFactory : public content::EmptyWebUIFactory { | 38 class SiteInstanceTestWebUIFactory : public content::EmptyWebUIFactory { |
| 38 public: | 39 public: |
| 39 virtual bool UseWebUIForURL(content::BrowserContext* browser_context, | 40 virtual bool UseWebUIForURL(content::BrowserContext* browser_context, |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 virtual ~TestBrowsingInstance() { | 146 virtual ~TestBrowsingInstance() { |
| 146 (*delete_counter_)++; | 147 (*delete_counter_)++; |
| 147 } | 148 } |
| 148 | 149 |
| 149 // Set by individual tests. | 150 // Set by individual tests. |
| 150 bool use_process_per_site_; | 151 bool use_process_per_site_; |
| 151 | 152 |
| 152 int* delete_counter_; | 153 int* delete_counter_; |
| 153 }; | 154 }; |
| 154 | 155 |
| 155 class TestSiteInstance : public SiteInstance { | 156 class TestSiteInstance : public SiteInstanceImpl { |
| 156 public: | 157 public: |
| 157 static TestSiteInstance* CreateTestSiteInstance( | 158 static TestSiteInstance* CreateTestSiteInstance( |
| 158 content::BrowserContext* browser_context, | 159 content::BrowserContext* browser_context, |
| 159 int* site_delete_counter, | 160 int* site_delete_counter, |
| 160 int* browsing_delete_counter) { | 161 int* browsing_delete_counter) { |
| 161 TestBrowsingInstance* browsing_instance = | 162 TestBrowsingInstance* browsing_instance = |
| 162 new TestBrowsingInstance(browser_context, browsing_delete_counter); | 163 new TestBrowsingInstance(browser_context, browsing_delete_counter); |
| 163 return new TestSiteInstance(browsing_instance, site_delete_counter); | 164 return new TestSiteInstance(browsing_instance, site_delete_counter); |
| 164 } | 165 } |
| 165 | 166 |
| 166 private: | 167 private: |
| 167 TestSiteInstance(BrowsingInstance* browsing_instance, int* delete_counter) | 168 TestSiteInstance(BrowsingInstance* browsing_instance, int* delete_counter) |
| 168 : SiteInstance(browsing_instance), delete_counter_(delete_counter) {} | 169 : SiteInstanceImpl(browsing_instance), delete_counter_(delete_counter) {} |
| 169 virtual ~TestSiteInstance() { | 170 virtual ~TestSiteInstance() { |
| 170 (*delete_counter_)++; | 171 (*delete_counter_)++; |
| 171 } | 172 } |
| 172 | 173 |
| 173 int* delete_counter_; | 174 int* delete_counter_; |
| 174 }; | 175 }; |
| 175 | 176 |
| 176 } // namespace | 177 } // namespace |
| 177 | 178 |
| 178 // Test to ensure no memory leaks for SiteInstance objects. | 179 // Test to ensure no memory leaks for SiteInstance objects. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 | 242 |
| 242 // Test that NavigationEntries with SiteInstances can be cloned, but that their | 243 // Test that NavigationEntries with SiteInstances can be cloned, but that their |
| 243 // SiteInstances can be changed afterwards. Also tests that the ref counts are | 244 // SiteInstances can be changed afterwards. Also tests that the ref counts are |
| 244 // updated properly after the change. | 245 // updated properly after the change. |
| 245 TEST_F(SiteInstanceTest, CloneNavigationEntry) { | 246 TEST_F(SiteInstanceTest, CloneNavigationEntry) { |
| 246 int site_delete_counter1 = 0; | 247 int site_delete_counter1 = 0; |
| 247 int site_delete_counter2 = 0; | 248 int site_delete_counter2 = 0; |
| 248 int browsing_delete_counter = 0; | 249 int browsing_delete_counter = 0; |
| 249 const GURL url("test:foo"); | 250 const GURL url("test:foo"); |
| 250 | 251 |
| 251 SiteInstance* instance1 = | 252 SiteInstanceImpl* instance1 = |
| 252 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter1, | 253 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter1, |
| 253 &browsing_delete_counter); | 254 &browsing_delete_counter); |
| 254 SiteInstance* instance2 = | 255 SiteInstanceImpl* instance2 = |
| 255 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter2, | 256 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter2, |
| 256 &browsing_delete_counter); | 257 &browsing_delete_counter); |
| 257 | 258 |
| 258 NavigationEntryImpl* e1 = new NavigationEntryImpl( | 259 NavigationEntryImpl* e1 = new NavigationEntryImpl( |
| 259 instance1, 0, url, content::Referrer(), string16(), | 260 instance1, 0, url, content::Referrer(), string16(), |
| 260 content::PAGE_TRANSITION_LINK, false); | 261 content::PAGE_TRANSITION_LINK, false); |
| 261 // Clone the entry | 262 // Clone the entry |
| 262 NavigationEntryImpl* e2 = new NavigationEntryImpl(*e1); | 263 NavigationEntryImpl* e2 = new NavigationEntryImpl(*e1); |
| 263 | 264 |
| 264 // Should be able to change the SiteInstance of the cloned entry. | 265 // Should be able to change the SiteInstance of the cloned entry. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 277 | 278 |
| 278 // Both BrowsingInstances are also now deleted | 279 // Both BrowsingInstances are also now deleted |
| 279 EXPECT_EQ(2, browsing_delete_counter); | 280 EXPECT_EQ(2, browsing_delete_counter); |
| 280 } | 281 } |
| 281 | 282 |
| 282 // Test to ensure GetProcess returns and creates processes correctly. | 283 // Test to ensure GetProcess returns and creates processes correctly. |
| 283 TEST_F(SiteInstanceTest, GetProcess) { | 284 TEST_F(SiteInstanceTest, GetProcess) { |
| 284 // Ensure that GetProcess returns a process. | 285 // Ensure that GetProcess returns a process. |
| 285 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 286 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 286 scoped_ptr<content::RenderProcessHost> host1; | 287 scoped_ptr<content::RenderProcessHost> host1; |
| 287 scoped_refptr<SiteInstance> instance( | 288 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>( |
| 288 SiteInstance::CreateSiteInstance(browser_context.get())); | 289 SiteInstance::Create(browser_context.get()))); |
| 289 host1.reset(instance->GetProcess()); | 290 host1.reset(instance->GetProcess()); |
| 290 EXPECT_TRUE(host1.get() != NULL); | 291 EXPECT_TRUE(host1.get() != NULL); |
| 291 | 292 |
| 292 // Ensure that GetProcess creates a new process. | 293 // Ensure that GetProcess creates a new process. |
| 293 scoped_refptr<SiteInstance> instance2( | 294 scoped_refptr<SiteInstanceImpl> instance2(static_cast<SiteInstanceImpl*>( |
| 294 SiteInstance::CreateSiteInstance(browser_context.get())); | 295 SiteInstance::Create(browser_context.get()))); |
| 295 scoped_ptr<content::RenderProcessHost> host2(instance2->GetProcess()); | 296 scoped_ptr<content::RenderProcessHost> host2(instance2->GetProcess()); |
| 296 EXPECT_TRUE(host2.get() != NULL); | 297 EXPECT_TRUE(host2.get() != NULL); |
| 297 EXPECT_NE(host1.get(), host2.get()); | 298 EXPECT_NE(host1.get(), host2.get()); |
| 298 } | 299 } |
| 299 | 300 |
| 300 // Test to ensure SetSite and site() work properly. | 301 // Test to ensure SetSite and site() work properly. |
| 301 TEST_F(SiteInstanceTest, SetSite) { | 302 TEST_F(SiteInstanceTest, SetSite) { |
| 302 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL)); | 303 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>( |
| 303 EXPECT_FALSE(instance->has_site()); | 304 SiteInstance::Create(NULL))); |
| 304 EXPECT_TRUE(instance->site().is_empty()); | 305 EXPECT_FALSE(instance->HasSite()); |
| 306 EXPECT_TRUE(instance->GetSite().is_empty()); |
| 305 | 307 |
| 306 instance->SetSite(GURL("http://www.google.com/index.html")); | 308 instance->SetSite(GURL("http://www.google.com/index.html")); |
| 307 EXPECT_EQ(GURL("http://google.com"), instance->site()); | 309 EXPECT_EQ(GURL("http://google.com"), instance->GetSite()); |
| 308 | 310 |
| 309 EXPECT_TRUE(instance->has_site()); | 311 EXPECT_TRUE(instance->HasSite()); |
| 310 } | 312 } |
| 311 | 313 |
| 312 // Test to ensure GetSiteForURL properly returns sites for URLs. | 314 // Test to ensure GetSiteForURL properly returns sites for URLs. |
| 313 TEST_F(SiteInstanceTest, GetSiteForURL) { | 315 TEST_F(SiteInstanceTest, GetSiteForURL) { |
| 314 // Pages are irrelevant. | 316 // Pages are irrelevant. |
| 315 GURL test_url = GURL("http://www.google.com/index.html"); | 317 GURL test_url = GURL("http://www.google.com/index.html"); |
| 316 EXPECT_EQ(GURL("http://google.com"), | 318 EXPECT_EQ(GURL("http://google.com"), |
| 317 SiteInstance::GetSiteForURL(NULL, test_url)); | 319 SiteInstance::GetSiteForURL(NULL, test_url)); |
| 318 | 320 |
| 319 // Ports are irrlevant. | 321 // Ports are irrlevant. |
| 320 test_url = GURL("https://www.google.com:8080"); | 322 test_url = GURL("https://www.google.com:8080"); |
| 321 EXPECT_EQ(GURL("https://google.com"), | 323 EXPECT_EQ(GURL("https://google.com"), |
| 322 SiteInstance::GetSiteForURL(NULL, test_url)); | 324 SiteInstance::GetSiteForURL(NULL, test_url)); |
| 323 | 325 |
| 324 // Javascript URLs have no site. | 326 // Javascript URLs have no site. |
| 325 test_url = GURL("javascript:foo();"); | 327 test_url = GURL("javascript:foo();"); |
| 326 EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); | 328 EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); |
| 327 | 329 |
| 328 test_url = GURL("http://foo/a.html"); | 330 test_url = GURL("http://foo/a.html"); |
| 329 EXPECT_EQ(GURL("http://foo"), SiteInstance::GetSiteForURL(NULL, test_url)); | 331 EXPECT_EQ(GURL("http://foo"), SiteInstance::GetSiteForURL( |
| 332 NULL, test_url)); |
| 330 | 333 |
| 331 test_url = GURL("file:///C:/Downloads/"); | 334 test_url = GURL("file:///C:/Downloads/"); |
| 332 EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); | 335 EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); |
| 333 | 336 |
| 334 // TODO(creis): Do we want to special case file URLs to ensure they have | 337 // TODO(creis): Do we want to special case file URLs to ensure they have |
| 335 // either no site or a special "file://" site? We currently return | 338 // either no site or a special "file://" site? We currently return |
| 336 // "file://home/" as the site, which seems broken. | 339 // "file://home/" as the site, which seems broken. |
| 337 // test_url = GURL("file://home/"); | 340 // test_url = GURL("file://home/"); |
| 338 // EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); | 341 // EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); |
| 339 } | 342 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 361 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_port)); | 364 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_port)); |
| 362 | 365 |
| 363 // JavaScript links should be considered same site for anything. | 366 // JavaScript links should be considered same site for anything. |
| 364 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo)); | 367 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo)); |
| 365 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_https)); | 368 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_https)); |
| 366 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_port)); | 369 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_port)); |
| 367 | 370 |
| 368 // The URLs specified by the ContentBrowserClient should also be treated as | 371 // The URLs specified by the ContentBrowserClient should also be treated as |
| 369 // same site. | 372 // same site. |
| 370 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_crash, url_foo)); | 373 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_crash, url_foo)); |
| 371 EXPECT_TRUE( | 374 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_browser_specified, |
| 372 SiteInstance::IsSameWebSite(NULL, url_browser_specified, url_foo)); | 375 url_foo)); |
| 373 } | 376 } |
| 374 | 377 |
| 375 // Test to ensure that there is only one SiteInstance per site in a given | 378 // Test to ensure that there is only one SiteInstance per site in a given |
| 376 // BrowsingInstance, when process-per-site is not in use. | 379 // BrowsingInstance, when process-per-site is not in use. |
| 377 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { | 380 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { |
| 378 int delete_counter = 0; | 381 int delete_counter = 0; |
| 379 TestBrowsingInstance* browsing_instance = | 382 TestBrowsingInstance* browsing_instance = |
| 380 new TestBrowsingInstance(NULL, &delete_counter); | 383 new TestBrowsingInstance(NULL, &delete_counter); |
| 381 browsing_instance->set_use_process_per_site(false); | 384 browsing_instance->set_use_process_per_site(false); |
| 382 | 385 |
| 383 const GURL url_a1("http://www.google.com/1.html"); | 386 const GURL url_a1("http://www.google.com/1.html"); |
| 384 scoped_refptr<SiteInstance> site_instance_a1( | 387 scoped_refptr<SiteInstanceImpl> site_instance_a1( |
| 385 browsing_instance->GetSiteInstanceForURL(url_a1)); | 388 static_cast<SiteInstanceImpl*>( |
| 389 browsing_instance->GetSiteInstanceForURL(url_a1))); |
| 386 EXPECT_TRUE(site_instance_a1.get() != NULL); | 390 EXPECT_TRUE(site_instance_a1.get() != NULL); |
| 387 | 391 |
| 388 // A separate site should create a separate SiteInstance. | 392 // A separate site should create a separate SiteInstance. |
| 389 const GURL url_b1("http://www.yahoo.com/"); | 393 const GURL url_b1("http://www.yahoo.com/"); |
| 390 scoped_refptr<SiteInstance> site_instance_b1( | 394 scoped_refptr<SiteInstanceImpl> site_instance_b1( |
| 391 browsing_instance->GetSiteInstanceForURL(url_b1)); | 395 static_cast<SiteInstanceImpl*>( |
| 396 browsing_instance->GetSiteInstanceForURL(url_b1))); |
| 392 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); | 397 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); |
| 393 | 398 |
| 394 // Getting the new SiteInstance from the BrowsingInstance and from another | 399 // Getting the new SiteInstance from the BrowsingInstance and from another |
| 395 // SiteInstance in the BrowsingInstance should give the same result. | 400 // SiteInstance in the BrowsingInstance should give the same result. |
| 396 EXPECT_EQ(site_instance_b1.get(), | 401 EXPECT_EQ(site_instance_b1.get(), |
| 397 site_instance_a1->GetRelatedSiteInstance(url_b1)); | 402 site_instance_a1->GetRelatedSiteInstance(url_b1)); |
| 398 | 403 |
| 399 // A second visit to the original site should return the same SiteInstance. | 404 // A second visit to the original site should return the same SiteInstance. |
| 400 const GURL url_a2("http://www.google.com/2.html"); | 405 const GURL url_a2("http://www.google.com/2.html"); |
| 401 EXPECT_EQ(site_instance_a1.get(), | 406 EXPECT_EQ(site_instance_a1.get(), |
| 402 browsing_instance->GetSiteInstanceForURL(url_a2)); | 407 browsing_instance->GetSiteInstanceForURL(url_a2)); |
| 403 EXPECT_EQ(site_instance_a1.get(), | 408 EXPECT_EQ(site_instance_a1.get(), |
| 404 site_instance_a1->GetRelatedSiteInstance(url_a2)); | 409 site_instance_a1->GetRelatedSiteInstance(url_a2)); |
| 405 | 410 |
| 406 // A visit to the original site in a new BrowsingInstance (same or different | 411 // A visit to the original site in a new BrowsingInstance (same or different |
| 407 // browser context) should return a different SiteInstance. | 412 // browser context) should return a different SiteInstance. |
| 408 TestBrowsingInstance* browsing_instance2 = | 413 TestBrowsingInstance* browsing_instance2 = |
| 409 new TestBrowsingInstance(NULL, &delete_counter); | 414 new TestBrowsingInstance(NULL, &delete_counter); |
| 410 browsing_instance2->set_use_process_per_site(false); | 415 browsing_instance2->set_use_process_per_site(false); |
| 411 // Ensure the new SiteInstance is ref counted so that it gets deleted. | 416 // Ensure the new SiteInstance is ref counted so that it gets deleted. |
| 412 scoped_refptr<SiteInstance> site_instance_a2_2( | 417 scoped_refptr<SiteInstanceImpl> site_instance_a2_2( |
| 413 browsing_instance2->GetSiteInstanceForURL(url_a2)); | 418 static_cast<SiteInstanceImpl*>( |
| 419 browsing_instance2->GetSiteInstanceForURL(url_a2))); |
| 414 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); | 420 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); |
| 415 | 421 |
| 416 // Should be able to see that we do have SiteInstances. | 422 // Should be able to see that we do have SiteInstances. |
| 417 EXPECT_TRUE(browsing_instance->HasSiteInstance( | 423 EXPECT_TRUE(browsing_instance->HasSiteInstance( |
| 418 GURL("http://mail.google.com"))); | 424 GURL("http://mail.google.com"))); |
| 419 EXPECT_TRUE(browsing_instance2->HasSiteInstance( | 425 EXPECT_TRUE(browsing_instance2->HasSiteInstance( |
| 420 GURL("http://mail.google.com"))); | 426 GURL("http://mail.google.com"))); |
| 421 EXPECT_TRUE(browsing_instance->HasSiteInstance( | 427 EXPECT_TRUE(browsing_instance->HasSiteInstance( |
| 422 GURL("http://mail.yahoo.com"))); | 428 GURL("http://mail.yahoo.com"))); |
| 423 | 429 |
| 424 // Should be able to see that we don't have SiteInstances. | 430 // Should be able to see that we don't have SiteInstances. |
| 425 EXPECT_FALSE(browsing_instance->HasSiteInstance( | 431 EXPECT_FALSE(browsing_instance->HasSiteInstance( |
| 426 GURL("https://www.google.com"))); | 432 GURL("https://www.google.com"))); |
| 427 EXPECT_FALSE(browsing_instance2->HasSiteInstance( | 433 EXPECT_FALSE(browsing_instance2->HasSiteInstance( |
| 428 GURL("http://www.yahoo.com"))); | 434 GURL("http://www.yahoo.com"))); |
| 429 | 435 |
| 430 // browsing_instances will be deleted when their SiteInstances are deleted | 436 // browsing_instances will be deleted when their SiteInstances are deleted |
| 431 } | 437 } |
| 432 | 438 |
| 433 // Test to ensure that there is only one SiteInstance per site for an entire | 439 // Test to ensure that there is only one SiteInstance per site for an entire |
| 434 // BrowserContext, if process-per-site is in use. | 440 // BrowserContext, if process-per-site is in use. |
| 435 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { | 441 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { |
| 436 int delete_counter = 0; | 442 int delete_counter = 0; |
| 437 TestBrowsingInstance* browsing_instance = | 443 TestBrowsingInstance* browsing_instance = |
| 438 new TestBrowsingInstance(NULL, &delete_counter); | 444 new TestBrowsingInstance(NULL, &delete_counter); |
| 439 browsing_instance->set_use_process_per_site(true); | 445 browsing_instance->set_use_process_per_site(true); |
| 440 | 446 |
| 441 const GURL url_a1("http://www.google.com/1.html"); | 447 const GURL url_a1("http://www.google.com/1.html"); |
| 442 scoped_refptr<SiteInstance> site_instance_a1( | 448 scoped_refptr<SiteInstanceImpl> site_instance_a1( |
| 443 browsing_instance->GetSiteInstanceForURL(url_a1)); | 449 static_cast<SiteInstanceImpl*>( |
| 450 browsing_instance->GetSiteInstanceForURL(url_a1))); |
| 444 EXPECT_TRUE(site_instance_a1.get() != NULL); | 451 EXPECT_TRUE(site_instance_a1.get() != NULL); |
| 445 | 452 |
| 446 // A separate site should create a separate SiteInstance. | 453 // A separate site should create a separate SiteInstance. |
| 447 const GURL url_b1("http://www.yahoo.com/"); | 454 const GURL url_b1("http://www.yahoo.com/"); |
| 448 scoped_refptr<SiteInstance> site_instance_b1( | 455 scoped_refptr<SiteInstanceImpl> site_instance_b1( |
| 449 browsing_instance->GetSiteInstanceForURL(url_b1)); | 456 static_cast<SiteInstanceImpl*>( |
| 457 browsing_instance->GetSiteInstanceForURL(url_b1))); |
| 450 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); | 458 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); |
| 451 | 459 |
| 452 // Getting the new SiteInstance from the BrowsingInstance and from another | 460 // Getting the new SiteInstance from the BrowsingInstance and from another |
| 453 // SiteInstance in the BrowsingInstance should give the same result. | 461 // SiteInstance in the BrowsingInstance should give the same result. |
| 454 EXPECT_EQ(site_instance_b1.get(), | 462 EXPECT_EQ(site_instance_b1.get(), |
| 455 site_instance_a1->GetRelatedSiteInstance(url_b1)); | 463 site_instance_a1->GetRelatedSiteInstance(url_b1)); |
| 456 | 464 |
| 457 // A second visit to the original site should return the same SiteInstance. | 465 // A second visit to the original site should return the same SiteInstance. |
| 458 const GURL url_a2("http://www.google.com/2.html"); | 466 const GURL url_a2("http://www.google.com/2.html"); |
| 459 EXPECT_EQ(site_instance_a1.get(), | 467 EXPECT_EQ(site_instance_a1.get(), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 472 EXPECT_EQ(site_instance_a1.get(), | 480 EXPECT_EQ(site_instance_a1.get(), |
| 473 browsing_instance2->GetSiteInstanceForURL(url_a2)); | 481 browsing_instance2->GetSiteInstanceForURL(url_a2)); |
| 474 | 482 |
| 475 // A visit to the original site in a new BrowsingInstance (different browser | 483 // A visit to the original site in a new BrowsingInstance (different browser |
| 476 // context) should return a different SiteInstance. | 484 // context) should return a different SiteInstance. |
| 477 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 485 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 478 TestBrowsingInstance* browsing_instance3 = | 486 TestBrowsingInstance* browsing_instance3 = |
| 479 new TestBrowsingInstance(browser_context.get(), &delete_counter); | 487 new TestBrowsingInstance(browser_context.get(), &delete_counter); |
| 480 browsing_instance3->set_use_process_per_site(true); | 488 browsing_instance3->set_use_process_per_site(true); |
| 481 // Ensure the new SiteInstance is ref counted so that it gets deleted. | 489 // Ensure the new SiteInstance is ref counted so that it gets deleted. |
| 482 scoped_refptr<SiteInstance> site_instance_a2_3( | 490 scoped_refptr<SiteInstanceImpl> site_instance_a2_3( |
| 483 browsing_instance3->GetSiteInstanceForURL(url_a2)); | 491 static_cast<SiteInstanceImpl*>( |
| 492 browsing_instance3->GetSiteInstanceForURL(url_a2))); |
| 484 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); | 493 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); |
| 485 | 494 |
| 486 // Should be able to see that we do have SiteInstances. | 495 // Should be able to see that we do have SiteInstances. |
| 487 EXPECT_TRUE(browsing_instance->HasSiteInstance( | 496 EXPECT_TRUE(browsing_instance->HasSiteInstance( |
| 488 GURL("http://mail.google.com"))); // visited before | 497 GURL("http://mail.google.com"))); // visited before |
| 489 EXPECT_TRUE(browsing_instance2->HasSiteInstance( | 498 EXPECT_TRUE(browsing_instance2->HasSiteInstance( |
| 490 GURL("http://mail.google.com"))); // visited before | 499 GURL("http://mail.google.com"))); // visited before |
| 491 EXPECT_TRUE(browsing_instance->HasSiteInstance( | 500 EXPECT_TRUE(browsing_instance->HasSiteInstance( |
| 492 GURL("http://mail.yahoo.com"))); // visited before | 501 GURL("http://mail.yahoo.com"))); // visited before |
| 493 EXPECT_TRUE(browsing_instance2->HasSiteInstance( | 502 EXPECT_TRUE(browsing_instance2->HasSiteInstance( |
| 494 GURL("http://www.yahoo.com"))); // different BI, but same browser context | 503 GURL("http://www.yahoo.com"))); // different BI, but same browser context |
| 495 | 504 |
| 496 // Should be able to see that we don't have SiteInstances. | 505 // Should be able to see that we don't have SiteInstances. |
| 497 EXPECT_FALSE(browsing_instance->HasSiteInstance( | 506 EXPECT_FALSE(browsing_instance->HasSiteInstance( |
| 498 GURL("https://www.google.com"))); // not visited before | 507 GURL("https://www.google.com"))); // not visited before |
| 499 EXPECT_FALSE(browsing_instance3->HasSiteInstance( | 508 EXPECT_FALSE(browsing_instance3->HasSiteInstance( |
| 500 GURL("http://www.yahoo.com"))); // different BI, different context | 509 GURL("http://www.yahoo.com"))); // different BI, different context |
| 501 | 510 |
| 502 // browsing_instances will be deleted when their SiteInstances are deleted | 511 // browsing_instances will be deleted when their SiteInstances are deleted |
| 503 } | 512 } |
| 504 | 513 |
| 505 static SiteInstance* CreateSiteInstance( | 514 static SiteInstanceImpl* CreateSiteInstance( |
| 506 content::RenderProcessHostFactory* factory, const GURL& url) { | 515 content::RenderProcessHostFactory* factory, const GURL& url) { |
| 507 SiteInstance* instance = SiteInstance::CreateSiteInstanceForURL(NULL, url); | 516 SiteInstanceImpl* instance = |
| 517 reinterpret_cast<SiteInstanceImpl*>( |
| 518 SiteInstance::CreateForURL(NULL, url)); |
| 508 instance->set_render_process_host_factory(factory); | 519 instance->set_render_process_host_factory(factory); |
| 509 return instance; | 520 return instance; |
| 510 } | 521 } |
| 511 | 522 |
| 512 // Test to ensure that pages that require certain privileges are grouped | 523 // Test to ensure that pages that require certain privileges are grouped |
| 513 // in processes with similar pages. | 524 // in processes with similar pages. |
| 514 TEST_F(SiteInstanceTest, ProcessSharingByType) { | 525 TEST_F(SiteInstanceTest, ProcessSharingByType) { |
| 515 MockRenderProcessHostFactory rph_factory; | 526 MockRenderProcessHostFactory rph_factory; |
| 516 ChildProcessSecurityPolicy* policy = | 527 ChildProcessSecurityPolicy* policy = |
| 517 ChildProcessSecurityPolicy::GetInstance(); | 528 ChildProcessSecurityPolicy::GetInstance(); |
| 518 | 529 |
| 519 // Make a bunch of mock renderers so that we hit the limit. | 530 // Make a bunch of mock renderers so that we hit the limit. |
| 520 std::vector<MockRenderProcessHost*> hosts; | 531 std::vector<MockRenderProcessHost*> hosts; |
| 521 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) | 532 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) |
| 522 hosts.push_back(new MockRenderProcessHost(NULL)); | 533 hosts.push_back(new MockRenderProcessHost(NULL)); |
| 523 | 534 |
| 524 // Create some extension instances and make sure they share a process. | 535 // Create some extension instances and make sure they share a process. |
| 525 scoped_refptr<SiteInstance> extension1_instance( | 536 scoped_refptr<SiteInstanceImpl> extension1_instance( |
| 526 CreateSiteInstance(&rph_factory, | 537 CreateSiteInstance(&rph_factory, |
| 527 GURL(kPrivilegedScheme + std::string("://foo/bar")))); | 538 GURL(kPrivilegedScheme + std::string("://foo/bar")))); |
| 528 set_privileged_process_id(extension1_instance->GetProcess()->GetID()); | 539 set_privileged_process_id(extension1_instance->GetProcess()->GetID()); |
| 529 | 540 |
| 530 scoped_refptr<SiteInstance> extension2_instance( | 541 scoped_refptr<SiteInstanceImpl> extension2_instance( |
| 531 CreateSiteInstance(&rph_factory, | 542 CreateSiteInstance(&rph_factory, |
| 532 GURL(kPrivilegedScheme + std::string("://baz/bar")))); | 543 GURL(kPrivilegedScheme + std::string("://baz/bar")))); |
| 533 | 544 |
| 534 scoped_ptr<content::RenderProcessHost> extension_host( | 545 scoped_ptr<content::RenderProcessHost> extension_host( |
| 535 extension1_instance->GetProcess()); | 546 extension1_instance->GetProcess()); |
| 536 EXPECT_EQ(extension1_instance->GetProcess(), | 547 EXPECT_EQ(extension1_instance->GetProcess(), |
| 537 extension2_instance->GetProcess()); | 548 extension2_instance->GetProcess()); |
| 538 | 549 |
| 539 // Create some WebUI instances and make sure they share a process. | 550 // Create some WebUI instances and make sure they share a process. |
| 540 scoped_refptr<SiteInstance> webui1_instance(CreateSiteInstance(&rph_factory, | 551 scoped_refptr<SiteInstanceImpl> webui1_instance(CreateSiteInstance( |
| 552 &rph_factory, |
| 541 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); | 553 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); |
| 542 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID()); | 554 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID()); |
| 543 | 555 |
| 544 scoped_refptr<SiteInstance> webui2_instance(CreateSiteInstance(&rph_factory, | 556 scoped_refptr<SiteInstanceImpl> webui2_instance(CreateSiteInstance( |
| 557 &rph_factory, |
| 545 GURL(chrome::kChromeUIScheme + std::string("://history")))); | 558 GURL(chrome::kChromeUIScheme + std::string("://history")))); |
| 546 | 559 |
| 547 scoped_ptr<content::RenderProcessHost> dom_host( | 560 scoped_ptr<content::RenderProcessHost> dom_host( |
| 548 webui1_instance->GetProcess()); | 561 webui1_instance->GetProcess()); |
| 549 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); | 562 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); |
| 550 | 563 |
| 551 // Make sure none of differing privilege processes are mixed. | 564 // Make sure none of differing privilege processes are mixed. |
| 552 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); | 565 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); |
| 553 | 566 |
| 554 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) { | 567 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) { |
| 555 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); | 568 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); |
| 556 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); | 569 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); |
| 557 } | 570 } |
| 558 | 571 |
| 559 STLDeleteContainerPointers(hosts.begin(), hosts.end()); | 572 STLDeleteContainerPointers(hosts.begin(), hosts.end()); |
| 560 } | 573 } |
| 561 | 574 |
| 562 // Test to ensure that HasWrongProcessForURL behaves properly for different | 575 // Test to ensure that HasWrongProcessForURL behaves properly for different |
| 563 // types of URLs. | 576 // types of URLs. |
| 564 TEST_F(SiteInstanceTest, HasWrongProcessForURL) { | 577 TEST_F(SiteInstanceTest, HasWrongProcessForURL) { |
| 565 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 578 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 566 scoped_ptr<content::RenderProcessHost> host; | 579 scoped_ptr<content::RenderProcessHost> host; |
| 567 scoped_refptr<SiteInstance> instance( | 580 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>( |
| 568 SiteInstance::CreateSiteInstance(browser_context.get())); | 581 SiteInstance::Create(browser_context.get()))); |
| 569 | 582 |
| 570 EXPECT_FALSE(instance->has_site()); | 583 EXPECT_FALSE(instance->HasSite()); |
| 571 EXPECT_TRUE(instance->site().is_empty()); | 584 EXPECT_TRUE(instance->GetSite().is_empty()); |
| 572 | 585 |
| 573 instance->SetSite(GURL("http://evernote.com/")); | 586 instance->SetSite(GURL("http://evernote.com/")); |
| 574 EXPECT_TRUE(instance->has_site()); | 587 EXPECT_TRUE(instance->HasSite()); |
| 575 | 588 |
| 576 // Check prior to "assigning" a process to the instance, which is expected | 589 // Check prior to "assigning" a process to the instance, which is expected |
| 577 // to return false due to not being attached to any process yet. | 590 // to return false due to not being attached to any process yet. |
| 578 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); | 591 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); |
| 579 | 592 |
| 580 // The call to GetProcess actually creates a new real process, which works | 593 // The call to GetProcess actually creates a new real process, which works |
| 581 // fine, but might be a cause for problems in different contexts. | 594 // fine, but might be a cause for problems in different contexts. |
| 582 host.reset(instance->GetProcess()); | 595 host.reset(instance->GetProcess()); |
| 583 EXPECT_TRUE(host.get() != NULL); | 596 EXPECT_TRUE(host.get() != NULL); |
| 584 EXPECT_TRUE(instance->HasProcess()); | 597 EXPECT_TRUE(instance->HasProcess()); |
| 585 | 598 |
| 586 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); | 599 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); |
| 587 EXPECT_FALSE(instance->HasWrongProcessForURL( | 600 EXPECT_FALSE(instance->HasWrongProcessForURL( |
| 588 GURL("javascript:alert(document.location.href);"))); | 601 GURL("javascript:alert(document.location.href);"))); |
| 589 | 602 |
| 590 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings"))); | 603 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings"))); |
| 591 } | 604 } |
| OLD | NEW |