render_frame_host_manager_browsertest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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);
4011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_EQ(new_site_instance.get(),
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.
41403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// This test crashes under ThreadSanitizer, http://crbug.com/356758.
41503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#if defined(THREAD_SANITIZER)
416d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#define MAYBE_DisownOpener DISABLED_DisownOpener
417d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#else
418d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#define MAYBE_DisownOpener DisownOpener
419d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif
420f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, MAYBE_DisownOpener) {
4215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
4222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Load a page with links that open in a new window.
4242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string replacement_path;
4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "files/click-noreferrer-links.html",
4275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
4282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &replacement_path));
4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
4302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Get the original SiteInstance for later comparison.
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
434868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Test clicking a target=_blank link.
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ShellAddedObserver new_shell_observer;
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool success = false;
4392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
4402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetBlankLink());",
4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &success));
4432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(success);
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
44503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_TRUE(new_shell->web_contents()->HasOpener());
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);
46203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_TRUE(new_shell->web_contents()->HasOpener());
4632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Now disown the opener.
4652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScript(new_shell->web_contents(),
4662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            "window.opener = null;"));
46703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_FALSE(new_shell->web_contents()->HasOpener());
4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Go back and ensure the opener is still null.
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  {
471868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(new_shell->web_contents());
4722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    new_shell->web_contents()->GetController().GoBack();
4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    back_nav_load_observer.Wait();
4742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  success = false;
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_shell->web_contents(),
4782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(window.opener == null);",
4792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &success));
4802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(success);
48103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_FALSE(new_shell->web_contents()->HasOpener());
4822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Now navigate forward again (creating a new process) and check opener.
4845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/title1.html"));
4852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  success = false;
4862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
4872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_shell->web_contents(),
4882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(window.opener == null);",
4892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &success));
4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(success);
49103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  EXPECT_FALSE(new_shell->web_contents()->HasOpener());
4922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Test that subframes can disown their openers.  http://crbug.com/225528.
495f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, DisownSubframeOpener) {
496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const GURL frame_url("data:text/html,<iframe name=\"foo\"></iframe>");
497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NavigateToURL(shell(), frame_url);
498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
499c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Give the frame an opener using window.open.
500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(ExecuteScript(shell()->web_contents(),
501c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            "window.open('about:blank','foo');"));
502c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
503c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Now disown the frame's opener.  Shouldn't crash.
504c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(ExecuteScript(shell()->web_contents(),
505c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            "window.frames[0].opener = null;"));
506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/99202.  PostMessage calls should still work after
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// navigating the source and target windows to different sites.
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Specifically:
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1) Create 3 windows (opener, "foo", and _blank) and send "foo" cross-process.
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 2) Fail to post a message from "foo" to opener with the wrong target origin.
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 3) Post a message from "foo" to opener, which replies back to "foo".
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 4) Post a message from _blank to "foo".
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 5) Post a message from "foo" to a subframe of opener, which replies back.
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 6) Post a message from _blank to a subframe of "foo".
517f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       SupportCrossProcessPostMessage) {
5195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
5255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance and RVHM for later comparison.
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContents* opener_contents = shell()->web_contents();
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      opener_contents->GetSiteInstance());
533868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
534f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  RenderFrameHostManager* opener_manager = static_cast<WebContentsImpl*>(
535868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      opener_contents)->GetRenderManagerForTesting();
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 1) Open two more windows, one named.  These initially have openers but no
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // reference to each other.  We will later post a message between them.
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // First, a named target=foo window.
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
5432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
5442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      opener_contents,
5452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetedLink());",
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new window to finish, if it hasn't, then
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // send it to post_message.html on a different site.
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContents* foo_contents = new_shell->web_contents();
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(foo_contents);
5543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
5553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            foo_contents->GetLastCommittedURL().path());
5565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/post_message.html"));
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> foo_site_instance(
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      foo_contents->GetSiteInstance());
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(orig_site_instance, foo_site_instance);
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Second, a target=_blank window.
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer2;
5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
5642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
5652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetBlankLink());",
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new window to finish, if it hasn't, then
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // send it to post_message.html on the original site.
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell2 = new_shell_observer2.GetShell();
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContents* new_contents = new_shell2->web_contents();
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_contents);
5743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  EXPECT_EQ("/files/title2.html", new_contents->GetLastCommittedURL().path());
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(new_shell2, test_server()->GetURL("files/post_message.html"));
5761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_EQ(orig_site_instance.get(), new_contents->GetSiteInstance());
577f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  RenderFrameHostManager* new_manager =
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      static_cast<WebContentsImpl*>(new_contents)->GetRenderManagerForTesting();
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We now have three windows.  The opener should have a swapped out RVH
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for the new SiteInstance, but the _blank window should not.
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(3u, Shell::windows().size());
583868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(
584868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      opener_manager->GetSwappedOutRenderViewHost(foo_site_instance.get()));
585868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(
586868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      new_manager->GetSwappedOutRenderViewHost(foo_site_instance.get()));
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 2) Fail to post a message from the foo window to the opener if the target
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // origin is wrong.  We won't see an error, but we can check for the right
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // number of received messages below.
5912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
5922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      foo_contents,
5932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(postToOpener('msg',"
5942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "    'http://google.com'));",
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
597868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_FALSE(
598868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      opener_manager->GetSwappedOutRenderViewHost(orig_site_instance.get()));
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 3) Post a message from the foo window to the opener.  The opener will
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // reply, causing the foo window to update its own title.
6024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  base::string16 expected_title = ASCIIToUTF16("msg");
6034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  TitleWatcher title_watcher(foo_contents, expected_title);
6042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
6052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      foo_contents,
6062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(postToOpener('msg','*'));",
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
609868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_FALSE(
610868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      opener_manager->GetSwappedOutRenderViewHost(orig_site_instance.get()));
6114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle());
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We should have received only 1 message in the opener and "foo" tabs,
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and updated the title.
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int opener_received_messages = 0;
6162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractInt(
6172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      opener_contents,
6182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(window.receivedMessages);",
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &opener_received_messages));
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int foo_received_messages = 0;
6212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractInt(
6222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      foo_contents,
6232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(window.receivedMessages);",
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &foo_received_messages));
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1, foo_received_messages);
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1, opener_received_messages);
6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("msg"), foo_contents->GetTitle());
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 4) Now post a message from the _blank window to the foo window.  The
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // foo window will update its title and will not reply.
6314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  expected_title = ASCIIToUTF16("msg2");
6324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  TitleWatcher title_watcher2(foo_contents, expected_title);
6332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
6342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_contents,
6352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(postToFoo('msg2'));",
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
6384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ASSERT_EQ(expected_title, title_watcher2.WaitAndGetTitle());
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This postMessage should have created a swapped out RVH for the new
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SiteInstance in the target=_blank window.
642868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(
643868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      new_manager->GetSwappedOutRenderViewHost(foo_site_instance.get()));
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(nasko): Test subframe targeting of postMessage once
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://crbug.com/153701 is fixed.
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Test for crbug.com/278336. MessagePorts should work cross-process. I.e.,
6508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// messages which contain Transferables and get intercepted by
6518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// RenderViewImpl::willCheckAndDispatchMessageEvent (because the RenderView is
6528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// swapped out) should work.
6538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Specifically:
6548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// 1) Create 2 windows (opener and "foo") and send "foo" cross-process.
6558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// 2) Post a message containing a message port from opener to "foo".
6568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// 3) Post a message from "foo" back to opener via the passed message port.
6578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// The test will be enabled when the feature implementation lands.
658f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
6598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                       SupportCrossProcessPostMessageWithMessagePort) {
6605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
6618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
6628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Load a page with links that open in a new window.
6638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  std::string replacement_path;
6648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
6658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "files/click-noreferrer-links.html",
6665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
6678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &replacement_path));
6688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
6698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
6708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Get the original SiteInstance and RVHM for later comparison.
6718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  WebContents* opener_contents = shell()->web_contents();
6728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
6738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_contents->GetSiteInstance());
6748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
675f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  RenderFrameHostManager* opener_manager = static_cast<WebContentsImpl*>(
6768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_contents)->GetRenderManagerForTesting();
6778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
6788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // 1) Open a named target=foo window. We will later post a message between the
6798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // opener and the new window.
6808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ShellAddedObserver new_shell_observer;
6818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bool success = false;
6828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
6838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_contents,
6848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetedLink());",
6858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &success));
6868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(success);
6878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
6888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
6898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Wait for the navigation in the new window to finish, if it hasn't, then
6908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // send it to post_message.html on a different site.
6918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  WebContents* foo_contents = new_shell->web_contents();
6928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  WaitForLoadStop(foo_contents);
6938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
6948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            foo_contents->GetLastCommittedURL().path());
6955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/post_message.html"));
6968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_refptr<SiteInstance> foo_site_instance(
6978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      foo_contents->GetSiteInstance());
6988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_NE(orig_site_instance, foo_site_instance);
6998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
7008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // We now have two windows. The opener should have a swapped out RVH
7018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // for the new SiteInstance.
7028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(2u, Shell::windows().size());
7038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(
7048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_manager->GetSwappedOutRenderViewHost(foo_site_instance.get()));
7058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
7068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // 2) Post a message containing a MessagePort from opener to the the foo
7078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // window. The foo window will reply via the passed port, causing the opener
7088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // to update its own title.
7095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 expected_title = ASCIIToUTF16("msg-back-via-port");
7105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TitleWatcher title_observer(opener_contents, expected_title);
7118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
7128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_contents,
7138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "window.domAutomationController.send(postWithPortToFoo());",
7148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &success));
7158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(success);
7168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_FALSE(
7178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_manager->GetSwappedOutRenderViewHost(orig_site_instance.get()));
7185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(expected_title, title_observer.WaitAndGetTitle());
7198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
7208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Check message counts.
7218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int opener_received_messages_via_port = 0;
7228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractInt(
7238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      opener_contents,
7248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "window.domAutomationController.send(window.receivedMessagesViaPort);",
7258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &opener_received_messages_via_port));
7268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int foo_received_messages = 0;
7278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractInt(
7288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      foo_contents,
7298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "window.domAutomationController.send(window.receivedMessages);",
7308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &foo_received_messages));
7318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int foo_received_messages_with_port = 0;
7328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractInt(
7338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      foo_contents,
7348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "window.domAutomationController.send(window.receivedMessagesWithPort);",
7358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &foo_received_messages_with_port));
7368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(1, foo_received_messages);
7378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(1, foo_received_messages_with_port);
7388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(1, opener_received_messages_via_port);
7398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("msg-with-port"), foo_contents->GetTitle());
7408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("msg-back-via-port"), opener_contents->GetTitle());
7418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
7428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/116192.  Navigations to a window's opener should
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// still work after a process swap.
745f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
7465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       AllowTargetedNavigationsInOpenerAfterSwap) {
7475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
7535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original tab and SiteInstance for later comparison.
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContents* orig_contents = shell()->web_contents();
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      orig_contents->GetSiteInstance());
761868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test clicking a target=foo link.
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
7662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
7672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      orig_contents,
7682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetedLink());",
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new window to finish, if it hasn't.
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
7763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have the same SiteInstance.
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> blank_site_instance(
7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, blank_site_instance);
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now navigate the original (opener) tab to a different site.
7845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title1.html"));
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> new_site_instance(
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(orig_site_instance, new_site_instance);
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The opened tab should be able to navigate the opener back to its process.
790868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TestNavigationObserver navigation_observer(orig_contents);
7912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
7922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_shell->web_contents(),
7932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(navigateOpener());",
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
7965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  navigation_observer.Wait();
7975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have swapped back into this process.
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> revisit_site_instance(
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, revisit_site_instance);
8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test that opening a new window in the same SiteInstance and then navigating
8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// both windows to a different SiteInstance allows the first process to exit.
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See http://crbug.com/126333.
807f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       ProcessExitWithSwappedOutViews) {
8095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
8105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
8125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
8145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
8155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
8165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
8175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
8185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance for later comparison.
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
822868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
8235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Test clicking a target=foo link.
8255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
8265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
8272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
8282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
8292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetedLink());",
8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
8315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
8335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new window to finish, if it hasn't.
8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
8373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have the same SiteInstance.
8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> opened_site_instance(
8415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
8425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, opened_site_instance);
8435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now navigate the opened window to a different site.
8455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/title1.html"));
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> new_site_instance(
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(orig_site_instance, new_site_instance);
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The original process should still be alive, since it is still used in the
8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // first window.
8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RenderProcessHost* orig_process = orig_site_instance->GetProcess();
8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(orig_process->HasConnection());
8545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Navigate the first window to a different site as well.  The original
8565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // process should exit, since all of its views are now swapped out.
8575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderProcessHostWatcher exit_observer(
8585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      orig_process,
8595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      RenderProcessHostWatcher::WATCH_FOR_HOST_DESTRUCTION);
8605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title1.html"));
8615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  exit_observer.Wait();
8625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> new_site_instance2(
8635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
8645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(new_site_instance, new_site_instance2);
8655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/76666.  A cross-site navigation that fails with a 204
8685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// error should not make us ignore future renderer-initiated navigations.
869f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, ClickLinkAfter204Error) {
8705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
8715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance for later comparison.
8735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
8745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
875868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(orig_site_instance.get() != NULL);
8765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a cross-site page that fails with a 204 error.
8780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  NavigateToURL(shell(), GetCrossSiteURL("nocontent"));
8795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // We should still be looking at the normal page.  Because we started from a
8810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // blank new tab, the typed URL will still be visible until the user clears it
8820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // manually.  The last committed URL will be the previous page.
8835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> post_nav_site_instance(
8845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
8855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(orig_site_instance, post_nav_site_instance);
8863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  EXPECT_EQ("/nocontent",
8873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            shell()->web_contents()->GetVisibleURL().path());
8880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  EXPECT_FALSE(
8890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      shell()->web_contents()->GetController().GetLastCommittedEntry());
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Renderer-initiated navigations should work.
8920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  base::string16 expected_title = ASCIIToUTF16("Title Of Awesomeness");
8930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  TitleWatcher title_watcher(shell()->web_contents(), expected_title);
8940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  GURL url = test_server()->GetURL("files/title2.html");
8950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  EXPECT_TRUE(ExecuteScript(
8962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
8970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      base::StringPrintf("location.href = '%s'", url.spec().c_str())));
8980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle());
8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Opens in same tab.
9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(1u, Shell::windows().size());
9023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  EXPECT_EQ("/files/title2.html",
9033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            shell()->web_contents()->GetLastCommittedURL().path());
9045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have the same SiteInstance.
9060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  scoped_refptr<SiteInstance> new_site_instance(
9075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
9080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  EXPECT_EQ(orig_site_instance, new_site_instance);
9095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
911868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Test for crbug.com/9682.  We should show the URL for a pending renderer-
912868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// initiated navigation in a new tab, until the content of the initial
913868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// about:blank page is modified by another window.  At that point, we should
914868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// revert to showing about:blank to prevent a URL spoof.
915f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, ShowLoadingURLUntilSpoof) {
916868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(test_server()->Start());
917868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
918868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Load a page that can open a URL that won't commit in a new window.
919868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToURL(
920868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      shell(), test_server()->GetURL("files/click-nocontent-link.html"));
921868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  WebContents* orig_contents = shell()->web_contents();
922868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
923868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Click a /nocontent link that opens in a new window but never commits.
924868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
925868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool success = false;
926868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
927868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      orig_contents,
928868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "window.domAutomationController.send(clickNoContentTargetedLink());",
929868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      &success));
930868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(success);
931868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
932868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Wait for the window to open.
933868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
934868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
935868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Ensure the destination URL is visible, because it is considered the
936868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // initial navigation.
937868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  WebContents* contents = new_shell->web_contents();
938868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(contents->GetController().IsInitialNavigation());
939868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ("/nocontent",
940868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            contents->GetController().GetVisibleEntry()->GetURL().path());
941868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
942868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Now modify the contents of the new window from the opener.  This will also
943868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // modify the title of the document to give us something to listen for.
9444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  base::string16 expected_title = ASCIIToUTF16("Modified Title");
9454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  TitleWatcher title_watcher(contents, expected_title);
946868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  success = false;
947868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
948868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      orig_contents,
949868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "window.domAutomationController.send(modifyNewWindow());",
950868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      &success));
951868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(success);
9524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle());
953868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
954868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // At this point, we should no longer be showing the destination URL.
955868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // The visible entry should be null, resulting in about:blank in the address
956868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // bar.
957868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(contents->GetController().GetVisibleEntry());
958868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
959868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
960868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Test for crbug.com/9682.  We should not show the URL for a pending renderer-
961868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// initiated navigation in a new tab if it is not the initial navigation.  In
962868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// this case, the renderer will not notify us of a modification, so we cannot
963868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// show the pending URL without allowing a spoof.
964f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
965868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                       DontShowLoadingURLIfNotInitialNav) {
966868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(test_server()->Start());
967868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
968868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Load a page that can open a URL that won't commit in a new window.
969868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NavigateToURL(
970868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      shell(), test_server()->GetURL("files/click-nocontent-link.html"));
971868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  WebContents* orig_contents = shell()->web_contents();
972868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
973868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Click a /nocontent link that opens in a new window but never commits.
974868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // By using an onclick handler that first creates the window, the slow
975868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // navigation is not considered an initial navigation.
976868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
977868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool success = false;
978868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
979868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      orig_contents,
980868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "window.domAutomationController.send("
981868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "clickNoContentScriptedTargetedLink());",
982868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      &success));
983868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(success);
984868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
985868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Wait for the window to open.
986868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
987868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
988868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Ensure the destination URL is not visible, because it is not the initial
989868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // navigation.
990868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  WebContents* contents = new_shell->web_contents();
991868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(contents->GetController().IsInitialNavigation());
992868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(contents->GetController().GetVisibleEntry());
993868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
994868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
995effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Crashes under ThreadSanitizer, http://crbug.com/356758.
996effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#if defined(THREAD_SANITIZER)
997effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define MAYBE_BackForwardNotStale DISABLED_BackForwardNotStale
998effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#else
999effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define MAYBE_BackForwardNotStale BackForwardNotStale
1000effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#endif
10015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for http://crbug.com/93427.  Ensure that cross-site navigations
10025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// do not cause back/forward navigations to be considered stale by the
10035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// renderer.
1004effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochIN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, MAYBE_BackForwardNotStale) {
10055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
1006f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  NavigateToURL(shell(), GURL(url::kAboutBlankURL));
10075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Visit a page on first site.
10095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL("files/title1.html"));
10105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Visit three pages on second site.
10125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title1.html"));
10135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title2.html"));
10145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title3.html"));
10155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // History is now [blank, A1, B1, B2, *B3].
10175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContents* contents = shell()->web_contents();
10185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(5, contents->GetController().GetEntryCount());
10195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Open another window in same process to keep this process alive.
10215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = CreateBrowser();
10225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/title1.html"));
10235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Go back three times to first site.
10255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1026868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(shell()->web_contents());
10275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoBack();
10285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    back_nav_load_observer.Wait();
10295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1031868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(shell()->web_contents());
10325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoBack();
10335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    back_nav_load_observer.Wait();
10345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1036868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(shell()->web_contents());
10375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoBack();
10385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    back_nav_load_observer.Wait();
10395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now go forward twice to B2.  Shouldn't be left spinning.
10425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1043868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver forward_nav_load_observer(shell()->web_contents());
10445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoForward();
10455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    forward_nav_load_observer.Wait();
10465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1048868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver forward_nav_load_observer(shell()->web_contents());
10495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoForward();
10505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    forward_nav_load_observer.Wait();
10515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Go back twice to first site.
10545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1055868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(shell()->web_contents());
10565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoBack();
10575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    back_nav_load_observer.Wait();
10585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1060868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(shell()->web_contents());
10615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoBack();
10625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    back_nav_load_observer.Wait();
10635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now go forward directly to B3.  Shouldn't be left spinning.
10665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1067868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver forward_nav_load_observer(shell()->web_contents());
10685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell()->web_contents()->GetController().GoToIndex(4);
10695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    forward_nav_load_observer.Wait();
10705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for http://crbug.com/130016.
10745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Swapping out a render view should update its visiblity state.
1075f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
10765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       SwappedOutViewHasCorrectVisibilityState) {
10775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
10785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a page with links that open in a new window.
10805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string replacement_path;
10815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
10825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "files/click-noreferrer-links.html",
10835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      foo_host_port_,
10845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &replacement_path));
10855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), test_server()->GetURL(replacement_path));
10865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Open a same-site link in a new widnow.
10885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
10895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
10902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
10912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
10922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(clickSameSiteTargetedLink());",
10935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
10945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
10955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
10965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new tab to finish, if it hasn't.
10985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
10995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
11003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
11015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RenderViewHost* rvh = new_shell->web_contents()->GetRenderViewHost();
11032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
11052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      rvh,
11062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send("
1107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "    document.visibilityState == 'visible');",
11085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
11095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
11105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now navigate the new window to a different site. This should swap out the
11125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // tab's existing RenderView, causing it become hidden.
11135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/title1.html"));
11145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
11162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      rvh,
11172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send("
1118f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "    document.visibilityState == 'hidden');",
11195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
11205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
11215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Going back should make the previously swapped-out view to become visible
11235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // again.
11245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
1125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    TestNavigationObserver back_nav_load_observer(new_shell->web_contents());
11265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_shell->web_contents()->GetController().GoBack();
11275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    back_nav_load_observer.Wait();
11285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
11295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("/files/navigate_opener.html",
11313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
11325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(rvh, new_shell->web_contents()->GetRenderViewHost());
11345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
11362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      rvh,
11372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send("
1138f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      "    document.visibilityState == 'visible');",
11395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
11405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
11415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// This class ensures that all the given RenderViewHosts have properly been
11441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// shutdown.
11451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class RenderViewHostDestructionObserver : public WebContentsObserver {
11465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
11471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  explicit RenderViewHostDestructionObserver(WebContents* web_contents)
11481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      : WebContentsObserver(web_contents) {}
11491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual ~RenderViewHostDestructionObserver() {}
11501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void EnsureRVHGetsDestructed(RenderViewHost* rvh) {
11511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    watched_render_view_hosts_.insert(rvh);
11525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
11531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  size_t GetNumberOfWatchedRenderViewHosts() const {
11541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return watched_render_view_hosts_.size();
11555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1156a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
11575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
11581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // WebContentsObserver implementation:
11591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void RenderViewDeleted(RenderViewHost* rvh) OVERRIDE {
11601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    watched_render_view_hosts_.erase(rvh);
11615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
11625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  std::set<RenderViewHost*> watched_render_view_hosts_;
11645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
11655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1166effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Crashes under ThreadSanitizer, http://crbug.com/356758.
1167effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#if defined(THREAD_SANITIZER)
1168effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define MAYBE_LeakingRenderViewHosts DISABLED_LeakingRenderViewHosts
1169effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#else
1170effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define MAYBE_LeakingRenderViewHosts LeakingRenderViewHosts
1171effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#endif
11725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/90867. Make sure we don't leak render view hosts since
11735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// they may cause crashes or memory corruptions when trying to call dead
11745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// delegate_. This test also verifies crbug.com/117420 and crbug.com/143255 to
11755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ensure that a separate SiteInstance is created when navigating to view-source
11765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// URLs, regardless of current URL.
1177effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochIN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
1178effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                       MAYBE_LeakingRenderViewHosts) {
11795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
11805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Observe the created render_view_host's to make sure they will not leak.
11821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  RenderViewHostDestructionObserver rvh_observers(shell()->web_contents());
11835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL navigated_url(test_server()->GetURL("files/title2.html"));
1185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  GURL view_source_url(kViewSourceScheme + std::string(":") +
1186868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                       navigated_url.spec());
11875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Let's ensure that when we start with a blank window, navigating away to a
11895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // view-source URL, we create a new SiteInstance.
1190868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  RenderViewHost* blank_rvh = shell()->web_contents()->GetRenderViewHost();
11915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SiteInstance* blank_site_instance = blank_rvh->GetSiteInstance();
11923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  EXPECT_EQ(shell()->web_contents()->GetLastCommittedURL(), GURL::EmptyGURL());
11935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(blank_site_instance->GetSiteURL(), GURL::EmptyGURL());
11941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  rvh_observers.EnsureRVHGetsDestructed(blank_rvh);
11955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now navigate to the view-source URL and ensure we got a different
11975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SiteInstance and RenderViewHost.
11985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), view_source_url);
11995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(blank_rvh, shell()->web_contents()->GetRenderViewHost());
12005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(blank_site_instance, shell()->web_contents()->
12015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GetRenderViewHost()->GetSiteInstance());
12021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  rvh_observers.EnsureRVHGetsDestructed(
12031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      shell()->web_contents()->GetRenderViewHost());
12045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Load a random page and then navigate to view-source: of it.
12065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This used to cause two RVH instances for the same SiteInstance, which
12075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // was a problem.  This is no longer the case.
12085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), navigated_url);
12095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SiteInstance* site_instance1 = shell()->web_contents()->
12105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GetRenderViewHost()->GetSiteInstance();
12111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  rvh_observers.EnsureRVHGetsDestructed(
12121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      shell()->web_contents()->GetRenderViewHost());
12135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(), view_source_url);
12151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  rvh_observers.EnsureRVHGetsDestructed(
12161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      shell()->web_contents()->GetRenderViewHost());
12175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SiteInstance* site_instance2 = shell()->web_contents()->
12185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GetRenderViewHost()->GetSiteInstance();
12195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Ensure that view-source navigations force a new SiteInstance.
12215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(site_instance1, site_instance2);
12225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Now navigate to a different instance so that we swap out again.
12245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title2.html"));
12251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  rvh_observers.EnsureRVHGetsDestructed(
12261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      shell()->web_contents()->GetRenderViewHost());
12275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This used to leak a render view host.
12295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  shell()->Close();
12305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RunAllPendingInMessageLoop();  // Needed on ChromeOS.
12325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_EQ(0U, rvh_observers.GetNumberOfWatchedRenderViewHosts());
12345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
12355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test for crbug.com/143155.  Frame tree updates during unload should not
12375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// interrupt the intended navigation and show swappedout:// instead.
12385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Specifically:
12395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 1) Open 2 tabs in an HTTP SiteInstance, with a subframe in the opener.
12405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 2) Send the second tab to a different foo.com SiteInstance.
12415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//    This creates a swapped out opener for the first tab in the foo process.
12425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 3) Navigate the first tab to the foo.com SiteInstance, and have the first
12435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    tab's unload handler remove its frame.
12445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This used to cause an update to the frame tree of the swapped out RV,
12455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// just as it was navigating to a real page.  That pre-empted the real
12465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// navigation and visibly sent the tab to swappedout://.
1247f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
12485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       DontPreemptNavigationWithFrameTreeUpdate) {
12495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StartServer();
12505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 1. Load a page that deletes its iframe during unload.
12525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURL(shell(),
12535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                test_server()->GetURL("files/remove_frame_on_unload.html"));
12545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the original SiteInstance for later comparison.
12565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> orig_site_instance(
12575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shell()->web_contents()->GetSiteInstance());
12585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Open a same-site page in a new window.
12605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ShellAddedObserver new_shell_observer;
12615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = false;
12622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(ExecuteScriptAndExtractBool(
12632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      shell()->web_contents(),
12642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "window.domAutomationController.send(openWindow());",
12655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &success));
12665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(success);
12675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* new_shell = new_shell_observer.GetShell();
12685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Wait for the navigation in the new window to finish, if it hasn't.
12705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(new_shell->web_contents());
12715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("/files/title1.html",
12723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            new_shell->web_contents()->GetLastCommittedURL().path());
12735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should have the same SiteInstance.
12751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_EQ(orig_site_instance.get(),
12761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            new_shell->web_contents()->GetSiteInstance());
12775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 2. Send the second tab to a different process.
12795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(new_shell, GetCrossSiteURL("files/title1.html"));
12805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<SiteInstance> new_site_instance(
12815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_shell->web_contents()->GetSiteInstance());
12825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_NE(orig_site_instance, new_site_instance);
12835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 3. Send the first tab to the second tab's process.
12855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GetCrossSiteURL("files/title1.html"));
12865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make sure it ends up at the right page.
12885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(shell()->web_contents());
12895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(GetCrossSiteURL("files/title1.html"),
12903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            shell()->web_contents()->GetLastCommittedURL());
12911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_EQ(new_site_instance.get(),
12921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            shell()->web_contents()->GetSiteInstance());
12935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
12945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Ensure that renderer-side debug URLs do not cause a process swap, since they
12965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// are meant to run in the current page.  We had a bug where we expected a
12975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// BrowsingInstance swap to occur on pages like view-source and extensions,
12985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// which broke chrome://crash and javascript: URLs.
12995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// See http://crbug.com/335503.
13005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, RendererDebugURLsDontSwap) {
13015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(test_server()->Start());
13025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL original_url(test_server()->GetURL("files/title2.html"));
13045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL view_source_url(kViewSourceScheme + std::string(":") +
13055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       original_url.spec());
13065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), view_source_url);
13085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Check that javascript: URLs work.
13105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 expected_title = ASCIIToUTF16("msg");
13115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TitleWatcher title_watcher(shell()->web_contents(), expected_title);
13125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  shell()->LoadURL(GURL("javascript:document.title='msg'"));
13135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle());
13145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Crash the renderer of the view-source page.
13165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderProcessHostWatcher crash_observer(
13175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      shell()->web_contents(),
13185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
13195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GURL(kChromeUICrashURL));
13205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  crash_observer.Wait();
13215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
13225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Ensure that renderer-side debug URLs don't take effect on crashed renderers.
13245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Otherwise, we might try to load an unprivileged about:blank page into a
13255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// WebUI-enabled RenderProcessHost, failing a safety check in InitRenderView.
13265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// See http://crbug.com/334214.
13275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest,
13285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       IgnoreRendererDebugURLsWhenCrashed) {
13295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Visit a WebUI page with bindings.
13305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL webui_url = GURL(std::string(kChromeUIScheme) + "://" +
13315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        std::string(kChromeUIGpuHost));
13325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), webui_url);
13335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
13345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                  shell()->web_contents()->GetRenderProcessHost()->GetID()));
13355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Crash the renderer of the WebUI page.
13375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderProcessHostWatcher crash_observer(
13385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      shell()->web_contents(),
13395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
13405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), GURL(kChromeUICrashURL));
13415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  crash_observer.Wait();
13425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Load the crash URL again but don't wait for any action.  If it is not
13445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // ignored this time, we will fail the WebUI CHECK in InitRenderView.
13455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  shell()->LoadURL(GURL(kChromeUICrashURL));
13465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Ensure that such URLs can still work as the initial navigation of a tab.
13485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // We postpone the initial navigation of the tab using an empty GURL, so that
13495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // we can add a watcher for crashes.
13505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Shell* shell2 = Shell::CreateNewWindow(
13515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      shell()->web_contents()->GetBrowserContext(), GURL(), NULL,
13525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      MSG_ROUTING_NONE, gfx::Size());
13535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderProcessHostWatcher crash_observer2(
13545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      shell2->web_contents(),
13555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
13565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell2, GURL(kChromeUIKillURL));
13575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  crash_observer2.Wait();
13585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
13595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Ensure that pending_and_current_web_ui_ is cleared when a URL commits.
13615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Otherwise it might get picked up by InitRenderView when granting bindings
13625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// to other RenderViewHosts.  See http://crbug.com/330811.
13635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, ClearPendingWebUIOnCommit) {
13645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Visit a WebUI page with bindings.
13655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL webui_url(GURL(std::string(kChromeUIScheme) + "://" +
13665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                      std::string(kChromeUIGpuHost)));
13675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), webui_url);
13685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
13695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                  shell()->web_contents()->GetRenderProcessHost()->GetID()));
13705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
13715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      shell()->web_contents());
13725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WebUIImpl* webui = web_contents->GetRenderManagerForTesting()->web_ui();
13735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(webui);
13745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(web_contents->GetRenderManagerForTesting()->pending_web_ui());
13755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Navigate to another WebUI URL that reuses the WebUI object.  Make sure we
13775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // clear pending_web_ui() when it commits.
13785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL webui_url2(webui_url.spec() + "#foo");
13795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NavigateToURL(shell(), webui_url2);
13805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(webui, web_contents->GetRenderManagerForTesting()->web_ui());
13815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(web_contents->GetRenderManagerForTesting()->pending_web_ui());
13825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
13835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1384a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class RFHMProcessPerTabTest : public RenderFrameHostManagerTest {
1385a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
1386a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  RFHMProcessPerTabTest() {}
1387a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
1389a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    command_line->AppendSwitch(switches::kProcessPerTab);
1390a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
1391a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
1392a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1393a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Test that we still swap processes for BrowsingInstance changes even in
1394a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// --process-per-tab mode.  See http://crbug.com/343017.
1395a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Disabled on Android: http://crbug.com/345873.
1396effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Crashes under ThreadSanitizer, http://crbug.com/356758.
1397effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#if defined(OS_ANDROID) || defined(THREAD_SANITIZER)
1398a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define MAYBE_BackFromWebUI DISABLED_BackFromWebUI
1399a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#else
1400a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define MAYBE_BackFromWebUI BackFromWebUI
1401a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
1402a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(RFHMProcessPerTabTest, MAYBE_BackFromWebUI) {
1403a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(test_server()->Start());
1404a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL original_url(test_server()->GetURL("files/title2.html"));
1405a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  NavigateToURL(shell(), original_url);
1406a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1407a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Visit a WebUI page with bindings.
1408a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL webui_url(GURL(std::string(kChromeUIScheme) + "://" +
1409a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                      std::string(kChromeUIGpuHost)));
1410a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  NavigateToURL(shell(), webui_url);
1411a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
1412a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  shell()->web_contents()->GetRenderProcessHost()->GetID()));
1413a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1414a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Go back and ensure we have no WebUI bindings.
1415a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TestNavigationObserver back_nav_load_observer(shell()->web_contents());
1416a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  shell()->web_contents()->GetController().GoBack();
1417a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  back_nav_load_observer.Wait();
1418a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(original_url, shell()->web_contents()->GetLastCommittedURL());
1419a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
1420a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  shell()->web_contents()->GetRenderProcessHost()->GetID()));
1421a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
1422a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1423cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// crbug.com/372360
1424cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// The test loads url1, opens a link pointing to url2 in a new tab, and
1425cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// navigates the new tab to url1.
1426cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// The following is needed for the bug to happen:
1427cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//  - url1 must require webui bindings;
1428cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//  - navigating to url2 in the site instance of url1 should not swap
1429cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//   browsing instances, but should require a new site instance.
1430cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(RenderFrameHostManagerTest, WebUIGetsBindings) {
1431cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  GURL url1(std::string(kChromeUIScheme) + "://" +
1432cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            std::string(kChromeUIGpuHost));
1433cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  GURL url2(std::string(kChromeUIScheme) + "://" +
1434cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            std::string(kChromeUIAccessibilityHost));
1435cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1436cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Visit a WebUI page with bindings.
1437cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NavigateToURL(shell(), url1);
1438cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
1439cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                  shell()->web_contents()->GetRenderProcessHost()->GetID()));
1440cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SiteInstance* site_instance1 = shell()->web_contents()->GetSiteInstance();
1441cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1442cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Open a new tab. Initially it gets a render view in the original tab's
1443cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // current site instance.
1444cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestNavigationObserver nav_observer(NULL);
1445cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  nav_observer.StartWatchingNewWebContents();
1446cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ShellAddedObserver shao;
1447cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  OpenUrlViaClickTarget(shell()->web_contents(), url2);
1448cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  nav_observer.Wait();
1449cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  Shell* new_shell = shao.GetShell();
1450cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  WebContentsImpl* new_web_contents = static_cast<WebContentsImpl*>(
1451cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      new_shell->web_contents());
1452cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SiteInstance* site_instance2 = new_web_contents->GetSiteInstance();
1453cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1454cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_NE(site_instance2, site_instance1);
1455cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(site_instance2->IsRelatedSiteInstance(site_instance1));
1456cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  RenderViewHost* initial_rvh = new_web_contents->
1457cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      GetRenderManagerForTesting()->GetSwappedOutRenderViewHost(site_instance1);
1458cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_TRUE(initial_rvh);
1459cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The following condition is what was causing the bug.
1460cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_EQ(0, initial_rvh->GetEnabledBindings());
1461cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1462cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Navigate to url1 and check bindings.
1463cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NavigateToURL(new_shell, url1);
1464cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The navigation should have used the first SiteInstance, otherwise
1465cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // |initial_rvh| did not have a chance to be used.
1466cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_EQ(new_web_contents->GetSiteInstance(), site_instance1);
1467cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_EQ(BINDINGS_POLICY_WEB_UI,
1468cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      new_web_contents->GetRenderViewHost()->GetEnabledBindings());
1469cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
1470cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
14715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
1472