render_frame_host_manager_browsertest.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include <set>
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/command_line.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/json/json_reader.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/values.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/browser/child_process_security_policy_impl.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/renderer_host/render_view_host_impl.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/site_instance_impl.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/web_contents/web_contents_impl.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/browser/webui/web_ui_impl.h"
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/common/content_constants_internal.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/navigation_controller.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/navigation_entry.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/render_process_host.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/web_contents_observer.h"
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/common/bindings_policy.h"
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/common/content_switches.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/common/url_constants.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/browser_test_utils.h"
28effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "content/public/test/content_browser_test.h"
29effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "content/public/test/content_browser_test_utils.h"
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/public/test/test_navigation_observer.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/test_utils.h"
323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/shell.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_util.h"
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/dns/mock_host_resolver.h"
35b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "net/test/spawned_test_server/spawned_test_server.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using base::ASCIIToUTF16;
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace {
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)const char kOpenUrlViaClickTargetFunc[] =
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    "(function(url) {\n"
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    "  var lnk = document.createElement(\"a\");\n"
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    "  lnk.href = url;\n"
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    "  lnk.target = \"_blank\";\n"
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    "  document.body.appendChild(lnk);\n"
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    "  lnk.click();\n"
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    "})";
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Adds a link with given url and target=_blank, and clicks on it.
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void OpenUrlViaClickTarget(const internal::ToRenderFrameHost& adapter,
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                           const GURL& url) {
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(ExecuteScript(adapter,
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      std::string(kOpenUrlViaClickTargetFunc) + "(\"" + url.spec() + "\");"));
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // anonymous namespace
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class RenderFrameHostManagerTest : public ContentBrowserTest {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderFrameHostManagerTest() : foo_com_("foo.com") {
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    replace_host_.SetHostStr(foo_com_);
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool GetFilePathWithHostAndPortReplacement(
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& original_file_path,
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const net::HostPortPair& host_port_pair,
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string* replacement_path) {
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    std::vector<net::SpawnedTestServer::StringPair> replacement_text;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    replacement_text.push_back(
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        make_pair("REPLACE_WITH_HOST_AND_PORT", host_port_pair.ToString()));
74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return net::SpawnedTestServer::GetFilePathWithReplacements(
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        original_file_path, replacement_text, replacement_path);
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void StartServer() {
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Support multiple sites on the test server.
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    host_resolver()->AddRule("*", "127.0.0.1");
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_TRUE(test_server()->Start());
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    foo_host_port_ = test_server()->host_port_pair();
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    foo_host_port_.set_host(foo_com_);
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns a URL on foo.com with the given path.
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL GetCrossSiteURL(const std::string& path) {
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GURL cross_site_url(test_server()->GetURL(path));
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return cross_site_url.ReplaceComponents(replace_host_);
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) protected:
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string foo_com_;
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL::Replacements replace_host_;
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  net::HostPortPair foo_host_port_;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Web pages should not have script access to the swapped out page.
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, NoScriptAccessAfterSwapOut) {
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance for later comparison.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Open a same-site link in a new window.
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetedLink());",
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new window to finish, if it hasn't.
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
1293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have the same SiteInstance.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> blank_site_instance(
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, blank_site_instance);
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We should have access to the opened window's location.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  success = false;
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(testScriptAccessToWindow());",
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now navigate the new window to a different site.
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/title1.html"));
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> new_site_instance(
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(orig_site_instance, new_site_instance);
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We should no longer have script access to the opened window's location.
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  success = false;
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(testScriptAccessToWindow());",
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(success);
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/24447.  Following a cross-site link with rel=noreferrer
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and target=_blank should create a new SiteInstance.
161f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       SwapProcessWithRelNoreferrerAndTargetBlank) {
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance for later comparison.
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test clicking a rel=noreferrer + target=blank link.
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickNoRefTargetBlankLink());",
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the window to open.
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  EXPECT_EQ("/files/title2.html",
1913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetVisibleURL().path());
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the cross-site transition in the new tab to finish.
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents());
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(web_contents->GetRenderManagerForTesting()->
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pending_render_view_host());
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have a new SiteInstance.
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> noref_blank_site_instance(
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(orig_site_instance, noref_blank_site_instance);
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// As of crbug.com/69267, we create a new BrowsingInstance (and SiteInstance)
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// for rel=noreferrer links in new windows, even to same site pages and named
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// targets.
209f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       SwapProcessWithSameSiteRelNoreferrer) {
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance for later comparison.
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
224868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test clicking a same-site rel=noreferrer + target=foo link.
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteNoRefTargetedLink());",
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the window to open.
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Opens in new window.
2393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  EXPECT_EQ("/files/title2.html",
2403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetVisibleURL().path());
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the cross-site transition in the new tab to finish.
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents());
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(web_contents->GetRenderManagerForTesting()->
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pending_render_view_host());
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have a new SiteInstance (in a new BrowsingInstance).
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> noref_blank_site_instance(
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(orig_site_instance, noref_blank_site_instance);
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/24447.  Following a cross-site link with just
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// target=_blank should not create a new SiteInstance.
257f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       DontSwapProcessWithOnlyTargetBlank) {
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance for later comparison.
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
272868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test clicking a target=blank link.
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickTargetBlankLink());",
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the window to open.
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the cross-site transition in the new tab to finish.
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("/files/title2.html",
2893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have the same SiteInstance.
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> blank_site_instance(
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, blank_site_instance);
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/24447.  Following a cross-site link with rel=noreferrer
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and no target=_blank should not create a new SiteInstance.
299f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       DontSwapProcessWithOnlyRelNoreferrer) {
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance for later comparison.
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
314868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test clicking a rel=noreferrer link.
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
3182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickNoRefLink());",
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the cross-site transition in the current tab to finish.
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(shell()->web_contents());
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Opens in same window.
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1u, Shell::windows().size());
3293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  EXPECT_EQ("/files/title2.html",
3303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            shell()->web_contents()->GetLastCommittedURL().path());
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have the same SiteInstance.
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> noref_site_instance(
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, noref_site_instance);
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/116192.  Targeted links should still work after the
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// named target window has swapped processes.
340f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       AllowTargetedNavigationsAfterSwap) {
3425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
3485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance for later comparison.
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
355868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test clicking a target=foo link.
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetedLink());",
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new tab to finish, if it hasn't.
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
3703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have the same SiteInstance.
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> blank_site_instance(
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, blank_site_instance);
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now navigate the new tab to a different site.
3785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL cross_site_url(GetCrossSiteURL("files/title1.html"));
3795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, cross_site_url);
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> new_site_instance(
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(orig_site_instance, new_site_instance);
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Clicking the original link in the first tab should cause us to swap back.
385868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TestNavigationObserver navigation_observer(new_shell->web_contents());
3862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
3872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
3882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetedLink());",
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  navigation_observer.Wait();
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have swapped back and shown the new window again.
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> revisit_site_instance(
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, revisit_site_instance);
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If it navigates away to another process, the original window should
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // still be able to close it (using a cross-process close message).
4005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, cross_site_url);
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(new_site_instance,
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            new_shell->web_contents()->GetSiteInstance());
4038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  WebContentsDestroyedWatcher close_watcher(new_shell->web_contents());
4042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
4052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
4062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(testCloseWindow());",
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
4098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  close_watcher.Wait();
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Test that setting the opener to null in a window affects cross-process
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// navigations, including those to existing entries.  http://crbug.com/156669.
414d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Flaky on windows: http://crbug.com/291249
415effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// This test also crashes under ThreadSanitizer, http://crbug.com/356758.
416effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#if defined(OS_WIN) || defined(THREAD_SANITIZER)
417d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#define MAYBE_DisownOpener DISABLED_DisownOpener
418d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#else
419d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#define MAYBE_DisownOpener DisownOpener
420d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif
421f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, MAYBE_DisownOpener) {
4225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
4232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Load a page with links that open in a new window.
4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string replacement_path;
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
4272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "files/click-noreferrer-links.html",
4285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &replacement_path));
4302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
4312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Get the original SiteInstance for later comparison.
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
435868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Test clicking a target=_blank link.
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ShellAddedObserver new_shell_observer;
4392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool success = false;
4402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetBlankLink());",
4432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &success));
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(success);
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
4462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Wait for the navigation in the new tab to finish, if it hasn't.
4482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
4492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ("/files/title2.html",
4503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
4512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Should have the same SiteInstance.
4532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<SiteInstance> blank_site_instance(
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
4552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(orig_site_instance, blank_site_instance);
4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Now navigate the new tab to a different site.
4585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/title1.html"));
4592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<SiteInstance> new_site_instance(
4602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
4612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_NE(orig_site_instance, new_site_instance);
4622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Now disown the opener.
4642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScript(new_shell->web_contents(),
4652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            "window.opener = null;"));
4662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Go back and ensure the opener is still null.
4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  {
469868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(new_shell->web_contents());
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    new_shell->web_contents()->GetController().GoBack();
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    back_nav_load_observer.Wait();
4722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  success = false;
4742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_shell->web_contents(),
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(window.opener == null);",
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &success));
4782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(success);
4792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Now navigate forward again (creating a new process) and check opener.
4815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/title1.html"));
4822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  success = false;
4832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
4842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_shell->web_contents(),
4852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(window.opener == null);",
4862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &success));
4872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(success);
4882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
490c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Test that subframes can disown their openers.  http://crbug.com/225528.
491f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, DisownSubframeOpener) {
492c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const GURL frame_url("data:text/html,<iframe name=\"foo\"></iframe>");
493c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NavigateToURL(shell(), frame_url);
494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Give the frame an opener using window.open.
496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(ExecuteScript(shell()->web_contents(),
497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            "window.open('about:blank','foo');"));
498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
499c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Now disown the frame's opener.  Shouldn't crash.
500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(ExecuteScript(shell()->web_contents(),
501c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            "window.frames[0].opener = null;"));
502c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
503c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/99202.  PostMessage calls should still work after
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// navigating the source and target windows to different sites.
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Specifically:
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1) Create 3 windows (opener, "foo", and _blank) and send "foo" cross-process.
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 2) Fail to post a message from "foo" to opener with the wrong target origin.
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 3) Post a message from "foo" to opener, which replies back to "foo".
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 4) Post a message from _blank to "foo".
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 5) Post a message from "foo" to a subframe of opener, which replies back.
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 6) Post a message from _blank to a subframe of "foo".
513f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       SupportCrossProcessPostMessage) {
5155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
5215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance and RVHM for later comparison.
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContents* opener_contents = shell()->web_contents();
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      opener_contents->GetSiteInstance());
529868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
530f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  RenderFrameHostManager* opener_manager = static_cast<WebContentsImpl*>(
531868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      opener_contents)->GetRenderManagerForTesting();
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 1) Open two more windows, one named.  These initially have openers but no
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // reference to each other.  We will later post a message between them.
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // First, a named target=foo window.
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
5392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      opener_contents,
5412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetedLink());",
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new window to finish, if it hasn't, then
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // send it to post_message.html on a different site.
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContents* foo_contents = new_shell->web_contents();
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(foo_contents);
5503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
5513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            foo_contents->GetLastCommittedURL().path());
5525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/post_message.html"));
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> foo_site_instance(
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      foo_contents->GetSiteInstance());
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(orig_site_instance, foo_site_instance);
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Second, a target=_blank window.
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer2;
5592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetBlankLink());",
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new window to finish, if it hasn't, then
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // send it to post_message.html on the original site.
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell2 = new_shell_observer2.GetShell();
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContents* new_contents = new_shell2->web_contents();
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_contents);
5703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  EXPECT_EQ("/files/title2.html", new_contents->GetLastCommittedURL().path());
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(new_shell2, test_server()->GetURL("files/post_message.html"));
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, new_contents->GetSiteInstance());
573f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  RenderFrameHostManager* new_manager =
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      static_cast<WebContentsImpl*>(new_contents)->GetRenderManagerForTesting();
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We now have three windows.  The opener should have a swapped out RVH
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for the new SiteInstance, but the _blank window should not.
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(3u, Shell::windows().size());
579868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(
580868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      opener_manager->GetSwappedOutRenderViewHost(foo_site_instance.get()));
581868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(
582868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      new_manager->GetSwappedOutRenderViewHost(foo_site_instance.get()));
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 2) Fail to post a message from the foo window to the opener if the target
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // origin is wrong.  We won't see an error, but we can check for the right
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // number of received messages below.
5872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
5882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      foo_contents,
5892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(postToOpener('msg',"
5902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "    'http://google.com'));",
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
593868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_FALSE(
594868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      opener_manager->GetSwappedOutRenderViewHost(orig_site_instance.get()));
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 3) Post a message from the foo window to the opener.  The opener will
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // reply, causing the foo window to update its own title.
5984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  base::string16 expected_title = ASCIIToUTF16("msg");
5994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  TitleWatcher title_watcher(foo_contents, expected_title);
6002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
6012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      foo_contents,
6022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(postToOpener('msg','*'));",
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
605868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_FALSE(
606868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      opener_manager->GetSwappedOutRenderViewHost(orig_site_instance.get()));
6074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle());
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We should have received only 1 message in the opener and "foo" tabs,
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and updated the title.
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int opener_received_messages = 0;
6122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractInt(
6132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      opener_contents,
6142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(window.receivedMessages);",
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &opener_received_messages));
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int foo_received_messages = 0;
6172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractInt(
6182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      foo_contents,
6192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(window.receivedMessages);",
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &foo_received_messages));
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1, foo_received_messages);
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1, opener_received_messages);
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("msg"), foo_contents->GetTitle());
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 4) Now post a message from the _blank window to the foo window.  The
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // foo window will update its title and will not reply.
6274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  expected_title = ASCIIToUTF16("msg2");
6284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  TitleWatcher title_watcher2(foo_contents, expected_title);
6292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
6302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_contents,
6312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(postToFoo('msg2'));",
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
6344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ASSERT_EQ(expected_title, title_watcher2.WaitAndGetTitle());
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This postMessage should have created a swapped out RVH for the new
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SiteInstance in the target=_blank window.
638868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(
639868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      new_manager->GetSwappedOutRenderViewHost(foo_site_instance.get()));
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(nasko): Test subframe targeting of postMessage once
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://crbug.com/153701 is fixed.
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Test for crbug.com/278336. MessagePorts should work cross-process. I.e.,
6468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// messages which contain Transferables and get intercepted by
6478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// RenderViewImpl::willCheckAndDispatchMessageEvent (because the RenderView is
6488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// swapped out) should work.
6498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Specifically:
6508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// 1) Create 2 windows (opener and "foo") and send "foo" cross-process.
6518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// 2) Post a message containing a message port from opener to "foo".
6528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// 3) Post a message from "foo" back to opener via the passed message port.
6538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// The test will be enabled when the feature implementation lands.
654f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
6558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                       SupportCrossProcessPostMessageWithMessagePort) {
6565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
6578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
6588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Load a page with links that open in a new window.
6598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  std::string replacement_path;
6608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
6618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "files/click-noreferrer-links.html",
6625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
6638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &replacement_path));
6648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
6658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
6668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Get the original SiteInstance and RVHM for later comparison.
6678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  WebContents* opener_contents = shell()->web_contents();
6688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
6698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_contents->GetSiteInstance());
6708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
671f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  RenderFrameHostManager* opener_manager = static_cast<WebContentsImpl*>(
6728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_contents)->GetRenderManagerForTesting();
6738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
6748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // 1) Open a named target=foo window. We will later post a message between the
6758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // opener and the new window.
6768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ShellAddedObserver new_shell_observer;
6778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bool success = false;
6788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
6798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_contents,
6808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetedLink());",
6818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &success));
6828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(success);
6838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
6848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
6858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Wait for the navigation in the new window to finish, if it hasn't, then
6868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // send it to post_message.html on a different site.
6878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  WebContents* foo_contents = new_shell->web_contents();
6888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  WaitForLoadStop(foo_contents);
6898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
6908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            foo_contents->GetLastCommittedURL().path());
6915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/post_message.html"));
6928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_refptr<SiteInstance> foo_site_instance(
6938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      foo_contents->GetSiteInstance());
6948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_NE(orig_site_instance, foo_site_instance);
6958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
6968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // We now have two windows. The opener should have a swapped out RVH
6978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // for the new SiteInstance.
6988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(2u, Shell::windows().size());
6998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(
7008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_manager->GetSwappedOutRenderViewHost(foo_site_instance.get()));
7018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
7028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // 2) Post a message containing a MessagePort from opener to the the foo
7038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // window. The foo window will reply via the passed port, causing the opener
7048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // to update its own title.
7055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 expected_title = ASCIIToUTF16("msg-back-via-port");
7065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TitleWatcher title_observer(opener_contents, expected_title);
7078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
7088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_contents,
7098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "window.domAutomationController.send(postWithPortToFoo());",
7108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &success));
7118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(success);
7128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_FALSE(
7138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_manager->GetSwappedOutRenderViewHost(orig_site_instance.get()));
7145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(expected_title, title_observer.WaitAndGetTitle());
7158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
7168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Check message counts.
7178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int opener_received_messages_via_port = 0;
7188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractInt(
7198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_contents,
7208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "window.domAutomationController.send(window.receivedMessagesViaPort);",
7218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &opener_received_messages_via_port));
7228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int foo_received_messages = 0;
7238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractInt(
7248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      foo_contents,
7258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "window.domAutomationController.send(window.receivedMessages);",
7268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &foo_received_messages));
7278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int foo_received_messages_with_port = 0;
7288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractInt(
7298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      foo_contents,
7308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "window.domAutomationController.send(window.receivedMessagesWithPort);",
7318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &foo_received_messages_with_port));
7328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(1, foo_received_messages);
7338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(1, foo_received_messages_with_port);
7348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(1, opener_received_messages_via_port);
7358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("msg-with-port"), foo_contents->GetTitle());
7368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("msg-back-via-port"), opener_contents->GetTitle());
7378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
7388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/116192.  Navigations to a window's opener should
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// still work after a process swap.
741f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       AllowTargetedNavigationsInOpenerAfterSwap) {
7435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
7465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
7495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original tab and SiteInstance for later comparison.
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContents* orig_contents = shell()->web_contents();
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      orig_contents->GetSiteInstance());
757868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test clicking a target=foo link.
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
7622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
7632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      orig_contents,
7642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetedLink());",
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new window to finish, if it hasn't.
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
7723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have the same SiteInstance.
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> blank_site_instance(
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, blank_site_instance);
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now navigate the original (opener) tab to a different site.
7805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title1.html"));
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> new_site_instance(
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(orig_site_instance, new_site_instance);
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The opened tab should be able to navigate the opener back to its process.
786868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TestNavigationObserver navigation_observer(orig_contents);
7872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
7882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_shell->web_contents(),
7892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(navigateOpener());",
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
7915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  navigation_observer.Wait();
7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have swapped back into this process.
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> revisit_site_instance(
7965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
7975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, revisit_site_instance);
7985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test that opening a new window in the same SiteInstance and then navigating
8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// both windows to a different SiteInstance allows the first process to exit.
8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See http://crbug.com/126333.
803f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       ProcessExitWithSwappedOutViews) {
8055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
8095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
8105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
8115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
8125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
8145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance for later comparison.
8165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
8175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
818868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test clicking a target=foo link.
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
8225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
8232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
8242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
8252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetedLink());",
8265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
8285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new window to finish, if it hasn't.
8315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
8333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
8345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have the same SiteInstance.
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> opened_site_instance(
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, opened_site_instance);
8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now navigate the opened window to a different site.
8415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/title1.html"));
8425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> new_site_instance(
8435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
8445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(orig_site_instance, new_site_instance);
8455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The original process should still be alive, since it is still used in the
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // first window.
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RenderProcessHost* orig_process = orig_site_instance->GetProcess();
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(orig_process->HasConnection());
8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Navigate the first window to a different site as well.  The original
8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // process should exit, since all of its views are now swapped out.
8535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderProcessHostWatcher exit_observer(
8545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      orig_process,
8555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      RenderProcessHostWatcher::WATCH_FOR_HOST_DESTRUCTION);
8565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title1.html"));
8575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  exit_observer.Wait();
8585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> new_site_instance2(
8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
8605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(new_site_instance, new_site_instance2);
8615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/76666.  A cross-site navigation that fails with a 204
8645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// error should not make us ignore future renderer-initiated navigations.
865f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, ClickLinkAfter204Error) {
8665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
8675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance for later comparison.
8695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
8705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
871868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
8725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a cross-site page that fails with a 204 error.
8740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  NavigateToURL(shell(), GetCrossSiteURL("nocontent"));
8755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // We should still be looking at the normal page.  Because we started from a
8770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // blank new tab, the typed URL will still be visible until the user clears it
8780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // manually.  The last committed URL will be the previous page.
8795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> post_nav_site_instance(
8805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
8815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, post_nav_site_instance);
8823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  EXPECT_EQ("/nocontent",
8833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            shell()->web_contents()->GetVisibleURL().path());
8840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  EXPECT_FALSE(
8850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      shell()->web_contents()->GetController().GetLastCommittedEntry());
8865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Renderer-initiated navigations should work.
8880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  base::string16 expected_title = ASCIIToUTF16("Title Of Awesomeness");
8890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  TitleWatcher title_watcher(shell()->web_contents(), expected_title);
8900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  GURL url = test_server()->GetURL("files/title2.html");
8910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  EXPECT_TRUE(ExecuteScript(
8922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
8930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      base::StringPrintf("location.href = '%s'", url.spec().c_str())));
8940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle());
8955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Opens in same tab.
8975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1u, Shell::windows().size());
8983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  EXPECT_EQ("/files/title2.html",
8993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            shell()->web_contents()->GetLastCommittedURL().path());
9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have the same SiteInstance.
9020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  scoped_refptr<SiteInstance> new_site_instance(
9035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
9040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  EXPECT_EQ(orig_site_instance, new_site_instance);
9055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
907868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Test for crbug.com/9682.  We should show the URL for a pending renderer-
908868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// initiated navigation in a new tab, until the content of the initial
909868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// about:blank page is modified by another window.  At that point, we should
910868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// revert to showing about:blank to prevent a URL spoof.
911f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, ShowLoadingURLUntilSpoof) {
912868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(test_server()->Start());
913868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
914868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Load a page that can open a URL that won't commit in a new window.
915868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToURL(
916868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      shell(), test_server()->GetURL("files/click-nocontent-link.html"));
917868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  WebContents* orig_contents = shell()->web_contents();
918868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
919868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Click a /nocontent link that opens in a new window but never commits.
920868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
921868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool success = false;
922868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
923868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      orig_contents,
924868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "window.domAutomationController.send(clickNoContentTargetedLink());",
925868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      &success));
926868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(success);
927868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
928868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Wait for the window to open.
929868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
930868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
931868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Ensure the destination URL is visible, because it is considered the
932868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // initial navigation.
933868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  WebContents* contents = new_shell->web_contents();
934868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(contents->GetController().IsInitialNavigation());
935868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("/nocontent",
936868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            contents->GetController().GetVisibleEntry()->GetURL().path());
937868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
938868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Now modify the contents of the new window from the opener.  This will also
939868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // modify the title of the document to give us something to listen for.
9404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  base::string16 expected_title = ASCIIToUTF16("Modified Title");
9414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  TitleWatcher title_watcher(contents, expected_title);
942868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  success = false;
943868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
944868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      orig_contents,
945868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "window.domAutomationController.send(modifyNewWindow());",
946868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      &success));
947868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(success);
9484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle());
949868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
950868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // At this point, we should no longer be showing the destination URL.
951868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // The visible entry should be null, resulting in about:blank in the address
952868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // bar.
953868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(contents->GetController().GetVisibleEntry());
954868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
955868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
956868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Test for crbug.com/9682.  We should not show the URL for a pending renderer-
957868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// initiated navigation in a new tab if it is not the initial navigation.  In
958868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// this case, the renderer will not notify us of a modification, so we cannot
959868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// show the pending URL without allowing a spoof.
960f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
961868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                       DontShowLoadingURLIfNotInitialNav) {
962868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(test_server()->Start());
963868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
964868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Load a page that can open a URL that won't commit in a new window.
965868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToURL(
966868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      shell(), test_server()->GetURL("files/click-nocontent-link.html"));
967868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  WebContents* orig_contents = shell()->web_contents();
968868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
969868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Click a /nocontent link that opens in a new window but never commits.
970868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // By using an onclick handler that first creates the window, the slow
971868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // navigation is not considered an initial navigation.
972868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
973868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool success = false;
974868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
975868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      orig_contents,
976868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "window.domAutomationController.send("
977868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "clickNoContentScriptedTargetedLink());",
978868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      &success));
979868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(success);
980868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
981868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Wait for the window to open.
982868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
983868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
984868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Ensure the destination URL is not visible, because it is not the initial
985868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // navigation.
986868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  WebContents* contents = new_shell->web_contents();
987868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(contents->GetController().IsInitialNavigation());
988868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(contents->GetController().GetVisibleEntry());
989868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
990868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
991effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Crashes under ThreadSanitizer, http://crbug.com/356758.
992effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#if defined(THREAD_SANITIZER)
993effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define MAYBE_BackForwardNotStale DISABLED_BackForwardNotStale
994effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#else
995effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define MAYBE_BackForwardNotStale BackForwardNotStale
996effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#endif
9975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for http://crbug.com/93427.  Ensure that cross-site navigations
9985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// do not cause back/forward navigations to be considered stale by the
9995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// renderer.
1000effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochIN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, MAYBE_BackForwardNotStale) {
10015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
1002f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  NavigateToURL(shell(), GURL(url::kAboutBlankURL));
10035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Visit a page on first site.
10055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL("files/title1.html"));
10065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Visit three pages on second site.
10085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title1.html"));
10095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title2.html"));
10105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title3.html"));
10115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // History is now [blank, A1, B1, B2, *B3].
10135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContents* contents = shell()->web_contents();
10145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(5, contents->GetController().GetEntryCount());
10155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Open another window in same process to keep this process alive.
10175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = CreateBrowser();
10185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/title1.html"));
10195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Go back three times to first site.
10215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1022868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(shell()->web_contents());
10235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoBack();
10245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    back_nav_load_observer.Wait();
10255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1027868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(shell()->web_contents());
10285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoBack();
10295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    back_nav_load_observer.Wait();
10305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1032868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(shell()->web_contents());
10335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoBack();
10345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    back_nav_load_observer.Wait();
10355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now go forward twice to B2.  Shouldn't be left spinning.
10385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1039868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver forward_nav_load_observer(shell()->web_contents());
10405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoForward();
10415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    forward_nav_load_observer.Wait();
10425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1044868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver forward_nav_load_observer(shell()->web_contents());
10455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoForward();
10465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    forward_nav_load_observer.Wait();
10475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Go back twice to first site.
10505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1051868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(shell()->web_contents());
10525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoBack();
10535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    back_nav_load_observer.Wait();
10545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1056868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(shell()->web_contents());
10575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoBack();
10585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    back_nav_load_observer.Wait();
10595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now go forward directly to B3.  Shouldn't be left spinning.
10625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1063868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver forward_nav_load_observer(shell()->web_contents());
10645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoToIndex(4);
10655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    forward_nav_load_observer.Wait();
10665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for http://crbug.com/130016.
10705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Swapping out a render view should update its visiblity state.
1071f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
10725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       SwappedOutViewHasCorrectVisibilityState) {
10735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
10745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
10765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
10775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
10785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
10795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
10805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
10815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
10825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Open a same-site link in a new widnow.
10845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
10855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
10862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
10872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
10882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetedLink());",
10895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
10905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
10915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
10925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new tab to finish, if it hasn't.
10945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
10955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
10963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
10975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RenderViewHost* rvh = new_shell->web_contents()->GetRenderViewHost();
10992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
11012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      rvh,
11022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send("
1103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "    document.visibilityState == 'visible');",
11045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
11055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
11065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now navigate the new window to a different site. This should swap out the
11085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // tab's existing RenderView, causing it become hidden.
11095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/title1.html"));
11105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
11122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      rvh,
11132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send("
1114f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "    document.visibilityState == 'hidden');",
11155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
11165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
11175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Going back should make the previously swapped-out view to become visible
11195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // again.
11205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(new_shell->web_contents());
11225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_shell->web_contents()->GetController().GoBack();
11235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    back_nav_load_observer.Wait();
11245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
11255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
11273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
11285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(rvh, new_shell->web_contents()->GetRenderViewHost());
11305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
11322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      rvh,
11332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send("
1134f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "    document.visibilityState == 'visible');",
11355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
11365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
11375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// This class ensures that all the given RenderViewHosts have properly been
11401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// shutdown.
11411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class RenderViewHostDestructionObserver : public WebContentsObserver {
11425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
11431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  explicit RenderViewHostDestructionObserver(WebContents* web_contents)
11441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      : WebContentsObserver(web_contents) {}
11451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual ~RenderViewHostDestructionObserver() {}
11461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void EnsureRVHGetsDestructed(RenderViewHost* rvh) {
11471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    watched_render_view_hosts_.insert(rvh);
11485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
11491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  size_t GetNumberOfWatchedRenderViewHosts() const {
11501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return watched_render_view_hosts_.size();
11515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1152a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
11535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
11541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // WebContentsObserver implementation:
11551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void RenderViewDeleted(RenderViewHost* rvh) OVERRIDE {
11561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    watched_render_view_hosts_.erase(rvh);
11575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
11585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  std::set<RenderViewHost*> watched_render_view_hosts_;
11605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
11615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1162effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Crashes under ThreadSanitizer, http://crbug.com/356758.
1163effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#if defined(THREAD_SANITIZER)
1164effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define MAYBE_LeakingRenderViewHosts DISABLED_LeakingRenderViewHosts
1165effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#else
1166effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define MAYBE_LeakingRenderViewHosts LeakingRenderViewHosts
1167effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#endif
11685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/90867. Make sure we don't leak render view hosts since
11695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// they may cause crashes or memory corruptions when trying to call dead
11705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// delegate_. This test also verifies crbug.com/117420 and crbug.com/143255 to
11715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ensure that a separate SiteInstance is created when navigating to view-source
11725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// URLs, regardless of current URL.
1173effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochIN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
1174effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                       MAYBE_LeakingRenderViewHosts) {
11755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
11765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Observe the created render_view_host's to make sure they will not leak.
11781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  RenderViewHostDestructionObserver rvh_observers(shell()->web_contents());
11795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL navigated_url(test_server()->GetURL("files/title2.html"));
1181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  GURL view_source_url(kViewSourceScheme + std::string(":") +
1182868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                       navigated_url.spec());
11835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Let's ensure that when we start with a blank window, navigating away to a
11855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // view-source URL, we create a new SiteInstance.
1186868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  RenderViewHost* blank_rvh = shell()->web_contents()->GetRenderViewHost();
11875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SiteInstance* blank_site_instance = blank_rvh->GetSiteInstance();
11883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(), GURL::EmptyGURL());
11895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(blank_site_instance->GetSiteURL(), GURL::EmptyGURL());
11901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  rvh_observers.EnsureRVHGetsDestructed(blank_rvh);
11915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now navigate to the view-source URL and ensure we got a different
11935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SiteInstance and RenderViewHost.
11945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), view_source_url);
11955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(blank_rvh, shell()->web_contents()->GetRenderViewHost());
11965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(blank_site_instance, shell()->web_contents()->
11975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GetRenderViewHost()->GetSiteInstance());
11981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  rvh_observers.EnsureRVHGetsDestructed(
11991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      shell()->web_contents()->GetRenderViewHost());
12005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a random page and then navigate to view-source: of it.
12025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This used to cause two RVH instances for the same SiteInstance, which
12035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // was a problem.  This is no longer the case.
12045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), navigated_url);
12055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SiteInstance* site_instance1 = shell()->web_contents()->
12065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GetRenderViewHost()->GetSiteInstance();
12071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  rvh_observers.EnsureRVHGetsDestructed(
12081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      shell()->web_contents()->GetRenderViewHost());
12095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), view_source_url);
12111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  rvh_observers.EnsureRVHGetsDestructed(
12121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      shell()->web_contents()->GetRenderViewHost());
12135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SiteInstance* site_instance2 = shell()->web_contents()->
12145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GetRenderViewHost()->GetSiteInstance();
12155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Ensure that view-source navigations force a new SiteInstance.
12175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(site_instance1, site_instance2);
12185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now navigate to a different instance so that we swap out again.
12205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title2.html"));
12211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  rvh_observers.EnsureRVHGetsDestructed(
12221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      shell()->web_contents()->GetRenderViewHost());
12235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This used to leak a render view host.
12255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  shell()->Close();
12265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RunAllPendingInMessageLoop();  // Needed on ChromeOS.
12285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_EQ(0U, rvh_observers.GetNumberOfWatchedRenderViewHosts());
12305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
12315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/143155.  Frame tree updates during unload should not
12335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// interrupt the intended navigation and show swappedout:// instead.
12345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Specifically:
12355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1) Open 2 tabs in an HTTP SiteInstance, with a subframe in the opener.
12365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 2) Send the second tab to a different foo.com SiteInstance.
12375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//    This creates a swapped out opener for the first tab in the foo process.
12385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 3) Navigate the first tab to the foo.com SiteInstance, and have the first
12395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    tab's unload handler remove its frame.
12405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This used to cause an update to the frame tree of the swapped out RV,
12415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// just as it was navigating to a real page.  That pre-empted the real
12425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// navigation and visibly sent the tab to swappedout://.
1243f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
12445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       DontPreemptNavigationWithFrameTreeUpdate) {
12455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
12465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 1. Load a page that deletes its iframe during unload.
12485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(),
12495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                test_server()->GetURL("files/remove_frame_on_unload.html"));
12505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance for later comparison.
12525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
12535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
12545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Open a same-site page in a new window.
12565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
12575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
12582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
12592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
12602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(openWindow());",
12615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
12625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
12635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
12645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new window to finish, if it hasn't.
12665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
12675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("/files/title1.html",
12683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
12695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have the same SiteInstance.
12715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, new_shell->web_contents()->GetSiteInstance());
12725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 2. Send the second tab to a different process.
12745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/title1.html"));
12755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> new_site_instance(
12765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
12775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(orig_site_instance, new_site_instance);
12785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 3. Send the first tab to the second tab's process.
12805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title1.html"));
12815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make sure it ends up at the right page.
12835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(shell()->web_contents());
12845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(GetCrossSiteURL("files/title1.html"),
12853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            shell()->web_contents()->GetLastCommittedURL());
12865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(new_site_instance, shell()->web_contents()->GetSiteInstance());
12875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
12885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Ensure that renderer-side debug URLs do not cause a process swap, since they
12905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// are meant to run in the current page.  We had a bug where we expected a
12915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// BrowsingInstance swap to occur on pages like view-source and extensions,
12925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// which broke chrome://crash and javascript: URLs.
12935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// See http://crbug.com/335503.
12945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, RendererDebugURLsDontSwap) {
12955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(test_server()->Start());
12965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
12975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL original_url(test_server()->GetURL("files/title2.html"));
12985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL view_source_url(kViewSourceScheme + std::string(":") +
12995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       original_url.spec());
13005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), view_source_url);
13025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Check that javascript: URLs work.
13045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 expected_title = ASCIIToUTF16("msg");
13055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TitleWatcher title_watcher(shell()->web_contents(), expected_title);
13065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  shell()->LoadURL(GURL("javascript:document.title='msg'"));
13075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle());
13085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Crash the renderer of the view-source page.
13105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderProcessHostWatcher crash_observer(
13115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      shell()->web_contents(),
13125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
13135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GURL(kChromeUICrashURL));
13145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  crash_observer.Wait();
13155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
13165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Ensure that renderer-side debug URLs don't take effect on crashed renderers.
13185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Otherwise, we might try to load an unprivileged about:blank page into a
13195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// WebUI-enabled RenderProcessHost, failing a safety check in InitRenderView.
13205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// See http://crbug.com/334214.
13215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
13225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       IgnoreRendererDebugURLsWhenCrashed) {
13235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Visit a WebUI page with bindings.
13245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL webui_url = GURL(std::string(kChromeUIScheme) + "://" +
13255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        std::string(kChromeUIGpuHost));
13265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), webui_url);
13275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
13285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                  shell()->web_contents()->GetRenderProcessHost()->GetID()));
13295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Crash the renderer of the WebUI page.
13315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderProcessHostWatcher crash_observer(
13325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      shell()->web_contents(),
13335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
13345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GURL(kChromeUICrashURL));
13355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  crash_observer.Wait();
13365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Load the crash URL again but don't wait for any action.  If it is not
13385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // ignored this time, we will fail the WebUI CHECK in InitRenderView.
13395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  shell()->LoadURL(GURL(kChromeUICrashURL));
13405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Ensure that such URLs can still work as the initial navigation of a tab.
13425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // We postpone the initial navigation of the tab using an empty GURL, so that
13435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // we can add a watcher for crashes.
13445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Shell* shell2 = Shell::CreateNewWindow(
13455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      shell()->web_contents()->GetBrowserContext(), GURL(), NULL,
13465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      MSG_ROUTING_NONE, gfx::Size());
13475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderProcessHostWatcher crash_observer2(
13485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      shell2->web_contents(),
13495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
13505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell2, GURL(kChromeUIKillURL));
13515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  crash_observer2.Wait();
13525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
13535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Ensure that pending_and_current_web_ui_ is cleared when a URL commits.
13555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Otherwise it might get picked up by InitRenderView when granting bindings
13565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// to other RenderViewHosts.  See http://crbug.com/330811.
13575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, ClearPendingWebUIOnCommit) {
13585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Visit a WebUI page with bindings.
13595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL webui_url(GURL(std::string(kChromeUIScheme) + "://" +
13605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                      std::string(kChromeUIGpuHost)));
13615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), webui_url);
13625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
13635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                  shell()->web_contents()->GetRenderProcessHost()->GetID()));
13645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
13655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      shell()->web_contents());
13665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WebUIImpl* webui = web_contents->GetRenderManagerForTesting()->web_ui();
13675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(webui);
13685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(web_contents->GetRenderManagerForTesting()->pending_web_ui());
13695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Navigate to another WebUI URL that reuses the WebUI object.  Make sure we
13715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // clear pending_web_ui() when it commits.
13725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL webui_url2(webui_url.spec() + "#foo");
13735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), webui_url2);
13745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(webui, web_contents->GetRenderManagerForTesting()->web_ui());
13755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(web_contents->GetRenderManagerForTesting()->pending_web_ui());
13765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
13775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1378a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class RFHMProcessPerTabTest : public RenderFrameHostManagerTest {
1379a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
1380a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  RFHMProcessPerTabTest() {}
1381a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1382a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
1383a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    command_line->AppendSwitch(switches::kProcessPerTab);
1384a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
1385a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
1386a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1387a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Test that we still swap processes for BrowsingInstance changes even in
1388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// --process-per-tab mode.  See http://crbug.com/343017.
1389a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Disabled on Android: http://crbug.com/345873.
1390effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Crashes under ThreadSanitizer, http://crbug.com/356758.
1391effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#if defined(OS_ANDROID) || defined(THREAD_SANITIZER)
1392a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define MAYBE_BackFromWebUI DISABLED_BackFromWebUI
1393a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#else
1394a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define MAYBE_BackFromWebUI BackFromWebUI
1395a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
1396a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(RFHMProcessPerTabTest, MAYBE_BackFromWebUI) {
1397a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(test_server()->Start());
1398a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL original_url(test_server()->GetURL("files/title2.html"));
1399a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  NavigateToURL(shell(), original_url);
1400a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1401a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Visit a WebUI page with bindings.
1402a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL webui_url(GURL(std::string(kChromeUIScheme) + "://" +
1403a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                      std::string(kChromeUIGpuHost)));
1404a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  NavigateToURL(shell(), webui_url);
1405a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
1406a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  shell()->web_contents()->GetRenderProcessHost()->GetID()));
1407a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1408a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Go back and ensure we have no WebUI bindings.
1409a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TestNavigationObserver back_nav_load_observer(shell()->web_contents());
1410a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  shell()->web_contents()->GetController().GoBack();
1411a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  back_nav_load_observer.Wait();
1412a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(original_url, shell()->web_contents()->GetLastCommittedURL());
1413a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
1414a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  shell()->web_contents()->GetRenderProcessHost()->GetID()));
1415a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
1416a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1417cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// crbug.com/372360
1418cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// The test loads url1, opens a link pointing to url2 in a new tab, and
1419cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// navigates the new tab to url1.
1420cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// The following is needed for the bug to happen:
1421cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//  - url1 must require webui bindings;
1422cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//  - navigating to url2 in the site instance of url1 should not swap
1423cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//   browsing instances, but should require a new site instance.
1424cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, WebUIGetsBindings) {
1425cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  GURL url1(std::string(kChromeUIScheme) + "://" +
1426cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            std::string(kChromeUIGpuHost));
1427cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  GURL url2(std::string(kChromeUIScheme) + "://" +
1428cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            std::string(kChromeUIAccessibilityHost));
1429cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1430cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Visit a WebUI page with bindings.
1431cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NavigateToURL(shell(), url1);
1432cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
1433cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                  shell()->web_contents()->GetRenderProcessHost()->GetID()));
1434cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SiteInstance* site_instance1 = shell()->web_contents()->GetSiteInstance();
1435cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1436cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Open a new tab. Initially it gets a render view in the original tab's
1437cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // current site instance.
1438cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestNavigationObserver nav_observer(NULL);
1439cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  nav_observer.StartWatchingNewWebContents();
1440cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ShellAddedObserver shao;
1441cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  OpenUrlViaClickTarget(shell()->web_contents(), url2);
1442cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  nav_observer.Wait();
1443cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  Shell* new_shell = shao.GetShell();
1444cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  WebContentsImpl* new_web_contents = static_cast<WebContentsImpl*>(
1445cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      new_shell->web_contents());
1446cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SiteInstance* site_instance2 = new_web_contents->GetSiteInstance();
1447cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1448cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_NE(site_instance2, site_instance1);
1449cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(site_instance2->IsRelatedSiteInstance(site_instance1));
1450cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  RenderViewHost* initial_rvh = new_web_contents->
1451cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      GetRenderManagerForTesting()->GetSwappedOutRenderViewHost(site_instance1);
1452cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_TRUE(initial_rvh);
1453cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The following condition is what was causing the bug.
1454cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_EQ(0, initial_rvh->GetEnabledBindings());
1455cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1456cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Navigate to url1 and check bindings.
1457cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NavigateToURL(new_shell, url1);
1458cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The navigation should have used the first SiteInstance, otherwise
1459cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // |initial_rvh| did not have a chance to be used.
1460cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_EQ(new_web_contents->GetSiteInstance(), site_instance1);
1461cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_EQ(BINDINGS_POLICY_WEB_UI,
1462cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      new_web_contents->GetRenderViewHost()->GetEnabledBindings());
1463cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
1464cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
14655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
1466