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

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: Added extra test case in the unit test and cleaned up code style. 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) {
awong 2012/01/13 21:19:04 nit: %s/deleteCounter/delete_counter/g
nasko 2012/01/13 21:47:10 Done.
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 set_use_process_per_site(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 void SetHasProcess(bool process) {
Charlie Reis 2012/01/13 21:16:29 set_has_process, since it's just a setter function
nasko 2012/01/13 21:47:10 Function is not needed anymore, since I create a r
159 has_process_ = process;
160 }
161
154 private: 162 private:
155 TestSiteInstance(BrowsingInstance* browsing_instance, int* deleteCounter) 163 TestSiteInstance(BrowsingInstance* browsing_instance, int* deleteCounter)
156 : SiteInstance(browsing_instance), deleteCounter_(deleteCounter) {} 164 : SiteInstance(browsing_instance), delete_counter_(deleteCounter) {}
157 ~TestSiteInstance() { 165 virtual ~TestSiteInstance() {
158 (*deleteCounter_)++; 166 (*delete_counter_)++;
159 } 167 }
160 168
161 int* deleteCounter_; 169 int* delete_counter_;
170 bool has_process_;
162 }; 171 };
163 172
164 } // namespace 173 } // namespace
165 174
166 // Test to ensure no memory leaks for SiteInstance objects. 175 // Test to ensure no memory leaks for SiteInstance objects.
167 TEST_F(SiteInstanceTest, SiteInstanceDestructor) { 176 TEST_F(SiteInstanceTest, SiteInstanceDestructor) {
168 // The existence of these factories will cause TabContents to create our test 177 // The existence of these factories will cause TabContents to create our test
169 // one instead of the real one. 178 // one instead of the real one.
170 MockRenderProcessHostFactory rph_factory; 179 MockRenderProcessHostFactory rph_factory;
171 TestRenderViewHostFactory rvh_factory(&rph_factory); 180 TestRenderViewHostFactory rvh_factory(&rph_factory);
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 EXPECT_TRUE( 368 EXPECT_TRUE(
360 SiteInstance::IsSameWebSite(NULL, url_browser_specified, url_foo)); 369 SiteInstance::IsSameWebSite(NULL, url_browser_specified, url_foo));
361 } 370 }
362 371
363 // Test to ensure that there is only one SiteInstance per site in a given 372 // Test to ensure that there is only one SiteInstance per site in a given
364 // BrowsingInstance, when process-per-site is not in use. 373 // BrowsingInstance, when process-per-site is not in use.
365 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { 374 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) {
366 int deleteCounter = 0; 375 int deleteCounter = 0;
367 TestBrowsingInstance* browsing_instance = 376 TestBrowsingInstance* browsing_instance =
368 new TestBrowsingInstance(NULL, &deleteCounter); 377 new TestBrowsingInstance(NULL, &deleteCounter);
369 browsing_instance->use_process_per_site = false; 378 browsing_instance->set_use_process_per_site(false);
370 379
371 const GURL url_a1("http://www.google.com/1.html"); 380 const GURL url_a1("http://www.google.com/1.html");
372 scoped_refptr<SiteInstance> site_instance_a1( 381 scoped_refptr<SiteInstance> site_instance_a1(
373 browsing_instance->GetSiteInstanceForURL(url_a1)); 382 browsing_instance->GetSiteInstanceForURL(url_a1));
374 EXPECT_TRUE(site_instance_a1.get() != NULL); 383 EXPECT_TRUE(site_instance_a1.get() != NULL);
375 384
376 // A separate site should create a separate SiteInstance. 385 // A separate site should create a separate SiteInstance.
377 const GURL url_b1("http://www.yahoo.com/"); 386 const GURL url_b1("http://www.yahoo.com/");
378 scoped_refptr<SiteInstance> site_instance_b1( 387 scoped_refptr<SiteInstance> site_instance_b1(
379 browsing_instance->GetSiteInstanceForURL(url_b1)); 388 browsing_instance->GetSiteInstanceForURL(url_b1));
380 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); 389 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
381 390
382 // Getting the new SiteInstance from the BrowsingInstance and from another 391 // Getting the new SiteInstance from the BrowsingInstance and from another
383 // SiteInstance in the BrowsingInstance should give the same result. 392 // SiteInstance in the BrowsingInstance should give the same result.
384 EXPECT_EQ(site_instance_b1.get(), 393 EXPECT_EQ(site_instance_b1.get(),
385 site_instance_a1->GetRelatedSiteInstance(url_b1)); 394 site_instance_a1->GetRelatedSiteInstance(url_b1));
386 395
387 // A second visit to the original site should return the same SiteInstance. 396 // A second visit to the original site should return the same SiteInstance.
388 const GURL url_a2("http://www.google.com/2.html"); 397 const GURL url_a2("http://www.google.com/2.html");
389 EXPECT_EQ(site_instance_a1.get(), 398 EXPECT_EQ(site_instance_a1.get(),
390 browsing_instance->GetSiteInstanceForURL(url_a2)); 399 browsing_instance->GetSiteInstanceForURL(url_a2));
391 EXPECT_EQ(site_instance_a1.get(), 400 EXPECT_EQ(site_instance_a1.get(),
392 site_instance_a1->GetRelatedSiteInstance(url_a2)); 401 site_instance_a1->GetRelatedSiteInstance(url_a2));
393 402
394 // A visit to the original site in a new BrowsingInstance (same or different 403 // A visit to the original site in a new BrowsingInstance (same or different
395 // browser context) should return a different SiteInstance. 404 // browser context) should return a different SiteInstance.
396 TestBrowsingInstance* browsing_instance2 = 405 TestBrowsingInstance* browsing_instance2 =
397 new TestBrowsingInstance(NULL, &deleteCounter); 406 new TestBrowsingInstance(NULL, &deleteCounter);
398 browsing_instance2->use_process_per_site = false; 407 browsing_instance2->set_use_process_per_site(false);
399 // Ensure the new SiteInstance is ref counted so that it gets deleted. 408 // Ensure the new SiteInstance is ref counted so that it gets deleted.
400 scoped_refptr<SiteInstance> site_instance_a2_2( 409 scoped_refptr<SiteInstance> site_instance_a2_2(
401 browsing_instance2->GetSiteInstanceForURL(url_a2)); 410 browsing_instance2->GetSiteInstanceForURL(url_a2));
402 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); 411 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get());
403 412
404 // Should be able to see that we do have SiteInstances. 413 // Should be able to see that we do have SiteInstances.
405 EXPECT_TRUE(browsing_instance->HasSiteInstance( 414 EXPECT_TRUE(browsing_instance->HasSiteInstance(
406 GURL("http://mail.google.com"))); 415 GURL("http://mail.google.com")));
407 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 416 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
408 GURL("http://mail.google.com"))); 417 GURL("http://mail.google.com")));
409 EXPECT_TRUE(browsing_instance->HasSiteInstance( 418 EXPECT_TRUE(browsing_instance->HasSiteInstance(
410 GURL("http://mail.yahoo.com"))); 419 GURL("http://mail.yahoo.com")));
411 420
412 // Should be able to see that we don't have SiteInstances. 421 // Should be able to see that we don't have SiteInstances.
413 EXPECT_FALSE(browsing_instance->HasSiteInstance( 422 EXPECT_FALSE(browsing_instance->HasSiteInstance(
414 GURL("https://www.google.com"))); 423 GURL("https://www.google.com")));
415 EXPECT_FALSE(browsing_instance2->HasSiteInstance( 424 EXPECT_FALSE(browsing_instance2->HasSiteInstance(
416 GURL("http://www.yahoo.com"))); 425 GURL("http://www.yahoo.com")));
417 426
418 // browsing_instances will be deleted when their SiteInstances are deleted 427 // browsing_instances will be deleted when their SiteInstances are deleted
419 } 428 }
420 429
421 // Test to ensure that there is only one SiteInstance per site for an entire 430 // Test to ensure that there is only one SiteInstance per site for an entire
422 // BrowserContext, if process-per-site is in use. 431 // BrowserContext, if process-per-site is in use.
423 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { 432 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) {
424 int deleteCounter = 0; 433 int deleteCounter = 0;
425 TestBrowsingInstance* browsing_instance = 434 TestBrowsingInstance* browsing_instance =
426 new TestBrowsingInstance(NULL, &deleteCounter); 435 new TestBrowsingInstance(NULL, &deleteCounter);
427 browsing_instance->use_process_per_site = true; 436 browsing_instance->set_use_process_per_site(true);
428 437
429 const GURL url_a1("http://www.google.com/1.html"); 438 const GURL url_a1("http://www.google.com/1.html");
430 scoped_refptr<SiteInstance> site_instance_a1( 439 scoped_refptr<SiteInstance> site_instance_a1(
431 browsing_instance->GetSiteInstanceForURL(url_a1)); 440 browsing_instance->GetSiteInstanceForURL(url_a1));
432 EXPECT_TRUE(site_instance_a1.get() != NULL); 441 EXPECT_TRUE(site_instance_a1.get() != NULL);
433 442
434 // A separate site should create a separate SiteInstance. 443 // A separate site should create a separate SiteInstance.
435 const GURL url_b1("http://www.yahoo.com/"); 444 const GURL url_b1("http://www.yahoo.com/");
436 scoped_refptr<SiteInstance> site_instance_b1( 445 scoped_refptr<SiteInstance> site_instance_b1(
437 browsing_instance->GetSiteInstanceForURL(url_b1)); 446 browsing_instance->GetSiteInstanceForURL(url_b1));
(...skipping 11 matching lines...) Expand all
449 EXPECT_EQ(site_instance_a1.get(), 458 EXPECT_EQ(site_instance_a1.get(),
450 site_instance_a1->GetRelatedSiteInstance(url_a2)); 459 site_instance_a1->GetRelatedSiteInstance(url_a2));
451 460
452 // A visit to the original site in a new BrowsingInstance (same browser 461 // A visit to the original site in a new BrowsingInstance (same browser
453 // context) should also return the same SiteInstance. 462 // context) should also return the same SiteInstance.
454 // This BrowsingInstance doesn't get its own SiteInstance within the test, so 463 // 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 464 // it won't be deleted by its children. Thus, we'll keep a ref count to it
456 // to make sure it gets deleted. 465 // to make sure it gets deleted.
457 scoped_refptr<TestBrowsingInstance> browsing_instance2( 466 scoped_refptr<TestBrowsingInstance> browsing_instance2(
458 new TestBrowsingInstance(NULL, &deleteCounter)); 467 new TestBrowsingInstance(NULL, &deleteCounter));
459 browsing_instance2->use_process_per_site = true; 468 browsing_instance2->set_use_process_per_site(true);
460 EXPECT_EQ(site_instance_a1.get(), 469 EXPECT_EQ(site_instance_a1.get(),
461 browsing_instance2->GetSiteInstanceForURL(url_a2)); 470 browsing_instance2->GetSiteInstanceForURL(url_a2));
462 471
463 // A visit to the original site in a new BrowsingInstance (different browser 472 // A visit to the original site in a new BrowsingInstance (different browser
464 // context) should return a different SiteInstance. 473 // context) should return a different SiteInstance.
465 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 474 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
466 TestBrowsingInstance* browsing_instance3 = 475 TestBrowsingInstance* browsing_instance3 =
467 new TestBrowsingInstance(browser_context.get(), &deleteCounter); 476 new TestBrowsingInstance(browser_context.get(), &deleteCounter);
468 browsing_instance3->use_process_per_site = true; 477 browsing_instance3->set_use_process_per_site(true);
469 // Ensure the new SiteInstance is ref counted so that it gets deleted. 478 // Ensure the new SiteInstance is ref counted so that it gets deleted.
470 scoped_refptr<SiteInstance> site_instance_a2_3( 479 scoped_refptr<SiteInstance> site_instance_a2_3(
471 browsing_instance3->GetSiteInstanceForURL(url_a2)); 480 browsing_instance3->GetSiteInstanceForURL(url_a2));
472 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); 481 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get());
473 482
474 // Should be able to see that we do have SiteInstances. 483 // Should be able to see that we do have SiteInstances.
475 EXPECT_TRUE(browsing_instance->HasSiteInstance( 484 EXPECT_TRUE(browsing_instance->HasSiteInstance(
476 GURL("http://mail.google.com"))); // visited before 485 GURL("http://mail.google.com"))); // visited before
477 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 486 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
478 GURL("http://mail.google.com"))); // visited before 487 GURL("http://mail.google.com"))); // visited before
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 scoped_ptr<content::RenderProcessHost> extension_host( 531 scoped_ptr<content::RenderProcessHost> extension_host(
523 extension1_instance->GetProcess()); 532 extension1_instance->GetProcess());
524 EXPECT_EQ(extension1_instance->GetProcess(), 533 EXPECT_EQ(extension1_instance->GetProcess(),
525 extension2_instance->GetProcess()); 534 extension2_instance->GetProcess());
526 535
527 // Create some WebUI instances and make sure they share a process. 536 // Create some WebUI instances and make sure they share a process.
528 scoped_refptr<SiteInstance> webui1_instance(CreateSiteInstance(&rph_factory, 537 scoped_refptr<SiteInstance> webui1_instance(CreateSiteInstance(&rph_factory,
529 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); 538 GURL(chrome::kChromeUIScheme + std::string("://newtab"))));
530 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID()); 539 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID());
531 540
532 scoped_refptr<SiteInstance> webui2_instance( CreateSiteInstance(&rph_factory, 541 scoped_refptr<SiteInstance> webui2_instance(CreateSiteInstance(&rph_factory,
533 GURL(chrome::kChromeUIScheme + std::string("://history")))); 542 GURL(chrome::kChromeUIScheme + std::string("://history"))));
534 543
535 scoped_ptr<content::RenderProcessHost> dom_host( 544 scoped_ptr<content::RenderProcessHost> dom_host(
536 webui1_instance->GetProcess()); 545 webui1_instance->GetProcess());
537 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); 546 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess());
538 547
539 // Make sure none of differing privilege processes are mixed. 548 // Make sure none of differing privilege processes are mixed.
540 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); 549 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess());
541 550
542 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) { 551 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) {
543 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); 552 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]);
544 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); 553 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]);
545 } 554 }
546 555
547 STLDeleteContainerPointers(hosts.begin(), hosts.end()); 556 STLDeleteContainerPointers(hosts.begin(), hosts.end());
548 } 557 }
558
559 // Test to ensure that HasWrongProcessForURL behaves properly for different
560 // types of URLs.
561 TEST_F(SiteInstanceTest, HasWrongProcessForURL) {
562 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
563 scoped_ptr<content::RenderProcessHost> host;
564 scoped_refptr<SiteInstance> instance(
565 SiteInstance::CreateSiteInstance(browser_context.get()));
566
567 EXPECT_FALSE(instance->has_site());
568 EXPECT_TRUE(instance->site().is_empty());
569
570 instance->SetSite(GURL("http://evernote.com/"));
571 EXPECT_TRUE(instance->has_site());
572
573 // Check prior to "assigning" a process to the instance, which is expected
574 // to return false due to not being attached to any process yet.
575 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com")));
576
577 // The call to GetProcess actually creates a new real process, which works
578 // fine, but might be a cause for problems in different contexts.
579 host.reset(instance->GetProcess());
580 EXPECT_TRUE(host.get() != NULL);
awong 2012/01/13 21:19:04 EXPECT_TRUE(host.get()) is jsut as common (maybe m
nasko 2012/01/13 21:47:10 I was trying to be consistent with the rest of the
581 EXPECT_TRUE(instance->HasProcess());
582
583 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com")));
584 EXPECT_FALSE(instance->HasWrongProcessForURL(
585 GURL("javascript:alert(document.location.href);")));
586
587 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings")));
588 }
OLDNEW
« content/browser/site_instance.cc ('K') | « content/browser/site_instance.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698