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

Side by Side Diff: content/browser/renderer_host/render_view_host_manager_browsertest.cc

Issue 9108001: Adds support for calling postMessage on a frame living in a different renderer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Invert check. Created 8 years, 7 months 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
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/file_util.h" 5 #include "base/file_util.h"
6 #include "base/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
7 #include "base/path_service.h" 7 #include "base/path_service.h"
8 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/ui/browser.h" 9 #include "chrome/browser/ui/browser.h"
9 #include "chrome/test/base/in_process_browser_test.h" 10 #include "chrome/test/base/in_process_browser_test.h"
10 #include "chrome/test/base/ui_test_utils.h" 11 #include "chrome/test/base/ui_test_utils.h"
11 #include "content/browser/renderer_host/render_view_host_impl.h" 12 #include "content/browser/renderer_host/render_view_host_impl.h"
12 #include "content/browser/site_instance_impl.h" 13 #include "content/browser/site_instance_impl.h"
13 #include "content/browser/web_contents/web_contents_impl.h" 14 #include "content/browser/web_contents/web_contents_impl.h"
14 #include "content/public/browser/navigation_controller.h" 15 #include "content/public/browser/navigation_controller.h"
15 #include "content/public/browser/notification_details.h" 16 #include "content/public/browser/notification_details.h"
16 #include "content/public/browser/notification_observer.h" 17 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h" 18 #include "content/public/browser/notification_registrar.h"
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 // Should have the same SiteInstance. 388 // Should have the same SiteInstance.
388 scoped_refptr<SiteInstance> blank_site_instance( 389 scoped_refptr<SiteInstance> blank_site_instance(
389 browser()->GetSelectedWebContents()->GetSiteInstance()); 390 browser()->GetSelectedWebContents()->GetSiteInstance());
390 EXPECT_EQ(orig_site_instance, blank_site_instance); 391 EXPECT_EQ(orig_site_instance, blank_site_instance);
391 392
392 // Now navigate the new tab to a different site. 393 // Now navigate the new tab to a different site.
393 content::WebContents* new_contents = browser()->GetSelectedWebContents(); 394 content::WebContents* new_contents = browser()->GetSelectedWebContents();
394 ui_test_utils::NavigateToURL(browser(), 395 ui_test_utils::NavigateToURL(browser(),
395 https_server.GetURL("files/title1.html")); 396 https_server.GetURL("files/title1.html"));
396 scoped_refptr<SiteInstance> new_site_instance( 397 scoped_refptr<SiteInstance> new_site_instance(
397 browser()->GetSelectedWebContents()->GetSiteInstance()); 398 new_contents->GetSiteInstance());
398 EXPECT_NE(orig_site_instance, new_site_instance); 399 EXPECT_NE(orig_site_instance, new_site_instance);
399 400
400 // Clicking the original link in the first tab should cause us to swap back. 401 // Clicking the original link in the first tab should cause us to swap back.
401 browser()->ActivateTabAt(0, true); 402 browser()->ActivateTabAt(0, true);
402 ui_test_utils::WindowedNotificationObserver navigation_observer( 403 ui_test_utils::WindowedNotificationObserver navigation_observer(
403 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 404 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
404 content::Source<content::NavigationController>( 405 content::Source<content::NavigationController>(
405 &new_contents->GetController())); 406 &new_contents->GetController()));
406 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( 407 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
407 browser()->GetSelectedWebContents()->GetRenderViewHost(), L"", 408 browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
(...skipping 19 matching lines...) Expand all
427 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 428 content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
428 content::Source<content::WebContents>(new_contents)); 429 content::Source<content::WebContents>(new_contents));
429 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( 430 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
430 browser()->GetSelectedWebContents()->GetRenderViewHost(), L"", 431 browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
431 L"window.domAutomationController.send(testCloseWindow());", 432 L"window.domAutomationController.send(testCloseWindow());",
432 &success)); 433 &success));
433 EXPECT_TRUE(success); 434 EXPECT_TRUE(success);
434 close_observer.Wait(); 435 close_observer.Wait();
435 } 436 }
436 437
438 // Test for crbug.com/99202. PostMessage calls should still work after
439 // navigating the source and target windows to different sites.
440 // Specifically:
441 // 1) Create 3 windows (opener, "foo", and _blank) and send "foo" cross-process.
442 // 2) Fail to post a message from "foo" to opener with the wrong target origin.
443 // 3) Post a message from "foo" to opener, which replies back to "foo".
444 // 4) Post a message from _blank to "foo".
445 IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest,
446 SupportCrossProcessPostMessage) {
447 // Start two servers with different sites.
448 ASSERT_TRUE(test_server()->Start());
449 net::TestServer https_server(
450 net::TestServer::TYPE_HTTPS,
451 net::TestServer::kLocalhost,
452 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
453 ASSERT_TRUE(https_server.Start());
454
455 // Load a page with links that open in a new window.
456 std::string replacement_path;
457 ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
458 "files/click-noreferrer-links.html",
459 https_server.host_port_pair(),
460 &replacement_path));
461 ui_test_utils::NavigateToURL(browser(),
462 test_server()->GetURL(replacement_path));
463
464 // Get the original SiteInstance and RVHM for later comparison.
465 content::WebContents* opener_contents = browser()->GetSelectedWebContents();
466 scoped_refptr<SiteInstance> orig_site_instance(
467 opener_contents->GetSiteInstance());
468 EXPECT_TRUE(orig_site_instance != NULL);
469 RenderViewHostManager* opener_manager =
470 static_cast<WebContentsImpl*>(opener_contents)->
471 GetRenderManagerForTesting();
472
473 // 1) Open two more windows, one named. These initially have openers but no
474 // reference to each other. We will later post a message between them.
475
476 // First, a named target=foo window.
477 ui_test_utils::WindowedTabAddedNotificationObserver new_tab_observer((
478 content::Source<content::WebContentsDelegate>(browser())));
479 bool success = false;
480 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
481 opener_contents->GetRenderViewHost(), L"",
482 L"window.domAutomationController.send(clickSameSiteTargetedLink());",
483 &success));
484 EXPECT_TRUE(success);
485 new_tab_observer.Wait();
486
487 // Wait for the navigation in the new tab to finish, if it hasn't, then
488 // send it to post_message.html on a different site.
489 content::WebContents* foo_contents = browser()->GetSelectedWebContents();
490 ui_test_utils::WaitForLoadStop(foo_contents);
491 EXPECT_EQ("/files/navigate_opener.html", foo_contents->GetURL().path());
492 EXPECT_EQ(1, browser()->active_index());
493 ui_test_utils::NavigateToURL(browser(),
494 https_server.GetURL("files/post_message.html"));
495 scoped_refptr<SiteInstance> foo_site_instance(
496 foo_contents->GetSiteInstance());
497 EXPECT_NE(orig_site_instance, foo_site_instance);
498
499 // Second, a target=_blank window.
500 browser()->ActivateTabAt(0, true);
501 ui_test_utils::WindowedTabAddedNotificationObserver new_tab_observer2((
502 content::Source<content::WebContentsDelegate>(browser())));
503 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
504 browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
505 L"window.domAutomationController.send(clickSameSiteTargetBlankLink());",
506 &success));
507 EXPECT_TRUE(success);
508 new_tab_observer2.Wait();
509
510 // Wait for the navigation in the new tab to finish, if it hasn't, then
511 // send it to post_message.html on the original site.
512 content::WebContents* new_contents = browser()->GetSelectedWebContents();
513 ui_test_utils::WaitForLoadStop(new_contents);
514 EXPECT_EQ("/files/title2.html", new_contents->GetURL().path());
515 EXPECT_EQ(1, browser()->active_index());
516 ui_test_utils::NavigateToURL(
517 browser(), test_server()->GetURL("files/post_message.html"));
518 EXPECT_EQ(orig_site_instance, new_contents->GetSiteInstance());
519 RenderViewHostManager* new_manager =
520 static_cast<WebContentsImpl*>(new_contents)->GetRenderManagerForTesting();
521
522 // We now have three windows. The opener should have a swapped out RVH
523 // for the new SiteInstance, but the _blank window should not.
524 EXPECT_EQ(3, browser()->tab_count());
525 EXPECT_TRUE(opener_manager->GetSwappedOutRenderViewHost(foo_site_instance));
526 EXPECT_FALSE(new_manager->GetSwappedOutRenderViewHost(foo_site_instance));
527
528 // 2) Fail to post a message from the foo window to the opener if the target
529 // origin is wrong. We won't see an error, but we can check for the right
530 // number of received messages below.
531 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
532 foo_contents->GetRenderViewHost(), L"",
533 L"window.domAutomationController.send(postToOpener('msg',"
534 L"'http://google.com'));",
535 &success));
536 EXPECT_TRUE(success);
537
538 // 3) Post a message from the foo window to the opener. The opener will
539 // reply, causing the foo window to update its own title.
540 ui_test_utils::WindowedNotificationObserver title_observer(
541 content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED,
542 content::Source<content::WebContents>(foo_contents));
543 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
544 foo_contents->GetRenderViewHost(), L"",
545 L"window.domAutomationController.send(postToOpener('msg','*'));",
546 &success));
547 EXPECT_TRUE(success);
548 title_observer.Wait();
549
550 // We should have received only 1 message in the opener and "foo" tabs,
551 // and updated the title.
552 int opener_received_messages = 0;
553 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractInt(
554 opener_contents->GetRenderViewHost(), L"",
555 L"window.domAutomationController.send(window.receivedMessages);",
556 &opener_received_messages));
557 int foo_received_messages = 0;
558 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractInt(
559 foo_contents->GetRenderViewHost(), L"",
560 L"window.domAutomationController.send(window.receivedMessages);",
561 &foo_received_messages));
562 EXPECT_EQ(1, foo_received_messages);
563 EXPECT_EQ(1, opener_received_messages);
564 EXPECT_EQ(ASCIIToUTF16("msg"), foo_contents->GetTitle());
565
566 // 4) Now post a message from the _blank window to the foo window. The
567 // foo window will update its title and will not reply.
568 ui_test_utils::WindowedNotificationObserver title_observer2(
569 content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED,
570 content::Source<content::WebContents>(foo_contents));
571 EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
572 new_contents->GetRenderViewHost(), L"",
573 L"window.domAutomationController.send(postToFoo('msg2'));",
574 &success));
575 EXPECT_TRUE(success);
576 title_observer2.Wait();
577 EXPECT_EQ(ASCIIToUTF16("msg2"), foo_contents->GetTitle());
578
579 // This postMessage should have created a swapped out RVH for the new
580 // SiteInstance in the target=_blank window.
581 EXPECT_TRUE(new_manager->GetSwappedOutRenderViewHost(foo_site_instance));
582 }
583
437 // Test for crbug.com/116192. Navigations to a window's opener should 584 // Test for crbug.com/116192. Navigations to a window's opener should
438 // still work after a process swap. 585 // still work after a process swap.
439 IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, 586 IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest,
440 AllowTargetedNavigationsInOpenerAfterSwap) { 587 AllowTargetedNavigationsInOpenerAfterSwap) {
441 // Start two servers with different sites. 588 // Start two servers with different sites.
442 ASSERT_TRUE(test_server()->Start()); 589 ASSERT_TRUE(test_server()->Start());
443 net::TestServer https_server( 590 net::TestServer https_server(
444 net::TestServer::TYPE_HTTPS, 591 net::TestServer::TYPE_HTTPS,
445 net::TestServer::kLocalhost, 592 net::TestServer::kLocalhost,
446 FilePath(FILE_PATH_LITERAL("chrome/test/data"))); 593 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 // Now navigate to a different instance so that we swap out again. 1016 // Now navigate to a different instance so that we swap out again.
870 ui_test_utils::NavigateToURL(browser(), 1017 ui_test_utils::NavigateToURL(browser(),
871 https_server.GetURL("files/title2.html")); 1018 https_server.GetURL("files/title2.html"));
872 rvh_observers.AddObserverToRVH(browser()->GetSelectedWebContents()-> 1019 rvh_observers.AddObserverToRVH(browser()->GetSelectedWebContents()->
873 GetRenderViewHost()); 1020 GetRenderViewHost());
874 1021
875 // This used to leak a render view host. 1022 // This used to leak a render view host.
876 browser()->CloseTabContents(browser()->GetSelectedWebContents()); 1023 browser()->CloseTabContents(browser()->GetSelectedWebContents());
877 EXPECT_EQ(0U, rvh_observers.GetNumObservers()); 1024 EXPECT_EQ(0U, rvh_observers.GetNumObservers());
878 } 1025 }
OLDNEW
« no previous file with comments | « content/browser/renderer_host/render_view_host_impl.cc ('k') | content/browser/web_contents/web_contents_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698