1 // Copyright (c) 2012 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.
5 #include "base/command_line.h"
6 #include "base/compiler_specific.h"
7 #include "base/stl_util.h"
8 #include "base/string16.h"
9 #include "content/browser/browser_thread_impl.h"
10 #include "content/browser/browsing_instance.h"
11 #include "content/browser/child_process_security_policy_impl.h"
12 #include "content/browser/renderer_host/render_process_host_impl.h"
13 #include "content/browser/renderer_host/render_view_host_impl.h"
14 #include "content/browser/renderer_host/test_render_view_host.h"
15 #include "content/browser/site_instance_impl.h"
16 #include "content/browser/web_contents/navigation_entry_impl.h"
17 #include "content/browser/web_contents/web_contents_impl.h"
18 #include "content/public/browser/web_ui_controller_factory.h"
19 #include "content/public/common/content_client.h"
20 #include "content/public/common/content_constants.h"
21 #include "content/public/common/content_switches.h"
22 #include "content/public/common/url_constants.h"
23 #include "content/public/test/mock_render_process_host.h"
24 #include "content/public/test/test_browser_context.h"
25 #include "content/public/test/test_browser_thread.h"
26 #include "content/test/test_content_browser_client.h"
27 #include "content/test/test_content_client.h"
28 #include "googleurl/src/url_util.h"
29 #include "testing/gtest/include/gtest/gtest.h"
34 const char kSameAsAnyInstanceURL
[] = "about:internets";
36 const char kPrivilegedScheme
[] = "privileged";
38 class SiteInstanceTestWebUIControllerFactory
: public WebUIControllerFactory
{
40 virtual WebUIController
* CreateWebUIControllerForURL(
41 WebUI
* web_ui
, const GURL
& url
) const OVERRIDE
{
44 virtual WebUI::TypeID
GetWebUIType(BrowserContext
* browser_context
,
45 const GURL
& url
) const OVERRIDE
{
46 return WebUI::kNoWebUI
;
48 virtual bool UseWebUIForURL(BrowserContext
* browser_context
,
49 const GURL
& url
) const OVERRIDE
{
50 return GetContentClient()->HasWebUIScheme(url
);
52 virtual bool UseWebUIBindingsForURL(BrowserContext
* browser_context
,
53 const GURL
& url
) const OVERRIDE
{
54 return GetContentClient()->HasWebUIScheme(url
);
56 virtual bool IsURLAcceptableForWebUI(
57 BrowserContext
* browser_context
,
59 bool data_urls_allowed
) const OVERRIDE
{
64 class SiteInstanceTestClient
: public TestContentClient
{
66 SiteInstanceTestClient() {
69 virtual bool HasWebUIScheme(const GURL
& url
) const OVERRIDE
{
70 return url
.SchemeIs(chrome::kChromeUIScheme
);
74 class SiteInstanceTestBrowserClient
: public TestContentBrowserClient
{
76 SiteInstanceTestBrowserClient()
77 : privileged_process_id_(-1) {
80 virtual WebUIControllerFactory
* GetWebUIControllerFactory() OVERRIDE
{
84 virtual bool IsSuitableHost(RenderProcessHost
* process_host
,
85 const GURL
& site_url
) OVERRIDE
{
86 return (privileged_process_id_
== process_host
->GetID()) ==
87 site_url
.SchemeIs(kPrivilegedScheme
);
90 void set_privileged_process_id(int process_id
) {
91 privileged_process_id_
= process_id
;
95 SiteInstanceTestWebUIControllerFactory factory_
;
96 int privileged_process_id_
;
99 class SiteInstanceTest
: public testing::Test
{
102 : ui_thread_(BrowserThread::UI
, &message_loop_
),
103 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING
,
105 io_thread_(BrowserThread::IO
, &message_loop_
),
107 old_browser_client_(NULL
) {
110 virtual void SetUp() {
111 old_client_
= GetContentClient();
112 old_browser_client_
= GetContentClient()->browser();
113 SetContentClient(&client_
);
114 GetContentClient()->set_browser_for_testing(&browser_client_
);
115 url_util::AddStandardScheme(kPrivilegedScheme
);
116 url_util::AddStandardScheme(chrome::kChromeUIScheme
);
119 virtual void TearDown() {
120 // Ensure that no RenderProcessHosts are left over after the tests.
121 EXPECT_TRUE(RenderProcessHost::AllHostsIterator().IsAtEnd());
123 GetContentClient()->set_browser_for_testing(old_browser_client_
);
124 SetContentClient(old_client_
);
126 // http://crbug.com/143565 found SiteInstanceTest leaking an
127 // AppCacheDatabase. This happens because some part of the test indirectly
128 // calls StoragePartitionImplMap::PostCreateInitialization(), which posts
129 // a task to the IO thread to create the AppCacheDatabase. Since the
130 // message loop is not running, the AppCacheDatabase ends up getting
131 // created when DrainMessageLoops() gets called at the end of a test case.
132 // Immediately after, the test case ends and the AppCacheDatabase gets
133 // scheduled for deletion. Here, call DrainMessageLoops() again so the
134 // AppCacheDatabase actually gets deleted.
138 void set_privileged_process_id(int process_id
) {
139 browser_client_
.set_privileged_process_id(process_id
);
142 void DrainMessageLoops() {
143 // We don't just do this in TearDown() because we create TestBrowserContext
144 // objects in each test, which will be destructed before
145 // TearDown() is called.
146 MessageLoop::current()->RunUntilIdle();
147 message_loop_
.RunUntilIdle();
151 MessageLoopForUI message_loop_
;
152 TestBrowserThread ui_thread_
;
153 TestBrowserThread file_user_blocking_thread_
;
154 TestBrowserThread io_thread_
;
156 SiteInstanceTestClient client_
;
157 SiteInstanceTestBrowserClient browser_client_
;
158 ContentClient
* old_client_
;
159 ContentBrowserClient
* old_browser_client_
;
162 // Subclass of BrowsingInstance that updates a counter when deleted and
163 // returns TestSiteInstances from GetSiteInstanceForURL.
164 class TestBrowsingInstance
: public BrowsingInstance
{
166 TestBrowsingInstance(BrowserContext
* browser_context
, int* delete_counter
)
167 : BrowsingInstance(browser_context
),
168 delete_counter_(delete_counter
) {
171 // Make a few methods public for tests.
172 using BrowsingInstance::browser_context
;
173 using BrowsingInstance::HasSiteInstance
;
174 using BrowsingInstance::GetSiteInstanceForURL
;
175 using BrowsingInstance::RegisterSiteInstance
;
176 using BrowsingInstance::UnregisterSiteInstance
;
179 virtual ~TestBrowsingInstance() {
180 (*delete_counter_
)++;
183 int* delete_counter_
;
186 // Subclass of SiteInstanceImpl that updates a counter when deleted.
187 class TestSiteInstance
: public SiteInstanceImpl
{
189 static TestSiteInstance
* CreateTestSiteInstance(
190 BrowserContext
* browser_context
,
191 int* site_delete_counter
,
192 int* browsing_delete_counter
) {
193 TestBrowsingInstance
* browsing_instance
=
194 new TestBrowsingInstance(browser_context
, browsing_delete_counter
);
195 return new TestSiteInstance(browsing_instance
, site_delete_counter
);
199 TestSiteInstance(BrowsingInstance
* browsing_instance
, int* delete_counter
)
200 : SiteInstanceImpl(browsing_instance
), delete_counter_(delete_counter
) {}
201 virtual ~TestSiteInstance() {
202 (*delete_counter_
)++;
205 int* delete_counter_
;
210 // Test to ensure no memory leaks for SiteInstance objects.
211 TEST_F(SiteInstanceTest
, SiteInstanceDestructor
) {
212 // The existence of this object will cause WebContentsImpl to create our
213 // test one instead of the real one.
214 RenderViewHostTestEnabler rvh_test_enabler
;
215 int site_delete_counter
= 0;
216 int browsing_delete_counter
= 0;
217 const GURL
url("test:foo");
219 // Ensure that instances are deleted when their NavigationEntries are gone.
220 TestSiteInstance
* instance
=
221 TestSiteInstance::CreateTestSiteInstance(NULL
, &site_delete_counter
,
222 &browsing_delete_counter
);
223 EXPECT_EQ(0, site_delete_counter
);
225 NavigationEntryImpl
* e1
= new NavigationEntryImpl(
226 instance
, 0, url
, Referrer(), string16(), PAGE_TRANSITION_LINK
, false);
228 // Redundantly setting e1's SiteInstance shouldn't affect the ref count.
229 e1
->set_site_instance(instance
);
230 EXPECT_EQ(0, site_delete_counter
);
232 // Add a second reference
233 NavigationEntryImpl
* e2
= new NavigationEntryImpl(
234 instance
, 0, url
, Referrer(), string16(), PAGE_TRANSITION_LINK
, false);
236 // Now delete both entries and be sure the SiteInstance goes away.
238 EXPECT_EQ(0, site_delete_counter
);
239 EXPECT_EQ(0, browsing_delete_counter
);
241 EXPECT_EQ(1, site_delete_counter
);
242 // instance is now deleted
243 EXPECT_EQ(1, browsing_delete_counter
);
244 // browsing_instance is now deleted
246 // Ensure that instances are deleted when their RenderViewHosts are gone.
247 scoped_ptr
<TestBrowserContext
> browser_context(new TestBrowserContext());
249 TestSiteInstance::CreateTestSiteInstance(browser_context
.get(),
250 &site_delete_counter
,
251 &browsing_delete_counter
);
253 scoped_ptr
<WebContentsImpl
> web_contents(static_cast<WebContentsImpl
*>(
254 WebContents::Create(WebContents::CreateParams(
255 browser_context
.get(), instance
))));
256 EXPECT_EQ(1, site_delete_counter
);
257 EXPECT_EQ(1, browsing_delete_counter
);
260 // Make sure that we flush any messages related to the above WebContentsImpl
264 EXPECT_EQ(2, site_delete_counter
);
265 EXPECT_EQ(2, browsing_delete_counter
);
266 // contents is now deleted, along with instance and browsing_instance
269 // Test that NavigationEntries with SiteInstances can be cloned, but that their
270 // SiteInstances can be changed afterwards. Also tests that the ref counts are
271 // updated properly after the change.
272 TEST_F(SiteInstanceTest
, CloneNavigationEntry
) {
273 int site_delete_counter1
= 0;
274 int site_delete_counter2
= 0;
275 int browsing_delete_counter
= 0;
276 const GURL
url("test:foo");
278 SiteInstanceImpl
* instance1
=
279 TestSiteInstance::CreateTestSiteInstance(NULL
, &site_delete_counter1
,
280 &browsing_delete_counter
);
281 SiteInstanceImpl
* instance2
=
282 TestSiteInstance::CreateTestSiteInstance(NULL
, &site_delete_counter2
,
283 &browsing_delete_counter
);
285 NavigationEntryImpl
* e1
= new NavigationEntryImpl(
286 instance1
, 0, url
, Referrer(), string16(), PAGE_TRANSITION_LINK
, false);
288 NavigationEntryImpl
* e2
= new NavigationEntryImpl(*e1
);
290 // Should be able to change the SiteInstance of the cloned entry.
291 e2
->set_site_instance(instance2
);
293 // The first SiteInstance should go away after deleting e1, since e2 should
294 // no longer be referencing it.
296 EXPECT_EQ(1, site_delete_counter1
);
297 EXPECT_EQ(0, site_delete_counter2
);
299 // The second SiteInstance should go away after deleting e2.
301 EXPECT_EQ(1, site_delete_counter1
);
302 EXPECT_EQ(1, site_delete_counter2
);
304 // Both BrowsingInstances are also now deleted
305 EXPECT_EQ(2, browsing_delete_counter
);
310 // Test to ensure GetProcess returns and creates processes correctly.
311 TEST_F(SiteInstanceTest
, GetProcess
) {
312 // Ensure that GetProcess returns a process.
313 scoped_ptr
<TestBrowserContext
> browser_context(new TestBrowserContext());
314 scoped_ptr
<RenderProcessHost
> host1
;
315 scoped_refptr
<SiteInstanceImpl
> instance(static_cast<SiteInstanceImpl
*>(
316 SiteInstance::Create(browser_context
.get())));
317 host1
.reset(instance
->GetProcess());
318 EXPECT_TRUE(host1
.get() != NULL
);
320 // Ensure that GetProcess creates a new process.
321 scoped_refptr
<SiteInstanceImpl
> instance2(static_cast<SiteInstanceImpl
*>(
322 SiteInstance::Create(browser_context
.get())));
323 scoped_ptr
<RenderProcessHost
> host2(instance2
->GetProcess());
324 EXPECT_TRUE(host2
.get() != NULL
);
325 EXPECT_NE(host1
.get(), host2
.get());
330 // Test to ensure SetSite and site() work properly.
331 TEST_F(SiteInstanceTest
, SetSite
) {
332 scoped_refptr
<SiteInstanceImpl
> instance(static_cast<SiteInstanceImpl
*>(
333 SiteInstance::Create(NULL
)));
334 EXPECT_FALSE(instance
->HasSite());
335 EXPECT_TRUE(instance
->GetSiteURL().is_empty());
337 instance
->SetSite(GURL("http://www.google.com/index.html"));
338 EXPECT_EQ(GURL("http://google.com"), instance
->GetSiteURL());
340 EXPECT_TRUE(instance
->HasSite());
345 // Test to ensure GetSiteForURL properly returns sites for URLs.
346 TEST_F(SiteInstanceTest
, GetSiteForURL
) {
347 // Pages are irrelevant.
348 GURL test_url
= GURL("http://www.google.com/index.html");
349 EXPECT_EQ(GURL("http://google.com"),
350 SiteInstanceImpl::GetSiteForURL(NULL
, test_url
));
352 // Ports are irrlevant.
353 test_url
= GURL("https://www.google.com:8080");
354 EXPECT_EQ(GURL("https://google.com"),
355 SiteInstanceImpl::GetSiteForURL(NULL
, test_url
));
357 // Javascript URLs have no site.
358 test_url
= GURL("javascript:foo();");
359 EXPECT_EQ(GURL(), SiteInstanceImpl::GetSiteForURL(NULL
, test_url
));
361 test_url
= GURL("http://foo/a.html");
362 EXPECT_EQ(GURL("http://foo"), SiteInstanceImpl::GetSiteForURL(
365 test_url
= GURL("file:///C:/Downloads/");
366 EXPECT_EQ(GURL(), SiteInstanceImpl::GetSiteForURL(NULL
, test_url
));
368 std::string
guest_url(chrome::kGuestScheme
);
369 guest_url
.append("://abc123");
370 test_url
= GURL(guest_url
);
371 EXPECT_EQ(test_url
, SiteInstanceImpl::GetSiteForURL(NULL
, test_url
));
373 // TODO(creis): Do we want to special case file URLs to ensure they have
374 // either no site or a special "file://" site? We currently return
375 // "file://home/" as the site, which seems broken.
376 // test_url = GURL("file://home/");
377 // EXPECT_EQ(GURL(), SiteInstanceImpl::GetSiteForURL(NULL, test_url));
382 // Test of distinguishing URLs from different sites. Most of this logic is
383 // tested in RegistryControlledDomainTest. This test focuses on URLs with
384 // different schemes or ports.
385 TEST_F(SiteInstanceTest
, IsSameWebSite
) {
386 GURL url_foo
= GURL("http://foo/a.html");
387 GURL url_foo2
= GURL("http://foo/b.html");
388 GURL url_foo_https
= GURL("https://foo/a.html");
389 GURL url_foo_port
= GURL("http://foo:8080/a.html");
390 GURL url_javascript
= GURL("javascript:alert(1);");
392 // Same scheme and port -> same site.
393 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL
, url_foo
, url_foo2
));
395 // Different scheme -> different site.
396 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL
, url_foo
, url_foo_https
));
398 // Different port -> same site.
399 // (Changes to document.domain make renderer ignore the port.)
400 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL
, url_foo
, url_foo_port
));
402 // JavaScript links should be considered same site for anything.
403 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL
, url_javascript
, url_foo
));
404 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL
, url_javascript
, url_foo_https
));
405 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL
, url_javascript
, url_foo_port
));
410 // Test to ensure that there is only one SiteInstance per site in a given
411 // BrowsingInstance, when process-per-site is not in use.
412 TEST_F(SiteInstanceTest
, OneSiteInstancePerSite
) {
413 ASSERT_FALSE(CommandLine::ForCurrentProcess()->HasSwitch(
414 switches::kProcessPerSite
));
415 int delete_counter
= 0;
416 scoped_ptr
<TestBrowserContext
> browser_context(new TestBrowserContext());
417 TestBrowsingInstance
* browsing_instance
=
418 new TestBrowsingInstance(browser_context
.get(), &delete_counter
);
420 const GURL
url_a1("http://www.google.com/1.html");
421 scoped_refptr
<SiteInstanceImpl
> site_instance_a1(
422 static_cast<SiteInstanceImpl
*>(
423 browsing_instance
->GetSiteInstanceForURL(url_a1
)));
424 EXPECT_TRUE(site_instance_a1
.get() != NULL
);
426 // A separate site should create a separate SiteInstance.
427 const GURL
url_b1("http://www.yahoo.com/");
428 scoped_refptr
<SiteInstanceImpl
> site_instance_b1(
429 static_cast<SiteInstanceImpl
*>(
430 browsing_instance
->GetSiteInstanceForURL(url_b1
)));
431 EXPECT_NE(site_instance_a1
.get(), site_instance_b1
.get());
432 EXPECT_TRUE(site_instance_a1
->IsRelatedSiteInstance(site_instance_b1
));
434 // Getting the new SiteInstance from the BrowsingInstance and from another
435 // SiteInstance in the BrowsingInstance should give the same result.
436 EXPECT_EQ(site_instance_b1
.get(),
437 site_instance_a1
->GetRelatedSiteInstance(url_b1
));
439 // A second visit to the original site should return the same SiteInstance.
440 const GURL
url_a2("http://www.google.com/2.html");
441 EXPECT_EQ(site_instance_a1
.get(),
442 browsing_instance
->GetSiteInstanceForURL(url_a2
));
443 EXPECT_EQ(site_instance_a1
.get(),
444 site_instance_a1
->GetRelatedSiteInstance(url_a2
));
446 // A visit to the original site in a new BrowsingInstance (same or different
447 // browser context) should return a different SiteInstance.
448 TestBrowsingInstance
* browsing_instance2
=
449 new TestBrowsingInstance(browser_context
.get(), &delete_counter
);
450 // Ensure the new SiteInstance is ref counted so that it gets deleted.
451 scoped_refptr
<SiteInstanceImpl
> site_instance_a2_2(
452 static_cast<SiteInstanceImpl
*>(
453 browsing_instance2
->GetSiteInstanceForURL(url_a2
)));
454 EXPECT_NE(site_instance_a1
.get(), site_instance_a2_2
.get());
455 EXPECT_FALSE(site_instance_a1
->IsRelatedSiteInstance(site_instance_a2_2
));
457 // The two SiteInstances for http://google.com should not use the same process
458 // if process-per-site is not enabled.
459 scoped_ptr
<RenderProcessHost
> process_a1(site_instance_a1
->GetProcess());
460 scoped_ptr
<RenderProcessHost
> process_a2_2(site_instance_a2_2
->GetProcess());
461 EXPECT_NE(process_a1
.get(), process_a2_2
.get());
463 // Should be able to see that we do have SiteInstances.
464 EXPECT_TRUE(browsing_instance
->HasSiteInstance(
465 GURL("http://mail.google.com")));
466 EXPECT_TRUE(browsing_instance2
->HasSiteInstance(
467 GURL("http://mail.google.com")));
468 EXPECT_TRUE(browsing_instance
->HasSiteInstance(
469 GURL("http://mail.yahoo.com")));
471 // Should be able to see that we don't have SiteInstances.
472 EXPECT_FALSE(browsing_instance
->HasSiteInstance(
473 GURL("https://www.google.com")));
474 EXPECT_FALSE(browsing_instance2
->HasSiteInstance(
475 GURL("http://www.yahoo.com")));
477 // browsing_instances will be deleted when their SiteInstances are deleted.
478 // The processes will be unregistered when the RPH scoped_ptrs go away.
483 // Test to ensure that there is only one RenderProcessHost per site for an
484 // entire BrowserContext, if process-per-site is in use.
485 TEST_F(SiteInstanceTest
, OneSiteInstancePerSiteInBrowserContext
) {
486 CommandLine::ForCurrentProcess()->AppendSwitch(
487 switches::kProcessPerSite
);
488 int delete_counter
= 0;
489 scoped_ptr
<TestBrowserContext
> browser_context(new TestBrowserContext());
490 TestBrowsingInstance
* browsing_instance
=
491 new TestBrowsingInstance(browser_context
.get(), &delete_counter
);
493 const GURL
url_a1("http://www.google.com/1.html");
494 scoped_refptr
<SiteInstanceImpl
> site_instance_a1(
495 static_cast<SiteInstanceImpl
*>(
496 browsing_instance
->GetSiteInstanceForURL(url_a1
)));
497 EXPECT_TRUE(site_instance_a1
.get() != NULL
);
498 scoped_ptr
<RenderProcessHost
> process_a1(site_instance_a1
->GetProcess());
500 // A separate site should create a separate SiteInstance.
501 const GURL
url_b1("http://www.yahoo.com/");
502 scoped_refptr
<SiteInstanceImpl
> site_instance_b1(
503 static_cast<SiteInstanceImpl
*>(
504 browsing_instance
->GetSiteInstanceForURL(url_b1
)));
505 EXPECT_NE(site_instance_a1
.get(), site_instance_b1
.get());
506 EXPECT_TRUE(site_instance_a1
->IsRelatedSiteInstance(site_instance_b1
));
508 // Getting the new SiteInstance from the BrowsingInstance and from another
509 // SiteInstance in the BrowsingInstance should give the same result.
510 EXPECT_EQ(site_instance_b1
.get(),
511 site_instance_a1
->GetRelatedSiteInstance(url_b1
));
513 // A second visit to the original site should return the same SiteInstance.
514 const GURL
url_a2("http://www.google.com/2.html");
515 EXPECT_EQ(site_instance_a1
.get(),
516 browsing_instance
->GetSiteInstanceForURL(url_a2
));
517 EXPECT_EQ(site_instance_a1
.get(),
518 site_instance_a1
->GetRelatedSiteInstance(url_a2
));
520 // A visit to the original site in a new BrowsingInstance (same browser
521 // context) should return a different SiteInstance with the same process.
522 TestBrowsingInstance
* browsing_instance2
=
523 new TestBrowsingInstance(browser_context
.get(), &delete_counter
);
524 scoped_refptr
<SiteInstanceImpl
> site_instance_a1_2(
525 static_cast<SiteInstanceImpl
*>(
526 browsing_instance2
->GetSiteInstanceForURL(url_a1
)));
527 EXPECT_TRUE(site_instance_a1
.get() != NULL
);
528 EXPECT_NE(site_instance_a1
.get(), site_instance_a1_2
.get());
529 EXPECT_EQ(process_a1
.get(), site_instance_a1_2
->GetProcess());
531 // A visit to the original site in a new BrowsingInstance (different browser
532 // context) should return a different SiteInstance with a different process.
533 scoped_ptr
<TestBrowserContext
> browser_context2(new TestBrowserContext());
534 TestBrowsingInstance
* browsing_instance3
=
535 new TestBrowsingInstance(browser_context2
.get(), &delete_counter
);
536 scoped_refptr
<SiteInstanceImpl
> site_instance_a2_3(
537 static_cast<SiteInstanceImpl
*>(
538 browsing_instance3
->GetSiteInstanceForURL(url_a2
)));
539 EXPECT_TRUE(site_instance_a2_3
.get() != NULL
);
540 scoped_ptr
<RenderProcessHost
> process_a2_3(site_instance_a2_3
->GetProcess());
541 EXPECT_NE(site_instance_a1
.get(), site_instance_a2_3
.get());
542 EXPECT_NE(process_a1
.get(), process_a2_3
.get());
544 // Should be able to see that we do have SiteInstances.
545 EXPECT_TRUE(browsing_instance
->HasSiteInstance(
546 GURL("http://mail.google.com"))); // visited before
547 EXPECT_TRUE(browsing_instance2
->HasSiteInstance(
548 GURL("http://mail.google.com"))); // visited before
549 EXPECT_TRUE(browsing_instance
->HasSiteInstance(
550 GURL("http://mail.yahoo.com"))); // visited before
552 // Should be able to see that we don't have SiteInstances.
553 EXPECT_FALSE(browsing_instance2
->HasSiteInstance(
554 GURL("http://www.yahoo.com"))); // different BI, same browser context
555 EXPECT_FALSE(browsing_instance
->HasSiteInstance(
556 GURL("https://www.google.com"))); // not visited before
557 EXPECT_FALSE(browsing_instance3
->HasSiteInstance(
558 GURL("http://www.yahoo.com"))); // different BI, different context
560 // browsing_instances will be deleted when their SiteInstances are deleted.
561 // The processes will be unregistered when the RPH scoped_ptrs go away.
566 static SiteInstanceImpl
* CreateSiteInstance(
567 BrowserContext
* browser_context
,
568 RenderProcessHostFactory
* factory
,
570 SiteInstanceImpl
* instance
=
571 reinterpret_cast<SiteInstanceImpl
*>(
572 SiteInstance::CreateForURL(browser_context
, url
));
573 instance
->set_render_process_host_factory(factory
);
577 // Test to ensure that pages that require certain privileges are grouped
578 // in processes with similar pages.
579 TEST_F(SiteInstanceTest
, ProcessSharingByType
) {
580 MockRenderProcessHostFactory rph_factory
;
581 ChildProcessSecurityPolicyImpl
* policy
=
582 ChildProcessSecurityPolicyImpl::GetInstance();
584 // Make a bunch of mock renderers so that we hit the limit.
585 scoped_ptr
<TestBrowserContext
> browser_context(new TestBrowserContext());
586 std::vector
<MockRenderProcessHost
*> hosts
;
587 for (size_t i
= 0; i
< kMaxRendererProcessCount
; ++i
)
588 hosts
.push_back(new MockRenderProcessHost(browser_context
.get()));
590 // Create some extension instances and make sure they share a process.
591 scoped_refptr
<SiteInstanceImpl
> extension1_instance(
592 CreateSiteInstance(browser_context
.get(), &rph_factory
,
593 GURL(kPrivilegedScheme
+ std::string("://foo/bar"))));
594 set_privileged_process_id(extension1_instance
->GetProcess()->GetID());
596 scoped_refptr
<SiteInstanceImpl
> extension2_instance(
597 CreateSiteInstance(browser_context
.get(), &rph_factory
,
598 GURL(kPrivilegedScheme
+ std::string("://baz/bar"))));
600 scoped_ptr
<RenderProcessHost
> extension_host(
601 extension1_instance
->GetProcess());
602 EXPECT_EQ(extension1_instance
->GetProcess(),
603 extension2_instance
->GetProcess());
605 // Create some WebUI instances and make sure they share a process.
606 scoped_refptr
<SiteInstanceImpl
> webui1_instance(CreateSiteInstance(
607 browser_context
.get(), &rph_factory
,
608 GURL(chrome::kChromeUIScheme
+ std::string("://newtab"))));
609 policy
->GrantWebUIBindings(webui1_instance
->GetProcess()->GetID());
611 scoped_refptr
<SiteInstanceImpl
> webui2_instance(CreateSiteInstance(
612 browser_context
.get(), &rph_factory
,
613 GURL(chrome::kChromeUIScheme
+ std::string("://history"))));
615 scoped_ptr
<RenderProcessHost
> dom_host(webui1_instance
->GetProcess());
616 EXPECT_EQ(webui1_instance
->GetProcess(), webui2_instance
->GetProcess());
618 // Make sure none of differing privilege processes are mixed.
619 EXPECT_NE(extension1_instance
->GetProcess(), webui1_instance
->GetProcess());
621 for (size_t i
= 0; i
< kMaxRendererProcessCount
; ++i
) {
622 EXPECT_NE(extension1_instance
->GetProcess(), hosts
[i
]);
623 EXPECT_NE(webui1_instance
->GetProcess(), hosts
[i
]);
626 STLDeleteContainerPointers(hosts
.begin(), hosts
.end());
631 // Test to ensure that HasWrongProcessForURL behaves properly for different
633 TEST_F(SiteInstanceTest
, HasWrongProcessForURL
) {
634 scoped_ptr
<TestBrowserContext
> browser_context(new TestBrowserContext());
635 scoped_ptr
<RenderProcessHost
> host
;
636 scoped_refptr
<SiteInstanceImpl
> instance(static_cast<SiteInstanceImpl
*>(
637 SiteInstance::Create(browser_context
.get())));
639 EXPECT_FALSE(instance
->HasSite());
640 EXPECT_TRUE(instance
->GetSiteURL().is_empty());
642 instance
->SetSite(GURL("http://evernote.com/"));
643 EXPECT_TRUE(instance
->HasSite());
645 // Check prior to "assigning" a process to the instance, which is expected
646 // to return false due to not being attached to any process yet.
647 EXPECT_FALSE(instance
->HasWrongProcessForURL(GURL("http://google.com")));
649 // The call to GetProcess actually creates a new real process, which works
650 // fine, but might be a cause for problems in different contexts.
651 host
.reset(instance
->GetProcess());
652 EXPECT_TRUE(host
.get() != NULL
);
653 EXPECT_TRUE(instance
->HasProcess());
655 EXPECT_FALSE(instance
->HasWrongProcessForURL(GURL("http://evernote.com")));
656 EXPECT_FALSE(instance
->HasWrongProcessForURL(
657 GURL("javascript:alert(document.location.href);")));
659 EXPECT_TRUE(instance
->HasWrongProcessForURL(GURL("chrome://settings")));
661 // Test that WebUI SiteInstances reject normal web URLs.
662 const GURL
webui_url("chrome://settings");
663 scoped_refptr
<SiteInstanceImpl
> webui_instance(static_cast<SiteInstanceImpl
*>(
664 SiteInstance::Create(browser_context
.get())));
665 webui_instance
->SetSite(webui_url
);
666 scoped_ptr
<RenderProcessHost
> webui_host(webui_instance
->GetProcess());
668 // Simulate granting WebUI bindings for the process.
669 ChildProcessSecurityPolicyImpl::GetInstance()->GrantWebUIBindings(
670 webui_host
->GetID());
672 EXPECT_TRUE(webui_instance
->HasProcess());
673 EXPECT_FALSE(webui_instance
->HasWrongProcessForURL(webui_url
));
674 EXPECT_TRUE(webui_instance
->HasWrongProcessForURL(GURL("http://google.com")));
676 // WebUI uses process-per-site, so another instance will use the same process
677 // even if we haven't called GetProcess yet. Make sure HasWrongProcessForURL
678 // doesn't crash (http://crbug.com/137070).
679 scoped_refptr
<SiteInstanceImpl
> webui_instance2(
680 static_cast<SiteInstanceImpl
*>(
681 SiteInstance::Create(browser_context
.get())));
682 webui_instance2
->SetSite(webui_url
);
683 EXPECT_FALSE(webui_instance2
->HasWrongProcessForURL(webui_url
));
685 webui_instance2
->HasWrongProcessForURL(GURL("http://google.com")));
690 // Test to ensure that HasWrongProcessForURL behaves properly even when
691 // --site-per-process is used (http://crbug.com/160671).
692 TEST_F(SiteInstanceTest
, HasWrongProcessForURLInSitePerProcess
) {
693 CommandLine::ForCurrentProcess()->AppendSwitch(
694 switches::kSitePerProcess
);
696 scoped_ptr
<TestBrowserContext
> browser_context(new TestBrowserContext());
697 scoped_ptr
<RenderProcessHost
> host
;
698 scoped_refptr
<SiteInstanceImpl
> instance(static_cast<SiteInstanceImpl
*>(
699 SiteInstance::Create(browser_context
.get())));
701 instance
->SetSite(GURL("http://evernote.com/"));
702 EXPECT_TRUE(instance
->HasSite());
704 // Check prior to "assigning" a process to the instance, which is expected
705 // to return false due to not being attached to any process yet.
706 EXPECT_FALSE(instance
->HasWrongProcessForURL(GURL("http://google.com")));
708 // The call to GetProcess actually creates a new real process, which works
709 // fine, but might be a cause for problems in different contexts.
710 host
.reset(instance
->GetProcess());
711 EXPECT_TRUE(host
.get() != NULL
);
712 EXPECT_TRUE(instance
->HasProcess());
714 EXPECT_FALSE(instance
->HasWrongProcessForURL(GURL("http://evernote.com")));
715 EXPECT_FALSE(instance
->HasWrongProcessForURL(
716 GURL("javascript:alert(document.location.href);")));
718 EXPECT_TRUE(instance
->HasWrongProcessForURL(GURL("chrome://settings")));
723 } // namespace content