[Offline pages] Moving browser code to android path
[chromium-blink-merge.git] / content / browser / site_instance_impl_unittest.cc
blob7267449eda77b4fcdd85345ceae1fb2f0ab3fb36
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/memory/scoped_vector.h"
8 #include "base/strings/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/frame_host/navigation_entry_impl.h"
13 #include "content/browser/renderer_host/render_process_host_impl.h"
14 #include "content/browser/renderer_host/render_view_host_impl.h"
15 #include "content/browser/site_instance_impl.h"
16 #include "content/browser/web_contents/web_contents_impl.h"
17 #include "content/browser/webui/content_web_ui_controller_factory.h"
18 #include "content/browser/webui/web_ui_controller_factory_registry.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/common/url_utils.h"
24 #include "content/public/test/mock_render_process_host.h"
25 #include "content/public/test/test_browser_context.h"
26 #include "content/public/test/test_browser_thread.h"
27 #include "content/test/test_content_browser_client.h"
28 #include "content/test/test_content_client.h"
29 #include "content/test/test_render_view_host.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #include "url/url_util.h"
33 namespace content {
34 namespace {
36 const char kPrivilegedScheme[] = "privileged";
38 class SiteInstanceTestBrowserClient : public TestContentBrowserClient {
39 public:
40 SiteInstanceTestBrowserClient()
41 : privileged_process_id_(-1) {
42 WebUIControllerFactory::RegisterFactory(
43 ContentWebUIControllerFactory::GetInstance());
46 ~SiteInstanceTestBrowserClient() override {
47 WebUIControllerFactory::UnregisterFactoryForTesting(
48 ContentWebUIControllerFactory::GetInstance());
51 bool IsSuitableHost(RenderProcessHost* process_host,
52 const GURL& site_url) override {
53 return (privileged_process_id_ == process_host->GetID()) ==
54 site_url.SchemeIs(kPrivilegedScheme);
57 void set_privileged_process_id(int process_id) {
58 privileged_process_id_ = process_id;
61 private:
62 int privileged_process_id_;
65 class SiteInstanceTest : public testing::Test {
66 public:
67 SiteInstanceTest()
68 : ui_thread_(BrowserThread::UI, &message_loop_),
69 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING,
70 &message_loop_),
71 io_thread_(BrowserThread::IO, &message_loop_),
72 old_browser_client_(NULL) {
75 void SetUp() override {
76 old_browser_client_ = SetBrowserClientForTesting(&browser_client_);
77 url::AddStandardScheme(kPrivilegedScheme);
78 url::AddStandardScheme(kChromeUIScheme);
80 SiteInstanceImpl::set_render_process_host_factory(&rph_factory_);
83 void TearDown() override {
84 // Ensure that no RenderProcessHosts are left over after the tests.
85 EXPECT_TRUE(RenderProcessHost::AllHostsIterator().IsAtEnd());
87 SetBrowserClientForTesting(old_browser_client_);
88 SiteInstanceImpl::set_render_process_host_factory(NULL);
90 // http://crbug.com/143565 found SiteInstanceTest leaking an
91 // AppCacheDatabase. This happens because some part of the test indirectly
92 // calls StoragePartitionImplMap::PostCreateInitialization(), which posts
93 // a task to the IO thread to create the AppCacheDatabase. Since the
94 // message loop is not running, the AppCacheDatabase ends up getting
95 // created when DrainMessageLoops() gets called at the end of a test case.
96 // Immediately after, the test case ends and the AppCacheDatabase gets
97 // scheduled for deletion. Here, call DrainMessageLoops() again so the
98 // AppCacheDatabase actually gets deleted.
99 DrainMessageLoops();
102 void set_privileged_process_id(int process_id) {
103 browser_client_.set_privileged_process_id(process_id);
106 void DrainMessageLoops() {
107 // We don't just do this in TearDown() because we create TestBrowserContext
108 // objects in each test, which will be destructed before
109 // TearDown() is called.
110 base::MessageLoop::current()->RunUntilIdle();
111 message_loop_.RunUntilIdle();
114 private:
115 base::MessageLoopForUI message_loop_;
116 TestBrowserThread ui_thread_;
117 TestBrowserThread file_user_blocking_thread_;
118 TestBrowserThread io_thread_;
120 SiteInstanceTestBrowserClient browser_client_;
121 ContentBrowserClient* old_browser_client_;
122 MockRenderProcessHostFactory rph_factory_;
125 // Subclass of BrowsingInstance that updates a counter when deleted and
126 // returns TestSiteInstances from GetSiteInstanceForURL.
127 class TestBrowsingInstance : public BrowsingInstance {
128 public:
129 TestBrowsingInstance(BrowserContext* browser_context, int* delete_counter)
130 : BrowsingInstance(browser_context),
131 delete_counter_(delete_counter) {
134 // Make a few methods public for tests.
135 using BrowsingInstance::browser_context;
136 using BrowsingInstance::HasSiteInstance;
137 using BrowsingInstance::GetSiteInstanceForURL;
138 using BrowsingInstance::RegisterSiteInstance;
139 using BrowsingInstance::UnregisterSiteInstance;
141 private:
142 ~TestBrowsingInstance() override { (*delete_counter_)++; }
144 int* delete_counter_;
147 // Subclass of SiteInstanceImpl that updates a counter when deleted.
148 class TestSiteInstance : public SiteInstanceImpl {
149 public:
150 static TestSiteInstance* CreateTestSiteInstance(
151 BrowserContext* browser_context,
152 int* site_delete_counter,
153 int* browsing_delete_counter) {
154 TestBrowsingInstance* browsing_instance =
155 new TestBrowsingInstance(browser_context, browsing_delete_counter);
156 return new TestSiteInstance(browsing_instance, site_delete_counter);
159 private:
160 TestSiteInstance(BrowsingInstance* browsing_instance, int* delete_counter)
161 : SiteInstanceImpl(browsing_instance), delete_counter_(delete_counter) {}
162 ~TestSiteInstance() override { (*delete_counter_)++; }
164 int* delete_counter_;
167 } // namespace
169 // Test to ensure no memory leaks for SiteInstance objects.
170 TEST_F(SiteInstanceTest, SiteInstanceDestructor) {
171 // The existence of this object will cause WebContentsImpl to create our
172 // test one instead of the real one.
173 RenderViewHostTestEnabler rvh_test_enabler;
174 int site_delete_counter = 0;
175 int browsing_delete_counter = 0;
176 const GURL url("test:foo");
178 // Ensure that instances are deleted when their NavigationEntries are gone.
179 TestSiteInstance* instance =
180 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter,
181 &browsing_delete_counter);
182 EXPECT_EQ(0, site_delete_counter);
184 NavigationEntryImpl* e1 = new NavigationEntryImpl(
185 instance, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK,
186 false);
188 // Redundantly setting e1's SiteInstance shouldn't affect the ref count.
189 e1->set_site_instance(instance);
190 EXPECT_EQ(0, site_delete_counter);
192 // Add a second reference
193 NavigationEntryImpl* e2 = new NavigationEntryImpl(
194 instance, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK,
195 false);
197 // Now delete both entries and be sure the SiteInstance goes away.
198 delete e1;
199 EXPECT_EQ(0, site_delete_counter);
200 EXPECT_EQ(0, browsing_delete_counter);
201 delete e2;
202 EXPECT_EQ(1, site_delete_counter);
203 // instance is now deleted
204 EXPECT_EQ(1, browsing_delete_counter);
205 // browsing_instance is now deleted
207 // Ensure that instances are deleted when their RenderViewHosts are gone.
208 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
209 instance =
210 TestSiteInstance::CreateTestSiteInstance(browser_context.get(),
211 &site_delete_counter,
212 &browsing_delete_counter);
214 scoped_ptr<WebContentsImpl> web_contents(static_cast<WebContentsImpl*>(
215 WebContents::Create(WebContents::CreateParams(
216 browser_context.get(), instance))));
217 EXPECT_EQ(1, site_delete_counter);
218 EXPECT_EQ(1, browsing_delete_counter);
221 // Make sure that we flush any messages related to the above WebContentsImpl
222 // destruction.
223 DrainMessageLoops();
225 EXPECT_EQ(2, site_delete_counter);
226 EXPECT_EQ(2, browsing_delete_counter);
227 // contents is now deleted, along with instance and browsing_instance
230 // Test that NavigationEntries with SiteInstances can be cloned, but that their
231 // SiteInstances can be changed afterwards. Also tests that the ref counts are
232 // updated properly after the change.
233 TEST_F(SiteInstanceTest, CloneNavigationEntry) {
234 int site_delete_counter1 = 0;
235 int site_delete_counter2 = 0;
236 int browsing_delete_counter = 0;
237 const GURL url("test:foo");
239 SiteInstanceImpl* instance1 =
240 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter1,
241 &browsing_delete_counter);
242 SiteInstanceImpl* instance2 =
243 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter2,
244 &browsing_delete_counter);
246 scoped_ptr<NavigationEntryImpl> e1 = make_scoped_ptr(new NavigationEntryImpl(
247 instance1, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK,
248 false));
249 // Clone the entry.
250 scoped_ptr<NavigationEntryImpl> e2 = e1->Clone();
252 // Should be able to change the SiteInstance of the cloned entry.
253 e2->set_site_instance(instance2);
255 // The first SiteInstance should go away after resetting e1, since e2 should
256 // no longer be referencing it.
257 e1.reset();
258 EXPECT_EQ(1, site_delete_counter1);
259 EXPECT_EQ(0, site_delete_counter2);
261 // The second SiteInstance should go away after resetting e2.
262 e2.reset();
263 EXPECT_EQ(1, site_delete_counter1);
264 EXPECT_EQ(1, site_delete_counter2);
266 // Both BrowsingInstances are also now deleted.
267 EXPECT_EQ(2, browsing_delete_counter);
269 DrainMessageLoops();
272 // Test to ensure GetProcess returns and creates processes correctly.
273 TEST_F(SiteInstanceTest, GetProcess) {
274 // Ensure that GetProcess returns a process.
275 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
276 scoped_ptr<RenderProcessHost> host1;
277 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
278 SiteInstance::Create(browser_context.get())));
279 host1.reset(instance->GetProcess());
280 EXPECT_TRUE(host1.get() != NULL);
282 // Ensure that GetProcess creates a new process.
283 scoped_refptr<SiteInstanceImpl> instance2(static_cast<SiteInstanceImpl*>(
284 SiteInstance::Create(browser_context.get())));
285 scoped_ptr<RenderProcessHost> host2(instance2->GetProcess());
286 EXPECT_TRUE(host2.get() != NULL);
287 EXPECT_NE(host1.get(), host2.get());
289 DrainMessageLoops();
292 // Test to ensure SetSite and site() work properly.
293 TEST_F(SiteInstanceTest, SetSite) {
294 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
295 SiteInstance::Create(NULL)));
296 EXPECT_FALSE(instance->HasSite());
297 EXPECT_TRUE(instance->GetSiteURL().is_empty());
299 instance->SetSite(GURL("http://www.google.com/index.html"));
300 EXPECT_EQ(GURL("http://google.com"), instance->GetSiteURL());
302 EXPECT_TRUE(instance->HasSite());
304 DrainMessageLoops();
307 // Test to ensure GetSiteForURL properly returns sites for URLs.
308 TEST_F(SiteInstanceTest, GetSiteForURL) {
309 // Pages are irrelevant.
310 GURL test_url = GURL("http://www.google.com/index.html");
311 GURL site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
312 EXPECT_EQ(GURL("http://google.com"), site_url);
313 EXPECT_EQ("http", site_url.scheme());
314 EXPECT_EQ("google.com", site_url.host());
316 // Ports are irrlevant.
317 test_url = GURL("https://www.google.com:8080");
318 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
319 EXPECT_EQ(GURL("https://google.com"), site_url);
321 // Hostnames without TLDs are ok.
322 test_url = GURL("http://foo/a.html");
323 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
324 EXPECT_EQ(GURL("http://foo"), site_url);
325 EXPECT_EQ("foo", site_url.host());
327 // File URLs should include the scheme.
328 test_url = GURL("file:///C:/Downloads/");
329 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
330 EXPECT_EQ(GURL("file:"), site_url);
331 EXPECT_EQ("file", site_url.scheme());
332 EXPECT_FALSE(site_url.has_host());
334 // Some file URLs have hosts in the path.
335 test_url = GURL("file://server/path");
336 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
337 EXPECT_EQ(GURL("file://server"), site_url);
338 EXPECT_EQ("server", site_url.host());
340 // Data URLs should include the scheme.
341 test_url = GURL("data:text/html,foo");
342 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
343 EXPECT_EQ(GURL("data:"), site_url);
344 EXPECT_EQ("data", site_url.scheme());
345 EXPECT_FALSE(site_url.has_host());
347 // Javascript URLs should include the scheme.
348 test_url = GURL("javascript:foo();");
349 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
350 EXPECT_EQ(GURL("javascript:"), site_url);
351 EXPECT_EQ("javascript", site_url.scheme());
352 EXPECT_FALSE(site_url.has_host());
354 // Guest URLs are special and need to have the path in the site as well,
355 // since it affects the StoragePartition configuration.
356 std::string guest_url(kGuestScheme);
357 guest_url.append("://abc123/path");
358 test_url = GURL(guest_url);
359 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
360 EXPECT_EQ(test_url, site_url);
362 DrainMessageLoops();
365 // Test of distinguishing URLs from different sites. Most of this logic is
366 // tested in RegistryControlledDomainTest. This test focuses on URLs with
367 // different schemes or ports.
368 TEST_F(SiteInstanceTest, IsSameWebSite) {
369 GURL url_foo = GURL("http://foo/a.html");
370 GURL url_foo2 = GURL("http://foo/b.html");
371 GURL url_foo_https = GURL("https://foo/a.html");
372 GURL url_foo_port = GURL("http://foo:8080/a.html");
373 GURL url_javascript = GURL("javascript:alert(1);");
374 GURL url_blank = GURL(url::kAboutBlankURL);
376 // Same scheme and port -> same site.
377 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo2));
379 // Different scheme -> different site.
380 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_https));
382 // Different port -> same site.
383 // (Changes to document.domain make renderer ignore the port.)
384 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_port));
386 // JavaScript links should be considered same site for anything.
387 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo));
388 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_https));
389 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_port));
391 // Navigating to a blank page is considered the same site.
392 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_blank));
393 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo_https, url_blank));
394 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo_port, url_blank));
396 // Navigating from a blank site is not considered to be the same site.
397 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_blank, url_foo));
398 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_blank, url_foo_https));
399 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_blank, url_foo_port));
401 DrainMessageLoops();
404 // Test to ensure that there is only one SiteInstance per site in a given
405 // BrowsingInstance, when process-per-site is not in use.
406 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) {
407 ASSERT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch(
408 switches::kProcessPerSite));
409 int delete_counter = 0;
410 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
411 TestBrowsingInstance* browsing_instance =
412 new TestBrowsingInstance(browser_context.get(), &delete_counter);
414 const GURL url_a1("http://www.google.com/1.html");
415 scoped_refptr<SiteInstanceImpl> site_instance_a1(
416 static_cast<SiteInstanceImpl*>(
417 browsing_instance->GetSiteInstanceForURL(url_a1)));
418 EXPECT_TRUE(site_instance_a1.get() != NULL);
420 // A separate site should create a separate SiteInstance.
421 const GURL url_b1("http://www.yahoo.com/");
422 scoped_refptr<SiteInstanceImpl> site_instance_b1(
423 static_cast<SiteInstanceImpl*>(
424 browsing_instance->GetSiteInstanceForURL(url_b1)));
425 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
426 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get()));
428 // Getting the new SiteInstance from the BrowsingInstance and from another
429 // SiteInstance in the BrowsingInstance should give the same result.
430 EXPECT_EQ(site_instance_b1.get(),
431 site_instance_a1->GetRelatedSiteInstance(url_b1));
433 // A second visit to the original site should return the same SiteInstance.
434 const GURL url_a2("http://www.google.com/2.html");
435 EXPECT_EQ(site_instance_a1.get(),
436 browsing_instance->GetSiteInstanceForURL(url_a2));
437 EXPECT_EQ(site_instance_a1.get(),
438 site_instance_a1->GetRelatedSiteInstance(url_a2));
440 // A visit to the original site in a new BrowsingInstance (same or different
441 // browser context) should return a different SiteInstance.
442 TestBrowsingInstance* browsing_instance2 =
443 new TestBrowsingInstance(browser_context.get(), &delete_counter);
444 // Ensure the new SiteInstance is ref counted so that it gets deleted.
445 scoped_refptr<SiteInstanceImpl> site_instance_a2_2(
446 static_cast<SiteInstanceImpl*>(
447 browsing_instance2->GetSiteInstanceForURL(url_a2)));
448 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get());
449 EXPECT_FALSE(
450 site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2.get()));
452 // The two SiteInstances for http://google.com should not use the same process
453 // if process-per-site is not enabled.
454 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess());
455 scoped_ptr<RenderProcessHost> process_a2_2(site_instance_a2_2->GetProcess());
456 EXPECT_NE(process_a1.get(), process_a2_2.get());
458 // Should be able to see that we do have SiteInstances.
459 EXPECT_TRUE(browsing_instance->HasSiteInstance(
460 GURL("http://mail.google.com")));
461 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
462 GURL("http://mail.google.com")));
463 EXPECT_TRUE(browsing_instance->HasSiteInstance(
464 GURL("http://mail.yahoo.com")));
466 // Should be able to see that we don't have SiteInstances.
467 EXPECT_FALSE(browsing_instance->HasSiteInstance(
468 GURL("https://www.google.com")));
469 EXPECT_FALSE(browsing_instance2->HasSiteInstance(
470 GURL("http://www.yahoo.com")));
472 // browsing_instances will be deleted when their SiteInstances are deleted.
473 // The processes will be unregistered when the RPH scoped_ptrs go away.
475 DrainMessageLoops();
478 // Test to ensure that there is only one RenderProcessHost per site for an
479 // entire BrowserContext, if process-per-site is in use.
480 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) {
481 base::CommandLine::ForCurrentProcess()->AppendSwitch(
482 switches::kProcessPerSite);
483 int delete_counter = 0;
484 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
485 TestBrowsingInstance* browsing_instance =
486 new TestBrowsingInstance(browser_context.get(), &delete_counter);
488 const GURL url_a1("http://www.google.com/1.html");
489 scoped_refptr<SiteInstanceImpl> site_instance_a1(
490 static_cast<SiteInstanceImpl*>(
491 browsing_instance->GetSiteInstanceForURL(url_a1)));
492 EXPECT_TRUE(site_instance_a1.get() != NULL);
493 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess());
495 // A separate site should create a separate SiteInstance.
496 const GURL url_b1("http://www.yahoo.com/");
497 scoped_refptr<SiteInstanceImpl> site_instance_b1(
498 static_cast<SiteInstanceImpl*>(
499 browsing_instance->GetSiteInstanceForURL(url_b1)));
500 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
501 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get()));
503 // Getting the new SiteInstance from the BrowsingInstance and from another
504 // SiteInstance in the BrowsingInstance should give the same result.
505 EXPECT_EQ(site_instance_b1.get(),
506 site_instance_a1->GetRelatedSiteInstance(url_b1));
508 // A second visit to the original site should return the same SiteInstance.
509 const GURL url_a2("http://www.google.com/2.html");
510 EXPECT_EQ(site_instance_a1.get(),
511 browsing_instance->GetSiteInstanceForURL(url_a2));
512 EXPECT_EQ(site_instance_a1.get(),
513 site_instance_a1->GetRelatedSiteInstance(url_a2));
515 // A visit to the original site in a new BrowsingInstance (same browser
516 // context) should return a different SiteInstance with the same process.
517 TestBrowsingInstance* browsing_instance2 =
518 new TestBrowsingInstance(browser_context.get(), &delete_counter);
519 scoped_refptr<SiteInstanceImpl> site_instance_a1_2(
520 static_cast<SiteInstanceImpl*>(
521 browsing_instance2->GetSiteInstanceForURL(url_a1)));
522 EXPECT_TRUE(site_instance_a1.get() != NULL);
523 EXPECT_NE(site_instance_a1.get(), site_instance_a1_2.get());
524 EXPECT_EQ(process_a1.get(), site_instance_a1_2->GetProcess());
526 // A visit to the original site in a new BrowsingInstance (different browser
527 // context) should return a different SiteInstance with a different process.
528 scoped_ptr<TestBrowserContext> browser_context2(new TestBrowserContext());
529 TestBrowsingInstance* browsing_instance3 =
530 new TestBrowsingInstance(browser_context2.get(), &delete_counter);
531 scoped_refptr<SiteInstanceImpl> site_instance_a2_3(
532 static_cast<SiteInstanceImpl*>(
533 browsing_instance3->GetSiteInstanceForURL(url_a2)));
534 EXPECT_TRUE(site_instance_a2_3.get() != NULL);
535 scoped_ptr<RenderProcessHost> process_a2_3(site_instance_a2_3->GetProcess());
536 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get());
537 EXPECT_NE(process_a1.get(), process_a2_3.get());
539 // Should be able to see that we do have SiteInstances.
540 EXPECT_TRUE(browsing_instance->HasSiteInstance(
541 GURL("http://mail.google.com"))); // visited before
542 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
543 GURL("http://mail.google.com"))); // visited before
544 EXPECT_TRUE(browsing_instance->HasSiteInstance(
545 GURL("http://mail.yahoo.com"))); // visited before
547 // Should be able to see that we don't have SiteInstances.
548 EXPECT_FALSE(browsing_instance2->HasSiteInstance(
549 GURL("http://www.yahoo.com"))); // different BI, same browser context
550 EXPECT_FALSE(browsing_instance->HasSiteInstance(
551 GURL("https://www.google.com"))); // not visited before
552 EXPECT_FALSE(browsing_instance3->HasSiteInstance(
553 GURL("http://www.yahoo.com"))); // different BI, different context
555 // browsing_instances will be deleted when their SiteInstances are deleted.
556 // The processes will be unregistered when the RPH scoped_ptrs go away.
558 DrainMessageLoops();
561 static SiteInstanceImpl* CreateSiteInstance(BrowserContext* browser_context,
562 const GURL& url) {
563 return static_cast<SiteInstanceImpl*>(
564 SiteInstance::CreateForURL(browser_context, url));
567 // Test to ensure that pages that require certain privileges are grouped
568 // in processes with similar pages.
569 TEST_F(SiteInstanceTest, ProcessSharingByType) {
570 // This test shouldn't run with --site-per-process mode, which prohibits
571 // the renderer process reuse this test explicitly exercises.
572 const base::CommandLine& command_line =
573 *base::CommandLine::ForCurrentProcess();
574 if (command_line.HasSwitch(switches::kSitePerProcess))
575 return;
577 // On Android by default the number of renderer hosts is unlimited and process
578 // sharing doesn't happen. We set the override so that the test can run
579 // everywhere.
580 RenderProcessHost::SetMaxRendererProcessCount(kMaxRendererProcessCount);
582 ChildProcessSecurityPolicyImpl* policy =
583 ChildProcessSecurityPolicyImpl::GetInstance();
585 // Make a bunch of mock renderers so that we hit the limit.
586 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
587 ScopedVector<MockRenderProcessHost> hosts;
588 for (size_t i = 0; i < kMaxRendererProcessCount; ++i)
589 hosts.push_back(new MockRenderProcessHost(browser_context.get()));
591 // Create some extension instances and make sure they share a process.
592 scoped_refptr<SiteInstanceImpl> extension1_instance(
593 CreateSiteInstance(browser_context.get(),
594 GURL(kPrivilegedScheme + std::string("://foo/bar"))));
595 set_privileged_process_id(extension1_instance->GetProcess()->GetID());
597 scoped_refptr<SiteInstanceImpl> extension2_instance(
598 CreateSiteInstance(browser_context.get(),
599 GURL(kPrivilegedScheme + std::string("://baz/bar"))));
601 scoped_ptr<RenderProcessHost> extension_host(
602 extension1_instance->GetProcess());
603 EXPECT_EQ(extension1_instance->GetProcess(),
604 extension2_instance->GetProcess());
606 // Create some WebUI instances and make sure they share a process.
607 scoped_refptr<SiteInstanceImpl> webui1_instance(CreateSiteInstance(
608 browser_context.get(), GURL(kChromeUIScheme + std::string("://gpu"))));
609 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID());
611 scoped_refptr<SiteInstanceImpl> webui2_instance(CreateSiteInstance(
612 browser_context.get(),
613 GURL(kChromeUIScheme + std::string("://media-internals"))));
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 DrainMessageLoops();
628 // Disable the process limit override.
629 RenderProcessHost::SetMaxRendererProcessCount(0u);
632 // Test to ensure that HasWrongProcessForURL behaves properly for different
633 // types of URLs.
634 TEST_F(SiteInstanceTest, HasWrongProcessForURL) {
635 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
636 scoped_ptr<RenderProcessHost> host;
637 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
638 SiteInstance::Create(browser_context.get())));
640 EXPECT_FALSE(instance->HasSite());
641 EXPECT_TRUE(instance->GetSiteURL().is_empty());
643 instance->SetSite(GURL("http://evernote.com/"));
644 EXPECT_TRUE(instance->HasSite());
646 // Check prior to "assigning" a process to the instance, which is expected
647 // to return false due to not being attached to any process yet.
648 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com")));
650 // The call to GetProcess actually creates a new real process, which works
651 // fine, but might be a cause for problems in different contexts.
652 host.reset(instance->GetProcess());
653 EXPECT_TRUE(host.get() != NULL);
654 EXPECT_TRUE(instance->HasProcess());
656 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com")));
657 EXPECT_FALSE(instance->HasWrongProcessForURL(
658 GURL("javascript:alert(document.location.href);")));
660 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://gpu")));
662 // Test that WebUI SiteInstances reject normal web URLs.
663 const GURL webui_url("chrome://gpu");
664 scoped_refptr<SiteInstanceImpl> webui_instance(static_cast<SiteInstanceImpl*>(
665 SiteInstance::Create(browser_context.get())));
666 webui_instance->SetSite(webui_url);
667 scoped_ptr<RenderProcessHost> webui_host(webui_instance->GetProcess());
669 // Simulate granting WebUI bindings for the process.
670 ChildProcessSecurityPolicyImpl::GetInstance()->GrantWebUIBindings(
671 webui_host->GetID());
673 EXPECT_TRUE(webui_instance->HasProcess());
674 EXPECT_FALSE(webui_instance->HasWrongProcessForURL(webui_url));
675 EXPECT_TRUE(webui_instance->HasWrongProcessForURL(GURL("http://google.com")));
676 EXPECT_TRUE(webui_instance->HasWrongProcessForURL(GURL("http://gpu")));
678 // WebUI uses process-per-site, so another instance will use the same process
679 // even if we haven't called GetProcess yet. Make sure HasWrongProcessForURL
680 // doesn't crash (http://crbug.com/137070).
681 scoped_refptr<SiteInstanceImpl> webui_instance2(
682 static_cast<SiteInstanceImpl*>(
683 SiteInstance::Create(browser_context.get())));
684 webui_instance2->SetSite(webui_url);
685 EXPECT_FALSE(webui_instance2->HasWrongProcessForURL(webui_url));
686 EXPECT_TRUE(
687 webui_instance2->HasWrongProcessForURL(GURL("http://google.com")));
689 DrainMessageLoops();
692 // Test to ensure that HasWrongProcessForURL behaves properly even when
693 // --site-per-process is used (http://crbug.com/160671).
694 TEST_F(SiteInstanceTest, HasWrongProcessForURLInSitePerProcess) {
695 base::CommandLine::ForCurrentProcess()->AppendSwitch(
696 switches::kSitePerProcess);
698 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
699 scoped_ptr<RenderProcessHost> host;
700 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
701 SiteInstance::Create(browser_context.get())));
703 instance->SetSite(GURL("http://evernote.com/"));
704 EXPECT_TRUE(instance->HasSite());
706 // Check prior to "assigning" a process to the instance, which is expected
707 // to return false due to not being attached to any process yet.
708 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com")));
710 // The call to GetProcess actually creates a new real process, which works
711 // fine, but might be a cause for problems in different contexts.
712 host.reset(instance->GetProcess());
713 EXPECT_TRUE(host.get() != NULL);
714 EXPECT_TRUE(instance->HasProcess());
716 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com")));
717 EXPECT_FALSE(instance->HasWrongProcessForURL(
718 GURL("javascript:alert(document.location.href);")));
720 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://gpu")));
722 DrainMessageLoops();
725 // Test that we do not reuse a process in process-per-site mode if it has the
726 // wrong bindings for its URL. http://crbug.com/174059.
727 TEST_F(SiteInstanceTest, ProcessPerSiteWithWrongBindings) {
728 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
729 scoped_ptr<RenderProcessHost> host;
730 scoped_ptr<RenderProcessHost> host2;
731 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
732 SiteInstance::Create(browser_context.get())));
734 EXPECT_FALSE(instance->HasSite());
735 EXPECT_TRUE(instance->GetSiteURL().is_empty());
737 // Simulate navigating to a WebUI URL in a process that does not have WebUI
738 // bindings. This already requires bypassing security checks.
739 const GURL webui_url("chrome://gpu");
740 instance->SetSite(webui_url);
741 EXPECT_TRUE(instance->HasSite());
743 // The call to GetProcess actually creates a new real process.
744 host.reset(instance->GetProcess());
745 EXPECT_TRUE(host.get() != NULL);
746 EXPECT_TRUE(instance->HasProcess());
748 // Without bindings, this should look like the wrong process.
749 EXPECT_TRUE(instance->HasWrongProcessForURL(webui_url));
751 // WebUI uses process-per-site, so another instance would normally use the
752 // same process. Make sure it doesn't use the same process if the bindings
753 // are missing.
754 scoped_refptr<SiteInstanceImpl> instance2(
755 static_cast<SiteInstanceImpl*>(
756 SiteInstance::Create(browser_context.get())));
757 instance2->SetSite(webui_url);
758 host2.reset(instance2->GetProcess());
759 EXPECT_TRUE(host2.get() != NULL);
760 EXPECT_TRUE(instance2->HasProcess());
761 EXPECT_NE(host.get(), host2.get());
763 DrainMessageLoops();
766 // Test that we do not register processes with empty sites for process-per-site
767 // mode.
768 TEST_F(SiteInstanceTest, NoProcessPerSiteForEmptySite) {
769 base::CommandLine::ForCurrentProcess()->AppendSwitch(
770 switches::kProcessPerSite);
771 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
772 scoped_ptr<RenderProcessHost> host;
773 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
774 SiteInstance::Create(browser_context.get())));
776 instance->SetSite(GURL());
777 EXPECT_TRUE(instance->HasSite());
778 EXPECT_TRUE(instance->GetSiteURL().is_empty());
779 host.reset(instance->GetProcess());
781 EXPECT_FALSE(RenderProcessHostImpl::GetProcessHostForSite(
782 browser_context.get(), GURL()));
784 DrainMessageLoops();
787 } // namespace content