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

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

Issue 11273049: Revert 164120 - content/browser: Move more files into the content namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/browser/site_instance_impl.cc ('k') | content/browser/system_message_window_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/compiler_specific.h" 6 #include "base/compiler_specific.h"
7 #include "base/stl_util.h" 7 #include "base/stl_util.h"
8 #include "base/string16.h" 8 #include "base/string16.h"
9 #include "content/browser/browser_thread_impl.h" 9 #include "content/browser/browser_thread_impl.h"
10 #include "content/browser/browsing_instance.h" 10 #include "content/browser/browsing_instance.h"
(...skipping 10 matching lines...) Expand all
21 #include "content/public/common/content_switches.h" 21 #include "content/public/common/content_switches.h"
22 #include "content/public/common/url_constants.h" 22 #include "content/public/common/url_constants.h"
23 #include "content/public/test/mock_render_process_host.h" 23 #include "content/public/test/mock_render_process_host.h"
24 #include "content/public/test/test_browser_context.h" 24 #include "content/public/test/test_browser_context.h"
25 #include "content/public/test/test_browser_thread.h" 25 #include "content/public/test/test_browser_thread.h"
26 #include "content/test/test_content_browser_client.h" 26 #include "content/test/test_content_browser_client.h"
27 #include "content/test/test_content_client.h" 27 #include "content/test/test_content_client.h"
28 #include "googleurl/src/url_util.h" 28 #include "googleurl/src/url_util.h"
29 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
30 30
31 namespace content { 31 using content::BrowserContext;
32 using content::BrowserThread;
33 using content::BrowserThreadImpl;
34 using content::MockRenderProcessHost;
35 using content::MockRenderProcessHostFactory;
36 using content::NavigationEntry;
37 using content::NavigationEntryImpl;
38 using content::SiteInstance;
39 using content::WebContentsImpl;
40 using content::WebUI;
41 using content::WebUIController;
32 42
33 namespace { 43 namespace {
34 44
35 const char kSameAsAnyInstanceURL[] = "about:internets"; 45 const char kSameAsAnyInstanceURL[] = "about:internets";
36 46
37 const char kPrivilegedScheme[] = "privileged"; 47 const char kPrivilegedScheme[] = "privileged";
38 48
39 class SiteInstanceTestWebUIControllerFactory : public WebUIControllerFactory { 49 class SiteInstanceTestWebUIControllerFactory
50 : public content::WebUIControllerFactory {
40 public: 51 public:
41 virtual WebUIController* CreateWebUIControllerForURL( 52 virtual WebUIController* CreateWebUIControllerForURL(
42 WebUI* web_ui, const GURL& url) const OVERRIDE { 53 WebUI* web_ui, const GURL& url) const OVERRIDE {
43 return NULL; 54 return NULL;
44 } 55 }
45 virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context, 56 virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context,
46 const GURL& url) const OVERRIDE { 57 const GURL& url) const OVERRIDE {
47 return WebUI::kNoWebUI; 58 return WebUI::kNoWebUI;
48 } 59 }
49 virtual bool UseWebUIForURL(BrowserContext* browser_context, 60 virtual bool UseWebUIForURL(BrowserContext* browser_context,
50 const GURL& url) const OVERRIDE { 61 const GURL& url) const OVERRIDE {
51 return GetContentClient()->HasWebUIScheme(url); 62 return content::GetContentClient()->HasWebUIScheme(url);
52 } 63 }
53 virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context, 64 virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context,
54 const GURL& url) const OVERRIDE { 65 const GURL& url) const OVERRIDE {
55 return GetContentClient()->HasWebUIScheme(url); 66 return content::GetContentClient()->HasWebUIScheme(url);
56 } 67 }
57 virtual bool IsURLAcceptableForWebUI( 68 virtual bool IsURLAcceptableForWebUI(
58 BrowserContext* browser_context, 69 BrowserContext* browser_context,
59 const GURL& url, 70 const GURL& url,
60 bool data_urls_allowed) const OVERRIDE { 71 bool data_urls_allowed) const OVERRIDE {
61 return false; 72 return false;
62 } 73 }
63 }; 74 };
64 75
65 class SiteInstanceTestClient : public TestContentClient { 76 class SiteInstanceTestClient : public TestContentClient {
66 public: 77 public:
67 SiteInstanceTestClient() { 78 SiteInstanceTestClient() {
68 } 79 }
69 80
70 virtual bool HasWebUIScheme(const GURL& url) const OVERRIDE { 81 virtual bool HasWebUIScheme(const GURL& url) const OVERRIDE {
71 return url.SchemeIs(chrome::kChromeUIScheme); 82 return url.SchemeIs(chrome::kChromeUIScheme);
72 } 83 }
73 }; 84 };
74 85
75 class SiteInstanceTestBrowserClient : public TestContentBrowserClient { 86 class SiteInstanceTestBrowserClient : public content::TestContentBrowserClient {
76 public: 87 public:
77 SiteInstanceTestBrowserClient() 88 SiteInstanceTestBrowserClient()
78 : privileged_process_id_(-1) { 89 : privileged_process_id_(-1) {
79 } 90 }
80 91
81 virtual WebUIControllerFactory* GetWebUIControllerFactory() OVERRIDE { 92 virtual content::WebUIControllerFactory*
93 GetWebUIControllerFactory() OVERRIDE {
82 return &factory_; 94 return &factory_;
83 } 95 }
84 96
85 virtual bool IsSuitableHost(RenderProcessHost* process_host, 97 virtual bool IsSuitableHost(content::RenderProcessHost* process_host,
86 const GURL& site_url) OVERRIDE { 98 const GURL& site_url) OVERRIDE {
87 return (privileged_process_id_ == process_host->GetID()) == 99 return (privileged_process_id_ == process_host->GetID()) ==
88 site_url.SchemeIs(kPrivilegedScheme); 100 site_url.SchemeIs(kPrivilegedScheme);
89 } 101 }
90 102
91 void set_privileged_process_id(int process_id) { 103 void set_privileged_process_id(int process_id) {
92 privileged_process_id_ = process_id; 104 privileged_process_id_ = process_id;
93 } 105 }
94 106
95 private: 107 private:
96 SiteInstanceTestWebUIControllerFactory factory_; 108 SiteInstanceTestWebUIControllerFactory factory_;
97 int privileged_process_id_; 109 int privileged_process_id_;
98 }; 110 };
99 111
100 class SiteInstanceTest : public testing::Test { 112 class SiteInstanceTest : public testing::Test {
101 public: 113 public:
102 SiteInstanceTest() 114 SiteInstanceTest()
103 : ui_thread_(BrowserThread::UI, &message_loop_), 115 : ui_thread_(BrowserThread::UI, &message_loop_),
104 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, 116 file_user_blocking_thread_(content::BrowserThread::FILE_USER_BLOCKING,
105 &message_loop_), 117 &message_loop_),
106 io_thread_(BrowserThread::IO, &message_loop_), 118 io_thread_(content::BrowserThread::IO, &message_loop_),
107 old_client_(NULL), 119 old_client_(NULL),
108 old_browser_client_(NULL) { 120 old_browser_client_(NULL) {
109 } 121 }
110 122
111 virtual void SetUp() { 123 virtual void SetUp() {
112 old_client_ = GetContentClient(); 124 old_client_ = content::GetContentClient();
113 old_browser_client_ = GetContentClient()->browser(); 125 old_browser_client_ = content::GetContentClient()->browser();
114 SetContentClient(&client_); 126 content::SetContentClient(&client_);
115 GetContentClient()->set_browser_for_testing(&browser_client_); 127 content::GetContentClient()->set_browser_for_testing(&browser_client_);
116 url_util::AddStandardScheme(kPrivilegedScheme); 128 url_util::AddStandardScheme(kPrivilegedScheme);
117 url_util::AddStandardScheme(chrome::kChromeUIScheme); 129 url_util::AddStandardScheme(chrome::kChromeUIScheme);
118 } 130 }
119 131
120 virtual void TearDown() { 132 virtual void TearDown() {
121 // Ensure that no RenderProcessHosts are left over after the tests. 133 // Ensure that no RenderProcessHosts are left over after the tests.
122 EXPECT_TRUE(RenderProcessHost::AllHostsIterator().IsAtEnd()); 134 EXPECT_TRUE(content::RenderProcessHost::AllHostsIterator().IsAtEnd());
123 135
124 GetContentClient()->set_browser_for_testing(old_browser_client_); 136 content::GetContentClient()->set_browser_for_testing(old_browser_client_);
125 SetContentClient(old_client_); 137 content::SetContentClient(old_client_);
126 138
127 // http://crbug.com/143565 found SiteInstanceTest leaking an 139 // http://crbug.com/143565 found SiteInstanceTest leaking an
128 // AppCacheDatabase. This happens because some part of the test indirectly 140 // AppCacheDatabase. This happens because some part of the test indirectly
129 // calls StoragePartitionImplMap::PostCreateInitialization(), which posts 141 // calls StoragePartitionImplMap::PostCreateInitialization(), which posts
130 // a task to the IO thread to create the AppCacheDatabase. Since the 142 // a task to the IO thread to create the AppCacheDatabase. Since the
131 // message loop is not running, the AppCacheDatabase ends up getting 143 // message loop is not running, the AppCacheDatabase ends up getting
132 // created when DrainMessageLoops() gets called at the end of a test case. 144 // created when DrainMessageLoops() gets called at the end of a test case.
133 // Immediately after, the test case ends and the AppCacheDatabase gets 145 // Immediately after, the test case ends and the AppCacheDatabase gets
134 // scheduled for deletion. Here, call DrainMessageLoops() again so the 146 // scheduled for deletion. Here, call DrainMessageLoops() again so the
135 // AppCacheDatabase actually gets deleted. 147 // AppCacheDatabase actually gets deleted.
136 DrainMessageLoops(); 148 DrainMessageLoops();
137 } 149 }
138 150
139 void set_privileged_process_id(int process_id) { 151 void set_privileged_process_id(int process_id) {
140 browser_client_.set_privileged_process_id(process_id); 152 browser_client_.set_privileged_process_id(process_id);
141 } 153 }
142 154
143 void DrainMessageLoops() { 155 void DrainMessageLoops() {
144 // We don't just do this in TearDown() because we create TestBrowserContext 156 // We don't just do this in TearDown() because we create TestBrowserContext
145 // objects in each test, which will be destructed before 157 // objects in each test, which will be destructed before
146 // TearDown() is called. 158 // TearDown() is called.
147 MessageLoop::current()->RunAllPending(); 159 MessageLoop::current()->RunAllPending();
148 message_loop_.RunAllPending(); 160 message_loop_.RunAllPending();
149 } 161 }
150 162
151 private: 163 private:
152 MessageLoopForUI message_loop_; 164 MessageLoopForUI message_loop_;
153 TestBrowserThread ui_thread_; 165 content::TestBrowserThread ui_thread_;
154 TestBrowserThread file_user_blocking_thread_; 166 content::TestBrowserThread file_user_blocking_thread_;
155 TestBrowserThread io_thread_; 167 content::TestBrowserThread io_thread_;
156 168
157 SiteInstanceTestClient client_; 169 SiteInstanceTestClient client_;
158 SiteInstanceTestBrowserClient browser_client_; 170 SiteInstanceTestBrowserClient browser_client_;
159 ContentClient* old_client_; 171 content::ContentClient* old_client_;
160 ContentBrowserClient* old_browser_client_; 172 content::ContentBrowserClient* old_browser_client_;
161 }; 173 };
162 174
163 // Subclass of BrowsingInstance that updates a counter when deleted and 175 // Subclass of BrowsingInstance that updates a counter when deleted and
164 // returns TestSiteInstances from GetSiteInstanceForURL. 176 // returns TestSiteInstances from GetSiteInstanceForURL.
165 class TestBrowsingInstance : public BrowsingInstance { 177 class TestBrowsingInstance : public BrowsingInstance {
166 public: 178 public:
167 TestBrowsingInstance(BrowserContext* browser_context, int* delete_counter) 179 TestBrowsingInstance(BrowserContext* browser_context, int* delete_counter)
168 : BrowsingInstance(browser_context), 180 : BrowsingInstance(browser_context),
169 delete_counter_(delete_counter) { 181 delete_counter_(delete_counter) {
170 } 182 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 217
206 int* delete_counter_; 218 int* delete_counter_;
207 }; 219 };
208 220
209 } // namespace 221 } // namespace
210 222
211 // Test to ensure no memory leaks for SiteInstance objects. 223 // Test to ensure no memory leaks for SiteInstance objects.
212 TEST_F(SiteInstanceTest, SiteInstanceDestructor) { 224 TEST_F(SiteInstanceTest, SiteInstanceDestructor) {
213 // The existence of this object will cause WebContentsImpl to create our 225 // The existence of this object will cause WebContentsImpl to create our
214 // test one instead of the real one. 226 // test one instead of the real one.
215 RenderViewHostTestEnabler rvh_test_enabler; 227 content::RenderViewHostTestEnabler rvh_test_enabler;
216 int site_delete_counter = 0; 228 int site_delete_counter = 0;
217 int browsing_delete_counter = 0; 229 int browsing_delete_counter = 0;
218 const GURL url("test:foo"); 230 const GURL url("test:foo");
219 231
220 // Ensure that instances are deleted when their NavigationEntries are gone. 232 // Ensure that instances are deleted when their NavigationEntries are gone.
221 TestSiteInstance* instance = 233 TestSiteInstance* instance =
222 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter, 234 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter,
223 &browsing_delete_counter); 235 &browsing_delete_counter);
224 EXPECT_EQ(0, site_delete_counter); 236 EXPECT_EQ(0, site_delete_counter);
225 237
226 NavigationEntryImpl* e1 = new NavigationEntryImpl( 238 NavigationEntryImpl* e1 = new NavigationEntryImpl(
227 instance, 0, url, Referrer(), string16(), PAGE_TRANSITION_LINK, false); 239 instance, 0, url, content::Referrer(), string16(),
240 content::PAGE_TRANSITION_LINK, false);
228 241
229 // Redundantly setting e1's SiteInstance shouldn't affect the ref count. 242 // Redundantly setting e1's SiteInstance shouldn't affect the ref count.
230 e1->set_site_instance(instance); 243 e1->set_site_instance(instance);
231 EXPECT_EQ(0, site_delete_counter); 244 EXPECT_EQ(0, site_delete_counter);
232 245
233 // Add a second reference 246 // Add a second reference
234 NavigationEntryImpl* e2 = new NavigationEntryImpl( 247 NavigationEntryImpl* e2 = new NavigationEntryImpl(
235 instance, 0, url, Referrer(), string16(), PAGE_TRANSITION_LINK, false); 248 instance, 0, url, content::Referrer(), string16(),
249 content::PAGE_TRANSITION_LINK, false);
236 250
237 // Now delete both entries and be sure the SiteInstance goes away. 251 // Now delete both entries and be sure the SiteInstance goes away.
238 delete e1; 252 delete e1;
239 EXPECT_EQ(0, site_delete_counter); 253 EXPECT_EQ(0, site_delete_counter);
240 EXPECT_EQ(0, browsing_delete_counter); 254 EXPECT_EQ(0, browsing_delete_counter);
241 delete e2; 255 delete e2;
242 EXPECT_EQ(1, site_delete_counter); 256 EXPECT_EQ(1, site_delete_counter);
243 // instance is now deleted 257 // instance is now deleted
244 EXPECT_EQ(1, browsing_delete_counter); 258 EXPECT_EQ(1, browsing_delete_counter);
245 // browsing_instance is now deleted 259 // browsing_instance is now deleted
246 260
247 // Ensure that instances are deleted when their RenderViewHosts are gone. 261 // Ensure that instances are deleted when their RenderViewHosts are gone.
248 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 262 scoped_ptr<content::TestBrowserContext> browser_context(
263 new content::TestBrowserContext());
249 instance = 264 instance =
250 TestSiteInstance::CreateTestSiteInstance(browser_context.get(), 265 TestSiteInstance::CreateTestSiteInstance(browser_context.get(),
251 &site_delete_counter, 266 &site_delete_counter,
252 &browsing_delete_counter); 267 &browsing_delete_counter);
253 { 268 {
254 scoped_ptr<WebContentsImpl> web_contents( 269 scoped_ptr<WebContentsImpl> web_contents(
255 WebContentsImpl::Create(browser_context.get(), instance, 270 WebContentsImpl::Create(browser_context.get(), instance,
256 MSG_ROUTING_NONE, NULL)); 271 MSG_ROUTING_NONE, NULL));
257 EXPECT_EQ(1, site_delete_counter); 272 EXPECT_EQ(1, site_delete_counter);
258 EXPECT_EQ(1, browsing_delete_counter); 273 EXPECT_EQ(1, browsing_delete_counter);
(...skipping 18 matching lines...) Expand all
277 const GURL url("test:foo"); 292 const GURL url("test:foo");
278 293
279 SiteInstanceImpl* instance1 = 294 SiteInstanceImpl* instance1 =
280 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter1, 295 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter1,
281 &browsing_delete_counter); 296 &browsing_delete_counter);
282 SiteInstanceImpl* instance2 = 297 SiteInstanceImpl* instance2 =
283 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter2, 298 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter2,
284 &browsing_delete_counter); 299 &browsing_delete_counter);
285 300
286 NavigationEntryImpl* e1 = new NavigationEntryImpl( 301 NavigationEntryImpl* e1 = new NavigationEntryImpl(
287 instance1, 0, url, Referrer(), string16(), PAGE_TRANSITION_LINK, false); 302 instance1, 0, url, content::Referrer(), string16(),
303 content::PAGE_TRANSITION_LINK, false);
288 // Clone the entry 304 // Clone the entry
289 NavigationEntryImpl* e2 = new NavigationEntryImpl(*e1); 305 NavigationEntryImpl* e2 = new NavigationEntryImpl(*e1);
290 306
291 // Should be able to change the SiteInstance of the cloned entry. 307 // Should be able to change the SiteInstance of the cloned entry.
292 e2->set_site_instance(instance2); 308 e2->set_site_instance(instance2);
293 309
294 // The first SiteInstance should go away after deleting e1, since e2 should 310 // The first SiteInstance should go away after deleting e1, since e2 should
295 // no longer be referencing it. 311 // no longer be referencing it.
296 delete e1; 312 delete e1;
297 EXPECT_EQ(1, site_delete_counter1); 313 EXPECT_EQ(1, site_delete_counter1);
298 EXPECT_EQ(0, site_delete_counter2); 314 EXPECT_EQ(0, site_delete_counter2);
299 315
300 // The second SiteInstance should go away after deleting e2. 316 // The second SiteInstance should go away after deleting e2.
301 delete e2; 317 delete e2;
302 EXPECT_EQ(1, site_delete_counter1); 318 EXPECT_EQ(1, site_delete_counter1);
303 EXPECT_EQ(1, site_delete_counter2); 319 EXPECT_EQ(1, site_delete_counter2);
304 320
305 // Both BrowsingInstances are also now deleted 321 // Both BrowsingInstances are also now deleted
306 EXPECT_EQ(2, browsing_delete_counter); 322 EXPECT_EQ(2, browsing_delete_counter);
307 323
308 DrainMessageLoops(); 324 DrainMessageLoops();
309 } 325 }
310 326
311 // Test to ensure GetProcess returns and creates processes correctly. 327 // Test to ensure GetProcess returns and creates processes correctly.
312 TEST_F(SiteInstanceTest, GetProcess) { 328 TEST_F(SiteInstanceTest, GetProcess) {
313 // Ensure that GetProcess returns a process. 329 // Ensure that GetProcess returns a process.
314 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 330 scoped_ptr<content::TestBrowserContext> browser_context(
315 scoped_ptr<RenderProcessHost> host1; 331 new content::TestBrowserContext());
332 scoped_ptr<content::RenderProcessHost> host1;
316 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>( 333 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
317 SiteInstance::Create(browser_context.get()))); 334 SiteInstance::Create(browser_context.get())));
318 host1.reset(instance->GetProcess()); 335 host1.reset(instance->GetProcess());
319 EXPECT_TRUE(host1.get() != NULL); 336 EXPECT_TRUE(host1.get() != NULL);
320 337
321 // Ensure that GetProcess creates a new process. 338 // Ensure that GetProcess creates a new process.
322 scoped_refptr<SiteInstanceImpl> instance2(static_cast<SiteInstanceImpl*>( 339 scoped_refptr<SiteInstanceImpl> instance2(static_cast<SiteInstanceImpl*>(
323 SiteInstance::Create(browser_context.get()))); 340 SiteInstance::Create(browser_context.get())));
324 scoped_ptr<RenderProcessHost> host2(instance2->GetProcess()); 341 scoped_ptr<content::RenderProcessHost> host2(instance2->GetProcess());
325 EXPECT_TRUE(host2.get() != NULL); 342 EXPECT_TRUE(host2.get() != NULL);
326 EXPECT_NE(host1.get(), host2.get()); 343 EXPECT_NE(host1.get(), host2.get());
327 344
328 DrainMessageLoops(); 345 DrainMessageLoops();
329 } 346 }
330 347
331 // Test to ensure SetSite and site() work properly. 348 // Test to ensure SetSite and site() work properly.
332 TEST_F(SiteInstanceTest, SetSite) { 349 TEST_F(SiteInstanceTest, SetSite) {
333 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>( 350 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
334 SiteInstance::Create(NULL))); 351 SiteInstance::Create(NULL)));
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 422
406 DrainMessageLoops(); 423 DrainMessageLoops();
407 } 424 }
408 425
409 // Test to ensure that there is only one SiteInstance per site in a given 426 // Test to ensure that there is only one SiteInstance per site in a given
410 // BrowsingInstance, when process-per-site is not in use. 427 // BrowsingInstance, when process-per-site is not in use.
411 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { 428 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) {
412 ASSERT_FALSE(CommandLine::ForCurrentProcess()->HasSwitch( 429 ASSERT_FALSE(CommandLine::ForCurrentProcess()->HasSwitch(
413 switches::kProcessPerSite)); 430 switches::kProcessPerSite));
414 int delete_counter = 0; 431 int delete_counter = 0;
415 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 432 scoped_ptr<content::TestBrowserContext> browser_context(
433 new content::TestBrowserContext());
416 TestBrowsingInstance* browsing_instance = 434 TestBrowsingInstance* browsing_instance =
417 new TestBrowsingInstance(browser_context.get(), &delete_counter); 435 new TestBrowsingInstance(browser_context.get(), &delete_counter);
418 436
419 const GURL url_a1("http://www.google.com/1.html"); 437 const GURL url_a1("http://www.google.com/1.html");
420 scoped_refptr<SiteInstanceImpl> site_instance_a1( 438 scoped_refptr<SiteInstanceImpl> site_instance_a1(
421 static_cast<SiteInstanceImpl*>( 439 static_cast<SiteInstanceImpl*>(
422 browsing_instance->GetSiteInstanceForURL(url_a1))); 440 browsing_instance->GetSiteInstanceForURL(url_a1)));
423 EXPECT_TRUE(site_instance_a1.get() != NULL); 441 EXPECT_TRUE(site_instance_a1.get() != NULL);
424 442
425 // A separate site should create a separate SiteInstance. 443 // A separate site should create a separate SiteInstance.
(...skipping 22 matching lines...) Expand all
448 new TestBrowsingInstance(browser_context.get(), &delete_counter); 466 new TestBrowsingInstance(browser_context.get(), &delete_counter);
449 // Ensure the new SiteInstance is ref counted so that it gets deleted. 467 // Ensure the new SiteInstance is ref counted so that it gets deleted.
450 scoped_refptr<SiteInstanceImpl> site_instance_a2_2( 468 scoped_refptr<SiteInstanceImpl> site_instance_a2_2(
451 static_cast<SiteInstanceImpl*>( 469 static_cast<SiteInstanceImpl*>(
452 browsing_instance2->GetSiteInstanceForURL(url_a2))); 470 browsing_instance2->GetSiteInstanceForURL(url_a2)));
453 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); 471 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get());
454 EXPECT_FALSE(site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2)); 472 EXPECT_FALSE(site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2));
455 473
456 // The two SiteInstances for http://google.com should not use the same process 474 // The two SiteInstances for http://google.com should not use the same process
457 // if process-per-site is not enabled. 475 // if process-per-site is not enabled.
458 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess()); 476 scoped_ptr<content::RenderProcessHost> process_a1(
459 scoped_ptr<RenderProcessHost> process_a2_2( 477 site_instance_a1->GetProcess());
478 scoped_ptr<content::RenderProcessHost> process_a2_2(
460 site_instance_a2_2->GetProcess()); 479 site_instance_a2_2->GetProcess());
461 EXPECT_NE(process_a1.get(), process_a2_2.get()); 480 EXPECT_NE(process_a1.get(), process_a2_2.get());
462 481
463 // Should be able to see that we do have SiteInstances. 482 // Should be able to see that we do have SiteInstances.
464 EXPECT_TRUE(browsing_instance->HasSiteInstance( 483 EXPECT_TRUE(browsing_instance->HasSiteInstance(
465 GURL("http://mail.google.com"))); 484 GURL("http://mail.google.com")));
466 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 485 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
467 GURL("http://mail.google.com"))); 486 GURL("http://mail.google.com")));
468 EXPECT_TRUE(browsing_instance->HasSiteInstance( 487 EXPECT_TRUE(browsing_instance->HasSiteInstance(
469 GURL("http://mail.yahoo.com"))); 488 GURL("http://mail.yahoo.com")));
470 489
471 // Should be able to see that we don't have SiteInstances. 490 // Should be able to see that we don't have SiteInstances.
472 EXPECT_FALSE(browsing_instance->HasSiteInstance( 491 EXPECT_FALSE(browsing_instance->HasSiteInstance(
473 GURL("https://www.google.com"))); 492 GURL("https://www.google.com")));
474 EXPECT_FALSE(browsing_instance2->HasSiteInstance( 493 EXPECT_FALSE(browsing_instance2->HasSiteInstance(
475 GURL("http://www.yahoo.com"))); 494 GURL("http://www.yahoo.com")));
476 495
477 // browsing_instances will be deleted when their SiteInstances are deleted. 496 // browsing_instances will be deleted when their SiteInstances are deleted.
478 // The processes will be unregistered when the RPH scoped_ptrs go away. 497 // The processes will be unregistered when the RPH scoped_ptrs go away.
479 498
480 DrainMessageLoops(); 499 DrainMessageLoops();
481 } 500 }
482 501
483 // Test to ensure that there is only one RenderProcessHost per site for an 502 // Test to ensure that there is only one RenderProcessHost per site for an
484 // entire BrowserContext, if process-per-site is in use. 503 // entire BrowserContext, if process-per-site is in use.
485 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { 504 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) {
486 CommandLine::ForCurrentProcess()->AppendSwitch( 505 CommandLine::ForCurrentProcess()->AppendSwitch(
487 switches::kProcessPerSite); 506 switches::kProcessPerSite);
488 int delete_counter = 0; 507 int delete_counter = 0;
489 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 508 scoped_ptr<content::TestBrowserContext> browser_context(
509 new content::TestBrowserContext());
490 TestBrowsingInstance* browsing_instance = 510 TestBrowsingInstance* browsing_instance =
491 new TestBrowsingInstance(browser_context.get(), &delete_counter); 511 new TestBrowsingInstance(browser_context.get(), &delete_counter);
492 512
493 const GURL url_a1("http://www.google.com/1.html"); 513 const GURL url_a1("http://www.google.com/1.html");
494 scoped_refptr<SiteInstanceImpl> site_instance_a1( 514 scoped_refptr<SiteInstanceImpl> site_instance_a1(
495 static_cast<SiteInstanceImpl*>( 515 static_cast<SiteInstanceImpl*>(
496 browsing_instance->GetSiteInstanceForURL(url_a1))); 516 browsing_instance->GetSiteInstanceForURL(url_a1)));
497 EXPECT_TRUE(site_instance_a1.get() != NULL); 517 EXPECT_TRUE(site_instance_a1.get() != NULL);
498 scoped_ptr<RenderProcessHost> process_a1( 518 scoped_ptr<content::RenderProcessHost> process_a1(
499 site_instance_a1->GetProcess()); 519 site_instance_a1->GetProcess());
500 520
501 // A separate site should create a separate SiteInstance. 521 // A separate site should create a separate SiteInstance.
502 const GURL url_b1("http://www.yahoo.com/"); 522 const GURL url_b1("http://www.yahoo.com/");
503 scoped_refptr<SiteInstanceImpl> site_instance_b1( 523 scoped_refptr<SiteInstanceImpl> site_instance_b1(
504 static_cast<SiteInstanceImpl*>( 524 static_cast<SiteInstanceImpl*>(
505 browsing_instance->GetSiteInstanceForURL(url_b1))); 525 browsing_instance->GetSiteInstanceForURL(url_b1)));
506 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); 526 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get());
507 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1)); 527 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1));
508 528
(...skipping 15 matching lines...) Expand all
524 new TestBrowsingInstance(browser_context.get(), &delete_counter); 544 new TestBrowsingInstance(browser_context.get(), &delete_counter);
525 scoped_refptr<SiteInstanceImpl> site_instance_a1_2( 545 scoped_refptr<SiteInstanceImpl> site_instance_a1_2(
526 static_cast<SiteInstanceImpl*>( 546 static_cast<SiteInstanceImpl*>(
527 browsing_instance2->GetSiteInstanceForURL(url_a1))); 547 browsing_instance2->GetSiteInstanceForURL(url_a1)));
528 EXPECT_TRUE(site_instance_a1.get() != NULL); 548 EXPECT_TRUE(site_instance_a1.get() != NULL);
529 EXPECT_NE(site_instance_a1.get(), site_instance_a1_2.get()); 549 EXPECT_NE(site_instance_a1.get(), site_instance_a1_2.get());
530 EXPECT_EQ(process_a1.get(), site_instance_a1_2->GetProcess()); 550 EXPECT_EQ(process_a1.get(), site_instance_a1_2->GetProcess());
531 551
532 // A visit to the original site in a new BrowsingInstance (different browser 552 // A visit to the original site in a new BrowsingInstance (different browser
533 // context) should return a different SiteInstance with a different process. 553 // context) should return a different SiteInstance with a different process.
534 scoped_ptr<TestBrowserContext> browser_context2(new TestBrowserContext()); 554 scoped_ptr<content::TestBrowserContext> browser_context2(
555 new content::TestBrowserContext());
535 TestBrowsingInstance* browsing_instance3 = 556 TestBrowsingInstance* browsing_instance3 =
536 new TestBrowsingInstance(browser_context2.get(), &delete_counter); 557 new TestBrowsingInstance(browser_context2.get(), &delete_counter);
537 scoped_refptr<SiteInstanceImpl> site_instance_a2_3( 558 scoped_refptr<SiteInstanceImpl> site_instance_a2_3(
538 static_cast<SiteInstanceImpl*>( 559 static_cast<SiteInstanceImpl*>(
539 browsing_instance3->GetSiteInstanceForURL(url_a2))); 560 browsing_instance3->GetSiteInstanceForURL(url_a2)));
540 EXPECT_TRUE(site_instance_a2_3.get() != NULL); 561 EXPECT_TRUE(site_instance_a2_3.get() != NULL);
541 scoped_ptr<RenderProcessHost> process_a2_3( 562 scoped_ptr<content::RenderProcessHost> process_a2_3(
542 site_instance_a2_3->GetProcess()); 563 site_instance_a2_3->GetProcess());
543 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); 564 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get());
544 EXPECT_NE(process_a1.get(), process_a2_3.get()); 565 EXPECT_NE(process_a1.get(), process_a2_3.get());
545 566
546 // Should be able to see that we do have SiteInstances. 567 // Should be able to see that we do have SiteInstances.
547 EXPECT_TRUE(browsing_instance->HasSiteInstance( 568 EXPECT_TRUE(browsing_instance->HasSiteInstance(
548 GURL("http://mail.google.com"))); // visited before 569 GURL("http://mail.google.com"))); // visited before
549 EXPECT_TRUE(browsing_instance2->HasSiteInstance( 570 EXPECT_TRUE(browsing_instance2->HasSiteInstance(
550 GURL("http://mail.google.com"))); // visited before 571 GURL("http://mail.google.com"))); // visited before
551 EXPECT_TRUE(browsing_instance->HasSiteInstance( 572 EXPECT_TRUE(browsing_instance->HasSiteInstance(
552 GURL("http://mail.yahoo.com"))); // visited before 573 GURL("http://mail.yahoo.com"))); // visited before
553 574
554 // Should be able to see that we don't have SiteInstances. 575 // Should be able to see that we don't have SiteInstances.
555 EXPECT_FALSE(browsing_instance2->HasSiteInstance( 576 EXPECT_FALSE(browsing_instance2->HasSiteInstance(
556 GURL("http://www.yahoo.com"))); // different BI, same browser context 577 GURL("http://www.yahoo.com"))); // different BI, same browser context
557 EXPECT_FALSE(browsing_instance->HasSiteInstance( 578 EXPECT_FALSE(browsing_instance->HasSiteInstance(
558 GURL("https://www.google.com"))); // not visited before 579 GURL("https://www.google.com"))); // not visited before
559 EXPECT_FALSE(browsing_instance3->HasSiteInstance( 580 EXPECT_FALSE(browsing_instance3->HasSiteInstance(
560 GURL("http://www.yahoo.com"))); // different BI, different context 581 GURL("http://www.yahoo.com"))); // different BI, different context
561 582
562 // browsing_instances will be deleted when their SiteInstances are deleted. 583 // browsing_instances will be deleted when their SiteInstances are deleted.
563 // The processes will be unregistered when the RPH scoped_ptrs go away. 584 // The processes will be unregistered when the RPH scoped_ptrs go away.
564 585
565 DrainMessageLoops(); 586 DrainMessageLoops();
566 } 587 }
567 588
568 static SiteInstanceImpl* CreateSiteInstance(BrowserContext* browser_context, 589 static SiteInstanceImpl* CreateSiteInstance(
569 RenderProcessHostFactory* factory, 590 content::BrowserContext* browser_context,
570 const GURL& url) { 591 content::RenderProcessHostFactory* factory,
592 const GURL& url) {
571 SiteInstanceImpl* instance = 593 SiteInstanceImpl* instance =
572 reinterpret_cast<SiteInstanceImpl*>( 594 reinterpret_cast<SiteInstanceImpl*>(
573 SiteInstance::CreateForURL(browser_context, url)); 595 SiteInstance::CreateForURL(browser_context, url));
574 instance->set_render_process_host_factory(factory); 596 instance->set_render_process_host_factory(factory);
575 return instance; 597 return instance;
576 } 598 }
577 599
578 // Test to ensure that pages that require certain privileges are grouped 600 // Test to ensure that pages that require certain privileges are grouped
579 // in processes with similar pages. 601 // in processes with similar pages.
580 TEST_F(SiteInstanceTest, ProcessSharingByType) { 602 TEST_F(SiteInstanceTest, ProcessSharingByType) {
581 MockRenderProcessHostFactory rph_factory; 603 MockRenderProcessHostFactory rph_factory;
582 ChildProcessSecurityPolicyImpl* policy = 604 ChildProcessSecurityPolicyImpl* policy =
583 ChildProcessSecurityPolicyImpl::GetInstance(); 605 ChildProcessSecurityPolicyImpl::GetInstance();
584 606
585 // Make a bunch of mock renderers so that we hit the limit. 607 // Make a bunch of mock renderers so that we hit the limit.
586 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 608 scoped_ptr<content::TestBrowserContext> browser_context(
609 new content::TestBrowserContext());
587 std::vector<MockRenderProcessHost*> hosts; 610 std::vector<MockRenderProcessHost*> hosts;
588 for (size_t i = 0; i < kMaxRendererProcessCount; ++i) 611 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i)
589 hosts.push_back(new MockRenderProcessHost(browser_context.get())); 612 hosts.push_back(new MockRenderProcessHost(browser_context.get()));
590 613
591 // Create some extension instances and make sure they share a process. 614 // Create some extension instances and make sure they share a process.
592 scoped_refptr<SiteInstanceImpl> extension1_instance( 615 scoped_refptr<SiteInstanceImpl> extension1_instance(
593 CreateSiteInstance(browser_context.get(), &rph_factory, 616 CreateSiteInstance(browser_context.get(), &rph_factory,
594 GURL(kPrivilegedScheme + std::string("://foo/bar")))); 617 GURL(kPrivilegedScheme + std::string("://foo/bar"))));
595 set_privileged_process_id(extension1_instance->GetProcess()->GetID()); 618 set_privileged_process_id(extension1_instance->GetProcess()->GetID());
596 619
597 scoped_refptr<SiteInstanceImpl> extension2_instance( 620 scoped_refptr<SiteInstanceImpl> extension2_instance(
598 CreateSiteInstance(browser_context.get(), &rph_factory, 621 CreateSiteInstance(browser_context.get(), &rph_factory,
599 GURL(kPrivilegedScheme + std::string("://baz/bar")))); 622 GURL(kPrivilegedScheme + std::string("://baz/bar"))));
600 623
601 scoped_ptr<RenderProcessHost> extension_host( 624 scoped_ptr<content::RenderProcessHost> extension_host(
602 extension1_instance->GetProcess()); 625 extension1_instance->GetProcess());
603 EXPECT_EQ(extension1_instance->GetProcess(), 626 EXPECT_EQ(extension1_instance->GetProcess(),
604 extension2_instance->GetProcess()); 627 extension2_instance->GetProcess());
605 628
606 // Create some WebUI instances and make sure they share a process. 629 // Create some WebUI instances and make sure they share a process.
607 scoped_refptr<SiteInstanceImpl> webui1_instance(CreateSiteInstance( 630 scoped_refptr<SiteInstanceImpl> webui1_instance(CreateSiteInstance(
608 browser_context.get(), &rph_factory, 631 browser_context.get(), &rph_factory,
609 GURL(chrome::kChromeUIScheme + std::string("://newtab")))); 632 GURL(chrome::kChromeUIScheme + std::string("://newtab"))));
610 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID()); 633 policy->GrantWebUIBindings(webui1_instance->GetProcess()->GetID());
611 634
612 scoped_refptr<SiteInstanceImpl> webui2_instance(CreateSiteInstance( 635 scoped_refptr<SiteInstanceImpl> webui2_instance(CreateSiteInstance(
613 browser_context.get(), &rph_factory, 636 browser_context.get(), &rph_factory,
614 GURL(chrome::kChromeUIScheme + std::string("://history")))); 637 GURL(chrome::kChromeUIScheme + std::string("://history"))));
615 638
616 scoped_ptr<RenderProcessHost> dom_host(webui1_instance->GetProcess()); 639 scoped_ptr<content::RenderProcessHost> dom_host(
640 webui1_instance->GetProcess());
617 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess()); 641 EXPECT_EQ(webui1_instance->GetProcess(), webui2_instance->GetProcess());
618 642
619 // Make sure none of differing privilege processes are mixed. 643 // Make sure none of differing privilege processes are mixed.
620 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess()); 644 EXPECT_NE(extension1_instance->GetProcess(), webui1_instance->GetProcess());
621 645
622 for (size_t i = 0; i < kMaxRendererProcessCount; ++i) { 646 for (size_t i = 0; i < content::kMaxRendererProcessCount; ++i) {
623 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]); 647 EXPECT_NE(extension1_instance->GetProcess(), hosts[i]);
624 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]); 648 EXPECT_NE(webui1_instance->GetProcess(), hosts[i]);
625 } 649 }
626 650
627 STLDeleteContainerPointers(hosts.begin(), hosts.end()); 651 STLDeleteContainerPointers(hosts.begin(), hosts.end());
628 652
629 DrainMessageLoops(); 653 DrainMessageLoops();
630 } 654 }
631 655
632 // Test to ensure that HasWrongProcessForURL behaves properly for different 656 // Test to ensure that HasWrongProcessForURL behaves properly for different
633 // types of URLs. 657 // types of URLs.
634 TEST_F(SiteInstanceTest, HasWrongProcessForURL) { 658 TEST_F(SiteInstanceTest, HasWrongProcessForURL) {
635 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); 659 scoped_ptr<content::TestBrowserContext> browser_context(
636 scoped_ptr<RenderProcessHost> host; 660 new content::TestBrowserContext());
661 scoped_ptr<content::RenderProcessHost> host;
637 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>( 662 scoped_refptr<SiteInstanceImpl> instance(static_cast<SiteInstanceImpl*>(
638 SiteInstance::Create(browser_context.get()))); 663 SiteInstance::Create(browser_context.get())));
639 664
640 EXPECT_FALSE(instance->HasSite()); 665 EXPECT_FALSE(instance->HasSite());
641 EXPECT_TRUE(instance->GetSiteURL().is_empty()); 666 EXPECT_TRUE(instance->GetSiteURL().is_empty());
642 667
643 instance->SetSite(GURL("http://evernote.com/")); 668 instance->SetSite(GURL("http://evernote.com/"));
644 EXPECT_TRUE(instance->HasSite()); 669 EXPECT_TRUE(instance->HasSite());
645 670
646 // Check prior to "assigning" a process to the instance, which is expected 671 // 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. 672 // to return false due to not being attached to any process yet.
648 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); 673 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com")));
649 674
650 // The call to GetProcess actually creates a new real process, which works 675 // The call to GetProcess actually creates a new real process, which works
651 // fine, but might be a cause for problems in different contexts. 676 // fine, but might be a cause for problems in different contexts.
652 host.reset(instance->GetProcess()); 677 host.reset(instance->GetProcess());
653 EXPECT_TRUE(host.get() != NULL); 678 EXPECT_TRUE(host.get() != NULL);
654 EXPECT_TRUE(instance->HasProcess()); 679 EXPECT_TRUE(instance->HasProcess());
655 680
656 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); 681 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com")));
657 EXPECT_FALSE(instance->HasWrongProcessForURL( 682 EXPECT_FALSE(instance->HasWrongProcessForURL(
658 GURL("javascript:alert(document.location.href);"))); 683 GURL("javascript:alert(document.location.href);")));
659 684
660 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings"))); 685 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://settings")));
661 686
662 DrainMessageLoops(); 687 DrainMessageLoops();
663 } 688 }
664
665 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/site_instance_impl.cc ('k') | content/browser/system_message_window_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698