| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/stl_util-inl.h" | |
| 6 #include "base/string16.h" | |
| 7 #include "chrome/browser/browsing_instance.h" | |
| 8 #include "chrome/browser/child_process_security_policy.h" | |
| 9 #include "chrome/browser/renderer_host/browser_render_process_host.h" | |
| 10 #include "chrome/browser/renderer_host/site_instance.h" | |
| 11 #include "chrome/browser/renderer_host/render_view_host.h" | |
| 12 #include "chrome/browser/renderer_host/test/test_render_view_host.h" | |
| 13 #include "chrome/browser/tab_contents/navigation_entry.h" | |
| 14 #include "chrome/browser/tab_contents/tab_contents.h" | |
| 15 #include "chrome/common/chrome_constants.h" | |
| 16 #include "chrome/common/url_constants.h" | |
| 17 #include "chrome/common/render_messages.h" | |
| 18 #include "chrome/test/testing_profile.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 class SiteInstanceTest : public testing::Test { | |
| 22 private: | |
| 23 MessageLoopForUI message_loop_; | |
| 24 }; | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 class TestBrowsingInstance : public BrowsingInstance { | |
| 29 public: | |
| 30 TestBrowsingInstance(Profile* profile, int* deleteCounter) | |
| 31 : BrowsingInstance(profile), | |
| 32 use_process_per_site(false), | |
| 33 deleteCounter_(deleteCounter) { | |
| 34 } | |
| 35 | |
| 36 // Overrides BrowsingInstance::ShouldUseProcessPerSite so that we can test | |
| 37 // both alternatives without using command-line switches. | |
| 38 bool ShouldUseProcessPerSite(const GURL& url) { | |
| 39 return use_process_per_site; | |
| 40 } | |
| 41 | |
| 42 // Set by individual tests. | |
| 43 bool use_process_per_site; | |
| 44 | |
| 45 private: | |
| 46 ~TestBrowsingInstance() { | |
| 47 (*deleteCounter_)++; | |
| 48 } | |
| 49 | |
| 50 int* deleteCounter_; | |
| 51 }; | |
| 52 | |
| 53 | |
| 54 class TestSiteInstance : public SiteInstance { | |
| 55 public: | |
| 56 static TestSiteInstance* CreateTestSiteInstance(Profile* profile, | |
| 57 int* siteDeleteCounter, | |
| 58 int* browsingDeleteCounter) { | |
| 59 TestBrowsingInstance* browsing_instance = | |
| 60 new TestBrowsingInstance(profile, browsingDeleteCounter); | |
| 61 return new TestSiteInstance(browsing_instance, siteDeleteCounter); | |
| 62 } | |
| 63 | |
| 64 private: | |
| 65 TestSiteInstance(BrowsingInstance* browsing_instance, int* deleteCounter) | |
| 66 : SiteInstance(browsing_instance), deleteCounter_(deleteCounter) {} | |
| 67 ~TestSiteInstance() { | |
| 68 (*deleteCounter_)++; | |
| 69 } | |
| 70 | |
| 71 int* deleteCounter_; | |
| 72 }; | |
| 73 | |
| 74 } // namespace | |
| 75 | |
| 76 // Test to ensure no memory leaks for SiteInstance objects. | |
| 77 TEST_F(SiteInstanceTest, SiteInstanceDestructor) { | |
| 78 // The existence of these factories will cause TabContents to create our test | |
| 79 // one instead of the real one. | |
| 80 MockRenderProcessHostFactory rph_factory; | |
| 81 TestRenderViewHostFactory rvh_factory(&rph_factory); | |
| 82 int siteDeleteCounter = 0; | |
| 83 int browsingDeleteCounter = 0; | |
| 84 const GURL url("test:foo"); | |
| 85 | |
| 86 // Ensure that instances are deleted when their NavigationEntries are gone. | |
| 87 TestSiteInstance* instance = | |
| 88 TestSiteInstance::CreateTestSiteInstance(NULL, &siteDeleteCounter, | |
| 89 &browsingDeleteCounter); | |
| 90 EXPECT_EQ(0, siteDeleteCounter); | |
| 91 | |
| 92 NavigationEntry* e1 = new NavigationEntry(instance, 0, url, GURL(), | |
| 93 string16(), | |
| 94 PageTransition::LINK); | |
| 95 | |
| 96 // Redundantly setting e1's SiteInstance shouldn't affect the ref count. | |
| 97 e1->set_site_instance(instance); | |
| 98 EXPECT_EQ(0, siteDeleteCounter); | |
| 99 | |
| 100 // Add a second reference | |
| 101 NavigationEntry* e2 = new NavigationEntry(instance, 0, url, | |
| 102 GURL(), string16(), | |
| 103 PageTransition::LINK); | |
| 104 | |
| 105 // Now delete both entries and be sure the SiteInstance goes away. | |
| 106 delete e1; | |
| 107 EXPECT_EQ(0, siteDeleteCounter); | |
| 108 EXPECT_EQ(0, browsingDeleteCounter); | |
| 109 delete e2; | |
| 110 EXPECT_EQ(1, siteDeleteCounter); | |
| 111 // instance is now deleted | |
| 112 EXPECT_EQ(1, browsingDeleteCounter); | |
| 113 // browsing_instance is now deleted | |
| 114 | |
| 115 // Ensure that instances are deleted when their RenderViewHosts are gone. | |
| 116 scoped_ptr<TestingProfile> profile(new TestingProfile()); | |
| 117 instance = | |
| 118 TestSiteInstance::CreateTestSiteInstance(profile.get(), | |
| 119 &siteDeleteCounter, | |
| 120 &browsingDeleteCounter); | |
| 121 { | |
| 122 TabContents contents(profile.get(), instance, MSG_ROUTING_NONE, NULL, NULL); | |
| 123 EXPECT_EQ(1, siteDeleteCounter); | |
| 124 EXPECT_EQ(1, browsingDeleteCounter); | |
| 125 } | |
| 126 | |
| 127 // Make sure that we flush any messages related to the above TabContents | |
| 128 // destruction. | |
| 129 MessageLoop::current()->RunAllPending(); | |
| 130 | |
| 131 EXPECT_EQ(2, siteDeleteCounter); | |
| 132 EXPECT_EQ(2, browsingDeleteCounter); | |
| 133 // contents is now deleted, along with instance and browsing_instance | |
| 134 } | |
| 135 | |
| 136 // Test that NavigationEntries with SiteInstances can be cloned, but that their | |
| 137 // SiteInstances can be changed afterwards. Also tests that the ref counts are | |
| 138 // updated properly after the change. | |
| 139 TEST_F(SiteInstanceTest, CloneNavigationEntry) { | |
| 140 int siteDeleteCounter1 = 0; | |
| 141 int siteDeleteCounter2 = 0; | |
| 142 int browsingDeleteCounter = 0; | |
| 143 const GURL url("test:foo"); | |
| 144 | |
| 145 SiteInstance* instance1 = | |
| 146 TestSiteInstance::CreateTestSiteInstance(NULL, &siteDeleteCounter1, | |
| 147 &browsingDeleteCounter); | |
| 148 SiteInstance* instance2 = | |
| 149 TestSiteInstance::CreateTestSiteInstance(NULL, &siteDeleteCounter2, | |
| 150 &browsingDeleteCounter); | |
| 151 | |
| 152 NavigationEntry* e1 = new NavigationEntry(instance1, 0, url, GURL(), | |
| 153 string16(), | |
| 154 PageTransition::LINK); | |
| 155 // Clone the entry | |
| 156 NavigationEntry* e2 = new NavigationEntry(*e1); | |
| 157 | |
| 158 // Should be able to change the SiteInstance of the cloned entry. | |
| 159 e2->set_site_instance(instance2); | |
| 160 | |
| 161 // The first SiteInstance should go away after deleting e1, since e2 should | |
| 162 // no longer be referencing it. | |
| 163 delete e1; | |
| 164 EXPECT_EQ(1, siteDeleteCounter1); | |
| 165 EXPECT_EQ(0, siteDeleteCounter2); | |
| 166 | |
| 167 // The second SiteInstance should go away after deleting e2. | |
| 168 delete e2; | |
| 169 EXPECT_EQ(1, siteDeleteCounter1); | |
| 170 EXPECT_EQ(1, siteDeleteCounter2); | |
| 171 | |
| 172 // Both BrowsingInstances are also now deleted | |
| 173 EXPECT_EQ(2, browsingDeleteCounter); | |
| 174 } | |
| 175 | |
| 176 // Test to ensure UpdateMaxPageID is working properly. | |
| 177 TEST_F(SiteInstanceTest, UpdateMaxPageID) { | |
| 178 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL)); | |
| 179 EXPECT_EQ(-1, instance->max_page_id()); | |
| 180 | |
| 181 // Make sure max_page_id_ is monotonically increasing. | |
| 182 instance->UpdateMaxPageID(3); | |
| 183 instance->UpdateMaxPageID(1); | |
| 184 EXPECT_EQ(3, instance->max_page_id()); | |
| 185 } | |
| 186 | |
| 187 // Test to ensure GetProcess returns and creates processes correctly. | |
| 188 TEST_F(SiteInstanceTest, GetProcess) { | |
| 189 // Ensure that GetProcess returns a process. | |
| 190 scoped_ptr<TestingProfile> profile(new TestingProfile()); | |
| 191 scoped_ptr<RenderProcessHost> host1; | |
| 192 scoped_refptr<SiteInstance> instance( | |
| 193 SiteInstance::CreateSiteInstance(profile.get())); | |
| 194 host1.reset(instance->GetProcess()); | |
| 195 EXPECT_TRUE(host1.get() != NULL); | |
| 196 | |
| 197 // Ensure that GetProcess creates a new process. | |
| 198 scoped_refptr<SiteInstance> instance2( | |
| 199 SiteInstance::CreateSiteInstance(profile.get())); | |
| 200 scoped_ptr<RenderProcessHost> host2(instance2->GetProcess()); | |
| 201 EXPECT_TRUE(host2.get() != NULL); | |
| 202 EXPECT_NE(host1.get(), host2.get()); | |
| 203 } | |
| 204 | |
| 205 // Test to ensure SetSite and site() work properly. | |
| 206 TEST_F(SiteInstanceTest, SetSite) { | |
| 207 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL)); | |
| 208 EXPECT_FALSE(instance->has_site()); | |
| 209 EXPECT_TRUE(instance->site().is_empty()); | |
| 210 | |
| 211 instance->SetSite(GURL("http://www.google.com/index.html")); | |
| 212 EXPECT_EQ(GURL("http://google.com"), instance->site()); | |
| 213 | |
| 214 EXPECT_TRUE(instance->has_site()); | |
| 215 } | |
| 216 | |
| 217 // Test to ensure GetSiteForURL properly returns sites for URLs. | |
| 218 TEST_F(SiteInstanceTest, GetSiteForURL) { | |
| 219 // Pages are irrelevant. | |
| 220 GURL test_url = GURL("http://www.google.com/index.html"); | |
| 221 EXPECT_EQ(GURL("http://google.com"), | |
| 222 SiteInstance::GetSiteForURL(NULL, test_url)); | |
| 223 | |
| 224 // Ports are irrlevant. | |
| 225 test_url = GURL("https://www.google.com:8080"); | |
| 226 EXPECT_EQ(GURL("https://google.com"), | |
| 227 SiteInstance::GetSiteForURL(NULL, test_url)); | |
| 228 | |
| 229 // Javascript URLs have no site. | |
| 230 test_url = GURL("javascript:foo();"); | |
| 231 EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); | |
| 232 | |
| 233 test_url = GURL("http://foo/a.html"); | |
| 234 EXPECT_EQ(GURL("http://foo"), SiteInstance::GetSiteForURL(NULL, test_url)); | |
| 235 | |
| 236 test_url = GURL("file:///C:/Downloads/"); | |
| 237 EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); | |
| 238 | |
| 239 // TODO(creis): Do we want to special case file URLs to ensure they have | |
| 240 // either no site or a special "file://" site? We currently return | |
| 241 // "file://home/" as the site, which seems broken. | |
| 242 // test_url = GURL("file://home/"); | |
| 243 // EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); | |
| 244 } | |
| 245 | |
| 246 // Test of distinguishing URLs from different sites. Most of this logic is | |
| 247 // tested in RegistryControlledDomainTest. This test focuses on URLs with | |
| 248 // different schemes or ports. | |
| 249 TEST_F(SiteInstanceTest, IsSameWebSite) { | |
| 250 GURL url_foo = GURL("http://foo/a.html"); | |
| 251 GURL url_foo2 = GURL("http://foo/b.html"); | |
| 252 GURL url_foo_https = GURL("https://foo/a.html"); | |
| 253 GURL url_foo_port = GURL("http://foo:8080/a.html"); | |
| 254 GURL url_javascript = GURL("javascript:alert(1);"); | |
| 255 GURL url_crash = GURL(chrome::kAboutCrashURL); | |
| 256 GURL url_hang = GURL(chrome::kAboutHangURL); | |
| 257 GURL url_shorthang = GURL(chrome::kAboutShorthangURL); | |
| 258 | |
| 259 // Same scheme and port -> same site. | |
| 260 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo2)); | |
| 261 | |
| 262 // Different scheme -> different site. | |
| 263 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_https)); | |
| 264 | |
| 265 // Different port -> same site. | |
| 266 // (Changes to document.domain make renderer ignore the port.) | |
| 267 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_port)); | |
| 268 | |
| 269 // JavaScript links should be considered same site for anything. | |
| 270 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo)); | |
| 271 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_https)); | |
| 272 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_port)); | |
| 273 | |
| 274 // The crash/hang URLs should also be treated as same site. (Bug 1143809.) | |
| 275 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_crash, url_foo)); | |
| 276 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_hang, url_foo)); | |
| 277 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_shorthang, url_foo)); | |
| 278 } | |
| 279 | |
| 280 // Test to ensure that there is only one SiteInstance per site in a given | |
| 281 // BrowsingInstance, when process-per-site is not in use. | |
| 282 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { | |
| 283 int deleteCounter = 0; | |
| 284 TestBrowsingInstance* browsing_instance = | |
| 285 new TestBrowsingInstance(NULL, &deleteCounter); | |
| 286 browsing_instance->use_process_per_site = false; | |
| 287 | |
| 288 const GURL url_a1("http://www.google.com/1.html"); | |
| 289 scoped_refptr<SiteInstance> site_instance_a1( | |
| 290 browsing_instance->GetSiteInstanceForURL(url_a1)); | |
| 291 EXPECT_TRUE(site_instance_a1.get() != NULL); | |
| 292 | |
| 293 // A separate site should create a separate SiteInstance. | |
| 294 const GURL url_b1("http://www.yahoo.com/"); | |
| 295 scoped_refptr<SiteInstance> site_instance_b1( | |
| 296 browsing_instance->GetSiteInstanceForURL(url_b1)); | |
| 297 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); | |
| 298 | |
| 299 // Getting the new SiteInstance from the BrowsingInstance and from another | |
| 300 // SiteInstance in the BrowsingInstance should give the same result. | |
| 301 EXPECT_EQ(site_instance_b1.get(), | |
| 302 site_instance_a1->GetRelatedSiteInstance(url_b1)); | |
| 303 | |
| 304 // A second visit to the original site should return the same SiteInstance. | |
| 305 const GURL url_a2("http://www.google.com/2.html"); | |
| 306 EXPECT_EQ(site_instance_a1.get(), | |
| 307 browsing_instance->GetSiteInstanceForURL(url_a2)); | |
| 308 EXPECT_EQ(site_instance_a1.get(), | |
| 309 site_instance_a1->GetRelatedSiteInstance(url_a2)); | |
| 310 | |
| 311 // A visit to the original site in a new BrowsingInstance (same or different | |
| 312 // profile) should return a different SiteInstance. | |
| 313 TestBrowsingInstance* browsing_instance2 = | |
| 314 new TestBrowsingInstance(NULL, &deleteCounter); | |
| 315 browsing_instance2->use_process_per_site = false; | |
| 316 // Ensure the new SiteInstance is ref counted so that it gets deleted. | |
| 317 scoped_refptr<SiteInstance> site_instance_a2_2( | |
| 318 browsing_instance2->GetSiteInstanceForURL(url_a2)); | |
| 319 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); | |
| 320 | |
| 321 // Should be able to see that we do have SiteInstances. | |
| 322 EXPECT_TRUE(browsing_instance->HasSiteInstance( | |
| 323 GURL("http://mail.google.com"))); | |
| 324 EXPECT_TRUE(browsing_instance2->HasSiteInstance( | |
| 325 GURL("http://mail.google.com"))); | |
| 326 EXPECT_TRUE(browsing_instance->HasSiteInstance( | |
| 327 GURL("http://mail.yahoo.com"))); | |
| 328 | |
| 329 // Should be able to see that we don't have SiteInstances. | |
| 330 EXPECT_FALSE(browsing_instance->HasSiteInstance( | |
| 331 GURL("https://www.google.com"))); | |
| 332 EXPECT_FALSE(browsing_instance2->HasSiteInstance( | |
| 333 GURL("http://www.yahoo.com"))); | |
| 334 | |
| 335 // browsing_instances will be deleted when their SiteInstances are deleted | |
| 336 } | |
| 337 | |
| 338 // Test to ensure that there is only one SiteInstance per site for an entire | |
| 339 // Profile, if process-per-site is in use. | |
| 340 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInProfile) { | |
| 341 int deleteCounter = 0; | |
| 342 TestBrowsingInstance* browsing_instance = | |
| 343 new TestBrowsingInstance(NULL, &deleteCounter); | |
| 344 browsing_instance->use_process_per_site = true; | |
| 345 | |
| 346 const GURL url_a1("http://www.google.com/1.html"); | |
| 347 scoped_refptr<SiteInstance> site_instance_a1( | |
| 348 browsing_instance->GetSiteInstanceForURL(url_a1)); | |
| 349 EXPECT_TRUE(site_instance_a1.get() != NULL); | |
| 350 | |
| 351 // A separate site should create a separate SiteInstance. | |
| 352 const GURL url_b1("http://www.yahoo.com/"); | |
| 353 scoped_refptr<SiteInstance> site_instance_b1( | |
| 354 browsing_instance->GetSiteInstanceForURL(url_b1)); | |
| 355 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); | |
| 356 | |
| 357 // Getting the new SiteInstance from the BrowsingInstance and from another | |
| 358 // SiteInstance in the BrowsingInstance should give the same result. | |
| 359 EXPECT_EQ(site_instance_b1.get(), | |
| 360 site_instance_a1->GetRelatedSiteInstance(url_b1)); | |
| 361 | |
| 362 // A second visit to the original site should return the same SiteInstance. | |
| 363 const GURL url_a2("http://www.google.com/2.html"); | |
| 364 EXPECT_EQ(site_instance_a1.get(), | |
| 365 browsing_instance->GetSiteInstanceForURL(url_a2)); | |
| 366 EXPECT_EQ(site_instance_a1.get(), | |
| 367 site_instance_a1->GetRelatedSiteInstance(url_a2)); | |
| 368 | |
| 369 // A visit to the original site in a new BrowsingInstance (same profile) | |
| 370 // should also return the same SiteInstance. | |
| 371 // This BrowsingInstance doesn't get its own SiteInstance within the test, so | |
| 372 // it won't be deleted by its children. Thus, we'll keep a ref count to it | |
| 373 // to make sure it gets deleted. | |
| 374 scoped_refptr<TestBrowsingInstance> browsing_instance2( | |
| 375 new TestBrowsingInstance(NULL, &deleteCounter)); | |
| 376 browsing_instance2->use_process_per_site = true; | |
| 377 EXPECT_EQ(site_instance_a1.get(), | |
| 378 browsing_instance2->GetSiteInstanceForURL(url_a2)); | |
| 379 | |
| 380 // A visit to the original site in a new BrowsingInstance (different profile) | |
| 381 // should return a different SiteInstance. | |
| 382 scoped_ptr<TestingProfile> profile(new TestingProfile()); | |
| 383 TestBrowsingInstance* browsing_instance3 = | |
| 384 new TestBrowsingInstance(profile.get(), &deleteCounter); | |
| 385 browsing_instance3->use_process_per_site = true; | |
| 386 // Ensure the new SiteInstance is ref counted so that it gets deleted. | |
| 387 scoped_refptr<SiteInstance> site_instance_a2_3( | |
| 388 browsing_instance3->GetSiteInstanceForURL(url_a2)); | |
| 389 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); | |
| 390 | |
| 391 // Should be able to see that we do have SiteInstances. | |
| 392 EXPECT_TRUE(browsing_instance->HasSiteInstance( | |
| 393 GURL("http://mail.google.com"))); // visited before | |
| 394 EXPECT_TRUE(browsing_instance2->HasSiteInstance( | |
| 395 GURL("http://mail.google.com"))); // visited before | |
| 396 EXPECT_TRUE(browsing_instance->HasSiteInstance( | |
| 397 GURL("http://mail.yahoo.com"))); // visited before | |
| 398 EXPECT_TRUE(browsing_instance2->HasSiteInstance( | |
| 399 GURL("http://www.yahoo.com"))); // different BI, but same profile | |
| 400 | |
| 401 // Should be able to see that we don't have SiteInstances. | |
| 402 EXPECT_FALSE(browsing_instance->HasSiteInstance( | |
| 403 GURL("https://www.google.com"))); // not visited before | |
| 404 EXPECT_FALSE(browsing_instance3->HasSiteInstance( | |
| 405 GURL("http://www.yahoo.com"))); // different BI, different profile | |
| 406 | |
| 407 // browsing_instances will be deleted when their SiteInstances are deleted | |
| 408 } | |
| 409 | |
| 410 static SiteInstance* CreateSiteInstance(RenderProcessHostFactory* factory, | |
| 411 const GURL& url) { | |
| 412 SiteInstance* instance = SiteInstance::CreateSiteInstanceForURL(NULL, url); | |
| 413 instance->set_render_process_host_factory(factory); | |
| 414 return instance; | |
| 415 } | |
| 416 | |
| 417 // Test to ensure that pages that require certain privileges are grouped | |
| 418 // in processes with similar pages. | |
| 419 TEST_F(SiteInstanceTest, ProcessSharingByType) { | |
| 420 MockRenderProcessHostFactory rph_factory; | |
| 421 ChildProcessSecurityPolicy* policy = | |
| 422 ChildProcessSecurityPolicy::GetInstance(); | |
| 423 | |
| 424 // Make a bunch of mock renderers so that we hit the limit. | |
| 425 std::vector<MockRenderProcessHost*> hosts; | |
| 426 for (size_t i = 0; i < chrome::kMaxRendererProcessCount; ++i) | |
| 427 hosts.push_back(new MockRenderProcessHost(NULL)); | |
| 428 | |
| 429 // Create some extension instances and make sure they share a process. | |
| 430 scoped_refptr<SiteInstance> extension1_instance( | |
| 431 CreateSiteInstance(&rph_factory, GURL("chrome-extension://foo/bar"))); | |
| 432 policy->GrantExtensionBindings(extension1_instance->GetProcess()->id()); | |
| 433 | |
| 434 scoped_refptr<SiteInstance> extension2_instance( | |
| 435 CreateSiteInstance(&rph_factory, GURL("chrome-extension://baz/bar"))); | |
| 436 | |
| 437 scoped_ptr<RenderProcessHost> extension_host( | |
| 438 extension1_instance->GetProcess()); | |
| 439 EXPECT_EQ(extension1_instance->GetProcess(), | |
| 440 extension2_instance->GetProcess()); | |
| 441 | |
| 442 // Create some WebUI instances and make sure they share a process. | |
| 443 scoped_refptr<SiteInstance> dom1_instance( | |
| 444 CreateSiteInstance(&rph_factory, GURL("chrome://newtab"))); | |
| 445 policy->GrantWebUIBindings(dom1_instance->GetProcess()->id()); | |
| 446 | |
| 447 scoped_refptr<SiteInstance> dom2_instance( | |
| 448 CreateSiteInstance(&rph_factory, GURL("chrome://history"))); | |
| 449 | |
| 450 scoped_ptr<RenderProcessHost> dom_host(dom1_instance->GetProcess()); | |
| 451 EXPECT_EQ(dom1_instance->GetProcess(), dom2_instance->GetProcess()); | |
| 452 | |
| 453 // Make sure none of differing privilege processes are mixed. | |
| 454 EXPECT_NE(extension1_instance->GetProcess(), dom1_instance->GetProcess()); | |
| 455 | |
| 456 for (size_t i = 0; i < chrome::kMaxRendererProcessCount; ++i) { | |
| 457 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); | |
| 458 EXPECT_NE(dom1_instance->GetProcess(), hosts[i]); | |
| 459 } | |
| 460 | |
| 461 STLDeleteContainerPointers(hosts.begin(), hosts.end()); | |
| 462 } | |
| 463 | |
| 464 // Test to ensure that profiles that derive from each other share site | |
| 465 // information. | |
| 466 TEST_F(SiteInstanceTest, GetSiteInstanceMap) { | |
| 467 int deleteCounter = 0; | |
| 468 | |
| 469 scoped_ptr<Profile> p1(new TestingProfile()); | |
| 470 scoped_ptr<Profile> p2(new TestingProfile()); | |
| 471 scoped_ptr<Profile> p3(new DerivedTestingProfile(p1.get())); | |
| 472 | |
| 473 // In this test, instances 1 and 2 will be deleted automatically when the | |
| 474 // SiteInstance objects they return are deleted. However, instance 3 never | |
| 475 // returns any SitesIntance objects in this test, so will not be automatically | |
| 476 // deleted. It must be deleted manually. | |
| 477 TestBrowsingInstance* instance1(new TestBrowsingInstance(p1.get(), | |
| 478 &deleteCounter)); | |
| 479 TestBrowsingInstance* instance2(new TestBrowsingInstance(p2.get(), | |
| 480 &deleteCounter)); | |
| 481 scoped_refptr<TestBrowsingInstance> instance3( | |
| 482 new TestBrowsingInstance(p3.get(), &deleteCounter)); | |
| 483 | |
| 484 instance1->use_process_per_site = true; | |
| 485 instance2->use_process_per_site = true; | |
| 486 instance3->use_process_per_site = true; | |
| 487 | |
| 488 // The same profile with the same site. | |
| 489 scoped_refptr<SiteInstance> s1a(instance1->GetSiteInstanceForURL( | |
| 490 GURL("chrome-extension://baz/bar"))); | |
| 491 scoped_refptr<SiteInstance> s1b(instance1->GetSiteInstanceForURL( | |
| 492 GURL("chrome-extension://baz/bar"))); | |
| 493 EXPECT_EQ(s1a, s1b); | |
| 494 | |
| 495 // The same profile with different sites. | |
| 496 scoped_refptr<SiteInstance> s2a(instance1->GetSiteInstanceForURL( | |
| 497 GURL("chrome-extension://baz/bar"))); | |
| 498 scoped_refptr<SiteInstance> s2b(instance1->GetSiteInstanceForURL( | |
| 499 GURL("chrome-extension://foo/boo"))); | |
| 500 EXPECT_NE(s2a, s2b); | |
| 501 | |
| 502 // The different profiles with the same site. | |
| 503 scoped_refptr<SiteInstance> s3a(instance1->GetSiteInstanceForURL( | |
| 504 GURL("chrome-extension://baz/bar"))); | |
| 505 scoped_refptr<SiteInstance> s3b(instance2->GetSiteInstanceForURL( | |
| 506 GURL("chrome-extension://baz/bar"))); | |
| 507 EXPECT_NE(s3a, s3b); | |
| 508 | |
| 509 // The different profiles with different sites. | |
| 510 scoped_refptr<SiteInstance> s4a(instance1->GetSiteInstanceForURL( | |
| 511 GURL("chrome-extension://baz/bar"))); | |
| 512 scoped_refptr<SiteInstance> s4b(instance2->GetSiteInstanceForURL( | |
| 513 GURL("chrome-extension://foo/boo"))); | |
| 514 EXPECT_NE(s4a, s4b); | |
| 515 | |
| 516 // The derived profiles with the same site. | |
| 517 scoped_refptr<SiteInstance> s5a(instance1->GetSiteInstanceForURL( | |
| 518 GURL("chrome-extension://baz/bar"))); | |
| 519 scoped_refptr<SiteInstance> s5b(instance3->GetSiteInstanceForURL( | |
| 520 GURL("chrome-extension://baz/bar"))); | |
| 521 EXPECT_EQ(s5a, s5b); | |
| 522 | |
| 523 // The derived profiles with the different sites. | |
| 524 scoped_refptr<SiteInstance> s6a(instance1->GetSiteInstanceForURL( | |
| 525 GURL("chrome-extension://baz/bar"))); | |
| 526 scoped_refptr<SiteInstance> s6b(instance3->GetSiteInstanceForURL( | |
| 527 GURL("chrome-extension://foo/boo"))); | |
| 528 EXPECT_NE(s6a, s6b); | |
| 529 } | |
| OLD | NEW |