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

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

Issue 9146028: Define the public interface for content browser SiteInstance. This interface is implemented by th... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
jam 2012/01/24 03:29:33 nit: rename this file to site_instance_impl_unitte
ananta 2012/01/24 23:46:26 Done.
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"
11 #include "content/browser/mock_content_browser_client.h" 11 #include "content/browser/mock_content_browser_client.h"
12 #include "content/browser/renderer_host/render_process_host_impl.h" 12 #include "content/browser/renderer_host/render_process_host_impl.h"
13 #include "content/browser/renderer_host/render_view_host.h" 13 #include "content/browser/renderer_host/render_view_host.h"
14 #include "content/browser/renderer_host/test_render_view_host.h" 14 #include "content/browser/renderer_host/test_render_view_host.h"
15 #include "content/browser/site_instance.h" 15 #include "content/browser/site_instance_impl.h"
16 #include "content/browser/tab_contents/navigation_entry_impl.h" 16 #include "content/browser/tab_contents/navigation_entry_impl.h"
17 #include "content/browser/tab_contents/tab_contents.h" 17 #include "content/browser/tab_contents/tab_contents.h"
18 #include "content/browser/webui/empty_web_ui_factory.h" 18 #include "content/browser/webui/empty_web_ui_factory.h"
19 #include "content/public/common/content_client.h" 19 #include "content/public/common/content_client.h"
20 #include "content/public/common/content_constants.h" 20 #include "content/public/common/content_constants.h"
21 #include "content/public/common/url_constants.h" 21 #include "content/public/common/url_constants.h"
22 #include "content/test/test_browser_context.h" 22 #include "content/test/test_browser_context.h"
23 #include "googleurl/src/url_util.h" 23 #include "googleurl/src/url_util.h"
24 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
25 25
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 virtual ~TestBrowsingInstance() { 145 virtual ~TestBrowsingInstance() {
146 (*delete_counter_)++; 146 (*delete_counter_)++;
147 } 147 }
148 148
149 // Set by individual tests. 149 // Set by individual tests.
150 bool use_process_per_site_; 150 bool use_process_per_site_;
151 151
152 int* delete_counter_; 152 int* delete_counter_;
153 }; 153 };
154 154
155 class TestSiteInstance : public SiteInstance { 155 class TestSiteInstance : public SiteInstanceImpl {
156 public: 156 public:
157 static TestSiteInstance* CreateTestSiteInstance( 157 static TestSiteInstance* CreateTestSiteInstance(
158 content::BrowserContext* browser_context, 158 content::BrowserContext* browser_context,
159 int* site_delete_counter, 159 int* site_delete_counter,
160 int* browsing_delete_counter) { 160 int* browsing_delete_counter) {
161 TestBrowsingInstance* browsing_instance = 161 TestBrowsingInstance* browsing_instance =
162 new TestBrowsingInstance(browser_context, browsing_delete_counter); 162 new TestBrowsingInstance(browser_context, browsing_delete_counter);
163 return new TestSiteInstance(browsing_instance, site_delete_counter); 163 return new TestSiteInstance(browsing_instance, site_delete_counter);
164 } 164 }
165 165
166 private: 166 private:
167 TestSiteInstance(BrowsingInstance* browsing_instance, int* delete_counter) 167 TestSiteInstance(BrowsingInstance* browsing_instance, int* delete_counter)
168 : SiteInstance(browsing_instance), delete_counter_(delete_counter) {} 168 : SiteInstanceImpl(browsing_instance), delete_counter_(delete_counter) {}
169 virtual ~TestSiteInstance() { 169 virtual ~TestSiteInstance() {
170 (*delete_counter_)++; 170 (*delete_counter_)++;
171 } 171 }
172 172
173 int* delete_counter_; 173 int* delete_counter_;
174 }; 174 };
175 175
176 } // namespace 176 } // namespace
177 177
178 // Test to ensure no memory leaks for SiteInstance objects. 178 // Test to ensure no memory leaks for SiteInstance objects.
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 241
242 // Test that NavigationEntries with SiteInstances can be cloned, but that their 242 // Test that NavigationEntries with SiteInstances can be cloned, but that their
243 // SiteInstances can be changed afterwards. Also tests that the ref counts are 243 // SiteInstances can be changed afterwards. Also tests that the ref counts are
244 // updated properly after the change. 244 // updated properly after the change.
245 TEST_F(SiteInstanceTest, CloneNavigationEntry) { 245 TEST_F(SiteInstanceTest, CloneNavigationEntry) {
246 int site_delete_counter1 = 0; 246 int site_delete_counter1 = 0;
247 int site_delete_counter2 = 0; 247 int site_delete_counter2 = 0;
248 int browsing_delete_counter = 0; 248 int browsing_delete_counter = 0;
249 const GURL url("test:foo"); 249 const GURL url("test:foo");
250 250
251 SiteInstance* instance1 = 251 content::SiteInstance* instance1 =
252 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter1, 252 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter1,
253 &browsing_delete_counter); 253 &browsing_delete_counter);
254 SiteInstance* instance2 = 254 content::SiteInstance* instance2 =
255 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter2, 255 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter2,
256 &browsing_delete_counter); 256 &browsing_delete_counter);
257 257
258 NavigationEntryImpl* e1 = new NavigationEntryImpl( 258 NavigationEntryImpl* e1 = new NavigationEntryImpl(
259 instance1, 0, url, content::Referrer(), string16(), 259 instance1, 0, url, content::Referrer(), string16(),
260 content::PAGE_TRANSITION_LINK, false); 260 content::PAGE_TRANSITION_LINK, false);
261 // Clone the entry 261 // Clone the entry
262 NavigationEntryImpl* e2 = new NavigationEntryImpl(*e1); 262 NavigationEntryImpl* e2 = new NavigationEntryImpl(*e1);
263 263
264 // Should be able to change the SiteInstance of the cloned entry. 264 // Should be able to change the SiteInstance of the cloned entry.
(...skipping 12 matching lines...) Expand all
277 277
278 // Both BrowsingInstances are also now deleted 278 // Both BrowsingInstances are also now deleted
279 EXPECT_EQ(2, browsing_delete_counter); 279 EXPECT_EQ(2, browsing_delete_counter);
280 } 280 }
281 281
282 // Test to ensure GetProcess returns and creates processes correctly. 282 // Test to ensure GetProcess returns and creates processes correctly.
283 TEST_F(SiteInstanceTest, GetProcess) { 283 TEST_F(SiteInstanceTest, GetProcess) {
284 // Ensure that GetProcess returns a process. 284 // Ensure that GetProcess returns a process.
285 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 285 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
286 scoped_ptr<content::RenderProcessHost> host1; 286 scoped_ptr<content::RenderProcessHost> host1;
287 scoped_refptr<SiteInstance> instance( 287 scoped_refptr<content::SiteInstance> instance(
288 SiteInstance::CreateSiteInstance(browser_context.get())); 288 content::SiteInstance::CreateSiteInstance(browser_context.get()));
289 host1.reset(instance->GetProcess()); 289 host1.reset(instance->GetProcess());
290 EXPECT_TRUE(host1.get() != NULL); 290 EXPECT_TRUE(host1.get() != NULL);
291 291
292 // Ensure that GetProcess creates a new process. 292 // Ensure that GetProcess creates a new process.
293 scoped_refptr<SiteInstance> instance2( 293 scoped_refptr<content::SiteInstance> instance2(
294 SiteInstance::CreateSiteInstance(browser_context.get())); 294 content::SiteInstance::CreateSiteInstance(browser_context.get()));
295 scoped_ptr<content::RenderProcessHost> host2(instance2->GetProcess()); 295 scoped_ptr<content::RenderProcessHost> host2(instance2->GetProcess());
296 EXPECT_TRUE(host2.get() != NULL); 296 EXPECT_TRUE(host2.get() != NULL);
297 EXPECT_NE(host1.get(), host2.get()); 297 EXPECT_NE(host1.get(), host2.get());
298 } 298 }
299 299
300 // Test to ensure SetSite and site() work properly. 300 // Test to ensure SetSite and site() work properly.
301 TEST_F(SiteInstanceTest, SetSite) { 301 TEST_F(SiteInstanceTest, SetSite) {
302 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL)); 302 scoped_refptr<content::SiteInstance> instance(
303 EXPECT_FALSE(instance->has_site()); 303 content::SiteInstance::CreateSiteInstance(NULL));
304 EXPECT_TRUE(instance->site().is_empty()); 304 EXPECT_FALSE(instance->HasSite());
305 EXPECT_TRUE(instance->GetSite().is_empty());
305 306
306 instance->SetSite(GURL("http://www.google.com/index.html")); 307 instance->SetSite(GURL("http://www.google.com/index.html"));
307 EXPECT_EQ(GURL("http://google.com"), instance->site()); 308 EXPECT_EQ(GURL("http://google.com"), instance->GetSite());
308 309
309 EXPECT_TRUE(instance->has_site()); 310 EXPECT_TRUE(instance->HasSite());
310 } 311 }
311 312
312 // Test to ensure GetSiteForURL properly returns sites for URLs. 313 // Test to ensure GetSiteForURL properly returns sites for URLs.
313 TEST_F(SiteInstanceTest, GetSiteForURL) { 314 TEST_F(SiteInstanceTest, GetSiteForURL) {
314 // Pages are irrelevant. 315 // Pages are irrelevant.
315 GURL test_url = GURL("http://www.google.com/index.html"); 316 GURL test_url = GURL("http://www.google.com/index.html");
316 EXPECT_EQ(GURL("http://google.com"), 317 EXPECT_EQ(GURL("http://google.com"),
317 SiteInstance::GetSiteForURL(NULL, test_url)); 318 content::SiteInstance::GetSiteForURL(NULL, test_url));
318 319
319 // Ports are irrlevant. 320 // Ports are irrlevant.
320 test_url = GURL("https://www.google.com:8080"); 321 test_url = GURL("https://www.google.com:8080");
321 EXPECT_EQ(GURL("https://google.com"), 322 EXPECT_EQ(GURL("https://google.com"),
322 SiteInstance::GetSiteForURL(NULL, test_url)); 323 content::SiteInstance::GetSiteForURL(NULL, test_url));
323 324
324 // Javascript URLs have no site. 325 // Javascript URLs have no site.
325 test_url = GURL("javascript:foo();"); 326 test_url = GURL("javascript:foo();");
326 EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); 327 EXPECT_EQ(GURL(), content::SiteInstance::GetSiteForURL(NULL, test_url));
327 328
328 test_url = GURL("http://foo/a.html"); 329 test_url = GURL("http://foo/a.html");
329 EXPECT_EQ(GURL("http://foo"), SiteInstance::GetSiteForURL(NULL, test_url)); 330 EXPECT_EQ(GURL("http://foo"), content::SiteInstance::GetSiteForURL(
331 NULL, test_url));
330 332
331 test_url = GURL("file:///C:/Downloads/"); 333 test_url = GURL("file:///C:/Downloads/");
332 EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); 334 EXPECT_EQ(GURL(), content::SiteInstance::GetSiteForURL(NULL, test_url));
333 335
334 // TODO(creis): Do we want to special case file URLs to ensure they have 336 // TODO(creis): Do we want to special case file URLs to ensure they have
335 // either no site or a special "file://" site? We currently return 337 // either no site or a special "file://" site? We currently return
336 // "file://home/" as the site, which seems broken. 338 // "file://home/" as the site, which seems broken.
337 // test_url = GURL("file://home/"); 339 // test_url = GURL("file://home/");
338 // EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); 340 // EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url));
339 } 341 }
340 342
341 // Test of distinguishing URLs from different sites. Most of this logic is 343 // Test of distinguishing URLs from different sites. Most of this logic is
342 // tested in RegistryControlledDomainTest. This test focuses on URLs with 344 // tested in RegistryControlledDomainTest. This test focuses on URLs with
343 // different schemes or ports. 345 // different schemes or ports.
344 TEST_F(SiteInstanceTest, IsSameWebSite) { 346 TEST_F(SiteInstanceTest, IsSameWebSite) {
345 GURL url_foo = GURL("http://foo/a.html"); 347 GURL url_foo = GURL("http://foo/a.html");
346 GURL url_foo2 = GURL("http://foo/b.html"); 348 GURL url_foo2 = GURL("http://foo/b.html");
347 GURL url_foo_https = GURL("https://foo/a.html"); 349 GURL url_foo_https = GURL("https://foo/a.html");
348 GURL url_foo_port = GURL("http://foo:8080/a.html"); 350 GURL url_foo_port = GURL("http://foo:8080/a.html");
349 GURL url_javascript = GURL("javascript:alert(1);"); 351 GURL url_javascript = GURL("javascript:alert(1);");
350 GURL url_crash = GURL(chrome::kAboutCrashURL); 352 GURL url_crash = GURL(chrome::kAboutCrashURL);
351 GURL url_browser_specified = GURL(kSameAsAnyInstanceURL); 353 GURL url_browser_specified = GURL(kSameAsAnyInstanceURL);
352 354
353 // Same scheme and port -> same site. 355 // Same scheme and port -> same site.
354 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo2)); 356 EXPECT_TRUE(content::SiteInstance::IsSameWebSite(NULL, url_foo, url_foo2));
355 357
356 // Different scheme -> different site. 358 // Different scheme -> different site.
357 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_https)); 359 EXPECT_FALSE(content::SiteInstance::IsSameWebSite(NULL, url_foo,
360 url_foo_https));
358 361
359 // Different port -> same site. 362 // Different port -> same site.
360 // (Changes to document.domain make renderer ignore the port.) 363 // (Changes to document.domain make renderer ignore the port.)
361 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_port)); 364 EXPECT_TRUE(content::SiteInstance::IsSameWebSite(NULL, url_foo,
365 url_foo_port));
362 366
363 // JavaScript links should be considered same site for anything. 367 // JavaScript links should be considered same site for anything.
364 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo)); 368 EXPECT_TRUE(content::SiteInstance::IsSameWebSite(NULL, url_javascript,
365 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_https)); 369 url_foo));
366 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_port)); 370 EXPECT_TRUE(content::SiteInstance::IsSameWebSite(NULL, url_javascript,
371 url_foo_https));
372 EXPECT_TRUE(content::SiteInstance::IsSameWebSite(NULL, url_javascript,
373 url_foo_port));
367 374
368 // The URLs specified by the ContentBrowserClient should also be treated as 375 // The URLs specified by the ContentBrowserClient should also be treated as
369 // same site. 376 // same site.
370 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_crash, url_foo)); 377 EXPECT_TRUE(content::SiteInstance::IsSameWebSite(NULL, url_crash,
378 url_foo));
371 EXPECT_TRUE( 379 EXPECT_TRUE(
372 SiteInstance::IsSameWebSite(NULL, url_browser_specified, url_foo)); 380 content::SiteInstance::IsSameWebSite(NULL, url_browser_specified,
381 url_foo));
373 } 382 }
374 383
375 // Test to ensure that there is only one SiteInstance per site in a given 384 // Test to ensure that there is only one SiteInstance per site in a given
376 // BrowsingInstance, when process-per-site is not in use. 385 // BrowsingInstance, when process-per-site is not in use.
377 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { 386 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) {
378 int delete_counter = 0; 387 int delete_counter = 0;
379 TestBrowsingInstance* browsing_instance = 388 TestBrowsingInstance* browsing_instance =
380 new TestBrowsingInstance(NULL, &delete_counter); 389 new TestBrowsingInstance(NULL, &delete_counter);
381 browsing_instance->set_use_process_per_site(false); 390 browsing_instance->set_use_process_per_site(false);
382 391
383 const GURL url_a1("http://www.google.com/1.html"); 392 const GURL url_a1("http://www.google.com/1.html");
384 scoped_refptr<SiteInstance> site_instance_a1( 393 scoped_refptr<content::SiteInstance> site_instance_a1(
385 browsing_instance->GetSiteInstanceForURL(url_a1)); 394 browsing_instance->GetSiteInstanceForURL(url_a1));
386 EXPECT_TRUE(site_instance_a1.get() != NULL); 395 EXPECT_TRUE(site_instance_a1.get() != NULL);
387 396
388 // A separate site should create a separate SiteInstance. 397 // A separate site should create a separate SiteInstance.
389 const GURL url_b1("http://www.yahoo.com/"); 398 const GURL url_b1("http://www.yahoo.com/");
390 scoped_refptr<SiteInstance> site_instance_b1( 399 scoped_refptr<content::SiteInstance> site_instance_b1(
391 browsing_instance->GetSiteInstanceForURL(url_b1)); 400 browsing_instance->GetSiteInstanceForURL(url_b1));
392 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); 401 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
393 402
394 // Getting the new SiteInstance from the BrowsingInstance and from another 403 // Getting the new SiteInstance from the BrowsingInstance and from another
395 // SiteInstance in the BrowsingInstance should give the same result. 404 // SiteInstance in the BrowsingInstance should give the same result.
396 EXPECT_EQ(site_instance_b1.get(), 405 EXPECT_EQ(site_instance_b1.get(),
397 site_instance_a1->GetRelatedSiteInstance(url_b1)); 406 site_instance_a1->GetRelatedSiteInstance(url_b1));
398 407
399 // A second visit to the original site should return the same SiteInstance. 408 // A second visit to the original site should return the same SiteInstance.
400 const GURL url_a2("http://www.google.com/2.html"); 409 const GURL url_a2("http://www.google.com/2.html");
401 EXPECT_EQ(site_instance_a1.get(), 410 EXPECT_EQ(site_instance_a1.get(),
402 browsing_instance->GetSiteInstanceForURL(url_a2)); 411 browsing_instance->GetSiteInstanceForURL(url_a2));
403 EXPECT_EQ(site_instance_a1.get(), 412 EXPECT_EQ(site_instance_a1.get(),
404 site_instance_a1->GetRelatedSiteInstance(url_a2)); 413 site_instance_a1->GetRelatedSiteInstance(url_a2));
405 414
406 // A visit to the original site in a new BrowsingInstance (same or different 415 // A visit to the original site in a new BrowsingInstance (same or different
407 // browser context) should return a different SiteInstance. 416 // browser context) should return a different SiteInstance.
408 TestBrowsingInstance* browsing_instance2 = 417 TestBrowsingInstance* browsing_instance2 =
409 new TestBrowsingInstance(NULL, &delete_counter); 418 new TestBrowsingInstance(NULL, &delete_counter);
410 browsing_instance2->set_use_process_per_site(false); 419 browsing_instance2->set_use_process_per_site(false);
411 // Ensure the new SiteInstance is ref counted so that it gets deleted. 420 // Ensure the new SiteInstance is ref counted so that it gets deleted.
412 scoped_refptr<SiteInstance> site_instance_a2_2( 421 scoped_refptr<content::SiteInstance> site_instance_a2_2(
413 browsing_instance2->GetSiteInstanceForURL(url_a2)); 422 browsing_instance2->GetSiteInstanceForURL(url_a2));
414 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); 423 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get());
415 424
416 // Should be able to see that we do have SiteInstances. 425 // Should be able to see that we do have SiteInstances.
417 EXPECT_TRUE(browsing_instance->HasSiteInstance( 426 EXPECT_TRUE(browsing_instance->HasSiteInstance(
418 GURL("http://mail.google.com"))); 427 GURL("http://mail.google.com")));
419 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 428 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
420 GURL("http://mail.google.com"))); 429 GURL("http://mail.google.com")));
421 EXPECT_TRUE(browsing_instance->HasSiteInstance( 430 EXPECT_TRUE(browsing_instance->HasSiteInstance(
422 GURL("http://mail.yahoo.com"))); 431 GURL("http://mail.yahoo.com")));
423 432
424 // Should be able to see that we don't have SiteInstances. 433 // Should be able to see that we don't have SiteInstances.
425 EXPECT_FALSE(browsing_instance->HasSiteInstance( 434 EXPECT_FALSE(browsing_instance->HasSiteInstance(
426 GURL("https://www.google.com"))); 435 GURL("https://www.google.com")));
427 EXPECT_FALSE(browsing_instance2->HasSiteInstance( 436 EXPECT_FALSE(browsing_instance2->HasSiteInstance(
428 GURL("http://www.yahoo.com"))); 437 GURL("http://www.yahoo.com")));
429 438
430 // browsing_instances will be deleted when their SiteInstances are deleted 439 // browsing_instances will be deleted when their SiteInstances are deleted
431 } 440 }
432 441
433 // Test to ensure that there is only one SiteInstance per site for an entire 442 // Test to ensure that there is only one SiteInstance per site for an entire
434 // BrowserContext, if process-per-site is in use. 443 // BrowserContext, if process-per-site is in use.
435 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { 444 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) {
436 int delete_counter = 0; 445 int delete_counter = 0;
437 TestBrowsingInstance* browsing_instance = 446 TestBrowsingInstance* browsing_instance =
438 new TestBrowsingInstance(NULL, &delete_counter); 447 new TestBrowsingInstance(NULL, &delete_counter);
439 browsing_instance->set_use_process_per_site(true); 448 browsing_instance->set_use_process_per_site(true);
440 449
441 const GURL url_a1("http://www.google.com/1.html"); 450 const GURL url_a1("http://www.google.com/1.html");
442 scoped_refptr<SiteInstance> site_instance_a1( 451 scoped_refptr<content::SiteInstance> site_instance_a1(
443 browsing_instance->GetSiteInstanceForURL(url_a1)); 452 browsing_instance->GetSiteInstanceForURL(url_a1));
444 EXPECT_TRUE(site_instance_a1.get() != NULL); 453 EXPECT_TRUE(site_instance_a1.get() != NULL);
445 454
446 // A separate site should create a separate SiteInstance. 455 // A separate site should create a separate SiteInstance.
447 const GURL url_b1("http://www.yahoo.com/"); 456 const GURL url_b1("http://www.yahoo.com/");
448 scoped_refptr<SiteInstance> site_instance_b1( 457 scoped_refptr<content::SiteInstance> site_instance_b1(
449 browsing_instance->GetSiteInstanceForURL(url_b1)); 458 browsing_instance->GetSiteInstanceForURL(url_b1));
450 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); 459 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
451 460
452 // Getting the new SiteInstance from the BrowsingInstance and from another 461 // Getting the new SiteInstance from the BrowsingInstance and from another
453 // SiteInstance in the BrowsingInstance should give the same result. 462 // SiteInstance in the BrowsingInstance should give the same result.
454 EXPECT_EQ(site_instance_b1.get(), 463 EXPECT_EQ(site_instance_b1.get(),
455 site_instance_a1->GetRelatedSiteInstance(url_b1)); 464 site_instance_a1->GetRelatedSiteInstance(url_b1));
456 465
457 // A second visit to the original site should return the same SiteInstance. 466 // A second visit to the original site should return the same SiteInstance.
458 const GURL url_a2("http://www.google.com/2.html"); 467 const GURL url_a2("http://www.google.com/2.html");
(...skipping 13 matching lines...) Expand all
472 EXPECT_EQ(site_instance_a1.get(), 481 EXPECT_EQ(site_instance_a1.get(),
473 browsing_instance2->GetSiteInstanceForURL(url_a2)); 482 browsing_instance2->GetSiteInstanceForURL(url_a2));
474 483
475 // A visit to the original site in a new BrowsingInstance (different browser 484 // A visit to the original site in a new BrowsingInstance (different browser
476 // context) should return a different SiteInstance. 485 // context) should return a different SiteInstance.
477 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 486 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
478 TestBrowsingInstance* browsing_instance3 = 487 TestBrowsingInstance* browsing_instance3 =
479 new TestBrowsingInstance(browser_context.get(), &delete_counter); 488 new TestBrowsingInstance(browser_context.get(), &delete_counter);
480 browsing_instance3->set_use_process_per_site(true); 489 browsing_instance3->set_use_process_per_site(true);
481 // Ensure the new SiteInstance is ref counted so that it gets deleted. 490 // Ensure the new SiteInstance is ref counted so that it gets deleted.
482 scoped_refptr<SiteInstance> site_instance_a2_3( 491 scoped_refptr<content::SiteInstance> site_instance_a2_3(
483 browsing_instance3->GetSiteInstanceForURL(url_a2)); 492 browsing_instance3->GetSiteInstanceForURL(url_a2));
484 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); 493 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get());
485 494
486 // Should be able to see that we do have SiteInstances. 495 // Should be able to see that we do have SiteInstances.
487 EXPECT_TRUE(browsing_instance->HasSiteInstance( 496 EXPECT_TRUE(browsing_instance->HasSiteInstance(
488 GURL("http://mail.google.com"))); // visited before 497 GURL("http://mail.google.com"))); // visited before
489 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 498 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
490 GURL("http://mail.google.com"))); // visited before 499 GURL("http://mail.google.com"))); // visited before
491 EXPECT_TRUE(browsing_instance->HasSiteInstance( 500 EXPECT_TRUE(browsing_instance->HasSiteInstance(
492 GURL("http://mail.yahoo.com"))); // visited before 501 GURL("http://mail.yahoo.com"))); // visited before
493 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 502 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
494 GURL("http://www.yahoo.com"))); // different BI, but same browser context 503 GURL("http://www.yahoo.com"))); // different BI, but same browser context
495 504
496 // Should be able to see that we don't have SiteInstances. 505 // Should be able to see that we don't have SiteInstances.
497 EXPECT_FALSE(browsing_instance->HasSiteInstance( 506 EXPECT_FALSE(browsing_instance->HasSiteInstance(
498 GURL("https://www.google.com"))); // not visited before 507 GURL("https://www.google.com"))); // not visited before
499 EXPECT_FALSE(browsing_instance3->HasSiteInstance( 508 EXPECT_FALSE(browsing_instance3->HasSiteInstance(
500 GURL("http://www.yahoo.com"))); // different BI, different context 509 GURL("http://www.yahoo.com"))); // different BI, different context
501 510
502 // browsing_instances will be deleted when their SiteInstances are deleted 511 // browsing_instances will be deleted when their SiteInstances are deleted
503 } 512 }
504 513
505 static SiteInstance* CreateSiteInstance( 514 static SiteInstanceImpl* CreateSiteInstance(
506 content::RenderProcessHostFactory* factory, const GURL& url) { 515 content::RenderProcessHostFactory* factory, const GURL& url) {
507 SiteInstance* instance = SiteInstance::CreateSiteInstanceForURL(NULL, url); 516 SiteInstanceImpl* instance =
517 reinterpret_cast<SiteInstanceImpl*>(
518 content::SiteInstance::CreateSiteInstanceForURL(NULL, url));
508 instance->set_render_process_host_factory(factory); 519 instance->set_render_process_host_factory(factory);
509 return instance; 520 return instance;
510 } 521 }
511 522
512 // Test to ensure that pages that require certain privileges are grouped 523 // Test to ensure that pages that require certain privileges are grouped
513 // in processes with similar pages. 524 // in processes with similar pages.
514 TEST_F(SiteInstanceTest, ProcessSharingByType) { 525 TEST_F(SiteInstanceTest, ProcessSharingByType) {
515 MockRenderProcessHostFactory rph_factory; 526 MockRenderProcessHostFactory rph_factory;
516 ChildProcessSecurityPolicy* policy = 527 ChildProcessSecurityPolicy* policy =
517 ChildProcessSecurityPolicy::GetInstance(); 528 ChildProcessSecurityPolicy::GetInstance();
518 529
519 // Make a bunch of mock renderers so that we hit the limit. 530 // Make a bunch of mock renderers so that we hit the limit.
520 std::vector<MockRenderProcessHost*> hosts; 531 std::vector<MockRenderProcessHost*> hosts;
521 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) 532 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i)
522 hosts.push_back(new MockRenderProcessHost(NULL)); 533 hosts.push_back(new MockRenderProcessHost(NULL));
523 534
524 // Create some extension instances and make sure they share a process. 535 // Create some extension instances and make sure they share a process.
525 scoped_refptr<SiteInstance> extension1_instance( 536 scoped_refptr<content::SiteInstance> extension1_instance(
526 CreateSiteInstance(&rph_factory, 537 CreateSiteInstance(&rph_factory,
527 GURL(kPrivilegedScheme + std::string("://foo/bar")))); 538 GURL(kPrivilegedScheme + std::string("://foo/bar"))));
528 set_privileged_process_id(extension1_instance->GetProcess()->GetID()); 539 set_privileged_process_id(extension1_instance->GetProcess()->GetID());
529 540
530 scoped_refptr<SiteInstance> extension2_instance( 541 scoped_refptr<content::SiteInstance> extension2_instance(
531 CreateSiteInstance(&rph_factory, 542 CreateSiteInstance(&rph_factory,
532 GURL(kPrivilegedScheme + std::string("://baz/bar")))); 543 GURL(kPrivilegedScheme + std::string("://baz/bar"))));
533 544
534 scoped_ptr<content::RenderProcessHost> extension_host( 545 scoped_ptr<content::RenderProcessHost> extension_host(
535 extension1_instance->GetProcess()); 546 extension1_instance->GetProcess());
536 EXPECT_EQ(extension1_instance->GetProcess(), 547 EXPECT_EQ(extension1_instance->GetProcess(),
537 extension2_instance->GetProcess()); 548 extension2_instance->GetProcess());
538 549
539 // Create some WebUI instances and make sure they share a process. 550 // Create some WebUI instances and make sure they share a process.
540 scoped_refptr<SiteInstance> webui1_instance(CreateSiteInstance(&rph_factory, 551 scoped_refptr<content::SiteInstance> webui1_instance(CreateSiteInstance(
552 &rph_factory,
541 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); 553 GURL(chrome::kChromeUIScheme + std::string("://newtab"))));
542 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID()); 554 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID());
543 555
544 scoped_refptr<SiteInstance> webui2_instance(CreateSiteInstance(&rph_factory, 556 scoped_refptr<content::SiteInstance> webui2_instance(CreateSiteInstance(
557 &rph_factory,
545 GURL(chrome::kChromeUIScheme + std::string("://history")))); 558 GURL(chrome::kChromeUIScheme + std::string("://history"))));
546 559
547 scoped_ptr<content::RenderProcessHost> dom_host( 560 scoped_ptr<content::RenderProcessHost> dom_host(
548 webui1_instance->GetProcess()); 561 webui1_instance->GetProcess());
549 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); 562 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess());
550 563
551 // Make sure none of differing privilege processes are mixed. 564 // Make sure none of differing privilege processes are mixed.
552 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); 565 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess());
553 566
554 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) { 567 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) {
555 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); 568 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]);
556 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); 569 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]);
557 } 570 }
558 571
559 STLDeleteContainerPointers(hosts.begin(), hosts.end()); 572 STLDeleteContainerPointers(hosts.begin(), hosts.end());
560 } 573 }
561 574
562 // Test to ensure that HasWrongProcessForURL behaves properly for different 575 // Test to ensure that HasWrongProcessForURL behaves properly for different
563 // types of URLs. 576 // types of URLs.
564 TEST_F(SiteInstanceTest, HasWrongProcessForURL) { 577 TEST_F(SiteInstanceTest, HasWrongProcessForURL) {
565 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 578 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
566 scoped_ptr<content::RenderProcessHost> host; 579 scoped_ptr<content::RenderProcessHost> host;
567 scoped_refptr<SiteInstance> instance( 580 scoped_refptr<content::SiteInstance> instance(
568 SiteInstance::CreateSiteInstance(browser_context.get())); 581 content::SiteInstance::CreateSiteInstance(browser_context.get()));
569 582
570 EXPECT_FALSE(instance->has_site()); 583 EXPECT_FALSE(instance->HasSite());
571 EXPECT_TRUE(instance->site().is_empty()); 584 EXPECT_TRUE(instance->GetSite().is_empty());
572 585
573 instance->SetSite(GURL("http://evernote.com/")); 586 instance->SetSite(GURL("http://evernote.com/"));
574 EXPECT_TRUE(instance->has_site()); 587 EXPECT_TRUE(instance->HasSite());
575 588
576 // Check prior to "assigning" a process to the instance, which is expected 589 // Check prior to "assigning" a process to the instance, which is expected
577 // to return false due to not being attached to any process yet. 590 // to return false due to not being attached to any process yet.
578 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); 591 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com")));
579 592
580 // The call to GetProcess actually creates a new real process, which works 593 // The call to GetProcess actually creates a new real process, which works
581 // fine, but might be a cause for problems in different contexts. 594 // fine, but might be a cause for problems in different contexts.
582 host.reset(instance->GetProcess()); 595 host.reset(instance->GetProcess());
583 EXPECT_TRUE(host.get() != NULL); 596 EXPECT_TRUE(host.get() != NULL);
584 EXPECT_TRUE(instance->HasProcess()); 597 EXPECT_TRUE(instance->HasProcess());
585 598
586 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); 599 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com")));
587 EXPECT_FALSE(instance->HasWrongProcessForURL( 600 EXPECT_FALSE(instance->HasWrongProcessForURL(
588 GURL("javascript:alert(document.location.href);"))); 601 GURL("javascript:alert(document.location.href);")));
589 602
590 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings"))); 603 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings")));
591 } 604 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698