OLD | NEW |
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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "content/public/test/test_browser_context.h" | 27 #include "content/public/test/test_browser_context.h" |
28 #include "content/public/test/test_browser_thread.h" | 28 #include "content/public/test/test_browser_thread.h" |
29 #include "content/public/test/test_utils.h" | 29 #include "content/public/test/test_utils.h" |
30 #include "content/test/test_content_browser_client.h" | 30 #include "content/test/test_content_browser_client.h" |
31 #include "content/test/test_content_client.h" | 31 #include "content/test/test_content_client.h" |
32 #include "content/test/test_render_view_host.h" | 32 #include "content/test/test_render_view_host.h" |
33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
34 #include "url/url_util.h" | 34 #include "url/url_util.h" |
35 | 35 |
36 namespace content { | 36 namespace content { |
37 namespace { | |
38 | 37 |
39 const char kPrivilegedScheme[] = "privileged"; | 38 const char kPrivilegedScheme[] = "privileged"; |
40 | 39 |
41 class SiteInstanceTestBrowserClient : public TestContentBrowserClient { | 40 class SiteInstanceTestBrowserClient : public TestContentBrowserClient { |
42 public: | 41 public: |
43 SiteInstanceTestBrowserClient() | 42 explicit SiteInstanceTestBrowserClient() |
44 : privileged_process_id_(-1) { | 43 : privileged_process_id_(-1), |
| 44 site_instance_delete_count_(0), |
| 45 browsing_instance_delete_count_(0) { |
45 WebUIControllerFactory::RegisterFactory( | 46 WebUIControllerFactory::RegisterFactory( |
46 ContentWebUIControllerFactory::GetInstance()); | 47 ContentWebUIControllerFactory::GetInstance()); |
47 } | 48 } |
48 | 49 |
49 ~SiteInstanceTestBrowserClient() override { | 50 ~SiteInstanceTestBrowserClient() override { |
50 WebUIControllerFactory::UnregisterFactoryForTesting( | 51 WebUIControllerFactory::UnregisterFactoryForTesting( |
51 ContentWebUIControllerFactory::GetInstance()); | 52 ContentWebUIControllerFactory::GetInstance()); |
52 } | 53 } |
53 | 54 |
54 bool IsSuitableHost(RenderProcessHost* process_host, | 55 bool IsSuitableHost(RenderProcessHost* process_host, |
55 const GURL& site_url) override { | 56 const GURL& site_url) override { |
56 return (privileged_process_id_ == process_host->GetID()) == | 57 return (privileged_process_id_ == process_host->GetID()) == |
57 site_url.SchemeIs(kPrivilegedScheme); | 58 site_url.SchemeIs(kPrivilegedScheme); |
58 } | 59 } |
59 | 60 |
60 void set_privileged_process_id(int process_id) { | 61 void set_privileged_process_id(int process_id) { |
61 privileged_process_id_ = process_id; | 62 privileged_process_id_ = process_id; |
62 } | 63 } |
63 | 64 |
| 65 void SiteInstanceDeleting(content::SiteInstance* site_instance) override { |
| 66 site_instance_delete_count_++; |
| 67 // Infer deletion of the browsing instance. |
| 68 if (static_cast<SiteInstanceImpl*>(site_instance) |
| 69 ->browsing_instance_->HasOneRef()) { |
| 70 browsing_instance_delete_count_++; |
| 71 } |
| 72 } |
| 73 |
| 74 int GetAndClearSiteInstanceDeleteCount() { |
| 75 int result = site_instance_delete_count_; |
| 76 site_instance_delete_count_ = 0; |
| 77 return result; |
| 78 } |
| 79 |
| 80 int GetAndClearBrowsingInstanceDeleteCount() { |
| 81 int result = browsing_instance_delete_count_; |
| 82 browsing_instance_delete_count_ = 0; |
| 83 return result; |
| 84 } |
| 85 |
64 private: | 86 private: |
65 int privileged_process_id_; | 87 int privileged_process_id_; |
| 88 |
| 89 int site_instance_delete_count_; |
| 90 int browsing_instance_delete_count_; |
66 }; | 91 }; |
67 | 92 |
68 class SiteInstanceTest : public testing::Test { | 93 class SiteInstanceTest : public testing::Test { |
69 public: | 94 public: |
70 SiteInstanceTest() | 95 SiteInstanceTest() |
71 : ui_thread_(BrowserThread::UI, &message_loop_), | 96 : ui_thread_(BrowserThread::UI, &message_loop_), |
72 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, | 97 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, |
73 &message_loop_), | 98 &message_loop_), |
74 io_thread_(BrowserThread::IO, &message_loop_), | 99 io_thread_(BrowserThread::IO, &message_loop_), |
75 old_browser_client_(NULL) { | 100 old_browser_client_(nullptr) {} |
76 } | |
77 | 101 |
78 void SetUp() override { | 102 void SetUp() override { |
79 old_browser_client_ = SetBrowserClientForTesting(&browser_client_); | 103 old_browser_client_ = SetBrowserClientForTesting(&browser_client_); |
80 url::AddStandardScheme(kPrivilegedScheme, url::SCHEME_WITHOUT_PORT); | 104 url::AddStandardScheme(kPrivilegedScheme, url::SCHEME_WITHOUT_PORT); |
81 url::AddStandardScheme(kChromeUIScheme, url::SCHEME_WITHOUT_PORT); | 105 url::AddStandardScheme(kChromeUIScheme, url::SCHEME_WITHOUT_PORT); |
82 | 106 |
83 SiteInstanceImpl::set_render_process_host_factory(&rph_factory_); | 107 SiteInstanceImpl::set_render_process_host_factory(&rph_factory_); |
84 } | 108 } |
85 | 109 |
86 void TearDown() override { | 110 void TearDown() override { |
87 // Ensure that no RenderProcessHosts are left over after the tests. | 111 // Ensure that no RenderProcessHosts are left over after the tests. |
88 EXPECT_TRUE(RenderProcessHost::AllHostsIterator().IsAtEnd()); | 112 EXPECT_TRUE(RenderProcessHost::AllHostsIterator().IsAtEnd()); |
89 | 113 |
90 SetBrowserClientForTesting(old_browser_client_); | 114 SetBrowserClientForTesting(old_browser_client_); |
91 SiteInstanceImpl::set_render_process_host_factory(NULL); | 115 SiteInstanceImpl::set_render_process_host_factory(nullptr); |
92 | 116 |
93 // http://crbug.com/143565 found SiteInstanceTest leaking an | 117 // http://crbug.com/143565 found SiteInstanceTest leaking an |
94 // AppCacheDatabase. This happens because some part of the test indirectly | 118 // AppCacheDatabase. This happens because some part of the test indirectly |
95 // calls StoragePartitionImplMap::PostCreateInitialization(), which posts | 119 // calls StoragePartitionImplMap::PostCreateInitialization(), which posts |
96 // a task to the IO thread to create the AppCacheDatabase. Since the | 120 // a task to the IO thread to create the AppCacheDatabase. Since the |
97 // message loop is not running, the AppCacheDatabase ends up getting | 121 // message loop is not running, the AppCacheDatabase ends up getting |
98 // created when DrainMessageLoops() gets called at the end of a test case. | 122 // created when DrainMessageLoops() gets called at the end of a test case. |
99 // Immediately after, the test case ends and the AppCacheDatabase gets | 123 // Immediately after, the test case ends and the AppCacheDatabase gets |
100 // scheduled for deletion. Here, call DrainMessageLoops() again so the | 124 // scheduled for deletion. Here, call DrainMessageLoops() again so the |
101 // AppCacheDatabase actually gets deleted. | 125 // AppCacheDatabase actually gets deleted. |
102 DrainMessageLoops(); | 126 DrainMessageLoops(); |
103 } | 127 } |
104 | 128 |
105 void set_privileged_process_id(int process_id) { | 129 void set_privileged_process_id(int process_id) { |
106 browser_client_.set_privileged_process_id(process_id); | 130 browser_client_.set_privileged_process_id(process_id); |
107 } | 131 } |
108 | 132 |
109 void DrainMessageLoops() { | 133 void DrainMessageLoops() { |
110 // We don't just do this in TearDown() because we create TestBrowserContext | 134 // We don't just do this in TearDown() because we create TestBrowserContext |
111 // objects in each test, which will be destructed before | 135 // objects in each test, which will be destructed before |
112 // TearDown() is called. | 136 // TearDown() is called. |
113 base::MessageLoop::current()->RunUntilIdle(); | 137 base::MessageLoop::current()->RunUntilIdle(); |
114 message_loop_.RunUntilIdle(); | 138 message_loop_.RunUntilIdle(); |
115 } | 139 } |
116 | 140 |
| 141 SiteInstanceTestBrowserClient* browser_client() { return &browser_client_; } |
| 142 |
117 private: | 143 private: |
118 base::MessageLoopForUI message_loop_; | 144 base::MessageLoopForUI message_loop_; |
119 TestBrowserThread ui_thread_; | 145 TestBrowserThread ui_thread_; |
120 TestBrowserThread file_user_blocking_thread_; | 146 TestBrowserThread file_user_blocking_thread_; |
121 TestBrowserThread io_thread_; | 147 TestBrowserThread io_thread_; |
122 | 148 |
123 SiteInstanceTestBrowserClient browser_client_; | 149 SiteInstanceTestBrowserClient browser_client_; |
124 ContentBrowserClient* old_browser_client_; | 150 ContentBrowserClient* old_browser_client_; |
125 MockRenderProcessHostFactory rph_factory_; | 151 MockRenderProcessHostFactory rph_factory_; |
126 }; | 152 }; |
127 | 153 |
128 // Subclass of BrowsingInstance that updates a counter when deleted and | |
129 // returns TestSiteInstances from GetSiteInstanceForURL. | |
130 class TestBrowsingInstance : public BrowsingInstance { | |
131 public: | |
132 TestBrowsingInstance(BrowserContext* browser_context, int* delete_counter) | |
133 : BrowsingInstance(browser_context), | |
134 delete_counter_(delete_counter) { | |
135 } | |
136 | |
137 // Make a few methods public for tests. | |
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 ~TestBrowsingInstance() override { (*delete_counter_)++; } | |
146 | |
147 int* delete_counter_; | |
148 }; | |
149 | |
150 // Subclass of SiteInstanceImpl that updates a counter when deleted. | |
151 class TestSiteInstance : public SiteInstanceImpl { | |
152 public: | |
153 static TestSiteInstance* CreateTestSiteInstance( | |
154 BrowserContext* browser_context, | |
155 int* site_delete_counter, | |
156 int* browsing_delete_counter) { | |
157 TestBrowsingInstance* browsing_instance = | |
158 new TestBrowsingInstance(browser_context, browsing_delete_counter); | |
159 return new TestSiteInstance(browsing_instance, site_delete_counter); | |
160 } | |
161 | |
162 private: | |
163 TestSiteInstance(BrowsingInstance* browsing_instance, int* delete_counter) | |
164 : SiteInstanceImpl(browsing_instance), delete_counter_(delete_counter) {} | |
165 ~TestSiteInstance() override { (*delete_counter_)++; } | |
166 | |
167 int* delete_counter_; | |
168 }; | |
169 | |
170 } // namespace | |
171 | |
172 // Test to ensure no memory leaks for SiteInstance objects. | 154 // Test to ensure no memory leaks for SiteInstance objects. |
173 TEST_F(SiteInstanceTest, SiteInstanceDestructor) { | 155 TEST_F(SiteInstanceTest, SiteInstanceDestructor) { |
174 // The existence of this object will cause WebContentsImpl to create our | 156 // The existence of this object will cause WebContentsImpl to create our |
175 // test one instead of the real one. | 157 // test one instead of the real one. |
176 RenderViewHostTestEnabler rvh_test_enabler; | 158 RenderViewHostTestEnabler rvh_test_enabler; |
177 int site_delete_counter = 0; | |
178 int browsing_delete_counter = 0; | |
179 const GURL url("test:foo"); | 159 const GURL url("test:foo"); |
180 | 160 |
181 // Ensure that instances are deleted when their NavigationEntries are gone. | 161 // Ensure that instances are deleted when their NavigationEntries are gone. |
182 TestSiteInstance* instance = | 162 scoped_refptr<SiteInstanceImpl> instance = SiteInstanceImpl::Create(nullptr); |
183 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter, | 163 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
184 &browsing_delete_counter); | |
185 EXPECT_EQ(0, site_delete_counter); | |
186 | 164 |
187 NavigationEntryImpl* e1 = new NavigationEntryImpl( | 165 NavigationEntryImpl* e1 = new NavigationEntryImpl( |
188 instance, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK, | 166 instance, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK, |
189 false); | 167 false); |
190 | 168 |
191 // Redundantly setting e1's SiteInstance shouldn't affect the ref count. | 169 // Redundantly setting e1's SiteInstance shouldn't affect the ref count. |
192 e1->set_site_instance(instance); | 170 e1->set_site_instance(instance); |
193 EXPECT_EQ(0, site_delete_counter); | 171 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 172 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
194 | 173 |
195 // Add a second reference | 174 // Add a second reference |
196 NavigationEntryImpl* e2 = new NavigationEntryImpl( | 175 NavigationEntryImpl* e2 = new NavigationEntryImpl( |
197 instance, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK, | 176 instance, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK, |
198 false); | 177 false); |
199 | 178 |
| 179 instance = nullptr; |
| 180 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 181 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| 182 |
200 // Now delete both entries and be sure the SiteInstance goes away. | 183 // Now delete both entries and be sure the SiteInstance goes away. |
201 delete e1; | 184 delete e1; |
202 EXPECT_EQ(0, site_delete_counter); | 185 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
203 EXPECT_EQ(0, browsing_delete_counter); | 186 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
204 delete e2; | 187 delete e2; |
205 EXPECT_EQ(1, site_delete_counter); | |
206 // instance is now deleted | 188 // instance is now deleted |
207 EXPECT_EQ(1, browsing_delete_counter); | 189 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 190 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
208 // browsing_instance is now deleted | 191 // browsing_instance is now deleted |
209 | 192 |
210 // Ensure that instances are deleted when their RenderViewHosts are gone. | 193 // Ensure that instances are deleted when their RenderViewHosts are gone. |
211 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 194 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
212 instance = | |
213 TestSiteInstance::CreateTestSiteInstance(browser_context.get(), | |
214 &site_delete_counter, | |
215 &browsing_delete_counter); | |
216 { | 195 { |
217 scoped_ptr<WebContentsImpl> web_contents(static_cast<WebContentsImpl*>( | 196 scoped_ptr<WebContentsImpl> web_contents(static_cast<WebContentsImpl*>( |
218 WebContents::Create(WebContents::CreateParams( | 197 WebContents::Create(WebContents::CreateParams( |
219 browser_context.get(), instance)))); | 198 browser_context.get(), |
220 EXPECT_EQ(1, site_delete_counter); | 199 SiteInstance::Create(browser_context.get()))))); |
221 EXPECT_EQ(1, browsing_delete_counter); | 200 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 201 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
222 } | 202 } |
223 | 203 |
224 // Make sure that we flush any messages related to the above WebContentsImpl | 204 // Make sure that we flush any messages related to the above WebContentsImpl |
225 // destruction. | 205 // destruction. |
226 DrainMessageLoops(); | 206 DrainMessageLoops(); |
227 | 207 |
228 EXPECT_EQ(2, site_delete_counter); | 208 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
229 EXPECT_EQ(2, browsing_delete_counter); | 209 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
230 // contents is now deleted, along with instance and browsing_instance | 210 // contents is now deleted, along with instance and browsing_instance |
231 } | 211 } |
232 | 212 |
233 // Test that NavigationEntries with SiteInstances can be cloned, but that their | 213 // Test that NavigationEntries with SiteInstances can be cloned, but that their |
234 // SiteInstances can be changed afterwards. Also tests that the ref counts are | 214 // SiteInstances can be changed afterwards. Also tests that the ref counts are |
235 // updated properly after the change. | 215 // updated properly after the change. |
236 TEST_F(SiteInstanceTest, CloneNavigationEntry) { | 216 TEST_F(SiteInstanceTest, CloneNavigationEntry) { |
237 int site_delete_counter1 = 0; | |
238 int site_delete_counter2 = 0; | |
239 int browsing_delete_counter = 0; | |
240 const GURL url("test:foo"); | 217 const GURL url("test:foo"); |
241 | 218 |
242 SiteInstanceImpl* instance1 = | 219 scoped_ptr<NavigationEntryImpl> e1 = make_scoped_ptr(new NavigationEntryImpl( |
243 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter1, | 220 SiteInstanceImpl::Create(nullptr), 0, url, Referrer(), base::string16(), |
244 &browsing_delete_counter); | 221 ui::PAGE_TRANSITION_LINK, false)); |
245 SiteInstanceImpl* instance2 = | |
246 TestSiteInstance::CreateTestSiteInstance(NULL, &site_delete_counter2, | |
247 &browsing_delete_counter); | |
248 | 222 |
249 scoped_ptr<NavigationEntryImpl> e1 = make_scoped_ptr(new NavigationEntryImpl( | |
250 instance1, 0, url, Referrer(), base::string16(), ui::PAGE_TRANSITION_LINK, | |
251 false)); | |
252 // Clone the entry. | 223 // Clone the entry. |
253 scoped_ptr<NavigationEntryImpl> e2 = e1->Clone(); | 224 scoped_ptr<NavigationEntryImpl> e2 = e1->Clone(); |
254 | 225 |
255 // Should be able to change the SiteInstance of the cloned entry. | 226 // Should be able to change the SiteInstance of the cloned entry. |
256 e2->set_site_instance(instance2); | 227 e2->set_site_instance(SiteInstanceImpl::Create(nullptr)); |
257 | 228 |
258 // The first SiteInstance should go away after resetting e1, since e2 should | 229 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
259 // no longer be referencing it. | 230 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| 231 |
| 232 // The first SiteInstance and BrowsingInstance should go away after resetting |
| 233 // e1, since e2 should no longer be referencing it. |
260 e1.reset(); | 234 e1.reset(); |
261 EXPECT_EQ(1, site_delete_counter1); | 235 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
262 EXPECT_EQ(0, site_delete_counter2); | 236 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
263 | 237 |
264 // The second SiteInstance should go away after resetting e2. | 238 // The second SiteInstance should go away after resetting e2. |
265 e2.reset(); | 239 e2.reset(); |
266 EXPECT_EQ(1, site_delete_counter1); | 240 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
267 EXPECT_EQ(1, site_delete_counter2); | 241 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
268 | |
269 // Both BrowsingInstances are also now deleted. | |
270 EXPECT_EQ(2, browsing_delete_counter); | |
271 | 242 |
272 DrainMessageLoops(); | 243 DrainMessageLoops(); |
273 } | 244 } |
274 | 245 |
275 // Test to ensure GetProcess returns and creates processes correctly. | 246 // Test to ensure GetProcess returns and creates processes correctly. |
276 TEST_F(SiteInstanceTest, GetProcess) { | 247 TEST_F(SiteInstanceTest, GetProcess) { |
277 // Ensure that GetProcess returns a process. | 248 // Ensure that GetProcess returns a process. |
278 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 249 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
279 scoped_ptr<RenderProcessHost> host1; | 250 scoped_ptr<RenderProcessHost> host1; |
280 scoped_refptr<SiteInstanceImpl> instance( | 251 scoped_refptr<SiteInstanceImpl> instance( |
281 SiteInstanceImpl::Create(browser_context.get())); | 252 SiteInstanceImpl::Create(browser_context.get())); |
282 host1.reset(instance->GetProcess()); | 253 host1.reset(instance->GetProcess()); |
283 EXPECT_TRUE(host1.get() != NULL); | 254 EXPECT_TRUE(host1.get() != nullptr); |
284 | 255 |
285 // Ensure that GetProcess creates a new process. | 256 // Ensure that GetProcess creates a new process. |
286 scoped_refptr<SiteInstanceImpl> instance2( | 257 scoped_refptr<SiteInstanceImpl> instance2( |
287 SiteInstanceImpl::Create(browser_context.get())); | 258 SiteInstanceImpl::Create(browser_context.get())); |
288 scoped_ptr<RenderProcessHost> host2(instance2->GetProcess()); | 259 scoped_ptr<RenderProcessHost> host2(instance2->GetProcess()); |
289 EXPECT_TRUE(host2.get() != NULL); | 260 EXPECT_TRUE(host2.get() != nullptr); |
290 EXPECT_NE(host1.get(), host2.get()); | 261 EXPECT_NE(host1.get(), host2.get()); |
291 | 262 |
292 DrainMessageLoops(); | 263 DrainMessageLoops(); |
293 } | 264 } |
294 | 265 |
295 // Test to ensure SetSite and site() work properly. | 266 // Test to ensure SetSite and site() work properly. |
296 TEST_F(SiteInstanceTest, SetSite) { | 267 TEST_F(SiteInstanceTest, SetSite) { |
297 scoped_refptr<SiteInstanceImpl> instance(SiteInstanceImpl::Create(NULL)); | 268 scoped_refptr<SiteInstanceImpl> instance(SiteInstanceImpl::Create(nullptr)); |
298 EXPECT_FALSE(instance->HasSite()); | 269 EXPECT_FALSE(instance->HasSite()); |
299 EXPECT_TRUE(instance->GetSiteURL().is_empty()); | 270 EXPECT_TRUE(instance->GetSiteURL().is_empty()); |
300 | 271 |
301 instance->SetSite(GURL("http://www.google.com/index.html")); | 272 instance->SetSite(GURL("http://www.google.com/index.html")); |
302 EXPECT_EQ(GURL("http://google.com"), instance->GetSiteURL()); | 273 EXPECT_EQ(GURL("http://google.com"), instance->GetSiteURL()); |
303 | 274 |
304 EXPECT_TRUE(instance->HasSite()); | 275 EXPECT_TRUE(instance->HasSite()); |
305 | 276 |
306 DrainMessageLoops(); | 277 DrainMessageLoops(); |
307 } | 278 } |
308 | 279 |
309 // Test to ensure GetSiteForURL properly returns sites for URLs. | 280 // Test to ensure GetSiteForURL properly returns sites for URLs. |
310 TEST_F(SiteInstanceTest, GetSiteForURL) { | 281 TEST_F(SiteInstanceTest, GetSiteForURL) { |
311 // Pages are irrelevant. | 282 // Pages are irrelevant. |
312 GURL test_url = GURL("http://www.google.com/index.html"); | 283 GURL test_url = GURL("http://www.google.com/index.html"); |
313 GURL site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url); | 284 GURL site_url = SiteInstanceImpl::GetSiteForURL(nullptr, test_url); |
314 EXPECT_EQ(GURL("http://google.com"), site_url); | 285 EXPECT_EQ(GURL("http://google.com"), site_url); |
315 EXPECT_EQ("http", site_url.scheme()); | 286 EXPECT_EQ("http", site_url.scheme()); |
316 EXPECT_EQ("google.com", site_url.host()); | 287 EXPECT_EQ("google.com", site_url.host()); |
317 | 288 |
318 // Ports are irrlevant. | 289 // Ports are irrlevant. |
319 test_url = GURL("https://www.google.com:8080"); | 290 test_url = GURL("https://www.google.com:8080"); |
320 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url); | 291 site_url = SiteInstanceImpl::GetSiteForURL(nullptr, test_url); |
321 EXPECT_EQ(GURL("https://google.com"), site_url); | 292 EXPECT_EQ(GURL("https://google.com"), site_url); |
322 | 293 |
323 // Hostnames without TLDs are ok. | 294 // Hostnames without TLDs are ok. |
324 test_url = GURL("http://foo/a.html"); | 295 test_url = GURL("http://foo/a.html"); |
325 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url); | 296 site_url = SiteInstanceImpl::GetSiteForURL(nullptr, test_url); |
326 EXPECT_EQ(GURL("http://foo"), site_url); | 297 EXPECT_EQ(GURL("http://foo"), site_url); |
327 EXPECT_EQ("foo", site_url.host()); | 298 EXPECT_EQ("foo", site_url.host()); |
328 | 299 |
329 // File URLs should include the scheme. | 300 // File URLs should include the scheme. |
330 test_url = GURL("file:///C:/Downloads/"); | 301 test_url = GURL("file:///C:/Downloads/"); |
331 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url); | 302 site_url = SiteInstanceImpl::GetSiteForURL(nullptr, test_url); |
332 EXPECT_EQ(GURL("file:"), site_url); | 303 EXPECT_EQ(GURL("file:"), site_url); |
333 EXPECT_EQ("file", site_url.scheme()); | 304 EXPECT_EQ("file", site_url.scheme()); |
334 EXPECT_FALSE(site_url.has_host()); | 305 EXPECT_FALSE(site_url.has_host()); |
335 | 306 |
336 // Some file URLs have hosts in the path. | 307 // Some file URLs have hosts in the path. |
337 test_url = GURL("file://server/path"); | 308 test_url = GURL("file://server/path"); |
338 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url); | 309 site_url = SiteInstanceImpl::GetSiteForURL(nullptr, test_url); |
339 EXPECT_EQ(GURL("file://server"), site_url); | 310 EXPECT_EQ(GURL("file://server"), site_url); |
340 EXPECT_EQ("server", site_url.host()); | 311 EXPECT_EQ("server", site_url.host()); |
341 | 312 |
342 // Data URLs should include the scheme. | 313 // Data URLs should include the scheme. |
343 test_url = GURL("data:text/html,foo"); | 314 test_url = GURL("data:text/html,foo"); |
344 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url); | 315 site_url = SiteInstanceImpl::GetSiteForURL(nullptr, test_url); |
345 EXPECT_EQ(GURL("data:"), site_url); | 316 EXPECT_EQ(GURL("data:"), site_url); |
346 EXPECT_EQ("data", site_url.scheme()); | 317 EXPECT_EQ("data", site_url.scheme()); |
347 EXPECT_FALSE(site_url.has_host()); | 318 EXPECT_FALSE(site_url.has_host()); |
348 | 319 |
349 // Javascript URLs should include the scheme. | 320 // Javascript URLs should include the scheme. |
350 test_url = GURL("javascript:foo();"); | 321 test_url = GURL("javascript:foo();"); |
351 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url); | 322 site_url = SiteInstanceImpl::GetSiteForURL(nullptr, test_url); |
352 EXPECT_EQ(GURL("javascript:"), site_url); | 323 EXPECT_EQ(GURL("javascript:"), site_url); |
353 EXPECT_EQ("javascript", site_url.scheme()); | 324 EXPECT_EQ("javascript", site_url.scheme()); |
354 EXPECT_FALSE(site_url.has_host()); | 325 EXPECT_FALSE(site_url.has_host()); |
355 | 326 |
356 // Guest URLs are special and need to have the path in the site as well, | 327 // Guest URLs are special and need to have the path in the site as well, |
357 // since it affects the StoragePartition configuration. | 328 // since it affects the StoragePartition configuration. |
358 std::string guest_url(kGuestScheme); | 329 std::string guest_url(kGuestScheme); |
359 guest_url.append("://abc123/path"); | 330 guest_url.append("://abc123/path"); |
360 test_url = GURL(guest_url); | 331 test_url = GURL(guest_url); |
361 site_url = SiteInstanceImpl::GetSiteForURL(NULL, test_url); | 332 site_url = SiteInstanceImpl::GetSiteForURL(nullptr, test_url); |
362 EXPECT_EQ(test_url, site_url); | 333 EXPECT_EQ(test_url, site_url); |
363 | 334 |
364 DrainMessageLoops(); | 335 DrainMessageLoops(); |
365 } | 336 } |
366 | 337 |
367 // Test of distinguishing URLs from different sites. Most of this logic is | 338 // Test of distinguishing URLs from different sites. Most of this logic is |
368 // tested in RegistryControlledDomainTest. This test focuses on URLs with | 339 // tested in RegistryControlledDomainTest. This test focuses on URLs with |
369 // different schemes or ports. | 340 // different schemes or ports. |
370 TEST_F(SiteInstanceTest, IsSameWebSite) { | 341 TEST_F(SiteInstanceTest, IsSameWebSite) { |
371 GURL url_foo = GURL("http://foo/a.html"); | 342 GURL url_foo = GURL("http://foo/a.html"); |
372 GURL url_foo2 = GURL("http://foo/b.html"); | 343 GURL url_foo2 = GURL("http://foo/b.html"); |
373 GURL url_foo_https = GURL("https://foo/a.html"); | 344 GURL url_foo_https = GURL("https://foo/a.html"); |
374 GURL url_foo_port = GURL("http://foo:8080/a.html"); | 345 GURL url_foo_port = GURL("http://foo:8080/a.html"); |
375 GURL url_javascript = GURL("javascript:alert(1);"); | 346 GURL url_javascript = GURL("javascript:alert(1);"); |
376 GURL url_blank = GURL(url::kAboutBlankURL); | 347 GURL url_blank = GURL(url::kAboutBlankURL); |
377 | 348 |
378 // Same scheme and port -> same site. | 349 // Same scheme and port -> same site. |
379 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo2)); | 350 EXPECT_TRUE(SiteInstance::IsSameWebSite(nullptr, url_foo, url_foo2)); |
380 | 351 |
381 // Different scheme -> different site. | 352 // Different scheme -> different site. |
382 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_https)); | 353 EXPECT_FALSE(SiteInstance::IsSameWebSite(nullptr, url_foo, url_foo_https)); |
383 | 354 |
384 // Different port -> same site. | 355 // Different port -> same site. |
385 // (Changes to document.domain make renderer ignore the port.) | 356 // (Changes to document.domain make renderer ignore the port.) |
386 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_foo_port)); | 357 EXPECT_TRUE(SiteInstance::IsSameWebSite(nullptr, url_foo, url_foo_port)); |
387 | 358 |
388 // JavaScript links should be considered same site for anything. | 359 // JavaScript links should be considered same site for anything. |
389 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo)); | 360 EXPECT_TRUE(SiteInstance::IsSameWebSite(nullptr, url_javascript, url_foo)); |
390 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_https)); | 361 EXPECT_TRUE( |
391 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_javascript, url_foo_port)); | 362 SiteInstance::IsSameWebSite(nullptr, url_javascript, url_foo_https)); |
| 363 EXPECT_TRUE( |
| 364 SiteInstance::IsSameWebSite(nullptr, url_javascript, url_foo_port)); |
392 | 365 |
393 // Navigating to a blank page is considered the same site. | 366 // Navigating to a blank page is considered the same site. |
394 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo, url_blank)); | 367 EXPECT_TRUE(SiteInstance::IsSameWebSite(nullptr, url_foo, url_blank)); |
395 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo_https, url_blank)); | 368 EXPECT_TRUE(SiteInstance::IsSameWebSite(nullptr, url_foo_https, url_blank)); |
396 EXPECT_TRUE(SiteInstance::IsSameWebSite(NULL, url_foo_port, url_blank)); | 369 EXPECT_TRUE(SiteInstance::IsSameWebSite(nullptr, url_foo_port, url_blank)); |
397 | 370 |
398 // Navigating from a blank site is not considered to be the same site. | 371 // Navigating from a blank site is not considered to be the same site. |
399 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_blank, url_foo)); | 372 EXPECT_FALSE(SiteInstance::IsSameWebSite(nullptr, url_blank, url_foo)); |
400 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_blank, url_foo_https)); | 373 EXPECT_FALSE(SiteInstance::IsSameWebSite(nullptr, url_blank, url_foo_https)); |
401 EXPECT_FALSE(SiteInstance::IsSameWebSite(NULL, url_blank, url_foo_port)); | 374 EXPECT_FALSE(SiteInstance::IsSameWebSite(nullptr, url_blank, url_foo_port)); |
402 | 375 |
403 DrainMessageLoops(); | 376 DrainMessageLoops(); |
404 } | 377 } |
405 | 378 |
406 // Test to ensure that there is only one SiteInstance per site in a given | 379 // Test to ensure that there is only one SiteInstance per site in a given |
407 // BrowsingInstance, when process-per-site is not in use. | 380 // BrowsingInstance, when process-per-site is not in use. |
408 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { | 381 TEST_F(SiteInstanceTest, OneSiteInstancePerSite) { |
409 ASSERT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch( | 382 ASSERT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch( |
410 switches::kProcessPerSite)); | 383 switches::kProcessPerSite)); |
411 int delete_counter = 0; | |
412 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 384 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
413 TestBrowsingInstance* browsing_instance = | 385 BrowsingInstance* browsing_instance = |
414 new TestBrowsingInstance(browser_context.get(), &delete_counter); | 386 new BrowsingInstance(browser_context.get()); |
415 | 387 |
416 const GURL url_a1("http://www.google.com/1.html"); | 388 const GURL url_a1("http://www.google.com/1.html"); |
417 scoped_refptr<SiteInstanceImpl> site_instance_a1( | 389 scoped_refptr<SiteInstanceImpl> site_instance_a1( |
418 browsing_instance->GetSiteInstanceForURL(url_a1)); | 390 browsing_instance->GetSiteInstanceForURL(url_a1)); |
419 EXPECT_TRUE(site_instance_a1.get() != NULL); | 391 EXPECT_TRUE(site_instance_a1.get() != nullptr); |
420 | 392 |
421 // A separate site should create a separate SiteInstance. | 393 // A separate site should create a separate SiteInstance. |
422 const GURL url_b1("http://www.yahoo.com/"); | 394 const GURL url_b1("http://www.yahoo.com/"); |
423 scoped_refptr<SiteInstanceImpl> site_instance_b1( | 395 scoped_refptr<SiteInstanceImpl> site_instance_b1( |
424 | 396 |
425 browsing_instance->GetSiteInstanceForURL(url_b1)); | 397 browsing_instance->GetSiteInstanceForURL(url_b1)); |
426 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); | 398 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); |
427 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get())); | 399 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get())); |
428 | 400 |
429 // Getting the new SiteInstance from the BrowsingInstance and from another | 401 // Getting the new SiteInstance from the BrowsingInstance and from another |
430 // SiteInstance in the BrowsingInstance should give the same result. | 402 // SiteInstance in the BrowsingInstance should give the same result. |
431 EXPECT_EQ(site_instance_b1.get(), | 403 EXPECT_EQ(site_instance_b1.get(), |
432 site_instance_a1->GetRelatedSiteInstance(url_b1)); | 404 site_instance_a1->GetRelatedSiteInstance(url_b1)); |
433 | 405 |
434 // A second visit to the original site should return the same SiteInstance. | 406 // A second visit to the original site should return the same SiteInstance. |
435 const GURL url_a2("http://www.google.com/2.html"); | 407 const GURL url_a2("http://www.google.com/2.html"); |
436 EXPECT_EQ(site_instance_a1.get(), | 408 EXPECT_EQ(site_instance_a1.get(), |
437 browsing_instance->GetSiteInstanceForURL(url_a2)); | 409 browsing_instance->GetSiteInstanceForURL(url_a2)); |
438 EXPECT_EQ(site_instance_a1.get(), | 410 EXPECT_EQ(site_instance_a1.get(), |
439 site_instance_a1->GetRelatedSiteInstance(url_a2)); | 411 site_instance_a1->GetRelatedSiteInstance(url_a2)); |
440 | 412 |
441 // A visit to the original site in a new BrowsingInstance (same or different | 413 // A visit to the original site in a new BrowsingInstance (same or different |
442 // browser context) should return a different SiteInstance. | 414 // browser context) should return a different SiteInstance. |
443 TestBrowsingInstance* browsing_instance2 = | 415 BrowsingInstance* browsing_instance2 = |
444 new TestBrowsingInstance(browser_context.get(), &delete_counter); | 416 new BrowsingInstance(browser_context.get()); |
445 // Ensure the new SiteInstance is ref counted so that it gets deleted. | 417 // Ensure the new SiteInstance is ref counted so that it gets deleted. |
446 scoped_refptr<SiteInstanceImpl> site_instance_a2_2( | 418 scoped_refptr<SiteInstanceImpl> site_instance_a2_2( |
447 browsing_instance2->GetSiteInstanceForURL(url_a2)); | 419 browsing_instance2->GetSiteInstanceForURL(url_a2)); |
448 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); | 420 EXPECT_NE(site_instance_a1.get(), site_instance_a2_2.get()); |
449 EXPECT_FALSE( | 421 EXPECT_FALSE( |
450 site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2.get())); | 422 site_instance_a1->IsRelatedSiteInstance(site_instance_a2_2.get())); |
451 | 423 |
452 // The two SiteInstances for http://google.com should not use the same process | 424 // The two SiteInstances for http://google.com should not use the same process |
453 // if process-per-site is not enabled. | 425 // if process-per-site is not enabled. |
454 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess()); | 426 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess()); |
(...skipping 18 matching lines...) Expand all Loading... |
473 // The processes will be unregistered when the RPH scoped_ptrs go away. | 445 // The processes will be unregistered when the RPH scoped_ptrs go away. |
474 | 446 |
475 DrainMessageLoops(); | 447 DrainMessageLoops(); |
476 } | 448 } |
477 | 449 |
478 // Test to ensure that there is only one RenderProcessHost per site for an | 450 // Test to ensure that there is only one RenderProcessHost per site for an |
479 // entire BrowserContext, if process-per-site is in use. | 451 // entire BrowserContext, if process-per-site is in use. |
480 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { | 452 TEST_F(SiteInstanceTest, OneSiteInstancePerSiteInBrowserContext) { |
481 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 453 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
482 switches::kProcessPerSite); | 454 switches::kProcessPerSite); |
483 int delete_counter = 0; | |
484 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 455 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
485 TestBrowsingInstance* browsing_instance = | 456 scoped_refptr<BrowsingInstance> browsing_instance = |
486 new TestBrowsingInstance(browser_context.get(), &delete_counter); | 457 new BrowsingInstance(browser_context.get()); |
487 | 458 |
488 const GURL url_a1("http://www.google.com/1.html"); | 459 const GURL url_a1("http://www.google.com/1.html"); |
489 scoped_refptr<SiteInstanceImpl> site_instance_a1( | 460 scoped_refptr<SiteInstanceImpl> site_instance_a1( |
490 browsing_instance->GetSiteInstanceForURL(url_a1)); | 461 browsing_instance->GetSiteInstanceForURL(url_a1)); |
491 EXPECT_TRUE(site_instance_a1.get() != NULL); | 462 EXPECT_TRUE(site_instance_a1.get() != nullptr); |
492 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess()); | 463 scoped_ptr<RenderProcessHost> process_a1(site_instance_a1->GetProcess()); |
493 | 464 |
494 // A separate site should create a separate SiteInstance. | 465 // A separate site should create a separate SiteInstance. |
495 const GURL url_b1("http://www.yahoo.com/"); | 466 const GURL url_b1("http://www.yahoo.com/"); |
496 scoped_refptr<SiteInstanceImpl> site_instance_b1( | 467 scoped_refptr<SiteInstanceImpl> site_instance_b1( |
497 browsing_instance->GetSiteInstanceForURL(url_b1)); | 468 browsing_instance->GetSiteInstanceForURL(url_b1)); |
498 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); | 469 EXPECT_NE(site_instance_a1.get(), site_instance_b1.get()); |
499 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get())); | 470 EXPECT_TRUE(site_instance_a1->IsRelatedSiteInstance(site_instance_b1.get())); |
500 | 471 |
501 // Getting the new SiteInstance from the BrowsingInstance and from another | 472 // Getting the new SiteInstance from the BrowsingInstance and from another |
502 // SiteInstance in the BrowsingInstance should give the same result. | 473 // SiteInstance in the BrowsingInstance should give the same result. |
503 EXPECT_EQ(site_instance_b1.get(), | 474 EXPECT_EQ(site_instance_b1.get(), |
504 site_instance_a1->GetRelatedSiteInstance(url_b1)); | 475 site_instance_a1->GetRelatedSiteInstance(url_b1)); |
505 | 476 |
506 // A second visit to the original site should return the same SiteInstance. | 477 // A second visit to the original site should return the same SiteInstance. |
507 const GURL url_a2("http://www.google.com/2.html"); | 478 const GURL url_a2("http://www.google.com/2.html"); |
508 EXPECT_EQ(site_instance_a1.get(), | 479 EXPECT_EQ(site_instance_a1.get(), |
509 browsing_instance->GetSiteInstanceForURL(url_a2)); | 480 browsing_instance->GetSiteInstanceForURL(url_a2)); |
510 EXPECT_EQ(site_instance_a1.get(), | 481 EXPECT_EQ(site_instance_a1.get(), |
511 site_instance_a1->GetRelatedSiteInstance(url_a2)); | 482 site_instance_a1->GetRelatedSiteInstance(url_a2)); |
512 | 483 |
513 // A visit to the original site in a new BrowsingInstance (same browser | 484 // A visit to the original site in a new BrowsingInstance (same browser |
514 // context) should return a different SiteInstance with the same process. | 485 // context) should return a different SiteInstance with the same process. |
515 TestBrowsingInstance* browsing_instance2 = | 486 BrowsingInstance* browsing_instance2 = |
516 new TestBrowsingInstance(browser_context.get(), &delete_counter); | 487 new BrowsingInstance(browser_context.get()); |
517 scoped_refptr<SiteInstanceImpl> site_instance_a1_2( | 488 scoped_refptr<SiteInstanceImpl> site_instance_a1_2( |
518 browsing_instance2->GetSiteInstanceForURL(url_a1)); | 489 browsing_instance2->GetSiteInstanceForURL(url_a1)); |
519 EXPECT_TRUE(site_instance_a1.get() != NULL); | 490 EXPECT_TRUE(site_instance_a1.get() != nullptr); |
520 EXPECT_NE(site_instance_a1.get(), site_instance_a1_2.get()); | 491 EXPECT_NE(site_instance_a1.get(), site_instance_a1_2.get()); |
521 EXPECT_EQ(process_a1.get(), site_instance_a1_2->GetProcess()); | 492 EXPECT_EQ(process_a1.get(), site_instance_a1_2->GetProcess()); |
522 | 493 |
523 // A visit to the original site in a new BrowsingInstance (different browser | 494 // A visit to the original site in a new BrowsingInstance (different browser |
524 // context) should return a different SiteInstance with a different process. | 495 // context) should return a different SiteInstance with a different process. |
525 scoped_ptr<TestBrowserContext> browser_context2(new TestBrowserContext()); | 496 scoped_ptr<TestBrowserContext> browser_context2(new TestBrowserContext()); |
526 TestBrowsingInstance* browsing_instance3 = | 497 BrowsingInstance* browsing_instance3 = |
527 new TestBrowsingInstance(browser_context2.get(), &delete_counter); | 498 new BrowsingInstance(browser_context2.get()); |
528 scoped_refptr<SiteInstanceImpl> site_instance_a2_3( | 499 scoped_refptr<SiteInstanceImpl> site_instance_a2_3( |
529 browsing_instance3->GetSiteInstanceForURL(url_a2)); | 500 browsing_instance3->GetSiteInstanceForURL(url_a2)); |
530 EXPECT_TRUE(site_instance_a2_3.get() != NULL); | 501 EXPECT_TRUE(site_instance_a2_3.get() != nullptr); |
531 scoped_ptr<RenderProcessHost> process_a2_3(site_instance_a2_3->GetProcess()); | 502 scoped_ptr<RenderProcessHost> process_a2_3(site_instance_a2_3->GetProcess()); |
532 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); | 503 EXPECT_NE(site_instance_a1.get(), site_instance_a2_3.get()); |
533 EXPECT_NE(process_a1.get(), process_a2_3.get()); | 504 EXPECT_NE(process_a1.get(), process_a2_3.get()); |
534 | 505 |
535 // Should be able to see that we do have SiteInstances. | 506 // Should be able to see that we do have SiteInstances. |
536 EXPECT_TRUE(browsing_instance->HasSiteInstance( | 507 EXPECT_TRUE(browsing_instance->HasSiteInstance( |
537 GURL("http://mail.google.com"))); // visited before | 508 GURL("http://mail.google.com"))); // visited before |
538 EXPECT_TRUE(browsing_instance2->HasSiteInstance( | 509 EXPECT_TRUE(browsing_instance2->HasSiteInstance( |
539 GURL("http://mail.google.com"))); // visited before | 510 GURL("http://mail.google.com"))); // visited before |
540 EXPECT_TRUE(browsing_instance->HasSiteInstance( | 511 EXPECT_TRUE(browsing_instance->HasSiteInstance( |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 instance->SetSite(GURL("http://evernote.com/")); | 608 instance->SetSite(GURL("http://evernote.com/")); |
638 EXPECT_TRUE(instance->HasSite()); | 609 EXPECT_TRUE(instance->HasSite()); |
639 | 610 |
640 // Check prior to "assigning" a process to the instance, which is expected | 611 // Check prior to "assigning" a process to the instance, which is expected |
641 // to return false due to not being attached to any process yet. | 612 // to return false due to not being attached to any process yet. |
642 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); | 613 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); |
643 | 614 |
644 // The call to GetProcess actually creates a new real process, which works | 615 // The call to GetProcess actually creates a new real process, which works |
645 // fine, but might be a cause for problems in different contexts. | 616 // fine, but might be a cause for problems in different contexts. |
646 host.reset(instance->GetProcess()); | 617 host.reset(instance->GetProcess()); |
647 EXPECT_TRUE(host.get() != NULL); | 618 EXPECT_TRUE(host.get() != nullptr); |
648 EXPECT_TRUE(instance->HasProcess()); | 619 EXPECT_TRUE(instance->HasProcess()); |
649 | 620 |
650 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); | 621 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); |
651 EXPECT_FALSE(instance->HasWrongProcessForURL( | 622 EXPECT_FALSE(instance->HasWrongProcessForURL( |
652 GURL("javascript:alert(document.location.href);"))); | 623 GURL("javascript:alert(document.location.href);"))); |
653 | 624 |
654 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://gpu"))); | 625 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://gpu"))); |
655 | 626 |
656 // Test that WebUI SiteInstances reject normal web URLs. | 627 // Test that WebUI SiteInstances reject normal web URLs. |
657 const GURL webui_url("chrome://gpu"); | 628 const GURL webui_url("chrome://gpu"); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 instance->SetSite(GURL("http://evernote.com/")); | 666 instance->SetSite(GURL("http://evernote.com/")); |
696 EXPECT_TRUE(instance->HasSite()); | 667 EXPECT_TRUE(instance->HasSite()); |
697 | 668 |
698 // Check prior to "assigning" a process to the instance, which is expected | 669 // Check prior to "assigning" a process to the instance, which is expected |
699 // to return false due to not being attached to any process yet. | 670 // to return false due to not being attached to any process yet. |
700 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); | 671 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://google.com"))); |
701 | 672 |
702 // The call to GetProcess actually creates a new real process, which works | 673 // The call to GetProcess actually creates a new real process, which works |
703 // fine, but might be a cause for problems in different contexts. | 674 // fine, but might be a cause for problems in different contexts. |
704 host.reset(instance->GetProcess()); | 675 host.reset(instance->GetProcess()); |
705 EXPECT_TRUE(host.get() != NULL); | 676 EXPECT_TRUE(host.get() != nullptr); |
706 EXPECT_TRUE(instance->HasProcess()); | 677 EXPECT_TRUE(instance->HasProcess()); |
707 | 678 |
708 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); | 679 EXPECT_FALSE(instance->HasWrongProcessForURL(GURL("http://evernote.com"))); |
709 EXPECT_FALSE(instance->HasWrongProcessForURL( | 680 EXPECT_FALSE(instance->HasWrongProcessForURL( |
710 GURL("javascript:alert(document.location.href);"))); | 681 GURL("javascript:alert(document.location.href);"))); |
711 | 682 |
712 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://gpu"))); | 683 EXPECT_TRUE(instance->HasWrongProcessForURL(GURL("chrome://gpu"))); |
713 | 684 |
714 DrainMessageLoops(); | 685 DrainMessageLoops(); |
715 } | 686 } |
(...skipping 11 matching lines...) Expand all Loading... |
727 EXPECT_TRUE(instance->GetSiteURL().is_empty()); | 698 EXPECT_TRUE(instance->GetSiteURL().is_empty()); |
728 | 699 |
729 // Simulate navigating to a WebUI URL in a process that does not have WebUI | 700 // Simulate navigating to a WebUI URL in a process that does not have WebUI |
730 // bindings. This already requires bypassing security checks. | 701 // bindings. This already requires bypassing security checks. |
731 const GURL webui_url("chrome://gpu"); | 702 const GURL webui_url("chrome://gpu"); |
732 instance->SetSite(webui_url); | 703 instance->SetSite(webui_url); |
733 EXPECT_TRUE(instance->HasSite()); | 704 EXPECT_TRUE(instance->HasSite()); |
734 | 705 |
735 // The call to GetProcess actually creates a new real process. | 706 // The call to GetProcess actually creates a new real process. |
736 host.reset(instance->GetProcess()); | 707 host.reset(instance->GetProcess()); |
737 EXPECT_TRUE(host.get() != NULL); | 708 EXPECT_TRUE(host.get() != nullptr); |
738 EXPECT_TRUE(instance->HasProcess()); | 709 EXPECT_TRUE(instance->HasProcess()); |
739 | 710 |
740 // Without bindings, this should look like the wrong process. | 711 // Without bindings, this should look like the wrong process. |
741 EXPECT_TRUE(instance->HasWrongProcessForURL(webui_url)); | 712 EXPECT_TRUE(instance->HasWrongProcessForURL(webui_url)); |
742 | 713 |
743 // WebUI uses process-per-site, so another instance would normally use the | 714 // WebUI uses process-per-site, so another instance would normally use the |
744 // same process. Make sure it doesn't use the same process if the bindings | 715 // same process. Make sure it doesn't use the same process if the bindings |
745 // are missing. | 716 // are missing. |
746 scoped_refptr<SiteInstanceImpl> instance2( | 717 scoped_refptr<SiteInstanceImpl> instance2( |
747 SiteInstanceImpl::Create(browser_context.get())); | 718 SiteInstanceImpl::Create(browser_context.get())); |
748 instance2->SetSite(webui_url); | 719 instance2->SetSite(webui_url); |
749 host2.reset(instance2->GetProcess()); | 720 host2.reset(instance2->GetProcess()); |
750 EXPECT_TRUE(host2.get() != NULL); | 721 EXPECT_TRUE(host2.get() != nullptr); |
751 EXPECT_TRUE(instance2->HasProcess()); | 722 EXPECT_TRUE(instance2->HasProcess()); |
752 EXPECT_NE(host.get(), host2.get()); | 723 EXPECT_NE(host.get(), host2.get()); |
753 | 724 |
754 DrainMessageLoops(); | 725 DrainMessageLoops(); |
755 } | 726 } |
756 | 727 |
757 // Test that we do not register processes with empty sites for process-per-site | 728 // Test that we do not register processes with empty sites for process-per-site |
758 // mode. | 729 // mode. |
759 TEST_F(SiteInstanceTest, NoProcessPerSiteForEmptySite) { | 730 TEST_F(SiteInstanceTest, NoProcessPerSiteForEmptySite) { |
760 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 731 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
761 switches::kProcessPerSite); | 732 switches::kProcessPerSite); |
762 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); | 733 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
763 scoped_ptr<RenderProcessHost> host; | 734 scoped_ptr<RenderProcessHost> host; |
764 scoped_refptr<SiteInstanceImpl> instance( | 735 scoped_refptr<SiteInstanceImpl> instance( |
765 SiteInstanceImpl::Create(browser_context.get())); | 736 SiteInstanceImpl::Create(browser_context.get())); |
766 | 737 |
767 instance->SetSite(GURL()); | 738 instance->SetSite(GURL()); |
768 EXPECT_TRUE(instance->HasSite()); | 739 EXPECT_TRUE(instance->HasSite()); |
769 EXPECT_TRUE(instance->GetSiteURL().is_empty()); | 740 EXPECT_TRUE(instance->GetSiteURL().is_empty()); |
770 host.reset(instance->GetProcess()); | 741 host.reset(instance->GetProcess()); |
771 | 742 |
772 EXPECT_FALSE(RenderProcessHostImpl::GetProcessHostForSite( | 743 EXPECT_FALSE(RenderProcessHostImpl::GetProcessHostForSite( |
773 browser_context.get(), GURL())); | 744 browser_context.get(), GURL())); |
774 | 745 |
775 DrainMessageLoops(); | 746 DrainMessageLoops(); |
776 } | 747 } |
777 | 748 |
| 749 TEST_F(SiteInstanceTest, DefaultSubframeSiteInstance) { |
| 750 if (AreAllSitesIsolatedForTesting()) |
| 751 return; // --top-document-isolation is not possible. |
| 752 |
| 753 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 754 switches::kTopDocumentIsolation); |
| 755 |
| 756 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext()); |
| 757 scoped_refptr<SiteInstanceImpl> main_instance = |
| 758 SiteInstanceImpl::Create(browser_context.get()); |
| 759 scoped_refptr<SiteInstanceImpl> subframe_instance = |
| 760 main_instance->GetDefaultSubframeSiteInstance(); |
| 761 int subframe_instance_id = subframe_instance->GetId(); |
| 762 |
| 763 EXPECT_NE(main_instance, subframe_instance); |
| 764 EXPECT_EQ(subframe_instance, main_instance->GetDefaultSubframeSiteInstance()); |
| 765 EXPECT_FALSE(main_instance->is_default_subframe_site_instance()); |
| 766 EXPECT_TRUE(subframe_instance->is_default_subframe_site_instance()); |
| 767 |
| 768 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 769 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| 770 |
| 771 // Free the subframe instance. |
| 772 subframe_instance = nullptr; |
| 773 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 774 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| 775 |
| 776 // Calling GetDefaultSubframeSiteInstance again should return a new |
| 777 // SiteInstance with a different ID from the original. |
| 778 subframe_instance = main_instance->GetDefaultSubframeSiteInstance(); |
| 779 EXPECT_NE(subframe_instance->GetId(), subframe_instance_id); |
| 780 EXPECT_FALSE(main_instance->is_default_subframe_site_instance()); |
| 781 EXPECT_TRUE(subframe_instance->is_default_subframe_site_instance()); |
| 782 EXPECT_EQ(subframe_instance->GetDefaultSubframeSiteInstance(), |
| 783 subframe_instance); |
| 784 EXPECT_EQ(0, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 785 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| 786 |
| 787 // Free the main instance. |
| 788 main_instance = nullptr; |
| 789 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 790 EXPECT_EQ(0, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| 791 |
| 792 // Free the subframe instance, which should free the browsing instance. |
| 793 subframe_instance = nullptr; |
| 794 EXPECT_EQ(1, browser_client()->GetAndClearSiteInstanceDeleteCount()); |
| 795 EXPECT_EQ(1, browser_client()->GetAndClearBrowsingInstanceDeleteCount()); |
| 796 } |
| 797 |
778 } // namespace content | 798 } // namespace content |
OLD | NEW |