blob: f24814143326186cf83cf798ab70875c318a4bfd [file] [log] [blame]
[email protected]9b159a52013-10-03 17:24:551// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d4a8ca482013-10-30 21:06:405#include "content/browser/frame_host/frame_tree.h"
[email protected]9b159a52013-10-03 17:24:556
7#include "base/run_loop.h"
[email protected]7cc7ebd2013-10-08 00:59:008#include "base/strings/string_number_conversions.h"
[email protected]6ea6bdf2013-12-06 13:35:019#include "content/browser/frame_host/navigator_impl.h"
[email protected]2a18ee222013-11-21 07:52:4410#include "content/browser/frame_host/render_frame_host_factory.h"
[email protected]d4a8ca482013-10-30 21:06:4011#include "content/browser/frame_host/render_frame_host_impl.h"
[email protected]9b159a52013-10-03 17:24:5512#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]94d0cc12013-12-18 00:07:4113#include "content/browser/web_contents/web_contents_impl.h"
naskob985af12015-02-06 04:15:3314#include "content/common/frame_messages.h"
[email protected]14266072014-04-19 00:35:2015#include "content/public/browser/web_contents_observer.h"
[email protected]9b159a52013-10-03 17:24:5516#include "content/public/test/mock_render_process_host.h"
17#include "content/public/test/test_browser_context.h"
18#include "content/public/test/test_browser_thread_bundle.h"
[email protected]6b50e362014-08-15 05:15:5919#include "content/test/test_render_frame_host.h"
[email protected]14266072014-04-19 00:35:2020#include "content/test/test_render_view_host.h"
21#include "content/test/test_web_contents.h"
[email protected]9b159a52013-10-03 17:24:5522#include "testing/gtest/include/gtest/gtest.h"
23
24namespace content {
dgroganfb22f9a2014-10-20 21:32:3225
[email protected]9b159a52013-10-03 17:24:5526namespace {
27
[email protected]14266072014-04-19 00:35:2028// Appends a description of the structure of the frame tree to |result|.
29void AppendTreeNodeState(FrameTreeNode* node, std::string* result) {
30 result->append(
31 base::Int64ToString(node->current_frame_host()->GetRoutingID()));
schenney6408fed22015-04-17 17:44:5732 if (!node->current_frame_host()->IsRenderFrameLive())
33 result->append("*"); // Asterisk next to dead frames.
34
[email protected]14266072014-04-19 00:35:2035 if (!node->frame_name().empty()) {
36 result->append(" '");
37 result->append(node->frame_name());
38 result->append("'");
39 }
40 result->append(": [");
41 const char* separator = "";
42 for (size_t i = 0; i < node->child_count(); i++) {
43 result->append(separator);
44 AppendTreeNodeState(node->child_at(i), result);
45 separator = ", ";
46 }
47 result->append("]");
48}
49
50// Logs calls to WebContentsObserver along with the state of the frame tree,
51// for later use in EXPECT_EQ().
52class TreeWalkingWebContentsLogger : public WebContentsObserver {
53 public:
54 explicit TreeWalkingWebContentsLogger(WebContents* web_contents)
55 : WebContentsObserver(web_contents) {}
56
dchengc2282aa2014-10-21 12:07:5857 ~TreeWalkingWebContentsLogger() override {
[email protected]14266072014-04-19 00:35:2058 EXPECT_EQ("", log_) << "Activity logged that was not expected";
59 }
60
61 // Gets and resets the log, which is a string of what happened.
62 std::string GetLog() {
63 std::string result = log_;
64 log_.clear();
65 return result;
66 }
67
68 // content::WebContentsObserver implementation.
dchengc2282aa2014-10-21 12:07:5869 void RenderFrameCreated(RenderFrameHost* render_frame_host) override {
[email protected]14266072014-04-19 00:35:2070 LogWhatHappened("RenderFrameCreated", render_frame_host);
71 }
72
dchengc2282aa2014-10-21 12:07:5873 void RenderFrameHostChanged(RenderFrameHost* old_host,
74 RenderFrameHost* new_host) override {
[email protected]02d7b6e2014-06-24 21:01:5075 if (old_host)
naskof5940b9f2015-03-02 23:04:0576 LogWhatHappened("RenderFrameHostChanged(old)", old_host);
77 LogWhatHappened("RenderFrameHostChanged(new)", new_host);
[email protected]02d7b6e2014-06-24 21:01:5078 }
79
dchengc2282aa2014-10-21 12:07:5880 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override {
[email protected]14266072014-04-19 00:35:2081 LogWhatHappened("RenderFrameDeleted", render_frame_host);
82 }
83
dchengc2282aa2014-10-21 12:07:5884 void RenderProcessGone(base::TerminationStatus status) override {
[email protected]14266072014-04-19 00:35:2085 LogWhatHappened("RenderProcessGone");
86 }
87
88 private:
89 void LogWhatHappened(const std::string& event_name) {
90 if (!log_.empty()) {
91 log_.append("\n");
92 }
93 log_.append(event_name + " -> ");
94 AppendTreeNodeState(
95 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree()->root(),
96 &log_);
97 }
98
99 void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) {
100 LogWhatHappened(
101 base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID()));
102 }
103
104 std::string log_;
105
106 DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger);
107};
108
dgroganfb22f9a2014-10-20 21:32:32109} // namespace
110
[email protected]14266072014-04-19 00:35:20111class FrameTreeTest : public RenderViewHostImplTestHarness {
[email protected]7cc7ebd2013-10-08 00:59:00112 protected:
113 // Prints a FrameTree, for easy assertions of the tree hierarchy.
114 std::string GetTreeState(FrameTree* frame_tree) {
115 std::string result;
[email protected]fa944cb82013-11-15 17:51:21116 AppendTreeNodeState(frame_tree->root(), &result);
[email protected]7cc7ebd2013-10-08 00:59:00117 return result;
118 }
[email protected]9b159a52013-10-03 17:24:55119};
120
[email protected]9b159a52013-10-03 17:24:55121// Exercise tree manipulation routines.
122// - Add a series of nodes and verify tree structure.
123// - Remove a series of nodes and verify tree structure.
nickf9acfbe2014-12-23 19:12:37124//
125// TODO(nick): http://crbug.com/444722 Disabled temporarily because of a bad
126// interaction with the WebContentsObserverConsistencyChecker -- calling
127// AddFrame directly causes the RFH to not be announced. We either need to
128// rewrite this test, or be consistent in the layer at which we announce render
129// frame creation.
130TEST_F(FrameTreeTest, DISABLED_Shape) {
[email protected]94d0cc12013-12-18 00:07:41131 // Use the FrameTree of the WebContents so that it has all the delegates it
132 // needs. We may want to consider a test version of this.
[email protected]14266072014-04-19 00:35:20133 FrameTree* frame_tree = contents()->GetFrameTree();
[email protected]58faf942014-02-20 21:03:58134 FrameTreeNode* root = frame_tree->root();
[email protected]190b8c52013-11-09 01:35:44135
[email protected]9b159a52013-10-03 17:24:55136 std::string no_children_node("no children node");
137 std::string deep_subtree("node with deep subtree");
dgroganfb22f9a2014-10-20 21:32:32138 int process_id = root->current_frame_host()->GetProcess()->GetID();
[email protected]9b159a52013-10-03 17:24:55139
[email protected]58faf942014-02-20 21:03:58140 ASSERT_EQ("1: []", GetTreeState(frame_tree));
[email protected]7cc7ebd2013-10-08 00:59:00141
[email protected]9b159a52013-10-03 17:24:55142 // Simulate attaching a series of frames to build the frame tree.
dgroganfb22f9a2014-10-20 21:32:32143 frame_tree->AddFrame(root, process_id, 14, std::string());
144 frame_tree->AddFrame(root, process_id, 15, std::string());
145 frame_tree->AddFrame(root, process_id, 16, std::string());
[email protected]9b159a52013-10-03 17:24:55146
dgroganfb22f9a2014-10-20 21:32:32147 frame_tree->AddFrame(root->child_at(0), process_id, 244, std::string());
148 frame_tree->AddFrame(root->child_at(1), process_id, 255, no_children_node);
149 frame_tree->AddFrame(root->child_at(0), process_id, 245, std::string());
[email protected]9b159a52013-10-03 17:24:55150
[email protected]58faf942014-02-20 21:03:58151 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00152 "15: [255 'no children node': []], "
153 "16: []]",
[email protected]94d0cc12013-12-18 00:07:41154 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55155
[email protected]58faf942014-02-20 21:03:58156 FrameTreeNode* child_16 = root->child_at(2);
dgroganfb22f9a2014-10-20 21:32:32157 frame_tree->AddFrame(child_16, process_id, 264, std::string());
158 frame_tree->AddFrame(child_16, process_id, 265, std::string());
159 frame_tree->AddFrame(child_16, process_id, 266, std::string());
160 frame_tree->AddFrame(child_16, process_id, 267, deep_subtree);
161 frame_tree->AddFrame(child_16, process_id, 268, std::string());
[email protected]9b159a52013-10-03 17:24:55162
[email protected]58faf942014-02-20 21:03:58163 FrameTreeNode* child_267 = child_16->child_at(3);
dgroganfb22f9a2014-10-20 21:32:32164 frame_tree->AddFrame(child_267, process_id, 365, std::string());
165 frame_tree->AddFrame(child_267->child_at(0), process_id, 455, std::string());
166 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), process_id, 555,
[email protected]58faf942014-02-20 21:03:58167 std::string());
dgroganfb22f9a2014-10-20 21:32:32168 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0),
169 process_id, 655, std::string());
[email protected]9b159a52013-10-03 17:24:55170
[email protected]7cc7ebd2013-10-08 00:59:00171 // Now that's it's fully built, verify the tree structure is as expected.
[email protected]58faf942014-02-20 21:03:58172 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00173 "15: [255 'no children node': []], "
174 "16: [264: [], 265: [], 266: [], "
175 "267 'node with deep subtree': "
176 "[365: [455: [555: [655: []]]]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41177 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55178
[email protected]58faf942014-02-20 21:03:58179 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0);
180 frame_tree->RemoveFrame(child_555);
181 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00182 "15: [255 'no children node': []], "
183 "16: [264: [], 265: [], 266: [], "
184 "267 'node with deep subtree': "
[email protected]58faf942014-02-20 21:03:58185 "[365: [455: []]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41186 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55187
[email protected]58faf942014-02-20 21:03:58188 frame_tree->RemoveFrame(child_16->child_at(1));
189 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00190 "15: [255 'no children node': []], "
191 "16: [264: [], 266: [], "
192 "267 'node with deep subtree': "
[email protected]58faf942014-02-20 21:03:58193 "[365: [455: []]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41194 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55195
[email protected]58faf942014-02-20 21:03:58196 frame_tree->RemoveFrame(root->child_at(1));
197 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00198 "16: [264: [], 266: [], "
199 "267 'node with deep subtree': "
[email protected]58faf942014-02-20 21:03:58200 "[365: [455: []]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41201 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55202}
203
[email protected]14266072014-04-19 00:35:20204// Do some simple manipulations of the frame tree, making sure that
205// WebContentsObservers see a consistent view of the tree as we go.
206TEST_F(FrameTreeTest, ObserverWalksTreeDuringFrameCreation) {
207 TreeWalkingWebContentsLogger activity(contents());
schenney6408fed22015-04-17 17:44:57208 contents()->NavigateAndCommit(GURL("http://www.google.com"));
209 EXPECT_EQ("", activity.GetLog());
210
[email protected]14266072014-04-19 00:35:20211 FrameTree* frame_tree = contents()->GetFrameTree();
212 FrameTreeNode* root = frame_tree->root();
213
[email protected]14266072014-04-19 00:35:20214 // Simulate attaching a series of frames to build the frame tree.
alexmose48b1df932015-01-16 01:34:17215 main_test_rfh()->OnCreateChildFrame(14, std::string(), SandboxFlags::NONE);
naskof5940b9f2015-03-02 23:04:05216 EXPECT_EQ(
217 "RenderFrameHostChanged(new)(14) -> 1: []\n"
218 "RenderFrameCreated(14) -> 1: [14: []]",
219 activity.GetLog());
alexmose48b1df932015-01-16 01:34:17220 main_test_rfh()->OnCreateChildFrame(18, std::string(), SandboxFlags::NONE);
naskof5940b9f2015-03-02 23:04:05221 EXPECT_EQ(
222 "RenderFrameHostChanged(new)(18) -> 1: [14: []]\n"
223 "RenderFrameCreated(18) -> 1: [14: [], 18: []]",
224 activity.GetLog());
[email protected]14266072014-04-19 00:35:20225 frame_tree->RemoveFrame(root->child_at(0));
226 EXPECT_EQ("RenderFrameDeleted(14) -> 1: [18: []]", activity.GetLog());
227 frame_tree->RemoveFrame(root->child_at(0));
228 EXPECT_EQ("RenderFrameDeleted(18) -> 1: []", activity.GetLog());
229}
230
231// Make sure that WebContentsObservers see a consistent view of the tree after
232// recovery from a render process crash.
233TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) {
234 TreeWalkingWebContentsLogger activity(contents());
schenney6408fed22015-04-17 17:44:57235 contents()->NavigateAndCommit(GURL("http://www.google.com"));
236 EXPECT_EQ("", activity.GetLog());
[email protected]14266072014-04-19 00:35:20237
alexmose48b1df932015-01-16 01:34:17238 main_test_rfh()->OnCreateChildFrame(22, std::string(), SandboxFlags::NONE);
naskof5940b9f2015-03-02 23:04:05239 EXPECT_EQ(
240 "RenderFrameHostChanged(new)(22) -> 1: []\n"
241 "RenderFrameCreated(22) -> 1: [22: []]",
242 activity.GetLog());
alexmose48b1df932015-01-16 01:34:17243 main_test_rfh()->OnCreateChildFrame(23, std::string(), SandboxFlags::NONE);
naskof5940b9f2015-03-02 23:04:05244 EXPECT_EQ(
245 "RenderFrameHostChanged(new)(23) -> 1: [22: []]\n"
246 "RenderFrameCreated(23) -> 1: [22: [], 23: []]",
247 activity.GetLog());
[email protected]14266072014-04-19 00:35:20248
249 // Crash the renderer
nick16b07652015-04-18 02:35:31250 main_test_rfh()->GetProcess()->SimulateCrash();
[email protected]14266072014-04-19 00:35:20251 EXPECT_EQ(
nick16b07652015-04-18 02:35:31252 "RenderFrameDeleted(23) -> 1: [22: [], 23*: []]\n"
253 "RenderFrameDeleted(22) -> 1: [22*: [], 23*: []]\n"
254 "RenderFrameDeleted(1) -> 1: []\n" // TODO(nick): Should be "1*:"
255 "RenderProcessGone -> 1*: []",
[email protected]14266072014-04-19 00:35:20256 activity.GetLog());
257}
258
dgroganfb22f9a2014-10-20 21:32:32259// Ensure that frames are not added to the tree, if the process passed in
260// is different than the process of the parent node.
261TEST_F(FrameTreeTest, FailAddFrameWithWrongProcessId) {
schenney6408fed22015-04-17 17:44:57262 contents()->NavigateAndCommit(GURL("http://www.google.com"));
dgroganfb22f9a2014-10-20 21:32:32263 FrameTree* frame_tree = contents()->GetFrameTree();
264 FrameTreeNode* root = frame_tree->root();
265 int process_id = root->current_frame_host()->GetProcess()->GetID();
266
267 ASSERT_EQ("1: []", GetTreeState(frame_tree));
268
269 // Simulate attaching a frame from mismatched process id.
270 ASSERT_FALSE(frame_tree->AddFrame(root, process_id + 1, 1, std::string()));
271 ASSERT_EQ("1: []", GetTreeState(frame_tree));
272}
273
naskoaeca57b2015-02-13 00:50:46274// Ensure that frames removed while a process has crashed are not preserved in
275// the global map of id->frame.
276TEST_F(FrameTreeTest, ProcessCrashClearsGlobalMap) {
277 // Add a couple child frames to the main frame.
278 FrameTreeNode* root = contents()->GetFrameTree()->root();
279
280 main_test_rfh()->OnCreateChildFrame(22, std::string(), SandboxFlags::NONE);
281 main_test_rfh()->OnCreateChildFrame(23, std::string(), SandboxFlags::NONE);
282
dmazzonie950ea232015-03-13 21:39:45283 // Add one grandchild frame.
284 RenderFrameHostImpl* child1_rfh = root->child_at(0)->current_frame_host();
285 child1_rfh->OnCreateChildFrame(33, std::string(), SandboxFlags::NONE);
286
naskoaeca57b2015-02-13 00:50:46287 // Ensure they can be found by id.
288 int64 id1 = root->child_at(0)->frame_tree_node_id();
289 int64 id2 = root->child_at(1)->frame_tree_node_id();
dmazzonie950ea232015-03-13 21:39:45290 int64 id3 = root->child_at(0)->child_at(0)->frame_tree_node_id();
291 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id1));
292 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id2));
293 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id3));
naskoaeca57b2015-02-13 00:50:46294
295 // Crash the renderer.
nick16b07652015-04-18 02:35:31296 main_test_rfh()->GetProcess()->SimulateCrash();
naskoaeca57b2015-02-13 00:50:46297
298 // Ensure they cannot be found by id after the process has crashed.
dmazzonie950ea232015-03-13 21:39:45299 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id1));
300 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id2));
301 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id3));
naskoaeca57b2015-02-13 00:50:46302}
303
[email protected]9b159a52013-10-03 17:24:55304} // namespace content