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 #ifndef CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_H_
6 #define CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_H_
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"
19 class RenderFrameHostDelegate
;
20 class RenderProcessHost
;
21 class RenderViewHostDelegate
;
22 class RenderViewHostImpl
;
23 class RenderFrameHostManager
;
24 class RenderWidgetHostDelegate
;
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.
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
34 // TODO(ajwong): Move NavigationController ownership to the main frame
35 // FrameTreeNode. Possibly expose access to it from here.
37 // This object is only used on the UI thread.
38 class CONTENT_EXPORT FrameTree
{
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
46 FrameTree(Navigator
* navigator
,
47 RenderFrameHostDelegate
* render_frame_delegate
,
48 RenderViewHostDelegate
* render_view_delegate
,
49 RenderWidgetHostDelegate
* render_widget_delegate
,
50 RenderFrameHostManager::Delegate
* manager_delegate
);
53 // Returns the FrameTreeNode with the given |frame_tree_node_id|.
54 static FrameTreeNode
* GloballyFindByID(int64 frame_tree_node_id
);
56 FrameTreeNode
* root() const { return root_
.get(); }
58 // Returns the FrameTreeNode with the given |frame_tree_node_id| if it is part
60 FrameTreeNode
* FindByID(int64 frame_tree_node_id
);
62 // Returns the FrameTreeNode with the given renderer-specific |routing_id|.
63 FrameTreeNode
* FindByRoutingID(int routing_id
, int process_id
);
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;
72 // Frame tree manipulation routines.
73 RenderFrameHostImpl
* AddFrame(FrameTreeNode
* parent
,
75 const std::string
& frame_name
);
76 void RemoveFrame(FrameTreeNode
* child
);
78 // This method walks the entire frame tree and creates a RenderFrameProxyHost
79 // for the given |site_instance| in each node except the |source| one --
80 // the source will have a RenderFrameHost. It assumes that no frame tree
81 // nodes already have RenderFrameProxyHost for the given |site_instance|.
82 void CreateProxiesForSiteInstance(
83 FrameTreeNode
* source
,
84 SiteInstance
* site_instance
);
86 // Clears process specific-state after a main frame process swap.
87 // This destroys most of the frame tree but retains the root node so that
88 // navigation state may be kept on it between process swaps. Used to
89 // support bookkeeping for top-level navigations.
90 // TODO(creis): Look into how we can remove the need for this method.
91 void ResetForMainFrameSwap();
93 // Update the frame tree after a process exits. Any nodes currently using the
94 // given |render_view_host| will lose all their children.
95 // TODO(creis): This should take a RenderProcessHost once RenderFrameHost
96 // knows its process. Until then, we would just be asking the RenderViewHost
97 // for its process, so we'll skip that step.
98 void RenderProcessGone(RenderViewHost
* render_view_host
);
100 // Convenience accessor for the main frame's RenderFrameHostImpl.
101 RenderFrameHostImpl
* GetMainFrame() const;
103 // Returns the focused frame.
104 FrameTreeNode
* GetFocusedFrame();
106 // Sets the focused frame.
107 void SetFocusedFrame(FrameTreeNode
* node
);
109 // Allows a client to listen for frame removal. The listener should expect
110 // to receive the RenderViewHostImpl containing the frame and the renderer-
111 // specific frame routing ID of the removed frame.
112 void SetFrameRemoveListener(
113 const base::Callback
<void(RenderFrameHost
*)>& on_frame_removed
);
115 // Creates a RenderViewHost for a new RenderFrameHost in the given
116 // |site_instance|. The RenderViewHost will have its Shutdown method called
117 // when all of the RenderFrameHosts using it are deleted.
118 RenderViewHostImpl
* CreateRenderViewHost(SiteInstance
* site_instance
,
120 int main_frame_routing_id
,
124 // Returns the existing RenderViewHost for a new RenderFrameHost.
125 // There should always be such a RenderViewHost, because the main frame
126 // RenderFrameHost for each SiteInstance should be created before subframes.
127 RenderViewHostImpl
* GetRenderViewHost(SiteInstance
* site_instance
);
129 // Keeps track of which RenderFrameHosts are using each RenderViewHost. When
130 // the number drops to zero, we call Shutdown on the RenderViewHost.
131 void RegisterRenderFrameHost(RenderFrameHostImpl
* render_frame_host
);
132 void UnregisterRenderFrameHost(RenderFrameHostImpl
* render_frame_host
);
135 typedef base::hash_map
<int, RenderViewHostImpl
*> RenderViewHostMap
;
136 typedef std::multimap
<int, RenderViewHostImpl
*> RenderViewHostMultiMap
;
138 // A variation to the public ForEach method with a difference that the subtree
139 // starting at |skip_this_subtree| will not be recursed into.
140 void ForEach(const base::Callback
<bool(FrameTreeNode
*)>& on_node
,
141 FrameTreeNode
* skip_this_subtree
) const;
143 // These delegates are installed into all the RenderViewHosts and
144 // RenderFrameHosts that we create.
145 RenderFrameHostDelegate
* render_frame_delegate_
;
146 RenderViewHostDelegate
* render_view_delegate_
;
147 RenderWidgetHostDelegate
* render_widget_delegate_
;
148 RenderFrameHostManager::Delegate
* manager_delegate_
;
150 // Map of SiteInstance ID to a RenderViewHost. This allows us to look up the
151 // RenderViewHost for a given SiteInstance when creating RenderFrameHosts.
152 // Combined with the refcount on RenderViewHost, this allows us to call
153 // Shutdown on the RenderViewHost and remove it from the map when no more
154 // RenderFrameHosts are using it.
156 // Must be declared before |root_| so that it is deleted afterward. Otherwise
157 // the map will be cleared before we delete the RenderFrameHosts in the tree.
158 RenderViewHostMap render_view_host_map_
;
160 // Map of SiteInstance ID to RenderViewHosts that are pending shutdown. The
161 // renderers of these RVH are currently executing the unload event in
162 // background. When the SwapOutACK is received, they will be deleted. In the
163 // meantime, they are kept in this map, as they should not be reused (part of
164 // their state is already gone away).
165 RenderViewHostMultiMap render_view_host_pending_shutdown_map_
;
167 scoped_ptr
<FrameTreeNode
> root_
;
169 int64 focused_frame_tree_node_id_
;
171 base::Callback
<void(RenderFrameHost
*)> on_frame_removed_
;
173 DISALLOW_COPY_AND_ASSIGN(FrameTree
);
176 } // namespace content
178 #endif // CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_H_