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());
407 // If the renderer isn't live, then try to create a new one to satisfy this
408 // navigation request.
409 if (!dest_render_frame_host
->IsRenderFrameLive()) {
410 // Instruct the destination render frame host to set up a Mojo connection
411 // with the new render frame if necessary. Note that this call needs to
412 // occur before initializing the RenderView; the flow of creating the
413 // RenderView can cause browser-side code to execute that expects the this
414 // RFH's ServiceRegistry to be initialized (e.g., if the site is a WebUI
415 // site that is handled via Mojo, then Mojo WebUI code in //chrome will
416 // add a service to this RFH's ServiceRegistry).
417 dest_render_frame_host
->SetUpMojoIfNeeded();
419 // Recreate the opener chain.
420 CreateOpenerProxies(dest_render_frame_host
->GetSiteInstance(),
422 if (!InitRenderView(dest_render_frame_host
->render_view_host(),
426 // Now that we've created a new renderer, be sure to hide it if it isn't
427 // our primary one. Otherwise, we might crash if we try to call Show()
429 if (dest_render_frame_host
!= render_frame_host_
) {
430 if (dest_render_frame_host
->GetView())
431 dest_render_frame_host
->GetView()->Hide();
433 // After a renderer crash we'd have marked the host as invisible, so we
434 // need to set the visibility of the new View to the correct value here
436 if (dest_render_frame_host
->GetView() &&
437 dest_render_frame_host
->render_view_host()->is_hidden() !=
438 delegate_
->IsHidden()) {
439 if (delegate_
->IsHidden()) {
440 dest_render_frame_host
->GetView()->Hide();
442 dest_render_frame_host
->GetView()->Show();
446 // TODO(nasko): This is a very ugly hack. The Chrome extensions process
447 // manager still uses NotificationService and expects to see a
448 // RenderViewHost changed notification after WebContents and
449 // RenderFrameHostManager are completely initialized. This should be
450 // removed once the process manager moves away from NotificationService.
451 // See https://crbug.com/462682.
452 delegate_
->NotifyMainFrameSwappedFromRenderManager(
453 nullptr, render_frame_host_
->render_view_host());
457 // If entry includes the request ID of a request that is being transferred,
458 // the destination render frame will take ownership, so release ownership of
460 if (cross_site_transferring_request_
.get() &&
461 cross_site_transferring_request_
->request_id() ==
462 entry
.transferred_global_request_id()) {
463 cross_site_transferring_request_
->ReleaseRequest();
465 // The navigating RenderFrameHost should take ownership of the
466 // NavigationHandle that came from the transferring RenderFrameHost.
467 DCHECK(transfer_navigation_handle_
);
468 dest_render_frame_host
->SetNavigationHandle(
469 transfer_navigation_handle_
.Pass());
471 DCHECK(!transfer_navigation_handle_
);
473 return dest_render_frame_host
;
476 void RenderFrameHostManager::Stop() {
477 render_frame_host_
->Stop();
479 // If a cross-process navigation is happening, the pending RenderFrameHost
480 // should stop. This will lead to a DidFailProvisionalLoad, which will
481 // properly destroy it.
482 if (pending_render_frame_host_
) {
483 pending_render_frame_host_
->Send(new FrameMsg_Stop(
484 pending_render_frame_host_
->GetRoutingID()));
487 // PlzNavigate: a loading speculative RenderFrameHost should also stop.
488 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
489 switches::kEnableBrowserSideNavigation
)) {
490 if (speculative_render_frame_host_
&&
491 speculative_render_frame_host_
->is_loading()) {
492 speculative_render_frame_host_
->Send(
493 new FrameMsg_Stop(speculative_render_frame_host_
->GetRoutingID()));
498 void RenderFrameHostManager::SetIsLoading(bool is_loading
) {
499 render_frame_host_
->render_view_host()->SetIsLoading(is_loading
);
500 if (pending_render_frame_host_
)
501 pending_render_frame_host_
->render_view_host()->SetIsLoading(is_loading
);
504 bool RenderFrameHostManager::ShouldCloseTabOnUnresponsiveRenderer() {
505 // If we're waiting for a close ACK, then the tab should close whether there's
506 // a navigation in progress or not. Unfortunately, we also need to check for
507 // cases that we arrive here with no navigation in progress, since there are
508 // some tab closure paths that don't set is_waiting_for_close_ack to true.
509 // TODO(creis): Clean this up in http://crbug.com/418266.
510 if (!pending_render_frame_host_
||
511 render_frame_host_
->render_view_host()->is_waiting_for_close_ack())
514 // We should always have a pending RFH when there's a cross-process navigation
515 // in progress. Sanity check this for http://crbug.com/276333.
516 CHECK(pending_render_frame_host_
);
518 // Unload handlers run in the background, so we should never get an
519 // unresponsiveness warning for them.
520 CHECK(!render_frame_host_
->IsWaitingForUnloadACK());
522 // If the tab becomes unresponsive during beforeunload while doing a
523 // cross-process navigation, proceed with the navigation. (This assumes that
524 // the pending RenderFrameHost is still responsive.)
525 if (render_frame_host_
->is_waiting_for_beforeunload_ack()) {
526 // Haven't gotten around to starting the request, because we're still
527 // waiting for the beforeunload handler to finish. We'll pretend that it
528 // did finish, to let the navigation proceed. Note that there's a danger
529 // that the beforeunload handler will later finish and possibly return
530 // false (meaning the navigation should not proceed), but we'll ignore it
531 // in this case because it took too long.
532 if (pending_render_frame_host_
->are_navigations_suspended()) {
533 pending_render_frame_host_
->SetNavigationsSuspended(
534 false, base::TimeTicks::Now());
540 void RenderFrameHostManager::OnBeforeUnloadACK(
541 bool for_cross_site_transition
,
543 const base::TimeTicks
& proceed_time
) {
544 if (for_cross_site_transition
) {
545 DCHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch(
546 switches::kEnableBrowserSideNavigation
));
547 // Ignore if we're not in a cross-process navigation.
548 if (!pending_render_frame_host_
)
552 // Ok to unload the current page, so proceed with the cross-process
553 // navigation. Note that if navigations are not currently suspended, it
554 // might be because the renderer was deemed unresponsive and this call was
555 // already made by ShouldCloseTabOnUnresponsiveRenderer. In that case, it
556 // is ok to do nothing here.
557 if (pending_render_frame_host_
&&
558 pending_render_frame_host_
->are_navigations_suspended()) {
559 pending_render_frame_host_
->SetNavigationsSuspended(false,
563 // Current page says to cancel.
567 // Non-cross-process transition means closing the entire tab.
568 bool proceed_to_fire_unload
;
569 delegate_
->BeforeUnloadFiredFromRenderManager(proceed
, proceed_time
,
570 &proceed_to_fire_unload
);
572 if (proceed_to_fire_unload
) {
573 // If we're about to close the tab and there's a pending RFH, cancel it.
574 // Otherwise, if the navigation in the pending RFH completes before the
575 // close in the current RFH, we'll lose the tab close.
576 if (pending_render_frame_host_
) {
580 // PlzNavigate: clean up the speculative RenderFrameHost if there is one.
581 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
582 switches::kEnableBrowserSideNavigation
) &&
583 speculative_render_frame_host_
) {
587 // This is not a cross-process navigation; the tab is being closed.
588 render_frame_host_
->render_view_host()->ClosePage();
593 void RenderFrameHostManager::OnCrossSiteResponse(
594 RenderFrameHostImpl
* pending_render_frame_host
,
595 const GlobalRequestID
& global_request_id
,
596 scoped_ptr
<CrossSiteTransferringRequest
> cross_site_transferring_request
,
597 const std::vector
<GURL
>& transfer_url_chain
,
598 const Referrer
& referrer
,
599 ui::PageTransition page_transition
,
600 bool should_replace_current_entry
) {
601 // We should only get here for transfer navigations. Most cross-process
602 // navigations can just continue and wait to run the unload handler (by
603 // swapping out) when the new navigation commits.
604 CHECK(cross_site_transferring_request
);
606 // A transfer should only have come from our pending or current RFH.
607 // TODO(creis): We need to handle the case that the pending RFH has changed
608 // in the mean time, while this was being posted from the IO thread. We
609 // should probably cancel the request in that case.
610 DCHECK(pending_render_frame_host
== pending_render_frame_host_
||
611 pending_render_frame_host
== render_frame_host_
);
613 // Store the transferring request so that we can release it if the transfer
614 // navigation matches.
615 cross_site_transferring_request_
= cross_site_transferring_request
.Pass();
617 // Store the NavigationHandle to give it to the appropriate RenderFrameHost
618 // after it started navigating.
619 transfer_navigation_handle_
=
620 pending_render_frame_host
->PassNavigationHandleOwnership();
621 DCHECK(transfer_navigation_handle_
);
623 // Sanity check that the params are for the correct frame and process.
624 // These should match the RenderFrameHost that made the request.
625 // If it started as a cross-process navigation via OpenURL, this is the
626 // pending one. If it wasn't cross-process until the transfer, this is
628 int render_frame_id
= pending_render_frame_host_
629 ? pending_render_frame_host_
->GetRoutingID()
630 : render_frame_host_
->GetRoutingID();
631 DCHECK_EQ(render_frame_id
, pending_render_frame_host
->GetRoutingID());
632 int process_id
= pending_render_frame_host_
?
633 pending_render_frame_host_
->GetProcess()->GetID() :
634 render_frame_host_
->GetProcess()->GetID();
635 DCHECK_EQ(process_id
, global_request_id
.child_id
);
637 // Treat the last URL in the chain as the destination and the remainder as
638 // the redirect chain.
639 CHECK(transfer_url_chain
.size());
640 GURL transfer_url
= transfer_url_chain
.back();
641 std::vector
<GURL
> rest_of_chain
= transfer_url_chain
;
642 rest_of_chain
.pop_back();
644 // We don't know whether the original request had |user_action| set to true.
645 // However, since we force the navigation to be in the current tab, it
647 pending_render_frame_host
->frame_tree_node()->navigator()->RequestTransferURL(
648 pending_render_frame_host
, transfer_url
, nullptr, rest_of_chain
, referrer
,
649 page_transition
, CURRENT_TAB
, global_request_id
,
650 should_replace_current_entry
, true);
652 // The transferring request was only needed during the RequestTransferURL
653 // call, so it is safe to clear at this point.
654 cross_site_transferring_request_
.reset();
656 // If the navigation continued, the NavigationHandle should have been
657 // transfered to a RenderFrameHost. In the other cases, it should be cleared.
658 transfer_navigation_handle_
.reset();
661 void RenderFrameHostManager::DidNavigateFrame(
662 RenderFrameHostImpl
* render_frame_host
,
663 bool was_caused_by_user_gesture
) {
664 CommitPendingIfNecessary(render_frame_host
, was_caused_by_user_gesture
);
666 // Make sure any dynamic changes to this frame's sandbox flags that were made
667 // prior to navigation take effect.
668 CommitPendingSandboxFlags();
671 void RenderFrameHostManager::CommitPendingIfNecessary(
672 RenderFrameHostImpl
* render_frame_host
,
673 bool was_caused_by_user_gesture
) {
674 if (!pending_render_frame_host_
&& !speculative_render_frame_host_
) {
675 DCHECK_IMPLIES(should_reuse_web_ui_
, web_ui_
);
677 // We should only hear this from our current renderer.
678 DCHECK_EQ(render_frame_host_
, render_frame_host
);
680 // Even when there is no pending RVH, there may be a pending Web UI.
681 if (pending_web_ui() || speculative_web_ui_
)
686 if (render_frame_host
== pending_render_frame_host_
||
687 render_frame_host
== speculative_render_frame_host_
) {
688 // The pending cross-process navigation completed, so show the renderer.
690 } else if (render_frame_host
== render_frame_host_
) {
691 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
692 switches::kEnableBrowserSideNavigation
)) {
695 if (was_caused_by_user_gesture
) {
696 // A navigation in the original page has taken place. Cancel the
697 // pending one. Only do it for user gesture originated navigations to
698 // prevent page doing any shenanigans to prevent user from navigating.
699 // See https://code.google.com/p/chromium/issues/detail?id=75195
704 // No one else should be sending us DidNavigate in this state.
709 void RenderFrameHostManager::DidChangeOpener(
710 int opener_routing_id
,
711 SiteInstance
* source_site_instance
) {
712 FrameTreeNode
* opener
= nullptr;
713 if (opener_routing_id
!= MSG_ROUTING_NONE
) {
714 RenderFrameHostImpl
* opener_rfhi
= RenderFrameHostImpl::FromID(
715 source_site_instance
->GetProcess()->GetID(), opener_routing_id
);
716 // If |opener_rfhi| is null, the opener RFH has already disappeared. In
717 // this case, clear the opener rather than keeping the old opener around.
719 opener
= opener_rfhi
->frame_tree_node();
722 if (frame_tree_node_
->opener() == opener
)
725 frame_tree_node_
->SetOpener(opener
);
727 for (const auto& pair
: *proxy_hosts_
) {
728 if (pair
.second
->GetSiteInstance() == source_site_instance
)
730 pair
.second
->UpdateOpener();
733 if (render_frame_host_
->GetSiteInstance() != source_site_instance
)
734 render_frame_host_
->UpdateOpener();
736 // Notify the pending and speculative RenderFrameHosts as well. This is
737 // necessary in case a process swap has started while the message was in
739 if (pending_render_frame_host_
&&
740 pending_render_frame_host_
->GetSiteInstance() != source_site_instance
) {
741 pending_render_frame_host_
->UpdateOpener();
744 if (speculative_render_frame_host_
&&
745 speculative_render_frame_host_
->GetSiteInstance() !=
746 source_site_instance
) {
747 speculative_render_frame_host_
->UpdateOpener();
751 void RenderFrameHostManager::CommitPendingSandboxFlags() {
752 // Return early if there were no pending sandbox flags updates.
753 if (!frame_tree_node_
->CommitPendingSandboxFlags())
756 // Sandbox flags updates can only happen when the frame has a parent.
757 CHECK(frame_tree_node_
->parent());
759 // Notify all of the frame's proxies about updated sandbox flags, excluding
760 // the parent process since it already knows the latest flags.
761 SiteInstance
* parent_site_instance
=
762 frame_tree_node_
->parent()->current_frame_host()->GetSiteInstance();
763 for (const auto& pair
: *proxy_hosts_
) {
764 if (pair
.second
->GetSiteInstance() != parent_site_instance
) {
765 pair
.second
->Send(new FrameMsg_DidUpdateSandboxFlags(
766 pair
.second
->GetRoutingID(),
767 frame_tree_node_
->current_replication_state().sandbox_flags
));
772 void RenderFrameHostManager::RendererProcessClosing(
773 RenderProcessHost
* render_process_host
) {
774 // Remove any swapped out RVHs from this process, so that we don't try to
775 // swap them back in while the process is exiting. Start by finding them,
776 // since there could be more than one.
777 std::list
<int> ids_to_remove
;
778 // Do not remove proxies in the dead process that still have active frame
779 // count though, we just reset them to be uninitialized.
780 std::list
<int> ids_to_keep
;
781 for (const auto& pair
: *proxy_hosts_
) {
782 RenderFrameProxyHost
* proxy
= pair
.second
;
783 if (proxy
->GetProcess() != render_process_host
)
786 if (static_cast<SiteInstanceImpl
*>(proxy
->GetSiteInstance())
787 ->active_frame_count() >= 1U) {
788 ids_to_keep
.push_back(pair
.first
);
790 ids_to_remove
.push_back(pair
.first
);
795 while (!ids_to_remove
.empty()) {
796 proxy_hosts_
->Remove(ids_to_remove
.back());
797 ids_to_remove
.pop_back();
800 while (!ids_to_keep
.empty()) {
801 frame_tree_node_
->frame_tree()->ForEach(
803 &RenderFrameHostManager::ResetProxiesInSiteInstance
,
804 ids_to_keep
.back()));
805 ids_to_keep
.pop_back();
809 void RenderFrameHostManager::SwapOutOldFrame(
810 scoped_ptr
<RenderFrameHostImpl
> old_render_frame_host
) {
811 TRACE_EVENT1("navigation", "RenderFrameHostManager::SwapOutOldFrame",
812 "FrameTreeNode id", frame_tree_node_
->frame_tree_node_id());
814 // Tell the renderer to suppress any further modal dialogs so that we can swap
815 // it out. This must be done before canceling any current dialog, in case
816 // there is a loop creating additional dialogs.
817 // TODO(creis): Handle modal dialogs in subframe processes.
818 old_render_frame_host
->render_view_host()->SuppressDialogsUntilSwapOut();
820 // Now close any modal dialogs that would prevent us from swapping out. This
821 // must be done separately from SwapOut, so that the PageGroupLoadDeferrer is
822 // no longer on the stack when we send the SwapOut message.
823 delegate_
->CancelModalDialogsForRenderManager();
825 // If the old RFH is not live, just return as there is no further work to do.
826 // It will be deleted and there will be no proxy created.
827 int32 old_site_instance_id
=
828 old_render_frame_host
->GetSiteInstance()->GetId();
829 if (!old_render_frame_host
->IsRenderFrameLive()) {
830 ShutdownProxiesIfLastActiveFrameInSiteInstance(old_render_frame_host
.get());
834 // If there are no active frames besides this one, we can delete the old
835 // RenderFrameHost once it runs its unload handler, without replacing it with
837 size_t active_frame_count
=
838 old_render_frame_host
->GetSiteInstance()->active_frame_count();
839 if (active_frame_count
<= 1) {
840 // Clear out any proxies from this SiteInstance, in case this was the
841 // last one keeping other proxies alive.
842 ShutdownProxiesIfLastActiveFrameInSiteInstance(old_render_frame_host
.get());
844 // Tell the old RenderFrameHost to swap out, with no proxy to replace it.
845 old_render_frame_host
->SwapOut(nullptr, true);
846 MoveToPendingDeleteHosts(old_render_frame_host
.Pass());
850 // Otherwise there are active views and we need a proxy for the old RFH.
851 // (There should not be one yet.)
852 RenderFrameProxyHost
* proxy
= new RenderFrameProxyHost(
853 old_render_frame_host
->GetSiteInstance(),
854 old_render_frame_host
->render_view_host(), frame_tree_node_
);
855 proxy_hosts_
->Add(old_site_instance_id
, make_scoped_ptr(proxy
));
857 // Tell the old RenderFrameHost to swap out and be replaced by the proxy.
858 old_render_frame_host
->SwapOut(proxy
, true);
860 // SwapOut creates a RenderFrameProxy, so set the proxy to be initialized.
861 proxy
->set_render_frame_proxy_created(true);
863 if (SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
864 // In --site-per-process, frames delete their RFH rather than storing it
865 // in the proxy. Schedule it for deletion once the SwapOutACK comes in.
866 // TODO(creis): This will be the default when we remove swappedout://.
867 MoveToPendingDeleteHosts(old_render_frame_host
.Pass());
869 // We shouldn't get here for subframes, since we only swap subframes when
870 // --site-per-process is used.
871 DCHECK(frame_tree_node_
->IsMainFrame());
873 // The old RenderFrameHost will stay alive inside the proxy so that existing
874 // JavaScript window references to it stay valid.
875 proxy
->TakeFrameHostOwnership(old_render_frame_host
.Pass());
879 void RenderFrameHostManager::DiscardUnusedFrame(
880 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
) {
881 // TODO(carlosk): this code is very similar to what can be found in
882 // SwapOutOldFrame and we should see that these are unified at some point.
884 // If the SiteInstance for the pending RFH is being used by others don't
885 // delete the RFH. Just swap it out and it can be reused at a later point.
886 // In --site-per-process, RenderFrameHosts are not kept around and are
887 // deleted when not used, replaced by RenderFrameProxyHosts.
888 SiteInstanceImpl
* site_instance
= render_frame_host
->GetSiteInstance();
889 if (site_instance
->HasSite() && site_instance
->active_frame_count() > 1) {
890 // Any currently suspended navigations are no longer needed.
891 render_frame_host
->CancelSuspendedNavigations();
893 RenderFrameProxyHost
* proxy
= new RenderFrameProxyHost(
894 site_instance
, render_frame_host
->render_view_host(), frame_tree_node_
);
895 proxy_hosts_
->Add(site_instance
->GetId(), make_scoped_ptr(proxy
));
897 // Check if the RenderFrameHost is already swapped out, to avoid swapping it
899 if (!render_frame_host
->is_swapped_out())
900 render_frame_host
->SwapOut(proxy
, false);
902 if (!SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
903 DCHECK(frame_tree_node_
->IsMainFrame());
904 proxy
->TakeFrameHostOwnership(render_frame_host
.Pass());
908 if (render_frame_host
) {
909 // We won't be coming back, so delete this one.
910 ShutdownProxiesIfLastActiveFrameInSiteInstance(render_frame_host
.get());
911 render_frame_host
.reset();
915 void RenderFrameHostManager::MoveToPendingDeleteHosts(
916 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
) {
917 // |render_frame_host| will be deleted when its SwapOut ACK is received, or
918 // when the timer times out, or when the RFHM itself is deleted (whichever
920 pending_delete_hosts_
.push_back(
921 linked_ptr
<RenderFrameHostImpl
>(render_frame_host
.release()));
924 bool RenderFrameHostManager::IsPendingDeletion(
925 RenderFrameHostImpl
* render_frame_host
) {
926 for (const auto& rfh
: pending_delete_hosts_
) {
927 if (rfh
== render_frame_host
)
933 bool RenderFrameHostManager::DeleteFromPendingList(
934 RenderFrameHostImpl
* render_frame_host
) {
935 for (RFHPendingDeleteList::iterator iter
= pending_delete_hosts_
.begin();
936 iter
!= pending_delete_hosts_
.end();
938 if (*iter
== render_frame_host
) {
939 pending_delete_hosts_
.erase(iter
);
946 void RenderFrameHostManager::ResetProxyHosts() {
947 proxy_hosts_
->Clear();
951 void RenderFrameHostManager::DidCreateNavigationRequest(
952 const NavigationRequest
& request
) {
953 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
954 switches::kEnableBrowserSideNavigation
));
955 // Clean up any state in case there's an ongoing navigation.
956 // TODO(carlosk): remove this cleanup here once we properly cancel ongoing
960 RenderFrameHostImpl
* dest_rfh
= GetFrameHostForNavigation(request
);
965 RenderFrameHostImpl
* RenderFrameHostManager::GetFrameHostForNavigation(
966 const NavigationRequest
& request
) {
967 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
968 switches::kEnableBrowserSideNavigation
));
970 SiteInstance
* current_site_instance
= render_frame_host_
->GetSiteInstance();
972 SiteInstance
* candidate_site_instance
=
973 speculative_render_frame_host_
974 ? speculative_render_frame_host_
->GetSiteInstance()
977 scoped_refptr
<SiteInstance
> dest_site_instance
= GetSiteInstanceForNavigation(
978 request
.common_params().url
, request
.source_site_instance(),
979 request
.dest_site_instance(), candidate_site_instance
,
980 request
.common_params().transition
,
981 request
.restore_type() != NavigationEntryImpl::RESTORE_NONE
,
982 request
.is_view_source());
984 // The appropriate RenderFrameHost to commit the navigation.
985 RenderFrameHostImpl
* navigation_rfh
= nullptr;
987 // Renderer-initiated main frame navigations that may require a SiteInstance
988 // swap are sent to the browser via the OpenURL IPC and are afterwards treated
989 // as browser-initiated navigations. NavigationRequests marked as
990 // renderer-initiated are created by receiving a BeginNavigation IPC, and will
991 // then proceed in the same renderer that sent the IPC due to the condition
993 // Subframe navigations will use the current renderer, unless
994 // --site-per-process is enabled.
995 // TODO(carlosk): Have renderer-initated main frame navigations swap processes
996 // if needed when it no longer breaks OAuth popups (see
997 // https://crbug.com/440266).
998 bool is_main_frame
= frame_tree_node_
->IsMainFrame();
999 if (current_site_instance
== dest_site_instance
.get() ||
1000 (!request
.browser_initiated() && is_main_frame
) ||
1001 (!is_main_frame
&& !dest_site_instance
->RequiresDedicatedProcess() &&
1002 !current_site_instance
->RequiresDedicatedProcess())) {
1003 // Reuse the current RFH if its SiteInstance matches the the navigation's
1004 // or if this is a subframe navigation. We only swap RFHs for subframes when
1005 // --site-per-process is enabled.
1006 CleanUpNavigation();
1007 navigation_rfh
= render_frame_host_
.get();
1009 // As SiteInstances are the same, check if the WebUI should be reused.
1010 const NavigationEntry
* current_navigation_entry
=
1011 delegate_
->GetLastCommittedNavigationEntryForRenderManager();
1012 should_reuse_web_ui_
= ShouldReuseWebUI(current_navigation_entry
,
1013 request
.common_params().url
);
1014 if (!should_reuse_web_ui_
) {
1015 speculative_web_ui_
= CreateWebUI(request
.common_params().url
,
1016 request
.bindings());
1017 // Make sure the current RenderViewHost has the right bindings.
1018 if (speculative_web_ui() &&
1019 !render_frame_host_
->GetProcess()->IsForGuestsOnly()) {
1020 render_frame_host_
->render_view_host()->AllowBindings(
1021 speculative_web_ui()->GetBindings());
1025 // If the SiteInstance for the final URL doesn't match the one from the
1026 // speculatively created RenderFrameHost, create a new RenderFrameHost using
1027 // this new SiteInstance.
1028 if (!speculative_render_frame_host_
||
1029 speculative_render_frame_host_
->GetSiteInstance() !=
1030 dest_site_instance
.get()) {
1031 CleanUpNavigation();
1032 bool success
= CreateSpeculativeRenderFrameHost(
1033 request
.common_params().url
, current_site_instance
,
1034 dest_site_instance
.get(), request
.bindings());
1037 DCHECK(speculative_render_frame_host_
);
1038 navigation_rfh
= speculative_render_frame_host_
.get();
1040 // Check if our current RFH is live.
1041 if (!render_frame_host_
->IsRenderFrameLive()) {
1042 // The current RFH is not live. There's no reason to sit around with a
1043 // sad tab or a newly created RFH while we wait for the navigation to
1044 // complete. Just switch to the speculative RFH now and go back to normal.
1045 // (Note that we don't care about on{before}unload handlers if the current
1050 DCHECK(navigation_rfh
&&
1051 (navigation_rfh
== render_frame_host_
.get() ||
1052 navigation_rfh
== speculative_render_frame_host_
.get()));
1054 // If the RenderFrame that needs to navigate is not live (its process was just
1055 // created or has crashed), initialize it.
1056 if (!navigation_rfh
->IsRenderFrameLive()) {
1057 // Recreate the opener chain.
1058 CreateOpenerProxies(navigation_rfh
->GetSiteInstance(), frame_tree_node_
);
1059 if (!InitRenderView(navigation_rfh
->render_view_host(), MSG_ROUTING_NONE
)) {
1063 if (navigation_rfh
== render_frame_host_
) {
1064 // TODO(nasko): This is a very ugly hack. The Chrome extensions process
1065 // manager still uses NotificationService and expects to see a
1066 // RenderViewHost changed notification after WebContents and
1067 // RenderFrameHostManager are completely initialized. This should be
1068 // removed once the process manager moves away from NotificationService.
1069 // See https://crbug.com/462682.
1070 delegate_
->NotifyMainFrameSwappedFromRenderManager(
1071 nullptr, render_frame_host_
->render_view_host());
1075 return navigation_rfh
;
1079 void RenderFrameHostManager::CleanUpNavigation() {
1080 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
1081 switches::kEnableBrowserSideNavigation
));
1082 speculative_web_ui_
.reset();
1083 should_reuse_web_ui_
= false;
1084 if (speculative_render_frame_host_
)
1085 DiscardUnusedFrame(UnsetSpeculativeRenderFrameHost());
1089 scoped_ptr
<RenderFrameHostImpl
>
1090 RenderFrameHostManager::UnsetSpeculativeRenderFrameHost() {
1091 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
1092 switches::kEnableBrowserSideNavigation
));
1093 speculative_render_frame_host_
->GetProcess()->RemovePendingView();
1094 return speculative_render_frame_host_
.Pass();
1097 void RenderFrameHostManager::OnDidStartLoading() {
1098 for (const auto& pair
: *proxy_hosts_
) {
1100 new FrameMsg_DidStartLoading(pair
.second
->GetRoutingID()));
1104 void RenderFrameHostManager::OnDidStopLoading() {
1105 for (const auto& pair
: *proxy_hosts_
) {
1106 pair
.second
->Send(new FrameMsg_DidStopLoading(pair
.second
->GetRoutingID()));
1110 void RenderFrameHostManager::OnDidUpdateName(const std::string
& name
) {
1111 // The window.name message may be sent outside of --site-per-process when
1112 // report_frame_name_changes renderer preference is set (used by
1113 // WebView). Don't send the update to proxies in those cases.
1114 // TODO(nick,nasko): Should this be IsSwappedOutStateForbidden, to match
1115 // OnDidUpdateOrigin?
1116 if (!SiteIsolationPolicy::AreCrossProcessFramesPossible())
1119 for (const auto& pair
: *proxy_hosts_
) {
1121 new FrameMsg_DidUpdateName(pair
.second
->GetRoutingID(), name
));
1125 void RenderFrameHostManager::OnDidUpdateOrigin(const url::Origin
& origin
) {
1126 if (!SiteIsolationPolicy::IsSwappedOutStateForbidden())
1129 for (const auto& pair
: *proxy_hosts_
) {
1131 new FrameMsg_DidUpdateOrigin(pair
.second
->GetRoutingID(), origin
));
1135 RenderFrameHostManager::SiteInstanceDescriptor::SiteInstanceDescriptor(
1136 BrowserContext
* browser_context
,
1138 bool related_to_current
)
1139 : existing_site_instance(nullptr),
1140 new_is_related_to_current(related_to_current
) {
1141 new_site_url
= SiteInstance::GetSiteForURL(browser_context
, dest_url
);
1145 bool RenderFrameHostManager::ClearProxiesInSiteInstance(
1146 int32 site_instance_id
,
1147 FrameTreeNode
* node
) {
1148 RenderFrameProxyHost
* proxy
=
1149 node
->render_manager()->proxy_hosts_
->Get(site_instance_id
);
1151 // Delete the proxy. If it is for a main frame (and thus the RFH is stored
1152 // in the proxy) and it was still pending swap out, move the RFH to the
1153 // pending deletion list first.
1154 if (node
->IsMainFrame() &&
1155 proxy
->render_frame_host() &&
1156 proxy
->render_frame_host()->rfh_state() ==
1157 RenderFrameHostImpl::STATE_PENDING_SWAP_OUT
) {
1158 DCHECK(!SiteIsolationPolicy::IsSwappedOutStateForbidden());
1159 scoped_ptr
<RenderFrameHostImpl
> swapped_out_rfh
=
1160 proxy
->PassFrameHostOwnership();
1161 node
->render_manager()->MoveToPendingDeleteHosts(swapped_out_rfh
.Pass());
1163 node
->render_manager()->proxy_hosts_
->Remove(site_instance_id
);
1170 bool RenderFrameHostManager::ResetProxiesInSiteInstance(int32 site_instance_id
,
1171 FrameTreeNode
* node
) {
1172 RenderFrameProxyHost
* proxy
=
1173 node
->render_manager()->proxy_hosts_
->Get(site_instance_id
);
1175 proxy
->set_render_frame_proxy_created(false);
1180 bool RenderFrameHostManager::ShouldTransitionCrossSite() {
1181 // The logic below is weaker than "are all sites isolated" -- it asks instead,
1182 // "is any site isolated". That's appropriate here since we're just trying to
1183 // figure out if we're in any kind of site isolated mode, and in which case,
1184 // we ignore the kSingleProcess and kProcessPerTab settings.
1186 // TODO(nick): Move all handling of kSingleProcess/kProcessPerTab into
1187 // SiteIsolationPolicy so we have a consistent behavior around the interaction
1188 // of the process model flags.
1189 if (SiteIsolationPolicy::AreCrossProcessFramesPossible())
1192 // False in the single-process mode, as it makes RVHs to accumulate
1193 // in swapped_out_hosts_.
1194 // True if we are using process-per-site-instance (default) or
1195 // process-per-site (kProcessPerSite).
1196 // TODO(nick): Move handling of kSingleProcess and kProcessPerTab into
1197 // SiteIsolationPolicy.
1198 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
1199 switches::kSingleProcess
) &&
1200 !base::CommandLine::ForCurrentProcess()->HasSwitch(
1201 switches::kProcessPerTab
);
1204 bool RenderFrameHostManager::ShouldSwapBrowsingInstancesForNavigation(
1205 const GURL
& current_effective_url
,
1206 bool current_is_view_source_mode
,
1207 SiteInstance
* new_site_instance
,
1208 const GURL
& new_effective_url
,
1209 bool new_is_view_source_mode
) const {
1210 // If new_entry already has a SiteInstance, assume it is correct. We only
1211 // need to force a swap if it is in a different BrowsingInstance.
1212 if (new_site_instance
) {
1213 return !new_site_instance
->IsRelatedSiteInstance(
1214 render_frame_host_
->GetSiteInstance());
1217 // Check for reasons to swap processes even if we are in a process model that
1218 // doesn't usually swap (e.g., process-per-tab). Any time we return true,
1219 // the new_entry will be rendered in a new SiteInstance AND BrowsingInstance.
1220 BrowserContext
* browser_context
=
1221 delegate_
->GetControllerForRenderManager().GetBrowserContext();
1223 // Don't force a new BrowsingInstance for debug URLs that are handled in the
1224 // renderer process, like javascript: or chrome://crash.
1225 if (IsRendererDebugURL(new_effective_url
))
1228 // For security, we should transition between processes when one is a Web UI
1229 // page and one isn't.
1230 if (ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
1231 render_frame_host_
->GetProcess()->GetID()) ||
1232 WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
1233 browser_context
, current_effective_url
)) {
1234 // If so, force a swap if destination is not an acceptable URL for Web UI.
1235 // Here, data URLs are never allowed.
1236 if (!WebUIControllerFactoryRegistry::GetInstance()->IsURLAcceptableForWebUI(
1237 browser_context
, new_effective_url
)) {
1241 // Force a swap if it's a Web UI URL.
1242 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
1243 browser_context
, new_effective_url
)) {
1248 // Check with the content client as well. Important to pass
1249 // current_effective_url here, which uses the SiteInstance's site if there is
1250 // no current_entry.
1251 if (GetContentClient()->browser()->ShouldSwapBrowsingInstancesForNavigation(
1252 render_frame_host_
->GetSiteInstance(),
1253 current_effective_url
, new_effective_url
)) {
1257 // We can't switch a RenderView between view source and non-view source mode
1258 // without screwing up the session history sometimes (when navigating between
1259 // "view-source:http://foo.com/" and "http://foo.com/", Blink doesn't treat
1260 // it as a new navigation). So require a BrowsingInstance switch.
1261 if (current_is_view_source_mode
!= new_is_view_source_mode
)
1267 bool RenderFrameHostManager::ShouldReuseWebUI(
1268 const NavigationEntry
* current_entry
,
1269 const GURL
& new_url
) const {
1270 NavigationControllerImpl
& controller
=
1271 delegate_
->GetControllerForRenderManager();
1272 return current_entry
&& web_ui_
&&
1273 (WebUIControllerFactoryRegistry::GetInstance()->GetWebUIType(
1274 controller
.GetBrowserContext(), current_entry
->GetURL()) ==
1275 WebUIControllerFactoryRegistry::GetInstance()->GetWebUIType(
1276 controller
.GetBrowserContext(), new_url
));
1279 SiteInstance
* RenderFrameHostManager::GetSiteInstanceForNavigation(
1280 const GURL
& dest_url
,
1281 SiteInstance
* source_instance
,
1282 SiteInstance
* dest_instance
,
1283 SiteInstance
* candidate_instance
,
1284 ui::PageTransition transition
,
1285 bool dest_is_restore
,
1286 bool dest_is_view_source_mode
) {
1287 SiteInstance
* current_instance
= render_frame_host_
->GetSiteInstance();
1289 // We do not currently swap processes for navigations in webview tag guests.
1290 if (current_instance
->GetSiteURL().SchemeIs(kGuestScheme
))
1291 return current_instance
;
1293 // Determine if we need a new BrowsingInstance for this entry. If true, this
1294 // implies that it will get a new SiteInstance (and likely process), and that
1295 // other tabs in the current BrowsingInstance will be unable to script it.
1296 // This is used for cases that require a process swap even in the
1297 // process-per-tab model, such as WebUI pages.
1298 // TODO(clamy): Remove the dependency on the current entry.
1299 const NavigationEntry
* current_entry
=
1300 delegate_
->GetLastCommittedNavigationEntryForRenderManager();
1301 BrowserContext
* browser_context
=
1302 delegate_
->GetControllerForRenderManager().GetBrowserContext();
1303 const GURL
& current_effective_url
= current_entry
?
1304 SiteInstanceImpl::GetEffectiveURL(browser_context
,
1305 current_entry
->GetURL()) :
1306 render_frame_host_
->GetSiteInstance()->GetSiteURL();
1307 bool current_is_view_source_mode
= current_entry
?
1308 current_entry
->IsViewSourceMode() : dest_is_view_source_mode
;
1309 bool force_swap
= ShouldSwapBrowsingInstancesForNavigation(
1310 current_effective_url
,
1311 current_is_view_source_mode
,
1313 SiteInstanceImpl::GetEffectiveURL(browser_context
, dest_url
),
1314 dest_is_view_source_mode
);
1315 SiteInstanceDescriptor new_instance_descriptor
=
1316 SiteInstanceDescriptor(current_instance
);
1317 if (ShouldTransitionCrossSite() || force_swap
) {
1318 new_instance_descriptor
= DetermineSiteInstanceForURL(
1319 dest_url
, source_instance
, current_instance
, dest_instance
, transition
,
1320 dest_is_restore
, dest_is_view_source_mode
, force_swap
);
1323 SiteInstance
* new_instance
=
1324 ConvertToSiteInstance(new_instance_descriptor
, candidate_instance
);
1326 // If |force_swap| is true, we must use a different SiteInstance than the
1327 // current one. If we didn't, we would have two RenderFrameHosts in the same
1328 // SiteInstance and the same frame, resulting in page_id conflicts for their
1329 // NavigationEntries.
1331 CHECK_NE(new_instance
, current_instance
);
1332 return new_instance
;
1335 RenderFrameHostManager::SiteInstanceDescriptor
1336 RenderFrameHostManager::DetermineSiteInstanceForURL(
1337 const GURL
& dest_url
,
1338 SiteInstance
* source_instance
,
1339 SiteInstance
* current_instance
,
1340 SiteInstance
* dest_instance
,
1341 ui::PageTransition transition
,
1342 bool dest_is_restore
,
1343 bool dest_is_view_source_mode
,
1344 bool force_browsing_instance_swap
) {
1345 SiteInstanceImpl
* current_instance_impl
=
1346 static_cast<SiteInstanceImpl
*>(current_instance
);
1347 NavigationControllerImpl
& controller
=
1348 delegate_
->GetControllerForRenderManager();
1349 BrowserContext
* browser_context
= controller
.GetBrowserContext();
1351 // If the entry has an instance already we should use it.
1352 if (dest_instance
) {
1353 // If we are forcing a swap, this should be in a different BrowsingInstance.
1354 if (force_browsing_instance_swap
) {
1355 CHECK(!dest_instance
->IsRelatedSiteInstance(
1356 render_frame_host_
->GetSiteInstance()));
1358 return SiteInstanceDescriptor(dest_instance
);
1361 // If a swap is required, we need to force the SiteInstance AND
1362 // BrowsingInstance to be different ones, using CreateForURL.
1363 if (force_browsing_instance_swap
)
1364 return SiteInstanceDescriptor(browser_context
, dest_url
, false);
1366 // (UGLY) HEURISTIC, process-per-site only:
1368 // If this navigation is generated, then it probably corresponds to a search
1369 // query. Given that search results typically lead to users navigating to
1370 // other sites, we don't really want to use the search engine hostname to
1371 // determine the site instance for this navigation.
1373 // NOTE: This can be removed once we have a way to transition between
1374 // RenderViews in response to a link click.
1376 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1377 switches::kProcessPerSite
) &&
1378 ui::PageTransitionCoreTypeIs(transition
, ui::PAGE_TRANSITION_GENERATED
)) {
1379 return SiteInstanceDescriptor(current_instance_impl
);
1382 // If we haven't used our SiteInstance (and thus RVH) yet, then we can use it
1383 // for this entry. We won't commit the SiteInstance to this site until the
1384 // navigation commits (in DidNavigate), unless the navigation entry was
1385 // restored or it's a Web UI as described below.
1386 if (!current_instance_impl
->HasSite()) {
1387 // If we've already created a SiteInstance for our destination, we don't
1388 // want to use this unused SiteInstance; use the existing one. (We don't
1389 // do this check if the current_instance has a site, because for now, we
1390 // want to compare against the current URL and not the SiteInstance's site.
1391 // In this case, there is no current URL, so comparing against the site is
1392 // ok. See additional comments below.)
1394 // Also, if the URL should use process-per-site mode and there is an
1395 // existing process for the site, we should use it. We can call
1396 // GetRelatedSiteInstance() for this, which will eagerly set the site and
1397 // thus use the correct process.
1398 bool use_process_per_site
=
1399 RenderProcessHost::ShouldUseProcessPerSite(browser_context
, dest_url
) &&
1400 RenderProcessHostImpl::GetProcessHostForSite(browser_context
, dest_url
);
1401 if (current_instance_impl
->HasRelatedSiteInstance(dest_url
) ||
1402 use_process_per_site
) {
1403 return SiteInstanceDescriptor(browser_context
, dest_url
, true);
1406 // For extensions, Web UI URLs (such as the new tab page), and apps we do
1407 // not want to use the |current_instance_impl| if it has no site, since it
1408 // will have a RenderProcessHost of PRIV_NORMAL. Create a new SiteInstance
1409 // for this URL instead (with the correct process type).
1410 if (current_instance_impl
->HasWrongProcessForURL(dest_url
))
1411 return SiteInstanceDescriptor(browser_context
, dest_url
, true);
1413 // View-source URLs must use a new SiteInstance and BrowsingInstance.
1414 // TODO(nasko): This is the same condition as later in the function. This
1415 // should be taken into account when refactoring this method as part of
1416 // http://crbug.com/123007.
1417 if (dest_is_view_source_mode
)
1418 return SiteInstanceDescriptor(browser_context
, dest_url
, false);
1420 // If we are navigating from a blank SiteInstance to a WebUI, make sure we
1421 // create a new SiteInstance.
1422 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
1423 browser_context
, dest_url
)) {
1424 return SiteInstanceDescriptor(browser_context
, dest_url
, false);
1427 // Normally the "site" on the SiteInstance is set lazily when the load
1428 // actually commits. This is to support better process sharing in case
1429 // the site redirects to some other site: we want to use the destination
1430 // site in the site instance.
1432 // In the case of session restore, as it loads all the pages immediately
1433 // we need to set the site first, otherwise after a restore none of the
1434 // pages would share renderers in process-per-site.
1436 // The embedder can request some urls never to be assigned to SiteInstance
1437 // through the ShouldAssignSiteForURL() content client method, so that
1438 // renderers created for particular chrome urls (e.g. the chrome-native://
1439 // scheme) can be reused for subsequent navigations in the same WebContents.
1440 // See http://crbug.com/386542.
1441 if (dest_is_restore
&&
1442 GetContentClient()->browser()->ShouldAssignSiteForURL(dest_url
)) {
1443 current_instance_impl
->SetSite(dest_url
);
1446 return SiteInstanceDescriptor(current_instance_impl
);
1449 // Otherwise, only create a new SiteInstance for a cross-process navigation.
1451 // TODO(creis): Once we intercept links and script-based navigations, we
1452 // will be able to enforce that all entries in a SiteInstance actually have
1453 // the same site, and it will be safe to compare the URL against the
1454 // SiteInstance's site, as follows:
1455 // const GURL& current_url = current_instance_impl->site();
1456 // For now, though, we're in a hybrid model where you only switch
1457 // SiteInstances if you type in a cross-site URL. This means we have to
1458 // compare the entry's URL to the last committed entry's URL.
1459 NavigationEntry
* current_entry
= controller
.GetLastCommittedEntry();
1460 if (interstitial_page_
) {
1461 // The interstitial is currently the last committed entry, but we want to
1462 // compare against the last non-interstitial entry.
1463 current_entry
= controller
.GetEntryAtOffset(-1);
1466 // View-source URLs must use a new SiteInstance and BrowsingInstance.
1467 // We don't need a swap when going from view-source to a debug URL like
1468 // chrome://crash, however.
1469 // TODO(creis): Refactor this method so this duplicated code isn't needed.
1470 // See http://crbug.com/123007.
1471 if (current_entry
&&
1472 current_entry
->IsViewSourceMode() != dest_is_view_source_mode
&&
1473 !IsRendererDebugURL(dest_url
)) {
1474 return SiteInstanceDescriptor(browser_context
, dest_url
, false);
1477 // Use the source SiteInstance in case of data URLs or about:blank pages,
1478 // because the content is then controlled and/or scriptable by the source
1480 GURL
about_blank(url::kAboutBlankURL
);
1481 if (source_instance
&&
1482 (dest_url
== about_blank
|| dest_url
.scheme() == url::kDataScheme
)) {
1483 return SiteInstanceDescriptor(source_instance
);
1486 // Use the current SiteInstance for same site navigations, as long as the
1487 // process type is correct. (The URL may have been installed as an app since
1488 // the last time we visited it.)
1489 const GURL
& current_url
=
1490 GetCurrentURLForSiteInstance(current_instance_impl
, current_entry
);
1491 if (SiteInstance::IsSameWebSite(browser_context
, current_url
, dest_url
) &&
1492 !current_instance_impl
->HasWrongProcessForURL(dest_url
)) {
1493 return SiteInstanceDescriptor(current_instance_impl
);
1496 // Start the new renderer in a new SiteInstance, but in the current
1497 // BrowsingInstance. It is important to immediately give this new
1498 // SiteInstance to a RenderViewHost (if it is different than our current
1499 // SiteInstance), so that it is ref counted. This will happen in
1500 // CreateRenderView.
1501 return SiteInstanceDescriptor(browser_context
, dest_url
, true);
1504 SiteInstance
* RenderFrameHostManager::ConvertToSiteInstance(
1505 const SiteInstanceDescriptor
& descriptor
,
1506 SiteInstance
* candidate_instance
) {
1507 SiteInstance
* current_instance
= render_frame_host_
->GetSiteInstance();
1509 // Note: If the |candidate_instance| matches the descriptor, it will already
1510 // be set to |descriptor.existing_site_instance|.
1511 if (descriptor
.existing_site_instance
)
1512 return descriptor
.existing_site_instance
;
1514 // Note: If the |candidate_instance| matches the descriptor,
1515 // GetRelatedSiteInstance will return it.
1516 if (descriptor
.new_is_related_to_current
)
1517 return current_instance
->GetRelatedSiteInstance(descriptor
.new_site_url
);
1519 // At this point we know an unrelated site instance must be returned. First
1520 // check if the candidate matches.
1521 if (candidate_instance
&&
1522 !current_instance
->IsRelatedSiteInstance(candidate_instance
) &&
1523 candidate_instance
->GetSiteURL() == descriptor
.new_site_url
) {
1524 return candidate_instance
;
1527 // Otherwise return a newly created one.
1528 return SiteInstance::CreateForURL(
1529 delegate_
->GetControllerForRenderManager().GetBrowserContext(),
1530 descriptor
.new_site_url
);
1533 const GURL
& RenderFrameHostManager::GetCurrentURLForSiteInstance(
1534 SiteInstance
* current_instance
, NavigationEntry
* current_entry
) {
1535 // If this is a subframe that is potentially out of process from its parent,
1536 // don't consider using current_entry's url for SiteInstance selection, since
1537 // current_entry's url is for the main frame and may be in a different site
1539 // TODO(creis): Remove this when we can check the FrameNavigationEntry's url.
1540 // See http://crbug.com/369654
1541 if (!frame_tree_node_
->IsMainFrame() &&
1542 SiteIsolationPolicy::AreCrossProcessFramesPossible())
1543 return frame_tree_node_
->current_url();
1545 // If there is no last non-interstitial entry (and current_instance already
1546 // has a site), then we must have been opened from another tab. We want
1547 // to compare against the URL of the page that opened us, but we can't
1548 // get to it directly. The best we can do is check against the site of
1549 // the SiteInstance. This will be correct when we intercept links and
1550 // script-based navigations, but for now, it could place some pages in a
1551 // new process unnecessarily. We should only hit this case if a page tries
1552 // to open a new tab to an interstitial-inducing URL, and then navigates
1553 // the page to a different same-site URL. (This seems very unlikely in
1556 return current_entry
->GetURL();
1557 return current_instance
->GetSiteURL();
1560 void RenderFrameHostManager::CreatePendingRenderFrameHost(
1561 SiteInstance
* old_instance
,
1562 SiteInstance
* new_instance
) {
1563 int create_render_frame_flags
= 0;
1564 if (delegate_
->IsHidden())
1565 create_render_frame_flags
|= CREATE_RF_HIDDEN
;
1567 if (pending_render_frame_host_
)
1570 // The process for the new SiteInstance may (if we're sharing a process with
1571 // another host that already initialized it) or may not (we have our own
1572 // process or the existing process crashed) have been initialized. Calling
1573 // Init multiple times will be ignored, so this is safe.
1574 if (!new_instance
->GetProcess()->Init())
1577 CreateProxiesForNewRenderFrameHost(old_instance
, new_instance
);
1579 // Create a non-swapped-out RFH with the given opener.
1580 pending_render_frame_host_
= CreateRenderFrame(
1581 new_instance
, pending_web_ui(), create_render_frame_flags
, nullptr);
1584 void RenderFrameHostManager::CreateProxiesForNewRenderFrameHost(
1585 SiteInstance
* old_instance
,
1586 SiteInstance
* new_instance
) {
1587 // Only create opener proxies if they are in the same BrowsingInstance.
1588 if (new_instance
->IsRelatedSiteInstance(old_instance
)) {
1589 CreateOpenerProxies(new_instance
, frame_tree_node_
);
1590 } else if (SiteIsolationPolicy::AreCrossProcessFramesPossible()) {
1591 // Ensure that the frame tree has RenderFrameProxyHosts for the
1592 // new SiteInstance in all nodes except the current one. We do this for
1593 // all frames in the tree, whether they are in the same BrowsingInstance or
1594 // not. If |new_instance| is in the same BrowsingInstance as
1595 // |old_instance|, this will be done as part of CreateOpenerProxies above;
1596 // otherwise, we do this here. We will still check whether two frames are
1597 // in the same BrowsingInstance before we allow them to interact (e.g.,
1599 frame_tree_node_
->frame_tree()->CreateProxiesForSiteInstance(
1600 frame_tree_node_
, new_instance
);
1604 void RenderFrameHostManager::CreateProxiesForNewNamedFrame() {
1605 if (!SiteIsolationPolicy::AreCrossProcessFramesPossible())
1608 DCHECK(!frame_tree_node_
->frame_name().empty());
1610 // If this is a top-level frame, create proxies for this node in the
1611 // SiteInstances of its opener's ancestors, which are allowed to discover
1612 // this frame by name (see https://crbug.com/511474 and part 4 of
1613 // https://html.spec.whatwg.org/#the-rules-for-choosing-a-browsing-context-
1614 // given-a-browsing-context-name).
1615 FrameTreeNode
* opener
= frame_tree_node_
->opener();
1616 if (!opener
|| !frame_tree_node_
->IsMainFrame())
1618 SiteInstance
* current_instance
= render_frame_host_
->GetSiteInstance();
1620 // Start from opener's parent. There's no need to create a proxy in the
1621 // opener's SiteInstance, since new windows are always first opened in the
1622 // same SiteInstance as their opener, and if the new window navigates
1623 // cross-site, that proxy would be created as part of swapping out.
1624 for (FrameTreeNode
* ancestor
= opener
->parent(); ancestor
;
1625 ancestor
= ancestor
->parent()) {
1626 RenderFrameHostImpl
* ancestor_rfh
= ancestor
->current_frame_host();
1627 if (ancestor_rfh
->GetSiteInstance() != current_instance
)
1628 CreateRenderFrameProxy(ancestor_rfh
->GetSiteInstance());
1632 scoped_ptr
<RenderFrameHostImpl
> RenderFrameHostManager::CreateRenderFrameHost(
1633 SiteInstance
* site_instance
,
1634 int32 view_routing_id
,
1635 int32 frame_routing_id
,
1636 int32 widget_routing_id
,
1639 if (frame_routing_id
== MSG_ROUTING_NONE
)
1640 frame_routing_id
= site_instance
->GetProcess()->GetNextRoutingID();
1642 bool swapped_out
= !!(flags
& CREATE_RF_SWAPPED_OUT
);
1643 bool hidden
= !!(flags
& CREATE_RF_HIDDEN
);
1645 // Create a RVH for main frames, or find the existing one for subframes.
1646 FrameTree
* frame_tree
= frame_tree_node_
->frame_tree();
1647 RenderViewHostImpl
* render_view_host
= nullptr;
1648 if (frame_tree_node_
->IsMainFrame()) {
1649 render_view_host
= frame_tree
->CreateRenderViewHost(
1650 site_instance
, view_routing_id
, frame_routing_id
, swapped_out
, hidden
);
1652 render_view_host
= frame_tree
->GetRenderViewHost(site_instance
);
1653 CHECK(render_view_host
);
1656 return RenderFrameHostFactory::Create(
1657 site_instance
, render_view_host
, render_frame_delegate_
,
1658 render_widget_delegate_
, frame_tree
, frame_tree_node_
, frame_routing_id
,
1659 widget_routing_id
, surface_id
, flags
);
1663 bool RenderFrameHostManager::CreateSpeculativeRenderFrameHost(
1665 SiteInstance
* old_instance
,
1666 SiteInstance
* new_instance
,
1668 CHECK(new_instance
);
1669 CHECK_NE(old_instance
, new_instance
);
1670 CHECK(!should_reuse_web_ui_
);
1672 // Note: |speculative_web_ui_| must be initialized before starting the
1673 // |speculative_render_frame_host_| creation steps otherwise the WebUI
1674 // won't be properly initialized.
1675 speculative_web_ui_
= CreateWebUI(url
, bindings
);
1677 // The process for the new SiteInstance may (if we're sharing a process with
1678 // another host that already initialized it) or may not (we have our own
1679 // process or the existing process crashed) have been initialized. Calling
1680 // Init multiple times will be ignored, so this is safe.
1681 if (!new_instance
->GetProcess()->Init())
1684 CreateProxiesForNewRenderFrameHost(old_instance
, new_instance
);
1686 int create_render_frame_flags
= 0;
1687 if (delegate_
->IsHidden())
1688 create_render_frame_flags
|= CREATE_RF_HIDDEN
;
1689 speculative_render_frame_host_
=
1690 CreateRenderFrame(new_instance
, speculative_web_ui_
.get(),
1691 create_render_frame_flags
, nullptr);
1693 if (!speculative_render_frame_host_
) {
1694 speculative_web_ui_
.reset();
1700 scoped_ptr
<RenderFrameHostImpl
> RenderFrameHostManager::CreateRenderFrame(
1701 SiteInstance
* instance
,
1704 int* view_routing_id_ptr
) {
1705 bool swapped_out
= !!(flags
& CREATE_RF_SWAPPED_OUT
);
1706 bool swapped_out_forbidden
=
1707 SiteIsolationPolicy::IsSwappedOutStateForbidden();
1710 CHECK_IMPLIES(swapped_out_forbidden
, !swapped_out
);
1711 CHECK_IMPLIES(!SiteIsolationPolicy::AreCrossProcessFramesPossible(),
1712 frame_tree_node_
->IsMainFrame());
1714 // Swapped out views should always be hidden.
1715 DCHECK_IMPLIES(swapped_out
, (flags
& CREATE_RF_HIDDEN
));
1717 scoped_ptr
<RenderFrameHostImpl
> new_render_frame_host
;
1718 bool success
= true;
1719 if (view_routing_id_ptr
)
1720 *view_routing_id_ptr
= MSG_ROUTING_NONE
;
1722 // We are creating a pending, speculative or swapped out RFH here. We should
1723 // never create it in the same SiteInstance as our current RFH.
1724 CHECK_NE(render_frame_host_
->GetSiteInstance(), instance
);
1726 // Check if we've already created an RFH for this SiteInstance. If so, try
1727 // to re-use the existing one, which has already been initialized. We'll
1728 // remove it from the list of proxy hosts below if it will be active.
1729 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(instance
);
1730 if (proxy
&& proxy
->render_frame_host()) {
1731 RenderViewHost
* render_view_host
= proxy
->GetRenderViewHost();
1732 CHECK(!swapped_out_forbidden
);
1733 if (view_routing_id_ptr
)
1734 *view_routing_id_ptr
= proxy
->GetRenderViewHost()->GetRoutingID();
1735 // Delete the existing RenderFrameProxyHost, but reuse the RenderFrameHost.
1736 // Prevent the process from exiting while we're trying to use it.
1738 new_render_frame_host
= proxy
->PassFrameHostOwnership();
1739 new_render_frame_host
->GetProcess()->AddPendingView();
1741 proxy_hosts_
->Remove(instance
->GetId());
1742 // NB |proxy| is deleted at this point.
1744 // If we are reusing the RenderViewHost and it doesn't already have a
1745 // RenderWidgetHostView, we need to create one if this is the main frame.
1746 if (!render_view_host
->GetView() && frame_tree_node_
->IsMainFrame())
1747 delegate_
->CreateRenderWidgetHostViewForRenderManager(render_view_host
);
1750 // Create a new RenderFrameHost if we don't find an existing one.
1752 int32 widget_routing_id
= MSG_ROUTING_NONE
;
1753 int32 surface_id
= 0;
1754 // A RenderFrame in a different process from its parent RenderFrame
1755 // requires a RenderWidget for input/layout/painting.
1756 if (frame_tree_node_
->parent() &&
1757 frame_tree_node_
->parent()->current_frame_host()->GetSiteInstance() !=
1759 CHECK(SiteIsolationPolicy::AreCrossProcessFramesPossible());
1760 widget_routing_id
= instance
->GetProcess()->GetNextRoutingID();
1761 surface_id
= GpuSurfaceTracker::Get()->AddSurfaceForRenderer(
1762 instance
->GetProcess()->GetID(), widget_routing_id
);
1765 new_render_frame_host
=
1766 CreateRenderFrameHost(instance
, MSG_ROUTING_NONE
, MSG_ROUTING_NONE
,
1767 widget_routing_id
, surface_id
, flags
);
1768 RenderViewHostImpl
* render_view_host
=
1769 new_render_frame_host
->render_view_host();
1770 int proxy_routing_id
= MSG_ROUTING_NONE
;
1772 // Prevent the process from exiting while we're trying to navigate in it.
1773 // Otherwise, if the new RFH is swapped out already, store it.
1775 new_render_frame_host
->GetProcess()->AddPendingView();
1777 proxy
= new RenderFrameProxyHost(
1778 new_render_frame_host
->GetSiteInstance(),
1779 new_render_frame_host
->render_view_host(), frame_tree_node_
);
1780 proxy_hosts_
->Add(instance
->GetId(), make_scoped_ptr(proxy
));
1781 proxy_routing_id
= proxy
->GetRoutingID();
1782 proxy
->TakeFrameHostOwnership(new_render_frame_host
.Pass());
1785 if (frame_tree_node_
->IsMainFrame()) {
1786 success
= InitRenderView(render_view_host
, proxy_routing_id
);
1788 // If we are reusing the RenderViewHost and it doesn't already have a
1789 // RenderWidgetHostView, we need to create one if this is the main frame.
1790 if (!swapped_out
&& !render_view_host
->GetView())
1791 delegate_
->CreateRenderWidgetHostViewForRenderManager(render_view_host
);
1793 DCHECK(render_view_host
->IsRenderViewLive());
1797 // Remember that InitRenderView also created the RenderFrameProxy.
1799 proxy
->set_render_frame_proxy_created(true);
1800 if (frame_tree_node_
->IsMainFrame()) {
1801 // Don't show the main frame's view until we get a DidNavigate from it.
1802 // Only the RenderViewHost for the top-level RenderFrameHost has a
1803 // RenderWidgetHostView; RenderWidgetHosts for out-of-process iframes
1804 // will be created later and hidden.
1805 if (render_view_host
->GetView())
1806 render_view_host
->GetView()->Hide();
1808 // RenderViewHost for |instance| might exist prior to calling
1809 // CreateRenderFrame. In such a case, InitRenderView will not create the
1810 // RenderFrame in the renderer process and it needs to be done
1812 if (swapped_out_forbidden
) {
1813 // Init the RFH, so a RenderFrame is created in the renderer.
1814 DCHECK(new_render_frame_host
);
1815 success
= InitRenderFrame(new_render_frame_host
.get());
1820 if (view_routing_id_ptr
)
1821 *view_routing_id_ptr
= render_view_host
->GetRoutingID();
1825 // When a new RenderView is created by the renderer process, the new
1826 // WebContents gets a RenderViewHost in the SiteInstance of its opener
1827 // WebContents. If not used in the first navigation, this RVH is swapped out
1828 // and is not granted bindings, so we may need to grant them when swapping it
1830 if (web_ui
&& !new_render_frame_host
->GetProcess()->IsForGuestsOnly()) {
1831 int required_bindings
= web_ui
->GetBindings();
1832 RenderViewHost
* render_view_host
=
1833 new_render_frame_host
->render_view_host();
1834 if ((render_view_host
->GetEnabledBindings() & required_bindings
) !=
1835 required_bindings
) {
1836 render_view_host
->AllowBindings(required_bindings
);
1840 // Returns the new RFH if it isn't swapped out.
1841 if (success
&& !swapped_out
) {
1842 DCHECK(new_render_frame_host
->GetSiteInstance() == instance
);
1843 return new_render_frame_host
.Pass();
1848 int RenderFrameHostManager::CreateRenderFrameProxy(SiteInstance
* instance
) {
1849 // A RenderFrameProxyHost should never be created in the same SiteInstance as
1852 CHECK_NE(instance
, render_frame_host_
->GetSiteInstance());
1854 RenderViewHostImpl
* render_view_host
= nullptr;
1856 // Ensure a RenderViewHost exists for |instance|, as it creates the page
1857 // level structure in Blink.
1858 if (SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
1860 frame_tree_node_
->frame_tree()->GetRenderViewHost(instance
);
1861 if (!render_view_host
) {
1862 CHECK(frame_tree_node_
->IsMainFrame());
1863 render_view_host
= frame_tree_node_
->frame_tree()->CreateRenderViewHost(
1864 instance
, MSG_ROUTING_NONE
, MSG_ROUTING_NONE
, true, true);
1868 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(instance
);
1869 if (proxy
&& proxy
->is_render_frame_proxy_live())
1870 return proxy
->GetRoutingID();
1874 new RenderFrameProxyHost(instance
, render_view_host
, frame_tree_node_
);
1875 proxy_hosts_
->Add(instance
->GetId(), make_scoped_ptr(proxy
));
1878 if (SiteIsolationPolicy::IsSwappedOutStateForbidden() &&
1879 frame_tree_node_
->IsMainFrame()) {
1880 InitRenderView(render_view_host
, proxy
->GetRoutingID());
1881 proxy
->set_render_frame_proxy_created(true);
1883 proxy
->InitRenderFrameProxy();
1886 return proxy
->GetRoutingID();
1889 void RenderFrameHostManager::CreateProxiesForChildFrame(FrameTreeNode
* child
) {
1890 for (const auto& pair
: *proxy_hosts_
) {
1891 // Do not create proxies for subframes in the outer delegate's process,
1892 // since the outer delegate does not need to interact with them.
1893 if (ForInnerDelegate() && pair
.second
== GetProxyToOuterDelegate())
1896 child
->render_manager()->CreateRenderFrameProxy(
1897 pair
.second
->GetSiteInstance());
1901 void RenderFrameHostManager::EnsureRenderViewInitialized(
1902 RenderViewHostImpl
* render_view_host
,
1903 SiteInstance
* instance
) {
1904 DCHECK(frame_tree_node_
->IsMainFrame());
1906 if (render_view_host
->IsRenderViewLive())
1909 // If the proxy in |instance| doesn't exist, this RenderView is not swapped
1910 // out and shouldn't be reinitialized here.
1911 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(instance
);
1915 InitRenderView(render_view_host
, proxy
->GetRoutingID());
1916 proxy
->set_render_frame_proxy_created(true);
1919 void RenderFrameHostManager::CreateOuterDelegateProxy(
1920 SiteInstance
* outer_contents_site_instance
,
1921 RenderFrameHostImpl
* render_frame_host
) {
1922 CHECK(BrowserPluginGuestMode::UseCrossProcessFramesForGuests());
1923 RenderFrameProxyHost
* proxy
= new RenderFrameProxyHost(
1924 outer_contents_site_instance
, nullptr, frame_tree_node_
);
1925 proxy_hosts_
->Add(outer_contents_site_instance
->GetId(),
1926 make_scoped_ptr(proxy
));
1928 // Swap the outer WebContents's frame with the proxy to inner WebContents.
1930 // We are in the outer WebContents, and its FrameTree would never see
1931 // a load start for any of its inner WebContents. Eventually, that also makes
1932 // the FrameTree never see the matching load stop. Therefore, we always pass
1933 // false to |is_loading| below.
1934 // TODO(lazyboy): This |is_loading| behavior might not be what we want,
1935 // investigate and fix.
1936 render_frame_host
->Send(new FrameMsg_SwapOut(
1937 render_frame_host
->GetRoutingID(), proxy
->GetRoutingID(),
1938 false /* is_loading */, FrameReplicationState()));
1939 proxy
->set_render_frame_proxy_created(true);
1942 void RenderFrameHostManager::SetRWHViewForInnerContents(
1943 RenderWidgetHostView
* child_rwhv
) {
1944 DCHECK(ForInnerDelegate() && frame_tree_node_
->IsMainFrame());
1945 GetProxyToOuterDelegate()->SetChildRWHView(child_rwhv
);
1948 bool RenderFrameHostManager::InitRenderView(
1949 RenderViewHostImpl
* render_view_host
,
1950 int proxy_routing_id
) {
1951 // Ensure the renderer process is initialized before creating the
1953 if (!render_view_host
->GetProcess()->Init())
1956 // We may have initialized this RenderViewHost for another RenderFrameHost.
1957 if (render_view_host
->IsRenderViewLive())
1960 // If the ongoing navigation is to a WebUI and the RenderView is not in a
1961 // guest process, tell the RenderViewHost about any bindings it will need
1963 WebUIImpl
* dest_web_ui
= nullptr;
1964 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1965 switches::kEnableBrowserSideNavigation
)) {
1967 should_reuse_web_ui_
? web_ui_
.get() : speculative_web_ui_
.get();
1969 dest_web_ui
= pending_web_ui();
1971 if (dest_web_ui
&& !render_view_host
->GetProcess()->IsForGuestsOnly()) {
1972 render_view_host
->AllowBindings(dest_web_ui
->GetBindings());
1974 // Ensure that we don't create an unprivileged RenderView in a WebUI-enabled
1975 // process unless it's swapped out.
1976 if (render_view_host
->is_active()) {
1977 CHECK(!ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
1978 render_view_host
->GetProcess()->GetID()));
1982 int opener_frame_routing_id
=
1983 GetOpenerRoutingID(render_view_host
->GetSiteInstance());
1985 return delegate_
->CreateRenderViewForRenderManager(
1986 render_view_host
, opener_frame_routing_id
, proxy_routing_id
,
1987 frame_tree_node_
->current_replication_state());
1990 bool RenderFrameHostManager::InitRenderFrame(
1991 RenderFrameHostImpl
* render_frame_host
) {
1992 if (render_frame_host
->IsRenderFrameLive())
1995 SiteInstance
* site_instance
= render_frame_host
->GetSiteInstance();
1997 int opener_routing_id
= MSG_ROUTING_NONE
;
1998 if (frame_tree_node_
->opener())
1999 opener_routing_id
= GetOpenerRoutingID(site_instance
);
2001 int parent_routing_id
= MSG_ROUTING_NONE
;
2002 if (frame_tree_node_
->parent()) {
2003 parent_routing_id
= frame_tree_node_
->parent()
2005 ->GetRoutingIdForSiteInstance(site_instance
);
2006 CHECK_NE(parent_routing_id
, MSG_ROUTING_NONE
);
2009 // At this point, all RenderFrameProxies for sibling frames have already been
2010 // created, including any proxies that come after this frame. To preserve
2011 // correct order for indexed window access (e.g., window.frames[1]), pass the
2012 // previous sibling frame so that this frame is correctly inserted into the
2013 // frame tree on the renderer side.
2014 int previous_sibling_routing_id
= MSG_ROUTING_NONE
;
2015 FrameTreeNode
* previous_sibling
= frame_tree_node_
->PreviousSibling();
2016 if (previous_sibling
) {
2017 previous_sibling_routing_id
=
2018 previous_sibling
->render_manager()->GetRoutingIdForSiteInstance(
2020 CHECK_NE(previous_sibling_routing_id
, MSG_ROUTING_NONE
);
2023 // Check whether there is an existing proxy for this frame in this
2024 // SiteInstance. If there is, the new RenderFrame needs to be able to find
2025 // the proxy it is replacing, so that it can fully initialize itself.
2026 // NOTE: This is the only time that a RenderFrameProxyHost can be in the same
2027 // SiteInstance as its RenderFrameHost. This is only the case until the
2028 // RenderFrameHost commits, at which point it will replace and delete the
2029 // RenderFrameProxyHost.
2030 int proxy_routing_id
= MSG_ROUTING_NONE
;
2031 RenderFrameProxyHost
* existing_proxy
= GetRenderFrameProxyHost(site_instance
);
2032 if (existing_proxy
) {
2033 proxy_routing_id
= existing_proxy
->GetRoutingID();
2034 CHECK_NE(proxy_routing_id
, MSG_ROUTING_NONE
);
2035 if (!existing_proxy
->is_render_frame_proxy_live())
2036 existing_proxy
->InitRenderFrameProxy();
2039 return delegate_
->CreateRenderFrameForRenderManager(
2040 render_frame_host
, proxy_routing_id
, opener_routing_id
, parent_routing_id
,
2041 previous_sibling_routing_id
);
2044 int RenderFrameHostManager::GetRoutingIdForSiteInstance(
2045 SiteInstance
* site_instance
) {
2046 if (render_frame_host_
->GetSiteInstance() == site_instance
)
2047 return render_frame_host_
->GetRoutingID();
2049 // If there is a matching pending RFH, only return it if swapped out is
2050 // allowed, since otherwise there should be a proxy that should be used
2052 if (pending_render_frame_host_
&&
2053 pending_render_frame_host_
->GetSiteInstance() == site_instance
&&
2054 !SiteIsolationPolicy::IsSwappedOutStateForbidden())
2055 return pending_render_frame_host_
->GetRoutingID();
2057 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(site_instance
);
2059 return proxy
->GetRoutingID();
2061 return MSG_ROUTING_NONE
;
2064 void RenderFrameHostManager::CommitPending() {
2065 TRACE_EVENT1("navigation", "RenderFrameHostManager::CommitPending",
2066 "FrameTreeNode id", frame_tree_node_
->frame_tree_node_id());
2067 bool browser_side_navigation
=
2068 base::CommandLine::ForCurrentProcess()->HasSwitch(
2069 switches::kEnableBrowserSideNavigation
);
2071 // First check whether we're going to want to focus the location bar after
2072 // this commit. We do this now because the navigation hasn't formally
2073 // committed yet, so if we've already cleared |pending_web_ui_| the call chain
2074 // this triggers won't be able to figure out what's going on.
2075 bool will_focus_location_bar
= delegate_
->FocusLocationBarByDefault();
2077 // Next commit the Web UI, if any. Either replace |web_ui_| with
2078 // |pending_web_ui_|, or clear |web_ui_| if there is no pending WebUI, or
2079 // leave |web_ui_| as is if reusing it.
2080 DCHECK(!(pending_web_ui_
&& pending_and_current_web_ui_
));
2081 if (pending_web_ui_
|| speculative_web_ui_
) {
2082 DCHECK(!should_reuse_web_ui_
);
2083 web_ui_
.reset(browser_side_navigation
? speculative_web_ui_
.release()
2084 : pending_web_ui_
.release());
2085 } else if (pending_and_current_web_ui_
|| should_reuse_web_ui_
) {
2086 if (browser_side_navigation
) {
2088 should_reuse_web_ui_
= false;
2090 DCHECK_EQ(pending_and_current_web_ui_
.get(), web_ui_
.get());
2091 pending_and_current_web_ui_
.reset();
2096 DCHECK(!speculative_web_ui_
);
2097 DCHECK(!should_reuse_web_ui_
);
2099 // It's possible for the pending_render_frame_host_ to be nullptr when we
2100 // aren't crossing process boundaries. If so, we just needed to handle the Web
2101 // UI committing above and we're done.
2102 if (!pending_render_frame_host_
&& !speculative_render_frame_host_
) {
2103 if (will_focus_location_bar
)
2104 delegate_
->SetFocusToLocationBar(false);
2108 // Remember if the page was focused so we can focus the new renderer in
2110 bool focus_render_view
= !will_focus_location_bar
&&
2111 render_frame_host_
->GetView() &&
2112 render_frame_host_
->GetView()->HasFocus();
2114 bool is_main_frame
= frame_tree_node_
->IsMainFrame();
2116 // Swap in the pending or speculative frame and make it active. Also ensure
2117 // the FrameTree stays in sync.
2118 scoped_ptr
<RenderFrameHostImpl
> old_render_frame_host
;
2119 if (!browser_side_navigation
) {
2120 DCHECK(!speculative_render_frame_host_
);
2121 old_render_frame_host
=
2122 SetRenderFrameHost(pending_render_frame_host_
.Pass());
2125 DCHECK(speculative_render_frame_host_
);
2126 old_render_frame_host
=
2127 SetRenderFrameHost(speculative_render_frame_host_
.Pass());
2130 // Remove the children of the old frame from the tree.
2131 frame_tree_node_
->ResetForNewProcess();
2133 // The process will no longer try to exit, so we can decrement the count.
2134 render_frame_host_
->GetProcess()->RemovePendingView();
2136 // Show the new view (or a sad tab) if necessary.
2137 bool new_rfh_has_view
= !!render_frame_host_
->GetView();
2138 if (!delegate_
->IsHidden() && new_rfh_has_view
) {
2139 // In most cases, we need to show the new view.
2140 render_frame_host_
->GetView()->Show();
2142 if (!new_rfh_has_view
) {
2143 // If the view is gone, then this RenderViewHost died while it was hidden.
2144 // We ignored the RenderProcessGone call at the time, so we should send it
2145 // now to make sure the sad tab shows up, etc.
2146 DCHECK(!render_frame_host_
->IsRenderFrameLive());
2147 DCHECK(!render_frame_host_
->render_view_host()->IsRenderViewLive());
2148 delegate_
->RenderProcessGoneFromRenderManager(
2149 render_frame_host_
->render_view_host());
2152 // For top-level frames, also hide the old RenderViewHost's view.
2153 // TODO(creis): As long as show/hide are on RVH, we don't want to hide on
2154 // subframe navigations or we will interfere with the top-level frame.
2155 if (is_main_frame
&& old_render_frame_host
->render_view_host()->GetView())
2156 old_render_frame_host
->render_view_host()->GetView()->Hide();
2158 // Make sure the size is up to date. (Fix for bug 1079768.)
2159 delegate_
->UpdateRenderViewSizeForRenderManager();
2161 if (will_focus_location_bar
) {
2162 delegate_
->SetFocusToLocationBar(false);
2163 } else if (focus_render_view
&& render_frame_host_
->GetView()) {
2164 render_frame_host_
->GetView()->Focus();
2167 // Notify that we've swapped RenderFrameHosts. We do this before shutting down
2168 // the RFH so that we can clean up RendererResources related to the RFH first.
2169 delegate_
->NotifySwappedFromRenderManager(
2170 old_render_frame_host
.get(), render_frame_host_
.get(), is_main_frame
);
2172 // The RenderViewHost keeps track of the main RenderFrameHost routing id.
2173 // If this is committing a main frame navigation, update it and set the
2174 // routing id in the RenderViewHost associated with the old RenderFrameHost
2175 // to MSG_ROUTING_NONE.
2176 if (is_main_frame
&& SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
2177 render_frame_host_
->render_view_host()->set_main_frame_routing_id(
2178 render_frame_host_
->routing_id());
2179 old_render_frame_host
->render_view_host()->set_main_frame_routing_id(
2183 // Swap out the old frame now that the new one is visible.
2184 // This will swap it out and then put it on the proxy list (if there are other
2185 // active views in its SiteInstance) or schedule it for deletion when the swap
2186 // out ack arrives (or immediately if the process isn't live).
2187 // In the --site-per-process case, old subframe RFHs are not kept alive inside
2189 SwapOutOldFrame(old_render_frame_host
.Pass());
2191 if (SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
2192 // Since the new RenderFrameHost is now committed, there must be no proxies
2193 // for its SiteInstance. Delete any existing ones.
2194 proxy_hosts_
->Remove(render_frame_host_
->GetSiteInstance()->GetId());
2197 // If this is a subframe, it should have a CrossProcessFrameConnector
2198 // created already. Use it to link the new RFH's view to the proxy that
2199 // belongs to the parent frame's SiteInstance. If this navigation causes
2200 // an out-of-process frame to return to the same process as its parent, the
2201 // proxy would have been removed from proxy_hosts_ above.
2202 // Note: We do this after swapping out the old RFH because that may create
2203 // the proxy we're looking for.
2204 RenderFrameProxyHost
* proxy_to_parent
= GetProxyToParent();
2205 if (proxy_to_parent
) {
2206 CHECK(SiteIsolationPolicy::AreCrossProcessFramesPossible());
2207 proxy_to_parent
->SetChildRWHView(render_frame_host_
->GetView());
2210 // After all is done, there must never be a proxy in the list which has the
2211 // same SiteInstance as the current RenderFrameHost.
2212 CHECK(!proxy_hosts_
->Get(render_frame_host_
->GetSiteInstance()->GetId()));
2215 void RenderFrameHostManager::ShutdownProxiesIfLastActiveFrameInSiteInstance(
2216 RenderFrameHostImpl
* render_frame_host
) {
2217 if (!render_frame_host
)
2219 if (!RenderFrameHostImpl::IsRFHStateActive(render_frame_host
->rfh_state()))
2221 if (render_frame_host
->GetSiteInstance()->active_frame_count() > 1U)
2224 // After |render_frame_host| goes away, there will be no active frames left in
2225 // its SiteInstance, so we can delete all proxies created in that SiteInstance
2226 // on behalf of frames anywhere in the BrowsingInstance.
2227 int32 site_instance_id
= render_frame_host
->GetSiteInstance()->GetId();
2229 // First remove any proxies for this SiteInstance from our own list.
2230 ClearProxiesInSiteInstance(site_instance_id
, frame_tree_node_
);
2232 // Use the safe RenderWidgetHost iterator for now to find all RenderViewHosts
2233 // in the SiteInstance, then tell their respective FrameTrees to remove all
2234 // RenderFrameProxyHosts corresponding to them.
2235 // TODO(creis): Replace this with a RenderFrameHostIterator that protects
2236 // against use-after-frees if a later element is deleted before getting to it.
2237 scoped_ptr
<RenderWidgetHostIterator
> widgets(
2238 RenderWidgetHostImpl::GetAllRenderWidgetHosts());
2239 while (RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
2240 if (!widget
->IsRenderView())
2242 RenderViewHostImpl
* rvh
=
2243 static_cast<RenderViewHostImpl
*>(RenderViewHost::From(widget
));
2244 if (site_instance_id
== rvh
->GetSiteInstance()->GetId()) {
2245 // This deletes all RenderFrameHosts using the |rvh|, which then causes
2246 // |rvh| to Shutdown.
2247 FrameTree
* tree
= rvh
->GetDelegate()->GetFrameTree();
2248 tree
->ForEach(base::Bind(
2249 &RenderFrameHostManager::ClearProxiesInSiteInstance
,
2255 RenderFrameHostImpl
* RenderFrameHostManager::UpdateStateForNavigate(
2256 const GURL
& dest_url
,
2257 SiteInstance
* source_instance
,
2258 SiteInstance
* dest_instance
,
2259 ui::PageTransition transition
,
2260 bool dest_is_restore
,
2261 bool dest_is_view_source_mode
,
2262 const GlobalRequestID
& transferred_request_id
,
2264 // Don't swap for subframes unless we are in --site-per-process. We can get
2265 // here in tests for subframes (e.g., NavigateFrameToURL).
2266 if (!frame_tree_node_
->IsMainFrame() &&
2267 !SiteIsolationPolicy::AreCrossProcessFramesPossible()) {
2268 return render_frame_host_
.get();
2271 // If we are currently navigating cross-process, we want to get back to normal
2272 // and then navigate as usual.
2273 if (pending_render_frame_host_
)
2276 SiteInstance
* current_instance
= render_frame_host_
->GetSiteInstance();
2277 scoped_refptr
<SiteInstance
> new_instance
= GetSiteInstanceForNavigation(
2278 dest_url
, source_instance
, dest_instance
, nullptr, transition
,
2279 dest_is_restore
, dest_is_view_source_mode
);
2281 const NavigationEntry
* current_entry
=
2282 delegate_
->GetLastCommittedNavigationEntryForRenderManager();
2284 DCHECK(!pending_render_frame_host_
);
2286 if (new_instance
.get() != current_instance
) {
2287 TRACE_EVENT_INSTANT2(
2289 "RenderFrameHostManager::UpdateStateForNavigate:New SiteInstance",
2290 TRACE_EVENT_SCOPE_THREAD
,
2291 "current_instance id", current_instance
->GetId(),
2292 "new_instance id", new_instance
->GetId());
2294 // New SiteInstance: create a pending RFH to navigate.
2296 // This will possibly create (set to nullptr) a Web UI object for the
2297 // pending page. We'll use this later to give the page special access. This
2298 // must happen before the new renderer is created below so it will get
2299 // bindings. It must also happen after the above conditional call to
2300 // CancelPending(), otherwise CancelPending may clear the pending_web_ui_
2301 // and the page will not have its bindings set appropriately.
2302 SetPendingWebUI(dest_url
, bindings
);
2303 CreatePendingRenderFrameHost(current_instance
, new_instance
.get());
2304 if (!pending_render_frame_host_
)
2307 // Check if our current RFH is live before we set up a transition.
2308 if (!render_frame_host_
->IsRenderFrameLive()) {
2309 // The current RFH is not live. There's no reason to sit around with a
2310 // sad tab or a newly created RFH while we wait for the pending RFH to
2311 // navigate. Just switch to the pending RFH now and go back to normal.
2312 // (Note that we don't care about on{before}unload handlers if the current
2315 return render_frame_host_
.get();
2317 // Otherwise, it's safe to treat this as a pending cross-process transition.
2319 // We now have a pending RFH.
2320 DCHECK(pending_render_frame_host_
);
2322 // We need to wait until the beforeunload handler has run, unless we are
2323 // transferring an existing request (in which case it has already run).
2324 // Suspend the new render view (i.e., don't let it send the cross-process
2325 // Navigate message) until we hear back from the old renderer's
2326 // beforeunload handler. If the handler returns false, we'll have to
2327 // cancel the request.
2329 DCHECK(!pending_render_frame_host_
->are_navigations_suspended());
2330 bool is_transfer
= transferred_request_id
!= GlobalRequestID();
2332 // We don't need to stop the old renderer or run beforeunload/unload
2333 // handlers, because those have already been done.
2334 DCHECK(cross_site_transferring_request_
->request_id() ==
2335 transferred_request_id
);
2337 // Also make sure the old render view stops, in case a load is in
2338 // progress. (We don't want to do this for transfers, since it will
2339 // interrupt the transfer with an unexpected DidStopLoading.)
2340 render_frame_host_
->Send(new FrameMsg_Stop(
2341 render_frame_host_
->GetRoutingID()));
2342 pending_render_frame_host_
->SetNavigationsSuspended(true,
2344 // Unless we are transferring an existing request, we should now tell the
2345 // old render view to run its beforeunload handler, since it doesn't
2346 // otherwise know that the cross-site request is happening. This will
2347 // trigger a call to OnBeforeUnloadACK with the reply.
2348 render_frame_host_
->DispatchBeforeUnload(true);
2351 return pending_render_frame_host_
.get();
2354 // Otherwise the same SiteInstance can be used. Navigate render_frame_host_.
2356 // It's possible to swap out the current RFH and then decide to navigate in it
2357 // anyway (e.g., a cross-process navigation that redirects back to the
2358 // original site). In that case, we have a proxy for the current RFH but
2359 // haven't deleted it yet. The new navigation will swap it back in, so we can
2360 // delete the proxy.
2361 proxy_hosts_
->Remove(new_instance
.get()->GetId());
2363 if (ShouldReuseWebUI(current_entry
, dest_url
)) {
2364 pending_web_ui_
.reset();
2365 pending_and_current_web_ui_
= web_ui_
->AsWeakPtr();
2367 SetPendingWebUI(dest_url
, bindings
);
2368 // Make sure the new RenderViewHost has the right bindings.
2369 if (pending_web_ui() &&
2370 !render_frame_host_
->GetProcess()->IsForGuestsOnly()) {
2371 render_frame_host_
->render_view_host()->AllowBindings(
2372 pending_web_ui()->GetBindings());
2376 if (pending_web_ui() && render_frame_host_
->IsRenderFrameLive()) {
2377 pending_web_ui()->GetController()->RenderViewReused(
2378 render_frame_host_
->render_view_host());
2381 // The renderer can exit view source mode when any error or cancellation
2382 // happen. We must overwrite to recover the mode.
2383 if (dest_is_view_source_mode
) {
2384 render_frame_host_
->render_view_host()->Send(
2385 new ViewMsg_EnableViewSourceMode(
2386 render_frame_host_
->render_view_host()->GetRoutingID()));
2389 return render_frame_host_
.get();
2392 void RenderFrameHostManager::CancelPending() {
2393 TRACE_EVENT1("navigation", "RenderFrameHostManager::CancelPending",
2394 "FrameTreeNode id", frame_tree_node_
->frame_tree_node_id());
2395 DiscardUnusedFrame(UnsetPendingRenderFrameHost());
2398 scoped_ptr
<RenderFrameHostImpl
>
2399 RenderFrameHostManager::UnsetPendingRenderFrameHost() {
2400 scoped_ptr
<RenderFrameHostImpl
> pending_render_frame_host
=
2401 pending_render_frame_host_
.Pass();
2403 RenderFrameDevToolsAgentHost::OnCancelPendingNavigation(
2404 pending_render_frame_host
.get(),
2405 render_frame_host_
.get());
2407 // We no longer need to prevent the process from exiting.
2408 pending_render_frame_host
->GetProcess()->RemovePendingView();
2410 pending_web_ui_
.reset();
2411 pending_and_current_web_ui_
.reset();
2413 return pending_render_frame_host
.Pass();
2416 scoped_ptr
<RenderFrameHostImpl
> RenderFrameHostManager::SetRenderFrameHost(
2417 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
) {
2419 scoped_ptr
<RenderFrameHostImpl
> old_render_frame_host
=
2420 render_frame_host_
.Pass();
2421 render_frame_host_
= render_frame_host
.Pass();
2423 if (frame_tree_node_
->IsMainFrame()) {
2424 // Update the count of top-level frames using this SiteInstance. All
2425 // subframes are in the same BrowsingInstance as the main frame, so we only
2426 // count top-level ones. This makes the value easier for consumers to
2428 if (render_frame_host_
) {
2429 render_frame_host_
->GetSiteInstance()->
2430 IncrementRelatedActiveContentsCount();
2432 if (old_render_frame_host
) {
2433 old_render_frame_host
->GetSiteInstance()->
2434 DecrementRelatedActiveContentsCount();
2438 return old_render_frame_host
.Pass();
2441 bool RenderFrameHostManager::IsRVHOnSwappedOutList(
2442 RenderViewHostImpl
* rvh
) const {
2443 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(
2444 rvh
->GetSiteInstance());
2447 // If there is a proxy without RFH, it is for a subframe in the SiteInstance
2448 // of |rvh|. Subframes should be ignored in this case.
2449 if (!proxy
->render_frame_host())
2451 return IsOnSwappedOutList(proxy
->render_frame_host());
2454 bool RenderFrameHostManager::IsOnSwappedOutList(
2455 RenderFrameHostImpl
* rfh
) const {
2456 if (!rfh
->GetSiteInstance())
2459 RenderFrameProxyHost
* host
=
2460 proxy_hosts_
->Get(rfh
->GetSiteInstance()->GetId());
2464 return host
->render_frame_host() == rfh
;
2467 RenderViewHostImpl
* RenderFrameHostManager::GetSwappedOutRenderViewHost(
2468 SiteInstance
* instance
) const {
2469 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(instance
);
2471 return proxy
->GetRenderViewHost();
2475 RenderFrameProxyHost
* RenderFrameHostManager::GetRenderFrameProxyHost(
2476 SiteInstance
* instance
) const {
2477 return proxy_hosts_
->Get(instance
->GetId());
2480 std::map
<int, RenderFrameProxyHost
*>
2481 RenderFrameHostManager::GetAllProxyHostsForTesting() {
2482 std::map
<int, RenderFrameProxyHost
*> result
;
2483 result
.insert(proxy_hosts_
->begin(), proxy_hosts_
->end());
2487 void RenderFrameHostManager::CollectOpenerFrameTrees(
2488 std::vector
<FrameTree
*>* opener_frame_trees
,
2489 base::hash_set
<FrameTreeNode
*>* nodes_with_back_links
) {
2490 CHECK(opener_frame_trees
);
2491 opener_frame_trees
->push_back(frame_tree_node_
->frame_tree());
2493 size_t visited_index
= 0;
2494 while (visited_index
< opener_frame_trees
->size()) {
2495 FrameTree
* frame_tree
= (*opener_frame_trees
)[visited_index
];
2497 frame_tree
->ForEach(base::Bind(&OpenerForFrameTreeNode
, visited_index
,
2498 opener_frame_trees
, nodes_with_back_links
));
2502 void RenderFrameHostManager::CreateOpenerProxies(
2503 SiteInstance
* instance
,
2504 FrameTreeNode
* skip_this_node
) {
2505 std::vector
<FrameTree
*> opener_frame_trees
;
2506 base::hash_set
<FrameTreeNode
*> nodes_with_back_links
;
2508 CollectOpenerFrameTrees(&opener_frame_trees
, &nodes_with_back_links
);
2510 // Create opener proxies for frame trees, processing furthest openers from
2511 // this node first and this node last. In the common case without cycles,
2512 // this will ensure that each tree's openers are created before the tree's
2513 // nodes need to reference them.
2514 for (int i
= opener_frame_trees
.size() - 1; i
>= 0; i
--) {
2515 opener_frame_trees
[i
]
2518 ->CreateOpenerProxiesForFrameTree(instance
, skip_this_node
);
2521 // Set openers for nodes in |nodes_with_back_links| in a second pass.
2522 // The proxies created at these FrameTreeNodes in
2523 // CreateOpenerProxiesForFrameTree won't have their opener routing ID
2524 // available when created due to cycles or back links in the opener chain.
2525 // They must have their openers updated as a separate step after proxy
2527 for (const auto& node
: nodes_with_back_links
) {
2528 RenderFrameProxyHost
* proxy
=
2529 node
->render_manager()->GetRenderFrameProxyHost(instance
);
2530 // If there is no proxy, the cycle may involve nodes in the same process,
2531 // or, if this is a subframe, --site-per-process may be off. Either way,
2532 // there's nothing more to do.
2536 int opener_routing_id
=
2537 node
->render_manager()->GetOpenerRoutingID(instance
);
2538 DCHECK_NE(opener_routing_id
, MSG_ROUTING_NONE
);
2539 proxy
->Send(new FrameMsg_UpdateOpener(proxy
->GetRoutingID(),
2540 opener_routing_id
));
2544 void RenderFrameHostManager::CreateOpenerProxiesForFrameTree(
2545 SiteInstance
* instance
,
2546 FrameTreeNode
* skip_this_node
) {
2547 // Currently, this function is only called on main frames. It should
2548 // actually work correctly for subframes as well, so if that need ever
2549 // arises, it should be sufficient to remove this DCHECK.
2550 DCHECK(frame_tree_node_
->IsMainFrame());
2552 if (frame_tree_node_
== skip_this_node
)
2555 FrameTree
* frame_tree
= frame_tree_node_
->frame_tree();
2556 if (SiteIsolationPolicy::AreCrossProcessFramesPossible()) {
2557 // Ensure that all the nodes in the opener's FrameTree have
2558 // RenderFrameProxyHosts for the new SiteInstance. Only pass the node to
2559 // be skipped if it's in the same FrameTree.
2560 if (skip_this_node
&& skip_this_node
->frame_tree() != frame_tree
)
2561 skip_this_node
= nullptr;
2562 frame_tree
->CreateProxiesForSiteInstance(skip_this_node
, instance
);
2564 // If any of the RenderViewHosts (current, pending, or swapped out) for this
2565 // FrameTree has the same SiteInstance, then we can return early and reuse
2566 // them. An exception is if we are in IsSwappedOutStateForbidden mode and
2567 // find a pending RenderViewHost: in this case, we should still create a
2568 // proxy, which will allow communicating with the opener until the pending
2569 // RenderView commits, or if the pending navigation is canceled.
2570 RenderViewHostImpl
* rvh
= frame_tree
->GetRenderViewHost(instance
);
2571 bool need_proxy_for_pending_rvh
=
2572 SiteIsolationPolicy::IsSwappedOutStateForbidden() &&
2573 (rvh
== pending_render_view_host());
2574 if (rvh
&& rvh
->IsRenderViewLive() && !need_proxy_for_pending_rvh
)
2577 if (rvh
&& !rvh
->IsRenderViewLive()) {
2578 EnsureRenderViewInitialized(rvh
, instance
);
2580 // Create a swapped out RenderView in the given SiteInstance if none
2581 // exists. Since an opener can point to a subframe, do this on the root
2582 // frame of the current opener's frame tree.
2583 if (SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
2584 frame_tree
->root()->render_manager()->CreateRenderFrameProxy(instance
);
2586 frame_tree
->root()->render_manager()->CreateRenderFrame(
2587 instance
, nullptr, CREATE_RF_SWAPPED_OUT
| CREATE_RF_HIDDEN
,
2594 int RenderFrameHostManager::GetOpenerRoutingID(SiteInstance
* instance
) {
2595 if (!frame_tree_node_
->opener())
2596 return MSG_ROUTING_NONE
;
2598 return frame_tree_node_
->opener()
2600 ->GetRoutingIdForSiteInstance(instance
);
2603 } // namespace content