OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/string16.h" | 5 #include "base/string16.h" |
| 6 #include "chrome/browser/child_process_security_policy.h" |
6 #include "chrome/browser/renderer_host/browser_render_process_host.h" | 7 #include "chrome/browser/renderer_host/browser_render_process_host.h" |
7 #include "chrome/browser/renderer_host/render_view_host.h" | 8 #include "chrome/browser/renderer_host/render_view_host.h" |
8 #include "chrome/browser/renderer_host/test_render_view_host.h" | 9 #include "chrome/browser/renderer_host/test_render_view_host.h" |
9 #include "chrome/browser/tab_contents/navigation_entry.h" | 10 #include "chrome/browser/tab_contents/navigation_entry.h" |
10 #include "chrome/browser/tab_contents/tab_contents.h" | 11 #include "chrome/browser/tab_contents/tab_contents.h" |
| 12 #include "chrome/common/chrome_constants.h" |
11 #include "chrome/common/render_messages.h" | 13 #include "chrome/common/render_messages.h" |
12 #include "chrome/test/testing_profile.h" | 14 #include "chrome/test/testing_profile.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 | 16 |
15 class SiteInstanceTest : public testing::Test { | 17 class SiteInstanceTest : public testing::Test { |
16 private: | 18 private: |
17 MessageLoopForUI message_loop_; | 19 MessageLoopForUI message_loop_; |
18 }; | 20 }; |
19 | 21 |
20 namespace { | 22 namespace { |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 EXPECT_EQ(1, siteDeleteCounter1); | 165 EXPECT_EQ(1, siteDeleteCounter1); |
164 EXPECT_EQ(1, siteDeleteCounter2); | 166 EXPECT_EQ(1, siteDeleteCounter2); |
165 | 167 |
166 // Both BrowsingInstances are also now deleted | 168 // Both BrowsingInstances are also now deleted |
167 EXPECT_EQ(2, browsingDeleteCounter); | 169 EXPECT_EQ(2, browsingDeleteCounter); |
168 } | 170 } |
169 | 171 |
170 // Test to ensure UpdateMaxPageID is working properly. | 172 // Test to ensure UpdateMaxPageID is working properly. |
171 TEST_F(SiteInstanceTest, UpdateMaxPageID) { | 173 TEST_F(SiteInstanceTest, UpdateMaxPageID) { |
172 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL)); | 174 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL)); |
173 EXPECT_EQ(-1, instance.get()->max_page_id()); | 175 EXPECT_EQ(-1, instance->max_page_id()); |
174 | 176 |
175 // Make sure max_page_id_ is monotonically increasing. | 177 // Make sure max_page_id_ is monotonically increasing. |
176 instance.get()->UpdateMaxPageID(3); | 178 instance->UpdateMaxPageID(3); |
177 instance.get()->UpdateMaxPageID(1); | 179 instance->UpdateMaxPageID(1); |
178 EXPECT_EQ(3, instance.get()->max_page_id()); | 180 EXPECT_EQ(3, instance->max_page_id()); |
179 } | 181 } |
180 | 182 |
181 // Test to ensure GetProcess returns and creates processes correctly. | 183 // Test to ensure GetProcess returns and creates processes correctly. |
182 TEST_F(SiteInstanceTest, GetProcess) { | 184 TEST_F(SiteInstanceTest, GetProcess) { |
183 // Ensure that GetProcess returns a process. | 185 // Ensure that GetProcess returns a process. |
184 scoped_ptr<TestingProfile> profile(new TestingProfile()); | 186 scoped_ptr<TestingProfile> profile(new TestingProfile()); |
185 scoped_ptr<RenderProcessHost> host1; | 187 scoped_ptr<RenderProcessHost> host1; |
186 scoped_refptr<SiteInstance> instance( | 188 scoped_refptr<SiteInstance> instance( |
187 SiteInstance::CreateSiteInstance(profile.get())); | 189 SiteInstance::CreateSiteInstance(profile.get())); |
188 host1.reset(instance.get()->GetProcess()); | 190 host1.reset(instance->GetProcess()); |
189 EXPECT_TRUE(host1.get() != NULL); | 191 EXPECT_TRUE(host1.get() != NULL); |
190 | 192 |
191 // Ensure that GetProcess creates a new process. | 193 // Ensure that GetProcess creates a new process. |
192 scoped_refptr<SiteInstance> instance2( | 194 scoped_refptr<SiteInstance> instance2( |
193 SiteInstance::CreateSiteInstance(profile.get())); | 195 SiteInstance::CreateSiteInstance(profile.get())); |
194 scoped_ptr<RenderProcessHost> host2(instance2.get()->GetProcess()); | 196 scoped_ptr<RenderProcessHost> host2(instance2->GetProcess()); |
195 EXPECT_TRUE(host2.get() != NULL); | 197 EXPECT_TRUE(host2.get() != NULL); |
196 EXPECT_NE(host1.get(), host2.get()); | 198 EXPECT_NE(host1.get(), host2.get()); |
197 } | 199 } |
198 | 200 |
199 // Test to ensure SetSite and site() work properly. | 201 // Test to ensure SetSite and site() work properly. |
200 TEST_F(SiteInstanceTest, SetSite) { | 202 TEST_F(SiteInstanceTest, SetSite) { |
201 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL)); | 203 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL)); |
202 EXPECT_FALSE(instance->has_site()); | 204 EXPECT_FALSE(instance->has_site()); |
203 EXPECT_TRUE(instance.get()->site().is_empty()); | 205 EXPECT_TRUE(instance->site().is_empty()); |
204 | 206 |
205 instance.get()->SetSite(GURL("http://www.google.com/index.html")); | 207 instance->SetSite(GURL("http://www.google.com/index.html")); |
206 EXPECT_EQ(GURL("http://google.com"), instance.get()->site()); | 208 EXPECT_EQ(GURL("http://google.com"), instance->site()); |
207 | 209 |
208 EXPECT_TRUE(instance->has_site()); | 210 EXPECT_TRUE(instance->has_site()); |
209 } | 211 } |
210 | 212 |
211 // Test to ensure GetSiteForURL properly returns sites for URLs. | 213 // Test to ensure GetSiteForURL properly returns sites for URLs. |
212 TEST_F(SiteInstanceTest, GetSiteForURL) { | 214 TEST_F(SiteInstanceTest, GetSiteForURL) { |
213 // Pages are irrelevant. | 215 // Pages are irrelevant. |
214 GURL test_url = GURL("http://www.google.com/index.html"); | 216 GURL test_url = GURL("http://www.google.com/index.html"); |
215 EXPECT_EQ(GURL("http://google.com"), SiteInstance::GetSiteForURL(test_url)); | 217 EXPECT_EQ(GURL("http://google.com"), SiteInstance::GetSiteForURL(test_url)); |
216 | 218 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 | 286 |
285 // A separate site should create a separate SiteInstance. | 287 // A separate site should create a separate SiteInstance. |
286 const GURL url_b1("http://www.yahoo.com/"); | 288 const GURL url_b1("http://www.yahoo.com/"); |
287 scoped_refptr<SiteInstance> site_instance_b1( | 289 scoped_refptr<SiteInstance> site_instance_b1( |
288 browsing_instance->GetSiteInstanceForURL(url_b1)); | 290 browsing_instance->GetSiteInstanceForURL(url_b1)); |
289 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); | 291 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); |
290 | 292 |
291 // Getting the new SiteInstance from the BrowsingInstance and from another | 293 // Getting the new SiteInstance from the BrowsingInstance and from another |
292 // SiteInstance in the BrowsingInstance should give the same result. | 294 // SiteInstance in the BrowsingInstance should give the same result. |
293 EXPECT_EQ(site_instance_b1.get(), | 295 EXPECT_EQ(site_instance_b1.get(), |
294 site_instance_a1.get()->GetRelatedSiteInstance(url_b1)); | 296 site_instance_a1->GetRelatedSiteInstance(url_b1)); |
295 | 297 |
296 // A second visit to the original site should return the same SiteInstance. | 298 // A second visit to the original site should return the same SiteInstance. |
297 const GURL url_a2("http://www.google.com/2.html"); | 299 const GURL url_a2("http://www.google.com/2.html"); |
298 EXPECT_EQ(site_instance_a1.get(), | 300 EXPECT_EQ(site_instance_a1.get(), |
299 browsing_instance->GetSiteInstanceForURL(url_a2)); | 301 browsing_instance->GetSiteInstanceForURL(url_a2)); |
300 EXPECT_EQ(site_instance_a1.get(), | 302 EXPECT_EQ(site_instance_a1.get(), |
301 site_instance_a1.get()->GetRelatedSiteInstance(url_a2)); | 303 site_instance_a1->GetRelatedSiteInstance(url_a2)); |
302 | 304 |
303 // A visit to the original site in a new BrowsingInstance (same or different | 305 // A visit to the original site in a new BrowsingInstance (same or different |
304 // profile) should return a different SiteInstance. | 306 // profile) should return a different SiteInstance. |
305 TestBrowsingInstance* browsing_instance2 = | 307 TestBrowsingInstance* browsing_instance2 = |
306 new TestBrowsingInstance(NULL, &deleteCounter); | 308 new TestBrowsingInstance(NULL, &deleteCounter); |
307 browsing_instance2->use_process_per_site = false; | 309 browsing_instance2->use_process_per_site = false; |
308 // Ensure the new SiteInstance is ref counted so that it gets deleted. | 310 // Ensure the new SiteInstance is ref counted so that it gets deleted. |
309 scoped_refptr<SiteInstance> site_instance_a2_2( | 311 scoped_refptr<SiteInstance> site_instance_a2_2( |
310 browsing_instance2->GetSiteInstanceForURL(url_a2)); | 312 browsing_instance2->GetSiteInstanceForURL(url_a2)); |
311 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); | 313 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); |
(...skipping 30 matching lines...) Expand all Loading... |
342 | 344 |
343 // A separate site should create a separate SiteInstance. | 345 // A separate site should create a separate SiteInstance. |
344 const GURL url_b1("http://www.yahoo.com/"); | 346 const GURL url_b1("http://www.yahoo.com/"); |
345 scoped_refptr<SiteInstance> site_instance_b1( | 347 scoped_refptr<SiteInstance> site_instance_b1( |
346 browsing_instance->GetSiteInstanceForURL(url_b1)); | 348 browsing_instance->GetSiteInstanceForURL(url_b1)); |
347 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); | 349 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); |
348 | 350 |
349 // Getting the new SiteInstance from the BrowsingInstance and from another | 351 // Getting the new SiteInstance from the BrowsingInstance and from another |
350 // SiteInstance in the BrowsingInstance should give the same result. | 352 // SiteInstance in the BrowsingInstance should give the same result. |
351 EXPECT_EQ(site_instance_b1.get(), | 353 EXPECT_EQ(site_instance_b1.get(), |
352 site_instance_a1.get()->GetRelatedSiteInstance(url_b1)); | 354 site_instance_a1->GetRelatedSiteInstance(url_b1)); |
353 | 355 |
354 // A second visit to the original site should return the same SiteInstance. | 356 // A second visit to the original site should return the same SiteInstance. |
355 const GURL url_a2("http://www.google.com/2.html"); | 357 const GURL url_a2("http://www.google.com/2.html"); |
356 EXPECT_EQ(site_instance_a1.get(), | 358 EXPECT_EQ(site_instance_a1.get(), |
357 browsing_instance->GetSiteInstanceForURL(url_a2)); | 359 browsing_instance->GetSiteInstanceForURL(url_a2)); |
358 EXPECT_EQ(site_instance_a1.get(), | 360 EXPECT_EQ(site_instance_a1.get(), |
359 site_instance_a1.get()->GetRelatedSiteInstance(url_a2)); | 361 site_instance_a1->GetRelatedSiteInstance(url_a2)); |
360 | 362 |
361 // A visit to the original site in a new BrowsingInstance (same profile) | 363 // A visit to the original site in a new BrowsingInstance (same profile) |
362 // should also return the same SiteInstance. | 364 // should also return the same SiteInstance. |
363 // This BrowsingInstance doesn't get its own SiteInstance within the test, so | 365 // This BrowsingInstance doesn't get its own SiteInstance within the test, so |
364 // it won't be deleted by its children. Thus, we'll keep a ref count to it | 366 // it won't be deleted by its children. Thus, we'll keep a ref count to it |
365 // to make sure it gets deleted. | 367 // to make sure it gets deleted. |
366 scoped_refptr<TestBrowsingInstance> browsing_instance2( | 368 scoped_refptr<TestBrowsingInstance> browsing_instance2( |
367 new TestBrowsingInstance(NULL, &deleteCounter)); | 369 new TestBrowsingInstance(NULL, &deleteCounter)); |
368 browsing_instance2->use_process_per_site = true; | 370 browsing_instance2->use_process_per_site = true; |
369 EXPECT_EQ(site_instance_a1.get(), | 371 EXPECT_EQ(site_instance_a1.get(), |
(...skipping 21 matching lines...) Expand all Loading... |
391 GURL("http://www.yahoo.com"))); // different BI, but same profile | 393 GURL("http://www.yahoo.com"))); // different BI, but same profile |
392 | 394 |
393 // Should be able to see that we don't have SiteInstances. | 395 // Should be able to see that we don't have SiteInstances. |
394 EXPECT_FALSE(browsing_instance->HasSiteInstance( | 396 EXPECT_FALSE(browsing_instance->HasSiteInstance( |
395 GURL("https://www.google.com"))); // not visited before | 397 GURL("https://www.google.com"))); // not visited before |
396 EXPECT_FALSE(browsing_instance3->HasSiteInstance( | 398 EXPECT_FALSE(browsing_instance3->HasSiteInstance( |
397 GURL("http://www.yahoo.com"))); // different BI, different profile | 399 GURL("http://www.yahoo.com"))); // different BI, different profile |
398 | 400 |
399 // browsing_instances will be deleted when their SiteInstances are deleted | 401 // browsing_instances will be deleted when their SiteInstances are deleted |
400 } | 402 } |
| 403 |
| 404 static SiteInstance* CreateSiteInstance(RenderProcessHostFactory* factory, |
| 405 const GURL& url) { |
| 406 SiteInstance* instance = SiteInstance::CreateSiteInstanceForURL(NULL, url); |
| 407 instance->set_render_process_host_factory(factory); |
| 408 return instance; |
| 409 } |
| 410 |
| 411 // Test to ensure that pages that require certain privileges are grouped |
| 412 // in processes with similar pages. |
| 413 TEST_F(SiteInstanceTest, ProcessSharingByType) { |
| 414 MockRenderProcessHostFactory rph_factory; |
| 415 ChildProcessSecurityPolicy* policy = |
| 416 ChildProcessSecurityPolicy::GetInstance(); |
| 417 |
| 418 // Make a bunch of mock renderers so that we hit the limit. |
| 419 std::vector<MockRenderProcessHost*> hosts; |
| 420 for (size_t i = 0; i < chrome::kMaxRendererProcessCount; ++i) |
| 421 hosts.push_back(new MockRenderProcessHost(NULL)); |
| 422 |
| 423 // Create some extension instances and make sure they share a process. |
| 424 scoped_refptr<SiteInstance> extension1_instance( |
| 425 CreateSiteInstance(&rph_factory, GURL("chrome-extension://foo/bar"))); |
| 426 policy->Add(extension1_instance->GetProcess()->pid()); |
| 427 policy->GrantExtensionBindings(extension1_instance->GetProcess()->pid()); |
| 428 |
| 429 scoped_refptr<SiteInstance> extension2_instance( |
| 430 CreateSiteInstance(&rph_factory, GURL("chrome-extension://baz/bar"))); |
| 431 |
| 432 scoped_ptr<RenderProcessHost> extension_host( |
| 433 extension1_instance->GetProcess()); |
| 434 EXPECT_EQ(extension1_instance->GetProcess(), |
| 435 extension2_instance->GetProcess()); |
| 436 |
| 437 // Create some DOMUI instances and make sure they share a process. |
| 438 scoped_refptr<SiteInstance> dom1_instance( |
| 439 CreateSiteInstance(&rph_factory, GURL("chrome://newtab"))); |
| 440 policy->Add(dom1_instance->GetProcess()->pid()); |
| 441 policy->GrantDOMUIBindings(dom1_instance->GetProcess()->pid()); |
| 442 |
| 443 scoped_refptr<SiteInstance> dom2_instance( |
| 444 CreateSiteInstance(&rph_factory, GURL("chrome://history"))); |
| 445 |
| 446 scoped_ptr<RenderProcessHost> dom_host(dom1_instance->GetProcess()); |
| 447 EXPECT_EQ(dom1_instance->GetProcess(), dom2_instance->GetProcess()); |
| 448 |
| 449 // Make sure none of differing privilege processes are mixed. |
| 450 EXPECT_NE(extension1_instance->GetProcess(), dom1_instance->GetProcess()); |
| 451 |
| 452 for (size_t i = 0; i < chrome::kMaxRendererProcessCount; ++i) { |
| 453 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); |
| 454 EXPECT_NE(dom1_instance->GetProcess(), hosts[i]); |
| 455 } |
| 456 |
| 457 STLDeleteContainerPointers(hosts.begin(), hosts.end()); |
| 458 } |
OLD | NEW |