Add ICU message format support
[chromium-blink-merge.git] / content / browser / site_instance_impl_unittest.cc
blob00677c5d38cab704f0949569dd7ba8f70128d87f
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/public/test/test_utils.h"
28 #include "content/test/test_content_browser_client.h"
29 #include "content/test/test_content_client.h"
30 #include "content/test/test_render_view_host.h"
31 #include "testing/gtest/include/gtest/gtest.h"
32 #include "url/url_util.h"
34 namespace content {
35 namespace {
37 const char kPrivilegedScheme[] = "privileged";
39 class SiteInstanceTestBrowserClient : public TestContentBrowserClient {
40 public:
41 SiteInstanceTestBrowserClient()
42 : privileged_process_id_(-1) {
43 WebUIControllerFactory::RegisterFactory(
44 ContentWebUIControllerFactory::GetInstance());
47 ~SiteInstanceTestBrowserClient() override {
48 WebUIControllerFactory::UnregisterFactoryForTesting(
49 ContentWebUIControllerFactory::GetInstance());
52 bool IsSuitableHost(RenderProcessHost* process_host,
53 const GURL& site_url) override {
54 return (privileged_process_id_ == process_host->GetID()) ==
55 site_url.SchemeIs(kPrivilegedScheme);
58 void set_privileged_process_id(int process_id) {
59 privileged_process_id_ = process_id;
62 private:
63 int privileged_process_id_;
66 class SiteInstanceTest : public testing::Test {
67 public:
68 SiteInstanceTest()
69 : ui_thread_(BrowserThread::UI, &message_loop_),
70 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING,
71 &message_loop_),
72 io_thread_(BrowserThread::IO, &message_loop_),
73 old_browser_client_(NULL) {
76 void SetUp() override {
77 old_browser_client_ = SetBrowserClientForTesting(&browser_client_);
78 url::AddStandardScheme(kPrivilegedScheme);
79 url::AddStandardScheme(kChromeUIScheme);
81 SiteInstanceImpl::set_render_process_host_factory(&rph_factory_);
84 void TearDown() override {
85 // Ensure that no RenderProcessHosts are left over after the tests.
86 EXPECT_TRUE(RenderProcessHost::AllHostsIterator().IsAtEnd());
88 SetBrowserClientForTesting(old_browser_client_);
89 SiteInstanceImpl::set_render_process_host_factory(NULL);
91 // http://crbug.com/143565 found SiteInstanceTest leaking an
92 // AppCacheDatabase. This happens because some part of the test indirectly
93 // calls StoragePartitionImplMap::PostCreateInitialization(), which posts
94 // a task to the IO thread to create the AppCacheDatabase. Since the
95 // message loop is not running, the AppCacheDatabase ends up getting
96 // created when DrainMessageLoops() gets called at the end of a test case.
97 // Immediately after, the test case ends and the AppCacheDatabase gets
98 // scheduled for deletion. Here, call DrainMessageLoops() again so the
99 // AppCacheDatabase actually gets deleted.
100 DrainMessageLoops();
103 void set_privileged_process_id(int process_id) {
104 browser_client_.set_privileged_process_id(process_id);
107 void DrainMessageLoops() {
108 // We don't just do this in TearDown() because we create TestBrowserContext
109 // objects in each test, which will be destructed before
110 // TearDown() is called.
111 base::MessageLoop::current()->RunUntilIdle();
112 message_loop_.RunUntilIdle();
115 private:
116 base::MessageLoopForUI message_loop_;
117 TestBrowserThread ui_thread_;
118 TestBrowserThread file_user_blocking_thread_;
119 TestBrowserThread io_thread_;
121 SiteInstanceTestBrowserClient browser_client_;
122 ContentBrowserClient* old_browser_client_;
123 MockRenderProcessHostFactory rph_factory_;
126 // Subclass of BrowsingInstance that updates a counter when deleted and
127 // returns TestSiteInstances from GetSiteInstanceForURL.
128 class TestBrowsingInstance : public BrowsingInstance {
129 public:
130 TestBrowsingInstance(BrowserContext* browser_context, int* delete_counter)
131 : BrowsingInstance(browser_context),
132 delete_counter_(delete_counter) {
135 // Make a few methods public for tests.
136 using BrowsingInstance::browser_context;
137 using BrowsingInstance::HasSiteInstance;
138 using BrowsingInstance::GetSiteInstanceForURL;
139 using BrowsingInstance::RegisterSiteInstance;
140 using BrowsingInstance::UnregisterSiteInstance;
142 private:
143 ~TestBrowsingInstance() override { (*delete_counter_)++; }
145 int* delete_counter_;
148 // Subclass of SiteInstanceImpl that updates a counter when deleted.
149 class TestSiteInstance : public SiteInstanceImpl {
150 public:
151 static TestSiteInstance* CreateTestSiteInstance(
152 BrowserContext* browser_context,
153 int* site_delete_counter,
154 int* browsing_delete_counter) {
155 TestBrowsingInstance* browsing_instance =
156 new TestBrowsingInstance(browser_context, browsing_delete_counter);
157 return new TestSiteInstance(browsing_instance, site_delete_counter);
160 private:
161 TestSiteInstance(BrowsingInstance* browsing_instance, int* delete_counter)
162 : SiteInstanceImpl(browsing_instance), delete_counter_(delete_counter) {}
163 ~TestSiteInstance() override { (*delete_counter_)++; }
165 int* delete_counter_;
168 } // namespace
170 // Test to ensure no memory leaks for SiteInstance objects.
171 TEST_F(SiteInstanceTest, SiteInstanceDestructor) {
172 // The existence of this object will cause WebContentsImpl to create our
173 // test one instead of the real one.
174 RenderViewHostTestEnabler rvh_test_enabler;
175 int site_delete_counter = 0;
176 int browsing_delete_counter = 0;
177 const GURL url("test:foo");
179 // Ensure that instances are deleted when their NavigationEntries are gone.
180 TestSiteInstance* instance =
181 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter,
182 &browsing_delete_counter);
183 EXPECT_EQ(0, site_delete_counter);
185 NavigationEntryImpl* e1 = new NavigationEntryImpl(
186 instance, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK,
187 false);
189 // Redundantly setting e1's SiteInstance shouldn't affect the ref count.
190 e1->set_site_instance(instance);
191 EXPECT_EQ(0, site_delete_counter);
193 // Add a second reference
194 NavigationEntryImpl* e2 = new NavigationEntryImpl(
195 instance, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK,
196 false);
198 // Now delete both entries and be sure the SiteInstance goes away.
199 delete e1;
200 EXPECT_EQ(0, site_delete_counter);
201 EXPECT_EQ(0, browsing_delete_counter);
202 delete e2;
203 EXPECT_EQ(1, site_delete_counter);
204 // instance is now deleted
205 EXPECT_EQ(1, browsing_delete_counter);
206 // browsing_instance is now deleted
208 // Ensure that instances are deleted when their RenderViewHosts are gone.
209 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
210 instance =
211 TestSiteInstance::CreateTestSiteInstance(browser_context.get(),
212 &site_delete_counter,
213 &browsing_delete_counter);
215 scoped_ptr<WebContentsImpl> web_contents(static_cast<WebContentsImpl*>(
216 WebContents::Create(WebContents::CreateParams(
217 browser_context.get(), instance))));
218 EXPECT_EQ(1, site_delete_counter);
219 EXPECT_EQ(1, browsing_delete_counter);
222 // Make sure that we flush any messages related to the above WebContentsImpl
223 // destruction.
224 DrainMessageLoops();
226 EXPECT_EQ(2, site_delete_counter);
227 EXPECT_EQ(2, browsing_delete_counter);
228 // contents is now deleted, along with instance and browsing_instance
231 // Test that NavigationEntries with SiteInstances can be cloned, but that their
232 // SiteInstances can be changed afterwards. Also tests that the ref counts are
233 // updated properly after the change.
234 TEST_F(SiteInstanceTest, CloneNavigationEntry) {
235 int site_delete_counter1 = 0;
236 int site_delete_counter2 = 0;
237 int browsing_delete_counter = 0;
238 const GURL url("test:foo");
240 SiteInstanceImpl* instance1 =
241 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter1,
242 &browsing_delete_counter);
243 SiteInstanceImpl* instance2 =
244 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter2,
245 &browsing_delete_counter);
247 scoped_ptr<NavigationEntryImpl> e1 = make_scoped_ptr(new NavigationEntryImpl(
248 instance1, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK,
249 false));
250 // Clone the entry.
251 scoped_ptr<NavigationEntryImpl> e2 = e1->Clone();
253 // Should be able to change the SiteInstance of the cloned entry.
254 e2->set_site_instance(instance2);
256 // The first SiteInstance should go away after resetting e1, since e2 should
257 // no longer be referencing it.
258 e1.reset();
259 EXPECT_EQ(1, site_delete_counter1);
260 EXPECT_EQ(0, site_delete_counter2);
262 // The second SiteInstance should go away after resetting e2.
263 e2.reset();
264 EXPECT_EQ(1, site_delete_counter1);
265 EXPECT_EQ(1, site_delete_counter2);
267 // Both BrowsingInstances are also now deleted.
268 EXPECT_EQ(2, browsing_delete_counter);
270 DrainMessageLoops();
273 // Test to ensure GetProcess returns and creates processes correctly.
274 TEST_F(SiteInstanceTest, GetProcess) {
275 // Ensure that GetProcess returns a process.
276 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
277 scoped_ptr<RenderProcessHost> host1;
278 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
279 SiteInstance::Create(browser_context.get())));
280 host1.reset(instance->GetProcess());
281 EXPECT_TRUE(host1.get() != NULL);
283 // Ensure that GetProcess creates a new process.
284 scoped_refptr<SiteInstanceImpl> instance2(static_cast<SiteInstanceImpl*>(
285 SiteInstance::Create(browser_context.get())));
286 scoped_ptr<RenderProcessHost> host2(instance2->GetProcess());
287 EXPECT_TRUE(host2.get() != NULL);
288 EXPECT_NE(host1.get(), host2.get());
290 DrainMessageLoops();
293 // Test to ensure SetSite and site() work properly.
294 TEST_F(SiteInstanceTest, SetSite) {
295 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
296 SiteInstance::Create(NULL)));
297 EXPECT_FALSE(instance->HasSite());
298 EXPECT_TRUE(instance->GetSiteURL().is_empty());
300 instance->SetSite(GURL("http://www.google.com/index.html"));
301 EXPECT_EQ(GURL("http://google.com"), instance->GetSiteURL());
303 EXPECT_TRUE(instance->HasSite());
305 DrainMessageLoops();
308 // Test to ensure GetSiteForURL properly returns sites for URLs.
309 TEST_F(SiteInstanceTest, GetSiteForURL) {
310 // Pages are irrelevant.
311 GURL test_url = GURL("http://www.google.com/index.html");
312 GURL site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
313 EXPECT_EQ(GURL("http://google.com"), site_url);
314 EXPECT_EQ("http", site_url.scheme());
315 EXPECT_EQ("google.com", site_url.host());
317 // Ports are irrlevant.
318 test_url = GURL("https://www.google.com:8080");
319 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
320 EXPECT_EQ(GURL("https://google.com"), site_url);
322 // Hostnames without TLDs are ok.
323 test_url = GURL("http://foo/a.html");
324 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
325 EXPECT_EQ(GURL("http://foo"), site_url);
326 EXPECT_EQ("foo", site_url.host());
328 // File URLs should include the scheme.
329 test_url = GURL("file:///C:/Downloads/");
330 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
331 EXPECT_EQ(GURL("file:"), site_url);
332 EXPECT_EQ("file", site_url.scheme());
333 EXPECT_FALSE(site_url.has_host());
335 // Some file URLs have hosts in the path.
336 test_url = GURL("file://server/path");
337 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
338 EXPECT_EQ(GURL("file://server"), site_url);
339 EXPECT_EQ("server", site_url.host());
341 // Data URLs should include the scheme.
342 test_url = GURL("data:text/html,foo");
343 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
344 EXPECT_EQ(GURL("data:"), site_url);
345 EXPECT_EQ("data", site_url.scheme());
346 EXPECT_FALSE(site_url.has_host());
348 // Javascript URLs should include the scheme.
349 test_url = GURL("javascript:foo();");
350 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
351 EXPECT_EQ(GURL("javascript:"), site_url);
352 EXPECT_EQ("javascript", site_url.scheme());
353 EXPECT_FALSE(site_url.has_host());
355 // Guest URLs are special and need to have the path in the site as well,
356 // since it affects the StoragePartition configuration.
357 std::string guest_url(kGuestScheme);
358 guest_url.append("://abc123/path");
359 test_url = GURL(guest_url);
360 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url);
361 EXPECT_EQ(test_url, site_url);
363 DrainMessageLoops();
366 // Test of distinguishing URLs from different sites. Most of this logic is
367 // tested in RegistryControlledDomainTest. This test focuses on URLs with
368 // different schemes or ports.
369 TEST_F(SiteInstanceTest, IsSameWebSite) {
370 GURL url_foo = GURL("http://foo/a.html");
371 GURL url_foo2 = GURL("http://foo/b.html");
372 GURL url_foo_https = GURL("https://foo/a.html");
373 GURL url_foo_port = GURL("http://foo:8080/a.html");
374 GURL url_javascript = GURL("javascript:alert(1);");
375 GURL url_blank = GURL(url::kAboutBlankURL);
377 // Same scheme and port -> same site.
378 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo2));
380 // Different scheme -> different site.
381 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_https));
383 // Different port -> same site.
384 // (Changes to document.domain make renderer ignore the port.)
385 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_port));
387 // JavaScript links should be considered same site for anything.
388 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo));
389 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_https));
390 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_port));
392 // Navigating to a blank page is considered the same site.
393 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_blank));
394 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo_https, url_blank));
395 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo_port, url_blank));
397 // Navigating from a blank site is not considered to be the same site.
398 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_blank, url_foo));
399 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_blank, url_foo_https));
400 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_blank, url_foo_port));
402 DrainMessageLoops();
405 // Test to ensure that there is only one SiteInstance per site in a given
406 // BrowsingInstance, when process-per-site is not in use.
407 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) {
408 ASSERT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch(
409 switches::kProcessPerSite));
410 int delete_counter = 0;
411 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
412 TestBrowsingInstance* browsing_instance =
413 new TestBrowsingInstance(browser_context.get(), &delete_counter);
415 const GURL url_a1("http://www.google.com/1.html");
416 scoped_refptr<SiteInstanceImpl> site_instance_a1(
417 static_cast<SiteInstanceImpl*>(
418 browsing_instance->GetSiteInstanceForURL(url_a1)));
419 EXPECT_TRUE(site_instance_a1.get() != NULL);
421 // A separate site should create a separate SiteInstance.
422 const GURL url_b1("http://www.yahoo.com/");
423 scoped_refptr<SiteInstanceImpl> site_instance_b1(
424 static_cast<SiteInstanceImpl*>(
425 browsing_instance->GetSiteInstanceForURL(url_b1)));
426 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
427 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get()));
429 // Getting the new SiteInstance from the BrowsingInstance and from another
430 // SiteInstance in the BrowsingInstance should give the same result.
431 EXPECT_EQ(site_instance_b1.get(),
432 site_instance_a1->GetRelatedSiteInstance(url_b1));
434 // A second visit to the original site should return the same SiteInstance.
435 const GURL url_a2("http://www.google.com/2.html");
436 EXPECT_EQ(site_instance_a1.get(),
437 browsing_instance->GetSiteInstanceForURL(url_a2));
438 EXPECT_EQ(site_instance_a1.get(),
439 site_instance_a1->GetRelatedSiteInstance(url_a2));
441 // A visit to the original site in a new BrowsingInstance (same or different
442 // browser context) should return a different SiteInstance.
443 TestBrowsingInstance* browsing_instance2 =
444 new TestBrowsingInstance(browser_context.get(), &delete_counter);
445 // Ensure the new SiteInstance is ref counted so that it gets deleted.
446 scoped_refptr<SiteInstanceImpl> site_instance_a2_2(
447 static_cast<SiteInstanceImpl*>(
448 browsing_instance2->GetSiteInstanceForURL(url_a2)));
449 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get());
450 EXPECT_FALSE(
451 site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2.get()));
453 // The two SiteInstances for http://google.com should not use the same process
454 // if process-per-site is not enabled.
455 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess());
456 scoped_ptr<RenderProcessHost> process_a2_2(site_instance_a2_2->GetProcess());
457 EXPECT_NE(process_a1.get(), process_a2_2.get());
459 // Should be able to see that we do have SiteInstances.
460 EXPECT_TRUE(browsing_instance->HasSiteInstance(
461 GURL("http://mail.google.com")));
462 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
463 GURL("http://mail.google.com")));
464 EXPECT_TRUE(browsing_instance->HasSiteInstance(
465 GURL("http://mail.yahoo.com")));
467 // Should be able to see that we don't have SiteInstances.
468 EXPECT_FALSE(browsing_instance->HasSiteInstance(
469 GURL("https://www.google.com")));
470 EXPECT_FALSE(browsing_instance2->HasSiteInstance(
471 GURL("http://www.yahoo.com")));
473 // browsing_instances will be deleted when their SiteInstances are deleted.
474 // The processes will be unregistered when the RPH scoped_ptrs go away.
476 DrainMessageLoops();
479 // Test to ensure that there is only one RenderProcessHost per site for an
480 // entire BrowserContext, if process-per-site is in use.
481 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) {
482 base::CommandLine::ForCurrentProcess()->AppendSwitch(
483 switches::kProcessPerSite);
484 int delete_counter = 0;
485 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
486 TestBrowsingInstance* browsing_instance =
487 new TestBrowsingInstance(browser_context.get(), &delete_counter);
489 const GURL url_a1("http://www.google.com/1.html");
490 scoped_refptr<SiteInstanceImpl> site_instance_a1(
491 static_cast<SiteInstanceImpl*>(
492 browsing_instance->GetSiteInstanceForURL(url_a1)));
493 EXPECT_TRUE(site_instance_a1.get() != NULL);
494 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess());
496 // A separate site should create a separate SiteInstance.
497 const GURL url_b1("http://www.yahoo.com/");
498 scoped_refptr<SiteInstanceImpl> site_instance_b1(
499 static_cast<SiteInstanceImpl*>(
500 browsing_instance->GetSiteInstanceForURL(url_b1)));
501 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
502 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get()));
504 // Getting the new SiteInstance from the BrowsingInstance and from another
505 // SiteInstance in the BrowsingInstance should give the same result.
506 EXPECT_EQ(site_instance_b1.get(),
507 site_instance_a1->GetRelatedSiteInstance(url_b1));
509 // A second visit to the original site should return the same SiteInstance.
510 const GURL url_a2("http://www.google.com/2.html");
511 EXPECT_EQ(site_instance_a1.get(),
512 browsing_instance->GetSiteInstanceForURL(url_a2));
513 EXPECT_EQ(site_instance_a1.get(),
514 site_instance_a1->GetRelatedSiteInstance(url_a2));
516 // A visit to the original site in a new BrowsingInstance (same browser
517 // context) should return a different SiteInstance with the same process.
518 TestBrowsingInstance* browsing_instance2 =
519 new TestBrowsingInstance(browser_context.get(), &delete_counter);
520 scoped_refptr<SiteInstanceImpl> site_instance_a1_2(
521 static_cast<SiteInstanceImpl*>(
522 browsing_instance2->GetSiteInstanceForURL(url_a1)));
523 EXPECT_TRUE(site_instance_a1.get() != NULL);
524 EXPECT_NE(site_instance_a1.get(), site_instance_a1_2.get());
525 EXPECT_EQ(process_a1.get(), site_instance_a1_2->GetProcess());
527 // A visit to the original site in a new BrowsingInstance (different browser
528 // context) should return a different SiteInstance with a different process.
529 scoped_ptr<TestBrowserContext> browser_context2(new TestBrowserContext());
530 TestBrowsingInstance* browsing_instance3 =
531 new TestBrowsingInstance(browser_context2.get(), &delete_counter);
532 scoped_refptr<SiteInstanceImpl> site_instance_a2_3(
533 static_cast<SiteInstanceImpl*>(
534 browsing_instance3->GetSiteInstanceForURL(url_a2)));
535 EXPECT_TRUE(site_instance_a2_3.get() != NULL);
536 scoped_ptr<RenderProcessHost> process_a2_3(site_instance_a2_3->GetProcess());
537 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get());
538 EXPECT_NE(process_a1.get(), process_a2_3.get());
540 // Should be able to see that we do have SiteInstances.
541 EXPECT_TRUE(browsing_instance->HasSiteInstance(
542 GURL("http://mail.google.com"))); // visited before
543 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
544 GURL("http://mail.google.com"))); // visited before
545 EXPECT_TRUE(browsing_instance->HasSiteInstance(
546 GURL("http://mail.yahoo.com"))); // visited before
548 // Should be able to see that we don't have SiteInstances.
549 EXPECT_FALSE(browsing_instance2->HasSiteInstance(
550 GURL("http://www.yahoo.com"))); // different BI, same browser context
551 EXPECT_FALSE(browsing_instance->HasSiteInstance(
552 GURL("https://www.google.com"))); // not visited before
553 EXPECT_FALSE(browsing_instance3->HasSiteInstance(
554 GURL("http://www.yahoo.com"))); // different BI, different context
556 // browsing_instances will be deleted when their SiteInstances are deleted.
557 // The processes will be unregistered when the RPH scoped_ptrs go away.
559 DrainMessageLoops();
562 static SiteInstanceImpl* CreateSiteInstance(BrowserContext* browser_context,
563 const GURL& url) {
564 return static_cast<SiteInstanceImpl*>(
565 SiteInstance::CreateForURL(browser_context, url));
568 // Test to ensure that pages that require certain privileges are grouped
569 // in processes with similar pages.
570 TEST_F(SiteInstanceTest, ProcessSharingByType) {
571 // This test shouldn't run with --site-per-process mode, which prohibits
572 // the renderer process reuse this test explicitly exercises.
573 if (AreAllSitesIsolatedForTesting())
574 return;
576 // On Android by default the number of renderer hosts is unlimited and process
577 // sharing doesn't happen. We set the override so that the test can run
578 // everywhere.
579 RenderProcessHost::SetMaxRendererProcessCount(kMaxRendererProcessCount);
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 ScopedVector<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(),
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(),
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(), GURL(kChromeUIScheme + std::string("://gpu"))));
608 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID());
610 scoped_refptr<SiteInstanceImpl> webui2_instance(CreateSiteInstance(
611 browser_context.get(),
612 GURL(kChromeUIScheme + std::string("://media-internals"))));
614 scoped_ptr<RenderProcessHost> dom_host(webui1_instance->GetProcess());
615 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess());
617 // Make sure none of differing privilege processes are mixed.
618 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess());
620 for (size_t i = 0; i < kMaxRendererProcessCount; ++i) {
621 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]);
622 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]);
625 DrainMessageLoops();
627 // Disable the process limit override.
628 RenderProcessHost::SetMaxRendererProcessCount(0u);
631 // Test to ensure that HasWrongProcessForURL behaves properly for different
632 // types of URLs.
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://gpu")));
661 // Test that WebUI SiteInstances reject normal web URLs.
662 const GURL webui_url("chrome://gpu");
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")));
675 EXPECT_TRUE(webui_instance->HasWrongProcessForURL(GURL("http://gpu")));
677 // WebUI uses process-per-site, so another instance will use the same process
678 // even if we haven't called GetProcess yet. Make sure HasWrongProcessForURL
679 // doesn't crash (http://crbug.com/137070).
680 scoped_refptr<SiteInstanceImpl> webui_instance2(
681 static_cast<SiteInstanceImpl*>(
682 SiteInstance::Create(browser_context.get())));
683 webui_instance2->SetSite(webui_url);
684 EXPECT_FALSE(webui_instance2->HasWrongProcessForURL(webui_url));
685 EXPECT_TRUE(
686 webui_instance2->HasWrongProcessForURL(GURL("http://google.com")));
688 DrainMessageLoops();
691 // Test to ensure that HasWrongProcessForURL behaves properly even when
692 // --site-per-process is used (http://crbug.com/160671).
693 TEST_F(SiteInstanceTest, HasWrongProcessForURLInSitePerProcess) {
694 IsolateAllSitesForTesting(base::CommandLine::ForCurrentProcess());
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://gpu")));
720 DrainMessageLoops();
723 // Test that we do not reuse a process in process-per-site mode if it has the
724 // wrong bindings for its URL. http://crbug.com/174059.
725 TEST_F(SiteInstanceTest, ProcessPerSiteWithWrongBindings) {
726 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
727 scoped_ptr<RenderProcessHost> host;
728 scoped_ptr<RenderProcessHost> host2;
729 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
730 SiteInstance::Create(browser_context.get())));
732 EXPECT_FALSE(instance->HasSite());
733 EXPECT_TRUE(instance->GetSiteURL().is_empty());
735 // Simulate navigating to a WebUI URL in a process that does not have WebUI
736 // bindings. This already requires bypassing security checks.
737 const GURL webui_url("chrome://gpu");
738 instance->SetSite(webui_url);
739 EXPECT_TRUE(instance->HasSite());
741 // The call to GetProcess actually creates a new real process.
742 host.reset(instance->GetProcess());
743 EXPECT_TRUE(host.get() != NULL);
744 EXPECT_TRUE(instance->HasProcess());
746 // Without bindings, this should look like the wrong process.
747 EXPECT_TRUE(instance->HasWrongProcessForURL(webui_url));
749 // WebUI uses process-per-site, so another instance would normally use the
750 // same process. Make sure it doesn't use the same process if the bindings
751 // are missing.
752 scoped_refptr<SiteInstanceImpl> instance2(
753 static_cast<SiteInstanceImpl*>(
754 SiteInstance::Create(browser_context.get())));
755 instance2->SetSite(webui_url);
756 host2.reset(instance2->GetProcess());
757 EXPECT_TRUE(host2.get() != NULL);
758 EXPECT_TRUE(instance2->HasProcess());
759 EXPECT_NE(host.get(), host2.get());
761 DrainMessageLoops();
764 // Test that we do not register processes with empty sites for process-per-site
765 // mode.
766 TEST_F(SiteInstanceTest, NoProcessPerSiteForEmptySite) {
767 base::CommandLine::ForCurrentProcess()->AppendSwitch(
768 switches::kProcessPerSite);
769 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext());
770 scoped_ptr<RenderProcessHost> host;
771 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
772 SiteInstance::Create(browser_context.get())));
774 instance->SetSite(GURL());
775 EXPECT_TRUE(instance->HasSite());
776 EXPECT_TRUE(instance->GetSiteURL().is_empty());
777 host.reset(instance->GetProcess());
779 EXPECT_FALSE(RenderProcessHostImpl::GetProcessHostForSite(
780 browser_context.get(), GURL()));
782 DrainMessageLoops();
785 } // namespace content