frame_tree.h revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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.
4
5#ifndef CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_H_
6#define CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_H_
7
8#include <string>
9
10#include "base/callback.h"
11#include "base/memory/scoped_ptr.h"
12#include "content/browser/frame_host/frame_tree_node.h"
13#include "content/common/content_export.h"
14
15namespace content {
16
17class FrameTreeNode;
18class RenderProcessHost;
19class RenderViewHostImpl;
20
21// Represents the frame tree for a page. With the exception of the main frame,
22// all FrameTreeNodes will be created/deleted in response to frame attach and
23// detach events in the DOM.
24//
25// The main frame's FrameTreeNode is special in that it is reused. This allows
26// it to serve as an anchor for state that needs to persist across top-level
27// page navigations.
28//
29// TODO(ajwong): Move NavigationController ownership to the main frame
30// FrameTreeNode. Possibly expose access to it from here.
31//
32// TODO(ajwong): Currently this class only contains FrameTreeNodes for
33// subframes if the --site-per-process flag is enabled.
34//
35// This object is only used on the UI thread.
36class CONTENT_EXPORT FrameTree {
37 public:
38  FrameTree();
39  ~FrameTree();
40
41  // Returns the FrameTreeNode with the given |frame_id|.
42  FrameTreeNode* FindByID(int64 frame_id);
43
44  // Executes |on_node| on each node in the frame tree.  If |on_node| returns
45  // false, terminates the iteration immediately. Returning false is useful
46  // if |on_node| is just doing a search over the tree.
47  void ForEach(const base::Callback<bool(FrameTreeNode*)>& on_node) const;
48
49  // After the FrameTree is created, or after SwapMainFrame() has been called,
50  // the root node does not yet have a frame id. This is allocated by the
51  // renderer and is published to the browser process on the first navigation
52  // after a swap. These two functions are used to set the root node's frame
53  // id.
54  //
55  // TODO(ajwong): Remove these once RenderFrameHost's routing id replaces
56  // frame_id.
57  bool IsFirstNavigationAfterSwap() const;
58  void OnFirstNavigationAfterSwap(int main_frame_id);
59
60  // Frame tree manipulation routines.
61  void AddFrame(int render_frame_host_id, int64 parent_frame_id,
62                int64 frame_id, const std::string& frame_name);
63  void RemoveFrame(int64 parent_frame_id, int64 frame_id);
64  void SetFrameUrl(int64 frame_id, const GURL& url);
65
66  // Resets the FrameTree and changes RenderFrameHost for the main frame.
67  // This destroys most of the frame tree but retains the root node so that
68  // navigation state may be kept on it between process swaps. Used to
69  // support bookkeeping for top-level navigations.
70  //
71  // If |main_frame| is NULL, reset tree to initially constructed state.
72  //
73  // TODO(ajwong): This function should not be given a |main_frame|. This is
74  // required currently because the RenderViewHost owns its main frame. When
75  // that relation is fixed, the FrameTree should be responsible for
76  // created/destroying the main frame on the swap.
77  void SwapMainFrame(RenderFrameHostImpl* main_frame);
78
79  // Convenience accessor for the main frame's RenderFrameHostImpl.
80  RenderFrameHostImpl* GetMainFrame() const;
81
82  // Allows a client to listen for frame removal.
83  void SetFrameRemoveListener(
84      const base::Callback<void(RenderViewHostImpl*, int64)>& on_frame_removed);
85
86  FrameTreeNode* GetRootForTesting() { return root_.get(); }
87
88 private:
89  scoped_ptr<FrameTreeNode> CreateNode(int64 frame_id,
90                                       const std::string& frame_name,
91                                       int render_frame_host_id,
92                                       RenderProcessHost* render_process_host);
93
94  scoped_ptr<FrameTreeNode> root_;
95
96  base::Callback<void(RenderViewHostImpl*, int64)> on_frame_removed_;
97
98  DISALLOW_COPY_AND_ASSIGN(FrameTree);
99};
100
101}  // namespace content
102
103#endif  // CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_H_
104