1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/run_loop.h"
#include "build/build_config.h"
#include "content/browser/devtools/render_frame_devtools_agent_host.h"
#include "content/browser/renderer_host/frame_tree.h"
#include "content/browser/site_per_process_browsertest.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/download_manager.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/shell/browser/shell_download_manager_delegate.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "content/test/render_document_feature.h"
#include "net/dns/mock_host_resolver.h"
namespace content {
class SitePerProcessDevToolsBrowserTest : public SitePerProcessBrowserTest {
public:
SitePerProcessDevToolsBrowserTest() {}
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
SitePerProcessBrowserTest::SetUpOnMainThread();
}
};
class TestClient: public DevToolsAgentHostClient {
public:
TestClient() : closed_(false), waiting_for_reply_(false) {}
~TestClient() override {}
bool closed() { return closed_; }
void DispatchProtocolMessage(DevToolsAgentHost* agent_host,
base::span<const uint8_t> message) override {
if (waiting_for_reply_) {
waiting_for_reply_ = false;
std::move(quit_closure_).Run();
}
}
void AgentHostClosed(DevToolsAgentHost* agent_host) override {
closed_ = true;
}
void WaitForReply() {
waiting_for_reply_ = true;
base::RunLoop loop;
quit_closure_ = loop.QuitClosure();
loop.Run();
}
private:
bool closed_;
bool waiting_for_reply_;
base::OnceClosure quit_closure_;
};
DevToolsAgentHost::List ExtractPageOrFrameTargets(
DevToolsAgentHost::List list) {
DevToolsAgentHost::List result;
for (auto& entry : list) {
if (entry->GetType() == DevToolsAgentHost::kTypePage ||
entry->GetType() == DevToolsAgentHost::kTypeFrame) {
result.push_back(std::move(entry));
}
}
return result;
}
// Fails on Android, http://crbug.com/464993.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_CrossSiteIframeAgentHost DISABLED_CrossSiteIframeAgentHost
#else
#define MAYBE_CrossSiteIframeAgentHost CrossSiteIframeAgentHost
#endif
IN_PROC_BROWSER_TEST_P(SitePerProcessDevToolsBrowserTest,
MAYBE_CrossSiteIframeAgentHost) {
DevToolsAgentHost::List list;
GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html"));
EXPECT_TRUE(NavigateToURL(shell(), main_url));
// It is safe to obtain the root frame tree node here, as it doesn't change.
FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents())
->GetPrimaryFrameTree()
.root();
list = ExtractPageOrFrameTargets(DevToolsAgentHost::GetOrCreateAll());
EXPECT_EQ(1U, list.size());
EXPECT_EQ(DevToolsAgentHost::kTypePage, list[0]->GetType());
EXPECT_EQ(main_url.spec(), list[0]->GetURL().spec());
// Load same-site page into iframe.
FrameTreeNode* child = root->child_at(0);
GURL http_url(embedded_test_server()->GetURL("/title1.html"));
EXPECT_TRUE(NavigateToURLFromRenderer(child, http_url));
list = ExtractPageOrFrameTargets(DevToolsAgentHost::GetOrCreateAll());
EXPECT_EQ(1U, list.size());
EXPECT_EQ(DevToolsAgentHost::kTypePage, list[0]->GetType());
EXPECT_EQ(main_url.spec(), list[0]->GetURL().spec());
// Load cross-site page into iframe.
GURL::Replacements replace_host;
GURL cross_site_url(embedded_test_server()->GetURL("/title2.html"));
replace_host.SetHostStr("foo.com");
cross_site_url = cross_site_url.ReplaceComponents(replace_host);
EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), cross_site_url));
list = ExtractPageOrFrameTargets(DevToolsAgentHost::GetOrCreateAll());
EXPECT_EQ(2U, list.size());
EXPECT_EQ(DevToolsAgentHost::kTypePage, list[0]->GetType());
EXPECT_EQ(main_url.spec(), list[0]->GetURL().spec());
EXPECT_EQ(DevToolsAgentHost::kTypeFrame, list[1]->GetType());
EXPECT_EQ(cross_site_url.spec(), list[1]->GetURL().spec());
EXPECT_EQ(std::string(), list[0]->GetParentId());
EXPECT_EQ(list[0]->GetId(), list[1]->GetParentId());
EXPECT_NE(list[1]->GetId(), list[0]->GetId());
// Attaching to both agent hosts.
scoped_refptr<DevToolsAgentHost> child_host = list[1];
TestClient child_client;
child_host->AttachClient(&child_client);
scoped_refptr<DevToolsAgentHost> parent_host = list[0];
TestClient parent_client;
parent_host->AttachClient(&parent_client);
// Send message to parent and child frames and get result back.
constexpr char kMsg[] = R"({"id":0,"method":"incorrect.method"})";
auto message = base::byte_span_from_cstring(kMsg);
child_host->DispatchProtocolMessage(&child_client, message);
child_client.WaitForReply();
parent_host->DispatchProtocolMessage(&parent_client, message);
parent_client.WaitForReply();
// Load back same-site page into iframe.
EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), http_url));
list = ExtractPageOrFrameTargets(DevToolsAgentHost::GetOrCreateAll());
EXPECT_EQ(1U, list.size());
EXPECT_EQ(DevToolsAgentHost::kTypePage, list[0]->GetType());
EXPECT_EQ(main_url.spec(), list[0]->GetURL().spec());
EXPECT_TRUE(child_client.closed());
child_host->DetachClient(&child_client);
child_host = nullptr;
EXPECT_FALSE(parent_client.closed());
parent_host->DetachClient(&parent_client);
parent_host = nullptr;
}
IN_PROC_BROWSER_TEST_P(SitePerProcessDevToolsBrowserTest, AgentHostForFrames) {
GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html"));
EXPECT_TRUE(NavigateToURL(shell(), main_url));
scoped_refptr<DevToolsAgentHost> page_agent =
DevToolsAgentHost::GetOrCreateFor(shell()->web_contents());
// It is safe to obtain the root frame tree node here, as it doesn't change.
FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents())
->GetPrimaryFrameTree()
.root();
scoped_refptr<DevToolsAgentHost> main_frame_agent =
RenderFrameDevToolsAgentHost::GetOrCreateFor(root);
EXPECT_EQ(page_agent.get(), main_frame_agent.get());
// Load same-site page into iframe.
FrameTreeNode* child = root->child_at(0);
GURL http_url(embedded_test_server()->GetURL("/title1.html"));
EXPECT_TRUE(NavigateToURLFromRenderer(child, http_url));
scoped_refptr<DevToolsAgentHost> child_frame_agent =
RenderFrameDevToolsAgentHost::GetOrCreateFor(child);
EXPECT_EQ(page_agent.get(), child_frame_agent.get());
// Load cross-site page into iframe.
GURL::Replacements replace_host;
GURL cross_site_url(embedded_test_server()->GetURL("/title2.html"));
replace_host.SetHostStr("foo.com");
cross_site_url = cross_site_url.ReplaceComponents(replace_host);
EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), cross_site_url));
child_frame_agent = RenderFrameDevToolsAgentHost::GetOrCreateFor(child);
EXPECT_NE(page_agent.get(), child_frame_agent.get());
EXPECT_EQ(child_frame_agent->GetParentId(), page_agent->GetId());
EXPECT_NE(child_frame_agent->GetId(), page_agent->GetId());
}
IN_PROC_BROWSER_TEST_P(SitePerProcessDevToolsBrowserTest,
AgentHostForPageEqualsOneForMainFrame) {
GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html"));
EXPECT_TRUE(NavigateToURL(shell(), main_url));
// It is safe to obtain the root frame tree node here, as it doesn't change.
FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents())
->GetPrimaryFrameTree()
.root();
FrameTreeNode* child = root->child_at(0);
// Load cross-site page into iframe.
GURL::Replacements replace_host;
GURL cross_site_url(embedded_test_server()->GetURL("/title2.html"));
replace_host.SetHostStr("foo.com");
cross_site_url = cross_site_url.ReplaceComponents(replace_host);
EXPECT_TRUE(NavigateToURLFromRenderer(child, cross_site_url));
// First ask for child frame, then for main frame.
scoped_refptr<DevToolsAgentHost> child_frame_agent =
RenderFrameDevToolsAgentHost::GetOrCreateFor(child);
scoped_refptr<DevToolsAgentHost> main_frame_agent =
RenderFrameDevToolsAgentHost::GetOrCreateFor(root);
EXPECT_NE(main_frame_agent.get(), child_frame_agent.get());
EXPECT_EQ(child_frame_agent->GetParentId(), main_frame_agent->GetId());
EXPECT_NE(child_frame_agent->GetId(), main_frame_agent->GetId());
// Agent for web contents should be the the main frame's one.
scoped_refptr<DevToolsAgentHost> page_agent =
DevToolsAgentHost::GetOrCreateFor(shell()->web_contents());
EXPECT_EQ(page_agent.get(), main_frame_agent.get());
}
class SitePerProcessDownloadDevToolsBrowserTest
: public SitePerProcessBrowserTest {
public:
SitePerProcessDownloadDevToolsBrowserTest() {}
void SetUpOnMainThread() override {
SitePerProcessBrowserTest::SetUpOnMainThread();
ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir());
DownloadManager* download_manager =
shell()->web_contents()->GetBrowserContext()->GetDownloadManager();
ShellDownloadManagerDelegate* download_delegate =
static_cast<ShellDownloadManagerDelegate*>(
download_manager->GetDelegate());
download_delegate->SetDownloadBehaviorForTesting(
downloads_directory_.GetPath());
}
base::ScopedTempDir downloads_directory_;
};
IN_PROC_BROWSER_TEST_P(SitePerProcessDownloadDevToolsBrowserTest,
NotCommittedNavigationDoesNotBlockAgent) {
ASSERT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")));
scoped_refptr<DevToolsAgentHost> agent =
DevToolsAgentHost::GetOrCreateFor(shell()->web_contents());
TestClient client;
agent->AttachClient(&client);
constexpr char kMsg[] = R"({"id":0,"method":"incorrect.method"})";
auto message = base::byte_span_from_cstring(kMsg);
// Check that client is responsive.
agent->DispatchProtocolMessage(&client, message);
client.WaitForReply();
// Do cross process navigation that ends up being download, which will be
// not committed navigation in that web contents/render frame.
GURL::Replacements replace_host;
GURL cross_site_url(embedded_test_server()->GetURL("/download/empty.bin"));
replace_host.SetHostStr("foo.com");
cross_site_url = cross_site_url.ReplaceComponents(replace_host);
ASSERT_TRUE(NavigateToURLAndExpectNoCommit(shell(), cross_site_url));
// Check that client is still responding after not committed navigation
// is finished.
agent->DispatchProtocolMessage(&client, message);
client.WaitForReply();
ASSERT_TRUE(agent->DetachClient(&client));
}
INSTANTIATE_TEST_SUITE_P(All,
SitePerProcessDevToolsBrowserTest,
testing::ValuesIn(RenderDocumentFeatureLevelValues()));
INSTANTIATE_TEST_SUITE_P(All,
SitePerProcessDownloadDevToolsBrowserTest,
testing::ValuesIn(RenderDocumentFeatureLevelValues()));
} // namespace content
|