frame_tree.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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  FrameTreeNode* root() const { return root_.get(); }
54
55  // Returns the FrameTreeNode with the given |frame_tree_node_id|.
56  FrameTreeNode* FindByID(int64 frame_tree_node_id);
57
58  // Returns the FrameTreeNode with the given renderer-specific |routing_id|.
59  FrameTreeNode* FindByRoutingID(int routing_id, int process_id);
60
61  // Executes |on_node| on each node in the frame tree.  If |on_node| returns
62  // false, terminates the iteration immediately. Returning false is useful
63  // if |on_node| is just doing a search over the tree.  The iteration proceeds
64  // top-down and visits a node before adding its children to the queue, making
65  // it safe to remove children during the callback.
66  void ForEach(const base::Callback<bool(FrameTreeNode*)>& on_node) const;
67
68  // Frame tree manipulation routines.
69  RenderFrameHostImpl* AddFrame(FrameTreeNode* parent,
70                                int new_routing_id,
71                                const std::string& frame_name);
72  void RemoveFrame(FrameTreeNode* child);
73
74  // Clears process specific-state after a main frame process swap.
75  // This destroys most of the frame tree but retains the root node so that
76  // navigation state may be kept on it between process swaps. Used to
77  // support bookkeeping for top-level navigations.
78  // TODO(creis): Look into how we can remove the need for this method.
79  void ResetForMainFrameSwap();
80
81  // Update the frame tree after a process exits.  Any nodes currently using the
82  // given |render_view_host| will lose all their children.
83  // TODO(creis): This should take a RenderProcessHost once RenderFrameHost
84  // knows its process.  Until then, we would just be asking the RenderViewHost
85  // for its process, so we'll skip that step.
86  void RenderProcessGone(RenderViewHost* render_view_host);
87
88  // Convenience accessor for the main frame's RenderFrameHostImpl.
89  RenderFrameHostImpl* GetMainFrame() const;
90
91  // Returns the focused frame.
92  FrameTreeNode* GetFocusedFrame();
93
94  // Sets the focused frame.
95  void SetFocusedFrame(FrameTreeNode* node);
96
97  // Allows a client to listen for frame removal.  The listener should expect
98  // to receive the RenderViewHostImpl containing the frame and the renderer-
99  // specific frame routing ID of the removed frame.
100  // TODO(creis): These parameters will later change to be the RenderFrameHost.
101  void SetFrameRemoveListener(
102      const base::Callback<void(RenderViewHostImpl*, int)>& on_frame_removed);
103
104  // Creates a RenderViewHost for a new main frame RenderFrameHost in the given
105  // |site_instance|.  The RenderViewHost will have its Shutdown method called
106  // when all of the RenderFrameHosts using it are deleted.
107  RenderViewHostImpl* CreateRenderViewHostForMainFrame(
108      SiteInstance* site_instance,
109      int routing_id,
110      int main_frame_routing_id,
111      bool swapped_out,
112      bool hidden);
113
114  // Returns the existing RenderViewHost for a new subframe RenderFrameHost.
115  // There should always be such a RenderViewHost, because the main frame
116  // RenderFrameHost for each SiteInstance should be created before subframes.
117  RenderViewHostImpl* GetRenderViewHostForSubFrame(SiteInstance* site_instance);
118
119  // Keeps track of which RenderFrameHosts are using each RenderViewHost.  When
120  // the number drops to zero, we call Shutdown on the RenderViewHost.
121  void RegisterRenderFrameHost(RenderFrameHostImpl* render_frame_host);
122  void UnregisterRenderFrameHost(RenderFrameHostImpl* render_frame_host);
123
124 private:
125  typedef base::hash_map<int, RenderViewHostImpl*> RenderViewHostMap;
126  typedef std::multimap<int, RenderViewHostImpl*> RenderViewHostMultiMap;
127
128  // These delegates are installed into all the RenderViewHosts and
129  // RenderFrameHosts that we create.
130  RenderFrameHostDelegate* render_frame_delegate_;
131  RenderViewHostDelegate* render_view_delegate_;
132  RenderWidgetHostDelegate* render_widget_delegate_;
133  RenderFrameHostManager::Delegate* manager_delegate_;
134
135  // Map of SiteInstance ID to a RenderViewHost.  This allows us to look up the
136  // RenderViewHost for a given SiteInstance when creating RenderFrameHosts.
137  // Combined with the refcount on RenderViewHost, this allows us to call
138  // Shutdown on the RenderViewHost and remove it from the map when no more
139  // RenderFrameHosts are using it.
140  //
141  // Must be declared before |root_| so that it is deleted afterward.  Otherwise
142  // the map will be cleared before we delete the RenderFrameHosts in the tree.
143  RenderViewHostMap render_view_host_map_;
144
145  // Map of SiteInstance ID to RenderViewHosts that are pending shutdown. The
146  // renderers of these RVH are currently executing the unload event in
147  // background. When the SwapOutACK is received, they will be deleted. In the
148  // meantime, they are kept in this map, as they should not be reused (part of
149  // their state is already gone away).
150  RenderViewHostMultiMap render_view_host_pending_shutdown_map_;
151
152  scoped_ptr<FrameTreeNode> root_;
153
154  int64 focused_frame_tree_node_id_;
155
156  base::Callback<void(RenderViewHostImpl*, int)> on_frame_removed_;
157
158  DISALLOW_COPY_AND_ASSIGN(FrameTree);
159};
160
161}  // namespace content
162
163#endif  // CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_H_
164