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 #include "content/browser/frame_host/frame_tree.h"
10 #include "base/callback.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/lazy_instance.h"
13 #include "content/browser/frame_host/frame_tree_node.h"
14 #include "content/browser/frame_host/navigator.h"
15 #include "content/browser/frame_host/render_frame_host_factory.h"
16 #include "content/browser/frame_host/render_frame_host_impl.h"
17 #include "content/browser/frame_host/render_frame_proxy_host.h"
18 #include "content/browser/renderer_host/render_view_host_factory.h"
19 #include "content/browser/renderer_host/render_view_host_impl.h"
20 #include "content/common/site_isolation_policy.h"
21 #include "third_party/WebKit/public/web/WebSandboxFlags.h"
27 // Used with FrameTree::ForEach() to search for the FrameTreeNode
28 // corresponding to |frame_tree_node_id| within a specific FrameTree.
29 bool FrameTreeNodeForId(int frame_tree_node_id
,
30 FrameTreeNode
** out_node
,
31 FrameTreeNode
* node
) {
32 if (node
->frame_tree_node_id() == frame_tree_node_id
) {
34 // Terminate iteration once the node has been found.
40 // Used with FrameTree::ForEach() to search for the FrameTreeNode with the given
41 // |name| within a specific FrameTree.
42 bool FrameTreeNodeForName(const std::string
& name
,
43 FrameTreeNode
** out_node
,
44 FrameTreeNode
* node
) {
45 if (node
->frame_name() == name
) {
47 // Terminate iteration once the node has been found.
53 bool CreateProxyForSiteInstance(const scoped_refptr
<SiteInstance
>& instance
,
54 FrameTreeNode
* node
) {
55 // If a new frame is created in the current SiteInstance, other frames in
56 // that SiteInstance don't need a proxy for the new frame.
57 SiteInstance
* current_instance
=
58 node
->render_manager()->current_frame_host()->GetSiteInstance();
59 if (current_instance
!= instance
.get())
60 node
->render_manager()->CreateRenderFrameProxy(instance
.get());
64 // Helper function used with FrameTree::ForEach() for retrieving the total
65 // loading progress and number of frames in a frame tree.
66 bool CollectLoadProgress(double* progress
,
68 FrameTreeNode
* node
) {
69 // Ignore the current frame if it has not started loading.
70 if (!node
->has_started_loading())
74 *progress
+= node
->loading_progress();
79 // Helper function used with FrameTree::ForEach() to reset the load progress.
80 bool ResetNodeLoadProgress(FrameTreeNode
* node
) {
81 node
->reset_loading_progress();
85 // Helper function used with FrameTree::ForEach() to check if at least one of
86 // the nodes is loading.
87 bool IsNodeLoading(bool* is_loading
, FrameTreeNode
* node
) {
88 if (node
->IsLoading()) {
89 // There is at least one node loading, so abort traversal.
98 FrameTree::FrameTree(Navigator
* navigator
,
99 RenderFrameHostDelegate
* render_frame_delegate
,
100 RenderViewHostDelegate
* render_view_delegate
,
101 RenderWidgetHostDelegate
* render_widget_delegate
,
102 RenderFrameHostManager::Delegate
* manager_delegate
)
103 : render_frame_delegate_(render_frame_delegate
),
104 render_view_delegate_(render_view_delegate
),
105 render_widget_delegate_(render_widget_delegate
),
106 manager_delegate_(manager_delegate
),
107 root_(new FrameTreeNode(this,
109 render_frame_delegate
,
110 render_view_delegate
,
111 render_widget_delegate
,
113 // The top-level frame must always be in a
115 blink::WebTreeScopeType::Document
,
117 blink::WebSandboxFlags::None
)),
118 focused_frame_tree_node_id_(-1),
119 load_progress_(0.0) {
122 FrameTree::~FrameTree() {
125 FrameTreeNode
* FrameTree::FindByID(int frame_tree_node_id
) {
126 FrameTreeNode
* node
= nullptr;
127 ForEach(base::Bind(&FrameTreeNodeForId
, frame_tree_node_id
, &node
));
131 FrameTreeNode
* FrameTree::FindByRoutingID(int process_id
, int routing_id
) {
132 RenderFrameHostImpl
* render_frame_host
=
133 RenderFrameHostImpl::FromID(process_id
, routing_id
);
134 if (render_frame_host
) {
135 FrameTreeNode
* result
= render_frame_host
->frame_tree_node();
136 if (this == result
->frame_tree())
140 RenderFrameProxyHost
* render_frame_proxy_host
=
141 RenderFrameProxyHost::FromID(process_id
, routing_id
);
142 if (render_frame_proxy_host
) {
143 FrameTreeNode
* result
= render_frame_proxy_host
->frame_tree_node();
144 if (this == result
->frame_tree())
151 FrameTreeNode
* FrameTree::FindByName(const std::string
& name
) {
155 FrameTreeNode
* node
= nullptr;
156 ForEach(base::Bind(&FrameTreeNodeForName
, name
, &node
));
160 void FrameTree::ForEach(
161 const base::Callback
<bool(FrameTreeNode
*)>& on_node
) const {
162 ForEach(on_node
, nullptr);
165 void FrameTree::ForEach(
166 const base::Callback
<bool(FrameTreeNode
*)>& on_node
,
167 FrameTreeNode
* skip_this_subtree
) const {
168 std::queue
<FrameTreeNode
*> queue
;
169 queue
.push(root_
.get());
171 while (!queue
.empty()) {
172 FrameTreeNode
* node
= queue
.front();
174 if (skip_this_subtree
== node
)
177 if (!on_node
.Run(node
))
180 for (size_t i
= 0; i
< node
->child_count(); ++i
)
181 queue
.push(node
->child_at(i
));
185 RenderFrameHostImpl
* FrameTree::AddFrame(FrameTreeNode
* parent
,
188 blink::WebTreeScopeType scope
,
189 const std::string
& frame_name
,
190 blink::WebSandboxFlags sandbox_flags
) {
191 // A child frame always starts with an initial empty document, which means
192 // it is in the same SiteInstance as the parent frame. Ensure that the process
193 // which requested a child frame to be added is the same as the process of the
195 // We return nullptr if this is not the case, which can happen in a race if an
196 // old RFH sends a CreateChildFrame message as we're swapping to a new RFH.
197 if (parent
->current_frame_host()->GetProcess()->GetID() != process_id
)
200 scoped_ptr
<FrameTreeNode
> node(
201 new FrameTreeNode(this, parent
->navigator(), render_frame_delegate_
,
202 render_view_delegate_
, render_widget_delegate_
,
203 manager_delegate_
, scope
, frame_name
, sandbox_flags
));
204 FrameTreeNode
* node_ptr
= node
.get();
205 // AddChild is what creates the RenderFrameHost.
206 parent
->AddChild(node
.Pass(), process_id
, new_routing_id
);
207 return node_ptr
->current_frame_host();
210 void FrameTree::RemoveFrame(FrameTreeNode
* child
) {
211 FrameTreeNode
* parent
= child
->parent();
213 NOTREACHED() << "Unexpected RemoveFrame call for main frame.";
217 parent
->RemoveChild(child
);
220 void FrameTree::CreateProxiesForSiteInstance(
221 FrameTreeNode
* source
,
222 SiteInstance
* site_instance
) {
223 // Create the swapped out RVH for the new SiteInstance. This will create
224 // a top-level swapped out RFH as well, which will then be wrapped by a
225 // RenderFrameProxyHost.
226 if (!source
|| !source
->IsMainFrame()) {
227 RenderViewHostImpl
* render_view_host
= GetRenderViewHost(site_instance
);
228 if (!render_view_host
) {
229 if (SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
230 root()->render_manager()->CreateRenderFrameProxy(site_instance
);
232 root()->render_manager()->CreateRenderFrame(
233 site_instance
, nullptr, CREATE_RF_SWAPPED_OUT
| CREATE_RF_HIDDEN
,
237 root()->render_manager()->EnsureRenderViewInitialized(render_view_host
,
242 scoped_refptr
<SiteInstance
> instance(site_instance
);
244 // Proxies are created in the FrameTree in response to a node navigating to a
245 // new SiteInstance. Since |source|'s navigation will replace the currently
246 // loaded document, the entire subtree under |source| will be removed.
247 ForEach(base::Bind(&CreateProxyForSiteInstance
, instance
), source
);
250 RenderFrameHostImpl
* FrameTree::GetMainFrame() const {
251 return root_
->current_frame_host();
254 FrameTreeNode
* FrameTree::GetFocusedFrame() {
255 return FindByID(focused_frame_tree_node_id_
);
258 void FrameTree::SetFocusedFrame(FrameTreeNode
* node
) {
259 focused_frame_tree_node_id_
= node
->frame_tree_node_id();
262 void FrameTree::SetFrameRemoveListener(
263 const base::Callback
<void(RenderFrameHost
*)>& on_frame_removed
) {
264 on_frame_removed_
= on_frame_removed
;
267 RenderViewHostImpl
* FrameTree::CreateRenderViewHost(SiteInstance
* site_instance
,
269 int main_frame_routing_id
,
272 RenderViewHostMap::iterator iter
=
273 render_view_host_map_
.find(site_instance
->GetId());
274 if (iter
!= render_view_host_map_
.end()) {
275 // If a RenderViewHost's main frame is pending deletion for this
276 // |site_instance|, put it in the map of RenderViewHosts pending shutdown.
277 // Otherwise return the existing RenderViewHost for the SiteInstance.
278 RenderFrameHostImpl
* main_frame
= static_cast<RenderFrameHostImpl
*>(
279 iter
->second
->GetMainFrame());
281 main_frame
->frame_tree_node()->render_manager()->IsPendingDeletion(
283 render_view_host_pending_shutdown_map_
.insert(
284 std::pair
<int, RenderViewHostImpl
*>(site_instance
->GetId(),
286 render_view_host_map_
.erase(iter
);
291 RenderViewHostImpl
* rvh
= static_cast<RenderViewHostImpl
*>(
292 RenderViewHostFactory::Create(site_instance
,
293 render_view_delegate_
,
294 render_widget_delegate_
,
296 main_frame_routing_id
,
300 render_view_host_map_
[site_instance
->GetId()] = rvh
;
304 RenderViewHostImpl
* FrameTree::GetRenderViewHost(SiteInstance
* site_instance
) {
305 RenderViewHostMap::iterator iter
=
306 render_view_host_map_
.find(site_instance
->GetId());
307 if (iter
== render_view_host_map_
.end())
312 void FrameTree::AddRenderViewHostRef(RenderViewHostImpl
* render_view_host
) {
313 SiteInstance
* site_instance
= render_view_host
->GetSiteInstance();
314 RenderViewHostMap::iterator iter
=
315 render_view_host_map_
.find(site_instance
->GetId());
316 CHECK(iter
!= render_view_host_map_
.end());
317 CHECK(iter
->second
== render_view_host
);
319 iter
->second
->increment_ref_count();
322 void FrameTree::ReleaseRenderViewHostRef(RenderViewHostImpl
* render_view_host
) {
323 SiteInstance
* site_instance
= render_view_host
->GetSiteInstance();
324 int32 site_instance_id
= site_instance
->GetId();
325 RenderViewHostMap::iterator iter
=
326 render_view_host_map_
.find(site_instance_id
);
327 if (iter
!= render_view_host_map_
.end() && iter
->second
== render_view_host
) {
328 // Decrement the refcount and shutdown the RenderViewHost if no one else is
330 CHECK_GT(iter
->second
->ref_count(), 0);
331 iter
->second
->decrement_ref_count();
332 if (iter
->second
->ref_count() == 0) {
333 iter
->second
->Shutdown();
334 render_view_host_map_
.erase(iter
);
337 // The RenderViewHost should be in the list of RenderViewHosts pending
339 bool render_view_host_found
= false;
340 std::pair
<RenderViewHostMultiMap::iterator
,
341 RenderViewHostMultiMap::iterator
> result
=
342 render_view_host_pending_shutdown_map_
.equal_range(site_instance_id
);
343 for (RenderViewHostMultiMap::iterator multi_iter
= result
.first
;
344 multi_iter
!= result
.second
;
346 if (multi_iter
->second
!= render_view_host
)
348 render_view_host_found
= true;
349 // Decrement the refcount and shutdown the RenderViewHost if no one else
351 CHECK_GT(render_view_host
->ref_count(), 0);
352 render_view_host
->decrement_ref_count();
353 if (render_view_host
->ref_count() == 0) {
354 render_view_host
->Shutdown();
355 render_view_host_pending_shutdown_map_
.erase(multi_iter
);
359 CHECK(render_view_host_found
);
363 void FrameTree::FrameRemoved(FrameTreeNode
* frame
) {
364 if (frame
->frame_tree_node_id() == focused_frame_tree_node_id_
)
365 focused_frame_tree_node_id_
= -1;
367 // No notification for the root frame.
368 if (!frame
->parent()) {
369 CHECK_EQ(frame
, root_
.get());
373 // Notify observers of the frame removal.
374 if (!on_frame_removed_
.is_null())
375 on_frame_removed_
.Run(frame
->current_frame_host());
378 void FrameTree::UpdateLoadProgress() {
379 double progress
= 0.0;
382 ForEach(base::Bind(&CollectLoadProgress
, &progress
, &frame_count
));
383 if (frame_count
!= 0)
384 progress
/= frame_count
;
386 if (progress
<= load_progress_
)
388 load_progress_
= progress
;
390 // Notify the WebContents.
391 root_
->navigator()->GetDelegate()->DidChangeLoadProgress();
394 void FrameTree::ResetLoadProgress() {
395 ForEach(base::Bind(&ResetNodeLoadProgress
));
396 load_progress_
= 0.0;
399 bool FrameTree::IsLoading() {
400 bool is_loading
= false;
401 ForEach(base::Bind(&IsNodeLoading
, &is_loading
));
405 } // namespace content