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"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12 #include "base/trace_event/trace_event.h"
13 #include "content/browser/child_process_security_policy_impl.h"
14 #include "content/browser/devtools/render_frame_devtools_agent_host.h"
15 #include "content/browser/frame_host/cross_site_transferring_request.h"
16 #include "content/browser/frame_host/debug_urls.h"
17 #include "content/browser/frame_host/interstitial_page_impl.h"
18 #include "content/browser/frame_host/navigation_controller_impl.h"
19 #include "content/browser/frame_host/navigation_entry_impl.h"
20 #include "content/browser/frame_host/navigation_request.h"
21 #include "content/browser/frame_host/navigator.h"
22 #include "content/browser/frame_host/render_frame_host_factory.h"
23 #include "content/browser/frame_host/render_frame_host_impl.h"
24 #include "content/browser/frame_host/render_frame_proxy_host.h"
25 #include "content/browser/renderer_host/render_process_host_impl.h"
26 #include "content/browser/renderer_host/render_view_host_factory.h"
27 #include "content/browser/renderer_host/render_view_host_impl.h"
28 #include "content/browser/site_instance_impl.h"
29 #include "content/browser/webui/web_ui_controller_factory_registry.h"
30 #include "content/browser/webui/web_ui_impl.h"
31 #include "content/common/frame_messages.h"
32 #include "content/common/view_messages.h"
33 #include "content/public/browser/content_browser_client.h"
34 #include "content/public/browser/notification_service.h"
35 #include "content/public/browser/notification_types.h"
36 #include "content/public/browser/render_widget_host_iterator.h"
37 #include "content/public/browser/render_widget_host_view.h"
38 #include "content/public/browser/user_metrics.h"
39 #include "content/public/browser/web_ui_controller.h"
40 #include "content/public/common/content_switches.h"
41 #include "content/public/common/referrer.h"
42 #include "content/public/common/url_constants.h"
47 bool RenderFrameHostManager::ClearRFHsPendingShutdown(FrameTreeNode
* node
) {
48 node
->render_manager()->pending_delete_hosts_
.clear();
52 RenderFrameHostManager::RenderFrameHostManager(
53 FrameTreeNode
* frame_tree_node
,
54 RenderFrameHostDelegate
* render_frame_delegate
,
55 RenderViewHostDelegate
* render_view_delegate
,
56 RenderWidgetHostDelegate
* render_widget_delegate
,
58 : frame_tree_node_(frame_tree_node
),
60 cross_navigation_pending_(false),
61 render_frame_delegate_(render_frame_delegate
),
62 render_view_delegate_(render_view_delegate
),
63 render_widget_delegate_(render_widget_delegate
),
64 interstitial_page_(nullptr),
65 should_reuse_web_ui_(false),
67 DCHECK(frame_tree_node_
);
70 RenderFrameHostManager::~RenderFrameHostManager() {
71 if (pending_render_frame_host_
) {
72 scoped_ptr
<RenderFrameHostImpl
> relic
= UnsetPendingRenderFrameHost();
73 ShutdownProxiesIfLastActiveFrameInSiteInstance(relic
.get());
76 if (speculative_render_frame_host_
) {
77 scoped_ptr
<RenderFrameHostImpl
> relic
= UnsetSpeculativeRenderFrameHost();
78 ShutdownProxiesIfLastActiveFrameInSiteInstance(relic
.get());
81 ShutdownProxiesIfLastActiveFrameInSiteInstance(render_frame_host_
.get());
83 // Delete any RenderFrameProxyHosts and swapped out RenderFrameHosts.
84 // It is important to delete those prior to deleting the current
85 // RenderFrameHost, since the CrossProcessFrameConnector (owned by
86 // RenderFrameProxyHost) points to the RenderWidgetHostView associated with
87 // the current RenderFrameHost and uses it during its destructor.
88 STLDeleteValues(&proxy_hosts_
);
90 // Release the WebUI prior to resetting the current RenderFrameHost, as the
91 // WebUI accesses the RenderFrameHost during cleanup.
94 // We should always have a current RenderFrameHost except in some tests.
95 SetRenderFrameHost(scoped_ptr
<RenderFrameHostImpl
>());
98 void RenderFrameHostManager::Init(BrowserContext
* browser_context
,
99 SiteInstance
* site_instance
,
101 int frame_routing_id
) {
102 // Create a RenderViewHost and RenderFrameHost, once we have an instance. It
103 // is important to immediately give this SiteInstance to a RenderViewHost so
104 // that the SiteInstance is ref counted.
106 site_instance
= SiteInstance::Create(browser_context
);
108 int flags
= delegate_
->IsHidden() ? CREATE_RF_HIDDEN
: 0;
109 SetRenderFrameHost(CreateRenderFrameHost(site_instance
, view_routing_id
,
110 frame_routing_id
, flags
));
112 // Notify the delegate of the creation of the current RenderFrameHost.
113 // Do this only for subframes, as the main frame case is taken care of by
114 // WebContentsImpl::Init.
115 if (!frame_tree_node_
->IsMainFrame()) {
116 delegate_
->NotifySwappedFromRenderManager(
117 nullptr, render_frame_host_
.get(), false);
120 // Keep track of renderer processes as they start to shut down or are
122 registrar_
.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED
,
123 NotificationService::AllSources());
124 registrar_
.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSING
,
125 NotificationService::AllSources());
128 RenderViewHostImpl
* RenderFrameHostManager::current_host() const {
129 if (!render_frame_host_
)
131 return render_frame_host_
->render_view_host();
134 RenderViewHostImpl
* RenderFrameHostManager::pending_render_view_host() const {
135 if (!pending_render_frame_host_
)
137 return pending_render_frame_host_
->render_view_host();
140 RenderWidgetHostView
* RenderFrameHostManager::GetRenderWidgetHostView() const {
141 if (interstitial_page_
)
142 return interstitial_page_
->GetView();
143 if (render_frame_host_
)
144 return render_frame_host_
->GetView();
148 RenderFrameProxyHost
* RenderFrameHostManager::GetProxyToParent() {
149 if (frame_tree_node_
->IsMainFrame())
152 RenderFrameProxyHostMap::iterator iter
=
153 proxy_hosts_
.find(frame_tree_node_
->parent()
155 ->current_frame_host()
158 if (iter
== proxy_hosts_
.end())
164 void RenderFrameHostManager::SetPendingWebUI(const GURL
& url
, int bindings
) {
165 pending_web_ui_
= CreateWebUI(url
, bindings
);
166 pending_and_current_web_ui_
.reset();
169 scoped_ptr
<WebUIImpl
> RenderFrameHostManager::CreateWebUI(const GURL
& url
,
171 scoped_ptr
<WebUIImpl
> new_web_ui(delegate_
->CreateWebUIForRenderManager(url
));
173 // If we have assigned (zero or more) bindings to this NavigationEntry in the
174 // past, make sure we're not granting it different bindings than it had
175 // before. If so, note it and don't give it any bindings, to avoid a
176 // potential privilege escalation.
177 if (new_web_ui
&& bindings
!= NavigationEntryImpl::kInvalidBindings
&&
178 new_web_ui
->GetBindings() != bindings
) {
179 RecordAction(base::UserMetricsAction("ProcessSwapBindingsMismatch_RVHM"));
182 return new_web_ui
.Pass();
185 RenderFrameHostImpl
* RenderFrameHostManager::Navigate(
186 const NavigationEntryImpl
& entry
) {
187 TRACE_EVENT1("navigation", "RenderFrameHostManager:Navigate",
188 "FrameTreeNode id", frame_tree_node_
->frame_tree_node_id());
189 // Create a pending RenderFrameHost to use for the navigation.
190 RenderFrameHostImpl
* dest_render_frame_host
= UpdateStateForNavigate(
191 entry
.GetURL(), entry
.source_site_instance(), entry
.site_instance(),
192 entry
.GetTransitionType(),
193 entry
.restore_type() != NavigationEntryImpl::RESTORE_NONE
,
194 entry
.IsViewSourceMode(), entry
.transferred_global_request_id(),
196 if (!dest_render_frame_host
)
197 return NULL
; // We weren't able to create a pending render frame host.
199 // If the current render_frame_host_ isn't live, we should create it so
200 // that we don't show a sad tab while the dest_render_frame_host fetches
201 // its first page. (Bug 1145340)
202 if (dest_render_frame_host
!= render_frame_host_
&&
203 !render_frame_host_
->IsRenderFrameLive()) {
204 // Note: we don't call InitRenderView here because we are navigating away
205 // soon anyway, and we don't have the NavigationEntry for this host.
206 delegate_
->CreateRenderViewForRenderManager(
207 render_frame_host_
->render_view_host(), MSG_ROUTING_NONE
,
208 MSG_ROUTING_NONE
, frame_tree_node_
->IsMainFrame());
211 // If the renderer crashed, then try to create a new one to satisfy this
212 // navigation request.
213 if (!dest_render_frame_host
->IsRenderFrameLive()) {
214 // Instruct the destination render frame host to set up a Mojo connection
215 // with the new render frame if necessary. Note that this call needs to
216 // occur before initializing the RenderView; the flow of creating the
217 // RenderView can cause browser-side code to execute that expects the this
218 // RFH's ServiceRegistry to be initialized (e.g., if the site is a WebUI
219 // site that is handled via Mojo, then Mojo WebUI code in //chrome will
220 // add a service to this RFH's ServiceRegistry).
221 dest_render_frame_host
->SetUpMojoIfNeeded();
223 // Recreate the opener chain.
224 int opener_route_id
= delegate_
->CreateOpenerRenderViewsForRenderManager(
225 dest_render_frame_host
->GetSiteInstance());
226 if (!InitRenderView(dest_render_frame_host
->render_view_host(),
229 frame_tree_node_
->IsMainFrame()))
232 // Now that we've created a new renderer, be sure to hide it if it isn't
233 // our primary one. Otherwise, we might crash if we try to call Show()
235 if (dest_render_frame_host
!= render_frame_host_
) {
236 if (dest_render_frame_host
->GetView())
237 dest_render_frame_host
->GetView()->Hide();
239 // TODO(nasko): This is a very ugly hack. The Chrome extensions process
240 // manager still uses NotificationService and expects to see a
241 // RenderViewHost changed notification after WebContents and
242 // RenderFrameHostManager are completely initialized. This should be
243 // removed once the process manager moves away from NotificationService.
244 // See https://crbug.com/462682.
245 delegate_
->NotifyMainFrameSwappedFromRenderManager(
246 nullptr, render_frame_host_
->render_view_host());
250 // If entry includes the request ID of a request that is being transferred,
251 // the destination render frame will take ownership, so release ownership of
253 if (cross_site_transferring_request_
.get() &&
254 cross_site_transferring_request_
->request_id() ==
255 entry
.transferred_global_request_id()) {
256 cross_site_transferring_request_
->ReleaseRequest();
259 return dest_render_frame_host
;
262 void RenderFrameHostManager::Stop() {
263 render_frame_host_
->Stop();
265 // If we are cross-navigating, we should stop the pending renderers. This
266 // will lead to a DidFailProvisionalLoad, which will properly destroy them.
267 if (cross_navigation_pending_
) {
268 pending_render_frame_host_
->Send(new FrameMsg_Stop(
269 pending_render_frame_host_
->GetRoutingID()));
273 void RenderFrameHostManager::SetIsLoading(bool is_loading
) {
274 render_frame_host_
->render_view_host()->SetIsLoading(is_loading
);
275 if (pending_render_frame_host_
)
276 pending_render_frame_host_
->render_view_host()->SetIsLoading(is_loading
);
279 bool RenderFrameHostManager::ShouldCloseTabOnUnresponsiveRenderer() {
280 // If we're waiting for a close ACK, then the tab should close whether there's
281 // a navigation in progress or not. Unfortunately, we also need to check for
282 // cases that we arrive here with no navigation in progress, since there are
283 // some tab closure paths that don't set is_waiting_for_close_ack to true.
284 // TODO(creis): Clean this up in http://crbug.com/418266.
285 if (!cross_navigation_pending_
||
286 render_frame_host_
->render_view_host()->is_waiting_for_close_ack())
289 // We should always have a pending RFH when there's a cross-process navigation
290 // in progress. Sanity check this for http://crbug.com/276333.
291 CHECK(pending_render_frame_host_
);
293 // Unload handlers run in the background, so we should never get an
294 // unresponsiveness warning for them.
295 CHECK(!render_frame_host_
->IsWaitingForUnloadACK());
297 // If the tab becomes unresponsive during beforeunload while doing a
298 // cross-site navigation, proceed with the navigation. (This assumes that
299 // the pending RenderFrameHost is still responsive.)
300 if (render_frame_host_
->IsWaitingForBeforeUnloadACK()) {
301 // Haven't gotten around to starting the request, because we're still
302 // waiting for the beforeunload handler to finish. We'll pretend that it
303 // did finish, to let the navigation proceed. Note that there's a danger
304 // that the beforeunload handler will later finish and possibly return
305 // false (meaning the navigation should not proceed), but we'll ignore it
306 // in this case because it took too long.
307 if (pending_render_frame_host_
->are_navigations_suspended()) {
308 pending_render_frame_host_
->SetNavigationsSuspended(
309 false, base::TimeTicks::Now());
315 void RenderFrameHostManager::OnBeforeUnloadACK(
316 bool for_cross_site_transition
,
318 const base::TimeTicks
& proceed_time
) {
319 if (for_cross_site_transition
) {
320 DCHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch(
321 switches::kEnableBrowserSideNavigation
));
322 // Ignore if we're not in a cross-site navigation.
323 if (!cross_navigation_pending_
)
327 // Ok to unload the current page, so proceed with the cross-site
328 // navigation. Note that if navigations are not currently suspended, it
329 // might be because the renderer was deemed unresponsive and this call was
330 // already made by ShouldCloseTabOnUnresponsiveRenderer. In that case, it
331 // is ok to do nothing here.
332 if (pending_render_frame_host_
&&
333 pending_render_frame_host_
->are_navigations_suspended()) {
334 pending_render_frame_host_
->SetNavigationsSuspended(false,
338 // Current page says to cancel.
340 cross_navigation_pending_
= false;
343 // Non-cross site transition means closing the entire tab.
344 bool proceed_to_fire_unload
;
345 delegate_
->BeforeUnloadFiredFromRenderManager(proceed
, proceed_time
,
346 &proceed_to_fire_unload
);
348 if (proceed_to_fire_unload
) {
349 // If we're about to close the tab and there's a pending RFH, cancel it.
350 // Otherwise, if the navigation in the pending RFH completes before the
351 // close in the current RFH, we'll lose the tab close.
352 if (pending_render_frame_host_
) {
354 cross_navigation_pending_
= false;
357 // This is not a cross-site navigation, the tab is being closed.
358 render_frame_host_
->render_view_host()->ClosePage();
363 void RenderFrameHostManager::OnCrossSiteResponse(
364 RenderFrameHostImpl
* pending_render_frame_host
,
365 const GlobalRequestID
& global_request_id
,
366 scoped_ptr
<CrossSiteTransferringRequest
> cross_site_transferring_request
,
367 const std::vector
<GURL
>& transfer_url_chain
,
368 const Referrer
& referrer
,
369 ui::PageTransition page_transition
,
370 bool should_replace_current_entry
) {
371 // We should only get here for transfer navigations. Most cross-process
372 // navigations can just continue and wait to run the unload handler (by
373 // swapping out) when the new navigation commits.
374 CHECK(cross_site_transferring_request
.get());
376 // A transfer should only have come from our pending or current RFH.
377 // TODO(creis): We need to handle the case that the pending RFH has changed
378 // in the mean time, while this was being posted from the IO thread. We
379 // should probably cancel the request in that case.
380 DCHECK(pending_render_frame_host
== pending_render_frame_host_
||
381 pending_render_frame_host
== render_frame_host_
);
383 // Store the transferring request so that we can release it if the transfer
384 // navigation matches.
385 cross_site_transferring_request_
= cross_site_transferring_request
.Pass();
387 // Sanity check that the params are for the correct frame and process.
388 // These should match the RenderFrameHost that made the request.
389 // If it started as a cross-process navigation via OpenURL, this is the
390 // pending one. If it wasn't cross-process until the transfer, this is the
392 int render_frame_id
= pending_render_frame_host_
?
393 pending_render_frame_host_
->GetRoutingID() :
394 render_frame_host_
->GetRoutingID();
395 DCHECK_EQ(render_frame_id
, pending_render_frame_host
->GetRoutingID());
396 int process_id
= pending_render_frame_host_
?
397 pending_render_frame_host_
->GetProcess()->GetID() :
398 render_frame_host_
->GetProcess()->GetID();
399 DCHECK_EQ(process_id
, global_request_id
.child_id
);
401 // Treat the last URL in the chain as the destination and the remainder as
402 // the redirect chain.
403 CHECK(transfer_url_chain
.size());
404 GURL transfer_url
= transfer_url_chain
.back();
405 std::vector
<GURL
> rest_of_chain
= transfer_url_chain
;
406 rest_of_chain
.pop_back();
408 // We don't know whether the original request had |user_action| set to true.
409 // However, since we force the navigation to be in the current tab, it
411 pending_render_frame_host
->frame_tree_node()->navigator()->RequestTransferURL(
412 pending_render_frame_host
, transfer_url
, nullptr, rest_of_chain
, referrer
,
413 page_transition
, CURRENT_TAB
, global_request_id
,
414 should_replace_current_entry
, true);
416 // The transferring request was only needed during the RequestTransferURL
417 // call, so it is safe to clear at this point.
418 cross_site_transferring_request_
.reset();
421 void RenderFrameHostManager::OnDeferredAfterResponseStarted(
422 const GlobalRequestID
& global_request_id
,
423 RenderFrameHostImpl
* pending_render_frame_host
) {
424 DCHECK(!response_started_id_
.get());
426 response_started_id_
.reset(new GlobalRequestID(global_request_id
));
429 void RenderFrameHostManager::ResumeResponseDeferredAtStart() {
430 DCHECK(response_started_id_
.get());
432 RenderProcessHostImpl
* process
=
433 static_cast<RenderProcessHostImpl
*>(render_frame_host_
->GetProcess());
434 process
->ResumeResponseDeferredAtStart(*response_started_id_
);
436 render_frame_host_
->ClearPendingTransitionRequestData();
438 response_started_id_
.reset();
441 void RenderFrameHostManager::ClearNavigationTransitionData() {
442 render_frame_host_
->ClearPendingTransitionRequestData();
445 void RenderFrameHostManager::DidNavigateFrame(
446 RenderFrameHostImpl
* render_frame_host
,
447 bool was_caused_by_user_gesture
) {
448 CommitPendingIfNecessary(render_frame_host
, was_caused_by_user_gesture
);
450 // Make sure any dynamic changes to this frame's sandbox flags that were made
451 // prior to navigation take effect.
452 CommitPendingSandboxFlags();
455 void RenderFrameHostManager::CommitPendingIfNecessary(
456 RenderFrameHostImpl
* render_frame_host
,
457 bool was_caused_by_user_gesture
) {
458 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
459 switches::kEnableBrowserSideNavigation
)) {
460 if (render_frame_host
== speculative_render_frame_host_
.get()) {
462 } else if (render_frame_host
== render_frame_host_
.get()) {
463 // TODO(carlosk): this code doesn't properly handle in-page navigation or
464 // interwoven navigation requests.
465 DCHECK(!speculative_render_frame_host_
);
467 // No one else should be sending us a DidNavigate in this state.
470 DCHECK(!speculative_render_frame_host_
);
474 if (!cross_navigation_pending_
) {
475 DCHECK(!pending_render_frame_host_
);
477 // We should only hear this from our current renderer.
478 DCHECK_EQ(render_frame_host_
, render_frame_host
);
480 // Even when there is no pending RVH, there may be a pending Web UI.
481 if (pending_web_ui())
486 if (render_frame_host
== pending_render_frame_host_
) {
487 // The pending cross-site navigation completed, so show the renderer.
489 } else if (render_frame_host
== render_frame_host_
) {
490 if (was_caused_by_user_gesture
) {
491 // A navigation in the original page has taken place. Cancel the pending
492 // one. Only do it for user gesture originated navigations to prevent
493 // page doing any shenanigans to prevent user from navigating.
494 // See https://code.google.com/p/chromium/issues/detail?id=75195
496 cross_navigation_pending_
= false;
499 // No one else should be sending us DidNavigate in this state.
504 void RenderFrameHostManager::DidDisownOpener(
505 RenderFrameHost
* render_frame_host
) {
506 // Notify all RenderFrameHosts but the one that notified us. This is necessary
507 // in case a process swap has occurred while the message was in flight.
508 for (RenderFrameProxyHostMap::iterator iter
= proxy_hosts_
.begin();
509 iter
!= proxy_hosts_
.end();
511 DCHECK_NE(iter
->second
->GetSiteInstance(),
512 current_frame_host()->GetSiteInstance());
513 iter
->second
->DisownOpener();
516 if (render_frame_host_
.get() != render_frame_host
)
517 render_frame_host_
->DisownOpener();
519 if (pending_render_frame_host_
&&
520 pending_render_frame_host_
.get() != render_frame_host
) {
521 pending_render_frame_host_
->DisownOpener();
525 void RenderFrameHostManager::CommitPendingSandboxFlags() {
526 // Return early if there were no pending sandbox flags updates.
527 if (!frame_tree_node_
->CommitPendingSandboxFlags())
530 // Sandbox flags updates can only happen when the frame has a parent.
531 CHECK(frame_tree_node_
->parent());
533 // Notify all of the frame's proxies about updated sandbox flags, excluding
534 // the parent process since it already knows the latest flags.
535 SiteInstance
* parent_site_instance
=
536 frame_tree_node_
->parent()->current_frame_host()->GetSiteInstance();
537 for (const auto& pair
: proxy_hosts_
) {
538 if (pair
.second
->GetSiteInstance() != parent_site_instance
) {
539 pair
.second
->Send(new FrameMsg_DidUpdateSandboxFlags(
540 pair
.second
->GetRoutingID(),
541 frame_tree_node_
->current_replication_state().sandbox_flags
));
546 void RenderFrameHostManager::RendererProcessClosing(
547 RenderProcessHost
* render_process_host
) {
548 // Remove any swapped out RVHs from this process, so that we don't try to
549 // swap them back in while the process is exiting. Start by finding them,
550 // since there could be more than one.
551 std::list
<int> ids_to_remove
;
552 // Do not remove proxies in the dead process that still have active frame
553 // count though, we just reset them to be uninitialized.
554 std::list
<int> ids_to_keep
;
555 for (RenderFrameProxyHostMap::iterator iter
= proxy_hosts_
.begin();
556 iter
!= proxy_hosts_
.end();
558 RenderFrameProxyHost
* proxy
= iter
->second
;
559 if (proxy
->GetProcess() != render_process_host
)
562 if (static_cast<SiteInstanceImpl
*>(proxy
->GetSiteInstance())
563 ->active_frame_count() >= 1U) {
564 ids_to_keep
.push_back(iter
->first
);
566 ids_to_remove
.push_back(iter
->first
);
571 while (!ids_to_remove
.empty()) {
572 delete proxy_hosts_
[ids_to_remove
.back()];
573 proxy_hosts_
.erase(ids_to_remove
.back());
574 ids_to_remove
.pop_back();
577 while (!ids_to_keep
.empty()) {
578 frame_tree_node_
->frame_tree()->ForEach(
580 &RenderFrameHostManager::ResetProxiesInSiteInstance
,
581 ids_to_keep
.back()));
582 ids_to_keep
.pop_back();
586 void RenderFrameHostManager::SwapOutOldFrame(
587 scoped_ptr
<RenderFrameHostImpl
> old_render_frame_host
) {
588 TRACE_EVENT1("navigation", "RenderFrameHostManager::SwapOutOldFrame",
589 "FrameTreeNode id", frame_tree_node_
->frame_tree_node_id());
591 // Tell the renderer to suppress any further modal dialogs so that we can swap
592 // it out. This must be done before canceling any current dialog, in case
593 // there is a loop creating additional dialogs.
594 // TODO(creis): Handle modal dialogs in subframe processes.
595 old_render_frame_host
->render_view_host()->SuppressDialogsUntilSwapOut();
597 // Now close any modal dialogs that would prevent us from swapping out. This
598 // must be done separately from SwapOut, so that the PageGroupLoadDeferrer is
599 // no longer on the stack when we send the SwapOut message.
600 delegate_
->CancelModalDialogsForRenderManager();
602 // If the old RFH is not live, just return as there is no further work to do.
603 // It will be deleted and there will be no proxy created.
604 int32 old_site_instance_id
=
605 old_render_frame_host
->GetSiteInstance()->GetId();
606 if (!old_render_frame_host
->IsRenderFrameLive()) {
607 ShutdownProxiesIfLastActiveFrameInSiteInstance(old_render_frame_host
.get());
611 // If there are no active frames besides this one, we can delete the old
612 // RenderFrameHost once it runs its unload handler, without replacing it with
614 size_t active_frame_count
=
615 old_render_frame_host
->GetSiteInstance()->active_frame_count();
616 if (active_frame_count
<= 1) {
617 // Tell the old RenderFrameHost to swap out, with no proxy to replace it.
618 old_render_frame_host
->SwapOut(NULL
, true);
619 MoveToPendingDeleteHosts(old_render_frame_host
.Pass());
621 // Also clear out any proxies from this SiteInstance, in case this was the
622 // last one keeping other proxies alive.
623 ShutdownProxiesIfLastActiveFrameInSiteInstance(old_render_frame_host
.get());
627 // Otherwise there are active views and we need a proxy for the old RFH.
628 // (There should not be one yet.)
629 CHECK(!GetRenderFrameProxyHost(old_render_frame_host
->GetSiteInstance()));
630 RenderFrameProxyHost
* proxy
= new RenderFrameProxyHost(
631 old_render_frame_host
->GetSiteInstance(), frame_tree_node_
);
632 CHECK(proxy_hosts_
.insert(std::make_pair(old_site_instance_id
, proxy
)).second
)
633 << "Inserting a duplicate item.";
635 // Tell the old RenderFrameHost to swap out and be replaced by the proxy.
636 old_render_frame_host
->SwapOut(proxy
, true);
638 // SwapOut creates a RenderFrameProxy, so set the proxy to be initialized.
639 proxy
->set_render_frame_proxy_created(true);
641 bool is_main_frame
= frame_tree_node_
->IsMainFrame();
642 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
643 switches::kSitePerProcess
) &&
645 // In --site-per-process, subframes delete their RFH rather than storing it
646 // in the proxy. Schedule it for deletion once the SwapOutACK comes in.
647 // TODO(creis): This will be the default when we remove swappedout://.
648 MoveToPendingDeleteHosts(old_render_frame_host
.Pass());
650 // We shouldn't get here for subframes, since we only swap subframes when
651 // --site-per-process is used.
652 DCHECK(is_main_frame
);
654 // The old RenderFrameHost will stay alive inside the proxy so that existing
655 // JavaScript window references to it stay valid.
656 proxy
->TakeFrameHostOwnership(old_render_frame_host
.Pass());
660 void RenderFrameHostManager::DiscardUnusedFrame(
661 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
) {
662 // TODO(carlosk): this code is very similar to what can be found in
663 // SwapOutOldFrame and we should see that these are unified at some point.
665 // If the SiteInstance for the pending RFH is being used by others don't
666 // delete the RFH. Just swap it out and it can be reused at a later point.
667 SiteInstanceImpl
* site_instance
= render_frame_host
->GetSiteInstance();
668 if (site_instance
->HasSite() && site_instance
->active_frame_count() > 1) {
669 // Any currently suspended navigations are no longer needed.
670 render_frame_host
->CancelSuspendedNavigations();
672 RenderFrameProxyHost
* proxy
=
673 new RenderFrameProxyHost(site_instance
, frame_tree_node_
);
674 proxy_hosts_
[site_instance
->GetId()] = proxy
;
676 // Check if the RenderFrameHost is already swapped out, to avoid swapping it
678 if (!render_frame_host
->is_swapped_out())
679 render_frame_host
->SwapOut(proxy
, false);
681 if (frame_tree_node_
->IsMainFrame())
682 proxy
->TakeFrameHostOwnership(render_frame_host
.Pass());
684 // We won't be coming back, so delete this one.
685 ShutdownProxiesIfLastActiveFrameInSiteInstance(render_frame_host
.get());
686 render_frame_host
.reset();
690 void RenderFrameHostManager::MoveToPendingDeleteHosts(
691 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
) {
692 // |render_frame_host| will be deleted when its SwapOut ACK is received, or
693 // when the timer times out, or when the RFHM itself is deleted (whichever
695 pending_delete_hosts_
.push_back(
696 linked_ptr
<RenderFrameHostImpl
>(render_frame_host
.release()));
699 bool RenderFrameHostManager::IsPendingDeletion(
700 RenderFrameHostImpl
* render_frame_host
) {
701 for (const auto& rfh
: pending_delete_hosts_
) {
702 if (rfh
== render_frame_host
)
708 bool RenderFrameHostManager::DeleteFromPendingList(
709 RenderFrameHostImpl
* render_frame_host
) {
710 for (RFHPendingDeleteList::iterator iter
= pending_delete_hosts_
.begin();
711 iter
!= pending_delete_hosts_
.end();
713 if (*iter
== render_frame_host
) {
714 pending_delete_hosts_
.erase(iter
);
721 void RenderFrameHostManager::ResetProxyHosts() {
722 STLDeleteValues(&proxy_hosts_
);
726 void RenderFrameHostManager::BeginNavigation(const NavigationRequest
& request
) {
727 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
728 switches::kEnableBrowserSideNavigation
));
729 // Clean up any state in case there's an ongoing navigation.
730 // TODO(carlosk): remove this cleanup here once we properly cancel ongoing
734 RenderFrameHostImpl
* dest_rfh
= GetFrameHostForNavigation(request
);
739 RenderFrameHostImpl
* RenderFrameHostManager::GetFrameHostForNavigation(
740 const NavigationRequest
& request
) {
741 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
742 switches::kEnableBrowserSideNavigation
));
744 SiteInstance
* current_site_instance
= render_frame_host_
->GetSiteInstance();
746 SiteInstance
* candidate_site_instance
=
747 speculative_render_frame_host_
748 ? speculative_render_frame_host_
->GetSiteInstance()
751 scoped_refptr
<SiteInstance
> dest_site_instance
= GetSiteInstanceForNavigation(
752 request
.common_params().url
, request
.source_site_instance(),
753 request
.dest_site_instance(), candidate_site_instance
,
754 request
.common_params().transition
,
755 request
.restore_type() != NavigationEntryImpl::RESTORE_NONE
,
756 request
.is_view_source());
758 // The appropriate RenderFrameHost to commit the navigation.
759 RenderFrameHostImpl
* navigation_rfh
= nullptr;
761 // Renderer-initiated navigations that may require a SiteInstance swap are
762 // sent to the browser via the OpenURL IPC and are afterwards treated as
763 // browser-initiated navigations. NavigationRequests marked as
764 // renderer-initiated are created by receiving a BeginNavigation IPC, and will
765 // then proceed in the same renderer that sent the IPC due to the condition
767 // TODO(carlosk): Once there is support for cross-process scripting check for
768 // non-browser-initiated navigations should be removed (see crbug.com/440266).
769 if (current_site_instance
== dest_site_instance
.get() ||
770 !request
.browser_initiated() ||
771 (!frame_tree_node_
->IsMainFrame() &&
772 !base::CommandLine::ForCurrentProcess()->HasSwitch(
773 switches::kSitePerProcess
))) {
774 // Reuse the current RFH if its SiteInstance matches the the navigation's
775 // or if this is a subframe navigation. We only swap RFHs for subframes when
776 // --site-per-process is enabled.
778 navigation_rfh
= render_frame_host_
.get();
780 // As SiteInstances are the same, check if the WebUI should be reused.
781 const NavigationEntry
* current_navigation_entry
=
782 delegate_
->GetLastCommittedNavigationEntryForRenderManager();
783 should_reuse_web_ui_
= ShouldReuseWebUI(current_navigation_entry
,
784 request
.common_params().url
);
785 if (!should_reuse_web_ui_
) {
786 speculative_web_ui_
= CreateWebUI(request
.common_params().url
,
788 // Make sure the current RenderViewHost has the right bindings.
789 if (speculative_web_ui() &&
790 !render_frame_host_
->GetProcess()->IsIsolatedGuest()) {
791 render_frame_host_
->render_view_host()->AllowBindings(
792 speculative_web_ui()->GetBindings());
796 // If the SiteInstance for the final URL doesn't match the one from the
797 // speculatively created RenderFrameHost, create a new RenderFrameHost using
798 // this new SiteInstance.
799 if (!speculative_render_frame_host_
||
800 speculative_render_frame_host_
->GetSiteInstance() !=
801 dest_site_instance
.get()) {
803 bool success
= CreateSpeculativeRenderFrameHost(
804 request
.common_params().url
, current_site_instance
,
805 dest_site_instance
.get(), request
.bindings());
808 DCHECK(speculative_render_frame_host_
);
809 navigation_rfh
= speculative_render_frame_host_
.get();
811 // Check if our current RFH is live.
812 if (!render_frame_host_
->IsRenderFrameLive()) {
813 // The current RFH is not live. There's no reason to sit around with a
814 // sad tab or a newly created RFH while we wait for the navigation to
815 // complete. Just switch to the speculative RFH now and go back to non
816 // cross-navigating (Note that we don't care about on{before}unload
817 // handlers if the current RFH isn't live.)
821 DCHECK(navigation_rfh
&&
822 (navigation_rfh
== render_frame_host_
.get() ||
823 navigation_rfh
== speculative_render_frame_host_
.get()));
825 // If the RenderFrame that needs to navigate is not live (its process was just
826 // created or has crashed), initialize it.
827 if (!navigation_rfh
->IsRenderFrameLive()) {
828 // Recreate the opener chain.
829 int opener_route_id
= delegate_
->CreateOpenerRenderViewsForRenderManager(
830 navigation_rfh
->GetSiteInstance());
831 if (!InitRenderView(navigation_rfh
->render_view_host(), opener_route_id
,
832 MSG_ROUTING_NONE
, frame_tree_node_
->IsMainFrame())) {
837 cross_navigation_pending_
= navigation_rfh
!= render_frame_host_
.get();
838 return navigation_rfh
;
842 void RenderFrameHostManager::CleanUpNavigation() {
843 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
844 switches::kEnableBrowserSideNavigation
));
845 speculative_web_ui_
.reset();
846 should_reuse_web_ui_
= false;
847 if (speculative_render_frame_host_
)
848 DiscardUnusedFrame(UnsetSpeculativeRenderFrameHost());
852 scoped_ptr
<RenderFrameHostImpl
>
853 RenderFrameHostManager::UnsetSpeculativeRenderFrameHost() {
854 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
855 switches::kEnableBrowserSideNavigation
));
856 speculative_render_frame_host_
->GetProcess()->RemovePendingView();
857 return speculative_render_frame_host_
.Pass();
860 void RenderFrameHostManager::OnDidStartLoading() {
861 for (const auto& pair
: proxy_hosts_
) {
863 new FrameMsg_DidStartLoading(pair
.second
->GetRoutingID()));
867 void RenderFrameHostManager::OnDidStopLoading() {
868 for (const auto& pair
: proxy_hosts_
) {
869 pair
.second
->Send(new FrameMsg_DidStopLoading(pair
.second
->GetRoutingID()));
873 void RenderFrameHostManager::OnDidUpdateName(const std::string
& name
) {
874 // The window.name message may be sent outside of --site-per-process when
875 // report_frame_name_changes renderer preference is set (used by
876 // WebView). Don't send the update to proxies in those cases.
877 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
878 switches::kSitePerProcess
))
881 for (const auto& pair
: proxy_hosts_
) {
883 new FrameMsg_DidUpdateName(pair
.second
->GetRoutingID(), name
));
887 void RenderFrameHostManager::Observe(
889 const NotificationSource
& source
,
890 const NotificationDetails
& details
) {
892 case NOTIFICATION_RENDERER_PROCESS_CLOSED
:
893 case NOTIFICATION_RENDERER_PROCESS_CLOSING
:
894 RendererProcessClosing(
895 Source
<RenderProcessHost
>(source
).ptr());
903 RenderFrameHostManager::SiteInstanceDescriptor::SiteInstanceDescriptor(
904 BrowserContext
* browser_context
,
906 bool related_to_current
)
907 : existing_site_instance(nullptr),
908 new_is_related_to_current(related_to_current
) {
909 new_site_url
= SiteInstance::GetSiteForURL(browser_context
, dest_url
);
913 bool RenderFrameHostManager::ClearProxiesInSiteInstance(
914 int32 site_instance_id
,
915 FrameTreeNode
* node
) {
916 RenderFrameProxyHostMap::iterator iter
=
917 node
->render_manager()->proxy_hosts_
.find(site_instance_id
);
918 if (iter
!= node
->render_manager()->proxy_hosts_
.end()) {
919 RenderFrameProxyHost
* proxy
= iter
->second
;
920 // Delete the proxy. If it is for a main frame (and thus the RFH is stored
921 // in the proxy) and it was still pending swap out, move the RFH to the
922 // pending deletion list first.
923 if (node
->IsMainFrame() &&
924 proxy
->render_frame_host()->rfh_state() ==
925 RenderFrameHostImpl::STATE_PENDING_SWAP_OUT
) {
926 scoped_ptr
<RenderFrameHostImpl
> swapped_out_rfh
=
927 proxy
->PassFrameHostOwnership();
928 node
->render_manager()->MoveToPendingDeleteHosts(swapped_out_rfh
.Pass());
931 node
->render_manager()->proxy_hosts_
.erase(site_instance_id
);
938 bool RenderFrameHostManager::ResetProxiesInSiteInstance(int32 site_instance_id
,
939 FrameTreeNode
* node
) {
940 RenderFrameProxyHostMap::iterator iter
=
941 node
->render_manager()->proxy_hosts_
.find(site_instance_id
);
942 if (iter
!= node
->render_manager()->proxy_hosts_
.end())
943 iter
->second
->set_render_frame_proxy_created(false);
948 bool RenderFrameHostManager::ShouldTransitionCrossSite() {
949 // True for --site-per-process, which overrides both kSingleProcess and
951 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
952 switches::kSitePerProcess
))
955 // False in the single-process mode, as it makes RVHs to accumulate
956 // in swapped_out_hosts_.
957 // True if we are using process-per-site-instance (default) or
958 // process-per-site (kProcessPerSite).
959 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
960 switches::kSingleProcess
) &&
961 !base::CommandLine::ForCurrentProcess()->HasSwitch(
962 switches::kProcessPerTab
);
965 bool RenderFrameHostManager::ShouldSwapBrowsingInstancesForNavigation(
966 const GURL
& current_effective_url
,
967 bool current_is_view_source_mode
,
968 SiteInstance
* new_site_instance
,
969 const GURL
& new_effective_url
,
970 bool new_is_view_source_mode
) const {
971 // If new_entry already has a SiteInstance, assume it is correct. We only
972 // need to force a swap if it is in a different BrowsingInstance.
973 if (new_site_instance
) {
974 return !new_site_instance
->IsRelatedSiteInstance(
975 render_frame_host_
->GetSiteInstance());
978 // Check for reasons to swap processes even if we are in a process model that
979 // doesn't usually swap (e.g., process-per-tab). Any time we return true,
980 // the new_entry will be rendered in a new SiteInstance AND BrowsingInstance.
981 BrowserContext
* browser_context
=
982 delegate_
->GetControllerForRenderManager().GetBrowserContext();
984 // Don't force a new BrowsingInstance for debug URLs that are handled in the
985 // renderer process, like javascript: or chrome://crash.
986 if (IsRendererDebugURL(new_effective_url
))
989 // For security, we should transition between processes when one is a Web UI
990 // page and one isn't.
991 if (ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
992 render_frame_host_
->GetProcess()->GetID()) ||
993 WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
994 browser_context
, current_effective_url
)) {
995 // If so, force a swap if destination is not an acceptable URL for Web UI.
996 // Here, data URLs are never allowed.
997 if (!WebUIControllerFactoryRegistry::GetInstance()->IsURLAcceptableForWebUI(
998 browser_context
, new_effective_url
)) {
1002 // Force a swap if it's a Web UI URL.
1003 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
1004 browser_context
, new_effective_url
)) {
1009 // Check with the content client as well. Important to pass
1010 // current_effective_url here, which uses the SiteInstance's site if there is
1011 // no current_entry.
1012 if (GetContentClient()->browser()->ShouldSwapBrowsingInstancesForNavigation(
1013 render_frame_host_
->GetSiteInstance(),
1014 current_effective_url
, new_effective_url
)) {
1018 // We can't switch a RenderView between view source and non-view source mode
1019 // without screwing up the session history sometimes (when navigating between
1020 // "view-source:http://foo.com/" and "http://foo.com/", Blink doesn't treat
1021 // it as a new navigation). So require a BrowsingInstance switch.
1022 if (current_is_view_source_mode
!= new_is_view_source_mode
)
1028 bool RenderFrameHostManager::ShouldReuseWebUI(
1029 const NavigationEntry
* current_entry
,
1030 const GURL
& new_url
) const {
1031 NavigationControllerImpl
& controller
=
1032 delegate_
->GetControllerForRenderManager();
1033 return current_entry
&& web_ui_
.get() &&
1034 (WebUIControllerFactoryRegistry::GetInstance()->GetWebUIType(
1035 controller
.GetBrowserContext(), current_entry
->GetURL()) ==
1036 WebUIControllerFactoryRegistry::GetInstance()->GetWebUIType(
1037 controller
.GetBrowserContext(), new_url
));
1040 SiteInstance
* RenderFrameHostManager::GetSiteInstanceForNavigation(
1041 const GURL
& dest_url
,
1042 SiteInstance
* source_instance
,
1043 SiteInstance
* dest_instance
,
1044 SiteInstance
* candidate_instance
,
1045 ui::PageTransition transition
,
1046 bool dest_is_restore
,
1047 bool dest_is_view_source_mode
) {
1048 SiteInstance
* current_instance
= render_frame_host_
->GetSiteInstance();
1050 // We do not currently swap processes for navigations in webview tag guests.
1051 if (current_instance
->GetSiteURL().SchemeIs(kGuestScheme
))
1052 return current_instance
;
1054 // Determine if we need a new BrowsingInstance for this entry. If true, this
1055 // implies that it will get a new SiteInstance (and likely process), and that
1056 // other tabs in the current BrowsingInstance will be unable to script it.
1057 // This is used for cases that require a process swap even in the
1058 // process-per-tab model, such as WebUI pages.
1059 // TODO(clamy): Remove the dependency on the current entry.
1060 const NavigationEntry
* current_entry
=
1061 delegate_
->GetLastCommittedNavigationEntryForRenderManager();
1062 BrowserContext
* browser_context
=
1063 delegate_
->GetControllerForRenderManager().GetBrowserContext();
1064 const GURL
& current_effective_url
= current_entry
?
1065 SiteInstanceImpl::GetEffectiveURL(browser_context
,
1066 current_entry
->GetURL()) :
1067 render_frame_host_
->GetSiteInstance()->GetSiteURL();
1068 bool current_is_view_source_mode
= current_entry
?
1069 current_entry
->IsViewSourceMode() : dest_is_view_source_mode
;
1070 bool force_swap
= ShouldSwapBrowsingInstancesForNavigation(
1071 current_effective_url
,
1072 current_is_view_source_mode
,
1074 SiteInstanceImpl::GetEffectiveURL(browser_context
, dest_url
),
1075 dest_is_view_source_mode
);
1076 SiteInstanceDescriptor new_instance_descriptor
=
1077 SiteInstanceDescriptor(current_instance
);
1078 if (ShouldTransitionCrossSite() || force_swap
) {
1079 new_instance_descriptor
= DetermineSiteInstanceForURL(
1080 dest_url
, source_instance
, current_instance
, dest_instance
, transition
,
1081 dest_is_restore
, dest_is_view_source_mode
, force_swap
);
1084 SiteInstance
* new_instance
=
1085 ConvertToSiteInstance(new_instance_descriptor
, candidate_instance
);
1087 // If |force_swap| is true, we must use a different SiteInstance than the
1088 // current one. If we didn't, we would have two RenderFrameHosts in the same
1089 // SiteInstance and the same frame, resulting in page_id conflicts for their
1090 // NavigationEntries.
1092 CHECK_NE(new_instance
, current_instance
);
1093 return new_instance
;
1096 RenderFrameHostManager::SiteInstanceDescriptor
1097 RenderFrameHostManager::DetermineSiteInstanceForURL(
1098 const GURL
& dest_url
,
1099 SiteInstance
* source_instance
,
1100 SiteInstance
* current_instance
,
1101 SiteInstance
* dest_instance
,
1102 ui::PageTransition transition
,
1103 bool dest_is_restore
,
1104 bool dest_is_view_source_mode
,
1105 bool force_browsing_instance_swap
) {
1106 SiteInstanceImpl
* current_instance_impl
=
1107 static_cast<SiteInstanceImpl
*>(current_instance
);
1108 NavigationControllerImpl
& controller
=
1109 delegate_
->GetControllerForRenderManager();
1110 BrowserContext
* browser_context
= controller
.GetBrowserContext();
1112 // If the entry has an instance already we should use it.
1113 if (dest_instance
) {
1114 // If we are forcing a swap, this should be in a different BrowsingInstance.
1115 if (force_browsing_instance_swap
) {
1116 CHECK(!dest_instance
->IsRelatedSiteInstance(
1117 render_frame_host_
->GetSiteInstance()));
1119 return SiteInstanceDescriptor(dest_instance
);
1122 // If a swap is required, we need to force the SiteInstance AND
1123 // BrowsingInstance to be different ones, using CreateForURL.
1124 if (force_browsing_instance_swap
)
1125 return SiteInstanceDescriptor(browser_context
, dest_url
, false);
1127 // (UGLY) HEURISTIC, process-per-site only:
1129 // If this navigation is generated, then it probably corresponds to a search
1130 // query. Given that search results typically lead to users navigating to
1131 // other sites, we don't really want to use the search engine hostname to
1132 // determine the site instance for this navigation.
1134 // NOTE: This can be removed once we have a way to transition between
1135 // RenderViews in response to a link click.
1137 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1138 switches::kProcessPerSite
) &&
1139 ui::PageTransitionCoreTypeIs(transition
, ui::PAGE_TRANSITION_GENERATED
)) {
1140 return SiteInstanceDescriptor(current_instance_impl
);
1143 // If we haven't used our SiteInstance (and thus RVH) yet, then we can use it
1144 // for this entry. We won't commit the SiteInstance to this site until the
1145 // navigation commits (in DidNavigate), unless the navigation entry was
1146 // restored or it's a Web UI as described below.
1147 if (!current_instance_impl
->HasSite()) {
1148 // If we've already created a SiteInstance for our destination, we don't
1149 // want to use this unused SiteInstance; use the existing one. (We don't
1150 // do this check if the current_instance has a site, because for now, we
1151 // want to compare against the current URL and not the SiteInstance's site.
1152 // In this case, there is no current URL, so comparing against the site is
1153 // ok. See additional comments below.)
1155 // Also, if the URL should use process-per-site mode and there is an
1156 // existing process for the site, we should use it. We can call
1157 // GetRelatedSiteInstance() for this, which will eagerly set the site and
1158 // thus use the correct process.
1159 bool use_process_per_site
=
1160 RenderProcessHost::ShouldUseProcessPerSite(browser_context
, dest_url
) &&
1161 RenderProcessHostImpl::GetProcessHostForSite(browser_context
, dest_url
);
1162 if (current_instance_impl
->HasRelatedSiteInstance(dest_url
) ||
1163 use_process_per_site
) {
1164 return SiteInstanceDescriptor(browser_context
, dest_url
, true);
1167 // For extensions, Web UI URLs (such as the new tab page), and apps we do
1168 // not want to use the |current_instance_impl| if it has no site, since it
1169 // will have a RenderProcessHost of PRIV_NORMAL. Create a new SiteInstance
1170 // for this URL instead (with the correct process type).
1171 if (current_instance_impl
->HasWrongProcessForURL(dest_url
))
1172 return SiteInstanceDescriptor(browser_context
, dest_url
, true);
1174 // View-source URLs must use a new SiteInstance and BrowsingInstance.
1175 // TODO(nasko): This is the same condition as later in the function. This
1176 // should be taken into account when refactoring this method as part of
1177 // http://crbug.com/123007.
1178 if (dest_is_view_source_mode
)
1179 return SiteInstanceDescriptor(browser_context
, dest_url
, false);
1181 // If we are navigating from a blank SiteInstance to a WebUI, make sure we
1182 // create a new SiteInstance.
1183 if (WebUIControllerFactoryRegistry::GetInstance()->UseWebUIForURL(
1184 browser_context
, dest_url
)) {
1185 return SiteInstanceDescriptor(browser_context
, dest_url
, false);
1188 // Normally the "site" on the SiteInstance is set lazily when the load
1189 // actually commits. This is to support better process sharing in case
1190 // the site redirects to some other site: we want to use the destination
1191 // site in the site instance.
1193 // In the case of session restore, as it loads all the pages immediately
1194 // we need to set the site first, otherwise after a restore none of the
1195 // pages would share renderers in process-per-site.
1197 // The embedder can request some urls never to be assigned to SiteInstance
1198 // through the ShouldAssignSiteForURL() content client method, so that
1199 // renderers created for particular chrome urls (e.g. the chrome-native://
1200 // scheme) can be reused for subsequent navigations in the same WebContents.
1201 // See http://crbug.com/386542.
1202 if (dest_is_restore
&&
1203 GetContentClient()->browser()->ShouldAssignSiteForURL(dest_url
)) {
1204 current_instance_impl
->SetSite(dest_url
);
1207 return SiteInstanceDescriptor(current_instance_impl
);
1210 // Otherwise, only create a new SiteInstance for a cross-site navigation.
1212 // TODO(creis): Once we intercept links and script-based navigations, we
1213 // will be able to enforce that all entries in a SiteInstance actually have
1214 // the same site, and it will be safe to compare the URL against the
1215 // SiteInstance's site, as follows:
1216 // const GURL& current_url = current_instance_impl->site();
1217 // For now, though, we're in a hybrid model where you only switch
1218 // SiteInstances if you type in a cross-site URL. This means we have to
1219 // compare the entry's URL to the last committed entry's URL.
1220 NavigationEntry
* current_entry
= controller
.GetLastCommittedEntry();
1221 if (interstitial_page_
) {
1222 // The interstitial is currently the last committed entry, but we want to
1223 // compare against the last non-interstitial entry.
1224 current_entry
= controller
.GetEntryAtOffset(-1);
1227 // View-source URLs must use a new SiteInstance and BrowsingInstance.
1228 // We don't need a swap when going from view-source to a debug URL like
1229 // chrome://crash, however.
1230 // TODO(creis): Refactor this method so this duplicated code isn't needed.
1231 // See http://crbug.com/123007.
1232 if (current_entry
&&
1233 current_entry
->IsViewSourceMode() != dest_is_view_source_mode
&&
1234 !IsRendererDebugURL(dest_url
)) {
1235 return SiteInstanceDescriptor(browser_context
, dest_url
, false);
1238 // Use the source SiteInstance in case of data URLs or about:blank pages,
1239 // because the content is then controlled and/or scriptable by the source
1241 GURL
about_blank(url::kAboutBlankURL
);
1242 if (source_instance
&&
1243 (dest_url
== about_blank
|| dest_url
.scheme() == url::kDataScheme
)) {
1244 return SiteInstanceDescriptor(source_instance
);
1247 // Use the current SiteInstance for same site navigations, as long as the
1248 // process type is correct. (The URL may have been installed as an app since
1249 // the last time we visited it.)
1250 const GURL
& current_url
=
1251 GetCurrentURLForSiteInstance(current_instance_impl
, current_entry
);
1252 if (SiteInstance::IsSameWebSite(browser_context
, current_url
, dest_url
) &&
1253 !current_instance_impl
->HasWrongProcessForURL(dest_url
)) {
1254 return SiteInstanceDescriptor(current_instance_impl
);
1257 // Start the new renderer in a new SiteInstance, but in the current
1258 // BrowsingInstance. It is important to immediately give this new
1259 // SiteInstance to a RenderViewHost (if it is different than our current
1260 // SiteInstance), so that it is ref counted. This will happen in
1261 // CreateRenderView.
1262 return SiteInstanceDescriptor(browser_context
, dest_url
, true);
1265 SiteInstance
* RenderFrameHostManager::ConvertToSiteInstance(
1266 const SiteInstanceDescriptor
& descriptor
,
1267 SiteInstance
* candidate_instance
) {
1268 SiteInstance
* current_instance
= render_frame_host_
->GetSiteInstance();
1270 // Note: If the |candidate_instance| matches the descriptor, it will already
1271 // be set to |descriptor.existing_site_instance|.
1272 if (descriptor
.existing_site_instance
)
1273 return descriptor
.existing_site_instance
;
1275 // Note: If the |candidate_instance| matches the descriptor,
1276 // GetRelatedSiteInstance will return it.
1277 if (descriptor
.new_is_related_to_current
)
1278 return current_instance
->GetRelatedSiteInstance(descriptor
.new_site_url
);
1280 // At this point we know an unrelated site instance must be returned. First
1281 // check if the candidate matches.
1282 if (candidate_instance
&&
1283 !current_instance
->IsRelatedSiteInstance(candidate_instance
) &&
1284 candidate_instance
->GetSiteURL() == descriptor
.new_site_url
) {
1285 return candidate_instance
;
1288 // Otherwise return a newly created one.
1289 return SiteInstance::CreateForURL(
1290 delegate_
->GetControllerForRenderManager().GetBrowserContext(),
1291 descriptor
.new_site_url
);
1294 const GURL
& RenderFrameHostManager::GetCurrentURLForSiteInstance(
1295 SiteInstance
* current_instance
, NavigationEntry
* current_entry
) {
1296 // If this is a subframe that is potentially out of process from its parent,
1297 // don't consider using current_entry's url for SiteInstance selection, since
1298 // current_entry's url is for the main frame and may be in a different site
1300 // TODO(creis): Remove this when we can check the FrameNavigationEntry's url.
1301 // See http://crbug.com/369654
1302 if (!frame_tree_node_
->IsMainFrame() &&
1303 base::CommandLine::ForCurrentProcess()->HasSwitch(
1304 switches::kSitePerProcess
))
1305 return frame_tree_node_
->current_url();
1307 // If there is no last non-interstitial entry (and current_instance already
1308 // has a site), then we must have been opened from another tab. We want
1309 // to compare against the URL of the page that opened us, but we can't
1310 // get to it directly. The best we can do is check against the site of
1311 // the SiteInstance. This will be correct when we intercept links and
1312 // script-based navigations, but for now, it could place some pages in a
1313 // new process unnecessarily. We should only hit this case if a page tries
1314 // to open a new tab to an interstitial-inducing URL, and then navigates
1315 // the page to a different same-site URL. (This seems very unlikely in
1318 return current_entry
->GetURL();
1319 return current_instance
->GetSiteURL();
1322 void RenderFrameHostManager::CreatePendingRenderFrameHost(
1323 SiteInstance
* old_instance
,
1324 SiteInstance
* new_instance
,
1325 bool is_main_frame
) {
1326 int create_render_frame_flags
= 0;
1328 create_render_frame_flags
|= CREATE_RF_FOR_MAIN_FRAME_NAVIGATION
;
1330 if (delegate_
->IsHidden())
1331 create_render_frame_flags
|= CREATE_RF_HIDDEN
;
1333 int opener_route_id
= CreateOpenerRenderViewsIfNeeded(
1334 old_instance
, new_instance
, &create_render_frame_flags
);
1336 if (pending_render_frame_host_
)
1339 // Create a non-swapped-out RFH with the given opener.
1340 pending_render_frame_host_
=
1341 CreateRenderFrame(new_instance
, pending_web_ui(), opener_route_id
,
1342 create_render_frame_flags
, nullptr);
1345 int RenderFrameHostManager::CreateOpenerRenderViewsIfNeeded(
1346 SiteInstance
* old_instance
,
1347 SiteInstance
* new_instance
,
1348 int* create_render_frame_flags
) {
1349 int opener_route_id
= MSG_ROUTING_NONE
;
1350 // Only create opener proxies if they are in the same BrowsingInstance.
1351 if (new_instance
->IsRelatedSiteInstance(old_instance
)) {
1353 delegate_
->CreateOpenerRenderViewsForRenderManager(new_instance
);
1355 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1356 switches::kSitePerProcess
)) {
1357 // Ensure that the frame tree has RenderFrameProxyHosts for the new
1358 // SiteInstance in all nodes except the current one. We do this for all
1359 // frames in the tree, whether they are in the same BrowsingInstance or not.
1360 // We will still check whether two frames are in the same BrowsingInstance
1361 // before we allow them to interact (e.g., postMessage).
1362 frame_tree_node_
->frame_tree()->CreateProxiesForSiteInstance(
1363 frame_tree_node_
, new_instance
);
1364 // RenderFrames in different processes from their parent RenderFrames
1365 // in the frame tree require RenderWidgets for rendering and processing
1367 if (frame_tree_node_
->parent() &&
1368 frame_tree_node_
->parent()->current_frame_host()->GetSiteInstance() !=
1370 *create_render_frame_flags
|= CREATE_RF_NEEDS_RENDER_WIDGET_HOST
;
1372 return opener_route_id
;
1375 scoped_ptr
<RenderFrameHostImpl
> RenderFrameHostManager::CreateRenderFrameHost(
1376 SiteInstance
* site_instance
,
1377 int view_routing_id
,
1378 int frame_routing_id
,
1380 if (frame_routing_id
== MSG_ROUTING_NONE
)
1381 frame_routing_id
= site_instance
->GetProcess()->GetNextRoutingID();
1383 bool swapped_out
= !!(flags
& CREATE_RF_SWAPPED_OUT
);
1384 bool hidden
= !!(flags
& CREATE_RF_HIDDEN
);
1386 // Create a RVH for main frames, or find the existing one for subframes.
1387 FrameTree
* frame_tree
= frame_tree_node_
->frame_tree();
1388 RenderViewHostImpl
* render_view_host
= nullptr;
1389 if (frame_tree_node_
->IsMainFrame()) {
1390 render_view_host
= frame_tree
->CreateRenderViewHost(
1391 site_instance
, view_routing_id
, frame_routing_id
, swapped_out
, hidden
);
1393 render_view_host
= frame_tree
->GetRenderViewHost(site_instance
);
1395 CHECK(render_view_host
);
1398 // TODO(creis): Pass hidden to RFH.
1399 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
= make_scoped_ptr(
1400 RenderFrameHostFactory::Create(
1401 site_instance
, render_view_host
, render_frame_delegate_
,
1402 render_widget_delegate_
, frame_tree
, frame_tree_node_
,
1403 frame_routing_id
, flags
).release());
1404 return render_frame_host
.Pass();
1408 bool RenderFrameHostManager::CreateSpeculativeRenderFrameHost(
1410 SiteInstance
* old_instance
,
1411 SiteInstance
* new_instance
,
1413 CHECK(new_instance
);
1414 CHECK_NE(old_instance
, new_instance
);
1415 CHECK(!should_reuse_web_ui_
);
1417 // Note: |speculative_web_ui_| must be initialized before starting the
1418 // |speculative_render_frame_host_| creation steps otherwise the WebUI
1419 // won't be properly initialized.
1420 speculative_web_ui_
= CreateWebUI(url
, bindings
);
1422 int create_render_frame_flags
= 0;
1423 int opener_route_id
=
1424 CreateOpenerRenderViewsIfNeeded(old_instance
, new_instance
,
1425 &create_render_frame_flags
);
1427 if (frame_tree_node_
->IsMainFrame())
1428 create_render_frame_flags
|= CREATE_RF_FOR_MAIN_FRAME_NAVIGATION
;
1429 if (delegate_
->IsHidden())
1430 create_render_frame_flags
|= CREATE_RF_HIDDEN
;
1431 speculative_render_frame_host_
=
1432 CreateRenderFrame(new_instance
, speculative_web_ui_
.get(),
1433 opener_route_id
, create_render_frame_flags
, nullptr);
1435 if (!speculative_render_frame_host_
) {
1436 speculative_web_ui_
.reset();
1442 scoped_ptr
<RenderFrameHostImpl
> RenderFrameHostManager::CreateRenderFrame(
1443 SiteInstance
* instance
,
1445 int opener_route_id
,
1447 int* view_routing_id_ptr
) {
1448 bool swapped_out
= !!(flags
& CREATE_RF_SWAPPED_OUT
);
1450 // Swapped out views should always be hidden.
1451 DCHECK(!swapped_out
|| (flags
& CREATE_RF_HIDDEN
));
1453 // TODO(nasko): Remove the following CHECK once cross-site navigation no
1454 // longer relies on swapped out RFH for the top-level frame.
1455 if (!frame_tree_node_
->IsMainFrame())
1456 CHECK(!swapped_out
);
1458 scoped_ptr
<RenderFrameHostImpl
> new_render_frame_host
;
1459 bool success
= true;
1460 if (view_routing_id_ptr
)
1461 *view_routing_id_ptr
= MSG_ROUTING_NONE
;
1463 // We are creating a pending, speculative or swapped out RFH here. We should
1464 // never create it in the same SiteInstance as our current RFH.
1465 CHECK_NE(render_frame_host_
->GetSiteInstance(), instance
);
1467 // Check if we've already created an RFH for this SiteInstance. If so, try
1468 // to re-use the existing one, which has already been initialized. We'll
1469 // remove it from the list of proxy hosts below if it will be active.
1470 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(instance
);
1471 if (proxy
&& proxy
->render_frame_host()) {
1472 if (view_routing_id_ptr
)
1473 *view_routing_id_ptr
= proxy
->GetRenderViewHost()->GetRoutingID();
1474 // Delete the existing RenderFrameProxyHost, but reuse the RenderFrameHost.
1475 // Prevent the process from exiting while we're trying to use it.
1477 new_render_frame_host
= proxy
->PassFrameHostOwnership();
1478 new_render_frame_host
->GetProcess()->AddPendingView();
1480 proxy_hosts_
.erase(instance
->GetId());
1483 // When a new render view is created by the renderer, the new WebContents
1484 // gets a RenderViewHost in the SiteInstance of its opener WebContents.
1485 // If not used in the first navigation, this RVH is swapped out and is not
1486 // granted bindings, so we may need to grant them when swapping it in.
1487 if (web_ui
&& !new_render_frame_host
->GetProcess()->IsIsolatedGuest()) {
1488 int required_bindings
= web_ui
->GetBindings();
1489 RenderViewHost
* render_view_host
=
1490 new_render_frame_host
->render_view_host();
1491 if ((render_view_host
->GetEnabledBindings() & required_bindings
) !=
1492 required_bindings
) {
1493 render_view_host
->AllowBindings(required_bindings
);
1498 // Create a new RenderFrameHost if we don't find an existing one.
1499 new_render_frame_host
= CreateRenderFrameHost(instance
, MSG_ROUTING_NONE
,
1500 MSG_ROUTING_NONE
, flags
);
1501 RenderViewHostImpl
* render_view_host
=
1502 new_render_frame_host
->render_view_host();
1503 int proxy_routing_id
= MSG_ROUTING_NONE
;
1505 // Prevent the process from exiting while we're trying to navigate in it.
1506 // Otherwise, if the new RFH is swapped out already, store it.
1508 new_render_frame_host
->GetProcess()->AddPendingView();
1510 proxy
= new RenderFrameProxyHost(
1511 new_render_frame_host
->GetSiteInstance(), frame_tree_node_
);
1512 proxy_hosts_
[instance
->GetId()] = proxy
;
1513 proxy_routing_id
= proxy
->GetRoutingID();
1514 proxy
->TakeFrameHostOwnership(new_render_frame_host
.Pass());
1518 InitRenderView(render_view_host
, opener_route_id
, proxy_routing_id
,
1519 !!(flags
& CREATE_RF_FOR_MAIN_FRAME_NAVIGATION
));
1521 if (frame_tree_node_
->IsMainFrame()) {
1522 // Don't show the main frame's view until we get a DidNavigate from it.
1523 // Only the RenderViewHost for the top-level RenderFrameHost has a
1524 // RenderWidgetHostView; RenderWidgetHosts for out-of-process iframes
1525 // will be created later and hidden.
1526 if (render_view_host
->GetView())
1527 render_view_host
->GetView()->Hide();
1528 } else if (!swapped_out
) {
1529 // Init the RFH, so a RenderFrame is created in the renderer.
1530 DCHECK(new_render_frame_host
.get());
1531 success
= InitRenderFrame(new_render_frame_host
.get());
1536 if (view_routing_id_ptr
)
1537 *view_routing_id_ptr
= render_view_host
->GetRoutingID();
1541 // Returns the new RFH if it isn't swapped out.
1542 if (success
&& !swapped_out
) {
1543 DCHECK(new_render_frame_host
->GetSiteInstance() == instance
);
1544 return new_render_frame_host
.Pass();
1549 int RenderFrameHostManager::CreateRenderFrameProxy(SiteInstance
* instance
) {
1550 // A RenderFrameProxyHost should never be created in the same SiteInstance as
1553 CHECK_NE(instance
, render_frame_host_
->GetSiteInstance());
1555 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(instance
);
1557 return proxy
->GetRoutingID();
1559 proxy
= new RenderFrameProxyHost(instance
, frame_tree_node_
);
1560 proxy_hosts_
[instance
->GetId()] = proxy
;
1561 proxy
->InitRenderFrameProxy();
1562 return proxy
->GetRoutingID();
1565 void RenderFrameHostManager::CreateProxiesForChildFrame(FrameTreeNode
* child
) {
1566 for (const auto& pair
: proxy_hosts_
) {
1567 child
->render_manager()->CreateRenderFrameProxy(
1568 pair
.second
->GetSiteInstance());
1572 void RenderFrameHostManager::EnsureRenderViewInitialized(
1573 FrameTreeNode
* source
,
1574 RenderViewHostImpl
* render_view_host
,
1575 SiteInstance
* instance
) {
1576 DCHECK(frame_tree_node_
->IsMainFrame());
1578 if (render_view_host
->IsRenderViewLive())
1581 // Recreate the opener chain.
1582 int opener_route_id
=
1583 delegate_
->CreateOpenerRenderViewsForRenderManager(instance
);
1584 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(instance
);
1585 InitRenderView(render_view_host
, opener_route_id
, proxy
->GetRoutingID(),
1586 source
->IsMainFrame());
1589 bool RenderFrameHostManager::InitRenderView(
1590 RenderViewHostImpl
* render_view_host
,
1591 int opener_route_id
,
1592 int proxy_routing_id
,
1593 bool for_main_frame_navigation
) {
1594 // We may have initialized this RenderViewHost for another RenderFrameHost.
1595 if (render_view_host
->IsRenderViewLive())
1598 // If the ongoing navigation is to a WebUI and the RenderView is not in a
1599 // guest process, tell the RenderViewHost about any bindings it will need
1601 WebUIImpl
* dest_web_ui
= nullptr;
1602 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1603 switches::kEnableBrowserSideNavigation
)) {
1605 should_reuse_web_ui_
? web_ui_
.get() : speculative_web_ui_
.get();
1607 dest_web_ui
= pending_web_ui();
1609 if (dest_web_ui
&& !render_view_host
->GetProcess()->IsIsolatedGuest()) {
1610 render_view_host
->AllowBindings(dest_web_ui
->GetBindings());
1612 // Ensure that we don't create an unprivileged RenderView in a WebUI-enabled
1613 // process unless it's swapped out.
1614 if (render_view_host
->is_active()) {
1615 CHECK(!ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
1616 render_view_host
->GetProcess()->GetID()));
1620 return delegate_
->CreateRenderViewForRenderManager(render_view_host
,
1623 for_main_frame_navigation
);
1626 bool RenderFrameHostManager::InitRenderFrame(
1627 RenderFrameHostImpl
* render_frame_host
) {
1628 if (render_frame_host
->IsRenderFrameLive())
1631 int parent_routing_id
= MSG_ROUTING_NONE
;
1632 int proxy_routing_id
= MSG_ROUTING_NONE
;
1633 if (frame_tree_node_
->parent()) {
1634 parent_routing_id
= frame_tree_node_
->parent()->render_manager()->
1635 GetRoutingIdForSiteInstance(render_frame_host
->GetSiteInstance());
1636 CHECK_NE(parent_routing_id
, MSG_ROUTING_NONE
);
1638 // Check whether there is an existing proxy for this frame in this
1639 // SiteInstance. If there is, the new RenderFrame needs to be able to find
1640 // the proxy it is replacing, so that it can fully initialize itself.
1641 // NOTE: This is the only time that a RenderFrameProxyHost can be in the same
1642 // SiteInstance as its RenderFrameHost. This is only the case until the
1643 // RenderFrameHost commits, at which point it will replace and delete the
1644 // RenderFrameProxyHost.
1645 RenderFrameProxyHost
* existing_proxy
=
1646 GetRenderFrameProxyHost(render_frame_host
->GetSiteInstance());
1647 if (existing_proxy
) {
1648 proxy_routing_id
= existing_proxy
->GetRoutingID();
1649 CHECK_NE(proxy_routing_id
, MSG_ROUTING_NONE
);
1650 if (!existing_proxy
->is_render_frame_proxy_live())
1651 existing_proxy
->InitRenderFrameProxy();
1653 return delegate_
->CreateRenderFrameForRenderManager(render_frame_host
,
1658 int RenderFrameHostManager::GetRoutingIdForSiteInstance(
1659 SiteInstance
* site_instance
) {
1660 if (render_frame_host_
->GetSiteInstance() == site_instance
)
1661 return render_frame_host_
->GetRoutingID();
1663 RenderFrameProxyHostMap::iterator iter
=
1664 proxy_hosts_
.find(site_instance
->GetId());
1665 if (iter
!= proxy_hosts_
.end())
1666 return iter
->second
->GetRoutingID();
1668 return MSG_ROUTING_NONE
;
1671 void RenderFrameHostManager::CommitPending() {
1672 TRACE_EVENT1("navigation", "RenderFrameHostManager::CommitPending",
1673 "FrameTreeNode id", frame_tree_node_
->frame_tree_node_id());
1674 bool browser_side_navigation
=
1675 base::CommandLine::ForCurrentProcess()->HasSwitch(
1676 switches::kEnableBrowserSideNavigation
);
1677 // First check whether we're going to want to focus the location bar after
1678 // this commit. We do this now because the navigation hasn't formally
1679 // committed yet, so if we've already cleared |pending_web_ui_| the call chain
1680 // this triggers won't be able to figure out what's going on.
1681 bool will_focus_location_bar
= delegate_
->FocusLocationBarByDefault();
1683 if (!browser_side_navigation
) {
1684 DCHECK(!speculative_web_ui_
);
1685 // Next commit the Web UI, if any. Either replace |web_ui_| with
1686 // |pending_web_ui_|, or clear |web_ui_| if there is no pending WebUI, or
1687 // leave |web_ui_| as is if reusing it.
1688 DCHECK(!(pending_web_ui_
.get() && pending_and_current_web_ui_
.get()));
1689 if (pending_web_ui_
) {
1690 web_ui_
.reset(pending_web_ui_
.release());
1691 } else if (!pending_and_current_web_ui_
.get()) {
1694 DCHECK_EQ(pending_and_current_web_ui_
.get(), web_ui_
.get());
1695 pending_and_current_web_ui_
.reset();
1699 if (!should_reuse_web_ui_
)
1700 web_ui_
.reset(speculative_web_ui_
.release());
1701 DCHECK(!speculative_web_ui_
);
1704 // It's possible for the pending_render_frame_host_ to be nullptr when we
1705 // aren't crossing process boundaries. If so, we just needed to handle the Web
1706 // UI committing above and we're done.
1707 if (!pending_render_frame_host_
&& !speculative_render_frame_host_
) {
1708 if (will_focus_location_bar
)
1709 delegate_
->SetFocusToLocationBar(false);
1713 // Remember if the page was focused so we can focus the new renderer in
1715 bool focus_render_view
= !will_focus_location_bar
&&
1716 render_frame_host_
->GetView() &&
1717 render_frame_host_
->GetView()->HasFocus();
1719 bool is_main_frame
= frame_tree_node_
->IsMainFrame();
1721 // Swap in the pending or speculative frame and make it active. Also ensure
1722 // the FrameTree stays in sync.
1723 scoped_ptr
<RenderFrameHostImpl
> old_render_frame_host
;
1724 if (!browser_side_navigation
) {
1725 DCHECK(!speculative_render_frame_host_
);
1726 old_render_frame_host
=
1727 SetRenderFrameHost(pending_render_frame_host_
.Pass());
1730 DCHECK(speculative_render_frame_host_
);
1731 old_render_frame_host
=
1732 SetRenderFrameHost(speculative_render_frame_host_
.Pass());
1734 cross_navigation_pending_
= false;
1737 render_frame_host_
->render_view_host()->AttachToFrameTree();
1739 // The process will no longer try to exit, so we can decrement the count.
1740 render_frame_host_
->GetProcess()->RemovePendingView();
1742 // Show the new view (or a sad tab) if necessary.
1743 bool new_rfh_has_view
= !!render_frame_host_
->GetView();
1744 if (!delegate_
->IsHidden() && new_rfh_has_view
) {
1745 // In most cases, we need to show the new view.
1746 render_frame_host_
->GetView()->Show();
1748 if (!new_rfh_has_view
) {
1749 // If the view is gone, then this RenderViewHost died while it was hidden.
1750 // We ignored the RenderProcessGone call at the time, so we should send it
1751 // now to make sure the sad tab shows up, etc.
1752 DCHECK(!render_frame_host_
->IsRenderFrameLive());
1753 DCHECK(!render_frame_host_
->render_view_host()->IsRenderViewLive());
1754 delegate_
->RenderProcessGoneFromRenderManager(
1755 render_frame_host_
->render_view_host());
1758 // For top-level frames, also hide the old RenderViewHost's view.
1759 // TODO(creis): As long as show/hide are on RVH, we don't want to hide on
1760 // subframe navigations or we will interfere with the top-level frame.
1761 if (is_main_frame
&& old_render_frame_host
->render_view_host()->GetView())
1762 old_render_frame_host
->render_view_host()->GetView()->Hide();
1764 // Make sure the size is up to date. (Fix for bug 1079768.)
1765 delegate_
->UpdateRenderViewSizeForRenderManager();
1767 if (will_focus_location_bar
) {
1768 delegate_
->SetFocusToLocationBar(false);
1769 } else if (focus_render_view
&& render_frame_host_
->GetView()) {
1770 render_frame_host_
->GetView()->Focus();
1773 // Notify that we've swapped RenderFrameHosts. We do this before shutting down
1774 // the RFH so that we can clean up RendererResources related to the RFH first.
1775 delegate_
->NotifySwappedFromRenderManager(
1776 old_render_frame_host
.get(), render_frame_host_
.get(), is_main_frame
);
1778 // Swap out the old frame now that the new one is visible.
1779 // This will swap it out and then put it on the proxy list (if there are other
1780 // active views in its SiteInstance) or schedule it for deletion when the swap
1781 // out ack arrives (or immediately if the process isn't live).
1782 // In the --site-per-process case, old subframe RHFs are not kept alive inside
1784 SwapOutOldFrame(old_render_frame_host
.Pass());
1786 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1787 switches::kSitePerProcess
) &&
1789 // Since the new RenderFrameHost is now committed, there must be no proxies
1790 // for its SiteInstance. Delete any existing ones.
1791 RenderFrameProxyHostMap::iterator iter
=
1792 proxy_hosts_
.find(render_frame_host_
->GetSiteInstance()->GetId());
1793 if (iter
!= proxy_hosts_
.end()) {
1794 delete iter
->second
;
1795 proxy_hosts_
.erase(iter
);
1798 // If this is a subframe, it should have a CrossProcessFrameConnector
1799 // created already. Use it to link the new RFH's view to the proxy that
1800 // belongs to the parent frame's SiteInstance. If this navigation causes
1801 // an out-of-process frame to return to the same process as its parent, the
1802 // proxy would have been removed from proxy_hosts_ above.
1803 // Note: We do this after swapping out the old RFH because that may create
1804 // the proxy we're looking for.
1805 RenderFrameProxyHost
* proxy_to_parent
= GetProxyToParent();
1806 if (proxy_to_parent
)
1807 proxy_to_parent
->SetChildRWHView(render_frame_host_
->GetView());
1810 // After all is done, there must never be a proxy in the list which has the
1811 // same SiteInstance as the current RenderFrameHost.
1812 CHECK(proxy_hosts_
.find(render_frame_host_
->GetSiteInstance()->GetId()) ==
1813 proxy_hosts_
.end());
1816 void RenderFrameHostManager::ShutdownProxiesIfLastActiveFrameInSiteInstance(
1817 RenderFrameHostImpl
* render_frame_host
) {
1818 if (!render_frame_host
)
1820 if (!RenderFrameHostImpl::IsRFHStateActive(render_frame_host
->rfh_state()))
1822 if (render_frame_host
->GetSiteInstance()->active_frame_count() > 1U)
1825 // After |render_frame_host| goes away, there will be no active frames left in
1826 // its SiteInstance, so we can delete all proxies created in that SiteInstance
1827 // on behalf of frames anywhere in the BrowsingInstance.
1828 int32 site_instance_id
= render_frame_host
->GetSiteInstance()->GetId();
1830 // First remove any proxies for this SiteInstance from our own list.
1831 ClearProxiesInSiteInstance(site_instance_id
, frame_tree_node_
);
1833 // Use the safe RenderWidgetHost iterator for now to find all RenderViewHosts
1834 // in the SiteInstance, then tell their respective FrameTrees to remove all
1835 // RenderFrameProxyHosts corresponding to them.
1836 // TODO(creis): Replace this with a RenderFrameHostIterator that protects
1837 // against use-after-frees if a later element is deleted before getting to it.
1838 scoped_ptr
<RenderWidgetHostIterator
> widgets(
1839 RenderWidgetHostImpl::GetAllRenderWidgetHosts());
1840 while (RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
1841 if (!widget
->IsRenderView())
1843 RenderViewHostImpl
* rvh
=
1844 static_cast<RenderViewHostImpl
*>(RenderViewHost::From(widget
));
1845 if (site_instance_id
== rvh
->GetSiteInstance()->GetId()) {
1846 // This deletes all RenderFrameHosts using the |rvh|, which then causes
1847 // |rvh| to Shutdown.
1848 FrameTree
* tree
= rvh
->GetDelegate()->GetFrameTree();
1849 tree
->ForEach(base::Bind(
1850 &RenderFrameHostManager::ClearProxiesInSiteInstance
,
1856 RenderFrameHostImpl
* RenderFrameHostManager::UpdateStateForNavigate(
1857 const GURL
& dest_url
,
1858 SiteInstance
* source_instance
,
1859 SiteInstance
* dest_instance
,
1860 ui::PageTransition transition
,
1861 bool dest_is_restore
,
1862 bool dest_is_view_source_mode
,
1863 const GlobalRequestID
& transferred_request_id
,
1865 // If we are currently navigating cross-process, we want to get back to normal
1866 // and then navigate as usual.
1867 if (cross_navigation_pending_
) {
1868 if (pending_render_frame_host_
)
1870 cross_navigation_pending_
= false;
1873 SiteInstance
* current_instance
= render_frame_host_
->GetSiteInstance();
1874 scoped_refptr
<SiteInstance
> new_instance
= GetSiteInstanceForNavigation(
1875 dest_url
, source_instance
, dest_instance
, nullptr, transition
,
1876 dest_is_restore
, dest_is_view_source_mode
);
1878 const NavigationEntry
* current_entry
=
1879 delegate_
->GetLastCommittedNavigationEntryForRenderManager();
1881 DCHECK(!cross_navigation_pending_
);
1883 if (new_instance
.get() != current_instance
) {
1884 TRACE_EVENT_INSTANT2(
1886 "RenderFrameHostManager::UpdateStateForNavigate:New SiteInstance",
1887 TRACE_EVENT_SCOPE_THREAD
,
1888 "current_instance id", current_instance
->GetId(),
1889 "new_instance id", new_instance
->GetId());
1891 // New SiteInstance: create a pending RFH to navigate.
1893 // This will possibly create (set to nullptr) a Web UI object for the
1894 // pending page. We'll use this later to give the page special access. This
1895 // must happen before the new renderer is created below so it will get
1896 // bindings. It must also happen after the above conditional call to
1897 // CancelPending(), otherwise CancelPending may clear the pending_web_ui_
1898 // and the page will not have its bindings set appropriately.
1899 SetPendingWebUI(dest_url
, bindings
);
1900 CreatePendingRenderFrameHost(current_instance
, new_instance
.get(),
1901 frame_tree_node_
->IsMainFrame());
1902 if (!pending_render_frame_host_
.get()) {
1906 // Check if our current RFH is live before we set up a transition.
1907 if (!render_frame_host_
->IsRenderFrameLive()) {
1908 if (!cross_navigation_pending_
) {
1909 // The current RFH is not live. There's no reason to sit around with a
1910 // sad tab or a newly created RFH while we wait for the pending RFH to
1911 // navigate. Just switch to the pending RFH now and go back to non
1912 // cross-navigating (Note that we don't care about on{before}unload
1913 // handlers if the current RFH isn't live.)
1915 return render_frame_host_
.get();
1918 return render_frame_host_
.get();
1921 // Otherwise, it's safe to treat this as a pending cross-site transition.
1923 // We now have a pending RFH.
1924 DCHECK(!cross_navigation_pending_
);
1925 cross_navigation_pending_
= true;
1927 // We need to wait until the beforeunload handler has run, unless we are
1928 // transferring an existing request (in which case it has already run).
1929 // Suspend the new render view (i.e., don't let it send the cross-site
1930 // Navigate message) until we hear back from the old renderer's
1931 // beforeunload handler. If the handler returns false, we'll have to
1932 // cancel the request.
1934 DCHECK(!pending_render_frame_host_
->are_navigations_suspended());
1935 bool is_transfer
= transferred_request_id
!= GlobalRequestID();
1937 // We don't need to stop the old renderer or run beforeunload/unload
1938 // handlers, because those have already been done.
1939 DCHECK(cross_site_transferring_request_
->request_id() ==
1940 transferred_request_id
);
1942 // Also make sure the old render view stops, in case a load is in
1943 // progress. (We don't want to do this for transfers, since it will
1944 // interrupt the transfer with an unexpected DidStopLoading.)
1945 render_frame_host_
->Send(new FrameMsg_Stop(
1946 render_frame_host_
->GetRoutingID()));
1947 pending_render_frame_host_
->SetNavigationsSuspended(true,
1949 // Unless we are transferring an existing request, we should now tell the
1950 // old render view to run its beforeunload handler, since it doesn't
1951 // otherwise know that the cross-site request is happening. This will
1952 // trigger a call to OnBeforeUnloadACK with the reply.
1953 render_frame_host_
->DispatchBeforeUnload(true);
1956 return pending_render_frame_host_
.get();
1959 // Otherwise the same SiteInstance can be used. Navigate render_frame_host_.
1961 // It's possible to swap out the current RFH and then decide to navigate in it
1962 // anyway (e.g., a cross-process navigation that redirects back to the
1963 // original site). In that case, we have a proxy for the current RFH but
1964 // haven't deleted it yet. The new navigation will swap it back in, so we can
1965 // delete the proxy.
1966 DeleteRenderFrameProxyHost(new_instance
.get());
1968 if (ShouldReuseWebUI(current_entry
, dest_url
)) {
1969 pending_web_ui_
.reset();
1970 pending_and_current_web_ui_
= web_ui_
->AsWeakPtr();
1972 SetPendingWebUI(dest_url
, bindings
);
1973 // Make sure the new RenderViewHost has the right bindings.
1974 if (pending_web_ui() &&
1975 !render_frame_host_
->GetProcess()->IsIsolatedGuest()) {
1976 render_frame_host_
->render_view_host()->AllowBindings(
1977 pending_web_ui()->GetBindings());
1981 if (pending_web_ui() && render_frame_host_
->IsRenderFrameLive()) {
1982 pending_web_ui()->GetController()->RenderViewReused(
1983 render_frame_host_
->render_view_host());
1986 // The renderer can exit view source mode when any error or cancellation
1987 // happen. We must overwrite to recover the mode.
1988 if (dest_is_view_source_mode
) {
1989 render_frame_host_
->render_view_host()->Send(
1990 new ViewMsg_EnableViewSourceMode(
1991 render_frame_host_
->render_view_host()->GetRoutingID()));
1994 return render_frame_host_
.get();
1997 void RenderFrameHostManager::CancelPending() {
1998 TRACE_EVENT1("navigation", "RenderFrameHostManager::CancelPending",
1999 "FrameTreeNode id", frame_tree_node_
->frame_tree_node_id());
2000 DiscardUnusedFrame(UnsetPendingRenderFrameHost());
2003 scoped_ptr
<RenderFrameHostImpl
>
2004 RenderFrameHostManager::UnsetPendingRenderFrameHost() {
2005 scoped_ptr
<RenderFrameHostImpl
> pending_render_frame_host
=
2006 pending_render_frame_host_
.Pass();
2008 RenderFrameDevToolsAgentHost::OnCancelPendingNavigation(
2009 pending_render_frame_host
.get(),
2010 render_frame_host_
.get());
2012 // We no longer need to prevent the process from exiting.
2013 pending_render_frame_host
->GetProcess()->RemovePendingView();
2015 pending_web_ui_
.reset();
2016 pending_and_current_web_ui_
.reset();
2018 return pending_render_frame_host
.Pass();
2021 scoped_ptr
<RenderFrameHostImpl
> RenderFrameHostManager::SetRenderFrameHost(
2022 scoped_ptr
<RenderFrameHostImpl
> render_frame_host
) {
2024 scoped_ptr
<RenderFrameHostImpl
> old_render_frame_host
=
2025 render_frame_host_
.Pass();
2026 render_frame_host_
= render_frame_host
.Pass();
2028 if (frame_tree_node_
->IsMainFrame()) {
2029 // Update the count of top-level frames using this SiteInstance. All
2030 // subframes are in the same BrowsingInstance as the main frame, so we only
2031 // count top-level ones. This makes the value easier for consumers to
2033 if (render_frame_host_
) {
2034 render_frame_host_
->GetSiteInstance()->
2035 IncrementRelatedActiveContentsCount();
2037 if (old_render_frame_host
) {
2038 old_render_frame_host
->GetSiteInstance()->
2039 DecrementRelatedActiveContentsCount();
2043 return old_render_frame_host
.Pass();
2046 bool RenderFrameHostManager::IsRVHOnSwappedOutList(
2047 RenderViewHostImpl
* rvh
) const {
2048 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(
2049 rvh
->GetSiteInstance());
2052 // If there is a proxy without RFH, it is for a subframe in the SiteInstance
2053 // of |rvh|. Subframes should be ignored in this case.
2054 if (!proxy
->render_frame_host())
2056 return IsOnSwappedOutList(proxy
->render_frame_host());
2059 bool RenderFrameHostManager::IsOnSwappedOutList(
2060 RenderFrameHostImpl
* rfh
) const {
2061 if (!rfh
->GetSiteInstance())
2064 RenderFrameProxyHostMap::const_iterator iter
= proxy_hosts_
.find(
2065 rfh
->GetSiteInstance()->GetId());
2066 if (iter
== proxy_hosts_
.end())
2069 return iter
->second
->render_frame_host() == rfh
;
2072 RenderViewHostImpl
* RenderFrameHostManager::GetSwappedOutRenderViewHost(
2073 SiteInstance
* instance
) const {
2074 RenderFrameProxyHost
* proxy
= GetRenderFrameProxyHost(instance
);
2076 return proxy
->GetRenderViewHost();
2080 RenderFrameProxyHost
* RenderFrameHostManager::GetRenderFrameProxyHost(
2081 SiteInstance
* instance
) const {
2082 RenderFrameProxyHostMap::const_iterator iter
=
2083 proxy_hosts_
.find(instance
->GetId());
2084 if (iter
!= proxy_hosts_
.end())
2085 return iter
->second
;
2090 void RenderFrameHostManager::DeleteRenderFrameProxyHost(
2091 SiteInstance
* instance
) {
2092 RenderFrameProxyHostMap::iterator iter
= proxy_hosts_
.find(instance
->GetId());
2093 if (iter
!= proxy_hosts_
.end()) {
2094 delete iter
->second
;
2095 proxy_hosts_
.erase(iter
);
2099 } // namespace content