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/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
6 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
7 #include "base/string16.h" | 8 #include "base/string16.h" |
8 #include "content/browser/browser_thread_impl.h" | 9 #include "content/browser/browser_thread_impl.h" |
9 #include "content/browser/browsing_instance.h" | 10 #include "content/browser/browsing_instance.h" |
10 #include "content/browser/child_process_security_policy_impl.h" | 11 #include "content/browser/child_process_security_policy_impl.h" |
11 #include "content/browser/renderer_host/render_process_host_impl.h" | 12 #include "content/browser/renderer_host/render_process_host_impl.h" |
12 #include "content/browser/renderer_host/render_view_host_impl.h" | 13 #include "content/browser/renderer_host/render_view_host_impl.h" |
13 #include "content/browser/renderer_host/test_render_view_host.h" | 14 #include "content/browser/renderer_host/test_render_view_host.h" |
14 #include "content/browser/site_instance_impl.h" | 15 #include "content/browser/site_instance_impl.h" |
15 #include "content/browser/web_contents/navigation_entry_impl.h" | 16 #include "content/browser/web_contents/navigation_entry_impl.h" |
16 #include "content/browser/web_contents/web_contents_impl.h" | 17 #include "content/browser/web_contents/web_contents_impl.h" |
17 #include "content/public/browser/web_ui_controller_factory.h" | 18 #include "content/public/browser/web_ui_controller_factory.h" |
18 #include "content/public/common/content_client.h" | 19 #include "content/public/common/content_client.h" |
19 #include "content/public/common/content_constants.h" | 20 #include "content/public/common/content_constants.h" |
| 21 #include "content/public/common/content_switches.h" |
20 #include "content/public/common/url_constants.h" | 22 #include "content/public/common/url_constants.h" |
21 #include "content/public/test/mock_render_process_host.h" | 23 #include "content/public/test/mock_render_process_host.h" |
22 #include "content/public/test/test_browser_context.h" | 24 #include "content/public/test/test_browser_context.h" |
23 #include "content/public/test/test_browser_thread.h" | 25 #include "content/public/test/test_browser_thread.h" |
24 #include "content/test/test_content_browser_client.h" | 26 #include "content/test/test_content_browser_client.h" |
25 #include "content/test/test_content_client.h" | 27 #include "content/test/test_content_client.h" |
26 #include "googleurl/src/url_util.h" | 28 #include "googleurl/src/url_util.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
28 | 30 |
29 using content::BrowserContext; | 31 using content::BrowserContext; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 public: | 87 public: |
86 SiteInstanceTestBrowserClient() | 88 SiteInstanceTestBrowserClient() |
87 : privileged_process_id_(-1) { | 89 : privileged_process_id_(-1) { |
88 } | 90 } |
89 | 91 |
90 virtual content::WebUIControllerFactory* | 92 virtual content::WebUIControllerFactory* |
91 GetWebUIControllerFactory() OVERRIDE { | 93 GetWebUIControllerFactory() OVERRIDE { |
92 return &factory_; | 94 return &factory_; |
93 } | 95 } |
94 | 96 |
95 virtual bool ShouldUseProcessPerSite(BrowserContext* browser_context, | |
96 const GURL& effective_url) OVERRIDE { | |
97 return false; | |
98 } | |
99 | |
100 virtual bool IsSuitableHost(content::RenderProcessHost* process_host, | 97 virtual bool IsSuitableHost(content::RenderProcessHost* process_host, |
101 const GURL& site_url) OVERRIDE { | 98 const GURL& site_url) OVERRIDE { |
102 return (privileged_process_id_ == process_host->GetID()) == | 99 return (privileged_process_id_ == process_host->GetID()) == |
103 site_url.SchemeIs(kPrivilegedScheme); | 100 site_url.SchemeIs(kPrivilegedScheme); |
104 } | 101 } |
105 | 102 |
106 void set_privileged_process_id(int process_id) { | 103 void set_privileged_process_id(int process_id) { |
107 privileged_process_id_ = process_id; | 104 privileged_process_id_ = process_id; |
108 } | 105 } |
109 | 106 |
(...skipping 16 matching lines...) Expand all Loading... |
126 virtual void SetUp() { | 123 virtual void SetUp() { |
127 old_client_ = content::GetContentClient(); | 124 old_client_ = content::GetContentClient(); |
128 old_browser_client_ = content::GetContentClient()->browser(); | 125 old_browser_client_ = content::GetContentClient()->browser(); |
129 content::SetContentClient(&client_); | 126 content::SetContentClient(&client_); |
130 content::GetContentClient()->set_browser_for_testing(&browser_client_); | 127 content::GetContentClient()->set_browser_for_testing(&browser_client_); |
131 url_util::AddStandardScheme(kPrivilegedScheme); | 128 url_util::AddStandardScheme(kPrivilegedScheme); |
132 url_util::AddStandardScheme(chrome::kChromeUIScheme); | 129 url_util::AddStandardScheme(chrome::kChromeUIScheme); |
133 } | 130 } |
134 | 131 |
135 virtual void TearDown() { | 132 virtual void TearDown() { |
| 133 // Ensure that no RenderProcessHosts are left over after the tests. |
| 134 EXPECT_TRUE(content::RenderProcessHost::AllHostsIterator().IsAtEnd()); |
| 135 |
136 content::GetContentClient()->set_browser_for_testing(old_browser_client_); | 136 content::GetContentClient()->set_browser_for_testing(old_browser_client_); |
137 content::SetContentClient(old_client_); | 137 content::SetContentClient(old_client_); |
138 MessageLoop::current()->RunAllPending(); | 138 MessageLoop::current()->RunAllPending(); |
139 message_loop_.RunAllPending(); | 139 message_loop_.RunAllPending(); |
140 } | 140 } |
141 | 141 |
142 void set_privileged_process_id(int process_id) { | 142 void set_privileged_process_id(int process_id) { |
143 browser_client_.set_privileged_process_id(process_id); | 143 browser_client_.set_privileged_process_id(process_id); |
144 } | 144 } |
145 | 145 |
146 private: | 146 private: |
147 MessageLoopForUI message_loop_; | 147 MessageLoopForUI message_loop_; |
148 content::TestBrowserThread ui_thread_; | 148 content::TestBrowserThread ui_thread_; |
149 content::TestBrowserThread file_user_blocking_thread_; | 149 content::TestBrowserThread file_user_blocking_thread_; |
150 content::TestBrowserThread io_thread_; | 150 content::TestBrowserThread io_thread_; |
151 | 151 |
152 SiteInstanceTestClient client_; | 152 SiteInstanceTestClient client_; |
153 SiteInstanceTestBrowserClient browser_client_; | 153 SiteInstanceTestBrowserClient browser_client_; |
154 content::ContentClient* old_client_; | 154 content::ContentClient* old_client_; |
155 content::ContentBrowserClient* old_browser_client_; | 155 content::ContentBrowserClient* old_browser_client_; |
156 }; | 156 }; |
157 | 157 |
| 158 // Subclass of BrowsingInstance that updates a counter when deleted and |
| 159 // returns TestSiteInstances from GetSiteInstanceForURL. |
158 class TestBrowsingInstance : public BrowsingInstance { | 160 class TestBrowsingInstance : public BrowsingInstance { |
159 public: | 161 public: |
160 TestBrowsingInstance(BrowserContext* browser_context, int* delete_counter) | 162 TestBrowsingInstance(BrowserContext* browser_context, int* delete_counter) |
161 : BrowsingInstance(browser_context), | 163 : BrowsingInstance(browser_context), |
162 use_process_per_site_(false), | |
163 delete_counter_(delete_counter) { | 164 delete_counter_(delete_counter) { |
164 } | 165 } |
165 | 166 |
166 // Overrides BrowsingInstance::ShouldUseProcessPerSite so that we can test | |
167 // both alternatives without using command-line switches. | |
168 bool ShouldUseProcessPerSite(const GURL& url) { | |
169 return use_process_per_site_; | |
170 } | |
171 | |
172 void set_use_process_per_site(bool use_process_per_site) { | |
173 use_process_per_site_ = use_process_per_site; | |
174 } | |
175 | |
176 // Make a few methods public for tests. | 167 // Make a few methods public for tests. |
177 using BrowsingInstance::ShouldUseProcessPerSite; | |
178 using BrowsingInstance::browser_context; | 168 using BrowsingInstance::browser_context; |
179 using BrowsingInstance::HasSiteInstance; | 169 using BrowsingInstance::HasSiteInstance; |
180 using BrowsingInstance::GetSiteInstanceForURL; | 170 using BrowsingInstance::GetSiteInstanceForURL; |
181 using BrowsingInstance::RegisterSiteInstance; | 171 using BrowsingInstance::RegisterSiteInstance; |
182 using BrowsingInstance::UnregisterSiteInstance; | 172 using BrowsingInstance::UnregisterSiteInstance; |
183 | 173 |
184 private: | 174 private: |
185 virtual ~TestBrowsingInstance() { | 175 virtual ~TestBrowsingInstance() { |
186 (*delete_counter_)++; | 176 (*delete_counter_)++; |
187 } | 177 } |
188 | 178 |
189 // Set by individual tests. | |
190 bool use_process_per_site_; | |
191 | |
192 int* delete_counter_; | 179 int* delete_counter_; |
193 }; | 180 }; |
194 | 181 |
| 182 // Subclass of SiteInstanceImpl that updates a counter when deleted. |
195 class TestSiteInstance : public SiteInstanceImpl { | 183 class TestSiteInstance : public SiteInstanceImpl { |
196 public: | 184 public: |
197 static TestSiteInstance* CreateTestSiteInstance( | 185 static TestSiteInstance* CreateTestSiteInstance( |
198 BrowserContext* browser_context, | 186 BrowserContext* browser_context, |
199 int* site_delete_counter, | 187 int* site_delete_counter, |
200 int* browsing_delete_counter) { | 188 int* browsing_delete_counter) { |
201 TestBrowsingInstance* browsing_instance = | 189 TestBrowsingInstance* browsing_instance = |
202 new TestBrowsingInstance(browser_context, browsing_delete_counter); | 190 new TestBrowsingInstance(browser_context, browsing_delete_counter); |
203 return new TestSiteInstance(browsing_instance, site_delete_counter); | 191 return new TestSiteInstance(browsing_instance, site_delete_counter); |
204 } | 192 } |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 | 391 |
404 // JavaScript links should be considered same site for anything. | 392 // JavaScript links should be considered same site for anything. |
405 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo)); | 393 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo)); |
406 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_https)); | 394 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_https)); |
407 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_port)); | 395 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_port)); |
408 } | 396 } |
409 | 397 |
410 // Test to ensure that there is only one SiteInstance per site in a given | 398 // Test to ensure that there is only one SiteInstance per site in a given |
411 // BrowsingInstance, when process-per-site is not in use. | 399 // BrowsingInstance, when process-per-site is not in use. |
412 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { | 400 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { |
| 401 ASSERT_FALSE(CommandLine::ForCurrentProcess()->HasSwitch( |
| 402 switches::kProcessPerSite)); |
413 int delete_counter = 0; | 403 int delete_counter = 0; |
| 404 scoped_ptr<content::TestBrowserContext> browser_context( |
| 405 new content::TestBrowserContext()); |
414 TestBrowsingInstance* browsing_instance = | 406 TestBrowsingInstance* browsing_instance = |
415 new TestBrowsingInstance(NULL, &delete_counter); | 407 new TestBrowsingInstance(browser_context.get(), &delete_counter); |
416 browsing_instance->set_use_process_per_site(false); | |
417 | 408 |
418 const GURL url_a1("http://www.google.com/1.html"); | 409 const GURL url_a1("http://www.google.com/1.html"); |
419 scoped_refptr<SiteInstanceImpl> site_instance_a1( | 410 scoped_refptr<SiteInstanceImpl> site_instance_a1( |
420 static_cast<SiteInstanceImpl*>( | 411 static_cast<SiteInstanceImpl*>( |
421 browsing_instance->GetSiteInstanceForURL(url_a1))); | 412 browsing_instance->GetSiteInstanceForURL(url_a1))); |
422 EXPECT_TRUE(site_instance_a1.get() != NULL); | 413 EXPECT_TRUE(site_instance_a1.get() != NULL); |
423 | 414 |
424 // A separate site should create a separate SiteInstance. | 415 // A separate site should create a separate SiteInstance. |
425 const GURL url_b1("http://www.yahoo.com/"); | 416 const GURL url_b1("http://www.yahoo.com/"); |
426 scoped_refptr<SiteInstanceImpl> site_instance_b1( | 417 scoped_refptr<SiteInstanceImpl> site_instance_b1( |
(...skipping 10 matching lines...) Expand all Loading... |
437 // A second visit to the original site should return the same SiteInstance. | 428 // A second visit to the original site should return the same SiteInstance. |
438 const GURL url_a2("http://www.google.com/2.html"); | 429 const GURL url_a2("http://www.google.com/2.html"); |
439 EXPECT_EQ(site_instance_a1.get(), | 430 EXPECT_EQ(site_instance_a1.get(), |
440 browsing_instance->GetSiteInstanceForURL(url_a2)); | 431 browsing_instance->GetSiteInstanceForURL(url_a2)); |
441 EXPECT_EQ(site_instance_a1.get(), | 432 EXPECT_EQ(site_instance_a1.get(), |
442 site_instance_a1->GetRelatedSiteInstance(url_a2)); | 433 site_instance_a1->GetRelatedSiteInstance(url_a2)); |
443 | 434 |
444 // A visit to the original site in a new BrowsingInstance (same or different | 435 // A visit to the original site in a new BrowsingInstance (same or different |
445 // browser context) should return a different SiteInstance. | 436 // browser context) should return a different SiteInstance. |
446 TestBrowsingInstance* browsing_instance2 = | 437 TestBrowsingInstance* browsing_instance2 = |
447 new TestBrowsingInstance(NULL, &delete_counter); | 438 new TestBrowsingInstance(browser_context.get(), &delete_counter); |
448 browsing_instance2->set_use_process_per_site(false); | |
449 // Ensure the new SiteInstance is ref counted so that it gets deleted. | 439 // Ensure the new SiteInstance is ref counted so that it gets deleted. |
450 scoped_refptr<SiteInstanceImpl> site_instance_a2_2( | 440 scoped_refptr<SiteInstanceImpl> site_instance_a2_2( |
451 static_cast<SiteInstanceImpl*>( | 441 static_cast<SiteInstanceImpl*>( |
452 browsing_instance2->GetSiteInstanceForURL(url_a2))); | 442 browsing_instance2->GetSiteInstanceForURL(url_a2))); |
453 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); | 443 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); |
454 EXPECT_FALSE(site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2)); | 444 EXPECT_FALSE(site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2)); |
455 | 445 |
| 446 // The two SiteInstances for http://google.com should not use the same process |
| 447 // if process-per-site is not enabled. |
| 448 scoped_ptr<content::RenderProcessHost> process_a1( |
| 449 site_instance_a1->GetProcess()); |
| 450 scoped_ptr<content::RenderProcessHost> process_a2_2( |
| 451 site_instance_a2_2->GetProcess()); |
| 452 EXPECT_NE(process_a1.get(), process_a2_2.get()); |
| 453 |
456 // Should be able to see that we do have SiteInstances. | 454 // Should be able to see that we do have SiteInstances. |
457 EXPECT_TRUE(browsing_instance->HasSiteInstance( | 455 EXPECT_TRUE(browsing_instance->HasSiteInstance( |
458 GURL("http://mail.google.com"))); | 456 GURL("http://mail.google.com"))); |
459 EXPECT_TRUE(browsing_instance2->HasSiteInstance( | 457 EXPECT_TRUE(browsing_instance2->HasSiteInstance( |
460 GURL("http://mail.google.com"))); | 458 GURL("http://mail.google.com"))); |
461 EXPECT_TRUE(browsing_instance->HasSiteInstance( | 459 EXPECT_TRUE(browsing_instance->HasSiteInstance( |
462 GURL("http://mail.yahoo.com"))); | 460 GURL("http://mail.yahoo.com"))); |
463 | 461 |
464 // Should be able to see that we don't have SiteInstances. | 462 // Should be able to see that we don't have SiteInstances. |
465 EXPECT_FALSE(browsing_instance->HasSiteInstance( | 463 EXPECT_FALSE(browsing_instance->HasSiteInstance( |
466 GURL("https://www.google.com"))); | 464 GURL("https://www.google.com"))); |
467 EXPECT_FALSE(browsing_instance2->HasSiteInstance( | 465 EXPECT_FALSE(browsing_instance2->HasSiteInstance( |
468 GURL("http://www.yahoo.com"))); | 466 GURL("http://www.yahoo.com"))); |
469 | 467 |
470 // browsing_instances will be deleted when their SiteInstances are deleted | 468 // browsing_instances will be deleted when their SiteInstances are deleted. |
| 469 // The processes will be unregistered when the RPH scoped_ptrs go away. |
471 } | 470 } |
472 | 471 |
473 // Test to ensure that there is only one SiteInstance per site for an entire | 472 // Test to ensure that there is only one RenderProcessHost per site for an |
474 // BrowserContext, if process-per-site is in use. | 473 // entire BrowserContext, if process-per-site is in use. |
475 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { | 474 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { |
| 475 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 476 switches::kProcessPerSite); |
476 int delete_counter = 0; | 477 int delete_counter = 0; |
| 478 scoped_ptr<content::TestBrowserContext> browser_context( |
| 479 new content::TestBrowserContext()); |
477 TestBrowsingInstance* browsing_instance = | 480 TestBrowsingInstance* browsing_instance = |
478 new TestBrowsingInstance(NULL, &delete_counter); | 481 new TestBrowsingInstance(browser_context.get(), &delete_counter); |
479 browsing_instance->set_use_process_per_site(true); | |
480 | 482 |
481 const GURL url_a1("http://www.google.com/1.html"); | 483 const GURL url_a1("http://www.google.com/1.html"); |
482 scoped_refptr<SiteInstanceImpl> site_instance_a1( | 484 scoped_refptr<SiteInstanceImpl> site_instance_a1( |
483 static_cast<SiteInstanceImpl*>( | 485 static_cast<SiteInstanceImpl*>( |
484 browsing_instance->GetSiteInstanceForURL(url_a1))); | 486 browsing_instance->GetSiteInstanceForURL(url_a1))); |
485 EXPECT_TRUE(site_instance_a1.get() != NULL); | 487 EXPECT_TRUE(site_instance_a1.get() != NULL); |
| 488 scoped_ptr<content::RenderProcessHost> process_a1( |
| 489 site_instance_a1->GetProcess()); |
486 | 490 |
487 // A separate site should create a separate SiteInstance. | 491 // A separate site should create a separate SiteInstance. |
488 const GURL url_b1("http://www.yahoo.com/"); | 492 const GURL url_b1("http://www.yahoo.com/"); |
489 scoped_refptr<SiteInstanceImpl> site_instance_b1( | 493 scoped_refptr<SiteInstanceImpl> site_instance_b1( |
490 static_cast<SiteInstanceImpl*>( | 494 static_cast<SiteInstanceImpl*>( |
491 browsing_instance->GetSiteInstanceForURL(url_b1))); | 495 browsing_instance->GetSiteInstanceForURL(url_b1))); |
492 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); | 496 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); |
493 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1)); | 497 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1)); |
494 | 498 |
495 // Getting the new SiteInstance from the BrowsingInstance and from another | 499 // Getting the new SiteInstance from the BrowsingInstance and from another |
496 // SiteInstance in the BrowsingInstance should give the same result. | 500 // SiteInstance in the BrowsingInstance should give the same result. |
497 EXPECT_EQ(site_instance_b1.get(), | 501 EXPECT_EQ(site_instance_b1.get(), |
498 site_instance_a1->GetRelatedSiteInstance(url_b1)); | 502 site_instance_a1->GetRelatedSiteInstance(url_b1)); |
499 | 503 |
500 // A second visit to the original site should return the same SiteInstance. | 504 // A second visit to the original site should return the same SiteInstance. |
501 const GURL url_a2("http://www.google.com/2.html"); | 505 const GURL url_a2("http://www.google.com/2.html"); |
502 EXPECT_EQ(site_instance_a1.get(), | 506 EXPECT_EQ(site_instance_a1.get(), |
503 browsing_instance->GetSiteInstanceForURL(url_a2)); | 507 browsing_instance->GetSiteInstanceForURL(url_a2)); |
504 EXPECT_EQ(site_instance_a1.get(), | 508 EXPECT_EQ(site_instance_a1.get(), |
505 site_instance_a1->GetRelatedSiteInstance(url_a2)); | 509 site_instance_a1->GetRelatedSiteInstance(url_a2)); |
506 | 510 |
507 // A visit to the original site in a new BrowsingInstance (same browser | 511 // A visit to the original site in a new BrowsingInstance (same browser |
508 // context) should also return the same SiteInstance. | 512 // context) should return a different SiteInstance with the same process. |
509 // This BrowsingInstance doesn't get its own SiteInstance within the test, so | 513 TestBrowsingInstance* browsing_instance2 = |
510 // it won't be deleted by its children. Thus, we'll keep a ref count to it | 514 new TestBrowsingInstance(browser_context.get(), &delete_counter); |
511 // to make sure it gets deleted. | 515 scoped_refptr<SiteInstanceImpl> site_instance_a1_2( |
512 scoped_refptr<TestBrowsingInstance> browsing_instance2( | 516 static_cast<SiteInstanceImpl*>( |
513 new TestBrowsingInstance(NULL, &delete_counter)); | 517 browsing_instance2->GetSiteInstanceForURL(url_a1))); |
514 browsing_instance2->set_use_process_per_site(true); | 518 EXPECT_TRUE(site_instance_a1.get() != NULL); |
515 EXPECT_EQ(site_instance_a1.get(), | 519 EXPECT_NE(site_instance_a1.get(), site_instance_a1_2.get()); |
516 browsing_instance2->GetSiteInstanceForURL(url_a2)); | 520 EXPECT_EQ(process_a1.get(), site_instance_a1_2->GetProcess()); |
517 | 521 |
518 // A visit to the original site in a new BrowsingInstance (different browser | 522 // A visit to the original site in a new BrowsingInstance (different browser |
519 // context) should return a different SiteInstance. | 523 // context) should return a different SiteInstance with a different process. |
520 scoped_ptr<content::TestBrowserContext> browser_context( | 524 scoped_ptr<content::TestBrowserContext> browser_context2( |
521 new content::TestBrowserContext()); | 525 new content::TestBrowserContext()); |
522 TestBrowsingInstance* browsing_instance3 = | 526 TestBrowsingInstance* browsing_instance3 = |
523 new TestBrowsingInstance(browser_context.get(), &delete_counter); | 527 new TestBrowsingInstance(browser_context2.get(), &delete_counter); |
524 browsing_instance3->set_use_process_per_site(true); | |
525 // Ensure the new SiteInstance is ref counted so that it gets deleted. | |
526 scoped_refptr<SiteInstanceImpl> site_instance_a2_3( | 528 scoped_refptr<SiteInstanceImpl> site_instance_a2_3( |
527 static_cast<SiteInstanceImpl*>( | 529 static_cast<SiteInstanceImpl*>( |
528 browsing_instance3->GetSiteInstanceForURL(url_a2))); | 530 browsing_instance3->GetSiteInstanceForURL(url_a2))); |
| 531 EXPECT_TRUE(site_instance_a2_3.get() != NULL); |
| 532 scoped_ptr<content::RenderProcessHost> process_a2_3( |
| 533 site_instance_a2_3->GetProcess()); |
529 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); | 534 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); |
| 535 EXPECT_NE(process_a1.get(), process_a2_3.get()); |
530 | 536 |
531 // Should be able to see that we do have SiteInstances. | 537 // Should be able to see that we do have SiteInstances. |
532 EXPECT_TRUE(browsing_instance->HasSiteInstance( | 538 EXPECT_TRUE(browsing_instance->HasSiteInstance( |
533 GURL("http://mail.google.com"))); // visited before | 539 GURL("http://mail.google.com"))); // visited before |
534 EXPECT_TRUE(browsing_instance2->HasSiteInstance( | 540 EXPECT_TRUE(browsing_instance2->HasSiteInstance( |
535 GURL("http://mail.google.com"))); // visited before | 541 GURL("http://mail.google.com"))); // visited before |
536 EXPECT_TRUE(browsing_instance->HasSiteInstance( | 542 EXPECT_TRUE(browsing_instance->HasSiteInstance( |
537 GURL("http://mail.yahoo.com"))); // visited before | 543 GURL("http://mail.yahoo.com"))); // visited before |
538 EXPECT_TRUE(browsing_instance2->HasSiteInstance( | |
539 GURL("http://www.yahoo.com"))); // different BI, but same browser context | |
540 | 544 |
541 // Should be able to see that we don't have SiteInstances. | 545 // Should be able to see that we don't have SiteInstances. |
| 546 EXPECT_FALSE(browsing_instance2->HasSiteInstance( |
| 547 GURL("http://www.yahoo.com"))); // different BI, same browser context |
542 EXPECT_FALSE(browsing_instance->HasSiteInstance( | 548 EXPECT_FALSE(browsing_instance->HasSiteInstance( |
543 GURL("https://www.google.com"))); // not visited before | 549 GURL("https://www.google.com"))); // not visited before |
544 EXPECT_FALSE(browsing_instance3->HasSiteInstance( | 550 EXPECT_FALSE(browsing_instance3->HasSiteInstance( |
545 GURL("http://www.yahoo.com"))); // different BI, different context | 551 GURL("http://www.yahoo.com"))); // different BI, different context |
546 | 552 |
547 // browsing_instances will be deleted when their SiteInstances are deleted | 553 // browsing_instances will be deleted when their SiteInstances are deleted. |
| 554 // The processes will be unregistered when the RPH scoped_ptrs go away. |
548 } | 555 } |
549 | 556 |
550 static SiteInstanceImpl* CreateSiteInstance( | 557 static SiteInstanceImpl* CreateSiteInstance( |
551 content::RenderProcessHostFactory* factory, const GURL& url) { | 558 content::BrowserContext* browser_context, |
| 559 content::RenderProcessHostFactory* factory, |
| 560 const GURL& url) { |
552 SiteInstanceImpl* instance = | 561 SiteInstanceImpl* instance = |
553 reinterpret_cast<SiteInstanceImpl*>( | 562 reinterpret_cast<SiteInstanceImpl*>( |
554 SiteInstance::CreateForURL(NULL, url)); | 563 SiteInstance::CreateForURL(browser_context, url)); |
555 instance->set_render_process_host_factory(factory); | 564 instance->set_render_process_host_factory(factory); |
556 return instance; | 565 return instance; |
557 } | 566 } |
558 | 567 |
559 // Test to ensure that pages that require certain privileges are grouped | 568 // Test to ensure that pages that require certain privileges are grouped |
560 // in processes with similar pages. | 569 // in processes with similar pages. |
561 TEST_F(SiteInstanceTest, ProcessSharingByType) { | 570 TEST_F(SiteInstanceTest, ProcessSharingByType) { |
562 MockRenderProcessHostFactory rph_factory; | 571 MockRenderProcessHostFactory rph_factory; |
563 ChildProcessSecurityPolicyImpl* policy = | 572 ChildProcessSecurityPolicyImpl* policy = |
564 ChildProcessSecurityPolicyImpl::GetInstance(); | 573 ChildProcessSecurityPolicyImpl::GetInstance(); |
565 | 574 |
566 // Make a bunch of mock renderers so that we hit the limit. | 575 // Make a bunch of mock renderers so that we hit the limit. |
| 576 scoped_ptr<content::TestBrowserContext> browser_context( |
| 577 new content::TestBrowserContext()); |
567 std::vector<MockRenderProcessHost*> hosts; | 578 std::vector<MockRenderProcessHost*> hosts; |
568 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) | 579 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) |
569 hosts.push_back(new MockRenderProcessHost(NULL)); | 580 hosts.push_back(new MockRenderProcessHost(browser_context.get())); |
570 | 581 |
571 // Create some extension instances and make sure they share a process. | 582 // Create some extension instances and make sure they share a process. |
572 scoped_refptr<SiteInstanceImpl> extension1_instance( | 583 scoped_refptr<SiteInstanceImpl> extension1_instance( |
573 CreateSiteInstance(&rph_factory, | 584 CreateSiteInstance(browser_context.get(), &rph_factory, |
574 GURL(kPrivilegedScheme + std::string("://foo/bar")))); | 585 GURL(kPrivilegedScheme + std::string("://foo/bar")))); |
575 set_privileged_process_id(extension1_instance->GetProcess()->GetID()); | 586 set_privileged_process_id(extension1_instance->GetProcess()->GetID()); |
576 | 587 |
577 scoped_refptr<SiteInstanceImpl> extension2_instance( | 588 scoped_refptr<SiteInstanceImpl> extension2_instance( |
578 CreateSiteInstance(&rph_factory, | 589 CreateSiteInstance(browser_context.get(), &rph_factory, |
579 GURL(kPrivilegedScheme + std::string("://baz/bar")))); | 590 GURL(kPrivilegedScheme + std::string("://baz/bar")))); |
580 | 591 |
581 scoped_ptr<content::RenderProcessHost> extension_host( | 592 scoped_ptr<content::RenderProcessHost> extension_host( |
582 extension1_instance->GetProcess()); | 593 extension1_instance->GetProcess()); |
583 EXPECT_EQ(extension1_instance->GetProcess(), | 594 EXPECT_EQ(extension1_instance->GetProcess(), |
584 extension2_instance->GetProcess()); | 595 extension2_instance->GetProcess()); |
585 | 596 |
586 // Create some WebUI instances and make sure they share a process. | 597 // Create some WebUI instances and make sure they share a process. |
587 scoped_refptr<SiteInstanceImpl> webui1_instance(CreateSiteInstance( | 598 scoped_refptr<SiteInstanceImpl> webui1_instance(CreateSiteInstance( |
588 &rph_factory, | 599 browser_context.get(), &rph_factory, |
589 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); | 600 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); |
590 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID()); | 601 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID()); |
591 | 602 |
592 scoped_refptr<SiteInstanceImpl> webui2_instance(CreateSiteInstance( | 603 scoped_refptr<SiteInstanceImpl> webui2_instance(CreateSiteInstance( |
593 &rph_factory, | 604 browser_context.get(), &rph_factory, |
594 GURL(chrome::kChromeUIScheme + std::string("://history")))); | 605 GURL(chrome::kChromeUIScheme + std::string("://history")))); |
595 | 606 |
596 scoped_ptr<content::RenderProcessHost> dom_host( | 607 scoped_ptr<content::RenderProcessHost> dom_host( |
597 webui1_instance->GetProcess()); | 608 webui1_instance->GetProcess()); |
598 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); | 609 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); |
599 | 610 |
600 // Make sure none of differing privilege processes are mixed. | 611 // Make sure none of differing privilege processes are mixed. |
601 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); | 612 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); |
602 | 613 |
603 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) { | 614 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) { |
(...skipping 28 matching lines...) Expand all Loading... |
632 host.reset(instance->GetProcess()); | 643 host.reset(instance->GetProcess()); |
633 EXPECT_TRUE(host.get() != NULL); | 644 EXPECT_TRUE(host.get() != NULL); |
634 EXPECT_TRUE(instance->HasProcess()); | 645 EXPECT_TRUE(instance->HasProcess()); |
635 | 646 |
636 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); | 647 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); |
637 EXPECT_FALSE(instance->HasWrongProcessForURL( | 648 EXPECT_FALSE(instance->HasWrongProcessForURL( |
638 GURL("javascript:alert(document.location.href);"))); | 649 GURL("javascript:alert(document.location.href);"))); |
639 | 650 |
640 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings"))); | 651 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings"))); |
641 } | 652 } |
OLD | NEW |