1 // 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.
5 #include "content/browser/frame_host/frame_tree.h"
7 #include "base/run_loop.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "content/browser/frame_host/navigator_impl.h"
10 #include "content/browser/frame_host/render_frame_host_factory.h"
11 #include "content/browser/frame_host/render_frame_host_impl.h"
12 #include "content/browser/renderer_host/render_view_host_impl.h"
13 #include "content/browser/web_contents/web_contents_impl.h"
14 #include "content/common/frame_messages.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #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"
19 #include "content/test/test_render_frame_host.h"
20 #include "content/test/test_render_view_host.h"
21 #include "content/test/test_web_contents.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "third_party/WebKit/public/web/WebSandboxFlags.h"
29 // Appends a description of the structure of the frame tree to |result|.
30 void AppendTreeNodeState(FrameTreeNode
* node
, std::string
* result
) {
32 base::Int64ToString(node
->current_frame_host()->GetRoutingID()));
33 if (!node
->current_frame_host()->IsRenderFrameLive())
34 result
->append("*"); // Asterisk next to dead frames.
36 if (!node
->frame_name().empty()) {
38 result
->append(node
->frame_name());
41 result
->append(": [");
42 const char* separator
= "";
43 for (size_t i
= 0; i
< node
->child_count(); i
++) {
44 result
->append(separator
);
45 AppendTreeNodeState(node
->child_at(i
), result
);
51 // Logs calls to WebContentsObserver along with the state of the frame tree,
52 // for later use in EXPECT_EQ().
53 class TreeWalkingWebContentsLogger
: public WebContentsObserver
{
55 explicit TreeWalkingWebContentsLogger(WebContents
* web_contents
)
56 : WebContentsObserver(web_contents
) {}
58 ~TreeWalkingWebContentsLogger() override
{
59 EXPECT_EQ("", log_
) << "Activity logged that was not expected";
62 // Gets and resets the log, which is a string of what happened.
63 std::string
GetLog() {
64 std::string result
= log_
;
69 // content::WebContentsObserver implementation.
70 void RenderFrameCreated(RenderFrameHost
* render_frame_host
) override
{
71 LogWhatHappened("RenderFrameCreated", render_frame_host
);
74 void RenderFrameHostChanged(RenderFrameHost
* old_host
,
75 RenderFrameHost
* new_host
) override
{
77 LogWhatHappened("RenderFrameHostChanged(old)", old_host
);
78 LogWhatHappened("RenderFrameHostChanged(new)", new_host
);
81 void RenderFrameDeleted(RenderFrameHost
* render_frame_host
) override
{
82 LogWhatHappened("RenderFrameDeleted", render_frame_host
);
85 void RenderProcessGone(base::TerminationStatus status
) override
{
86 LogWhatHappened("RenderProcessGone");
90 void LogWhatHappened(const std::string
& event_name
) {
94 log_
.append(event_name
+ " -> ");
96 static_cast<WebContentsImpl
*>(web_contents())->GetFrameTree()->root(),
100 void LogWhatHappened(const std::string
& event_name
, RenderFrameHost
* rfh
) {
102 base::StringPrintf("%s(%d)", event_name
.c_str(), rfh
->GetRoutingID()));
107 DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger
);
112 class FrameTreeTest
: public RenderViewHostImplTestHarness
{
114 // Prints a FrameTree, for easy assertions of the tree hierarchy.
115 std::string
GetTreeState(FrameTree
* frame_tree
) {
117 AppendTreeNodeState(frame_tree
->root(), &result
);
122 // Exercise tree manipulation routines.
123 // - Add a series of nodes and verify tree structure.
124 // - Remove a series of nodes and verify tree structure.
126 // TODO(nick): http://crbug.com/444722 Disabled temporarily because of a bad
127 // interaction with the WebContentsObserverConsistencyChecker -- calling
128 // AddFrame directly causes the RFH to not be announced. We either need to
129 // rewrite this test, or be consistent in the layer at which we announce render
131 TEST_F(FrameTreeTest
, DISABLED_Shape
) {
132 // Use the FrameTree of the WebContents so that it has all the delegates it
133 // needs. We may want to consider a test version of this.
134 FrameTree
* frame_tree
= contents()->GetFrameTree();
135 FrameTreeNode
* root
= frame_tree
->root();
137 std::string
no_children_node("no children node");
138 std::string
deep_subtree("node with deep subtree");
139 int process_id
= root
->current_frame_host()->GetProcess()->GetID();
141 ASSERT_EQ("1: []", GetTreeState(frame_tree
));
143 // Simulate attaching a series of frames to build the frame tree.
144 frame_tree
->AddFrame(root
, process_id
, 14, blink::WebTreeScopeType::Document
,
145 std::string(), blink::WebSandboxFlags::None
);
146 frame_tree
->AddFrame(root
, process_id
, 15, blink::WebTreeScopeType::Document
,
147 std::string(), blink::WebSandboxFlags::None
);
148 frame_tree
->AddFrame(root
, process_id
, 16, blink::WebTreeScopeType::Document
,
149 std::string(), blink::WebSandboxFlags::None
);
151 frame_tree
->AddFrame(root
->child_at(0), process_id
, 244,
152 blink::WebTreeScopeType::Document
, std::string(),
153 blink::WebSandboxFlags::None
);
154 frame_tree
->AddFrame(root
->child_at(1), process_id
, 255,
155 blink::WebTreeScopeType::Document
, no_children_node
,
156 blink::WebSandboxFlags::None
);
157 frame_tree
->AddFrame(root
->child_at(0), process_id
, 245,
158 blink::WebTreeScopeType::Document
, std::string(),
159 blink::WebSandboxFlags::None
);
161 ASSERT_EQ("1: [14: [244: [], 245: []], "
162 "15: [255 'no children node': []], "
164 GetTreeState(frame_tree
));
166 FrameTreeNode
* child_16
= root
->child_at(2);
167 frame_tree
->AddFrame(child_16
, process_id
, 264,
168 blink::WebTreeScopeType::Document
, std::string(),
169 blink::WebSandboxFlags::None
);
170 frame_tree
->AddFrame(child_16
, process_id
, 265,
171 blink::WebTreeScopeType::Document
, std::string(),
172 blink::WebSandboxFlags::None
);
173 frame_tree
->AddFrame(child_16
, process_id
, 266,
174 blink::WebTreeScopeType::Document
, std::string(),
175 blink::WebSandboxFlags::None
);
176 frame_tree
->AddFrame(child_16
, process_id
, 267,
177 blink::WebTreeScopeType::Document
, deep_subtree
,
178 blink::WebSandboxFlags::None
);
179 frame_tree
->AddFrame(child_16
, process_id
, 268,
180 blink::WebTreeScopeType::Document
, std::string(),
181 blink::WebSandboxFlags::None
);
183 FrameTreeNode
* child_267
= child_16
->child_at(3);
184 frame_tree
->AddFrame(child_267
, process_id
, 365,
185 blink::WebTreeScopeType::Document
, std::string(),
186 blink::WebSandboxFlags::None
);
187 frame_tree
->AddFrame(child_267
->child_at(0), process_id
, 455,
188 blink::WebTreeScopeType::Document
, std::string(),
189 blink::WebSandboxFlags::None
);
190 frame_tree
->AddFrame(child_267
->child_at(0)->child_at(0), process_id
, 555,
191 blink::WebTreeScopeType::Document
, std::string(),
192 blink::WebSandboxFlags::None
);
193 frame_tree
->AddFrame(child_267
->child_at(0)->child_at(0)->child_at(0),
194 process_id
, 655, blink::WebTreeScopeType::Document
,
195 std::string(), blink::WebSandboxFlags::None
);
197 // Now that's it's fully built, verify the tree structure is as expected.
198 ASSERT_EQ("1: [14: [244: [], 245: []], "
199 "15: [255 'no children node': []], "
200 "16: [264: [], 265: [], 266: [], "
201 "267 'node with deep subtree': "
202 "[365: [455: [555: [655: []]]]], 268: []]]",
203 GetTreeState(frame_tree
));
205 FrameTreeNode
* child_555
= child_267
->child_at(0)->child_at(0)->child_at(0);
206 frame_tree
->RemoveFrame(child_555
);
207 ASSERT_EQ("1: [14: [244: [], 245: []], "
208 "15: [255 'no children node': []], "
209 "16: [264: [], 265: [], 266: [], "
210 "267 'node with deep subtree': "
211 "[365: [455: []]], 268: []]]",
212 GetTreeState(frame_tree
));
214 frame_tree
->RemoveFrame(child_16
->child_at(1));
215 ASSERT_EQ("1: [14: [244: [], 245: []], "
216 "15: [255 'no children node': []], "
217 "16: [264: [], 266: [], "
218 "267 'node with deep subtree': "
219 "[365: [455: []]], 268: []]]",
220 GetTreeState(frame_tree
));
222 frame_tree
->RemoveFrame(root
->child_at(1));
223 ASSERT_EQ("1: [14: [244: [], 245: []], "
224 "16: [264: [], 266: [], "
225 "267 'node with deep subtree': "
226 "[365: [455: []]], 268: []]]",
227 GetTreeState(frame_tree
));
230 // Ensure frames can be found by frame_tree_node_id, routing ID, or name.
231 TEST_F(FrameTreeTest
, FindFrames
) {
232 main_test_rfh()->InitializeRenderFrameIfNeeded();
234 // Add a few child frames to the main frame.
235 FrameTree
* frame_tree
= contents()->GetFrameTree();
236 FrameTreeNode
* root
= frame_tree
->root();
238 main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document
,
239 "child0", blink::WebSandboxFlags::None
);
240 main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document
,
241 "child1", blink::WebSandboxFlags::None
);
242 main_test_rfh()->OnCreateChildFrame(24, blink::WebTreeScopeType::Document
,
244 blink::WebSandboxFlags::None
);
245 FrameTreeNode
* child0
= root
->child_at(0);
246 FrameTreeNode
* child1
= root
->child_at(1);
248 FrameTreeNode
* child2
= root
->child_at(2);
250 // Add one grandchild frame.
251 child1
->current_frame_host()->OnCreateChildFrame(
252 33, blink::WebTreeScopeType::Document
, "grandchild",
253 blink::WebSandboxFlags::None
);
254 FrameTreeNode
* grandchild
= child1
->child_at(0);
256 // Ensure they can be found by FTN id.
257 EXPECT_EQ(root
, frame_tree
->FindByID(root
->frame_tree_node_id()));
258 EXPECT_EQ(child0
, frame_tree
->FindByID(child0
->frame_tree_node_id()));
259 EXPECT_EQ(child1
, frame_tree
->FindByID(child1
->frame_tree_node_id()));
260 EXPECT_EQ(child2
, frame_tree
->FindByID(child2
->frame_tree_node_id()));
261 EXPECT_EQ(grandchild
, frame_tree
->FindByID(grandchild
->frame_tree_node_id()));
262 EXPECT_EQ(nullptr, frame_tree
->FindByID(-1));
264 // Ensure they can be found by routing id.
265 int process_id
= main_test_rfh()->GetProcess()->GetID();
266 EXPECT_EQ(root
, frame_tree
->FindByRoutingID(process_id
,
267 main_test_rfh()->GetRoutingID()));
268 EXPECT_EQ(child0
, frame_tree
->FindByRoutingID(process_id
, 22));
269 EXPECT_EQ(child1
, frame_tree
->FindByRoutingID(process_id
, 23));
270 EXPECT_EQ(child2
, frame_tree
->FindByRoutingID(process_id
, 24));
271 EXPECT_EQ(grandchild
, frame_tree
->FindByRoutingID(process_id
, 33));
272 EXPECT_EQ(nullptr, frame_tree
->FindByRoutingID(process_id
, 37));
274 // Ensure they can be found by name, if they have one.
275 EXPECT_EQ(root
, frame_tree
->FindByName(std::string()));
276 EXPECT_EQ(child0
, frame_tree
->FindByName("child0"));
277 EXPECT_EQ(child1
, frame_tree
->FindByName("child1"));
278 EXPECT_EQ(grandchild
, frame_tree
->FindByName("grandchild"));
279 EXPECT_EQ(nullptr, frame_tree
->FindByName("no such frame"));
282 // Check that PreviousSibling() is retrieved correctly.
283 TEST_F(FrameTreeTest
, PreviousSibling
) {
284 main_test_rfh()->InitializeRenderFrameIfNeeded();
286 // Add a few child frames to the main frame.
287 FrameTree
* frame_tree
= contents()->GetFrameTree();
288 FrameTreeNode
* root
= frame_tree
->root();
289 main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document
,
290 "child0", blink::WebSandboxFlags::None
);
291 main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document
,
292 "child1", blink::WebSandboxFlags::None
);
293 main_test_rfh()->OnCreateChildFrame(24, blink::WebTreeScopeType::Document
,
294 "child2", blink::WebSandboxFlags::None
);
295 FrameTreeNode
* child0
= root
->child_at(0);
296 FrameTreeNode
* child1
= root
->child_at(1);
297 FrameTreeNode
* child2
= root
->child_at(2);
299 // Add one grandchild frame.
300 child1
->current_frame_host()->OnCreateChildFrame(
301 33, blink::WebTreeScopeType::Document
, "grandchild",
302 blink::WebSandboxFlags::None
);
303 FrameTreeNode
* grandchild
= child1
->child_at(0);
305 EXPECT_EQ(nullptr, root
->PreviousSibling());
306 EXPECT_EQ(nullptr, child0
->PreviousSibling());
307 EXPECT_EQ(child0
, child1
->PreviousSibling());
308 EXPECT_EQ(child1
, child2
->PreviousSibling());
309 EXPECT_EQ(nullptr, grandchild
->PreviousSibling());
312 // Do some simple manipulations of the frame tree, making sure that
313 // WebContentsObservers see a consistent view of the tree as we go.
314 TEST_F(FrameTreeTest
, ObserverWalksTreeDuringFrameCreation
) {
315 TreeWalkingWebContentsLogger
activity(contents());
316 contents()->NavigateAndCommit(GURL("http://www.google.com"));
317 EXPECT_EQ("RenderFrameCreated(1) -> 1: []", activity
.GetLog());
319 FrameTree
* frame_tree
= contents()->GetFrameTree();
320 FrameTreeNode
* root
= frame_tree
->root();
322 // Simulate attaching a series of frames to build the frame tree.
323 main_test_rfh()->OnCreateChildFrame(14, blink::WebTreeScopeType::Document
,
325 blink::WebSandboxFlags::None
);
327 "RenderFrameHostChanged(new)(14) -> 1: []\n"
328 "RenderFrameCreated(14) -> 1: [14: []]",
330 main_test_rfh()->OnCreateChildFrame(18, blink::WebTreeScopeType::Document
,
332 blink::WebSandboxFlags::None
);
334 "RenderFrameHostChanged(new)(18) -> 1: [14: []]\n"
335 "RenderFrameCreated(18) -> 1: [14: [], 18: []]",
337 frame_tree
->RemoveFrame(root
->child_at(0));
338 EXPECT_EQ("RenderFrameDeleted(14) -> 1: [18: []]", activity
.GetLog());
339 frame_tree
->RemoveFrame(root
->child_at(0));
340 EXPECT_EQ("RenderFrameDeleted(18) -> 1: []", activity
.GetLog());
343 // Make sure that WebContentsObservers see a consistent view of the tree after
344 // recovery from a render process crash.
345 TEST_F(FrameTreeTest
, ObserverWalksTreeAfterCrash
) {
346 TreeWalkingWebContentsLogger
activity(contents());
347 contents()->NavigateAndCommit(GURL("http://www.google.com"));
348 EXPECT_EQ("RenderFrameCreated(1) -> 1: []", activity
.GetLog());
350 main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document
,
352 blink::WebSandboxFlags::None
);
354 "RenderFrameHostChanged(new)(22) -> 1: []\n"
355 "RenderFrameCreated(22) -> 1: [22: []]",
357 main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document
,
359 blink::WebSandboxFlags::None
);
361 "RenderFrameHostChanged(new)(23) -> 1: [22: []]\n"
362 "RenderFrameCreated(23) -> 1: [22: [], 23: []]",
365 // Crash the renderer
366 main_test_rfh()->GetProcess()->SimulateCrash();
368 "RenderProcessGone -> 1*: [22*: [], 23*: []]\n"
369 "RenderFrameDeleted(23) -> 1*: [22*: [], 23*: []]\n"
370 "RenderFrameDeleted(22) -> 1*: [22*: [], 23*: []]\n"
371 "RenderFrameDeleted(1) -> 1*: []",
375 // Ensure that frames are not added to the tree, if the process passed in
376 // is different than the process of the parent node.
377 TEST_F(FrameTreeTest
, FailAddFrameWithWrongProcessId
) {
378 contents()->NavigateAndCommit(GURL("http://www.google.com"));
379 FrameTree
* frame_tree
= contents()->GetFrameTree();
380 FrameTreeNode
* root
= frame_tree
->root();
381 int process_id
= root
->current_frame_host()->GetProcess()->GetID();
383 ASSERT_EQ("1: []", GetTreeState(frame_tree
));
385 // Simulate attaching a frame from mismatched process id.
386 ASSERT_FALSE(frame_tree
->AddFrame(
387 root
, process_id
+ 1, 1, blink::WebTreeScopeType::Document
, std::string(),
388 blink::WebSandboxFlags::None
));
389 ASSERT_EQ("1: []", GetTreeState(frame_tree
));
392 // Ensure that frames removed while a process has crashed are not preserved in
393 // the global map of id->frame.
394 TEST_F(FrameTreeTest
, ProcessCrashClearsGlobalMap
) {
395 main_test_rfh()->InitializeRenderFrameIfNeeded();
397 // Add a couple child frames to the main frame.
398 FrameTreeNode
* root
= contents()->GetFrameTree()->root();
400 main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document
,
402 blink::WebSandboxFlags::None
);
403 main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document
,
405 blink::WebSandboxFlags::None
);
407 // Add one grandchild frame.
408 RenderFrameHostImpl
* child1_rfh
= root
->child_at(0)->current_frame_host();
409 child1_rfh
->OnCreateChildFrame(33, blink::WebTreeScopeType::Document
,
410 std::string(), blink::WebSandboxFlags::None
);
412 // Ensure they can be found by id.
413 int id1
= root
->child_at(0)->frame_tree_node_id();
414 int id2
= root
->child_at(1)->frame_tree_node_id();
415 int id3
= root
->child_at(0)->child_at(0)->frame_tree_node_id();
416 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id1
));
417 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id2
));
418 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id3
));
420 // Crash the renderer.
421 main_test_rfh()->GetProcess()->SimulateCrash();
423 // Ensure they cannot be found by id after the process has crashed.
424 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id1
));
425 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id2
));
426 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id3
));
429 } // namespace content