frame_tree.h revision 1675a649fd7a8b3cb80ffddae2dc181f122353c5
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 Navigator;
19class RenderFrameHostDelegate;
20class RenderProcessHost;
21class RenderViewHostDelegate;
22class RenderViewHostImpl;
23class RenderFrameHostManager;
24class RenderWidgetHostDelegate;
25
26// Represents the frame tree for a page. With the exception of the main frame,
27// all FrameTreeNodes will be created/deleted in response to frame attach and
28// detach events in the DOM.
29//
30// The main frame's FrameTreeNode is special in that it is reused. This allows
31// it to serve as an anchor for state that needs to persist across top-level
32// page navigations.
33//
34// TODO(ajwong): Move NavigationController ownership to the main frame
35// FrameTreeNode. Possibly expose access to it from here.
36//
37// This object is only used on the UI thread.
38class CONTENT_EXPORT FrameTree {
39 public:
40  // Each FrameTreeNode will default to using the given |navigator| for
41  // navigation tasks in the frame.
42  // A set of delegates are remembered here so that we can create
43  // RenderFrameHostManagers.
44  // TODO(creis): This set of delegates will change as we move things to
45  // Navigator.
46  FrameTree(Navigator* navigator,
47            RenderFrameHostDelegate* render_frame_delegate,
48            RenderViewHostDelegate* render_view_delegate,
49            RenderWidgetHostDelegate* render_widget_delegate,
50            RenderFrameHostManager::Delegate* manager_delegate);
51  ~FrameTree();
52
53  // Returns the FrameTreeNode with the given |frame_tree_node_id|.
54  static FrameTreeNode* GloballyFindByID(int64 frame_tree_node_id);
55
56  FrameTreeNode* root() const { return root_.get(); }
57
58  // Returns the FrameTreeNode with the given |frame_tree_node_id| if it is part
59  // of this FrameTree.
60  FrameTreeNode* FindByID(int64 frame_tree_node_id);
61
62  // Returns the FrameTreeNode with the given renderer-specific |routing_id|.
63  FrameTreeNode* FindByRoutingID(int routing_id, int process_id);
64
65  // Executes |on_node| on each node in the frame tree.  If |on_node| returns
66  // false, terminates the iteration immediately. Returning false is useful
67  // if |on_node| is just doing a search over the tree.  The iteration proceeds
68  // top-down and visits a node before adding its children to the queue, making
69  // it safe to remove children during the callback.
70  void ForEach(const base::Callback<bool(FrameTreeNode*)>& on_node) const;
71
72  // Frame tree manipulation routines.
73  RenderFrameHostImpl* AddFrame(FrameTreeNode* parent,
74                                int process_id,
75                                int new_routing_id,
76                                const std::string& frame_name);
77  void RemoveFrame(FrameTreeNode* child);
78
79  // This method walks the entire frame tree and creates a RenderFrameProxyHost
80  // for the given |site_instance| in each node except the |source| one --
81  // the source will have a RenderFrameHost. It assumes that no frame tree
82  // nodes already have RenderFrameProxyHost for the given |site_instance|.
83  void CreateProxiesForSiteInstance(
84      FrameTreeNode* source,
85      SiteInstance* site_instance);
86
87  // Clears process specific-state after a main frame process swap.
88  // This destroys most of the frame tree but retains the root node so that
89  // navigation state may be kept on it between process swaps. Used to
90  // support bookkeeping for top-level navigations.
91  // TODO(creis): Look into how we can remove the need for this method.
92  void ResetForMainFrameSwap();
93
94  // Update the frame tree after a process exits.  Any nodes currently using the
95  // given |render_view_host| will lose all their children.
96  // TODO(creis): This should take a RenderProcessHost once RenderFrameHost
97  // knows its process.  Until then, we would just be asking the RenderViewHost
98  // for its process, so we'll skip that step.
99  void RenderProcessGone(RenderViewHost* render_view_host);
100
101  // Convenience accessor for the main frame's RenderFrameHostImpl.
102  RenderFrameHostImpl* GetMainFrame() const;
103
104  // Returns the focused frame.
105  FrameTreeNode* GetFocusedFrame();
106
107  // Sets the focused frame.
108  void SetFocusedFrame(FrameTreeNode* node);
109
110  // Allows a client to listen for frame removal.  The listener should expect
111  // to receive the RenderViewHostImpl containing the frame and the renderer-
112  // specific frame routing ID of the removed frame.
113  void SetFrameRemoveListener(
114      const base::Callback<void(RenderFrameHost*)>& on_frame_removed);
115
116  // Creates a RenderViewHost for a new RenderFrameHost in the given
117  // |site_instance|.  The RenderViewHost will have its Shutdown method called
118  // when all of the RenderFrameHosts using it are deleted.
119  RenderViewHostImpl* CreateRenderViewHost(SiteInstance* site_instance,
120                                           int routing_id,
121                                           int main_frame_routing_id,
122                                           bool swapped_out,
123                                           bool hidden);
124
125  // Returns the existing RenderViewHost for a new RenderFrameHost.
126  // There should always be such a RenderViewHost, because the main frame
127  // RenderFrameHost for each SiteInstance should be created before subframes.
128  RenderViewHostImpl* GetRenderViewHost(SiteInstance* site_instance);
129
130  // Keeps track of which RenderFrameHosts are using each RenderViewHost.  When
131  // the number drops to zero, we call Shutdown on the RenderViewHost.
132  void RegisterRenderFrameHost(RenderFrameHostImpl* render_frame_host);
133  void UnregisterRenderFrameHost(RenderFrameHostImpl* render_frame_host);
134
135 private:
136  typedef base::hash_map<int, RenderViewHostImpl*> RenderViewHostMap;
137  typedef std::multimap<int, RenderViewHostImpl*> RenderViewHostMultiMap;
138
139  // These delegates are installed into all the RenderViewHosts and
140  // RenderFrameHosts that we create.
141  RenderFrameHostDelegate* render_frame_delegate_;
142  RenderViewHostDelegate* render_view_delegate_;
143  RenderWidgetHostDelegate* render_widget_delegate_;
144  RenderFrameHostManager::Delegate* manager_delegate_;
145
146  // Map of SiteInstance ID to a RenderViewHost.  This allows us to look up the
147  // RenderViewHost for a given SiteInstance when creating RenderFrameHosts.
148  // Combined with the refcount on RenderViewHost, this allows us to call
149  // Shutdown on the RenderViewHost and remove it from the map when no more
150  // RenderFrameHosts are using it.
151  //
152  // Must be declared before |root_| so that it is deleted afterward.  Otherwise
153  // the map will be cleared before we delete the RenderFrameHosts in the tree.
154  RenderViewHostMap render_view_host_map_;
155
156  // Map of SiteInstance ID to RenderViewHosts that are pending shutdown. The
157  // renderers of these RVH are currently executing the unload event in
158  // background. When the SwapOutACK is received, they will be deleted. In the
159  // meantime, they are kept in this map, as they should not be reused (part of
160  // their state is already gone away).
161  RenderViewHostMultiMap render_view_host_pending_shutdown_map_;
162
163  scoped_ptr<FrameTreeNode> root_;
164
165  int64 focused_frame_tree_node_id_;
166
167  base::Callback<void(RenderFrameHost*)> on_frame_removed_;
168
169  DISALLOW_COPY_AND_ASSIGN(FrameTree);
170};
171
172}  // namespace content
173
174#endif  // CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_H_
175