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

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
« no previous file with comments | « content/browser/site_instance.cc ('k') | content/content_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 IsSuitableHost(RenderProcessHost* process_host,
69 const GURL& site_url) OVERRIDE {
70 return (privileged_process_id_ == process_host->id()) ==
71 site_url.SchemeIs(kPrivilegedScheme);
72 }
73
63 virtual GURL GetEffectiveURL(content::BrowserContext* browser_context, 74 virtual GURL GetEffectiveURL(content::BrowserContext* browser_context,
64 const GURL& url) OVERRIDE { 75 const GURL& url) OVERRIDE {
65 return url; 76 return url;
66 } 77 }
67 78
68 void SetOriginalClient(content::ContentBrowserClient* old_browser_client) { 79 void SetOriginalClient(content::ContentBrowserClient* old_browser_client) {
69 old_browser_client_ = old_browser_client; 80 old_browser_client_ = old_browser_client;
70 } 81 }
71 82
83 void SetPrivilegedProcessId(int process_id) {
84 privileged_process_id_ = process_id;
85 }
86
72 private: 87 private:
73 SiteInstanceTestWebUIFactory factory_; 88 SiteInstanceTestWebUIFactory factory_;
74 content::ContentBrowserClient* old_browser_client_; 89 content::ContentBrowserClient* old_browser_client_;
90 int privileged_process_id_;
75 }; 91 };
76 92
77 class SiteInstanceTest : public testing::Test { 93 class SiteInstanceTest : public testing::Test {
78 public: 94 public:
79 SiteInstanceTest() 95 SiteInstanceTest()
80 : ui_thread_(BrowserThread::UI, &message_loop_), 96 : ui_thread_(BrowserThread::UI, &message_loop_),
81 old_browser_client_(NULL) { 97 old_browser_client_(NULL) {
82 } 98 }
83 99
84 virtual void SetUp() { 100 virtual void SetUp() {
85 old_browser_client_ = content::GetContentClient()->browser(); 101 old_browser_client_ = content::GetContentClient()->browser();
86 browser_client_.SetOriginalClient(old_browser_client_); 102 browser_client_.SetOriginalClient(old_browser_client_);
87 content::GetContentClient()->set_browser(&browser_client_); 103 content::GetContentClient()->set_browser(&browser_client_);
104 url_util::AddStandardScheme(kPrivilegedScheme);
105 url_util::AddStandardScheme(chrome::kChromeUIScheme);
88 } 106 }
89 107
90 virtual void TearDown() { 108 virtual void TearDown() {
91 content::GetContentClient()->set_browser(old_browser_client_); 109 content::GetContentClient()->set_browser(old_browser_client_);
92 } 110 }
93 111
112 void SetPrivilegedProcessId(int process_id) {
113 browser_client_.SetPrivilegedProcessId(process_id);
114 }
115
94 private: 116 private:
95 MessageLoopForUI message_loop_; 117 MessageLoopForUI message_loop_;
96 BrowserThread ui_thread_; 118 BrowserThread ui_thread_;
97 119
98 SiteInstanceTestBrowserClient browser_client_; 120 SiteInstanceTestBrowserClient browser_client_;
99 content::ContentBrowserClient* old_browser_client_; 121 content::ContentBrowserClient* old_browser_client_;
100 }; 122 };
101 123
102 class TestBrowsingInstance : public BrowsingInstance { 124 class TestBrowsingInstance : public BrowsingInstance {
103 public: 125 public:
104 TestBrowsingInstance(Profile* profile, int* deleteCounter) 126 TestBrowsingInstance(content::BrowserContext* browser_context,
105 : BrowsingInstance(profile), 127 int* deleteCounter)
128 : BrowsingInstance(browser_context),
106 use_process_per_site(false), 129 use_process_per_site(false),
107 deleteCounter_(deleteCounter) { 130 deleteCounter_(deleteCounter) {
108 } 131 }
109 132
110 // Overrides BrowsingInstance::ShouldUseProcessPerSite so that we can test 133 // Overrides BrowsingInstance::ShouldUseProcessPerSite so that we can test
111 // both alternatives without using command-line switches. 134 // both alternatives without using command-line switches.
112 bool ShouldUseProcessPerSite(const GURL& url) { 135 bool ShouldUseProcessPerSite(const GURL& url) {
113 return use_process_per_site; 136 return use_process_per_site;
114 } 137 }
115 138
116 // Set by individual tests. 139 // Set by individual tests.
117 bool use_process_per_site; 140 bool use_process_per_site;
118 141
119 private: 142 private:
120 ~TestBrowsingInstance() { 143 ~TestBrowsingInstance() {
121 (*deleteCounter_)++; 144 (*deleteCounter_)++;
122 } 145 }
123 146
124 int* deleteCounter_; 147 int* deleteCounter_;
125 }; 148 };
126 149
127 class TestSiteInstance : public SiteInstance { 150 class TestSiteInstance : public SiteInstance {
128 public: 151 public:
129 static TestSiteInstance* CreateTestSiteInstance(Profile* profile, 152 static TestSiteInstance* CreateTestSiteInstance(
130 int* siteDeleteCounter, 153 content::BrowserContext* browser_context,
131 int* browsingDeleteCounter) { 154 int* siteDeleteCounter,
155 int* browsingDeleteCounter) {
132 TestBrowsingInstance* browsing_instance = 156 TestBrowsingInstance* browsing_instance =
133 new TestBrowsingInstance(profile, browsingDeleteCounter); 157 new TestBrowsingInstance(browser_context, browsingDeleteCounter);
134 return new TestSiteInstance(browsing_instance, siteDeleteCounter); 158 return new TestSiteInstance(browsing_instance, siteDeleteCounter);
135 } 159 }
136 160
137 private: 161 private:
138 TestSiteInstance(BrowsingInstance* browsing_instance, int* deleteCounter) 162 TestSiteInstance(BrowsingInstance* browsing_instance, int* deleteCounter)
139 : SiteInstance(browsing_instance), deleteCounter_(deleteCounter) {} 163 : SiteInstance(browsing_instance), deleteCounter_(deleteCounter) {}
140 ~TestSiteInstance() { 164 ~TestSiteInstance() {
141 (*deleteCounter_)++; 165 (*deleteCounter_)++;
142 } 166 }
143 167
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 delete e1; 203 delete e1;
180 EXPECT_EQ(0, siteDeleteCounter); 204 EXPECT_EQ(0, siteDeleteCounter);
181 EXPECT_EQ(0, browsingDeleteCounter); 205 EXPECT_EQ(0, browsingDeleteCounter);
182 delete e2; 206 delete e2;
183 EXPECT_EQ(1, siteDeleteCounter); 207 EXPECT_EQ(1, siteDeleteCounter);
184 // instance is now deleted 208 // instance is now deleted
185 EXPECT_EQ(1, browsingDeleteCounter); 209 EXPECT_EQ(1, browsingDeleteCounter);
186 // browsing_instance is now deleted 210 // browsing_instance is now deleted
187 211
188 // Ensure that instances are deleted when their RenderViewHosts are gone. 212 // Ensure that instances are deleted when their RenderViewHosts are gone.
189 scoped_ptr<TestingProfile> profile(new TestingProfile()); 213 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
190 instance = 214 instance =
191 TestSiteInstance::CreateTestSiteInstance(profile.get(), 215 TestSiteInstance::CreateTestSiteInstance(browser_context.get(),
192 &siteDeleteCounter, 216 &siteDeleteCounter,
193 &browsingDeleteCounter); 217 &browsingDeleteCounter);
194 { 218 {
195 TabContents contents(profile.get(), instance, MSG_ROUTING_NONE, NULL, NULL); 219 TabContents contents(browser_context.get(),
220 instance,
221 MSG_ROUTING_NONE,
222 NULL,
223 NULL);
196 EXPECT_EQ(1, siteDeleteCounter); 224 EXPECT_EQ(1, siteDeleteCounter);
197 EXPECT_EQ(1, browsingDeleteCounter); 225 EXPECT_EQ(1, browsingDeleteCounter);
198 } 226 }
199 227
200 // Make sure that we flush any messages related to the above TabContents 228 // Make sure that we flush any messages related to the above TabContents
201 // destruction. 229 // destruction.
202 MessageLoop::current()->RunAllPending(); 230 MessageLoop::current()->RunAllPending();
203 231
204 EXPECT_EQ(2, siteDeleteCounter); 232 EXPECT_EQ(2, siteDeleteCounter);
205 EXPECT_EQ(2, browsingDeleteCounter); 233 EXPECT_EQ(2, browsingDeleteCounter);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 281
254 // Make sure max_page_id_ is monotonically increasing. 282 // Make sure max_page_id_ is monotonically increasing.
255 instance->UpdateMaxPageID(3); 283 instance->UpdateMaxPageID(3);
256 instance->UpdateMaxPageID(1); 284 instance->UpdateMaxPageID(1);
257 EXPECT_EQ(3, instance->max_page_id()); 285 EXPECT_EQ(3, instance->max_page_id());
258 } 286 }
259 287
260 // Test to ensure GetProcess returns and creates processes correctly. 288 // Test to ensure GetProcess returns and creates processes correctly.
261 TEST_F(SiteInstanceTest, GetProcess) { 289 TEST_F(SiteInstanceTest, GetProcess) {
262 // Ensure that GetProcess returns a process. 290 // Ensure that GetProcess returns a process.
263 scoped_ptr<TestingProfile> profile(new TestingProfile()); 291 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
264 scoped_ptr<RenderProcessHost> host1; 292 scoped_ptr<RenderProcessHost> host1;
265 scoped_refptr<SiteInstance> instance( 293 scoped_refptr<SiteInstance> instance(
266 SiteInstance::CreateSiteInstance(profile.get())); 294 SiteInstance::CreateSiteInstance(browser_context.get()));
267 host1.reset(instance->GetProcess()); 295 host1.reset(instance->GetProcess());
268 EXPECT_TRUE(host1.get() != NULL); 296 EXPECT_TRUE(host1.get() != NULL);
269 297
270 // Ensure that GetProcess creates a new process. 298 // Ensure that GetProcess creates a new process.
271 scoped_refptr<SiteInstance> instance2( 299 scoped_refptr<SiteInstance> instance2(
272 SiteInstance::CreateSiteInstance(profile.get())); 300 SiteInstance::CreateSiteInstance(browser_context.get()));
273 scoped_ptr<RenderProcessHost> host2(instance2->GetProcess()); 301 scoped_ptr<RenderProcessHost> host2(instance2->GetProcess());
274 EXPECT_TRUE(host2.get() != NULL); 302 EXPECT_TRUE(host2.get() != NULL);
275 EXPECT_NE(host1.get(), host2.get()); 303 EXPECT_NE(host1.get(), host2.get());
276 } 304 }
277 305
278 // Test to ensure SetSite and site() work properly. 306 // Test to ensure SetSite and site() work properly.
279 TEST_F(SiteInstanceTest, SetSite) { 307 TEST_F(SiteInstanceTest, SetSite) {
280 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL)); 308 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL));
281 EXPECT_FALSE(instance->has_site()); 309 EXPECT_FALSE(instance->has_site());
282 EXPECT_TRUE(instance->site().is_empty()); 310 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)); 403 site_instance_a1->GetRelatedSiteInstance(url_b1));
376 404
377 // A second visit to the original site should return the same SiteInstance. 405 // A second visit to the original site should return the same SiteInstance.
378 const GURL url_a2("http://www.google.com/2.html"); 406 const GURL url_a2("http://www.google.com/2.html");
379 EXPECT_EQ(site_instance_a1.get(), 407 EXPECT_EQ(site_instance_a1.get(),
380 browsing_instance->GetSiteInstanceForURL(url_a2)); 408 browsing_instance->GetSiteInstanceForURL(url_a2));
381 EXPECT_EQ(site_instance_a1.get(), 409 EXPECT_EQ(site_instance_a1.get(),
382 site_instance_a1->GetRelatedSiteInstance(url_a2)); 410 site_instance_a1->GetRelatedSiteInstance(url_a2));
383 411
384 // A visit to the original site in a new BrowsingInstance (same or different 412 // A visit to the original site in a new BrowsingInstance (same or different
385 // profile) should return a different SiteInstance. 413 // browser context) should return a different SiteInstance.
386 TestBrowsingInstance* browsing_instance2 = 414 TestBrowsingInstance* browsing_instance2 =
387 new TestBrowsingInstance(NULL, &deleteCounter); 415 new TestBrowsingInstance(NULL, &deleteCounter);
388 browsing_instance2->use_process_per_site = false; 416 browsing_instance2->use_process_per_site = false;
389 // Ensure the new SiteInstance is ref counted so that it gets deleted. 417 // Ensure the new SiteInstance is ref counted so that it gets deleted.
390 scoped_refptr<SiteInstance> site_instance_a2_2( 418 scoped_refptr<SiteInstance> site_instance_a2_2(
391 browsing_instance2->GetSiteInstanceForURL(url_a2)); 419 browsing_instance2->GetSiteInstanceForURL(url_a2));
392 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); 420 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get());
393 421
394 // Should be able to see that we do have SiteInstances. 422 // Should be able to see that we do have SiteInstances.
395 EXPECT_TRUE(browsing_instance->HasSiteInstance( 423 EXPECT_TRUE(browsing_instance->HasSiteInstance(
396 GURL("http://mail.google.com"))); 424 GURL("http://mail.google.com")));
397 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 425 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
398 GURL("http://mail.google.com"))); 426 GURL("http://mail.google.com")));
399 EXPECT_TRUE(browsing_instance->HasSiteInstance( 427 EXPECT_TRUE(browsing_instance->HasSiteInstance(
400 GURL("http://mail.yahoo.com"))); 428 GURL("http://mail.yahoo.com")));
401 429
402 // Should be able to see that we don't have SiteInstances. 430 // Should be able to see that we don't have SiteInstances.
403 EXPECT_FALSE(browsing_instance->HasSiteInstance( 431 EXPECT_FALSE(browsing_instance->HasSiteInstance(
404 GURL("https://www.google.com"))); 432 GURL("https://www.google.com")));
405 EXPECT_FALSE(browsing_instance2->HasSiteInstance( 433 EXPECT_FALSE(browsing_instance2->HasSiteInstance(
406 GURL("http://www.yahoo.com"))); 434 GURL("http://www.yahoo.com")));
407 435
408 // browsing_instances will be deleted when their SiteInstances are deleted 436 // browsing_instances will be deleted when their SiteInstances are deleted
409 } 437 }
410 438
411 // 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
412 // Profile, if process-per-site is in use. 440 // BrowserContext, if process-per-site is in use.
413 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInProfile) { 441 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) {
414 int deleteCounter = 0; 442 int deleteCounter = 0;
415 TestBrowsingInstance* browsing_instance = 443 TestBrowsingInstance* browsing_instance =
416 new TestBrowsingInstance(NULL, &deleteCounter); 444 new TestBrowsingInstance(NULL, &deleteCounter);
417 browsing_instance->use_process_per_site = true; 445 browsing_instance->use_process_per_site = true;
418 446
419 const GURL url_a1("http://www.google.com/1.html"); 447 const GURL url_a1("http://www.google.com/1.html");
420 scoped_refptr<SiteInstance> site_instance_a1( 448 scoped_refptr<SiteInstance> site_instance_a1(
421 browsing_instance->GetSiteInstanceForURL(url_a1)); 449 browsing_instance->GetSiteInstanceForURL(url_a1));
422 EXPECT_TRUE(site_instance_a1.get() != NULL); 450 EXPECT_TRUE(site_instance_a1.get() != NULL);
423 451
424 // A separate site should create a separate SiteInstance. 452 // A separate site should create a separate SiteInstance.
425 const GURL url_b1("http://www.yahoo.com/"); 453 const GURL url_b1("http://www.yahoo.com/");
426 scoped_refptr<SiteInstance> site_instance_b1( 454 scoped_refptr<SiteInstance> site_instance_b1(
427 browsing_instance->GetSiteInstanceForURL(url_b1)); 455 browsing_instance->GetSiteInstanceForURL(url_b1));
428 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); 456 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
429 457
430 // Getting the new SiteInstance from the BrowsingInstance and from another 458 // Getting the new SiteInstance from the BrowsingInstance and from another
431 // SiteInstance in the BrowsingInstance should give the same result. 459 // SiteInstance in the BrowsingInstance should give the same result.
432 EXPECT_EQ(site_instance_b1.get(), 460 EXPECT_EQ(site_instance_b1.get(),
433 site_instance_a1->GetRelatedSiteInstance(url_b1)); 461 site_instance_a1->GetRelatedSiteInstance(url_b1));
434 462
435 // A second visit to the original site should return the same SiteInstance. 463 // A second visit to the original site should return the same SiteInstance.
436 const GURL url_a2("http://www.google.com/2.html"); 464 const GURL url_a2("http://www.google.com/2.html");
437 EXPECT_EQ(site_instance_a1.get(), 465 EXPECT_EQ(site_instance_a1.get(),
438 browsing_instance->GetSiteInstanceForURL(url_a2)); 466 browsing_instance->GetSiteInstanceForURL(url_a2));
439 EXPECT_EQ(site_instance_a1.get(), 467 EXPECT_EQ(site_instance_a1.get(),
440 site_instance_a1->GetRelatedSiteInstance(url_a2)); 468 site_instance_a1->GetRelatedSiteInstance(url_a2));
441 469
442 // A visit to the original site in a new BrowsingInstance (same profile) 470 // A visit to the original site in a new BrowsingInstance (same browser
443 // should also return the same SiteInstance. 471 // context) should also return the same SiteInstance.
444 // This BrowsingInstance doesn't get its own SiteInstance within the test, so 472 // 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 473 // it won't be deleted by its children. Thus, we'll keep a ref count to it
446 // to make sure it gets deleted. 474 // to make sure it gets deleted.
447 scoped_refptr<TestBrowsingInstance> browsing_instance2( 475 scoped_refptr<TestBrowsingInstance> browsing_instance2(
448 new TestBrowsingInstance(NULL, &deleteCounter)); 476 new TestBrowsingInstance(NULL, &deleteCounter));
449 browsing_instance2->use_process_per_site = true; 477 browsing_instance2->use_process_per_site = true;
450 EXPECT_EQ(site_instance_a1.get(), 478 EXPECT_EQ(site_instance_a1.get(),
451 browsing_instance2->GetSiteInstanceForURL(url_a2)); 479 browsing_instance2->GetSiteInstanceForURL(url_a2));
452 480
453 // A visit to the original site in a new BrowsingInstance (different profile) 481 // A visit to the original site in a new BrowsingInstance (different browser
454 // should return a different SiteInstance. 482 // context) should return a different SiteInstance.
455 scoped_ptr<TestingProfile> profile(new TestingProfile()); 483 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
456 TestBrowsingInstance* browsing_instance3 = 484 TestBrowsingInstance* browsing_instance3 =
457 new TestBrowsingInstance(profile.get(), &deleteCounter); 485 new TestBrowsingInstance(browser_context.get(), &deleteCounter);
458 browsing_instance3->use_process_per_site = true; 486 browsing_instance3->use_process_per_site = true;
459 // Ensure the new SiteInstance is ref counted so that it gets deleted. 487 // Ensure the new SiteInstance is ref counted so that it gets deleted.
460 scoped_refptr<SiteInstance> site_instance_a2_3( 488 scoped_refptr<SiteInstance> site_instance_a2_3(
461 browsing_instance3->GetSiteInstanceForURL(url_a2)); 489 browsing_instance3->GetSiteInstanceForURL(url_a2));
462 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); 490 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get());
463 491
464 // Should be able to see that we do have SiteInstances. 492 // Should be able to see that we do have SiteInstances.
465 EXPECT_TRUE(browsing_instance->HasSiteInstance( 493 EXPECT_TRUE(browsing_instance->HasSiteInstance(
466 GURL("http://mail.google.com"))); // visited before 494 GURL("http://mail.google.com"))); // visited before
467 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 495 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
468 GURL("http://mail.google.com"))); // visited before 496 GURL("http://mail.google.com"))); // visited before
469 EXPECT_TRUE(browsing_instance->HasSiteInstance( 497 EXPECT_TRUE(browsing_instance->HasSiteInstance(
470 GURL("http://mail.yahoo.com"))); // visited before 498 GURL("http://mail.yahoo.com"))); // visited before
471 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 499 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
472 GURL("http://www.yahoo.com"))); // different BI, but same profile 500 GURL("http://www.yahoo.com"))); // different BI, but same browser context
473 501
474 // Should be able to see that we don't have SiteInstances. 502 // Should be able to see that we don't have SiteInstances.
475 EXPECT_FALSE(browsing_instance->HasSiteInstance( 503 EXPECT_FALSE(browsing_instance->HasSiteInstance(
476 GURL("https://www.google.com"))); // not visited before 504 GURL("https://www.google.com"))); // not visited before
477 EXPECT_FALSE(browsing_instance3->HasSiteInstance( 505 EXPECT_FALSE(browsing_instance3->HasSiteInstance(
478 GURL("http://www.yahoo.com"))); // different BI, different profile 506 GURL("http://www.yahoo.com"))); // different BI, different context
479 507
480 // browsing_instances will be deleted when their SiteInstances are deleted 508 // browsing_instances will be deleted when their SiteInstances are deleted
481 } 509 }
482 510
483 static SiteInstance* CreateSiteInstance(RenderProcessHostFactory* factory, 511 static SiteInstance* CreateSiteInstance(RenderProcessHostFactory* factory,
484 const GURL& url) { 512 const GURL& url) {
485 SiteInstance* instance = SiteInstance::CreateSiteInstanceForURL(NULL, url); 513 SiteInstance* instance = SiteInstance::CreateSiteInstanceForURL(NULL, url);
486 instance->set_render_process_host_factory(factory); 514 instance->set_render_process_host_factory(factory);
487 return instance; 515 return instance;
488 } 516 }
489 517
490 // Test to ensure that pages that require certain privileges are grouped 518 // Test to ensure that pages that require certain privileges are grouped
491 // in processes with similar pages. 519 // in processes with similar pages.
492 TEST_F(SiteInstanceTest, ProcessSharingByType) { 520 TEST_F(SiteInstanceTest, ProcessSharingByType) {
493 MockRenderProcessHostFactory rph_factory; 521 MockRenderProcessHostFactory rph_factory;
494 ChildProcessSecurityPolicy* policy = 522 ChildProcessSecurityPolicy* policy =
495 ChildProcessSecurityPolicy::GetInstance(); 523 ChildProcessSecurityPolicy::GetInstance();
496 524
497 // Make a bunch of mock renderers so that we hit the limit. 525 // Make a bunch of mock renderers so that we hit the limit.
498 std::vector<MockRenderProcessHost*> hosts; 526 std::vector<MockRenderProcessHost*> hosts;
499 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) 527 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i)
500 hosts.push_back(new MockRenderProcessHost(NULL)); 528 hosts.push_back(new MockRenderProcessHost(NULL));
501 529
502 // Create some extension instances and make sure they share a process. 530 // Create some extension instances and make sure they share a process.
503 scoped_refptr<SiteInstance> extension1_instance( 531 scoped_refptr<SiteInstance> extension1_instance(
504 CreateSiteInstance(&rph_factory, GURL("chrome-extension://foo/bar"))); 532 CreateSiteInstance(&rph_factory,
505 policy->GrantExtensionBindings(extension1_instance->GetProcess()->id()); 533 GURL(kPrivilegedScheme + std::string("://foo/bar"))));
534 SetPrivilegedProcessId(extension1_instance->GetProcess()->id());
506 535
507 scoped_refptr<SiteInstance> extension2_instance( 536 scoped_refptr<SiteInstance> extension2_instance(
508 CreateSiteInstance(&rph_factory, GURL("chrome-extension://baz/bar"))); 537 CreateSiteInstance(&rph_factory,
538 GURL(kPrivilegedScheme + std::string("://baz/bar"))));
509 539
510 scoped_ptr<RenderProcessHost> extension_host( 540 scoped_ptr<RenderProcessHost> extension_host(
511 extension1_instance->GetProcess()); 541 extension1_instance->GetProcess());
512 EXPECT_EQ(extension1_instance->GetProcess(), 542 EXPECT_EQ(extension1_instance->GetProcess(),
513 extension2_instance->GetProcess()); 543 extension2_instance->GetProcess());
514 544
515 // Create some WebUI instances and make sure they share a process. 545 // Create some WebUI instances and make sure they share a process.
516 scoped_refptr<SiteInstance> webui1_instance(CreateSiteInstance(&rph_factory, 546 scoped_refptr<SiteInstance> webui1_instance(CreateSiteInstance(&rph_factory,
517 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); 547 GURL(chrome::kChromeUIScheme + std::string("://newtab"))));
518 policy->GrantWebUIBindings(webui1_instance->GetProcess()->id()); 548 policy->GrantWebUIBindings(webui1_instance->GetProcess()->id());
519 549
520 scoped_refptr<SiteInstance> webui2_instance( CreateSiteInstance(&rph_factory, 550 scoped_refptr<SiteInstance> webui2_instance( CreateSiteInstance(&rph_factory,
521 GURL(chrome::kChromeUIScheme + std::string("://history")))); 551 GURL(chrome::kChromeUIScheme + std::string("://history"))));
522 552
523 scoped_ptr<RenderProcessHost> dom_host(webui1_instance->GetProcess()); 553 scoped_ptr<RenderProcessHost> dom_host(webui1_instance->GetProcess());
524 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); 554 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess());
525 555
526 // Make sure none of differing privilege processes are mixed. 556 // Make sure none of differing privilege processes are mixed.
527 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); 557 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess());
528 558
529 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) { 559 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) {
530 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); 560 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]);
531 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); 561 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]);
532 } 562 }
533 563
534 STLDeleteContainerPointers(hosts.begin(), hosts.end()); 564 STLDeleteContainerPointers(hosts.begin(), hosts.end());
535 } 565 }
OLDNEW
« no previous file with comments | « content/browser/site_instance.cc ('k') | content/content_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698