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 process_id
, int routing_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 // |process_id| is required to disambiguate |new_routing_id|, and it must
74 // match the process of the |parent| node. Otherwise this method returns
75 // nullptr. Passing MSG_ROUTING_NONE for |new_routing_id| will allocate a new
76 // routing ID for the new frame.
77 RenderFrameHostImpl
* AddFrame(FrameTreeNode
* parent
,
80 const std::string
& frame_name
);
81 void RemoveFrame(FrameTreeNode
* child
);
83 // This method walks the entire frame tree and creates a RenderFrameProxyHost
84 // for the given |site_instance| in each node except the |source| one --
85 // the source will have a RenderFrameHost. It assumes that no frame tree
86 // nodes already have RenderFrameProxyHost for the given |site_instance|.
87 void CreateProxiesForSiteInstance(
88 FrameTreeNode
* source
,
89 SiteInstance
* site_instance
);
91 // Clears process specific-state after a main frame process swap.
92 // This destroys most of the frame tree but retains the root node so that
93 // navigation state may be kept on it between process swaps. Used to
94 // support bookkeeping for top-level navigations.
95 // TODO(creis): Look into how we can remove the need for this method.
96 void ResetForMainFrameSwap();
98 // Convenience accessor for the main frame's RenderFrameHostImpl.
99 RenderFrameHostImpl
* GetMainFrame() const;
101 // Returns the focused frame.
102 FrameTreeNode
* GetFocusedFrame();
104 // Sets the focused frame.
105 void SetFocusedFrame(FrameTreeNode
* node
);
107 // Allows a client to listen for frame removal. The listener should expect
108 // to receive the RenderViewHostImpl containing the frame and the renderer-
109 // specific frame routing ID of the removed frame.
110 void SetFrameRemoveListener(
111 const base::Callback
<void(RenderFrameHost
*)>& on_frame_removed
);
113 // Creates a RenderViewHost for a new RenderFrameHost in the given
114 // |site_instance|. The RenderViewHost will have its Shutdown method called
115 // when all of the RenderFrameHosts using it are deleted.
116 RenderViewHostImpl
* CreateRenderViewHost(SiteInstance
* site_instance
,
118 int main_frame_routing_id
,
122 // Returns the existing RenderViewHost for a new RenderFrameHost.
123 // There should always be such a RenderViewHost, because the main frame
124 // RenderFrameHost for each SiteInstance should be created before subframes.
125 RenderViewHostImpl
* GetRenderViewHost(SiteInstance
* site_instance
);
127 // Keeps track of which RenderFrameHosts are using each RenderViewHost. When
128 // the number drops to zero, we call Shutdown on the RenderViewHost.
129 void RegisterRenderFrameHost(RenderFrameHostImpl
* render_frame_host
);
130 void UnregisterRenderFrameHost(RenderFrameHostImpl
* render_frame_host
);
133 typedef base::hash_map
<int, RenderViewHostImpl
*> RenderViewHostMap
;
134 typedef std::multimap
<int, RenderViewHostImpl
*> RenderViewHostMultiMap
;
136 // A variation to the public ForEach method with a difference that the subtree
137 // starting at |skip_this_subtree| will not be recursed into.
138 void ForEach(const base::Callback
<bool(FrameTreeNode
*)>& on_node
,
139 FrameTreeNode
* skip_this_subtree
) const;
141 // These delegates are installed into all the RenderViewHosts and
142 // RenderFrameHosts that we create.
143 RenderFrameHostDelegate
* render_frame_delegate_
;
144 RenderViewHostDelegate
* render_view_delegate_
;
145 RenderWidgetHostDelegate
* render_widget_delegate_
;
146 RenderFrameHostManager::Delegate
* manager_delegate_
;
148 // Map of SiteInstance ID to a RenderViewHost. This allows us to look up the
149 // RenderViewHost for a given SiteInstance when creating RenderFrameHosts.
150 // Combined with the refcount on RenderViewHost, this allows us to call
151 // Shutdown on the RenderViewHost and remove it from the map when no more
152 // RenderFrameHosts are using it.
154 // Must be declared before |root_| so that it is deleted afterward. Otherwise
155 // the map will be cleared before we delete the RenderFrameHosts in the tree.
156 RenderViewHostMap render_view_host_map_
;
158 // Map of SiteInstance ID to RenderViewHosts that are pending shutdown. The
159 // renderers of these RVH are currently executing the unload event in
160 // background. When the SwapOutACK is received, they will be deleted. In the
161 // meantime, they are kept in this map, as they should not be reused (part of
162 // their state is already gone away).
163 RenderViewHostMultiMap render_view_host_pending_shutdown_map_
;
165 scoped_ptr
<FrameTreeNode
> root_
;
167 int64 focused_frame_tree_node_id_
;
169 base::Callback
<void(RenderFrameHost
*)> on_frame_removed_
;
171 DISALLOW_COPY_AND_ASSIGN(FrameTree
);
174 } // namespace content
176 #endif // CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_H_