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/render_frame_host_manager.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "base/stl_util.h"
13 #include "base/trace_event/trace_event.h"
14 #include "content/browser/child_process_security_policy_impl.h"
15 #include "content/browser/devtools/render_frame_devtools_agent_host.h"
16 #include "content/browser/frame_host/cross_site_transferring_request.h"
17 #include "content/browser/frame_host/debug_urls.h"
18 #include "content/browser/frame_host/frame_navigation_entry.h"
19 #include "content/browser/frame_host/interstitial_page_impl.h"
20 #include "content/browser/frame_host/navigation_controller_impl.h"
21 #include "content/browser/frame_host/navigation_entry_impl.h"
22 #include "content/browser/frame_host/navigation_handle_impl.h"
23 #include "content/browser/frame_host/navigation_request.h"
24 #include "content/browser/frame_host/navigator.h"
25 #include "content/browser/frame_host/render_frame_host_factory.h"
26 #include "content/browser/frame_host/render_frame_host_impl.h"
27 #include "content/browser/frame_host/render_frame_proxy_host.h"
28 #include "content/browser/gpu/gpu_surface_tracker.h"
29 #include "content/browser/renderer_host/render_process_host_impl.h"
30 #include "content/browser/renderer_host/render_view_host_factory.h"
31 #include "content/browser/renderer_host/render_view_host_impl.h"
32 #include "content/browser/site_instance_impl.h"
33 #include "content/browser/webui/web_ui_controller_factory_registry.h"
34 #include "content/browser/webui/web_ui_impl.h"
35 #include "content/common/frame_messages.h"
36 #include "content/common/site_isolation_policy.h"
37 #include "content/common/view_messages.h"
38 #include "content/public/browser/content_browser_client.h"
39 #include "content/public/browser/render_process_host_observer.h"
40 #include "content/public/browser/render_widget_host_iterator.h"
41 #include "content/public/browser/render_widget_host_view.h"
42 #include "content/public/browser/user_metrics.h"
43 #include "content/public/browser/web_ui_controller.h"
44 #include "content/public/common/browser_plugin_guest_mode.h"
45 #include "content/public/common/content_switches.h"
46 #include "content/public/common/referrer.h"
47 #include "content/public/common/url_constants.h"
53 // Helper function to add the FrameTree of the given node's opener to the list
54 // of |opener_trees|, if it doesn't exist there already. |visited_index|
55 // indicates which FrameTrees in |opener_trees| have already been visited
56 // (i.e., those at indices less than |visited_index|). |nodes_with_back_links|
57 // collects FrameTreeNodes with openers in FrameTrees that have already been
58 // visited (such as those with cycles). This function is intended to be used
59 // with FrameTree::ForEach, so it always returns true to visit all nodes in the
61 bool OpenerForFrameTreeNode(
63 std::vector
<FrameTree
*>* opener_trees
,
64 base::hash_set
<FrameTreeNode
*>* nodes_with_back_links
,
65 FrameTreeNode
* node
) {
69 FrameTree
* opener_tree
= node
->opener()->frame_tree();
71 const auto& existing_tree_it
=
72 std::find(opener_trees
->begin(), opener_trees
->end(), opener_tree
);
73 if (existing_tree_it
== opener_trees
->end()) {
74 // This is a new opener tree that we will need to process.
75 opener_trees
->push_back(opener_tree
);
77 // If this tree is already on our processing list *and* we have visited it,
78 // then this node's opener is a back link. This means the node will need
79 // special treatment to process its opener.
80 size_t position
= std::distance(opener_trees
->begin(), existing_tree_it
);
81 if (position
< visited_index
)
82 nodes_with_back_links
->insert(node
);
89 // A helper class to hold all frame proxies and register as a
90 // RenderProcessHostObserver for them.
91 class RenderFrameHostManager::RenderFrameProxyHostMap
92 : public RenderProcessHostObserver
{
94 using MapType
= base::hash_map
<int32
, RenderFrameProxyHost
*>;
96 RenderFrameProxyHostMap(RenderFrameHostManager
* manager
);
97 ~RenderFrameProxyHostMap() override
;
99 // Read-only access to the underlying map of site instance ID to
100 // RenderFrameProxyHosts.
101 MapType::const_iterator
begin() const { return map_
.begin(); }
102 MapType::const_iterator
end() const { return map_
.end(); }
103 bool empty() const { return map_
.empty(); }
105 // Returns the proxy with the specified ID, or nullptr if there is no such
107 RenderFrameProxyHost
* Get(int32 id
);
109 // Adds the specified proxy with the specified ID. It is an error (and fatal)
110 // to add more than one proxy with the specified ID.
111 void Add(int32 id
, scoped_ptr
<RenderFrameProxyHost
> proxy
);
113 // Removes the proxy with the specified site instance ID.
114 void Remove(int32 id
);
116 // Removes all proxies.
119 // RenderProcessHostObserver implementation.
120 void RenderProcessWillExit(RenderProcessHost
* host
) override
;
121 void RenderProcessExited(RenderProcessHost
* host
,
122 base::TerminationStatus status
,
123 int exit_code
) override
;
126 RenderFrameHostManager
* manager_
;
130 RenderFrameHostManager::RenderFrameProxyHostMap::RenderFrameProxyHostMap(
131 RenderFrameHostManager
* manager
)
132 : manager_(manager
) {}
134 RenderFrameHostManager::RenderFrameProxyHostMap::~RenderFrameProxyHostMap() {
138 RenderFrameProxyHost
* RenderFrameHostManager::RenderFrameProxyHostMap::Get(
140 auto it
= map_
.find(id
);
141 if (it
!= map_
.end())
146 void RenderFrameHostManager::RenderFrameProxyHostMap::Add(
148 scoped_ptr
<RenderFrameProxyHost
> proxy
) {
149 CHECK_EQ(0u, map_
.count(id
)) << "Inserting a duplicate item.";
151 // If this is the first proxy that has this process host, observe the
153 RenderProcessHost
* host
= proxy
->GetProcess();
155 std::count_if(begin(), end(), [host
](MapType::value_type item
) {
156 return item
.second
->GetProcess() == host
;
159 host
->AddObserver(this);
161 map_
[id
] = proxy
.release();
164 void RenderFrameHostManager::RenderFrameProxyHostMap::Remove(int32 id
) {
165 auto it
= map_
.find(id
);
166 if (it
== map_
.end())
169 // If this is the last proxy that has this process host, stop observing the
171 RenderProcessHost
* host
= it
->second
->GetProcess();
173 std::count_if(begin(), end(), [host
](MapType::value_type item
) {
174 return item
.second
->GetProcess() == host
;
177 host
->RemoveObserver(this);
183 void RenderFrameHostManager::RenderFrameProxyHostMap::Clear() {
184 std::set
<RenderProcessHost
*> hosts
;
185 for (const auto& pair
: map_
)
186 hosts
.insert(pair
.second
->GetProcess());
187 for (auto host
: hosts
)
188 host
->RemoveObserver(this);
190 STLDeleteValues(&map_
);
193 void RenderFrameHostManager::RenderFrameProxyHostMap::RenderProcessWillExit(
194 RenderProcessHost
* host
) {
195 manager_
->RendererProcessClosing(host
);
198 void RenderFrameHostManager::RenderFrameProxyHostMap::RenderProcessExited(
199 RenderProcessHost
* host
,
200 base::TerminationStatus status
,
202 manager_
->RendererProcessClosing(host
);
206 bool RenderFrameHostManager::ClearRFHsPendingShutdown(FrameTreeNode
* node
) {
207 node
->render_manager()->pending_delete_hosts_
.clear();
211 RenderFrameHostManager::RenderFrameHostManager(
212 FrameTreeNode
* frame_tree_node
,
213 RenderFrameHostDelegate
* render_frame_delegate
,
214 RenderViewHostDelegate
* render_view_delegate
,
215 RenderWidgetHostDelegate
* render_widget_delegate
,
217 : frame_tree_node_(frame_tree_node
),
219 render_frame_delegate_(render_frame_delegate
),
220 render_view_delegate_(render_view_delegate
),
221 render_widget_delegate_(render_widget_delegate
),
222 proxy_hosts_(new RenderFrameProxyHostMap(this)),
223 interstitial_page_(nullptr),
224 should_reuse_web_ui_(false),
225 weak_factory_(this) {
226 DCHECK(frame_tree_node_
);
229 RenderFrameHostManager::~RenderFrameHostManager() {
230 if (pending_render_frame_host_
) {
231 scoped_ptr
<RenderFrameHostImpl
> relic
= UnsetPendingRenderFrameHost();
232 ShutdownProxiesIfLastActiveFrameInSiteInstance(relic
.get());
235 if (speculative_render_frame_host_
) {
236 scoped_ptr
<RenderFrameHostImpl
> relic
= UnsetSpeculativeRenderFrameHost();
237 ShutdownProxiesIfLastActiveFrameInSiteInstance(relic
.get());
240 ShutdownProxiesIfLastActiveFrameInSiteInstance(render_frame_host_
.get());
242 // Delete any RenderFrameProxyHosts and swapped out RenderFrameHosts.
243 // It is important to delete those prior to deleting the current
244 // RenderFrameHost, since the CrossProcessFrameConnector (owned by
245 // RenderFrameProxyHost) points to the RenderWidgetHostView associated with
246 // the current RenderFrameHost and uses it during its destructor.
249 // Release the WebUI prior to resetting the current RenderFrameHost, as the
250 // WebUI accesses the RenderFrameHost during cleanup.
253 // We should always have a current RenderFrameHost except in some tests.
254 SetRenderFrameHost(scoped_ptr
<RenderFrameHostImpl
>());
257 void RenderFrameHostManager::Init(BrowserContext
* browser_context
,
258 SiteInstance
* site_instance
,
259 int32 view_routing_id
,
260 int32 frame_routing_id
,
261 int32 widget_routing_id
,
263 // Create a RenderViewHost and RenderFrameHost, once we have an instance. It
264 // is important to immediately give this SiteInstance to a RenderViewHost so
265 // that the SiteInstance is ref counted.
267 site_instance
= SiteInstance::Create(browser_context
);
269 int flags
= delegate_
->IsHidden() ? CREATE_RF_HIDDEN
: 0;
270 SetRenderFrameHost(CreateRenderFrameHost(site_instance
, view_routing_id
,
271 frame_routing_id
, widget_routing_id
,
274 // Notify the delegate of the creation of the current RenderFrameHost.
275 // Do this only for subframes, as the main frame case is taken care of by
276 // WebContentsImpl::Init.
277 if (!frame_tree_node_
->IsMainFrame()) {
278 delegate_
->NotifySwappedFromRenderManager(
279 nullptr, render_frame_host_
.get(), false);
283 RenderViewHostImpl
* RenderFrameHostManager::current_host() const {
284 if (!render_frame_host_
)
286 return render_frame_host_
->render_view_host();
289 RenderViewHostImpl
* RenderFrameHostManager::pending_render_view_host() const {
290 if (!pending_render_frame_host_
)
292 return pending_render_frame_host_
->render_view_host();
295 RenderWidgetHostView
* RenderFrameHostManager::GetRenderWidgetHostView() const {
296 if (interstitial_page_
)
297 return interstitial_page_
->GetView();
298 if (render_frame_host_
)
299 return render_frame_host_
->GetView();
303 bool RenderFrameHostManager::ForInnerDelegate() {
304 return delegate_
->GetOuterDelegateFrameTreeNodeID() !=
305 FrameTreeNode::kFrameTreeNodeInvalidID
;
308 RenderWidgetHostImpl
*
309 RenderFrameHostManager::GetOuterRenderWidgetHostForKeyboardInput() {
310 if (!ForInnerDelegate() || !frame_tree_node_
->IsMainFrame())
313 FrameTreeNode
* outer_contents_frame_tree_node
=
314 FrameTreeNode::GloballyFindByID(
315 delegate_
->GetOuterDelegateFrameTreeNodeID());
316 return outer_contents_frame_tree_node
->parent()
317 ->current_frame_host()
318 ->render_view_host();
321 RenderFrameProxyHost
* RenderFrameHostManager::GetProxyToParent() {
322 if (frame_tree_node_
->IsMainFrame())
325 return proxy_hosts_
->Get(frame_tree_node_
->parent()
327 ->current_frame_host()
332 RenderFrameProxyHost
* RenderFrameHostManager::GetProxyToOuterDelegate() {
333 int outer_contents_frame_tree_node_id
=
334 delegate_
->GetOuterDelegateFrameTreeNodeID();
335 FrameTreeNode
* outer_contents_frame_tree_node
=
336 FrameTreeNode::GloballyFindByID(outer_contents_frame_tree_node_id
);
337 if (!outer_contents_frame_tree_node
||
338 !outer_contents_frame_tree_node
->parent()) {
342 return GetRenderFrameProxyHost(outer_contents_frame_tree_node
->parent()
343 ->current_frame_host()
344 ->GetSiteInstance());
347 void RenderFrameHostManager::RemoveOuterDelegateFrame() {
348 FrameTreeNode
* outer_delegate_frame_tree_node
=
349 FrameTreeNode::GloballyFindByID(
350 delegate_
->GetOuterDelegateFrameTreeNodeID());
351 DCHECK(outer_delegate_frame_tree_node
->parent());
352 outer_delegate_frame_tree_node
->frame_tree()->RemoveFrame(
353 outer_delegate_frame_tree_node
);
356 void RenderFrameHostManager::SetPendingWebUI(const GURL
& url
, int bindings
) {
357 pending_web_ui_
= CreateWebUI(url
, bindings
);
358 pending_and_current_web_ui_
.reset();
361 scoped_ptr
<WebUIImpl
> RenderFrameHostManager::CreateWebUI(const GURL
& url
,
363 scoped_ptr
<WebUIImpl
> new_web_ui(delegate_
->CreateWebUIForRenderManager(url
));
365 // If we have assigned (zero or more) bindings to this NavigationEntry in the
366 // past, make sure we're not granting it different bindings than it had
367 // before. If so, note it and don't give it any bindings, to avoid a
368 // potential privilege escalation.
369 if (new_web_ui
&& bindings
!= NavigationEntryImpl::kInvalidBindings
&&
370 new_web_ui
->GetBindings() != bindings
) {
371 RecordAction(base::UserMetricsAction("ProcessSwapBindingsMismatch_RVHM"));
374 return new_web_ui
.Pass();
377 RenderFrameHostImpl
* RenderFrameHostManager::Navigate(
378 const GURL
& dest_url
,
379 const FrameNavigationEntry
& frame_entry
,
380 const NavigationEntryImpl
& entry
) {
381 TRACE_EVENT1("navigation", "RenderFrameHostManager:Navigate",
382 "FrameTreeNode id", frame_tree_node_
->frame_tree_node_id());
383 // Create a pending RenderFrameHost to use for the navigation.
384 RenderFrameHostImpl
* dest_render_frame_host
= UpdateStateForNavigate(
386 // TODO(creis): Move source_site_instance to FNE.
387 entry
.source_site_instance(), frame_entry
.site_instance(),
388 entry
.GetTransitionType(),
389 entry
.restore_type() != NavigationEntryImpl::RESTORE_NONE
,
390 entry
.IsViewSourceMode(), entry
.transferred_global_request_id(),
392 if (!dest_render_frame_host
)
393 return nullptr; // We weren't able to create a pending render frame host.
395 // If the current render_frame_host_ isn't live, we should create it so
396 // that we don't show a sad tab while the dest_render_frame_host fetches
397 // its first page. (Bug 1145340)
398 if (dest_render_frame_host
!= render_frame_host_
&&
399 !render_frame_host_
->IsRenderFrameLive()) {
400 // Note: we don't call InitRenderView here because we are navigating away
401 // soon anyway, and we don't have the NavigationEntry for this host.
402 delegate_
->CreateRenderViewForRenderManager(
403 render_frame_host_
->render_view_host(), MSG_ROUTING_NONE
,
404 MSG_ROUTING_NONE
, frame_tree_node_
->current_replication_state(),
405 frame_tree_node_
->IsMainFrame());
408 // If the renderer isn't live, then try to create a new one to satisfy this
409 // navigation request.
410 if (!dest_render_frame_host
->IsRenderFrameLive()) {
411 // Instruct the destination render frame host to set up a Mojo connection
412 // with the new render frame if necessary. Note that this call needs to
413 // occur before initializing the RenderView; the flow of creating the
414 // RenderView can cause browser-side code to execute that expects the this
415 // RFH's ServiceRegistry to be initialized (e.g., if the site is a WebUI
416 // site that is handled via Mojo, then Mojo WebUI code in //chrome will
417 // add a service to this RFH's ServiceRegistry).
418 dest_render_frame_host
->SetUpMojoIfNeeded();
420 // Recreate the opener chain.
421 CreateOpenerProxies(dest_render_frame_host
->GetSiteInstance(),
423 if (!InitRenderView(dest_render_frame_host
->render_view_host(),
425 frame_tree_node_
->IsMainFrame()))
428 // Now that we've created a new renderer, be sure to hide it if it isn't
429 // our primary one. Otherwise, we might crash if we try to call Show()
431 if (dest_render_frame_host
!= render_frame_host_
) {
432 if (dest_render_frame_host
->GetView())
433 dest_render_frame_host
->GetView()->Hide();
435 // After a renderer crash we'd have marked the host as invisible, so we
436 // need to set the visibility of the new View to the correct value here
438 if (dest_render_frame_host
->GetView() &&
439 dest_render_frame_host
->render_view_host()->is_hidden() !=
440 delegate_
->IsHidden()) {
441 if (delegate_
->IsHidden()) {
442 dest_render_frame_host
->GetView()->Hide();
444 dest_render_frame_host
->GetView()->Show();
448 // TODO(nasko): This is a very ugly hack. The Chrome extensions process
449 // manager still uses NotificationService and expects to see a
450 // RenderViewHost changed notification after WebContents and
451 // RenderFrameHostManager are completely initialized. This should be
452 // removed once the process manager moves away from NotificationService.
453 // See https://crbug.com/462682.
454 delegate_
->NotifyMainFrameSwappedFromRenderManager(
455 nullptr, render_frame_host_
->render_view_host());
459 // If entry includes the request ID of a request that is being transferred,
460 // the destination render frame will take ownership, so release ownership of
462 if (cross_site_transferring_request_
.get() &&
463 cross_site_transferring_request_
->request_id() ==
464 entry
.transferred_global_request_id()) {
465 cross_site_transferring_request_
->ReleaseRequest();
467 // The navigating RenderFrameHost should take ownership of the
468 // NavigationHandle that came from the transferring RenderFrameHost.
469 DCHECK(transfer_navigation_handle_
);
470 dest_render_frame_host
->SetNavigationHandle(
471 transfer_navigation_handle_
.Pass());
473 DCHECK(!transfer_navigation_handle_
);
475 return dest_render_frame_host
;
478 void RenderFrameHostManager::Stop() {
479 render_frame_host_
->Stop();
481 // If a cross-process navigation is happening, the pending RenderFrameHost
482 // should stop. This will lead to a DidFailProvisionalLoad, which will
483 // properly destroy it.
484 if (pending_render_frame_host_
) {
485 pending_render_frame_host_
->Send(new FrameMsg_Stop(
486 pending_render_frame_host_
->GetRoutingID()));
489 // PlzNavigate: a loading speculative RenderFrameHost should also stop.
490 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
491 switches::kEnableBrowserSideNavigation
)) {
492 if (speculative_render_frame_host_
&&
493 speculative_render_frame_host_
->is_loading()) {
494 speculative_render_frame_host_
->Send(
495 new FrameMsg_Stop(speculative_render_frame_host_
->GetRoutingID()));
500 void RenderFrameHostManager::SetIsLoading(bool is_loading
) {
501 render_frame_host_
->render_view_host()->SetIsLoading(is_loading
);
502 if (pending_render_frame_host_
)
503 pending_render_frame_host_
->render_view_host()->SetIsLoading(is_loading
);
506 bool RenderFrameHostManager::ShouldCloseTabOnUnresponsiveRenderer() {
507 // If we're waiting for a close ACK, then the tab should close whether there's
508 // a navigation in progress or not. Unfortunately, we also need to check for
509 // cases that we arrive here with no navigation in progress, since there are
510 // some tab closure paths that don't set is_waiting_for_close_ack to true.
511 // TODO(creis): Clean this up in http://crbug.com/418266.
512 if (!pending_render_frame_host_
||
513 render_frame_host_
->render_view_host()->is_waiting_for_close_ack())
516 // We should always have a pending RFH when there's a cross-process navigation
517 // in progress. Sanity check this for http://crbug.com/276333.
518 CHECK(pending_render_frame_host_
);
520 // Unload handlers run in the background, so we should never get an
521 // unresponsiveness warning for them.
522 CHECK(!render_frame_host_
->IsWaitingForUnloadACK());
524 // If the tab becomes unresponsive during beforeunload while doing a
525 // cross-process navigation, proceed with the navigation. (This assumes that
526 // the pending RenderFrameHost is still responsive.)
527 if (render_frame_host_
->is_waiting_for_beforeunload_ack()) {
528 // Haven't gotten around to starting the request, because we're still
529 // waiting for the beforeunload handler to finish. We'll pretend that it
530 // did finish, to let the navigation proceed. Note that there's a danger
531 // that the beforeunload handler will later finish and possibly return
532 // false (meaning the navigation should not proceed), but we'll ignore it
533 // in this case because it took too long.
534 if (pending_render_frame_host_
->are_navigations_suspended()) {
535 pending_render_frame_host_
->SetNavigationsSuspended(
536 false, base::TimeTicks::Now());
542 void RenderFrameHostManager::OnBeforeUnloadACK(
543 bool for_cross_site_transition
,
545 const base::TimeTicks
& proceed_time
) {
546 if (for_cross_site_transition
) {
547 DCHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch(
548 switches::kEnableBrowserSideNavigation
));
549 // Ignore if we're not in a cross-process navigation.
550 if (!pending_render_frame_host_
)
554 // Ok to unload the current page, so proceed with the cross-process
555 // navigation. Note that if navigations are not currently suspended, it
556 // might be because the renderer was deemed unresponsive and this call was
557 // already made by ShouldCloseTabOnUnresponsiveRenderer. In that case, it
558 // is ok to do nothing here.
559 if (pending_render_frame_host_
&&
560 pending_render_frame_host_
->are_navigations_suspended()) {
561 pending_render_frame_host_
->SetNavigationsSuspended(false,
565 // Current page says to cancel.
569 // Non-cross-process transition means closing the entire tab.
570 bool proceed_to_fire_unload
;
571 delegate_
->BeforeUnloadFiredFromRenderManager(proceed
, proceed_time
,
572 &proceed_to_fire_unload
);
574 if (proceed_to_fire_unload
) {
575 // If we're about to close the tab and there's a pending RFH, cancel it.
576 // Otherwise, if the navigation in the pending RFH completes before the
577 // close in the current RFH, we'll lose the tab close.
578 if (pending_render_frame_host_
) {
582 // PlzNavigate: clean up the speculative RenderFrameHost if there is one.
583 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
584 switches::kEnableBrowserSideNavigation
) &&
585 speculative_render_frame_host_
) {
589 // This is not a cross-process navigation; the tab is being closed.
590 render_frame_host_
->render_view_host()->ClosePage();
595 void RenderFrameHostManager::OnCrossSiteResponse(
596 RenderFrameHostImpl
* pending_render_frame_host
,
597 const GlobalRequestID
& global_request_id
,
598 scoped_ptr
<CrossSiteTransferringRequest
> cross_site_transferring_request
,
599 const std::vector
<GURL
>& transfer_url_chain
,
600 const Referrer
& referrer
,
601 ui::PageTransition page_transition
,
602 bool should_replace_current_entry
) {
603 // We should only get here for transfer navigations. Most cross-process
604 // navigations can just continue and wait to run the unload handler (by
605 // swapping out) when the new navigation commits.
606 CHECK(cross_site_transferring_request
);
608 // A transfer should only have come from our pending or current RFH.
609 // TODO(creis): We need to handle the case that the pending RFH has changed
610 // in the mean time, while this was being posted from the IO thread. We
611 // should probably cancel the request in that case.
612 DCHECK(pending_render_frame_host
== pending_render_frame_host_
||
613 pending_render_frame_host
== render_frame_host_
);
615 // Store the transferring request so that we can release it if the transfer
616 // navigation matches.
617 cross_site_transferring_request_
= cross_site_transferring_request
.Pass();
619 // Store the NavigationHandle to give it to the appropriate RenderFrameHost
620 // after it started navigating.
621 transfer_navigation_handle_
=
622 pending_render_frame_host
->PassNavigationHandleOwnership();
623 DCHECK(transfer_navigation_handle_
);
625 // Sanity check that the params are for the correct frame and process.
626 // These should match the RenderFrameHost that made the request.
627 // If it started as a cross-process navigation via OpenURL, this is the
628 // pending one. If it wasn't cross-process until the transfer, this is
630 int render_frame_id
= pending_render_frame_host_
631 ? pending_render_frame_host_
->GetRoutingID()
632 : render_frame_host_
->GetRoutingID();
633 DCHECK_EQ(render_frame_id
, pending_render_frame_host
->GetRoutingID());
634 int process_id
= pending_render_frame_host_
?
635 pending_render_frame_host_
->GetProcess()->GetID() :
636 render_frame_host_
->GetProcess()->GetID();
637 DCHECK_EQ(process_id
, global_request_id
.child_id
);
639 // Treat the last URL in the chain as the destination and the remainder as
640 // the redirect chain.
641 CHECK(transfer_url_chain
.size());
642 GURL transfer_url
= transfer_url_chain
.back();
643 std::vector
<GURL
> rest_of_chain
= transfer_url_chain
;
644 rest_of_chain
.pop_back();
646 // We don't know whether the original request had |user_action| set to true.
647 // However, since we force the navigation to be in the current tab, it
649 pending_render_frame_host
->frame_tree_node()->navigator()->RequestTransferURL(
650 pending_render_frame_host
, transfer_url
, nullptr, rest_of_chain
, referrer
,
651 page_transition
, CURRENT_TAB
, global_request_id
,
652 should_replace_current_entry
, true);
654 // The transferring request was only needed during the RequestTransferURL
655 // call, so it is safe to clear at this point.
656 cross_site_transferring_request_
.reset();
658 // If the navigation continued, the NavigationHandle should have been
659 // transfered to a RenderFrameHost. In the other cases, it should be cleared.
660 transfer_navigation_handle_
.reset();
663 void RenderFrameHostManager::DidNavigateFrame(
664 RenderFrameHostImpl
* render_frame_host
,
665 bool was_caused_by_user_gesture
) {
666 CommitPendingIfNecessary(render_frame_host
, was_caused_by_user_gesture
);
668 // Make sure any dynamic changes to this frame's sandbox flags that were made
669 // prior to navigation take effect.
670 CommitPendingSandboxFlags();
673 void RenderFrameHostManager::CommitPendingIfNecessary(
674 RenderFrameHostImpl
* render_frame_host
,
675 bool was_caused_by_user_gesture
) {
676 if (!pending_render_frame_host_
&& !speculative_render_frame_host_
) {
677 DCHECK_IMPLIES(should_reuse_web_ui_
, web_ui_
);
679 // We should only hear this from our current renderer.
680 DCHECK_EQ(render_frame_host_
, render_frame_host
);
682 // Even when there is no pending RVH, there may be a pending Web UI.
683 if (pending_web_ui() || speculative_web_ui_
)
688 if (render_frame_host
== pending_render_frame_host_
||
689 render_frame_host
== speculative_render_frame_host_
) {
690 // The pending cross-process navigation completed, so show the renderer.
692 } else if (render_frame_host
== render_frame_host_
) {
693 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
694 switches::kEnableBrowserSideNavigation
)) {
697 if (was_caused_by_user_gesture
) {
698 // A navigation in the original page has taken place. Cancel the
699 // pending one. Only do it for user gesture originated navigations to
700 // prevent page doing any shenanigans to prevent user from navigating.
701 // See https://code.google.com/p/chromium/issues/detail?id=75195
706 // No one else should be sending us DidNavigate in this state.
711 void RenderFrameHostManager::DidChangeOpener(
712 int opener_routing_id
,
713 SiteInstance
* source_site_instance
) {
714 FrameTreeNode
* opener
= nullptr;
715 if (opener_routing_id
!= MSG_ROUTING_NONE
) {
716 RenderFrameHostImpl
* opener_rfhi
= RenderFrameHostImpl::FromID(
717 source_site_instance
->GetProcess()->GetID(), opener_routing_id
);
718 // If |opener_rfhi| is null, the opener RFH has already disappeared. In
719 // this case, clear the opener rather than keeping the old opener around.
721 opener
= opener_rfhi
->frame_tree_node();
724 if (frame_tree_node_
->opener() == opener
)
727 frame_tree_node_
->SetOpener(opener
);
729 for (const auto& pair
: *proxy_hosts_
) {
730 if (pair
.second
->GetSiteInstance() == source_site_instance
)
732 pair
.second
->UpdateOpener();
735 if (render_frame_host_
->GetSiteInstance() != source_site_instance
)
736 render_frame_host_
->UpdateOpener();
738 // Notify the pending and speculative RenderFrameHosts as well. This is
739 // necessary in case a process swap has started while the message was in
741 if (pending_render_frame_host_
&&
742 pending_render_frame_host_
->GetSiteInstance() != source_site_instance
) {
743 pending_render_frame_host_
->UpdateOpener();
746 if (speculative_render_frame_host_
&&
747 speculative_render_frame_host_
->GetSiteInstance() !=
748 source_site_instance
) {
749 speculative_render_frame_host_
->UpdateOpener();
753 void RenderFrameHostManager::CommitPendingSandboxFlags() {
754 // Return early if there were no pending sandbox flags updates.
755 if (!frame_tree_node_
->CommitPendingSandboxFlags())
758 // Sandbox flags updates can only happen when the frame has a parent.
759 CHECK(frame_tree_node_
->parent());
761 // Notify all of the frame's proxies about updated sandbox flags, excluding
762 // the parent process since it already knows the latest flags.
763 SiteInstance
* parent_site_instance
=
764 frame_tree_node_
->parent()->current_frame_host()->GetSiteInstance();
765 for (const auto& pair
: *proxy_hosts_
) {
766 if (pair
.second
->GetSiteInstance() != parent_site_instance
) {
767 pair
.second
->Send(new FrameMsg_DidUpdateSandboxFlags(
768 pair
.second
->GetRoutingID(),
769 frame_tree_node_
->current_replication_state().sandbox_flags
));
774 void RenderFrameHostManager::RendererProcessClosing(
775 RenderProcessHost
* render_process_host
) {
776 // Remove any swapped out RVHs from this process, so that we don't try to
777 // swap them back in while the process is exiting. Start by finding them,
778 // since there could be more than one.
779 std::list
<int> ids_to_remove
;
780 // Do not remove proxies in the dead process that still have active frame
781 // count though, we just reset them to be uninitialized.
782 std::list
<int> ids_to_keep
;
783 for (const auto& pair
: *proxy_hosts_
) {
784 RenderFrameProxyHost
* proxy
= pair
.second
;
785 if (proxy
->GetProcess() != render_process_host
)
788 if (static_cast<SiteInstanceImpl
*>(proxy
->GetSiteInstance())
789 ->active_frame_count() >= 1U) {
790 ids_to_keep
.push_back(pair
.first
);
792 ids_to_remove
.push_back(pair
.first
);
797 while (!ids_to_remove
.empty()) {
798 proxy_hosts_
->Remove(ids_to_remove
.back());
799 ids_to_remove
.pop_back();
802 while (!ids_to_keep
.empty()) {
803 frame_tree_node_
->frame_tree()->ForEach(
805 &RenderFrameHostManager::ResetProxiesInSiteInstance
,
806 ids_to_keep
.back()));
807 ids_to_keep
.pop_back();
811 void RenderFrameHostManager::SwapOutOldFrame(
812 scoped_ptr
<RenderFrameHostImpl
> old_render_frame_host
) {
813 TRACE_EVENT1("navigation", "RenderFrameHostManager::SwapOutOldFrame",
814 "FrameTreeNode id", frame_tree_node_
->frame_tree_node_id());
816 // Tell the renderer to suppress any further modal dialogs so that we can swap
817 // it out. This must be done before canceling any current dialog, in case
818 // there is a loop creating additional dialogs.
819 // TODO(creis): Handle modal dialogs in subframe processes.
820 old_render_frame_host
->render_view_host()->SuppressDialogsUntilSwapOut();
822 // Now close any modal dialogs that would prevent us from swapping out. This
823 // must be done separately from SwapOut, so that the PageGroupLoadDeferrer is
824 // no longer on the stack when we send the SwapOut message.
825 delegate_
->CancelModalDialogsForRenderManager();
827 // If the old RFH is not live, just return as there is no further work to do.
828 // It will be deleted and there will be no proxy created.
829 int32 old_site_instance_id
=
830 old_render_frame_host
->GetSiteInstance()->GetId();
831 if (!old_render_frame_host
->IsRenderFrameLive()) {
832 ShutdownProxiesIfLastActiveFrameInSiteInstance(old_render_frame_host
.get());
836 // If there are no active frames besides this one, we can delete the old
837 // RenderFrameHost once it runs its unload handler, without replacing it with
839 size_t active_frame_count
=
840 old_render_frame_host
->GetSiteInstance()->active_frame_count();
841 if (active_frame_count
<= 1) {
842 // Clear out any proxies from this SiteInstance, in case this was the
843 // last one keeping other proxies alive.
844 ShutdownProxiesIfLastActiveFrameInSiteInstance(old_render_frame_host
.get());
846 // Tell the old RenderFrameHost to swap out, with no proxy to replace it.
847 old_render_frame_host
->SwapOut(nullptr, true);
848 MoveToPendingDeleteHosts(old_render_frame_host
.Pass());
852 // Otherwise there are active views and we need a proxy for the old RFH.
853 // (There should not be one yet.)
854 RenderFrameProxyHost
* proxy
= new RenderFrameProxyHost(
855 old_render_frame_host
->GetSiteInstance(),
856 old_render_frame_host
->render_view_host(), frame_tree_node_
);
857 proxy_hosts_
->Add(old_site_instance_id
, make_scoped_ptr(proxy
));
859 // Tell the old RenderFrameHost to swap out and be replaced by the proxy.
860 old_render_frame_host
->SwapOut(proxy
, true);
862 // SwapOut creates a RenderFrameProxy, so set the proxy to be initialized.
863 proxy
->set_render_frame_proxy_created(true);
865 if (SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
866 // In --site-per-process, frames delete their RFH rather than storing it
867 // in the proxy. Schedule it for deletion once the SwapOutACK comes in.
868 // TODO(creis): This will be the default when we remove swappedout://.
869 MoveToPendingDeleteHosts(old_render_frame_host
.Pass());
871 // We shouldn't get here for subframes, since we only swap subframes when
872 // --site-per-process is used.
873 DCHECK(frame_tree_node_
->IsMainFrame());
875 // The old RenderFrameHost will stay alive inside the proxy so that existing
876 // JavaScript window references to it stay valid.
877 proxy
->TakeFrameHostOwnership(old_render_frame_host
.Pass());
881 void RenderFrameHostManager::DiscardUnusedFrame(
882 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
) {
883 // TODO(carlosk): this code is very similar to what can be found in
884 // SwapOutOldFrame and we should see that these are unified at some point.
886 // If the SiteInstance for the pending RFH is being used by others don't
887 // delete the RFH. Just swap it out and it can be reused at a later point.
888 // In --site-per-process, RenderFrameHosts are not kept around and are
889 // deleted when not used, replaced by RenderFrameProxyHosts.
890 SiteInstanceImpl
* site_instance
= render_frame_host
->GetSiteInstance();
891 if (site_instance
->HasSite() && site_instance
->active_frame_count() > 1) {
892 // Any currently suspended navigations are no longer needed.
893 render_frame_host
->CancelSuspendedNavigations();
895 RenderFrameProxyHost
* proxy
= new RenderFrameProxyHost(
896 site_instance
, render_frame_host
->render_view_host(), frame_tree_node_
);
897 proxy_hosts_
->Add(site_instance
->GetId(), make_scoped_ptr(proxy
));
899 // Check if the RenderFrameHost is already swapped out, to avoid swapping it
901 if (!render_frame_host
->is_swapped_out())
902 render_frame_host
->SwapOut(proxy
, false);
904 if (!SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
905 DCHECK(frame_tree_node_
->IsMainFrame());
906 proxy
->TakeFrameHostOwnership(render_frame_host
.Pass());
910 if (render_frame_host
) {
911 // We won't be coming back, so delete this one.
912 ShutdownProxiesIfLastActiveFrameInSiteInstance(render_frame_host
.get());
913 render_frame_host
.reset();
917 void RenderFrameHostManager::MoveToPendingDeleteHosts(
918 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
) {
919 // |render_frame_host| will be deleted when its SwapOut ACK is received, or
920 // when the timer times out, or when the RFHM itself is deleted (whichever
922 pending_delete_hosts_
.push_back(
923 linked_ptr
<RenderFrameHostImpl
>(render_frame_host
.release()));
926 bool RenderFrameHostManager::IsPendingDeletion(
927 RenderFrameHostImpl
* render_frame_host
) {
928 for (const auto& rfh
: pending_delete_hosts_
) {
929 if (rfh
== render_frame_host
)
935 bool RenderFrameHostManager::DeleteFromPendingList(
936 RenderFrameHostImpl
* render_frame_host
) {
937 for (RFHPendingDeleteList::iterator iter
= pending_delete_hosts_
.begin();
938 iter
!= pending_delete_hosts_
.end();
940 if (*iter
== render_frame_host
) {
941 pending_delete_hosts_
.erase(iter
);
948 void RenderFrameHostManager::ResetProxyHosts() {
949 proxy_hosts_
->Clear();
953 void RenderFrameHostManager::DidCreateNavigationRequest(
954 const NavigationRequest
& request
) {
955 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
956 switches::kEnableBrowserSideNavigation
));
957 // Clean up any state in case there's an ongoing navigation.
958 // TODO(carlosk): remove this cleanup here once we properly cancel ongoing
962 RenderFrameHostImpl
* dest_rfh
= GetFrameHostForNavigation(request
);
967 RenderFrameHostImpl
* RenderFrameHostManager::GetFrameHostForNavigation(
968 const NavigationRequest
& request
) {
969 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
970 switches::kEnableBrowserSideNavigation
));
972 SiteInstance
* current_site_instance
= render_frame_host_
->GetSiteInstance();
974 SiteInstance
* candidate_site_instance
=
975 speculative_render_frame_host_
976 ? speculative_render_frame_host_
->GetSiteInstance()
979 scoped_refptr
<SiteInstance
> dest_site_instance
= GetSiteInstanceForNavigation(
980 request
.common_params().url
, request
.source_site_instance(),
981 request
.dest_site_instance(), candidate_site_instance
,
982 request
.common_params().transition
,
983 request
.restore_type() != NavigationEntryImpl::RESTORE_NONE
,
984 request
.is_view_source());
986 // The appropriate RenderFrameHost to commit the navigation.
987 RenderFrameHostImpl
* navigation_rfh
= nullptr;
989 // Renderer-initiated main frame navigations that may require a SiteInstance
990 // swap are sent to the browser via the OpenURL IPC and are afterwards treated
991 // as browser-initiated navigations. NavigationRequests marked as
992 // renderer-initiated are created by receiving a BeginNavigation IPC, and will
993 // then proceed in the same renderer that sent the IPC due to the condition
995 // Subframe navigations will use the current renderer, unless
996 // --site-per-process is enabled.
997 // TODO(carlosk): Have renderer-initated main frame navigations swap processes
998 // if needed when it no longer breaks OAuth popups (see
999 // https://crbug.com/440266).
1000 bool is_main_frame
= frame_tree_node_
->IsMainFrame();
1001 if (current_site_instance
== dest_site_instance
.get() ||
1002 (!request
.browser_initiated() && is_main_frame
) ||
1003 (!is_main_frame
&& !dest_site_instance
->RequiresDedicatedProcess() &&
1004 !current_site_instance
->RequiresDedicatedProcess())) {
1005 // Reuse the current RFH if its SiteInstance matches the the navigation's
1006 // or if this is a subframe navigation. We only swap RFHs for subframes when
1007 // --site-per-process is enabled.
1008 CleanUpNavigation();
1009 navigation_rfh
= render_frame_host_
.get();
1011 // As SiteInstances are the same, check if the WebUI should be reused.
1012 const NavigationEntry
* current_navigation_entry
=
1013 delegate_
->GetLastCommittedNavigationEntryForRenderManager();
1014 should_reuse_web_ui_
= ShouldReuseWebUI(current_navigation_entry
,
1015 request
.common_params().url
);
1016 if (!should_reuse_web_ui_
) {
1017 speculative_web_ui_
= CreateWebUI(request
.common_params().url
,
1018 request
.bindings());
1019 // Make sure the current RenderViewHost has the right bindings.
1020 if (speculative_web_ui() &&
1021 !render_frame_host_
->GetProcess()->IsForGuestsOnly()) {
1022 render_frame_host_
->render_view_host()->AllowBindings(
1023 speculative_web_ui()->GetBindings());
1027 // If the SiteInstance for the final URL doesn't match the one from the
1028 // speculatively created RenderFrameHost, create a new RenderFrameHost using
1029 // this new SiteInstance.
1030 if (!speculative_render_frame_host_
||
1031 speculative_render_frame_host_
->GetSiteInstance() !=
1032 dest_site_instance
.get()) {
1033 CleanUpNavigation();
1034 bool success
= CreateSpeculativeRenderFrameHost(
1035 request
.common_params().url
, current_site_instance
,
1036 dest_site_instance
.get(), request
.bindings());
1039 DCHECK(speculative_render_frame_host_
);
1040 navigation_rfh
= speculative_render_frame_host_
.get();
1042 // Check if our current RFH is live.
1043 if (!render_frame_host_
->IsRenderFrameLive()) {
1044 // The current RFH is not live. There's no reason to sit around with a
1045 // sad tab or a newly created RFH while we wait for the navigation to
1046 // complete. Just switch to the speculative RFH now and go back to normal.
1047 // (Note that we don't care about on{before}unload handlers if the current
1052 DCHECK(navigation_rfh
&&
1053 (navigation_rfh
== render_frame_host_
.get() ||
1054 navigation_rfh
== speculative_render_frame_host_
.get()));
1056 // If the RenderFrame that needs to navigate is not live (its process was just
1057 // created or has crashed), initialize it.
1058 if (!navigation_rfh
->IsRenderFrameLive()) {
1059 // Recreate the opener chain.
1060 CreateOpenerProxies(navigation_rfh
->GetSiteInstance(), frame_tree_node_
);
1061 if (!InitRenderView(navigation_rfh
->render_view_host(), MSG_ROUTING_NONE
,
1062 frame_tree_node_
->IsMainFrame())) {
1066 if (navigation_rfh
== render_frame_host_
) {
1067 // TODO(nasko): This is a very ugly hack. The Chrome extensions process
1068 // manager still uses NotificationService and expects to see a
1069 // RenderViewHost changed notification after WebContents and
1070 // RenderFrameHostManager are completely initialized. This should be
1071 // removed once the process manager moves away from NotificationService.
1072 // See https://crbug.com/462682.
1073 delegate_
->NotifyMainFrameSwappedFromRenderManager(
1074 nullptr, render_frame_host_
->render_view_host());
1078 return navigation_rfh
;
1082 void RenderFrameHostManager::CleanUpNavigation() {
1083 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
1084 switches::kEnableBrowserSideNavigation
));
1085 speculative_web_ui_
.reset();
1086 should_reuse_web_ui_
= false;
1087 if (speculative_render_frame_host_
)
1088 DiscardUnusedFrame(UnsetSpeculativeRenderFrameHost());
1092 scoped_ptr
<RenderFrameHostImpl
>
1093 RenderFrameHostManager::UnsetSpeculativeRenderFrameHost() {
1094 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
1095 switches::kEnableBrowserSideNavigation
));
1096 speculative_render_frame_host_
->GetProcess()->RemovePendingView();
1097 return speculative_render_frame_host_
.Pass();
1100 void RenderFrameHostManager::OnDidStartLoading() {
1101 for (const auto& pair
: *proxy_hosts_
) {
1103 new FrameMsg_DidStartLoading(pair
.second
->GetRoutingID()));
1107 void RenderFrameHostManager::OnDidStopLoading() {
1108 for (const auto& pair
: *proxy_hosts_
) {
1109 pair
.second
->Send(new FrameMsg_DidStopLoading(pair
.second
->GetRoutingID()));
1113 void RenderFrameHostManager::OnDidUpdateName(const std::string
& name
) {
1114 // The window.name message may be sent outside of --site-per-process when
1115 // report_frame_name_changes renderer preference is set (used by
1116 // WebView). Don't send the update to proxies in those cases.
1117 // TODO(nick,nasko): Should this be IsSwappedOutStateForbidden, to match
1118 // OnDidUpdateOrigin?
1119 if (!SiteIsolationPolicy::AreCrossProcessFramesPossible())
1122 for (const auto& pair
: *proxy_hosts_
) {
1124 new FrameMsg_DidUpdateName(pair
.second
->GetRoutingID(), name
));
1128 void RenderFrameHostManager::OnDidUpdateOrigin(const url::Origin
& origin
) {
1129 if (!SiteIsolationPolicy::IsSwappedOutStateForbidden())
1132 for (const auto& pair
: *proxy_hosts_
) {
1134 new FrameMsg_DidUpdateOrigin(pair
.second
->GetRoutingID(), origin
));
1138 RenderFrameHostManager::SiteInstanceDescriptor::SiteInstanceDescriptor(
1139 BrowserContext
* browser_context
,
1141 bool related_to_current
)
1142 : existing_site_instance(nullptr),
1143 new_is_related_to_current(related_to_current
) {
1144 new_site_url
= SiteInstance::GetSiteForURL(browser_context
, dest_url
);
1148 bool RenderFrameHostManager::ClearProxiesInSiteInstance(
1149 int32 site_instance_id
,
1150 FrameTreeNode
* node
) {
1151 RenderFrameProxyHost
* proxy
=
1152 node
->render_manager()->proxy_hosts_
->Get(site_instance_id
);
1154 // Delete the proxy. If it is for a main frame (and thus the RFH is stored
1155 // in the proxy) and it was still pending swap out, move the RFH to the
1156 // pending deletion list first.
1157 if (node
->IsMainFrame() &&
1158 proxy
->render_frame_host() &&
1159 proxy
->render_frame_host()->rfh_state() ==
1160 RenderFrameHostImpl::STATE_PENDING_SWAP_OUT
) {
1161 DCHECK(!SiteIsolationPolicy::IsSwappedOutStateForbidden());
1162 scoped_ptr
<RenderFrameHostImpl
> swapped_out_rfh
=
1163 proxy
->PassFrameHostOwnership();
1164 node
->render_manager()->MoveToPendingDeleteHosts(swapped_out_rfh
.Pass());
1166 node
->render_manager()->proxy_hosts_
->Remove(site_instance_id
);
1173 bool RenderFrameHostManager::ResetProxiesInSiteInstance(int32 site_instance_id
,
1174 FrameTreeNode
* node
) {
1175 RenderFrameProxyHost
* proxy
=
1176 node
->render_manager()->proxy_hosts_
->Get(site_instance_id
);
1178 proxy
->set_render_frame_proxy_created(false);
1183 bool RenderFrameHostManager::ShouldTransitionCrossSite() {
1184 // The logic below is weaker than "are all sites isolated" -- it asks instead,
1185 // "is any site isolated". That's appropriate here since we're just trying to
1186 // figure out if we're in any kind of site isolated mode, and in which case,
1187 // we ignore the kSingleProcess and kProcessPerTab settings.
1189 // TODO(nick): Move all handling of kSingleProcess/kProcessPerTab into
1190 // SiteIsolationPolicy so we have a consistent behavior around the interaction
1191 // of the process model flags.
1192 if (SiteIsolationPolicy::AreCrossProcessFramesPossible())
1195 // False in the single-process mode, as it makes RVHs to accumulate
1196 // in swapped_out_hosts_.
1197 // True if we are using process-per-site-instance (default) or
1198 // process-per-site (kProcessPerSite).
1199 // TODO(nick): Move handling of kSingleProcess and kProcessPerTab into
1200 // SiteIsolationPolicy.
1201 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
1202 switches::kSingleProcess
) &&
1203 !base::CommandLine::ForCurrentProcess()->HasSwitch(
1204 switches::kProcessPerTab
);
1207 bool RenderFrameHostManager::ShouldSwapBrowsingInstancesForNavigation(
1208 const GURL
& current_effective_url
,
1209 bool current_is_view_source_mode
,
1210 SiteInstance
* new_site_instance
,
1211 const GURL
& new_effective_url
,
1212 bool new_is_view_source_mode
) const {
1213 // If new_entry already has a SiteInstance, assume it is correct. We only
1214 // need to force a swap if it is in a different BrowsingInstance.
1215 if (new_site_instance
) {
1216 return !new_site_instance
->IsRelatedSiteInstance(
1217 render_frame_host_
->GetSiteInstance());
1220 // Check for reasons to swap processes even if we are in a process model that
1221 // doesn't usually swap (e.g., process-per-tab). Any time we return true,
1222 // the new_entry will be rendered in a new SiteInstance AND BrowsingInstance.
1223 BrowserContext
* browser_context
=
1224 delegate_
->GetControllerForRenderManager().GetBrowserContext();
1226 // Don't force a new BrowsingInstance for debug URLs that are handled in the
1227 // renderer process, like javascript: or chrome://crash.
1228 if (IsRendererDebugURL(new_effective_url
))
1231 // For security, we should transition between processes when one is a Web UI
1232 // page and one isn't.
1233 if (ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
1234 render_frame_host_
->GetProcess()->GetID()) ||
1235 WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
1236 browser_context
, current_effective_url
)) {
1237 // If so, force a swap if destination is not an acceptable URL for Web UI.
1238 // Here, data URLs are never allowed.
1239 if (!WebUIControllerFactoryRegistry::GetInstance()->IsURLAcceptableForWebUI(
1240 browser_context
, new_effective_url
)) {
1244 // Force a swap if it's a Web UI URL.
1245 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
1246 browser_context
, new_effective_url
)) {
1251 // Check with the content client as well. Important to pass
1252 // current_effective_url here, which uses the SiteInstance's site if there is
1253 // no current_entry.
1254 if (GetContentClient()->browser()->ShouldSwapBrowsingInstancesForNavigation(
1255 render_frame_host_
->GetSiteInstance(),
1256 current_effective_url
, new_effective_url
)) {
1260 // We can't switch a RenderView between view source and non-view source mode
1261 // without screwing up the session history sometimes (when navigating between
1262 // "view-source:http://foo.com/" and "http://foo.com/", Blink doesn't treat
1263 // it as a new navigation). So require a BrowsingInstance switch.
1264 if (current_is_view_source_mode
!= new_is_view_source_mode
)
1270 bool RenderFrameHostManager::ShouldReuseWebUI(
1271 const NavigationEntry
* current_entry
,
1272 const GURL
& new_url
) const {
1273 NavigationControllerImpl
& controller
=
1274 delegate_
->GetControllerForRenderManager();
1275 return current_entry
&& web_ui_
&&
1276 (WebUIControllerFactoryRegistry::GetInstance()->GetWebUIType(
1277 controller
.GetBrowserContext(), current_entry
->GetURL()) ==
1278 WebUIControllerFactoryRegistry::GetInstance()->GetWebUIType(
1279 controller
.GetBrowserContext(), new_url
));
1282 SiteInstance
* RenderFrameHostManager::GetSiteInstanceForNavigation(
1283 const GURL
& dest_url
,
1284 SiteInstance
* source_instance
,
1285 SiteInstance
* dest_instance
,
1286 SiteInstance
* candidate_instance
,
1287 ui::PageTransition transition
,
1288 bool dest_is_restore
,
1289 bool dest_is_view_source_mode
) {
1290 SiteInstance
* current_instance
= render_frame_host_
->GetSiteInstance();
1292 // We do not currently swap processes for navigations in webview tag guests.
1293 if (current_instance
->GetSiteURL().SchemeIs(kGuestScheme
))
1294 return current_instance
;
1296 // Determine if we need a new BrowsingInstance for this entry. If true, this
1297 // implies that it will get a new SiteInstance (and likely process), and that
1298 // other tabs in the current BrowsingInstance will be unable to script it.
1299 // This is used for cases that require a process swap even in the
1300 // process-per-tab model, such as WebUI pages.
1301 // TODO(clamy): Remove the dependency on the current entry.
1302 const NavigationEntry
* current_entry
=
1303 delegate_
->GetLastCommittedNavigationEntryForRenderManager();
1304 BrowserContext
* browser_context
=
1305 delegate_
->GetControllerForRenderManager().GetBrowserContext();
1306 const GURL
& current_effective_url
= current_entry
?
1307 SiteInstanceImpl::GetEffectiveURL(browser_context
,
1308 current_entry
->GetURL()) :
1309 render_frame_host_
->GetSiteInstance()->GetSiteURL();
1310 bool current_is_view_source_mode
= current_entry
?
1311 current_entry
->IsViewSourceMode() : dest_is_view_source_mode
;
1312 bool force_swap
= ShouldSwapBrowsingInstancesForNavigation(
1313 current_effective_url
,
1314 current_is_view_source_mode
,
1316 SiteInstanceImpl::GetEffectiveURL(browser_context
, dest_url
),
1317 dest_is_view_source_mode
);
1318 SiteInstanceDescriptor new_instance_descriptor
=
1319 SiteInstanceDescriptor(current_instance
);
1320 if (ShouldTransitionCrossSite() || force_swap
) {
1321 new_instance_descriptor
= DetermineSiteInstanceForURL(
1322 dest_url
, source_instance
, current_instance
, dest_instance
, transition
,
1323 dest_is_restore
, dest_is_view_source_mode
, force_swap
);
1326 SiteInstance
* new_instance
=
1327 ConvertToSiteInstance(new_instance_descriptor
, candidate_instance
);
1329 // If |force_swap| is true, we must use a different SiteInstance than the
1330 // current one. If we didn't, we would have two RenderFrameHosts in the same
1331 // SiteInstance and the same frame, resulting in page_id conflicts for their
1332 // NavigationEntries.
1334 CHECK_NE(new_instance
, current_instance
);
1335 return new_instance
;
1338 RenderFrameHostManager::SiteInstanceDescriptor
1339 RenderFrameHostManager::DetermineSiteInstanceForURL(
1340 const GURL
& dest_url
,
1341 SiteInstance
* source_instance
,
1342 SiteInstance
* current_instance
,
1343 SiteInstance
* dest_instance
,
1344 ui::PageTransition transition
,
1345 bool dest_is_restore
,
1346 bool dest_is_view_source_mode
,
1347 bool force_browsing_instance_swap
) {
1348 SiteInstanceImpl
* current_instance_impl
=
1349 static_cast<SiteInstanceImpl
*>(current_instance
);
1350 NavigationControllerImpl
& controller
=
1351 delegate_
->GetControllerForRenderManager();
1352 BrowserContext
* browser_context
= controller
.GetBrowserContext();
1354 // If the entry has an instance already we should use it.
1355 if (dest_instance
) {
1356 // If we are forcing a swap, this should be in a different BrowsingInstance.
1357 if (force_browsing_instance_swap
) {
1358 CHECK(!dest_instance
->IsRelatedSiteInstance(
1359 render_frame_host_
->GetSiteInstance()));
1361 return SiteInstanceDescriptor(dest_instance
);
1364 // If a swap is required, we need to force the SiteInstance AND
1365 // BrowsingInstance to be different ones, using CreateForURL.
1366 if (force_browsing_instance_swap
)
1367 return SiteInstanceDescriptor(browser_context
, dest_url
, false);
1369 // (UGLY) HEURISTIC, process-per-site only:
1371 // If this navigation is generated, then it probably corresponds to a search
1372 // query. Given that search results typically lead to users navigating to
1373 // other sites, we don't really want to use the search engine hostname to
1374 // determine the site instance for this navigation.
1376 // NOTE: This can be removed once we have a way to transition between
1377 // RenderViews in response to a link click.
1379 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1380 switches::kProcessPerSite
) &&
1381 ui::PageTransitionCoreTypeIs(transition
, ui::PAGE_TRANSITION_GENERATED
)) {
1382 return SiteInstanceDescriptor(current_instance_impl
);
1385 // If we haven't used our SiteInstance (and thus RVH) yet, then we can use it
1386 // for this entry. We won't commit the SiteInstance to this site until the
1387 // navigation commits (in DidNavigate), unless the navigation entry was
1388 // restored or it's a Web UI as described below.
1389 if (!current_instance_impl
->HasSite()) {
1390 // If we've already created a SiteInstance for our destination, we don't
1391 // want to use this unused SiteInstance; use the existing one. (We don't
1392 // do this check if the current_instance has a site, because for now, we
1393 // want to compare against the current URL and not the SiteInstance's site.
1394 // In this case, there is no current URL, so comparing against the site is
1395 // ok. See additional comments below.)
1397 // Also, if the URL should use process-per-site mode and there is an
1398 // existing process for the site, we should use it. We can call
1399 // GetRelatedSiteInstance() for this, which will eagerly set the site and
1400 // thus use the correct process.
1401 bool use_process_per_site
=
1402 RenderProcessHost::ShouldUseProcessPerSite(browser_context
, dest_url
) &&
1403 RenderProcessHostImpl::GetProcessHostForSite(browser_context
, dest_url
);
1404 if (current_instance_impl
->HasRelatedSiteInstance(dest_url
) ||
1405 use_process_per_site
) {
1406 return SiteInstanceDescriptor(browser_context
, dest_url
, true);
1409 // For extensions, Web UI URLs (such as the new tab page), and apps we do
1410 // not want to use the |current_instance_impl| if it has no site, since it
1411 // will have a RenderProcessHost of PRIV_NORMAL. Create a new SiteInstance
1412 // for this URL instead (with the correct process type).
1413 if (current_instance_impl
->HasWrongProcessForURL(dest_url
))
1414 return SiteInstanceDescriptor(browser_context
, dest_url
, true);
1416 // View-source URLs must use a new SiteInstance and BrowsingInstance.
1417 // TODO(nasko): This is the same condition as later in the function. This
1418 // should be taken into account when refactoring this method as part of
1419 // http://crbug.com/123007.
1420 if (dest_is_view_source_mode
)
1421 return SiteInstanceDescriptor(browser_context
, dest_url
, false);
1423 // If we are navigating from a blank SiteInstance to a WebUI, make sure we
1424 // create a new SiteInstance.
1425 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
1426 browser_context
, dest_url
)) {
1427 return SiteInstanceDescriptor(browser_context
, dest_url
, false);
1430 // Normally the "site" on the SiteInstance is set lazily when the load
1431 // actually commits. This is to support better process sharing in case
1432 // the site redirects to some other site: we want to use the destination
1433 // site in the site instance.
1435 // In the case of session restore, as it loads all the pages immediately
1436 // we need to set the site first, otherwise after a restore none of the
1437 // pages would share renderers in process-per-site.
1439 // The embedder can request some urls never to be assigned to SiteInstance
1440 // through the ShouldAssignSiteForURL() content client method, so that
1441 // renderers created for particular chrome urls (e.g. the chrome-native://
1442 // scheme) can be reused for subsequent navigations in the same WebContents.
1443 // See http://crbug.com/386542.
1444 if (dest_is_restore
&&
1445 GetContentClient()->browser()->ShouldAssignSiteForURL(dest_url
)) {
1446 current_instance_impl
->SetSite(dest_url
);
1449 return SiteInstanceDescriptor(current_instance_impl
);
1452 // Otherwise, only create a new SiteInstance for a cross-process navigation.
1454 // TODO(creis): Once we intercept links and script-based navigations, we
1455 // will be able to enforce that all entries in a SiteInstance actually have
1456 // the same site, and it will be safe to compare the URL against the
1457 // SiteInstance's site, as follows:
1458 // const GURL& current_url = current_instance_impl->site();
1459 // For now, though, we're in a hybrid model where you only switch
1460 // SiteInstances if you type in a cross-site URL. This means we have to
1461 // compare the entry's URL to the last committed entry's URL.
1462 NavigationEntry
* current_entry
= controller
.GetLastCommittedEntry();
1463 if (interstitial_page_
) {
1464 // The interstitial is currently the last committed entry, but we want to
1465 // compare against the last non-interstitial entry.
1466 current_entry
= controller
.GetEntryAtOffset(-1);
1469 // View-source URLs must use a new SiteInstance and BrowsingInstance.
1470 // We don't need a swap when going from view-source to a debug URL like
1471 // chrome://crash, however.
1472 // TODO(creis): Refactor this method so this duplicated code isn't needed.
1473 // See http://crbug.com/123007.
1474 if (current_entry
&&
1475 current_entry
->IsViewSourceMode() != dest_is_view_source_mode
&&
1476 !IsRendererDebugURL(dest_url
)) {
1477 return SiteInstanceDescriptor(browser_context
, dest_url
, false);
1480 // Use the source SiteInstance in case of data URLs or about:blank pages,
1481 // because the content is then controlled and/or scriptable by the source
1483 GURL
about_blank(url::kAboutBlankURL
);
1484 if (source_instance
&&
1485 (dest_url
== about_blank
|| dest_url
.scheme() == url::kDataScheme
)) {
1486 return SiteInstanceDescriptor(source_instance
);
1489 // Use the current SiteInstance for same site navigations, as long as the
1490 // process type is correct. (The URL may have been installed as an app since
1491 // the last time we visited it.)
1492 const GURL
& current_url
=
1493 GetCurrentURLForSiteInstance(current_instance_impl
, current_entry
);
1494 if (SiteInstance::IsSameWebSite(browser_context
, current_url
, dest_url
) &&
1495 !current_instance_impl
->HasWrongProcessForURL(dest_url
)) {
1496 return SiteInstanceDescriptor(current_instance_impl
);
1499 // Start the new renderer in a new SiteInstance, but in the current
1500 // BrowsingInstance. It is important to immediately give this new
1501 // SiteInstance to a RenderViewHost (if it is different than our current
1502 // SiteInstance), so that it is ref counted. This will happen in
1503 // CreateRenderView.
1504 return SiteInstanceDescriptor(browser_context
, dest_url
, true);
1507 SiteInstance
* RenderFrameHostManager::ConvertToSiteInstance(
1508 const SiteInstanceDescriptor
& descriptor
,
1509 SiteInstance
* candidate_instance
) {
1510 SiteInstance
* current_instance
= render_frame_host_
->GetSiteInstance();
1512 // Note: If the |candidate_instance| matches the descriptor, it will already
1513 // be set to |descriptor.existing_site_instance|.
1514 if (descriptor
.existing_site_instance
)
1515 return descriptor
.existing_site_instance
;
1517 // Note: If the |candidate_instance| matches the descriptor,
1518 // GetRelatedSiteInstance will return it.
1519 if (descriptor
.new_is_related_to_current
)
1520 return current_instance
->GetRelatedSiteInstance(descriptor
.new_site_url
);
1522 // At this point we know an unrelated site instance must be returned. First
1523 // check if the candidate matches.
1524 if (candidate_instance
&&
1525 !current_instance
->IsRelatedSiteInstance(candidate_instance
) &&
1526 candidate_instance
->GetSiteURL() == descriptor
.new_site_url
) {
1527 return candidate_instance
;
1530 // Otherwise return a newly created one.
1531 return SiteInstance::CreateForURL(
1532 delegate_
->GetControllerForRenderManager().GetBrowserContext(),
1533 descriptor
.new_site_url
);
1536 const GURL
& RenderFrameHostManager::GetCurrentURLForSiteInstance(
1537 SiteInstance
* current_instance
, NavigationEntry
* current_entry
) {
1538 // If this is a subframe that is potentially out of process from its parent,
1539 // don't consider using current_entry's url for SiteInstance selection, since
1540 // current_entry's url is for the main frame and may be in a different site
1542 // TODO(creis): Remove this when we can check the FrameNavigationEntry's url.
1543 // See http://crbug.com/369654
1544 if (!frame_tree_node_
->IsMainFrame() &&
1545 SiteIsolationPolicy::AreCrossProcessFramesPossible())
1546 return frame_tree_node_
->current_url();
1548 // If there is no last non-interstitial entry (and current_instance already
1549 // has a site), then we must have been opened from another tab. We want
1550 // to compare against the URL of the page that opened us, but we can't
1551 // get to it directly. The best we can do is check against the site of
1552 // the SiteInstance. This will be correct when we intercept links and
1553 // script-based navigations, but for now, it could place some pages in a
1554 // new process unnecessarily. We should only hit this case if a page tries
1555 // to open a new tab to an interstitial-inducing URL, and then navigates
1556 // the page to a different same-site URL. (This seems very unlikely in
1559 return current_entry
->GetURL();
1560 return current_instance
->GetSiteURL();
1563 void RenderFrameHostManager::CreatePendingRenderFrameHost(
1564 SiteInstance
* old_instance
,
1565 SiteInstance
* new_instance
,
1566 bool is_main_frame
) {
1567 int create_render_frame_flags
= 0;
1569 create_render_frame_flags
|= CREATE_RF_FOR_MAIN_FRAME_NAVIGATION
;
1571 if (delegate_
->IsHidden())
1572 create_render_frame_flags
|= CREATE_RF_HIDDEN
;
1574 if (pending_render_frame_host_
)
1577 // The process for the new SiteInstance may (if we're sharing a process with
1578 // another host that already initialized it) or may not (we have our own
1579 // process or the existing process crashed) have been initialized. Calling
1580 // Init multiple times will be ignored, so this is safe.
1581 if (!new_instance
->GetProcess()->Init())
1584 CreateProxiesForNewRenderFrameHost(old_instance
, new_instance
);
1586 // Create a non-swapped-out RFH with the given opener.
1587 pending_render_frame_host_
= CreateRenderFrame(
1588 new_instance
, pending_web_ui(), create_render_frame_flags
, nullptr);
1591 void RenderFrameHostManager::CreateProxiesForNewRenderFrameHost(
1592 SiteInstance
* old_instance
,
1593 SiteInstance
* new_instance
) {
1594 // Only create opener proxies if they are in the same BrowsingInstance.
1595 if (new_instance
->IsRelatedSiteInstance(old_instance
)) {
1596 CreateOpenerProxies(new_instance
, frame_tree_node_
);
1597 } else if (SiteIsolationPolicy::AreCrossProcessFramesPossible()) {
1598 // Ensure that the frame tree has RenderFrameProxyHosts for the
1599 // new SiteInstance in all nodes except the current one. We do this for
1600 // all frames in the tree, whether they are in the same BrowsingInstance or
1601 // not. If |new_instance| is in the same BrowsingInstance as
1602 // |old_instance|, this will be done as part of CreateOpenerProxies above;
1603 // otherwise, we do this here. We will still check whether two frames are
1604 // in the same BrowsingInstance before we allow them to interact (e.g.,
1606 frame_tree_node_
->frame_tree()->CreateProxiesForSiteInstance(
1607 frame_tree_node_
, new_instance
);
1611 void RenderFrameHostManager::CreateProxiesForNewNamedFrame() {
1612 if (!SiteIsolationPolicy::AreCrossProcessFramesPossible())
1615 DCHECK(!frame_tree_node_
->frame_name().empty());
1617 // If this is a top-level frame, create proxies for this node in the
1618 // SiteInstances of its opener's ancestors, which are allowed to discover
1619 // this frame by name (see https://crbug.com/511474 and part 4 of
1620 // https://html.spec.whatwg.org/#the-rules-for-choosing-a-browsing-context-
1621 // given-a-browsing-context-name).
1622 FrameTreeNode
* opener
= frame_tree_node_
->opener();
1623 if (!opener
|| !frame_tree_node_
->IsMainFrame())
1625 SiteInstance
* current_instance
= render_frame_host_
->GetSiteInstance();
1627 // Start from opener's parent. There's no need to create a proxy in the
1628 // opener's SiteInstance, since new windows are always first opened in the
1629 // same SiteInstance as their opener, and if the new window navigates
1630 // cross-site, that proxy would be created as part of swapping out.
1631 for (FrameTreeNode
* ancestor
= opener
->parent(); ancestor
;
1632 ancestor
= ancestor
->parent()) {
1633 RenderFrameHostImpl
* ancestor_rfh
= ancestor
->current_frame_host();
1634 if (ancestor_rfh
->GetSiteInstance() != current_instance
)
1635 CreateRenderFrameProxy(ancestor_rfh
->GetSiteInstance());
1639 scoped_ptr
<RenderFrameHostImpl
> RenderFrameHostManager::CreateRenderFrameHost(
1640 SiteInstance
* site_instance
,
1641 int32 view_routing_id
,
1642 int32 frame_routing_id
,
1643 int32 widget_routing_id
,
1646 if (frame_routing_id
== MSG_ROUTING_NONE
)
1647 frame_routing_id
= site_instance
->GetProcess()->GetNextRoutingID();
1649 bool swapped_out
= !!(flags
& CREATE_RF_SWAPPED_OUT
);
1650 bool hidden
= !!(flags
& CREATE_RF_HIDDEN
);
1652 // Create a RVH for main frames, or find the existing one for subframes.
1653 FrameTree
* frame_tree
= frame_tree_node_
->frame_tree();
1654 RenderViewHostImpl
* render_view_host
= nullptr;
1655 if (frame_tree_node_
->IsMainFrame()) {
1656 render_view_host
= frame_tree
->CreateRenderViewHost(
1657 site_instance
, view_routing_id
, frame_routing_id
, swapped_out
, hidden
);
1659 render_view_host
= frame_tree
->GetRenderViewHost(site_instance
);
1660 CHECK(render_view_host
);
1663 return RenderFrameHostFactory::Create(
1664 site_instance
, render_view_host
, render_frame_delegate_
,
1665 render_widget_delegate_
, frame_tree
, frame_tree_node_
, frame_routing_id
,
1666 widget_routing_id
, surface_id
, flags
);
1670 bool RenderFrameHostManager::CreateSpeculativeRenderFrameHost(
1672 SiteInstance
* old_instance
,
1673 SiteInstance
* new_instance
,
1675 CHECK(new_instance
);
1676 CHECK_NE(old_instance
, new_instance
);
1677 CHECK(!should_reuse_web_ui_
);
1679 // Note: |speculative_web_ui_| must be initialized before starting the
1680 // |speculative_render_frame_host_| creation steps otherwise the WebUI
1681 // won't be properly initialized.
1682 speculative_web_ui_
= CreateWebUI(url
, bindings
);
1684 // The process for the new SiteInstance may (if we're sharing a process with
1685 // another host that already initialized it) or may not (we have our own
1686 // process or the existing process crashed) have been initialized. Calling
1687 // Init multiple times will be ignored, so this is safe.
1688 if (!new_instance
->GetProcess()->Init())
1691 CreateProxiesForNewRenderFrameHost(old_instance
, new_instance
);
1693 int create_render_frame_flags
= 0;
1694 if (frame_tree_node_
->IsMainFrame())
1695 create_render_frame_flags
|= CREATE_RF_FOR_MAIN_FRAME_NAVIGATION
;
1696 if (delegate_
->IsHidden())
1697 create_render_frame_flags
|= CREATE_RF_HIDDEN
;
1698 speculative_render_frame_host_
=
1699 CreateRenderFrame(new_instance
, speculative_web_ui_
.get(),
1700 create_render_frame_flags
, nullptr);
1702 if (!speculative_render_frame_host_
) {
1703 speculative_web_ui_
.reset();
1709 scoped_ptr
<RenderFrameHostImpl
> RenderFrameHostManager::CreateRenderFrame(
1710 SiteInstance
* instance
,
1713 int* view_routing_id_ptr
) {
1714 bool swapped_out
= !!(flags
& CREATE_RF_SWAPPED_OUT
);
1715 bool swapped_out_forbidden
=
1716 SiteIsolationPolicy::IsSwappedOutStateForbidden();
1719 CHECK_IMPLIES(swapped_out_forbidden
, !swapped_out
);
1720 CHECK_IMPLIES(!SiteIsolationPolicy::AreCrossProcessFramesPossible(),
1721 frame_tree_node_
->IsMainFrame());
1723 // Swapped out views should always be hidden.
1724 DCHECK_IMPLIES(swapped_out
, (flags
& CREATE_RF_HIDDEN
));
1726 scoped_ptr
<RenderFrameHostImpl
> new_render_frame_host
;
1727 bool success
= true;
1728 if (view_routing_id_ptr
)
1729 *view_routing_id_ptr
= MSG_ROUTING_NONE
;
1731 // We are creating a pending, speculative or swapped out RFH here. We should
1732 // never create it in the same SiteInstance as our current RFH.
1733 CHECK_NE(render_frame_host_
->GetSiteInstance(), instance
);
1735 // Check if we've already created an RFH for this SiteInstance. If so, try
1736 // to re-use the existing one, which has already been initialized. We'll
1737 // remove it from the list of proxy hosts below if it will be active.
1738 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(instance
);
1739 if (proxy
&& proxy
->render_frame_host()) {
1740 CHECK(!swapped_out_forbidden
);
1741 if (view_routing_id_ptr
)
1742 *view_routing_id_ptr
= proxy
->GetRenderViewHost()->GetRoutingID();
1743 // Delete the existing RenderFrameProxyHost, but reuse the RenderFrameHost.
1744 // Prevent the process from exiting while we're trying to use it.
1746 new_render_frame_host
= proxy
->PassFrameHostOwnership();
1747 new_render_frame_host
->GetProcess()->AddPendingView();
1749 proxy_hosts_
->Remove(instance
->GetId());
1750 // NB |proxy| is deleted at this point.
1753 // Create a new RenderFrameHost if we don't find an existing one.
1755 int32 widget_routing_id
= MSG_ROUTING_NONE
;
1756 int32 surface_id
= 0;
1757 // A RenderFrame in a different process from its parent RenderFrame
1758 // requires a RenderWidget for input/layout/painting.
1759 if (frame_tree_node_
->parent() &&
1760 frame_tree_node_
->parent()->current_frame_host()->GetSiteInstance() !=
1762 CHECK(SiteIsolationPolicy::AreCrossProcessFramesPossible());
1763 widget_routing_id
= instance
->GetProcess()->GetNextRoutingID();
1764 surface_id
= GpuSurfaceTracker::Get()->AddSurfaceForRenderer(
1765 instance
->GetProcess()->GetID(), widget_routing_id
);
1768 new_render_frame_host
=
1769 CreateRenderFrameHost(instance
, MSG_ROUTING_NONE
, MSG_ROUTING_NONE
,
1770 widget_routing_id
, surface_id
, flags
);
1771 RenderViewHostImpl
* render_view_host
=
1772 new_render_frame_host
->render_view_host();
1773 int proxy_routing_id
= MSG_ROUTING_NONE
;
1775 // Prevent the process from exiting while we're trying to navigate in it.
1776 // Otherwise, if the new RFH is swapped out already, store it.
1778 new_render_frame_host
->GetProcess()->AddPendingView();
1780 proxy
= new RenderFrameProxyHost(
1781 new_render_frame_host
->GetSiteInstance(),
1782 new_render_frame_host
->render_view_host(), frame_tree_node_
);
1783 proxy_hosts_
->Add(instance
->GetId(), make_scoped_ptr(proxy
));
1784 proxy_routing_id
= proxy
->GetRoutingID();
1785 proxy
->TakeFrameHostOwnership(new_render_frame_host
.Pass());
1788 success
= InitRenderView(render_view_host
, proxy_routing_id
,
1789 !!(flags
& CREATE_RF_FOR_MAIN_FRAME_NAVIGATION
));
1791 // Remember that InitRenderView also created the RenderFrameProxy.
1793 proxy
->set_render_frame_proxy_created(true);
1794 if (frame_tree_node_
->IsMainFrame()) {
1795 // Don't show the main frame's view until we get a DidNavigate from it.
1796 // Only the RenderViewHost for the top-level RenderFrameHost has a
1797 // RenderWidgetHostView; RenderWidgetHosts for out-of-process iframes
1798 // will be created later and hidden.
1799 if (render_view_host
->GetView())
1800 render_view_host
->GetView()->Hide();
1802 // RenderViewHost for |instance| might exist prior to calling
1803 // CreateRenderFrame. In such a case, InitRenderView will not create the
1804 // RenderFrame in the renderer process and it needs to be done
1806 if (swapped_out_forbidden
) {
1807 // Init the RFH, so a RenderFrame is created in the renderer.
1808 DCHECK(new_render_frame_host
);
1809 success
= InitRenderFrame(new_render_frame_host
.get());
1814 if (view_routing_id_ptr
)
1815 *view_routing_id_ptr
= render_view_host
->GetRoutingID();
1819 // When a new RenderView is created by the renderer process, the new
1820 // WebContents gets a RenderViewHost in the SiteInstance of its opener
1821 // WebContents. If not used in the first navigation, this RVH is swapped out
1822 // and is not granted bindings, so we may need to grant them when swapping it
1824 if (web_ui
&& !new_render_frame_host
->GetProcess()->IsForGuestsOnly()) {
1825 int required_bindings
= web_ui
->GetBindings();
1826 RenderViewHost
* render_view_host
=
1827 new_render_frame_host
->render_view_host();
1828 if ((render_view_host
->GetEnabledBindings() & required_bindings
) !=
1829 required_bindings
) {
1830 render_view_host
->AllowBindings(required_bindings
);
1834 // Returns the new RFH if it isn't swapped out.
1835 if (success
&& !swapped_out
) {
1836 DCHECK(new_render_frame_host
->GetSiteInstance() == instance
);
1837 return new_render_frame_host
.Pass();
1842 int RenderFrameHostManager::CreateRenderFrameProxy(SiteInstance
* instance
) {
1843 // A RenderFrameProxyHost should never be created in the same SiteInstance as
1846 CHECK_NE(instance
, render_frame_host_
->GetSiteInstance());
1848 RenderViewHostImpl
* render_view_host
= nullptr;
1850 // Ensure a RenderViewHost exists for |instance|, as it creates the page
1851 // level structure in Blink.
1852 if (SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
1854 frame_tree_node_
->frame_tree()->GetRenderViewHost(instance
);
1855 if (!render_view_host
) {
1856 CHECK(frame_tree_node_
->IsMainFrame());
1857 render_view_host
= frame_tree_node_
->frame_tree()->CreateRenderViewHost(
1858 instance
, MSG_ROUTING_NONE
, MSG_ROUTING_NONE
, true, true);
1862 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(instance
);
1863 if (proxy
&& proxy
->is_render_frame_proxy_live())
1864 return proxy
->GetRoutingID();
1868 new RenderFrameProxyHost(instance
, render_view_host
, frame_tree_node_
);
1869 proxy_hosts_
->Add(instance
->GetId(), make_scoped_ptr(proxy
));
1872 if (SiteIsolationPolicy::IsSwappedOutStateForbidden() &&
1873 frame_tree_node_
->IsMainFrame()) {
1874 InitRenderView(render_view_host
, proxy
->GetRoutingID(), true);
1875 proxy
->set_render_frame_proxy_created(true);
1877 proxy
->InitRenderFrameProxy();
1880 return proxy
->GetRoutingID();
1883 void RenderFrameHostManager::CreateProxiesForChildFrame(FrameTreeNode
* child
) {
1884 for (const auto& pair
: *proxy_hosts_
) {
1885 // Do not create proxies for subframes in the outer delegate's process,
1886 // since the outer delegate does not need to interact with them.
1887 if (ForInnerDelegate() && pair
.second
== GetProxyToOuterDelegate())
1890 child
->render_manager()->CreateRenderFrameProxy(
1891 pair
.second
->GetSiteInstance());
1895 void RenderFrameHostManager::EnsureRenderViewInitialized(
1896 RenderViewHostImpl
* render_view_host
,
1897 SiteInstance
* instance
) {
1898 DCHECK(frame_tree_node_
->IsMainFrame());
1900 if (render_view_host
->IsRenderViewLive())
1903 // If the proxy in |instance| doesn't exist, this RenderView is not swapped
1904 // out and shouldn't be reinitialized here.
1905 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(instance
);
1909 InitRenderView(render_view_host
, proxy
->GetRoutingID(), false);
1910 proxy
->set_render_frame_proxy_created(true);
1913 void RenderFrameHostManager::CreateOuterDelegateProxy(
1914 SiteInstance
* outer_contents_site_instance
,
1915 RenderFrameHostImpl
* render_frame_host
) {
1916 CHECK(BrowserPluginGuestMode::UseCrossProcessFramesForGuests());
1917 RenderFrameProxyHost
* proxy
= new RenderFrameProxyHost(
1918 outer_contents_site_instance
, nullptr, frame_tree_node_
);
1919 proxy_hosts_
->Add(outer_contents_site_instance
->GetId(),
1920 make_scoped_ptr(proxy
));
1922 // Swap the outer WebContents's frame with the proxy to inner WebContents.
1924 // We are in the outer WebContents, and its FrameTree would never see
1925 // a load start for any of its inner WebContents. Eventually, that also makes
1926 // the FrameTree never see the matching load stop. Therefore, we always pass
1927 // false to |is_loading| below.
1928 // TODO(lazyboy): This |is_loading| behavior might not be what we want,
1929 // investigate and fix.
1930 render_frame_host
->Send(new FrameMsg_SwapOut(
1931 render_frame_host
->GetRoutingID(), proxy
->GetRoutingID(),
1932 false /* is_loading */, FrameReplicationState()));
1933 proxy
->set_render_frame_proxy_created(true);
1936 void RenderFrameHostManager::SetRWHViewForInnerContents(
1937 RenderWidgetHostView
* child_rwhv
) {
1938 DCHECK(ForInnerDelegate() && frame_tree_node_
->IsMainFrame());
1939 GetProxyToOuterDelegate()->SetChildRWHView(child_rwhv
);
1942 bool RenderFrameHostManager::InitRenderView(
1943 RenderViewHostImpl
* render_view_host
,
1944 int proxy_routing_id
,
1945 bool for_main_frame_navigation
) {
1946 // Ensure the renderer process is initialized before creating the
1948 if (!render_view_host
->GetProcess()->Init())
1951 // We may have initialized this RenderViewHost for another RenderFrameHost.
1952 if (render_view_host
->IsRenderViewLive())
1955 // If the ongoing navigation is to a WebUI and the RenderView is not in a
1956 // guest process, tell the RenderViewHost about any bindings it will need
1958 WebUIImpl
* dest_web_ui
= nullptr;
1959 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1960 switches::kEnableBrowserSideNavigation
)) {
1962 should_reuse_web_ui_
? web_ui_
.get() : speculative_web_ui_
.get();
1964 dest_web_ui
= pending_web_ui();
1966 if (dest_web_ui
&& !render_view_host
->GetProcess()->IsForGuestsOnly()) {
1967 render_view_host
->AllowBindings(dest_web_ui
->GetBindings());
1969 // Ensure that we don't create an unprivileged RenderView in a WebUI-enabled
1970 // process unless it's swapped out.
1971 if (render_view_host
->is_active()) {
1972 CHECK(!ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
1973 render_view_host
->GetProcess()->GetID()));
1977 int opener_frame_routing_id
=
1978 GetOpenerRoutingID(render_view_host
->GetSiteInstance());
1980 return delegate_
->CreateRenderViewForRenderManager(
1981 render_view_host
, opener_frame_routing_id
, proxy_routing_id
,
1982 frame_tree_node_
->current_replication_state(), for_main_frame_navigation
);
1985 bool RenderFrameHostManager::InitRenderFrame(
1986 RenderFrameHostImpl
* render_frame_host
) {
1987 if (render_frame_host
->IsRenderFrameLive())
1990 SiteInstance
* site_instance
= render_frame_host
->GetSiteInstance();
1992 int opener_routing_id
= MSG_ROUTING_NONE
;
1993 if (frame_tree_node_
->opener())
1994 opener_routing_id
= GetOpenerRoutingID(site_instance
);
1996 int parent_routing_id
= MSG_ROUTING_NONE
;
1997 if (frame_tree_node_
->parent()) {
1998 parent_routing_id
= frame_tree_node_
->parent()
2000 ->GetRoutingIdForSiteInstance(site_instance
);
2001 CHECK_NE(parent_routing_id
, MSG_ROUTING_NONE
);
2004 // At this point, all RenderFrameProxies for sibling frames have already been
2005 // created, including any proxies that come after this frame. To preserve
2006 // correct order for indexed window access (e.g., window.frames[1]), pass the
2007 // previous sibling frame so that this frame is correctly inserted into the
2008 // frame tree on the renderer side.
2009 int previous_sibling_routing_id
= MSG_ROUTING_NONE
;
2010 FrameTreeNode
* previous_sibling
= frame_tree_node_
->PreviousSibling();
2011 if (previous_sibling
) {
2012 previous_sibling_routing_id
=
2013 previous_sibling
->render_manager()->GetRoutingIdForSiteInstance(
2015 CHECK_NE(previous_sibling_routing_id
, MSG_ROUTING_NONE
);
2018 // Check whether there is an existing proxy for this frame in this
2019 // SiteInstance. If there is, the new RenderFrame needs to be able to find
2020 // the proxy it is replacing, so that it can fully initialize itself.
2021 // NOTE: This is the only time that a RenderFrameProxyHost can be in the same
2022 // SiteInstance as its RenderFrameHost. This is only the case until the
2023 // RenderFrameHost commits, at which point it will replace and delete the
2024 // RenderFrameProxyHost.
2025 int proxy_routing_id
= MSG_ROUTING_NONE
;
2026 RenderFrameProxyHost
* existing_proxy
= GetRenderFrameProxyHost(site_instance
);
2027 if (existing_proxy
) {
2028 proxy_routing_id
= existing_proxy
->GetRoutingID();
2029 CHECK_NE(proxy_routing_id
, MSG_ROUTING_NONE
);
2030 if (!existing_proxy
->is_render_frame_proxy_live())
2031 existing_proxy
->InitRenderFrameProxy();
2034 return delegate_
->CreateRenderFrameForRenderManager(
2035 render_frame_host
, proxy_routing_id
, opener_routing_id
, parent_routing_id
,
2036 previous_sibling_routing_id
);
2039 int RenderFrameHostManager::GetRoutingIdForSiteInstance(
2040 SiteInstance
* site_instance
) {
2041 if (render_frame_host_
->GetSiteInstance() == site_instance
)
2042 return render_frame_host_
->GetRoutingID();
2044 // If there is a matching pending RFH, only return it if swapped out is
2045 // allowed, since otherwise there should be a proxy that should be used
2047 if (pending_render_frame_host_
&&
2048 pending_render_frame_host_
->GetSiteInstance() == site_instance
&&
2049 !SiteIsolationPolicy::IsSwappedOutStateForbidden())
2050 return pending_render_frame_host_
->GetRoutingID();
2052 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(site_instance
);
2054 return proxy
->GetRoutingID();
2056 return MSG_ROUTING_NONE
;
2059 void RenderFrameHostManager::CommitPending() {
2060 TRACE_EVENT1("navigation", "RenderFrameHostManager::CommitPending",
2061 "FrameTreeNode id", frame_tree_node_
->frame_tree_node_id());
2062 bool browser_side_navigation
=
2063 base::CommandLine::ForCurrentProcess()->HasSwitch(
2064 switches::kEnableBrowserSideNavigation
);
2066 // First check whether we're going to want to focus the location bar after
2067 // this commit. We do this now because the navigation hasn't formally
2068 // committed yet, so if we've already cleared |pending_web_ui_| the call chain
2069 // this triggers won't be able to figure out what's going on.
2070 bool will_focus_location_bar
= delegate_
->FocusLocationBarByDefault();
2072 // Next commit the Web UI, if any. Either replace |web_ui_| with
2073 // |pending_web_ui_|, or clear |web_ui_| if there is no pending WebUI, or
2074 // leave |web_ui_| as is if reusing it.
2075 DCHECK(!(pending_web_ui_
&& pending_and_current_web_ui_
));
2076 if (pending_web_ui_
|| speculative_web_ui_
) {
2077 DCHECK(!should_reuse_web_ui_
);
2078 web_ui_
.reset(browser_side_navigation
? speculative_web_ui_
.release()
2079 : pending_web_ui_
.release());
2080 } else if (pending_and_current_web_ui_
|| should_reuse_web_ui_
) {
2081 if (browser_side_navigation
) {
2083 should_reuse_web_ui_
= false;
2085 DCHECK_EQ(pending_and_current_web_ui_
.get(), web_ui_
.get());
2086 pending_and_current_web_ui_
.reset();
2091 DCHECK(!speculative_web_ui_
);
2092 DCHECK(!should_reuse_web_ui_
);
2094 // It's possible for the pending_render_frame_host_ to be nullptr when we
2095 // aren't crossing process boundaries. If so, we just needed to handle the Web
2096 // UI committing above and we're done.
2097 if (!pending_render_frame_host_
&& !speculative_render_frame_host_
) {
2098 if (will_focus_location_bar
)
2099 delegate_
->SetFocusToLocationBar(false);
2103 // Remember if the page was focused so we can focus the new renderer in
2105 bool focus_render_view
= !will_focus_location_bar
&&
2106 render_frame_host_
->GetView() &&
2107 render_frame_host_
->GetView()->HasFocus();
2109 bool is_main_frame
= frame_tree_node_
->IsMainFrame();
2111 // Swap in the pending or speculative frame and make it active. Also ensure
2112 // the FrameTree stays in sync.
2113 scoped_ptr
<RenderFrameHostImpl
> old_render_frame_host
;
2114 if (!browser_side_navigation
) {
2115 DCHECK(!speculative_render_frame_host_
);
2116 old_render_frame_host
=
2117 SetRenderFrameHost(pending_render_frame_host_
.Pass());
2120 DCHECK(speculative_render_frame_host_
);
2121 old_render_frame_host
=
2122 SetRenderFrameHost(speculative_render_frame_host_
.Pass());
2125 // Remove the children of the old frame from the tree.
2126 frame_tree_node_
->ResetForNewProcess();
2128 // The process will no longer try to exit, so we can decrement the count.
2129 render_frame_host_
->GetProcess()->RemovePendingView();
2131 // Show the new view (or a sad tab) if necessary.
2132 bool new_rfh_has_view
= !!render_frame_host_
->GetView();
2133 if (!delegate_
->IsHidden() && new_rfh_has_view
) {
2134 // In most cases, we need to show the new view.
2135 render_frame_host_
->GetView()->Show();
2137 if (!new_rfh_has_view
) {
2138 // If the view is gone, then this RenderViewHost died while it was hidden.
2139 // We ignored the RenderProcessGone call at the time, so we should send it
2140 // now to make sure the sad tab shows up, etc.
2141 DCHECK(!render_frame_host_
->IsRenderFrameLive());
2142 DCHECK(!render_frame_host_
->render_view_host()->IsRenderViewLive());
2143 delegate_
->RenderProcessGoneFromRenderManager(
2144 render_frame_host_
->render_view_host());
2147 // For top-level frames, also hide the old RenderViewHost's view.
2148 // TODO(creis): As long as show/hide are on RVH, we don't want to hide on
2149 // subframe navigations or we will interfere with the top-level frame.
2150 if (is_main_frame
&& old_render_frame_host
->render_view_host()->GetView())
2151 old_render_frame_host
->render_view_host()->GetView()->Hide();
2153 // Make sure the size is up to date. (Fix for bug 1079768.)
2154 delegate_
->UpdateRenderViewSizeForRenderManager();
2156 if (will_focus_location_bar
) {
2157 delegate_
->SetFocusToLocationBar(false);
2158 } else if (focus_render_view
&& render_frame_host_
->GetView()) {
2159 render_frame_host_
->GetView()->Focus();
2162 // Notify that we've swapped RenderFrameHosts. We do this before shutting down
2163 // the RFH so that we can clean up RendererResources related to the RFH first.
2164 delegate_
->NotifySwappedFromRenderManager(
2165 old_render_frame_host
.get(), render_frame_host_
.get(), is_main_frame
);
2167 // The RenderViewHost keeps track of the main RenderFrameHost routing id.
2168 // If this is committing a main frame navigation, update it and set the
2169 // routing id in the RenderViewHost associated with the old RenderFrameHost
2170 // to MSG_ROUTING_NONE.
2171 if (is_main_frame
&& SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
2172 render_frame_host_
->render_view_host()->set_main_frame_routing_id(
2173 render_frame_host_
->routing_id());
2174 old_render_frame_host
->render_view_host()->set_main_frame_routing_id(
2178 // Swap out the old frame now that the new one is visible.
2179 // This will swap it out and then put it on the proxy list (if there are other
2180 // active views in its SiteInstance) or schedule it for deletion when the swap
2181 // out ack arrives (or immediately if the process isn't live).
2182 // In the --site-per-process case, old subframe RFHs are not kept alive inside
2184 SwapOutOldFrame(old_render_frame_host
.Pass());
2186 if (SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
2187 // Since the new RenderFrameHost is now committed, there must be no proxies
2188 // for its SiteInstance. Delete any existing ones.
2189 proxy_hosts_
->Remove(render_frame_host_
->GetSiteInstance()->GetId());
2192 // If this is a subframe, it should have a CrossProcessFrameConnector
2193 // created already. Use it to link the new RFH's view to the proxy that
2194 // belongs to the parent frame's SiteInstance. If this navigation causes
2195 // an out-of-process frame to return to the same process as its parent, the
2196 // proxy would have been removed from proxy_hosts_ above.
2197 // Note: We do this after swapping out the old RFH because that may create
2198 // the proxy we're looking for.
2199 RenderFrameProxyHost
* proxy_to_parent
= GetProxyToParent();
2200 if (proxy_to_parent
) {
2201 CHECK(SiteIsolationPolicy::AreCrossProcessFramesPossible());
2202 proxy_to_parent
->SetChildRWHView(render_frame_host_
->GetView());
2205 // After all is done, there must never be a proxy in the list which has the
2206 // same SiteInstance as the current RenderFrameHost.
2207 CHECK(!proxy_hosts_
->Get(render_frame_host_
->GetSiteInstance()->GetId()));
2210 void RenderFrameHostManager::ShutdownProxiesIfLastActiveFrameInSiteInstance(
2211 RenderFrameHostImpl
* render_frame_host
) {
2212 if (!render_frame_host
)
2214 if (!RenderFrameHostImpl::IsRFHStateActive(render_frame_host
->rfh_state()))
2216 if (render_frame_host
->GetSiteInstance()->active_frame_count() > 1U)
2219 // After |render_frame_host| goes away, there will be no active frames left in
2220 // its SiteInstance, so we can delete all proxies created in that SiteInstance
2221 // on behalf of frames anywhere in the BrowsingInstance.
2222 int32 site_instance_id
= render_frame_host
->GetSiteInstance()->GetId();
2224 // First remove any proxies for this SiteInstance from our own list.
2225 ClearProxiesInSiteInstance(site_instance_id
, frame_tree_node_
);
2227 // Use the safe RenderWidgetHost iterator for now to find all RenderViewHosts
2228 // in the SiteInstance, then tell their respective FrameTrees to remove all
2229 // RenderFrameProxyHosts corresponding to them.
2230 // TODO(creis): Replace this with a RenderFrameHostIterator that protects
2231 // against use-after-frees if a later element is deleted before getting to it.
2232 scoped_ptr
<RenderWidgetHostIterator
> widgets(
2233 RenderWidgetHostImpl::GetAllRenderWidgetHosts());
2234 while (RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
2235 if (!widget
->IsRenderView())
2237 RenderViewHostImpl
* rvh
=
2238 static_cast<RenderViewHostImpl
*>(RenderViewHost::From(widget
));
2239 if (site_instance_id
== rvh
->GetSiteInstance()->GetId()) {
2240 // This deletes all RenderFrameHosts using the |rvh|, which then causes
2241 // |rvh| to Shutdown.
2242 FrameTree
* tree
= rvh
->GetDelegate()->GetFrameTree();
2243 tree
->ForEach(base::Bind(
2244 &RenderFrameHostManager::ClearProxiesInSiteInstance
,
2250 RenderFrameHostImpl
* RenderFrameHostManager::UpdateStateForNavigate(
2251 const GURL
& dest_url
,
2252 SiteInstance
* source_instance
,
2253 SiteInstance
* dest_instance
,
2254 ui::PageTransition transition
,
2255 bool dest_is_restore
,
2256 bool dest_is_view_source_mode
,
2257 const GlobalRequestID
& transferred_request_id
,
2259 // Don't swap for subframes unless we are in --site-per-process. We can get
2260 // here in tests for subframes (e.g., NavigateFrameToURL).
2261 if (!frame_tree_node_
->IsMainFrame() &&
2262 !SiteIsolationPolicy::AreCrossProcessFramesPossible()) {
2263 return render_frame_host_
.get();
2266 // If we are currently navigating cross-process, we want to get back to normal
2267 // and then navigate as usual.
2268 if (pending_render_frame_host_
)
2271 SiteInstance
* current_instance
= render_frame_host_
->GetSiteInstance();
2272 scoped_refptr
<SiteInstance
> new_instance
= GetSiteInstanceForNavigation(
2273 dest_url
, source_instance
, dest_instance
, nullptr, transition
,
2274 dest_is_restore
, dest_is_view_source_mode
);
2276 const NavigationEntry
* current_entry
=
2277 delegate_
->GetLastCommittedNavigationEntryForRenderManager();
2279 DCHECK(!pending_render_frame_host_
);
2281 if (new_instance
.get() != current_instance
) {
2282 TRACE_EVENT_INSTANT2(
2284 "RenderFrameHostManager::UpdateStateForNavigate:New SiteInstance",
2285 TRACE_EVENT_SCOPE_THREAD
,
2286 "current_instance id", current_instance
->GetId(),
2287 "new_instance id", new_instance
->GetId());
2289 // New SiteInstance: create a pending RFH to navigate.
2291 // This will possibly create (set to nullptr) a Web UI object for the
2292 // pending page. We'll use this later to give the page special access. This
2293 // must happen before the new renderer is created below so it will get
2294 // bindings. It must also happen after the above conditional call to
2295 // CancelPending(), otherwise CancelPending may clear the pending_web_ui_
2296 // and the page will not have its bindings set appropriately.
2297 SetPendingWebUI(dest_url
, bindings
);
2298 CreatePendingRenderFrameHost(current_instance
, new_instance
.get(),
2299 frame_tree_node_
->IsMainFrame());
2300 if (!pending_render_frame_host_
)
2303 // Check if our current RFH is live before we set up a transition.
2304 if (!render_frame_host_
->IsRenderFrameLive()) {
2305 // The current RFH is not live. There's no reason to sit around with a
2306 // sad tab or a newly created RFH while we wait for the pending RFH to
2307 // navigate. Just switch to the pending RFH now and go back to normal.
2308 // (Note that we don't care about on{before}unload handlers if the current
2311 return render_frame_host_
.get();
2313 // Otherwise, it's safe to treat this as a pending cross-process transition.
2315 // We now have a pending RFH.
2316 DCHECK(pending_render_frame_host_
);
2318 // We need to wait until the beforeunload handler has run, unless we are
2319 // transferring an existing request (in which case it has already run).
2320 // Suspend the new render view (i.e., don't let it send the cross-process
2321 // Navigate message) until we hear back from the old renderer's
2322 // beforeunload handler. If the handler returns false, we'll have to
2323 // cancel the request.
2325 DCHECK(!pending_render_frame_host_
->are_navigations_suspended());
2326 bool is_transfer
= transferred_request_id
!= GlobalRequestID();
2328 // We don't need to stop the old renderer or run beforeunload/unload
2329 // handlers, because those have already been done.
2330 DCHECK(cross_site_transferring_request_
->request_id() ==
2331 transferred_request_id
);
2333 // Also make sure the old render view stops, in case a load is in
2334 // progress. (We don't want to do this for transfers, since it will
2335 // interrupt the transfer with an unexpected DidStopLoading.)
2336 render_frame_host_
->Send(new FrameMsg_Stop(
2337 render_frame_host_
->GetRoutingID()));
2338 pending_render_frame_host_
->SetNavigationsSuspended(true,
2340 // Unless we are transferring an existing request, we should now tell the
2341 // old render view to run its beforeunload handler, since it doesn't
2342 // otherwise know that the cross-site request is happening. This will
2343 // trigger a call to OnBeforeUnloadACK with the reply.
2344 render_frame_host_
->DispatchBeforeUnload(true);
2347 return pending_render_frame_host_
.get();
2350 // Otherwise the same SiteInstance can be used. Navigate render_frame_host_.
2352 // It's possible to swap out the current RFH and then decide to navigate in it
2353 // anyway (e.g., a cross-process navigation that redirects back to the
2354 // original site). In that case, we have a proxy for the current RFH but
2355 // haven't deleted it yet. The new navigation will swap it back in, so we can
2356 // delete the proxy.
2357 proxy_hosts_
->Remove(new_instance
.get()->GetId());
2359 if (ShouldReuseWebUI(current_entry
, dest_url
)) {
2360 pending_web_ui_
.reset();
2361 pending_and_current_web_ui_
= web_ui_
->AsWeakPtr();
2363 SetPendingWebUI(dest_url
, bindings
);
2364 // Make sure the new RenderViewHost has the right bindings.
2365 if (pending_web_ui() &&
2366 !render_frame_host_
->GetProcess()->IsForGuestsOnly()) {
2367 render_frame_host_
->render_view_host()->AllowBindings(
2368 pending_web_ui()->GetBindings());
2372 if (pending_web_ui() && render_frame_host_
->IsRenderFrameLive()) {
2373 pending_web_ui()->GetController()->RenderViewReused(
2374 render_frame_host_
->render_view_host());
2377 // The renderer can exit view source mode when any error or cancellation
2378 // happen. We must overwrite to recover the mode.
2379 if (dest_is_view_source_mode
) {
2380 render_frame_host_
->render_view_host()->Send(
2381 new ViewMsg_EnableViewSourceMode(
2382 render_frame_host_
->render_view_host()->GetRoutingID()));
2385 return render_frame_host_
.get();
2388 void RenderFrameHostManager::CancelPending() {
2389 TRACE_EVENT1("navigation", "RenderFrameHostManager::CancelPending",
2390 "FrameTreeNode id", frame_tree_node_
->frame_tree_node_id());
2391 DiscardUnusedFrame(UnsetPendingRenderFrameHost());
2394 scoped_ptr
<RenderFrameHostImpl
>
2395 RenderFrameHostManager::UnsetPendingRenderFrameHost() {
2396 scoped_ptr
<RenderFrameHostImpl
> pending_render_frame_host
=
2397 pending_render_frame_host_
.Pass();
2399 RenderFrameDevToolsAgentHost::OnCancelPendingNavigation(
2400 pending_render_frame_host
.get(),
2401 render_frame_host_
.get());
2403 // We no longer need to prevent the process from exiting.
2404 pending_render_frame_host
->GetProcess()->RemovePendingView();
2406 pending_web_ui_
.reset();
2407 pending_and_current_web_ui_
.reset();
2409 return pending_render_frame_host
.Pass();
2412 scoped_ptr
<RenderFrameHostImpl
> RenderFrameHostManager::SetRenderFrameHost(
2413 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
) {
2415 scoped_ptr
<RenderFrameHostImpl
> old_render_frame_host
=
2416 render_frame_host_
.Pass();
2417 render_frame_host_
= render_frame_host
.Pass();
2419 if (frame_tree_node_
->IsMainFrame()) {
2420 // Update the count of top-level frames using this SiteInstance. All
2421 // subframes are in the same BrowsingInstance as the main frame, so we only
2422 // count top-level ones. This makes the value easier for consumers to
2424 if (render_frame_host_
) {
2425 render_frame_host_
->GetSiteInstance()->
2426 IncrementRelatedActiveContentsCount();
2428 if (old_render_frame_host
) {
2429 old_render_frame_host
->GetSiteInstance()->
2430 DecrementRelatedActiveContentsCount();
2434 return old_render_frame_host
.Pass();
2437 bool RenderFrameHostManager::IsRVHOnSwappedOutList(
2438 RenderViewHostImpl
* rvh
) const {
2439 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(
2440 rvh
->GetSiteInstance());
2443 // If there is a proxy without RFH, it is for a subframe in the SiteInstance
2444 // of |rvh|. Subframes should be ignored in this case.
2445 if (!proxy
->render_frame_host())
2447 return IsOnSwappedOutList(proxy
->render_frame_host());
2450 bool RenderFrameHostManager::IsOnSwappedOutList(
2451 RenderFrameHostImpl
* rfh
) const {
2452 if (!rfh
->GetSiteInstance())
2455 RenderFrameProxyHost
* host
=
2456 proxy_hosts_
->Get(rfh
->GetSiteInstance()->GetId());
2460 return host
->render_frame_host() == rfh
;
2463 RenderViewHostImpl
* RenderFrameHostManager::GetSwappedOutRenderViewHost(
2464 SiteInstance
* instance
) const {
2465 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(instance
);
2467 return proxy
->GetRenderViewHost();
2471 RenderFrameProxyHost
* RenderFrameHostManager::GetRenderFrameProxyHost(
2472 SiteInstance
* instance
) const {
2473 return proxy_hosts_
->Get(instance
->GetId());
2476 std::map
<int, RenderFrameProxyHost
*>
2477 RenderFrameHostManager::GetAllProxyHostsForTesting() {
2478 std::map
<int, RenderFrameProxyHost
*> result
;
2479 result
.insert(proxy_hosts_
->begin(), proxy_hosts_
->end());
2483 void RenderFrameHostManager::CollectOpenerFrameTrees(
2484 std::vector
<FrameTree
*>* opener_frame_trees
,
2485 base::hash_set
<FrameTreeNode
*>* nodes_with_back_links
) {
2486 CHECK(opener_frame_trees
);
2487 opener_frame_trees
->push_back(frame_tree_node_
->frame_tree());
2489 size_t visited_index
= 0;
2490 while (visited_index
< opener_frame_trees
->size()) {
2491 FrameTree
* frame_tree
= (*opener_frame_trees
)[visited_index
];
2493 frame_tree
->ForEach(base::Bind(&OpenerForFrameTreeNode
, visited_index
,
2494 opener_frame_trees
, nodes_with_back_links
));
2498 void RenderFrameHostManager::CreateOpenerProxies(
2499 SiteInstance
* instance
,
2500 FrameTreeNode
* skip_this_node
) {
2501 std::vector
<FrameTree
*> opener_frame_trees
;
2502 base::hash_set
<FrameTreeNode
*> nodes_with_back_links
;
2504 CollectOpenerFrameTrees(&opener_frame_trees
, &nodes_with_back_links
);
2506 // Create opener proxies for frame trees, processing furthest openers from
2507 // this node first and this node last. In the common case without cycles,
2508 // this will ensure that each tree's openers are created before the tree's
2509 // nodes need to reference them.
2510 for (int i
= opener_frame_trees
.size() - 1; i
>= 0; i
--) {
2511 opener_frame_trees
[i
]
2514 ->CreateOpenerProxiesForFrameTree(instance
, skip_this_node
);
2517 // Set openers for nodes in |nodes_with_back_links| in a second pass.
2518 // The proxies created at these FrameTreeNodes in
2519 // CreateOpenerProxiesForFrameTree won't have their opener routing ID
2520 // available when created due to cycles or back links in the opener chain.
2521 // They must have their openers updated as a separate step after proxy
2523 for (const auto& node
: nodes_with_back_links
) {
2524 RenderFrameProxyHost
* proxy
=
2525 node
->render_manager()->GetRenderFrameProxyHost(instance
);
2526 // If there is no proxy, the cycle may involve nodes in the same process,
2527 // or, if this is a subframe, --site-per-process may be off. Either way,
2528 // there's nothing more to do.
2532 int opener_routing_id
=
2533 node
->render_manager()->GetOpenerRoutingID(instance
);
2534 DCHECK_NE(opener_routing_id
, MSG_ROUTING_NONE
);
2535 proxy
->Send(new FrameMsg_UpdateOpener(proxy
->GetRoutingID(),
2536 opener_routing_id
));
2540 void RenderFrameHostManager::CreateOpenerProxiesForFrameTree(
2541 SiteInstance
* instance
,
2542 FrameTreeNode
* skip_this_node
) {
2543 // Currently, this function is only called on main frames. It should
2544 // actually work correctly for subframes as well, so if that need ever
2545 // arises, it should be sufficient to remove this DCHECK.
2546 DCHECK(frame_tree_node_
->IsMainFrame());
2548 if (frame_tree_node_
== skip_this_node
)
2551 FrameTree
* frame_tree
= frame_tree_node_
->frame_tree();
2552 if (SiteIsolationPolicy::AreCrossProcessFramesPossible()) {
2553 // Ensure that all the nodes in the opener's FrameTree have
2554 // RenderFrameProxyHosts for the new SiteInstance. Only pass the node to
2555 // be skipped if it's in the same FrameTree.
2556 if (skip_this_node
&& skip_this_node
->frame_tree() != frame_tree
)
2557 skip_this_node
= nullptr;
2558 frame_tree
->CreateProxiesForSiteInstance(skip_this_node
, instance
);
2560 // If any of the RenderViewHosts (current, pending, or swapped out) for this
2561 // FrameTree has the same SiteInstance, then we can return early and reuse
2562 // them. An exception is if we are in IsSwappedOutStateForbidden mode and
2563 // find a pending RenderViewHost: in this case, we should still create a
2564 // proxy, which will allow communicating with the opener until the pending
2565 // RenderView commits, or if the pending navigation is canceled.
2566 RenderViewHostImpl
* rvh
= frame_tree
->GetRenderViewHost(instance
);
2567 bool need_proxy_for_pending_rvh
=
2568 SiteIsolationPolicy::IsSwappedOutStateForbidden() &&
2569 (rvh
== pending_render_view_host());
2570 if (rvh
&& rvh
->IsRenderViewLive() && !need_proxy_for_pending_rvh
)
2573 if (rvh
&& !rvh
->IsRenderViewLive()) {
2574 EnsureRenderViewInitialized(rvh
, instance
);
2576 // Create a swapped out RenderView in the given SiteInstance if none
2577 // exists. Since an opener can point to a subframe, do this on the root
2578 // frame of the current opener's frame tree.
2579 frame_tree
->root()->render_manager()->
2580 CreateRenderFrame(instance
, nullptr,
2581 CREATE_RF_FOR_MAIN_FRAME_NAVIGATION
|
2582 CREATE_RF_SWAPPED_OUT
| CREATE_RF_HIDDEN
,
2588 int RenderFrameHostManager::GetOpenerRoutingID(SiteInstance
* instance
) {
2589 if (!frame_tree_node_
->opener())
2590 return MSG_ROUTING_NONE
;
2592 return frame_tree_node_
->opener()
2594 ->GetRoutingIdForSiteInstance(instance
);
2597 } // namespace content