OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "content/browser/child_process_security_policy_impl.h" | 6 #include "content/browser/child_process_security_policy_impl.h" |
7 #include "content/browser/web_contents/web_contents_impl.h" | 7 #include "content/browser/web_contents/web_contents_impl.h" |
8 #include "content/public/browser/render_process_host.h" | 8 #include "content/public/browser/render_process_host.h" |
9 #include "content/public/common/content_switches.h" | 9 #include "content/public/common/content_switches.h" |
10 #include "content/public/test/browser_test_utils.h" | 10 #include "content/public/test/browser_test_utils.h" |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 // main frame's SiteInstance. | 248 // main frame's SiteInstance. |
249 GURL bar_url(embedded_test_server()->GetURL("bar.com", "/title1.html")); | 249 GURL bar_url(embedded_test_server()->GetURL("bar.com", "/title1.html")); |
250 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance(); | 250 auto* policy = ChildProcessSecurityPolicyImpl::GetInstance(); |
251 EXPECT_FALSE(policy->IsIsolatedOrigin(url::Origin(bar_url))); | 251 EXPECT_FALSE(policy->IsIsolatedOrigin(url::Origin(bar_url))); |
252 NavigateIframeToURL(web_contents(), "test_iframe", bar_url); | 252 NavigateIframeToURL(web_contents(), "test_iframe", bar_url); |
253 EXPECT_EQ(web_contents()->GetSiteInstance(), | 253 EXPECT_EQ(web_contents()->GetSiteInstance(), |
254 child->current_frame_host()->GetSiteInstance()); | 254 child->current_frame_host()->GetSiteInstance()); |
255 EXPECT_FALSE(child->current_frame_host()->IsCrossProcessSubframe()); | 255 EXPECT_FALSE(child->current_frame_host()->IsCrossProcessSubframe()); |
256 } | 256 } |
257 | 257 |
| 258 // Check that a new isolated origin subframe will attempt to reuse an existing |
| 259 // process for that isolated origin, even across BrowsingInstances. Also check |
| 260 // that main frame navigations to an isolated origin keep using the default |
| 261 // process model and do not reuse existing processes. |
| 262 IN_PROC_BROWSER_TEST_F(IsolatedOriginTest, SubframeReusesExistingProcess) { |
| 263 GURL top_url( |
| 264 embedded_test_server()->GetURL("www.foo.com", "/page_with_iframe.html")); |
| 265 EXPECT_TRUE(NavigateToURL(shell(), top_url)); |
| 266 FrameTreeNode* root = web_contents()->GetFrameTree()->root(); |
| 267 FrameTreeNode* child = root->child_at(0); |
| 268 |
| 269 // Open an unrelated tab in a separate BrowsingInstance, and navigate it to |
| 270 // to an isolated origin. This SiteInstance should have a default process |
| 271 // reuse policy - only subframes attempt process reuse. |
| 272 GURL isolated_url(embedded_test_server()->GetURL("isolated.foo.com", |
| 273 "/page_with_iframe.html")); |
| 274 Shell* second_shell = CreateBrowser(); |
| 275 EXPECT_TRUE(NavigateToURL(second_shell, isolated_url)); |
| 276 scoped_refptr<SiteInstanceImpl> second_shell_instance = |
| 277 static_cast<SiteInstanceImpl*>( |
| 278 second_shell->web_contents()->GetMainFrame()->GetSiteInstance()); |
| 279 EXPECT_FALSE(second_shell_instance->IsRelatedSiteInstance( |
| 280 root->current_frame_host()->GetSiteInstance())); |
| 281 RenderProcessHost* isolated_process = second_shell_instance->GetProcess(); |
| 282 EXPECT_EQ(SiteInstanceImpl::ProcessReusePolicy::DEFAULT, |
| 283 second_shell_instance->process_reuse_policy()); |
| 284 |
| 285 // Now navigate the first tab's subframe to an isolated origin. See that it |
| 286 // reuses the existing |isolated_process|. |
| 287 NavigateIframeToURL(web_contents(), "test_iframe", isolated_url); |
| 288 EXPECT_EQ(isolated_url, child->current_url()); |
| 289 EXPECT_EQ(isolated_process, child->current_frame_host()->GetProcess()); |
| 290 EXPECT_EQ( |
| 291 SiteInstanceImpl::ProcessReusePolicy::REUSE_PENDING_OR_COMMITTED_SITE, |
| 292 child->current_frame_host()->GetSiteInstance()->process_reuse_policy()); |
| 293 |
| 294 EXPECT_TRUE(child->current_frame_host()->IsCrossProcessSubframe()); |
| 295 EXPECT_EQ(isolated_url.GetOrigin(), |
| 296 child->current_frame_host()->GetSiteInstance()->GetSiteURL()); |
| 297 |
| 298 // The subframe's SiteInstance should still be different from second_shell's |
| 299 // SiteInstance, and they should be in separate BrowsingInstances. |
| 300 EXPECT_NE(second_shell_instance, |
| 301 child->current_frame_host()->GetSiteInstance()); |
| 302 EXPECT_FALSE(second_shell_instance->IsRelatedSiteInstance( |
| 303 child->current_frame_host()->GetSiteInstance())); |
| 304 |
| 305 // Navigate the second tab to a normal URL with a same-site subframe. This |
| 306 // leaves only the first tab's subframe in the isolated origin process. |
| 307 EXPECT_TRUE(NavigateToURL(second_shell, top_url)); |
| 308 EXPECT_NE(isolated_process, |
| 309 second_shell->web_contents()->GetMainFrame()->GetProcess()); |
| 310 |
| 311 // Navigate the second tab's subframe to an isolated origin, and check that |
| 312 // this new subframe reuses the isolated process of the subframe in the first |
| 313 // tab, even though the two are in separate BrowsingInstances. |
| 314 NavigateIframeToURL(second_shell->web_contents(), "test_iframe", |
| 315 isolated_url); |
| 316 FrameTreeNode* second_subframe = |
| 317 static_cast<WebContentsImpl*>(second_shell->web_contents()) |
| 318 ->GetFrameTree() |
| 319 ->root() |
| 320 ->child_at(0); |
| 321 EXPECT_EQ(isolated_process, |
| 322 second_subframe->current_frame_host()->GetProcess()); |
| 323 EXPECT_NE(child->current_frame_host()->GetSiteInstance(), |
| 324 second_subframe->current_frame_host()->GetSiteInstance()); |
| 325 |
| 326 // Open a third, unrelated tab, navigate it to an isolated origin, and check |
| 327 // that its main frame doesn't share a process with the existing isolated |
| 328 // subframes. |
| 329 Shell* third_shell = CreateBrowser(); |
| 330 EXPECT_TRUE(NavigateToURL(third_shell, isolated_url)); |
| 331 SiteInstanceImpl* third_shell_instance = static_cast<SiteInstanceImpl*>( |
| 332 third_shell->web_contents()->GetMainFrame()->GetSiteInstance()); |
| 333 EXPECT_NE(third_shell_instance, |
| 334 second_subframe->current_frame_host()->GetSiteInstance()); |
| 335 EXPECT_NE(third_shell_instance, |
| 336 child->current_frame_host()->GetSiteInstance()); |
| 337 EXPECT_NE(third_shell_instance->GetProcess(), isolated_process); |
| 338 } |
| 339 |
258 // Check that isolated origins can access cookies. This requires cookie checks | 340 // Check that isolated origins can access cookies. This requires cookie checks |
259 // on the IO thread to be aware of isolated origins. | 341 // on the IO thread to be aware of isolated origins. |
260 IN_PROC_BROWSER_TEST_F(IsolatedOriginTest, Cookies) { | 342 IN_PROC_BROWSER_TEST_F(IsolatedOriginTest, Cookies) { |
261 GURL isolated_url( | 343 GURL isolated_url( |
262 embedded_test_server()->GetURL("isolated.foo.com", "/title2.html")); | 344 embedded_test_server()->GetURL("isolated.foo.com", "/title2.html")); |
263 EXPECT_TRUE(NavigateToURL(shell(), isolated_url)); | 345 EXPECT_TRUE(NavigateToURL(shell(), isolated_url)); |
264 | 346 |
265 EXPECT_TRUE(ExecuteScript(web_contents(), "document.cookie = 'foo=bar';")); | 347 EXPECT_TRUE(ExecuteScript(web_contents(), "document.cookie = 'foo=bar';")); |
266 | 348 |
267 std::string cookie; | 349 std::string cookie; |
268 EXPECT_TRUE(ExecuteScriptAndExtractString( | 350 EXPECT_TRUE(ExecuteScriptAndExtractString( |
269 web_contents(), "window.domAutomationController.send(document.cookie);", | 351 web_contents(), "window.domAutomationController.send(document.cookie);", |
270 &cookie)); | 352 &cookie)); |
271 EXPECT_EQ("foo=bar", cookie); | 353 EXPECT_EQ("foo=bar", cookie); |
272 } | 354 } |
273 | 355 |
274 } // namespace content | 356 } // namespace content |
OLD | NEW |