Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: content/browser/site_instance_unittest.cc

Issue 8033001: Delegate decision what site instances can be rendered in what process to chrome (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: updates Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/test/base/testing_profile.h"
9 #include "content/browser/browser_thread.h" 8 #include "content/browser/browser_thread.h"
10 #include "content/browser/browsing_instance.h" 9 #include "content/browser/browsing_instance.h"
11 #include "content/browser/child_process_security_policy.h" 10 #include "content/browser/child_process_security_policy.h"
12 #include "content/browser/mock_content_browser_client.h" 11 #include "content/browser/mock_content_browser_client.h"
13 #include "content/browser/renderer_host/browser_render_process_host.h" 12 #include "content/browser/renderer_host/browser_render_process_host.h"
14 #include "content/browser/renderer_host/render_view_host.h" 13 #include "content/browser/renderer_host/render_view_host.h"
15 #include "content/browser/renderer_host/test_render_view_host.h" 14 #include "content/browser/renderer_host/test_render_view_host.h"
16 #include "content/browser/site_instance.h" 15 #include "content/browser/site_instance.h"
17 #include "content/browser/tab_contents/navigation_entry.h" 16 #include "content/browser/tab_contents/navigation_entry.h"
18 #include "content/browser/tab_contents/tab_contents.h" 17 #include "content/browser/tab_contents/tab_contents.h"
19 #include "content/browser/webui/empty_web_ui_factory.h" 18 #include "content/browser/webui/empty_web_ui_factory.h"
20 #include "content/common/content_client.h" 19 #include "content/common/content_client.h"
21 #include "content/common/content_constants.h" 20 #include "content/common/content_constants.h"
22 #include "content/common/url_constants.h" 21 #include "content/common/url_constants.h"
22 #include "content/test/test_browser_context.h"
23 #include "googleurl/src/url_util.h"
23 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
24 25
25 namespace { 26 namespace {
26 27
27 const char kSameAsAnyInstanceURL[] = "about:internets"; 28 const char kSameAsAnyInstanceURL[] = "about:internets";
28 29
30 const char kPrivilegedScheme[] = "privileged";
31
29 class SiteInstanceTestWebUIFactory : public content::EmptyWebUIFactory { 32 class SiteInstanceTestWebUIFactory : public content::EmptyWebUIFactory {
30 public: 33 public:
31 virtual bool UseWebUIForURL(content::BrowserContext* browser_context, 34 virtual bool UseWebUIForURL(content::BrowserContext* browser_context,
32 const GURL& url) const OVERRIDE { 35 const GURL& url) const OVERRIDE {
33 return HasWebUIScheme(url); 36 return HasWebUIScheme(url);
34 } 37 }
35 virtual bool HasWebUIScheme(const GURL& url) const OVERRIDE { 38 virtual bool HasWebUIScheme(const GURL& url) const OVERRIDE {
36 return url.SchemeIs(chrome::kChromeUIScheme); 39 return url.SchemeIs(chrome::kChromeUIScheme);
37 } 40 }
38 }; 41 };
39 42
40 class SiteInstanceTestBrowserClient : public content::MockContentBrowserClient { 43 class SiteInstanceTestBrowserClient : public content::MockContentBrowserClient {
41 public: 44 public:
42 SiteInstanceTestBrowserClient() : old_browser_client_(NULL) { 45 SiteInstanceTestBrowserClient()
46 : old_browser_client_(NULL),
47 privileged_process_id_(-1) {
43 } 48 }
44 49
45 virtual TabContentsView* CreateTabContentsView(TabContents* tab_contents) { 50 virtual TabContentsView* CreateTabContentsView(TabContents* tab_contents) {
46 return old_browser_client_->CreateTabContentsView(tab_contents); 51 return old_browser_client_->CreateTabContentsView(tab_contents);
47 } 52 }
48 53
49 virtual content::WebUIFactory* GetWebUIFactory() OVERRIDE { 54 virtual content::WebUIFactory* GetWebUIFactory() OVERRIDE {
50 return &factory_; 55 return &factory_;
51 } 56 }
52 57
53 virtual bool ShouldUseProcessPerSite(content::BrowserContext* browser_context, 58 virtual bool ShouldUseProcessPerSite(content::BrowserContext* browser_context,
54 const GURL& effective_url) OVERRIDE { 59 const GURL& effective_url) OVERRIDE {
55 return false; 60 return false;
56 } 61 }
57 62
58 virtual bool IsURLSameAsAnySiteInstance(const GURL& url) OVERRIDE { 63 virtual bool IsURLSameAsAnySiteInstance(const GURL& url) OVERRIDE {
59 return url == GURL(kSameAsAnyInstanceURL) || 64 return url == GURL(kSameAsAnyInstanceURL) ||
60 url == GURL(chrome::kAboutCrashURL); 65 url == GURL(chrome::kAboutCrashURL);
61 } 66 }
62 67
68 virtual bool ShouldRunInPrivilegedProcess(const GURL& url) OVERRIDE {
69 return url.SchemeIs(kPrivilegedScheme);
70 }
71
72 virtual bool IsSuitableHost(RenderProcessHost* process_host,
73 const GURL& site_url) OVERRIDE {
74 return (privileged_process_id_ == process_host->id()) ==
75 site_url.SchemeIs(kPrivilegedScheme);
76 }
77
63 virtual GURL GetEffectiveURL(content::BrowserContext* browser_context, 78 virtual GURL GetEffectiveURL(content::BrowserContext* browser_context,
64 const GURL& url) OVERRIDE { 79 const GURL& url) OVERRIDE {
65 return url; 80 return url;
66 } 81 }
67 82
68 void SetOriginalClient(content::ContentBrowserClient* old_browser_client) { 83 void SetOriginalClient(content::ContentBrowserClient* old_browser_client) {
69 old_browser_client_ = old_browser_client; 84 old_browser_client_ = old_browser_client;
70 } 85 }
71 86
87 void SetPrivilegedProcessId(int process_id) {
88 privileged_process_id_ = process_id;
89 }
90
72 private: 91 private:
73 SiteInstanceTestWebUIFactory factory_; 92 SiteInstanceTestWebUIFactory factory_;
74 content::ContentBrowserClient* old_browser_client_; 93 content::ContentBrowserClient* old_browser_client_;
94 int privileged_process_id_;
75 }; 95 };
76 96
77 class SiteInstanceTest : public testing::Test { 97 class SiteInstanceTest : public testing::Test {
78 public: 98 public:
79 SiteInstanceTest() 99 SiteInstanceTest()
80 : ui_thread_(BrowserThread::UI, &message_loop_), 100 : ui_thread_(BrowserThread::UI, &message_loop_),
81 old_browser_client_(NULL) { 101 old_browser_client_(NULL) {
82 } 102 }
83 103
84 virtual void SetUp() { 104 virtual void SetUp() {
85 old_browser_client_ = content::GetContentClient()->browser(); 105 old_browser_client_ = content::GetContentClient()->browser();
86 browser_client_.SetOriginalClient(old_browser_client_); 106 browser_client_.SetOriginalClient(old_browser_client_);
87 content::GetContentClient()->set_browser(&browser_client_); 107 content::GetContentClient()->set_browser(&browser_client_);
108 url_util::AddStandardScheme(kPrivilegedScheme);
109 url_util::AddStandardScheme(chrome::kChromeUIScheme);
88 } 110 }
89 111
90 virtual void TearDown() { 112 virtual void TearDown() {
91 content::GetContentClient()->set_browser(old_browser_client_); 113 content::GetContentClient()->set_browser(old_browser_client_);
92 } 114 }
93 115
116 void SetPrivilegedProcessId(int process_id) {
117 browser_client_.SetPrivilegedProcessId(process_id);
118 }
119
94 private: 120 private:
95 MessageLoopForUI message_loop_; 121 MessageLoopForUI message_loop_;
96 BrowserThread ui_thread_; 122 BrowserThread ui_thread_;
97 123
98 SiteInstanceTestBrowserClient browser_client_; 124 SiteInstanceTestBrowserClient browser_client_;
99 content::ContentBrowserClient* old_browser_client_; 125 content::ContentBrowserClient* old_browser_client_;
100 }; 126 };
101 127
102 class TestBrowsingInstance : public BrowsingInstance { 128 class TestBrowsingInstance : public BrowsingInstance {
103 public: 129 public:
104 TestBrowsingInstance(Profile* profile, int* deleteCounter) 130 TestBrowsingInstance(content::BrowserContext* browser_context,
105 : BrowsingInstance(profile), 131 int* deleteCounter)
132 : BrowsingInstance(browser_context),
106 use_process_per_site(false), 133 use_process_per_site(false),
107 deleteCounter_(deleteCounter) { 134 deleteCounter_(deleteCounter) {
108 } 135 }
109 136
110 // Overrides BrowsingInstance::ShouldUseProcessPerSite so that we can test 137 // Overrides BrowsingInstance::ShouldUseProcessPerSite so that we can test
111 // both alternatives without using command-line switches. 138 // both alternatives without using command-line switches.
112 bool ShouldUseProcessPerSite(const GURL& url) { 139 bool ShouldUseProcessPerSite(const GURL& url) {
113 return use_process_per_site; 140 return use_process_per_site;
114 } 141 }
115 142
116 // Set by individual tests. 143 // Set by individual tests.
117 bool use_process_per_site; 144 bool use_process_per_site;
118 145
119 private: 146 private:
120 ~TestBrowsingInstance() { 147 ~TestBrowsingInstance() {
121 (*deleteCounter_)++; 148 (*deleteCounter_)++;
122 } 149 }
123 150
124 int* deleteCounter_; 151 int* deleteCounter_;
125 }; 152 };
126 153
127 class TestSiteInstance : public SiteInstance { 154 class TestSiteInstance : public SiteInstance {
128 public: 155 public:
129 static TestSiteInstance* CreateTestSiteInstance(Profile* profile, 156 static TestSiteInstance* CreateTestSiteInstance(
130 int* siteDeleteCounter, 157 content::BrowserContext* browser_context,
131 int* browsingDeleteCounter) { 158 int* siteDeleteCounter,
159 int* browsingDeleteCounter) {
132 TestBrowsingInstance* browsing_instance = 160 TestBrowsingInstance* browsing_instance =
133 new TestBrowsingInstance(profile, browsingDeleteCounter); 161 new TestBrowsingInstance(browser_context, browsingDeleteCounter);
134 return new TestSiteInstance(browsing_instance, siteDeleteCounter); 162 return new TestSiteInstance(browsing_instance, siteDeleteCounter);
135 } 163 }
136 164
137 private: 165 private:
138 TestSiteInstance(BrowsingInstance* browsing_instance, int* deleteCounter) 166 TestSiteInstance(BrowsingInstance* browsing_instance, int* deleteCounter)
139 : SiteInstance(browsing_instance), deleteCounter_(deleteCounter) {} 167 : SiteInstance(browsing_instance), deleteCounter_(deleteCounter) {}
140 ~TestSiteInstance() { 168 ~TestSiteInstance() {
141 (*deleteCounter_)++; 169 (*deleteCounter_)++;
142 } 170 }
143 171
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 delete e1; 207 delete e1;
180 EXPECT_EQ(0, siteDeleteCounter); 208 EXPECT_EQ(0, siteDeleteCounter);
181 EXPECT_EQ(0, browsingDeleteCounter); 209 EXPECT_EQ(0, browsingDeleteCounter);
182 delete e2; 210 delete e2;
183 EXPECT_EQ(1, siteDeleteCounter); 211 EXPECT_EQ(1, siteDeleteCounter);
184 // instance is now deleted 212 // instance is now deleted
185 EXPECT_EQ(1, browsingDeleteCounter); 213 EXPECT_EQ(1, browsingDeleteCounter);
186 // browsing_instance is now deleted 214 // browsing_instance is now deleted
187 215
188 // Ensure that instances are deleted when their RenderViewHosts are gone. 216 // Ensure that instances are deleted when their RenderViewHosts are gone.
189 scoped_ptr<TestingProfile> profile(new TestingProfile()); 217 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
190 instance = 218 instance =
191 TestSiteInstance::CreateTestSiteInstance(profile.get(), 219 TestSiteInstance::CreateTestSiteInstance(browser_context.get(),
192 &siteDeleteCounter, 220 &siteDeleteCounter,
193 &browsingDeleteCounter); 221 &browsingDeleteCounter);
194 { 222 {
195 TabContents contents(profile.get(), instance, MSG_ROUTING_NONE, NULL, NULL); 223 TabContents contents(browser_context.get(),
224 instance,
225 MSG_ROUTING_NONE,
226 NULL,
227 NULL);
196 EXPECT_EQ(1, siteDeleteCounter); 228 EXPECT_EQ(1, siteDeleteCounter);
197 EXPECT_EQ(1, browsingDeleteCounter); 229 EXPECT_EQ(1, browsingDeleteCounter);
198 } 230 }
199 231
200 // Make sure that we flush any messages related to the above TabContents 232 // Make sure that we flush any messages related to the above TabContents
201 // destruction. 233 // destruction.
202 MessageLoop::current()->RunAllPending(); 234 MessageLoop::current()->RunAllPending();
203 235
204 EXPECT_EQ(2, siteDeleteCounter); 236 EXPECT_EQ(2, siteDeleteCounter);
205 EXPECT_EQ(2, browsingDeleteCounter); 237 EXPECT_EQ(2, browsingDeleteCounter);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 285
254 // Make sure max_page_id_ is monotonically increasing. 286 // Make sure max_page_id_ is monotonically increasing.
255 instance->UpdateMaxPageID(3); 287 instance->UpdateMaxPageID(3);
256 instance->UpdateMaxPageID(1); 288 instance->UpdateMaxPageID(1);
257 EXPECT_EQ(3, instance->max_page_id()); 289 EXPECT_EQ(3, instance->max_page_id());
258 } 290 }
259 291
260 // Test to ensure GetProcess returns and creates processes correctly. 292 // Test to ensure GetProcess returns and creates processes correctly.
261 TEST_F(SiteInstanceTest, GetProcess) { 293 TEST_F(SiteInstanceTest, GetProcess) {
262 // Ensure that GetProcess returns a process. 294 // Ensure that GetProcess returns a process.
263 scoped_ptr<TestingProfile> profile(new TestingProfile()); 295 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
264 scoped_ptr<RenderProcessHost> host1; 296 scoped_ptr<RenderProcessHost> host1;
265 scoped_refptr<SiteInstance> instance( 297 scoped_refptr<SiteInstance> instance(
266 SiteInstance::CreateSiteInstance(profile.get())); 298 SiteInstance::CreateSiteInstance(browser_context.get()));
267 host1.reset(instance->GetProcess()); 299 host1.reset(instance->GetProcess());
268 EXPECT_TRUE(host1.get() != NULL); 300 EXPECT_TRUE(host1.get() != NULL);
269 301
270 // Ensure that GetProcess creates a new process. 302 // Ensure that GetProcess creates a new process.
271 scoped_refptr<SiteInstance> instance2( 303 scoped_refptr<SiteInstance> instance2(
272 SiteInstance::CreateSiteInstance(profile.get())); 304 SiteInstance::CreateSiteInstance(browser_context.get()));
273 scoped_ptr<RenderProcessHost> host2(instance2->GetProcess()); 305 scoped_ptr<RenderProcessHost> host2(instance2->GetProcess());
274 EXPECT_TRUE(host2.get() != NULL); 306 EXPECT_TRUE(host2.get() != NULL);
275 EXPECT_NE(host1.get(), host2.get()); 307 EXPECT_NE(host1.get(), host2.get());
276 } 308 }
277 309
278 // Test to ensure SetSite and site() work properly. 310 // Test to ensure SetSite and site() work properly.
279 TEST_F(SiteInstanceTest, SetSite) { 311 TEST_F(SiteInstanceTest, SetSite) {
280 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL)); 312 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL));
281 EXPECT_FALSE(instance->has_site()); 313 EXPECT_FALSE(instance->has_site());
282 EXPECT_TRUE(instance->site().is_empty()); 314 EXPECT_TRUE(instance->site().is_empty());
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 site_instance_a1->GetRelatedSiteInstance(url_b1)); 407 site_instance_a1->GetRelatedSiteInstance(url_b1));
376 408
377 // A second visit to the original site should return the same SiteInstance. 409 // A second visit to the original site should return the same SiteInstance.
378 const GURL url_a2("http://www.google.com/2.html"); 410 const GURL url_a2("http://www.google.com/2.html");
379 EXPECT_EQ(site_instance_a1.get(), 411 EXPECT_EQ(site_instance_a1.get(),
380 browsing_instance->GetSiteInstanceForURL(url_a2)); 412 browsing_instance->GetSiteInstanceForURL(url_a2));
381 EXPECT_EQ(site_instance_a1.get(), 413 EXPECT_EQ(site_instance_a1.get(),
382 site_instance_a1->GetRelatedSiteInstance(url_a2)); 414 site_instance_a1->GetRelatedSiteInstance(url_a2));
383 415
384 // A visit to the original site in a new BrowsingInstance (same or different 416 // A visit to the original site in a new BrowsingInstance (same or different
385 // profile) should return a different SiteInstance. 417 // browser context) should return a different SiteInstance.
386 TestBrowsingInstance* browsing_instance2 = 418 TestBrowsingInstance* browsing_instance2 =
387 new TestBrowsingInstance(NULL, &deleteCounter); 419 new TestBrowsingInstance(NULL, &deleteCounter);
388 browsing_instance2->use_process_per_site = false; 420 browsing_instance2->use_process_per_site = false;
389 // Ensure the new SiteInstance is ref counted so that it gets deleted. 421 // Ensure the new SiteInstance is ref counted so that it gets deleted.
390 scoped_refptr<SiteInstance> site_instance_a2_2( 422 scoped_refptr<SiteInstance> site_instance_a2_2(
391 browsing_instance2->GetSiteInstanceForURL(url_a2)); 423 browsing_instance2->GetSiteInstanceForURL(url_a2));
392 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); 424 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get());
393 425
394 // Should be able to see that we do have SiteInstances. 426 // Should be able to see that we do have SiteInstances.
395 EXPECT_TRUE(browsing_instance->HasSiteInstance( 427 EXPECT_TRUE(browsing_instance->HasSiteInstance(
396 GURL("http://mail.google.com"))); 428 GURL("http://mail.google.com")));
397 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 429 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
398 GURL("http://mail.google.com"))); 430 GURL("http://mail.google.com")));
399 EXPECT_TRUE(browsing_instance->HasSiteInstance( 431 EXPECT_TRUE(browsing_instance->HasSiteInstance(
400 GURL("http://mail.yahoo.com"))); 432 GURL("http://mail.yahoo.com")));
401 433
402 // Should be able to see that we don't have SiteInstances. 434 // Should be able to see that we don't have SiteInstances.
403 EXPECT_FALSE(browsing_instance->HasSiteInstance( 435 EXPECT_FALSE(browsing_instance->HasSiteInstance(
404 GURL("https://www.google.com"))); 436 GURL("https://www.google.com")));
405 EXPECT_FALSE(browsing_instance2->HasSiteInstance( 437 EXPECT_FALSE(browsing_instance2->HasSiteInstance(
406 GURL("http://www.yahoo.com"))); 438 GURL("http://www.yahoo.com")));
407 439
408 // browsing_instances will be deleted when their SiteInstances are deleted 440 // browsing_instances will be deleted when their SiteInstances are deleted
409 } 441 }
410 442
411 // Test to ensure that there is only one SiteInstance per site for an entire 443 // Test to ensure that there is only one SiteInstance per site for an entire
412 // Profile, if process-per-site is in use. 444 // BrowserContext, if process-per-site is in use.
413 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInProfile) { 445 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) {
414 int deleteCounter = 0; 446 int deleteCounter = 0;
415 TestBrowsingInstance* browsing_instance = 447 TestBrowsingInstance* browsing_instance =
416 new TestBrowsingInstance(NULL, &deleteCounter); 448 new TestBrowsingInstance(NULL, &deleteCounter);
417 browsing_instance->use_process_per_site = true; 449 browsing_instance->use_process_per_site = true;
418 450
419 const GURL url_a1("http://www.google.com/1.html"); 451 const GURL url_a1("http://www.google.com/1.html");
420 scoped_refptr<SiteInstance> site_instance_a1( 452 scoped_refptr<SiteInstance> site_instance_a1(
421 browsing_instance->GetSiteInstanceForURL(url_a1)); 453 browsing_instance->GetSiteInstanceForURL(url_a1));
422 EXPECT_TRUE(site_instance_a1.get() != NULL); 454 EXPECT_TRUE(site_instance_a1.get() != NULL);
423 455
424 // A separate site should create a separate SiteInstance. 456 // A separate site should create a separate SiteInstance.
425 const GURL url_b1("http://www.yahoo.com/"); 457 const GURL url_b1("http://www.yahoo.com/");
426 scoped_refptr<SiteInstance> site_instance_b1( 458 scoped_refptr<SiteInstance> site_instance_b1(
427 browsing_instance->GetSiteInstanceForURL(url_b1)); 459 browsing_instance->GetSiteInstanceForURL(url_b1));
428 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); 460 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
429 461
430 // Getting the new SiteInstance from the BrowsingInstance and from another 462 // Getting the new SiteInstance from the BrowsingInstance and from another
431 // SiteInstance in the BrowsingInstance should give the same result. 463 // SiteInstance in the BrowsingInstance should give the same result.
432 EXPECT_EQ(site_instance_b1.get(), 464 EXPECT_EQ(site_instance_b1.get(),
433 site_instance_a1->GetRelatedSiteInstance(url_b1)); 465 site_instance_a1->GetRelatedSiteInstance(url_b1));
434 466
435 // A second visit to the original site should return the same SiteInstance. 467 // A second visit to the original site should return the same SiteInstance.
436 const GURL url_a2("http://www.google.com/2.html"); 468 const GURL url_a2("http://www.google.com/2.html");
437 EXPECT_EQ(site_instance_a1.get(), 469 EXPECT_EQ(site_instance_a1.get(),
438 browsing_instance->GetSiteInstanceForURL(url_a2)); 470 browsing_instance->GetSiteInstanceForURL(url_a2));
439 EXPECT_EQ(site_instance_a1.get(), 471 EXPECT_EQ(site_instance_a1.get(),
440 site_instance_a1->GetRelatedSiteInstance(url_a2)); 472 site_instance_a1->GetRelatedSiteInstance(url_a2));
441 473
442 // A visit to the original site in a new BrowsingInstance (same profile) 474 // A visit to the original site in a new BrowsingInstance (same browser
443 // should also return the same SiteInstance. 475 // context) should also return the same SiteInstance.
444 // This BrowsingInstance doesn't get its own SiteInstance within the test, so 476 // This BrowsingInstance doesn't get its own SiteInstance within the test, so
445 // it won't be deleted by its children. Thus, we'll keep a ref count to it 477 // it won't be deleted by its children. Thus, we'll keep a ref count to it
446 // to make sure it gets deleted. 478 // to make sure it gets deleted.
447 scoped_refptr<TestBrowsingInstance> browsing_instance2( 479 scoped_refptr<TestBrowsingInstance> browsing_instance2(
448 new TestBrowsingInstance(NULL, &deleteCounter)); 480 new TestBrowsingInstance(NULL, &deleteCounter));
449 browsing_instance2->use_process_per_site = true; 481 browsing_instance2->use_process_per_site = true;
450 EXPECT_EQ(site_instance_a1.get(), 482 EXPECT_EQ(site_instance_a1.get(),
451 browsing_instance2->GetSiteInstanceForURL(url_a2)); 483 browsing_instance2->GetSiteInstanceForURL(url_a2));
452 484
453 // A visit to the original site in a new BrowsingInstance (different profile) 485 // A visit to the original site in a new BrowsingInstance (different browser
454 // should return a different SiteInstance. 486 // context) should return a different SiteInstance.
455 scoped_ptr<TestingProfile> profile(new TestingProfile()); 487 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
456 TestBrowsingInstance* browsing_instance3 = 488 TestBrowsingInstance* browsing_instance3 =
457 new TestBrowsingInstance(profile.get(), &deleteCounter); 489 new TestBrowsingInstance(browser_context.get(), &deleteCounter);
458 browsing_instance3->use_process_per_site = true; 490 browsing_instance3->use_process_per_site = true;
459 // Ensure the new SiteInstance is ref counted so that it gets deleted. 491 // Ensure the new SiteInstance is ref counted so that it gets deleted.
460 scoped_refptr<SiteInstance> site_instance_a2_3( 492 scoped_refptr<SiteInstance> site_instance_a2_3(
461 browsing_instance3->GetSiteInstanceForURL(url_a2)); 493 browsing_instance3->GetSiteInstanceForURL(url_a2));
462 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); 494 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get());
463 495
464 // Should be able to see that we do have SiteInstances. 496 // Should be able to see that we do have SiteInstances.
465 EXPECT_TRUE(browsing_instance->HasSiteInstance( 497 EXPECT_TRUE(browsing_instance->HasSiteInstance(
466 GURL("http://mail.google.com"))); // visited before 498 GURL("http://mail.google.com"))); // visited before
467 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 499 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
468 GURL("http://mail.google.com"))); // visited before 500 GURL("http://mail.google.com"))); // visited before
469 EXPECT_TRUE(browsing_instance->HasSiteInstance( 501 EXPECT_TRUE(browsing_instance->HasSiteInstance(
470 GURL("http://mail.yahoo.com"))); // visited before 502 GURL("http://mail.yahoo.com"))); // visited before
471 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 503 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
472 GURL("http://www.yahoo.com"))); // different BI, but same profile 504 GURL("http://www.yahoo.com"))); // different BI, but same browser context
473 505
474 // Should be able to see that we don't have SiteInstances. 506 // Should be able to see that we don't have SiteInstances.
475 EXPECT_FALSE(browsing_instance->HasSiteInstance( 507 EXPECT_FALSE(browsing_instance->HasSiteInstance(
476 GURL("https://www.google.com"))); // not visited before 508 GURL("https://www.google.com"))); // not visited before
477 EXPECT_FALSE(browsing_instance3->HasSiteInstance( 509 EXPECT_FALSE(browsing_instance3->HasSiteInstance(
478 GURL("http://www.yahoo.com"))); // different BI, different profile 510 GURL("http://www.yahoo.com"))); // different BI, different context
479 511
480 // browsing_instances will be deleted when their SiteInstances are deleted 512 // browsing_instances will be deleted when their SiteInstances are deleted
481 } 513 }
482 514
483 static SiteInstance* CreateSiteInstance(RenderProcessHostFactory* factory, 515 static SiteInstance* CreateSiteInstance(RenderProcessHostFactory* factory,
484 const GURL& url) { 516 const GURL& url) {
485 SiteInstance* instance = SiteInstance::CreateSiteInstanceForURL(NULL, url); 517 SiteInstance* instance = SiteInstance::CreateSiteInstanceForURL(NULL, url);
486 instance->set_render_process_host_factory(factory); 518 instance->set_render_process_host_factory(factory);
487 return instance; 519 return instance;
488 } 520 }
489 521
490 // Test to ensure that pages that require certain privileges are grouped 522 // Test to ensure that pages that require certain privileges are grouped
491 // in processes with similar pages. 523 // in processes with similar pages.
492 TEST_F(SiteInstanceTest, ProcessSharingByType) { 524 TEST_F(SiteInstanceTest, ProcessSharingByType) {
493 MockRenderProcessHostFactory rph_factory; 525 MockRenderProcessHostFactory rph_factory;
494 ChildProcessSecurityPolicy* policy = 526 ChildProcessSecurityPolicy* policy =
495 ChildProcessSecurityPolicy::GetInstance(); 527 ChildProcessSecurityPolicy::GetInstance();
496 528
497 // Make a bunch of mock renderers so that we hit the limit. 529 // Make a bunch of mock renderers so that we hit the limit.
498 std::vector<MockRenderProcessHost*> hosts; 530 std::vector<MockRenderProcessHost*> hosts;
499 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) 531 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i)
500 hosts.push_back(new MockRenderProcessHost(NULL)); 532 hosts.push_back(new MockRenderProcessHost(NULL));
501 533
502 // Create some extension instances and make sure they share a process. 534 // Create some extension instances and make sure they share a process.
503 scoped_refptr<SiteInstance> extension1_instance( 535 scoped_refptr<SiteInstance> extension1_instance(
504 CreateSiteInstance(&rph_factory, GURL("chrome-extension://foo/bar"))); 536 CreateSiteInstance(&rph_factory,
505 policy->GrantExtensionBindings(extension1_instance->GetProcess()->id()); 537 GURL(kPrivilegedScheme + std::string("://foo/bar"))));
538 SetPrivilegedProcessId(extension1_instance->GetProcess()->id());
506 539
507 scoped_refptr<SiteInstance> extension2_instance( 540 scoped_refptr<SiteInstance> extension2_instance(
508 CreateSiteInstance(&rph_factory, GURL("chrome-extension://baz/bar"))); 541 CreateSiteInstance(&rph_factory,
542 GURL(kPrivilegedScheme + std::string("://baz/bar"))));
509 543
510 scoped_ptr<RenderProcessHost> extension_host( 544 scoped_ptr<RenderProcessHost> extension_host(
511 extension1_instance->GetProcess()); 545 extension1_instance->GetProcess());
512 EXPECT_EQ(extension1_instance->GetProcess(), 546 EXPECT_EQ(extension1_instance->GetProcess(),
513 extension2_instance->GetProcess()); 547 extension2_instance->GetProcess());
514 548
515 // Create some WebUI instances and make sure they share a process. 549 // Create some WebUI instances and make sure they share a process.
516 scoped_refptr<SiteInstance> webui1_instance(CreateSiteInstance(&rph_factory, 550 scoped_refptr<SiteInstance> webui1_instance(CreateSiteInstance(&rph_factory,
517 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); 551 GURL(chrome::kChromeUIScheme + std::string("://newtab"))));
518 policy->GrantWebUIBindings(webui1_instance->GetProcess()->id()); 552 policy->GrantWebUIBindings(webui1_instance->GetProcess()->id());
519 553
520 scoped_refptr<SiteInstance> webui2_instance( CreateSiteInstance(&rph_factory, 554 scoped_refptr<SiteInstance> webui2_instance( CreateSiteInstance(&rph_factory,
521 GURL(chrome::kChromeUIScheme + std::string("://history")))); 555 GURL(chrome::kChromeUIScheme + std::string("://history"))));
522 556
523 scoped_ptr<RenderProcessHost> dom_host(webui1_instance->GetProcess()); 557 scoped_ptr<RenderProcessHost> dom_host(webui1_instance->GetProcess());
524 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); 558 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess());
525 559
526 // Make sure none of differing privilege processes are mixed. 560 // Make sure none of differing privilege processes are mixed.
527 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); 561 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess());
528 562
529 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) { 563 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) {
530 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); 564 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]);
531 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); 565 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]);
532 } 566 }
533 567
534 STLDeleteContainerPointers(hosts.begin(), hosts.end()); 568 STLDeleteContainerPointers(hosts.begin(), hosts.end());
535 } 569 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698