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 FrameTreeNode
* root() const { return root_
.get(); }
55 // Returns the FrameTreeNode with the given |frame_tree_node_id| if it is part
57 FrameTreeNode
* FindByID(int frame_tree_node_id
);
59 // Returns the FrameTreeNode with the given renderer-specific |routing_id|.
60 FrameTreeNode
* FindByRoutingID(int process_id
, int routing_id
);
62 // Returns the first frame in this tree with the given |name|, or the main
63 // frame if |name| is empty.
64 // Note that this does NOT support pseudo-names like _self, _top, and _blank,
65 // nor searching other FrameTrees (unlike blink::WebView::findFrameByName).
66 FrameTreeNode
* FindByName(const std::string
& name
);
68 // Executes |on_node| on each node in the frame tree. If |on_node| returns
69 // false, terminates the iteration immediately. Returning false is useful
70 // if |on_node| is just doing a search over the tree. The iteration proceeds
71 // top-down and visits a node before adding its children to the queue, making
72 // it safe to remove children during the callback.
73 void ForEach(const base::Callback
<bool(FrameTreeNode
*)>& on_node
) const;
75 // Frame tree manipulation routines.
76 // |process_id| is required to disambiguate |new_routing_id|, and it must
77 // match the process of the |parent| node. Otherwise this method returns
78 // nullptr. Passing MSG_ROUTING_NONE for |new_routing_id| will allocate a new
79 // routing ID for the new frame.
80 RenderFrameHostImpl
* AddFrame(FrameTreeNode
* parent
,
83 blink::WebTreeScopeType scope
,
84 const std::string
& frame_name
,
85 blink::WebSandboxFlags sandbox_flags
);
86 void RemoveFrame(FrameTreeNode
* child
);
88 // This method walks the entire frame tree and creates a RenderFrameProxyHost
89 // for the given |site_instance| in each node except the |source| one --
90 // the source will have a RenderFrameHost. It assumes that no frame tree
91 // nodes already have RenderFrameProxyHost for the given |site_instance|.
92 void CreateProxiesForSiteInstance(
93 FrameTreeNode
* source
,
94 SiteInstance
* site_instance
);
96 // Convenience accessor for the main frame's RenderFrameHostImpl.
97 RenderFrameHostImpl
* GetMainFrame() const;
99 // Returns the focused frame.
100 FrameTreeNode
* GetFocusedFrame();
102 // Sets the focused frame.
103 void SetFocusedFrame(FrameTreeNode
* node
);
105 // Allows a client to listen for frame removal. The listener should expect
106 // to receive the RenderViewHostImpl containing the frame and the renderer-
107 // specific frame routing ID of the removed frame.
108 void SetFrameRemoveListener(
109 const base::Callback
<void(RenderFrameHost
*)>& on_frame_removed
);
111 // Creates a RenderViewHost for a new RenderFrameHost in the given
112 // |site_instance|. The RenderViewHost will have its Shutdown method called
113 // when all of the RenderFrameHosts using it are deleted.
114 RenderViewHostImpl
* CreateRenderViewHost(SiteInstance
* site_instance
,
116 int main_frame_routing_id
,
120 // Returns the existing RenderViewHost for a new RenderFrameHost.
121 // There should always be such a RenderViewHost, because the main frame
122 // RenderFrameHost for each SiteInstance should be created before subframes.
123 RenderViewHostImpl
* GetRenderViewHost(SiteInstance
* site_instance
);
125 // Keeps track of which RenderFrameHosts and RenderFrameProxyHosts are using
126 // each RenderViewHost. When the number drops to zero, we call Shutdown on
127 // the RenderViewHost.
128 void AddRenderViewHostRef(RenderViewHostImpl
* render_view_host
);
129 void ReleaseRenderViewHostRef(RenderViewHostImpl
* render_view_host
);
131 // This is only meant to be called by FrameTreeNode. Triggers calling
132 // the listener installed by SetFrameRemoveListener.
133 void FrameRemoved(FrameTreeNode
* frame
);
135 // Updates the overall load progress and notifies the WebContents.
136 void UpdateLoadProgress();
138 // Returns this FrameTree's total load progress.
139 double load_progress() { return load_progress_
; }
141 // Resets the load progress on all nodes in this FrameTree.
142 void ResetLoadProgress();
144 // Returns true if at least one of the nodes in this FrameTree is loading.
148 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostImplBrowserTest
, RemoveFocusedFrame
);
149 typedef base::hash_map
<int, RenderViewHostImpl
*> RenderViewHostMap
;
150 typedef std::multimap
<int, RenderViewHostImpl
*> RenderViewHostMultiMap
;
152 // A variation to the public ForEach method with a difference that the subtree
153 // starting at |skip_this_subtree| will not be recursed into.
154 void ForEach(const base::Callback
<bool(FrameTreeNode
*)>& on_node
,
155 FrameTreeNode
* skip_this_subtree
) const;
157 // These delegates are installed into all the RenderViewHosts and
158 // RenderFrameHosts that we create.
159 RenderFrameHostDelegate
* render_frame_delegate_
;
160 RenderViewHostDelegate
* render_view_delegate_
;
161 RenderWidgetHostDelegate
* render_widget_delegate_
;
162 RenderFrameHostManager::Delegate
* manager_delegate_
;
164 // Map of SiteInstance ID to a RenderViewHost. This allows us to look up the
165 // RenderViewHost for a given SiteInstance when creating RenderFrameHosts.
166 // Combined with the refcount on RenderViewHost, this allows us to call
167 // Shutdown on the RenderViewHost and remove it from the map when no more
168 // RenderFrameHosts are using it.
170 // Must be declared before |root_| so that it is deleted afterward. Otherwise
171 // the map will be cleared before we delete the RenderFrameHosts in the tree.
172 RenderViewHostMap render_view_host_map_
;
174 // Map of SiteInstance ID to RenderViewHosts that are pending shutdown. The
175 // renderers of these RVH are currently executing the unload event in
176 // background. When the SwapOutACK is received, they will be deleted. In the
177 // meantime, they are kept in this map, as they should not be reused (part of
178 // their state is already gone away).
179 RenderViewHostMultiMap render_view_host_pending_shutdown_map_
;
181 scoped_ptr
<FrameTreeNode
> root_
;
183 int focused_frame_tree_node_id_
;
185 base::Callback
<void(RenderFrameHost
*)> on_frame_removed_
;
187 // Overall load progress.
188 double load_progress_
;
190 DISALLOW_COPY_AND_ASSIGN(FrameTree
);
193 } // namespace content
195 #endif // CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_H_