OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "base/compiler_specific.h" | |
6 #include "base/stl_util.h" | |
7 #include "base/string16.h" | |
8 #include "content/browser/browser_thread_impl.h" | |
9 #include "content/browser/browsing_instance.h" | |
10 #include "content/browser/child_process_security_policy.h" | |
11 #include "content/browser/mock_content_browser_client.h" | |
12 #include "content/browser/renderer_host/render_process_host_impl.h" | |
13 #include "content/browser/renderer_host/render_view_host.h" | |
14 #include "content/browser/renderer_host/test_render_view_host.h" | |
15 #include "content/browser/site_instance.h" | |
16 #include "content/browser/tab_contents/navigation_entry_impl.h" | |
17 #include "content/browser/tab_contents/tab_contents.h" | |
18 #include "content/browser/webui/empty_web_ui_factory.h" | |
19 #include "content/public/common/content_client.h" | |
20 #include "content/public/common/content_constants.h" | |
21 #include "content/public/common/url_constants.h" | |
22 #include "content/test/test_browser_context.h" | |
23 #include "googleurl/src/url_util.h" | |
24 #include "testing/gtest/include/gtest/gtest.h" | |
25 | |
26 using content::BrowserThread; | |
27 using content::BrowserThreadImpl; | |
28 using content::NavigationEntry; | |
29 using content::NavigationEntryImpl; | |
30 | |
31 namespace { | |
32 | |
33 const char kSameAsAnyInstanceURL[] = "about:internets"; | |
34 | |
35 const char kPrivilegedScheme[] = "privileged"; | |
36 | |
37 class SiteInstanceTestWebUIFactory : public content::EmptyWebUIFactory { | |
38 public: | |
39 virtual bool UseWebUIForURL(content::BrowserContext* browser_context, | |
40 const GURL& url) const OVERRIDE { | |
41 return HasWebUIScheme(url); | |
42 } | |
43 virtual bool UseWebUIBindingsForURL(content::BrowserContext* browser_context, | |
44 const GURL& url) const OVERRIDE { | |
45 return HasWebUIScheme(url); | |
46 } | |
47 virtual bool HasWebUIScheme(const GURL& url) const OVERRIDE { | |
48 return url.SchemeIs(chrome::kChromeUIScheme); | |
49 } | |
50 }; | |
51 | |
52 class SiteInstanceTestBrowserClient : public content::MockContentBrowserClient { | |
53 public: | |
54 SiteInstanceTestBrowserClient() | |
55 : privileged_process_id_(-1) { | |
56 } | |
57 | |
58 virtual content::WebUIFactory* GetWebUIFactory() OVERRIDE { | |
59 return &factory_; | |
60 } | |
61 | |
62 virtual bool ShouldUseProcessPerSite(content::BrowserContext* browser_context, | |
63 const GURL& effective_url) OVERRIDE { | |
64 return false; | |
65 } | |
66 | |
67 virtual bool IsURLSameAsAnySiteInstance(const GURL& url) OVERRIDE { | |
68 return url == GURL(kSameAsAnyInstanceURL) || | |
69 url == GURL(chrome::kAboutCrashURL); | |
70 } | |
71 | |
72 virtual bool IsSuitableHost(content::RenderProcessHost* process_host, | |
73 const GURL& site_url) OVERRIDE { | |
74 return (privileged_process_id_ == process_host->GetID()) == | |
75 site_url.SchemeIs(kPrivilegedScheme); | |
76 } | |
77 | |
78 void set_privileged_process_id(int process_id) { | |
79 privileged_process_id_ = process_id; | |
80 } | |
81 | |
82 private: | |
83 SiteInstanceTestWebUIFactory factory_; | |
84 int privileged_process_id_; | |
85 }; | |
86 | |
87 class SiteInstanceTest : public testing::Test { | |
88 public: | |
89 SiteInstanceTest() | |
90 : ui_thread_(BrowserThread::UI, &message_loop_), | |
91 old_browser_client_(NULL) { | |
92 } | |
93 | |
94 virtual void SetUp() { | |
95 old_browser_client_ = content::GetContentClient()->browser(); | |
96 content::GetContentClient()->set_browser(&browser_client_); | |
97 url_util::AddStandardScheme(kPrivilegedScheme); | |
98 url_util::AddStandardScheme(chrome::kChromeUIScheme); | |
99 } | |
100 | |
101 virtual void TearDown() { | |
102 content::GetContentClient()->set_browser(old_browser_client_); | |
103 } | |
104 | |
105 void set_privileged_process_id(int process_id) { | |
106 browser_client_.set_privileged_process_id(process_id); | |
107 } | |
108 | |
109 private: | |
110 MessageLoopForUI message_loop_; | |
111 BrowserThreadImpl ui_thread_; | |
112 | |
113 SiteInstanceTestBrowserClient browser_client_; | |
114 content::ContentBrowserClient* old_browser_client_; | |
115 }; | |
116 | |
117 class TestBrowsingInstance : public BrowsingInstance { | |
118 public: | |
119 TestBrowsingInstance(content::BrowserContext* browser_context, | |
120 int* delete_counter) | |
121 : BrowsingInstance(browser_context), | |
122 use_process_per_site_(false), | |
123 delete_counter_(delete_counter) { | |
124 } | |
125 | |
126 // Overrides BrowsingInstance::ShouldUseProcessPerSite so that we can test | |
127 // both alternatives without using command-line switches. | |
128 bool ShouldUseProcessPerSite(const GURL& url) { | |
129 return use_process_per_site_; | |
130 } | |
131 | |
132 void set_use_process_per_site(bool use_process_per_site) { | |
133 use_process_per_site_ = use_process_per_site; | |
134 } | |
135 | |
136 // Make a few methods public for tests. | |
137 using BrowsingInstance::ShouldUseProcessPerSite; | |
138 using BrowsingInstance::browser_context; | |
139 using BrowsingInstance::HasSiteInstance; | |
140 using BrowsingInstance::GetSiteInstanceForURL; | |
141 using BrowsingInstance::RegisterSiteInstance; | |
142 using BrowsingInstance::UnregisterSiteInstance; | |
143 | |
144 private: | |
145 virtual ~TestBrowsingInstance() { | |
146 (*delete_counter_)++; | |
147 } | |
148 | |
149 // Set by individual tests. | |
150 bool use_process_per_site_; | |
151 | |
152 int* delete_counter_; | |
153 }; | |
154 | |
155 class TestSiteInstance : public SiteInstance { | |
156 public: | |
157 static TestSiteInstance* CreateTestSiteInstance( | |
158 content::BrowserContext* browser_context, | |
159 int* site_delete_counter, | |
160 int* browsing_delete_counter) { | |
161 TestBrowsingInstance* browsing_instance = | |
162 new TestBrowsingInstance(browser_context, browsing_delete_counter); | |
163 return new TestSiteInstance(browsing_instance, site_delete_counter); | |
164 } | |
165 | |
166 private: | |
167 TestSiteInstance(BrowsingInstance* browsing_instance, int* delete_counter) | |
168 : SiteInstance(browsing_instance), delete_counter_(delete_counter) {} | |
169 virtual ~TestSiteInstance() { | |
170 (*delete_counter_)++; | |
171 } | |
172 | |
173 int* delete_counter_; | |
174 }; | |
175 | |
176 } // namespace | |
177 | |
178 // Test to ensure no memory leaks for SiteInstance objects. | |
179 TEST_F(SiteInstanceTest, SiteInstanceDestructor) { | |
180 // The existence of these factories will cause TabContents to create our test | |
181 // one instead of the real one. | |
182 MockRenderProcessHostFactory rph_factory; | |
183 TestRenderViewHostFactory rvh_factory(&rph_factory); | |
184 int site_delete_counter = 0; | |
185 int browsing_delete_counter = 0; | |
186 const GURL url("test:foo"); | |
187 | |
188 // Ensure that instances are deleted when their NavigationEntries are gone. | |
189 TestSiteInstance* instance = | |
190 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter, | |
191 &browsing_delete_counter); | |
192 EXPECT_EQ(0, site_delete_counter); | |
193 | |
194 NavigationEntryImpl* e1 = new NavigationEntryImpl( | |
195 instance, 0, url, content::Referrer(), string16(), | |
196 content::PAGE_TRANSITION_LINK, false); | |
197 | |
198 // Redundantly setting e1's SiteInstance shouldn't affect the ref count. | |
199 e1->set_site_instance(instance); | |
200 EXPECT_EQ(0, site_delete_counter); | |
201 | |
202 // Add a second reference | |
203 NavigationEntryImpl* e2 = new NavigationEntryImpl( | |
204 instance, 0, url, content::Referrer(), string16(), | |
205 content::PAGE_TRANSITION_LINK, false); | |
206 | |
207 // Now delete both entries and be sure the SiteInstance goes away. | |
208 delete e1; | |
209 EXPECT_EQ(0, site_delete_counter); | |
210 EXPECT_EQ(0, browsing_delete_counter); | |
211 delete e2; | |
212 EXPECT_EQ(1, site_delete_counter); | |
213 // instance is now deleted | |
214 EXPECT_EQ(1, browsing_delete_counter); | |
215 // browsing_instance is now deleted | |
216 | |
217 // Ensure that instances are deleted when their RenderViewHosts are gone. | |
218 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | |
219 instance = | |
220 TestSiteInstance::CreateTestSiteInstance(browser_context.get(), | |
221 &site_delete_counter, | |
222 &browsing_delete_counter); | |
223 { | |
224 TabContents contents(browser_context.get(), | |
225 instance, | |
226 MSG_ROUTING_NONE, | |
227 NULL, | |
228 NULL); | |
229 EXPECT_EQ(1, site_delete_counter); | |
230 EXPECT_EQ(1, browsing_delete_counter); | |
231 } | |
232 | |
233 // Make sure that we flush any messages related to the above TabContents | |
234 // destruction. | |
235 MessageLoop::current()->RunAllPending(); | |
236 | |
237 EXPECT_EQ(2, site_delete_counter); | |
238 EXPECT_EQ(2, browsing_delete_counter); | |
239 // contents is now deleted, along with instance and browsing_instance | |
240 } | |
241 | |
242 // Test that NavigationEntries with SiteInstances can be cloned, but that their | |
243 // SiteInstances can be changed afterwards. Also tests that the ref counts are | |
244 // updated properly after the change. | |
245 TEST_F(SiteInstanceTest, CloneNavigationEntry) { | |
246 int site_delete_counter1 = 0; | |
247 int site_delete_counter2 = 0; | |
248 int browsing_delete_counter = 0; | |
249 const GURL url("test:foo"); | |
250 | |
251 SiteInstance* instance1 = | |
252 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter1, | |
253 &browsing_delete_counter); | |
254 SiteInstance* instance2 = | |
255 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter2, | |
256 &browsing_delete_counter); | |
257 | |
258 NavigationEntryImpl* e1 = new NavigationEntryImpl( | |
259 instance1, 0, url, content::Referrer(), string16(), | |
260 content::PAGE_TRANSITION_LINK, false); | |
261 // Clone the entry | |
262 NavigationEntryImpl* e2 = new NavigationEntryImpl(*e1); | |
263 | |
264 // Should be able to change the SiteInstance of the cloned entry. | |
265 e2->set_site_instance(instance2); | |
266 | |
267 // The first SiteInstance should go away after deleting e1, since e2 should | |
268 // no longer be referencing it. | |
269 delete e1; | |
270 EXPECT_EQ(1, site_delete_counter1); | |
271 EXPECT_EQ(0, site_delete_counter2); | |
272 | |
273 // The second SiteInstance should go away after deleting e2. | |
274 delete e2; | |
275 EXPECT_EQ(1, site_delete_counter1); | |
276 EXPECT_EQ(1, site_delete_counter2); | |
277 | |
278 // Both BrowsingInstances are also now deleted | |
279 EXPECT_EQ(2, browsing_delete_counter); | |
280 } | |
281 | |
282 // Test to ensure GetProcess returns and creates processes correctly. | |
283 TEST_F(SiteInstanceTest, GetProcess) { | |
284 // Ensure that GetProcess returns a process. | |
285 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | |
286 scoped_ptr<content::RenderProcessHost> host1; | |
287 scoped_refptr<SiteInstance> instance( | |
288 SiteInstance::CreateSiteInstance(browser_context.get())); | |
289 host1.reset(instance->GetProcess()); | |
290 EXPECT_TRUE(host1.get() != NULL); | |
291 | |
292 // Ensure that GetProcess creates a new process. | |
293 scoped_refptr<SiteInstance> instance2( | |
294 SiteInstance::CreateSiteInstance(browser_context.get())); | |
295 scoped_ptr<content::RenderProcessHost> host2(instance2->GetProcess()); | |
296 EXPECT_TRUE(host2.get() != NULL); | |
297 EXPECT_NE(host1.get(), host2.get()); | |
298 } | |
299 | |
300 // Test to ensure SetSite and site() work properly. | |
301 TEST_F(SiteInstanceTest, SetSite) { | |
302 scoped_refptr<SiteInstance> instance(SiteInstance::CreateSiteInstance(NULL)); | |
303 EXPECT_FALSE(instance->has_site()); | |
304 EXPECT_TRUE(instance->site().is_empty()); | |
305 | |
306 instance->SetSite(GURL("http://www.google.com/index.html")); | |
307 EXPECT_EQ(GURL("http://google.com"), instance->site()); | |
308 | |
309 EXPECT_TRUE(instance->has_site()); | |
310 } | |
311 | |
312 // Test to ensure GetSiteForURL properly returns sites for URLs. | |
313 TEST_F(SiteInstanceTest, GetSiteForURL) { | |
314 // Pages are irrelevant. | |
315 GURL test_url = GURL("http://www.google.com/index.html"); | |
316 EXPECT_EQ(GURL("http://google.com"), | |
317 SiteInstance::GetSiteForURL(NULL, test_url)); | |
318 | |
319 // Ports are irrlevant. | |
320 test_url = GURL("https://www.google.com:8080"); | |
321 EXPECT_EQ(GURL("https://google.com"), | |
322 SiteInstance::GetSiteForURL(NULL, test_url)); | |
323 | |
324 // Javascript URLs have no site. | |
325 test_url = GURL("javascript:foo();"); | |
326 EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); | |
327 | |
328 test_url = GURL("http://foo/a.html"); | |
329 EXPECT_EQ(GURL("http://foo"), SiteInstance::GetSiteForURL(NULL, test_url)); | |
330 | |
331 test_url = GURL("file:///C:/Downloads/"); | |
332 EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); | |
333 | |
334 // TODO(creis): Do we want to special case file URLs to ensure they have | |
335 // either no site or a special "file://" site? We currently return | |
336 // "file://home/" as the site, which seems broken. | |
337 // test_url = GURL("file://home/"); | |
338 // EXPECT_EQ(GURL(), SiteInstance::GetSiteForURL(NULL, test_url)); | |
339 } | |
340 | |
341 // Test of distinguishing URLs from different sites. Most of this logic is | |
342 // tested in RegistryControlledDomainTest. This test focuses on URLs with | |
343 // different schemes or ports. | |
344 TEST_F(SiteInstanceTest, IsSameWebSite) { | |
345 GURL url_foo = GURL("http://foo/a.html"); | |
346 GURL url_foo2 = GURL("http://foo/b.html"); | |
347 GURL url_foo_https = GURL("https://foo/a.html"); | |
348 GURL url_foo_port = GURL("http://foo:8080/a.html"); | |
349 GURL url_javascript = GURL("javascript:alert(1);"); | |
350 GURL url_crash = GURL(chrome::kAboutCrashURL); | |
351 GURL url_browser_specified = GURL(kSameAsAnyInstanceURL); | |
352 | |
353 // Same scheme and port -> same site. | |
354 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo2)); | |
355 | |
356 // Different scheme -> different site. | |
357 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_https)); | |
358 | |
359 // Different port -> same site. | |
360 // (Changes to document.domain make renderer ignore the port.) | |
361 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_port)); | |
362 | |
363 // JavaScript links should be considered same site for anything. | |
364 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo)); | |
365 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_https)); | |
366 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_port)); | |
367 | |
368 // The URLs specified by the ContentBrowserClient should also be treated as | |
369 // same site. | |
370 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_crash, url_foo)); | |
371 EXPECT_TRUE( | |
372 SiteInstance::IsSameWebSite(NULL, url_browser_specified, url_foo)); | |
373 } | |
374 | |
375 // Test to ensure that there is only one SiteInstance per site in a given | |
376 // BrowsingInstance, when process-per-site is not in use. | |
377 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { | |
378 int delete_counter = 0; | |
379 TestBrowsingInstance* browsing_instance = | |
380 new TestBrowsingInstance(NULL, &delete_counter); | |
381 browsing_instance->set_use_process_per_site(false); | |
382 | |
383 const GURL url_a1("http://www.google.com/1.html"); | |
384 scoped_refptr<SiteInstance> site_instance_a1( | |
385 browsing_instance->GetSiteInstanceForURL(url_a1)); | |
386 EXPECT_TRUE(site_instance_a1.get() != NULL); | |
387 | |
388 // A separate site should create a separate SiteInstance. | |
389 const GURL url_b1("http://www.yahoo.com/"); | |
390 scoped_refptr<SiteInstance> site_instance_b1( | |
391 browsing_instance->GetSiteInstanceForURL(url_b1)); | |
392 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); | |
393 | |
394 // Getting the new SiteInstance from the BrowsingInstance and from another | |
395 // SiteInstance in the BrowsingInstance should give the same result. | |
396 EXPECT_EQ(site_instance_b1.get(), | |
397 site_instance_a1->GetRelatedSiteInstance(url_b1)); | |
398 | |
399 // A second visit to the original site should return the same SiteInstance. | |
400 const GURL url_a2("http://www.google.com/2.html"); | |
401 EXPECT_EQ(site_instance_a1.get(), | |
402 browsing_instance->GetSiteInstanceForURL(url_a2)); | |
403 EXPECT_EQ(site_instance_a1.get(), | |
404 site_instance_a1->GetRelatedSiteInstance(url_a2)); | |
405 | |
406 // A visit to the original site in a new BrowsingInstance (same or different | |
407 // browser context) should return a different SiteInstance. | |
408 TestBrowsingInstance* browsing_instance2 = | |
409 new TestBrowsingInstance(NULL, &delete_counter); | |
410 browsing_instance2->set_use_process_per_site(false); | |
411 // Ensure the new SiteInstance is ref counted so that it gets deleted. | |
412 scoped_refptr<SiteInstance> site_instance_a2_2( | |
413 browsing_instance2->GetSiteInstanceForURL(url_a2)); | |
414 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); | |
415 | |
416 // Should be able to see that we do have SiteInstances. | |
417 EXPECT_TRUE(browsing_instance->HasSiteInstance( | |
418 GURL("http://mail.google.com"))); | |
419 EXPECT_TRUE(browsing_instance2->HasSiteInstance( | |
420 GURL("http://mail.google.com"))); | |
421 EXPECT_TRUE(browsing_instance->HasSiteInstance( | |
422 GURL("http://mail.yahoo.com"))); | |
423 | |
424 // Should be able to see that we don't have SiteInstances. | |
425 EXPECT_FALSE(browsing_instance->HasSiteInstance( | |
426 GURL("https://www.google.com"))); | |
427 EXPECT_FALSE(browsing_instance2->HasSiteInstance( | |
428 GURL("http://www.yahoo.com"))); | |
429 | |
430 // browsing_instances will be deleted when their SiteInstances are deleted | |
431 } | |
432 | |
433 // Test to ensure that there is only one SiteInstance per site for an entire | |
434 // BrowserContext, if process-per-site is in use. | |
435 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { | |
436 int delete_counter = 0; | |
437 TestBrowsingInstance* browsing_instance = | |
438 new TestBrowsingInstance(NULL, &delete_counter); | |
439 browsing_instance->set_use_process_per_site(true); | |
440 | |
441 const GURL url_a1("http://www.google.com/1.html"); | |
442 scoped_refptr<SiteInstance> site_instance_a1( | |
443 browsing_instance->GetSiteInstanceForURL(url_a1)); | |
444 EXPECT_TRUE(site_instance_a1.get() != NULL); | |
445 | |
446 // A separate site should create a separate SiteInstance. | |
447 const GURL url_b1("http://www.yahoo.com/"); | |
448 scoped_refptr<SiteInstance> site_instance_b1( | |
449 browsing_instance->GetSiteInstanceForURL(url_b1)); | |
450 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); | |
451 | |
452 // Getting the new SiteInstance from the BrowsingInstance and from another | |
453 // SiteInstance in the BrowsingInstance should give the same result. | |
454 EXPECT_EQ(site_instance_b1.get(), | |
455 site_instance_a1->GetRelatedSiteInstance(url_b1)); | |
456 | |
457 // A second visit to the original site should return the same SiteInstance. | |
458 const GURL url_a2("http://www.google.com/2.html"); | |
459 EXPECT_EQ(site_instance_a1.get(), | |
460 browsing_instance->GetSiteInstanceForURL(url_a2)); | |
461 EXPECT_EQ(site_instance_a1.get(), | |
462 site_instance_a1->GetRelatedSiteInstance(url_a2)); | |
463 | |
464 // A visit to the original site in a new BrowsingInstance (same browser | |
465 // context) should also return the same SiteInstance. | |
466 // This BrowsingInstance doesn't get its own SiteInstance within the test, so | |
467 // it won't be deleted by its children. Thus, we'll keep a ref count to it | |
468 // to make sure it gets deleted. | |
469 scoped_refptr<TestBrowsingInstance> browsing_instance2( | |
470 new TestBrowsingInstance(NULL, &delete_counter)); | |
471 browsing_instance2->set_use_process_per_site(true); | |
472 EXPECT_EQ(site_instance_a1.get(), | |
473 browsing_instance2->GetSiteInstanceForURL(url_a2)); | |
474 | |
475 // A visit to the original site in a new BrowsingInstance (different browser | |
476 // context) should return a different SiteInstance. | |
477 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | |
478 TestBrowsingInstance* browsing_instance3 = | |
479 new TestBrowsingInstance(browser_context.get(), &delete_counter); | |
480 browsing_instance3->set_use_process_per_site(true); | |
481 // Ensure the new SiteInstance is ref counted so that it gets deleted. | |
482 scoped_refptr<SiteInstance> site_instance_a2_3( | |
483 browsing_instance3->GetSiteInstanceForURL(url_a2)); | |
484 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); | |
485 | |
486 // Should be able to see that we do have SiteInstances. | |
487 EXPECT_TRUE(browsing_instance->HasSiteInstance( | |
488 GURL("http://mail.google.com"))); // visited before | |
489 EXPECT_TRUE(browsing_instance2->HasSiteInstance( | |
490 GURL("http://mail.google.com"))); // visited before | |
491 EXPECT_TRUE(browsing_instance->HasSiteInstance( | |
492 GURL("http://mail.yahoo.com"))); // visited before | |
493 EXPECT_TRUE(browsing_instance2->HasSiteInstance( | |
494 GURL("http://www.yahoo.com"))); // different BI, but same browser context | |
495 | |
496 // Should be able to see that we don't have SiteInstances. | |
497 EXPECT_FALSE(browsing_instance->HasSiteInstance( | |
498 GURL("https://www.google.com"))); // not visited before | |
499 EXPECT_FALSE(browsing_instance3->HasSiteInstance( | |
500 GURL("http://www.yahoo.com"))); // different BI, different context | |
501 | |
502 // browsing_instances will be deleted when their SiteInstances are deleted | |
503 } | |
504 | |
505 static SiteInstance* CreateSiteInstance( | |
506 content::RenderProcessHostFactory* factory, const GURL& url) { | |
507 SiteInstance* instance = SiteInstance::CreateSiteInstanceForURL(NULL, url); | |
508 instance->set_render_process_host_factory(factory); | |
509 return instance; | |
510 } | |
511 | |
512 // Test to ensure that pages that require certain privileges are grouped | |
513 // in processes with similar pages. | |
514 TEST_F(SiteInstanceTest, ProcessSharingByType) { | |
515 MockRenderProcessHostFactory rph_factory; | |
516 ChildProcessSecurityPolicy* policy = | |
517 ChildProcessSecurityPolicy::GetInstance(); | |
518 | |
519 // Make a bunch of mock renderers so that we hit the limit. | |
520 std::vector<MockRenderProcessHost*> hosts; | |
521 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) | |
522 hosts.push_back(new MockRenderProcessHost(NULL)); | |
523 | |
524 // Create some extension instances and make sure they share a process. | |
525 scoped_refptr<SiteInstance> extension1_instance( | |
526 CreateSiteInstance(&rph_factory, | |
527 GURL(kPrivilegedScheme + std::string("://foo/bar")))); | |
528 set_privileged_process_id(extension1_instance->GetProcess()->GetID()); | |
529 | |
530 scoped_refptr<SiteInstance> extension2_instance( | |
531 CreateSiteInstance(&rph_factory, | |
532 GURL(kPrivilegedScheme + std::string("://baz/bar")))); | |
533 | |
534 scoped_ptr<content::RenderProcessHost> extension_host( | |
535 extension1_instance->GetProcess()); | |
536 EXPECT_EQ(extension1_instance->GetProcess(), | |
537 extension2_instance->GetProcess()); | |
538 | |
539 // Create some WebUI instances and make sure they share a process. | |
540 scoped_refptr<SiteInstance> webui1_instance(CreateSiteInstance(&rph_factory, | |
541 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); | |
542 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID()); | |
543 | |
544 scoped_refptr<SiteInstance> webui2_instance(CreateSiteInstance(&rph_factory, | |
545 GURL(chrome::kChromeUIScheme + std::string("://history")))); | |
546 | |
547 scoped_ptr<content::RenderProcessHost> dom_host( | |
548 webui1_instance->GetProcess()); | |
549 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); | |
550 | |
551 // Make sure none of differing privilege processes are mixed. | |
552 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); | |
553 | |
554 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) { | |
555 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); | |
556 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); | |
557 } | |
558 | |
559 STLDeleteContainerPointers(hosts.begin(), hosts.end()); | |
560 } | |
561 | |
562 // Test to ensure that HasWrongProcessForURL behaves properly for different | |
563 // types of URLs. | |
564 TEST_F(SiteInstanceTest, HasWrongProcessForURL) { | |
565 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | |
566 scoped_ptr<content::RenderProcessHost> host; | |
567 scoped_refptr<SiteInstance> instance( | |
568 SiteInstance::CreateSiteInstance(browser_context.get())); | |
569 | |
570 EXPECT_FALSE(instance->has_site()); | |
571 EXPECT_TRUE(instance->site().is_empty()); | |
572 | |
573 instance->SetSite(GURL("http://evernote.com/")); | |
574 EXPECT_TRUE(instance->has_site()); | |
575 | |
576 // Check prior to "assigning" a process to the instance, which is expected | |
577 // to return false due to not being attached to any process yet. | |
578 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); | |
579 | |
580 // The call to GetProcess actually creates a new real process, which works | |
581 // fine, but might be a cause for problems in different contexts. | |
582 host.reset(instance->GetProcess()); | |
583 EXPECT_TRUE(host.get() != NULL); | |
584 EXPECT_TRUE(instance->HasProcess()); | |
585 | |
586 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); | |
587 EXPECT_FALSE(instance->HasWrongProcessForURL( | |
588 GURL("javascript:alert(document.location.href);"))); | |
589 | |
590 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings"))); | |
591 } | |
OLD | NEW |