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

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

Issue 9147051: Don't swap processes for javascript: URLs. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed the unit test and restored the function to not be virtual. Created 8 years, 11 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
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 "content/browser/browser_thread_impl.h" 8 #include "content/browser/browser_thread_impl.h"
9 #include "content/browser/browsing_instance.h" 9 #include "content/browser/browsing_instance.h"
10 #include "content/browser/child_process_security_policy.h" 10 #include "content/browser/child_process_security_policy.h"
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 112
113 SiteInstanceTestBrowserClient browser_client_; 113 SiteInstanceTestBrowserClient browser_client_;
114 content::ContentBrowserClient* old_browser_client_; 114 content::ContentBrowserClient* old_browser_client_;
115 }; 115 };
116 116
117 class TestBrowsingInstance : public BrowsingInstance { 117 class TestBrowsingInstance : public BrowsingInstance {
118 public: 118 public:
119 TestBrowsingInstance(content::BrowserContext* browser_context, 119 TestBrowsingInstance(content::BrowserContext* browser_context,
120 int* deleteCounter) 120 int* deleteCounter)
121 : BrowsingInstance(browser_context), 121 : BrowsingInstance(browser_context),
122 use_process_per_site(false), 122 use_process_per_site_(false),
123 deleteCounter_(deleteCounter) { 123 delete_counter_(deleteCounter) {
124 } 124 }
125 125
126 // Overrides BrowsingInstance::ShouldUseProcessPerSite so that we can test 126 // Overrides BrowsingInstance::ShouldUseProcessPerSite so that we can test
127 // both alternatives without using command-line switches. 127 // both alternatives without using command-line switches.
128 bool ShouldUseProcessPerSite(const GURL& url) { 128 bool ShouldUseProcessPerSite(const GURL& url) {
129 return use_process_per_site; 129 return use_process_per_site_;
130 }
131
132 void SetProcessPerSite(bool use_process_per_site) {
133 use_process_per_site_ = use_process_per_site;
134 }
135
136 private:
137 virtual ~TestBrowsingInstance() {
138 (*delete_counter_)++;
130 } 139 }
131 140
132 // Set by individual tests. 141 // Set by individual tests.
133 bool use_process_per_site; 142 bool use_process_per_site_;
134 143
135 private: 144 int* delete_counter_;
136 ~TestBrowsingInstance() {
137 (*deleteCounter_)++;
138 }
139
140 int* deleteCounter_;
141 }; 145 };
142 146
143 class TestSiteInstance : public SiteInstance { 147 class TestSiteInstance : public SiteInstance {
144 public: 148 public:
145 static TestSiteInstance* CreateTestSiteInstance( 149 static TestSiteInstance* CreateTestSiteInstance(
146 content::BrowserContext* browser_context, 150 content::BrowserContext* browser_context,
147 int* siteDeleteCounter, 151 int* siteDeleteCounter,
148 int* browsingDeleteCounter) { 152 int* browsingDeleteCounter) {
149 TestBrowsingInstance* browsing_instance = 153 TestBrowsingInstance* browsing_instance =
150 new TestBrowsingInstance(browser_context, browsingDeleteCounter); 154 new TestBrowsingInstance(browser_context, browsingDeleteCounter);
151 return new TestSiteInstance(browsing_instance, siteDeleteCounter); 155 return new TestSiteInstance(browsing_instance, siteDeleteCounter);
152 } 156 }
153 157
158 bool HasProcess() const OVERRIDE {
Charlie Reis 2012/01/13 18:37:46 We can't override this any more if it's not virtua
nasko 2012/01/13 20:54:49 Done.
159 return has_process_;
160 }
161
162 void SetHasProcess(bool process) {
163 has_process_ = process;
164 }
165
166
154 private: 167 private:
155 TestSiteInstance(BrowsingInstance* browsing_instance, int* deleteCounter) 168 TestSiteInstance(BrowsingInstance* browsing_instance, int* deleteCounter)
156 : SiteInstance(browsing_instance), deleteCounter_(deleteCounter) {} 169 : SiteInstance(browsing_instance), delete_counter_(deleteCounter) {}
157 ~TestSiteInstance() { 170 virtual ~TestSiteInstance() {
158 (*deleteCounter_)++; 171 (*delete_counter_)++;
159 } 172 }
160 173
161 int* deleteCounter_; 174 int* delete_counter_;
175 bool has_process_;
162 }; 176 };
163 177
164 } // namespace 178 } // namespace
165 179
166 // Test to ensure no memory leaks for SiteInstance objects. 180 // Test to ensure no memory leaks for SiteInstance objects.
167 TEST_F(SiteInstanceTest, SiteInstanceDestructor) { 181 TEST_F(SiteInstanceTest, SiteInstanceDestructor) {
168 // The existence of these factories will cause TabContents to create our test 182 // The existence of these factories will cause TabContents to create our test
169 // one instead of the real one. 183 // one instead of the real one.
170 MockRenderProcessHostFactory rph_factory; 184 MockRenderProcessHostFactory rph_factory;
171 TestRenderViewHostFactory rvh_factory(&rph_factory); 185 TestRenderViewHostFactory rvh_factory(&rph_factory);
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 EXPECT_TRUE( 373 EXPECT_TRUE(
360 SiteInstance::IsSameWebSite(NULL, url_browser_specified, url_foo)); 374 SiteInstance::IsSameWebSite(NULL, url_browser_specified, url_foo));
361 } 375 }
362 376
363 // Test to ensure that there is only one SiteInstance per site in a given 377 // Test to ensure that there is only one SiteInstance per site in a given
364 // BrowsingInstance, when process-per-site is not in use. 378 // BrowsingInstance, when process-per-site is not in use.
365 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { 379 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) {
366 int deleteCounter = 0; 380 int deleteCounter = 0;
367 TestBrowsingInstance* browsing_instance = 381 TestBrowsingInstance* browsing_instance =
368 new TestBrowsingInstance(NULL, &deleteCounter); 382 new TestBrowsingInstance(NULL, &deleteCounter);
369 browsing_instance->use_process_per_site = false; 383 browsing_instance->SetProcessPerSite(false);
370 384
371 const GURL url_a1("http://www.google.com/1.html"); 385 const GURL url_a1("http://www.google.com/1.html");
372 scoped_refptr<SiteInstance> site_instance_a1( 386 scoped_refptr<SiteInstance> site_instance_a1(
373 browsing_instance->GetSiteInstanceForURL(url_a1)); 387 browsing_instance->GetSiteInstanceForURL(url_a1));
374 EXPECT_TRUE(site_instance_a1.get() != NULL); 388 EXPECT_TRUE(site_instance_a1.get() != NULL);
375 389
376 // A separate site should create a separate SiteInstance. 390 // A separate site should create a separate SiteInstance.
377 const GURL url_b1("http://www.yahoo.com/"); 391 const GURL url_b1("http://www.yahoo.com/");
378 scoped_refptr<SiteInstance> site_instance_b1( 392 scoped_refptr<SiteInstance> site_instance_b1(
379 browsing_instance->GetSiteInstanceForURL(url_b1)); 393 browsing_instance->GetSiteInstanceForURL(url_b1));
380 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); 394 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
381 395
382 // Getting the new SiteInstance from the BrowsingInstance and from another 396 // Getting the new SiteInstance from the BrowsingInstance and from another
383 // SiteInstance in the BrowsingInstance should give the same result. 397 // SiteInstance in the BrowsingInstance should give the same result.
384 EXPECT_EQ(site_instance_b1.get(), 398 EXPECT_EQ(site_instance_b1.get(),
385 site_instance_a1->GetRelatedSiteInstance(url_b1)); 399 site_instance_a1->GetRelatedSiteInstance(url_b1));
386 400
387 // A second visit to the original site should return the same SiteInstance. 401 // A second visit to the original site should return the same SiteInstance.
388 const GURL url_a2("http://www.google.com/2.html"); 402 const GURL url_a2("http://www.google.com/2.html");
389 EXPECT_EQ(site_instance_a1.get(), 403 EXPECT_EQ(site_instance_a1.get(),
390 browsing_instance->GetSiteInstanceForURL(url_a2)); 404 browsing_instance->GetSiteInstanceForURL(url_a2));
391 EXPECT_EQ(site_instance_a1.get(), 405 EXPECT_EQ(site_instance_a1.get(),
392 site_instance_a1->GetRelatedSiteInstance(url_a2)); 406 site_instance_a1->GetRelatedSiteInstance(url_a2));
393 407
394 // A visit to the original site in a new BrowsingInstance (same or different 408 // A visit to the original site in a new BrowsingInstance (same or different
395 // browser context) should return a different SiteInstance. 409 // browser context) should return a different SiteInstance.
396 TestBrowsingInstance* browsing_instance2 = 410 TestBrowsingInstance* browsing_instance2 =
397 new TestBrowsingInstance(NULL, &deleteCounter); 411 new TestBrowsingInstance(NULL, &deleteCounter);
398 browsing_instance2->use_process_per_site = false; 412 browsing_instance2->SetProcessPerSite(false);
399 // Ensure the new SiteInstance is ref counted so that it gets deleted. 413 // Ensure the new SiteInstance is ref counted so that it gets deleted.
400 scoped_refptr<SiteInstance> site_instance_a2_2( 414 scoped_refptr<SiteInstance> site_instance_a2_2(
401 browsing_instance2->GetSiteInstanceForURL(url_a2)); 415 browsing_instance2->GetSiteInstanceForURL(url_a2));
402 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); 416 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get());
403 417
404 // Should be able to see that we do have SiteInstances. 418 // Should be able to see that we do have SiteInstances.
405 EXPECT_TRUE(browsing_instance->HasSiteInstance( 419 EXPECT_TRUE(browsing_instance->HasSiteInstance(
406 GURL("http://mail.google.com"))); 420 GURL("http://mail.google.com")));
407 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 421 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
408 GURL("http://mail.google.com"))); 422 GURL("http://mail.google.com")));
409 EXPECT_TRUE(browsing_instance->HasSiteInstance( 423 EXPECT_TRUE(browsing_instance->HasSiteInstance(
410 GURL("http://mail.yahoo.com"))); 424 GURL("http://mail.yahoo.com")));
411 425
412 // Should be able to see that we don't have SiteInstances. 426 // Should be able to see that we don't have SiteInstances.
413 EXPECT_FALSE(browsing_instance->HasSiteInstance( 427 EXPECT_FALSE(browsing_instance->HasSiteInstance(
414 GURL("https://www.google.com"))); 428 GURL("https://www.google.com")));
415 EXPECT_FALSE(browsing_instance2->HasSiteInstance( 429 EXPECT_FALSE(browsing_instance2->HasSiteInstance(
416 GURL("http://www.yahoo.com"))); 430 GURL("http://www.yahoo.com")));
417 431
418 // browsing_instances will be deleted when their SiteInstances are deleted 432 // browsing_instances will be deleted when their SiteInstances are deleted
419 } 433 }
420 434
421 // Test to ensure that there is only one SiteInstance per site for an entire 435 // Test to ensure that there is only one SiteInstance per site for an entire
422 // BrowserContext, if process-per-site is in use. 436 // BrowserContext, if process-per-site is in use.
423 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { 437 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) {
424 int deleteCounter = 0; 438 int deleteCounter = 0;
425 TestBrowsingInstance* browsing_instance = 439 TestBrowsingInstance* browsing_instance =
426 new TestBrowsingInstance(NULL, &deleteCounter); 440 new TestBrowsingInstance(NULL, &deleteCounter);
427 browsing_instance->use_process_per_site = true; 441 browsing_instance->SetProcessPerSite(true);
428 442
429 const GURL url_a1("http://www.google.com/1.html"); 443 const GURL url_a1("http://www.google.com/1.html");
430 scoped_refptr<SiteInstance> site_instance_a1( 444 scoped_refptr<SiteInstance> site_instance_a1(
431 browsing_instance->GetSiteInstanceForURL(url_a1)); 445 browsing_instance->GetSiteInstanceForURL(url_a1));
432 EXPECT_TRUE(site_instance_a1.get() != NULL); 446 EXPECT_TRUE(site_instance_a1.get() != NULL);
433 447
434 // A separate site should create a separate SiteInstance. 448 // A separate site should create a separate SiteInstance.
435 const GURL url_b1("http://www.yahoo.com/"); 449 const GURL url_b1("http://www.yahoo.com/");
436 scoped_refptr<SiteInstance> site_instance_b1( 450 scoped_refptr<SiteInstance> site_instance_b1(
437 browsing_instance->GetSiteInstanceForURL(url_b1)); 451 browsing_instance->GetSiteInstanceForURL(url_b1));
(...skipping 11 matching lines...) Expand all
449 EXPECT_EQ(site_instance_a1.get(), 463 EXPECT_EQ(site_instance_a1.get(),
450 site_instance_a1->GetRelatedSiteInstance(url_a2)); 464 site_instance_a1->GetRelatedSiteInstance(url_a2));
451 465
452 // A visit to the original site in a new BrowsingInstance (same browser 466 // A visit to the original site in a new BrowsingInstance (same browser
453 // context) should also return the same SiteInstance. 467 // context) should also return the same SiteInstance.
454 // This BrowsingInstance doesn't get its own SiteInstance within the test, so 468 // This BrowsingInstance doesn't get its own SiteInstance within the test, so
455 // it won't be deleted by its children. Thus, we'll keep a ref count to it 469 // it won't be deleted by its children. Thus, we'll keep a ref count to it
456 // to make sure it gets deleted. 470 // to make sure it gets deleted.
457 scoped_refptr<TestBrowsingInstance> browsing_instance2( 471 scoped_refptr<TestBrowsingInstance> browsing_instance2(
458 new TestBrowsingInstance(NULL, &deleteCounter)); 472 new TestBrowsingInstance(NULL, &deleteCounter));
459 browsing_instance2->use_process_per_site = true; 473 browsing_instance2->SetProcessPerSite(true);
460 EXPECT_EQ(site_instance_a1.get(), 474 EXPECT_EQ(site_instance_a1.get(),
461 browsing_instance2->GetSiteInstanceForURL(url_a2)); 475 browsing_instance2->GetSiteInstanceForURL(url_a2));
462 476
463 // A visit to the original site in a new BrowsingInstance (different browser 477 // A visit to the original site in a new BrowsingInstance (different browser
464 // context) should return a different SiteInstance. 478 // context) should return a different SiteInstance.
465 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 479 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
466 TestBrowsingInstance* browsing_instance3 = 480 TestBrowsingInstance* browsing_instance3 =
467 new TestBrowsingInstance(browser_context.get(), &deleteCounter); 481 new TestBrowsingInstance(browser_context.get(), &deleteCounter);
468 browsing_instance3->use_process_per_site = true; 482 browsing_instance3->SetProcessPerSite(true);
469 // Ensure the new SiteInstance is ref counted so that it gets deleted. 483 // Ensure the new SiteInstance is ref counted so that it gets deleted.
470 scoped_refptr<SiteInstance> site_instance_a2_3( 484 scoped_refptr<SiteInstance> site_instance_a2_3(
471 browsing_instance3->GetSiteInstanceForURL(url_a2)); 485 browsing_instance3->GetSiteInstanceForURL(url_a2));
472 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); 486 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get());
473 487
474 // Should be able to see that we do have SiteInstances. 488 // Should be able to see that we do have SiteInstances.
475 EXPECT_TRUE(browsing_instance->HasSiteInstance( 489 EXPECT_TRUE(browsing_instance->HasSiteInstance(
476 GURL("http://mail.google.com"))); // visited before 490 GURL("http://mail.google.com"))); // visited before
477 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 491 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
478 GURL("http://mail.google.com"))); // visited before 492 GURL("http://mail.google.com"))); // visited before
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 // Make sure none of differing privilege processes are mixed. 553 // Make sure none of differing privilege processes are mixed.
540 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); 554 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess());
541 555
542 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) { 556 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) {
543 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); 557 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]);
544 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); 558 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]);
545 } 559 }
546 560
547 STLDeleteContainerPointers(hosts.begin(), hosts.end()); 561 STLDeleteContainerPointers(hosts.begin(), hosts.end());
548 } 562 }
563
564 // Test to ensure that javascript URLs always run in the same process.
Charlie Reis 2012/01/13 18:37:46 Please update this comment as well.
nasko 2012/01/13 20:54:49 Done.
565 TEST_F(SiteInstanceTest, HasWrongProcessForURL) {
566
567 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
568 scoped_ptr<content::RenderProcessHost> host;
569 scoped_refptr<SiteInstance> instance(
570 SiteInstance::CreateSiteInstance(browser_context.get()));
571
572 EXPECT_FALSE(instance->has_site());
573 EXPECT_TRUE(instance->site().is_empty());
574
575 instance->SetSite(GURL("http://evernote.com/"));
576 EXPECT_TRUE(instance->has_site());
577
578 // Check prior to "assigning" a process to the instance, which is expected
579 // to return false due to not being attached to any process yet.
580 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com")));
581
582 host.reset(instance->GetProcess());
Charlie Reis 2012/01/13 18:37:46 Wow, I'm impressed if this works. Do you know if
nasko 2012/01/13 20:54:49 Yes, it indeed spawns a new process.
583 EXPECT_TRUE(host.get() != NULL);
584 EXPECT_TRUE(instance->HasProcess());
585
586 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com")));
587 EXPECT_FALSE(instance->HasWrongProcessForURL(
588 GURL("javascript:alert(document.location.href);")));
589
Charlie Reis 2012/01/13 18:37:46 Extra line can be deleted.
nasko 2012/01/13 20:54:49 Done.
590 }
OLDNEW
« content/browser/site_instance.h ('K') | « content/browser/site_instance.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698